|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +/* |
| 3 | + * Copyright © 2025 Intel Corporation |
| 4 | + */ |
| 5 | + |
| 6 | +#include "xe_bo.h" |
| 7 | +#include "xe_device.h" |
| 8 | +#include "xe_printk.h" |
| 9 | +#include "xe_sriov_packet.h" |
| 10 | +#include "xe_sriov_packet_types.h" |
| 11 | + |
| 12 | +static bool pkt_needs_bo(struct xe_sriov_packet *data) |
| 13 | +{ |
| 14 | + return data->hdr.type == XE_SRIOV_PACKET_TYPE_VRAM; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * xe_sriov_packet_alloc() - Allocate migration data packet |
| 19 | + * @xe: the &xe_device |
| 20 | + * |
| 21 | + * Only allocates the "outer" structure, without initializing the migration |
| 22 | + * data backing storage. |
| 23 | + * |
| 24 | + * Return: Pointer to &xe_sriov_packet on success, |
| 25 | + * NULL in case of error. |
| 26 | + */ |
| 27 | +struct xe_sriov_packet *xe_sriov_packet_alloc(struct xe_device *xe) |
| 28 | +{ |
| 29 | + struct xe_sriov_packet *data; |
| 30 | + |
| 31 | + data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 32 | + if (!data) |
| 33 | + return NULL; |
| 34 | + |
| 35 | + data->xe = xe; |
| 36 | + data->hdr_remaining = sizeof(data->hdr); |
| 37 | + |
| 38 | + return data; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * xe_sriov_packet_free() - Free migration data packet. |
| 43 | + * @data: the &xe_sriov_packet |
| 44 | + */ |
| 45 | +void xe_sriov_packet_free(struct xe_sriov_packet *data) |
| 46 | +{ |
| 47 | + if (IS_ERR_OR_NULL(data)) |
| 48 | + return; |
| 49 | + |
| 50 | + if (pkt_needs_bo(data)) |
| 51 | + xe_bo_unpin_map_no_vm(data->bo); |
| 52 | + else |
| 53 | + kvfree(data->buff); |
| 54 | + |
| 55 | + kfree(data); |
| 56 | +} |
| 57 | + |
| 58 | +static int pkt_init(struct xe_sriov_packet *data) |
| 59 | +{ |
| 60 | + struct xe_gt *gt = xe_device_get_gt(data->xe, data->hdr.gt_id); |
| 61 | + |
| 62 | + if (!gt) |
| 63 | + return -EINVAL; |
| 64 | + |
| 65 | + if (data->hdr.size == 0) |
| 66 | + return 0; |
| 67 | + |
| 68 | + if (pkt_needs_bo(data)) { |
| 69 | + struct xe_bo *bo; |
| 70 | + |
| 71 | + bo = xe_bo_create_pin_map_novm(data->xe, gt->tile, PAGE_ALIGN(data->hdr.size), |
| 72 | + ttm_bo_type_kernel, |
| 73 | + XE_BO_FLAG_SYSTEM | XE_BO_FLAG_PINNED, false); |
| 74 | + if (IS_ERR(bo)) |
| 75 | + return PTR_ERR(bo); |
| 76 | + |
| 77 | + data->bo = bo; |
| 78 | + data->vaddr = bo->vmap.vaddr; |
| 79 | + } else { |
| 80 | + void *buff = kvzalloc(data->hdr.size, GFP_KERNEL); |
| 81 | + |
| 82 | + if (!buff) |
| 83 | + return -ENOMEM; |
| 84 | + |
| 85 | + data->buff = buff; |
| 86 | + data->vaddr = buff; |
| 87 | + } |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +#define XE_SRIOV_PACKET_SUPPORTED_VERSION 1 |
| 93 | + |
| 94 | +/** |
| 95 | + * xe_sriov_packet_init() - Initialize migration packet header and backing storage. |
| 96 | + * @data: the &xe_sriov_packet |
| 97 | + * @tile_id: tile identifier |
| 98 | + * @gt_id: GT identifier |
| 99 | + * @type: &xe_sriov_packet_type |
| 100 | + * @offset: offset of data packet payload (within wider resource) |
| 101 | + * @size: size of data packet payload |
| 102 | + * |
| 103 | + * Return: 0 on success or a negative error code on failure. |
| 104 | + */ |
| 105 | +int xe_sriov_packet_init(struct xe_sriov_packet *data, u8 tile_id, u8 gt_id, |
| 106 | + enum xe_sriov_packet_type type, loff_t offset, size_t size) |
| 107 | +{ |
| 108 | + data->hdr.version = XE_SRIOV_PACKET_SUPPORTED_VERSION; |
| 109 | + data->hdr.type = type; |
| 110 | + data->hdr.tile_id = tile_id; |
| 111 | + data->hdr.gt_id = gt_id; |
| 112 | + data->hdr.offset = offset; |
| 113 | + data->hdr.size = size; |
| 114 | + data->remaining = size; |
| 115 | + |
| 116 | + return pkt_init(data); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * xe_sriov_packet_init_from_hdr() - Initialize migration packet backing storage based on header. |
| 121 | + * @data: the &xe_sriov_packet |
| 122 | + * |
| 123 | + * Header data is expected to be filled prior to calling this function. |
| 124 | + * |
| 125 | + * Return: 0 on success or a negative error code on failure. |
| 126 | + */ |
| 127 | +int xe_sriov_packet_init_from_hdr(struct xe_sriov_packet *data) |
| 128 | +{ |
| 129 | + xe_assert(data->xe, !data->hdr_remaining); |
| 130 | + |
| 131 | + if (data->hdr.version != XE_SRIOV_PACKET_SUPPORTED_VERSION) |
| 132 | + return -EINVAL; |
| 133 | + |
| 134 | + data->remaining = data->hdr.size; |
| 135 | + |
| 136 | + return pkt_init(data); |
| 137 | +} |
0 commit comments