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