xref: /openbmc/linux/drivers/gpu/drm/nouveau/nouveau_abi16.c (revision 6446c1ca2dd2ee5485cd27b1ee4b5fb70f83aacb)
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 <nvif/client.h>
25 #include <nvif/driver.h>
26 #include <nvif/fifo.h>
27 #include <nvif/ioctl.h>
28 #include <nvif/class.h>
29 #include <nvif/cl0002.h>
30 #include <nvif/unpack.h>
31 
32 #include "nouveau_drv.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_exec.h"
35 #include "nouveau_gem.h"
36 #include "nouveau_chan.h"
37 #include "nouveau_abi16.h"
38 #include "nouveau_vmm.h"
39 #include "nouveau_sched.h"
40 
41 static struct nouveau_abi16 *
42 nouveau_abi16(struct drm_file *file_priv)
43 {
44 	struct nouveau_cli *cli = nouveau_cli(file_priv);
45 	if (!cli->abi16) {
46 		struct nouveau_abi16 *abi16;
47 		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
48 		if (cli->abi16) {
49 			struct nv_device_v0 args = {
50 				.device = ~0ULL,
51 			};
52 
53 			INIT_LIST_HEAD(&abi16->channels);
54 
55 			/* allocate device object targeting client's default
56 			 * device (ie. the one that belongs to the fd it
57 			 * opened)
58 			 */
59 			if (nvif_device_ctor(&cli->base.object, "abi16Device",
60 					     0, NV_DEVICE, &args, sizeof(args),
61 					     &abi16->device) == 0)
62 				return cli->abi16;
63 
64 			kfree(cli->abi16);
65 			cli->abi16 = NULL;
66 		}
67 	}
68 	return cli->abi16;
69 }
70 
71 struct nouveau_abi16 *
72 nouveau_abi16_get(struct drm_file *file_priv)
73 {
74 	struct nouveau_cli *cli = nouveau_cli(file_priv);
75 	mutex_lock(&cli->mutex);
76 	if (nouveau_abi16(file_priv))
77 		return cli->abi16;
78 	mutex_unlock(&cli->mutex);
79 	return NULL;
80 }
81 
82 int
83 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
84 {
85 	struct nouveau_cli *cli = (void *)abi16->device.object.client;
86 	mutex_unlock(&cli->mutex);
87 	return ret;
88 }
89 
90 s32
91 nouveau_abi16_swclass(struct nouveau_drm *drm)
92 {
93 	switch (drm->client.device.info.family) {
94 	case NV_DEVICE_INFO_V0_TNT:
95 		return NVIF_CLASS_SW_NV04;
96 	case NV_DEVICE_INFO_V0_CELSIUS:
97 	case NV_DEVICE_INFO_V0_KELVIN:
98 	case NV_DEVICE_INFO_V0_RANKINE:
99 	case NV_DEVICE_INFO_V0_CURIE:
100 		return NVIF_CLASS_SW_NV10;
101 	case NV_DEVICE_INFO_V0_TESLA:
102 		return NVIF_CLASS_SW_NV50;
103 	case NV_DEVICE_INFO_V0_FERMI:
104 	case NV_DEVICE_INFO_V0_KEPLER:
105 	case NV_DEVICE_INFO_V0_MAXWELL:
106 	case NV_DEVICE_INFO_V0_PASCAL:
107 	case NV_DEVICE_INFO_V0_VOLTA:
108 		return NVIF_CLASS_SW_GF100;
109 	}
110 
111 	return 0x0000;
112 }
113 
114 static void
115 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
116 			struct nouveau_abi16_ntfy *ntfy)
117 {
118 	nvif_object_dtor(&ntfy->object);
119 	nvkm_mm_free(&chan->heap, &ntfy->node);
120 	list_del(&ntfy->head);
121 	kfree(ntfy);
122 }
123 
124 static void
125 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
126 			struct nouveau_abi16_chan *chan)
127 {
128 	struct nouveau_abi16_ntfy *ntfy, *temp;
129 
130 	/* When a client exits without waiting for it's queued up jobs to
131 	 * finish it might happen that we fault the channel. This is due to
132 	 * drm_file_free() calling drm_gem_release() before the postclose()
133 	 * callback. Hence, we can't tear down this scheduler entity before
134 	 * uvmm mappings are unmapped. Currently, we can't detect this case.
135 	 *
136 	 * However, this should be rare and harmless, since the channel isn't
137 	 * needed anymore.
138 	 */
139 	nouveau_sched_entity_fini(&chan->sched_entity);
140 
141 	/* wait for all activity to stop before cleaning up */
142 	if (chan->chan)
143 		nouveau_channel_idle(chan->chan);
144 
145 	/* cleanup notifier state */
146 	list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
147 		nouveau_abi16_ntfy_fini(chan, ntfy);
148 	}
149 
150 	if (chan->ntfy) {
151 		nouveau_vma_del(&chan->ntfy_vma);
152 		nouveau_bo_unpin(chan->ntfy);
153 		drm_gem_object_put(&chan->ntfy->bo.base);
154 	}
155 
156 	if (chan->heap.block_size)
157 		nvkm_mm_fini(&chan->heap);
158 
159 	/* destroy channel object, all children will be killed too */
160 	if (chan->chan) {
161 		nvif_object_dtor(&chan->ce);
162 		nouveau_channel_del(&chan->chan);
163 	}
164 
165 	list_del(&chan->head);
166 	kfree(chan);
167 }
168 
169 void
170 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
171 {
172 	struct nouveau_cli *cli = (void *)abi16->device.object.client;
173 	struct nouveau_abi16_chan *chan, *temp;
174 
175 	/* cleanup channels */
176 	list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
177 		nouveau_abi16_chan_fini(abi16, chan);
178 	}
179 
180 	/* destroy the device object */
181 	nvif_device_dtor(&abi16->device);
182 
183 	kfree(cli->abi16);
184 	cli->abi16 = NULL;
185 }
186 
187 static inline int
188 getparam_dma_ib_max(struct nvif_device *device)
189 {
190 	const struct nvif_mclass dmas[] = {
191 		{ NV03_CHANNEL_DMA, 0 },
192 		{ NV10_CHANNEL_DMA, 0 },
193 		{ NV17_CHANNEL_DMA, 0 },
194 		{ NV40_CHANNEL_DMA, 0 },
195 		{}
196 	};
197 
198 	return nvif_mclass(&device->object, dmas) < 0 ? NV50_DMA_IB_MAX : 0;
199 }
200 
201 int
202 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
203 {
204 	struct nouveau_cli *cli = nouveau_cli(file_priv);
205 	struct nouveau_drm *drm = nouveau_drm(dev);
206 	struct nvif_device *device = &drm->client.device;
207 	struct nvkm_device *nvkm_device = nvxx_device(&drm->client.device);
208 	struct nvkm_gr *gr = nvxx_gr(device);
209 	struct drm_nouveau_getparam *getparam = data;
210 	struct pci_dev *pdev = to_pci_dev(dev->dev);
211 
212 	switch (getparam->param) {
213 	case NOUVEAU_GETPARAM_CHIPSET_ID:
214 		getparam->value = device->info.chipset;
215 		break;
216 	case NOUVEAU_GETPARAM_PCI_VENDOR:
217 		if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
218 			getparam->value = pdev->vendor;
219 		else
220 			getparam->value = 0;
221 		break;
222 	case NOUVEAU_GETPARAM_PCI_DEVICE:
223 		if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
224 			getparam->value = pdev->device;
225 		else
226 			getparam->value = 0;
227 		break;
228 	case NOUVEAU_GETPARAM_BUS_TYPE:
229 		switch (device->info.platform) {
230 		case NV_DEVICE_INFO_V0_AGP : getparam->value = 0; break;
231 		case NV_DEVICE_INFO_V0_PCI : getparam->value = 1; break;
232 		case NV_DEVICE_INFO_V0_PCIE: getparam->value = 2; break;
233 		case NV_DEVICE_INFO_V0_SOC : getparam->value = 3; break;
234 		case NV_DEVICE_INFO_V0_IGP :
235 			if (!pci_is_pcie(pdev))
236 				getparam->value = 1;
237 			else
238 				getparam->value = 2;
239 			break;
240 		default:
241 			WARN_ON(1);
242 			break;
243 		}
244 		break;
245 	case NOUVEAU_GETPARAM_FB_SIZE:
246 		getparam->value = drm->gem.vram_available;
247 		break;
248 	case NOUVEAU_GETPARAM_AGP_SIZE:
249 		getparam->value = drm->gem.gart_available;
250 		break;
251 	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
252 		getparam->value = 0; /* deprecated */
253 		break;
254 	case NOUVEAU_GETPARAM_PTIMER_TIME:
255 		getparam->value = nvif_device_time(device);
256 		break;
257 	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
258 		getparam->value = 1;
259 		break;
260 	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
261 		getparam->value = 1;
262 		break;
263 	case NOUVEAU_GETPARAM_GRAPH_UNITS:
264 		getparam->value = nvkm_gr_units(gr);
265 		break;
266 	case NOUVEAU_GETPARAM_EXEC_PUSH_MAX: {
267 		int ib_max = getparam_dma_ib_max(device);
268 
269 		getparam->value = nouveau_exec_push_max_from_ib_max(ib_max);
270 		break;
271 	}
272 	case NOUVEAU_GETPARAM_VRAM_BAR_SIZE:
273 		getparam->value = nvkm_device->func->resource_size(nvkm_device, 1);
274 		break;
275 	case NOUVEAU_GETPARAM_VRAM_USED: {
276 		struct ttm_resource_manager *vram_mgr = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
277 		getparam->value = (u64)ttm_resource_manager_usage(vram_mgr) << PAGE_SHIFT;
278 		break;
279 	}
280 	default:
281 		NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
282 		return -EINVAL;
283 	}
284 
285 	return 0;
286 }
287 
288 int
289 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
290 {
291 	struct drm_nouveau_channel_alloc *init = data;
292 	struct nouveau_cli *cli = nouveau_cli(file_priv);
293 	struct nouveau_drm *drm = nouveau_drm(dev);
294 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
295 	struct nouveau_abi16_chan *chan;
296 	struct nvif_device *device;
297 	u64 engine, runm;
298 	int ret;
299 
300 	if (unlikely(!abi16))
301 		return -ENOMEM;
302 
303 	if (!drm->channel)
304 		return nouveau_abi16_put(abi16, -ENODEV);
305 
306 	/* If uvmm wasn't initialized until now disable it completely to prevent
307 	 * userspace from mixing up UAPIs.
308 	 *
309 	 * The client lock is already acquired by nouveau_abi16_get().
310 	 */
311 	__nouveau_cli_disable_uvmm_noinit(cli);
312 
313 	device = &abi16->device;
314 	engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR;
315 
316 	/* hack to allow channel engine type specification on kepler */
317 	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
318 		if (init->fb_ctxdma_handle == ~0) {
319 			switch (init->tt_ctxdma_handle) {
320 			case 0x01: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR    ; break;
321 			case 0x02: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPDEC; break;
322 			case 0x04: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPPP ; break;
323 			case 0x08: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSVLD ; break;
324 			case 0x30: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_CE    ; break;
325 			default:
326 				return nouveau_abi16_put(abi16, -ENOSYS);
327 			}
328 
329 			init->fb_ctxdma_handle = 0;
330 			init->tt_ctxdma_handle = 0;
331 		}
332 	}
333 
334 	if (engine != NV_DEVICE_HOST_RUNLIST_ENGINES_CE)
335 		runm = nvif_fifo_runlist(device, engine);
336 	else
337 		runm = nvif_fifo_runlist_ce(device);
338 
339 	if (!runm || init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
340 		return nouveau_abi16_put(abi16, -EINVAL);
341 
342 	/* allocate "abi16 channel" data and make up a handle for it */
343 	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
344 	if (!chan)
345 		return nouveau_abi16_put(abi16, -ENOMEM);
346 
347 	INIT_LIST_HEAD(&chan->notifiers);
348 	list_add(&chan->head, &abi16->channels);
349 
350 	/* create channel object and initialise dma and fence management */
351 	ret = nouveau_channel_new(drm, device, false, runm, init->fb_ctxdma_handle,
352 				  init->tt_ctxdma_handle, &chan->chan);
353 	if (ret)
354 		goto done;
355 
356 	ret = nouveau_sched_entity_init(&chan->sched_entity, &drm->sched,
357 					drm->sched_wq);
358 	if (ret)
359 		goto done;
360 
361 	init->channel = chan->chan->chid;
362 
363 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
364 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
365 					NOUVEAU_GEM_DOMAIN_GART;
366 	else
367 	if (chan->chan->push.buffer->bo.resource->mem_type == TTM_PL_VRAM)
368 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
369 	else
370 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
371 
372 	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
373 		init->subchan[0].handle = 0x00000000;
374 		init->subchan[0].grclass = 0x0000;
375 		init->subchan[1].handle = chan->chan->nvsw.handle;
376 		init->subchan[1].grclass = 0x506e;
377 		init->nr_subchan = 2;
378 	}
379 
380 	/* Workaround "nvc0" gallium driver using classes it doesn't allocate on
381 	 * Kepler and above.  NVKM no longer always sets CE_CTX_VALID as part of
382 	 * channel init, now we know what that stuff actually is.
383 	 *
384 	 * Doesn't matter for Kepler/Pascal, CE context stored in NV_RAMIN.
385 	 *
386 	 * Userspace was fixed prior to adding Ampere support.
387 	 */
388 	switch (device->info.family) {
389 	case NV_DEVICE_INFO_V0_VOLTA:
390 		ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, VOLTA_DMA_COPY_A,
391 				       NULL, 0, &chan->ce);
392 		if (ret)
393 			goto done;
394 		break;
395 	case NV_DEVICE_INFO_V0_TURING:
396 		ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, TURING_DMA_COPY_A,
397 				       NULL, 0, &chan->ce);
398 		if (ret)
399 			goto done;
400 		break;
401 	default:
402 		break;
403 	}
404 
405 	/* Named memory object area */
406 	ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
407 			      0, 0, &chan->ntfy);
408 	if (ret == 0)
409 		ret = nouveau_bo_pin(chan->ntfy, NOUVEAU_GEM_DOMAIN_GART,
410 				     false);
411 	if (ret)
412 		goto done;
413 
414 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
415 		ret = nouveau_vma_new(chan->ntfy, chan->chan->vmm,
416 				      &chan->ntfy_vma);
417 		if (ret)
418 			goto done;
419 	}
420 
421 	ret = drm_gem_handle_create(file_priv, &chan->ntfy->bo.base,
422 				    &init->notifier_handle);
423 	if (ret)
424 		goto done;
425 
426 	ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1);
427 done:
428 	if (ret)
429 		nouveau_abi16_chan_fini(abi16, chan);
430 	return nouveau_abi16_put(abi16, ret);
431 }
432 
433 static struct nouveau_abi16_chan *
434 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
435 {
436 	struct nouveau_abi16_chan *chan;
437 
438 	list_for_each_entry(chan, &abi16->channels, head) {
439 		if (chan->chan->chid == channel)
440 			return chan;
441 	}
442 
443 	return NULL;
444 }
445 
446 int
447 nouveau_abi16_usif(struct drm_file *file_priv, void *data, u32 size)
448 {
449 	union {
450 		struct nvif_ioctl_v0 v0;
451 	} *args = data;
452 	struct nouveau_abi16_chan *chan;
453 	struct nouveau_abi16 *abi16;
454 	int ret = -ENOSYS;
455 
456 	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
457 		switch (args->v0.type) {
458 		case NVIF_IOCTL_V0_NEW:
459 		case NVIF_IOCTL_V0_MTHD:
460 		case NVIF_IOCTL_V0_SCLASS:
461 			break;
462 		default:
463 			return -EACCES;
464 		}
465 	} else
466 		return ret;
467 
468 	if (!(abi16 = nouveau_abi16(file_priv)))
469 		return -ENOMEM;
470 
471 	if (args->v0.token != ~0ULL) {
472 		if (!(chan = nouveau_abi16_chan(abi16, args->v0.token)))
473 			return -EINVAL;
474 		args->v0.object = nvif_handle(&chan->chan->user);
475 		args->v0.owner  = NVIF_IOCTL_V0_OWNER_ANY;
476 		return 0;
477 	}
478 
479 	args->v0.object = nvif_handle(&abi16->device.object);
480 	args->v0.owner  = NVIF_IOCTL_V0_OWNER_ANY;
481 	return 0;
482 }
483 
484 int
485 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
486 {
487 	struct drm_nouveau_channel_free *req = data;
488 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
489 	struct nouveau_abi16_chan *chan;
490 
491 	if (unlikely(!abi16))
492 		return -ENOMEM;
493 
494 	chan = nouveau_abi16_chan(abi16, req->channel);
495 	if (!chan)
496 		return nouveau_abi16_put(abi16, -ENOENT);
497 	nouveau_abi16_chan_fini(abi16, chan);
498 	return nouveau_abi16_put(abi16, 0);
499 }
500 
501 int
502 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
503 {
504 	struct drm_nouveau_grobj_alloc *init = data;
505 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
506 	struct nouveau_abi16_chan *chan;
507 	struct nouveau_abi16_ntfy *ntfy;
508 	struct nvif_client *client;
509 	struct nvif_sclass *sclass;
510 	s32 oclass = 0;
511 	int ret, i;
512 
513 	if (unlikely(!abi16))
514 		return -ENOMEM;
515 
516 	if (init->handle == ~0)
517 		return nouveau_abi16_put(abi16, -EINVAL);
518 	client = abi16->device.object.client;
519 
520 	chan = nouveau_abi16_chan(abi16, init->channel);
521 	if (!chan)
522 		return nouveau_abi16_put(abi16, -ENOENT);
523 
524 	ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
525 	if (ret < 0)
526 		return nouveau_abi16_put(abi16, ret);
527 
528 	if ((init->class & 0x00ff) == 0x006e) {
529 		/* nvsw: compatibility with older 0x*6e class identifier */
530 		for (i = 0; !oclass && i < ret; i++) {
531 			switch (sclass[i].oclass) {
532 			case NVIF_CLASS_SW_NV04:
533 			case NVIF_CLASS_SW_NV10:
534 			case NVIF_CLASS_SW_NV50:
535 			case NVIF_CLASS_SW_GF100:
536 				oclass = sclass[i].oclass;
537 				break;
538 			default:
539 				break;
540 			}
541 		}
542 	} else
543 	if ((init->class & 0x00ff) == 0x00b1) {
544 		/* msvld: compatibility with incorrect version exposure */
545 		for (i = 0; i < ret; i++) {
546 			if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
547 				oclass = sclass[i].oclass;
548 				break;
549 			}
550 		}
551 	} else
552 	if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
553 		/* mspdec: compatibility with incorrect version exposure */
554 		for (i = 0; i < ret; i++) {
555 			if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
556 				oclass = sclass[i].oclass;
557 				break;
558 			}
559 		}
560 	} else
561 	if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
562 		/* msppp: compatibility with incorrect version exposure */
563 		for (i = 0; i < ret; i++) {
564 			if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
565 				oclass = sclass[i].oclass;
566 				break;
567 			}
568 		}
569 	} else {
570 		oclass = init->class;
571 	}
572 
573 	nvif_object_sclass_put(&sclass);
574 	if (!oclass)
575 		return nouveau_abi16_put(abi16, -EINVAL);
576 
577 	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
578 	if (!ntfy)
579 		return nouveau_abi16_put(abi16, -ENOMEM);
580 
581 	list_add(&ntfy->head, &chan->notifiers);
582 
583 	client->route = NVDRM_OBJECT_ABI16;
584 	ret = nvif_object_ctor(&chan->chan->user, "abi16EngObj", init->handle,
585 			       oclass, NULL, 0, &ntfy->object);
586 	client->route = NVDRM_OBJECT_NVIF;
587 
588 	if (ret)
589 		nouveau_abi16_ntfy_fini(chan, ntfy);
590 	return nouveau_abi16_put(abi16, ret);
591 }
592 
593 int
594 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
595 {
596 	struct drm_nouveau_notifierobj_alloc *info = data;
597 	struct nouveau_drm *drm = nouveau_drm(dev);
598 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
599 	struct nouveau_abi16_chan *chan;
600 	struct nouveau_abi16_ntfy *ntfy;
601 	struct nvif_device *device = &abi16->device;
602 	struct nvif_client *client;
603 	struct nv_dma_v0 args = {};
604 	int ret;
605 
606 	if (unlikely(!abi16))
607 		return -ENOMEM;
608 
609 	/* completely unnecessary for these chipsets... */
610 	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
611 		return nouveau_abi16_put(abi16, -EINVAL);
612 	client = abi16->device.object.client;
613 
614 	chan = nouveau_abi16_chan(abi16, info->channel);
615 	if (!chan)
616 		return nouveau_abi16_put(abi16, -ENOENT);
617 
618 	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
619 	if (!ntfy)
620 		return nouveau_abi16_put(abi16, -ENOMEM);
621 
622 	list_add(&ntfy->head, &chan->notifiers);
623 
624 	ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
625 			   &ntfy->node);
626 	if (ret)
627 		goto done;
628 
629 	args.start = ntfy->node->offset;
630 	args.limit = ntfy->node->offset + ntfy->node->length - 1;
631 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
632 		args.target = NV_DMA_V0_TARGET_VM;
633 		args.access = NV_DMA_V0_ACCESS_VM;
634 		args.start += chan->ntfy_vma->addr;
635 		args.limit += chan->ntfy_vma->addr;
636 	} else
637 	if (drm->agp.bridge) {
638 		args.target = NV_DMA_V0_TARGET_AGP;
639 		args.access = NV_DMA_V0_ACCESS_RDWR;
640 		args.start += drm->agp.base + chan->ntfy->offset;
641 		args.limit += drm->agp.base + chan->ntfy->offset;
642 	} else {
643 		args.target = NV_DMA_V0_TARGET_VM;
644 		args.access = NV_DMA_V0_ACCESS_RDWR;
645 		args.start += chan->ntfy->offset;
646 		args.limit += chan->ntfy->offset;
647 	}
648 
649 	client->route = NVDRM_OBJECT_ABI16;
650 	ret = nvif_object_ctor(&chan->chan->user, "abi16Ntfy", info->handle,
651 			       NV_DMA_IN_MEMORY, &args, sizeof(args),
652 			       &ntfy->object);
653 	client->route = NVDRM_OBJECT_NVIF;
654 	if (ret)
655 		goto done;
656 
657 	info->offset = ntfy->node->offset;
658 done:
659 	if (ret)
660 		nouveau_abi16_ntfy_fini(chan, ntfy);
661 	return nouveau_abi16_put(abi16, ret);
662 }
663 
664 int
665 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
666 {
667 	struct drm_nouveau_gpuobj_free *fini = data;
668 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
669 	struct nouveau_abi16_chan *chan;
670 	struct nouveau_abi16_ntfy *ntfy;
671 	int ret = -ENOENT;
672 
673 	if (unlikely(!abi16))
674 		return -ENOMEM;
675 
676 	chan = nouveau_abi16_chan(abi16, fini->channel);
677 	if (!chan)
678 		return nouveau_abi16_put(abi16, -EINVAL);
679 
680 	/* synchronize with the user channel and destroy the gpu object */
681 	nouveau_channel_idle(chan->chan);
682 
683 	list_for_each_entry(ntfy, &chan->notifiers, head) {
684 		if (ntfy->object.handle == fini->handle) {
685 			nouveau_abi16_ntfy_fini(chan, ntfy);
686 			ret = 0;
687 			break;
688 		}
689 	}
690 
691 	return nouveau_abi16_put(abi16, ret);
692 }
693