nouveau_chan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_drm *drm = chan->drm;
  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(drm, "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. int
  165. nouveau_channel_ind(struct nouveau_drm *drm, struct nouveau_cli *cli,
  166. u32 parent, u32 handle, struct nouveau_channel **pchan)
  167. {
  168. static const u16 oclasses[] = { 0xa06f, 0x906f, 0x826f, 0x506f, 0 };
  169. const u16 *oclass = oclasses;
  170. struct nve0_channel_ind_class args;
  171. struct nouveau_channel *chan;
  172. int ret;
  173. /* allocate dma push buffer */
  174. ret = nouveau_channel_prep(drm, cli, parent, handle, 0x12000, &chan);
  175. *pchan = chan;
  176. if (ret)
  177. return ret;
  178. /* create channel object */
  179. args.pushbuf = chan->push.handle;
  180. args.ioffset = 0x10000 + chan->push.vma.offset;
  181. args.ilength = 0x02000;
  182. args.engine = NVE0_CHANNEL_IND_ENGINE_GR;
  183. do {
  184. ret = nouveau_object_new(nv_object(cli), parent, handle,
  185. *oclass++, &args, sizeof(args),
  186. &chan->object);
  187. if (ret == 0)
  188. return ret;
  189. } while (*oclass);
  190. nouveau_channel_del(pchan);
  191. return ret;
  192. }
  193. static int
  194. nouveau_channel_dma(struct nouveau_drm *drm, struct nouveau_cli *cli,
  195. u32 parent, u32 handle, struct nouveau_channel **pchan)
  196. {
  197. static const u16 oclasses[] = { 0x006e, 0 };
  198. const u16 *oclass = oclasses;
  199. struct nv_channel_dma_class args;
  200. struct nouveau_channel *chan;
  201. int ret;
  202. /* allocate dma push buffer */
  203. ret = nouveau_channel_prep(drm, cli, parent, handle, 0x10000, &chan);
  204. *pchan = chan;
  205. if (ret)
  206. return ret;
  207. /* create channel object */
  208. args.pushbuf = chan->push.handle;
  209. args.offset = chan->push.vma.offset;
  210. do {
  211. ret = nouveau_object_new(nv_object(cli), parent, handle,
  212. *oclass++, &args, sizeof(args),
  213. &chan->object);
  214. if (ret == 0)
  215. return ret;
  216. } while (ret && *oclass);
  217. nouveau_channel_del(pchan);
  218. return ret;
  219. }
  220. static int
  221. nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
  222. {
  223. struct nouveau_client *client = nv_client(chan->cli);
  224. struct nouveau_device *device = nv_device(chan->drm->device);
  225. struct nouveau_instmem *imem = nouveau_instmem(device);
  226. struct nouveau_vmmgr *vmm = nouveau_vmmgr(device);
  227. struct nouveau_fb *pfb = nouveau_fb(device);
  228. struct nouveau_software_chan *swch;
  229. struct nouveau_object *object;
  230. struct nv_dma_class args;
  231. int ret, i;
  232. chan->vram = vram;
  233. chan->gart = gart;
  234. /* allocate dma objects to cover all allowed vram, and gart */
  235. if (device->card_type < NV_C0) {
  236. if (device->card_type >= NV_50) {
  237. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
  238. args.start = 0;
  239. args.limit = client->vm->vmm->limit - 1;
  240. } else {
  241. args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
  242. args.start = 0;
  243. args.limit = pfb->ram.size - imem->reserved - 1;
  244. }
  245. ret = nouveau_object_new(nv_object(client), chan->handle, vram,
  246. 0x003d, &args, sizeof(args), &object);
  247. if (ret)
  248. return ret;
  249. if (device->card_type >= NV_50) {
  250. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
  251. args.start = 0;
  252. args.limit = client->vm->vmm->limit - 1;
  253. } else
  254. if (chan->drm->agp.stat == ENABLED) {
  255. args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
  256. args.start = chan->drm->agp.base;
  257. args.limit = chan->drm->agp.base +
  258. chan->drm->agp.size - 1;
  259. } else {
  260. args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
  261. args.start = 0;
  262. args.limit = vmm->limit - 1;
  263. }
  264. ret = nouveau_object_new(nv_object(client), chan->handle, gart,
  265. 0x003d, &args, sizeof(args), &object);
  266. if (ret)
  267. return ret;
  268. }
  269. /* initialise dma tracking parameters */
  270. switch (nv_hclass(chan->object) & 0xffff) {
  271. case 0x006e:
  272. chan->user_put = 0x40;
  273. chan->user_get = 0x44;
  274. chan->dma.max = (0x10000 / 4) - 2;
  275. break;
  276. default:
  277. chan->user_put = 0x40;
  278. chan->user_get = 0x44;
  279. chan->user_get_hi = 0x60;
  280. chan->dma.ib_base = 0x10000 / 4;
  281. chan->dma.ib_max = (0x02000 / 8) - 1;
  282. chan->dma.ib_put = 0;
  283. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  284. chan->dma.max = chan->dma.ib_base;
  285. break;
  286. }
  287. chan->dma.put = 0;
  288. chan->dma.cur = chan->dma.put;
  289. chan->dma.free = chan->dma.max - chan->dma.cur;
  290. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  291. if (ret)
  292. return ret;
  293. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  294. OUT_RING(chan, 0x00000000);
  295. /* allocate software object class (used for fences on <= nv05, and
  296. * to signal flip completion), bind it to a subchannel.
  297. */
  298. ret = nouveau_object_new(nv_object(client), chan->handle,
  299. NvSw, nouveau_abi16_swclass(chan->drm),
  300. NULL, 0, &object);
  301. if (ret)
  302. return ret;
  303. swch = (void *)object->parent;
  304. swch->flip = nouveau_flip_complete;
  305. swch->flip_data = chan;
  306. if (device->card_type < NV_C0) {
  307. ret = RING_SPACE(chan, 2);
  308. if (ret)
  309. return ret;
  310. BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
  311. OUT_RING (chan, NvSw);
  312. FIRE_RING (chan);
  313. }
  314. /* initialise synchronisation */
  315. return nouveau_fence(chan->drm)->context_new(chan);
  316. }
  317. int
  318. nouveau_channel_new(struct nouveau_drm *drm, struct nouveau_cli *cli,
  319. u32 parent, u32 handle, u32 vram, u32 gart,
  320. struct nouveau_channel **pchan)
  321. {
  322. int ret;
  323. ret = nouveau_channel_ind(drm, cli, parent, handle, pchan);
  324. if (ret) {
  325. NV_DEBUG(drm, "ib channel create, %d\n", ret);
  326. ret = nouveau_channel_dma(drm, cli, parent, handle, pchan);
  327. if (ret) {
  328. NV_DEBUG(drm, "dma channel create, %d\n", ret);
  329. return ret;
  330. }
  331. }
  332. ret = nouveau_channel_init(*pchan, vram, gart);
  333. if (ret) {
  334. NV_ERROR(drm, "channel failed to initialise, %d\n", ret);
  335. nouveau_channel_del(pchan);
  336. return ret;
  337. }
  338. return 0;
  339. }