nouveau_chan.c 12 KB

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