nouveau_nvkm_subdev_instmem_nv50.c revision 1.5
1/*	$NetBSD: nouveau_nvkm_subdev_instmem_nv50.c,v 1.5 2018/08/27 14:18:18 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_nv50.c,v 1.5 2018/08/27 14:18:18 riastradh Exp $");
28
29#define nv50_instmem(p) container_of((p), struct nv50_instmem, base)
30#include "priv.h"
31
32#include <core/memory.h>
33#include <subdev/bar.h>
34#include <subdev/fb.h>
35#include <subdev/mmu.h>
36
37#ifdef __NetBSD__
38#  define	__iomem	__nvkm_memory_iomem
39#endif
40
41struct nv50_instmem {
42	struct nvkm_instmem base;
43	unsigned long lock_flags;
44	spinlock_t lock;
45	u64 addr;
46};
47
48/******************************************************************************
49 * instmem object implementation
50 *****************************************************************************/
51#define nv50_instobj(p) container_of((p), struct nv50_instobj, memory)
52
53struct nv50_instobj {
54	struct nvkm_memory memory;
55	struct nv50_instmem *imem;
56	struct nvkm_mem *mem;
57	struct nvkm_vma bar;
58#ifdef __NetBSD__
59	bus_space_tag_t bst;
60	bus_space_handle_t bsh;
61#endif
62	void *map;
63};
64
65static enum nvkm_memory_target
66nv50_instobj_target(struct nvkm_memory *memory)
67{
68	return NVKM_MEM_TARGET_VRAM;
69}
70
71static u64
72nv50_instobj_addr(struct nvkm_memory *memory)
73{
74	return nv50_instobj(memory)->mem->offset;
75}
76
77static u64
78nv50_instobj_size(struct nvkm_memory *memory)
79{
80	return (u64)nv50_instobj(memory)->mem->size << NVKM_RAM_MM_SHIFT;
81}
82
83static void
84nv50_instobj_boot(struct nvkm_memory *memory, struct nvkm_vm *vm)
85{
86	struct nv50_instobj *iobj = nv50_instobj(memory);
87	struct nvkm_subdev *subdev = &iobj->imem->base.subdev;
88	struct nvkm_device *device = subdev->device;
89	u64 size = nvkm_memory_size(memory);
90#ifndef __NetBSD__
91	void __iomem *map;
92#endif
93	int ret;
94
95	iobj->map = ERR_PTR(-ENOMEM);
96
97	ret = nvkm_vm_get(vm, size, 12, NV_MEM_ACCESS_RW, &iobj->bar);
98	if (ret == 0) {
99#ifdef __NetBSD__
100		bus_space_tag_t bst = device->func->resource_tag(device, 3);
101		bus_addr_t base = device->func->resource_addr(device, 3);
102		bus_space_handle_t bsh;
103		int ret;
104
105		/* Yes, truncation is really intended here.  */
106		/* XXX errno NetBSD->Linux */
107		ret = -bus_space_map(bst, base + (u32)iobj->bar.offset, size,
108		    BUS_SPACE_MAP_LINEAR, &bsh);
109		if (ret == 0) {
110			nvkm_memory_map(memory, &iobj->bar, 0);
111			iobj->bst = bst;
112			iobj->bsh = bsh;
113			iobj->map = bus_space_vaddr(bst, bsh);
114		} else {
115			nvkm_warn(subdev, "PRAMIN bus_space_map failed: %d\n",
116			    ret);
117			nvkm_vm_put(&iobj->bar);
118		}
119#else
120		map = ioremap(device->func->resource_addr(device, 3) +
121			      (u32)iobj->bar.offset, size);
122		if (map) {
123			nvkm_memory_map(memory, &iobj->bar, 0);
124			iobj->map = map;
125		} else {
126			nvkm_warn(subdev, "PRAMIN ioremap failed\n");
127			nvkm_vm_put(&iobj->bar);
128		}
129#endif
130	} else {
131		nvkm_warn(subdev, "PRAMIN exhausted\n");
132	}
133}
134
135static void
136nv50_instobj_release(struct nvkm_memory *memory)
137{
138	struct nv50_instmem *imem = nv50_instobj(memory)->imem;
139	spin_unlock_irqrestore(&imem->lock, imem->lock_flags);
140}
141
142static void __iomem *
143nv50_instobj_acquire(struct nvkm_memory *memory)
144{
145	struct nv50_instobj *iobj = nv50_instobj(memory);
146	struct nv50_instmem *imem = iobj->imem;
147	struct nvkm_bar *bar = imem->base.subdev.device->bar;
148	struct nvkm_vm *vm;
149	unsigned long flags;
150
151	if (!iobj->map && (vm = nvkm_bar_kmap(bar)))
152		nvkm_memory_boot(memory, vm);
153	if (!IS_ERR_OR_NULL(iobj->map))
154		return iobj->map;
155
156	spin_lock_irqsave(&imem->lock, flags);
157	imem->lock_flags = flags;
158	return NULL;
159}
160
161static u32
162nv50_instobj_rd32(struct nvkm_memory *memory, u64 offset)
163{
164	struct nv50_instobj *iobj = nv50_instobj(memory);
165	struct nv50_instmem *imem = iobj->imem;
166	struct nvkm_device *device = imem->base.subdev.device;
167	u64 base = (iobj->mem->offset + offset) & 0xffffff00000ULL;
168	u64 addr = (iobj->mem->offset + offset) & 0x000000fffffULL;
169	u32 data;
170
171	if (unlikely(imem->addr != base)) {
172		nvkm_wr32(device, 0x001700, base >> 16);
173		imem->addr = base;
174	}
175	data = nvkm_rd32(device, 0x700000 + addr);
176	return data;
177}
178
179static void
180nv50_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
181{
182	struct nv50_instobj *iobj = nv50_instobj(memory);
183	struct nv50_instmem *imem = iobj->imem;
184	struct nvkm_device *device = imem->base.subdev.device;
185	u64 base = (iobj->mem->offset + offset) & 0xffffff00000ULL;
186	u64 addr = (iobj->mem->offset + offset) & 0x000000fffffULL;
187
188	if (unlikely(imem->addr != base)) {
189		nvkm_wr32(device, 0x001700, base >> 16);
190		imem->addr = base;
191	}
192	nvkm_wr32(device, 0x700000 + addr, data);
193}
194
195static void
196nv50_instobj_map(struct nvkm_memory *memory, struct nvkm_vma *vma, u64 offset)
197{
198	struct nv50_instobj *iobj = nv50_instobj(memory);
199	nvkm_vm_map_at(vma, offset, iobj->mem);
200}
201
202static void *
203nv50_instobj_dtor(struct nvkm_memory *memory)
204{
205	struct nv50_instobj *iobj = nv50_instobj(memory);
206	struct nvkm_ram *ram = iobj->imem->base.subdev.device->fb->ram;
207	if (!IS_ERR_OR_NULL(iobj->map)) {
208		nvkm_vm_put(&iobj->bar);
209#ifdef __NetBSD__
210		bus_space_unmap(iobj->bst, iobj->bsh,
211		    nvkm_memory_size(&iobj->memory));
212		iobj->map = NULL;
213#else
214		iounmap(iobj->map);
215#endif
216	}
217	ram->func->put(ram, &iobj->mem);
218	return iobj;
219}
220
221static const struct nvkm_memory_func
222nv50_instobj_func = {
223	.dtor = nv50_instobj_dtor,
224	.target = nv50_instobj_target,
225	.size = nv50_instobj_size,
226	.addr = nv50_instobj_addr,
227	.boot = nv50_instobj_boot,
228	.acquire = nv50_instobj_acquire,
229	.release = nv50_instobj_release,
230	.rd32 = nv50_instobj_rd32,
231	.wr32 = nv50_instobj_wr32,
232	.map = nv50_instobj_map,
233};
234
235static int
236nv50_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
237		 struct nvkm_memory **pmemory)
238{
239	struct nv50_instmem *imem = nv50_instmem(base);
240	struct nv50_instobj *iobj;
241	struct nvkm_ram *ram = imem->base.subdev.device->fb->ram;
242	int ret;
243
244	if (!(iobj = kzalloc(sizeof(*iobj), GFP_KERNEL)))
245		return -ENOMEM;
246	*pmemory = &iobj->memory;
247
248	nvkm_memory_ctor(&nv50_instobj_func, &iobj->memory);
249	iobj->imem = imem;
250
251	size  = max((size  + 4095) & ~4095, (u32)4096);
252	align = max((align + 4095) & ~4095, (u32)4096);
253
254	ret = ram->func->get(ram, size, align, 0, 0x800, &iobj->mem);
255	if (ret)
256		return ret;
257
258	iobj->mem->page_shift = 12;
259	return 0;
260}
261
262/******************************************************************************
263 * instmem subdev implementation
264 *****************************************************************************/
265
266static void
267nv50_instmem_fini(struct nvkm_instmem *base)
268{
269	nv50_instmem(base)->addr = ~0ULL;
270}
271
272static const struct nvkm_instmem_func
273nv50_instmem = {
274	.fini = nv50_instmem_fini,
275	.memory_new = nv50_instobj_new,
276	.persistent = false,
277	.zero = false,
278};
279
280int
281nv50_instmem_new(struct nvkm_device *device, int index,
282		 struct nvkm_instmem **pimem)
283{
284	struct nv50_instmem *imem;
285
286	if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
287		return -ENOMEM;
288	nvkm_instmem_ctor(&nv50_instmem, device, index, &imem->base);
289	spin_lock_init(&imem->lock);
290	*pimem = &imem->base;
291	return 0;
292}
293