nouveau_chan.c 12 KB

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