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