xref: /linux/drivers/gpu/drm/i915/gem/i915_gem_pages.c (revision b1cce98493a095925fb51be045ccf6e08edb4aa0)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2014-2016 Intel Corporation
4  */
5 
6 #include <drm/drm_cache.h>
7 #include <drm/drm_panic.h>
8 #include <linux/vmalloc.h>
9 
10 #include "display/intel_fb.h"
11 #include "display/intel_display_types.h"
12 #include "gt/intel_gt.h"
13 #include "gt/intel_tlb.h"
14 
15 #include "i915_drv.h"
16 #include "i915_gem_object.h"
17 #include "i915_scatterlist.h"
18 #include "i915_gem_lmem.h"
19 #include "i915_gem_mman.h"
20 
21 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
22 				 struct sg_table *pages)
23 {
24 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
25 	unsigned long supported = RUNTIME_INFO(i915)->page_sizes;
26 	bool shrinkable;
27 	int i;
28 
29 	assert_object_held_shared(obj);
30 
31 	if (i915_gem_object_is_volatile(obj))
32 		obj->mm.madv = I915_MADV_DONTNEED;
33 
34 	/* Make the pages coherent with the GPU (flushing any swapin). */
35 	if (obj->cache_dirty) {
36 		WARN_ON_ONCE(IS_DGFX(i915));
37 		obj->write_domain = 0;
38 		if (i915_gem_object_has_struct_page(obj))
39 			drm_clflush_sg(pages);
40 		obj->cache_dirty = false;
41 	}
42 
43 	obj->mm.get_page.sg_pos = pages->sgl;
44 	obj->mm.get_page.sg_idx = 0;
45 	obj->mm.get_dma_page.sg_pos = pages->sgl;
46 	obj->mm.get_dma_page.sg_idx = 0;
47 
48 	obj->mm.pages = pages;
49 
50 	obj->mm.page_sizes.phys = i915_sg_dma_sizes(pages->sgl);
51 	GEM_BUG_ON(!obj->mm.page_sizes.phys);
52 
53 	/*
54 	 * Calculate the supported page-sizes which fit into the given
55 	 * sg_page_sizes. This will give us the page-sizes which we may be able
56 	 * to use opportunistically when later inserting into the GTT. For
57 	 * example if phys=2G, then in theory we should be able to use 1G, 2M,
58 	 * 64K or 4K pages, although in practice this will depend on a number of
59 	 * other factors.
60 	 */
61 	obj->mm.page_sizes.sg = 0;
62 	for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
63 		if (obj->mm.page_sizes.phys & ~0u << i)
64 			obj->mm.page_sizes.sg |= BIT(i);
65 	}
66 	GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
67 
68 	shrinkable = i915_gem_object_is_shrinkable(obj);
69 
70 	if (i915_gem_object_is_tiled(obj) &&
71 	    i915->gem_quirks & GEM_QUIRK_PIN_SWIZZLED_PAGES) {
72 		GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
73 		i915_gem_object_set_tiling_quirk(obj);
74 		GEM_BUG_ON(!list_empty(&obj->mm.link));
75 		atomic_inc(&obj->mm.shrink_pin);
76 		shrinkable = false;
77 	}
78 
79 	if (shrinkable && !i915_gem_object_has_self_managed_shrink_list(obj)) {
80 		struct list_head *list;
81 		unsigned long flags;
82 
83 		assert_object_held(obj);
84 		spin_lock_irqsave(&i915->mm.obj_lock, flags);
85 
86 		i915->mm.shrink_count++;
87 		i915->mm.shrink_memory += obj->base.size;
88 
89 		if (obj->mm.madv != I915_MADV_WILLNEED)
90 			list = &i915->mm.purge_list;
91 		else
92 			list = &i915->mm.shrink_list;
93 		list_add_tail(&obj->mm.link, list);
94 
95 		atomic_set(&obj->mm.shrink_pin, 0);
96 		spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
97 	}
98 }
99 
100 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
101 {
102 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
103 	int err;
104 
105 	assert_object_held_shared(obj);
106 
107 	if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
108 		drm_dbg(&i915->drm,
109 			"Attempting to obtain a purgeable object\n");
110 		return -EFAULT;
111 	}
112 
113 	err = obj->ops->get_pages(obj);
114 	GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
115 
116 	return err;
117 }
118 
119 /* Ensure that the associated pages are gathered from the backing storage
120  * and pinned into our object. i915_gem_object_pin_pages() may be called
121  * multiple times before they are released by a single call to
122  * i915_gem_object_unpin_pages() - once the pages are no longer referenced
123  * either as a result of memory pressure (reaping pages under the shrinker)
124  * or as the object is itself released.
125  */
126 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
127 {
128 	int err;
129 
130 	assert_object_held(obj);
131 
132 	assert_object_held_shared(obj);
133 
134 	if (unlikely(!i915_gem_object_has_pages(obj))) {
135 		GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
136 
137 		err = ____i915_gem_object_get_pages(obj);
138 		if (err)
139 			return err;
140 
141 		smp_mb__before_atomic();
142 	}
143 	atomic_inc(&obj->mm.pages_pin_count);
144 
145 	return 0;
146 }
147 
148 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj)
149 {
150 	struct i915_gem_ww_ctx ww;
151 	int err;
152 
153 	i915_gem_ww_ctx_init(&ww, true);
154 retry:
155 	err = i915_gem_object_lock(obj, &ww);
156 	if (!err)
157 		err = i915_gem_object_pin_pages(obj);
158 
159 	if (err == -EDEADLK) {
160 		err = i915_gem_ww_ctx_backoff(&ww);
161 		if (!err)
162 			goto retry;
163 	}
164 	i915_gem_ww_ctx_fini(&ww);
165 	return err;
166 }
167 
168 /* Immediately discard the backing storage */
169 int i915_gem_object_truncate(struct drm_i915_gem_object *obj)
170 {
171 	if (obj->ops->truncate)
172 		return obj->ops->truncate(obj);
173 
174 	return 0;
175 }
176 
177 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
178 {
179 	struct radix_tree_iter iter;
180 	void __rcu **slot;
181 
182 	rcu_read_lock();
183 	radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
184 		radix_tree_delete(&obj->mm.get_page.radix, iter.index);
185 	radix_tree_for_each_slot(slot, &obj->mm.get_dma_page.radix, &iter, 0)
186 		radix_tree_delete(&obj->mm.get_dma_page.radix, iter.index);
187 	rcu_read_unlock();
188 }
189 
190 static void unmap_object(struct drm_i915_gem_object *obj, void *ptr)
191 {
192 	if (is_vmalloc_addr(ptr))
193 		vunmap(ptr);
194 }
195 
196 static void flush_tlb_invalidate(struct drm_i915_gem_object *obj)
197 {
198 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
199 	struct intel_gt *gt;
200 	int id;
201 
202 	for_each_gt(gt, i915, id) {
203 		if (!obj->mm.tlb[id])
204 			continue;
205 
206 		intel_gt_invalidate_tlb_full(gt, obj->mm.tlb[id]);
207 		obj->mm.tlb[id] = 0;
208 	}
209 }
210 
211 struct sg_table *
212 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
213 {
214 	struct sg_table *pages;
215 
216 	assert_object_held_shared(obj);
217 
218 	pages = fetch_and_zero(&obj->mm.pages);
219 	if (IS_ERR_OR_NULL(pages))
220 		return pages;
221 
222 	if (i915_gem_object_is_volatile(obj))
223 		obj->mm.madv = I915_MADV_WILLNEED;
224 
225 	if (!i915_gem_object_has_self_managed_shrink_list(obj))
226 		i915_gem_object_make_unshrinkable(obj);
227 
228 	if (obj->mm.mapping) {
229 		unmap_object(obj, page_mask_bits(obj->mm.mapping));
230 		obj->mm.mapping = NULL;
231 	}
232 
233 	__i915_gem_object_reset_page_iter(obj);
234 	obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
235 
236 	flush_tlb_invalidate(obj);
237 
238 	return pages;
239 }
240 
241 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
242 {
243 	struct sg_table *pages;
244 
245 	if (i915_gem_object_has_pinned_pages(obj))
246 		return -EBUSY;
247 
248 	/* May be called by shrinker from within get_pages() (on another bo) */
249 	assert_object_held_shared(obj);
250 
251 	i915_gem_object_release_mmap_offset(obj);
252 
253 	/*
254 	 * ->put_pages might need to allocate memory for the bit17 swizzle
255 	 * array, hence protect them from being reaped by removing them from gtt
256 	 * lists early.
257 	 */
258 	pages = __i915_gem_object_unset_pages(obj);
259 
260 	/*
261 	 * XXX Temporary hijinx to avoid updating all backends to handle
262 	 * NULL pages. In the future, when we have more asynchronous
263 	 * get_pages backends we should be better able to handle the
264 	 * cancellation of the async task in a more uniform manner.
265 	 */
266 	if (!IS_ERR_OR_NULL(pages))
267 		obj->ops->put_pages(obj, pages);
268 
269 	return 0;
270 }
271 
272 /* The 'mapping' part of i915_gem_object_pin_map() below */
273 static void *i915_gem_object_map_page(struct drm_i915_gem_object *obj,
274 				      enum i915_map_type type)
275 {
276 	unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i;
277 	struct page *stack[32], **pages = stack, *page;
278 	struct sgt_iter iter;
279 	pgprot_t pgprot;
280 	void *vaddr;
281 
282 	switch (type) {
283 	default:
284 		MISSING_CASE(type);
285 		fallthrough;	/* to use PAGE_KERNEL anyway */
286 	case I915_MAP_WB:
287 		/*
288 		 * On 32b, highmem using a finite set of indirect PTE (i.e.
289 		 * vmap) to provide virtual mappings of the high pages.
290 		 * As these are finite, map_new_virtual() must wait for some
291 		 * other kmap() to finish when it runs out. If we map a large
292 		 * number of objects, there is no method for it to tell us
293 		 * to release the mappings, and we deadlock.
294 		 *
295 		 * However, if we make an explicit vmap of the page, that
296 		 * uses a larger vmalloc arena, and also has the ability
297 		 * to tell us to release unwanted mappings. Most importantly,
298 		 * it will fail and propagate an error instead of waiting
299 		 * forever.
300 		 *
301 		 * So if the page is beyond the 32b boundary, make an explicit
302 		 * vmap.
303 		 */
304 		if (n_pages == 1 && !PageHighMem(sg_page(obj->mm.pages->sgl)))
305 			return page_address(sg_page(obj->mm.pages->sgl));
306 		pgprot = PAGE_KERNEL;
307 		break;
308 	case I915_MAP_WC:
309 		pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
310 		break;
311 	}
312 
313 	if (n_pages > ARRAY_SIZE(stack)) {
314 		/* Too big for stack -- allocate temporary array instead */
315 		pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
316 		if (!pages)
317 			return ERR_PTR(-ENOMEM);
318 	}
319 
320 	i = 0;
321 	for_each_sgt_page(page, iter, obj->mm.pages)
322 		pages[i++] = page;
323 	vaddr = vmap(pages, n_pages, 0, pgprot);
324 	if (pages != stack)
325 		kvfree(pages);
326 
327 	return vaddr ?: ERR_PTR(-ENOMEM);
328 }
329 
330 static void *i915_gem_object_map_pfn(struct drm_i915_gem_object *obj,
331 				     enum i915_map_type type)
332 {
333 	resource_size_t iomap = obj->mm.region->iomap.base -
334 		obj->mm.region->region.start;
335 	unsigned long n_pfn = obj->base.size >> PAGE_SHIFT;
336 	unsigned long stack[32], *pfns = stack, i;
337 	struct sgt_iter iter;
338 	dma_addr_t addr;
339 	void *vaddr;
340 
341 	GEM_BUG_ON(type != I915_MAP_WC);
342 
343 	if (n_pfn > ARRAY_SIZE(stack)) {
344 		/* Too big for stack -- allocate temporary array instead */
345 		pfns = kvmalloc_array(n_pfn, sizeof(*pfns), GFP_KERNEL);
346 		if (!pfns)
347 			return ERR_PTR(-ENOMEM);
348 	}
349 
350 	i = 0;
351 	for_each_sgt_daddr(addr, iter, obj->mm.pages)
352 		pfns[i++] = (iomap + addr) >> PAGE_SHIFT;
353 	vaddr = vmap_pfn(pfns, n_pfn, pgprot_writecombine(PAGE_KERNEL_IO));
354 	if (pfns != stack)
355 		kvfree(pfns);
356 
357 	return vaddr ?: ERR_PTR(-ENOMEM);
358 }
359 
360 struct i915_panic_data {
361 	struct page **pages;
362 	int page;
363 	void *vaddr;
364 };
365 
366 struct i915_framebuffer {
367 	struct intel_framebuffer base;
368 	struct i915_panic_data panic;
369 };
370 
371 static inline struct i915_panic_data *to_i915_panic_data(struct intel_framebuffer *fb)
372 {
373 	return &container_of_const(fb, struct i915_framebuffer, base)->panic;
374 }
375 
376 static void i915_panic_kunmap(struct i915_panic_data *panic)
377 {
378 	if (panic->vaddr) {
379 		drm_clflush_virt_range(panic->vaddr, PAGE_SIZE);
380 		kunmap_local(panic->vaddr);
381 		panic->vaddr = NULL;
382 	}
383 }
384 
385 static struct page **i915_gem_object_panic_pages(struct drm_i915_gem_object *obj)
386 {
387 	unsigned long n_pages = obj->base.size >> PAGE_SHIFT, i;
388 	struct page *page;
389 	struct page **pages;
390 	struct sgt_iter iter;
391 
392 	/* For a 3840x2160 32 bits Framebuffer, this should require ~64K */
393 	pages = kmalloc_array(n_pages, sizeof(*pages), GFP_ATOMIC);
394 	if (!pages)
395 		return NULL;
396 
397 	i = 0;
398 	for_each_sgt_page(page, iter, obj->mm.pages)
399 		pages[i++] = page;
400 	return pages;
401 }
402 
403 static void i915_gem_object_panic_map_set_pixel(struct drm_scanout_buffer *sb, unsigned int x,
404 						unsigned int y, u32 color)
405 {
406 	struct intel_framebuffer *fb = (struct intel_framebuffer *)sb->private;
407 	unsigned int offset = fb->panic_tiling(sb->width, x, y);
408 
409 	iosys_map_wr(&sb->map[0], offset, u32, color);
410 }
411 
412 /*
413  * The scanout buffer pages are not mapped, so for each pixel,
414  * use kmap_local_page_try_from_panic() to map the page, and write the pixel.
415  * Try to keep the map from the previous pixel, to avoid too much map/unmap.
416  */
417 static void i915_gem_object_panic_page_set_pixel(struct drm_scanout_buffer *sb, unsigned int x,
418 						 unsigned int y, u32 color)
419 {
420 	unsigned int new_page;
421 	unsigned int offset;
422 	struct intel_framebuffer *fb = (struct intel_framebuffer *)sb->private;
423 	struct i915_panic_data *panic = to_i915_panic_data(fb);
424 
425 	if (fb->panic_tiling)
426 		offset = fb->panic_tiling(sb->width, x, y);
427 	else
428 		offset = y * sb->pitch[0] + x * sb->format->cpp[0];
429 
430 	new_page = offset >> PAGE_SHIFT;
431 	offset = offset % PAGE_SIZE;
432 	if (new_page != panic->page) {
433 		i915_panic_kunmap(panic);
434 		panic->page = new_page;
435 		panic->vaddr =
436 			kmap_local_page_try_from_panic(panic->pages[panic->page]);
437 	}
438 	if (panic->vaddr) {
439 		u32 *pix = panic->vaddr + offset;
440 		*pix = color;
441 	}
442 }
443 
444 struct intel_framebuffer *i915_gem_object_alloc_framebuffer(void)
445 {
446 	struct i915_framebuffer *i915_fb;
447 
448 	i915_fb = kzalloc(sizeof(*i915_fb), GFP_KERNEL);
449 	if (i915_fb)
450 		return &i915_fb->base;
451 	return NULL;
452 }
453 
454 /*
455  * Setup the gem framebuffer for drm_panic access.
456  * Use current vaddr if it exists, or setup a list of pages.
457  * pfn is not supported yet.
458  */
459 int i915_gem_object_panic_setup(struct drm_scanout_buffer *sb)
460 {
461 	enum i915_map_type has_type;
462 	struct intel_framebuffer *fb = (struct intel_framebuffer *)sb->private;
463 	struct i915_panic_data *panic = to_i915_panic_data(fb);
464 	struct drm_i915_gem_object *obj = to_intel_bo(intel_fb_bo(&fb->base));
465 	void *ptr;
466 
467 	ptr = page_unpack_bits(obj->mm.mapping, &has_type);
468 	if (ptr) {
469 		if (i915_gem_object_has_iomem(obj))
470 			iosys_map_set_vaddr_iomem(&sb->map[0], (void __iomem *)ptr);
471 		else
472 			iosys_map_set_vaddr(&sb->map[0], ptr);
473 
474 		if (fb->panic_tiling)
475 			sb->set_pixel = i915_gem_object_panic_map_set_pixel;
476 		return 0;
477 	}
478 	if (i915_gem_object_has_struct_page(obj)) {
479 		panic->pages = i915_gem_object_panic_pages(obj);
480 		if (!panic->pages)
481 			return -ENOMEM;
482 		panic->page = -1;
483 		sb->set_pixel = i915_gem_object_panic_page_set_pixel;
484 		return 0;
485 	}
486 	return -EOPNOTSUPP;
487 }
488 
489 void i915_gem_object_panic_finish(struct intel_framebuffer *fb)
490 {
491 	struct i915_panic_data *panic = to_i915_panic_data(fb);
492 
493 	i915_panic_kunmap(panic);
494 	panic->page = -1;
495 	kfree(panic->pages);
496 	panic->pages = NULL;
497 }
498 
499 /* get, pin, and map the pages of the object into kernel space */
500 void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
501 			      enum i915_map_type type)
502 {
503 	enum i915_map_type has_type;
504 	bool pinned;
505 	void *ptr;
506 	int err;
507 
508 	if (!i915_gem_object_has_struct_page(obj) &&
509 	    !i915_gem_object_has_iomem(obj))
510 		return ERR_PTR(-ENXIO);
511 
512 	if (WARN_ON_ONCE(obj->flags & I915_BO_ALLOC_GPU_ONLY))
513 		return ERR_PTR(-EINVAL);
514 
515 	assert_object_held(obj);
516 
517 	pinned = !(type & I915_MAP_OVERRIDE);
518 	type &= ~I915_MAP_OVERRIDE;
519 
520 	if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
521 		if (unlikely(!i915_gem_object_has_pages(obj))) {
522 			GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
523 
524 			err = ____i915_gem_object_get_pages(obj);
525 			if (err)
526 				return ERR_PTR(err);
527 
528 			smp_mb__before_atomic();
529 		}
530 		atomic_inc(&obj->mm.pages_pin_count);
531 		pinned = false;
532 	}
533 	GEM_BUG_ON(!i915_gem_object_has_pages(obj));
534 
535 	/*
536 	 * For discrete our CPU mappings needs to be consistent in order to
537 	 * function correctly on !x86. When mapping things through TTM, we use
538 	 * the same rules to determine the caching type.
539 	 *
540 	 * The caching rules, starting from DG1:
541 	 *
542 	 *	- If the object can be placed in device local-memory, then the
543 	 *	  pages should be allocated and mapped as write-combined only.
544 	 *
545 	 *	- Everything else is always allocated and mapped as write-back,
546 	 *	  with the guarantee that everything is also coherent with the
547 	 *	  GPU.
548 	 *
549 	 * Internal users of lmem are already expected to get this right, so no
550 	 * fudging needed there.
551 	 */
552 	if (i915_gem_object_placement_possible(obj, INTEL_MEMORY_LOCAL)) {
553 		if (type != I915_MAP_WC && !obj->mm.n_placements) {
554 			ptr = ERR_PTR(-ENODEV);
555 			goto err_unpin;
556 		}
557 
558 		type = I915_MAP_WC;
559 	} else if (IS_DGFX(to_i915(obj->base.dev))) {
560 		type = I915_MAP_WB;
561 	}
562 
563 	ptr = page_unpack_bits(obj->mm.mapping, &has_type);
564 	if (ptr && has_type != type) {
565 		if (pinned) {
566 			ptr = ERR_PTR(-EBUSY);
567 			goto err_unpin;
568 		}
569 
570 		unmap_object(obj, ptr);
571 
572 		ptr = obj->mm.mapping = NULL;
573 	}
574 
575 	if (!ptr) {
576 		err = i915_gem_object_wait_moving_fence(obj, true);
577 		if (err) {
578 			ptr = ERR_PTR(err);
579 			goto err_unpin;
580 		}
581 
582 		if (GEM_WARN_ON(type == I915_MAP_WC && !pat_enabled()))
583 			ptr = ERR_PTR(-ENODEV);
584 		else if (i915_gem_object_has_struct_page(obj))
585 			ptr = i915_gem_object_map_page(obj, type);
586 		else
587 			ptr = i915_gem_object_map_pfn(obj, type);
588 		if (IS_ERR(ptr))
589 			goto err_unpin;
590 
591 		obj->mm.mapping = page_pack_bits(ptr, type);
592 	}
593 
594 	return ptr;
595 
596 err_unpin:
597 	atomic_dec(&obj->mm.pages_pin_count);
598 	return ptr;
599 }
600 
601 void *i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
602 				       enum i915_map_type type)
603 {
604 	void *ret;
605 
606 	i915_gem_object_lock(obj, NULL);
607 	ret = i915_gem_object_pin_map(obj, type);
608 	i915_gem_object_unlock(obj);
609 
610 	return ret;
611 }
612 
613 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
614 				 unsigned long offset,
615 				 unsigned long size)
616 {
617 	enum i915_map_type has_type;
618 	void *ptr;
619 
620 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
621 	GEM_BUG_ON(range_overflows_t(typeof(obj->base.size),
622 				     offset, size, obj->base.size));
623 
624 	wmb(); /* let all previous writes be visible to coherent partners */
625 	obj->mm.dirty = true;
626 
627 	if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)
628 		return;
629 
630 	ptr = page_unpack_bits(obj->mm.mapping, &has_type);
631 	if (has_type == I915_MAP_WC)
632 		return;
633 
634 	drm_clflush_virt_range(ptr + offset, size);
635 	if (size == obj->base.size) {
636 		obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
637 		obj->cache_dirty = false;
638 	}
639 }
640 
641 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj)
642 {
643 	GEM_BUG_ON(!obj->mm.mapping);
644 
645 	/*
646 	 * We allow removing the mapping from underneath pinned pages!
647 	 *
648 	 * Furthermore, since this is an unsafe operation reserved only
649 	 * for construction time manipulation, we ignore locking prudence.
650 	 */
651 	unmap_object(obj, page_mask_bits(fetch_and_zero(&obj->mm.mapping)));
652 
653 	i915_gem_object_unpin_map(obj);
654 }
655 
656 struct scatterlist *
657 __i915_gem_object_page_iter_get_sg(struct drm_i915_gem_object *obj,
658 				   struct i915_gem_object_page_iter *iter,
659 				   pgoff_t n,
660 				   unsigned int *offset)
661 
662 {
663 	const bool dma = iter == &obj->mm.get_dma_page ||
664 			 iter == &obj->ttm.get_io_page;
665 	unsigned int idx, count;
666 	struct scatterlist *sg;
667 
668 	might_sleep();
669 	GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
670 	if (!i915_gem_object_has_pinned_pages(obj))
671 		assert_object_held(obj);
672 
673 	/* As we iterate forward through the sg, we record each entry in a
674 	 * radixtree for quick repeated (backwards) lookups. If we have seen
675 	 * this index previously, we will have an entry for it.
676 	 *
677 	 * Initial lookup is O(N), but this is amortized to O(1) for
678 	 * sequential page access (where each new request is consecutive
679 	 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
680 	 * i.e. O(1) with a large constant!
681 	 */
682 	if (n < READ_ONCE(iter->sg_idx))
683 		goto lookup;
684 
685 	mutex_lock(&iter->lock);
686 
687 	/* We prefer to reuse the last sg so that repeated lookup of this
688 	 * (or the subsequent) sg are fast - comparing against the last
689 	 * sg is faster than going through the radixtree.
690 	 */
691 
692 	sg = iter->sg_pos;
693 	idx = iter->sg_idx;
694 	count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
695 
696 	while (idx + count <= n) {
697 		void *entry;
698 		unsigned long i;
699 		int ret;
700 
701 		/* If we cannot allocate and insert this entry, or the
702 		 * individual pages from this range, cancel updating the
703 		 * sg_idx so that on this lookup we are forced to linearly
704 		 * scan onwards, but on future lookups we will try the
705 		 * insertion again (in which case we need to be careful of
706 		 * the error return reporting that we have already inserted
707 		 * this index).
708 		 */
709 		ret = radix_tree_insert(&iter->radix, idx, sg);
710 		if (ret && ret != -EEXIST)
711 			goto scan;
712 
713 		entry = xa_mk_value(idx);
714 		for (i = 1; i < count; i++) {
715 			ret = radix_tree_insert(&iter->radix, idx + i, entry);
716 			if (ret && ret != -EEXIST)
717 				goto scan;
718 		}
719 
720 		idx += count;
721 		sg = ____sg_next(sg);
722 		count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
723 	}
724 
725 scan:
726 	iter->sg_pos = sg;
727 	iter->sg_idx = idx;
728 
729 	mutex_unlock(&iter->lock);
730 
731 	if (unlikely(n < idx)) /* insertion completed by another thread */
732 		goto lookup;
733 
734 	/* In case we failed to insert the entry into the radixtree, we need
735 	 * to look beyond the current sg.
736 	 */
737 	while (idx + count <= n) {
738 		idx += count;
739 		sg = ____sg_next(sg);
740 		count = dma ? __sg_dma_page_count(sg) : __sg_page_count(sg);
741 	}
742 
743 	*offset = n - idx;
744 	return sg;
745 
746 lookup:
747 	rcu_read_lock();
748 
749 	sg = radix_tree_lookup(&iter->radix, n);
750 	GEM_BUG_ON(!sg);
751 
752 	/* If this index is in the middle of multi-page sg entry,
753 	 * the radix tree will contain a value entry that points
754 	 * to the start of that range. We will return the pointer to
755 	 * the base page and the offset of this page within the
756 	 * sg entry's range.
757 	 */
758 	*offset = 0;
759 	if (unlikely(xa_is_value(sg))) {
760 		unsigned long base = xa_to_value(sg);
761 
762 		sg = radix_tree_lookup(&iter->radix, base);
763 		GEM_BUG_ON(!sg);
764 
765 		*offset = n - base;
766 	}
767 
768 	rcu_read_unlock();
769 
770 	return sg;
771 }
772 
773 struct page *
774 __i915_gem_object_get_page(struct drm_i915_gem_object *obj, pgoff_t n)
775 {
776 	struct scatterlist *sg;
777 	unsigned int offset;
778 
779 	GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
780 
781 	sg = i915_gem_object_get_sg(obj, n, &offset);
782 	return nth_page(sg_page(sg), offset);
783 }
784 
785 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
786 struct page *
787 __i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, pgoff_t n)
788 {
789 	struct page *page;
790 
791 	page = i915_gem_object_get_page(obj, n);
792 	if (!obj->mm.dirty)
793 		set_page_dirty(page);
794 
795 	return page;
796 }
797 
798 dma_addr_t
799 __i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
800 				      pgoff_t n, unsigned int *len)
801 {
802 	struct scatterlist *sg;
803 	unsigned int offset;
804 
805 	sg = i915_gem_object_get_sg_dma(obj, n, &offset);
806 
807 	if (len)
808 		*len = sg_dma_len(sg) - (offset << PAGE_SHIFT);
809 
810 	return sg_dma_address(sg) + (offset << PAGE_SHIFT);
811 }
812 
813 dma_addr_t
814 __i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, pgoff_t n)
815 {
816 	return i915_gem_object_get_dma_address_len(obj, n, NULL);
817 }
818