nouveau_chan.c 11 KB

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