110be98a7SChris Wilson /*
210be98a7SChris Wilson  * SPDX-License-Identifier: MIT
310be98a7SChris Wilson  *
410be98a7SChris Wilson  * Copyright © 2014-2016 Intel Corporation
510be98a7SChris Wilson  */
610be98a7SChris Wilson 
710be98a7SChris Wilson #include <linux/scatterlist.h>
810be98a7SChris Wilson #include <linux/slab.h>
910be98a7SChris Wilson 
1010be98a7SChris Wilson #include "i915_drv.h"
1110be98a7SChris Wilson #include "i915_gem.h"
12b508d01fSJani Nikula #include "i915_gem_internal.h"
1310be98a7SChris Wilson #include "i915_gem_object.h"
1437d63f8fSChris Wilson #include "i915_scatterlist.h"
1510be98a7SChris Wilson #include "i915_utils.h"
1610be98a7SChris Wilson 
1710be98a7SChris Wilson #define QUIET (__GFP_NORETRY | __GFP_NOWARN)
1810be98a7SChris Wilson #define MAYFAIL (__GFP_RETRY_MAYFAIL | __GFP_NOWARN)
1910be98a7SChris Wilson 
internal_free_pages(struct sg_table * st)2010be98a7SChris Wilson static void internal_free_pages(struct sg_table *st)
2110be98a7SChris Wilson {
2210be98a7SChris Wilson 	struct scatterlist *sg;
2310be98a7SChris Wilson 
2410be98a7SChris Wilson 	for (sg = st->sgl; sg; sg = __sg_next(sg)) {
2510be98a7SChris Wilson 		if (sg_page(sg))
2610be98a7SChris Wilson 			__free_pages(sg_page(sg), get_order(sg->length));
2710be98a7SChris Wilson 	}
2810be98a7SChris Wilson 
2910be98a7SChris Wilson 	sg_free_table(st);
3010be98a7SChris Wilson 	kfree(st);
3110be98a7SChris Wilson }
3210be98a7SChris Wilson 
i915_gem_object_get_pages_internal(struct drm_i915_gem_object * obj)3310be98a7SChris Wilson static int i915_gem_object_get_pages_internal(struct drm_i915_gem_object *obj)
3410be98a7SChris Wilson {
3510be98a7SChris Wilson 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
3610be98a7SChris Wilson 	struct sg_table *st;
3710be98a7SChris Wilson 	struct scatterlist *sg;
38c3bfba9aSChris Wilson 	unsigned int npages; /* restricted by sg_alloc_table */
39*23baf831SKirill A. Shutemov 	int max_order = MAX_ORDER;
4078a07fe7SRobert Beckett 	unsigned int max_segment;
4110be98a7SChris Wilson 	gfp_t gfp;
4210be98a7SChris Wilson 
43c3bfba9aSChris Wilson 	if (overflows_type(obj->base.size >> PAGE_SHIFT, npages))
44c3bfba9aSChris Wilson 		return -E2BIG;
45c3bfba9aSChris Wilson 
46c3bfba9aSChris Wilson 	npages = obj->base.size >> PAGE_SHIFT;
4778a07fe7SRobert Beckett 	max_segment = i915_sg_segment_size(i915->drm.dev) >> PAGE_SHIFT;
4878a07fe7SRobert Beckett 	max_order = min(max_order, get_order(max_segment));
4910be98a7SChris Wilson 
5010be98a7SChris Wilson 	gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_RECLAIMABLE;
5110be98a7SChris Wilson 	if (IS_I965GM(i915) || IS_I965G(i915)) {
5210be98a7SChris Wilson 		/* 965gm cannot relocate objects above 4GiB. */
5310be98a7SChris Wilson 		gfp &= ~__GFP_HIGHMEM;
5410be98a7SChris Wilson 		gfp |= __GFP_DMA32;
5510be98a7SChris Wilson 	}
5610be98a7SChris Wilson 
5710be98a7SChris Wilson create_st:
5810be98a7SChris Wilson 	st = kmalloc(sizeof(*st), GFP_KERNEL);
5910be98a7SChris Wilson 	if (!st)
6010be98a7SChris Wilson 		return -ENOMEM;
6110be98a7SChris Wilson 
6210be98a7SChris Wilson 	if (sg_alloc_table(st, npages, GFP_KERNEL)) {
6310be98a7SChris Wilson 		kfree(st);
6410be98a7SChris Wilson 		return -ENOMEM;
6510be98a7SChris Wilson 	}
6610be98a7SChris Wilson 
6710be98a7SChris Wilson 	sg = st->sgl;
6810be98a7SChris Wilson 	st->nents = 0;
6910be98a7SChris Wilson 
7010be98a7SChris Wilson 	do {
7110be98a7SChris Wilson 		int order = min(fls(npages) - 1, max_order);
7210be98a7SChris Wilson 		struct page *page;
7310be98a7SChris Wilson 
7410be98a7SChris Wilson 		do {
7510be98a7SChris Wilson 			page = alloc_pages(gfp | (order ? QUIET : MAYFAIL),
7610be98a7SChris Wilson 					   order);
7710be98a7SChris Wilson 			if (page)
7810be98a7SChris Wilson 				break;
7910be98a7SChris Wilson 			if (!order--)
8010be98a7SChris Wilson 				goto err;
8110be98a7SChris Wilson 
8210be98a7SChris Wilson 			/* Limit subsequent allocations as well */
8310be98a7SChris Wilson 			max_order = order;
8410be98a7SChris Wilson 		} while (1);
8510be98a7SChris Wilson 
8610be98a7SChris Wilson 		sg_set_page(sg, page, PAGE_SIZE << order, 0);
8710be98a7SChris Wilson 		st->nents++;
8810be98a7SChris Wilson 
8910be98a7SChris Wilson 		npages -= 1 << order;
9010be98a7SChris Wilson 		if (!npages) {
9110be98a7SChris Wilson 			sg_mark_end(sg);
9210be98a7SChris Wilson 			break;
9310be98a7SChris Wilson 		}
9410be98a7SChris Wilson 
9510be98a7SChris Wilson 		sg = __sg_next(sg);
9610be98a7SChris Wilson 	} while (1);
9710be98a7SChris Wilson 
9810be98a7SChris Wilson 	if (i915_gem_gtt_prepare_pages(obj, st)) {
9910be98a7SChris Wilson 		/* Failed to dma-map try again with single page sg segments */
10010be98a7SChris Wilson 		if (get_order(st->sgl->length)) {
10110be98a7SChris Wilson 			internal_free_pages(st);
10210be98a7SChris Wilson 			max_order = 0;
10310be98a7SChris Wilson 			goto create_st;
10410be98a7SChris Wilson 		}
10510be98a7SChris Wilson 		goto err;
10610be98a7SChris Wilson 	}
10710be98a7SChris Wilson 
1088c949515SMatthew Auld 	__i915_gem_object_set_pages(obj, st);
10910be98a7SChris Wilson 
11010be98a7SChris Wilson 	return 0;
11110be98a7SChris Wilson 
11210be98a7SChris Wilson err:
11310be98a7SChris Wilson 	sg_set_page(sg, NULL, 0, 0);
11410be98a7SChris Wilson 	sg_mark_end(sg);
11510be98a7SChris Wilson 	internal_free_pages(st);
11610be98a7SChris Wilson 
11710be98a7SChris Wilson 	return -ENOMEM;
11810be98a7SChris Wilson }
11910be98a7SChris Wilson 
i915_gem_object_put_pages_internal(struct drm_i915_gem_object * obj,struct sg_table * pages)12010be98a7SChris Wilson static void i915_gem_object_put_pages_internal(struct drm_i915_gem_object *obj,
12110be98a7SChris Wilson 					       struct sg_table *pages)
12210be98a7SChris Wilson {
12310be98a7SChris Wilson 	i915_gem_gtt_finish_pages(obj, pages);
12410be98a7SChris Wilson 	internal_free_pages(pages);
12510be98a7SChris Wilson 
12610be98a7SChris Wilson 	obj->mm.dirty = false;
1273884d8afSMatthew Auld 
1283884d8afSMatthew Auld 	__start_cpu_write(obj);
12910be98a7SChris Wilson }
13010be98a7SChris Wilson 
13110be98a7SChris Wilson static const struct drm_i915_gem_object_ops i915_gem_object_internal_ops = {
1327d192daaSChris Wilson 	.name = "i915_gem_object_internal",
133c471748dSMaarten Lankhorst 	.flags = I915_GEM_OBJECT_IS_SHRINKABLE,
13410be98a7SChris Wilson 	.get_pages = i915_gem_object_get_pages_internal,
13510be98a7SChris Wilson 	.put_pages = i915_gem_object_put_pages_internal,
13610be98a7SChris Wilson };
13710be98a7SChris Wilson 
138b0b0f2d2SMaarten Lankhorst struct drm_i915_gem_object *
__i915_gem_object_create_internal(struct drm_i915_private * i915,const struct drm_i915_gem_object_ops * ops,phys_addr_t size)139b0b0f2d2SMaarten Lankhorst __i915_gem_object_create_internal(struct drm_i915_private *i915,
140b0b0f2d2SMaarten Lankhorst 				  const struct drm_i915_gem_object_ops *ops,
141b0b0f2d2SMaarten Lankhorst 				  phys_addr_t size)
142b0b0f2d2SMaarten Lankhorst {
143b0b0f2d2SMaarten Lankhorst 	static struct lock_class_key lock_class;
144b0b0f2d2SMaarten Lankhorst 	struct drm_i915_gem_object *obj;
145b0b0f2d2SMaarten Lankhorst 	unsigned int cache_level;
146b0b0f2d2SMaarten Lankhorst 
147b0b0f2d2SMaarten Lankhorst 	GEM_BUG_ON(!size);
148b0b0f2d2SMaarten Lankhorst 	GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
149b0b0f2d2SMaarten Lankhorst 
150b0b0f2d2SMaarten Lankhorst 	if (overflows_type(size, obj->base.size))
151b0b0f2d2SMaarten Lankhorst 		return ERR_PTR(-E2BIG);
152b0b0f2d2SMaarten Lankhorst 
153b0b0f2d2SMaarten Lankhorst 	obj = i915_gem_object_alloc();
154b0b0f2d2SMaarten Lankhorst 	if (!obj)
155b0b0f2d2SMaarten Lankhorst 		return ERR_PTR(-ENOMEM);
156b0b0f2d2SMaarten Lankhorst 
157b0b0f2d2SMaarten Lankhorst 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
158b0b0f2d2SMaarten Lankhorst 	i915_gem_object_init(obj, ops, &lock_class, 0);
159b0b0f2d2SMaarten Lankhorst 	obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
160b0b0f2d2SMaarten Lankhorst 
161b0b0f2d2SMaarten Lankhorst 	/*
162b0b0f2d2SMaarten Lankhorst 	 * Mark the object as volatile, such that the pages are marked as
163b0b0f2d2SMaarten Lankhorst 	 * dontneed whilst they are still pinned. As soon as they are unpinned
164b0b0f2d2SMaarten Lankhorst 	 * they are allowed to be reaped by the shrinker, and the caller is
165b0b0f2d2SMaarten Lankhorst 	 * expected to repopulate - the contents of this object are only valid
166b0b0f2d2SMaarten Lankhorst 	 * whilst active and pinned.
167b0b0f2d2SMaarten Lankhorst 	 */
168b0b0f2d2SMaarten Lankhorst 	i915_gem_object_set_volatile(obj);
169b0b0f2d2SMaarten Lankhorst 
170b0b0f2d2SMaarten Lankhorst 	obj->read_domains = I915_GEM_DOMAIN_CPU;
171b0b0f2d2SMaarten Lankhorst 	obj->write_domain = I915_GEM_DOMAIN_CPU;
172b0b0f2d2SMaarten Lankhorst 
173b0b0f2d2SMaarten Lankhorst 	cache_level = HAS_LLC(i915) ? I915_CACHE_LLC : I915_CACHE_NONE;
174b0b0f2d2SMaarten Lankhorst 	i915_gem_object_set_cache_coherency(obj, cache_level);
175b0b0f2d2SMaarten Lankhorst 
176b0b0f2d2SMaarten Lankhorst 	return obj;
177b0b0f2d2SMaarten Lankhorst }
178b0b0f2d2SMaarten Lankhorst 
17910be98a7SChris Wilson /**
18010be98a7SChris Wilson  * i915_gem_object_create_internal: create an object with volatile pages
18110be98a7SChris Wilson  * @i915: the i915 device
18210be98a7SChris Wilson  * @size: the size in bytes of backing storage to allocate for the object
18310be98a7SChris Wilson  *
18410be98a7SChris Wilson  * Creates a new object that wraps some internal memory for private use.
18510be98a7SChris Wilson  * This object is not backed by swappable storage, and as such its contents
18610be98a7SChris Wilson  * are volatile and only valid whilst pinned. If the object is reaped by the
18710be98a7SChris Wilson  * shrinker, its pages and data will be discarded. Equally, it is not a full
18810be98a7SChris Wilson  * GEM object and so not valid for access from userspace. This makes it useful
18910be98a7SChris Wilson  * for hardware interfaces like ringbuffers (which are pinned from the time
19010be98a7SChris Wilson  * the request is written to the time the hardware stops accessing it), but
19110be98a7SChris Wilson  * not for contexts (which need to be preserved when not active for later
19210be98a7SChris Wilson  * reuse). Note that it is not cleared upon allocation.
19310be98a7SChris Wilson  */
19410be98a7SChris Wilson struct drm_i915_gem_object *
i915_gem_object_create_internal(struct drm_i915_private * i915,phys_addr_t size)19510be98a7SChris Wilson i915_gem_object_create_internal(struct drm_i915_private *i915,
19610be98a7SChris Wilson 				phys_addr_t size)
19710be98a7SChris Wilson {
198b0b0f2d2SMaarten Lankhorst 	return __i915_gem_object_create_internal(i915, &i915_gem_object_internal_ops, size);
19910be98a7SChris Wilson }
200