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