|
5 | 5 |
|
6 | 6 | #include "xe_gsc.h"
|
7 | 7 |
|
| 8 | +#include <drm/drm_managed.h> |
| 9 | + |
| 10 | +#include "xe_bb.h" |
| 11 | +#include "xe_bo.h" |
8 | 12 | #include "xe_device.h"
|
| 13 | +#include "xe_exec_queue.h" |
9 | 14 | #include "xe_gt.h"
|
10 | 15 | #include "xe_gt_printk.h"
|
| 16 | +#include "xe_map.h" |
| 17 | +#include "xe_mmio.h" |
| 18 | +#include "xe_sched_job.h" |
11 | 19 | #include "xe_uc_fw.h"
|
| 20 | +#include "instructions/xe_gsc_commands.h" |
| 21 | +#include "regs/xe_gsc_regs.h" |
12 | 22 |
|
13 | 23 | static struct xe_gt *
|
14 | 24 | gsc_to_gt(struct xe_gsc *gsc)
|
15 | 25 | {
|
16 | 26 | return container_of(gsc, struct xe_gt, uc.gsc);
|
17 | 27 | }
|
18 | 28 |
|
| 29 | +static int memcpy_fw(struct xe_gsc *gsc) |
| 30 | +{ |
| 31 | + struct xe_gt *gt = gsc_to_gt(gsc); |
| 32 | + struct xe_device *xe = gt_to_xe(gt); |
| 33 | + u32 fw_size = gsc->fw.size; |
| 34 | + void *storage; |
| 35 | + |
| 36 | + /* |
| 37 | + * FIXME: xe_migrate_copy does not work with stolen mem yet, so we use |
| 38 | + * a memcpy for now. |
| 39 | + */ |
| 40 | + storage = kmalloc(fw_size, GFP_KERNEL); |
| 41 | + if (!storage) |
| 42 | + return -ENOMEM; |
| 43 | + |
| 44 | + xe_map_memcpy_from(xe, storage, &gsc->fw.bo->vmap, 0, fw_size); |
| 45 | + xe_map_memcpy_to(xe, &gsc->private->vmap, 0, storage, fw_size); |
| 46 | + xe_map_memset(xe, &gsc->private->vmap, fw_size, 0, gsc->private->size - fw_size); |
| 47 | + |
| 48 | + kfree(storage); |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +static int emit_gsc_upload(struct xe_gsc *gsc) |
| 54 | +{ |
| 55 | + struct xe_gt *gt = gsc_to_gt(gsc); |
| 56 | + u64 offset = xe_bo_ggtt_addr(gsc->private); |
| 57 | + struct xe_bb *bb; |
| 58 | + struct xe_sched_job *job; |
| 59 | + struct dma_fence *fence; |
| 60 | + long timeout; |
| 61 | + |
| 62 | + bb = xe_bb_new(gt, 4, false); |
| 63 | + if (IS_ERR(bb)) |
| 64 | + return PTR_ERR(bb); |
| 65 | + |
| 66 | + bb->cs[bb->len++] = GSC_FW_LOAD; |
| 67 | + bb->cs[bb->len++] = lower_32_bits(offset); |
| 68 | + bb->cs[bb->len++] = upper_32_bits(offset); |
| 69 | + bb->cs[bb->len++] = (gsc->private->size / SZ_4K) | GSC_FW_LOAD_LIMIT_VALID; |
| 70 | + |
| 71 | + job = xe_bb_create_job(gsc->q, bb); |
| 72 | + if (IS_ERR(job)) { |
| 73 | + xe_bb_free(bb, NULL); |
| 74 | + return PTR_ERR(job); |
| 75 | + } |
| 76 | + |
| 77 | + xe_sched_job_arm(job); |
| 78 | + fence = dma_fence_get(&job->drm.s_fence->finished); |
| 79 | + xe_sched_job_push(job); |
| 80 | + |
| 81 | + timeout = dma_fence_wait_timeout(fence, false, HZ); |
| 82 | + dma_fence_put(fence); |
| 83 | + xe_bb_free(bb, NULL); |
| 84 | + if (timeout < 0) |
| 85 | + return timeout; |
| 86 | + else if (!timeout) |
| 87 | + return -ETIME; |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +static int gsc_fw_is_loaded(struct xe_gt *gt) |
| 93 | +{ |
| 94 | + return xe_mmio_read32(gt, HECI_FWSTS1(MTL_GSC_HECI1_BASE)) & |
| 95 | + HECI1_FWSTS1_INIT_COMPLETE; |
| 96 | +} |
| 97 | + |
| 98 | +static int gsc_fw_wait(struct xe_gt *gt) |
| 99 | +{ |
| 100 | + /* |
| 101 | + * GSC load can take up to 250ms from the moment the instruction is |
| 102 | + * executed by the GSCCS. To account for possible submission delays or |
| 103 | + * other issues, we use a 500ms timeout in the wait here. |
| 104 | + */ |
| 105 | + return xe_mmio_wait32(gt, HECI_FWSTS1(MTL_GSC_HECI1_BASE), |
| 106 | + HECI1_FWSTS1_INIT_COMPLETE, |
| 107 | + HECI1_FWSTS1_INIT_COMPLETE, |
| 108 | + 500 * USEC_PER_MSEC, NULL, false); |
| 109 | +} |
| 110 | + |
| 111 | +static int gsc_upload(struct xe_gsc *gsc) |
| 112 | +{ |
| 113 | + struct xe_gt *gt = gsc_to_gt(gsc); |
| 114 | + struct xe_device *xe = gt_to_xe(gt); |
| 115 | + int err; |
| 116 | + |
| 117 | + /* we should only be here if the init step were successful */ |
| 118 | + xe_assert(xe, xe_uc_fw_is_loadable(&gsc->fw) && gsc->q); |
| 119 | + |
| 120 | + if (gsc_fw_is_loaded(gt)) { |
| 121 | + xe_gt_err(gt, "GSC already loaded at upload time\n"); |
| 122 | + return -EEXIST; |
| 123 | + } |
| 124 | + |
| 125 | + err = memcpy_fw(gsc); |
| 126 | + if (err) { |
| 127 | + xe_gt_err(gt, "Failed to memcpy GSC FW\n"); |
| 128 | + return err; |
| 129 | + } |
| 130 | + |
| 131 | + err = emit_gsc_upload(gsc); |
| 132 | + if (err) { |
| 133 | + xe_gt_err(gt, "Failed to emit GSC FW upload (%pe)\n", ERR_PTR(err)); |
| 134 | + return err; |
| 135 | + } |
| 136 | + |
| 137 | + err = gsc_fw_wait(gt); |
| 138 | + if (err) { |
| 139 | + xe_gt_err(gt, "Failed to wait for GSC load (%pe)\n", ERR_PTR(err)); |
| 140 | + return err; |
| 141 | + } |
| 142 | + |
| 143 | + xe_gt_dbg(gt, "GSC FW async load completed\n"); |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +static void gsc_work(struct work_struct *work) |
| 149 | +{ |
| 150 | + struct xe_gsc *gsc = container_of(work, typeof(*gsc), work); |
| 151 | + struct xe_gt *gt = gsc_to_gt(gsc); |
| 152 | + struct xe_device *xe = gt_to_xe(gt); |
| 153 | + int ret; |
| 154 | + |
| 155 | + xe_device_mem_access_get(xe); |
| 156 | + xe_force_wake_get(gt_to_fw(gt), XE_FW_GSC); |
| 157 | + |
| 158 | + ret = gsc_upload(gsc); |
| 159 | + if (ret && ret != -EEXIST) |
| 160 | + xe_uc_fw_change_status(&gsc->fw, XE_UC_FIRMWARE_LOAD_FAIL); |
| 161 | + else |
| 162 | + xe_uc_fw_change_status(&gsc->fw, XE_UC_FIRMWARE_TRANSFERRED); |
| 163 | + |
| 164 | + xe_force_wake_put(gt_to_fw(gt), XE_FW_GSC); |
| 165 | + xe_device_mem_access_put(xe); |
| 166 | +} |
| 167 | + |
19 | 168 | int xe_gsc_init(struct xe_gsc *gsc)
|
20 | 169 | {
|
21 | 170 | struct xe_gt *gt = gsc_to_gt(gsc);
|
22 | 171 | struct xe_tile *tile = gt_to_tile(gt);
|
23 | 172 | int ret;
|
24 | 173 |
|
25 | 174 | gsc->fw.type = XE_UC_FW_TYPE_GSC;
|
| 175 | + INIT_WORK(&gsc->work, gsc_work); |
26 | 176 |
|
27 | 177 | /* The GSC uC is only available on the media GT */
|
28 | 178 | if (tile->media_gt && (gt != tile->media_gt)) {
|
@@ -50,3 +200,103 @@ int xe_gsc_init(struct xe_gsc *gsc)
|
50 | 200 | return ret;
|
51 | 201 | }
|
52 | 202 |
|
| 203 | +static void free_resources(struct drm_device *drm, void *arg) |
| 204 | +{ |
| 205 | + struct xe_gsc *gsc = arg; |
| 206 | + |
| 207 | + if (gsc->wq) { |
| 208 | + destroy_workqueue(gsc->wq); |
| 209 | + gsc->wq = NULL; |
| 210 | + } |
| 211 | + |
| 212 | + if (gsc->q) { |
| 213 | + xe_exec_queue_put(gsc->q); |
| 214 | + gsc->q = NULL; |
| 215 | + } |
| 216 | + |
| 217 | + if (gsc->private) { |
| 218 | + xe_bo_unpin_map_no_vm(gsc->private); |
| 219 | + gsc->private = NULL; |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +int xe_gsc_init_post_hwconfig(struct xe_gsc *gsc) |
| 224 | +{ |
| 225 | + struct xe_gt *gt = gsc_to_gt(gsc); |
| 226 | + struct xe_tile *tile = gt_to_tile(gt); |
| 227 | + struct xe_device *xe = gt_to_xe(gt); |
| 228 | + struct xe_hw_engine *hwe = xe_gt_hw_engine(gt, XE_ENGINE_CLASS_OTHER, 0, true); |
| 229 | + struct xe_exec_queue *q; |
| 230 | + struct workqueue_struct *wq; |
| 231 | + struct xe_bo *bo; |
| 232 | + int err; |
| 233 | + |
| 234 | + if (!xe_uc_fw_is_available(&gsc->fw)) |
| 235 | + return 0; |
| 236 | + |
| 237 | + if (!hwe) |
| 238 | + return -ENODEV; |
| 239 | + |
| 240 | + bo = xe_bo_create_pin_map(xe, tile, NULL, SZ_4M, |
| 241 | + ttm_bo_type_kernel, |
| 242 | + XE_BO_CREATE_STOLEN_BIT | |
| 243 | + XE_BO_CREATE_GGTT_BIT); |
| 244 | + if (IS_ERR(bo)) |
| 245 | + return PTR_ERR(bo); |
| 246 | + |
| 247 | + q = xe_exec_queue_create(xe, NULL, |
| 248 | + BIT(hwe->logical_instance), 1, hwe, |
| 249 | + EXEC_QUEUE_FLAG_KERNEL | |
| 250 | + EXEC_QUEUE_FLAG_PERMANENT); |
| 251 | + if (IS_ERR(q)) { |
| 252 | + xe_gt_err(gt, "Failed to create queue for GSC submission\n"); |
| 253 | + err = PTR_ERR(q); |
| 254 | + goto out_bo; |
| 255 | + } |
| 256 | + |
| 257 | + wq = alloc_ordered_workqueue("gsc-ordered-wq", 0); |
| 258 | + if (!wq) { |
| 259 | + err = -ENOMEM; |
| 260 | + goto out_q; |
| 261 | + } |
| 262 | + |
| 263 | + gsc->private = bo; |
| 264 | + gsc->q = q; |
| 265 | + gsc->wq = wq; |
| 266 | + |
| 267 | + err = drmm_add_action_or_reset(&xe->drm, free_resources, gsc); |
| 268 | + if (err) |
| 269 | + return err; |
| 270 | + |
| 271 | + xe_uc_fw_change_status(&gsc->fw, XE_UC_FIRMWARE_LOADABLE); |
| 272 | + |
| 273 | + return 0; |
| 274 | + |
| 275 | +out_q: |
| 276 | + xe_exec_queue_put(q); |
| 277 | +out_bo: |
| 278 | + xe_bo_unpin_map_no_vm(bo); |
| 279 | + return err; |
| 280 | +} |
| 281 | + |
| 282 | +void xe_gsc_load_start(struct xe_gsc *gsc) |
| 283 | +{ |
| 284 | + struct xe_gt *gt = gsc_to_gt(gsc); |
| 285 | + |
| 286 | + if (!xe_uc_fw_is_loadable(&gsc->fw) || !gsc->q) |
| 287 | + return; |
| 288 | + |
| 289 | + /* GSC FW survives GT reset and D3Hot */ |
| 290 | + if (gsc_fw_is_loaded(gt)) { |
| 291 | + xe_uc_fw_change_status(&gsc->fw, XE_UC_FIRMWARE_TRANSFERRED); |
| 292 | + return; |
| 293 | + } |
| 294 | + |
| 295 | + queue_work(gsc->wq, &gsc->work); |
| 296 | +} |
| 297 | + |
| 298 | +void xe_gsc_wait_for_worker_completion(struct xe_gsc *gsc) |
| 299 | +{ |
| 300 | + if (xe_uc_fw_is_loadable(&gsc->fw) && gsc->wq) |
| 301 | + flush_work(&gsc->work); |
| 302 | +} |
0 commit comments