nouveau_chan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * Authors: Ben Skeggs
  23. */
  24. #include <core/object.h>
  25. #include <core/client.h>
  26. #include <core/device.h>
  27. #include <core/class.h>
  28. #include <subdev/fb.h>
  29. #include <subdev/vm.h>
  30. #include <subdev/instmem.h>
  31. #include <engine/software.h>
  32. #include "nouveau_drm.h"
  33. #include "nouveau_dma.h"
  34. #include "nouveau_bo.h"
  35. #include "nouveau_chan.h"
  36. #include "nouveau_fence.h"
  37. #include "nouveau_abi16.h"
  38. MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
  39. static int nouveau_vram_pushbuf;
  40. module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
  41. int
  42. nouveau_channel_idle(struct nouveau_channel *chan)
  43. {
  44. struct nouveau_cli *cli = chan->cli;
  45. struct nouveau_fence *fence = NULL;
  46. int ret;
  47. ret = nouveau_fence_new(chan, false, &fence);
  48. if (!ret) {
  49. ret = nouveau_fence_wait(fence, false, false);
  50. nouveau_fence_unref(&fence);
  51. }
  52. if (ret)
  53. NV_ERROR(cli, "failed to idle channel 0x%08x [%s]\n",
  54. chan->handle, cli->base.name);
  55. return ret;
  56. }
  57. void
  58. nouveau_channel_del(struct nouveau_channel **pchan)
  59. {
  60. struct nouveau_channel *chan = *pchan;
  61. if (chan) {
  62. struct nouveau_object *client = nv_object(chan->cli);
  63. if (chan->fence) {
  64. nouveau_channel_idle(chan);
  65. nouveau_fence(chan->drm)->context_del(chan);
  66. }
  67. nouveau_object_del(client, NVDRM_DEVICE, chan->handle);
  68. nouveau_object_del(client, NVDRM_DEVICE, chan->push.handle);
  69. nouveau_bo_vma_del(chan->push.buffer, &chan->push.vma);
  70. nouveau_bo_unmap(chan->push.buffer);
  71. if (chan->push.buffer && chan->push.buffer->pin_refcnt)
  72. nouveau_bo_unpin(chan->push.buffer);
  73. nouveau_bo_ref(NULL, &chan->push.buffer);
  74. kfree(chan);
  75. }
  76. *pchan = NULL;
  77. }
  78. static int
  79. nouveau_channel_prep(struct nouveau_drm *drm, struct nouveau_cli *cli,
  80. u32 parent, u32 handle, u32 size,
  81. struct nouveau_channel **pchan)
  82. {
  83. struct nouveau_device *device = nv_device(drm->device);
  84. struct nouveau_instmem *imem = nouveau_instmem(device);
  85. struct nouveau_vmmgr *vmm = nouveau_vmmgr(device);
  86. struct nouveau_fb *pfb = nouveau_fb(device);
  87. struct nouveau_client *client = &cli->base;
  88. struct nv_dma_class args = {};
  89. struct nouveau_channel *chan;
  90. struct nouveau_object *push;
  91. u32 target;
  92. int ret;
  93. chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
  94. if (!chan)
  95. return -ENOMEM;
  96. chan->cli = cli;
  97. chan->drm = drm;
  98. chan->handle = handle;
  99. /* allocate memory for dma push buffer */
  100. target = TTM_PL_FLAG_TT;
  101. if (nouveau_vram_pushbuf)
  102. target = TTM_PL_FLAG_VRAM;
  103. ret = nouveau_bo_new(drm->dev, size, 0, target, 0, 0, NULL,
  104. &chan->push.buffer);
  105. if (ret == 0) {
  106. ret = nouveau_bo_pin(chan->push.buffer, target);
  107. if (ret == 0)
  108. ret = nouveau_bo_map(chan->push.buffer);
  109. }
  110. if (ret) {
  111. nouveau_channel_del(pchan);
  112. return ret;
  113. }
  114. /* create dma object covering the *entire* memory space that the
  115. * pushbuf lives in, this is because the GEM code requires that
  116. * we be able to call out to other (indirect) push buffers
  117. */
  118. chan->push.vma.offset = chan->push.buffer->bo.offset;
  119. chan->push.handle = NVDRM_PUSH | (handle & 0xffff);
  120. if (device->card_type >= NV_50) {
  121. ret = nouveau_bo_vma_add(chan->push.buffer, client->vm,
  122. &chan->push.vma);
  123. if (ret) {
  124. nouveau_channel_del(pchan);
  125. return ret;
  126. }
  127. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
  128. args.start = 0;
  129. args.limit = client->vm->vmm->limit - 1;
  130. } else
  131. if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
  132. u64 limit = pfb->ram.size - imem->reserved - 1;
  133. if (device->card_type == NV_04) {
  134. /* nv04 vram pushbuf hack, retarget to its location in
  135. * the framebuffer bar rather than direct vram access..
  136. * nfi why this exists, it came from the -nv ddx.
  137. */
  138. args.flags = NV_DMA_TARGET_PCI | NV_DMA_ACCESS_RDWR;
  139. args.start = pci_resource_start(device->pdev, 1);
  140. args.limit = args.start + limit;
  141. } else {
  142. args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
  143. args.start = 0;
  144. args.limit = limit;
  145. }
  146. } else {
  147. if (chan->drm->agp.stat == ENABLED) {
  148. args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
  149. args.start = chan->drm->agp.base;
  150. args.limit = chan->drm->agp.base +
  151. chan->drm->agp.size - 1;
  152. } else {
  153. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
  154. args.start = 0;
  155. args.limit = vmm->limit - 1;
  156. }
  157. }
  158. ret = nouveau_object_new(nv_object(chan->cli), parent,
  159. chan->push.handle, 0x0002,
  160. &args, sizeof(args), &push);
  161. if (ret) {
  162. nouveau_channel_del(pchan);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. static int
  168. nouveau_channel_ind(struct nouveau_drm *drm, struct nouveau_cli *cli,
  169. u32 parent, u32 handle, u32 engine,
  170. struct nouveau_channel **pchan)
  171. {
  172. static const u16 oclasses[] = { NVE0_CHANNEL_IND_CLASS,
  173. NVC0_CHANNEL_IND_CLASS,
  174. NV84_CHANNEL_IND_CLASS,
  175. NV50_CHANNEL_IND_CLASS,
  176. 0 };
  177. const u16 *oclass = oclasses;
  178. struct nve0_channel_ind_class args;
  179. struct nouveau_channel *chan;
  180. int ret;
  181. /* allocate dma push buffer */
  182. ret = nouveau_channel_prep(drm, cli, parent, handle, 0x12000, &chan);
  183. *pchan = chan;
  184. if (ret)
  185. return ret;
  186. /* create channel object */
  187. args.pushbuf = chan->push.handle;
  188. args.ioffset = 0x10000 + chan->push.vma.offset;
  189. args.ilength = 0x02000;
  190. args.engine = engine;
  191. do {
  192. ret = nouveau_object_new(nv_object(cli), parent, handle,
  193. *oclass++, &args, sizeof(args),
  194. &chan->object);
  195. if (ret == 0)
  196. return ret;
  197. } while (*oclass);
  198. nouveau_channel_del(pchan);
  199. return ret;
  200. }
  201. static int
  202. nouveau_channel_dma(struct nouveau_drm *drm, struct nouveau_cli *cli,
  203. u32 parent, u32 handle, struct nouveau_channel **pchan)
  204. {
  205. static const u16 oclasses[] = { NV40_CHANNEL_DMA_CLASS,
  206. NV17_CHANNEL_DMA_CLASS,
  207. NV10_CHANNEL_DMA_CLASS,
  208. NV03_CHANNEL_DMA_CLASS,
  209. 0 };
  210. const u16 *oclass = oclasses;
  211. struct nv03_channel_dma_class args;
  212. struct nouveau_channel *chan;
  213. int ret;
  214. /* allocate dma push buffer */
  215. ret = nouveau_channel_prep(drm, cli, parent, handle, 0x10000, &chan);
  216. *pchan = chan;
  217. if (ret)
  218. return ret;
  219. /* create channel object */
  220. args.pushbuf = chan->push.handle;
  221. args.offset = chan->push.vma.offset;
  222. do {
  223. ret = nouveau_object_new(nv_object(cli), parent, handle,
  224. *oclass++, &args, sizeof(args),
  225. &chan->object);
  226. if (ret == 0)
  227. return ret;
  228. } while (ret && *oclass);
  229. nouveau_channel_del(pchan);
  230. return ret;
  231. }
  232. static int
  233. nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
  234. {
  235. struct nouveau_client *client = nv_client(chan->cli);
  236. struct nouveau_device *device = nv_device(chan->drm->device);
  237. struct nouveau_instmem *imem = nouveau_instmem(device);
  238. struct nouveau_vmmgr *vmm = nouveau_vmmgr(device);
  239. struct nouveau_fb *pfb = nouveau_fb(device);
  240. struct nouveau_software_chan *swch;
  241. struct nouveau_object *object;
  242. struct nv_dma_class args = {};
  243. int ret, i;
  244. /* allocate dma objects to cover all allowed vram, and gart */
  245. if (device->card_type < NV_C0) {
  246. if (device->card_type >= NV_50) {
  247. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
  248. args.start = 0;
  249. args.limit = client->vm->vmm->limit - 1;
  250. } else {
  251. args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
  252. args.start = 0;
  253. args.limit = pfb->ram.size - imem->reserved - 1;
  254. }
  255. ret = nouveau_object_new(nv_object(client), chan->handle, vram,
  256. 0x003d, &args, sizeof(args), &object);
  257. if (ret)
  258. return ret;
  259. if (device->card_type >= NV_50) {
  260. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
  261. args.start = 0;
  262. args.limit = client->vm->vmm->limit - 1;
  263. } else
  264. if (chan->drm->agp.stat == ENABLED) {
  265. args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
  266. args.start = chan->drm->agp.base;
  267. args.limit = chan->drm->agp.base +
  268. chan->drm->agp.size - 1;
  269. } else {
  270. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
  271. args.start = 0;
  272. args.limit = vmm->limit - 1;
  273. }
  274. ret = nouveau_object_new(nv_object(client), chan->handle, gart,
  275. 0x003d, &args, sizeof(args), &object);
  276. if (ret)
  277. return ret;
  278. chan->vram = vram;
  279. chan->gart = gart;
  280. }
  281. /* initialise dma tracking parameters */
  282. switch (nv_hclass(chan->object) & 0x00ff) {
  283. case 0x006b:
  284. case 0x006e:
  285. chan->user_put = 0x40;
  286. chan->user_get = 0x44;
  287. chan->dma.max = (0x10000 / 4) - 2;
  288. break;
  289. default:
  290. chan->user_put = 0x40;
  291. chan->user_get = 0x44;
  292. chan->user_get_hi = 0x60;
  293. chan->dma.ib_base = 0x10000 / 4;
  294. chan->dma.ib_max = (0x02000 / 8) - 1;
  295. chan->dma.ib_put = 0;
  296. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  297. chan->dma.max = chan->dma.ib_base;
  298. break;
  299. }
  300. chan->dma.put = 0;
  301. chan->dma.cur = chan->dma.put;
  302. chan->dma.free = chan->dma.max - chan->dma.cur;
  303. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  304. if (ret)
  305. return ret;
  306. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  307. OUT_RING(chan, 0x00000000);
  308. /* allocate software object class (used for fences on <= nv05, and
  309. * to signal flip completion), bind it to a subchannel.
  310. */
  311. if ((device->card_type < NV_E0) || gart /* nve0: want_nvsw */) {
  312. ret = nouveau_object_new(nv_object(client), chan->handle,
  313. NvSw, nouveau_abi16_swclass(chan->drm),
  314. NULL, 0, &object);
  315. if (ret)
  316. return ret;
  317. swch = (void *)object->parent;
  318. swch->flip = nouveau_flip_complete;
  319. swch->flip_data = chan;
  320. }
  321. if (device->card_type < NV_C0) {
  322. ret = RING_SPACE(chan, 2);
  323. if (ret)
  324. return ret;
  325. BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
  326. OUT_RING (chan, NvSw);
  327. FIRE_RING (chan);
  328. }
  329. /* initialise synchronisation */
  330. return nouveau_fence(chan->drm)->context_new(chan);
  331. }
  332. int
  333. nouveau_channel_new(struct nouveau_drm *drm, struct nouveau_cli *cli,
  334. u32 parent, u32 handle, u32 arg0, u32 arg1,
  335. struct nouveau_channel **pchan)
  336. {
  337. int ret;
  338. ret = nouveau_channel_ind(drm, cli, parent, handle, arg0, pchan);
  339. if (ret) {
  340. NV_DEBUG(cli, "ib channel create, %d\n", ret);
  341. ret = nouveau_channel_dma(drm, cli, parent, handle, pchan);
  342. if (ret) {
  343. NV_DEBUG(cli, "dma channel create, %d\n", ret);
  344. return ret;
  345. }
  346. }
  347. ret = nouveau_channel_init(*pchan, arg0, arg1);
  348. if (ret) {
  349. NV_ERROR(cli, "channel failed to initialise, %d\n", ret);
  350. nouveau_channel_del(pchan);
  351. return ret;
  352. }
  353. return 0;
  354. }