nouveau_abi16.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 <core/object.h>
  24. #include <core/client.h>
  25. #include <core/device.h>
  26. #include <core/class.h>
  27. #include <core/mm.h>
  28. #include <subdev/fb.h>
  29. #include <subdev/timer.h>
  30. #include <subdev/instmem.h>
  31. #include "nouveau_drm.h"
  32. #include "nouveau_dma.h"
  33. #include "nouveau_gem.h"
  34. #include "nouveau_chan.h"
  35. #include "nouveau_abi16.h"
  36. struct nouveau_abi16 *
  37. nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
  38. {
  39. struct nouveau_cli *cli = nouveau_cli(file_priv);
  40. mutex_lock(&cli->mutex);
  41. if (!cli->abi16) {
  42. struct nouveau_abi16 *abi16;
  43. cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
  44. if (cli->abi16) {
  45. INIT_LIST_HEAD(&abi16->channels);
  46. abi16->client = nv_object(cli);
  47. /* allocate device object targeting client's default
  48. * device (ie. the one that belongs to the fd it
  49. * opened)
  50. */
  51. if (nouveau_object_new(abi16->client, NVDRM_CLIENT,
  52. NVDRM_DEVICE, 0x0080,
  53. &(struct nv_device_class) {
  54. .device = ~0ULL,
  55. },
  56. sizeof(struct nv_device_class),
  57. &abi16->device) == 0)
  58. return cli->abi16;
  59. kfree(cli->abi16);
  60. cli->abi16 = NULL;
  61. }
  62. mutex_unlock(&cli->mutex);
  63. }
  64. return cli->abi16;
  65. }
  66. int
  67. nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
  68. {
  69. struct nouveau_cli *cli = (void *)abi16->client;
  70. mutex_unlock(&cli->mutex);
  71. return ret;
  72. }
  73. u16
  74. nouveau_abi16_swclass(struct nouveau_drm *drm)
  75. {
  76. switch (nv_device(drm->device)->card_type) {
  77. case NV_04:
  78. return 0x006e;
  79. case NV_10:
  80. case NV_20:
  81. case NV_30:
  82. case NV_40:
  83. return 0x016e;
  84. case NV_50:
  85. return 0x506e;
  86. case NV_C0:
  87. case NV_D0:
  88. case NV_E0:
  89. return 0x906e;
  90. }
  91. return 0x0000;
  92. }
  93. static void
  94. nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
  95. struct nouveau_abi16_ntfy *ntfy)
  96. {
  97. nouveau_mm_free(&chan->heap, &ntfy->node);
  98. list_del(&ntfy->head);
  99. kfree(ntfy);
  100. }
  101. static void
  102. nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
  103. struct nouveau_abi16_chan *chan)
  104. {
  105. struct nouveau_abi16_ntfy *ntfy, *temp;
  106. /* wait for all activity to stop before releasing notify object, which
  107. * may be still in use */
  108. if (chan->chan && chan->ntfy)
  109. nouveau_channel_idle(chan->chan);
  110. /* cleanup notifier state */
  111. list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
  112. nouveau_abi16_ntfy_fini(chan, ntfy);
  113. }
  114. if (chan->ntfy) {
  115. nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
  116. drm_gem_object_unreference_unlocked(chan->ntfy->gem);
  117. }
  118. if (chan->heap.block_size)
  119. nouveau_mm_fini(&chan->heap);
  120. /* destroy channel object, all children will be killed too */
  121. if (chan->chan) {
  122. abi16->handles &= ~(1 << (chan->chan->handle & 0xffff));
  123. nouveau_channel_del(&chan->chan);
  124. }
  125. list_del(&chan->head);
  126. kfree(chan);
  127. }
  128. void
  129. nouveau_abi16_fini(struct nouveau_abi16 *abi16)
  130. {
  131. struct nouveau_cli *cli = (void *)abi16->client;
  132. struct nouveau_abi16_chan *chan, *temp;
  133. /* cleanup channels */
  134. list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
  135. nouveau_abi16_chan_fini(abi16, chan);
  136. }
  137. /* destroy the device object */
  138. nouveau_object_del(abi16->client, NVDRM_CLIENT, NVDRM_DEVICE);
  139. kfree(cli->abi16);
  140. cli->abi16 = NULL;
  141. }
  142. int
  143. nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
  144. {
  145. struct nouveau_drm *drm = nouveau_drm(dev);
  146. struct nouveau_device *device = nv_device(drm->device);
  147. struct nouveau_timer *ptimer = nouveau_timer(device);
  148. struct drm_nouveau_getparam *getparam = data;
  149. switch (getparam->param) {
  150. case NOUVEAU_GETPARAM_CHIPSET_ID:
  151. getparam->value = device->chipset;
  152. break;
  153. case NOUVEAU_GETPARAM_PCI_VENDOR:
  154. getparam->value = dev->pci_vendor;
  155. break;
  156. case NOUVEAU_GETPARAM_PCI_DEVICE:
  157. getparam->value = dev->pci_device;
  158. break;
  159. case NOUVEAU_GETPARAM_BUS_TYPE:
  160. if (drm_pci_device_is_agp(dev))
  161. getparam->value = 0;
  162. else
  163. if (!pci_is_pcie(dev->pdev))
  164. getparam->value = 1;
  165. else
  166. getparam->value = 2;
  167. break;
  168. case NOUVEAU_GETPARAM_FB_SIZE:
  169. getparam->value = drm->gem.vram_available;
  170. break;
  171. case NOUVEAU_GETPARAM_AGP_SIZE:
  172. getparam->value = drm->gem.gart_available;
  173. break;
  174. case NOUVEAU_GETPARAM_VM_VRAM_BASE:
  175. getparam->value = 0; /* deprecated */
  176. break;
  177. case NOUVEAU_GETPARAM_PTIMER_TIME:
  178. getparam->value = ptimer->read(ptimer);
  179. break;
  180. case NOUVEAU_GETPARAM_HAS_BO_USAGE:
  181. getparam->value = 1;
  182. break;
  183. case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
  184. getparam->value = 1;
  185. break;
  186. case NOUVEAU_GETPARAM_GRAPH_UNITS:
  187. /* NV40 and NV50 versions are quite different, but register
  188. * address is the same. User is supposed to know the card
  189. * family anyway... */
  190. if (device->chipset >= 0x40) {
  191. getparam->value = nv_rd32(device, 0x001540);
  192. break;
  193. }
  194. /* FALLTHRU */
  195. default:
  196. nv_debug(device, "unknown parameter %lld\n", getparam->param);
  197. return -EINVAL;
  198. }
  199. return 0;
  200. }
  201. int
  202. nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
  203. {
  204. return -EINVAL;
  205. }
  206. int
  207. nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
  208. {
  209. struct drm_nouveau_channel_alloc *init = data;
  210. struct nouveau_cli *cli = nouveau_cli(file_priv);
  211. struct nouveau_drm *drm = nouveau_drm(dev);
  212. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
  213. struct nouveau_abi16_chan *chan;
  214. struct nouveau_client *client;
  215. struct nouveau_device *device;
  216. struct nouveau_instmem *imem;
  217. struct nouveau_fb *pfb;
  218. int ret;
  219. if (unlikely(!abi16))
  220. return -ENOMEM;
  221. if (!drm->channel)
  222. return nouveau_abi16_put(abi16, -ENODEV);
  223. client = nv_client(abi16->client);
  224. device = nv_device(abi16->device);
  225. imem = nouveau_instmem(device);
  226. pfb = nouveau_fb(device);
  227. /* hack to allow channel engine type specification on kepler */
  228. if (device->card_type >= NV_E0) {
  229. if (init->fb_ctxdma_handle != ~0)
  230. init->fb_ctxdma_handle = NVE0_CHANNEL_IND_ENGINE_GR;
  231. else
  232. init->fb_ctxdma_handle = init->tt_ctxdma_handle;
  233. /* allow flips to be executed if this is a graphics channel */
  234. init->tt_ctxdma_handle = 0;
  235. if (init->fb_ctxdma_handle == NVE0_CHANNEL_IND_ENGINE_GR)
  236. init->tt_ctxdma_handle = 1;
  237. }
  238. if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
  239. return nouveau_abi16_put(abi16, -EINVAL);
  240. /* allocate "abi16 channel" data and make up a handle for it */
  241. init->channel = ffsll(~abi16->handles);
  242. if (!init->channel--)
  243. return nouveau_abi16_put(abi16, -ENOSPC);
  244. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  245. if (!chan)
  246. return nouveau_abi16_put(abi16, -ENOMEM);
  247. INIT_LIST_HEAD(&chan->notifiers);
  248. list_add(&chan->head, &abi16->channels);
  249. abi16->handles |= (1 << init->channel);
  250. /* create channel object and initialise dma and fence management */
  251. ret = nouveau_channel_new(drm, cli, NVDRM_DEVICE, NVDRM_CHAN |
  252. init->channel, init->fb_ctxdma_handle,
  253. init->tt_ctxdma_handle, &chan->chan);
  254. if (ret)
  255. goto done;
  256. if (device->card_type >= NV_50)
  257. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
  258. NOUVEAU_GEM_DOMAIN_GART;
  259. else
  260. if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
  261. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
  262. else
  263. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
  264. if (device->card_type < NV_C0) {
  265. init->subchan[0].handle = 0x00000000;
  266. init->subchan[0].grclass = 0x0000;
  267. init->subchan[1].handle = NvSw;
  268. init->subchan[1].grclass = 0x506e;
  269. init->nr_subchan = 2;
  270. }
  271. /* Named memory object area */
  272. ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
  273. 0, 0, &chan->ntfy);
  274. if (ret == 0)
  275. ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT);
  276. if (ret)
  277. goto done;
  278. if (device->card_type >= NV_50) {
  279. ret = nouveau_bo_vma_add(chan->ntfy, client->vm,
  280. &chan->ntfy_vma);
  281. if (ret)
  282. goto done;
  283. }
  284. ret = drm_gem_handle_create(file_priv, chan->ntfy->gem,
  285. &init->notifier_handle);
  286. if (ret)
  287. goto done;
  288. ret = nouveau_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
  289. done:
  290. if (ret)
  291. nouveau_abi16_chan_fini(abi16, chan);
  292. return nouveau_abi16_put(abi16, ret);
  293. }
  294. int
  295. nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
  296. {
  297. struct drm_nouveau_channel_free *req = data;
  298. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
  299. struct nouveau_abi16_chan *chan;
  300. int ret = -ENOENT;
  301. if (unlikely(!abi16))
  302. return -ENOMEM;
  303. list_for_each_entry(chan, &abi16->channels, head) {
  304. if (chan->chan->handle == (NVDRM_CHAN | req->channel)) {
  305. nouveau_abi16_chan_fini(abi16, chan);
  306. return nouveau_abi16_put(abi16, 0);
  307. }
  308. }
  309. return nouveau_abi16_put(abi16, ret);
  310. }
  311. int
  312. nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
  313. {
  314. struct drm_nouveau_grobj_alloc *init = data;
  315. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
  316. struct nouveau_drm *drm = nouveau_drm(dev);
  317. struct nouveau_object *object;
  318. int ret;
  319. if (unlikely(!abi16))
  320. return -ENOMEM;
  321. if (init->handle == ~0)
  322. return nouveau_abi16_put(abi16, -EINVAL);
  323. /* compatibility with userspace that assumes 506e for all chipsets */
  324. if (init->class == 0x506e) {
  325. init->class = nouveau_abi16_swclass(drm);
  326. if (init->class == 0x906e)
  327. return nouveau_abi16_put(abi16, 0);
  328. }
  329. ret = nouveau_object_new(abi16->client, NVDRM_CHAN | init->channel,
  330. init->handle, init->class, NULL, 0, &object);
  331. return nouveau_abi16_put(abi16, ret);
  332. }
  333. int
  334. nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
  335. {
  336. struct drm_nouveau_notifierobj_alloc *info = data;
  337. struct nouveau_drm *drm = nouveau_drm(dev);
  338. struct nouveau_device *device = nv_device(drm->device);
  339. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
  340. struct nouveau_abi16_chan *chan, *temp;
  341. struct nouveau_abi16_ntfy *ntfy;
  342. struct nouveau_object *object;
  343. struct nv_dma_class args = {};
  344. int ret;
  345. if (unlikely(!abi16))
  346. return -ENOMEM;
  347. /* completely unnecessary for these chipsets... */
  348. if (unlikely(nv_device(abi16->device)->card_type >= NV_C0))
  349. return nouveau_abi16_put(abi16, -EINVAL);
  350. list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
  351. if (chan->chan->handle == (NVDRM_CHAN | info->channel))
  352. break;
  353. chan = NULL;
  354. }
  355. if (!chan)
  356. return nouveau_abi16_put(abi16, -ENOENT);
  357. ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
  358. if (!ntfy)
  359. return nouveau_abi16_put(abi16, -ENOMEM);
  360. list_add(&ntfy->head, &chan->notifiers);
  361. ntfy->handle = info->handle;
  362. ret = nouveau_mm_head(&chan->heap, 1, info->size, info->size, 1,
  363. &ntfy->node);
  364. if (ret)
  365. goto done;
  366. args.start = ntfy->node->offset;
  367. args.limit = ntfy->node->offset + ntfy->node->length - 1;
  368. if (device->card_type >= NV_50) {
  369. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
  370. args.start += chan->ntfy_vma.offset;
  371. args.limit += chan->ntfy_vma.offset;
  372. } else
  373. if (drm->agp.stat == ENABLED) {
  374. args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
  375. args.start += drm->agp.base + chan->ntfy->bo.offset;
  376. args.limit += drm->agp.base + chan->ntfy->bo.offset;
  377. } else {
  378. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
  379. args.start += chan->ntfy->bo.offset;
  380. args.limit += chan->ntfy->bo.offset;
  381. }
  382. ret = nouveau_object_new(abi16->client, chan->chan->handle,
  383. ntfy->handle, 0x003d, &args,
  384. sizeof(args), &object);
  385. if (ret)
  386. goto done;
  387. done:
  388. if (ret)
  389. nouveau_abi16_ntfy_fini(chan, ntfy);
  390. return nouveau_abi16_put(abi16, ret);
  391. }
  392. int
  393. nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
  394. {
  395. struct drm_nouveau_gpuobj_free *fini = data;
  396. struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
  397. struct nouveau_abi16_chan *chan, *temp;
  398. struct nouveau_abi16_ntfy *ntfy;
  399. int ret;
  400. if (unlikely(!abi16))
  401. return -ENOMEM;
  402. list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
  403. if (chan->chan->handle == (NVDRM_CHAN | fini->channel))
  404. break;
  405. chan = NULL;
  406. }
  407. if (!chan)
  408. return nouveau_abi16_put(abi16, -ENOENT);
  409. /* synchronize with the user channel and destroy the gpu object */
  410. nouveau_channel_idle(chan->chan);
  411. ret = nouveau_object_del(abi16->client, chan->chan->handle, fini->handle);
  412. if (ret)
  413. return nouveau_abi16_put(abi16, ret);
  414. /* cleanup extra state if this object was a notifier */
  415. list_for_each_entry(ntfy, &chan->notifiers, head) {
  416. if (ntfy->handle == fini->handle) {
  417. nouveau_mm_free(&chan->heap, &ntfy->node);
  418. list_del(&ntfy->head);
  419. break;
  420. }
  421. }
  422. return nouveau_abi16_put(abi16, 0);
  423. }