nouveau_chan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 <nvif/os.h>
  25. #include <nvif/class.h>
  26. #include <nvif/cl0002.h>
  27. #include <nvif/cl006b.h>
  28. #include <nvif/cl506f.h>
  29. #include <nvif/cl906f.h>
  30. #include <nvif/cla06f.h>
  31. #include <nvif/ioctl.h>
  32. /*XXX*/
  33. #include <core/client.h>
  34. #include "nouveau_drm.h"
  35. #include "nouveau_dma.h"
  36. #include "nouveau_bo.h"
  37. #include "nouveau_chan.h"
  38. #include "nouveau_fence.h"
  39. #include "nouveau_abi16.h"
  40. MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
  41. int nouveau_vram_pushbuf;
  42. module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
  43. int
  44. nouveau_channel_idle(struct nouveau_channel *chan)
  45. {
  46. if (likely(chan && chan->fence)) {
  47. struct nouveau_cli *cli = (void *)chan->user.client;
  48. struct nouveau_fence *fence = NULL;
  49. int ret;
  50. ret = nouveau_fence_new(chan, false, &fence);
  51. if (!ret) {
  52. ret = nouveau_fence_wait(fence, false, false);
  53. nouveau_fence_unref(&fence);
  54. }
  55. if (ret) {
  56. NV_PRINTK(err, cli, "failed to idle channel %d [%s]\n",
  57. chan->chid, nvxx_client(&cli->base)->name);
  58. return ret;
  59. }
  60. }
  61. return 0;
  62. }
  63. void
  64. nouveau_channel_del(struct nouveau_channel **pchan)
  65. {
  66. struct nouveau_channel *chan = *pchan;
  67. if (chan) {
  68. if (chan->fence)
  69. nouveau_fence(chan->drm)->context_del(chan);
  70. nvif_object_fini(&chan->nvsw);
  71. nvif_object_fini(&chan->gart);
  72. nvif_object_fini(&chan->vram);
  73. nvif_object_fini(&chan->user);
  74. nvif_object_fini(&chan->push.ctxdma);
  75. nouveau_bo_vma_del(chan->push.buffer, &chan->push.vma);
  76. nouveau_bo_unmap(chan->push.buffer);
  77. if (chan->push.buffer && chan->push.buffer->pin_refcnt)
  78. nouveau_bo_unpin(chan->push.buffer);
  79. nouveau_bo_ref(NULL, &chan->push.buffer);
  80. kfree(chan);
  81. }
  82. *pchan = NULL;
  83. }
  84. static int
  85. nouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
  86. u32 size, struct nouveau_channel **pchan)
  87. {
  88. struct nouveau_cli *cli = (void *)device->object.client;
  89. struct nvkm_mmu *mmu = nvxx_mmu(device);
  90. struct nv_dma_v0 args = {};
  91. struct nouveau_channel *chan;
  92. u32 target;
  93. int ret;
  94. chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
  95. if (!chan)
  96. return -ENOMEM;
  97. chan->device = device;
  98. chan->drm = drm;
  99. /* allocate memory for dma push buffer */
  100. target = TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
  101. if (nouveau_vram_pushbuf)
  102. target = TTM_PL_FLAG_VRAM;
  103. ret = nouveau_bo_new(drm->dev, size, 0, target, 0, 0, NULL, NULL,
  104. &chan->push.buffer);
  105. if (ret == 0) {
  106. ret = nouveau_bo_pin(chan->push.buffer, target, false);
  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. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  120. ret = nouveau_bo_vma_add(chan->push.buffer, cli->vm,
  121. &chan->push.vma);
  122. if (ret) {
  123. nouveau_channel_del(pchan);
  124. return ret;
  125. }
  126. args.target = NV_DMA_V0_TARGET_VM;
  127. args.access = NV_DMA_V0_ACCESS_VM;
  128. args.start = 0;
  129. args.limit = cli->vm->mmu->limit - 1;
  130. } else
  131. if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
  132. if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
  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.target = NV_DMA_V0_TARGET_PCI;
  138. args.access = NV_DMA_V0_ACCESS_RDWR;
  139. args.start = nvxx_device(device)->func->
  140. resource_addr(nvxx_device(device), 1);
  141. args.limit = args.start + device->info.ram_user - 1;
  142. } else {
  143. args.target = NV_DMA_V0_TARGET_VRAM;
  144. args.access = NV_DMA_V0_ACCESS_RDWR;
  145. args.start = 0;
  146. args.limit = device->info.ram_user - 1;
  147. }
  148. } else {
  149. if (chan->drm->agp.bridge) {
  150. args.target = NV_DMA_V0_TARGET_AGP;
  151. args.access = NV_DMA_V0_ACCESS_RDWR;
  152. args.start = chan->drm->agp.base;
  153. args.limit = chan->drm->agp.base +
  154. chan->drm->agp.size - 1;
  155. } else {
  156. args.target = NV_DMA_V0_TARGET_VM;
  157. args.access = NV_DMA_V0_ACCESS_RDWR;
  158. args.start = 0;
  159. args.limit = mmu->limit - 1;
  160. }
  161. }
  162. ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
  163. &args, sizeof(args), &chan->push.ctxdma);
  164. if (ret) {
  165. nouveau_channel_del(pchan);
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static int
  171. nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
  172. u32 engine, struct nouveau_channel **pchan)
  173. {
  174. static const u16 oclasses[] = { MAXWELL_CHANNEL_GPFIFO_A,
  175. KEPLER_CHANNEL_GPFIFO_B,
  176. KEPLER_CHANNEL_GPFIFO_A,
  177. FERMI_CHANNEL_GPFIFO,
  178. G82_CHANNEL_GPFIFO,
  179. NV50_CHANNEL_GPFIFO,
  180. 0 };
  181. const u16 *oclass = oclasses;
  182. union {
  183. struct nv50_channel_gpfifo_v0 nv50;
  184. struct fermi_channel_gpfifo_v0 fermi;
  185. struct kepler_channel_gpfifo_a_v0 kepler;
  186. } args;
  187. struct nouveau_channel *chan;
  188. u32 size;
  189. int ret;
  190. /* allocate dma push buffer */
  191. ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
  192. *pchan = chan;
  193. if (ret)
  194. return ret;
  195. /* create channel object */
  196. do {
  197. if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
  198. args.kepler.version = 0;
  199. args.kepler.engines = engine;
  200. args.kepler.ilength = 0x02000;
  201. args.kepler.ioffset = 0x10000 + chan->push.vma.offset;
  202. args.kepler.vm = 0;
  203. size = sizeof(args.kepler);
  204. } else
  205. if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
  206. args.fermi.version = 0;
  207. args.fermi.ilength = 0x02000;
  208. args.fermi.ioffset = 0x10000 + chan->push.vma.offset;
  209. args.fermi.vm = 0;
  210. size = sizeof(args.fermi);
  211. } else {
  212. args.nv50.version = 0;
  213. args.nv50.ilength = 0x02000;
  214. args.nv50.ioffset = 0x10000 + chan->push.vma.offset;
  215. args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
  216. args.nv50.vm = 0;
  217. size = sizeof(args.nv50);
  218. }
  219. ret = nvif_object_init(&device->object, 0, *oclass++,
  220. &args, size, &chan->user);
  221. if (ret == 0) {
  222. if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A)
  223. chan->chid = args.kepler.chid;
  224. else
  225. if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO)
  226. chan->chid = args.fermi.chid;
  227. else
  228. chan->chid = args.nv50.chid;
  229. return ret;
  230. }
  231. } while (*oclass);
  232. nouveau_channel_del(pchan);
  233. return ret;
  234. }
  235. static int
  236. nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
  237. struct nouveau_channel **pchan)
  238. {
  239. static const u16 oclasses[] = { NV40_CHANNEL_DMA,
  240. NV17_CHANNEL_DMA,
  241. NV10_CHANNEL_DMA,
  242. NV03_CHANNEL_DMA,
  243. 0 };
  244. const u16 *oclass = oclasses;
  245. struct nv03_channel_dma_v0 args;
  246. struct nouveau_channel *chan;
  247. int ret;
  248. /* allocate dma push buffer */
  249. ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
  250. *pchan = chan;
  251. if (ret)
  252. return ret;
  253. /* create channel object */
  254. args.version = 0;
  255. args.pushbuf = nvif_handle(&chan->push.ctxdma);
  256. args.offset = chan->push.vma.offset;
  257. do {
  258. ret = nvif_object_init(&device->object, 0, *oclass++,
  259. &args, sizeof(args), &chan->user);
  260. if (ret == 0) {
  261. chan->chid = args.chid;
  262. return ret;
  263. }
  264. } while (ret && *oclass);
  265. nouveau_channel_del(pchan);
  266. return ret;
  267. }
  268. static int
  269. nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
  270. {
  271. struct nvif_device *device = chan->device;
  272. struct nouveau_cli *cli = (void *)chan->user.client;
  273. struct nvkm_mmu *mmu = nvxx_mmu(device);
  274. struct nv_dma_v0 args = {};
  275. int ret, i;
  276. nvif_object_map(&chan->user);
  277. /* allocate dma objects to cover all allowed vram, and gart */
  278. if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
  279. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  280. args.target = NV_DMA_V0_TARGET_VM;
  281. args.access = NV_DMA_V0_ACCESS_VM;
  282. args.start = 0;
  283. args.limit = cli->vm->mmu->limit - 1;
  284. } else {
  285. args.target = NV_DMA_V0_TARGET_VRAM;
  286. args.access = NV_DMA_V0_ACCESS_RDWR;
  287. args.start = 0;
  288. args.limit = device->info.ram_user - 1;
  289. }
  290. ret = nvif_object_init(&chan->user, vram, NV_DMA_IN_MEMORY,
  291. &args, sizeof(args), &chan->vram);
  292. if (ret)
  293. return ret;
  294. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  295. args.target = NV_DMA_V0_TARGET_VM;
  296. args.access = NV_DMA_V0_ACCESS_VM;
  297. args.start = 0;
  298. args.limit = cli->vm->mmu->limit - 1;
  299. } else
  300. if (chan->drm->agp.bridge) {
  301. args.target = NV_DMA_V0_TARGET_AGP;
  302. args.access = NV_DMA_V0_ACCESS_RDWR;
  303. args.start = chan->drm->agp.base;
  304. args.limit = chan->drm->agp.base +
  305. chan->drm->agp.size - 1;
  306. } else {
  307. args.target = NV_DMA_V0_TARGET_VM;
  308. args.access = NV_DMA_V0_ACCESS_RDWR;
  309. args.start = 0;
  310. args.limit = mmu->limit - 1;
  311. }
  312. ret = nvif_object_init(&chan->user, gart, NV_DMA_IN_MEMORY,
  313. &args, sizeof(args), &chan->gart);
  314. if (ret)
  315. return ret;
  316. }
  317. /* initialise dma tracking parameters */
  318. switch (chan->user.oclass & 0x00ff) {
  319. case 0x006b:
  320. case 0x006e:
  321. chan->user_put = 0x40;
  322. chan->user_get = 0x44;
  323. chan->dma.max = (0x10000 / 4) - 2;
  324. break;
  325. default:
  326. chan->user_put = 0x40;
  327. chan->user_get = 0x44;
  328. chan->user_get_hi = 0x60;
  329. chan->dma.ib_base = 0x10000 / 4;
  330. chan->dma.ib_max = (0x02000 / 8) - 1;
  331. chan->dma.ib_put = 0;
  332. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  333. chan->dma.max = chan->dma.ib_base;
  334. break;
  335. }
  336. chan->dma.put = 0;
  337. chan->dma.cur = chan->dma.put;
  338. chan->dma.free = chan->dma.max - chan->dma.cur;
  339. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  340. if (ret)
  341. return ret;
  342. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  343. OUT_RING(chan, 0x00000000);
  344. /* allocate software object class (used for fences on <= nv05) */
  345. if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  346. ret = nvif_object_init(&chan->user, 0x006e,
  347. NVIF_CLASS_SW_NV04,
  348. NULL, 0, &chan->nvsw);
  349. if (ret)
  350. return ret;
  351. ret = RING_SPACE(chan, 2);
  352. if (ret)
  353. return ret;
  354. BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
  355. OUT_RING (chan, chan->nvsw.handle);
  356. FIRE_RING (chan);
  357. }
  358. /* initialise synchronisation */
  359. return nouveau_fence(chan->drm)->context_new(chan);
  360. }
  361. int
  362. nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
  363. u32 arg0, u32 arg1, struct nouveau_channel **pchan)
  364. {
  365. struct nouveau_cli *cli = (void *)device->object.client;
  366. bool super;
  367. int ret;
  368. /* hack until fencenv50 is fixed, and agp access relaxed */
  369. super = cli->base.super;
  370. cli->base.super = true;
  371. ret = nouveau_channel_ind(drm, device, arg0, pchan);
  372. if (ret) {
  373. NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
  374. ret = nouveau_channel_dma(drm, device, pchan);
  375. if (ret) {
  376. NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
  377. goto done;
  378. }
  379. }
  380. ret = nouveau_channel_init(*pchan, arg0, arg1);
  381. if (ret) {
  382. NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
  383. nouveau_channel_del(pchan);
  384. }
  385. done:
  386. cli->base.super = super;
  387. return ret;
  388. }