Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/drmP.h> |
| 30 | #include <drm/i915_drm.h> |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 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 | |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 45 | static unsigned long i915_stolen_to_physical(struct drm_device *dev) |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 46 | { |
| 47 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | eaba1b8 | 2013-07-04 12:28:35 +0100 | [diff] [blame] | 48 | struct resource *r; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 49 | u32 base; |
| 50 | |
Chris Wilson | 17fec8a | 2013-07-04 00:23:33 +0100 | [diff] [blame] | 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. |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 59 | * |
Chris Wilson | 17fec8a | 2013-07-04 00:23:33 +0100 | [diff] [blame] | 60 | * XXX However gen2 requires an unavailable symbol. |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 61 | */ |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 62 | base = 0; |
Chris Wilson | 17fec8a | 2013-07-04 00:23:33 +0100 | [diff] [blame] | 63 | if (INTEL_INFO(dev)->gen >= 3) { |
| 64 | /* Read Graphics Base of Stolen Memory directly */ |
Jesse Barnes | c9cddff | 2013-05-08 10:45:13 -0700 | [diff] [blame] | 65 | pci_read_config_dword(dev->pdev, 0x5c, &base); |
| 66 | base &= ~((1<<20) - 1); |
Chris Wilson | 17fec8a | 2013-07-04 00:23:33 +0100 | [diff] [blame] | 67 | } else { /* GEN2 */ |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 68 | #if 0 |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 69 | /* Stolen is immediately above Top of Memory */ |
| 70 | base = max_low_pfn_mapped << PAGE_SHIFT; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 71 | #endif |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 72 | } |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 73 | |
Chris Wilson | eaba1b8 | 2013-07-04 12:28:35 +0100 | [diff] [blame] | 74 | if (base == 0) |
| 75 | return 0; |
| 76 | |
| 77 | /* Verify that nothing else uses this physical address. Stolen |
| 78 | * memory should be reserved by the BIOS and hidden from the |
| 79 | * kernel. So if the region is already marked as busy, something |
| 80 | * is seriously wrong. |
| 81 | */ |
| 82 | r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size, |
| 83 | "Graphics Stolen Memory"); |
| 84 | if (r == NULL) { |
| 85 | DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n", |
| 86 | base, base + (uint32_t)dev_priv->gtt.stolen_size); |
| 87 | base = 0; |
| 88 | } |
| 89 | |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 90 | return base; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 93 | static int i915_setup_compression(struct drm_device *dev, int size) |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 94 | { |
| 95 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 96 | struct drm_mm_node *compressed_fb, *uninitialized_var(compressed_llb); |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 97 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 98 | /* Try to over-allocate to reduce reallocations and fragmentation */ |
| 99 | compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen, |
| 100 | size <<= 1, 4096, 0); |
| 101 | if (!compressed_fb) |
| 102 | compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen, |
| 103 | size >>= 1, 4096, 0); |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 104 | if (compressed_fb) |
| 105 | compressed_fb = drm_mm_get_block(compressed_fb, size, 4096); |
| 106 | if (!compressed_fb) |
| 107 | goto err; |
| 108 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 109 | if (HAS_PCH_SPLIT(dev)) |
| 110 | I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start); |
| 111 | else if (IS_GM45(dev)) { |
| 112 | I915_WRITE(DPFC_CB_BASE, compressed_fb->start); |
| 113 | } else { |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 114 | compressed_llb = drm_mm_search_free(&dev_priv->mm.stolen, |
| 115 | 4096, 4096, 0); |
| 116 | if (compressed_llb) |
| 117 | compressed_llb = drm_mm_get_block(compressed_llb, |
| 118 | 4096, 4096); |
| 119 | if (!compressed_llb) |
| 120 | goto err_fb; |
| 121 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 122 | dev_priv->fbc.compressed_llb = compressed_llb; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 123 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 124 | I915_WRITE(FBC_CFB_BASE, |
| 125 | dev_priv->mm.stolen_base + compressed_fb->start); |
| 126 | I915_WRITE(FBC_LL_BASE, |
| 127 | dev_priv->mm.stolen_base + compressed_llb->start); |
| 128 | } |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 129 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 130 | dev_priv->fbc.compressed_fb = compressed_fb; |
| 131 | dev_priv->fbc.size = size; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 132 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 133 | DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n", |
| 134 | size); |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 135 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 138 | err_fb: |
| 139 | drm_mm_put_block(compressed_fb); |
| 140 | err: |
Chris Wilson | d824178 | 2013-04-27 12:44:16 +0100 | [diff] [blame] | 141 | 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); |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 142 | return -ENOSPC; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 145 | int i915_gem_stolen_setup_compression(struct drm_device *dev, int size) |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 146 | { |
| 147 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 148 | |
Daniel Vetter | 446f8d8 | 2013-07-02 10:48:31 +0200 | [diff] [blame] | 149 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 150 | return -ENODEV; |
| 151 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 152 | if (size < dev_priv->fbc.size) |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | |
| 155 | /* Release any current block */ |
| 156 | i915_gem_stolen_cleanup_compression(dev); |
| 157 | |
| 158 | return i915_setup_compression(dev, size); |
| 159 | } |
| 160 | |
| 161 | void i915_gem_stolen_cleanup_compression(struct drm_device *dev) |
| 162 | { |
| 163 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 164 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 165 | if (dev_priv->fbc.size == 0) |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 166 | return; |
| 167 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 168 | if (dev_priv->fbc.compressed_fb) |
| 169 | drm_mm_put_block(dev_priv->fbc.compressed_fb); |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 170 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 171 | if (dev_priv->fbc.compressed_llb) |
| 172 | drm_mm_put_block(dev_priv->fbc.compressed_llb); |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 173 | |
Ben Widawsky | 5c3fe8b | 2013-06-27 16:30:21 -0700 | [diff] [blame] | 174 | dev_priv->fbc.size = 0; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void i915_gem_cleanup_stolen(struct drm_device *dev) |
| 178 | { |
Daniel Vetter | 4d7bb01 | 2012-12-18 15:24:37 +0100 | [diff] [blame] | 179 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 180 | |
Daniel Vetter | 446f8d8 | 2013-07-02 10:48:31 +0200 | [diff] [blame] | 181 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
| 182 | return; |
| 183 | |
Chris Wilson | 11be49e | 2012-11-15 11:32:20 +0000 | [diff] [blame] | 184 | i915_gem_stolen_cleanup_compression(dev); |
Daniel Vetter | 4d7bb01 | 2012-12-18 15:24:37 +0100 | [diff] [blame] | 185 | drm_mm_takedown(&dev_priv->mm.stolen); |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | int i915_gem_init_stolen(struct drm_device *dev) |
| 189 | { |
| 190 | struct drm_i915_private *dev_priv = dev->dev_private; |
Jesse Barnes | c9cddff | 2013-05-08 10:45:13 -0700 | [diff] [blame] | 191 | int bios_reserved = 0; |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 192 | |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 193 | dev_priv->mm.stolen_base = i915_stolen_to_physical(dev); |
| 194 | if (dev_priv->mm.stolen_base == 0) |
| 195 | return 0; |
| 196 | |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 197 | DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n", |
| 198 | dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base); |
Chris Wilson | e12a2d5 | 2012-11-15 11:32:18 +0000 | [diff] [blame] | 199 | |
Jesse Barnes | c9cddff | 2013-05-08 10:45:13 -0700 | [diff] [blame] | 200 | if (IS_VALLEYVIEW(dev)) |
| 201 | bios_reserved = 1024*1024; /* top 1M on VLV/BYT */ |
| 202 | |
Daniel Vetter | 897f9ed | 2013-07-09 14:44:27 +0200 | [diff] [blame] | 203 | if (WARN_ON(bios_reserved > dev_priv->gtt.stolen_size)) |
| 204 | return 0; |
| 205 | |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 206 | /* Basic memrange allocator for stolen space */ |
Jesse Barnes | c9cddff | 2013-05-08 10:45:13 -0700 | [diff] [blame] | 207 | drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size - |
| 208 | bios_reserved); |
Chris Wilson | 9797fbf | 2012-04-24 15:47:39 +0100 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 212 | |
| 213 | static struct sg_table * |
| 214 | i915_pages_create_for_stolen(struct drm_device *dev, |
| 215 | u32 offset, u32 size) |
| 216 | { |
| 217 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 218 | struct sg_table *st; |
| 219 | struct scatterlist *sg; |
| 220 | |
| 221 | DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size); |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 222 | BUG_ON(offset > dev_priv->gtt.stolen_size - size); |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 223 | |
| 224 | /* We hide that we have no struct page backing our stolen object |
| 225 | * by wrapping the contiguous physical allocation with a fake |
| 226 | * dma mapping in a single scatterlist. |
| 227 | */ |
| 228 | |
| 229 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 230 | if (st == NULL) |
| 231 | return NULL; |
| 232 | |
| 233 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 234 | kfree(st); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | sg = st->sgl; |
Imre Deak | ed23abd | 2013-03-26 15:14:19 +0200 | [diff] [blame] | 239 | sg->offset = offset; |
| 240 | sg->length = size; |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 241 | |
| 242 | sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset; |
| 243 | sg_dma_len(sg) = size; |
| 244 | |
| 245 | return st; |
| 246 | } |
| 247 | |
| 248 | static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj) |
| 249 | { |
| 250 | BUG(); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | |
| 254 | static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj) |
| 255 | { |
| 256 | /* Should only be called during free */ |
| 257 | sg_free_table(obj->pages); |
| 258 | kfree(obj->pages); |
| 259 | } |
| 260 | |
| 261 | static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = { |
| 262 | .get_pages = i915_gem_object_get_pages_stolen, |
| 263 | .put_pages = i915_gem_object_put_pages_stolen, |
| 264 | }; |
| 265 | |
| 266 | static struct drm_i915_gem_object * |
| 267 | _i915_gem_object_create_stolen(struct drm_device *dev, |
| 268 | struct drm_mm_node *stolen) |
| 269 | { |
| 270 | struct drm_i915_gem_object *obj; |
| 271 | |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 272 | obj = i915_gem_object_alloc(dev); |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 273 | if (obj == NULL) |
| 274 | return NULL; |
| 275 | |
| 276 | if (drm_gem_private_object_init(dev, &obj->base, stolen->size)) |
| 277 | goto cleanup; |
| 278 | |
| 279 | i915_gem_object_init(obj, &i915_gem_object_stolen_ops); |
| 280 | |
| 281 | obj->pages = i915_pages_create_for_stolen(dev, |
| 282 | stolen->start, stolen->size); |
| 283 | if (obj->pages == NULL) |
| 284 | goto cleanup; |
| 285 | |
| 286 | obj->has_dma_mapping = true; |
Ben Widawsky | dd53e1b | 2013-05-31 14:46:19 -0700 | [diff] [blame] | 287 | i915_gem_object_pin_pages(obj); |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 288 | obj->stolen = stolen; |
| 289 | |
| 290 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; |
| 291 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
| 292 | obj->cache_level = I915_CACHE_NONE; |
| 293 | |
| 294 | return obj; |
| 295 | |
| 296 | cleanup: |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 297 | i915_gem_object_free(obj); |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | struct drm_i915_gem_object * |
| 302 | i915_gem_object_create_stolen(struct drm_device *dev, u32 size) |
| 303 | { |
| 304 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 305 | struct drm_i915_gem_object *obj; |
| 306 | struct drm_mm_node *stolen; |
| 307 | |
Daniel Vetter | 446f8d8 | 2013-07-02 10:48:31 +0200 | [diff] [blame] | 308 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 309 | return NULL; |
| 310 | |
| 311 | DRM_DEBUG_KMS("creating stolen object: size=%x\n", size); |
| 312 | if (size == 0) |
| 313 | return NULL; |
| 314 | |
| 315 | stolen = drm_mm_search_free(&dev_priv->mm.stolen, size, 4096, 0); |
| 316 | if (stolen) |
| 317 | stolen = drm_mm_get_block(stolen, size, 4096); |
| 318 | if (stolen == NULL) |
| 319 | return NULL; |
| 320 | |
| 321 | obj = _i915_gem_object_create_stolen(dev, stolen); |
| 322 | if (obj) |
| 323 | return obj; |
| 324 | |
| 325 | drm_mm_put_block(stolen); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 329 | struct drm_i915_gem_object * |
| 330 | i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev, |
| 331 | u32 stolen_offset, |
| 332 | u32 gtt_offset, |
| 333 | u32 size) |
| 334 | { |
| 335 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 336 | struct i915_address_space *ggtt = &dev_priv->gtt.base; |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 337 | struct drm_i915_gem_object *obj; |
| 338 | struct drm_mm_node *stolen; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 339 | struct i915_vma *vma; |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 340 | int ret; |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 341 | |
Daniel Vetter | 446f8d8 | 2013-07-02 10:48:31 +0200 | [diff] [blame] | 342 | if (!drm_mm_initialized(&dev_priv->mm.stolen)) |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 343 | return NULL; |
| 344 | |
| 345 | DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n", |
| 346 | stolen_offset, gtt_offset, size); |
| 347 | |
| 348 | /* KISS and expect everything to be page-aligned */ |
| 349 | BUG_ON(stolen_offset & 4095); |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 350 | BUG_ON(size & 4095); |
| 351 | |
| 352 | if (WARN_ON(size == 0)) |
| 353 | return NULL; |
| 354 | |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 355 | stolen = kzalloc(sizeof(*stolen), GFP_KERNEL); |
| 356 | if (!stolen) |
| 357 | return NULL; |
| 358 | |
Ben Widawsky | 338710e | 2013-07-05 14:41:03 -0700 | [diff] [blame] | 359 | stolen->start = stolen_offset; |
| 360 | stolen->size = size; |
| 361 | ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen); |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 362 | if (ret) { |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 363 | DRM_DEBUG_KMS("failed to allocate stolen space\n"); |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 364 | kfree(stolen); |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | obj = _i915_gem_object_create_stolen(dev, stolen); |
| 369 | if (obj == NULL) { |
| 370 | DRM_DEBUG_KMS("failed to allocate stolen object\n"); |
| 371 | drm_mm_put_block(stolen); |
| 372 | return NULL; |
| 373 | } |
| 374 | |
Jesse Barnes | 3727d55 | 2013-05-08 10:45:14 -0700 | [diff] [blame] | 375 | /* Some objects just need physical mem from stolen space */ |
Daniel Vetter | 190d6cd | 2013-07-04 13:06:28 +0200 | [diff] [blame] | 376 | if (gtt_offset == I915_GTT_OFFSET_NONE) |
Jesse Barnes | 3727d55 | 2013-05-08 10:45:14 -0700 | [diff] [blame] | 377 | return obj; |
| 378 | |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 379 | vma = i915_gem_vma_create(obj, ggtt); |
Dan Carpenter | db473b3 | 2013-07-19 08:45:46 +0300 | [diff] [blame] | 380 | if (IS_ERR(vma)) { |
| 381 | ret = PTR_ERR(vma); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 382 | goto err_out; |
| 383 | } |
| 384 | |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 385 | /* To simplify the initialisation sequence between KMS and GTT, |
| 386 | * we allow construction of the stolen object prior to |
| 387 | * setting up the GTT space. The actual reservation will occur |
| 388 | * later. |
| 389 | */ |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 390 | vma->node.start = gtt_offset; |
| 391 | vma->node.size = size; |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 392 | if (drm_mm_initialized(&ggtt->mm)) { |
| 393 | ret = drm_mm_reserve_node(&ggtt->mm, &vma->node); |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 394 | if (ret) { |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 395 | DRM_DEBUG_KMS("failed to allocate stolen GTT space\n"); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 396 | i915_gem_vma_destroy(vma); |
Ben Widawsky | f7f1818 | 2013-07-17 12:19:02 -0700 | [diff] [blame] | 397 | goto err_out; |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 398 | } |
Ben Widawsky | edd41a8 | 2013-07-05 14:41:05 -0700 | [diff] [blame] | 399 | } |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 400 | |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 401 | obj->has_global_gtt_mapping = 1; |
| 402 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 403 | list_add_tail(&obj->global_list, &dev_priv->mm.bound_list); |
Ben Widawsky | ca191b1 | 2013-07-31 17:00:14 -0700 | [diff] [blame] | 404 | list_add_tail(&vma->mm_list, &ggtt->inactive_list); |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 405 | |
| 406 | return obj; |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 407 | |
Ben Widawsky | f7f1818 | 2013-07-17 12:19:02 -0700 | [diff] [blame] | 408 | err_out: |
| 409 | drm_mm_put_block(stolen); |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 410 | drm_gem_object_unreference(&obj->base); |
| 411 | return NULL; |
Chris Wilson | 866d12b | 2013-02-19 13:31:37 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 414 | void |
| 415 | i915_gem_object_release_stolen(struct drm_i915_gem_object *obj) |
| 416 | { |
| 417 | if (obj->stolen) { |
| 418 | drm_mm_put_block(obj->stolen); |
| 419 | obj->stolen = NULL; |
| 420 | } |
| 421 | } |