nouveau_abi16.c 13 KB

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