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 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 30 | #include <core/engine.h> |
Chris Metcalf | 3e2b756 | 2013-02-01 13:44:33 -0500 | [diff] [blame] | 31 | #include <linux/swiotlb.h> |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 32 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 33 | #include <subdev/fb.h> |
| 34 | #include <subdev/vm.h> |
| 35 | #include <subdev/bar.h> |
| 36 | |
| 37 | #include "nouveau_drm.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 38 | #include "nouveau_dma.h" |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 39 | #include "nouveau_fence.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 40 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 41 | #include "nouveau_bo.h" |
| 42 | #include "nouveau_ttm.h" |
| 43 | #include "nouveau_gem.h" |
Maarten Maathuis | a510604 | 2009-12-26 21:46:36 +0100 | [diff] [blame] | 44 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 45 | /* |
| 46 | * NV10-NV40 tiling helpers |
| 47 | */ |
| 48 | |
| 49 | static void |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 50 | nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg, |
| 51 | u32 addr, u32 size, u32 pitch, u32 flags) |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 52 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 53 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 54 | int i = reg - drm->tile.reg; |
| 55 | struct nouveau_fb *pfb = nouveau_fb(drm->device); |
| 56 | struct nouveau_fb_tile *tile = &pfb->tile.region[i]; |
| 57 | struct nouveau_engine *engine; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 58 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 59 | nouveau_fence_unref(®->fence); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 60 | |
| 61 | if (tile->pitch) |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 62 | pfb->tile.fini(pfb, i, tile); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 63 | |
| 64 | if (pitch) |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 65 | pfb->tile.init(pfb, i, addr, size, pitch, flags, tile); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 66 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 67 | pfb->tile.prog(pfb, i, tile); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 68 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 69 | if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_GR))) |
| 70 | engine->tile_prog(engine, i); |
| 71 | if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_MPEG))) |
| 72 | engine->tile_prog(engine, i); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 73 | } |
| 74 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 75 | static struct nouveau_drm_tile * |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 76 | nv10_bo_get_tile_region(struct drm_device *dev, int i) |
| 77 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 78 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 79 | struct nouveau_drm_tile *tile = &drm->tile.reg[i]; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 80 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 81 | spin_lock(&drm->tile.lock); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 82 | |
| 83 | if (!tile->used && |
| 84 | (!tile->fence || nouveau_fence_done(tile->fence))) |
| 85 | tile->used = true; |
| 86 | else |
| 87 | tile = NULL; |
| 88 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 89 | spin_unlock(&drm->tile.lock); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 90 | return tile; |
| 91 | } |
| 92 | |
| 93 | static void |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 94 | nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile, |
| 95 | struct nouveau_fence *fence) |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 96 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 97 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 98 | |
| 99 | if (tile) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 100 | spin_lock(&drm->tile.lock); |
Ben Skeggs | 5d216f6 | 2013-11-13 10:23:46 +1000 | [diff] [blame] | 101 | tile->fence = nouveau_fence_ref(fence); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 102 | tile->used = false; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 103 | spin_unlock(&drm->tile.lock); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 107 | static struct nouveau_drm_tile * |
| 108 | nv10_bo_set_tiling(struct drm_device *dev, u32 addr, |
| 109 | u32 size, u32 pitch, u32 flags) |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 110 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 111 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 112 | struct nouveau_fb *pfb = nouveau_fb(drm->device); |
| 113 | struct nouveau_drm_tile *tile, *found = NULL; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 114 | int i; |
| 115 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 116 | for (i = 0; i < pfb->tile.regions; i++) { |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 117 | tile = nv10_bo_get_tile_region(dev, i); |
| 118 | |
| 119 | if (pitch && !found) { |
| 120 | found = tile; |
| 121 | continue; |
| 122 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 123 | } else if (tile && pfb->tile.region[i].pitch) { |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 124 | /* Kill an unused tile region. */ |
| 125 | nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0); |
| 126 | } |
| 127 | |
| 128 | nv10_bo_put_tile_region(dev, tile, NULL); |
| 129 | } |
| 130 | |
| 131 | if (found) |
| 132 | nv10_bo_update_tile_region(dev, found, addr, size, |
| 133 | pitch, flags); |
| 134 | return found; |
| 135 | } |
| 136 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 137 | static void |
| 138 | nouveau_bo_del_ttm(struct ttm_buffer_object *bo) |
| 139 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 140 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
| 141 | struct drm_device *dev = drm->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 142 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 143 | |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 144 | if (unlikely(nvbo->gem.filp)) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 145 | DRM_ERROR("bo %p still attached to GEM object\n", bo); |
Maarten Lankhorst | 4f38559 | 2013-07-07 10:37:35 +0200 | [diff] [blame] | 146 | WARN_ON(nvbo->pin_refcnt > 0); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 147 | nv10_bo_put_tile_region(dev, nvbo->tile, NULL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 148 | kfree(nvbo); |
| 149 | } |
| 150 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 151 | static void |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 152 | nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags, |
Ben Skeggs | f91bac5 | 2011-06-06 14:15:46 +1000 | [diff] [blame] | 153 | int *align, int *size) |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 154 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 155 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
| 156 | struct nouveau_device *device = nv_device(drm->device); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 157 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 158 | if (device->card_type < NV_50) { |
Ben Skeggs | bfd83ac | 2010-11-12 15:12:51 +1000 | [diff] [blame] | 159 | if (nvbo->tile_mode) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 160 | if (device->chipset >= 0x40) { |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 161 | *align = 65536; |
Ben Skeggs | bfd83ac | 2010-11-12 15:12:51 +1000 | [diff] [blame] | 162 | *size = roundup(*size, 64 * nvbo->tile_mode); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 163 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 164 | } else if (device->chipset >= 0x30) { |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 165 | *align = 32768; |
Ben Skeggs | bfd83ac | 2010-11-12 15:12:51 +1000 | [diff] [blame] | 166 | *size = roundup(*size, 64 * nvbo->tile_mode); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 167 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 168 | } else if (device->chipset >= 0x20) { |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 169 | *align = 16384; |
Ben Skeggs | bfd83ac | 2010-11-12 15:12:51 +1000 | [diff] [blame] | 170 | *size = roundup(*size, 64 * nvbo->tile_mode); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 171 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 172 | } else if (device->chipset >= 0x10) { |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 173 | *align = 16384; |
Ben Skeggs | bfd83ac | 2010-11-12 15:12:51 +1000 | [diff] [blame] | 174 | *size = roundup(*size, 32 * nvbo->tile_mode); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
Ben Skeggs | bfd83ac | 2010-11-12 15:12:51 +1000 | [diff] [blame] | 177 | } else { |
Ben Skeggs | f91bac5 | 2011-06-06 14:15:46 +1000 | [diff] [blame] | 178 | *size = roundup(*size, (1 << nvbo->page_shift)); |
| 179 | *align = max((1 << nvbo->page_shift), *align); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Maarten Maathuis | 1c7059e | 2009-12-25 18:51:17 +0100 | [diff] [blame] | 182 | *size = roundup(*size, PAGE_SIZE); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 185 | int |
Ben Skeggs | 7375c95 | 2011-06-07 14:21:29 +1000 | [diff] [blame] | 186 | nouveau_bo_new(struct drm_device *dev, int size, int align, |
| 187 | uint32_t flags, uint32_t tile_mode, uint32_t tile_flags, |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 188 | struct sg_table *sg, |
Ben Skeggs | 7375c95 | 2011-06-07 14:21:29 +1000 | [diff] [blame] | 189 | struct nouveau_bo **pnvbo) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 190 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 191 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 192 | struct nouveau_bo *nvbo; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 193 | size_t acc_size; |
Ben Skeggs | f91bac5 | 2011-06-06 14:15:46 +1000 | [diff] [blame] | 194 | int ret; |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 195 | int type = ttm_bo_type_device; |
Maarten Lankhorst | 35095f7 | 2013-07-27 10:17:12 +0200 | [diff] [blame] | 196 | int lpg_shift = 12; |
| 197 | int max_size; |
| 198 | |
| 199 | if (drm->client.base.vm) |
| 200 | lpg_shift = drm->client.base.vm->vmm->lpg_shift; |
| 201 | max_size = INT_MAX & ~((1 << lpg_shift) - 1); |
Maarten Lankhorst | 0108bc8 | 2013-07-07 10:40:19 +0200 | [diff] [blame] | 202 | |
| 203 | if (size <= 0 || size > max_size) { |
| 204 | nv_warn(drm, "skipped size %x\n", (u32)size); |
| 205 | return -EINVAL; |
| 206 | } |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 207 | |
| 208 | if (sg) |
| 209 | type = ttm_bo_type_sg; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 210 | |
| 211 | nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL); |
| 212 | if (!nvbo) |
| 213 | return -ENOMEM; |
| 214 | INIT_LIST_HEAD(&nvbo->head); |
| 215 | INIT_LIST_HEAD(&nvbo->entry); |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 216 | INIT_LIST_HEAD(&nvbo->vma_list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 217 | nvbo->tile_mode = tile_mode; |
| 218 | nvbo->tile_flags = tile_flags; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 219 | nvbo->bo.bdev = &drm->ttm.bdev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 220 | |
Ben Skeggs | f91bac5 | 2011-06-06 14:15:46 +1000 | [diff] [blame] | 221 | nvbo->page_shift = 12; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 222 | if (drm->client.base.vm) { |
Ben Skeggs | f91bac5 | 2011-06-06 14:15:46 +1000 | [diff] [blame] | 223 | if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024) |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 224 | nvbo->page_shift = drm->client.base.vm->vmm->lpg_shift; |
Ben Skeggs | f91bac5 | 2011-06-06 14:15:46 +1000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | nouveau_bo_fixup_align(nvbo, flags, &align, &size); |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 228 | nvbo->bo.mem.num_pages = size >> PAGE_SHIFT; |
| 229 | nouveau_bo_placement_set(nvbo, flags, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 230 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 231 | acc_size = ttm_bo_dma_acc_size(&drm->ttm.bdev, size, |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 232 | sizeof(struct nouveau_bo)); |
| 233 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 234 | ret = ttm_bo_init(&drm->ttm.bdev, &nvbo->bo, size, |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 235 | type, &nvbo->placement, |
Marcin Slusarz | 0b91c4a | 2012-11-06 21:49:51 +0000 | [diff] [blame] | 236 | align >> PAGE_SHIFT, false, NULL, acc_size, sg, |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 237 | nouveau_bo_del_ttm); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 238 | if (ret) { |
| 239 | /* ttm will call nouveau_bo_del_ttm if it fails.. */ |
| 240 | return ret; |
| 241 | } |
| 242 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 243 | *pnvbo = nvbo; |
| 244 | return 0; |
| 245 | } |
| 246 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 247 | static void |
| 248 | 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] | 249 | { |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 250 | *n = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 251 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 252 | if (type & TTM_PL_FLAG_VRAM) |
| 253 | pl[(*n)++] = TTM_PL_FLAG_VRAM | flags; |
| 254 | if (type & TTM_PL_FLAG_TT) |
| 255 | pl[(*n)++] = TTM_PL_FLAG_TT | flags; |
| 256 | if (type & TTM_PL_FLAG_SYSTEM) |
| 257 | pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags; |
| 258 | } |
Ben Skeggs | 37cb3e08 | 2009-12-16 16:22:42 +1000 | [diff] [blame] | 259 | |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 260 | static void |
| 261 | set_placement_range(struct nouveau_bo *nvbo, uint32_t type) |
| 262 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 263 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
| 264 | struct nouveau_fb *pfb = nouveau_fb(drm->device); |
Ben Skeggs | dceef5d | 2013-03-04 13:01:21 +1000 | [diff] [blame] | 265 | u32 vram_pages = pfb->ram->size >> PAGE_SHIFT; |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 266 | |
Ilia Mirkin | 4a0ff75 | 2013-09-05 04:45:02 -0400 | [diff] [blame] | 267 | if ((nv_device(drm->device)->card_type == NV_10 || |
| 268 | nv_device(drm->device)->card_type == NV_11) && |
Francisco Jerez | 812f219 | 2011-02-03 01:49:33 +0100 | [diff] [blame] | 269 | nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) && |
Francisco Jerez | 4beb116 | 2011-11-06 21:21:28 +0100 | [diff] [blame] | 270 | nvbo->bo.mem.num_pages < vram_pages / 4) { |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 271 | /* |
| 272 | * Make sure that the color and depth buffers are handled |
| 273 | * by independent memory controller units. Up to a 9x |
| 274 | * speed up when alpha-blending and depth-test are enabled |
| 275 | * at the same time. |
| 276 | */ |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 277 | if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) { |
| 278 | nvbo->placement.fpfn = vram_pages / 2; |
| 279 | nvbo->placement.lpfn = ~0; |
| 280 | } else { |
| 281 | nvbo->placement.fpfn = 0; |
| 282 | nvbo->placement.lpfn = vram_pages / 2; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 287 | void |
| 288 | nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy) |
| 289 | { |
| 290 | struct ttm_placement *pl = &nvbo->placement; |
| 291 | uint32_t flags = TTM_PL_MASK_CACHING | |
| 292 | (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0); |
| 293 | |
| 294 | pl->placement = nvbo->placements; |
| 295 | set_placement_list(nvbo->placements, &pl->num_placement, |
| 296 | type, flags); |
| 297 | |
| 298 | pl->busy_placement = nvbo->busy_placements; |
| 299 | set_placement_list(nvbo->busy_placements, &pl->num_busy_placement, |
| 300 | type | busy, flags); |
Francisco Jerez | 699ddfd | 2010-10-10 06:07:32 +0200 | [diff] [blame] | 301 | |
| 302 | set_placement_range(nvbo, type); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | int |
| 306 | nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype) |
| 307 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 308 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 309 | struct ttm_buffer_object *bo = &nvbo->bo; |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 310 | int ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 311 | |
Daniel Vetter | 0ae6d7b | 2012-12-11 21:52:30 +0100 | [diff] [blame] | 312 | ret = ttm_bo_reserve(bo, false, false, false, 0); |
| 313 | if (ret) |
| 314 | goto out; |
| 315 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 316 | if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 317 | NV_ERROR(drm, "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 318 | 1 << bo->mem.mem_type, memtype); |
Daniel Vetter | 0ae6d7b | 2012-12-11 21:52:30 +0100 | [diff] [blame] | 319 | ret = -EINVAL; |
| 320 | goto out; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (nvbo->pin_refcnt++) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 324 | goto out; |
| 325 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 326 | nouveau_bo_placement_set(nvbo, memtype, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 327 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 328 | ret = nouveau_bo_validate(nvbo, false, false); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 329 | if (ret == 0) { |
| 330 | switch (bo->mem.mem_type) { |
| 331 | case TTM_PL_VRAM: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 332 | drm->gem.vram_available -= bo->mem.size; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 333 | break; |
| 334 | case TTM_PL_TT: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 335 | drm->gem.gart_available -= bo->mem.size; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 336 | break; |
| 337 | default: |
| 338 | break; |
| 339 | } |
| 340 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 341 | out: |
Daniel Vetter | 0ae6d7b | 2012-12-11 21:52:30 +0100 | [diff] [blame] | 342 | ttm_bo_unreserve(bo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | int |
| 347 | nouveau_bo_unpin(struct nouveau_bo *nvbo) |
| 348 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 349 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 350 | struct ttm_buffer_object *bo = &nvbo->bo; |
Maarten Lankhorst | 4f38559 | 2013-07-07 10:37:35 +0200 | [diff] [blame] | 351 | int ret, ref; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 352 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 353 | ret = ttm_bo_reserve(bo, false, false, false, 0); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | |
Maarten Lankhorst | 4f38559 | 2013-07-07 10:37:35 +0200 | [diff] [blame] | 357 | ref = --nvbo->pin_refcnt; |
| 358 | WARN_ON_ONCE(ref < 0); |
| 359 | if (ref) |
Daniel Vetter | 0ae6d7b | 2012-12-11 21:52:30 +0100 | [diff] [blame] | 360 | goto out; |
| 361 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 362 | nouveau_bo_placement_set(nvbo, bo->mem.placement, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 363 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 364 | ret = nouveau_bo_validate(nvbo, false, false); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 365 | if (ret == 0) { |
| 366 | switch (bo->mem.mem_type) { |
| 367 | case TTM_PL_VRAM: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 368 | drm->gem.vram_available += bo->mem.size; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 369 | break; |
| 370 | case TTM_PL_TT: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 371 | drm->gem.gart_available += bo->mem.size; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 372 | break; |
| 373 | default: |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
Daniel Vetter | 0ae6d7b | 2012-12-11 21:52:30 +0100 | [diff] [blame] | 378 | out: |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 379 | ttm_bo_unreserve(bo); |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | int |
| 384 | nouveau_bo_map(struct nouveau_bo *nvbo) |
| 385 | { |
| 386 | int ret; |
| 387 | |
| 388 | ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0); |
| 389 | if (ret) |
| 390 | return ret; |
| 391 | |
| 392 | ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap); |
| 393 | ttm_bo_unreserve(&nvbo->bo); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | nouveau_bo_unmap(struct nouveau_bo *nvbo) |
| 399 | { |
Ben Skeggs | 9d59e8a | 2010-08-27 13:04:41 +1000 | [diff] [blame] | 400 | if (nvbo) |
| 401 | ttm_bo_kunmap(&nvbo->kmap); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 402 | } |
| 403 | |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame] | 404 | int |
| 405 | nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 406 | bool no_wait_gpu) |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame] | 407 | { |
| 408 | int ret; |
| 409 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 410 | ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, |
| 411 | interruptible, no_wait_gpu); |
Ben Skeggs | 7a45d76 | 2010-11-22 08:50:27 +1000 | [diff] [blame] | 412 | if (ret) |
| 413 | return ret; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 418 | u16 |
| 419 | nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index) |
| 420 | { |
| 421 | bool is_iomem; |
| 422 | u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 423 | mem = &mem[index]; |
| 424 | if (is_iomem) |
| 425 | return ioread16_native((void __force __iomem *)mem); |
| 426 | else |
| 427 | return *mem; |
| 428 | } |
| 429 | |
| 430 | void |
| 431 | nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) |
| 432 | { |
| 433 | bool is_iomem; |
| 434 | u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 435 | mem = &mem[index]; |
| 436 | if (is_iomem) |
| 437 | iowrite16_native(val, (void __force __iomem *)mem); |
| 438 | else |
| 439 | *mem = val; |
| 440 | } |
| 441 | |
| 442 | u32 |
| 443 | nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index) |
| 444 | { |
| 445 | bool is_iomem; |
| 446 | u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 447 | mem = &mem[index]; |
| 448 | if (is_iomem) |
| 449 | return ioread32_native((void __force __iomem *)mem); |
| 450 | else |
| 451 | return *mem; |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) |
| 456 | { |
| 457 | bool is_iomem; |
| 458 | u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); |
| 459 | mem = &mem[index]; |
| 460 | if (is_iomem) |
| 461 | iowrite32_native(val, (void __force __iomem *)mem); |
| 462 | else |
| 463 | *mem = val; |
| 464 | } |
| 465 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 466 | static struct ttm_tt * |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 467 | nouveau_ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size, |
| 468 | uint32_t page_flags, struct page *dummy_read) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 469 | { |
Max Filippov | df1b4b9 | 2012-10-14 01:58:26 +0400 | [diff] [blame] | 470 | #if __OS_HAS_AGP |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 471 | struct nouveau_drm *drm = nouveau_bdev(bdev); |
| 472 | struct drm_device *dev = drm->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 473 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 474 | if (drm->agp.stat == ENABLED) { |
| 475 | return ttm_agp_tt_create(bdev, dev->agp->bridge, size, |
| 476 | page_flags, dummy_read); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 477 | } |
Max Filippov | df1b4b9 | 2012-10-14 01:58:26 +0400 | [diff] [blame] | 478 | #endif |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 479 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 480 | return nouveau_sgdma_create_ttm(bdev, size, page_flags, dummy_read); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int |
| 484 | nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) |
| 485 | { |
| 486 | /* We'll do this from user space. */ |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int |
| 491 | nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, |
| 492 | struct ttm_mem_type_manager *man) |
| 493 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 494 | struct nouveau_drm *drm = nouveau_bdev(bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 495 | |
| 496 | switch (type) { |
| 497 | case TTM_PL_SYSTEM: |
| 498 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
| 499 | man->available_caching = TTM_PL_MASK_CACHING; |
| 500 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 501 | break; |
| 502 | case TTM_PL_VRAM: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 503 | if (nv_device(drm->device)->card_type >= NV_50) { |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 504 | man->func = &nouveau_vram_manager; |
Ben Skeggs | f869ef8 | 2010-11-15 11:53:16 +1000 | [diff] [blame] | 505 | man->io_reserve_fastpath = false; |
| 506 | man->use_io_reserve_lru = true; |
| 507 | } else { |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 508 | man->func = &ttm_bo_manager_func; |
Ben Skeggs | f869ef8 | 2010-11-15 11:53:16 +1000 | [diff] [blame] | 509 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 510 | man->flags = TTM_MEMTYPE_FLAG_FIXED | |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 511 | TTM_MEMTYPE_FLAG_MAPPABLE; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 512 | man->available_caching = TTM_PL_FLAG_UNCACHED | |
| 513 | TTM_PL_FLAG_WC; |
| 514 | man->default_caching = TTM_PL_FLAG_WC; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 515 | break; |
| 516 | case TTM_PL_TT: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 517 | if (nv_device(drm->device)->card_type >= NV_50) |
Ben Skeggs | 26c0c9e | 2011-02-10 12:59:51 +1000 | [diff] [blame] | 518 | man->func = &nouveau_gart_manager; |
| 519 | else |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 520 | if (drm->agp.stat != ENABLED) |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 521 | man->func = &nv04_gart_manager; |
| 522 | else |
Ben Skeggs | 26c0c9e | 2011-02-10 12:59:51 +1000 | [diff] [blame] | 523 | man->func = &ttm_bo_manager_func; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 524 | |
| 525 | if (drm->agp.stat == ENABLED) { |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 526 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
Francisco Jerez | a3d487e | 2010-11-20 22:11:22 +0100 | [diff] [blame] | 527 | man->available_caching = TTM_PL_FLAG_UNCACHED | |
| 528 | TTM_PL_FLAG_WC; |
| 529 | man->default_caching = TTM_PL_FLAG_WC; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 530 | } else { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 531 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | |
| 532 | TTM_MEMTYPE_FLAG_CMA; |
| 533 | man->available_caching = TTM_PL_MASK_CACHING; |
| 534 | man->default_caching = TTM_PL_FLAG_CACHED; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 535 | } |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 536 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 537 | break; |
| 538 | default: |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 539 | return -EINVAL; |
| 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static void |
| 545 | nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl) |
| 546 | { |
| 547 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 548 | |
| 549 | switch (bo->mem.mem_type) { |
Francisco Jerez | 22fbd53 | 2009-12-11 18:40:17 +0100 | [diff] [blame] | 550 | case TTM_PL_VRAM: |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 551 | nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT, |
| 552 | TTM_PL_FLAG_SYSTEM); |
Francisco Jerez | 22fbd53 | 2009-12-11 18:40:17 +0100 | [diff] [blame] | 553 | break; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 554 | default: |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 555 | nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 556 | break; |
| 557 | } |
Francisco Jerez | 22fbd53 | 2009-12-11 18:40:17 +0100 | [diff] [blame] | 558 | |
| 559 | *pl = nvbo->placement; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
| 563 | /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access |
| 564 | * TTM_PL_{VRAM,TT} directly. |
| 565 | */ |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 566 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 567 | static int |
| 568 | nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 569 | struct nouveau_bo *nvbo, bool evict, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 570 | bool no_wait_gpu, struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 571 | { |
| 572 | struct nouveau_fence *fence = NULL; |
| 573 | int ret; |
| 574 | |
Ben Skeggs | 264ce19 | 2013-02-14 13:43:21 +1000 | [diff] [blame] | 575 | ret = nouveau_fence_new(chan, false, &fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 576 | if (ret) |
| 577 | return ret; |
| 578 | |
Maarten Lankhorst | b03640b | 2012-10-12 15:03:11 +0000 | [diff] [blame] | 579 | ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, evict, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 580 | no_wait_gpu, new_mem); |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 581 | nouveau_fence_unref(&fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 582 | return ret; |
| 583 | } |
| 584 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 585 | static int |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 586 | nve0_bo_move_init(struct nouveau_channel *chan, u32 handle) |
| 587 | { |
| 588 | int ret = RING_SPACE(chan, 2); |
| 589 | if (ret == 0) { |
| 590 | BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1); |
Ben Skeggs | 00fc6f6 | 2013-07-09 14:20:15 +1000 | [diff] [blame] | 591 | OUT_RING (chan, handle & 0x0000ffff); |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 592 | FIRE_RING (chan); |
| 593 | } |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | static int |
Ben Skeggs | c6b7e89 | 2012-03-20 14:36:04 +1000 | [diff] [blame] | 598 | nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 599 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 600 | { |
| 601 | struct nouveau_mem *node = old_mem->mm_node; |
| 602 | int ret = RING_SPACE(chan, 10); |
| 603 | if (ret == 0) { |
Ben Skeggs | 6d59702 | 2012-04-01 21:09:13 +1000 | [diff] [blame] | 604 | BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8); |
Ben Skeggs | c6b7e89 | 2012-03-20 14:36:04 +1000 | [diff] [blame] | 605 | OUT_RING (chan, upper_32_bits(node->vma[0].offset)); |
| 606 | OUT_RING (chan, lower_32_bits(node->vma[0].offset)); |
| 607 | OUT_RING (chan, upper_32_bits(node->vma[1].offset)); |
| 608 | OUT_RING (chan, lower_32_bits(node->vma[1].offset)); |
| 609 | OUT_RING (chan, PAGE_SIZE); |
| 610 | OUT_RING (chan, PAGE_SIZE); |
| 611 | OUT_RING (chan, PAGE_SIZE); |
| 612 | OUT_RING (chan, new_mem->num_pages); |
Ben Skeggs | 6d59702 | 2012-04-01 21:09:13 +1000 | [diff] [blame] | 613 | BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386); |
Ben Skeggs | c6b7e89 | 2012-03-20 14:36:04 +1000 | [diff] [blame] | 614 | } |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static int |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 619 | nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle) |
| 620 | { |
| 621 | int ret = RING_SPACE(chan, 2); |
| 622 | if (ret == 0) { |
| 623 | BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1); |
| 624 | OUT_RING (chan, handle); |
| 625 | } |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | static int |
Ben Skeggs | 1a46098 | 2012-05-04 15:17:28 +1000 | [diff] [blame] | 630 | nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 631 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 632 | { |
| 633 | struct nouveau_mem *node = old_mem->mm_node; |
| 634 | u64 src_offset = node->vma[0].offset; |
| 635 | u64 dst_offset = node->vma[1].offset; |
| 636 | u32 page_count = new_mem->num_pages; |
| 637 | int ret; |
| 638 | |
| 639 | page_count = new_mem->num_pages; |
| 640 | while (page_count) { |
| 641 | int line_count = (page_count > 8191) ? 8191 : page_count; |
| 642 | |
| 643 | ret = RING_SPACE(chan, 11); |
| 644 | if (ret) |
| 645 | return ret; |
| 646 | |
| 647 | BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8); |
| 648 | OUT_RING (chan, upper_32_bits(src_offset)); |
| 649 | OUT_RING (chan, lower_32_bits(src_offset)); |
| 650 | OUT_RING (chan, upper_32_bits(dst_offset)); |
| 651 | OUT_RING (chan, lower_32_bits(dst_offset)); |
| 652 | OUT_RING (chan, PAGE_SIZE); |
| 653 | OUT_RING (chan, PAGE_SIZE); |
| 654 | OUT_RING (chan, PAGE_SIZE); |
| 655 | OUT_RING (chan, line_count); |
| 656 | BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1); |
| 657 | OUT_RING (chan, 0x00000110); |
| 658 | |
| 659 | page_count -= line_count; |
| 660 | src_offset += (PAGE_SIZE * line_count); |
| 661 | dst_offset += (PAGE_SIZE * line_count); |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int |
Ben Skeggs | 183720b | 2010-12-09 15:17:10 +1000 | [diff] [blame] | 668 | nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 669 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 670 | { |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 671 | struct nouveau_mem *node = old_mem->mm_node; |
| 672 | u64 src_offset = node->vma[0].offset; |
| 673 | u64 dst_offset = node->vma[1].offset; |
Ben Skeggs | 183720b | 2010-12-09 15:17:10 +1000 | [diff] [blame] | 674 | u32 page_count = new_mem->num_pages; |
| 675 | int ret; |
| 676 | |
Ben Skeggs | 183720b | 2010-12-09 15:17:10 +1000 | [diff] [blame] | 677 | page_count = new_mem->num_pages; |
| 678 | while (page_count) { |
| 679 | int line_count = (page_count > 2047) ? 2047 : page_count; |
| 680 | |
| 681 | ret = RING_SPACE(chan, 12); |
| 682 | if (ret) |
| 683 | return ret; |
| 684 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 685 | BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2); |
Ben Skeggs | 183720b | 2010-12-09 15:17:10 +1000 | [diff] [blame] | 686 | OUT_RING (chan, upper_32_bits(dst_offset)); |
| 687 | OUT_RING (chan, lower_32_bits(dst_offset)); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 688 | BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6); |
Ben Skeggs | 183720b | 2010-12-09 15:17:10 +1000 | [diff] [blame] | 689 | OUT_RING (chan, upper_32_bits(src_offset)); |
| 690 | OUT_RING (chan, lower_32_bits(src_offset)); |
| 691 | OUT_RING (chan, PAGE_SIZE); /* src_pitch */ |
| 692 | OUT_RING (chan, PAGE_SIZE); /* dst_pitch */ |
| 693 | OUT_RING (chan, PAGE_SIZE); /* line_length */ |
| 694 | OUT_RING (chan, line_count); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 695 | BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1); |
Ben Skeggs | 183720b | 2010-12-09 15:17:10 +1000 | [diff] [blame] | 696 | OUT_RING (chan, 0x00100110); |
| 697 | |
| 698 | page_count -= line_count; |
| 699 | src_offset += (PAGE_SIZE * line_count); |
| 700 | dst_offset += (PAGE_SIZE * line_count); |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static int |
Ben Skeggs | fdf5324 | 2012-05-04 15:15:12 +1000 | [diff] [blame] | 707 | nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 708 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 709 | { |
| 710 | struct nouveau_mem *node = old_mem->mm_node; |
| 711 | u64 src_offset = node->vma[0].offset; |
| 712 | u64 dst_offset = node->vma[1].offset; |
| 713 | u32 page_count = new_mem->num_pages; |
| 714 | int ret; |
| 715 | |
| 716 | page_count = new_mem->num_pages; |
| 717 | while (page_count) { |
| 718 | int line_count = (page_count > 8191) ? 8191 : page_count; |
| 719 | |
| 720 | ret = RING_SPACE(chan, 11); |
| 721 | if (ret) |
| 722 | return ret; |
| 723 | |
| 724 | BEGIN_NV04(chan, NvSubCopy, 0x030c, 8); |
| 725 | OUT_RING (chan, upper_32_bits(src_offset)); |
| 726 | OUT_RING (chan, lower_32_bits(src_offset)); |
| 727 | OUT_RING (chan, upper_32_bits(dst_offset)); |
| 728 | OUT_RING (chan, lower_32_bits(dst_offset)); |
| 729 | OUT_RING (chan, PAGE_SIZE); |
| 730 | OUT_RING (chan, PAGE_SIZE); |
| 731 | OUT_RING (chan, PAGE_SIZE); |
| 732 | OUT_RING (chan, line_count); |
| 733 | BEGIN_NV04(chan, NvSubCopy, 0x0300, 1); |
| 734 | OUT_RING (chan, 0x00000110); |
| 735 | |
| 736 | page_count -= line_count; |
| 737 | src_offset += (PAGE_SIZE * line_count); |
| 738 | dst_offset += (PAGE_SIZE * line_count); |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int |
Ben Skeggs | 5490e5d | 2012-05-04 14:34:16 +1000 | [diff] [blame] | 745 | nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 746 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 747 | { |
| 748 | struct nouveau_mem *node = old_mem->mm_node; |
| 749 | int ret = RING_SPACE(chan, 7); |
| 750 | if (ret == 0) { |
| 751 | BEGIN_NV04(chan, NvSubCopy, 0x0320, 6); |
| 752 | OUT_RING (chan, upper_32_bits(node->vma[0].offset)); |
| 753 | OUT_RING (chan, lower_32_bits(node->vma[0].offset)); |
| 754 | OUT_RING (chan, upper_32_bits(node->vma[1].offset)); |
| 755 | OUT_RING (chan, lower_32_bits(node->vma[1].offset)); |
| 756 | OUT_RING (chan, 0x00000000 /* COPY */); |
| 757 | OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT); |
| 758 | } |
| 759 | return ret; |
| 760 | } |
| 761 | |
| 762 | static int |
Ben Skeggs | 4c193d2 | 2012-05-04 14:21:15 +1000 | [diff] [blame] | 763 | nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 764 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 765 | { |
| 766 | struct nouveau_mem *node = old_mem->mm_node; |
| 767 | int ret = RING_SPACE(chan, 7); |
| 768 | if (ret == 0) { |
| 769 | BEGIN_NV04(chan, NvSubCopy, 0x0304, 6); |
| 770 | OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT); |
| 771 | OUT_RING (chan, upper_32_bits(node->vma[0].offset)); |
| 772 | OUT_RING (chan, lower_32_bits(node->vma[0].offset)); |
| 773 | OUT_RING (chan, upper_32_bits(node->vma[1].offset)); |
| 774 | OUT_RING (chan, lower_32_bits(node->vma[1].offset)); |
| 775 | OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */); |
| 776 | } |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | static int |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 781 | nv50_bo_move_init(struct nouveau_channel *chan, u32 handle) |
| 782 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 783 | int ret = RING_SPACE(chan, 6); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 784 | if (ret == 0) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 785 | BEGIN_NV04(chan, NvSubCopy, 0x0000, 1); |
| 786 | OUT_RING (chan, handle); |
| 787 | BEGIN_NV04(chan, NvSubCopy, 0x0180, 3); |
| 788 | OUT_RING (chan, NvNotify0); |
| 789 | OUT_RING (chan, NvDmaFB); |
| 790 | OUT_RING (chan, NvDmaFB); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | static int |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 797 | nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 798 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 799 | { |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 800 | struct nouveau_mem *node = old_mem->mm_node; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 801 | u64 length = (new_mem->num_pages << PAGE_SHIFT); |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 802 | u64 src_offset = node->vma[0].offset; |
| 803 | u64 dst_offset = node->vma[1].offset; |
Maarten Lankhorst | ce8f769 | 2013-11-12 13:34:08 +0100 | [diff] [blame] | 804 | int src_tiled = !!node->memtype; |
| 805 | int dst_tiled = !!((struct nouveau_mem *)new_mem->mm_node)->memtype; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 806 | int ret; |
| 807 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 808 | while (length) { |
| 809 | u32 amount, stride, height; |
| 810 | |
Maarten Lankhorst | ce8f769 | 2013-11-12 13:34:08 +0100 | [diff] [blame] | 811 | ret = RING_SPACE(chan, 18 + 6 * (src_tiled + dst_tiled)); |
| 812 | if (ret) |
| 813 | return ret; |
| 814 | |
Ben Skeggs | 5220b3c | 2010-09-23 15:21:17 +1000 | [diff] [blame] | 815 | amount = min(length, (u64)(4 * 1024 * 1024)); |
| 816 | stride = 16 * 4; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 817 | height = amount / stride; |
| 818 | |
Maarten Lankhorst | ce8f769 | 2013-11-12 13:34:08 +0100 | [diff] [blame] | 819 | if (src_tiled) { |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 820 | BEGIN_NV04(chan, NvSubCopy, 0x0200, 7); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 821 | OUT_RING (chan, 0); |
Ben Skeggs | 5220b3c | 2010-09-23 15:21:17 +1000 | [diff] [blame] | 822 | OUT_RING (chan, 0); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 823 | OUT_RING (chan, stride); |
| 824 | OUT_RING (chan, height); |
| 825 | OUT_RING (chan, 1); |
| 826 | OUT_RING (chan, 0); |
| 827 | OUT_RING (chan, 0); |
| 828 | } else { |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 829 | BEGIN_NV04(chan, NvSubCopy, 0x0200, 1); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 830 | OUT_RING (chan, 1); |
| 831 | } |
Maarten Lankhorst | ce8f769 | 2013-11-12 13:34:08 +0100 | [diff] [blame] | 832 | if (dst_tiled) { |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 833 | BEGIN_NV04(chan, NvSubCopy, 0x021c, 7); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 834 | OUT_RING (chan, 0); |
Ben Skeggs | 5220b3c | 2010-09-23 15:21:17 +1000 | [diff] [blame] | 835 | OUT_RING (chan, 0); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 836 | OUT_RING (chan, stride); |
| 837 | OUT_RING (chan, height); |
| 838 | OUT_RING (chan, 1); |
| 839 | OUT_RING (chan, 0); |
| 840 | OUT_RING (chan, 0); |
| 841 | } else { |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 842 | BEGIN_NV04(chan, NvSubCopy, 0x021c, 1); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 843 | OUT_RING (chan, 1); |
| 844 | } |
| 845 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 846 | BEGIN_NV04(chan, NvSubCopy, 0x0238, 2); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 847 | OUT_RING (chan, upper_32_bits(src_offset)); |
| 848 | OUT_RING (chan, upper_32_bits(dst_offset)); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 849 | BEGIN_NV04(chan, NvSubCopy, 0x030c, 8); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 850 | OUT_RING (chan, lower_32_bits(src_offset)); |
| 851 | OUT_RING (chan, lower_32_bits(dst_offset)); |
| 852 | OUT_RING (chan, stride); |
| 853 | OUT_RING (chan, stride); |
| 854 | OUT_RING (chan, stride); |
| 855 | OUT_RING (chan, height); |
| 856 | OUT_RING (chan, 0x00000101); |
| 857 | OUT_RING (chan, 0x00000000); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 858 | BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 859 | OUT_RING (chan, 0); |
| 860 | |
| 861 | length -= amount; |
| 862 | src_offset += amount; |
| 863 | dst_offset += amount; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 864 | } |
| 865 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 866 | return 0; |
| 867 | } |
| 868 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 869 | static int |
| 870 | nv04_bo_move_init(struct nouveau_channel *chan, u32 handle) |
| 871 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 872 | int ret = RING_SPACE(chan, 4); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 873 | if (ret == 0) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 874 | BEGIN_NV04(chan, NvSubCopy, 0x0000, 1); |
| 875 | OUT_RING (chan, handle); |
| 876 | BEGIN_NV04(chan, NvSubCopy, 0x0180, 1); |
| 877 | OUT_RING (chan, NvNotify0); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | return ret; |
| 881 | } |
| 882 | |
Ben Skeggs | a670478 | 2011-02-16 09:10:20 +1000 | [diff] [blame] | 883 | static inline uint32_t |
| 884 | nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo, |
| 885 | struct nouveau_channel *chan, struct ttm_mem_reg *mem) |
| 886 | { |
| 887 | if (mem->mem_type == TTM_PL_TT) |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 888 | return NvDmaTT; |
| 889 | return NvDmaFB; |
Ben Skeggs | a670478 | 2011-02-16 09:10:20 +1000 | [diff] [blame] | 890 | } |
| 891 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 892 | static int |
| 893 | nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, |
| 894 | struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem) |
| 895 | { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 896 | u32 src_offset = old_mem->start << PAGE_SHIFT; |
| 897 | u32 dst_offset = new_mem->start << PAGE_SHIFT; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 898 | u32 page_count = new_mem->num_pages; |
| 899 | int ret; |
| 900 | |
| 901 | ret = RING_SPACE(chan, 3); |
| 902 | if (ret) |
| 903 | return ret; |
| 904 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 905 | BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 906 | OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem)); |
| 907 | OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem)); |
| 908 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 909 | page_count = new_mem->num_pages; |
| 910 | while (page_count) { |
| 911 | int line_count = (page_count > 2047) ? 2047 : page_count; |
| 912 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 913 | ret = RING_SPACE(chan, 11); |
| 914 | if (ret) |
| 915 | return ret; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 916 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 917 | BEGIN_NV04(chan, NvSubCopy, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 918 | NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 919 | OUT_RING (chan, src_offset); |
| 920 | OUT_RING (chan, dst_offset); |
| 921 | OUT_RING (chan, PAGE_SIZE); /* src_pitch */ |
| 922 | OUT_RING (chan, PAGE_SIZE); /* dst_pitch */ |
| 923 | OUT_RING (chan, PAGE_SIZE); /* line_length */ |
| 924 | OUT_RING (chan, line_count); |
| 925 | OUT_RING (chan, 0x00000101); |
| 926 | OUT_RING (chan, 0x00000000); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 927 | BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 928 | OUT_RING (chan, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 929 | |
| 930 | page_count -= line_count; |
| 931 | src_offset += (PAGE_SIZE * line_count); |
| 932 | dst_offset += (PAGE_SIZE * line_count); |
| 933 | } |
| 934 | |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | static int |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 939 | nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo, |
| 940 | struct ttm_mem_reg *mem, struct nouveau_vma *vma) |
| 941 | { |
| 942 | struct nouveau_mem *node = mem->mm_node; |
| 943 | int ret; |
| 944 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 945 | ret = nouveau_vm_get(nv_client(chan->cli)->vm, mem->num_pages << |
| 946 | PAGE_SHIFT, node->page_shift, |
| 947 | NV_MEM_ACCESS_RW, vma); |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 948 | if (ret) |
| 949 | return ret; |
| 950 | |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 951 | nouveau_vm_map(vma, node); |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 956 | nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 957 | bool no_wait_gpu, struct ttm_mem_reg *new_mem) |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 958 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 959 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
Dave Jones | 1934a2a | 2013-09-17 17:26:34 -0400 | [diff] [blame] | 960 | struct nouveau_channel *chan = drm->ttm.chan; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 961 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | 3425df4 | 2011-02-10 11:22:12 +1000 | [diff] [blame] | 962 | struct ttm_mem_reg *old_mem = &bo->mem; |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 963 | int ret; |
| 964 | |
Ben Skeggs | 060810d | 2013-07-08 14:15:51 +1000 | [diff] [blame] | 965 | mutex_lock_nested(&chan->cli->mutex, SINGLE_DEPTH_NESTING); |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 966 | |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 967 | /* create temporary vmas for the transfer and attach them to the |
| 968 | * old nouveau_mem node, these will get cleaned up after ttm has |
| 969 | * destroyed the ttm_mem_reg |
Ben Skeggs | 3425df4 | 2011-02-10 11:22:12 +1000 | [diff] [blame] | 970 | */ |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 971 | if (nv_device(drm->device)->card_type >= NV_50) { |
Ben Skeggs | d5f4239 | 2011-02-10 12:22:52 +1000 | [diff] [blame] | 972 | struct nouveau_mem *node = old_mem->mm_node; |
Ben Skeggs | 3425df4 | 2011-02-10 11:22:12 +1000 | [diff] [blame] | 973 | |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 974 | ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]); |
| 975 | if (ret) |
| 976 | goto out; |
Ben Skeggs | 3425df4 | 2011-02-10 11:22:12 +1000 | [diff] [blame] | 977 | |
Ben Skeggs | d2f96666 | 2011-06-06 20:54:42 +1000 | [diff] [blame] | 978 | ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]); |
| 979 | if (ret) |
| 980 | goto out; |
Ben Skeggs | 3425df4 | 2011-02-10 11:22:12 +1000 | [diff] [blame] | 981 | } |
| 982 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 983 | ret = drm->ttm.move(chan, bo, &bo->mem, new_mem); |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 984 | if (ret == 0) { |
| 985 | ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict, |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 986 | no_wait_gpu, new_mem); |
| 987 | } |
Ben Skeggs | f1ab0cc | 2010-08-26 11:32:01 +1000 | [diff] [blame] | 988 | |
Ben Skeggs | 3425df4 | 2011-02-10 11:22:12 +1000 | [diff] [blame] | 989 | out: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 990 | mutex_unlock(&chan->cli->mutex); |
Ben Skeggs | 6a6b73f | 2010-10-05 16:53:48 +1000 | [diff] [blame] | 991 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 992 | } |
| 993 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 994 | void |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 995 | nouveau_bo_move_init(struct nouveau_drm *drm) |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 996 | { |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 997 | static const struct { |
| 998 | const char *name; |
Ben Skeggs | 1a46098 | 2012-05-04 15:17:28 +1000 | [diff] [blame] | 999 | int engine; |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1000 | u32 oclass; |
| 1001 | int (*exec)(struct nouveau_channel *, |
| 1002 | struct ttm_buffer_object *, |
| 1003 | struct ttm_mem_reg *, struct ttm_mem_reg *); |
| 1004 | int (*init)(struct nouveau_channel *, u32 handle); |
| 1005 | } _methods[] = { |
Ben Skeggs | 00fc6f6 | 2013-07-09 14:20:15 +1000 | [diff] [blame] | 1006 | { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init }, |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 1007 | { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init }, |
Ben Skeggs | 1a46098 | 2012-05-04 15:17:28 +1000 | [diff] [blame] | 1008 | { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init }, |
| 1009 | { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init }, |
| 1010 | { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init }, |
| 1011 | { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init }, |
| 1012 | { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init }, |
| 1013 | { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init }, |
| 1014 | { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init }, |
Ben Skeggs | 5490e5d | 2012-05-04 14:34:16 +1000 | [diff] [blame] | 1015 | {}, |
Ben Skeggs | 1a46098 | 2012-05-04 15:17:28 +1000 | [diff] [blame] | 1016 | { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init }, |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1017 | }, *mthd = _methods; |
| 1018 | const char *name = "CPU"; |
| 1019 | int ret; |
| 1020 | |
| 1021 | do { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1022 | struct nouveau_object *object; |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 1023 | struct nouveau_channel *chan; |
Ben Skeggs | 1a46098 | 2012-05-04 15:17:28 +1000 | [diff] [blame] | 1024 | u32 handle = (mthd->engine << 16) | mthd->oclass; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1025 | |
Ben Skeggs | 00fc6f6 | 2013-07-09 14:20:15 +1000 | [diff] [blame] | 1026 | if (mthd->engine) |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 1027 | chan = drm->cechan; |
| 1028 | else |
| 1029 | chan = drm->channel; |
| 1030 | if (chan == NULL) |
| 1031 | continue; |
| 1032 | |
| 1033 | ret = nouveau_object_new(nv_object(drm), chan->handle, handle, |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1034 | mthd->oclass, NULL, 0, &object); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1035 | if (ret == 0) { |
Ben Skeggs | 1a46098 | 2012-05-04 15:17:28 +1000 | [diff] [blame] | 1036 | ret = mthd->init(chan, handle); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1037 | if (ret) { |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 1038 | nouveau_object_del(nv_object(drm), |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1039 | chan->handle, handle); |
| 1040 | continue; |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1041 | } |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1042 | |
| 1043 | drm->ttm.move = mthd->exec; |
Ben Skeggs | 1bb3f6a | 2013-07-08 10:40:35 +1000 | [diff] [blame] | 1044 | drm->ttm.chan = chan; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1045 | name = mthd->name; |
| 1046 | break; |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1047 | } |
| 1048 | } while ((++mthd)->exec); |
| 1049 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1050 | NV_INFO(drm, "MM: using %s for buffer copies\n", name); |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1051 | } |
| 1052 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1053 | static int |
| 1054 | nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1055 | bool no_wait_gpu, struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1056 | { |
| 1057 | u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING; |
| 1058 | struct ttm_placement placement; |
| 1059 | struct ttm_mem_reg tmp_mem; |
| 1060 | int ret; |
| 1061 | |
| 1062 | placement.fpfn = placement.lpfn = 0; |
| 1063 | placement.num_placement = placement.num_busy_placement = 1; |
Francisco Jerez | 77e2b5e | 2009-12-16 19:05:00 +0100 | [diff] [blame] | 1064 | placement.placement = placement.busy_placement = &placement_memtype; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1065 | |
| 1066 | tmp_mem = *new_mem; |
| 1067 | tmp_mem.mm_node = NULL; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1068 | ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1069 | if (ret) |
| 1070 | return ret; |
| 1071 | |
| 1072 | ret = ttm_tt_bind(bo->ttm, &tmp_mem); |
| 1073 | if (ret) |
| 1074 | goto out; |
| 1075 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1076 | ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1077 | if (ret) |
| 1078 | goto out; |
| 1079 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1080 | ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1081 | out: |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 1082 | ttm_bo_mem_put(bo, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1083 | return ret; |
| 1084 | } |
| 1085 | |
| 1086 | static int |
| 1087 | nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1088 | bool no_wait_gpu, struct ttm_mem_reg *new_mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1089 | { |
| 1090 | u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING; |
| 1091 | struct ttm_placement placement; |
| 1092 | struct ttm_mem_reg tmp_mem; |
| 1093 | int ret; |
| 1094 | |
| 1095 | placement.fpfn = placement.lpfn = 0; |
| 1096 | placement.num_placement = placement.num_busy_placement = 1; |
Francisco Jerez | 77e2b5e | 2009-12-16 19:05:00 +0100 | [diff] [blame] | 1097 | placement.placement = placement.busy_placement = &placement_memtype; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1098 | |
| 1099 | tmp_mem = *new_mem; |
| 1100 | tmp_mem.mm_node = NULL; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1101 | ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1102 | if (ret) |
| 1103 | return ret; |
| 1104 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1105 | ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1106 | if (ret) |
| 1107 | goto out; |
| 1108 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1109 | ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1110 | if (ret) |
| 1111 | goto out; |
| 1112 | |
| 1113 | out: |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 1114 | ttm_bo_mem_put(bo, &tmp_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1115 | return ret; |
| 1116 | } |
| 1117 | |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1118 | static void |
| 1119 | nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem) |
| 1120 | { |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1121 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1122 | struct nouveau_vma *vma; |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1123 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 1124 | /* ttm can now (stupidly) pass the driver bos it didn't create... */ |
| 1125 | if (bo->destroy != nouveau_bo_del_ttm) |
| 1126 | return; |
| 1127 | |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1128 | list_for_each_entry(vma, &nvbo->vma_list, head) { |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 1129 | if (new_mem && new_mem->mem_type != TTM_PL_SYSTEM && |
| 1130 | (new_mem->mem_type == TTM_PL_VRAM || |
| 1131 | nvbo->page_shift != vma->vm->vmm->lpg_shift)) { |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1132 | nouveau_vm_map(vma, new_mem->mm_node); |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1133 | } else { |
| 1134 | nouveau_vm_unmap(vma); |
| 1135 | } |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1139 | static int |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1140 | nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem, |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1141 | struct nouveau_drm_tile **new_tile) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1142 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1143 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
| 1144 | struct drm_device *dev = drm->dev; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1145 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1146 | u64 offset = new_mem->start << PAGE_SHIFT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1147 | |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1148 | *new_tile = NULL; |
| 1149 | if (new_mem->mem_type != TTM_PL_VRAM) |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1150 | return 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1151 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1152 | if (nv_device(drm->device)->card_type >= NV_10) { |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 1153 | *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size, |
Francisco Jerez | a5cf68b | 2010-10-24 16:14:41 +0200 | [diff] [blame] | 1154 | nvbo->tile_mode, |
| 1155 | nvbo->tile_flags); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1156 | } |
| 1157 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1160 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1161 | static void |
| 1162 | nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo, |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1163 | struct nouveau_drm_tile *new_tile, |
| 1164 | struct nouveau_drm_tile **old_tile) |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1165 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1166 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
| 1167 | struct drm_device *dev = drm->dev; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1168 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 1169 | nv10_bo_put_tile_region(dev, *old_tile, bo->sync_obj); |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1170 | *old_tile = new_tile; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static int |
| 1174 | nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1175 | bool no_wait_gpu, struct ttm_mem_reg *new_mem) |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1176 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1177 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1178 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 1179 | struct ttm_mem_reg *old_mem = &bo->mem; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1180 | struct nouveau_drm_tile *new_tile = NULL; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1181 | int ret = 0; |
| 1182 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1183 | if (nv_device(drm->device)->card_type < NV_50) { |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1184 | ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile); |
| 1185 | if (ret) |
| 1186 | return ret; |
| 1187 | } |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1188 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1189 | /* Fake bo copy. */ |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1190 | if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) { |
| 1191 | BUG_ON(bo->mem.mm_node != NULL); |
| 1192 | bo->mem = *new_mem; |
| 1193 | new_mem->mm_node = NULL; |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1194 | goto out; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1195 | } |
| 1196 | |
Ben Skeggs | d1b167e | 2012-05-04 14:01:52 +1000 | [diff] [blame] | 1197 | /* CPU copy if we have no accelerated method available */ |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1198 | if (!drm->ttm.move) { |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1199 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem); |
Ben Skeggs | b8a6a80 | 2010-08-27 11:55:43 +1000 | [diff] [blame] | 1200 | goto out; |
| 1201 | } |
| 1202 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1203 | /* Hardware assisted copy. */ |
| 1204 | if (new_mem->mem_type == TTM_PL_SYSTEM) |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1205 | ret = nouveau_bo_move_flipd(bo, evict, intr, |
| 1206 | no_wait_gpu, new_mem); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1207 | else if (old_mem->mem_type == TTM_PL_SYSTEM) |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1208 | ret = nouveau_bo_move_flips(bo, evict, intr, |
| 1209 | no_wait_gpu, new_mem); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1210 | else |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1211 | ret = nouveau_bo_move_m2mf(bo, evict, intr, |
| 1212 | no_wait_gpu, new_mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1213 | |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1214 | if (!ret) |
| 1215 | goto out; |
| 1216 | |
| 1217 | /* Fallback to software copy. */ |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1218 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem); |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1219 | |
| 1220 | out: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1221 | if (nv_device(drm->device)->card_type < NV_50) { |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1222 | if (ret) |
| 1223 | nouveau_bo_vm_cleanup(bo, NULL, &new_tile); |
| 1224 | else |
| 1225 | nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile); |
| 1226 | } |
Francisco Jerez | a0af9ad | 2009-12-11 16:51:09 +0100 | [diff] [blame] | 1227 | |
| 1228 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | static int |
| 1232 | nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp) |
| 1233 | { |
David Herrmann | acb4652 | 2013-08-25 18:28:59 +0200 | [diff] [blame] | 1234 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
| 1235 | |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 1236 | return drm_vma_node_verify_access(&nvbo->gem.vma_node, filp); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1237 | } |
| 1238 | |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1239 | static int |
| 1240 | nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1241 | { |
| 1242 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1243 | struct nouveau_drm *drm = nouveau_bdev(bdev); |
Maarten Lankhorst | a554090 | 2013-11-12 13:34:09 +0100 | [diff] [blame] | 1244 | struct nouveau_mem *node = mem->mm_node; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1245 | struct drm_device *dev = drm->dev; |
Ben Skeggs | f869ef8 | 2010-11-15 11:53:16 +1000 | [diff] [blame] | 1246 | int ret; |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1247 | |
| 1248 | mem->bus.addr = NULL; |
| 1249 | mem->bus.offset = 0; |
| 1250 | mem->bus.size = mem->num_pages << PAGE_SHIFT; |
| 1251 | mem->bus.base = 0; |
| 1252 | mem->bus.is_iomem = false; |
| 1253 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 1254 | return -EINVAL; |
| 1255 | switch (mem->mem_type) { |
| 1256 | case TTM_PL_SYSTEM: |
| 1257 | /* System memory */ |
| 1258 | return 0; |
| 1259 | case TTM_PL_TT: |
| 1260 | #if __OS_HAS_AGP |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1261 | if (drm->agp.stat == ENABLED) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1262 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1263 | mem->bus.base = drm->agp.base; |
Aaro Koskinen | eda85d6 | 2012-12-31 03:34:59 +0200 | [diff] [blame] | 1264 | mem->bus.is_iomem = !dev->agp->cant_use_aperture; |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1265 | } |
| 1266 | #endif |
Maarten Lankhorst | a554090 | 2013-11-12 13:34:09 +0100 | [diff] [blame] | 1267 | if (!node->memtype) |
| 1268 | /* untiled */ |
| 1269 | break; |
| 1270 | /* fallthrough, tiled memory */ |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1271 | case TTM_PL_VRAM: |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 1272 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Jordan Crouse | 01d73a6 | 2010-05-27 13:40:24 -0600 | [diff] [blame] | 1273 | mem->bus.base = pci_resource_start(dev->pdev, 1); |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1274 | mem->bus.is_iomem = true; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1275 | if (nv_device(drm->device)->card_type >= NV_50) { |
| 1276 | struct nouveau_bar *bar = nouveau_bar(drm->device); |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 1277 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1278 | ret = bar->umap(bar, node, NV_MEM_ACCESS_RW, |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 1279 | &node->bar_vma); |
| 1280 | if (ret) |
| 1281 | return ret; |
| 1282 | |
| 1283 | mem->bus.offset = node->bar_vma.offset; |
| 1284 | } |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1285 | break; |
| 1286 | default: |
| 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | static void |
| 1293 | nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1294 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1295 | struct nouveau_drm *drm = nouveau_bdev(bdev); |
| 1296 | struct nouveau_bar *bar = nouveau_bar(drm->device); |
Ben Skeggs | d5f4239 | 2011-02-10 12:22:52 +1000 | [diff] [blame] | 1297 | struct nouveau_mem *node = mem->mm_node; |
Ben Skeggs | f869ef8 | 2010-11-15 11:53:16 +1000 | [diff] [blame] | 1298 | |
Ben Skeggs | d5f4239 | 2011-02-10 12:22:52 +1000 | [diff] [blame] | 1299 | if (!node->bar_vma.node) |
Ben Skeggs | f869ef8 | 2010-11-15 11:53:16 +1000 | [diff] [blame] | 1300 | return; |
| 1301 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1302 | bar->unmap(bar, &node->bar_vma); |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static int |
| 1306 | nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo) |
| 1307 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1308 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 1309 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1310 | struct nouveau_device *device = nv_device(drm->device); |
| 1311 | u32 mappable = pci_resource_len(device->pdev, 1) >> PAGE_SHIFT; |
Maarten Lankhorst | a554090 | 2013-11-12 13:34:09 +0100 | [diff] [blame] | 1312 | int ret; |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 1313 | |
| 1314 | /* as long as the bo isn't in vram, and isn't tiled, we've got |
| 1315 | * nothing to do here. |
| 1316 | */ |
| 1317 | if (bo->mem.mem_type != TTM_PL_VRAM) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1318 | if (nv_device(drm->device)->card_type < NV_50 || |
Francisco Jerez | f13b326 | 2010-10-10 06:01:08 +0200 | [diff] [blame] | 1319 | !nouveau_bo_tile_layout(nvbo)) |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 1320 | return 0; |
Maarten Lankhorst | a554090 | 2013-11-12 13:34:09 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { |
| 1323 | nouveau_bo_placement_set(nvbo, TTM_PL_TT, 0); |
| 1324 | |
| 1325 | ret = nouveau_bo_validate(nvbo, false, false); |
| 1326 | if (ret) |
| 1327 | return ret; |
| 1328 | } |
| 1329 | return 0; |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* make sure bo is in mappable vram */ |
Maarten Lankhorst | a554090 | 2013-11-12 13:34:09 +0100 | [diff] [blame] | 1333 | if (nv_device(drm->device)->card_type >= NV_50 || |
| 1334 | bo->mem.start + bo->mem.num_pages < mappable) |
Ben Skeggs | e1429b4 | 2010-09-10 11:12:25 +1000 | [diff] [blame] | 1335 | return 0; |
| 1336 | |
| 1337 | |
| 1338 | nvbo->placement.fpfn = 0; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1339 | nvbo->placement.lpfn = mappable; |
Dave Airlie | c284815 | 2012-05-18 15:31:12 +0100 | [diff] [blame] | 1340 | nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0); |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1341 | return nouveau_bo_validate(nvbo, false, false); |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1344 | static int |
| 1345 | nouveau_ttm_tt_populate(struct ttm_tt *ttm) |
| 1346 | { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1347 | struct ttm_dma_tt *ttm_dma = (void *)ttm; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1348 | struct nouveau_drm *drm; |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1349 | struct drm_device *dev; |
| 1350 | unsigned i; |
| 1351 | int r; |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 1352 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1353 | |
| 1354 | if (ttm->state != tt_unpopulated) |
| 1355 | return 0; |
| 1356 | |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 1357 | if (slave && ttm->sg) { |
| 1358 | /* make userspace faulting work */ |
| 1359 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 1360 | ttm_dma->dma_address, ttm->num_pages); |
| 1361 | ttm->state = tt_unbound; |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1365 | drm = nouveau_bdev(ttm->bdev); |
| 1366 | dev = drm->dev; |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1367 | |
Jerome Glisse | dea7e0a | 2012-01-03 17:37:37 -0500 | [diff] [blame] | 1368 | #if __OS_HAS_AGP |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1369 | if (drm->agp.stat == ENABLED) { |
Jerome Glisse | dea7e0a | 2012-01-03 17:37:37 -0500 | [diff] [blame] | 1370 | return ttm_agp_tt_populate(ttm); |
| 1371 | } |
| 1372 | #endif |
| 1373 | |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1374 | #ifdef CONFIG_SWIOTLB |
| 1375 | if (swiotlb_nr_tbl()) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1376 | return ttm_dma_populate((void *)ttm, dev->dev); |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1377 | } |
| 1378 | #endif |
| 1379 | |
| 1380 | r = ttm_pool_populate(ttm); |
| 1381 | if (r) { |
| 1382 | return r; |
| 1383 | } |
| 1384 | |
| 1385 | for (i = 0; i < ttm->num_pages; i++) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1386 | ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i], |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1387 | 0, PAGE_SIZE, |
| 1388 | PCI_DMA_BIDIRECTIONAL); |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1389 | if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) { |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1390 | while (--i) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1391 | pci_unmap_page(dev->pdev, ttm_dma->dma_address[i], |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1392 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1393 | ttm_dma->dma_address[i] = 0; |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1394 | } |
| 1395 | ttm_pool_unpopulate(ttm); |
| 1396 | return -EFAULT; |
| 1397 | } |
| 1398 | } |
| 1399 | return 0; |
| 1400 | } |
| 1401 | |
| 1402 | static void |
| 1403 | nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm) |
| 1404 | { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1405 | struct ttm_dma_tt *ttm_dma = (void *)ttm; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1406 | struct nouveau_drm *drm; |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1407 | struct drm_device *dev; |
| 1408 | unsigned i; |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 1409 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
| 1410 | |
| 1411 | if (slave) |
| 1412 | return; |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1413 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1414 | drm = nouveau_bdev(ttm->bdev); |
| 1415 | dev = drm->dev; |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1416 | |
Jerome Glisse | dea7e0a | 2012-01-03 17:37:37 -0500 | [diff] [blame] | 1417 | #if __OS_HAS_AGP |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 1418 | if (drm->agp.stat == ENABLED) { |
Jerome Glisse | dea7e0a | 2012-01-03 17:37:37 -0500 | [diff] [blame] | 1419 | ttm_agp_tt_unpopulate(ttm); |
| 1420 | return; |
| 1421 | } |
| 1422 | #endif |
| 1423 | |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1424 | #ifdef CONFIG_SWIOTLB |
| 1425 | if (swiotlb_nr_tbl()) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1426 | ttm_dma_unpopulate((void *)ttm, dev->dev); |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1427 | return; |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
| 1431 | for (i = 0; i < ttm->num_pages; i++) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 1432 | if (ttm_dma->dma_address[i]) { |
| 1433 | pci_unmap_page(dev->pdev, ttm_dma->dma_address[i], |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1434 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | ttm_pool_unpopulate(ttm); |
| 1439 | } |
| 1440 | |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1441 | void |
| 1442 | nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence) |
| 1443 | { |
Ben Skeggs | 5d216f6 | 2013-11-13 10:23:46 +1000 | [diff] [blame] | 1444 | struct nouveau_fence *new_fence = nouveau_fence_ref(fence); |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1445 | struct nouveau_fence *old_fence = NULL; |
| 1446 | |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1447 | spin_lock(&nvbo->bo.bdev->fence_lock); |
| 1448 | old_fence = nvbo->bo.sync_obj; |
Ben Skeggs | 5d216f6 | 2013-11-13 10:23:46 +1000 | [diff] [blame] | 1449 | nvbo->bo.sync_obj = new_fence; |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1450 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
| 1451 | |
| 1452 | nouveau_fence_unref(&old_fence); |
| 1453 | } |
| 1454 | |
| 1455 | static void |
| 1456 | nouveau_bo_fence_unref(void **sync_obj) |
| 1457 | { |
| 1458 | nouveau_fence_unref((struct nouveau_fence **)sync_obj); |
| 1459 | } |
| 1460 | |
| 1461 | static void * |
| 1462 | nouveau_bo_fence_ref(void *sync_obj) |
| 1463 | { |
| 1464 | return nouveau_fence_ref(sync_obj); |
| 1465 | } |
| 1466 | |
| 1467 | static bool |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1468 | nouveau_bo_fence_signalled(void *sync_obj) |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1469 | { |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 1470 | return nouveau_fence_done(sync_obj); |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | static int |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1474 | nouveau_bo_fence_wait(void *sync_obj, bool lazy, bool intr) |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1475 | { |
| 1476 | return nouveau_fence_wait(sync_obj, lazy, intr); |
| 1477 | } |
| 1478 | |
| 1479 | static int |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1480 | nouveau_bo_fence_flush(void *sync_obj) |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1481 | { |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1485 | struct ttm_bo_driver nouveau_bo_driver = { |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 1486 | .ttm_tt_create = &nouveau_ttm_tt_create, |
Konrad Rzeszutek Wilk | 3230cfc | 2011-10-17 17:14:26 -0400 | [diff] [blame] | 1487 | .ttm_tt_populate = &nouveau_ttm_tt_populate, |
| 1488 | .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1489 | .invalidate_caches = nouveau_bo_invalidate_caches, |
| 1490 | .init_mem_type = nouveau_bo_init_mem_type, |
| 1491 | .evict_flags = nouveau_bo_evict_flags, |
Ben Skeggs | a4154bb | 2011-02-10 10:35:16 +1000 | [diff] [blame] | 1492 | .move_notify = nouveau_bo_move_ntfy, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1493 | .move = nouveau_bo_move, |
| 1494 | .verify_access = nouveau_bo_verify_access, |
Ben Skeggs | 875ac34 | 2012-04-30 12:51:48 +1000 | [diff] [blame] | 1495 | .sync_obj_signaled = nouveau_bo_fence_signalled, |
| 1496 | .sync_obj_wait = nouveau_bo_fence_wait, |
| 1497 | .sync_obj_flush = nouveau_bo_fence_flush, |
| 1498 | .sync_obj_unref = nouveau_bo_fence_unref, |
| 1499 | .sync_obj_ref = nouveau_bo_fence_ref, |
Jerome Glisse | f32f02f | 2010-04-09 14:39:25 +0200 | [diff] [blame] | 1500 | .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify, |
| 1501 | .io_mem_reserve = &nouveau_ttm_io_mem_reserve, |
| 1502 | .io_mem_free = &nouveau_ttm_io_mem_free, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1503 | }; |
| 1504 | |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1505 | struct nouveau_vma * |
| 1506 | nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm) |
| 1507 | { |
| 1508 | struct nouveau_vma *vma; |
| 1509 | list_for_each_entry(vma, &nvbo->vma_list, head) { |
| 1510 | if (vma->vm == vm) |
| 1511 | return vma; |
| 1512 | } |
| 1513 | |
| 1514 | return NULL; |
| 1515 | } |
| 1516 | |
| 1517 | int |
| 1518 | nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm, |
| 1519 | struct nouveau_vma *vma) |
| 1520 | { |
| 1521 | const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT; |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1522 | int ret; |
| 1523 | |
| 1524 | ret = nouveau_vm_get(vm, size, nvbo->page_shift, |
| 1525 | NV_MEM_ACCESS_RW, vma); |
| 1526 | if (ret) |
| 1527 | return ret; |
| 1528 | |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 1529 | if ( nvbo->bo.mem.mem_type != TTM_PL_SYSTEM && |
| 1530 | (nvbo->bo.mem.mem_type == TTM_PL_VRAM || |
| 1531 | nvbo->page_shift != vma->vm->vmm->lpg_shift)) |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1532 | nouveau_vm_map(vma, nvbo->bo.mem.mm_node); |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1533 | |
| 1534 | list_add_tail(&vma->head, &nvbo->vma_list); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 1535 | vma->refcount = 1; |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | void |
| 1540 | nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma) |
| 1541 | { |
| 1542 | if (vma->node) { |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 1543 | if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1544 | nouveau_vm_unmap(vma); |
Ben Skeggs | fd2871a | 2011-06-06 14:07:04 +1000 | [diff] [blame] | 1545 | nouveau_vm_put(vma); |
| 1546 | list_del(&vma->head); |
| 1547 | } |
| 1548 | } |