|
3 | 3 | * Copyright © 2024 Intel Corporation
|
4 | 4 | */
|
5 | 5 |
|
| 6 | +#include "xe_gt_tlb_invalidation.h" |
| 7 | +#include "xe_pt.h" |
6 | 8 | #include "xe_svm.h"
|
7 | 9 | #include "xe_vm.h"
|
8 | 10 | #include "xe_vm_types.h"
|
9 | 11 |
|
| 12 | +static struct xe_vm *gpusvm_to_vm(struct drm_gpusvm *gpusvm) |
| 13 | +{ |
| 14 | + return container_of(gpusvm, struct xe_vm, svm.gpusvm); |
| 15 | +} |
| 16 | + |
| 17 | +static struct xe_vm *range_to_vm(struct drm_gpusvm_range *r) |
| 18 | +{ |
| 19 | + return gpusvm_to_vm(r->gpusvm); |
| 20 | +} |
| 21 | + |
| 22 | +static unsigned long xe_svm_range_start(struct xe_svm_range *range) |
| 23 | +{ |
| 24 | + return drm_gpusvm_range_start(&range->base); |
| 25 | +} |
| 26 | + |
| 27 | +static unsigned long xe_svm_range_end(struct xe_svm_range *range) |
| 28 | +{ |
| 29 | + return drm_gpusvm_range_end(&range->base); |
| 30 | +} |
| 31 | + |
| 32 | +static struct drm_gpusvm_range * |
| 33 | +xe_svm_range_alloc(struct drm_gpusvm *gpusvm) |
| 34 | +{ |
| 35 | + struct xe_svm_range *range; |
| 36 | + |
| 37 | + range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 38 | + if (!range) |
| 39 | + return ERR_PTR(-ENOMEM); |
| 40 | + |
| 41 | + xe_vm_get(gpusvm_to_vm(gpusvm)); |
| 42 | + |
| 43 | + return &range->base; |
| 44 | +} |
| 45 | + |
| 46 | +static void xe_svm_range_free(struct drm_gpusvm_range *range) |
| 47 | +{ |
| 48 | + xe_vm_put(range_to_vm(range)); |
| 49 | + kfree(range); |
| 50 | +} |
| 51 | + |
| 52 | +static struct xe_svm_range *to_xe_range(struct drm_gpusvm_range *r) |
| 53 | +{ |
| 54 | + return container_of(r, struct xe_svm_range, base); |
| 55 | +} |
| 56 | + |
| 57 | +static u8 |
| 58 | +xe_svm_range_notifier_event_begin(struct xe_vm *vm, struct drm_gpusvm_range *r, |
| 59 | + const struct mmu_notifier_range *mmu_range, |
| 60 | + u64 *adj_start, u64 *adj_end) |
| 61 | +{ |
| 62 | + struct xe_svm_range *range = to_xe_range(r); |
| 63 | + struct xe_device *xe = vm->xe; |
| 64 | + struct xe_tile *tile; |
| 65 | + u8 tile_mask = 0; |
| 66 | + u8 id; |
| 67 | + |
| 68 | + xe_svm_assert_in_notifier(vm); |
| 69 | + |
| 70 | + /* Skip if already unmapped or if no binding exist */ |
| 71 | + if (range->base.flags.unmapped || !range->tile_present) |
| 72 | + return 0; |
| 73 | + |
| 74 | + /* Adjust invalidation to range boundaries */ |
| 75 | + *adj_start = min(xe_svm_range_start(range), mmu_range->start); |
| 76 | + *adj_end = max(xe_svm_range_end(range), mmu_range->end); |
| 77 | + |
| 78 | + /* |
| 79 | + * XXX: Ideally would zap PTEs in one shot in xe_svm_invalidate but the |
| 80 | + * invalidation code can't correctly cope with sparse ranges or |
| 81 | + * invalidations spanning multiple ranges. |
| 82 | + */ |
| 83 | + for_each_tile(tile, xe, id) |
| 84 | + if (xe_pt_zap_ptes_range(tile, vm, range)) { |
| 85 | + tile_mask |= BIT(id); |
| 86 | + range->tile_invalidated |= BIT(id); |
| 87 | + } |
| 88 | + |
| 89 | + return tile_mask; |
| 90 | +} |
| 91 | + |
| 92 | +static void |
| 93 | +xe_svm_range_notifier_event_end(struct xe_vm *vm, struct drm_gpusvm_range *r, |
| 94 | + const struct mmu_notifier_range *mmu_range) |
| 95 | +{ |
| 96 | + struct drm_gpusvm_ctx ctx = { .in_notifier = true, }; |
| 97 | + |
| 98 | + xe_svm_assert_in_notifier(vm); |
| 99 | + |
| 100 | + drm_gpusvm_range_unmap_pages(&vm->svm.gpusvm, r, &ctx); |
| 101 | + /* TODO: Add range to garbage collector if VM is not closed */ |
| 102 | +} |
| 103 | + |
10 | 104 | static void xe_svm_invalidate(struct drm_gpusvm *gpusvm,
|
11 | 105 | struct drm_gpusvm_notifier *notifier,
|
12 | 106 | const struct mmu_notifier_range *mmu_range)
|
13 | 107 | {
|
14 |
| - /* TODO: Implement */ |
| 108 | + struct xe_vm *vm = gpusvm_to_vm(gpusvm); |
| 109 | + struct xe_device *xe = vm->xe; |
| 110 | + struct xe_tile *tile; |
| 111 | + struct drm_gpusvm_range *r, *first; |
| 112 | + struct xe_gt_tlb_invalidation_fence |
| 113 | + fence[XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE]; |
| 114 | + u64 adj_start = mmu_range->start, adj_end = mmu_range->end; |
| 115 | + u8 tile_mask = 0; |
| 116 | + u8 id; |
| 117 | + u32 fence_id = 0; |
| 118 | + long err; |
| 119 | + |
| 120 | + xe_svm_assert_in_notifier(vm); |
| 121 | + |
| 122 | + /* Adjust invalidation to notifier boundaries */ |
| 123 | + adj_start = max(drm_gpusvm_notifier_start(notifier), adj_start); |
| 124 | + adj_end = min(drm_gpusvm_notifier_end(notifier), adj_end); |
| 125 | + |
| 126 | + first = drm_gpusvm_range_find(notifier, adj_start, adj_end); |
| 127 | + if (!first) |
| 128 | + return; |
| 129 | + |
| 130 | + /* |
| 131 | + * PTs may be getting destroyed so not safe to touch these but PT should |
| 132 | + * be invalidated at this point in time. Regardless we still need to |
| 133 | + * ensure any dma mappings are unmapped in the here. |
| 134 | + */ |
| 135 | + if (xe_vm_is_closed(vm)) |
| 136 | + goto range_notifier_event_end; |
| 137 | + |
| 138 | + /* |
| 139 | + * XXX: Less than ideal to always wait on VM's resv slots if an |
| 140 | + * invalidation is not required. Could walk range list twice to figure |
| 141 | + * out if an invalidations is need, but also not ideal. |
| 142 | + */ |
| 143 | + err = dma_resv_wait_timeout(xe_vm_resv(vm), |
| 144 | + DMA_RESV_USAGE_BOOKKEEP, |
| 145 | + false, MAX_SCHEDULE_TIMEOUT); |
| 146 | + XE_WARN_ON(err <= 0); |
| 147 | + |
| 148 | + r = first; |
| 149 | + drm_gpusvm_for_each_range(r, notifier, adj_start, adj_end) |
| 150 | + tile_mask |= xe_svm_range_notifier_event_begin(vm, r, mmu_range, |
| 151 | + &adj_start, |
| 152 | + &adj_end); |
| 153 | + if (!tile_mask) |
| 154 | + goto range_notifier_event_end; |
| 155 | + |
| 156 | + xe_device_wmb(xe); |
| 157 | + |
| 158 | + for_each_tile(tile, xe, id) { |
| 159 | + if (tile_mask & BIT(id)) { |
| 160 | + int err; |
| 161 | + |
| 162 | + xe_gt_tlb_invalidation_fence_init(tile->primary_gt, |
| 163 | + &fence[fence_id], true); |
| 164 | + |
| 165 | + err = xe_gt_tlb_invalidation_range(tile->primary_gt, |
| 166 | + &fence[fence_id], |
| 167 | + adj_start, |
| 168 | + adj_end, |
| 169 | + vm->usm.asid); |
| 170 | + if (WARN_ON_ONCE(err < 0)) |
| 171 | + goto wait; |
| 172 | + ++fence_id; |
| 173 | + |
| 174 | + if (!tile->media_gt) |
| 175 | + continue; |
| 176 | + |
| 177 | + xe_gt_tlb_invalidation_fence_init(tile->media_gt, |
| 178 | + &fence[fence_id], true); |
| 179 | + |
| 180 | + err = xe_gt_tlb_invalidation_range(tile->media_gt, |
| 181 | + &fence[fence_id], |
| 182 | + adj_start, |
| 183 | + adj_end, |
| 184 | + vm->usm.asid); |
| 185 | + if (WARN_ON_ONCE(err < 0)) |
| 186 | + goto wait; |
| 187 | + ++fence_id; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | +wait: |
| 192 | + for (id = 0; id < fence_id; ++id) |
| 193 | + xe_gt_tlb_invalidation_fence_wait(&fence[id]); |
| 194 | + |
| 195 | +range_notifier_event_end: |
| 196 | + r = first; |
| 197 | + drm_gpusvm_for_each_range(r, notifier, adj_start, adj_end) |
| 198 | + xe_svm_range_notifier_event_end(vm, r, mmu_range); |
15 | 199 | }
|
16 | 200 |
|
17 | 201 | static const struct drm_gpusvm_ops gpusvm_ops = {
|
| 202 | + .range_alloc = xe_svm_range_alloc, |
| 203 | + .range_free = xe_svm_range_free, |
18 | 204 | .invalidate = xe_svm_invalidate,
|
19 | 205 | };
|
20 | 206 |
|
@@ -71,3 +257,44 @@ void xe_svm_fini(struct xe_vm *vm)
|
71 | 257 |
|
72 | 258 | drm_gpusvm_fini(&vm->svm.gpusvm);
|
73 | 259 | }
|
| 260 | + |
| 261 | +/** |
| 262 | + * xe_svm_handle_pagefault() - SVM handle page fault |
| 263 | + * @vm: The VM. |
| 264 | + * @vma: The CPU address mirror VMA. |
| 265 | + * @tile: The tile upon the fault occurred. |
| 266 | + * @fault_addr: The GPU fault address. |
| 267 | + * @atomic: The fault atomic access bit. |
| 268 | + * |
| 269 | + * Create GPU bindings for a SVM page fault. |
| 270 | + * |
| 271 | + * Return: 0 on success, negative error code on error. |
| 272 | + */ |
| 273 | +int xe_svm_handle_pagefault(struct xe_vm *vm, struct xe_vma *vma, |
| 274 | + struct xe_tile *tile, u64 fault_addr, |
| 275 | + bool atomic) |
| 276 | +{ |
| 277 | + struct drm_gpusvm_ctx ctx = { .read_only = xe_vma_read_only(vma), }; |
| 278 | + struct drm_gpusvm_range *r; |
| 279 | + int err; |
| 280 | + |
| 281 | + lockdep_assert_held_write(&vm->lock); |
| 282 | + xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(vma)); |
| 283 | + |
| 284 | +retry: |
| 285 | + /* TODO: Run garbage collector */ |
| 286 | + |
| 287 | + r = drm_gpusvm_range_find_or_insert(&vm->svm.gpusvm, fault_addr, |
| 288 | + xe_vma_start(vma), xe_vma_end(vma), |
| 289 | + &ctx); |
| 290 | + if (IS_ERR(r)) |
| 291 | + return PTR_ERR(r); |
| 292 | + |
| 293 | + err = drm_gpusvm_range_get_pages(&vm->svm.gpusvm, r, &ctx); |
| 294 | + if (err == -EFAULT || err == -EPERM) /* Corner where CPU mappings have changed */ |
| 295 | + goto retry; |
| 296 | + |
| 297 | + /* TODO: Issue bind */ |
| 298 | + |
| 299 | + return err; |
| 300 | +} |
0 commit comments