1 /*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24 #define nv04_instmem(p) container_of((p), struct nv04_instmem, base)
25 #include "priv.h"
26
27 #include <core/ramht.h>
28
29 struct nv04_instmem {
30 struct nvkm_instmem base;
31 struct nvkm_mm heap;
32 };
33
34 /******************************************************************************
35 * instmem object implementation
36 *****************************************************************************/
37 #define nv04_instobj(p) container_of((p), struct nv04_instobj, base.memory)
38
39 struct nv04_instobj {
40 struct nvkm_instobj base;
41 struct nv04_instmem *imem;
42 struct nvkm_mm_node *node;
43 };
44
45 static void
nv04_instobj_wr32(struct nvkm_memory * memory,u64 offset,u32 data)46 nv04_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
47 {
48 struct nv04_instobj *iobj = nv04_instobj(memory);
49 struct nvkm_device *device = iobj->imem->base.subdev.device;
50 nvkm_wr32(device, 0x700000 + iobj->node->offset + offset, data);
51 }
52
53 static u32
nv04_instobj_rd32(struct nvkm_memory * memory,u64 offset)54 nv04_instobj_rd32(struct nvkm_memory *memory, u64 offset)
55 {
56 struct nv04_instobj *iobj = nv04_instobj(memory);
57 struct nvkm_device *device = iobj->imem->base.subdev.device;
58 return nvkm_rd32(device, 0x700000 + iobj->node->offset + offset);
59 }
60
61 static const struct nvkm_memory_ptrs
62 nv04_instobj_ptrs = {
63 .rd32 = nv04_instobj_rd32,
64 .wr32 = nv04_instobj_wr32,
65 };
66
67 static void
nv04_instobj_release(struct nvkm_memory * memory)68 nv04_instobj_release(struct nvkm_memory *memory)
69 {
70 }
71
72 static void __iomem *
nv04_instobj_acquire(struct nvkm_memory * memory)73 nv04_instobj_acquire(struct nvkm_memory *memory)
74 {
75 struct nv04_instobj *iobj = nv04_instobj(memory);
76 struct nvkm_device *device = iobj->imem->base.subdev.device;
77 return device->pri + 0x700000 + iobj->node->offset;
78 }
79
80 static u64
nv04_instobj_size(struct nvkm_memory * memory)81 nv04_instobj_size(struct nvkm_memory *memory)
82 {
83 return nv04_instobj(memory)->node->length;
84 }
85
86 static u64
nv04_instobj_addr(struct nvkm_memory * memory)87 nv04_instobj_addr(struct nvkm_memory *memory)
88 {
89 return nv04_instobj(memory)->node->offset;
90 }
91
92 static enum nvkm_memory_target
nv04_instobj_target(struct nvkm_memory * memory)93 nv04_instobj_target(struct nvkm_memory *memory)
94 {
95 return NVKM_MEM_TARGET_INST;
96 }
97
98 static void *
nv04_instobj_dtor(struct nvkm_memory * memory)99 nv04_instobj_dtor(struct nvkm_memory *memory)
100 {
101 struct nv04_instobj *iobj = nv04_instobj(memory);
102 mutex_lock(&iobj->imem->base.mutex);
103 nvkm_mm_free(&iobj->imem->heap, &iobj->node);
104 mutex_unlock(&iobj->imem->base.mutex);
105 nvkm_instobj_dtor(&iobj->imem->base, &iobj->base);
106 return iobj;
107 }
108
109 static const struct nvkm_memory_func
110 nv04_instobj_func = {
111 .dtor = nv04_instobj_dtor,
112 .target = nv04_instobj_target,
113 .size = nv04_instobj_size,
114 .addr = nv04_instobj_addr,
115 .acquire = nv04_instobj_acquire,
116 .release = nv04_instobj_release,
117 };
118
119 static int
nv04_instobj_new(struct nvkm_instmem * base,u32 size,u32 align,bool zero,struct nvkm_memory ** pmemory)120 nv04_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
121 struct nvkm_memory **pmemory)
122 {
123 struct nv04_instmem *imem = nv04_instmem(base);
124 struct nv04_instobj *iobj;
125 int ret;
126
127 if (!(iobj = kzalloc(sizeof(*iobj), GFP_KERNEL)))
128 return -ENOMEM;
129 *pmemory = &iobj->base.memory;
130
131 nvkm_instobj_ctor(&nv04_instobj_func, &imem->base, &iobj->base);
132 iobj->base.memory.ptrs = &nv04_instobj_ptrs;
133 iobj->imem = imem;
134
135 mutex_lock(&imem->base.mutex);
136 ret = nvkm_mm_head(&imem->heap, 0, 1, size, size, align ? align : 1, &iobj->node);
137 mutex_unlock(&imem->base.mutex);
138 return ret;
139 }
140
141 /******************************************************************************
142 * instmem subdev implementation
143 *****************************************************************************/
144
145 static u32
nv04_instmem_rd32(struct nvkm_instmem * imem,u32 addr)146 nv04_instmem_rd32(struct nvkm_instmem *imem, u32 addr)
147 {
148 return nvkm_rd32(imem->subdev.device, 0x700000 + addr);
149 }
150
151 static void
nv04_instmem_wr32(struct nvkm_instmem * imem,u32 addr,u32 data)152 nv04_instmem_wr32(struct nvkm_instmem *imem, u32 addr, u32 data)
153 {
154 nvkm_wr32(imem->subdev.device, 0x700000 + addr, data);
155 }
156
157 static int
nv04_instmem_oneinit(struct nvkm_instmem * base)158 nv04_instmem_oneinit(struct nvkm_instmem *base)
159 {
160 struct nv04_instmem *imem = nv04_instmem(base);
161 struct nvkm_device *device = imem->base.subdev.device;
162 int ret;
163
164 /* PRAMIN aperture maps over the end of VRAM, reserve it */
165 imem->base.reserved = 512 * 1024;
166
167 ret = nvkm_mm_init(&imem->heap, 0, 0, imem->base.reserved, 1);
168 if (ret)
169 return ret;
170
171 /* 0x00000-0x10000: reserve for probable vbios image */
172 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x10000, 0, false,
173 &imem->base.vbios);
174 if (ret)
175 return ret;
176
177 /* 0x10000-0x18000: reserve for RAMHT */
178 ret = nvkm_ramht_new(device, 0x08000, 0, NULL, &imem->base.ramht);
179 if (ret)
180 return ret;
181
182 /* 0x18000-0x18800: reserve for RAMFC (enough for 32 nv30 channels) */
183 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x00800, 0, true,
184 &imem->base.ramfc);
185 if (ret)
186 return ret;
187
188 /* 0x18800-0x18a00: reserve for RAMRO */
189 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x00200, 0, false,
190 &imem->base.ramro);
191 if (ret)
192 return ret;
193
194 return 0;
195 }
196
197 static void *
nv04_instmem_dtor(struct nvkm_instmem * base)198 nv04_instmem_dtor(struct nvkm_instmem *base)
199 {
200 struct nv04_instmem *imem = nv04_instmem(base);
201 nvkm_memory_unref(&imem->base.ramfc);
202 nvkm_memory_unref(&imem->base.ramro);
203 nvkm_ramht_del(&imem->base.ramht);
204 nvkm_memory_unref(&imem->base.vbios);
205 nvkm_mm_fini(&imem->heap);
206 return imem;
207 }
208
209 static const struct nvkm_instmem_func
210 nv04_instmem = {
211 .dtor = nv04_instmem_dtor,
212 .oneinit = nv04_instmem_oneinit,
213 .rd32 = nv04_instmem_rd32,
214 .wr32 = nv04_instmem_wr32,
215 .memory_new = nv04_instobj_new,
216 .zero = false,
217 };
218
219 int
nv04_instmem_new(struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_instmem ** pimem)220 nv04_instmem_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
221 struct nvkm_instmem **pimem)
222 {
223 struct nv04_instmem *imem;
224
225 if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
226 return -ENOMEM;
227 nvkm_instmem_ctor(&nv04_instmem, device, type, inst, &imem->base);
228 *pimem = &imem->base;
229 return 0;
230 }
231