drm/xe/xe2: Allocate extra pages for ccs during bo create
[linux-2.6-microblaze.git] / drivers / gpu / drm / xe / xe_bo.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5
6 #include "xe_bo.h"
7
8 #include <linux/dma-buf.h>
9
10 #include <drm/drm_drv.h>
11 #include <drm/drm_gem_ttm_helper.h>
12 #include <drm/drm_managed.h>
13 #include <drm/ttm/ttm_device.h>
14 #include <drm/ttm/ttm_placement.h>
15 #include <drm/ttm/ttm_tt.h>
16 #include <drm/xe_drm.h>
17
18 #include "xe_device.h"
19 #include "xe_dma_buf.h"
20 #include "xe_drm_client.h"
21 #include "xe_ggtt.h"
22 #include "xe_gt.h"
23 #include "xe_map.h"
24 #include "xe_migrate.h"
25 #include "xe_preempt_fence.h"
26 #include "xe_res_cursor.h"
27 #include "xe_trace.h"
28 #include "xe_ttm_stolen_mgr.h"
29 #include "xe_vm.h"
30
31 static const struct ttm_place sys_placement_flags = {
32         .fpfn = 0,
33         .lpfn = 0,
34         .mem_type = XE_PL_SYSTEM,
35         .flags = 0,
36 };
37
38 static struct ttm_placement sys_placement = {
39         .num_placement = 1,
40         .placement = &sys_placement_flags,
41         .num_busy_placement = 1,
42         .busy_placement = &sys_placement_flags,
43 };
44
45 static const struct ttm_place tt_placement_flags = {
46         .fpfn = 0,
47         .lpfn = 0,
48         .mem_type = XE_PL_TT,
49         .flags = 0,
50 };
51
52 static struct ttm_placement tt_placement = {
53         .num_placement = 1,
54         .placement = &tt_placement_flags,
55         .num_busy_placement = 1,
56         .busy_placement = &sys_placement_flags,
57 };
58
59 bool mem_type_is_vram(u32 mem_type)
60 {
61         return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN;
62 }
63
64 static bool resource_is_stolen_vram(struct xe_device *xe, struct ttm_resource *res)
65 {
66         return res->mem_type == XE_PL_STOLEN && IS_DGFX(xe);
67 }
68
69 static bool resource_is_vram(struct ttm_resource *res)
70 {
71         return mem_type_is_vram(res->mem_type);
72 }
73
74 bool xe_bo_is_vram(struct xe_bo *bo)
75 {
76         return resource_is_vram(bo->ttm.resource) ||
77                 resource_is_stolen_vram(xe_bo_device(bo), bo->ttm.resource);
78 }
79
80 bool xe_bo_is_stolen(struct xe_bo *bo)
81 {
82         return bo->ttm.resource->mem_type == XE_PL_STOLEN;
83 }
84
85 /**
86  * xe_bo_is_stolen_devmem - check if BO is of stolen type accessed via PCI BAR
87  * @bo: The BO
88  *
89  * The stolen memory is accessed through the PCI BAR for both DGFX and some
90  * integrated platforms that have a dedicated bit in the PTE for devmem (DM).
91  *
92  * Returns: true if it's stolen memory accessed via PCI BAR, false otherwise.
93  */
94 bool xe_bo_is_stolen_devmem(struct xe_bo *bo)
95 {
96         return xe_bo_is_stolen(bo) &&
97                 GRAPHICS_VERx100(xe_bo_device(bo)) >= 1270;
98 }
99
100 static bool xe_bo_is_user(struct xe_bo *bo)
101 {
102         return bo->flags & XE_BO_CREATE_USER_BIT;
103 }
104
105 static struct xe_migrate *
106 mem_type_to_migrate(struct xe_device *xe, u32 mem_type)
107 {
108         struct xe_tile *tile;
109
110         xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type));
111         tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)];
112         return tile->migrate;
113 }
114
115 static struct xe_mem_region *res_to_mem_region(struct ttm_resource *res)
116 {
117         struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
118         struct ttm_resource_manager *mgr;
119
120         xe_assert(xe, resource_is_vram(res));
121         mgr = ttm_manager_type(&xe->ttm, res->mem_type);
122         return to_xe_ttm_vram_mgr(mgr)->vram;
123 }
124
125 static void try_add_system(struct xe_device *xe, struct xe_bo *bo,
126                            u32 bo_flags, u32 *c)
127 {
128         xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
129
130         if (bo_flags & XE_BO_CREATE_SYSTEM_BIT) {
131                 bo->placements[*c] = (struct ttm_place) {
132                         .mem_type = XE_PL_TT,
133                 };
134                 *c += 1;
135
136                 if (bo->props.preferred_mem_type == XE_BO_PROPS_INVALID)
137                         bo->props.preferred_mem_type = XE_PL_TT;
138         }
139 }
140
141 static void add_vram(struct xe_device *xe, struct xe_bo *bo,
142                      struct ttm_place *places, u32 bo_flags, u32 mem_type, u32 *c)
143 {
144         struct ttm_place place = { .mem_type = mem_type };
145         struct xe_mem_region *vram;
146         u64 io_size;
147
148         vram = to_xe_ttm_vram_mgr(ttm_manager_type(&xe->ttm, mem_type))->vram;
149         xe_assert(xe, vram && vram->usable_size);
150         io_size = vram->io_size;
151
152         /*
153          * For eviction / restore on suspend / resume objects
154          * pinned in VRAM must be contiguous
155          */
156         if (bo_flags & (XE_BO_CREATE_PINNED_BIT |
157                         XE_BO_CREATE_GGTT_BIT))
158                 place.flags |= TTM_PL_FLAG_CONTIGUOUS;
159
160         if (io_size < vram->usable_size) {
161                 if (bo_flags & XE_BO_NEEDS_CPU_ACCESS) {
162                         place.fpfn = 0;
163                         place.lpfn = io_size >> PAGE_SHIFT;
164                 } else {
165                         place.flags |= TTM_PL_FLAG_TOPDOWN;
166                 }
167         }
168         places[*c] = place;
169         *c += 1;
170
171         if (bo->props.preferred_mem_type == XE_BO_PROPS_INVALID)
172                 bo->props.preferred_mem_type = mem_type;
173 }
174
175 static void try_add_vram(struct xe_device *xe, struct xe_bo *bo,
176                          u32 bo_flags, u32 *c)
177 {
178         xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
179
180         if (bo->props.preferred_gt == XE_GT1) {
181                 if (bo_flags & XE_BO_CREATE_VRAM1_BIT)
182                         add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM1, c);
183                 if (bo_flags & XE_BO_CREATE_VRAM0_BIT)
184                         add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM0, c);
185         } else {
186                 if (bo_flags & XE_BO_CREATE_VRAM0_BIT)
187                         add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM0, c);
188                 if (bo_flags & XE_BO_CREATE_VRAM1_BIT)
189                         add_vram(xe, bo, bo->placements, bo_flags, XE_PL_VRAM1, c);
190         }
191 }
192
193 static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo,
194                            u32 bo_flags, u32 *c)
195 {
196         xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
197
198         if (bo_flags & XE_BO_CREATE_STOLEN_BIT) {
199                 bo->placements[*c] = (struct ttm_place) {
200                         .mem_type = XE_PL_STOLEN,
201                         .flags = bo_flags & (XE_BO_CREATE_PINNED_BIT |
202                                              XE_BO_CREATE_GGTT_BIT) ?
203                                 TTM_PL_FLAG_CONTIGUOUS : 0,
204                 };
205                 *c += 1;
206         }
207 }
208
209 static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
210                                        u32 bo_flags)
211 {
212         u32 c = 0;
213
214         bo->props.preferred_mem_type = XE_BO_PROPS_INVALID;
215
216         /* The order of placements should indicate preferred location */
217
218         if (bo->props.preferred_mem_class == DRM_XE_MEM_REGION_CLASS_SYSMEM) {
219                 try_add_system(xe, bo, bo_flags, &c);
220                 try_add_vram(xe, bo, bo_flags, &c);
221         } else {
222                 try_add_vram(xe, bo, bo_flags, &c);
223                 try_add_system(xe, bo, bo_flags, &c);
224         }
225         try_add_stolen(xe, bo, bo_flags, &c);
226
227         if (!c)
228                 return -EINVAL;
229
230         bo->placement = (struct ttm_placement) {
231                 .num_placement = c,
232                 .placement = bo->placements,
233                 .num_busy_placement = c,
234                 .busy_placement = bo->placements,
235         };
236
237         return 0;
238 }
239
240 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
241                               u32 bo_flags)
242 {
243         xe_bo_assert_held(bo);
244         return __xe_bo_placement_for_flags(xe, bo, bo_flags);
245 }
246
247 static void xe_evict_flags(struct ttm_buffer_object *tbo,
248                            struct ttm_placement *placement)
249 {
250         struct xe_bo *bo;
251
252         if (!xe_bo_is_xe_bo(tbo)) {
253                 /* Don't handle scatter gather BOs */
254                 if (tbo->type == ttm_bo_type_sg) {
255                         placement->num_placement = 0;
256                         placement->num_busy_placement = 0;
257                         return;
258                 }
259
260                 *placement = sys_placement;
261                 return;
262         }
263
264         /*
265          * For xe, sg bos that are evicted to system just triggers a
266          * rebind of the sg list upon subsequent validation to XE_PL_TT.
267          */
268
269         bo = ttm_to_xe_bo(tbo);
270         switch (tbo->resource->mem_type) {
271         case XE_PL_VRAM0:
272         case XE_PL_VRAM1:
273         case XE_PL_STOLEN:
274                 *placement = tt_placement;
275                 break;
276         case XE_PL_TT:
277         default:
278                 *placement = sys_placement;
279                 break;
280         }
281 }
282
283 struct xe_ttm_tt {
284         struct ttm_tt ttm;
285         struct device *dev;
286         struct sg_table sgt;
287         struct sg_table *sg;
288 };
289
290 static int xe_tt_map_sg(struct ttm_tt *tt)
291 {
292         struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
293         unsigned long num_pages = tt->num_pages;
294         int ret;
295
296         XE_WARN_ON(tt->page_flags & TTM_TT_FLAG_EXTERNAL);
297
298         if (xe_tt->sg)
299                 return 0;
300
301         ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages,
302                                                 num_pages, 0,
303                                                 (u64)num_pages << PAGE_SHIFT,
304                                                 xe_sg_segment_size(xe_tt->dev),
305                                                 GFP_KERNEL);
306         if (ret)
307                 return ret;
308
309         xe_tt->sg = &xe_tt->sgt;
310         ret = dma_map_sgtable(xe_tt->dev, xe_tt->sg, DMA_BIDIRECTIONAL,
311                               DMA_ATTR_SKIP_CPU_SYNC);
312         if (ret) {
313                 sg_free_table(xe_tt->sg);
314                 xe_tt->sg = NULL;
315                 return ret;
316         }
317
318         return 0;
319 }
320
321 struct sg_table *xe_bo_sg(struct xe_bo *bo)
322 {
323         struct ttm_tt *tt = bo->ttm.ttm;
324         struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
325
326         return xe_tt->sg;
327 }
328
329 static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
330                                        u32 page_flags)
331 {
332         struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
333         struct xe_device *xe = xe_bo_device(bo);
334         struct xe_ttm_tt *tt;
335         unsigned long extra_pages;
336         enum ttm_caching caching;
337         int err;
338
339         tt = kzalloc(sizeof(*tt), GFP_KERNEL);
340         if (!tt)
341                 return NULL;
342
343         tt->dev = xe->drm.dev;
344
345         extra_pages = 0;
346         if (xe_bo_needs_ccs_pages(bo))
347                 extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, bo->size),
348                                            PAGE_SIZE);
349
350         switch (bo->cpu_caching) {
351         case DRM_XE_GEM_CPU_CACHING_WC:
352                 caching = ttm_write_combined;
353                 break;
354         default:
355                 caching = ttm_cached;
356                 break;
357         }
358
359         WARN_ON((bo->flags & XE_BO_CREATE_USER_BIT) && !bo->cpu_caching);
360
361         /*
362          * Display scanout is always non-coherent with the CPU cache.
363          *
364          * For Xe_LPG and beyond, PPGTT PTE lookups are also non-coherent and
365          * require a CPU:WC mapping.
366          */
367         if ((!bo->cpu_caching && bo->flags & XE_BO_SCANOUT_BIT) ||
368             (xe->info.graphics_verx100 >= 1270 && bo->flags & XE_BO_PAGETABLE))
369                 caching = ttm_write_combined;
370
371         err = ttm_tt_init(&tt->ttm, &bo->ttm, page_flags, caching, extra_pages);
372         if (err) {
373                 kfree(tt);
374                 return NULL;
375         }
376
377         return &tt->ttm;
378 }
379
380 static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
381                               struct ttm_operation_ctx *ctx)
382 {
383         int err;
384
385         /*
386          * dma-bufs are not populated with pages, and the dma-
387          * addresses are set up when moved to XE_PL_TT.
388          */
389         if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
390                 return 0;
391
392         err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx);
393         if (err)
394                 return err;
395
396         /* A follow up may move this xe_bo_move when BO is moved to XE_PL_TT */
397         err = xe_tt_map_sg(tt);
398         if (err)
399                 ttm_pool_free(&ttm_dev->pool, tt);
400
401         return err;
402 }
403
404 static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
405 {
406         struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
407
408         if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
409                 return;
410
411         if (xe_tt->sg) {
412                 dma_unmap_sgtable(xe_tt->dev, xe_tt->sg,
413                                   DMA_BIDIRECTIONAL, 0);
414                 sg_free_table(xe_tt->sg);
415                 xe_tt->sg = NULL;
416         }
417
418         return ttm_pool_free(&ttm_dev->pool, tt);
419 }
420
421 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
422 {
423         ttm_tt_fini(tt);
424         kfree(tt);
425 }
426
427 static int xe_ttm_io_mem_reserve(struct ttm_device *bdev,
428                                  struct ttm_resource *mem)
429 {
430         struct xe_device *xe = ttm_to_xe_device(bdev);
431
432         switch (mem->mem_type) {
433         case XE_PL_SYSTEM:
434         case XE_PL_TT:
435                 return 0;
436         case XE_PL_VRAM0:
437         case XE_PL_VRAM1: {
438                 struct xe_ttm_vram_mgr_resource *vres =
439                         to_xe_ttm_vram_mgr_resource(mem);
440                 struct xe_mem_region *vram = res_to_mem_region(mem);
441
442                 if (vres->used_visible_size < mem->size)
443                         return -EINVAL;
444
445                 mem->bus.offset = mem->start << PAGE_SHIFT;
446
447                 if (vram->mapping &&
448                     mem->placement & TTM_PL_FLAG_CONTIGUOUS)
449                         mem->bus.addr = (u8 *)vram->mapping +
450                                 mem->bus.offset;
451
452                 mem->bus.offset += vram->io_start;
453                 mem->bus.is_iomem = true;
454
455 #if  !defined(CONFIG_X86)
456                 mem->bus.caching = ttm_write_combined;
457 #endif
458                 return 0;
459         } case XE_PL_STOLEN:
460                 return xe_ttm_stolen_io_mem_reserve(xe, mem);
461         default:
462                 return -EINVAL;
463         }
464 }
465
466 static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
467                                 const struct ttm_operation_ctx *ctx)
468 {
469         struct dma_resv_iter cursor;
470         struct dma_fence *fence;
471         struct drm_gem_object *obj = &bo->ttm.base;
472         struct drm_gpuvm_bo *vm_bo;
473         bool idle = false;
474         int ret = 0;
475
476         dma_resv_assert_held(bo->ttm.base.resv);
477
478         if (!list_empty(&bo->ttm.base.gpuva.list)) {
479                 dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
480                                     DMA_RESV_USAGE_BOOKKEEP);
481                 dma_resv_for_each_fence_unlocked(&cursor, fence)
482                         dma_fence_enable_sw_signaling(fence);
483                 dma_resv_iter_end(&cursor);
484         }
485
486         drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
487                 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
488                 struct drm_gpuva *gpuva;
489
490                 if (!xe_vm_in_fault_mode(vm)) {
491                         drm_gpuvm_bo_evict(vm_bo, true);
492                         continue;
493                 }
494
495                 if (!idle) {
496                         long timeout;
497
498                         if (ctx->no_wait_gpu &&
499                             !dma_resv_test_signaled(bo->ttm.base.resv,
500                                                     DMA_RESV_USAGE_BOOKKEEP))
501                                 return -EBUSY;
502
503                         timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
504                                                         DMA_RESV_USAGE_BOOKKEEP,
505                                                         ctx->interruptible,
506                                                         MAX_SCHEDULE_TIMEOUT);
507                         if (!timeout)
508                                 return -ETIME;
509                         if (timeout < 0)
510                                 return timeout;
511
512                         idle = true;
513                 }
514
515                 drm_gpuvm_bo_for_each_va(gpuva, vm_bo) {
516                         struct xe_vma *vma = gpuva_to_vma(gpuva);
517
518                         trace_xe_vma_evict(vma);
519                         ret = xe_vm_invalidate_vma(vma);
520                         if (XE_WARN_ON(ret))
521                                 return ret;
522                 }
523         }
524
525         return ret;
526 }
527
528 /*
529  * The dma-buf map_attachment() / unmap_attachment() is hooked up here.
530  * Note that unmapping the attachment is deferred to the next
531  * map_attachment time, or to bo destroy (after idling) whichever comes first.
532  * This is to avoid syncing before unmap_attachment(), assuming that the
533  * caller relies on idling the reservation object before moving the
534  * backing store out. Should that assumption not hold, then we will be able
535  * to unconditionally call unmap_attachment() when moving out to system.
536  */
537 static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
538                              struct ttm_resource *new_res)
539 {
540         struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
541         struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
542                                                ttm);
543         struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
544         struct sg_table *sg;
545
546         xe_assert(xe, attach);
547         xe_assert(xe, ttm_bo->ttm);
548
549         if (new_res->mem_type == XE_PL_SYSTEM)
550                 goto out;
551
552         if (ttm_bo->sg) {
553                 dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
554                 ttm_bo->sg = NULL;
555         }
556
557         sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
558         if (IS_ERR(sg))
559                 return PTR_ERR(sg);
560
561         ttm_bo->sg = sg;
562         xe_tt->sg = sg;
563
564 out:
565         ttm_bo_move_null(ttm_bo, new_res);
566
567         return 0;
568 }
569
570 /**
571  * xe_bo_move_notify - Notify subsystems of a pending move
572  * @bo: The buffer object
573  * @ctx: The struct ttm_operation_ctx controlling locking and waits.
574  *
575  * This function notifies subsystems of an upcoming buffer move.
576  * Upon receiving such a notification, subsystems should schedule
577  * halting access to the underlying pages and optionally add a fence
578  * to the buffer object's dma_resv object, that signals when access is
579  * stopped. The caller will wait on all dma_resv fences before
580  * starting the move.
581  *
582  * A subsystem may commence access to the object after obtaining
583  * bindings to the new backing memory under the object lock.
584  *
585  * Return: 0 on success, -EINTR or -ERESTARTSYS if interrupted in fault mode,
586  * negative error code on error.
587  */
588 static int xe_bo_move_notify(struct xe_bo *bo,
589                              const struct ttm_operation_ctx *ctx)
590 {
591         struct ttm_buffer_object *ttm_bo = &bo->ttm;
592         struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
593         int ret;
594
595         /*
596          * If this starts to call into many components, consider
597          * using a notification chain here.
598          */
599
600         if (xe_bo_is_pinned(bo))
601                 return -EINVAL;
602
603         xe_bo_vunmap(bo);
604         ret = xe_bo_trigger_rebind(xe, bo, ctx);
605         if (ret)
606                 return ret;
607
608         /* Don't call move_notify() for imported dma-bufs. */
609         if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach)
610                 dma_buf_move_notify(ttm_bo->base.dma_buf);
611
612         return 0;
613 }
614
615 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
616                       struct ttm_operation_ctx *ctx,
617                       struct ttm_resource *new_mem,
618                       struct ttm_place *hop)
619 {
620         struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
621         struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
622         struct ttm_resource *old_mem = ttm_bo->resource;
623         u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
624         struct ttm_tt *ttm = ttm_bo->ttm;
625         struct xe_migrate *migrate = NULL;
626         struct dma_fence *fence;
627         bool move_lacks_source;
628         bool tt_has_data;
629         bool needs_clear;
630         int ret = 0;
631
632         /* Bo creation path, moving to system or TT. No clearing required. */
633         if (!old_mem && ttm) {
634                 ttm_bo_move_null(ttm_bo, new_mem);
635                 return 0;
636         }
637
638         if (ttm_bo->type == ttm_bo_type_sg) {
639                 ret = xe_bo_move_notify(bo, ctx);
640                 if (!ret)
641                         ret = xe_bo_move_dmabuf(ttm_bo, new_mem);
642                 goto out;
643         }
644
645         tt_has_data = ttm && (ttm_tt_is_populated(ttm) ||
646                               (ttm->page_flags & TTM_TT_FLAG_SWAPPED));
647
648         move_lacks_source = !mem_type_is_vram(old_mem_type) && !tt_has_data;
649
650         needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
651                 (!ttm && ttm_bo->type == ttm_bo_type_device);
652
653         if ((move_lacks_source && !needs_clear) ||
654             (old_mem_type == XE_PL_SYSTEM &&
655              new_mem->mem_type == XE_PL_TT)) {
656                 ttm_bo_move_null(ttm_bo, new_mem);
657                 goto out;
658         }
659
660         /*
661          * Failed multi-hop where the old_mem is still marked as
662          * TTM_PL_FLAG_TEMPORARY, should just be a dummy move.
663          */
664         if (old_mem_type == XE_PL_TT &&
665             new_mem->mem_type == XE_PL_TT) {
666                 ttm_bo_move_null(ttm_bo, new_mem);
667                 goto out;
668         }
669
670         if (!move_lacks_source && !xe_bo_is_pinned(bo)) {
671                 ret = xe_bo_move_notify(bo, ctx);
672                 if (ret)
673                         goto out;
674         }
675
676         if (old_mem_type == XE_PL_TT &&
677             new_mem->mem_type == XE_PL_SYSTEM) {
678                 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
679                                                      DMA_RESV_USAGE_BOOKKEEP,
680                                                      true,
681                                                      MAX_SCHEDULE_TIMEOUT);
682                 if (timeout < 0) {
683                         ret = timeout;
684                         goto out;
685                 }
686                 ttm_bo_move_null(ttm_bo, new_mem);
687                 goto out;
688         }
689
690         if (!move_lacks_source &&
691             ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) ||
692              (mem_type_is_vram(old_mem_type) &&
693               new_mem->mem_type == XE_PL_SYSTEM))) {
694                 hop->fpfn = 0;
695                 hop->lpfn = 0;
696                 hop->mem_type = XE_PL_TT;
697                 hop->flags = TTM_PL_FLAG_TEMPORARY;
698                 ret = -EMULTIHOP;
699                 goto out;
700         }
701
702         if (bo->tile)
703                 migrate = bo->tile->migrate;
704         else if (resource_is_vram(new_mem))
705                 migrate = mem_type_to_migrate(xe, new_mem->mem_type);
706         else if (mem_type_is_vram(old_mem_type))
707                 migrate = mem_type_to_migrate(xe, old_mem_type);
708
709         xe_assert(xe, migrate);
710
711         trace_xe_bo_move(bo);
712         xe_device_mem_access_get(xe);
713
714         if (xe_bo_is_pinned(bo) && !xe_bo_is_user(bo)) {
715                 /*
716                  * Kernel memory that is pinned should only be moved on suspend
717                  * / resume, some of the pinned memory is required for the
718                  * device to resume / use the GPU to move other evicted memory
719                  * (user memory) around. This likely could be optimized a bit
720                  * futher where we find the minimum set of pinned memory
721                  * required for resume but for simplity doing a memcpy for all
722                  * pinned memory.
723                  */
724                 ret = xe_bo_vmap(bo);
725                 if (!ret) {
726                         ret = ttm_bo_move_memcpy(ttm_bo, ctx, new_mem);
727
728                         /* Create a new VMAP once kernel BO back in VRAM */
729                         if (!ret && resource_is_vram(new_mem)) {
730                                 struct xe_mem_region *vram = res_to_mem_region(new_mem);
731                                 void *new_addr = vram->mapping +
732                                         (new_mem->start << PAGE_SHIFT);
733
734                                 if (XE_WARN_ON(new_mem->start == XE_BO_INVALID_OFFSET)) {
735                                         ret = -EINVAL;
736                                         xe_device_mem_access_put(xe);
737                                         goto out;
738                                 }
739
740                                 xe_assert(xe, new_mem->start ==
741                                           bo->placements->fpfn);
742
743                                 iosys_map_set_vaddr_iomem(&bo->vmap, new_addr);
744                         }
745                 }
746         } else {
747                 if (move_lacks_source)
748                         fence = xe_migrate_clear(migrate, bo, new_mem);
749                 else
750                         fence = xe_migrate_copy(migrate,
751                                                 bo, bo, old_mem, new_mem);
752                 if (IS_ERR(fence)) {
753                         ret = PTR_ERR(fence);
754                         xe_device_mem_access_put(xe);
755                         goto out;
756                 }
757                 if (!move_lacks_source) {
758                         ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict,
759                                                         true, new_mem);
760                         if (ret) {
761                                 dma_fence_wait(fence, false);
762                                 ttm_bo_move_null(ttm_bo, new_mem);
763                                 ret = 0;
764                         }
765                 } else {
766                         /*
767                          * ttm_bo_move_accel_cleanup() may blow up if
768                          * bo->resource == NULL, so just attach the
769                          * fence and set the new resource.
770                          */
771                         dma_resv_add_fence(ttm_bo->base.resv, fence,
772                                            DMA_RESV_USAGE_KERNEL);
773                         ttm_bo_move_null(ttm_bo, new_mem);
774                 }
775
776                 dma_fence_put(fence);
777         }
778
779         xe_device_mem_access_put(xe);
780
781 out:
782         return ret;
783
784 }
785
786 /**
787  * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory
788  * @bo: The buffer object to move.
789  *
790  * On successful completion, the object memory will be moved to sytem memory.
791  * This function blocks until the object has been fully moved.
792  *
793  * This is needed to for special handling of pinned VRAM object during
794  * suspend-resume.
795  *
796  * Return: 0 on success. Negative error code on failure.
797  */
798 int xe_bo_evict_pinned(struct xe_bo *bo)
799 {
800         struct ttm_place place = {
801                 .mem_type = XE_PL_TT,
802         };
803         struct ttm_placement placement = {
804                 .placement = &place,
805                 .num_placement = 1,
806         };
807         struct ttm_operation_ctx ctx = {
808                 .interruptible = false,
809         };
810         struct ttm_resource *new_mem;
811         int ret;
812
813         xe_bo_assert_held(bo);
814
815         if (WARN_ON(!bo->ttm.resource))
816                 return -EINVAL;
817
818         if (WARN_ON(!xe_bo_is_pinned(bo)))
819                 return -EINVAL;
820
821         if (WARN_ON(!xe_bo_is_vram(bo)))
822                 return -EINVAL;
823
824         ret = ttm_bo_mem_space(&bo->ttm, &placement, &new_mem, &ctx);
825         if (ret)
826                 return ret;
827
828         if (!bo->ttm.ttm) {
829                 bo->ttm.ttm = xe_ttm_tt_create(&bo->ttm, 0);
830                 if (!bo->ttm.ttm) {
831                         ret = -ENOMEM;
832                         goto err_res_free;
833                 }
834         }
835
836         ret = ttm_tt_populate(bo->ttm.bdev, bo->ttm.ttm, &ctx);
837         if (ret)
838                 goto err_res_free;
839
840         ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
841         if (ret)
842                 goto err_res_free;
843
844         ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
845         if (ret)
846                 goto err_res_free;
847
848         dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
849                               false, MAX_SCHEDULE_TIMEOUT);
850
851         return 0;
852
853 err_res_free:
854         ttm_resource_free(&bo->ttm, &new_mem);
855         return ret;
856 }
857
858 /**
859  * xe_bo_restore_pinned() - Restore a pinned VRAM object
860  * @bo: The buffer object to move.
861  *
862  * On successful completion, the object memory will be moved back to VRAM.
863  * This function blocks until the object has been fully moved.
864  *
865  * This is needed to for special handling of pinned VRAM object during
866  * suspend-resume.
867  *
868  * Return: 0 on success. Negative error code on failure.
869  */
870 int xe_bo_restore_pinned(struct xe_bo *bo)
871 {
872         struct ttm_operation_ctx ctx = {
873                 .interruptible = false,
874         };
875         struct ttm_resource *new_mem;
876         int ret;
877
878         xe_bo_assert_held(bo);
879
880         if (WARN_ON(!bo->ttm.resource))
881                 return -EINVAL;
882
883         if (WARN_ON(!xe_bo_is_pinned(bo)))
884                 return -EINVAL;
885
886         if (WARN_ON(xe_bo_is_vram(bo) || !bo->ttm.ttm))
887                 return -EINVAL;
888
889         ret = ttm_bo_mem_space(&bo->ttm, &bo->placement, &new_mem, &ctx);
890         if (ret)
891                 return ret;
892
893         ret = ttm_tt_populate(bo->ttm.bdev, bo->ttm.ttm, &ctx);
894         if (ret)
895                 goto err_res_free;
896
897         ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
898         if (ret)
899                 goto err_res_free;
900
901         ret = xe_bo_move(&bo->ttm, false, &ctx, new_mem, NULL);
902         if (ret)
903                 goto err_res_free;
904
905         dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
906                               false, MAX_SCHEDULE_TIMEOUT);
907
908         return 0;
909
910 err_res_free:
911         ttm_resource_free(&bo->ttm, &new_mem);
912         return ret;
913 }
914
915 static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo,
916                                        unsigned long page_offset)
917 {
918         struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
919         struct xe_res_cursor cursor;
920         struct xe_mem_region *vram;
921
922         if (ttm_bo->resource->mem_type == XE_PL_STOLEN)
923                 return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT;
924
925         vram = res_to_mem_region(ttm_bo->resource);
926         xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor);
927         return (vram->io_start + cursor.start) >> PAGE_SHIFT;
928 }
929
930 static void __xe_bo_vunmap(struct xe_bo *bo);
931
932 /*
933  * TODO: Move this function to TTM so we don't rely on how TTM does its
934  * locking, thereby abusing TTM internals.
935  */
936 static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
937 {
938         struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
939         bool locked;
940
941         xe_assert(xe, !kref_read(&ttm_bo->kref));
942
943         /*
944          * We can typically only race with TTM trylocking under the
945          * lru_lock, which will immediately be unlocked again since
946          * the ttm_bo refcount is zero at this point. So trylocking *should*
947          * always succeed here, as long as we hold the lru lock.
948          */
949         spin_lock(&ttm_bo->bdev->lru_lock);
950         locked = dma_resv_trylock(ttm_bo->base.resv);
951         spin_unlock(&ttm_bo->bdev->lru_lock);
952         xe_assert(xe, locked);
953
954         return locked;
955 }
956
957 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
958 {
959         struct dma_resv_iter cursor;
960         struct dma_fence *fence;
961         struct dma_fence *replacement = NULL;
962         struct xe_bo *bo;
963
964         if (!xe_bo_is_xe_bo(ttm_bo))
965                 return;
966
967         bo = ttm_to_xe_bo(ttm_bo);
968         xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount)));
969
970         /*
971          * Corner case where TTM fails to allocate memory and this BOs resv
972          * still points the VMs resv
973          */
974         if (ttm_bo->base.resv != &ttm_bo->base._resv)
975                 return;
976
977         if (!xe_ttm_bo_lock_in_destructor(ttm_bo))
978                 return;
979
980         /*
981          * Scrub the preempt fences if any. The unbind fence is already
982          * attached to the resv.
983          * TODO: Don't do this for external bos once we scrub them after
984          * unbind.
985          */
986         dma_resv_for_each_fence(&cursor, ttm_bo->base.resv,
987                                 DMA_RESV_USAGE_BOOKKEEP, fence) {
988                 if (xe_fence_is_xe_preempt(fence) &&
989                     !dma_fence_is_signaled(fence)) {
990                         if (!replacement)
991                                 replacement = dma_fence_get_stub();
992
993                         dma_resv_replace_fences(ttm_bo->base.resv,
994                                                 fence->context,
995                                                 replacement,
996                                                 DMA_RESV_USAGE_BOOKKEEP);
997                 }
998         }
999         dma_fence_put(replacement);
1000
1001         dma_resv_unlock(ttm_bo->base.resv);
1002 }
1003
1004 static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
1005 {
1006         if (!xe_bo_is_xe_bo(ttm_bo))
1007                 return;
1008
1009         /*
1010          * Object is idle and about to be destroyed. Release the
1011          * dma-buf attachment.
1012          */
1013         if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
1014                 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
1015                                                        struct xe_ttm_tt, ttm);
1016
1017                 dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
1018                                          DMA_BIDIRECTIONAL);
1019                 ttm_bo->sg = NULL;
1020                 xe_tt->sg = NULL;
1021         }
1022 }
1023
1024 struct ttm_device_funcs xe_ttm_funcs = {
1025         .ttm_tt_create = xe_ttm_tt_create,
1026         .ttm_tt_populate = xe_ttm_tt_populate,
1027         .ttm_tt_unpopulate = xe_ttm_tt_unpopulate,
1028         .ttm_tt_destroy = xe_ttm_tt_destroy,
1029         .evict_flags = xe_evict_flags,
1030         .move = xe_bo_move,
1031         .io_mem_reserve = xe_ttm_io_mem_reserve,
1032         .io_mem_pfn = xe_ttm_io_mem_pfn,
1033         .release_notify = xe_ttm_bo_release_notify,
1034         .eviction_valuable = ttm_bo_eviction_valuable,
1035         .delete_mem_notify = xe_ttm_bo_delete_mem_notify,
1036 };
1037
1038 static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo)
1039 {
1040         struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
1041         struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
1042
1043         if (bo->ttm.base.import_attach)
1044                 drm_prime_gem_destroy(&bo->ttm.base, NULL);
1045         drm_gem_object_release(&bo->ttm.base);
1046
1047         xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list));
1048
1049         if (bo->ggtt_node.size)
1050                 xe_ggtt_remove_bo(bo->tile->mem.ggtt, bo);
1051
1052 #ifdef CONFIG_PROC_FS
1053         if (bo->client)
1054                 xe_drm_client_remove_bo(bo);
1055 #endif
1056
1057         if (bo->vm && xe_bo_is_user(bo))
1058                 xe_vm_put(bo->vm);
1059
1060         kfree(bo);
1061 }
1062
1063 static void xe_gem_object_free(struct drm_gem_object *obj)
1064 {
1065         /* Our BO reference counting scheme works as follows:
1066          *
1067          * The gem object kref is typically used throughout the driver,
1068          * and the gem object holds a ttm_buffer_object refcount, so
1069          * that when the last gem object reference is put, which is when
1070          * we end up in this function, we put also that ttm_buffer_object
1071          * refcount. Anything using gem interfaces is then no longer
1072          * allowed to access the object in a way that requires a gem
1073          * refcount, including locking the object.
1074          *
1075          * driver ttm callbacks is allowed to use the ttm_buffer_object
1076          * refcount directly if needed.
1077          */
1078         __xe_bo_vunmap(gem_to_xe_bo(obj));
1079         ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
1080 }
1081
1082 static void xe_gem_object_close(struct drm_gem_object *obj,
1083                                 struct drm_file *file_priv)
1084 {
1085         struct xe_bo *bo = gem_to_xe_bo(obj);
1086
1087         if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) {
1088                 xe_assert(xe_bo_device(bo), xe_bo_is_user(bo));
1089
1090                 xe_bo_lock(bo, false);
1091                 ttm_bo_set_bulk_move(&bo->ttm, NULL);
1092                 xe_bo_unlock(bo);
1093         }
1094 }
1095
1096 static bool should_migrate_to_system(struct xe_bo *bo)
1097 {
1098         struct xe_device *xe = xe_bo_device(bo);
1099
1100         return xe_device_in_fault_mode(xe) && bo->props.cpu_atomic;
1101 }
1102
1103 static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
1104 {
1105         struct ttm_buffer_object *tbo = vmf->vma->vm_private_data;
1106         struct drm_device *ddev = tbo->base.dev;
1107         vm_fault_t ret;
1108         int idx, r = 0;
1109
1110         ret = ttm_bo_vm_reserve(tbo, vmf);
1111         if (ret)
1112                 return ret;
1113
1114         if (drm_dev_enter(ddev, &idx)) {
1115                 struct xe_bo *bo = ttm_to_xe_bo(tbo);
1116
1117                 trace_xe_bo_cpu_fault(bo);
1118
1119                 if (should_migrate_to_system(bo)) {
1120                         r = xe_bo_migrate(bo, XE_PL_TT);
1121                         if (r == -EBUSY || r == -ERESTARTSYS || r == -EINTR)
1122                                 ret = VM_FAULT_NOPAGE;
1123                         else if (r)
1124                                 ret = VM_FAULT_SIGBUS;
1125                 }
1126                 if (!ret)
1127                         ret = ttm_bo_vm_fault_reserved(vmf,
1128                                                        vmf->vma->vm_page_prot,
1129                                                        TTM_BO_VM_NUM_PREFAULT);
1130                 drm_dev_exit(idx);
1131         } else {
1132                 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1133         }
1134         if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1135                 return ret;
1136
1137         dma_resv_unlock(tbo->base.resv);
1138         return ret;
1139 }
1140
1141 static const struct vm_operations_struct xe_gem_vm_ops = {
1142         .fault = xe_gem_fault,
1143         .open = ttm_bo_vm_open,
1144         .close = ttm_bo_vm_close,
1145         .access = ttm_bo_vm_access
1146 };
1147
1148 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
1149         .free = xe_gem_object_free,
1150         .close = xe_gem_object_close,
1151         .mmap = drm_gem_ttm_mmap,
1152         .export = xe_gem_prime_export,
1153         .vm_ops = &xe_gem_vm_ops,
1154 };
1155
1156 /**
1157  * xe_bo_alloc - Allocate storage for a struct xe_bo
1158  *
1159  * This funcition is intended to allocate storage to be used for input
1160  * to __xe_bo_create_locked(), in the case a pointer to the bo to be
1161  * created is needed before the call to __xe_bo_create_locked().
1162  * If __xe_bo_create_locked ends up never to be called, then the
1163  * storage allocated with this function needs to be freed using
1164  * xe_bo_free().
1165  *
1166  * Return: A pointer to an uninitialized struct xe_bo on success,
1167  * ERR_PTR(-ENOMEM) on error.
1168  */
1169 struct xe_bo *xe_bo_alloc(void)
1170 {
1171         struct xe_bo *bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1172
1173         if (!bo)
1174                 return ERR_PTR(-ENOMEM);
1175
1176         return bo;
1177 }
1178
1179 /**
1180  * xe_bo_free - Free storage allocated using xe_bo_alloc()
1181  * @bo: The buffer object storage.
1182  *
1183  * Refer to xe_bo_alloc() documentation for valid use-cases.
1184  */
1185 void xe_bo_free(struct xe_bo *bo)
1186 {
1187         kfree(bo);
1188 }
1189
1190 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
1191                                      struct xe_tile *tile, struct dma_resv *resv,
1192                                      struct ttm_lru_bulk_move *bulk, size_t size,
1193                                      u16 cpu_caching, enum ttm_bo_type type,
1194                                      u32 flags)
1195 {
1196         struct ttm_operation_ctx ctx = {
1197                 .interruptible = true,
1198                 .no_wait_gpu = false,
1199         };
1200         struct ttm_placement *placement;
1201         uint32_t alignment;
1202         size_t aligned_size;
1203         int err;
1204
1205         /* Only kernel objects should set GT */
1206         xe_assert(xe, !tile || type == ttm_bo_type_kernel);
1207
1208         if (XE_WARN_ON(!size)) {
1209                 xe_bo_free(bo);
1210                 return ERR_PTR(-EINVAL);
1211         }
1212
1213         if (flags & (XE_BO_CREATE_VRAM_MASK | XE_BO_CREATE_STOLEN_BIT) &&
1214             !(flags & XE_BO_CREATE_IGNORE_MIN_PAGE_SIZE_BIT) &&
1215             xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) {
1216                 aligned_size = ALIGN(size, SZ_64K);
1217                 if (type != ttm_bo_type_device)
1218                         size = ALIGN(size, SZ_64K);
1219                 flags |= XE_BO_INTERNAL_64K;
1220                 alignment = SZ_64K >> PAGE_SHIFT;
1221
1222         } else {
1223                 aligned_size = ALIGN(size, SZ_4K);
1224                 flags &= ~XE_BO_INTERNAL_64K;
1225                 alignment = SZ_4K >> PAGE_SHIFT;
1226         }
1227
1228         if (type == ttm_bo_type_device && aligned_size != size)
1229                 return ERR_PTR(-EINVAL);
1230
1231         if (!bo) {
1232                 bo = xe_bo_alloc();
1233                 if (IS_ERR(bo))
1234                         return bo;
1235         }
1236
1237         bo->tile = tile;
1238         bo->size = size;
1239         bo->flags = flags;
1240         bo->cpu_caching = cpu_caching;
1241         bo->ttm.base.funcs = &xe_gem_object_funcs;
1242         bo->props.preferred_mem_class = XE_BO_PROPS_INVALID;
1243         bo->props.preferred_gt = XE_BO_PROPS_INVALID;
1244         bo->props.preferred_mem_type = XE_BO_PROPS_INVALID;
1245         bo->ttm.priority = XE_BO_PRIORITY_NORMAL;
1246         INIT_LIST_HEAD(&bo->pinned_link);
1247 #ifdef CONFIG_PROC_FS
1248         INIT_LIST_HEAD(&bo->client_link);
1249 #endif
1250
1251         drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
1252
1253         if (resv) {
1254                 ctx.allow_res_evict = !(flags & XE_BO_CREATE_NO_RESV_EVICT);
1255                 ctx.resv = resv;
1256         }
1257
1258         if (!(flags & XE_BO_FIXED_PLACEMENT_BIT)) {
1259                 err = __xe_bo_placement_for_flags(xe, bo, bo->flags);
1260                 if (WARN_ON(err)) {
1261                         xe_ttm_bo_destroy(&bo->ttm);
1262                         return ERR_PTR(err);
1263                 }
1264         }
1265
1266         /* Defer populating type_sg bos */
1267         placement = (type == ttm_bo_type_sg ||
1268                      bo->flags & XE_BO_DEFER_BACKING) ? &sys_placement :
1269                 &bo->placement;
1270         err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type,
1271                                    placement, alignment,
1272                                    &ctx, NULL, resv, xe_ttm_bo_destroy);
1273         if (err)
1274                 return ERR_PTR(err);
1275
1276         /*
1277          * The VRAM pages underneath are potentially still being accessed by the
1278          * GPU, as per async GPU clearing and async evictions. However TTM makes
1279          * sure to add any corresponding move/clear fences into the objects
1280          * dma-resv using the DMA_RESV_USAGE_KERNEL slot.
1281          *
1282          * For KMD internal buffers we don't care about GPU clearing, however we
1283          * still need to handle async evictions, where the VRAM is still being
1284          * accessed by the GPU. Most internal callers are not expecting this,
1285          * since they are missing the required synchronisation before accessing
1286          * the memory. To keep things simple just sync wait any kernel fences
1287          * here, if the buffer is designated KMD internal.
1288          *
1289          * For normal userspace objects we should already have the required
1290          * pipelining or sync waiting elsewhere, since we already have to deal
1291          * with things like async GPU clearing.
1292          */
1293         if (type == ttm_bo_type_kernel) {
1294                 long timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
1295                                                      DMA_RESV_USAGE_KERNEL,
1296                                                      ctx.interruptible,
1297                                                      MAX_SCHEDULE_TIMEOUT);
1298
1299                 if (timeout < 0) {
1300                         if (!resv)
1301                                 dma_resv_unlock(bo->ttm.base.resv);
1302                         xe_bo_put(bo);
1303                         return ERR_PTR(timeout);
1304                 }
1305         }
1306
1307         bo->created = true;
1308         if (bulk)
1309                 ttm_bo_set_bulk_move(&bo->ttm, bulk);
1310         else
1311                 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1312
1313         return bo;
1314 }
1315
1316 static int __xe_bo_fixed_placement(struct xe_device *xe,
1317                                    struct xe_bo *bo,
1318                                    u32 flags,
1319                                    u64 start, u64 end, u64 size)
1320 {
1321         struct ttm_place *place = bo->placements;
1322
1323         if (flags & (XE_BO_CREATE_USER_BIT|XE_BO_CREATE_SYSTEM_BIT))
1324                 return -EINVAL;
1325
1326         place->flags = TTM_PL_FLAG_CONTIGUOUS;
1327         place->fpfn = start >> PAGE_SHIFT;
1328         place->lpfn = end >> PAGE_SHIFT;
1329
1330         switch (flags & (XE_BO_CREATE_STOLEN_BIT | XE_BO_CREATE_VRAM_MASK)) {
1331         case XE_BO_CREATE_VRAM0_BIT:
1332                 place->mem_type = XE_PL_VRAM0;
1333                 break;
1334         case XE_BO_CREATE_VRAM1_BIT:
1335                 place->mem_type = XE_PL_VRAM1;
1336                 break;
1337         case XE_BO_CREATE_STOLEN_BIT:
1338                 place->mem_type = XE_PL_STOLEN;
1339                 break;
1340
1341         default:
1342                 /* 0 or multiple of the above set */
1343                 return -EINVAL;
1344         }
1345
1346         bo->placement = (struct ttm_placement) {
1347                 .num_placement = 1,
1348                 .placement = place,
1349                 .num_busy_placement = 1,
1350                 .busy_placement = place,
1351         };
1352
1353         return 0;
1354 }
1355
1356 static struct xe_bo *
1357 __xe_bo_create_locked(struct xe_device *xe,
1358                       struct xe_tile *tile, struct xe_vm *vm,
1359                       size_t size, u64 start, u64 end,
1360                       u16 cpu_caching, enum ttm_bo_type type, u32 flags)
1361 {
1362         struct xe_bo *bo = NULL;
1363         int err;
1364
1365         if (vm)
1366                 xe_vm_assert_held(vm);
1367
1368         if (start || end != ~0ULL) {
1369                 bo = xe_bo_alloc();
1370                 if (IS_ERR(bo))
1371                         return bo;
1372
1373                 flags |= XE_BO_FIXED_PLACEMENT_BIT;
1374                 err = __xe_bo_fixed_placement(xe, bo, flags, start, end, size);
1375                 if (err) {
1376                         xe_bo_free(bo);
1377                         return ERR_PTR(err);
1378                 }
1379         }
1380
1381         bo = ___xe_bo_create_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL,
1382                                     vm && !xe_vm_in_fault_mode(vm) &&
1383                                     flags & XE_BO_CREATE_USER_BIT ?
1384                                     &vm->lru_bulk_move : NULL, size,
1385                                     cpu_caching, type, flags);
1386         if (IS_ERR(bo))
1387                 return bo;
1388
1389         /*
1390          * Note that instead of taking a reference no the drm_gpuvm_resv_bo(),
1391          * to ensure the shared resv doesn't disappear under the bo, the bo
1392          * will keep a reference to the vm, and avoid circular references
1393          * by having all the vm's bo refereferences released at vm close
1394          * time.
1395          */
1396         if (vm && xe_bo_is_user(bo))
1397                 xe_vm_get(vm);
1398         bo->vm = vm;
1399
1400         if (bo->flags & XE_BO_CREATE_GGTT_BIT) {
1401                 if (!tile && flags & XE_BO_CREATE_STOLEN_BIT)
1402                         tile = xe_device_get_root_tile(xe);
1403
1404                 xe_assert(xe, tile);
1405
1406                 if (flags & XE_BO_FIXED_PLACEMENT_BIT) {
1407                         err = xe_ggtt_insert_bo_at(tile->mem.ggtt, bo,
1408                                                    start + bo->size, U64_MAX);
1409                 } else {
1410                         err = xe_ggtt_insert_bo(tile->mem.ggtt, bo);
1411                 }
1412                 if (err)
1413                         goto err_unlock_put_bo;
1414         }
1415
1416         return bo;
1417
1418 err_unlock_put_bo:
1419         __xe_bo_unset_bulk_move(bo);
1420         xe_bo_unlock_vm_held(bo);
1421         xe_bo_put(bo);
1422         return ERR_PTR(err);
1423 }
1424
1425 struct xe_bo *
1426 xe_bo_create_locked_range(struct xe_device *xe,
1427                           struct xe_tile *tile, struct xe_vm *vm,
1428                           size_t size, u64 start, u64 end,
1429                           enum ttm_bo_type type, u32 flags)
1430 {
1431         return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type, flags);
1432 }
1433
1434 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
1435                                   struct xe_vm *vm, size_t size,
1436                                   enum ttm_bo_type type, u32 flags)
1437 {
1438         return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type, flags);
1439 }
1440
1441 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
1442                                 struct xe_vm *vm, size_t size,
1443                                 u16 cpu_caching,
1444                                 enum ttm_bo_type type,
1445                                 u32 flags)
1446 {
1447         struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
1448                                                  cpu_caching, type,
1449                                                  flags | XE_BO_CREATE_USER_BIT);
1450         if (!IS_ERR(bo))
1451                 xe_bo_unlock_vm_held(bo);
1452
1453         return bo;
1454 }
1455
1456 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
1457                            struct xe_vm *vm, size_t size,
1458                            enum ttm_bo_type type, u32 flags)
1459 {
1460         struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags);
1461
1462         if (!IS_ERR(bo))
1463                 xe_bo_unlock_vm_held(bo);
1464
1465         return bo;
1466 }
1467
1468 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
1469                                       struct xe_vm *vm,
1470                                       size_t size, u64 offset,
1471                                       enum ttm_bo_type type, u32 flags)
1472 {
1473         struct xe_bo *bo;
1474         int err;
1475         u64 start = offset == ~0ull ? 0 : offset;
1476         u64 end = offset == ~0ull ? offset : start + size;
1477
1478         if (flags & XE_BO_CREATE_STOLEN_BIT &&
1479             xe_ttm_stolen_cpu_access_needs_ggtt(xe))
1480                 flags |= XE_BO_CREATE_GGTT_BIT;
1481
1482         bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type,
1483                                        flags | XE_BO_NEEDS_CPU_ACCESS);
1484         if (IS_ERR(bo))
1485                 return bo;
1486
1487         err = xe_bo_pin(bo);
1488         if (err)
1489                 goto err_put;
1490
1491         err = xe_bo_vmap(bo);
1492         if (err)
1493                 goto err_unpin;
1494
1495         xe_bo_unlock_vm_held(bo);
1496
1497         return bo;
1498
1499 err_unpin:
1500         xe_bo_unpin(bo);
1501 err_put:
1502         xe_bo_unlock_vm_held(bo);
1503         xe_bo_put(bo);
1504         return ERR_PTR(err);
1505 }
1506
1507 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1508                                    struct xe_vm *vm, size_t size,
1509                                    enum ttm_bo_type type, u32 flags)
1510 {
1511         return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags);
1512 }
1513
1514 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1515                                      const void *data, size_t size,
1516                                      enum ttm_bo_type type, u32 flags)
1517 {
1518         struct xe_bo *bo = xe_bo_create_pin_map(xe, tile, NULL,
1519                                                 ALIGN(size, PAGE_SIZE),
1520                                                 type, flags);
1521         if (IS_ERR(bo))
1522                 return bo;
1523
1524         xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1525
1526         return bo;
1527 }
1528
1529 static void __xe_bo_unpin_map_no_vm(struct drm_device *drm, void *arg)
1530 {
1531         xe_bo_unpin_map_no_vm(arg);
1532 }
1533
1534 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
1535                                            size_t size, u32 flags)
1536 {
1537         struct xe_bo *bo;
1538         int ret;
1539
1540         bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, flags);
1541         if (IS_ERR(bo))
1542                 return bo;
1543
1544         ret = drmm_add_action_or_reset(&xe->drm, __xe_bo_unpin_map_no_vm, bo);
1545         if (ret)
1546                 return ERR_PTR(ret);
1547
1548         return bo;
1549 }
1550
1551 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
1552                                              const void *data, size_t size, u32 flags)
1553 {
1554         struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
1555
1556         if (IS_ERR(bo))
1557                 return bo;
1558
1559         xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
1560
1561         return bo;
1562 }
1563
1564 /*
1565  * XXX: This is in the VM bind data path, likely should calculate this once and
1566  * store, with a recalculation if the BO is moved.
1567  */
1568 uint64_t vram_region_gpu_offset(struct ttm_resource *res)
1569 {
1570         struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
1571
1572         if (res->mem_type == XE_PL_STOLEN)
1573                 return xe_ttm_stolen_gpu_offset(xe);
1574
1575         return res_to_mem_region(res)->dpa_base;
1576 }
1577
1578 /**
1579  * xe_bo_pin_external - pin an external BO
1580  * @bo: buffer object to be pinned
1581  *
1582  * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD)
1583  * BO. Unique call compared to xe_bo_pin as this function has it own set of
1584  * asserts and code to ensure evict / restore on suspend / resume.
1585  *
1586  * Returns 0 for success, negative error code otherwise.
1587  */
1588 int xe_bo_pin_external(struct xe_bo *bo)
1589 {
1590         struct xe_device *xe = xe_bo_device(bo);
1591         int err;
1592
1593         xe_assert(xe, !bo->vm);
1594         xe_assert(xe, xe_bo_is_user(bo));
1595
1596         if (!xe_bo_is_pinned(bo)) {
1597                 err = xe_bo_validate(bo, NULL, false);
1598                 if (err)
1599                         return err;
1600
1601                 if (xe_bo_is_vram(bo)) {
1602                         spin_lock(&xe->pinned.lock);
1603                         list_add_tail(&bo->pinned_link,
1604                                       &xe->pinned.external_vram);
1605                         spin_unlock(&xe->pinned.lock);
1606                 }
1607         }
1608
1609         ttm_bo_pin(&bo->ttm);
1610
1611         /*
1612          * FIXME: If we always use the reserve / unreserve functions for locking
1613          * we do not need this.
1614          */
1615         ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1616
1617         return 0;
1618 }
1619
1620 int xe_bo_pin(struct xe_bo *bo)
1621 {
1622         struct xe_device *xe = xe_bo_device(bo);
1623         int err;
1624
1625         /* We currently don't expect user BO to be pinned */
1626         xe_assert(xe, !xe_bo_is_user(bo));
1627
1628         /* Pinned object must be in GGTT or have pinned flag */
1629         xe_assert(xe, bo->flags & (XE_BO_CREATE_PINNED_BIT |
1630                                    XE_BO_CREATE_GGTT_BIT));
1631
1632         /*
1633          * No reason we can't support pinning imported dma-bufs we just don't
1634          * expect to pin an imported dma-buf.
1635          */
1636         xe_assert(xe, !bo->ttm.base.import_attach);
1637
1638         /* We only expect at most 1 pin */
1639         xe_assert(xe, !xe_bo_is_pinned(bo));
1640
1641         err = xe_bo_validate(bo, NULL, false);
1642         if (err)
1643                 return err;
1644
1645         /*
1646          * For pinned objects in on DGFX, which are also in vram, we expect
1647          * these to be in contiguous VRAM memory. Required eviction / restore
1648          * during suspend / resume (force restore to same physical address).
1649          */
1650         if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
1651             bo->flags & XE_BO_INTERNAL_TEST)) {
1652                 struct ttm_place *place = &(bo->placements[0]);
1653
1654                 if (mem_type_is_vram(place->mem_type)) {
1655                         xe_assert(xe, place->flags & TTM_PL_FLAG_CONTIGUOUS);
1656
1657                         place->fpfn = (xe_bo_addr(bo, 0, PAGE_SIZE) -
1658                                        vram_region_gpu_offset(bo->ttm.resource)) >> PAGE_SHIFT;
1659                         place->lpfn = place->fpfn + (bo->size >> PAGE_SHIFT);
1660
1661                         spin_lock(&xe->pinned.lock);
1662                         list_add_tail(&bo->pinned_link, &xe->pinned.kernel_bo_present);
1663                         spin_unlock(&xe->pinned.lock);
1664                 }
1665         }
1666
1667         ttm_bo_pin(&bo->ttm);
1668
1669         /*
1670          * FIXME: If we always use the reserve / unreserve functions for locking
1671          * we do not need this.
1672          */
1673         ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1674
1675         return 0;
1676 }
1677
1678 /**
1679  * xe_bo_unpin_external - unpin an external BO
1680  * @bo: buffer object to be unpinned
1681  *
1682  * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD)
1683  * BO. Unique call compared to xe_bo_unpin as this function has it own set of
1684  * asserts and code to ensure evict / restore on suspend / resume.
1685  *
1686  * Returns 0 for success, negative error code otherwise.
1687  */
1688 void xe_bo_unpin_external(struct xe_bo *bo)
1689 {
1690         struct xe_device *xe = xe_bo_device(bo);
1691
1692         xe_assert(xe, !bo->vm);
1693         xe_assert(xe, xe_bo_is_pinned(bo));
1694         xe_assert(xe, xe_bo_is_user(bo));
1695
1696         if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link)) {
1697                 spin_lock(&xe->pinned.lock);
1698                 list_del_init(&bo->pinned_link);
1699                 spin_unlock(&xe->pinned.lock);
1700         }
1701
1702         ttm_bo_unpin(&bo->ttm);
1703
1704         /*
1705          * FIXME: If we always use the reserve / unreserve functions for locking
1706          * we do not need this.
1707          */
1708         ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
1709 }
1710
1711 void xe_bo_unpin(struct xe_bo *bo)
1712 {
1713         struct xe_device *xe = xe_bo_device(bo);
1714
1715         xe_assert(xe, !bo->ttm.base.import_attach);
1716         xe_assert(xe, xe_bo_is_pinned(bo));
1717
1718         if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) &&
1719             bo->flags & XE_BO_INTERNAL_TEST)) {
1720                 struct ttm_place *place = &(bo->placements[0]);
1721
1722                 if (mem_type_is_vram(place->mem_type)) {
1723                         xe_assert(xe, !list_empty(&bo->pinned_link));
1724
1725                         spin_lock(&xe->pinned.lock);
1726                         list_del_init(&bo->pinned_link);
1727                         spin_unlock(&xe->pinned.lock);
1728                 }
1729         }
1730
1731         ttm_bo_unpin(&bo->ttm);
1732 }
1733
1734 /**
1735  * xe_bo_validate() - Make sure the bo is in an allowed placement
1736  * @bo: The bo,
1737  * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or
1738  *      NULL. Used together with @allow_res_evict.
1739  * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's
1740  *                   reservation object.
1741  *
1742  * Make sure the bo is in allowed placement, migrating it if necessary. If
1743  * needed, other bos will be evicted. If bos selected for eviction shares
1744  * the @vm's reservation object, they can be evicted iff @allow_res_evict is
1745  * set to true, otherwise they will be bypassed.
1746  *
1747  * Return: 0 on success, negative error code on failure. May return
1748  * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal.
1749  */
1750 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict)
1751 {
1752         struct ttm_operation_ctx ctx = {
1753                 .interruptible = true,
1754                 .no_wait_gpu = false,
1755         };
1756
1757         if (vm) {
1758                 lockdep_assert_held(&vm->lock);
1759                 xe_vm_assert_held(vm);
1760
1761                 ctx.allow_res_evict = allow_res_evict;
1762                 ctx.resv = xe_vm_resv(vm);
1763         }
1764
1765         return ttm_bo_validate(&bo->ttm, &bo->placement, &ctx);
1766 }
1767
1768 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo)
1769 {
1770         if (bo->destroy == &xe_ttm_bo_destroy)
1771                 return true;
1772
1773         return false;
1774 }
1775
1776 /*
1777  * Resolve a BO address. There is no assert to check if the proper lock is held
1778  * so it should only be used in cases where it is not fatal to get the wrong
1779  * address, such as printing debug information, but not in cases where memory is
1780  * written based on this result.
1781  */
1782 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
1783 {
1784         struct xe_device *xe = xe_bo_device(bo);
1785         struct xe_res_cursor cur;
1786         u64 page;
1787
1788         xe_assert(xe, page_size <= PAGE_SIZE);
1789         page = offset >> PAGE_SHIFT;
1790         offset &= (PAGE_SIZE - 1);
1791
1792         if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
1793                 xe_assert(xe, bo->ttm.ttm);
1794
1795                 xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
1796                                 page_size, &cur);
1797                 return xe_res_dma(&cur) + offset;
1798         } else {
1799                 struct xe_res_cursor cur;
1800
1801                 xe_res_first(bo->ttm.resource, page << PAGE_SHIFT,
1802                              page_size, &cur);
1803                 return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource);
1804         }
1805 }
1806
1807 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
1808 {
1809         if (!READ_ONCE(bo->ttm.pin_count))
1810                 xe_bo_assert_held(bo);
1811         return __xe_bo_addr(bo, offset, page_size);
1812 }
1813
1814 int xe_bo_vmap(struct xe_bo *bo)
1815 {
1816         void *virtual;
1817         bool is_iomem;
1818         int ret;
1819
1820         xe_bo_assert_held(bo);
1821
1822         if (!(bo->flags & XE_BO_NEEDS_CPU_ACCESS))
1823                 return -EINVAL;
1824
1825         if (!iosys_map_is_null(&bo->vmap))
1826                 return 0;
1827
1828         /*
1829          * We use this more or less deprecated interface for now since
1830          * ttm_bo_vmap() doesn't offer the optimization of kmapping
1831          * single page bos, which is done here.
1832          * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap
1833          * to use struct iosys_map.
1834          */
1835         ret = ttm_bo_kmap(&bo->ttm, 0, bo->size >> PAGE_SHIFT, &bo->kmap);
1836         if (ret)
1837                 return ret;
1838
1839         virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
1840         if (is_iomem)
1841                 iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual);
1842         else
1843                 iosys_map_set_vaddr(&bo->vmap, virtual);
1844
1845         return 0;
1846 }
1847
1848 static void __xe_bo_vunmap(struct xe_bo *bo)
1849 {
1850         if (!iosys_map_is_null(&bo->vmap)) {
1851                 iosys_map_clear(&bo->vmap);
1852                 ttm_bo_kunmap(&bo->kmap);
1853         }
1854 }
1855
1856 void xe_bo_vunmap(struct xe_bo *bo)
1857 {
1858         xe_bo_assert_held(bo);
1859         __xe_bo_vunmap(bo);
1860 }
1861
1862 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
1863                         struct drm_file *file)
1864 {
1865         struct xe_device *xe = to_xe_device(dev);
1866         struct xe_file *xef = to_xe_file(file);
1867         struct drm_xe_gem_create *args = data;
1868         struct xe_vm *vm = NULL;
1869         struct xe_bo *bo;
1870         unsigned int bo_flags;
1871         u32 handle;
1872         int err;
1873
1874         if (XE_IOCTL_DBG(xe, args->extensions) ||
1875             XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
1876             XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1877                 return -EINVAL;
1878
1879         /* at least one valid memory placement must be specified */
1880         if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) ||
1881                          !args->placement))
1882                 return -EINVAL;
1883
1884         if (XE_IOCTL_DBG(xe, args->flags &
1885                          ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING |
1886                            DRM_XE_GEM_CREATE_FLAG_SCANOUT |
1887                            DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM)))
1888                 return -EINVAL;
1889
1890         if (XE_IOCTL_DBG(xe, args->handle))
1891                 return -EINVAL;
1892
1893         if (XE_IOCTL_DBG(xe, !args->size))
1894                 return -EINVAL;
1895
1896         if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX))
1897                 return -EINVAL;
1898
1899         if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
1900                 return -EINVAL;
1901
1902         bo_flags = 0;
1903         if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING)
1904                 bo_flags |= XE_BO_DEFER_BACKING;
1905
1906         if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT)
1907                 bo_flags |= XE_BO_SCANOUT_BIT;
1908
1909         bo_flags |= args->placement << (ffs(XE_BO_CREATE_SYSTEM_BIT) - 1);
1910
1911         if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) {
1912                 if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_CREATE_VRAM_MASK)))
1913                         return -EINVAL;
1914
1915                 bo_flags |= XE_BO_NEEDS_CPU_ACCESS;
1916         }
1917
1918         if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
1919                          args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC))
1920                 return -EINVAL;
1921
1922         if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_CREATE_VRAM_MASK &&
1923                          args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC))
1924                 return -EINVAL;
1925
1926         if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_SCANOUT_BIT &&
1927                          args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB))
1928                 return -EINVAL;
1929
1930         if (args->vm_id) {
1931                 vm = xe_vm_lookup(xef, args->vm_id);
1932                 if (XE_IOCTL_DBG(xe, !vm))
1933                         return -ENOENT;
1934                 err = xe_vm_lock(vm, true);
1935                 if (err)
1936                         goto out_vm;
1937         }
1938
1939         bo = xe_bo_create_user(xe, NULL, vm, args->size, args->cpu_caching,
1940                                ttm_bo_type_device, bo_flags);
1941
1942         if (vm)
1943                 xe_vm_unlock(vm);
1944
1945         if (IS_ERR(bo)) {
1946                 err = PTR_ERR(bo);
1947                 goto out_vm;
1948         }
1949
1950         err = drm_gem_handle_create(file, &bo->ttm.base, &handle);
1951         if (err)
1952                 goto out_bulk;
1953
1954         args->handle = handle;
1955         goto out_put;
1956
1957 out_bulk:
1958         if (vm && !xe_vm_in_fault_mode(vm)) {
1959                 xe_vm_lock(vm, false);
1960                 __xe_bo_unset_bulk_move(bo);
1961                 xe_vm_unlock(vm);
1962         }
1963 out_put:
1964         xe_bo_put(bo);
1965 out_vm:
1966         if (vm)
1967                 xe_vm_put(vm);
1968
1969         return err;
1970 }
1971
1972 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
1973                              struct drm_file *file)
1974 {
1975         struct xe_device *xe = to_xe_device(dev);
1976         struct drm_xe_gem_mmap_offset *args = data;
1977         struct drm_gem_object *gem_obj;
1978
1979         if (XE_IOCTL_DBG(xe, args->extensions) ||
1980             XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1981                 return -EINVAL;
1982
1983         if (XE_IOCTL_DBG(xe, args->flags))
1984                 return -EINVAL;
1985
1986         gem_obj = drm_gem_object_lookup(file, args->handle);
1987         if (XE_IOCTL_DBG(xe, !gem_obj))
1988                 return -ENOENT;
1989
1990         /* The mmap offset was set up at BO allocation time. */
1991         args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
1992
1993         xe_bo_put(gem_to_xe_bo(gem_obj));
1994         return 0;
1995 }
1996
1997 /**
1998  * xe_bo_lock() - Lock the buffer object's dma_resv object
1999  * @bo: The struct xe_bo whose lock is to be taken
2000  * @intr: Whether to perform any wait interruptible
2001  *
2002  * Locks the buffer object's dma_resv object. If the buffer object is
2003  * pointing to a shared dma_resv object, that shared lock is locked.
2004  *
2005  * Return: 0 on success, -EINTR if @intr is true and the wait for a
2006  * contended lock was interrupted. If @intr is set to false, the
2007  * function always returns 0.
2008  */
2009 int xe_bo_lock(struct xe_bo *bo, bool intr)
2010 {
2011         if (intr)
2012                 return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL);
2013
2014         dma_resv_lock(bo->ttm.base.resv, NULL);
2015
2016         return 0;
2017 }
2018
2019 /**
2020  * xe_bo_unlock() - Unlock the buffer object's dma_resv object
2021  * @bo: The struct xe_bo whose lock is to be released.
2022  *
2023  * Unlock a buffer object lock that was locked by xe_bo_lock().
2024  */
2025 void xe_bo_unlock(struct xe_bo *bo)
2026 {
2027         dma_resv_unlock(bo->ttm.base.resv);
2028 }
2029
2030 /**
2031  * xe_bo_can_migrate - Whether a buffer object likely can be migrated
2032  * @bo: The buffer object to migrate
2033  * @mem_type: The TTM memory type intended to migrate to
2034  *
2035  * Check whether the buffer object supports migration to the
2036  * given memory type. Note that pinning may affect the ability to migrate as
2037  * returned by this function.
2038  *
2039  * This function is primarily intended as a helper for checking the
2040  * possibility to migrate buffer objects and can be called without
2041  * the object lock held.
2042  *
2043  * Return: true if migration is possible, false otherwise.
2044  */
2045 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type)
2046 {
2047         unsigned int cur_place;
2048
2049         if (bo->ttm.type == ttm_bo_type_kernel)
2050                 return true;
2051
2052         if (bo->ttm.type == ttm_bo_type_sg)
2053                 return false;
2054
2055         for (cur_place = 0; cur_place < bo->placement.num_placement;
2056              cur_place++) {
2057                 if (bo->placements[cur_place].mem_type == mem_type)
2058                         return true;
2059         }
2060
2061         return false;
2062 }
2063
2064 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
2065 {
2066         memset(place, 0, sizeof(*place));
2067         place->mem_type = mem_type;
2068 }
2069
2070 /**
2071  * xe_bo_migrate - Migrate an object to the desired region id
2072  * @bo: The buffer object to migrate.
2073  * @mem_type: The TTM region type to migrate to.
2074  *
2075  * Attempt to migrate the buffer object to the desired memory region. The
2076  * buffer object may not be pinned, and must be locked.
2077  * On successful completion, the object memory type will be updated,
2078  * but an async migration task may not have completed yet, and to
2079  * accomplish that, the object's kernel fences must be signaled with
2080  * the object lock held.
2081  *
2082  * Return: 0 on success. Negative error code on failure. In particular may
2083  * return -EINTR or -ERESTARTSYS if signal pending.
2084  */
2085 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type)
2086 {
2087         struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
2088         struct ttm_operation_ctx ctx = {
2089                 .interruptible = true,
2090                 .no_wait_gpu = false,
2091         };
2092         struct ttm_placement placement;
2093         struct ttm_place requested;
2094
2095         xe_bo_assert_held(bo);
2096
2097         if (bo->ttm.resource->mem_type == mem_type)
2098                 return 0;
2099
2100         if (xe_bo_is_pinned(bo))
2101                 return -EBUSY;
2102
2103         if (!xe_bo_can_migrate(bo, mem_type))
2104                 return -EINVAL;
2105
2106         xe_place_from_ttm_type(mem_type, &requested);
2107         placement.num_placement = 1;
2108         placement.num_busy_placement = 1;
2109         placement.placement = &requested;
2110         placement.busy_placement = &requested;
2111
2112         /*
2113          * Stolen needs to be handled like below VRAM handling if we ever need
2114          * to support it.
2115          */
2116         drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN);
2117
2118         if (mem_type_is_vram(mem_type)) {
2119                 u32 c = 0;
2120
2121                 add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
2122         }
2123
2124         return ttm_bo_validate(&bo->ttm, &placement, &ctx);
2125 }
2126
2127 /**
2128  * xe_bo_evict - Evict an object to evict placement
2129  * @bo: The buffer object to migrate.
2130  * @force_alloc: Set force_alloc in ttm_operation_ctx
2131  *
2132  * On successful completion, the object memory will be moved to evict
2133  * placement. Ths function blocks until the object has been fully moved.
2134  *
2135  * Return: 0 on success. Negative error code on failure.
2136  */
2137 int xe_bo_evict(struct xe_bo *bo, bool force_alloc)
2138 {
2139         struct ttm_operation_ctx ctx = {
2140                 .interruptible = false,
2141                 .no_wait_gpu = false,
2142                 .force_alloc = force_alloc,
2143         };
2144         struct ttm_placement placement;
2145         int ret;
2146
2147         xe_evict_flags(&bo->ttm, &placement);
2148         ret = ttm_bo_validate(&bo->ttm, &placement, &ctx);
2149         if (ret)
2150                 return ret;
2151
2152         dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
2153                               false, MAX_SCHEDULE_TIMEOUT);
2154
2155         return 0;
2156 }
2157
2158 /**
2159  * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when
2160  * placed in system memory.
2161  * @bo: The xe_bo
2162  *
2163  * Return: true if extra pages need to be allocated, false otherwise.
2164  */
2165 bool xe_bo_needs_ccs_pages(struct xe_bo *bo)
2166 {
2167         struct xe_device *xe = xe_bo_device(bo);
2168
2169         if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device)
2170                 return false;
2171
2172         /* On discrete GPUs, if the GPU can access this buffer from
2173          * system memory (i.e., it allows XE_PL_TT placement), FlatCCS
2174          * can't be used since there's no CCS storage associated with
2175          * non-VRAM addresses.
2176          */
2177         if (IS_DGFX(xe) && (bo->flags & XE_BO_CREATE_SYSTEM_BIT))
2178                 return false;
2179
2180         return true;
2181 }
2182
2183 /**
2184  * __xe_bo_release_dummy() - Dummy kref release function
2185  * @kref: The embedded struct kref.
2186  *
2187  * Dummy release function for xe_bo_put_deferred(). Keep off.
2188  */
2189 void __xe_bo_release_dummy(struct kref *kref)
2190 {
2191 }
2192
2193 /**
2194  * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred().
2195  * @deferred: The lockless list used for the call to xe_bo_put_deferred().
2196  *
2197  * Puts all bos whose put was deferred by xe_bo_put_deferred().
2198  * The @deferred list can be either an onstack local list or a global
2199  * shared list used by a workqueue.
2200  */
2201 void xe_bo_put_commit(struct llist_head *deferred)
2202 {
2203         struct llist_node *freed;
2204         struct xe_bo *bo, *next;
2205
2206         if (!deferred)
2207                 return;
2208
2209         freed = llist_del_all(deferred);
2210         if (!freed)
2211                 return;
2212
2213         llist_for_each_entry_safe(bo, next, freed, freed)
2214                 drm_gem_object_free(&bo->ttm.base.refcount);
2215 }
2216
2217 /**
2218  * xe_bo_dumb_create - Create a dumb bo as backing for a fb
2219  * @file_priv: ...
2220  * @dev: ...
2221  * @args: ...
2222  *
2223  * See dumb_create() hook in include/drm/drm_drv.h
2224  *
2225  * Return: ...
2226  */
2227 int xe_bo_dumb_create(struct drm_file *file_priv,
2228                       struct drm_device *dev,
2229                       struct drm_mode_create_dumb *args)
2230 {
2231         struct xe_device *xe = to_xe_device(dev);
2232         struct xe_bo *bo;
2233         uint32_t handle;
2234         int cpp = DIV_ROUND_UP(args->bpp, 8);
2235         int err;
2236         u32 page_size = max_t(u32, PAGE_SIZE,
2237                 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K);
2238
2239         args->pitch = ALIGN(args->width * cpp, 64);
2240         args->size = ALIGN(mul_u32_u32(args->pitch, args->height),
2241                            page_size);
2242
2243         bo = xe_bo_create_user(xe, NULL, NULL, args->size,
2244                                DRM_XE_GEM_CPU_CACHING_WC,
2245                                ttm_bo_type_device,
2246                                XE_BO_CREATE_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
2247                                XE_BO_CREATE_USER_BIT | XE_BO_SCANOUT_BIT |
2248                                XE_BO_NEEDS_CPU_ACCESS);
2249         if (IS_ERR(bo))
2250                 return PTR_ERR(bo);
2251
2252         err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle);
2253         /* drop reference from allocate - handle holds it now */
2254         drm_gem_object_put(&bo->ttm.base);
2255         if (!err)
2256                 args->handle = handle;
2257         return err;
2258 }
2259
2260 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
2261 #include "tests/xe_bo.c"
2262 #endif