1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6 
7 #include "i915_scatterlist.h"
8 
9 #include "huge_gem_object.h"
10 
huge_free_pages(struct drm_i915_gem_object * obj,struct sg_table * pages)11 static void huge_free_pages(struct drm_i915_gem_object *obj,
12 			    struct sg_table *pages)
13 {
14 	unsigned long nreal = obj->scratch / PAGE_SIZE;
15 	struct sgt_iter sgt_iter;
16 	struct page *page;
17 
18 	for_each_sgt_page(page, sgt_iter, pages) {
19 		__free_page(page);
20 		if (!--nreal)
21 			break;
22 	}
23 
24 	sg_free_table(pages);
25 	kfree(pages);
26 }
27 
huge_get_pages(struct drm_i915_gem_object * obj)28 static int huge_get_pages(struct drm_i915_gem_object *obj)
29 {
30 #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL)
31 	const unsigned long nreal = obj->scratch / PAGE_SIZE;
32 	const unsigned long npages = obj->base.size / PAGE_SIZE;
33 	struct scatterlist *sg, *src, *end;
34 	struct sg_table *pages;
35 	unsigned long n;
36 
37 	pages = kmalloc(sizeof(*pages), GFP);
38 	if (!pages)
39 		return -ENOMEM;
40 
41 	if (sg_alloc_table(pages, npages, GFP)) {
42 		kfree(pages);
43 		return -ENOMEM;
44 	}
45 
46 	sg = pages->sgl;
47 	for (n = 0; n < nreal; n++) {
48 		struct page *page;
49 
50 		page = alloc_page(GFP | __GFP_HIGHMEM);
51 		if (!page) {
52 			sg_mark_end(sg);
53 			goto err;
54 		}
55 
56 		sg_set_page(sg, page, PAGE_SIZE, 0);
57 		sg = __sg_next(sg);
58 	}
59 	if (nreal < npages) {
60 		for (end = sg, src = pages->sgl; sg; sg = __sg_next(sg)) {
61 			sg_set_page(sg, sg_page(src), PAGE_SIZE, 0);
62 			src = __sg_next(src);
63 			if (src == end)
64 				src = pages->sgl;
65 		}
66 	}
67 
68 	if (i915_gem_gtt_prepare_pages(obj, pages))
69 		goto err;
70 
71 	__i915_gem_object_set_pages(obj, pages, PAGE_SIZE);
72 
73 	return 0;
74 
75 err:
76 	huge_free_pages(obj, pages);
77 	return -ENOMEM;
78 #undef GFP
79 }
80 
huge_put_pages(struct drm_i915_gem_object * obj,struct sg_table * pages)81 static void huge_put_pages(struct drm_i915_gem_object *obj,
82 			   struct sg_table *pages)
83 {
84 	i915_gem_gtt_finish_pages(obj, pages);
85 	huge_free_pages(obj, pages);
86 
87 	obj->mm.dirty = false;
88 }
89 
90 static const struct drm_i915_gem_object_ops huge_ops = {
91 	.name = "huge-gem",
92 	.get_pages = huge_get_pages,
93 	.put_pages = huge_put_pages,
94 };
95 
96 struct drm_i915_gem_object *
huge_gem_object(struct drm_i915_private * i915,phys_addr_t phys_size,dma_addr_t dma_size)97 huge_gem_object(struct drm_i915_private *i915,
98 		phys_addr_t phys_size,
99 		dma_addr_t dma_size)
100 {
101 	static struct lock_class_key lock_class;
102 	struct drm_i915_gem_object *obj;
103 	unsigned int cache_level;
104 
105 	GEM_BUG_ON(!phys_size || phys_size > dma_size);
106 	GEM_BUG_ON(!IS_ALIGNED(phys_size, PAGE_SIZE));
107 	GEM_BUG_ON(!IS_ALIGNED(dma_size, I915_GTT_PAGE_SIZE));
108 
109 	if (overflows_type(dma_size, obj->base.size))
110 		return ERR_PTR(-E2BIG);
111 
112 	obj = i915_gem_object_alloc();
113 	if (!obj)
114 		return ERR_PTR(-ENOMEM);
115 
116 	drm_gem_private_object_init(&i915->drm, &obj->base, dma_size);
117 	i915_gem_object_init(obj, &huge_ops, &lock_class, 0);
118 	obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
119 
120 	obj->read_domains = I915_GEM_DOMAIN_CPU;
121 	obj->write_domain = I915_GEM_DOMAIN_CPU;
122 	cache_level = HAS_LLC(i915) ? I915_CACHE_LLC : I915_CACHE_NONE;
123 	i915_gem_object_set_cache_coherency(obj, cache_level);
124 	obj->scratch = phys_size;
125 
126 	return obj;
127 }
128