1/*	$NetBSD: nouveau_nvkm_subdev_top_gk104.c,v 1.2 2021/12/18 23:45:42 riastradh Exp $	*/
2
3/*
4 * Copyright 2016 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 <bskeggs@redhat.com>
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_top_gk104.c,v 1.2 2021/12/18 23:45:42 riastradh Exp $");
28
29#include "priv.h"
30
31static int
32gk104_top_oneinit(struct nvkm_top *top)
33{
34	struct nvkm_subdev *subdev = &top->subdev;
35	struct nvkm_device *device = subdev->device;
36	struct nvkm_top_device *info = NULL;
37	u32 data, type, inst;
38	int i;
39
40	for (i = 0; i < 64; i++) {
41		if (!info) {
42			if (!(info = nvkm_top_device_new(top)))
43				return -ENOMEM;
44			type = ~0;
45			inst = 0;
46		}
47
48		data = nvkm_rd32(device, 0x022700 + (i * 0x04));
49		nvkm_trace(subdev, "%02x: %08x\n", i, data);
50		switch (data & 0x00000003) {
51		case 0x00000000: /* NOT_VALID */
52			continue;
53		case 0x00000001: /* DATA */
54			inst        = (data & 0x3c000000) >> 26;
55			info->addr  = (data & 0x00fff000);
56			if (data & 0x00000004)
57				info->fault = (data & 0x000003f8) >> 3;
58			break;
59		case 0x00000002: /* ENUM */
60			if (data & 0x00000020)
61				info->engine  = (data & 0x3c000000) >> 26;
62			if (data & 0x00000010)
63				info->runlist = (data & 0x01e00000) >> 21;
64			if (data & 0x00000008)
65				info->intr    = (data & 0x000f8000) >> 15;
66			if (data & 0x00000004)
67				info->reset   = (data & 0x00003e00) >> 9;
68			break;
69		case 0x00000003: /* ENGINE_TYPE */
70			type = (data & 0x7ffffffc) >> 2;
71			break;
72		}
73
74		if (data & 0x80000000)
75			continue;
76
77		/* Translate engine type to NVKM engine identifier. */
78#define A_(A) if (inst == 0) info->index = NVKM_ENGINE_##A
79#define B_(A) if (inst + NVKM_ENGINE_##A##0 < NVKM_ENGINE_##A##_LAST + 1)      \
80		info->index = NVKM_ENGINE_##A##0 + inst
81#define C_(A) if (inst == 0) info->index = NVKM_SUBDEV_##A
82		switch (type) {
83		case 0x00000000: A_(GR    ); break;
84		case 0x00000001: A_(CE0   ); break;
85		case 0x00000002: A_(CE1   ); break;
86		case 0x00000003: A_(CE2   ); break;
87		case 0x00000008: A_(MSPDEC); break;
88		case 0x00000009: A_(MSPPP ); break;
89		case 0x0000000a: A_(MSVLD ); break;
90		case 0x0000000b: A_(MSENC ); break;
91		case 0x0000000c: A_(VIC   ); break;
92		case 0x0000000d: A_(SEC2  ); break;
93		case 0x0000000e: B_(NVENC ); break;
94		case 0x0000000f: A_(NVENC1); break;
95		case 0x00000010: B_(NVDEC ); break;
96		case 0x00000013: B_(CE    ); break;
97		case 0x00000014: C_(GSP   ); break;
98			break;
99		default:
100			break;
101		}
102
103		nvkm_debug(subdev, "%02x.%d (%8s): addr %06x fault %2d "
104				   "engine %2d runlist %2d intr %2d "
105				   "reset %2d\n", type, inst,
106			   info->index == NVKM_SUBDEV_NR ? NULL :
107					  nvkm_subdev_name[info->index],
108			   info->addr, info->fault, info->engine, info->runlist,
109			   info->intr, info->reset);
110		info = NULL;
111	}
112
113	return 0;
114}
115
116static const struct nvkm_top_func
117gk104_top = {
118	.oneinit = gk104_top_oneinit,
119};
120
121int
122gk104_top_new(struct nvkm_device *device, int index, struct nvkm_top **ptop)
123{
124	return nvkm_top_new_(&gk104_top, device, index, ptop);
125}
126