xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/top/nouveau_nvkm_subdev_top_base.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_top_base.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_base.c,v 1.2 2021/12/18 23:45:42 riastradh Exp $");
28 
29 #include "priv.h"
30 
31 struct nvkm_top_device *
nvkm_top_device_new(struct nvkm_top * top)32 nvkm_top_device_new(struct nvkm_top *top)
33 {
34 	struct nvkm_top_device *info = kmalloc(sizeof(*info), GFP_KERNEL);
35 	if (info) {
36 		info->index = NVKM_SUBDEV_NR;
37 		info->addr = 0;
38 		info->fault = -1;
39 		info->engine = -1;
40 		info->runlist = -1;
41 		info->reset = -1;
42 		info->intr = -1;
43 		list_add_tail(&info->head, &top->device);
44 	}
45 	return info;
46 }
47 
48 u32
nvkm_top_addr(struct nvkm_device * device,enum nvkm_devidx index)49 nvkm_top_addr(struct nvkm_device *device, enum nvkm_devidx index)
50 {
51 	struct nvkm_top *top = device->top;
52 	struct nvkm_top_device *info;
53 
54 	if (top) {
55 		list_for_each_entry(info, &top->device, head) {
56 			if (info->index == index)
57 				return info->addr;
58 		}
59 	}
60 
61 	return 0;
62 }
63 
64 u32
nvkm_top_reset(struct nvkm_device * device,enum nvkm_devidx index)65 nvkm_top_reset(struct nvkm_device *device, enum nvkm_devidx index)
66 {
67 	struct nvkm_top *top = device->top;
68 	struct nvkm_top_device *info;
69 
70 	if (top) {
71 		list_for_each_entry(info, &top->device, head) {
72 			if (info->index == index && info->reset >= 0)
73 				return BIT(info->reset);
74 		}
75 	}
76 
77 	return 0;
78 }
79 
80 u32
nvkm_top_intr_mask(struct nvkm_device * device,enum nvkm_devidx devidx)81 nvkm_top_intr_mask(struct nvkm_device *device, enum nvkm_devidx devidx)
82 {
83 	struct nvkm_top *top = device->top;
84 	struct nvkm_top_device *info;
85 
86 	if (top) {
87 		list_for_each_entry(info, &top->device, head) {
88 			if (info->index == devidx && info->intr >= 0)
89 				return BIT(info->intr);
90 		}
91 	}
92 
93 	return 0;
94 }
95 
96 u32
nvkm_top_intr(struct nvkm_device * device,u32 intr,u64 * psubdevs)97 nvkm_top_intr(struct nvkm_device *device, u32 intr, u64 *psubdevs)
98 {
99 	struct nvkm_top *top = device->top;
100 	struct nvkm_top_device *info;
101 	u64 subdevs = 0;
102 	u32 handled = 0;
103 
104 	if (top) {
105 		list_for_each_entry(info, &top->device, head) {
106 			if (info->index != NVKM_SUBDEV_NR && info->intr >= 0) {
107 				if (intr & BIT(info->intr)) {
108 					subdevs |= BIT_ULL(info->index);
109 					handled |= BIT(info->intr);
110 				}
111 			}
112 		}
113 	}
114 
115 	*psubdevs = subdevs;
116 	return intr & ~handled;
117 }
118 
119 int
nvkm_top_fault_id(struct nvkm_device * device,enum nvkm_devidx devidx)120 nvkm_top_fault_id(struct nvkm_device *device, enum nvkm_devidx devidx)
121 {
122 	struct nvkm_top *top = device->top;
123 	struct nvkm_top_device *info;
124 
125 	list_for_each_entry(info, &top->device, head) {
126 		if (info->index == devidx && info->fault >= 0)
127 			return info->fault;
128 	}
129 
130 	return -ENOENT;
131 }
132 
133 enum nvkm_devidx
nvkm_top_fault(struct nvkm_device * device,int fault)134 nvkm_top_fault(struct nvkm_device *device, int fault)
135 {
136 	struct nvkm_top *top = device->top;
137 	struct nvkm_top_device *info;
138 
139 	list_for_each_entry(info, &top->device, head) {
140 		if (info->fault == fault)
141 			return info->index;
142 	}
143 
144 	return NVKM_SUBDEV_NR;
145 }
146 
147 enum nvkm_devidx
nvkm_top_engine(struct nvkm_device * device,int index,int * runl,int * engn)148 nvkm_top_engine(struct nvkm_device *device, int index, int *runl, int *engn)
149 {
150 	struct nvkm_top *top = device->top;
151 	struct nvkm_top_device *info;
152 	int n = 0;
153 
154 	list_for_each_entry(info, &top->device, head) {
155 		if (info->engine >= 0 && info->runlist >= 0 && n++ == index) {
156 			*runl = info->runlist;
157 			*engn = info->engine;
158 			return info->index;
159 		}
160 	}
161 
162 	return -ENODEV;
163 }
164 
165 static int
nvkm_top_oneinit(struct nvkm_subdev * subdev)166 nvkm_top_oneinit(struct nvkm_subdev *subdev)
167 {
168 	struct nvkm_top *top = nvkm_top(subdev);
169 	return top->func->oneinit(top);
170 }
171 
172 static void *
nvkm_top_dtor(struct nvkm_subdev * subdev)173 nvkm_top_dtor(struct nvkm_subdev *subdev)
174 {
175 	struct nvkm_top *top = nvkm_top(subdev);
176 	struct nvkm_top_device *info, *temp;
177 
178 	list_for_each_entry_safe(info, temp, &top->device, head) {
179 		list_del(&info->head);
180 		kfree(info);
181 	}
182 
183 	return top;
184 }
185 
186 static const struct nvkm_subdev_func
187 nvkm_top = {
188 	.dtor = nvkm_top_dtor,
189 	.oneinit = nvkm_top_oneinit,
190 };
191 
192 int
nvkm_top_new_(const struct nvkm_top_func * func,struct nvkm_device * device,int index,struct nvkm_top ** ptop)193 nvkm_top_new_(const struct nvkm_top_func *func, struct nvkm_device *device,
194 	      int index, struct nvkm_top **ptop)
195 {
196 	struct nvkm_top *top;
197 	if (!(top = *ptop = kzalloc(sizeof(*top), GFP_KERNEL)))
198 		return -ENOMEM;
199 	nvkm_subdev_ctor(&nvkm_top, device, index, &top->subdev);
200 	top->func = func;
201 	INIT_LIST_HEAD(&top->device);
202 	return 0;
203 }
204