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, struct ttm_resource *reg)
45 nouveau_vram_manager_new(struct ttm_resource_manager *man,
46 struct ttm_buffer_object *bo,
47 const struct ttm_place *place,
48 struct ttm_resource **res)
50 struct nouveau_bo *nvbo = nouveau_bo(bo);
51 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
54 if (drm->client.device.info.ram_size == 0)
57 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
61 ttm_resource_init(bo, place, *res);
63 ret = nouveau_mem_vram(*res, nvbo->contig, nvbo->page);
65 nouveau_mem_del(*res);
72 const struct ttm_resource_manager_func nouveau_vram_manager = {
73 .alloc = nouveau_vram_manager_new,
74 .free = nouveau_manager_del,
78 nouveau_gart_manager_new(struct ttm_resource_manager *man,
79 struct ttm_buffer_object *bo,
80 const struct ttm_place *place,
81 struct ttm_resource **res)
83 struct nouveau_bo *nvbo = nouveau_bo(bo);
84 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
87 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
91 ttm_resource_init(bo, place, *res);
96 const struct ttm_resource_manager_func nouveau_gart_manager = {
97 .alloc = nouveau_gart_manager_new,
98 .free = nouveau_manager_del,
102 nv04_gart_manager_new(struct ttm_resource_manager *man,
103 struct ttm_buffer_object *bo,
104 const struct ttm_place *place,
105 struct ttm_resource **res)
107 struct nouveau_bo *nvbo = nouveau_bo(bo);
108 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
109 struct nouveau_mem *mem;
112 ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
116 mem = nouveau_mem(*res);
117 ttm_resource_init(bo, place, *res);
118 ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0,
119 (long)(*res)->num_pages << PAGE_SHIFT, &mem->vma[0]);
121 nouveau_mem_del(*res);
125 (*res)->start = mem->vma[0].addr >> PAGE_SHIFT;
129 const struct ttm_resource_manager_func nv04_gart_manager = {
130 .alloc = nv04_gart_manager_new,
131 .free = nouveau_manager_del,
135 nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind)
137 struct nvif_mmu *mmu = &drm->client.mmu;
140 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE |
141 kind | NVIF_MEM_COHERENT);
145 drm->ttm.type_host[!!kind] = typei;
147 typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | kind);
151 drm->ttm.type_ncoh[!!kind] = typei;
156 nouveau_ttm_init_vram(struct nouveau_drm *drm)
158 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
159 struct ttm_resource_manager *man = kzalloc(sizeof(*man), GFP_KERNEL);
164 man->func = &nouveau_vram_manager;
166 ttm_resource_manager_init(man,
167 drm->gem.vram_available >> PAGE_SHIFT);
168 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_VRAM, man);
169 ttm_resource_manager_set_used(man, true);
172 return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_VRAM, false,
173 drm->gem.vram_available >> PAGE_SHIFT);
178 nouveau_ttm_fini_vram(struct nouveau_drm *drm)
180 struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
182 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
183 ttm_resource_manager_set_used(man, false);
184 ttm_resource_manager_evict_all(&drm->ttm.bdev, man);
185 ttm_resource_manager_cleanup(man);
186 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_VRAM, NULL);
189 ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_VRAM);
193 nouveau_ttm_init_gtt(struct nouveau_drm *drm)
195 struct ttm_resource_manager *man;
196 unsigned long size_pages = drm->gem.gart_available >> PAGE_SHIFT;
197 const struct ttm_resource_manager_func *func = NULL;
199 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
200 func = &nouveau_gart_manager;
201 else if (!drm->agp.bridge)
202 func = &nv04_gart_manager;
204 return ttm_range_man_init(&drm->ttm.bdev, TTM_PL_TT, true,
207 man = kzalloc(sizeof(*man), GFP_KERNEL);
213 ttm_resource_manager_init(man, size_pages);
214 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_TT, man);
215 ttm_resource_manager_set_used(man, true);
220 nouveau_ttm_fini_gtt(struct nouveau_drm *drm)
222 struct ttm_resource_manager *man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_TT);
224 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA &&
226 ttm_range_man_fini(&drm->ttm.bdev, TTM_PL_TT);
228 ttm_resource_manager_set_used(man, false);
229 ttm_resource_manager_evict_all(&drm->ttm.bdev, man);
230 ttm_resource_manager_cleanup(man);
231 ttm_set_driver_manager(&drm->ttm.bdev, TTM_PL_TT, NULL);
237 nouveau_ttm_init(struct nouveau_drm *drm)
239 struct nvkm_device *device = nvxx_device(&drm->client.device);
240 struct nvkm_pci *pci = device->pci;
241 struct nvif_mmu *mmu = &drm->client.mmu;
242 struct drm_device *dev = drm->dev;
243 bool need_swiotlb = false;
246 ret = nouveau_ttm_init_host(drm, 0);
250 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
251 drm->client.device.info.chipset != 0x50) {
252 ret = nouveau_ttm_init_host(drm, NVIF_MEM_KIND);
257 if (drm->client.device.info.platform != NV_DEVICE_INFO_V0_SOC &&
258 drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
259 typei = nvif_mmu_type(mmu, NVIF_MEM_VRAM | NVIF_MEM_MAPPABLE |
266 drm->ttm.type_vram = typei;
268 drm->ttm.type_vram = -1;
271 if (pci && pci->agp.bridge) {
272 drm->agp.bridge = pci->agp.bridge;
273 drm->agp.base = pci->agp.base;
274 drm->agp.size = pci->agp.size;
275 drm->agp.cma = pci->agp.cma;
278 #if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
279 need_swiotlb = is_swiotlb_active();
282 ret = ttm_device_init(&drm->ttm.bdev, &nouveau_bo_driver, drm->dev->dev,
283 dev->anon_inode->i_mapping,
284 dev->vma_offset_manager, need_swiotlb,
285 drm->client.mmu.dmabits <= 32);
287 NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
292 drm->gem.vram_available = drm->client.device.info.ram_user;
294 arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1),
295 device->func->resource_size(device, 1));
297 ret = nouveau_ttm_init_vram(drm);
299 NV_ERROR(drm, "VRAM mm init failed, %d\n", ret);
303 drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1),
304 device->func->resource_size(device, 1));
307 if (!drm->agp.bridge) {
308 drm->gem.gart_available = drm->client.vmm.vmm.limit;
310 drm->gem.gart_available = drm->agp.size;
313 ret = nouveau_ttm_init_gtt(drm);
315 NV_ERROR(drm, "GART mm init failed, %d\n", ret);
319 mutex_init(&drm->ttm.io_reserve_mutex);
320 INIT_LIST_HEAD(&drm->ttm.io_reserve_lru);
322 NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20));
323 NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20));
328 nouveau_ttm_fini(struct nouveau_drm *drm)
330 struct nvkm_device *device = nvxx_device(&drm->client.device);
332 nouveau_ttm_fini_vram(drm);
333 nouveau_ttm_fini_gtt(drm);
335 ttm_device_fini(&drm->ttm.bdev);
337 arch_phys_wc_del(drm->ttm.mtrr);
339 arch_io_free_memtype_wc(device->func->resource_addr(device, 1),
340 device->func->resource_size(device, 1));