blob: f1a80bfa9919986df52e281a9c2b9ca59a3ca1f7 [file] [log] [blame]
Chris Wilson9797fbf2012-04-24 15:47:39 +01001/*
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
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/i915_drm.h>
Chris Wilson9797fbf2012-04-24 15:47:39 +010031#include "i915_drv.h"
32
Ville Syrjälä0ad98c72015-10-08 12:08:20 +030033#define KB(x) ((x) * 1024)
34#define MB(x) (KB(x) * 1024)
35
Chris Wilson9797fbf2012-04-24 15:47:39 +010036/*
37 * The BIOS typically reserves some of the system's memory for the exclusive
38 * use of the integrated graphics. This memory is no longer available for
39 * use by the OS and so the user finds that his system has less memory
40 * available than he put in. We refer to this memory as stolen.
41 *
42 * The BIOS will allocate its framebuffer from the stolen memory. Our
43 * goal is try to reuse that object for our own fbcon which must always
44 * be available for panics. Anything else we can reuse the stolen memory
45 * for is a boon.
46 */
47
Paulo Zanonia9da5122015-09-14 15:19:57 -030048int i915_gem_stolen_insert_node_in_range(struct drm_i915_private *dev_priv,
49 struct drm_mm_node *node, u64 size,
50 unsigned alignment, u64 start, u64 end)
Paulo Zanonid713fd42015-07-02 19:25:07 -030051{
Paulo Zanoni92e97d22015-07-02 19:25:09 -030052 int ret;
53
Paulo Zanonid713fd42015-07-02 19:25:07 -030054 if (!drm_mm_initialized(&dev_priv->mm.stolen))
55 return -ENODEV;
56
Paulo Zanoni92e97d22015-07-02 19:25:09 -030057 mutex_lock(&dev_priv->mm.stolen_lock);
Paulo Zanonia9da5122015-09-14 15:19:57 -030058 ret = drm_mm_insert_node_in_range(&dev_priv->mm.stolen, node, size,
59 alignment, start, end,
60 DRM_MM_SEARCH_DEFAULT);
Paulo Zanoni92e97d22015-07-02 19:25:09 -030061 mutex_unlock(&dev_priv->mm.stolen_lock);
62
63 return ret;
Paulo Zanonid713fd42015-07-02 19:25:07 -030064}
65
Paulo Zanonia9da5122015-09-14 15:19:57 -030066int i915_gem_stolen_insert_node(struct drm_i915_private *dev_priv,
67 struct drm_mm_node *node, u64 size,
68 unsigned alignment)
69{
70 return i915_gem_stolen_insert_node_in_range(dev_priv, node, size,
Paulo Zanoni3c6b29b2016-12-15 11:23:55 -020071 alignment, 0, U64_MAX);
Paulo Zanonia9da5122015-09-14 15:19:57 -030072}
73
Paulo Zanonid713fd42015-07-02 19:25:07 -030074void i915_gem_stolen_remove_node(struct drm_i915_private *dev_priv,
75 struct drm_mm_node *node)
76{
Paulo Zanoni92e97d22015-07-02 19:25:09 -030077 mutex_lock(&dev_priv->mm.stolen_lock);
Paulo Zanonid713fd42015-07-02 19:25:07 -030078 drm_mm_remove_node(node);
Paulo Zanoni92e97d22015-07-02 19:25:09 -030079 mutex_unlock(&dev_priv->mm.stolen_lock);
Paulo Zanonid713fd42015-07-02 19:25:07 -030080}
81
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +000082static unsigned long i915_stolen_to_physical(struct drm_i915_private *dev_priv)
Chris Wilson9797fbf2012-04-24 15:47:39 +010083{
David Weinehall52a05c32016-08-22 13:32:44 +030084 struct pci_dev *pdev = dev_priv->drm.pdev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +030085 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoneaba1b82013-07-04 12:28:35 +010086 struct resource *r;
Chris Wilson9797fbf2012-04-24 15:47:39 +010087 u32 base;
88
Chris Wilson17fec8a2013-07-04 00:23:33 +010089 /* Almost universally we can find the Graphics Base of Stolen Memory
Joonas Lahtinene10fa552016-04-15 12:03:39 +030090 * at register BSM (0x5c) in the igfx configuration space. On a few
91 * (desktop) machines this is also mirrored in the bridge device at
92 * different locations, or in the MCHBAR.
Chris Wilsone12a2d52012-11-15 11:32:18 +000093 *
Ville Syrjälä0ad98c72015-10-08 12:08:20 +030094 * On 865 we just check the TOUD register.
95 *
96 * On 830/845/85x the stolen memory base isn't available in any
97 * register. We need to calculate it as TOM-TSEG_SIZE-stolen_size.
98 *
Chris Wilson9797fbf2012-04-24 15:47:39 +010099 */
Chris Wilsone12a2d52012-11-15 11:32:18 +0000100 base = 0;
Ville Syrjäläa9097be2016-10-31 22:37:20 +0200101 if (INTEL_GEN(dev_priv) >= 3) {
Joonas Lahtinene10fa552016-04-15 12:03:39 +0300102 u32 bsm;
103
David Weinehall52a05c32016-08-22 13:32:44 +0300104 pci_read_config_dword(pdev, INTEL_BSM, &bsm);
Joonas Lahtinene10fa552016-04-15 12:03:39 +0300105
Joonas Lahtinenc0dd3462016-04-22 13:29:26 +0300106 base = bsm & INTEL_BSM_MASK;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100107 } else if (IS_I865G(dev_priv)) {
Ville Syrjäläd721b022016-08-08 13:58:39 +0300108 u32 tseg_size = 0;
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300109 u16 toud = 0;
Ville Syrjäläd721b022016-08-08 13:58:39 +0300110 u8 tmp;
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300111
David Weinehall52a05c32016-08-22 13:32:44 +0300112 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjäläd721b022016-08-08 13:58:39 +0300113 I845_ESMRAMC, &tmp);
114
115 if (tmp & TSEG_ENABLE) {
116 switch (tmp & I845_TSEG_SIZE_MASK) {
117 case I845_TSEG_SIZE_512K:
118 tseg_size = KB(512);
119 break;
120 case I845_TSEG_SIZE_1M:
121 tseg_size = MB(1);
122 break;
123 }
124 }
125
David Weinehall52a05c32016-08-22 13:32:44 +0300126 pci_bus_read_config_word(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300127 I865_TOUD, &toud);
128
Ville Syrjäläd721b022016-08-08 13:58:39 +0300129 base = (toud << 16) + tseg_size;
Ville Syrjäläa9097be2016-10-31 22:37:20 +0200130 } else if (IS_I85X(dev_priv)) {
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300131 u32 tseg_size = 0;
132 u32 tom;
133 u8 tmp;
134
David Weinehall52a05c32016-08-22 13:32:44 +0300135 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300136 I85X_ESMRAMC, &tmp);
137
138 if (tmp & TSEG_ENABLE)
139 tseg_size = MB(1);
140
David Weinehall52a05c32016-08-22 13:32:44 +0300141 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 1),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300142 I85X_DRB3, &tmp);
143 tom = tmp * MB(32);
144
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 base = tom - tseg_size - ggtt->stolen_size;
Jani Nikula2a307c22016-11-30 17:43:04 +0200146 } else if (IS_I845G(dev_priv)) {
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300147 u32 tseg_size = 0;
148 u32 tom;
149 u8 tmp;
150
David Weinehall52a05c32016-08-22 13:32:44 +0300151 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300152 I845_ESMRAMC, &tmp);
153
154 if (tmp & TSEG_ENABLE) {
155 switch (tmp & I845_TSEG_SIZE_MASK) {
156 case I845_TSEG_SIZE_512K:
157 tseg_size = KB(512);
158 break;
159 case I845_TSEG_SIZE_1M:
160 tseg_size = MB(1);
161 break;
162 }
163 }
164
David Weinehall52a05c32016-08-22 13:32:44 +0300165 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300166 I830_DRB3, &tmp);
167 tom = tmp * MB(32);
168
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300169 base = tom - tseg_size - ggtt->stolen_size;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100170 } else if (IS_I830(dev_priv)) {
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300171 u32 tseg_size = 0;
172 u32 tom;
173 u8 tmp;
174
David Weinehall52a05c32016-08-22 13:32:44 +0300175 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300176 I830_ESMRAMC, &tmp);
177
178 if (tmp & TSEG_ENABLE) {
179 if (tmp & I830_TSEG_SIZE_1M)
180 tseg_size = MB(1);
181 else
182 tseg_size = KB(512);
183 }
184
David Weinehall52a05c32016-08-22 13:32:44 +0300185 pci_bus_read_config_byte(pdev->bus, PCI_DEVFN(0, 0),
Ville Syrjälä0ad98c72015-10-08 12:08:20 +0300186 I830_DRB3, &tmp);
187 tom = tmp * MB(32);
188
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300189 base = tom - tseg_size - ggtt->stolen_size;
Chris Wilsone12a2d52012-11-15 11:32:18 +0000190 }
Chris Wilson9797fbf2012-04-24 15:47:39 +0100191
Chris Wilsoneaba1b82013-07-04 12:28:35 +0100192 if (base == 0)
193 return 0;
194
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300195 /* make sure we don't clobber the GTT if it's within stolen memory */
Jani Nikula73f67aa2016-12-07 22:48:09 +0200196 if (INTEL_GEN(dev_priv) <= 4 &&
197 !IS_G33(dev_priv) && !IS_PINEVIEW(dev_priv) && !IS_G4X(dev_priv)) {
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300198 struct {
199 u32 start, end;
200 } stolen[2] = {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300201 { .start = base, .end = base + ggtt->stolen_size, },
202 { .start = base, .end = base + ggtt->stolen_size, },
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300203 };
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300204 u64 ggtt_start, ggtt_end;
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300205
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300206 ggtt_start = I915_READ(PGTBL_CTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100207 if (IS_GEN4(dev_priv))
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300208 ggtt_start = (ggtt_start & PGTBL_ADDRESS_LO_MASK) |
209 (ggtt_start & PGTBL_ADDRESS_HI_MASK) << 28;
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300210 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300211 ggtt_start &= PGTBL_ADDRESS_LO_MASK;
212 ggtt_end = ggtt_start + ggtt_total_entries(ggtt) * 4;
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300213
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300214 if (ggtt_start >= stolen[0].start && ggtt_start < stolen[0].end)
215 stolen[0].end = ggtt_start;
216 if (ggtt_end > stolen[1].start && ggtt_end <= stolen[1].end)
217 stolen[1].start = ggtt_end;
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300218
219 /* pick the larger of the two chunks */
220 if (stolen[0].end - stolen[0].start >
221 stolen[1].end - stolen[1].start) {
222 base = stolen[0].start;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300223 ggtt->stolen_size = stolen[0].end - stolen[0].start;
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300224 } else {
225 base = stolen[1].start;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300226 ggtt->stolen_size = stolen[1].end - stolen[1].start;
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300227 }
228
229 if (stolen[0].start != stolen[1].start ||
230 stolen[0].end != stolen[1].end) {
231 DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n",
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300232 (unsigned long long)ggtt_start,
233 (unsigned long long)ggtt_end - 1);
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300234 DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n",
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300235 base, base + (u32)ggtt->stolen_size - 1);
Ville Syrjäläf1e1c212014-06-05 20:02:59 +0300236 }
237 }
238
239
Chris Wilsoneaba1b82013-07-04 12:28:35 +0100240 /* Verify that nothing else uses this physical address. Stolen
241 * memory should be reserved by the BIOS and hidden from the
242 * kernel. So if the region is already marked as busy, something
243 * is seriously wrong.
244 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +0000245 r = devm_request_mem_region(dev_priv->drm.dev, base, ggtt->stolen_size,
Chris Wilsoneaba1b82013-07-04 12:28:35 +0100246 "Graphics Stolen Memory");
247 if (r == NULL) {
Akash Goel3617dc92014-01-13 16:25:21 +0530248 /*
249 * One more attempt but this time requesting region from
250 * base + 1, as we have seen that this resolves the region
251 * conflict with the PCI Bus.
252 * This is a BIOS w/a: Some BIOS wrap stolen in the root
253 * PCI bus, but have an off-by-one error. Hence retry the
254 * reservation starting from 1 instead of 0.
255 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +0000256 r = devm_request_mem_region(dev_priv->drm.dev, base + 1,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300257 ggtt->stolen_size - 1,
Akash Goel3617dc92014-01-13 16:25:21 +0530258 "Graphics Stolen Memory");
Daniel Vetter0b6d24c2014-04-11 15:55:17 +0200259 /*
260 * GEN3 firmware likes to smash pci bridges into the stolen
261 * range. Apparently this works.
262 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100263 if (r == NULL && !IS_GEN3(dev_priv)) {
Akash Goel3617dc92014-01-13 16:25:21 +0530264 DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300265 base, base + (uint32_t)ggtt->stolen_size);
Akash Goel3617dc92014-01-13 16:25:21 +0530266 base = 0;
267 }
Chris Wilsoneaba1b82013-07-04 12:28:35 +0100268 }
269
Chris Wilsone12a2d52012-11-15 11:32:18 +0000270 return base;
Chris Wilson9797fbf2012-04-24 15:47:39 +0100271}
272
Chris Wilson9797fbf2012-04-24 15:47:39 +0100273void i915_gem_cleanup_stolen(struct drm_device *dev)
274{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100275 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +0100276
Daniel Vetter446f8d82013-07-02 10:48:31 +0200277 if (!drm_mm_initialized(&dev_priv->mm.stolen))
278 return;
279
Daniel Vetter4d7bb012012-12-18 15:24:37 +0100280 drm_mm_takedown(&dev_priv->mm.stolen);
Chris Wilson9797fbf2012-04-24 15:47:39 +0100281}
282
Ville Syrjälä7d316ae2015-09-16 21:28:50 +0300283static void g4x_get_stolen_reserved(struct drm_i915_private *dev_priv,
284 unsigned long *base, unsigned long *size)
285{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300286 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ville Syrjälä7d316ae2015-09-16 21:28:50 +0300287 uint32_t reg_val = I915_READ(IS_GM45(dev_priv) ?
288 CTG_STOLEN_RESERVED :
289 ELK_STOLEN_RESERVED);
290 unsigned long stolen_top = dev_priv->mm.stolen_base +
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300291 ggtt->stolen_size;
Ville Syrjälä7d316ae2015-09-16 21:28:50 +0300292
293 *base = (reg_val & G4X_STOLEN_RESERVED_ADDR2_MASK) << 16;
294
295 WARN_ON((reg_val & G4X_STOLEN_RESERVED_ADDR1_MASK) < *base);
296
297 /* On these platforms, the register doesn't have a size field, so the
298 * size is the distance between the base and the top of the stolen
299 * memory. We also have the genuine case where base is zero and there's
300 * nothing reserved. */
301 if (*base == 0)
302 *size = 0;
303 else
304 *size = stolen_top - *base;
305}
306
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300307static void gen6_get_stolen_reserved(struct drm_i915_private *dev_priv,
308 unsigned long *base, unsigned long *size)
309{
310 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
311
312 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
313
314 switch (reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK) {
315 case GEN6_STOLEN_RESERVED_1M:
316 *size = 1024 * 1024;
317 break;
318 case GEN6_STOLEN_RESERVED_512K:
319 *size = 512 * 1024;
320 break;
321 case GEN6_STOLEN_RESERVED_256K:
322 *size = 256 * 1024;
323 break;
324 case GEN6_STOLEN_RESERVED_128K:
325 *size = 128 * 1024;
326 break;
327 default:
328 *size = 1024 * 1024;
329 MISSING_CASE(reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK);
330 }
331}
332
333static void gen7_get_stolen_reserved(struct drm_i915_private *dev_priv,
334 unsigned long *base, unsigned long *size)
335{
336 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
337
338 *base = reg_val & GEN7_STOLEN_RESERVED_ADDR_MASK;
339
340 switch (reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK) {
341 case GEN7_STOLEN_RESERVED_1M:
342 *size = 1024 * 1024;
343 break;
344 case GEN7_STOLEN_RESERVED_256K:
345 *size = 256 * 1024;
346 break;
347 default:
348 *size = 1024 * 1024;
349 MISSING_CASE(reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK);
350 }
351}
352
Rodrigo Vivi9244f852016-12-18 13:36:27 -0800353static void chv_get_stolen_reserved(struct drm_i915_private *dev_priv,
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300354 unsigned long *base, unsigned long *size)
355{
356 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
357
358 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
359
360 switch (reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK) {
361 case GEN8_STOLEN_RESERVED_1M:
362 *size = 1024 * 1024;
363 break;
364 case GEN8_STOLEN_RESERVED_2M:
365 *size = 2 * 1024 * 1024;
366 break;
367 case GEN8_STOLEN_RESERVED_4M:
368 *size = 4 * 1024 * 1024;
369 break;
370 case GEN8_STOLEN_RESERVED_8M:
371 *size = 8 * 1024 * 1024;
372 break;
373 default:
374 *size = 8 * 1024 * 1024;
375 MISSING_CASE(reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK);
376 }
377}
378
379static void bdw_get_stolen_reserved(struct drm_i915_private *dev_priv,
380 unsigned long *base, unsigned long *size)
381{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300382 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300383 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
384 unsigned long stolen_top;
385
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300386 stolen_top = dev_priv->mm.stolen_base + ggtt->stolen_size;
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300387
388 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
389
390 /* On these platforms, the register doesn't have a size field, so the
391 * size is the distance between the base and the top of the stolen
392 * memory. We also have the genuine case where base is zero and there's
393 * nothing reserved. */
394 if (*base == 0)
395 *size = 0;
396 else
397 *size = stolen_top - *base;
398}
399
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +0000400int i915_gem_init_stolen(struct drm_i915_private *dev_priv)
Chris Wilson9797fbf2012-04-24 15:47:39 +0100401{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300402 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ville Syrjäläd7884d62015-09-11 21:14:29 +0300403 unsigned long reserved_total, reserved_base = 0, reserved_size;
Paulo Zanoni3c6b29b2016-12-15 11:23:55 -0200404 unsigned long stolen_usable_start, stolen_top;
Chris Wilson9797fbf2012-04-24 15:47:39 +0100405
Paulo Zanoni92e97d22015-07-02 19:25:09 -0300406 mutex_init(&dev_priv->mm.stolen_lock);
407
Chris Wilson0f4706d2014-03-18 14:50:50 +0200408#ifdef CONFIG_INTEL_IOMMU
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +0000409 if (intel_iommu_gfx_mapped && INTEL_GEN(dev_priv) < 8) {
Chris Wilson0f4706d2014-03-18 14:50:50 +0200410 DRM_INFO("DMAR active, disabling use of stolen memory\n");
411 return 0;
412 }
413#endif
414
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300415 if (ggtt->stolen_size == 0)
Chris Wilson6644a4e2013-09-05 13:40:25 +0100416 return 0;
417
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +0000418 dev_priv->mm.stolen_base = i915_stolen_to_physical(dev_priv);
Chris Wilsone12a2d52012-11-15 11:32:18 +0000419 if (dev_priv->mm.stolen_base == 0)
420 return 0;
421
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300422 stolen_top = dev_priv->mm.stolen_base + ggtt->stolen_size;
Chris Wilsone12a2d52012-11-15 11:32:18 +0000423
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300424 switch (INTEL_INFO(dev_priv)->gen) {
425 case 2:
426 case 3:
Ville Syrjälä7d316ae2015-09-16 21:28:50 +0300427 break;
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300428 case 4:
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +0100429 if (IS_G4X(dev_priv))
Ville Syrjälä7d316ae2015-09-16 21:28:50 +0300430 g4x_get_stolen_reserved(dev_priv, &reserved_base,
431 &reserved_size);
432 break;
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300433 case 5:
434 /* Assume the gen6 maximum for the older platforms. */
435 reserved_size = 1024 * 1024;
436 reserved_base = stolen_top - reserved_size;
437 break;
438 case 6:
439 gen6_get_stolen_reserved(dev_priv, &reserved_base,
440 &reserved_size);
441 break;
442 case 7:
443 gen7_get_stolen_reserved(dev_priv, &reserved_base,
444 &reserved_size);
445 break;
446 default:
Rodrigo Vivi5af7edc52016-12-19 11:05:47 -0800447 if (IS_LP(dev_priv))
448 chv_get_stolen_reserved(dev_priv, &reserved_base,
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300449 &reserved_size);
450 else
Rodrigo Vivi5af7edc52016-12-19 11:05:47 -0800451 bdw_get_stolen_reserved(dev_priv, &reserved_base,
452 &reserved_size);
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300453 break;
Daniel Vetter40bae732014-09-11 13:28:08 +0200454 }
Jesse Barnesc9cddff2013-05-08 10:45:13 -0700455
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300456 /* It is possible for the reserved base to be zero, but the register
457 * field for size doesn't have a zero option. */
458 if (reserved_base == 0) {
459 reserved_size = 0;
460 reserved_base = stolen_top;
461 }
462
463 if (reserved_base < dev_priv->mm.stolen_base ||
464 reserved_base + reserved_size > stolen_top) {
465 DRM_DEBUG_KMS("Stolen reserved area [0x%08lx - 0x%08lx] outside stolen memory [0x%08lx - 0x%08lx]\n",
466 reserved_base, reserved_base + reserved_size,
467 dev_priv->mm.stolen_base, stolen_top);
Daniel Vetter897f9ed2013-07-09 14:44:27 +0200468 return 0;
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300469 }
470
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300471 ggtt->stolen_reserved_base = reserved_base;
472 ggtt->stolen_reserved_size = reserved_size;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +0530473
Paulo Zanoni3774eb52015-08-10 14:57:32 -0300474 /* It is possible for the reserved area to end before the end of stolen
475 * memory, so just consider the start. */
476 reserved_total = stolen_top - reserved_base;
477
Thierry Reding8e9d5972015-08-14 12:35:23 +0200478 DRM_DEBUG_KMS("Memory reserved for graphics device: %zuK, usable: %luK\n",
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300479 ggtt->stolen_size >> 10,
480 (ggtt->stolen_size - reserved_total) >> 10);
Daniel Vetter897f9ed2013-07-09 14:44:27 +0200481
Paulo Zanoni3c6b29b2016-12-15 11:23:55 -0200482 stolen_usable_start = 0;
483 /* WaSkipStolenMemoryFirstPage:bdw+ */
484 if (INTEL_GEN(dev_priv) >= 8)
485 stolen_usable_start = 4096;
Paulo Zanonia9da5122015-09-14 15:19:57 -0300486
Paulo Zanoni3c6b29b2016-12-15 11:23:55 -0200487 ggtt->stolen_usable_size = ggtt->stolen_size - reserved_total -
488 stolen_usable_start;
489
490 /* Basic memrange allocator for stolen space. */
491 drm_mm_init(&dev_priv->mm.stolen, stolen_usable_start,
492 ggtt->stolen_usable_size);
Chris Wilson9797fbf2012-04-24 15:47:39 +0100493
494 return 0;
495}
Chris Wilson0104fdb2012-11-15 11:32:26 +0000496
497static struct sg_table *
498i915_pages_create_for_stolen(struct drm_device *dev,
499 u32 offset, u32 size)
500{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300501 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000502 struct sg_table *st;
503 struct scatterlist *sg;
504
Chris Wilson6288c792016-11-17 15:58:46 +0000505 GEM_BUG_ON(offset > dev_priv->ggtt.stolen_size - size);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000506
507 /* We hide that we have no struct page backing our stolen object
508 * by wrapping the contiguous physical allocation with a fake
509 * dma mapping in a single scatterlist.
510 */
511
512 st = kmalloc(sizeof(*st), GFP_KERNEL);
513 if (st == NULL)
Matthew Auld43e157f2016-11-18 17:02:16 +0000514 return ERR_PTR(-ENOMEM);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000515
516 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
517 kfree(st);
Matthew Auld43e157f2016-11-18 17:02:16 +0000518 return ERR_PTR(-ENOMEM);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000519 }
520
521 sg = st->sgl;
Akash Goelec14ba42014-01-13 16:24:45 +0530522 sg->offset = 0;
Imre Deaked23abd2013-03-26 15:14:19 +0200523 sg->length = size;
Chris Wilson0104fdb2012-11-15 11:32:26 +0000524
525 sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
526 sg_dma_len(sg) = size;
527
528 return st;
529}
530
Chris Wilson03ac84f2016-10-28 13:58:36 +0100531static struct sg_table *
532i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
Chris Wilson0104fdb2012-11-15 11:32:26 +0000533{
Chris Wilson03ac84f2016-10-28 13:58:36 +0100534 return i915_pages_create_for_stolen(obj->base.dev,
535 obj->stolen->start,
536 obj->stolen->size);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000537}
538
Chris Wilson03ac84f2016-10-28 13:58:36 +0100539static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj,
540 struct sg_table *pages)
Chris Wilson0104fdb2012-11-15 11:32:26 +0000541{
Chris Wilson6288c792016-11-17 15:58:46 +0000542 /* Should only be called from i915_gem_object_release_stolen() */
Chris Wilson03ac84f2016-10-28 13:58:36 +0100543 sg_free_table(pages);
544 kfree(pages);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000545}
546
Chris Wilsonef0cf272014-06-06 10:22:54 +0100547static void
548i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
549{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100550 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson6288c792016-11-17 15:58:46 +0000551 struct drm_mm_node *stolen = fetch_and_zero(&obj->stolen);
552
553 GEM_BUG_ON(!stolen);
Paulo Zanonid713fd42015-07-02 19:25:07 -0300554
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100555 __i915_gem_object_unpin_pages(obj);
556
Chris Wilson6288c792016-11-17 15:58:46 +0000557 i915_gem_stolen_remove_node(dev_priv, stolen);
558 kfree(stolen);
Chris Wilsonef0cf272014-06-06 10:22:54 +0100559}
Chris Wilson6288c792016-11-17 15:58:46 +0000560
Chris Wilson0104fdb2012-11-15 11:32:26 +0000561static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
562 .get_pages = i915_gem_object_get_pages_stolen,
563 .put_pages = i915_gem_object_put_pages_stolen,
Chris Wilsonef0cf272014-06-06 10:22:54 +0100564 .release = i915_gem_object_release_stolen,
Chris Wilson0104fdb2012-11-15 11:32:26 +0000565};
566
567static struct drm_i915_gem_object *
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000568_i915_gem_object_create_stolen(struct drm_i915_private *dev_priv,
Chris Wilson0104fdb2012-11-15 11:32:26 +0000569 struct drm_mm_node *stolen)
570{
571 struct drm_i915_gem_object *obj;
572
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000573 obj = i915_gem_object_alloc(dev_priv);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000574 if (obj == NULL)
575 return NULL;
576
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000577 drm_gem_private_object_init(&dev_priv->drm, &obj->base, stolen->size);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000578 i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
579
Chris Wilson0104fdb2012-11-15 11:32:26 +0000580 obj->stolen = stolen;
Chris Wilsond46f1c32013-08-08 14:41:06 +0100581 obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000582 obj->cache_level = HAS_LLC(dev_priv) ? I915_CACHE_LLC : I915_CACHE_NONE;
Chris Wilson0104fdb2012-11-15 11:32:26 +0000583
Chris Wilson03ac84f2016-10-28 13:58:36 +0100584 if (i915_gem_object_pin_pages(obj))
585 goto cleanup;
586
Chris Wilson0104fdb2012-11-15 11:32:26 +0000587 return obj;
588
589cleanup:
Chris Wilson42dcedd2012-11-15 11:32:30 +0000590 i915_gem_object_free(obj);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000591 return NULL;
592}
593
594struct drm_i915_gem_object *
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000595i915_gem_object_create_stolen(struct drm_i915_private *dev_priv, u32 size)
Chris Wilson0104fdb2012-11-15 11:32:26 +0000596{
Chris Wilson0104fdb2012-11-15 11:32:26 +0000597 struct drm_i915_gem_object *obj;
598 struct drm_mm_node *stolen;
David Herrmann06e78ed2013-07-27 16:21:27 +0200599 int ret;
Chris Wilson0104fdb2012-11-15 11:32:26 +0000600
Daniel Vetter446f8d82013-07-02 10:48:31 +0200601 if (!drm_mm_initialized(&dev_priv->mm.stolen))
Chris Wilson0104fdb2012-11-15 11:32:26 +0000602 return NULL;
603
Chris Wilson0104fdb2012-11-15 11:32:26 +0000604 if (size == 0)
605 return NULL;
606
David Herrmann06e78ed2013-07-27 16:21:27 +0200607 stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
608 if (!stolen)
Chris Wilson0104fdb2012-11-15 11:32:26 +0000609 return NULL;
610
Paulo Zanonid713fd42015-07-02 19:25:07 -0300611 ret = i915_gem_stolen_insert_node(dev_priv, stolen, size, 4096);
David Herrmann06e78ed2013-07-27 16:21:27 +0200612 if (ret) {
613 kfree(stolen);
614 return NULL;
615 }
616
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000617 obj = _i915_gem_object_create_stolen(dev_priv, stolen);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000618 if (obj)
619 return obj;
620
Paulo Zanonid713fd42015-07-02 19:25:07 -0300621 i915_gem_stolen_remove_node(dev_priv, stolen);
David Herrmann06e78ed2013-07-27 16:21:27 +0200622 kfree(stolen);
Chris Wilson0104fdb2012-11-15 11:32:26 +0000623 return NULL;
624}
625
Chris Wilson866d12b2013-02-19 13:31:37 -0800626struct drm_i915_gem_object *
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000627i915_gem_object_create_stolen_for_preallocated(struct drm_i915_private *dev_priv,
Chris Wilson866d12b2013-02-19 13:31:37 -0800628 u32 stolen_offset,
629 u32 gtt_offset,
630 u32 size)
631{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300632 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson866d12b2013-02-19 13:31:37 -0800633 struct drm_i915_gem_object *obj;
634 struct drm_mm_node *stolen;
Ben Widawsky2f633152013-07-17 12:19:03 -0700635 struct i915_vma *vma;
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700636 int ret;
Chris Wilson866d12b2013-02-19 13:31:37 -0800637
Daniel Vetter446f8d82013-07-02 10:48:31 +0200638 if (!drm_mm_initialized(&dev_priv->mm.stolen))
Chris Wilson866d12b2013-02-19 13:31:37 -0800639 return NULL;
640
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000641 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Tvrtko Ursulin12c83d92016-02-11 10:27:29 +0000642
Chris Wilson866d12b2013-02-19 13:31:37 -0800643 DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
644 stolen_offset, gtt_offset, size);
645
646 /* KISS and expect everything to be page-aligned */
Daniel Vetterf37b5c22015-02-10 23:12:27 +0100647 if (WARN_ON(size == 0) || WARN_ON(size & 4095) ||
648 WARN_ON(stolen_offset & 4095))
Chris Wilson866d12b2013-02-19 13:31:37 -0800649 return NULL;
650
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700651 stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
652 if (!stolen)
653 return NULL;
654
Ben Widawsky338710e2013-07-05 14:41:03 -0700655 stolen->start = stolen_offset;
656 stolen->size = size;
Paulo Zanoni92e97d22015-07-02 19:25:09 -0300657 mutex_lock(&dev_priv->mm.stolen_lock);
Ben Widawsky338710e2013-07-05 14:41:03 -0700658 ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
Paulo Zanoni92e97d22015-07-02 19:25:09 -0300659 mutex_unlock(&dev_priv->mm.stolen_lock);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700660 if (ret) {
Chris Wilson866d12b2013-02-19 13:31:37 -0800661 DRM_DEBUG_KMS("failed to allocate stolen space\n");
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700662 kfree(stolen);
Chris Wilson866d12b2013-02-19 13:31:37 -0800663 return NULL;
664 }
665
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000666 obj = _i915_gem_object_create_stolen(dev_priv, stolen);
Chris Wilson866d12b2013-02-19 13:31:37 -0800667 if (obj == NULL) {
668 DRM_DEBUG_KMS("failed to allocate stolen object\n");
Paulo Zanonid713fd42015-07-02 19:25:07 -0300669 i915_gem_stolen_remove_node(dev_priv, stolen);
David Herrmann06e78ed2013-07-27 16:21:27 +0200670 kfree(stolen);
Chris Wilson866d12b2013-02-19 13:31:37 -0800671 return NULL;
672 }
673
Jesse Barnes3727d552013-05-08 10:45:14 -0700674 /* Some objects just need physical mem from stolen space */
Daniel Vetter190d6cd2013-07-04 13:06:28 +0200675 if (gtt_offset == I915_GTT_OFFSET_NONE)
Jesse Barnes3727d552013-05-08 10:45:14 -0700676 return obj;
677
Chris Wilson03ac84f2016-10-28 13:58:36 +0100678 ret = i915_gem_object_pin_pages(obj);
679 if (ret)
680 goto err;
681
Chris Wilson058d88c2016-08-15 10:49:06 +0100682 vma = i915_gem_obj_lookup_or_create_vma(obj, &ggtt->base, NULL);
Dan Carpenterdb473b32013-07-19 08:45:46 +0300683 if (IS_ERR(vma)) {
684 ret = PTR_ERR(vma);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100685 goto err_pages;
Ben Widawsky2f633152013-07-17 12:19:03 -0700686 }
687
Chris Wilson866d12b2013-02-19 13:31:37 -0800688 /* To simplify the initialisation sequence between KMS and GTT,
689 * we allow construction of the stolen object prior to
690 * setting up the GTT space. The actual reservation will occur
691 * later.
692 */
Ben Widawsky2f633152013-07-17 12:19:03 -0700693 vma->node.start = gtt_offset;
694 vma->node.size = size;
Chris Wilson7c4a7d62015-09-24 11:57:45 +0100695
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +0100696 ret = drm_mm_reserve_node(&ggtt->base.mm, &vma->node);
697 if (ret) {
698 DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
Chris Wilson03ac84f2016-10-28 13:58:36 +0100699 goto err_pages;
Ben Widawskyedd41a82013-07-05 14:41:05 -0700700 }
Chris Wilson866d12b2013-02-19 13:31:37 -0800701
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100702 vma->pages = obj->mm.pages;
Chris Wilson3272db52016-08-04 16:32:32 +0100703 vma->flags |= I915_VMA_GLOBAL_BIND;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +0100704 __i915_vma_set_map_and_fenceable(vma);
Chris Wilson50e046b2016-08-04 07:52:46 +0100705 list_move_tail(&vma->vm_link, &ggtt->base.inactive_list);
Joonas Lahtinen56cea322016-11-02 12:16:04 +0200706 list_move_tail(&obj->global_link, &dev_priv->mm.bound_list);
Chris Wilson15717de2016-08-04 07:52:26 +0100707 obj->bind_count++;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +0100708
Chris Wilson866d12b2013-02-19 13:31:37 -0800709 return obj;
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700710
Chris Wilson03ac84f2016-10-28 13:58:36 +0100711err_pages:
712 i915_gem_object_unpin_pages(obj);
Chris Wilson7c4a7d62015-09-24 11:57:45 +0100713err:
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100714 i915_gem_object_put(obj);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700715 return NULL;
Chris Wilson866d12b2013-02-19 13:31:37 -0800716}