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