1/*	$NetBSD: nouveau_nvkm_engine_device_user.c,v 1.8 2021/12/19 10:51:57 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_engine_device_user.c,v 1.8 2021/12/19 10:51:57 riastradh Exp $");
28
29#define nvkm_udevice(p) container_of((p), struct nvkm_udevice, object)
30#include "priv.h"
31#include "ctrl.h"
32
33#include <core/client.h>
34#include <subdev/fb.h>
35#include <subdev/instmem.h>
36#include <subdev/timer.h>
37
38#include <nvif/class.h>
39#include <nvif/cl0080.h>
40#include <nvif/unpack.h>
41
42struct nvkm_udevice {
43	struct nvkm_object object;
44	struct nvkm_device *device;
45};
46
47static int
48nvkm_udevice_info_subdev(struct nvkm_device *device, u64 mthd, u64 *data)
49{
50	struct nvkm_subdev *subdev;
51	enum nvkm_devidx subidx;
52
53	switch (mthd & NV_DEVICE_INFO_UNIT) {
54	case NV_DEVICE_FIFO(0): subidx = NVKM_ENGINE_FIFO; break;
55	default:
56		return -EINVAL;
57	}
58
59	subdev = nvkm_device_subdev(device, subidx);
60	if (subdev)
61		return nvkm_subdev_info(subdev, mthd, data);
62	return -ENODEV;
63}
64
65static void
66nvkm_udevice_info_v1(struct nvkm_device *device,
67		     struct nv_device_info_v1_data *args)
68{
69	if (args->mthd & NV_DEVICE_INFO_UNIT) {
70		if (nvkm_udevice_info_subdev(device, args->mthd, &args->data))
71			args->mthd = NV_DEVICE_INFO_INVALID;
72		return;
73	}
74
75	switch (args->mthd) {
76#define ENGINE__(A,B,C) NV_DEVICE_INFO_ENGINE_##A: { int _i;                   \
77	for (_i = (B), args->data = 0ULL; _i <= (C); _i++) {                   \
78		if (nvkm_device_engine(device, _i))                            \
79			args->data |= BIT_ULL(_i);                             \
80	}                                                                      \
81}
82#define ENGINE_A(A) ENGINE__(A, NVKM_ENGINE_##A   , NVKM_ENGINE_##A)
83#define ENGINE_B(A) ENGINE__(A, NVKM_ENGINE_##A##0, NVKM_ENGINE_##A##_LAST)
84	case ENGINE_A(SW    ); break;
85	case ENGINE_A(GR    ); break;
86	case ENGINE_A(MPEG  ); break;
87	case ENGINE_A(ME    ); break;
88	case ENGINE_A(CIPHER); break;
89	case ENGINE_A(BSP   ); break;
90	case ENGINE_A(VP    ); break;
91	case ENGINE_B(CE    ); break;
92	case ENGINE_A(SEC   ); break;
93	case ENGINE_A(MSVLD ); break;
94	case ENGINE_A(MSPDEC); break;
95	case ENGINE_A(MSPPP ); break;
96	case ENGINE_A(MSENC ); break;
97	case ENGINE_A(VIC   ); break;
98	case ENGINE_A(SEC2  ); break;
99	case ENGINE_B(NVDEC ); break;
100	case ENGINE_B(NVENC ); break;
101	default:
102		args->mthd = NV_DEVICE_INFO_INVALID;
103		break;
104	}
105}
106
107static int
108nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size)
109{
110	struct nvkm_object *object = &udev->object;
111	struct nvkm_device *device = udev->device;
112	struct nvkm_fb *fb = device->fb;
113	struct nvkm_instmem *imem = device->imem;
114	union {
115		struct nv_device_info_v0 v0;
116		struct nv_device_info_v1 v1;
117	} *args = data;
118	int ret = -ENOSYS, i;
119
120	nvif_ioctl(object, "device info size %d\n", size);
121	if (!(ret = nvif_unpack(ret, &data, &size, args->v1, 1, 1, true))) {
122		nvif_ioctl(object, "device info vers %d count %d\n",
123			   args->v1.version, args->v1.count);
124		if (args->v1.count * sizeof(args->v1.data[0]) == size) {
125			for (i = 0; i < args->v1.count; i++)
126				nvkm_udevice_info_v1(device, &args->v1.data[i]);
127			return 0;
128		}
129		return -EINVAL;
130	} else
131	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
132		nvif_ioctl(object, "device info vers %d\n", args->v0.version);
133	} else
134		return ret;
135
136	switch (device->chipset) {
137	case 0x01a:
138	case 0x01f:
139	case 0x04c:
140	case 0x04e:
141	case 0x063:
142	case 0x067:
143	case 0x068:
144	case 0x0aa:
145	case 0x0ac:
146	case 0x0af:
147		args->v0.platform = NV_DEVICE_INFO_V0_IGP;
148		break;
149	default:
150		switch (device->type) {
151		case NVKM_DEVICE_PCI:
152			args->v0.platform = NV_DEVICE_INFO_V0_PCI;
153			break;
154		case NVKM_DEVICE_AGP:
155			args->v0.platform = NV_DEVICE_INFO_V0_AGP;
156			break;
157		case NVKM_DEVICE_PCIE:
158			args->v0.platform = NV_DEVICE_INFO_V0_PCIE;
159			break;
160		case NVKM_DEVICE_TEGRA:
161			args->v0.platform = NV_DEVICE_INFO_V0_SOC;
162			break;
163		default:
164			WARN_ON(1);
165			break;
166		}
167		break;
168	}
169
170	switch (device->card_type) {
171	case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break;
172	case NV_10:
173	case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break;
174	case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break;
175	case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break;
176	case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break;
177	case NV_50: args->v0.family = NV_DEVICE_INFO_V0_TESLA; break;
178	case NV_C0: args->v0.family = NV_DEVICE_INFO_V0_FERMI; break;
179	case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break;
180	case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break;
181	case GP100: args->v0.family = NV_DEVICE_INFO_V0_PASCAL; break;
182	case GV100: args->v0.family = NV_DEVICE_INFO_V0_VOLTA; break;
183	case TU100: args->v0.family = NV_DEVICE_INFO_V0_TURING; break;
184	default:
185		args->v0.family = 0;
186		break;
187	}
188
189	args->v0.chipset  = device->chipset;
190	args->v0.revision = device->chiprev;
191	if (fb && fb->ram)
192		args->v0.ram_size = args->v0.ram_user = fb->ram->size;
193	else
194		args->v0.ram_size = args->v0.ram_user = 0;
195	if (imem && args->v0.ram_size > 0)
196		args->v0.ram_user = args->v0.ram_user - imem->reserved;
197
198	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
199	strncpy(args->v0.name, device->name, sizeof(args->v0.name));
200	return 0;
201}
202
203static int
204nvkm_udevice_time(struct nvkm_udevice *udev, void *data, u32 size)
205{
206	struct nvkm_object *object = &udev->object;
207	struct nvkm_device *device = udev->device;
208	union {
209		struct nv_device_time_v0 v0;
210	} *args = data;
211	int ret = -ENOSYS;
212
213	nvif_ioctl(object, "device time size %d\n", size);
214	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
215		nvif_ioctl(object, "device time vers %d\n", args->v0.version);
216		args->v0.time = nvkm_timer_read(device->timer);
217	}
218
219	return ret;
220}
221
222static int
223nvkm_udevice_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
224{
225	struct nvkm_udevice *udev = nvkm_udevice(object);
226	nvif_ioctl(object, "device mthd %08x\n", mthd);
227	switch (mthd) {
228	case NV_DEVICE_V0_INFO:
229		return nvkm_udevice_info(udev, data, size);
230	case NV_DEVICE_V0_TIME:
231		return nvkm_udevice_time(udev, data, size);
232	default:
233		break;
234	}
235	return -EINVAL;
236}
237
238static int
239nvkm_udevice_rd08(struct nvkm_object *object, u64 addr, u8 *data)
240{
241	struct nvkm_udevice *udev = nvkm_udevice(object);
242	*data = nvkm_rd08(udev->device, addr);
243	return 0;
244}
245
246static int
247nvkm_udevice_rd16(struct nvkm_object *object, u64 addr, u16 *data)
248{
249	struct nvkm_udevice *udev = nvkm_udevice(object);
250	*data = nvkm_rd16(udev->device, addr);
251	return 0;
252}
253
254static int
255nvkm_udevice_rd32(struct nvkm_object *object, u64 addr, u32 *data)
256{
257	struct nvkm_udevice *udev = nvkm_udevice(object);
258	*data = nvkm_rd32(udev->device, addr);
259	return 0;
260}
261
262static int
263nvkm_udevice_wr08(struct nvkm_object *object, u64 addr, u8 data)
264{
265	struct nvkm_udevice *udev = nvkm_udevice(object);
266	nvkm_wr08(udev->device, addr, data);
267	return 0;
268}
269
270static int
271nvkm_udevice_wr16(struct nvkm_object *object, u64 addr, u16 data)
272{
273	struct nvkm_udevice *udev = nvkm_udevice(object);
274	nvkm_wr16(udev->device, addr, data);
275	return 0;
276}
277
278static int
279nvkm_udevice_wr32(struct nvkm_object *object, u64 addr, u32 data)
280{
281	struct nvkm_udevice *udev = nvkm_udevice(object);
282	nvkm_wr32(udev->device, addr, data);
283	return 0;
284}
285
286static int
287#ifdef __NetBSD__
288nvkm_udevice_map(struct nvkm_object *object, void *argv, u32 argc,
289		 enum nvkm_object_map *type, bus_space_tag_t *tagp,
290		 u64 *addr, u64 *size)
291#else
292nvkm_udevice_map(struct nvkm_object *object, void *argv, u32 argc,
293		 enum nvkm_object_map *type, u64 *addr, u64 *size)
294#endif
295{
296	struct nvkm_udevice *udev = nvkm_udevice(object);
297	struct nvkm_device *device = udev->device;
298	*type = NVKM_OBJECT_MAP_IO;
299#ifdef __NetBSD__
300	*tagp = device->func->resource_tag(device, 0);
301#endif
302	*addr = device->func->resource_addr(device, 0);
303	*size = device->func->resource_size(device, 0);
304	return 0;
305}
306
307static int
308nvkm_udevice_fini(struct nvkm_object *object, bool suspend)
309{
310	struct nvkm_udevice *udev = nvkm_udevice(object);
311	struct nvkm_device *device = udev->device;
312	int ret = 0;
313
314	mutex_lock(&device->mutex);
315	if (!--device->refcount) {
316		ret = nvkm_device_fini(device, suspend);
317		if (ret && suspend) {
318			device->refcount++;
319			goto done;
320		}
321	}
322
323done:
324	mutex_unlock(&device->mutex);
325	return ret;
326}
327
328static int
329nvkm_udevice_init(struct nvkm_object *object)
330{
331	struct nvkm_udevice *udev = nvkm_udevice(object);
332	struct nvkm_device *device = udev->device;
333	int ret = 0;
334
335	mutex_lock(&device->mutex);
336	if (!device->refcount++) {
337		ret = nvkm_device_init(device);
338		if (ret) {
339			device->refcount--;
340			goto done;
341		}
342	}
343
344done:
345	mutex_unlock(&device->mutex);
346	return ret;
347}
348
349static int
350nvkm_udevice_child_new(const struct nvkm_oclass *oclass,
351		       void *data, u32 size, struct nvkm_object **pobject)
352{
353	struct nvkm_udevice *udev = nvkm_udevice(oclass->parent);
354	const struct nvkm_device_oclass *sclass = oclass->priv;
355	return sclass->ctor(udev->device, oclass, data, size, pobject);
356}
357
358static int
359nvkm_udevice_child_get(struct nvkm_object *object, int index,
360		       struct nvkm_oclass *oclass)
361{
362	struct nvkm_udevice *udev = nvkm_udevice(object);
363	struct nvkm_device *device = udev->device;
364	struct nvkm_engine *engine;
365	u64 mask = (1ULL << NVKM_ENGINE_DMAOBJ) |
366		   (1ULL << NVKM_ENGINE_FIFO) |
367		   (1ULL << NVKM_ENGINE_DISP) |
368		   (1ULL << NVKM_ENGINE_PM);
369	const struct nvkm_device_oclass *sclass = NULL;
370	int i;
371
372	for (; mask && !sclass && (i = __ffs64(mask), 1); mask &= ~(1ULL << i)) {
373		if (!(engine = nvkm_device_engine(device, i)) ||
374		    !(engine->func->base.sclass))
375			continue;
376		oclass->engine = engine;
377
378		index -= engine->func->base.sclass(oclass, index, &sclass);
379	}
380
381	if (!sclass) {
382		if (index-- == 0)
383			sclass = &nvkm_control_oclass;
384		else if (device->mmu && index-- == 0)
385			sclass = &device->mmu->user;
386		else if (device->fault && index-- == 0)
387			sclass = &device->fault->user;
388		else
389			return -EINVAL;
390
391		oclass->base = sclass->base;
392	}
393
394	oclass->ctor = nvkm_udevice_child_new;
395	oclass->priv = sclass;
396	return 0;
397}
398
399static const struct nvkm_object_func
400nvkm_udevice_super = {
401	.init = nvkm_udevice_init,
402	.fini = nvkm_udevice_fini,
403	.mthd = nvkm_udevice_mthd,
404	.map = nvkm_udevice_map,
405	.rd08 = nvkm_udevice_rd08,
406	.rd16 = nvkm_udevice_rd16,
407	.rd32 = nvkm_udevice_rd32,
408	.wr08 = nvkm_udevice_wr08,
409	.wr16 = nvkm_udevice_wr16,
410	.wr32 = nvkm_udevice_wr32,
411	.sclass = nvkm_udevice_child_get,
412};
413
414static const struct nvkm_object_func
415nvkm_udevice = {
416	.init = nvkm_udevice_init,
417	.fini = nvkm_udevice_fini,
418	.mthd = nvkm_udevice_mthd,
419	.sclass = nvkm_udevice_child_get,
420};
421
422static int
423nvkm_udevice_new(const struct nvkm_oclass *oclass, void *data, u32 size,
424		 struct nvkm_object **pobject)
425{
426	union {
427		struct nv_device_v0 v0;
428	} *args = data;
429	struct nvkm_client *client = oclass->client;
430	struct nvkm_object *parent = &client->object;
431	const struct nvkm_object_func *func;
432	struct nvkm_udevice *udev;
433	int ret = -ENOSYS;
434
435	nvif_ioctl(parent, "create device size %d\n", size);
436	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
437		nvif_ioctl(parent, "create device v%d device %016"PRIx64"\n",
438			   args->v0.version, args->v0.device);
439	} else
440		return ret;
441
442	/* give priviledged clients register access */
443	if (client->super)
444		func = &nvkm_udevice_super;
445	else
446		func = &nvkm_udevice;
447
448	if (!(udev = kzalloc(sizeof(*udev), GFP_KERNEL)))
449		return -ENOMEM;
450	nvkm_object_ctor(func, oclass, &udev->object);
451	*pobject = &udev->object;
452
453	/* find the device that matches what the client requested */
454	if (args->v0.device != ~0)
455		udev->device = nvkm_device_find(args->v0.device);
456	else
457		udev->device = nvkm_device_find(client->device);
458	if (!udev->device)
459		return -ENODEV;
460
461	return 0;
462}
463
464const struct nvkm_sclass
465nvkm_udevice_sclass = {
466	.oclass = NV_DEVICE,
467	.minver = 0,
468	.maxver = 0,
469	.ctor = nvkm_udevice_new,
470};
471