Loading...
Note: File does not exist in v3.1.
1/*
2 * Copyright © 2014-2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include <drm/drmP.h>
26#include <drm/i915_drm.h>
27#include "i915_drv.h"
28
29#define QUIET (__GFP_NORETRY | __GFP_NOWARN)
30
31/* convert swiotlb segment size into sensible units (pages)! */
32#define IO_TLB_SEGPAGES (IO_TLB_SEGSIZE << IO_TLB_SHIFT >> PAGE_SHIFT)
33
34static void internal_free_pages(struct sg_table *st)
35{
36 struct scatterlist *sg;
37
38 for (sg = st->sgl; sg; sg = __sg_next(sg))
39 __free_pages(sg_page(sg), get_order(sg->length));
40
41 sg_free_table(st);
42 kfree(st);
43}
44
45static struct sg_table *
46i915_gem_object_get_pages_internal(struct drm_i915_gem_object *obj)
47{
48 struct drm_i915_private *i915 = to_i915(obj->base.dev);
49 struct sg_table *st;
50 struct scatterlist *sg;
51 unsigned int npages;
52 int max_order;
53 gfp_t gfp;
54
55 max_order = MAX_ORDER;
56#ifdef CONFIG_SWIOTLB
57 if (swiotlb_nr_tbl()) {
58 unsigned int max_segment;
59
60 max_segment = swiotlb_max_segment();
61 if (max_segment) {
62 max_segment = max_t(unsigned int, max_segment,
63 PAGE_SIZE) >> PAGE_SHIFT;
64 max_order = min(max_order, ilog2(max_segment));
65 }
66 }
67#endif
68
69 gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_RECLAIMABLE;
70 if (IS_CRESTLINE(i915) || IS_BROADWATER(i915)) {
71 /* 965gm cannot relocate objects above 4GiB. */
72 gfp &= ~__GFP_HIGHMEM;
73 gfp |= __GFP_DMA32;
74 }
75
76create_st:
77 st = kmalloc(sizeof(*st), GFP_KERNEL);
78 if (!st)
79 return ERR_PTR(-ENOMEM);
80
81 npages = obj->base.size / PAGE_SIZE;
82 if (sg_alloc_table(st, npages, GFP_KERNEL)) {
83 kfree(st);
84 return ERR_PTR(-ENOMEM);
85 }
86
87 sg = st->sgl;
88 st->nents = 0;
89
90 do {
91 int order = min(fls(npages) - 1, max_order);
92 struct page *page;
93
94 do {
95 page = alloc_pages(gfp | (order ? QUIET : 0), order);
96 if (page)
97 break;
98 if (!order--)
99 goto err;
100
101 /* Limit subsequent allocations as well */
102 max_order = order;
103 } while (1);
104
105 sg_set_page(sg, page, PAGE_SIZE << order, 0);
106 st->nents++;
107
108 npages -= 1 << order;
109 if (!npages) {
110 sg_mark_end(sg);
111 break;
112 }
113
114 sg = __sg_next(sg);
115 } while (1);
116
117 if (i915_gem_gtt_prepare_pages(obj, st)) {
118 /* Failed to dma-map try again with single page sg segments */
119 if (get_order(st->sgl->length)) {
120 internal_free_pages(st);
121 max_order = 0;
122 goto create_st;
123 }
124 goto err;
125 }
126
127 /* Mark the pages as dontneed whilst they are still pinned. As soon
128 * as they are unpinned they are allowed to be reaped by the shrinker,
129 * and the caller is expected to repopulate - the contents of this
130 * object are only valid whilst active and pinned.
131 */
132 obj->mm.madv = I915_MADV_DONTNEED;
133 return st;
134
135err:
136 sg_mark_end(sg);
137 internal_free_pages(st);
138 return ERR_PTR(-ENOMEM);
139}
140
141static void i915_gem_object_put_pages_internal(struct drm_i915_gem_object *obj,
142 struct sg_table *pages)
143{
144 i915_gem_gtt_finish_pages(obj, pages);
145 internal_free_pages(pages);
146
147 obj->mm.dirty = false;
148 obj->mm.madv = I915_MADV_WILLNEED;
149}
150
151static const struct drm_i915_gem_object_ops i915_gem_object_internal_ops = {
152 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
153 I915_GEM_OBJECT_IS_SHRINKABLE,
154 .get_pages = i915_gem_object_get_pages_internal,
155 .put_pages = i915_gem_object_put_pages_internal,
156};
157
158/**
159 * Creates a new object that wraps some internal memory for private use.
160 * This object is not backed by swappable storage, and as such its contents
161 * are volatile and only valid whilst pinned. If the object is reaped by the
162 * shrinker, its pages and data will be discarded. Equally, it is not a full
163 * GEM object and so not valid for access from userspace. This makes it useful
164 * for hardware interfaces like ringbuffers (which are pinned from the time
165 * the request is written to the time the hardware stops accessing it), but
166 * not for contexts (which need to be preserved when not active for later
167 * reuse). Note that it is not cleared upon allocation.
168 */
169struct drm_i915_gem_object *
170i915_gem_object_create_internal(struct drm_i915_private *i915,
171 unsigned int size)
172{
173 struct drm_i915_gem_object *obj;
174
175 obj = i915_gem_object_alloc(&i915->drm);
176 if (!obj)
177 return ERR_PTR(-ENOMEM);
178
179 drm_gem_private_object_init(&i915->drm, &obj->base, size);
180 i915_gem_object_init(obj, &i915_gem_object_internal_ops);
181
182 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
183 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
184 obj->cache_level = HAS_LLC(i915) ? I915_CACHE_LLC : I915_CACHE_NONE;
185
186 return obj;
187}