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 int 46 nouveau_vram_manager_new(struct ttm_resource_manager *man, 47 struct ttm_buffer_object *bo, 48 const struct ttm_place *place, 49 struct ttm_resource **res) 50 { 51 struct nouveau_bo *nvbo = nouveau_bo(bo); 52 struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 53 int ret; 54 55 if (drm->client.device.info.ram_size == 0) 56 return -ENOMEM; 57 58 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res); 59 if (ret) 60 return ret; 61 62 ttm_resource_init(bo, place, *res); 63 64 ret = nouveau_mem_vram(*res, nvbo->contig, nvbo->page); 65 if (ret) { 66 nouveau_mem_del(man, *res); 67 return ret; 68 } 69 70 return 0; 71 } 72 73 const struct ttm_resource_manager_func nouveau_vram_manager = { 74 .alloc = nouveau_vram_manager_new, 75 .free = nouveau_manager_del, 76 }; 77 78 static int 79 nouveau_gart_manager_new(struct ttm_resource_manager *man, 80 struct ttm_buffer_object *bo, 81 const struct ttm_place *place, 82 struct ttm_resource **res) 83 { 84 struct nouveau_bo *nvbo = nouveau_bo(bo); 85 struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 86 int ret; 87 88 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res); 89 if (ret) 90 return ret; 91 92 ttm_resource_init(bo, place, *res); 93 (*res)->start = 0; 94 return 0; 95 } 96 97 const struct ttm_resource_manager_func nouveau_gart_manager = { 98 .alloc = nouveau_gart_manager_new, 99 .free = nouveau_manager_del, 100 }; 101 102 static int 103 nv04_gart_manager_new(struct ttm_resource_manager *man, 104 struct ttm_buffer_object *bo, 105 const struct ttm_place *place, 106 struct ttm_resource **res) 107 { 108 struct nouveau_bo *nvbo = nouveau_bo(bo); 109 struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 110 struct nouveau_mem *mem; 111 int ret; 112 113 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res); 114 if (ret) 115 return ret; 116 117 mem = nouveau_mem(*res); 118 ttm_resource_init(bo, place, *res); 119 ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0, 120 (long)(*res)->num_pages << PAGE_SHIFT, &mem->vma[0]); 121 if (ret) { 122 nouveau_mem_del(man, *res); 123 return ret; 124 } 125 126 (*res)->start = mem->vma[0].addr >> PAGE_SHIFT; 127 return 0; 128 } 129 130 const struct ttm_resource_manager_func nv04_gart_manager = { 131 .alloc = nv04_gart_manager_new, 132 .free = nouveau_manager_del, 133 }; 134 135 static int 136 nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind) 137 { 138 struct nvif_mmu *mmu = &drm->client.mmu; 139 int typei; 140 141 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | 142 kind | NVIF_MEM_COHERENT); 143 if (typei < 0) 144 return -ENOSYS; 145 146 drm->ttm.type_host[!!kind] = typei; 147 148 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | kind); 149 if (typei < 0) 150 return -ENOSYS; 151 152 drm->ttm.type_ncoh[!!kind] = typei; 153 return 0; 154 } 155 156 static int 157 nouveau_ttm_init_vram(struct nouveau_drm *drm) 158 { 159 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 160 struct ttm_resource_manager *man = kzalloc(sizeof(*man), GFP_KERNEL); 161 162 if (!man) 163 return -ENOMEM; 164 165 man->func = &nouveau_vram_manager; 166 167 ttm_resource_manager_init(man, &drm->ttm.bdev, 168 drm->gem.vram_available >> PAGE_SHIFT); 169 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_VRAM, man); 170 ttm_resource_manager_set_used(man, true); 171 return 0; 172 } else { 173 return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_VRAM, false, 174 drm->gem.vram_available >> PAGE_SHIFT); 175 } 176 } 177 178 static void 179 nouveau_ttm_fini_vram(struct nouveau_drm *drm) 180 { 181 struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM); 182 183 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 184 ttm_resource_manager_set_used(man, false); 185 ttm_resource_manager_evict_all(&drm->ttm.bdev, man); 186 ttm_resource_manager_cleanup(man); 187 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_VRAM, NULL); 188 kfree(man); 189 } else 190 ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_VRAM); 191 } 192 193 static int 194 nouveau_ttm_init_gtt(struct nouveau_drm *drm) 195 { 196 struct ttm_resource_manager *man; 197 unsigned long size_pages = drm->gem.gart_available >> PAGE_SHIFT; 198 const struct ttm_resource_manager_func *func = NULL; 199 200 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) 201 func = &nouveau_gart_manager; 202 else if (!drm->agp.bridge) 203 func = &nv04_gart_manager; 204 else 205 return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_TT, true, 206 size_pages); 207 208 man = kzalloc(sizeof(*man), GFP_KERNEL); 209 if (!man) 210 return -ENOMEM; 211 212 man->func = func; 213 man->use_tt = true; 214 ttm_resource_manager_init(man, &drm->ttm.bdev, size_pages); 215 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_TT, man); 216 ttm_resource_manager_set_used(man, true); 217 return 0; 218 } 219 220 static void 221 nouveau_ttm_fini_gtt(struct nouveau_drm *drm) 222 { 223 struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_TT); 224 225 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA && 226 drm->agp.bridge) 227 ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_TT); 228 else { 229 ttm_resource_manager_set_used(man, false); 230 ttm_resource_manager_evict_all(&drm->ttm.bdev, man); 231 ttm_resource_manager_cleanup(man); 232 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_TT, NULL); 233 kfree(man); 234 } 235 } 236 237 int 238 nouveau_ttm_init(struct nouveau_drm *drm) 239 { 240 struct nvkm_device *device = nvxx_device(&drm->client.device); 241 struct nvkm_pci *pci = device->pci; 242 struct nvif_mmu *mmu = &drm->client.mmu; 243 struct drm_device *dev = drm->dev; 244 bool need_swiotlb = false; 245 int typei, ret; 246 247 ret = nouveau_ttm_init_host(drm, 0); 248 if (ret) 249 return ret; 250 251 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA && 252 drm->client.device.info.chipset != 0x50) { 253 ret = nouveau_ttm_init_host(drm, NVIF_MEM_KIND); 254 if (ret) 255 return ret; 256 } 257 258 if (drm->client.device.info.platform != NV_DEVICE_INFO_V0_SOC && 259 drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 260 typei = nvif_mmu_type(mmu, NVIF_MEM_VRAM | NVIF_MEM_MAPPABLE | 261 NVIF_MEM_KIND | 262 NVIF_MEM_COMP | 263 NVIF_MEM_DISP); 264 if (typei < 0) 265 return -ENOSYS; 266 267 drm->ttm.type_vram = typei; 268 } else { 269 drm->ttm.type_vram = -1; 270 } 271 272 if (pci && pci->agp.bridge) { 273 drm->agp.bridge = pci->agp.bridge; 274 drm->agp.base = pci->agp.base; 275 drm->agp.size = pci->agp.size; 276 drm->agp.cma = pci->agp.cma; 277 } 278 279 #if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86) 280 need_swiotlb = is_swiotlb_active(dev->dev); 281 #endif 282 283 ret = ttm_device_init(&drm->ttm.bdev, &nouveau_bo_driver, drm->dev->dev, 284 dev->anon_inode->i_mapping, 285 dev->vma_offset_manager, need_swiotlb, 286 drm->client.mmu.dmabits <= 32); 287 if (ret) { 288 NV_ERROR(drm, "error initialising bo driver, %d\n", ret); 289 return ret; 290 } 291 292 /* VRAM init */ 293 drm->gem.vram_available = drm->client.device.info.ram_user; 294 295 arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1), 296 device->func->resource_size(device, 1)); 297 298 ret = nouveau_ttm_init_vram(drm); 299 if (ret) { 300 NV_ERROR(drm, "VRAM mm init failed, %d\n", ret); 301 return ret; 302 } 303 304 drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1), 305 device->func->resource_size(device, 1)); 306 307 /* GART init */ 308 if (!drm->agp.bridge) { 309 drm->gem.gart_available = drm->client.vmm.vmm.limit; 310 } else { 311 drm->gem.gart_available = drm->agp.size; 312 } 313 314 ret = nouveau_ttm_init_gtt(drm); 315 if (ret) { 316 NV_ERROR(drm, "GART mm init failed, %d\n", ret); 317 return ret; 318 } 319 320 mutex_init(&drm->ttm.io_reserve_mutex); 321 INIT_LIST_HEAD(&drm->ttm.io_reserve_lru); 322 323 NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20)); 324 NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20)); 325 return 0; 326 } 327 328 void 329 nouveau_ttm_fini(struct nouveau_drm *drm) 330 { 331 struct nvkm_device *device = nvxx_device(&drm->client.device); 332 333 nouveau_ttm_fini_vram(drm); 334 nouveau_ttm_fini_gtt(drm); 335 336 ttm_device_fini(&drm->ttm.bdev); 337 338 arch_phys_wc_del(drm->ttm.mtrr); 339 drm->ttm.mtrr = 0; 340 arch_io_free_memtype_wc(device->func->resource_addr(device, 1), 341 device->func->resource_size(device, 1)); 342 343 } 344