nouveau_chan.c 12 KB

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