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