nouveau_chan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_drv.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[] = { PASCAL_CHANNEL_GPFIFO_A,
  175. MAXWELL_CHANNEL_GPFIFO_A,
  176. KEPLER_CHANNEL_GPFIFO_B,
  177. KEPLER_CHANNEL_GPFIFO_A,
  178. FERMI_CHANNEL_GPFIFO,
  179. G82_CHANNEL_GPFIFO,
  180. NV50_CHANNEL_GPFIFO,
  181. 0 };
  182. const u16 *oclass = oclasses;
  183. union {
  184. struct nv50_channel_gpfifo_v0 nv50;
  185. struct fermi_channel_gpfifo_v0 fermi;
  186. struct kepler_channel_gpfifo_a_v0 kepler;
  187. } args;
  188. struct nouveau_channel *chan;
  189. u32 size;
  190. int ret;
  191. /* allocate dma push buffer */
  192. ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
  193. *pchan = chan;
  194. if (ret)
  195. return ret;
  196. /* create channel object */
  197. do {
  198. if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
  199. args.kepler.version = 0;
  200. args.kepler.engines = engine;
  201. args.kepler.ilength = 0x02000;
  202. args.kepler.ioffset = 0x10000 + chan->push.vma.offset;
  203. args.kepler.vm = 0;
  204. size = sizeof(args.kepler);
  205. } else
  206. if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
  207. args.fermi.version = 0;
  208. args.fermi.ilength = 0x02000;
  209. args.fermi.ioffset = 0x10000 + chan->push.vma.offset;
  210. args.fermi.vm = 0;
  211. size = sizeof(args.fermi);
  212. } else {
  213. args.nv50.version = 0;
  214. args.nv50.ilength = 0x02000;
  215. args.nv50.ioffset = 0x10000 + chan->push.vma.offset;
  216. args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
  217. args.nv50.vm = 0;
  218. size = sizeof(args.nv50);
  219. }
  220. ret = nvif_object_init(&device->object, 0, *oclass++,
  221. &args, size, &chan->user);
  222. if (ret == 0) {
  223. if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A)
  224. chan->chid = args.kepler.chid;
  225. else
  226. if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO)
  227. chan->chid = args.fermi.chid;
  228. else
  229. chan->chid = args.nv50.chid;
  230. return ret;
  231. }
  232. } while (*oclass);
  233. nouveau_channel_del(pchan);
  234. return ret;
  235. }
  236. static int
  237. nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
  238. struct nouveau_channel **pchan)
  239. {
  240. static const u16 oclasses[] = { NV40_CHANNEL_DMA,
  241. NV17_CHANNEL_DMA,
  242. NV10_CHANNEL_DMA,
  243. NV03_CHANNEL_DMA,
  244. 0 };
  245. const u16 *oclass = oclasses;
  246. struct nv03_channel_dma_v0 args;
  247. struct nouveau_channel *chan;
  248. int ret;
  249. /* allocate dma push buffer */
  250. ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
  251. *pchan = chan;
  252. if (ret)
  253. return ret;
  254. /* create channel object */
  255. args.version = 0;
  256. args.pushbuf = nvif_handle(&chan->push.ctxdma);
  257. args.offset = chan->push.vma.offset;
  258. do {
  259. ret = nvif_object_init(&device->object, 0, *oclass++,
  260. &args, sizeof(args), &chan->user);
  261. if (ret == 0) {
  262. chan->chid = args.chid;
  263. return ret;
  264. }
  265. } while (ret && *oclass);
  266. nouveau_channel_del(pchan);
  267. return ret;
  268. }
  269. static int
  270. nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
  271. {
  272. struct nvif_device *device = chan->device;
  273. struct nouveau_cli *cli = (void *)chan->user.client;
  274. struct nvkm_mmu *mmu = nvxx_mmu(device);
  275. struct nv_dma_v0 args = {};
  276. int ret, i;
  277. nvif_object_map(&chan->user);
  278. /* allocate dma objects to cover all allowed vram, and gart */
  279. if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
  280. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  281. args.target = NV_DMA_V0_TARGET_VM;
  282. args.access = NV_DMA_V0_ACCESS_VM;
  283. args.start = 0;
  284. args.limit = cli->vm->mmu->limit - 1;
  285. } else {
  286. args.target = NV_DMA_V0_TARGET_VRAM;
  287. args.access = NV_DMA_V0_ACCESS_RDWR;
  288. args.start = 0;
  289. args.limit = device->info.ram_user - 1;
  290. }
  291. ret = nvif_object_init(&chan->user, vram, NV_DMA_IN_MEMORY,
  292. &args, sizeof(args), &chan->vram);
  293. if (ret)
  294. return ret;
  295. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  296. args.target = NV_DMA_V0_TARGET_VM;
  297. args.access = NV_DMA_V0_ACCESS_VM;
  298. args.start = 0;
  299. args.limit = cli->vm->mmu->limit - 1;
  300. } else
  301. if (chan->drm->agp.bridge) {
  302. args.target = NV_DMA_V0_TARGET_AGP;
  303. args.access = NV_DMA_V0_ACCESS_RDWR;
  304. args.start = chan->drm->agp.base;
  305. args.limit = chan->drm->agp.base +
  306. chan->drm->agp.size - 1;
  307. } else {
  308. args.target = NV_DMA_V0_TARGET_VM;
  309. args.access = NV_DMA_V0_ACCESS_RDWR;
  310. args.start = 0;
  311. args.limit = mmu->limit - 1;
  312. }
  313. ret = nvif_object_init(&chan->user, gart, NV_DMA_IN_MEMORY,
  314. &args, sizeof(args), &chan->gart);
  315. if (ret)
  316. return ret;
  317. }
  318. /* initialise dma tracking parameters */
  319. switch (chan->user.oclass & 0x00ff) {
  320. case 0x006b:
  321. case 0x006e:
  322. chan->user_put = 0x40;
  323. chan->user_get = 0x44;
  324. chan->dma.max = (0x10000 / 4) - 2;
  325. break;
  326. default:
  327. chan->user_put = 0x40;
  328. chan->user_get = 0x44;
  329. chan->user_get_hi = 0x60;
  330. chan->dma.ib_base = 0x10000 / 4;
  331. chan->dma.ib_max = (0x02000 / 8) - 1;
  332. chan->dma.ib_put = 0;
  333. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  334. chan->dma.max = chan->dma.ib_base;
  335. break;
  336. }
  337. chan->dma.put = 0;
  338. chan->dma.cur = chan->dma.put;
  339. chan->dma.free = chan->dma.max - chan->dma.cur;
  340. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  341. if (ret)
  342. return ret;
  343. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  344. OUT_RING(chan, 0x00000000);
  345. /* allocate software object class (used for fences on <= nv05) */
  346. if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  347. ret = nvif_object_init(&chan->user, 0x006e,
  348. NVIF_CLASS_SW_NV04,
  349. NULL, 0, &chan->nvsw);
  350. if (ret)
  351. return ret;
  352. ret = RING_SPACE(chan, 2);
  353. if (ret)
  354. return ret;
  355. BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
  356. OUT_RING (chan, chan->nvsw.handle);
  357. FIRE_RING (chan);
  358. }
  359. /* initialise synchronisation */
  360. return nouveau_fence(chan->drm)->context_new(chan);
  361. }
  362. int
  363. nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
  364. u32 arg0, u32 arg1, struct nouveau_channel **pchan)
  365. {
  366. struct nouveau_cli *cli = (void *)device->object.client;
  367. bool super;
  368. int ret;
  369. /* hack until fencenv50 is fixed, and agp access relaxed */
  370. super = cli->base.super;
  371. cli->base.super = true;
  372. ret = nouveau_channel_ind(drm, device, arg0, pchan);
  373. if (ret) {
  374. NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
  375. ret = nouveau_channel_dma(drm, device, pchan);
  376. if (ret) {
  377. NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
  378. goto done;
  379. }
  380. }
  381. ret = nouveau_channel_init(*pchan, arg0, arg1);
  382. if (ret) {
  383. NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
  384. nouveau_channel_del(pchan);
  385. }
  386. done:
  387. cli->base.super = super;
  388. return ret;
  389. }