1 /* $NetBSD: nouveau_ttm.c,v 1.10 2022/05/21 17:50:21 riastradh Exp $ */ 2 3 // SPDX-License-Identifier: GPL-2.0 OR MIT 4 /* 5 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA, 6 * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA, 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sub license, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: nouveau_ttm.c,v 1.10 2022/05/21 17:50:21 riastradh Exp $"); 29 30 #include <sys/param.h> 31 #include <uvm/uvm_extern.h> /* pmap_pv_track/untrack */ 32 33 #include "nouveau_drv.h" 34 #include "nouveau_gem.h" 35 #include "nouveau_mem.h" 36 #include "nouveau_ttm.h" 37 38 #include <drm/drm_legacy.h> 39 40 #include <core/tegra.h> 41 42 static int 43 nouveau_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) 44 { 45 return 0; 46 } 47 48 static int 49 nouveau_manager_fini(struct ttm_mem_type_manager *man) 50 { 51 return 0; 52 } 53 54 static void 55 nouveau_manager_del(struct ttm_mem_type_manager *man, struct ttm_mem_reg *reg) 56 { 57 nouveau_mem_del(reg); 58 } 59 60 static void 61 nouveau_manager_debug(struct ttm_mem_type_manager *man, 62 struct drm_printer *printer) 63 { 64 } 65 66 static int 67 nouveau_vram_manager_new(struct ttm_mem_type_manager *man, 68 struct ttm_buffer_object *bo, 69 const struct ttm_place *place, 70 struct ttm_mem_reg *reg) 71 { 72 struct nouveau_bo *nvbo = nouveau_bo(bo); 73 struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 74 int ret; 75 76 if (drm->client.device.info.ram_size == 0) 77 return -ENOMEM; 78 79 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg); 80 if (ret) 81 return ret; 82 83 ret = nouveau_mem_vram(reg, nvbo->contig, nvbo->page); 84 if (ret) { 85 nouveau_mem_del(reg); 86 if (ret == -ENOSPC) { 87 reg->mm_node = NULL; 88 return 0; 89 } 90 return ret; 91 } 92 93 return 0; 94 } 95 96 const struct ttm_mem_type_manager_func nouveau_vram_manager = { 97 .init = nouveau_manager_init, 98 .takedown = nouveau_manager_fini, 99 .get_node = nouveau_vram_manager_new, 100 .put_node = nouveau_manager_del, 101 .debug = nouveau_manager_debug, 102 }; 103 104 static int 105 nouveau_gart_manager_new(struct ttm_mem_type_manager *man, 106 struct ttm_buffer_object *bo, 107 const struct ttm_place *place, 108 struct ttm_mem_reg *reg) 109 { 110 struct nouveau_bo *nvbo = nouveau_bo(bo); 111 struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 112 int ret; 113 114 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg); 115 if (ret) 116 return ret; 117 118 reg->start = 0; 119 return 0; 120 } 121 122 const struct ttm_mem_type_manager_func nouveau_gart_manager = { 123 .init = nouveau_manager_init, 124 .takedown = nouveau_manager_fini, 125 .get_node = nouveau_gart_manager_new, 126 .put_node = nouveau_manager_del, 127 .debug = nouveau_manager_debug 128 }; 129 130 static int 131 nv04_gart_manager_new(struct ttm_mem_type_manager *man, 132 struct ttm_buffer_object *bo, 133 const struct ttm_place *place, 134 struct ttm_mem_reg *reg) 135 { 136 struct nouveau_bo *nvbo = nouveau_bo(bo); 137 struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 138 struct nouveau_mem *mem; 139 int ret; 140 141 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg); 142 mem = nouveau_mem(reg); 143 if (ret) 144 return ret; 145 146 ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0, 147 reg->num_pages << PAGE_SHIFT, &mem->vma[0]); 148 if (ret) { 149 nouveau_mem_del(reg); 150 if (ret == -ENOSPC) { 151 reg->mm_node = NULL; 152 return 0; 153 } 154 return ret; 155 } 156 157 reg->start = mem->vma[0].addr >> PAGE_SHIFT; 158 return 0; 159 } 160 161 const struct ttm_mem_type_manager_func nv04_gart_manager = { 162 .init = nouveau_manager_init, 163 .takedown = nouveau_manager_fini, 164 .get_node = nv04_gart_manager_new, 165 .put_node = nouveau_manager_del, 166 .debug = nouveau_manager_debug 167 }; 168 169 #ifdef __NetBSD__ 170 171 int 172 nouveau_ttm_mmap_object(struct drm_device *dev, off_t offset, size_t size, 173 vm_prot_t prot, struct uvm_object **uobjp, voff_t *uoffsetp, 174 struct file *file) 175 { 176 struct nouveau_drm *const drm = nouveau_drm(dev); 177 178 KASSERT(0 == (offset & (PAGE_SIZE - 1))); 179 180 return ttm_bo_mmap_object(&drm->ttm.bdev, offset, size, prot, 181 uobjp, uoffsetp, file); 182 } 183 184 #else 185 186 int 187 nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma) 188 { 189 struct drm_file *file_priv = filp->private_data; 190 struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev); 191 192 return ttm_bo_mmap(filp, vma, &drm->ttm.bdev); 193 } 194 195 #endif 196 197 static int 198 nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind) 199 { 200 struct nvif_mmu *mmu = &drm->client.mmu; 201 int typei; 202 203 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | 204 kind | NVIF_MEM_COHERENT); 205 if (typei < 0) 206 return -ENOSYS; 207 208 drm->ttm.type_host[!!kind] = typei; 209 210 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | kind); 211 if (typei < 0) 212 return -ENOSYS; 213 214 drm->ttm.type_ncoh[!!kind] = typei; 215 return 0; 216 } 217 218 int 219 nouveau_ttm_init(struct nouveau_drm *drm) 220 { 221 struct nvkm_device *device = nvxx_device(&drm->client.device); 222 struct nvkm_pci *pci = device->pci; 223 struct nvif_mmu *mmu = &drm->client.mmu; 224 struct drm_device *dev = drm->dev; 225 int typei, ret; 226 227 ret = nouveau_ttm_init_host(drm, 0); 228 if (ret) 229 return ret; 230 231 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA && 232 drm->client.device.info.chipset != 0x50) { 233 ret = nouveau_ttm_init_host(drm, NVIF_MEM_KIND); 234 if (ret) 235 return ret; 236 } 237 238 if (drm->client.device.info.platform != NV_DEVICE_INFO_V0_SOC && 239 drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 240 typei = nvif_mmu_type(mmu, NVIF_MEM_VRAM | NVIF_MEM_MAPPABLE | 241 NVIF_MEM_KIND | 242 NVIF_MEM_COMP | 243 NVIF_MEM_DISP); 244 if (typei < 0) 245 return -ENOSYS; 246 247 drm->ttm.type_vram = typei; 248 } else { 249 drm->ttm.type_vram = -1; 250 } 251 252 if (pci && pci->agp.bridge) { 253 drm->agp.bridge = pci->agp.bridge; 254 drm->agp.base = pci->agp.base; 255 drm->agp.size = pci->agp.size; 256 drm->agp.cma = pci->agp.cma; 257 } 258 259 ret = ttm_bo_device_init(&drm->ttm.bdev, 260 &nouveau_bo_driver, 261 #ifdef __NetBSD__ 262 dev->bst, 263 dev->dmat, 264 #else 265 dev->anon_inode->i_mapping, 266 #endif 267 dev->vma_offset_manager, 268 drm->client.mmu.dmabits <= 32 ? true : false); 269 if (ret) { 270 NV_ERROR(drm, "error initialising bo driver, %d\n", ret); 271 return ret; 272 } 273 274 /* VRAM init */ 275 drm->gem.vram_available = drm->client.device.info.ram_user; 276 277 arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1), 278 device->func->resource_size(device, 1)); 279 280 ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM, 281 drm->gem.vram_available >> PAGE_SHIFT); 282 if (ret) { 283 NV_ERROR(drm, "VRAM mm init failed, %d\n", ret); 284 return ret; 285 } 286 287 drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1), 288 device->func->resource_size(device, 1)); 289 290 #ifdef __NetBSD__ 291 pmap_pv_track(device->func->resource_addr(device, 1), 292 device->func->resource_size(device, 1)); 293 #endif 294 295 /* GART init */ 296 if (!drm->agp.bridge) { 297 drm->gem.gart_available = drm->client.vmm.vmm.limit; 298 } else { 299 drm->gem.gart_available = drm->agp.size; 300 } 301 302 ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT, 303 drm->gem.gart_available >> PAGE_SHIFT); 304 if (ret) { 305 NV_ERROR(drm, "GART mm init failed, %d\n", ret); 306 return ret; 307 } 308 309 NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20)); 310 NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20)); 311 return 0; 312 } 313 314 void 315 nouveau_ttm_fini(struct nouveau_drm *drm) 316 { 317 struct nvkm_device *device = nvxx_device(&drm->client.device); 318 319 ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM); 320 ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT); 321 322 ttm_bo_device_release(&drm->ttm.bdev); 323 324 arch_phys_wc_del(drm->ttm.mtrr); 325 drm->ttm.mtrr = 0; 326 327 #ifdef __NetBSD__ 328 pmap_pv_untrack(device->func->resource_addr(device, 1), 329 device->func->resource_size(device, 1)); 330 #endif 331 arch_io_free_memtype_wc(device->func->resource_addr(device, 1), 332 device->func->resource_size(device, 1)); 333 334 } 335