Commit | Line | Data |
---|---|---|
9797fbfb CW |
1 | /* |
2 | * Copyright © 2008-2012 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 | * Authors: | |
24 | * Eric Anholt <eric@anholt.net> | |
25 | * Chris Wilson <chris@chris-wilson.co.uk> | |
26 | * | |
27 | */ | |
28 | ||
760285e7 DH |
29 | #include <drm/drmP.h> |
30 | #include <drm/i915_drm.h> | |
9797fbfb CW |
31 | #include "i915_drv.h" |
32 | ||
33 | /* | |
34 | * The BIOS typically reserves some of the system's memory for the exclusive | |
35 | * use of the integrated graphics. This memory is no longer available for | |
36 | * use by the OS and so the user finds that his system has less memory | |
37 | * available than he put in. We refer to this memory as stolen. | |
38 | * | |
39 | * The BIOS will allocate its framebuffer from the stolen memory. Our | |
40 | * goal is try to reuse that object for our own fbcon which must always | |
41 | * be available for panics. Anything else we can reuse the stolen memory | |
42 | * for is a boon. | |
43 | */ | |
44 | ||
e12a2d53 | 45 | static unsigned long i915_stolen_to_physical(struct drm_device *dev) |
9797fbfb CW |
46 | { |
47 | struct drm_i915_private *dev_priv = dev->dev_private; | |
eaba1b8f | 48 | struct resource *r; |
9797fbfb CW |
49 | u32 base; |
50 | ||
17fec8a0 CW |
51 | /* Almost universally we can find the Graphics Base of Stolen Memory |
52 | * at offset 0x5c in the igfx configuration space. On a few (desktop) | |
53 | * machines this is also mirrored in the bridge device at different | |
54 | * locations, or in the MCHBAR. On gen2, the layout is again slightly | |
55 | * different with the Graphics Segment immediately following Top of | |
56 | * Memory (or Top of Usable DRAM). Note it appears that TOUD is only | |
57 | * reported by 865g, so we just use the top of memory as determined | |
58 | * by the e820 probe. | |
e12a2d53 | 59 | * |
17fec8a0 | 60 | * XXX However gen2 requires an unavailable symbol. |
9797fbfb | 61 | */ |
e12a2d53 | 62 | base = 0; |
17fec8a0 CW |
63 | if (INTEL_INFO(dev)->gen >= 3) { |
64 | /* Read Graphics Base of Stolen Memory directly */ | |
c9cddffc JB |
65 | pci_read_config_dword(dev->pdev, 0x5c, &base); |
66 | base &= ~((1<<20) - 1); | |
17fec8a0 | 67 | } else { /* GEN2 */ |
e12a2d53 | 68 | #if 0 |
e12a2d53 CW |
69 | /* Stolen is immediately above Top of Memory */ |
70 | base = max_low_pfn_mapped << PAGE_SHIFT; | |
9797fbfb | 71 | #endif |
e12a2d53 | 72 | } |
9797fbfb | 73 | |
eaba1b8f CW |
74 | if (base == 0) |
75 | return 0; | |
76 | ||
f1e1c212 VS |
77 | /* make sure we don't clobber the GTT if it's within stolen memory */ |
78 | if (INTEL_INFO(dev)->gen <= 4 && !IS_G33(dev) && !IS_G4X(dev)) { | |
79 | struct { | |
80 | u32 start, end; | |
81 | } stolen[2] = { | |
82 | { .start = base, .end = base + dev_priv->gtt.stolen_size, }, | |
83 | { .start = base, .end = base + dev_priv->gtt.stolen_size, }, | |
84 | }; | |
85 | u64 gtt_start, gtt_end; | |
86 | ||
87 | gtt_start = I915_READ(PGTBL_CTL); | |
88 | if (IS_GEN4(dev)) | |
89 | gtt_start = (gtt_start & PGTBL_ADDRESS_LO_MASK) | | |
90 | (gtt_start & PGTBL_ADDRESS_HI_MASK) << 28; | |
91 | else | |
92 | gtt_start &= PGTBL_ADDRESS_LO_MASK; | |
93 | gtt_end = gtt_start + gtt_total_entries(dev_priv->gtt) * 4; | |
94 | ||
95 | if (gtt_start >= stolen[0].start && gtt_start < stolen[0].end) | |
96 | stolen[0].end = gtt_start; | |
97 | if (gtt_end > stolen[1].start && gtt_end <= stolen[1].end) | |
98 | stolen[1].start = gtt_end; | |
99 | ||
100 | /* pick the larger of the two chunks */ | |
101 | if (stolen[0].end - stolen[0].start > | |
102 | stolen[1].end - stolen[1].start) { | |
103 | base = stolen[0].start; | |
104 | dev_priv->gtt.stolen_size = stolen[0].end - stolen[0].start; | |
105 | } else { | |
106 | base = stolen[1].start; | |
107 | dev_priv->gtt.stolen_size = stolen[1].end - stolen[1].start; | |
108 | } | |
109 | ||
110 | if (stolen[0].start != stolen[1].start || | |
111 | stolen[0].end != stolen[1].end) { | |
112 | DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n", | |
113 | (unsigned long long) gtt_start, | |
114 | (unsigned long long) gtt_end - 1); | |
115 | DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n", | |
116 | base, base + (u32) dev_priv->gtt.stolen_size - 1); | |
117 | } | |
118 | } | |
119 | ||
120 | ||
eaba1b8f CW |
121 | /* Verify that nothing else uses this physical address. Stolen |
122 | * memory should be reserved by the BIOS and hidden from the | |
123 | * kernel. So if the region is already marked as busy, something | |
124 | * is seriously wrong. | |
125 | */ | |
126 | r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size, | |
127 | "Graphics Stolen Memory"); | |
128 | if (r == NULL) { | |
3617dc96 AG |
129 | /* |
130 | * One more attempt but this time requesting region from | |
131 | * base + 1, as we have seen that this resolves the region | |
132 | * conflict with the PCI Bus. | |
133 | * This is a BIOS w/a: Some BIOS wrap stolen in the root | |
134 | * PCI bus, but have an off-by-one error. Hence retry the | |
135 | * reservation starting from 1 instead of 0. | |
136 | */ | |
137 | r = devm_request_mem_region(dev->dev, base + 1, | |
138 | dev_priv->gtt.stolen_size - 1, | |
139 | "Graphics Stolen Memory"); | |
0b6d24c0 DV |
140 | /* |
141 | * GEN3 firmware likes to smash pci bridges into the stolen | |
142 | * range. Apparently this works. | |
143 | */ | |
144 | if (r == NULL && !IS_GEN3(dev)) { | |
3617dc96 AG |
145 | DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n", |
146 | base, base + (uint32_t)dev_priv->gtt.stolen_size); | |
147 | base = 0; | |
148 | } | |
eaba1b8f CW |
149 | } |
150 | ||
e12a2d53 | 151 | return base; |
9797fbfb CW |
152 | } |
153 | ||
edc0fdbb BW |
154 | static int find_compression_threshold(struct drm_device *dev, |
155 | struct drm_mm_node *node, | |
5e59f717 BW |
156 | int size, |
157 | int fb_cpp) | |
9797fbfb CW |
158 | { |
159 | struct drm_i915_private *dev_priv = dev->dev_private; | |
5e59f717 | 160 | int compression_threshold = 1; |
06e78edf | 161 | int ret; |
9797fbfb | 162 | |
5e59f717 BW |
163 | /* HACK: This code depends on what we will do in *_enable_fbc. If that |
164 | * code changes, this code needs to change as well. | |
165 | * | |
166 | * The enable_fbc code will attempt to use one of our 2 compression | |
167 | * thresholds, therefore, in that case, we only have 1 resort. | |
168 | */ | |
169 | ||
170 | /* Try to over-allocate to reduce reallocations and fragmentation. */ | |
edc0fdbb | 171 | ret = drm_mm_insert_node(&dev_priv->mm.stolen, node, |
06e78edf | 172 | size <<= 1, 4096, DRM_MM_SEARCH_DEFAULT); |
5e59f717 BW |
173 | if (ret == 0) |
174 | return compression_threshold; | |
175 | ||
176 | again: | |
177 | /* HW's ability to limit the CFB is 1:4 */ | |
178 | if (compression_threshold > 4 || | |
179 | (fb_cpp == 2 && compression_threshold == 2)) | |
edc0fdbb | 180 | return 0; |
5e59f717 BW |
181 | |
182 | ret = drm_mm_insert_node(&dev_priv->mm.stolen, node, | |
183 | size >>= 1, 4096, | |
184 | DRM_MM_SEARCH_DEFAULT); | |
185 | if (ret && INTEL_INFO(dev)->gen <= 4) { | |
186 | return 0; | |
187 | } else if (ret) { | |
188 | compression_threshold <<= 1; | |
189 | goto again; | |
190 | } else { | |
edc0fdbb | 191 | return compression_threshold; |
5e59f717 | 192 | } |
edc0fdbb BW |
193 | } |
194 | ||
5e59f717 | 195 | static int i915_setup_compression(struct drm_device *dev, int size, int fb_cpp) |
edc0fdbb BW |
196 | { |
197 | struct drm_i915_private *dev_priv = dev->dev_private; | |
198 | struct drm_mm_node *uninitialized_var(compressed_llb); | |
199 | int ret; | |
200 | ||
201 | ret = find_compression_threshold(dev, &dev_priv->fbc.compressed_fb, | |
5e59f717 | 202 | size, fb_cpp); |
edc0fdbb | 203 | if (!ret) |
06e78edf | 204 | goto err_llb; |
5e59f717 BW |
205 | else if (ret > 1) { |
206 | DRM_INFO("Reducing the compressed framebuffer size. This may lead to less power savings than a non-reduced-size. Try to increase stolen memory size if available in BIOS.\n"); | |
207 | ||
208 | } | |
209 | ||
210 | dev_priv->fbc.threshold = ret; | |
9797fbfb | 211 | |
46ec15f2 | 212 | if (INTEL_INFO(dev_priv)->gen >= 5) |
c4213885 | 213 | I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start); |
11be49eb | 214 | else if (IS_GM45(dev)) { |
c4213885 | 215 | I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start); |
11be49eb | 216 | } else { |
06e78edf | 217 | compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL); |
9797fbfb CW |
218 | if (!compressed_llb) |
219 | goto err_fb; | |
220 | ||
06e78edf DH |
221 | ret = drm_mm_insert_node(&dev_priv->mm.stolen, compressed_llb, |
222 | 4096, 4096, DRM_MM_SEARCH_DEFAULT); | |
223 | if (ret) | |
224 | goto err_fb; | |
225 | ||
5c3fe8b0 | 226 | dev_priv->fbc.compressed_llb = compressed_llb; |
11be49eb CW |
227 | |
228 | I915_WRITE(FBC_CFB_BASE, | |
c4213885 | 229 | dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start); |
11be49eb CW |
230 | I915_WRITE(FBC_LL_BASE, |
231 | dev_priv->mm.stolen_base + compressed_llb->start); | |
9797fbfb CW |
232 | } |
233 | ||
60ee5cd2 | 234 | dev_priv->fbc.uncompressed_size = size; |
9797fbfb | 235 | |
11be49eb CW |
236 | DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n", |
237 | size); | |
9797fbfb | 238 | |
11be49eb | 239 | return 0; |
9797fbfb | 240 | |
9797fbfb | 241 | err_fb: |
06e78edf | 242 | kfree(compressed_llb); |
c4213885 | 243 | drm_mm_remove_node(&dev_priv->fbc.compressed_fb); |
06e78edf | 244 | err_llb: |
d8241785 | 245 | pr_info_once("drm: not enough stolen space for compressed buffer (need %d more bytes), disabling. Hint: you may be able to increase stolen memory size in the BIOS to avoid this.\n", size); |
11be49eb CW |
246 | return -ENOSPC; |
247 | } | |
248 | ||
5e59f717 | 249 | int i915_gem_stolen_setup_compression(struct drm_device *dev, int size, int fb_cpp) |
11be49eb CW |
250 | { |
251 | struct drm_i915_private *dev_priv = dev->dev_private; | |
252 | ||
446f8d81 | 253 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
11be49eb CW |
254 | return -ENODEV; |
255 | ||
cb0a08c1 | 256 | if (size <= dev_priv->fbc.uncompressed_size) |
11be49eb CW |
257 | return 0; |
258 | ||
259 | /* Release any current block */ | |
260 | i915_gem_stolen_cleanup_compression(dev); | |
261 | ||
5e59f717 | 262 | return i915_setup_compression(dev, size, fb_cpp); |
9797fbfb CW |
263 | } |
264 | ||
11be49eb | 265 | void i915_gem_stolen_cleanup_compression(struct drm_device *dev) |
9797fbfb CW |
266 | { |
267 | struct drm_i915_private *dev_priv = dev->dev_private; | |
268 | ||
60ee5cd2 | 269 | if (dev_priv->fbc.uncompressed_size == 0) |
11be49eb CW |
270 | return; |
271 | ||
c4213885 | 272 | drm_mm_remove_node(&dev_priv->fbc.compressed_fb); |
11be49eb | 273 | |
06e78edf DH |
274 | if (dev_priv->fbc.compressed_llb) { |
275 | drm_mm_remove_node(dev_priv->fbc.compressed_llb); | |
276 | kfree(dev_priv->fbc.compressed_llb); | |
277 | } | |
11be49eb | 278 | |
60ee5cd2 | 279 | dev_priv->fbc.uncompressed_size = 0; |
9797fbfb CW |
280 | } |
281 | ||
282 | void i915_gem_cleanup_stolen(struct drm_device *dev) | |
283 | { | |
4d7bb011 DV |
284 | struct drm_i915_private *dev_priv = dev->dev_private; |
285 | ||
446f8d81 DV |
286 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
287 | return; | |
288 | ||
11be49eb | 289 | i915_gem_stolen_cleanup_compression(dev); |
4d7bb011 | 290 | drm_mm_takedown(&dev_priv->mm.stolen); |
9797fbfb CW |
291 | } |
292 | ||
293 | int i915_gem_init_stolen(struct drm_device *dev) | |
294 | { | |
295 | struct drm_i915_private *dev_priv = dev->dev_private; | |
40bae736 | 296 | u32 tmp; |
c9cddffc | 297 | int bios_reserved = 0; |
9797fbfb | 298 | |
0f4706d2 | 299 | #ifdef CONFIG_INTEL_IOMMU |
fcc9fe1a | 300 | if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) { |
0f4706d2 CW |
301 | DRM_INFO("DMAR active, disabling use of stolen memory\n"); |
302 | return 0; | |
303 | } | |
304 | #endif | |
305 | ||
6644a4e9 CW |
306 | if (dev_priv->gtt.stolen_size == 0) |
307 | return 0; | |
308 | ||
e12a2d53 CW |
309 | dev_priv->mm.stolen_base = i915_stolen_to_physical(dev); |
310 | if (dev_priv->mm.stolen_base == 0) | |
311 | return 0; | |
312 | ||
a54c0c27 BW |
313 | DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n", |
314 | dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base); | |
e12a2d53 | 315 | |
40bae736 DV |
316 | if (INTEL_INFO(dev)->gen >= 8) { |
317 | tmp = I915_READ(GEN7_BIOS_RESERVED); | |
318 | tmp >>= GEN8_BIOS_RESERVED_SHIFT; | |
319 | tmp &= GEN8_BIOS_RESERVED_MASK; | |
320 | bios_reserved = (1024*1024) << tmp; | |
321 | } else if (IS_GEN7(dev)) { | |
322 | tmp = I915_READ(GEN7_BIOS_RESERVED); | |
323 | bios_reserved = tmp & GEN7_BIOS_RESERVED_256K ? | |
324 | 256*1024 : 1024*1024; | |
325 | } | |
c9cddffc | 326 | |
897f9ed0 DV |
327 | if (WARN_ON(bios_reserved > dev_priv->gtt.stolen_size)) |
328 | return 0; | |
329 | ||
9797fbfb | 330 | /* Basic memrange allocator for stolen space */ |
c9cddffc JB |
331 | drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size - |
332 | bios_reserved); | |
9797fbfb CW |
333 | |
334 | return 0; | |
335 | } | |
0104fdbb CW |
336 | |
337 | static struct sg_table * | |
338 | i915_pages_create_for_stolen(struct drm_device *dev, | |
339 | u32 offset, u32 size) | |
340 | { | |
341 | struct drm_i915_private *dev_priv = dev->dev_private; | |
342 | struct sg_table *st; | |
343 | struct scatterlist *sg; | |
344 | ||
345 | DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size); | |
a54c0c27 | 346 | BUG_ON(offset > dev_priv->gtt.stolen_size - size); |
0104fdbb CW |
347 | |
348 | /* We hide that we have no struct page backing our stolen object | |
349 | * by wrapping the contiguous physical allocation with a fake | |
350 | * dma mapping in a single scatterlist. | |
351 | */ | |
352 | ||
353 | st = kmalloc(sizeof(*st), GFP_KERNEL); | |
354 | if (st == NULL) | |
355 | return NULL; | |
356 | ||
357 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { | |
358 | kfree(st); | |
359 | return NULL; | |
360 | } | |
361 | ||
362 | sg = st->sgl; | |
ec14ba47 | 363 | sg->offset = 0; |
ed23abdd | 364 | sg->length = size; |
0104fdbb CW |
365 | |
366 | sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset; | |
367 | sg_dma_len(sg) = size; | |
368 | ||
369 | return st; | |
370 | } | |
371 | ||
372 | static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj) | |
373 | { | |
374 | BUG(); | |
375 | return -EINVAL; | |
376 | } | |
377 | ||
378 | static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj) | |
379 | { | |
380 | /* Should only be called during free */ | |
381 | sg_free_table(obj->pages); | |
382 | kfree(obj->pages); | |
383 | } | |
384 | ||
ef0cf27c CW |
385 | |
386 | static void | |
387 | i915_gem_object_release_stolen(struct drm_i915_gem_object *obj) | |
388 | { | |
389 | if (obj->stolen) { | |
390 | drm_mm_remove_node(obj->stolen); | |
391 | kfree(obj->stolen); | |
392 | obj->stolen = NULL; | |
393 | } | |
394 | } | |
0104fdbb CW |
395 | static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = { |
396 | .get_pages = i915_gem_object_get_pages_stolen, | |
397 | .put_pages = i915_gem_object_put_pages_stolen, | |
ef0cf27c | 398 | .release = i915_gem_object_release_stolen, |
0104fdbb CW |
399 | }; |
400 | ||
401 | static struct drm_i915_gem_object * | |
402 | _i915_gem_object_create_stolen(struct drm_device *dev, | |
403 | struct drm_mm_node *stolen) | |
404 | { | |
405 | struct drm_i915_gem_object *obj; | |
406 | ||
42dcedd4 | 407 | obj = i915_gem_object_alloc(dev); |
0104fdbb CW |
408 | if (obj == NULL) |
409 | return NULL; | |
410 | ||
89c8233f | 411 | drm_gem_private_object_init(dev, &obj->base, stolen->size); |
0104fdbb CW |
412 | i915_gem_object_init(obj, &i915_gem_object_stolen_ops); |
413 | ||
414 | obj->pages = i915_pages_create_for_stolen(dev, | |
415 | stolen->start, stolen->size); | |
416 | if (obj->pages == NULL) | |
417 | goto cleanup; | |
418 | ||
dd53e1b0 | 419 | i915_gem_object_pin_pages(obj); |
0104fdbb CW |
420 | obj->stolen = stolen; |
421 | ||
d46f1c3f CW |
422 | obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT; |
423 | obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE; | |
0104fdbb CW |
424 | |
425 | return obj; | |
426 | ||
427 | cleanup: | |
42dcedd4 | 428 | i915_gem_object_free(obj); |
0104fdbb CW |
429 | return NULL; |
430 | } | |
431 | ||
432 | struct drm_i915_gem_object * | |
433 | i915_gem_object_create_stolen(struct drm_device *dev, u32 size) | |
434 | { | |
435 | struct drm_i915_private *dev_priv = dev->dev_private; | |
436 | struct drm_i915_gem_object *obj; | |
437 | struct drm_mm_node *stolen; | |
06e78edf | 438 | int ret; |
0104fdbb | 439 | |
446f8d81 | 440 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
0104fdbb CW |
441 | return NULL; |
442 | ||
443 | DRM_DEBUG_KMS("creating stolen object: size=%x\n", size); | |
444 | if (size == 0) | |
445 | return NULL; | |
446 | ||
06e78edf DH |
447 | stolen = kzalloc(sizeof(*stolen), GFP_KERNEL); |
448 | if (!stolen) | |
0104fdbb CW |
449 | return NULL; |
450 | ||
06e78edf DH |
451 | ret = drm_mm_insert_node(&dev_priv->mm.stolen, stolen, size, |
452 | 4096, DRM_MM_SEARCH_DEFAULT); | |
453 | if (ret) { | |
454 | kfree(stolen); | |
455 | return NULL; | |
456 | } | |
457 | ||
0104fdbb CW |
458 | obj = _i915_gem_object_create_stolen(dev, stolen); |
459 | if (obj) | |
460 | return obj; | |
461 | ||
06e78edf DH |
462 | drm_mm_remove_node(stolen); |
463 | kfree(stolen); | |
0104fdbb CW |
464 | return NULL; |
465 | } | |
466 | ||
866d12b4 CW |
467 | struct drm_i915_gem_object * |
468 | i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev, | |
469 | u32 stolen_offset, | |
470 | u32 gtt_offset, | |
471 | u32 size) | |
472 | { | |
473 | struct drm_i915_private *dev_priv = dev->dev_private; | |
40d74980 | 474 | struct i915_address_space *ggtt = &dev_priv->gtt.base; |
866d12b4 CW |
475 | struct drm_i915_gem_object *obj; |
476 | struct drm_mm_node *stolen; | |
2f633156 | 477 | struct i915_vma *vma; |
b3a070cc | 478 | int ret; |
866d12b4 | 479 | |
446f8d81 | 480 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
866d12b4 CW |
481 | return NULL; |
482 | ||
483 | DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n", | |
484 | stolen_offset, gtt_offset, size); | |
485 | ||
486 | /* KISS and expect everything to be page-aligned */ | |
f37b5c2b DV |
487 | if (WARN_ON(size == 0) || WARN_ON(size & 4095) || |
488 | WARN_ON(stolen_offset & 4095)) | |
866d12b4 CW |
489 | return NULL; |
490 | ||
b3a070cc BW |
491 | stolen = kzalloc(sizeof(*stolen), GFP_KERNEL); |
492 | if (!stolen) | |
493 | return NULL; | |
494 | ||
338710e7 BW |
495 | stolen->start = stolen_offset; |
496 | stolen->size = size; | |
497 | ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen); | |
b3a070cc | 498 | if (ret) { |
866d12b4 | 499 | DRM_DEBUG_KMS("failed to allocate stolen space\n"); |
b3a070cc | 500 | kfree(stolen); |
866d12b4 CW |
501 | return NULL; |
502 | } | |
503 | ||
504 | obj = _i915_gem_object_create_stolen(dev, stolen); | |
505 | if (obj == NULL) { | |
506 | DRM_DEBUG_KMS("failed to allocate stolen object\n"); | |
06e78edf DH |
507 | drm_mm_remove_node(stolen); |
508 | kfree(stolen); | |
866d12b4 CW |
509 | return NULL; |
510 | } | |
511 | ||
3727d55e | 512 | /* Some objects just need physical mem from stolen space */ |
190d6cd5 | 513 | if (gtt_offset == I915_GTT_OFFSET_NONE) |
3727d55e JB |
514 | return obj; |
515 | ||
e656a6cb | 516 | vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt); |
db473b36 DC |
517 | if (IS_ERR(vma)) { |
518 | ret = PTR_ERR(vma); | |
2f633156 BW |
519 | goto err_out; |
520 | } | |
521 | ||
866d12b4 CW |
522 | /* To simplify the initialisation sequence between KMS and GTT, |
523 | * we allow construction of the stolen object prior to | |
524 | * setting up the GTT space. The actual reservation will occur | |
525 | * later. | |
526 | */ | |
2f633156 BW |
527 | vma->node.start = gtt_offset; |
528 | vma->node.size = size; | |
40d74980 BW |
529 | if (drm_mm_initialized(&ggtt->mm)) { |
530 | ret = drm_mm_reserve_node(&ggtt->mm, &vma->node); | |
b3a070cc | 531 | if (ret) { |
866d12b4 | 532 | DRM_DEBUG_KMS("failed to allocate stolen GTT space\n"); |
4a025e26 | 533 | goto err_vma; |
866d12b4 | 534 | } |
edd41a87 | 535 | } |
866d12b4 | 536 | |
aff43766 | 537 | vma->bound |= GLOBAL_BIND; |
866d12b4 | 538 | |
35c20a60 | 539 | list_add_tail(&obj->global_list, &dev_priv->mm.bound_list); |
ca191b13 | 540 | list_add_tail(&vma->mm_list, &ggtt->inactive_list); |
d8ccba86 | 541 | i915_gem_object_pin_pages(obj); |
866d12b4 CW |
542 | |
543 | return obj; | |
b3a070cc | 544 | |
4a025e26 DV |
545 | err_vma: |
546 | i915_gem_vma_destroy(vma); | |
f7f18184 | 547 | err_out: |
32c913e4 DA |
548 | drm_mm_remove_node(stolen); |
549 | kfree(stolen); | |
b3a070cc BW |
550 | drm_gem_object_unreference(&obj->base); |
551 | return NULL; | |
866d12b4 | 552 | } |