Home | History | Annotate | Line # | Download | only in nouveau
nouveau_ttm.c revision 1.9
      1 /*	$NetBSD: nouveau_ttm.c,v 1.9 2021/12/19 10:51:56 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.9 2021/12/19 10:51:56 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 	if (__predict_false((offset >> PAGE_SHIFT) < DRM_FILE_PAGE_OFFSET))
    181 		return drm_legacy_mmap_object(dev, offset, size, prot, uobjp,
    182 		    uoffsetp, file);
    183 	else
    184 		return ttm_bo_mmap_object(&drm->ttm.bdev, offset, size, prot,
    185 		    uobjp, uoffsetp, file);
    186 }
    187 
    188 #else
    189 
    190 int
    191 nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
    192 {
    193 	struct drm_file *file_priv = filp->private_data;
    194 	struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev);
    195 
    196 	return ttm_bo_mmap(filp, vma, &drm->ttm.bdev);
    197 }
    198 
    199 #endif
    200 
    201 static int
    202 nouveau_ttm_init_host(struct nouveau_drm *drm, u8 kind)
    203 {
    204 	struct nvif_mmu *mmu = &drm->client.mmu;
    205 	int typei;
    206 
    207 	typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE |
    208 					    kind | NVIF_MEM_COHERENT);
    209 	if (typei < 0)
    210 		return -ENOSYS;
    211 
    212 	drm->ttm.type_host[!!kind] = typei;
    213 
    214 	typei = nvif_mmu_type(mmu, NVIF_MEM_HOST | NVIF_MEM_MAPPABLE | kind);
    215 	if (typei < 0)
    216 		return -ENOSYS;
    217 
    218 	drm->ttm.type_ncoh[!!kind] = typei;
    219 	return 0;
    220 }
    221 
    222 int
    223 nouveau_ttm_init(struct nouveau_drm *drm)
    224 {
    225 	struct nvkm_device *device = nvxx_device(&drm->client.device);
    226 	struct nvkm_pci *pci = device->pci;
    227 	struct nvif_mmu *mmu = &drm->client.mmu;
    228 	struct drm_device *dev = drm->dev;
    229 	int typei, ret;
    230 
    231 	ret = nouveau_ttm_init_host(drm, 0);
    232 	if (ret)
    233 		return ret;
    234 
    235 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
    236 	    drm->client.device.info.chipset != 0x50) {
    237 		ret = nouveau_ttm_init_host(drm, NVIF_MEM_KIND);
    238 		if (ret)
    239 			return ret;
    240 	}
    241 
    242 	if (drm->client.device.info.platform != NV_DEVICE_INFO_V0_SOC &&
    243 	    drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
    244 		typei = nvif_mmu_type(mmu, NVIF_MEM_VRAM | NVIF_MEM_MAPPABLE |
    245 					   NVIF_MEM_KIND |
    246 					   NVIF_MEM_COMP |
    247 					   NVIF_MEM_DISP);
    248 		if (typei < 0)
    249 			return -ENOSYS;
    250 
    251 		drm->ttm.type_vram = typei;
    252 	} else {
    253 		drm->ttm.type_vram = -1;
    254 	}
    255 
    256 	if (pci && pci->agp.bridge) {
    257 		drm->agp.bridge = pci->agp.bridge;
    258 		drm->agp.base = pci->agp.base;
    259 		drm->agp.size = pci->agp.size;
    260 		drm->agp.cma = pci->agp.cma;
    261 	}
    262 
    263 	ret = ttm_bo_device_init(&drm->ttm.bdev,
    264 				  &nouveau_bo_driver,
    265 #ifdef __NetBSD__
    266 				  dev->bst,
    267 				  dev->dmat,
    268 #else
    269 				  dev->anon_inode->i_mapping,
    270 #endif
    271 				  dev->vma_offset_manager,
    272 				  drm->client.mmu.dmabits <= 32 ? true : false);
    273 	if (ret) {
    274 		NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
    275 		return ret;
    276 	}
    277 
    278 	/* VRAM init */
    279 	drm->gem.vram_available = drm->client.device.info.ram_user;
    280 
    281 	arch_io_reserve_memtype_wc(device->func->resource_addr(device, 1),
    282 				   device->func->resource_size(device, 1));
    283 
    284 	ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM,
    285 			      drm->gem.vram_available >> PAGE_SHIFT);
    286 	if (ret) {
    287 		NV_ERROR(drm, "VRAM mm init failed, %d\n", ret);
    288 		return ret;
    289 	}
    290 
    291 	drm->ttm.mtrr = arch_phys_wc_add(device->func->resource_addr(device, 1),
    292 					 device->func->resource_size(device, 1));
    293 
    294 #ifdef __NetBSD__
    295 	pmap_pv_track(device->func->resource_addr(device, 1),
    296 	    device->func->resource_size(device, 1));
    297 #endif
    298 
    299 	/* GART init */
    300 	if (!drm->agp.bridge) {
    301 		drm->gem.gart_available = drm->client.vmm.vmm.limit;
    302 	} else {
    303 		drm->gem.gart_available = drm->agp.size;
    304 	}
    305 
    306 	ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT,
    307 			      drm->gem.gart_available >> PAGE_SHIFT);
    308 	if (ret) {
    309 		NV_ERROR(drm, "GART mm init failed, %d\n", ret);
    310 		return ret;
    311 	}
    312 
    313 	NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20));
    314 	NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20));
    315 	return 0;
    316 }
    317 
    318 void
    319 nouveau_ttm_fini(struct nouveau_drm *drm)
    320 {
    321 	struct nvkm_device *device = nvxx_device(&drm->client.device);
    322 
    323 	ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM);
    324 	ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT);
    325 
    326 	ttm_bo_device_release(&drm->ttm.bdev);
    327 
    328 	arch_phys_wc_del(drm->ttm.mtrr);
    329 	drm->ttm.mtrr = 0;
    330 
    331 #ifdef __NetBSD__
    332 	pmap_pv_untrack(device->func->resource_addr(device, 1),
    333 	    device->func->resource_size(device, 1));
    334 #endif
    335 	arch_io_free_memtype_wc(device->func->resource_addr(device, 1),
    336 				device->func->resource_size(device, 1));
    337 
    338 }
    339