1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA,
4 * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA,
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 #include <linux/limits.h>
27 #include <linux/swiotlb.h>
29 #include <drm/ttm/ttm_range_manager.h>
31 #include "nouveau_drv.h"
32 #include "nouveau_gem.h"
33 #include "nouveau_mem.h"
34 #include "nouveau_ttm.h"
36 #include <core/tegra.h>
39 nouveau_manager_del(struct ttm_resource_manager *man,
40 struct ttm_resource *reg)
42 nouveau_mem_del(man, reg);
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)
51 struct nouveau_bo *nvbo = nouveau_bo(bo);
52 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
55 if (drm->client.device.info.ram_size == 0)
58 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
62 ttm_resource_init(bo, place, *res);
64 ret = nouveau_mem_vram(*res, nvbo->contig, nvbo->page);
66 nouveau_mem_del(man, *res);
73 const struct ttm_resource_manager_func nouveau_vram_manager = {
74 .alloc = nouveau_vram_manager_new,
75 .free = nouveau_manager_del,
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)
84 struct nouveau_bo *nvbo = nouveau_bo(bo);
85 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
88 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
92 ttm_resource_init(bo, place, *res);
97 const struct ttm_resource_manager_func nouveau_gart_manager = {
98 .alloc = nouveau_gart_manager_new,
99 .free = nouveau_manager_del,
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)
108 struct nouveau_bo *nvbo = nouveau_bo(bo);
109 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
110 struct nouveau_mem *mem;
113 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
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]);
122 nouveau_mem_del(man, *res);
126 (*res)->start = mem->vma[0].addr >> PAGE_SHIFT;
130 const struct ttm_resource_manager_func nv04_gart_manager = {
131 .alloc = nv04_gart_manager_new,
132 .free = nouveau_manager_del,
136 nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind)
138 struct nvif_mmu *mmu = &drm->client.mmu;
141 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE |
142 kind | NVIF_MEM_COHERENT);
146 drm->ttm.type_host[!!kind] = typei;
148 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | kind);
152 drm->ttm.type_ncoh[!!kind] = typei;
157 nouveau_ttm_init_vram(struct nouveau_drm *drm)
159 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
160 struct ttm_resource_manager *man = kzalloc(sizeof(*man), GFP_KERNEL);
165 man->func = &nouveau_vram_manager;
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);
173 return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_VRAM, false,
174 drm->gem.vram_available >> PAGE_SHIFT);
179 nouveau_ttm_fini_vram(struct nouveau_drm *drm)
181 struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
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);
190 ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_VRAM);
194 nouveau_ttm_init_gtt(struct nouveau_drm *drm)
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;
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;
205 return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_TT, true,
208 man = kzalloc(sizeof(*man), GFP_KERNEL);
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);
221 nouveau_ttm_fini_gtt(struct nouveau_drm *drm)
223 struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_TT);
225 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA &&
227 ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_TT);
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);
238 nouveau_ttm_init(struct nouveau_drm *drm)
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;
247 ret = nouveau_ttm_init_host(drm, 0);
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);
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 |
267 drm->ttm.type_vram = typei;
269 drm->ttm.type_vram = -1;
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;
279 #if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
280 need_swiotlb = is_swiotlb_active(dev->dev);
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);
288 NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
293 drm->gem.vram_available = drm->client.device.info.ram_user;
295 arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1),
296 device->func->resource_size(device, 1));
298 ret = nouveau_ttm_init_vram(drm);
300 NV_ERROR(drm, "VRAM mm init failed, %d\n", ret);
304 drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1),
305 device->func->resource_size(device, 1));
308 if (!drm->agp.bridge) {
309 drm->gem.gart_available = drm->client.vmm.vmm.limit;
311 drm->gem.gart_available = drm->agp.size;
314 ret = nouveau_ttm_init_gtt(drm);
316 NV_ERROR(drm, "GART mm init failed, %d\n", ret);
320 mutex_init(&drm->ttm.io_reserve_mutex);
321 INIT_LIST_HEAD(&drm->ttm.io_reserve_lru);
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));
329 nouveau_ttm_fini(struct nouveau_drm *drm)
331 struct nvkm_device *device = nvxx_device(&drm->client.device);
333 nouveau_ttm_fini_vram(drm);
334 nouveau_ttm_fini_gtt(drm);
336 ttm_device_fini(&drm->ttm.bdev);
338 arch_phys_wc_del(drm->ttm.mtrr);
340 arch_io_free_memtype_wc(device->func->resource_addr(device, 1),
341 device->func->resource_size(device, 1));