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 * 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_init(&cli->base.object, 0, NV_DEVICE, 59 &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 * 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 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 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 114 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan, 115 struct nouveau_abi16_ntfy *ntfy) 116 { 117 nvif_object_fini(&ntfy->object); 118 nvkm_mm_free(&chan->heap, &ntfy->node); 119 list_del(&ntfy->head); 120 kfree(ntfy); 121 } 122 123 static void 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 releasing notify object, which 130 * may be still in use */ 131 if (chan->chan && chan->ntfy) 132 nouveau_channel_idle(chan->chan); 133 134 /* cleanup notifier state */ 135 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) { 136 nouveau_abi16_ntfy_fini(chan, ntfy); 137 } 138 139 if (chan->ntfy) { 140 nouveau_vma_del(&chan->ntfy_vma); 141 nouveau_bo_unpin(chan->ntfy); 142 drm_gem_object_unreference_unlocked(&chan->ntfy->gem); 143 } 144 145 if (chan->heap.block_size) 146 nvkm_mm_fini(&chan->heap); 147 148 /* destroy channel object, all children will be killed too */ 149 if (chan->chan) { 150 nouveau_channel_idle(chan->chan); 151 nouveau_channel_del(&chan->chan); 152 } 153 154 list_del(&chan->head); 155 kfree(chan); 156 } 157 158 void 159 nouveau_abi16_fini(struct nouveau_abi16 *abi16) 160 { 161 struct nouveau_cli *cli = (void *)abi16->device.object.client; 162 struct nouveau_abi16_chan *chan, *temp; 163 164 /* cleanup channels */ 165 list_for_each_entry_safe(chan, temp, &abi16->channels, head) { 166 nouveau_abi16_chan_fini(abi16, chan); 167 } 168 169 /* destroy the device object */ 170 nvif_device_fini(&abi16->device); 171 172 kfree(cli->abi16); 173 cli->abi16 = NULL; 174 } 175 176 int 177 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS) 178 { 179 struct nouveau_cli *cli = nouveau_cli(file_priv); 180 struct nouveau_drm *drm = nouveau_drm(dev); 181 struct nvif_device *device = &drm->client.device; 182 struct nvkm_gr *gr = nvxx_gr(device); 183 struct drm_nouveau_getparam *getparam = data; 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 = dev->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 = dev->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(dev->pdev)) 209 getparam->value = 1; 210 else 211 getparam->value = 2; 212 break; 213 default: 214 WARN_ON(1); 215 break; 216 } 217 case NOUVEAU_GETPARAM_FB_SIZE: 218 getparam->value = drm->gem.vram_available; 219 break; 220 case NOUVEAU_GETPARAM_AGP_SIZE: 221 getparam->value = drm->gem.gart_available; 222 break; 223 case NOUVEAU_GETPARAM_VM_VRAM_BASE: 224 getparam->value = 0; /* deprecated */ 225 break; 226 case NOUVEAU_GETPARAM_PTIMER_TIME: 227 getparam->value = nvif_device_time(device); 228 break; 229 case NOUVEAU_GETPARAM_HAS_BO_USAGE: 230 getparam->value = 1; 231 break; 232 case NOUVEAU_GETPARAM_HAS_PAGEFLIP: 233 getparam->value = 1; 234 break; 235 case NOUVEAU_GETPARAM_GRAPH_UNITS: 236 getparam->value = nvkm_gr_units(gr); 237 break; 238 default: 239 NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param); 240 return -EINVAL; 241 } 242 243 return 0; 244 } 245 246 int 247 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS) 248 { 249 return -EINVAL; 250 } 251 252 int 253 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS) 254 { 255 struct drm_nouveau_channel_alloc *init = data; 256 struct nouveau_cli *cli = nouveau_cli(file_priv); 257 struct nouveau_drm *drm = nouveau_drm(dev); 258 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv); 259 struct nouveau_abi16_chan *chan; 260 struct nvif_device *device; 261 u64 engine; 262 int ret; 263 264 if (unlikely(!abi16)) 265 return -ENOMEM; 266 267 if (!drm->channel) 268 return nouveau_abi16_put(abi16, -ENODEV); 269 270 device = &abi16->device; 271 272 /* hack to allow channel engine type specification on kepler */ 273 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) { 274 if (init->fb_ctxdma_handle == ~0) { 275 switch (init->tt_ctxdma_handle) { 276 case 0x01: engine = NV_DEVICE_INFO_ENGINE_GR ; break; 277 case 0x02: engine = NV_DEVICE_INFO_ENGINE_MSPDEC; break; 278 case 0x04: engine = NV_DEVICE_INFO_ENGINE_MSPPP ; break; 279 case 0x08: engine = NV_DEVICE_INFO_ENGINE_MSVLD ; break; 280 case 0x30: engine = NV_DEVICE_INFO_ENGINE_CE ; break; 281 default: 282 return nouveau_abi16_put(abi16, -ENOSYS); 283 } 284 } else { 285 engine = NV_DEVICE_INFO_ENGINE_GR; 286 } 287 288 if (engine != NV_DEVICE_INFO_ENGINE_CE) 289 engine = nvif_fifo_runlist(device, engine); 290 else 291 engine = nvif_fifo_runlist_ce(device); 292 init->fb_ctxdma_handle = engine; 293 init->tt_ctxdma_handle = 0; 294 } 295 296 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0) 297 return nouveau_abi16_put(abi16, -EINVAL); 298 299 /* allocate "abi16 channel" data and make up a handle for it */ 300 chan = kzalloc(sizeof(*chan), GFP_KERNEL); 301 if (!chan) 302 return nouveau_abi16_put(abi16, -ENOMEM); 303 304 INIT_LIST_HEAD(&chan->notifiers); 305 list_add(&chan->head, &abi16->channels); 306 307 /* create channel object and initialise dma and fence management */ 308 ret = nouveau_channel_new(drm, device, init->fb_ctxdma_handle, 309 init->tt_ctxdma_handle, &chan->chan); 310 if (ret) 311 goto done; 312 313 init->channel = chan->chan->chid; 314 315 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) 316 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM | 317 NOUVEAU_GEM_DOMAIN_GART; 318 else 319 if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) 320 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM; 321 else 322 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART; 323 324 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) { 325 init->subchan[0].handle = 0x00000000; 326 init->subchan[0].grclass = 0x0000; 327 init->subchan[1].handle = chan->chan->nvsw.handle; 328 init->subchan[1].grclass = 0x506e; 329 init->nr_subchan = 2; 330 } 331 332 /* Named memory object area */ 333 ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART, 334 0, 0, &chan->ntfy); 335 if (ret == 0) 336 ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false); 337 if (ret) 338 goto done; 339 340 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) { 341 ret = nouveau_vma_new(chan->ntfy, &cli->vmm, &chan->ntfy_vma); 342 if (ret) 343 goto done; 344 } 345 346 ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem, 347 &init->notifier_handle); 348 if (ret) 349 goto done; 350 351 ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1); 352 done: 353 if (ret) 354 nouveau_abi16_chan_fini(abi16, chan); 355 return nouveau_abi16_put(abi16, ret); 356 } 357 358 static struct nouveau_abi16_chan * 359 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel) 360 { 361 struct nouveau_abi16_chan *chan; 362 363 list_for_each_entry(chan, &abi16->channels, head) { 364 if (chan->chan->chid == channel) 365 return chan; 366 } 367 368 return NULL; 369 } 370 371 int 372 nouveau_abi16_usif(struct drm_file *file_priv, void *data, u32 size) 373 { 374 union { 375 struct nvif_ioctl_v0 v0; 376 } *args = data; 377 struct nouveau_abi16_chan *chan; 378 struct nouveau_abi16 *abi16; 379 int ret = -ENOSYS; 380 381 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) { 382 switch (args->v0.type) { 383 case NVIF_IOCTL_V0_NEW: 384 case NVIF_IOCTL_V0_MTHD: 385 case NVIF_IOCTL_V0_SCLASS: 386 break; 387 default: 388 return -EACCES; 389 } 390 } else 391 return ret; 392 393 if (!(abi16 = nouveau_abi16(file_priv))) 394 return -ENOMEM; 395 396 if (args->v0.token != ~0ULL) { 397 if (!(chan = nouveau_abi16_chan(abi16, args->v0.token))) 398 return -EINVAL; 399 args->v0.object = nvif_handle(&chan->chan->user); 400 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY; 401 return 0; 402 } 403 404 args->v0.object = nvif_handle(&abi16->device.object); 405 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY; 406 return 0; 407 } 408 409 int 410 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS) 411 { 412 struct drm_nouveau_channel_free *req = data; 413 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv); 414 struct nouveau_abi16_chan *chan; 415 416 if (unlikely(!abi16)) 417 return -ENOMEM; 418 419 chan = nouveau_abi16_chan(abi16, req->channel); 420 if (!chan) 421 return nouveau_abi16_put(abi16, -ENOENT); 422 nouveau_abi16_chan_fini(abi16, chan); 423 return nouveau_abi16_put(abi16, 0); 424 } 425 426 int 427 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS) 428 { 429 struct drm_nouveau_grobj_alloc *init = data; 430 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv); 431 struct nouveau_abi16_chan *chan; 432 struct nouveau_abi16_ntfy *ntfy; 433 struct nvif_client *client; 434 struct nvif_sclass *sclass; 435 s32 oclass = 0; 436 int ret, i; 437 438 if (unlikely(!abi16)) 439 return -ENOMEM; 440 441 if (init->handle == ~0) 442 return nouveau_abi16_put(abi16, -EINVAL); 443 client = abi16->device.object.client; 444 445 chan = nouveau_abi16_chan(abi16, init->channel); 446 if (!chan) 447 return nouveau_abi16_put(abi16, -ENOENT); 448 449 ret = nvif_object_sclass_get(&chan->chan->user, &sclass); 450 if (ret < 0) 451 return nouveau_abi16_put(abi16, ret); 452 453 if ((init->class & 0x00ff) == 0x006e) { 454 /* nvsw: compatibility with older 0x*6e class identifier */ 455 for (i = 0; !oclass && i < ret; i++) { 456 switch (sclass[i].oclass) { 457 case NVIF_CLASS_SW_NV04: 458 case NVIF_CLASS_SW_NV10: 459 case NVIF_CLASS_SW_NV50: 460 case NVIF_CLASS_SW_GF100: 461 oclass = sclass[i].oclass; 462 break; 463 default: 464 break; 465 } 466 } 467 } else 468 if ((init->class & 0x00ff) == 0x00b1) { 469 /* msvld: compatibility with incorrect version exposure */ 470 for (i = 0; i < ret; i++) { 471 if ((sclass[i].oclass & 0x00ff) == 0x00b1) { 472 oclass = sclass[i].oclass; 473 break; 474 } 475 } 476 } else 477 if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */ 478 /* mspdec: compatibility with incorrect version exposure */ 479 for (i = 0; i < ret; i++) { 480 if ((sclass[i].oclass & 0x00ff) == 0x00b2) { 481 oclass = sclass[i].oclass; 482 break; 483 } 484 } 485 } else 486 if ((init->class & 0x00ff) == 0x00b3) { /* msppp */ 487 /* msppp: compatibility with incorrect version exposure */ 488 for (i = 0; i < ret; i++) { 489 if ((sclass[i].oclass & 0x00ff) == 0x00b3) { 490 oclass = sclass[i].oclass; 491 break; 492 } 493 } 494 } else { 495 oclass = init->class; 496 } 497 498 nvif_object_sclass_put(&sclass); 499 if (!oclass) 500 return nouveau_abi16_put(abi16, -EINVAL); 501 502 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL); 503 if (!ntfy) 504 return nouveau_abi16_put(abi16, -ENOMEM); 505 506 list_add(&ntfy->head, &chan->notifiers); 507 508 client->route = NVDRM_OBJECT_ABI16; 509 ret = nvif_object_init(&chan->chan->user, init->handle, oclass, 510 NULL, 0, &ntfy->object); 511 client->route = NVDRM_OBJECT_NVIF; 512 513 if (ret) 514 nouveau_abi16_ntfy_fini(chan, ntfy); 515 return nouveau_abi16_put(abi16, ret); 516 } 517 518 int 519 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS) 520 { 521 struct drm_nouveau_notifierobj_alloc *info = data; 522 struct nouveau_drm *drm = nouveau_drm(dev); 523 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv); 524 struct nouveau_abi16_chan *chan; 525 struct nouveau_abi16_ntfy *ntfy; 526 struct nvif_device *device = &abi16->device; 527 struct nvif_client *client; 528 struct nv_dma_v0 args = {}; 529 int ret; 530 531 if (unlikely(!abi16)) 532 return -ENOMEM; 533 534 /* completely unnecessary for these chipsets... */ 535 if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI)) 536 return nouveau_abi16_put(abi16, -EINVAL); 537 client = abi16->device.object.client; 538 539 chan = nouveau_abi16_chan(abi16, info->channel); 540 if (!chan) 541 return nouveau_abi16_put(abi16, -ENOENT); 542 543 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL); 544 if (!ntfy) 545 return nouveau_abi16_put(abi16, -ENOMEM); 546 547 list_add(&ntfy->head, &chan->notifiers); 548 549 ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1, 550 &ntfy->node); 551 if (ret) 552 goto done; 553 554 args.start = ntfy->node->offset; 555 args.limit = ntfy->node->offset + ntfy->node->length - 1; 556 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) { 557 args.target = NV_DMA_V0_TARGET_VM; 558 args.access = NV_DMA_V0_ACCESS_VM; 559 args.start += chan->ntfy_vma->addr; 560 args.limit += chan->ntfy_vma->addr; 561 } else 562 if (drm->agp.bridge) { 563 args.target = NV_DMA_V0_TARGET_AGP; 564 args.access = NV_DMA_V0_ACCESS_RDWR; 565 args.start += drm->agp.base + chan->ntfy->bo.offset; 566 args.limit += drm->agp.base + chan->ntfy->bo.offset; 567 } else { 568 args.target = NV_DMA_V0_TARGET_VM; 569 args.access = NV_DMA_V0_ACCESS_RDWR; 570 args.start += chan->ntfy->bo.offset; 571 args.limit += chan->ntfy->bo.offset; 572 } 573 574 client->route = NVDRM_OBJECT_ABI16; 575 client->super = true; 576 ret = nvif_object_init(&chan->chan->user, info->handle, 577 NV_DMA_IN_MEMORY, &args, sizeof(args), 578 &ntfy->object); 579 client->super = false; 580 client->route = NVDRM_OBJECT_NVIF; 581 if (ret) 582 goto done; 583 584 info->offset = ntfy->node->offset; 585 done: 586 if (ret) 587 nouveau_abi16_ntfy_fini(chan, ntfy); 588 return nouveau_abi16_put(abi16, ret); 589 } 590 591 int 592 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS) 593 { 594 struct drm_nouveau_gpuobj_free *fini = data; 595 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv); 596 struct nouveau_abi16_chan *chan; 597 struct nouveau_abi16_ntfy *ntfy; 598 int ret = -ENOENT; 599 600 if (unlikely(!abi16)) 601 return -ENOMEM; 602 603 chan = nouveau_abi16_chan(abi16, fini->channel); 604 if (!chan) 605 return nouveau_abi16_put(abi16, -EINVAL); 606 607 /* synchronize with the user channel and destroy the gpu object */ 608 nouveau_channel_idle(chan->chan); 609 610 list_for_each_entry(ntfy, &chan->notifiers, head) { 611 if (ntfy->object.handle == fini->handle) { 612 nouveau_abi16_ntfy_fini(chan, ntfy); 613 ret = 0; 614 break; 615 } 616 } 617 618 return nouveau_abi16_put(abi16, ret); 619 } 620