blob: 2b9d5e94a8ae028fa4243f2654f69e047a1876c2 [file] [log] [blame]
Chris Wilson920cf412016-10-28 13:58:30 +01001/*
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
Chris Wilson4703b042017-01-31 10:46:30 +000038 for (sg = st->sgl; sg; sg = __sg_next(sg)) {
39 if (sg_page(sg))
40 __free_pages(sg_page(sg), get_order(sg->length));
41 }
Chris Wilson920cf412016-10-28 13:58:30 +010042
43 sg_free_table(st);
44 kfree(st);
45}
46
Chris Wilson03ac84f2016-10-28 13:58:36 +010047static struct sg_table *
48i915_gem_object_get_pages_internal(struct drm_i915_gem_object *obj)
Chris Wilson920cf412016-10-28 13:58:30 +010049{
50 struct drm_i915_private *i915 = to_i915(obj->base.dev);
51 unsigned int npages = obj->base.size / PAGE_SIZE;
52 struct sg_table *st;
53 struct scatterlist *sg;
54 int max_order;
55 gfp_t gfp;
56
57 st = kmalloc(sizeof(*st), GFP_KERNEL);
58 if (!st)
Chris Wilson03ac84f2016-10-28 13:58:36 +010059 return ERR_PTR(-ENOMEM);
Chris Wilson920cf412016-10-28 13:58:30 +010060
61 if (sg_alloc_table(st, npages, GFP_KERNEL)) {
62 kfree(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +010063 return ERR_PTR(-ENOMEM);
Chris Wilson920cf412016-10-28 13:58:30 +010064 }
65
66 sg = st->sgl;
67 st->nents = 0;
68
69 max_order = MAX_ORDER;
70#ifdef CONFIG_SWIOTLB
Juergen Gross5584f1b2017-02-02 10:47:11 +010071 if (swiotlb_nr_tbl()) {
72 unsigned int max_segment;
73
74 max_segment = swiotlb_max_segment();
75 if (max_segment) {
76 max_segment = max_t(unsigned int, max_segment,
77 PAGE_SIZE) >> PAGE_SHIFT;
78 max_order = min(max_order, ilog2(max_segment));
79 }
80 }
Chris Wilson920cf412016-10-28 13:58:30 +010081#endif
82
83 gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +020084 if (IS_I965GM(i915) || IS_I965G(i915)) {
Chris Wilson920cf412016-10-28 13:58:30 +010085 /* 965gm cannot relocate objects above 4GiB. */
86 gfp &= ~__GFP_HIGHMEM;
87 gfp |= __GFP_DMA32;
88 }
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);
Chris Wilson920cf412016-10-28 13:58:30 +0100116
Chris Wilson03ac84f2016-10-28 13:58:36 +0100117 if (i915_gem_gtt_prepare_pages(obj, st))
Chris Wilson920cf412016-10-28 13:58:30 +0100118 goto err;
Chris Wilson920cf412016-10-28 13:58:30 +0100119
120 /* Mark the pages as dontneed whilst they are still pinned. As soon
121 * as they are unpinned they are allowed to be reaped by the shrinker,
122 * and the caller is expected to repopulate - the contents of this
123 * object are only valid whilst active and pinned.
124 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100125 obj->mm.madv = I915_MADV_DONTNEED;
Chris Wilson03ac84f2016-10-28 13:58:36 +0100126 return st;
Chris Wilson920cf412016-10-28 13:58:30 +0100127
128err:
Chris Wilson4703b042017-01-31 10:46:30 +0000129 sg_set_page(sg, NULL, 0, 0);
Chris Wilson920cf412016-10-28 13:58:30 +0100130 sg_mark_end(sg);
131 internal_free_pages(st);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100132 return ERR_PTR(-ENOMEM);
Chris Wilson920cf412016-10-28 13:58:30 +0100133}
134
Chris Wilson03ac84f2016-10-28 13:58:36 +0100135static void i915_gem_object_put_pages_internal(struct drm_i915_gem_object *obj,
136 struct sg_table *pages)
Chris Wilson920cf412016-10-28 13:58:30 +0100137{
Chris Wilson03ac84f2016-10-28 13:58:36 +0100138 i915_gem_gtt_finish_pages(obj, pages);
139 internal_free_pages(pages);
Chris Wilson920cf412016-10-28 13:58:30 +0100140
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100141 obj->mm.dirty = false;
142 obj->mm.madv = I915_MADV_WILLNEED;
Chris Wilson920cf412016-10-28 13:58:30 +0100143}
144
145static const struct drm_i915_gem_object_ops i915_gem_object_internal_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +0000146 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
147 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson920cf412016-10-28 13:58:30 +0100148 .get_pages = i915_gem_object_get_pages_internal,
149 .put_pages = i915_gem_object_put_pages_internal,
150};
151
152/**
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.
162 */
163struct drm_i915_gem_object *
164i915_gem_object_create_internal(struct drm_i915_private *i915,
Chris Wilsonfcd46e52017-01-12 13:04:31 +0000165 phys_addr_t size)
Chris Wilson920cf412016-10-28 13:58:30 +0100166{
167 struct drm_i915_gem_object *obj;
168
Chris Wilsonfcd46e52017-01-12 13:04:31 +0000169 GEM_BUG_ON(!size);
Chris Wilsonbf6b2032017-01-16 14:52:42 +0000170 GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
Chris Wilsonfcd46e52017-01-12 13:04:31 +0000171
172 if (overflows_type(size, obj->base.size))
173 return ERR_PTR(-E2BIG);
174
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000175 obj = i915_gem_object_alloc(i915);
Chris Wilson920cf412016-10-28 13:58:30 +0100176 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}