nouveau_chan.c 13 KB

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