nouveau_abi16.c 15 KB

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