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