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 | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 36 | struct nvkm_fb *pfb = nvxx_fb(&drm->device); |
Marcin Slusarz | 897a6e2 | 2013-03-02 20:00:31 +0100 | [diff] [blame] | 37 | man->priv = pfb; |
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 | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 67 | struct nvkm_fb *pfb = nvxx_fb(&drm->device); |
| 68 | nvkm_mem_node_cleanup(mem->mm_node); |
| 69 | pfb->ram->put(pfb, (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 | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 79 | struct nvkm_fb *pfb = nvxx_fb(&drm->device); |
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 | dceef5d | 2013-03-04 13:01:21 +1000 | [diff] [blame] | 91 | ret = pfb->ram->get(pfb, mem->num_pages << PAGE_SHIFT, |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 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 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 106 | static void |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 107 | nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
| 108 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 109 | struct nvkm_fb *pfb = man->priv; |
| 110 | struct nvkm_mm *mm = &pfb->vram; |
| 111 | struct nvkm_mm_node *r; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 112 | u32 total = 0, free = 0; |
| 113 | |
Ben Skeggs | 51a506c | 2013-05-13 22:30:56 +1000 | [diff] [blame] | 114 | mutex_lock(&nv_subdev(pfb)->mutex); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 115 | list_for_each_entry(r, &mm->nodes, nl_entry) { |
| 116 | printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n", |
| 117 | prefix, r->type, ((u64)r->offset << 12), |
| 118 | (((u64)r->offset + r->length) << 12)); |
| 119 | |
| 120 | total += r->length; |
| 121 | if (!r->type) |
| 122 | free += r->length; |
| 123 | } |
Ben Skeggs | 51a506c | 2013-05-13 22:30:56 +1000 | [diff] [blame] | 124 | mutex_unlock(&nv_subdev(pfb)->mutex); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 125 | |
| 126 | printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n", |
| 127 | prefix, (u64)total << 12, (u64)free << 12); |
| 128 | printk(KERN_DEBUG "%s block: 0x%08x\n", |
| 129 | prefix, mm->block_size << 12); |
| 130 | } |
| 131 | |
| 132 | const struct ttm_mem_type_manager_func nouveau_vram_manager = { |
| 133 | nouveau_vram_manager_init, |
| 134 | nouveau_vram_manager_fini, |
| 135 | nouveau_vram_manager_new, |
| 136 | nouveau_vram_manager_del, |
| 137 | nouveau_vram_manager_debug |
| 138 | }; |
| 139 | |
| 140 | static int |
| 141 | nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) |
| 142 | { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int |
| 147 | nouveau_gart_manager_fini(struct ttm_mem_type_manager *man) |
| 148 | { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | nouveau_gart_manager_del(struct ttm_mem_type_manager *man, |
| 154 | struct ttm_mem_reg *mem) |
| 155 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 156 | nvkm_mem_node_cleanup(mem->mm_node); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 157 | kfree(mem->mm_node); |
| 158 | mem->mm_node = NULL; |
| 159 | } |
| 160 | |
| 161 | static int |
| 162 | nouveau_gart_manager_new(struct ttm_mem_type_manager *man, |
| 163 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 164 | const struct ttm_place *place, |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 165 | struct ttm_mem_reg *mem) |
| 166 | { |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 167 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
| 168 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 169 | struct nvkm_mem *node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 170 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 171 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 172 | if (!node) |
| 173 | return -ENOMEM; |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 174 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 175 | node->page_shift = 12; |
| 176 | |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 177 | switch (drm->device.info.family) { |
Alexandre Courbot | eb48b12 | 2015-07-09 17:15:14 +0900 | [diff] [blame] | 178 | case NV_DEVICE_INFO_V0_TNT: |
| 179 | case NV_DEVICE_INFO_V0_CELSIUS: |
| 180 | case NV_DEVICE_INFO_V0_KELVIN: |
| 181 | case NV_DEVICE_INFO_V0_RANKINE: |
| 182 | case NV_DEVICE_INFO_V0_CURIE: |
| 183 | break; |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 184 | case NV_DEVICE_INFO_V0_TESLA: |
| 185 | if (drm->device.info.chipset != 0x50) |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 186 | node->memtype = (nvbo->tile_flags & 0x7f00) >> 8; |
| 187 | break; |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 188 | case NV_DEVICE_INFO_V0_FERMI: |
| 189 | case NV_DEVICE_INFO_V0_KEPLER: |
Alexandre Courbot | eb48b12 | 2015-07-09 17:15:14 +0900 | [diff] [blame] | 190 | case NV_DEVICE_INFO_V0_MAXWELL: |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 191 | node->memtype = (nvbo->tile_flags & 0xff00) >> 8; |
| 192 | break; |
| 193 | default: |
Alexandre Courbot | eb48b12 | 2015-07-09 17:15:14 +0900 | [diff] [blame] | 194 | NV_WARN(drm, "%s: unhandled family type %x\n", __func__, |
| 195 | drm->device.info.family); |
Ben Skeggs | de7b7d5 | 2013-03-22 12:12:17 +1000 | [diff] [blame] | 196 | break; |
| 197 | } |
| 198 | |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 199 | mem->mm_node = node; |
| 200 | mem->start = 0; |
| 201 | return 0; |
| 202 | } |
| 203 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 204 | static void |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 205 | nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | const struct ttm_mem_type_manager_func nouveau_gart_manager = { |
| 210 | nouveau_gart_manager_init, |
| 211 | nouveau_gart_manager_fini, |
| 212 | nouveau_gart_manager_new, |
| 213 | nouveau_gart_manager_del, |
| 214 | nouveau_gart_manager_debug |
| 215 | }; |
| 216 | |
Ben Skeggs | fdb751e | 2014-08-10 04:10:23 +1000 | [diff] [blame] | 217 | /*XXX*/ |
Ben Skeggs | 5ce3bf3 | 2015-01-14 09:57:36 +1000 | [diff] [blame] | 218 | #include <subdev/mmu/nv04.h> |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 219 | static int |
| 220 | nv04_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) |
| 221 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 222 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 223 | struct nvkm_mmu *mmu = nvxx_mmu(&drm->device); |
Ben Skeggs | 5ce3bf3 | 2015-01-14 09:57:36 +1000 | [diff] [blame] | 224 | struct nv04_mmu_priv *priv = (void *)mmu; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 225 | struct nvkm_vm *vm = NULL; |
| 226 | nvkm_vm_ref(priv->vm, &vm, NULL); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 227 | man->priv = vm; |
| 228 | return 0; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int |
| 232 | nv04_gart_manager_fini(struct ttm_mem_type_manager *man) |
| 233 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 234 | struct nvkm_vm *vm = man->priv; |
| 235 | nvkm_vm_ref(NULL, &vm, NULL); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 236 | man->priv = NULL; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | nv04_gart_manager_del(struct ttm_mem_type_manager *man, struct ttm_mem_reg *mem) |
| 242 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 243 | struct nvkm_mem *node = mem->mm_node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 244 | if (node->vma[0].node) |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 245 | nvkm_vm_put(&node->vma[0]); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 246 | kfree(mem->mm_node); |
| 247 | mem->mm_node = NULL; |
| 248 | } |
| 249 | |
| 250 | static int |
| 251 | nv04_gart_manager_new(struct ttm_mem_type_manager *man, |
| 252 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 253 | const struct ttm_place *place, |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 254 | struct ttm_mem_reg *mem) |
| 255 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 256 | struct nvkm_mem *node; |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 257 | int ret; |
| 258 | |
| 259 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 260 | if (!node) |
| 261 | return -ENOMEM; |
| 262 | |
| 263 | node->page_shift = 12; |
| 264 | |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 265 | ret = nvkm_vm_get(man->priv, mem->num_pages << 12, node->page_shift, |
| 266 | NV_MEM_ACCESS_RW, &node->vma[0]); |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 267 | if (ret) { |
| 268 | kfree(node); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | mem->mm_node = node; |
| 273 | mem->start = node->vma[0].offset >> PAGE_SHIFT; |
| 274 | return 0; |
| 275 | } |
| 276 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 277 | static void |
Ben Skeggs | bc9e7b9 | 2012-07-19 17:54:21 +1000 | [diff] [blame] | 278 | nv04_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
| 279 | { |
| 280 | } |
| 281 | |
| 282 | const struct ttm_mem_type_manager_func nv04_gart_manager = { |
| 283 | nv04_gart_manager_init, |
| 284 | nv04_gart_manager_fini, |
| 285 | nv04_gart_manager_new, |
| 286 | nv04_gart_manager_del, |
| 287 | nv04_gart_manager_debug |
| 288 | }; |
| 289 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 290 | int |
| 291 | nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma) |
| 292 | { |
| 293 | struct drm_file *file_priv = filp->private_data; |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 294 | struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 295 | |
| 296 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) |
Dave Airlie | 2036eaa | 2014-12-16 16:33:09 +1000 | [diff] [blame] | 297 | return drm_legacy_mmap(filp, vma); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 298 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 299 | return ttm_bo_mmap(filp, vma, &drm->ttm.bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 303 | nouveau_ttm_mem_global_init(struct drm_global_reference *ref) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 304 | { |
| 305 | return ttm_mem_global_init(ref->object); |
| 306 | } |
| 307 | |
| 308 | static void |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 309 | nouveau_ttm_mem_global_release(struct drm_global_reference *ref) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 310 | { |
| 311 | ttm_mem_global_release(ref->object); |
| 312 | } |
| 313 | |
| 314 | int |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 315 | nouveau_ttm_global_init(struct nouveau_drm *drm) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 316 | { |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 317 | struct drm_global_reference *global_ref; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 318 | int ret; |
| 319 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 320 | global_ref = &drm->ttm.mem_global_ref; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 321 | global_ref->global_type = DRM_GLOBAL_TTM_MEM; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 322 | global_ref->size = sizeof(struct ttm_mem_global); |
| 323 | global_ref->init = &nouveau_ttm_mem_global_init; |
| 324 | global_ref->release = &nouveau_ttm_mem_global_release; |
| 325 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 326 | ret = drm_global_item_ref(global_ref); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 327 | if (unlikely(ret != 0)) { |
| 328 | DRM_ERROR("Failed setting up TTM memory accounting\n"); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 329 | drm->ttm.mem_global_ref.release = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 333 | drm->ttm.bo_global_ref.mem_glob = global_ref->object; |
| 334 | global_ref = &drm->ttm.bo_global_ref.ref; |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 335 | global_ref->global_type = DRM_GLOBAL_TTM_BO; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 336 | global_ref->size = sizeof(struct ttm_bo_global); |
| 337 | global_ref->init = &ttm_bo_global_init; |
| 338 | global_ref->release = &ttm_bo_global_release; |
| 339 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 340 | ret = drm_global_item_ref(global_ref); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 341 | if (unlikely(ret != 0)) { |
| 342 | DRM_ERROR("Failed setting up TTM BO subsystem\n"); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 343 | drm_global_item_unref(&drm->ttm.mem_global_ref); |
| 344 | drm->ttm.mem_global_ref.release = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | void |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 352 | nouveau_ttm_global_release(struct nouveau_drm *drm) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 353 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 354 | if (drm->ttm.mem_global_ref.release == NULL) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 355 | return; |
| 356 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 357 | drm_global_item_unref(&drm->ttm.bo_global_ref.ref); |
| 358 | drm_global_item_unref(&drm->ttm.mem_global_ref); |
| 359 | drm->ttm.mem_global_ref.release = NULL; |
| 360 | } |
| 361 | |
| 362 | int |
| 363 | nouveau_ttm_init(struct nouveau_drm *drm) |
| 364 | { |
| 365 | struct drm_device *dev = drm->dev; |
| 366 | u32 bits; |
| 367 | int ret; |
| 368 | |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 369 | bits = nvxx_mmu(&drm->device)->dma_bits; |
| 370 | if (nv_device_is_pci(nvxx_device(&drm->device))) { |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 371 | if (drm->agp.stat == ENABLED || |
| 372 | !pci_dma_supported(dev->pdev, DMA_BIT_MASK(bits))) |
| 373 | bits = 32; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 374 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 375 | ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(bits)); |
| 376 | if (ret) |
| 377 | return ret; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 378 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 379 | ret = pci_set_consistent_dma_mask(dev->pdev, |
| 380 | DMA_BIT_MASK(bits)); |
| 381 | if (ret) |
| 382 | pci_set_consistent_dma_mask(dev->pdev, |
| 383 | DMA_BIT_MASK(32)); |
| 384 | } |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 385 | |
| 386 | ret = nouveau_ttm_global_init(drm); |
| 387 | if (ret) |
| 388 | return ret; |
| 389 | |
| 390 | ret = ttm_bo_device_init(&drm->ttm.bdev, |
| 391 | drm->ttm.bo_global_ref.ref.object, |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 392 | &nouveau_bo_driver, |
| 393 | dev->anon_inode->i_mapping, |
| 394 | DRM_FILE_PAGE_OFFSET, |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 395 | bits <= 32 ? true : false); |
| 396 | if (ret) { |
| 397 | NV_ERROR(drm, "error initialising bo driver, %d\n", ret); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | /* VRAM init */ |
Ben Skeggs | f392ec4 | 2014-08-10 04:10:28 +1000 | [diff] [blame] | 402 | drm->gem.vram_available = drm->device.info.ram_user; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 403 | |
| 404 | ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM, |
| 405 | drm->gem.vram_available >> PAGE_SHIFT); |
| 406 | if (ret) { |
| 407 | NV_ERROR(drm, "VRAM mm init failed, %d\n", ret); |
| 408 | return ret; |
| 409 | } |
| 410 | |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 411 | drm->ttm.mtrr = arch_phys_wc_add(nv_device_resource_start(nvxx_device(&drm->device), 1), |
| 412 | nv_device_resource_len(nvxx_device(&drm->device), 1)); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 413 | |
| 414 | /* GART init */ |
| 415 | if (drm->agp.stat != ENABLED) { |
Ben Skeggs | 989aa5b | 2015-01-12 12:33:37 +1000 | [diff] [blame] | 416 | drm->gem.gart_available = nvxx_mmu(&drm->device)->limit; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 417 | } else { |
| 418 | drm->gem.gart_available = drm->agp.size; |
| 419 | } |
| 420 | |
| 421 | ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT, |
| 422 | drm->gem.gart_available >> PAGE_SHIFT); |
| 423 | if (ret) { |
| 424 | NV_ERROR(drm, "GART mm init failed, %d\n", ret); |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20)); |
| 429 | NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20)); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | void |
| 434 | nouveau_ttm_fini(struct nouveau_drm *drm) |
| 435 | { |
| 436 | mutex_lock(&drm->dev->struct_mutex); |
| 437 | ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM); |
| 438 | ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT); |
| 439 | mutex_unlock(&drm->dev->struct_mutex); |
| 440 | |
| 441 | ttm_bo_device_release(&drm->ttm.bdev); |
| 442 | |
| 443 | nouveau_ttm_global_release(drm); |
| 444 | |
Andy Lutomirski | 247d36d | 2013-05-13 23:58:41 +0000 | [diff] [blame] | 445 | arch_phys_wc_del(drm->ttm.mtrr); |
| 446 | drm->ttm.mtrr = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 447 | } |