1 /* $NetBSD: nouveau_nvkm_subdev_mmu_memgf100.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_memgf100.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/if900b.h>
35 #include <nvif/if900d.h>
36 #include <nvif/unpack.h>
37
38 int
39 #ifdef __NetBSD__
gf100_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 gf100_mem_map(struct nvkm_mmu *mmu, struct nvkm_memory *memory, void *argv,
41 u32 argc, bus_space_tag_t *ptag, u64 *paddr, u64 *psize,
42 struct nvkm_vma **pvma)
43 #else
44 gf100_mem_map(struct nvkm_mmu *mmu, struct nvkm_memory *memory, void *argv,
45 u32 argc, u64 *paddr, u64 *psize, struct nvkm_vma **pvma)
46 #endif
47 {
48 struct gf100_vmm_map_v0 uvmm = {};
49 union {
50 struct gf100_mem_map_vn vn;
51 struct gf100_mem_map_v0 v0;
52 } *args = argv;
53 struct nvkm_device *device = mmu->subdev.device;
54 struct nvkm_vmm *bar = nvkm_bar_bar1_vmm(device);
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 } else
61 if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
62 } else
63 return ret;
64
65 ret = nvkm_vmm_get(bar, nvkm_memory_page(memory),
66 nvkm_memory_size(memory), pvma);
67 if (ret)
68 return ret;
69
70 ret = nvkm_memory_map(memory, 0, bar, *pvma, &uvmm, sizeof(uvmm));
71 if (ret)
72 return ret;
73
74 #ifdef __NetBSD__
75 *ptag = device->func->resource_tag(device, 1);
76 #endif
77 *paddr = device->func->resource_addr(device, 1) + (*pvma)->addr;
78 *psize = (*pvma)->size;
79 return 0;
80 }
81
82 int
gf100_mem_new(struct nvkm_mmu * mmu,int type,u8 page,u64 size,void * argv,u32 argc,struct nvkm_memory ** pmemory)83 gf100_mem_new(struct nvkm_mmu *mmu, int type, u8 page, u64 size,
84 void *argv, u32 argc, struct nvkm_memory **pmemory)
85 {
86 union {
87 struct gf100_mem_vn vn;
88 struct gf100_mem_v0 v0;
89 } *args = argv;
90 int ret = -ENOSYS;
91 bool contig;
92
93 if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
94 contig = args->v0.contig;
95 } else
96 if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
97 contig = false;
98 } else
99 return ret;
100
101 if (mmu->type[type].type & (NVKM_MEM_DISP | NVKM_MEM_COMP))
102 type = NVKM_RAM_MM_NORMAL;
103 else
104 type = NVKM_RAM_MM_MIXED;
105
106 return nvkm_ram_get(mmu->subdev.device, type, 0x01, page,
107 size, contig, false, pmemory);
108 }
109