Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007, Intel Corporation. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com> |
| 19 | * Alan Cox <alan@linux.intel.com> |
| 20 | */ |
| 21 | |
| 22 | #include <drm/drmP.h> |
Alan Cox | e912b6d | 2012-01-24 16:57:42 +0000 | [diff] [blame] | 23 | #include <linux/shmem_fs.h> |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 24 | #include "psb_drv.h" |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * GTT resource allocator - manage page mappings in GTT space |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * psb_gtt_mask_pte - generate GTT pte entry |
| 33 | * @pfn: page number to encode |
| 34 | * @type: type of memory in the GTT |
| 35 | * |
| 36 | * Set the GTT entry for the appropriate memory type. |
| 37 | */ |
| 38 | static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type) |
| 39 | { |
| 40 | uint32_t mask = PSB_PTE_VALID; |
| 41 | |
Alan Cox | 398b470 | 2012-04-25 14:38:47 +0100 | [diff] [blame] | 42 | /* Ensure we explode rather than put an invalid low mapping of |
| 43 | a high mapping page into the gtt */ |
| 44 | BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT)); |
| 45 | |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 46 | if (type & PSB_MMU_CACHED_MEMORY) |
| 47 | mask |= PSB_PTE_CACHED; |
| 48 | if (type & PSB_MMU_RO_MEMORY) |
| 49 | mask |= PSB_PTE_RO; |
| 50 | if (type & PSB_MMU_WO_MEMORY) |
| 51 | mask |= PSB_PTE_WO; |
| 52 | |
| 53 | return (pfn << PAGE_SHIFT) | mask; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * psb_gtt_entry - find the GTT entries for a gtt_range |
| 58 | * @dev: our DRM device |
| 59 | * @r: our GTT range |
| 60 | * |
| 61 | * Given a gtt_range object return the GTT offset of the page table |
| 62 | * entries for this gtt_range |
| 63 | */ |
Kirill A. Shutemov | ffe94d9 | 2012-03-08 16:03:55 +0000 | [diff] [blame] | 64 | static u32 *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r) |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 65 | { |
| 66 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 67 | unsigned long offset; |
| 68 | |
| 69 | offset = r->resource.start - dev_priv->gtt_mem->start; |
| 70 | |
| 71 | return dev_priv->gtt_map + (offset >> PAGE_SHIFT); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * psb_gtt_insert - put an object into the GTT |
| 76 | * @dev: our DRM device |
| 77 | * @r: our GTT range |
| 78 | * |
| 79 | * Take our preallocated GTT range and insert the GEM object into |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 80 | * the GTT. This is protected via the gtt mutex which the caller |
| 81 | * must hold. |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 82 | */ |
| 83 | static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r) |
| 84 | { |
| 85 | u32 *gtt_slot, pte; |
| 86 | struct page **pages; |
| 87 | int i; |
| 88 | |
| 89 | if (r->pages == NULL) { |
| 90 | WARN_ON(1); |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | WARN_ON(r->stolen); /* refcount these maybe ? */ |
| 95 | |
| 96 | gtt_slot = psb_gtt_entry(dev, r); |
| 97 | pages = r->pages; |
| 98 | |
| 99 | /* Make sure changes are visible to the GPU */ |
Alan Cox | d955e71 | 2012-04-25 14:37:00 +0100 | [diff] [blame] | 100 | set_pages_array_wc(pages, r->npage); |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 101 | |
| 102 | /* Write our page entries into the GTT itself */ |
Alan Cox | a6ba582 | 2011-11-29 22:27:22 +0000 | [diff] [blame] | 103 | for (i = r->roll; i < r->npage; i++) { |
| 104 | pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0); |
| 105 | iowrite32(pte, gtt_slot++); |
| 106 | } |
| 107 | for (i = 0; i < r->roll; i++) { |
| 108 | pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0); |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 109 | iowrite32(pte, gtt_slot++); |
| 110 | } |
| 111 | /* Make sure all the entries are set before we return */ |
| 112 | ioread32(gtt_slot - 1); |
Alan Cox | a6ba582 | 2011-11-29 22:27:22 +0000 | [diff] [blame] | 113 | |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * psb_gtt_remove - remove an object from the GTT |
| 119 | * @dev: our DRM device |
| 120 | * @r: our GTT range |
| 121 | * |
| 122 | * Remove a preallocated GTT range from the GTT. Overwrite all the |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 123 | * page table entries with the dummy page. This is protected via the gtt |
| 124 | * mutex which the caller must hold. |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 125 | */ |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 126 | static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r) |
| 127 | { |
| 128 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 129 | u32 *gtt_slot, pte; |
| 130 | int i; |
| 131 | |
| 132 | WARN_ON(r->stolen); |
| 133 | |
| 134 | gtt_slot = psb_gtt_entry(dev, r); |
| 135 | pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 0); |
| 136 | |
| 137 | for (i = 0; i < r->npage; i++) |
| 138 | iowrite32(pte, gtt_slot++); |
| 139 | ioread32(gtt_slot - 1); |
| 140 | set_pages_array_wb(r->pages, r->npage); |
| 141 | } |
| 142 | |
| 143 | /** |
Alan Cox | a6ba582 | 2011-11-29 22:27:22 +0000 | [diff] [blame] | 144 | * psb_gtt_roll - set scrolling position |
| 145 | * @dev: our DRM device |
| 146 | * @r: the gtt mapping we are using |
| 147 | * @roll: roll offset |
| 148 | * |
| 149 | * Roll an existing pinned mapping by moving the pages through the GTT. |
| 150 | * This allows us to implement hardware scrolling on the consoles without |
| 151 | * a 2D engine |
| 152 | */ |
| 153 | void psb_gtt_roll(struct drm_device *dev, struct gtt_range *r, int roll) |
| 154 | { |
| 155 | u32 *gtt_slot, pte; |
| 156 | int i; |
| 157 | |
| 158 | if (roll >= r->npage) { |
| 159 | WARN_ON(1); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | r->roll = roll; |
| 164 | |
| 165 | /* Not currently in the GTT - no worry we will write the mapping at |
| 166 | the right position when it gets pinned */ |
| 167 | if (!r->stolen && !r->in_gart) |
| 168 | return; |
| 169 | |
| 170 | gtt_slot = psb_gtt_entry(dev, r); |
| 171 | |
| 172 | for (i = r->roll; i < r->npage; i++) { |
| 173 | pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0); |
| 174 | iowrite32(pte, gtt_slot++); |
| 175 | } |
| 176 | for (i = 0; i < r->roll; i++) { |
| 177 | pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0); |
| 178 | iowrite32(pte, gtt_slot++); |
| 179 | } |
| 180 | ioread32(gtt_slot - 1); |
| 181 | } |
| 182 | |
| 183 | /** |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 184 | * psb_gtt_attach_pages - attach and pin GEM pages |
| 185 | * @gt: the gtt range |
| 186 | * |
| 187 | * Pin and build an in kernel list of the pages that back our GEM object. |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 188 | * While we hold this the pages cannot be swapped out. This is protected |
| 189 | * via the gtt mutex which the caller must hold. |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 190 | */ |
| 191 | static int psb_gtt_attach_pages(struct gtt_range *gt) |
| 192 | { |
| 193 | struct inode *inode; |
| 194 | struct address_space *mapping; |
| 195 | int i; |
| 196 | struct page *p; |
| 197 | int pages = gt->gem.size / PAGE_SIZE; |
| 198 | |
| 199 | WARN_ON(gt->pages); |
| 200 | |
| 201 | /* This is the shared memory object that backs the GEM resource */ |
| 202 | inode = gt->gem.filp->f_path.dentry->d_inode; |
| 203 | mapping = inode->i_mapping; |
| 204 | |
| 205 | gt->pages = kmalloc(pages * sizeof(struct page *), GFP_KERNEL); |
| 206 | if (gt->pages == NULL) |
| 207 | return -ENOMEM; |
| 208 | gt->npage = pages; |
| 209 | |
| 210 | for (i = 0; i < pages; i++) { |
Alan Cox | e912b6d | 2012-01-24 16:57:42 +0000 | [diff] [blame] | 211 | p = shmem_read_mapping_page(mapping, i); |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 212 | if (IS_ERR(p)) |
| 213 | goto err; |
| 214 | gt->pages[i] = p; |
| 215 | } |
| 216 | return 0; |
| 217 | |
| 218 | err: |
| 219 | while (i--) |
| 220 | page_cache_release(gt->pages[i]); |
| 221 | kfree(gt->pages); |
| 222 | gt->pages = NULL; |
| 223 | return PTR_ERR(p); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * psb_gtt_detach_pages - attach and pin GEM pages |
| 228 | * @gt: the gtt range |
| 229 | * |
| 230 | * Undo the effect of psb_gtt_attach_pages. At this point the pages |
| 231 | * must have been removed from the GTT as they could now be paged out |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 232 | * and move bus address. This is protected via the gtt mutex which the |
| 233 | * caller must hold. |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 234 | */ |
| 235 | static void psb_gtt_detach_pages(struct gtt_range *gt) |
| 236 | { |
| 237 | int i; |
| 238 | for (i = 0; i < gt->npage; i++) { |
| 239 | /* FIXME: do we need to force dirty */ |
| 240 | set_page_dirty(gt->pages[i]); |
| 241 | page_cache_release(gt->pages[i]); |
| 242 | } |
| 243 | kfree(gt->pages); |
| 244 | gt->pages = NULL; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * psb_gtt_pin - pin pages into the GTT |
| 249 | * @gt: range to pin |
| 250 | * |
| 251 | * Pin a set of pages into the GTT. The pins are refcounted so that |
| 252 | * multiple pins need multiple unpins to undo. |
| 253 | * |
| 254 | * Non GEM backed objects treat this as a no-op as they are always GTT |
| 255 | * backed objects. |
| 256 | */ |
| 257 | int psb_gtt_pin(struct gtt_range *gt) |
| 258 | { |
| 259 | int ret = 0; |
| 260 | struct drm_device *dev = gt->gem.dev; |
| 261 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 262 | |
| 263 | mutex_lock(&dev_priv->gtt_mutex); |
| 264 | |
| 265 | if (gt->in_gart == 0 && gt->stolen == 0) { |
| 266 | ret = psb_gtt_attach_pages(gt); |
| 267 | if (ret < 0) |
| 268 | goto out; |
| 269 | ret = psb_gtt_insert(dev, gt); |
| 270 | if (ret < 0) { |
| 271 | psb_gtt_detach_pages(gt); |
| 272 | goto out; |
| 273 | } |
| 274 | } |
| 275 | gt->in_gart++; |
| 276 | out: |
| 277 | mutex_unlock(&dev_priv->gtt_mutex); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * psb_gtt_unpin - Drop a GTT pin requirement |
| 283 | * @gt: range to pin |
| 284 | * |
| 285 | * Undoes the effect of psb_gtt_pin. On the last drop the GEM object |
| 286 | * will be removed from the GTT which will also drop the page references |
| 287 | * and allow the VM to clean up or page stuff. |
| 288 | * |
| 289 | * Non GEM backed objects treat this as a no-op as they are always GTT |
| 290 | * backed objects. |
| 291 | */ |
| 292 | void psb_gtt_unpin(struct gtt_range *gt) |
| 293 | { |
| 294 | struct drm_device *dev = gt->gem.dev; |
| 295 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 296 | |
| 297 | mutex_lock(&dev_priv->gtt_mutex); |
| 298 | |
| 299 | WARN_ON(!gt->in_gart); |
| 300 | |
| 301 | gt->in_gart--; |
| 302 | if (gt->in_gart == 0 && gt->stolen == 0) { |
| 303 | psb_gtt_remove(dev, gt); |
| 304 | psb_gtt_detach_pages(gt); |
| 305 | } |
| 306 | mutex_unlock(&dev_priv->gtt_mutex); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * GTT resource allocator - allocate and manage GTT address space |
| 311 | */ |
| 312 | |
| 313 | /** |
| 314 | * psb_gtt_alloc_range - allocate GTT address space |
| 315 | * @dev: Our DRM device |
| 316 | * @len: length (bytes) of address space required |
| 317 | * @name: resource name |
| 318 | * @backed: resource should be backed by stolen pages |
| 319 | * |
| 320 | * Ask the kernel core to find us a suitable range of addresses |
| 321 | * to use for a GTT mapping. |
| 322 | * |
| 323 | * Returns a gtt_range structure describing the object, or NULL on |
| 324 | * error. On successful return the resource is both allocated and marked |
| 325 | * as in use. |
| 326 | */ |
| 327 | struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len, |
| 328 | const char *name, int backed) |
| 329 | { |
| 330 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 331 | struct gtt_range *gt; |
| 332 | struct resource *r = dev_priv->gtt_mem; |
| 333 | int ret; |
| 334 | unsigned long start, end; |
| 335 | |
| 336 | if (backed) { |
| 337 | /* The start of the GTT is the stolen pages */ |
| 338 | start = r->start; |
| 339 | end = r->start + dev_priv->gtt.stolen_size - 1; |
| 340 | } else { |
| 341 | /* The rest we will use for GEM backed objects */ |
| 342 | start = r->start + dev_priv->gtt.stolen_size; |
| 343 | end = r->end; |
| 344 | } |
| 345 | |
| 346 | gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL); |
| 347 | if (gt == NULL) |
| 348 | return NULL; |
| 349 | gt->resource.name = name; |
| 350 | gt->stolen = backed; |
| 351 | gt->in_gart = backed; |
Alan Cox | a6ba582 | 2011-11-29 22:27:22 +0000 | [diff] [blame] | 352 | gt->roll = 0; |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 353 | /* Ensure this is set for non GEM objects */ |
| 354 | gt->gem.dev = dev; |
| 355 | ret = allocate_resource(dev_priv->gtt_mem, >->resource, |
| 356 | len, start, end, PAGE_SIZE, NULL, NULL); |
| 357 | if (ret == 0) { |
| 358 | gt->offset = gt->resource.start - r->start; |
| 359 | return gt; |
| 360 | } |
| 361 | kfree(gt); |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * psb_gtt_free_range - release GTT address space |
| 367 | * @dev: our DRM device |
| 368 | * @gt: a mapping created with psb_gtt_alloc_range |
| 369 | * |
| 370 | * Release a resource that was allocated with psb_gtt_alloc_range. If the |
| 371 | * object has been pinned by mmap users we clean this up here currently. |
| 372 | */ |
| 373 | void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt) |
| 374 | { |
| 375 | /* Undo the mmap pin if we are destroying the object */ |
| 376 | if (gt->mmapping) { |
| 377 | psb_gtt_unpin(gt); |
| 378 | gt->mmapping = 0; |
| 379 | } |
| 380 | WARN_ON(gt->in_gart && !gt->stolen); |
| 381 | release_resource(>->resource); |
| 382 | kfree(gt); |
| 383 | } |
| 384 | |
Kirill A. Shutemov | ffe94d9 | 2012-03-08 16:03:55 +0000 | [diff] [blame] | 385 | static void psb_gtt_alloc(struct drm_device *dev) |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 386 | { |
| 387 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 388 | init_rwsem(&dev_priv->gtt.sem); |
| 389 | } |
| 390 | |
| 391 | void psb_gtt_takedown(struct drm_device *dev) |
| 392 | { |
| 393 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 394 | |
| 395 | if (dev_priv->gtt_map) { |
| 396 | iounmap(dev_priv->gtt_map); |
| 397 | dev_priv->gtt_map = NULL; |
| 398 | } |
| 399 | if (dev_priv->gtt_initialized) { |
| 400 | pci_write_config_word(dev->pdev, PSB_GMCH_CTRL, |
| 401 | dev_priv->gmch_ctrl); |
| 402 | PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL); |
| 403 | (void) PSB_RVDC32(PSB_PGETBL_CTL); |
| 404 | } |
| 405 | if (dev_priv->vram_addr) |
| 406 | iounmap(dev_priv->gtt_map); |
| 407 | } |
| 408 | |
| 409 | int psb_gtt_init(struct drm_device *dev, int resume) |
| 410 | { |
| 411 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 412 | unsigned gtt_pages; |
| 413 | unsigned long stolen_size, vram_stolen_size; |
| 414 | unsigned i, num_pages; |
| 415 | unsigned pfn_base; |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 416 | uint32_t dvmt_mode = 0; |
| 417 | struct psb_gtt *pg; |
| 418 | |
| 419 | int ret = 0; |
| 420 | uint32_t pte; |
| 421 | |
| 422 | mutex_init(&dev_priv->gtt_mutex); |
| 423 | |
| 424 | psb_gtt_alloc(dev); |
| 425 | pg = &dev_priv->gtt; |
| 426 | |
| 427 | /* Enable the GTT */ |
| 428 | pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl); |
| 429 | pci_write_config_word(dev->pdev, PSB_GMCH_CTRL, |
| 430 | dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED); |
| 431 | |
| 432 | dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL); |
| 433 | PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL); |
| 434 | (void) PSB_RVDC32(PSB_PGETBL_CTL); |
| 435 | |
| 436 | /* The root resource we allocate address space from */ |
| 437 | dev_priv->gtt_initialized = 1; |
| 438 | |
| 439 | pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK; |
| 440 | |
| 441 | /* |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 442 | * The video mmu has a hw bug when accessing 0x0D0000000. |
| 443 | * Make gatt start at 0x0e000,0000. This doesn't actually |
| 444 | * matter for us but may do if the video acceleration ever |
| 445 | * gets opened up. |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 446 | */ |
| 447 | pg->mmu_gatt_start = 0xE0000000; |
| 448 | |
| 449 | pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE); |
| 450 | gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) |
| 451 | >> PAGE_SHIFT; |
Alan Cox | 055bf38 | 2012-03-05 14:22:16 +0000 | [diff] [blame] | 452 | /* CDV doesn't report this. In which case the system has 64 gtt pages */ |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 453 | if (pg->gtt_start == 0 || gtt_pages == 0) { |
Alan Cox | 055bf38 | 2012-03-05 14:22:16 +0000 | [diff] [blame] | 454 | dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n"); |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 455 | gtt_pages = 64; |
| 456 | pg->gtt_start = dev_priv->pge_ctl; |
| 457 | } |
| 458 | |
| 459 | pg->gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE); |
| 460 | pg->gatt_pages = pci_resource_len(dev->pdev, PSB_GATT_RESOURCE) |
| 461 | >> PAGE_SHIFT; |
| 462 | dev_priv->gtt_mem = &dev->pdev->resource[PSB_GATT_RESOURCE]; |
| 463 | |
| 464 | if (pg->gatt_pages == 0 || pg->gatt_start == 0) { |
| 465 | static struct resource fudge; /* Preferably peppermint */ |
Alan Cox | 055bf38 | 2012-03-05 14:22:16 +0000 | [diff] [blame] | 466 | /* This can occur on CDV systems. Fudge it in this case. |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 467 | We really don't care what imaginary space is being allocated |
| 468 | at this point */ |
Alan Cox | 055bf38 | 2012-03-05 14:22:16 +0000 | [diff] [blame] | 469 | dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n"); |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 470 | pg->gatt_start = 0x40000000; |
| 471 | pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT; |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 472 | /* This is a little confusing but in fact the GTT is providing |
| 473 | a view from the GPU into memory and not vice versa. As such |
| 474 | this is really allocating space that is not the same as the |
| 475 | CPU address space on CDV */ |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 476 | fudge.start = 0x40000000; |
| 477 | fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1; |
| 478 | fudge.name = "fudge"; |
| 479 | fudge.flags = IORESOURCE_MEM; |
| 480 | dev_priv->gtt_mem = &fudge; |
| 481 | } |
| 482 | |
| 483 | pci_read_config_dword(dev->pdev, PSB_BSM, &dev_priv->stolen_base); |
| 484 | vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base |
| 485 | - PAGE_SIZE; |
| 486 | |
| 487 | stolen_size = vram_stolen_size; |
| 488 | |
| 489 | printk(KERN_INFO "Stolen memory information\n"); |
| 490 | printk(KERN_INFO " base in RAM: 0x%x\n", dev_priv->stolen_base); |
| 491 | printk(KERN_INFO " size: %luK, calculated by (GTT RAM base) - (Stolen base), seems wrong\n", |
| 492 | vram_stolen_size/1024); |
| 493 | dvmt_mode = (dev_priv->gmch_ctrl >> 4) & 0x7; |
| 494 | printk(KERN_INFO " the correct size should be: %dM(dvmt mode=%d)\n", |
| 495 | (dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode); |
| 496 | |
| 497 | if (resume && (gtt_pages != pg->gtt_pages) && |
| 498 | (stolen_size != pg->stolen_size)) { |
| 499 | dev_err(dev->dev, "GTT resume error.\n"); |
| 500 | ret = -EINVAL; |
| 501 | goto out_err; |
| 502 | } |
| 503 | |
| 504 | pg->gtt_pages = gtt_pages; |
| 505 | pg->stolen_size = stolen_size; |
| 506 | dev_priv->vram_stolen_size = vram_stolen_size; |
| 507 | |
| 508 | /* |
| 509 | * Map the GTT and the stolen memory area |
| 510 | */ |
| 511 | dev_priv->gtt_map = ioremap_nocache(pg->gtt_phys_start, |
| 512 | gtt_pages << PAGE_SHIFT); |
| 513 | if (!dev_priv->gtt_map) { |
| 514 | dev_err(dev->dev, "Failure to map gtt.\n"); |
| 515 | ret = -ENOMEM; |
| 516 | goto out_err; |
| 517 | } |
| 518 | |
| 519 | dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base, stolen_size); |
| 520 | if (!dev_priv->vram_addr) { |
| 521 | dev_err(dev->dev, "Failure to map stolen base.\n"); |
| 522 | ret = -ENOMEM; |
| 523 | goto out_err; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Insert vram stolen pages into the GTT |
| 528 | */ |
| 529 | |
| 530 | pfn_base = dev_priv->stolen_base >> PAGE_SHIFT; |
Kirill A. Shutemov | f728bd1 | 2012-05-03 15:07:27 +0100 | [diff] [blame^] | 531 | num_pages = vram_stolen_size >> PAGE_SHIFT; |
Alan Cox | 8c8f1c9 | 2011-11-03 18:21:09 +0000 | [diff] [blame] | 532 | printk(KERN_INFO"Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n", |
| 533 | num_pages, pfn_base << PAGE_SHIFT, 0); |
| 534 | for (i = 0; i < num_pages; ++i) { |
| 535 | pte = psb_gtt_mask_pte(pfn_base + i, 0); |
| 536 | iowrite32(pte, dev_priv->gtt_map + i); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Init rest of GTT to the scratch page to avoid accidents or scribbles |
| 541 | */ |
| 542 | |
| 543 | pfn_base = page_to_pfn(dev_priv->scratch_page); |
| 544 | pte = psb_gtt_mask_pte(pfn_base, 0); |
| 545 | for (; i < gtt_pages; ++i) |
| 546 | iowrite32(pte, dev_priv->gtt_map + i); |
| 547 | |
| 548 | (void) ioread32(dev_priv->gtt_map + i - 1); |
| 549 | return 0; |
| 550 | |
| 551 | out_err: |
| 552 | psb_gtt_takedown(dev); |
| 553 | return ret; |
| 554 | } |