nouveau_chan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. chan->push.addr = chan->push.vma->addr;
  146. if (device->info.family >= NV_DEVICE_INFO_V0_FERMI)
  147. return 0;
  148. args.target = NV_DMA_V0_TARGET_VM;
  149. args.access = NV_DMA_V0_ACCESS_VM;
  150. args.start = 0;
  151. args.limit = cli->vmm.vmm.limit - 1;
  152. } else
  153. if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
  154. if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
  155. /* nv04 vram pushbuf hack, retarget to its location in
  156. * the framebuffer bar rather than direct vram access..
  157. * nfi why this exists, it came from the -nv ddx.
  158. */
  159. args.target = NV_DMA_V0_TARGET_PCI;
  160. args.access = NV_DMA_V0_ACCESS_RDWR;
  161. args.start = nvxx_device(device)->func->
  162. resource_addr(nvxx_device(device), 1);
  163. args.limit = args.start + device->info.ram_user - 1;
  164. } else {
  165. args.target = NV_DMA_V0_TARGET_VRAM;
  166. args.access = NV_DMA_V0_ACCESS_RDWR;
  167. args.start = 0;
  168. args.limit = device->info.ram_user - 1;
  169. }
  170. } else {
  171. if (chan->drm->agp.bridge) {
  172. args.target = NV_DMA_V0_TARGET_AGP;
  173. args.access = NV_DMA_V0_ACCESS_RDWR;
  174. args.start = chan->drm->agp.base;
  175. args.limit = chan->drm->agp.base +
  176. chan->drm->agp.size - 1;
  177. } else {
  178. args.target = NV_DMA_V0_TARGET_VM;
  179. args.access = NV_DMA_V0_ACCESS_RDWR;
  180. args.start = 0;
  181. args.limit = cli->vmm.vmm.limit - 1;
  182. }
  183. }
  184. ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
  185. &args, sizeof(args), &chan->push.ctxdma);
  186. if (ret) {
  187. nouveau_channel_del(pchan);
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int
  193. nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
  194. u64 runlist, struct nouveau_channel **pchan)
  195. {
  196. struct nouveau_cli *cli = (void *)device->object.client;
  197. static const u16 oclasses[] = { VOLTA_CHANNEL_GPFIFO_A,
  198. PASCAL_CHANNEL_GPFIFO_A,
  199. MAXWELL_CHANNEL_GPFIFO_A,
  200. KEPLER_CHANNEL_GPFIFO_B,
  201. KEPLER_CHANNEL_GPFIFO_A,
  202. FERMI_CHANNEL_GPFIFO,
  203. G82_CHANNEL_GPFIFO,
  204. NV50_CHANNEL_GPFIFO,
  205. 0 };
  206. const u16 *oclass = oclasses;
  207. union {
  208. struct nv50_channel_gpfifo_v0 nv50;
  209. struct fermi_channel_gpfifo_v0 fermi;
  210. struct kepler_channel_gpfifo_a_v0 kepler;
  211. } args;
  212. struct nouveau_channel *chan;
  213. u32 size;
  214. int ret;
  215. /* allocate dma push buffer */
  216. ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
  217. *pchan = chan;
  218. if (ret)
  219. return ret;
  220. /* create channel object */
  221. do {
  222. if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
  223. args.kepler.version = 0;
  224. args.kepler.ilength = 0x02000;
  225. args.kepler.ioffset = 0x10000 + chan->push.addr;
  226. args.kepler.runlist = runlist;
  227. args.kepler.vmm = nvif_handle(&cli->vmm.vmm.object);
  228. size = sizeof(args.kepler);
  229. } else
  230. if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
  231. args.fermi.version = 0;
  232. args.fermi.ilength = 0x02000;
  233. args.fermi.ioffset = 0x10000 + chan->push.addr;
  234. args.fermi.vmm = nvif_handle(&cli->vmm.vmm.object);
  235. size = sizeof(args.fermi);
  236. } else {
  237. args.nv50.version = 0;
  238. args.nv50.ilength = 0x02000;
  239. args.nv50.ioffset = 0x10000 + chan->push.addr;
  240. args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
  241. args.nv50.vmm = nvif_handle(&cli->vmm.vmm.object);
  242. size = sizeof(args.nv50);
  243. }
  244. ret = nvif_object_init(&device->object, 0, *oclass++,
  245. &args, size, &chan->user);
  246. if (ret == 0) {
  247. if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A)
  248. chan->chid = args.kepler.chid;
  249. else
  250. if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO)
  251. chan->chid = args.fermi.chid;
  252. else
  253. chan->chid = args.nv50.chid;
  254. return ret;
  255. }
  256. } while (*oclass);
  257. nouveau_channel_del(pchan);
  258. return ret;
  259. }
  260. static int
  261. nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
  262. struct nouveau_channel **pchan)
  263. {
  264. static const u16 oclasses[] = { NV40_CHANNEL_DMA,
  265. NV17_CHANNEL_DMA,
  266. NV10_CHANNEL_DMA,
  267. NV03_CHANNEL_DMA,
  268. 0 };
  269. const u16 *oclass = oclasses;
  270. struct nv03_channel_dma_v0 args;
  271. struct nouveau_channel *chan;
  272. int ret;
  273. /* allocate dma push buffer */
  274. ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
  275. *pchan = chan;
  276. if (ret)
  277. return ret;
  278. /* create channel object */
  279. args.version = 0;
  280. args.pushbuf = nvif_handle(&chan->push.ctxdma);
  281. args.offset = chan->push.addr;
  282. do {
  283. ret = nvif_object_init(&device->object, 0, *oclass++,
  284. &args, sizeof(args), &chan->user);
  285. if (ret == 0) {
  286. chan->chid = args.chid;
  287. return ret;
  288. }
  289. } while (ret && *oclass);
  290. nouveau_channel_del(pchan);
  291. return ret;
  292. }
  293. static int
  294. nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
  295. {
  296. struct nvif_device *device = chan->device;
  297. struct nouveau_cli *cli = (void *)chan->user.client;
  298. struct nouveau_drm *drm = chan->drm;
  299. struct nv_dma_v0 args = {};
  300. int ret, i;
  301. nvif_object_map(&chan->user, NULL, 0);
  302. if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO) {
  303. ret = nvif_notify_init(&chan->user, nouveau_channel_killed,
  304. true, NV906F_V0_NTFY_KILLED,
  305. NULL, 0, 0, &chan->kill);
  306. if (ret == 0)
  307. ret = nvif_notify_get(&chan->kill);
  308. if (ret) {
  309. NV_ERROR(drm, "Failed to request channel kill "
  310. "notification: %d\n", ret);
  311. return ret;
  312. }
  313. }
  314. /* allocate dma objects to cover all allowed vram, and gart */
  315. if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
  316. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  317. args.target = NV_DMA_V0_TARGET_VM;
  318. args.access = NV_DMA_V0_ACCESS_VM;
  319. args.start = 0;
  320. args.limit = cli->vmm.vmm.limit - 1;
  321. } else {
  322. args.target = NV_DMA_V0_TARGET_VRAM;
  323. args.access = NV_DMA_V0_ACCESS_RDWR;
  324. args.start = 0;
  325. args.limit = device->info.ram_user - 1;
  326. }
  327. ret = nvif_object_init(&chan->user, vram, NV_DMA_IN_MEMORY,
  328. &args, sizeof(args), &chan->vram);
  329. if (ret)
  330. return ret;
  331. if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
  332. args.target = NV_DMA_V0_TARGET_VM;
  333. args.access = NV_DMA_V0_ACCESS_VM;
  334. args.start = 0;
  335. args.limit = cli->vmm.vmm.limit - 1;
  336. } else
  337. if (chan->drm->agp.bridge) {
  338. args.target = NV_DMA_V0_TARGET_AGP;
  339. args.access = NV_DMA_V0_ACCESS_RDWR;
  340. args.start = chan->drm->agp.base;
  341. args.limit = chan->drm->agp.base +
  342. chan->drm->agp.size - 1;
  343. } else {
  344. args.target = NV_DMA_V0_TARGET_VM;
  345. args.access = NV_DMA_V0_ACCESS_RDWR;
  346. args.start = 0;
  347. args.limit = cli->vmm.vmm.limit - 1;
  348. }
  349. ret = nvif_object_init(&chan->user, gart, NV_DMA_IN_MEMORY,
  350. &args, sizeof(args), &chan->gart);
  351. if (ret)
  352. return ret;
  353. }
  354. /* initialise dma tracking parameters */
  355. switch (chan->user.oclass & 0x00ff) {
  356. case 0x006b:
  357. case 0x006e:
  358. chan->user_put = 0x40;
  359. chan->user_get = 0x44;
  360. chan->dma.max = (0x10000 / 4) - 2;
  361. break;
  362. default:
  363. chan->user_put = 0x40;
  364. chan->user_get = 0x44;
  365. chan->user_get_hi = 0x60;
  366. chan->dma.ib_base = 0x10000 / 4;
  367. chan->dma.ib_max = (0x02000 / 8) - 1;
  368. chan->dma.ib_put = 0;
  369. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  370. chan->dma.max = chan->dma.ib_base;
  371. break;
  372. }
  373. chan->dma.put = 0;
  374. chan->dma.cur = chan->dma.put;
  375. chan->dma.free = chan->dma.max - chan->dma.cur;
  376. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  377. if (ret)
  378. return ret;
  379. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  380. OUT_RING(chan, 0x00000000);
  381. /* allocate software object class (used for fences on <= nv05) */
  382. if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  383. ret = nvif_object_init(&chan->user, 0x006e,
  384. NVIF_CLASS_SW_NV04,
  385. NULL, 0, &chan->nvsw);
  386. if (ret)
  387. return ret;
  388. ret = RING_SPACE(chan, 2);
  389. if (ret)
  390. return ret;
  391. BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
  392. OUT_RING (chan, chan->nvsw.handle);
  393. FIRE_RING (chan);
  394. }
  395. /* initialise synchronisation */
  396. return nouveau_fence(chan->drm)->context_new(chan);
  397. }
  398. int
  399. nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
  400. u32 arg0, u32 arg1, struct nouveau_channel **pchan)
  401. {
  402. struct nouveau_cli *cli = (void *)device->object.client;
  403. bool super;
  404. int ret;
  405. /* hack until fencenv50 is fixed, and agp access relaxed */
  406. super = cli->base.super;
  407. cli->base.super = true;
  408. ret = nouveau_channel_ind(drm, device, arg0, pchan);
  409. if (ret) {
  410. NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
  411. ret = nouveau_channel_dma(drm, device, pchan);
  412. if (ret) {
  413. NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
  414. goto done;
  415. }
  416. }
  417. ret = nouveau_channel_init(*pchan, arg0, arg1);
  418. if (ret) {
  419. NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
  420. nouveau_channel_del(pchan);
  421. }
  422. done:
  423. cli->base.super = super;
  424. return ret;
  425. }
  426. int
  427. nouveau_channels_init(struct nouveau_drm *drm)
  428. {
  429. struct {
  430. struct nv_device_info_v1 m;
  431. struct {
  432. struct nv_device_info_v1_data channels;
  433. } v;
  434. } args = {
  435. .m.version = 1,
  436. .m.count = sizeof(args.v) / sizeof(args.v.channels),
  437. .v.channels.mthd = NV_DEVICE_FIFO_CHANNELS,
  438. };
  439. struct nvif_object *device = &drm->client.device.object;
  440. int ret;
  441. ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
  442. if (ret || args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
  443. return -ENODEV;
  444. drm->chan.nr = args.v.channels.data;
  445. drm->chan.context_base = dma_fence_context_alloc(drm->chan.nr);
  446. return 0;
  447. }