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  */
23 
24 #include <core/object.h>
25 #include <core/client.h>
26 #include <core/device.h>
27 #include <core/class.h>
28 #include <core/mm.h>
29 
30 #include <subdev/fb.h>
31 #include <subdev/timer.h>
32 #include <subdev/instmem.h>
33 #include <engine/graph.h>
34 
35 #include "nouveau_drm.h"
36 #include "nouveau_dma.h"
37 #include "nouveau_gem.h"
38 #include "nouveau_chan.h"
39 #include "nouveau_abi16.h"
40 
41 struct nouveau_abi16 *
42 nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
43 {
44 	struct nouveau_cli *cli = nouveau_cli(file_priv);
45 	mutex_lock(&cli->mutex);
46 	if (!cli->abi16) {
47 		struct nouveau_abi16 *abi16;
48 		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
49 		if (cli->abi16) {
50 			INIT_LIST_HEAD(&abi16->channels);
51 			abi16->client = nv_object(cli);
52 
53 			/* allocate device object targeting client's default
54 			 * device (ie. the one that belongs to the fd it
55 			 * opened)
56 			 */
57 			if (nouveau_object_new(abi16->client, NVDRM_CLIENT,
58 					       NVDRM_DEVICE, 0x0080,
59 					       &(struct nv_device_class) {
60 						.device = ~0ULL,
61 					       },
62 					       sizeof(struct nv_device_class),
63 					       &abi16->device) == 0)
64 				return cli->abi16;
65 
66 			kfree(cli->abi16);
67 			cli->abi16 = NULL;
68 		}
69 
70 		mutex_unlock(&cli->mutex);
71 	}
72 	return cli->abi16;
73 }
74 
75 int
76 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
77 {
78 	struct nouveau_cli *cli = (void *)abi16->client;
79 	mutex_unlock(&cli->mutex);
80 	return ret;
81 }
82 
83 u16
84 nouveau_abi16_swclass(struct nouveau_drm *drm)
85 {
86 	switch (nv_device(drm->device)->card_type) {
87 	case NV_04:
88 		return 0x006e;
89 	case NV_10:
90 	case NV_20:
91 	case NV_30:
92 	case NV_40:
93 		return 0x016e;
94 	case NV_50:
95 		return 0x506e;
96 	case NV_C0:
97 	case NV_D0:
98 	case NV_E0:
99 		return 0x906e;
100 	}
101 
102 	return 0x0000;
103 }
104 
105 static void
106 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
107 			struct nouveau_abi16_ntfy *ntfy)
108 {
109 	nouveau_mm_free(&chan->heap, &ntfy->node);
110 	list_del(&ntfy->head);
111 	kfree(ntfy);
112 }
113 
114 static void
115 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
116 			struct nouveau_abi16_chan *chan)
117 {
118 	struct nouveau_abi16_ntfy *ntfy, *temp;
119 
120 	/* wait for all activity to stop before releasing notify object, which
121 	 * may be still in use */
122 	if (chan->chan && chan->ntfy)
123 		nouveau_channel_idle(chan->chan);
124 
125 	/* cleanup notifier state */
126 	list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
127 		nouveau_abi16_ntfy_fini(chan, ntfy);
128 	}
129 
130 	if (chan->ntfy) {
131 		nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
132 		nouveau_bo_unpin(chan->ntfy);
133 		drm_gem_object_unreference_unlocked(chan->ntfy->gem);
134 	}
135 
136 	if (chan->heap.block_size)
137 		nouveau_mm_fini(&chan->heap);
138 
139 	/* destroy channel object, all children will be killed too */
140 	if (chan->chan) {
141 		abi16->handles &= ~(1 << (chan->chan->handle & 0xffff));
142 		nouveau_channel_del(&chan->chan);
143 	}
144 
145 	list_del(&chan->head);
146 	kfree(chan);
147 }
148 
149 void
150 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
151 {
152 	struct nouveau_cli *cli = (void *)abi16->client;
153 	struct nouveau_abi16_chan *chan, *temp;
154 
155 	/* cleanup channels */
156 	list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
157 		nouveau_abi16_chan_fini(abi16, chan);
158 	}
159 
160 	/* destroy the device object */
161 	nouveau_object_del(abi16->client, NVDRM_CLIENT, NVDRM_DEVICE);
162 
163 	kfree(cli->abi16);
164 	cli->abi16 = NULL;
165 }
166 
167 int
168 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
169 {
170 	struct nouveau_drm *drm = nouveau_drm(dev);
171 	struct nouveau_device *device = nv_device(drm->device);
172 	struct nouveau_timer *ptimer = nouveau_timer(device);
173 	struct nouveau_graph *graph = (void *)nouveau_engine(device, NVDEV_ENGINE_GR);
174 	struct drm_nouveau_getparam *getparam = data;
175 
176 	switch (getparam->param) {
177 	case NOUVEAU_GETPARAM_CHIPSET_ID:
178 		getparam->value = device->chipset;
179 		break;
180 	case NOUVEAU_GETPARAM_PCI_VENDOR:
181 		getparam->value = dev->pci_vendor;
182 		break;
183 	case NOUVEAU_GETPARAM_PCI_DEVICE:
184 		getparam->value = dev->pci_device;
185 		break;
186 	case NOUVEAU_GETPARAM_BUS_TYPE:
187 		if (drm_pci_device_is_agp(dev))
188 			getparam->value = 0;
189 		else
190 		if (!pci_is_pcie(dev->pdev))
191 			getparam->value = 1;
192 		else
193 			getparam->value = 2;
194 		break;
195 	case NOUVEAU_GETPARAM_FB_SIZE:
196 		getparam->value = drm->gem.vram_available;
197 		break;
198 	case NOUVEAU_GETPARAM_AGP_SIZE:
199 		getparam->value = drm->gem.gart_available;
200 		break;
201 	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
202 		getparam->value = 0; /* deprecated */
203 		break;
204 	case NOUVEAU_GETPARAM_PTIMER_TIME:
205 		getparam->value = ptimer->read(ptimer);
206 		break;
207 	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
208 		getparam->value = 1;
209 		break;
210 	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
211 		getparam->value = 1;
212 		break;
213 	case NOUVEAU_GETPARAM_GRAPH_UNITS:
214 		getparam->value = graph->units ? graph->units(graph) : 0;
215 		break;
216 	default:
217 		nv_debug(device, "unknown parameter %lld\n", getparam->param);
218 		return -EINVAL;
219 	}
220 
221 	return 0;
222 }
223 
224 int
225 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
226 {
227 	return -EINVAL;
228 }
229 
230 int
231 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
232 {
233 	struct drm_nouveau_channel_alloc *init = data;
234 	struct nouveau_cli *cli = nouveau_cli(file_priv);
235 	struct nouveau_drm *drm = nouveau_drm(dev);
236 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
237 	struct nouveau_abi16_chan *chan;
238 	struct nouveau_client *client;
239 	struct nouveau_device *device;
240 	struct nouveau_instmem *imem;
241 	struct nouveau_fb *pfb;
242 	int ret;
243 
244 	if (unlikely(!abi16))
245 		return -ENOMEM;
246 
247 	if (!drm->channel)
248 		return nouveau_abi16_put(abi16, -ENODEV);
249 
250 	client = nv_client(abi16->client);
251 	device = nv_device(abi16->device);
252 	imem   = nouveau_instmem(device);
253 	pfb    = nouveau_fb(device);
254 
255 	/* hack to allow channel engine type specification on kepler */
256 	if (device->card_type >= NV_E0) {
257 		if (init->fb_ctxdma_handle != ~0)
258 			init->fb_ctxdma_handle = NVE0_CHANNEL_IND_ENGINE_GR;
259 		else
260 			init->fb_ctxdma_handle = init->tt_ctxdma_handle;
261 
262 		/* allow flips to be executed if this is a graphics channel */
263 		init->tt_ctxdma_handle = 0;
264 		if (init->fb_ctxdma_handle == NVE0_CHANNEL_IND_ENGINE_GR)
265 			init->tt_ctxdma_handle = 1;
266 	}
267 
268 	if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
269 		return nouveau_abi16_put(abi16, -EINVAL);
270 
271 	/* allocate "abi16 channel" data and make up a handle for it */
272 	init->channel = ffsll(~abi16->handles);
273 	if (!init->channel--)
274 		return nouveau_abi16_put(abi16, -ENOSPC);
275 
276 	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
277 	if (!chan)
278 		return nouveau_abi16_put(abi16, -ENOMEM);
279 
280 	INIT_LIST_HEAD(&chan->notifiers);
281 	list_add(&chan->head, &abi16->channels);
282 	abi16->handles |= (1 << init->channel);
283 
284 	/* create channel object and initialise dma and fence management */
285 	ret = nouveau_channel_new(drm, cli, NVDRM_DEVICE, NVDRM_CHAN |
286 				  init->channel, init->fb_ctxdma_handle,
287 				  init->tt_ctxdma_handle, &chan->chan);
288 	if (ret)
289 		goto done;
290 
291 	if (device->card_type >= NV_50)
292 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
293 					NOUVEAU_GEM_DOMAIN_GART;
294 	else
295 	if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
296 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
297 	else
298 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
299 
300 	if (device->card_type < NV_C0) {
301 		init->subchan[0].handle = 0x00000000;
302 		init->subchan[0].grclass = 0x0000;
303 		init->subchan[1].handle = NvSw;
304 		init->subchan[1].grclass = 0x506e;
305 		init->nr_subchan = 2;
306 	}
307 
308 	/* Named memory object area */
309 	ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
310 			      0, 0, &chan->ntfy);
311 	if (ret == 0)
312 		ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT);
313 	if (ret)
314 		goto done;
315 
316 	if (device->card_type >= NV_50) {
317 		ret = nouveau_bo_vma_add(chan->ntfy, client->vm,
318 					&chan->ntfy_vma);
319 		if (ret)
320 			goto done;
321 	}
322 
323 	ret = drm_gem_handle_create(file_priv, chan->ntfy->gem,
324 				    &init->notifier_handle);
325 	if (ret)
326 		goto done;
327 
328 	ret = nouveau_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
329 done:
330 	if (ret)
331 		nouveau_abi16_chan_fini(abi16, chan);
332 	return nouveau_abi16_put(abi16, ret);
333 }
334 
335 
336 int
337 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
338 {
339 	struct drm_nouveau_channel_free *req = data;
340 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
341 	struct nouveau_abi16_chan *chan;
342 	int ret = -ENOENT;
343 
344 	if (unlikely(!abi16))
345 		return -ENOMEM;
346 
347 	list_for_each_entry(chan, &abi16->channels, head) {
348 		if (chan->chan->handle == (NVDRM_CHAN | req->channel)) {
349 			nouveau_abi16_chan_fini(abi16, chan);
350 			return nouveau_abi16_put(abi16, 0);
351 		}
352 	}
353 
354 	return nouveau_abi16_put(abi16, ret);
355 }
356 
357 int
358 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
359 {
360 	struct drm_nouveau_grobj_alloc *init = data;
361 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
362 	struct nouveau_drm *drm = nouveau_drm(dev);
363 	struct nouveau_object *object;
364 	int ret;
365 
366 	if (unlikely(!abi16))
367 		return -ENOMEM;
368 
369 	if (init->handle == ~0)
370 		return nouveau_abi16_put(abi16, -EINVAL);
371 
372 	/* compatibility with userspace that assumes 506e for all chipsets */
373 	if (init->class == 0x506e) {
374 		init->class = nouveau_abi16_swclass(drm);
375 		if (init->class == 0x906e)
376 			return nouveau_abi16_put(abi16, 0);
377 	}
378 
379 	ret = nouveau_object_new(abi16->client, NVDRM_CHAN | init->channel,
380 				  init->handle, init->class, NULL, 0, &object);
381 	return nouveau_abi16_put(abi16, ret);
382 }
383 
384 int
385 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
386 {
387 	struct drm_nouveau_notifierobj_alloc *info = data;
388 	struct nouveau_drm *drm = nouveau_drm(dev);
389 	struct nouveau_device *device = nv_device(drm->device);
390 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
391 	struct nouveau_abi16_chan *chan = NULL, *temp;
392 	struct nouveau_abi16_ntfy *ntfy;
393 	struct nouveau_object *object;
394 	struct nv_dma_class args = {};
395 	int ret;
396 
397 	if (unlikely(!abi16))
398 		return -ENOMEM;
399 
400 	/* completely unnecessary for these chipsets... */
401 	if (unlikely(nv_device(abi16->device)->card_type >= NV_C0))
402 		return nouveau_abi16_put(abi16, -EINVAL);
403 
404 	list_for_each_entry(temp, &abi16->channels, head) {
405 		if (temp->chan->handle == (NVDRM_CHAN | info->channel)) {
406 			chan = temp;
407 			break;
408 		}
409 	}
410 
411 	if (!chan)
412 		return nouveau_abi16_put(abi16, -ENOENT);
413 
414 	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
415 	if (!ntfy)
416 		return nouveau_abi16_put(abi16, -ENOMEM);
417 
418 	list_add(&ntfy->head, &chan->notifiers);
419 	ntfy->handle = info->handle;
420 
421 	ret = nouveau_mm_head(&chan->heap, 1, info->size, info->size, 1,
422 			      &ntfy->node);
423 	if (ret)
424 		goto done;
425 
426 	args.start = ntfy->node->offset;
427 	args.limit = ntfy->node->offset + ntfy->node->length - 1;
428 	if (device->card_type >= NV_50) {
429 		args.flags  = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
430 		args.start += chan->ntfy_vma.offset;
431 		args.limit += chan->ntfy_vma.offset;
432 	} else
433 	if (drm->agp.stat == ENABLED) {
434 		args.flags  = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
435 		args.start += drm->agp.base + chan->ntfy->bo.offset;
436 		args.limit += drm->agp.base + chan->ntfy->bo.offset;
437 	} else {
438 		args.flags  = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
439 		args.start += chan->ntfy->bo.offset;
440 		args.limit += chan->ntfy->bo.offset;
441 	}
442 
443 	ret = nouveau_object_new(abi16->client, chan->chan->handle,
444 				 ntfy->handle, 0x003d, &args,
445 				 sizeof(args), &object);
446 	if (ret)
447 		goto done;
448 
449 done:
450 	if (ret)
451 		nouveau_abi16_ntfy_fini(chan, ntfy);
452 	return nouveau_abi16_put(abi16, ret);
453 }
454 
455 int
456 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
457 {
458 	struct drm_nouveau_gpuobj_free *fini = data;
459 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
460 	struct nouveau_abi16_chan *chan = NULL, *temp;
461 	struct nouveau_abi16_ntfy *ntfy;
462 	int ret;
463 
464 	if (unlikely(!abi16))
465 		return -ENOMEM;
466 
467 	list_for_each_entry(temp, &abi16->channels, head) {
468 		if (temp->chan->handle == (NVDRM_CHAN | fini->channel)) {
469 			chan = temp;
470 			break;
471 		}
472 	}
473 
474 	if (!chan)
475 		return nouveau_abi16_put(abi16, -ENOENT);
476 
477 	/* synchronize with the user channel and destroy the gpu object */
478 	nouveau_channel_idle(chan->chan);
479 
480 	ret = nouveau_object_del(abi16->client, chan->chan->handle, fini->handle);
481 	if (ret)
482 		return nouveau_abi16_put(abi16, ret);
483 
484 	/* cleanup extra state if this object was a notifier */
485 	list_for_each_entry(ntfy, &chan->notifiers, head) {
486 		if (ntfy->handle == fini->handle) {
487 			nouveau_mm_free(&chan->heap, &ntfy->node);
488 			list_del(&ntfy->head);
489 			break;
490 		}
491 	}
492 
493 	return nouveau_abi16_put(abi16, 0);
494 }
495