1 /* $NetBSD: i915_gem_pages.c,v 1.6 2021/12/19 12:00:57 riastradh Exp $ */
2
3 /*
4 * SPDX-License-Identifier: MIT
5 *
6 * Copyright © 2014-2016 Intel Corporation
7 */
8
9 #include <sys/cdefs.h>
10 __KERNEL_RCSID(0, "$NetBSD: i915_gem_pages.c,v 1.6 2021/12/19 12:00:57 riastradh Exp $");
11
12 #include "i915_drv.h"
13 #include "i915_gem_object.h"
14 #include "i915_scatterlist.h"
15 #include "i915_gem_lmem.h"
16 #include "i915_gem_mman.h"
17
18 #ifdef __NetBSD__
19 #include <sys/param.h>
20 #include <uvm/uvm_extern.h>
21 #endif
22
__i915_gem_object_set_pages(struct drm_i915_gem_object * obj,struct sg_table * pages,unsigned int sg_page_sizes)23 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
24 struct sg_table *pages,
25 unsigned int sg_page_sizes)
26 {
27 struct drm_i915_private *i915 = to_i915(obj->base.dev);
28 unsigned long supported = INTEL_INFO(i915)->page_sizes;
29 int i;
30
31 lockdep_assert_held(&obj->mm.lock);
32
33 if (i915_gem_object_is_volatile(obj))
34 obj->mm.madv = I915_MADV_DONTNEED;
35
36 /* Make the pages coherent with the GPU (flushing any swapin). */
37 if (obj->cache_dirty) {
38 obj->write_domain = 0;
39 if (i915_gem_object_has_struct_page(obj))
40 drm_clflush_sg(pages);
41 obj->cache_dirty = false;
42 }
43
44 #ifndef __NetBSD__
45 obj->mm.get_page.sg_pos = pages->sgl;
46 obj->mm.get_page.sg_idx = 0;
47 #endif
48
49 obj->mm.pages = pages;
50
51 if (i915_gem_object_is_tiled(obj) &&
52 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
53 GEM_BUG_ON(obj->mm.quirked);
54 __i915_gem_object_pin_pages(obj);
55 obj->mm.quirked = true;
56 }
57
58 GEM_BUG_ON(!sg_page_sizes);
59 obj->mm.page_sizes.phys = sg_page_sizes;
60
61 /*
62 * Calculate the supported page-sizes which fit into the given
63 * sg_page_sizes. This will give us the page-sizes which we may be able
64 * to use opportunistically when later inserting into the GTT. For
65 * example if phys=2G, then in theory we should be able to use 1G, 2M,
66 * 64K or 4K pages, although in practice this will depend on a number of
67 * other factors.
68 */
69 obj->mm.page_sizes.sg = 0;
70 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
71 if (obj->mm.page_sizes.phys & ~0u << i)
72 obj->mm.page_sizes.sg |= BIT(i);
73 }
74 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
75
76 if (i915_gem_object_is_shrinkable(obj)) {
77 struct list_head *list;
78 unsigned long flags;
79
80 spin_lock_irqsave(&i915->mm.obj_lock, flags);
81
82 i915->mm.shrink_count++;
83 i915->mm.shrink_memory += obj->base.size;
84
85 if (obj->mm.madv != I915_MADV_WILLNEED)
86 list = &i915->mm.purge_list;
87 else
88 list = &i915->mm.shrink_list;
89 list_add_tail(&obj->mm.link, list);
90
91 atomic_set(&obj->mm.shrink_pin, 0);
92 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
93 }
94 }
95
____i915_gem_object_get_pages(struct drm_i915_gem_object * obj)96 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
97 {
98 int err;
99
100 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
101 DRM_DEBUG("Attempting to obtain a purgeable object\n");
102 return -EFAULT;
103 }
104
105 err = obj->ops->get_pages(obj);
106 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
107
108 return err;
109 }
110
111 /* Ensure that the associated pages are gathered from the backing storage
112 * and pinned into our object. i915_gem_object_pin_pages() may be called
113 * multiple times before they are released by a single call to
114 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
115 * either as a result of memory pressure (reaping pages under the shrinker)
116 * or as the object is itself released.
117 */
__i915_gem_object_get_pages(struct drm_i915_gem_object * obj)118 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
119 {
120 int err;
121
122 err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES);
123 if (err)
124 return err;
125
126 if (unlikely(!i915_gem_object_has_pages(obj))) {
127 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
128
129 err = ____i915_gem_object_get_pages(obj);
130 if (err)
131 goto unlock;
132
133 smp_mb__before_atomic();
134 }
135 atomic_inc(&obj->mm.pages_pin_count);
136
137 unlock:
138 mutex_unlock(&obj->mm.lock);
139 return err;
140 }
141
142 /* Immediately discard the backing storage */
i915_gem_object_truncate(struct drm_i915_gem_object * obj)143 void i915_gem_object_truncate(struct drm_i915_gem_object *obj)
144 {
145 drm_gem_free_mmap_offset(&obj->base);
146 if (obj->ops->truncate)
147 obj->ops->truncate(obj);
148 }
149
150 /* Try to discard unwanted pages */
i915_gem_object_writeback(struct drm_i915_gem_object * obj)151 void i915_gem_object_writeback(struct drm_i915_gem_object *obj)
152 {
153 lockdep_assert_held(&obj->mm.lock);
154 GEM_BUG_ON(i915_gem_object_has_pages(obj));
155
156 if (obj->ops->writeback)
157 obj->ops->writeback(obj);
158 }
159
__i915_gem_object_reset_page_iter(struct drm_i915_gem_object * obj)160 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
161 {
162 struct radix_tree_iter iter;
163 void __rcu **slot;
164
165 rcu_read_lock();
166 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
167 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
168 rcu_read_unlock();
169 }
170
unmap_object(struct drm_i915_gem_object * obj,void * ptr)171 static void unmap_object(struct drm_i915_gem_object *obj, void *ptr)
172 {
173 #ifdef __NetBSD__
174 pmap_kremove((vaddr_t)ptr, obj->base.size);
175 pmap_update(pmap_kernel());
176 uvm_km_free(kernel_map, (vaddr_t)ptr, obj->base.size, UVM_KMF_VAONLY);
177 #else
178 if (is_vmalloc_addr(ptr))
179 vunmap(ptr);
180 else
181 kunmap(kmap_to_page(ptr));
182 #endif
183 }
184
185 struct sg_table *
__i915_gem_object_unset_pages(struct drm_i915_gem_object * obj)186 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
187 {
188 struct sg_table *pages;
189
190 pages = fetch_and_zero(&obj->mm.pages);
191 if (IS_ERR_OR_NULL(pages))
192 return pages;
193
194 if (i915_gem_object_is_volatile(obj))
195 obj->mm.madv = I915_MADV_WILLNEED;
196
197 i915_gem_object_make_unshrinkable(obj);
198
199 if (obj->mm.mapping) {
200 unmap_object(obj, page_mask_bits(obj->mm.mapping));
201 obj->mm.mapping = NULL;
202 }
203
204 __i915_gem_object_reset_page_iter(obj);
205 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
206
207 return pages;
208 }
209
__i915_gem_object_put_pages(struct drm_i915_gem_object * obj)210 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
211 {
212 struct sg_table *pages;
213 int err;
214
215 if (i915_gem_object_has_pinned_pages(obj))
216 return -EBUSY;
217
218 GEM_BUG_ON(atomic_read(&obj->bind_count));
219
220 /* May be called by shrinker from within get_pages() (on another bo) */
221 mutex_lock(&obj->mm.lock);
222 if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
223 err = -EBUSY;
224 goto unlock;
225 }
226
227 i915_gem_object_release_mmap_offset(obj);
228
229 /*
230 * ->put_pages might need to allocate memory for the bit17 swizzle
231 * array, hence protect them from being reaped by removing them from gtt
232 * lists early.
233 */
234 pages = __i915_gem_object_unset_pages(obj);
235
236 /*
237 * XXX Temporary hijinx to avoid updating all backends to handle
238 * NULL pages. In the future, when we have more asynchronous
239 * get_pages backends we should be better able to handle the
240 * cancellation of the async task in a more uniform manner.
241 */
242 if (!pages && !i915_gem_object_needs_async_cancel(obj))
243 pages = ERR_PTR(-EINVAL);
244
245 if (!IS_ERR(pages))
246 obj->ops->put_pages(obj, pages);
247
248 err = 0;
249 unlock:
250 mutex_unlock(&obj->mm.lock);
251
252 return err;
253 }
254
255 #ifndef __NetBSD__
iomap_pte(resource_size_t base,dma_addr_t offset,pgprot_t prot)256 static inline pte_t iomap_pte(resource_size_t base,
257 dma_addr_t offset,
258 pgprot_t prot)
259 {
260 return pte_mkspecial(pfn_pte((base + offset) >> PAGE_SHIFT, prot));
261 }
262 #endif
263
264 /* The 'mapping' part of i915_gem_object_pin_map() below */
i915_gem_object_map(struct drm_i915_gem_object * obj,enum i915_map_type type)265 static void *i915_gem_object_map(struct drm_i915_gem_object *obj,
266 enum i915_map_type type)
267 {
268 #ifdef __NetBSD__
269 vaddr_t va;
270 struct page *page;
271 paddr_t pa;
272 unsigned i;
273 int kmflags = UVM_KMF_VAONLY|UVM_KMF_WAITVA;
274 int prot = VM_PROT_READ|VM_PROT_WRITE;
275 int flags = 0;
276
277 /*
278 * XXX Be nice if we had bus_dmamem segments so we could use
279 * bus_dmamem_map, but we don't so we can't.
280 */
281
282 /* Verify the object is reasonable to map. */
283 /* XXX sync with below */
284 if (!i915_gem_object_has_struct_page(obj) && type != I915_MAP_WC)
285 return NULL;
286
287 /* Incorporate mapping type into pmap flags. */
288 switch (type) {
289 case I915_MAP_WC:
290 flags |= PMAP_WRITE_COMBINE;
291 break;
292 case I915_MAP_WB:
293 default:
294 break;
295 }
296
297 /* Allow failure if >1 page. */
298 if (obj->base.size > PAGE_SIZE)
299 kmflags |= UVM_KMF_CANFAIL;
300
301 /* Allocate a contiguous chunk of KVA. */
302 va = uvm_km_alloc(kernel_map, obj->base.size, PAGE_SIZE, kmflags);
303 if (va == 0)
304 return NULL;
305
306 /* Wire the KVA to the right physical addresses. */
307 for (i = 0; i < obj->base.size >> PAGE_SHIFT; i++) {
308 page = obj->mm.pages->sgl->sg_pgs[i];
309 pa = VM_PAGE_TO_PHYS(&page->p_vmp);
310 pmap_kenter_pa(va + i*PAGE_SIZE, pa, prot, flags);
311 }
312 pmap_update(pmap_kernel());
313
314 return (void *)va;
315 #else
316 unsigned long n_pte = obj->base.size >> PAGE_SHIFT;
317 struct sg_table *sgt = obj->mm.pages;
318 pte_t *stack[32], **mem;
319 struct vm_struct *area;
320 pgprot_t pgprot;
321
322 if (!i915_gem_object_has_struct_page(obj) && type != I915_MAP_WC)
323 return NULL;
324
325 /* A single page can always be kmapped */
326 if (n_pte == 1 && type == I915_MAP_WB)
327 return kmap(sg_page(sgt->sgl));
328
329 mem = stack;
330 if (n_pte > ARRAY_SIZE(stack)) {
331 /* Too big for stack -- allocate temporary array instead */
332 mem = kvmalloc_array(n_pte, sizeof(*mem), GFP_KERNEL);
333 if (!mem)
334 return NULL;
335 }
336
337 area = alloc_vm_area(obj->base.size, mem);
338 if (!area) {
339 if (mem != stack)
340 kvfree(mem);
341 return NULL;
342 }
343
344 switch (type) {
345 default:
346 MISSING_CASE(type);
347 /* fallthrough - to use PAGE_KERNEL anyway */
348 case I915_MAP_WB:
349 pgprot = PAGE_KERNEL;
350 break;
351 case I915_MAP_WC:
352 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
353 break;
354 }
355
356 if (i915_gem_object_has_struct_page(obj)) {
357 struct sgt_iter iter;
358 struct page *page;
359 pte_t **ptes = mem;
360
361 for_each_sgt_page(page, iter, sgt)
362 **ptes++ = mk_pte(page, pgprot);
363 } else {
364 resource_size_t iomap;
365 struct sgt_iter iter;
366 pte_t **ptes = mem;
367 dma_addr_t addr;
368
369 iomap = obj->mm.region->iomap.base;
370 iomap -= obj->mm.region->region.start;
371
372 for_each_sgt_daddr(addr, iter, sgt)
373 **ptes++ = iomap_pte(iomap, addr, pgprot);
374 }
375
376 if (mem != stack)
377 kvfree(mem);
378
379 return area->addr;
380 #endif
381 }
382
383 /* get, pin, and map the pages of the object into kernel space */
i915_gem_object_pin_map(struct drm_i915_gem_object * obj,enum i915_map_type type)384 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
385 enum i915_map_type type)
386 {
387 enum i915_map_type has_type;
388 unsigned int flags;
389 bool pinned;
390 void *ptr;
391 int err;
392
393 flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | I915_GEM_OBJECT_HAS_IOMEM;
394 if (!i915_gem_object_type_has(obj, flags))
395 return ERR_PTR(-ENXIO);
396
397 err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES);
398 if (err)
399 return ERR_PTR(err);
400
401 pinned = !(type & I915_MAP_OVERRIDE);
402 type &= ~I915_MAP_OVERRIDE;
403
404 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
405 if (unlikely(!i915_gem_object_has_pages(obj))) {
406 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
407
408 err = ____i915_gem_object_get_pages(obj);
409 if (err)
410 goto err_unlock;
411
412 smp_mb__before_atomic();
413 }
414 atomic_inc(&obj->mm.pages_pin_count);
415 pinned = false;
416 }
417 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
418
419 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
420 if (ptr && has_type != type) {
421 if (pinned) {
422 err = -EBUSY;
423 goto err_unpin;
424 }
425
426 unmap_object(obj, ptr);
427
428 ptr = obj->mm.mapping = NULL;
429 }
430
431 if (!ptr) {
432 ptr = i915_gem_object_map(obj, type);
433 if (!ptr) {
434 err = -ENOMEM;
435 goto err_unpin;
436 }
437
438 obj->mm.mapping = page_pack_bits(ptr, type);
439 }
440
441 out_unlock:
442 mutex_unlock(&obj->mm.lock);
443 return ptr;
444
445 err_unpin:
446 atomic_dec(&obj->mm.pages_pin_count);
447 err_unlock:
448 ptr = ERR_PTR(err);
449 goto out_unlock;
450 }
451
__i915_gem_object_flush_map(struct drm_i915_gem_object * obj,unsigned long offset,unsigned long size)452 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
453 unsigned long offset,
454 unsigned long size)
455 {
456 enum i915_map_type has_type;
457 void *ptr;
458
459 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
460 GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
461 offset, size, obj->base.size));
462
463 obj->mm.dirty = true;
464
465 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
466 return;
467
468 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
469 if (has_type == I915_MAP_WC)
470 return;
471
472 drm_clflush_virt_range(ptr + offset, size);
473 if (size == obj->base.size) {
474 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
475 obj->cache_dirty = false;
476 }
477 }
478
479 #ifndef __NetBSD__
480 struct scatterlist *
i915_gem_object_get_sg(struct drm_i915_gem_object * obj,unsigned int n,unsigned int * offset)481 i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
482 unsigned int n,
483 unsigned int *offset)
484 {
485 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
486 struct scatterlist *sg;
487 unsigned int idx, count;
488
489 might_sleep();
490 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
491 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
492
493 /* As we iterate forward through the sg, we record each entry in a
494 * radixtree for quick repeated (backwards) lookups. If we have seen
495 * this index previously, we will have an entry for it.
496 *
497 * Initial lookup is O(N), but this is amortized to O(1) for
498 * sequential page access (where each new request is consecutive
499 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
500 * i.e. O(1) with a large constant!
501 */
502 if (n < READ_ONCE(iter->sg_idx))
503 goto lookup;
504
505 mutex_lock(&iter->lock);
506
507 /* We prefer to reuse the last sg so that repeated lookup of this
508 * (or the subsequent) sg are fast - comparing against the last
509 * sg is faster than going through the radixtree.
510 */
511
512 sg = iter->sg_pos;
513 idx = iter->sg_idx;
514 count = __sg_page_count(sg);
515
516 while (idx + count <= n) {
517 void *entry;
518 unsigned long i;
519 int ret;
520
521 /* If we cannot allocate and insert this entry, or the
522 * individual pages from this range, cancel updating the
523 * sg_idx so that on this lookup we are forced to linearly
524 * scan onwards, but on future lookups we will try the
525 * insertion again (in which case we need to be careful of
526 * the error return reporting that we have already inserted
527 * this index).
528 */
529 ret = radix_tree_insert(&iter->radix, idx, sg);
530 if (ret && ret != -EEXIST)
531 goto scan;
532
533 entry = xa_mk_value(idx);
534 for (i = 1; i < count; i++) {
535 ret = radix_tree_insert(&iter->radix, idx + i, entry);
536 if (ret && ret != -EEXIST)
537 goto scan;
538 }
539
540 idx += count;
541 sg = ____sg_next(sg);
542 count = __sg_page_count(sg);
543 }
544
545 scan:
546 iter->sg_pos = sg;
547 iter->sg_idx = idx;
548
549 mutex_unlock(&iter->lock);
550
551 if (unlikely(n < idx)) /* insertion completed by another thread */
552 goto lookup;
553
554 /* In case we failed to insert the entry into the radixtree, we need
555 * to look beyond the current sg.
556 */
557 while (idx + count <= n) {
558 idx += count;
559 sg = ____sg_next(sg);
560 count = __sg_page_count(sg);
561 }
562
563 *offset = n - idx;
564 return sg;
565
566 lookup:
567 rcu_read_lock();
568
569 sg = radix_tree_lookup(&iter->radix, n);
570 GEM_BUG_ON(!sg);
571
572 /* If this index is in the middle of multi-page sg entry,
573 * the radix tree will contain a value entry that points
574 * to the start of that range. We will return the pointer to
575 * the base page and the offset of this page within the
576 * sg entry's range.
577 */
578 *offset = 0;
579 if (unlikely(xa_is_value(sg))) {
580 unsigned long base = xa_to_value(sg);
581
582 sg = radix_tree_lookup(&iter->radix, base);
583 GEM_BUG_ON(!sg);
584
585 *offset = n - base;
586 }
587
588 rcu_read_unlock();
589
590 return sg;
591 }
592 #endif
593
594 struct page *
i915_gem_object_get_page(struct drm_i915_gem_object * obj,unsigned int n)595 i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
596 {
597 #ifdef __NetBSD__
598 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
599 KASSERT(n < obj->mm.pages->sgl->sg_npgs);
600 return obj->mm.pages->sgl->sg_pgs[n];
601 #else
602 struct scatterlist *sg;
603 unsigned int offset;
604
605 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
606
607 sg = i915_gem_object_get_sg(obj, n, &offset);
608 return nth_page(sg_page(sg), offset);
609 #endif
610 }
611
612 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
613 struct page *
i915_gem_object_get_dirty_page(struct drm_i915_gem_object * obj,unsigned int n)614 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
615 unsigned int n)
616 {
617 struct page *page;
618
619 page = i915_gem_object_get_page(obj, n);
620 if (!obj->mm.dirty)
621 set_page_dirty(page);
622
623 return page;
624 }
625
626 dma_addr_t
i915_gem_object_get_dma_address_len(struct drm_i915_gem_object * obj,unsigned long n,unsigned int * len)627 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
628 unsigned long n,
629 unsigned int *len)
630 {
631 #ifdef __NetBSD__
632 bus_dmamap_t map = obj->mm.pages->sgl->sg_dmamap;
633 bus_addr_t poff = (bus_addr_t)n << PAGE_SHIFT;
634 unsigned seg;
635
636 for (seg = 0; seg < map->dm_nsegs; seg++) {
637 if (poff < map->dm_segs[seg].ds_len) {
638 *len = map->dm_segs[seg].ds_len - poff;
639 return map->dm_segs[seg].ds_addr + poff;
640 }
641 poff -= map->dm_segs[seg].ds_len;
642 }
643 KASSERT(0);
644 return 0;
645 #else
646 struct scatterlist *sg;
647 unsigned int offset;
648
649 sg = i915_gem_object_get_sg(obj, n, &offset);
650
651 if (len)
652 *len = sg_dma_len(sg) - (offset << PAGE_SHIFT);
653
654 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
655 #endif
656 }
657
658 dma_addr_t
i915_gem_object_get_dma_address(struct drm_i915_gem_object * obj,unsigned long n)659 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
660 unsigned long n)
661 {
662 return i915_gem_object_get_dma_address_len(obj, n, NULL);
663 }
664