2 * Copyright © 2008-2012 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
30 #include <drm/i915_drm.h>
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.
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
45 int i915_gem_stolen_insert_node_in_range(struct drm_i915_private
*dev_priv
,
46 struct drm_mm_node
*node
, u64 size
,
47 unsigned alignment
, u64 start
, u64 end
)
51 if (!drm_mm_initialized(&dev_priv
->mm
.stolen
))
54 mutex_lock(&dev_priv
->mm
.stolen_lock
);
55 ret
= drm_mm_insert_node_in_range(&dev_priv
->mm
.stolen
, node
,
57 start
, end
, DRM_MM_INSERT_BEST
);
58 mutex_unlock(&dev_priv
->mm
.stolen_lock
);
63 int i915_gem_stolen_insert_node(struct drm_i915_private
*dev_priv
,
64 struct drm_mm_node
*node
, u64 size
,
67 return i915_gem_stolen_insert_node_in_range(dev_priv
, node
, size
,
68 alignment
, 0, U64_MAX
);
71 void i915_gem_stolen_remove_node(struct drm_i915_private
*dev_priv
,
72 struct drm_mm_node
*node
)
74 mutex_lock(&dev_priv
->mm
.stolen_lock
);
75 drm_mm_remove_node(node
);
76 mutex_unlock(&dev_priv
->mm
.stolen_lock
);
79 static int i915_adjust_stolen(struct drm_i915_private
*dev_priv
,
82 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
85 if (dsm
->start
== 0 || dsm
->end
<= dsm
->start
)
89 * TODO: We have yet too encounter the case where the GTT wasn't at the
90 * end of stolen. With that assumption we could simplify this.
93 /* Make sure we don't clobber the GTT if it's within stolen memory */
94 if (INTEL_GEN(dev_priv
) <= 4 &&
95 !IS_G33(dev_priv
) && !IS_PINEVIEW(dev_priv
) && !IS_G4X(dev_priv
)) {
96 struct resource stolen
[2] = {*dsm
, *dsm
};
97 struct resource ggtt_res
;
98 resource_size_t ggtt_start
;
100 ggtt_start
= I915_READ(PGTBL_CTL
);
101 if (IS_GEN4(dev_priv
))
102 ggtt_start
= (ggtt_start
& PGTBL_ADDRESS_LO_MASK
) |
103 (ggtt_start
& PGTBL_ADDRESS_HI_MASK
) << 28;
105 ggtt_start
&= PGTBL_ADDRESS_LO_MASK
;
108 (struct resource
) DEFINE_RES_MEM(ggtt_start
,
109 ggtt_total_entries(ggtt
) * 4);
111 if (ggtt_res
.start
>= stolen
[0].start
&& ggtt_res
.start
< stolen
[0].end
)
112 stolen
[0].end
= ggtt_res
.start
;
113 if (ggtt_res
.end
> stolen
[1].start
&& ggtt_res
.end
<= stolen
[1].end
)
114 stolen
[1].start
= ggtt_res
.end
;
116 /* Pick the larger of the two chunks */
117 if (resource_size(&stolen
[0]) > resource_size(&stolen
[1]))
122 if (stolen
[0].start
!= stolen
[1].start
||
123 stolen
[0].end
!= stolen
[1].end
) {
124 DRM_DEBUG_KMS("GTT within stolen memory at %pR\n", &ggtt_res
);
125 DRM_DEBUG_KMS("Stolen memory adjusted to %pR\n", dsm
);
130 * Verify that nothing else uses this physical address. Stolen
131 * memory should be reserved by the BIOS and hidden from the
132 * kernel. So if the region is already marked as busy, something
133 * is seriously wrong.
135 r
= devm_request_mem_region(dev_priv
->drm
.dev
, dsm
->start
,
137 "Graphics Stolen Memory");
140 * One more attempt but this time requesting region from
141 * start + 1, as we have seen that this resolves the region
142 * conflict with the PCI Bus.
143 * This is a BIOS w/a: Some BIOS wrap stolen in the root
144 * PCI bus, but have an off-by-one error. Hence retry the
145 * reservation starting from 1 instead of 0.
146 * There's also BIOS with off-by-one on the other end.
148 r
= devm_request_mem_region(dev_priv
->drm
.dev
, dsm
->start
+ 1,
149 resource_size(dsm
) - 2,
150 "Graphics Stolen Memory");
152 * GEN3 firmware likes to smash pci bridges into the stolen
153 * range. Apparently this works.
155 if (r
== NULL
&& !IS_GEN3(dev_priv
)) {
156 DRM_ERROR("conflict detected with stolen region: %pR\n",
166 void i915_gem_cleanup_stolen(struct drm_device
*dev
)
168 struct drm_i915_private
*dev_priv
= to_i915(dev
);
170 if (!drm_mm_initialized(&dev_priv
->mm
.stolen
))
173 drm_mm_takedown(&dev_priv
->mm
.stolen
);
176 static void g4x_get_stolen_reserved(struct drm_i915_private
*dev_priv
,
177 resource_size_t
*base
, resource_size_t
*size
)
179 uint32_t reg_val
= I915_READ(IS_GM45(dev_priv
) ?
180 CTG_STOLEN_RESERVED
:
181 ELK_STOLEN_RESERVED
);
182 resource_size_t stolen_top
= dev_priv
->dsm
.end
+ 1;
184 if ((reg_val
& G4X_STOLEN_RESERVED_ENABLE
) == 0) {
191 * Whether ILK really reuses the ELK register for this is unclear.
192 * Let's see if we catch anyone with this supposedly enabled on ILK.
194 WARN(IS_GEN5(dev_priv
), "ILK stolen reserved found? 0x%08x\n", reg_val
);
196 *base
= (reg_val
& G4X_STOLEN_RESERVED_ADDR2_MASK
) << 16;
198 WARN_ON((reg_val
& G4X_STOLEN_RESERVED_ADDR1_MASK
) < *base
);
200 /* On these platforms, the register doesn't have a size field, so the
201 * size is the distance between the base and the top of the stolen
202 * memory. We also have the genuine case where base is zero and there's
203 * nothing reserved. */
207 *size
= stolen_top
- *base
;
210 static void gen6_get_stolen_reserved(struct drm_i915_private
*dev_priv
,
211 resource_size_t
*base
, resource_size_t
*size
)
213 uint32_t reg_val
= I915_READ(GEN6_STOLEN_RESERVED
);
215 if ((reg_val
& GEN6_STOLEN_RESERVED_ENABLE
) == 0) {
221 *base
= reg_val
& GEN6_STOLEN_RESERVED_ADDR_MASK
;
223 switch (reg_val
& GEN6_STOLEN_RESERVED_SIZE_MASK
) {
224 case GEN6_STOLEN_RESERVED_1M
:
227 case GEN6_STOLEN_RESERVED_512K
:
230 case GEN6_STOLEN_RESERVED_256K
:
233 case GEN6_STOLEN_RESERVED_128K
:
238 MISSING_CASE(reg_val
& GEN6_STOLEN_RESERVED_SIZE_MASK
);
242 static void gen7_get_stolen_reserved(struct drm_i915_private
*dev_priv
,
243 resource_size_t
*base
, resource_size_t
*size
)
245 uint32_t reg_val
= I915_READ(GEN6_STOLEN_RESERVED
);
247 if ((reg_val
& GEN6_STOLEN_RESERVED_ENABLE
) == 0) {
253 *base
= reg_val
& GEN7_STOLEN_RESERVED_ADDR_MASK
;
255 switch (reg_val
& GEN7_STOLEN_RESERVED_SIZE_MASK
) {
256 case GEN7_STOLEN_RESERVED_1M
:
259 case GEN7_STOLEN_RESERVED_256K
:
264 MISSING_CASE(reg_val
& GEN7_STOLEN_RESERVED_SIZE_MASK
);
268 static void chv_get_stolen_reserved(struct drm_i915_private
*dev_priv
,
269 resource_size_t
*base
, resource_size_t
*size
)
271 uint32_t reg_val
= I915_READ(GEN6_STOLEN_RESERVED
);
273 if ((reg_val
& GEN6_STOLEN_RESERVED_ENABLE
) == 0) {
279 *base
= reg_val
& GEN6_STOLEN_RESERVED_ADDR_MASK
;
281 switch (reg_val
& GEN8_STOLEN_RESERVED_SIZE_MASK
) {
282 case GEN8_STOLEN_RESERVED_1M
:
285 case GEN8_STOLEN_RESERVED_2M
:
286 *size
= 2 * 1024 * 1024;
288 case GEN8_STOLEN_RESERVED_4M
:
289 *size
= 4 * 1024 * 1024;
291 case GEN8_STOLEN_RESERVED_8M
:
292 *size
= 8 * 1024 * 1024;
295 *size
= 8 * 1024 * 1024;
296 MISSING_CASE(reg_val
& GEN8_STOLEN_RESERVED_SIZE_MASK
);
300 static void bdw_get_stolen_reserved(struct drm_i915_private
*dev_priv
,
301 resource_size_t
*base
, resource_size_t
*size
)
303 uint32_t reg_val
= I915_READ(GEN6_STOLEN_RESERVED
);
304 resource_size_t stolen_top
;
306 if ((reg_val
& GEN6_STOLEN_RESERVED_ENABLE
) == 0) {
312 stolen_top
= dev_priv
->dsm
.end
+ 1;
314 *base
= reg_val
& GEN6_STOLEN_RESERVED_ADDR_MASK
;
316 /* On these platforms, the register doesn't have a size field, so the
317 * size is the distance between the base and the top of the stolen
318 * memory. We also have the genuine case where base is zero and there's
319 * nothing reserved. */
323 *size
= stolen_top
- *base
;
326 int i915_gem_init_stolen(struct drm_i915_private
*dev_priv
)
328 resource_size_t reserved_base
, stolen_top
;
329 resource_size_t reserved_total
, reserved_size
;
330 resource_size_t stolen_usable_start
;
332 mutex_init(&dev_priv
->mm
.stolen_lock
);
334 if (intel_vgpu_active(dev_priv
)) {
335 DRM_INFO("iGVT-g active, disabling use of stolen memory\n");
339 if (intel_vtd_active() && INTEL_GEN(dev_priv
) < 8) {
340 DRM_INFO("DMAR active, disabling use of stolen memory\n");
344 if (resource_size(&intel_graphics_stolen_res
) == 0)
347 dev_priv
->dsm
= intel_graphics_stolen_res
;
349 if (i915_adjust_stolen(dev_priv
, &dev_priv
->dsm
))
352 GEM_BUG_ON(dev_priv
->dsm
.start
== 0);
353 GEM_BUG_ON(dev_priv
->dsm
.end
<= dev_priv
->dsm
.start
);
355 stolen_top
= dev_priv
->dsm
.end
+ 1;
359 switch (INTEL_INFO(dev_priv
)->gen
) {
364 if (!IS_G4X(dev_priv
))
368 g4x_get_stolen_reserved(dev_priv
,
369 &reserved_base
, &reserved_size
);
372 gen6_get_stolen_reserved(dev_priv
,
373 &reserved_base
, &reserved_size
);
376 gen7_get_stolen_reserved(dev_priv
,
377 &reserved_base
, &reserved_size
);
381 chv_get_stolen_reserved(dev_priv
,
382 &reserved_base
, &reserved_size
);
384 bdw_get_stolen_reserved(dev_priv
,
385 &reserved_base
, &reserved_size
);
389 /* It is possible for the reserved base to be zero, but the register
390 * field for size doesn't have a zero option. */
391 if (reserved_base
== 0) {
393 reserved_base
= stolen_top
;
396 dev_priv
->dsm_reserved
=
397 (struct resource
) DEFINE_RES_MEM(reserved_base
, reserved_size
);
399 if (!resource_contains(&dev_priv
->dsm
, &dev_priv
->dsm_reserved
)) {
400 DRM_ERROR("Stolen reserved area %pR outside stolen memory %pR\n",
401 &dev_priv
->dsm_reserved
, &dev_priv
->dsm
);
405 /* It is possible for the reserved area to end before the end of stolen
406 * memory, so just consider the start. */
407 reserved_total
= stolen_top
- reserved_base
;
409 DRM_DEBUG_KMS("Memory reserved for graphics device: %lluK, usable: %lluK\n",
410 (u64
)resource_size(&dev_priv
->dsm
) >> 10,
411 ((u64
)resource_size(&dev_priv
->dsm
) - reserved_total
) >> 10);
413 stolen_usable_start
= 0;
414 /* WaSkipStolenMemoryFirstPage:bdw+ */
415 if (INTEL_GEN(dev_priv
) >= 8)
416 stolen_usable_start
= 4096;
418 dev_priv
->stolen_usable_size
=
419 resource_size(&dev_priv
->dsm
) - reserved_total
- stolen_usable_start
;
421 /* Basic memrange allocator for stolen space. */
422 drm_mm_init(&dev_priv
->mm
.stolen
, stolen_usable_start
,
423 dev_priv
->stolen_usable_size
);
428 static struct sg_table
*
429 i915_pages_create_for_stolen(struct drm_device
*dev
,
430 resource_size_t offset
, resource_size_t size
)
432 struct drm_i915_private
*dev_priv
= to_i915(dev
);
434 struct scatterlist
*sg
;
436 GEM_BUG_ON(range_overflows(offset
, size
, resource_size(&dev_priv
->dsm
)));
438 /* We hide that we have no struct page backing our stolen object
439 * by wrapping the contiguous physical allocation with a fake
440 * dma mapping in a single scatterlist.
443 st
= kmalloc(sizeof(*st
), GFP_KERNEL
);
445 return ERR_PTR(-ENOMEM
);
447 if (sg_alloc_table(st
, 1, GFP_KERNEL
)) {
449 return ERR_PTR(-ENOMEM
);
456 sg_dma_address(sg
) = (dma_addr_t
)dev_priv
->dsm
.start
+ offset
;
457 sg_dma_len(sg
) = size
;
462 static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object
*obj
)
464 struct sg_table
*pages
=
465 i915_pages_create_for_stolen(obj
->base
.dev
,
469 return PTR_ERR(pages
);
471 __i915_gem_object_set_pages(obj
, pages
, obj
->stolen
->size
);
476 static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object
*obj
,
477 struct sg_table
*pages
)
479 /* Should only be called from i915_gem_object_release_stolen() */
480 sg_free_table(pages
);
485 i915_gem_object_release_stolen(struct drm_i915_gem_object
*obj
)
487 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
488 struct drm_mm_node
*stolen
= fetch_and_zero(&obj
->stolen
);
492 __i915_gem_object_unpin_pages(obj
);
494 i915_gem_stolen_remove_node(dev_priv
, stolen
);
498 static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops
= {
499 .get_pages
= i915_gem_object_get_pages_stolen
,
500 .put_pages
= i915_gem_object_put_pages_stolen
,
501 .release
= i915_gem_object_release_stolen
,
504 static struct drm_i915_gem_object
*
505 _i915_gem_object_create_stolen(struct drm_i915_private
*dev_priv
,
506 struct drm_mm_node
*stolen
)
508 struct drm_i915_gem_object
*obj
;
509 unsigned int cache_level
;
511 obj
= i915_gem_object_alloc(dev_priv
);
515 drm_gem_private_object_init(&dev_priv
->drm
, &obj
->base
, stolen
->size
);
516 i915_gem_object_init(obj
, &i915_gem_object_stolen_ops
);
518 obj
->stolen
= stolen
;
519 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
| I915_GEM_DOMAIN_GTT
;
520 cache_level
= HAS_LLC(dev_priv
) ? I915_CACHE_LLC
: I915_CACHE_NONE
;
521 i915_gem_object_set_cache_coherency(obj
, cache_level
);
523 if (i915_gem_object_pin_pages(obj
))
529 i915_gem_object_free(obj
);
533 struct drm_i915_gem_object
*
534 i915_gem_object_create_stolen(struct drm_i915_private
*dev_priv
,
535 resource_size_t size
)
537 struct drm_i915_gem_object
*obj
;
538 struct drm_mm_node
*stolen
;
541 if (!drm_mm_initialized(&dev_priv
->mm
.stolen
))
547 stolen
= kzalloc(sizeof(*stolen
), GFP_KERNEL
);
551 ret
= i915_gem_stolen_insert_node(dev_priv
, stolen
, size
, 4096);
557 obj
= _i915_gem_object_create_stolen(dev_priv
, stolen
);
561 i915_gem_stolen_remove_node(dev_priv
, stolen
);
566 struct drm_i915_gem_object
*
567 i915_gem_object_create_stolen_for_preallocated(struct drm_i915_private
*dev_priv
,
568 resource_size_t stolen_offset
,
569 resource_size_t gtt_offset
,
570 resource_size_t size
)
572 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
573 struct drm_i915_gem_object
*obj
;
574 struct drm_mm_node
*stolen
;
575 struct i915_vma
*vma
;
578 if (!drm_mm_initialized(&dev_priv
->mm
.stolen
))
581 lockdep_assert_held(&dev_priv
->drm
.struct_mutex
);
583 DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%pa, gtt_offset=%pa, size=%pa\n",
584 &stolen_offset
, >t_offset
, &size
);
586 /* KISS and expect everything to be page-aligned */
587 if (WARN_ON(size
== 0) ||
588 WARN_ON(!IS_ALIGNED(size
, I915_GTT_PAGE_SIZE
)) ||
589 WARN_ON(!IS_ALIGNED(stolen_offset
, I915_GTT_MIN_ALIGNMENT
)))
592 stolen
= kzalloc(sizeof(*stolen
), GFP_KERNEL
);
596 stolen
->start
= stolen_offset
;
598 mutex_lock(&dev_priv
->mm
.stolen_lock
);
599 ret
= drm_mm_reserve_node(&dev_priv
->mm
.stolen
, stolen
);
600 mutex_unlock(&dev_priv
->mm
.stolen_lock
);
602 DRM_DEBUG_KMS("failed to allocate stolen space\n");
607 obj
= _i915_gem_object_create_stolen(dev_priv
, stolen
);
609 DRM_DEBUG_KMS("failed to allocate stolen object\n");
610 i915_gem_stolen_remove_node(dev_priv
, stolen
);
615 /* Some objects just need physical mem from stolen space */
616 if (gtt_offset
== I915_GTT_OFFSET_NONE
)
619 ret
= i915_gem_object_pin_pages(obj
);
623 vma
= i915_vma_instance(obj
, &ggtt
->base
, NULL
);
629 /* To simplify the initialisation sequence between KMS and GTT,
630 * we allow construction of the stolen object prior to
631 * setting up the GTT space. The actual reservation will occur
634 ret
= i915_gem_gtt_reserve(&ggtt
->base
, &vma
->node
,
635 size
, gtt_offset
, obj
->cache_level
,
638 DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
642 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
644 vma
->pages
= obj
->mm
.pages
;
645 vma
->flags
|= I915_VMA_GLOBAL_BIND
;
646 __i915_vma_set_map_and_fenceable(vma
);
647 list_move_tail(&vma
->vm_link
, &ggtt
->base
.inactive_list
);
649 spin_lock(&dev_priv
->mm
.obj_lock
);
650 list_move_tail(&obj
->mm
.link
, &dev_priv
->mm
.bound_list
);
652 spin_unlock(&dev_priv
->mm
.obj_lock
);
657 i915_gem_object_unpin_pages(obj
);
659 i915_gem_object_put(obj
);