2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2016 Intel Corporation
7 #include <linux/scatterlist.h>
8 #include <linux/slab.h>
9 #include <linux/swiotlb.h>
11 #include <drm/i915_drm.h>
15 #include "i915_gem_object.h"
16 #include "i915_scatterlist.h"
17 #include "i915_utils.h"
19 #define QUIET (__GFP_NORETRY | __GFP_NOWARN)
20 #define MAYFAIL (__GFP_RETRY_MAYFAIL | __GFP_NOWARN)
22 static void internal_free_pages(struct sg_table
*st
)
24 struct scatterlist
*sg
;
26 for (sg
= st
->sgl
; sg
; sg
= __sg_next(sg
)) {
28 __free_pages(sg_page(sg
), get_order(sg
->length
));
35 static int i915_gem_object_get_pages_internal(struct drm_i915_gem_object
*obj
)
37 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
39 struct scatterlist
*sg
;
40 unsigned int sg_page_sizes
;
45 max_order
= MAX_ORDER
;
47 if (swiotlb_nr_tbl()) {
48 unsigned int max_segment
;
50 max_segment
= swiotlb_max_segment();
52 max_segment
= max_t(unsigned int, max_segment
,
53 PAGE_SIZE
) >> PAGE_SHIFT
;
54 max_order
= min(max_order
, ilog2(max_segment
));
59 gfp
= GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_RECLAIMABLE
;
60 if (IS_I965GM(i915
) || IS_I965G(i915
)) {
61 /* 965gm cannot relocate objects above 4GiB. */
62 gfp
&= ~__GFP_HIGHMEM
;
67 st
= kmalloc(sizeof(*st
), GFP_KERNEL
);
71 npages
= obj
->base
.size
/ PAGE_SIZE
;
72 if (sg_alloc_table(st
, npages
, GFP_KERNEL
)) {
82 int order
= min(fls(npages
) - 1, max_order
);
86 page
= alloc_pages(gfp
| (order
? QUIET
: MAYFAIL
),
93 /* Limit subsequent allocations as well */
97 sg_set_page(sg
, page
, PAGE_SIZE
<< order
, 0);
98 sg_page_sizes
|= PAGE_SIZE
<< order
;
101 npages
-= 1 << order
;
110 if (i915_gem_gtt_prepare_pages(obj
, st
)) {
111 /* Failed to dma-map try again with single page sg segments */
112 if (get_order(st
->sgl
->length
)) {
113 internal_free_pages(st
);
120 __i915_gem_object_set_pages(obj
, st
, sg_page_sizes
);
125 sg_set_page(sg
, NULL
, 0, 0);
127 internal_free_pages(st
);
132 static void i915_gem_object_put_pages_internal(struct drm_i915_gem_object
*obj
,
133 struct sg_table
*pages
)
135 i915_gem_gtt_finish_pages(obj
, pages
);
136 internal_free_pages(pages
);
138 obj
->mm
.dirty
= false;
141 static const struct drm_i915_gem_object_ops i915_gem_object_internal_ops
= {
142 .flags
= I915_GEM_OBJECT_HAS_STRUCT_PAGE
|
143 I915_GEM_OBJECT_IS_SHRINKABLE
,
144 .get_pages
= i915_gem_object_get_pages_internal
,
145 .put_pages
= i915_gem_object_put_pages_internal
,
149 * i915_gem_object_create_internal: create an object with volatile pages
150 * @i915: the i915 device
151 * @size: the size in bytes of backing storage to allocate for the object
153 * Creates a new object that wraps some internal memory for private use.
154 * This object is not backed by swappable storage, and as such its contents
155 * are volatile and only valid whilst pinned. If the object is reaped by the
156 * shrinker, its pages and data will be discarded. Equally, it is not a full
157 * GEM object and so not valid for access from userspace. This makes it useful
158 * for hardware interfaces like ringbuffers (which are pinned from the time
159 * the request is written to the time the hardware stops accessing it), but
160 * not for contexts (which need to be preserved when not active for later
161 * reuse). Note that it is not cleared upon allocation.
163 struct drm_i915_gem_object
*
164 i915_gem_object_create_internal(struct drm_i915_private
*i915
,
167 static struct lock_class_key lock_class
;
168 struct drm_i915_gem_object
*obj
;
169 unsigned int cache_level
;
172 GEM_BUG_ON(!IS_ALIGNED(size
, PAGE_SIZE
));
174 if (overflows_type(size
, obj
->base
.size
))
175 return ERR_PTR(-E2BIG
);
177 obj
= i915_gem_object_alloc();
179 return ERR_PTR(-ENOMEM
);
181 drm_gem_private_object_init(&i915
->drm
, &obj
->base
, size
);
182 i915_gem_object_init(obj
, &i915_gem_object_internal_ops
, &lock_class
);
185 * Mark the object as volatile, such that the pages are marked as
186 * dontneed whilst they are still pinned. As soon as they are unpinned
187 * they are allowed to be reaped by the shrinker, and the caller is
188 * expected to repopulate - the contents of this object are only valid
189 * whilst active and pinned.
191 i915_gem_object_set_volatile(obj
);
193 obj
->read_domains
= I915_GEM_DOMAIN_CPU
;
194 obj
->write_domain
= I915_GEM_DOMAIN_CPU
;
196 cache_level
= HAS_LLC(i915
) ? I915_CACHE_LLC
: I915_CACHE_NONE
;
197 i915_gem_object_set_cache_coherency(obj
, cache_level
);