Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 Dave Airlied |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | /* |
| 25 | * Authors: Dave Airlied <airlied@linux.ie> |
| 26 | * Ben Skeggs <darktama@iinet.net.au> |
| 27 | * Jeremy Kolb <jkolb@brandeis.edu> |
| 28 | */ |
| 29 | |
| 30 | #include "drmP.h" |
| 31 | |
| 32 | #include "nouveau_drm.h" |
| 33 | #include "nouveau_drv.h" |
| 34 | #include "nouveau_dma.h" |
| 35 | |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 36 | #include <linux/log2.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 38 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 39 | static void |
| 40 | nouveau_bo_del_ttm(struct ttm_buffer_object *bo) |
| 41 | { |
| 42 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 43 | struct drm_device *dev = dev_priv->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 44 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 45 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 46 | if (unlikely(nvbo->gem)) |
| 47 | DRM_ERROR("bo %p still attached to GEM object\n", bo); |
| 48 | |
Francisco Jerez | a5cf68b | 2010-10-24 16:14:41 +0200 | [diff] [blame] | 49 | nv10_mem_put_tile_region(dev, nvbo->tile, NULL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 50 | kfree(nvbo); |
| 51 | } |
| 52 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 53 | static void |
| 54 | nouveau_bo_fixup_align(struct drm_device *dev, |
| 55 | uint32_t tile_mode, uint32_t tile_flags, |
| 56 | int *align, int *size) |
| 57 | { |
| 58 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 59 | |
| 60 | /* |
| 61 | * Some of the tile_flags have a periodic structure of N*4096 bytes, |
Maarten Maathuis | eb1dba0 | 2009-12-27 12:22:07 +0100 | [diff] [blame] | 62 | * align to to that as well as the page size. Align the size to the |
| 63 | * appropriate boundaries. This does imply that sizes are rounded up |
| 64 | * 3-7 pages, so be aware of this and do not waste memory by allocating |
| 65 | * many small buffers. |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 66 | */ |
| 67 | if (dev_priv->card_type == NV_50) { |
Ben Skeggs | a76fb4e | 2010-03-18 09:45:20 +1000 | [diff] [blame] | 68 | uint32_t block_size = dev_priv->vram_size >> 15; |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 69 | int i; |
| 70 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 71 | switch (tile_flags) { |
| 72 | case 0x1800: |
| 73 | case 0x2800: |
| 74 | case 0x4800: |
| 75 | case 0x7a00: |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 76 | if (is_power_of_2(block_size)) { |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 77 | for (i = 1; i < 10; i++) { |
| 78 | *align = 12 * i * block_size; |
| 79 | if (!(*align % 65536)) |
| 80 | break; |
| 81 | } |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 82 | } else { |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 83 | for (i = 1; i < 10; i++) { |
| 84 | *align = 8 * i * block_size; |
| 85 | if (!(*align % 65536)) |
| 86 | break; |
| 87 | } |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 88 | } |
Maarten Maathuis | eb1dba0 | 2009-12-27 12:22:07 +0100 | [diff] [blame] | 89 | *size = roundup(*size, *align); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 90 | break; |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | } else { |
| 96 | if (tile_mode) { |
| 97 | if (dev_priv->chipset >= 0x40) { |
| 98 | *align = 65536; |
| 99 | *size = roundup(*size, 64 * tile_mode); |
| 100 | |
| 101 | } else if (dev_priv->chipset >= 0x30) { |
| 102 | *align = 32768; |
| 103 | *size = roundup(*size, 64 * tile_mode); |
| 104 | |
| 105 | } else if (dev_priv->chipset >= 0x20) { |
| 106 | *align = 16384; |
| 107 | *size = roundup(*size, 64 * tile_mode); |
| 108 | |
| 109 | } else if (dev_priv->chipset >= 0x10) { |
| 110 | *align = 16384; |
| 111 | *size = roundup(*size, 32 * tile_mode); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Maarten Maathuis | 1c7059e | 2009-12-25 18:51:17 +0100 | [diff] [blame] | 116 | /* ALIGN works only on powers of two. */ |
| 117 | *size = roundup(*size, PAGE_SIZE); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 118 | |
| 119 | if (dev_priv->card_type == NV_50) { |
Maarten Maathuis | 1c7059e | 2009-12-25 18:51:17 +0100 | [diff] [blame] | 120 | *size = roundup(*size, 65536); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 121 | *align = max(65536, *align); |
| 122 | } |
| 123 | } |
| 124 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 125 | int |
| 126 | nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan, |
| 127 | int size, int align, uint32_t flags, uint32_t tile_mode, |
| 128 | uint32_t tile_flags, bool no_vm, bool mappable, |
| 129 | struct nouveau_bo **pnvbo) |
| 130 | { |
| 131 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 132 | struct nouveau_bo *nvbo; |
Francisco Jerez | 8dea4a1 | 2009-12-16 19:03:28 +0100 | [diff] [blame] | 133 | int ret = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 134 | |
| 135 | nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL); |
| 136 | if (!nvbo) |
| 137 | return -ENOMEM; |
| 138 | INIT_LIST_HEAD(&nvbo->head); |
| 139 | INIT_LIST_HEAD(&nvbo->entry); |
| 140 | nvbo->mappable = mappable; |
| 141 | nvbo->no_vm = no_vm; |
| 142 | nvbo->tile_mode = tile_mode; |
| 143 | nvbo->tile_flags = tile_flags; |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 144 | nvbo->bo.bdev = &dev_priv->ttm.bdev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 145 | |
Francisco Jerez | f13b326 | 2010-10-10 06:01:08 +0200 | [diff] [blame] | 146 | nouveau_bo_fixup_align(dev, tile_mode, nouveau_bo_tile_layout(nvbo), |
| 147 | &align, &size); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 148 | align >>= PAGE_SHIFT; |
| 149 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 150 | nouveau_bo_placement_set(nvbo, flags, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 151 | |
| 152 | nvbo->channel = chan; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 153 | ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size, |
| 154 | ttm_bo_type_device, &nvbo->placement, align, 0, |
| 155 | false, NULL, size, nouveau_bo_del_ttm); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 156 | if (ret) { |
| 157 | /* ttm will call nouveau_bo_del_ttm if it fails.. */ |
| 158 | return ret; |
| 159 | } |
Ben Skeggs | 90af89b | 2010-04-15 14:42:34 +1000 | [diff] [blame] | 160 | nvbo->channel = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 161 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 162 | *pnvbo = nvbo; |
| 163 | return 0; |
| 164 | } |
| 165 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 166 | static void |
| 167 | set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 168 | { |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 169 | *n = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 170 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 171 | if (type & TTM_PL_FLAG_VRAM) |
| 172 | pl[(*n)++] = TTM_PL_FLAG_VRAM | flags; |
| 173 | if (type & TTM_PL_FLAG_TT) |
| 174 | pl[(*n)++] = TTM_PL_FLAG_TT | flags; |
| 175 | if (type & TTM_PL_FLAG_SYSTEM) |
| 176 | pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags; |
| 177 | } |
Ben Skeggs | 37cb3e08 | 2009-12-16 16:22:42 +1000 | [diff] [blame] | 178 | |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 179 | static void |
| 180 | set_placement_range(struct nouveau_bo *nvbo, uint32_t type) |
| 181 | { |
| 182 | struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev); |
| 183 | |
| 184 | if (dev_priv->card_type == NV_10 && |
| 185 | nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM)) { |
| 186 | /* |
| 187 | * Make sure that the color and depth buffers are handled |
| 188 | * by independent memory controller units. Up to a 9x |
| 189 | * speed up when alpha-blending and depth-test are enabled |
| 190 | * at the same time. |
| 191 | */ |
| 192 | int vram_pages = dev_priv->vram_size >> PAGE_SHIFT; |
| 193 | |
| 194 | if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) { |
| 195 | nvbo->placement.fpfn = vram_pages / 2; |
| 196 | nvbo->placement.lpfn = ~0; |
| 197 | } else { |
| 198 | nvbo->placement.fpfn = 0; |
| 199 | nvbo->placement.lpfn = vram_pages / 2; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 204 | void |
| 205 | nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy) |
| 206 | { |
| 207 | struct ttm_placement *pl = &nvbo->placement; |
| 208 | uint32_t flags = TTM_PL_MASK_CACHING | |
| 209 | (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0); |
| 210 | |
| 211 | pl->placement = nvbo->placements; |
| 212 | set_placement_list(nvbo->placements, &pl->num_placement, |
| 213 | type, flags); |
| 214 | |
| 215 | pl->busy_placement = nvbo->busy_placements; |
| 216 | set_placement_list(nvbo->busy_placements, &pl->num_busy_placement, |
| 217 | type | busy, flags); |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 218 | |
| 219 | set_placement_range(nvbo, type); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int |
| 223 | nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype) |
| 224 | { |
| 225 | struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev); |
| 226 | struct ttm_buffer_object *bo = &nvbo->bo; |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 227 | int ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 228 | |
| 229 | if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) { |
| 230 | NV_ERROR(nouveau_bdev(bo->bdev)->dev, |
| 231 | "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo, |
| 232 | 1 << bo->mem.mem_type, memtype); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | if (nvbo->pin_refcnt++) |
| 237 | return 0; |
| 238 | |
| 239 | ret = ttm_bo_reserve(bo, false, false, false, 0); |
| 240 | if (ret) |
| 241 | goto out; |
| 242 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 243 | nouveau_bo_placement_set(nvbo, memtype, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 244 | |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame^] | 245 | ret = nouveau_bo_validate(nvbo, false, false, false); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 246 | if (ret == 0) { |
| 247 | switch (bo->mem.mem_type) { |
| 248 | case TTM_PL_VRAM: |
| 249 | dev_priv->fb_aper_free -= bo->mem.size; |
| 250 | break; |
| 251 | case TTM_PL_TT: |
| 252 | dev_priv->gart_info.aper_free -= bo->mem.size; |
| 253 | break; |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | ttm_bo_unreserve(bo); |
| 259 | out: |
| 260 | if (unlikely(ret)) |
| 261 | nvbo->pin_refcnt--; |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | int |
| 266 | nouveau_bo_unpin(struct nouveau_bo *nvbo) |
| 267 | { |
| 268 | struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev); |
| 269 | struct ttm_buffer_object *bo = &nvbo->bo; |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 270 | int ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 271 | |
| 272 | if (--nvbo->pin_refcnt) |
| 273 | return 0; |
| 274 | |
| 275 | ret = ttm_bo_reserve(bo, false, false, false, 0); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 279 | nouveau_bo_placement_set(nvbo, bo->mem.placement, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 280 | |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame^] | 281 | ret = nouveau_bo_validate(nvbo, false, false, false); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 282 | if (ret == 0) { |
| 283 | switch (bo->mem.mem_type) { |
| 284 | case TTM_PL_VRAM: |
| 285 | dev_priv->fb_aper_free += bo->mem.size; |
| 286 | break; |
| 287 | case TTM_PL_TT: |
| 288 | dev_priv->gart_info.aper_free += bo->mem.size; |
| 289 | break; |
| 290 | default: |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | ttm_bo_unreserve(bo); |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | int |
| 300 | nouveau_bo_map(struct nouveau_bo *nvbo) |
| 301 | { |
| 302 | int ret; |
| 303 | |
| 304 | ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
| 308 | ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap); |
| 309 | ttm_bo_unreserve(&nvbo->bo); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | nouveau_bo_unmap(struct nouveau_bo *nvbo) |
| 315 | { |
Ben Skeggs | 9d59e8a | 2010-08-27 13:04:41 +1000 | [diff] [blame] | 316 | if (nvbo) |
| 317 | ttm_bo_kunmap(&nvbo->kmap); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 318 | } |
| 319 | |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame^] | 320 | int |
| 321 | nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible, |
| 322 | bool no_wait_reserve, bool no_wait_gpu) |
| 323 | { |
| 324 | int ret; |
| 325 | |
| 326 | ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible, |
| 327 | no_wait_reserve, no_wait_gpu); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 334 | u16 |
| 335 | nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index) |
| 336 | { |
| 337 | bool is_iomem; |
| 338 | u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 339 | mem = &mem[index]; |
| 340 | if (is_iomem) |
| 341 | return ioread16_native((void __force __iomem *)mem); |
| 342 | else |
| 343 | return *mem; |
| 344 | } |
| 345 | |
| 346 | void |
| 347 | nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) |
| 348 | { |
| 349 | bool is_iomem; |
| 350 | u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 351 | mem = &mem[index]; |
| 352 | if (is_iomem) |
| 353 | iowrite16_native(val, (void __force __iomem *)mem); |
| 354 | else |
| 355 | *mem = val; |
| 356 | } |
| 357 | |
| 358 | u32 |
| 359 | nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index) |
| 360 | { |
| 361 | bool is_iomem; |
| 362 | u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 363 | mem = &mem[index]; |
| 364 | if (is_iomem) |
| 365 | return ioread32_native((void __force __iomem *)mem); |
| 366 | else |
| 367 | return *mem; |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) |
| 372 | { |
| 373 | bool is_iomem; |
| 374 | u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 375 | mem = &mem[index]; |
| 376 | if (is_iomem) |
| 377 | iowrite32_native(val, (void __force __iomem *)mem); |
| 378 | else |
| 379 | *mem = val; |
| 380 | } |
| 381 | |
| 382 | static struct ttm_backend * |
| 383 | nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev) |
| 384 | { |
| 385 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev); |
| 386 | struct drm_device *dev = dev_priv->dev; |
| 387 | |
| 388 | switch (dev_priv->gart_info.type) { |
Ben Skeggs | b694dfb | 2009-12-15 10:38:32 +1000 | [diff] [blame] | 389 | #if __OS_HAS_AGP |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 390 | case NOUVEAU_GART_AGP: |
| 391 | return ttm_agp_backend_init(bdev, dev->agp->bridge); |
Ben Skeggs | b694dfb | 2009-12-15 10:38:32 +1000 | [diff] [blame] | 392 | #endif |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 393 | case NOUVEAU_GART_SGDMA: |
| 394 | return nouveau_sgdma_init_ttm(dev); |
| 395 | default: |
| 396 | NV_ERROR(dev, "Unknown GART type %d\n", |
| 397 | dev_priv->gart_info.type); |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | static int |
| 405 | nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) |
| 406 | { |
| 407 | /* We'll do this from user space. */ |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int |
| 412 | nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, |
| 413 | struct ttm_mem_type_manager *man) |
| 414 | { |
| 415 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev); |
| 416 | struct drm_device *dev = dev_priv->dev; |
| 417 | |
| 418 | switch (type) { |
| 419 | case TTM_PL_SYSTEM: |
| 420 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
| 421 | man->available_caching = TTM_PL_MASK_CACHING; |
| 422 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 423 | break; |
| 424 | case TTM_PL_VRAM: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 425 | man->func = &ttm_bo_manager_func; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 426 | man->flags = TTM_MEMTYPE_FLAG_FIXED | |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 427 | TTM_MEMTYPE_FLAG_MAPPABLE; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 428 | man->available_caching = TTM_PL_FLAG_UNCACHED | |
| 429 | TTM_PL_FLAG_WC; |
| 430 | man->default_caching = TTM_PL_FLAG_WC; |
Ben Skeggs | fbd2895 | 2010-09-01 15:24:34 +1000 | [diff] [blame] | 431 | if (dev_priv->card_type == NV_50) |
| 432 | man->gpu_offset = 0x40000000; |
| 433 | else |
| 434 | man->gpu_offset = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 435 | break; |
| 436 | case TTM_PL_TT: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 437 | man->func = &ttm_bo_manager_func; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 438 | switch (dev_priv->gart_info.type) { |
| 439 | case NOUVEAU_GART_AGP: |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 440 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
Francisco Jerez | a3d487e | 2010-11-20 22:11:22 +0100 | [diff] [blame] | 441 | man->available_caching = TTM_PL_FLAG_UNCACHED | |
| 442 | TTM_PL_FLAG_WC; |
| 443 | man->default_caching = TTM_PL_FLAG_WC; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 444 | break; |
| 445 | case NOUVEAU_GART_SGDMA: |
| 446 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | |
| 447 | TTM_MEMTYPE_FLAG_CMA; |
| 448 | man->available_caching = TTM_PL_MASK_CACHING; |
| 449 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 450 | break; |
| 451 | default: |
| 452 | NV_ERROR(dev, "Unknown GART type: %d\n", |
| 453 | dev_priv->gart_info.type); |
| 454 | return -EINVAL; |
| 455 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 456 | man->gpu_offset = dev_priv->vm_gart_base; |
| 457 | break; |
| 458 | default: |
| 459 | NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type); |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl) |
| 467 | { |
| 468 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 469 | |
| 470 | switch (bo->mem.mem_type) { |
Francisco Jerez | 22fbd53 | 2009-12-11 18:40:17 +0100 | [diff] [blame] | 471 | case TTM_PL_VRAM: |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 472 | nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT, |
| 473 | TTM_PL_FLAG_SYSTEM); |
Francisco Jerez | 22fbd53 | 2009-12-11 18:40:17 +0100 | [diff] [blame] | 474 | break; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 475 | default: |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 476 | nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 477 | break; |
| 478 | } |
Francisco Jerez | 22fbd53 | 2009-12-11 18:40:17 +0100 | [diff] [blame] | 479 | |
| 480 | *pl = nvbo->placement; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access |
| 485 | * TTM_PL_{VRAM,TT} directly. |
| 486 | */ |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 487 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 488 | static int |
| 489 | nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 490 | struct nouveau_bo *nvbo, bool evict, |
| 491 | bool no_wait_reserve, bool no_wait_gpu, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 492 | struct ttm_mem_reg *new_mem) |
| 493 | { |
| 494 | struct nouveau_fence *fence = NULL; |
| 495 | int ret; |
| 496 | |
| 497 | ret = nouveau_fence_new(chan, &fence, true); |
| 498 | if (ret) |
| 499 | return ret; |
| 500 | |
Francisco Jerez | 6479881 | 2010-09-21 19:02:01 +0200 | [diff] [blame] | 501 | ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict, |
Francisco Jerez | 311ab69 | 2010-07-04 12:54:23 +0200 | [diff] [blame] | 502 | no_wait_reserve, no_wait_gpu, new_mem); |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 503 | nouveau_fence_unref(&fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | static inline uint32_t |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 508 | nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo, |
| 509 | struct nouveau_channel *chan, struct ttm_mem_reg *mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 510 | { |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 511 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 512 | |
| 513 | if (nvbo->no_vm) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 514 | if (mem->mem_type == TTM_PL_TT) |
| 515 | return NvDmaGART; |
| 516 | return NvDmaVRAM; |
| 517 | } |
| 518 | |
| 519 | if (mem->mem_type == TTM_PL_TT) |
| 520 | return chan->gart_handle; |
| 521 | return chan->vram_handle; |
| 522 | } |
| 523 | |
| 524 | static int |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 525 | nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 526 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 527 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 528 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 529 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 530 | u64 length = (new_mem->num_pages << PAGE_SHIFT); |
| 531 | u64 src_offset, dst_offset; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 532 | int ret; |
| 533 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 534 | src_offset = old_mem->start << PAGE_SHIFT; |
| 535 | dst_offset = new_mem->start << PAGE_SHIFT; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 536 | if (!nvbo->no_vm) { |
| 537 | if (old_mem->mem_type == TTM_PL_VRAM) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 538 | src_offset += dev_priv->vm_vram_base; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 539 | else |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 540 | src_offset += dev_priv->vm_gart_base; |
| 541 | |
| 542 | if (new_mem->mem_type == TTM_PL_VRAM) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 543 | dst_offset += dev_priv->vm_vram_base; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 544 | else |
| 545 | dst_offset += dev_priv->vm_gart_base; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | ret = RING_SPACE(chan, 3); |
| 549 | if (ret) |
| 550 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 551 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 552 | BEGIN_RING(chan, NvSubM2MF, 0x0184, 2); |
| 553 | OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem)); |
| 554 | OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem)); |
| 555 | |
| 556 | while (length) { |
| 557 | u32 amount, stride, height; |
| 558 | |
Ben Skeggs | 5220b3c | 2010-09-23 15:21:17 +1000 | [diff] [blame] | 559 | amount = min(length, (u64)(4 * 1024 * 1024)); |
| 560 | stride = 16 * 4; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 561 | height = amount / stride; |
| 562 | |
Francisco Jerez | f13b326 | 2010-10-10 06:01:08 +0200 | [diff] [blame] | 563 | if (new_mem->mem_type == TTM_PL_VRAM && |
| 564 | nouveau_bo_tile_layout(nvbo)) { |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 565 | ret = RING_SPACE(chan, 8); |
| 566 | if (ret) |
| 567 | return ret; |
| 568 | |
| 569 | BEGIN_RING(chan, NvSubM2MF, 0x0200, 7); |
| 570 | OUT_RING (chan, 0); |
Ben Skeggs | 5220b3c | 2010-09-23 15:21:17 +1000 | [diff] [blame] | 571 | OUT_RING (chan, 0); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 572 | OUT_RING (chan, stride); |
| 573 | OUT_RING (chan, height); |
| 574 | OUT_RING (chan, 1); |
| 575 | OUT_RING (chan, 0); |
| 576 | OUT_RING (chan, 0); |
| 577 | } else { |
| 578 | ret = RING_SPACE(chan, 2); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | |
| 582 | BEGIN_RING(chan, NvSubM2MF, 0x0200, 1); |
| 583 | OUT_RING (chan, 1); |
| 584 | } |
Francisco Jerez | f13b326 | 2010-10-10 06:01:08 +0200 | [diff] [blame] | 585 | if (old_mem->mem_type == TTM_PL_VRAM && |
| 586 | nouveau_bo_tile_layout(nvbo)) { |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 587 | ret = RING_SPACE(chan, 8); |
| 588 | if (ret) |
| 589 | return ret; |
| 590 | |
| 591 | BEGIN_RING(chan, NvSubM2MF, 0x021c, 7); |
| 592 | OUT_RING (chan, 0); |
Ben Skeggs | 5220b3c | 2010-09-23 15:21:17 +1000 | [diff] [blame] | 593 | OUT_RING (chan, 0); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 594 | OUT_RING (chan, stride); |
| 595 | OUT_RING (chan, height); |
| 596 | OUT_RING (chan, 1); |
| 597 | OUT_RING (chan, 0); |
| 598 | OUT_RING (chan, 0); |
| 599 | } else { |
| 600 | ret = RING_SPACE(chan, 2); |
| 601 | if (ret) |
| 602 | return ret; |
| 603 | |
| 604 | BEGIN_RING(chan, NvSubM2MF, 0x021c, 1); |
| 605 | OUT_RING (chan, 1); |
| 606 | } |
| 607 | |
| 608 | ret = RING_SPACE(chan, 14); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 609 | if (ret) |
| 610 | return ret; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 611 | |
| 612 | BEGIN_RING(chan, NvSubM2MF, 0x0238, 2); |
| 613 | OUT_RING (chan, upper_32_bits(src_offset)); |
| 614 | OUT_RING (chan, upper_32_bits(dst_offset)); |
| 615 | BEGIN_RING(chan, NvSubM2MF, 0x030c, 8); |
| 616 | OUT_RING (chan, lower_32_bits(src_offset)); |
| 617 | OUT_RING (chan, lower_32_bits(dst_offset)); |
| 618 | OUT_RING (chan, stride); |
| 619 | OUT_RING (chan, stride); |
| 620 | OUT_RING (chan, stride); |
| 621 | OUT_RING (chan, height); |
| 622 | OUT_RING (chan, 0x00000101); |
| 623 | OUT_RING (chan, 0x00000000); |
| 624 | BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1); |
| 625 | OUT_RING (chan, 0); |
| 626 | |
| 627 | length -= amount; |
| 628 | src_offset += amount; |
| 629 | dst_offset += amount; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 630 | } |
| 631 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int |
| 636 | nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 637 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 638 | { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 639 | u32 src_offset = old_mem->start << PAGE_SHIFT; |
| 640 | u32 dst_offset = new_mem->start << PAGE_SHIFT; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 641 | u32 page_count = new_mem->num_pages; |
| 642 | int ret; |
| 643 | |
| 644 | ret = RING_SPACE(chan, 3); |
| 645 | if (ret) |
| 646 | return ret; |
| 647 | |
| 648 | BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2); |
| 649 | OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem)); |
| 650 | OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem)); |
| 651 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 652 | page_count = new_mem->num_pages; |
| 653 | while (page_count) { |
| 654 | int line_count = (page_count > 2047) ? 2047 : page_count; |
| 655 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 656 | ret = RING_SPACE(chan, 11); |
| 657 | if (ret) |
| 658 | return ret; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 659 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 660 | BEGIN_RING(chan, NvSubM2MF, |
| 661 | NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 662 | OUT_RING (chan, src_offset); |
| 663 | OUT_RING (chan, dst_offset); |
| 664 | OUT_RING (chan, PAGE_SIZE); /* src_pitch */ |
| 665 | OUT_RING (chan, PAGE_SIZE); /* dst_pitch */ |
| 666 | OUT_RING (chan, PAGE_SIZE); /* line_length */ |
| 667 | OUT_RING (chan, line_count); |
| 668 | OUT_RING (chan, 0x00000101); |
| 669 | OUT_RING (chan, 0x00000000); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 670 | BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 671 | OUT_RING (chan, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 672 | |
| 673 | page_count -= line_count; |
| 674 | src_offset += (PAGE_SIZE * line_count); |
| 675 | dst_offset += (PAGE_SIZE * line_count); |
| 676 | } |
| 677 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static int |
| 682 | nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr, |
| 683 | bool no_wait_reserve, bool no_wait_gpu, |
| 684 | struct ttm_mem_reg *new_mem) |
| 685 | { |
| 686 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
| 687 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 688 | struct nouveau_channel *chan; |
| 689 | int ret; |
| 690 | |
| 691 | chan = nvbo->channel; |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 692 | if (!chan || nvbo->no_vm) { |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 693 | chan = dev_priv->channel; |
Francisco Jerez | e419cf0 | 2010-10-25 23:38:59 +0200 | [diff] [blame] | 694 | mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX); |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 695 | } |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 696 | |
| 697 | if (dev_priv->card_type < NV_50) |
| 698 | ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem); |
| 699 | else |
| 700 | ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem); |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 701 | if (ret == 0) { |
| 702 | ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict, |
| 703 | no_wait_reserve, |
| 704 | no_wait_gpu, new_mem); |
| 705 | } |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 706 | |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 707 | if (chan == dev_priv->channel) |
| 708 | mutex_unlock(&chan->mutex); |
| 709 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | static int |
| 713 | nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 714 | bool no_wait_reserve, bool no_wait_gpu, |
| 715 | struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 716 | { |
| 717 | u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING; |
| 718 | struct ttm_placement placement; |
| 719 | struct ttm_mem_reg tmp_mem; |
| 720 | int ret; |
| 721 | |
| 722 | placement.fpfn = placement.lpfn = 0; |
| 723 | placement.num_placement = placement.num_busy_placement = 1; |
Francisco Jerez | 77e2b5e | 2009-12-16 19:05:00 +0100 | [diff] [blame] | 724 | placement.placement = placement.busy_placement = &placement_memtype; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 725 | |
| 726 | tmp_mem = *new_mem; |
| 727 | tmp_mem.mm_node = NULL; |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 728 | ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 729 | if (ret) |
| 730 | return ret; |
| 731 | |
| 732 | ret = ttm_tt_bind(bo->ttm, &tmp_mem); |
| 733 | if (ret) |
| 734 | goto out; |
| 735 | |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 736 | ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 737 | if (ret) |
| 738 | goto out; |
| 739 | |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 740 | ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 741 | out: |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 742 | ttm_bo_mem_put(bo, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | static int |
| 747 | nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 748 | bool no_wait_reserve, bool no_wait_gpu, |
| 749 | struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 750 | { |
| 751 | u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING; |
| 752 | struct ttm_placement placement; |
| 753 | struct ttm_mem_reg tmp_mem; |
| 754 | int ret; |
| 755 | |
| 756 | placement.fpfn = placement.lpfn = 0; |
| 757 | placement.num_placement = placement.num_busy_placement = 1; |
Francisco Jerez | 77e2b5e | 2009-12-16 19:05:00 +0100 | [diff] [blame] | 758 | placement.placement = placement.busy_placement = &placement_memtype; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 759 | |
| 760 | tmp_mem = *new_mem; |
| 761 | tmp_mem.mm_node = NULL; |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 762 | ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 763 | if (ret) |
| 764 | return ret; |
| 765 | |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 766 | ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 767 | if (ret) |
| 768 | goto out; |
| 769 | |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 770 | ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 771 | if (ret) |
| 772 | goto out; |
| 773 | |
| 774 | out: |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 775 | ttm_bo_mem_put(bo, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | static int |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 780 | nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem, |
| 781 | struct nouveau_tile_reg **new_tile) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 782 | { |
| 783 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 784 | struct drm_device *dev = dev_priv->dev; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 785 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 786 | uint64_t offset; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 787 | int ret; |
| 788 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 789 | if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) { |
| 790 | /* Nothing to do. */ |
| 791 | *new_tile = NULL; |
| 792 | return 0; |
| 793 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 794 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 795 | offset = new_mem->start << PAGE_SHIFT; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 796 | |
| 797 | if (dev_priv->card_type == NV_50) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 798 | ret = nv50_mem_vm_bind_linear(dev, |
| 799 | offset + dev_priv->vm_vram_base, |
Francisco Jerez | f13b326 | 2010-10-10 06:01:08 +0200 | [diff] [blame] | 800 | new_mem->size, |
| 801 | nouveau_bo_tile_layout(nvbo), |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 802 | offset); |
| 803 | if (ret) |
| 804 | return ret; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 805 | |
| 806 | } else if (dev_priv->card_type >= NV_10) { |
| 807 | *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size, |
Francisco Jerez | a5cf68b | 2010-10-24 16:14:41 +0200 | [diff] [blame] | 808 | nvbo->tile_mode, |
| 809 | nvbo->tile_flags); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 810 | } |
| 811 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 812 | return 0; |
| 813 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 814 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 815 | static void |
| 816 | nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo, |
| 817 | struct nouveau_tile_reg *new_tile, |
| 818 | struct nouveau_tile_reg **old_tile) |
| 819 | { |
| 820 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
| 821 | struct drm_device *dev = dev_priv->dev; |
| 822 | |
| 823 | if (dev_priv->card_type >= NV_10 && |
| 824 | dev_priv->card_type < NV_50) { |
Francisco Jerez | a5cf68b | 2010-10-24 16:14:41 +0200 | [diff] [blame] | 825 | nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 826 | *old_tile = new_tile; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | static int |
| 831 | nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 832 | bool no_wait_reserve, bool no_wait_gpu, |
| 833 | struct ttm_mem_reg *new_mem) |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 834 | { |
| 835 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
| 836 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 837 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 838 | struct nouveau_tile_reg *new_tile = NULL; |
| 839 | int ret = 0; |
| 840 | |
| 841 | ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile); |
| 842 | if (ret) |
| 843 | return ret; |
| 844 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 845 | /* Fake bo copy. */ |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 846 | if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) { |
| 847 | BUG_ON(bo->mem.mm_node != NULL); |
| 848 | bo->mem = *new_mem; |
| 849 | new_mem->mm_node = NULL; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 850 | goto out; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 851 | } |
| 852 | |
Ben Skeggs | b8a6a80 | 2010-08-27 11:55:43 +1000 | [diff] [blame] | 853 | /* Software copy if the card isn't up and running yet. */ |
| 854 | if (!dev_priv->channel) { |
| 855 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem); |
| 856 | goto out; |
| 857 | } |
| 858 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 859 | /* Hardware assisted copy. */ |
| 860 | if (new_mem->mem_type == TTM_PL_SYSTEM) |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 861 | ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 862 | else if (old_mem->mem_type == TTM_PL_SYSTEM) |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 863 | ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 864 | else |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 865 | ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 866 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 867 | if (!ret) |
| 868 | goto out; |
| 869 | |
| 870 | /* Fallback to software copy. */ |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 871 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 872 | |
| 873 | out: |
| 874 | if (ret) |
| 875 | nouveau_bo_vm_cleanup(bo, NULL, &new_tile); |
| 876 | else |
| 877 | nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile); |
| 878 | |
| 879 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static int |
| 883 | nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp) |
| 884 | { |
| 885 | return 0; |
| 886 | } |
| 887 | |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 888 | static int |
| 889 | nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 890 | { |
| 891 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 892 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev); |
| 893 | struct drm_device *dev = dev_priv->dev; |
| 894 | |
| 895 | mem->bus.addr = NULL; |
| 896 | mem->bus.offset = 0; |
| 897 | mem->bus.size = mem->num_pages << PAGE_SHIFT; |
| 898 | mem->bus.base = 0; |
| 899 | mem->bus.is_iomem = false; |
| 900 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 901 | return -EINVAL; |
| 902 | switch (mem->mem_type) { |
| 903 | case TTM_PL_SYSTEM: |
| 904 | /* System memory */ |
| 905 | return 0; |
| 906 | case TTM_PL_TT: |
| 907 | #if __OS_HAS_AGP |
| 908 | if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 909 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 910 | mem->bus.base = dev_priv->gart_info.aper_base; |
| 911 | mem->bus.is_iomem = true; |
| 912 | } |
| 913 | #endif |
| 914 | break; |
| 915 | case TTM_PL_VRAM: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 916 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Jordan Crouse | 01d73a6 | 2010-05-27 13:40:24 -0600 | [diff] [blame] | 917 | mem->bus.base = pci_resource_start(dev->pdev, 1); |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 918 | mem->bus.is_iomem = true; |
| 919 | break; |
| 920 | default: |
| 921 | return -EINVAL; |
| 922 | } |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | static void |
| 927 | nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 928 | { |
| 929 | } |
| 930 | |
| 931 | static int |
| 932 | nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo) |
| 933 | { |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 934 | struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev); |
| 935 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 936 | |
| 937 | /* as long as the bo isn't in vram, and isn't tiled, we've got |
| 938 | * nothing to do here. |
| 939 | */ |
| 940 | if (bo->mem.mem_type != TTM_PL_VRAM) { |
Francisco Jerez | f13b326 | 2010-10-10 06:01:08 +0200 | [diff] [blame] | 941 | if (dev_priv->card_type < NV_50 || |
| 942 | !nouveau_bo_tile_layout(nvbo)) |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | /* make sure bo is in mappable vram */ |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 947 | if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages) |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 948 | return 0; |
| 949 | |
| 950 | |
| 951 | nvbo->placement.fpfn = 0; |
| 952 | nvbo->placement.lpfn = dev_priv->fb_mappable_pages; |
| 953 | nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0); |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame^] | 954 | return nouveau_bo_validate(nvbo, false, true, false); |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Francisco Jerez | 332b242 | 2010-10-20 23:35:40 +0200 | [diff] [blame] | 957 | void |
| 958 | nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence) |
| 959 | { |
Francisco Jerez | 23c45e8 | 2010-10-28 23:10:29 +0200 | [diff] [blame] | 960 | struct nouveau_fence *old_fence; |
Francisco Jerez | 332b242 | 2010-10-20 23:35:40 +0200 | [diff] [blame] | 961 | |
| 962 | if (likely(fence)) |
Francisco Jerez | 23c45e8 | 2010-10-28 23:10:29 +0200 | [diff] [blame] | 963 | nouveau_fence_ref(fence); |
Francisco Jerez | 332b242 | 2010-10-20 23:35:40 +0200 | [diff] [blame] | 964 | |
Francisco Jerez | 23c45e8 | 2010-10-28 23:10:29 +0200 | [diff] [blame] | 965 | spin_lock(&nvbo->bo.bdev->fence_lock); |
| 966 | old_fence = nvbo->bo.sync_obj; |
| 967 | nvbo->bo.sync_obj = fence; |
Francisco Jerez | 332b242 | 2010-10-20 23:35:40 +0200 | [diff] [blame] | 968 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
Francisco Jerez | 23c45e8 | 2010-10-28 23:10:29 +0200 | [diff] [blame] | 969 | |
| 970 | nouveau_fence_unref(&old_fence); |
Francisco Jerez | 332b242 | 2010-10-20 23:35:40 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 973 | struct ttm_bo_driver nouveau_bo_driver = { |
| 974 | .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry, |
| 975 | .invalidate_caches = nouveau_bo_invalidate_caches, |
| 976 | .init_mem_type = nouveau_bo_init_mem_type, |
| 977 | .evict_flags = nouveau_bo_evict_flags, |
| 978 | .move = nouveau_bo_move, |
| 979 | .verify_access = nouveau_bo_verify_access, |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 980 | .sync_obj_signaled = __nouveau_fence_signalled, |
| 981 | .sync_obj_wait = __nouveau_fence_wait, |
| 982 | .sync_obj_flush = __nouveau_fence_flush, |
| 983 | .sync_obj_unref = __nouveau_fence_unref, |
| 984 | .sync_obj_ref = __nouveau_fence_ref, |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 985 | .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify, |
| 986 | .io_mem_reserve = &nouveau_ttm_io_mem_reserve, |
| 987 | .io_mem_free = &nouveau_ttm_io_mem_free, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 988 | }; |
| 989 | |