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
nvkm_udevice_info_subdev(struct nvkm_device * device,u64 mthd,u64 * data)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
nvkm_udevice_info_v1(struct nvkm_device * device,struct nv_device_info_v1_data * args)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
nvkm_udevice_info(struct nvkm_udevice * udev,void * data,u32 size)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
nvkm_udevice_time(struct nvkm_udevice * udev,void * data,u32 size)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
nvkm_udevice_mthd(struct nvkm_object * object,u32 mthd,void * data,u32 size)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
nvkm_udevice_map(struct nvkm_object * object,void * argv,u32 argc,enum nvkm_object_map * type,u64 * addr,u64 * size)206 nvkm_udevice_map(struct nvkm_object *object, void *argv, u32 argc,
207 		 enum nvkm_object_map *type, u64 *addr, u64 *size)
208 {
209 	struct nvkm_udevice *udev = nvkm_udevice(object);
210 	struct nvkm_device *device = udev->device;
211 	*type = NVKM_OBJECT_MAP_IO;
212 	*addr = device->func->resource_addr(device, 0);
213 	*size = device->func->resource_size(device, 0);
214 	return 0;
215 }
216 
217 static int
nvkm_udevice_fini(struct nvkm_object * object,bool suspend)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
nvkm_udevice_init(struct nvkm_object * object)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
nvkm_udevice_child_new(const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)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
nvkm_udevice_child_get(struct nvkm_object * object,int index,struct nvkm_oclass * oclass)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 	const struct nvkm_device_oclass *sclass = NULL;
279 	int i;
280 
281 	for (; i = __ffs64(mask), mask && !sclass; mask &= ~(1ULL << i)) {
282 		if (!(engine = nvkm_device_engine(device, i, 0)) ||
283 		    !(engine->func->base.sclass))
284 			continue;
285 		oclass->engine = engine;
286 
287 		index -= engine->func->base.sclass(oclass, index, &sclass);
288 	}
289 
290 	if (!sclass) {
291 		if (index-- == 0)
292 			sclass = &nvkm_control_oclass;
293 		else if (device->mmu && index-- == 0)
294 			sclass = &device->mmu->user;
295 		else if (device->fault && index-- == 0)
296 			sclass = &device->fault->user;
297 		else if (device->vfn && index-- == 0)
298 			sclass = &device->vfn->user;
299 		else
300 			return -EINVAL;
301 
302 		oclass->base = sclass->base;
303 		oclass->engine = NULL;
304 	}
305 
306 	oclass->ctor = nvkm_udevice_child_new;
307 	oclass->priv = sclass;
308 	return 0;
309 }
310 
311 static const struct nvkm_object_func
312 nvkm_udevice = {
313 	.init = nvkm_udevice_init,
314 	.fini = nvkm_udevice_fini,
315 	.mthd = nvkm_udevice_mthd,
316 	.map = nvkm_udevice_map,
317 	.sclass = nvkm_udevice_child_get,
318 };
319 
320 static int
nvkm_udevice_new(const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)321 nvkm_udevice_new(const struct nvkm_oclass *oclass, void *data, u32 size,
322 		 struct nvkm_object **pobject)
323 {
324 	struct nvkm_client *client = oclass->client;
325 	struct nvkm_udevice *udev;
326 
327 	if (!(udev = kzalloc(sizeof(*udev), GFP_KERNEL)))
328 		return -ENOMEM;
329 	nvkm_object_ctor(&nvkm_udevice, oclass, &udev->object);
330 	*pobject = &udev->object;
331 
332 	/* find the device that matches what the client requested */
333 	udev->device = nvkm_device_find(client->device);
334 	if (!udev->device)
335 		return -ENODEV;
336 
337 	return 0;
338 }
339 
340 const struct nvkm_sclass
341 nvkm_udevice_sclass = {
342 	.oclass = NV_DEVICE,
343 	.minver = 0,
344 	.maxver = 0,
345 	.ctor = nvkm_udevice_new,
346 };
347