1 /* $NetBSD: nouveau_nvkm_subdev_instmem_nv04.c,v 1.4 2021/12/18 23:45:40 riastradh Exp $ */
2
3 /*
4 * Copyright 2012 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 * Authors: Ben Skeggs
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_instmem_nv04.c,v 1.4 2021/12/18 23:45:40 riastradh Exp $");
28
29 #define nv04_instmem(p) container_of((p), struct nv04_instmem, base)
30 #include "priv.h"
31
32 #include <core/ramht.h>
33
34 #ifdef __NetBSD__
35 # define __iomem __nvkm_memory_iomem
36 #endif
37
38 struct nv04_instmem {
39 struct nvkm_instmem base;
40 struct nvkm_mm heap;
41 };
42
43 /******************************************************************************
44 * instmem object implementation
45 *****************************************************************************/
46 #define nv04_instobj(p) container_of((p), struct nv04_instobj, base.memory)
47
48 struct nv04_instobj {
49 struct nvkm_instobj base;
50 struct nv04_instmem *imem;
51 struct nvkm_mm_node *node;
52 };
53
54 static void
nv04_instobj_wr32(struct nvkm_memory * memory,u64 offset,u32 data)55 nv04_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
56 {
57 struct nv04_instobj *iobj = nv04_instobj(memory);
58 struct nvkm_device *device = iobj->imem->base.subdev.device;
59 nvkm_wr32(device, 0x700000 + iobj->node->offset + offset, data);
60 }
61
62 static u32
nv04_instobj_rd32(struct nvkm_memory * memory,u64 offset)63 nv04_instobj_rd32(struct nvkm_memory *memory, u64 offset)
64 {
65 struct nv04_instobj *iobj = nv04_instobj(memory);
66 struct nvkm_device *device = iobj->imem->base.subdev.device;
67 return nvkm_rd32(device, 0x700000 + iobj->node->offset + offset);
68 }
69
70 static const struct nvkm_memory_ptrs
71 nv04_instobj_ptrs = {
72 .rd32 = nv04_instobj_rd32,
73 .wr32 = nv04_instobj_wr32,
74 };
75
76 static void
nv04_instobj_release(struct nvkm_memory * memory)77 nv04_instobj_release(struct nvkm_memory *memory)
78 {
79 }
80
81 static void __iomem *
nv04_instobj_acquire(struct nvkm_memory * memory)82 nv04_instobj_acquire(struct nvkm_memory *memory)
83 {
84 struct nv04_instobj *iobj = nv04_instobj(memory);
85 struct nvkm_device *device = iobj->imem->base.subdev.device;
86 return (char __iomem *)bus_space_vaddr(device->mmiot, device->mmioh) +
87 0x700000 + iobj->node->offset;
88 }
89
90 static u64
nv04_instobj_size(struct nvkm_memory * memory)91 nv04_instobj_size(struct nvkm_memory *memory)
92 {
93 return nv04_instobj(memory)->node->length;
94 }
95
96 static u64
nv04_instobj_addr(struct nvkm_memory * memory)97 nv04_instobj_addr(struct nvkm_memory *memory)
98 {
99 return nv04_instobj(memory)->node->offset;
100 }
101
102 static enum nvkm_memory_target
nv04_instobj_target(struct nvkm_memory * memory)103 nv04_instobj_target(struct nvkm_memory *memory)
104 {
105 return NVKM_MEM_TARGET_INST;
106 }
107
108 static void *
nv04_instobj_dtor(struct nvkm_memory * memory)109 nv04_instobj_dtor(struct nvkm_memory *memory)
110 {
111 struct nv04_instobj *iobj = nv04_instobj(memory);
112 mutex_lock(&iobj->imem->base.subdev.mutex);
113 nvkm_mm_free(&iobj->imem->heap, &iobj->node);
114 mutex_unlock(&iobj->imem->base.subdev.mutex);
115 nvkm_instobj_dtor(&iobj->imem->base, &iobj->base);
116 return iobj;
117 }
118
119 static const struct nvkm_memory_func
120 nv04_instobj_func = {
121 .dtor = nv04_instobj_dtor,
122 .target = nv04_instobj_target,
123 .size = nv04_instobj_size,
124 .addr = nv04_instobj_addr,
125 .acquire = nv04_instobj_acquire,
126 .release = nv04_instobj_release,
127 };
128
129 static int
nv04_instobj_new(struct nvkm_instmem * base,u32 size,u32 align,bool zero,struct nvkm_memory ** pmemory)130 nv04_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
131 struct nvkm_memory **pmemory)
132 {
133 struct nv04_instmem *imem = nv04_instmem(base);
134 struct nv04_instobj *iobj;
135 int ret;
136
137 if (!(iobj = kzalloc(sizeof(*iobj), GFP_KERNEL)))
138 return -ENOMEM;
139 *pmemory = &iobj->base.memory;
140
141 nvkm_instobj_ctor(&nv04_instobj_func, &imem->base, &iobj->base);
142 iobj->base.memory.ptrs = &nv04_instobj_ptrs;
143 iobj->imem = imem;
144
145 mutex_lock(&imem->base.subdev.mutex);
146 ret = nvkm_mm_head(&imem->heap, 0, 1, size, size,
147 align ? align : 1, &iobj->node);
148 mutex_unlock(&imem->base.subdev.mutex);
149 return ret;
150 }
151
152 /******************************************************************************
153 * instmem subdev implementation
154 *****************************************************************************/
155
156 static u32
nv04_instmem_rd32(struct nvkm_instmem * imem,u32 addr)157 nv04_instmem_rd32(struct nvkm_instmem *imem, u32 addr)
158 {
159 return nvkm_rd32(imem->subdev.device, 0x700000 + addr);
160 }
161
162 static void
nv04_instmem_wr32(struct nvkm_instmem * imem,u32 addr,u32 data)163 nv04_instmem_wr32(struct nvkm_instmem *imem, u32 addr, u32 data)
164 {
165 nvkm_wr32(imem->subdev.device, 0x700000 + addr, data);
166 }
167
168 static int
nv04_instmem_oneinit(struct nvkm_instmem * base)169 nv04_instmem_oneinit(struct nvkm_instmem *base)
170 {
171 struct nv04_instmem *imem = nv04_instmem(base);
172 struct nvkm_device *device = imem->base.subdev.device;
173 int ret;
174
175 /* PRAMIN aperture maps over the end of VRAM, reserve it */
176 imem->base.reserved = 512 * 1024;
177
178 ret = nvkm_mm_init(&imem->heap, 0, 0, imem->base.reserved, 1);
179 if (ret)
180 return ret;
181
182 /* 0x00000-0x10000: reserve for probable vbios image */
183 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x10000, 0, false,
184 &imem->base.vbios);
185 if (ret)
186 return ret;
187
188 /* 0x10000-0x18000: reserve for RAMHT */
189 ret = nvkm_ramht_new(device, 0x08000, 0, NULL, &imem->base.ramht);
190 if (ret)
191 return ret;
192
193 /* 0x18000-0x18800: reserve for RAMFC (enough for 32 nv30 channels) */
194 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x00800, 0, true,
195 &imem->base.ramfc);
196 if (ret)
197 return ret;
198
199 /* 0x18800-0x18a00: reserve for RAMRO */
200 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x00200, 0, false,
201 &imem->base.ramro);
202 if (ret)
203 return ret;
204
205 return 0;
206 }
207
208 static void *
nv04_instmem_dtor(struct nvkm_instmem * base)209 nv04_instmem_dtor(struct nvkm_instmem *base)
210 {
211 struct nv04_instmem *imem = nv04_instmem(base);
212 nvkm_memory_unref(&imem->base.ramfc);
213 nvkm_memory_unref(&imem->base.ramro);
214 nvkm_ramht_del(&imem->base.ramht);
215 nvkm_memory_unref(&imem->base.vbios);
216 nvkm_mm_fini(&imem->heap);
217 return imem;
218 }
219
220 static const struct nvkm_instmem_func
221 nv04_instmem = {
222 .dtor = nv04_instmem_dtor,
223 .oneinit = nv04_instmem_oneinit,
224 .rd32 = nv04_instmem_rd32,
225 .wr32 = nv04_instmem_wr32,
226 .memory_new = nv04_instobj_new,
227 .zero = false,
228 };
229
230 int
nv04_instmem_new(struct nvkm_device * device,int index,struct nvkm_instmem ** pimem)231 nv04_instmem_new(struct nvkm_device *device, int index,
232 struct nvkm_instmem **pimem)
233 {
234 struct nv04_instmem *imem;
235
236 if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
237 return -ENOMEM;
238 nvkm_instmem_ctor(&nv04_instmem, device, index, &imem->base);
239 *pimem = &imem->base;
240 return 0;
241 }
242