Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA, |
| 3 | * All Rights Reserved. |
| 4 | * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA, |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sub license, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 27 | #include "nouveau_drm.h" |
| 28 | #include "nouveau_ttm.h" |
| 29 | #include "nouveau_gem.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 30 | |
Dave Airlie | 2036eaa | 2014-12-16 16:33:09 +1000 | [diff] [blame] | 31 | #include "drm_legacy.h" |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 32 | static int |
| 33 | nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) |
| 34 | { |
Marcin Slusarz | 897a6e2 | 2013-03-02 20:00:31 +0100 | [diff] [blame] | 35 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
Ben Skeggs | b1e4553 | 2015-08-20 14:54:06 +1000 | [diff] [blame] | 36 | struct nvkm_fb *fb = nvxx_fb(&drm->device); |
| 37 | man->priv = fb; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int |
| 42 | nouveau_vram_manager_fini(struct ttm_mem_type_manager *man) |
| 43 | { |
Marcin Slusarz | 897a6e2 | 2013-03-02 20:00:31 +0100 | [diff] [blame] | 44 | man->priv = NULL; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static inline void |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 49 | nvkm_mem_node_cleanup(struct nvkm_mem *node) |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 50 | { |
| 51 | if (node->vma[0].node) { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 52 | nvkm_vm_unmap(&node->vma[0]); |
| 53 | nvkm_vm_put(&node->vma[0]); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | if (node->vma[1].node) { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 57 | nvkm_vm_unmap(&node->vma[1]); |
| 58 | nvkm_vm_put(&node->vma[1]); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | static void |
| 63 | nouveau_vram_manager_del(struct ttm_mem_type_manager *man, |
| 64 | struct ttm_mem_reg *mem) |
| 65 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 66 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
Ben Skeggs | d36a99d | 2015-08-20 14:54:14 +1000 | [diff] [blame] | 67 | struct nvkm_ram *ram = nvxx_fb(&drm->device)->ram; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 68 | nvkm_mem_node_cleanup(mem->mm_node); |
Ben Skeggs | d36a99d | 2015-08-20 14:54:14 +1000 | [diff] [blame] | 69 | ram->func->put(ram, (struct nvkm_mem **)&mem->mm_node); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int |
| 73 | nouveau_vram_manager_new(struct ttm_mem_type_manager *man, |
| 74 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 75 | const struct ttm_place *place, |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 76 | struct ttm_mem_reg *mem) |
| 77 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 78 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
Ben Skeggs | d36a99d | 2015-08-20 14:54:14 +1000 | [diff] [blame] | 79 | struct nvkm_ram *ram = nvxx_fb(&drm->device)->ram; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 80 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 81 | struct nvkm_mem *node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 82 | u32 size_nc = 0; |
| 83 | int ret; |
| 84 | |
Alexandre Courbot | eaecf03 | 2015-02-20 18:22:59 +0900 | [diff] [blame] | 85 | if (drm->device.info.ram_size == 0) |
| 86 | return -ENOMEM; |
| 87 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 88 | if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) |
| 89 | size_nc = 1 << nvbo->page_shift; |
| 90 | |
Ben Skeggs | d36a99d | 2015-08-20 14:54:14 +1000 | [diff] [blame] | 91 | ret = ram->func->get(ram, mem->num_pages << PAGE_SHIFT, |
| 92 | mem->page_alignment << PAGE_SHIFT, size_nc, |
| 93 | (nvbo->tile_flags >> 8) & 0x3ff, &node); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 94 | if (ret) { |
| 95 | mem->mm_node = NULL; |
| 96 | return (ret == -ENOSPC) ? 0 : ret; |
| 97 | } |
| 98 | |
| 99 | node->page_shift = nvbo->page_shift; |
| 100 | |
| 101 | mem->mm_node = node; |
| 102 | mem->start = node->offset >> PAGE_SHIFT; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 106 | const struct ttm_mem_type_manager_func nouveau_vram_manager = { |
| 107 | nouveau_vram_manager_init, |
| 108 | nouveau_vram_manager_fini, |
| 109 | nouveau_vram_manager_new, |
| 110 | nouveau_vram_manager_del, |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static int |
| 114 | nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int |
| 120 | nouveau_gart_manager_fini(struct ttm_mem_type_manager *man) |
| 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | nouveau_gart_manager_del(struct ttm_mem_type_manager *man, |
| 127 | struct ttm_mem_reg *mem) |
| 128 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 129 | nvkm_mem_node_cleanup(mem->mm_node); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 130 | kfree(mem->mm_node); |
| 131 | mem->mm_node = NULL; |
| 132 | } |
| 133 | |
| 134 | static int |
| 135 | nouveau_gart_manager_new(struct ttm_mem_type_manager *man, |
| 136 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 137 | const struct ttm_place *place, |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 138 | struct ttm_mem_reg *mem) |
| 139 | { |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 140 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
| 141 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 142 | struct nvkm_mem *node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 143 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 144 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 145 | if (!node) |
| 146 | return -ENOMEM; |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 147 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 148 | node->page_shift = 12; |
| 149 | |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 150 | switch (drm->device.info.family) { |
Alexandre Courbot | eb48b12 | 2015-07-09 17:15:14 +0900 | [diff] [blame] | 151 | case NV_DEVICE_INFO_V0_TNT: |
| 152 | case NV_DEVICE_INFO_V0_CELSIUS: |
| 153 | case NV_DEVICE_INFO_V0_KELVIN: |
| 154 | case NV_DEVICE_INFO_V0_RANKINE: |
| 155 | case NV_DEVICE_INFO_V0_CURIE: |
| 156 | break; |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 157 | case NV_DEVICE_INFO_V0_TESLA: |
| 158 | if (drm->device.info.chipset != 0x50) |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 159 | node->memtype = (nvbo->tile_flags & 0x7f00) >> 8; |
| 160 | break; |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 161 | case NV_DEVICE_INFO_V0_FERMI: |
| 162 | case NV_DEVICE_INFO_V0_KEPLER: |
Alexandre Courbot | eb48b12 | 2015-07-09 17:15:14 +0900 | [diff] [blame] | 163 | case NV_DEVICE_INFO_V0_MAXWELL: |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 164 | node->memtype = (nvbo->tile_flags & 0xff00) >> 8; |
| 165 | break; |
| 166 | default: |
Alexandre Courbot | eb48b12 | 2015-07-09 17:15:14 +0900 | [diff] [blame] | 167 | NV_WARN(drm, "%s: unhandled family type %x\n", __func__, |
| 168 | drm->device.info.family); |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 172 | mem->mm_node = node; |
| 173 | mem->start = 0; |
| 174 | return 0; |
| 175 | } |
| 176 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 177 | static void |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 178 | nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | const struct ttm_mem_type_manager_func nouveau_gart_manager = { |
| 183 | nouveau_gart_manager_init, |
| 184 | nouveau_gart_manager_fini, |
| 185 | nouveau_gart_manager_new, |
| 186 | nouveau_gart_manager_del, |
| 187 | nouveau_gart_manager_debug |
| 188 | }; |
| 189 | |
Ben Skeggs | fdb751e | 2014-08-10 04:10:23 +1000 | [diff] [blame] | 190 | /*XXX*/ |
Ben Skeggs | 5ce3bf3 | 2015-01-14 09:57:36 +1000 | [diff] [blame] | 191 | #include <subdev/mmu/nv04.h> |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 192 | static int |
| 193 | nv04_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) |
| 194 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 195 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 196 | struct nvkm_mmu *mmu = nvxx_mmu(&drm->device); |
Ben Skeggs | 1f5bffc | 2015-08-20 14:54:07 +1000 | [diff] [blame] | 197 | struct nv04_mmu *priv = (void *)mmu; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 198 | struct nvkm_vm *vm = NULL; |
| 199 | nvkm_vm_ref(priv->vm, &vm, NULL); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 200 | man->priv = vm; |
| 201 | return 0; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int |
| 205 | nv04_gart_manager_fini(struct ttm_mem_type_manager *man) |
| 206 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 207 | struct nvkm_vm *vm = man->priv; |
| 208 | nvkm_vm_ref(NULL, &vm, NULL); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 209 | man->priv = NULL; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | nv04_gart_manager_del(struct ttm_mem_type_manager *man, struct ttm_mem_reg *mem) |
| 215 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 216 | struct nvkm_mem *node = mem->mm_node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 217 | if (node->vma[0].node) |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 218 | nvkm_vm_put(&node->vma[0]); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 219 | kfree(mem->mm_node); |
| 220 | mem->mm_node = NULL; |
| 221 | } |
| 222 | |
| 223 | static int |
| 224 | nv04_gart_manager_new(struct ttm_mem_type_manager *man, |
| 225 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 226 | const struct ttm_place *place, |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 227 | struct ttm_mem_reg *mem) |
| 228 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 229 | struct nvkm_mem *node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 230 | int ret; |
| 231 | |
| 232 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 233 | if (!node) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | node->page_shift = 12; |
| 237 | |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 238 | ret = nvkm_vm_get(man->priv, mem->num_pages << 12, node->page_shift, |
| 239 | NV_MEM_ACCESS_RW, &node->vma[0]); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 240 | if (ret) { |
| 241 | kfree(node); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | mem->mm_node = node; |
| 246 | mem->start = node->vma[0].offset >> PAGE_SHIFT; |
| 247 | return 0; |
| 248 | } |
| 249 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 250 | static void |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 251 | nv04_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | const struct ttm_mem_type_manager_func nv04_gart_manager = { |
| 256 | nv04_gart_manager_init, |
| 257 | nv04_gart_manager_fini, |
| 258 | nv04_gart_manager_new, |
| 259 | nv04_gart_manager_del, |
| 260 | nv04_gart_manager_debug |
| 261 | }; |
| 262 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 263 | int |
| 264 | nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma) |
| 265 | { |
| 266 | struct drm_file *file_priv = filp->private_data; |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 267 | struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 268 | |
| 269 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) |
Dave Airlie | 2036eaa | 2014-12-16 16:33:09 +1000 | [diff] [blame] | 270 | return drm_legacy_mmap(filp, vma); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 271 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 272 | return ttm_bo_mmap(filp, vma, &drm->ttm.bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 276 | nouveau_ttm_mem_global_init(struct drm_global_reference *ref) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 277 | { |
| 278 | return ttm_mem_global_init(ref->object); |
| 279 | } |
| 280 | |
| 281 | static void |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 282 | nouveau_ttm_mem_global_release(struct drm_global_reference *ref) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 283 | { |
| 284 | ttm_mem_global_release(ref->object); |
| 285 | } |
| 286 | |
| 287 | int |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 288 | nouveau_ttm_global_init(struct nouveau_drm *drm) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 289 | { |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 290 | struct drm_global_reference *global_ref; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 291 | int ret; |
| 292 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 293 | global_ref = &drm->ttm.mem_global_ref; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 294 | global_ref->global_type = DRM_GLOBAL_TTM_MEM; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 295 | global_ref->size = sizeof(struct ttm_mem_global); |
| 296 | global_ref->init = &nouveau_ttm_mem_global_init; |
| 297 | global_ref->release = &nouveau_ttm_mem_global_release; |
| 298 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 299 | ret = drm_global_item_ref(global_ref); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 300 | if (unlikely(ret != 0)) { |
| 301 | DRM_ERROR("Failed setting up TTM memory accounting\n"); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 302 | drm->ttm.mem_global_ref.release = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 303 | return ret; |
| 304 | } |
| 305 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 306 | drm->ttm.bo_global_ref.mem_glob = global_ref->object; |
| 307 | global_ref = &drm->ttm.bo_global_ref.ref; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 308 | global_ref->global_type = DRM_GLOBAL_TTM_BO; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 309 | global_ref->size = sizeof(struct ttm_bo_global); |
| 310 | global_ref->init = &ttm_bo_global_init; |
| 311 | global_ref->release = &ttm_bo_global_release; |
| 312 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 313 | ret = drm_global_item_ref(global_ref); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 314 | if (unlikely(ret != 0)) { |
| 315 | DRM_ERROR("Failed setting up TTM BO subsystem\n"); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 316 | drm_global_item_unref(&drm->ttm.mem_global_ref); |
| 317 | drm->ttm.mem_global_ref.release = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | void |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 325 | nouveau_ttm_global_release(struct nouveau_drm *drm) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 326 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 327 | if (drm->ttm.mem_global_ref.release == NULL) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 328 | return; |
| 329 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 330 | drm_global_item_unref(&drm->ttm.bo_global_ref.ref); |
| 331 | drm_global_item_unref(&drm->ttm.mem_global_ref); |
| 332 | drm->ttm.mem_global_ref.release = NULL; |
| 333 | } |
| 334 | |
| 335 | int |
| 336 | nouveau_ttm_init(struct nouveau_drm *drm) |
| 337 | { |
| 338 | struct drm_device *dev = drm->dev; |
| 339 | u32 bits; |
| 340 | int ret; |
| 341 | |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 342 | bits = nvxx_mmu(&drm->device)->dma_bits; |
| 343 | if (nv_device_is_pci(nvxx_device(&drm->device))) { |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 344 | if (drm->agp.stat == ENABLED || |
| 345 | !pci_dma_supported(dev->pdev, DMA_BIT_MASK(bits))) |
| 346 | bits = 32; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 347 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 348 | ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(bits)); |
| 349 | if (ret) |
| 350 | return ret; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 351 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 352 | ret = pci_set_consistent_dma_mask(dev->pdev, |
| 353 | DMA_BIT_MASK(bits)); |
| 354 | if (ret) |
| 355 | pci_set_consistent_dma_mask(dev->pdev, |
| 356 | DMA_BIT_MASK(32)); |
| 357 | } |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 358 | |
| 359 | ret = nouveau_ttm_global_init(drm); |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | |
| 363 | ret = ttm_bo_device_init(&drm->ttm.bdev, |
| 364 | drm->ttm.bo_global_ref.ref.object, |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 365 | &nouveau_bo_driver, |
| 366 | dev->anon_inode->i_mapping, |
| 367 | DRM_FILE_PAGE_OFFSET, |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 368 | bits <= 32 ? true : false); |
| 369 | if (ret) { |
| 370 | NV_ERROR(drm, "error initialising bo driver, %d\n", ret); |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | /* VRAM init */ |
Ben Skeggs | f392ec4 | 2014-08-10 04:10:28 +1000 | [diff] [blame] | 375 | drm->gem.vram_available = drm->device.info.ram_user; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 376 | |
| 377 | ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM, |
| 378 | drm->gem.vram_available >> PAGE_SHIFT); |
| 379 | if (ret) { |
| 380 | NV_ERROR(drm, "VRAM mm init failed, %d\n", ret); |
| 381 | return ret; |
| 382 | } |
| 383 | |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 384 | drm->ttm.mtrr = arch_phys_wc_add(nv_device_resource_start(nvxx_device(&drm->device), 1), |
| 385 | nv_device_resource_len(nvxx_device(&drm->device), 1)); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 386 | |
| 387 | /* GART init */ |
| 388 | if (drm->agp.stat != ENABLED) { |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 389 | drm->gem.gart_available = nvxx_mmu(&drm->device)->limit; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 390 | } else { |
| 391 | drm->gem.gart_available = drm->agp.size; |
| 392 | } |
| 393 | |
| 394 | ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT, |
| 395 | drm->gem.gart_available >> PAGE_SHIFT); |
| 396 | if (ret) { |
| 397 | NV_ERROR(drm, "GART mm init failed, %d\n", ret); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20)); |
| 402 | NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20)); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | nouveau_ttm_fini(struct nouveau_drm *drm) |
| 408 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 409 | ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM); |
| 410 | ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 411 | |
| 412 | ttm_bo_device_release(&drm->ttm.bdev); |
| 413 | |
| 414 | nouveau_ttm_global_release(drm); |
| 415 | |
Andy Lutomirski | 247d36d | 2013-05-13 23:58:41 +0000 | [diff] [blame] | 416 | arch_phys_wc_del(drm->ttm.mtrr); |
| 417 | drm->ttm.mtrr = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 418 | } |