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