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