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