nouveau_chan.c 11 KB

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