xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/mmu/nouveau_nvkm_subdev_mmu_memnv50.c (revision 7649e88fcfe6a7c92de68bd5e592dec3e35224fb)
1 /*	$NetBSD: nouveau_nvkm_subdev_mmu_memnv50.c,v 1.3 2021/12/19 10:51:58 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2017 Red Hat Inc.
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, sublicense,
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 shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_mmu_memnv50.c,v 1.3 2021/12/19 10:51:58 riastradh Exp $");
26 
27 #include "mem.h"
28 
29 #include <core/memory.h>
30 #include <subdev/bar.h>
31 #include <subdev/fb.h>
32 
33 #include <nvif/class.h>
34 #include <nvif/if500b.h>
35 #include <nvif/if500d.h>
36 #include <nvif/unpack.h>
37 
38 int
39 #ifdef __NetBSD__
nv50_mem_map(struct nvkm_mmu * mmu,struct nvkm_memory * memory,void * argv,u32 argc,bus_space_tag_t * ptag,u64 * paddr,u64 * psize,struct nvkm_vma ** pvma)40 nv50_mem_map(struct nvkm_mmu *mmu, struct nvkm_memory *memory, void *argv,
41 	     u32 argc, bus_space_tag_t *ptag, u64 *paddr, u64 *psize, struct nvkm_vma **pvma)
42 #else
43 nv50_mem_map(struct nvkm_mmu *mmu, struct nvkm_memory *memory, void *argv,
44 	     u32 argc, u64 *paddr, u64 *psize, struct nvkm_vma **pvma)
45 #endif
46 {
47 	struct nv50_vmm_map_v0 uvmm = {};
48 	union {
49 		struct nv50_mem_map_vn vn;
50 		struct nv50_mem_map_v0 v0;
51 	} *args = argv;
52 	struct nvkm_device *device = mmu->subdev.device;
53 	struct nvkm_vmm *bar = nvkm_bar_bar1_vmm(device);
54 	u64 size = nvkm_memory_size(memory);
55 	int ret = -ENOSYS;
56 
57 	if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
58 		uvmm.ro   = args->v0.ro;
59 		uvmm.kind = args->v0.kind;
60 		uvmm.comp = args->v0.comp;
61 	} else
62 	if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
63 	} else
64 		return ret;
65 
66 	ret = nvkm_vmm_get(bar, 12, size, pvma);
67 	if (ret)
68 		return ret;
69 
70 #ifdef __NetBSD__
71 	*ptag = device->func->resource_tag(device, 1);
72 #endif
73 	*paddr = device->func->resource_addr(device, 1) + (*pvma)->addr;
74 	*psize = (*pvma)->size;
75 	return nvkm_memory_map(memory, 0, bar, *pvma, &uvmm, sizeof(uvmm));
76 }
77 
78 int
nv50_mem_new(struct nvkm_mmu * mmu,int type,u8 page,u64 size,void * argv,u32 argc,struct nvkm_memory ** pmemory)79 nv50_mem_new(struct nvkm_mmu *mmu, int type, u8 page, u64 size,
80 	     void *argv, u32 argc, struct nvkm_memory **pmemory)
81 {
82 	union {
83 		struct nv50_mem_vn vn;
84 		struct nv50_mem_v0 v0;
85 	} *args = argv;
86 	int ret = -ENOSYS;
87 	bool contig;
88 
89 	if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
90 		type   = args->v0.bankswz ? 0x02 : 0x01;
91 		contig = args->v0.contig;
92 	} else
93 	if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
94 		type   = 0x01;
95 		contig = false;
96 	} else
97 		return -ENOSYS;
98 
99 	return nvkm_ram_get(mmu->subdev.device, NVKM_RAM_MM_NORMAL, type,
100 			    page, size, contig, false, pmemory);
101 }
102