nouveau_chan.c 12 KB

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