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