gpfifogk104.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "changk104.h"
  25. #include <core/client.h>
  26. #include <core/gpuobj.h>
  27. #include <subdev/fb.h>
  28. #include <subdev/mmu.h>
  29. #include <subdev/timer.h>
  30. #include <nvif/class.h>
  31. #include <nvif/unpack.h>
  32. static int
  33. gk104_fifo_gpfifo_kick(struct gk104_fifo_chan *chan)
  34. {
  35. struct gk104_fifo *fifo = chan->fifo;
  36. struct nvkm_subdev *subdev = &fifo->base.engine.subdev;
  37. struct nvkm_device *device = subdev->device;
  38. struct nvkm_client *client = chan->base.object.client;
  39. nvkm_wr32(device, 0x002634, chan->base.chid);
  40. if (nvkm_msec(device, 2000,
  41. if (!(nvkm_rd32(device, 0x002634) & 0x00100000))
  42. break;
  43. ) < 0) {
  44. nvkm_error(subdev, "channel %d [%s] kick timeout\n",
  45. chan->base.chid, client->name);
  46. return -EBUSY;
  47. }
  48. return 0;
  49. }
  50. static u32
  51. gk104_fifo_gpfifo_engine_addr(struct nvkm_engine *engine)
  52. {
  53. switch (engine->subdev.index) {
  54. case NVKM_ENGINE_SW :
  55. case NVKM_ENGINE_CE0 :
  56. case NVKM_ENGINE_CE1 :
  57. case NVKM_ENGINE_CE2 : return 0x0000;
  58. case NVKM_ENGINE_GR : return 0x0210;
  59. case NVKM_ENGINE_MSPDEC: return 0x0250;
  60. case NVKM_ENGINE_MSPPP : return 0x0260;
  61. case NVKM_ENGINE_MSVLD : return 0x0270;
  62. default:
  63. WARN_ON(1);
  64. return 0;
  65. }
  66. }
  67. static int
  68. gk104_fifo_gpfifo_engine_fini(struct nvkm_fifo_chan *base,
  69. struct nvkm_engine *engine, bool suspend)
  70. {
  71. const u32 offset = gk104_fifo_gpfifo_engine_addr(engine);
  72. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  73. struct nvkm_gpuobj *inst = chan->base.inst;
  74. int ret;
  75. ret = gk104_fifo_gpfifo_kick(chan);
  76. if (ret && suspend)
  77. return ret;
  78. if (offset) {
  79. nvkm_kmap(inst);
  80. nvkm_wo32(inst, offset + 0x00, 0x00000000);
  81. nvkm_wo32(inst, offset + 0x04, 0x00000000);
  82. nvkm_done(inst);
  83. }
  84. return ret;
  85. }
  86. static int
  87. gk104_fifo_gpfifo_engine_init(struct nvkm_fifo_chan *base,
  88. struct nvkm_engine *engine)
  89. {
  90. const u32 offset = gk104_fifo_gpfifo_engine_addr(engine);
  91. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  92. struct nvkm_gpuobj *inst = chan->base.inst;
  93. if (offset) {
  94. u64 addr = chan->engn[engine->subdev.index].vma.offset;
  95. nvkm_kmap(inst);
  96. nvkm_wo32(inst, offset + 0x00, lower_32_bits(addr) | 4);
  97. nvkm_wo32(inst, offset + 0x04, upper_32_bits(addr));
  98. nvkm_done(inst);
  99. }
  100. return 0;
  101. }
  102. static void
  103. gk104_fifo_gpfifo_engine_dtor(struct nvkm_fifo_chan *base,
  104. struct nvkm_engine *engine)
  105. {
  106. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  107. nvkm_gpuobj_unmap(&chan->engn[engine->subdev.index].vma);
  108. nvkm_gpuobj_del(&chan->engn[engine->subdev.index].inst);
  109. }
  110. static int
  111. gk104_fifo_gpfifo_engine_ctor(struct nvkm_fifo_chan *base,
  112. struct nvkm_engine *engine,
  113. struct nvkm_object *object)
  114. {
  115. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  116. int engn = engine->subdev.index;
  117. int ret;
  118. if (!gk104_fifo_gpfifo_engine_addr(engine))
  119. return 0;
  120. ret = nvkm_object_bind(object, NULL, 0, &chan->engn[engn].inst);
  121. if (ret)
  122. return ret;
  123. return nvkm_gpuobj_map(chan->engn[engn].inst, chan->vm,
  124. NV_MEM_ACCESS_RW, &chan->engn[engn].vma);
  125. }
  126. static void
  127. gk104_fifo_gpfifo_fini(struct nvkm_fifo_chan *base)
  128. {
  129. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  130. struct gk104_fifo *fifo = chan->fifo;
  131. struct nvkm_device *device = fifo->base.engine.subdev.device;
  132. u32 coff = chan->base.chid * 8;
  133. if (!list_empty(&chan->head)) {
  134. list_del_init(&chan->head);
  135. nvkm_mask(device, 0x800004 + coff, 0x00000800, 0x00000800);
  136. gk104_fifo_runlist_update(fifo, chan->engine);
  137. }
  138. nvkm_wr32(device, 0x800000 + coff, 0x00000000);
  139. }
  140. static void
  141. gk104_fifo_gpfifo_init(struct nvkm_fifo_chan *base)
  142. {
  143. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  144. struct gk104_fifo *fifo = chan->fifo;
  145. struct nvkm_device *device = fifo->base.engine.subdev.device;
  146. u32 addr = chan->base.inst->addr >> 12;
  147. u32 coff = chan->base.chid * 8;
  148. nvkm_mask(device, 0x800004 + coff, 0x000f0000, chan->engine << 16);
  149. nvkm_wr32(device, 0x800000 + coff, 0x80000000 | addr);
  150. if (list_empty(&chan->head) && !chan->killed) {
  151. list_add_tail(&chan->head, &fifo->engine[chan->engine].chan);
  152. nvkm_mask(device, 0x800004 + coff, 0x00000400, 0x00000400);
  153. gk104_fifo_runlist_update(fifo, chan->engine);
  154. nvkm_mask(device, 0x800004 + coff, 0x00000400, 0x00000400);
  155. }
  156. }
  157. static void *
  158. gk104_fifo_gpfifo_dtor(struct nvkm_fifo_chan *base)
  159. {
  160. struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
  161. nvkm_vm_ref(NULL, &chan->vm, chan->pgd);
  162. nvkm_gpuobj_del(&chan->pgd);
  163. return chan;
  164. }
  165. static const struct nvkm_fifo_chan_func
  166. gk104_fifo_gpfifo_func = {
  167. .dtor = gk104_fifo_gpfifo_dtor,
  168. .init = gk104_fifo_gpfifo_init,
  169. .fini = gk104_fifo_gpfifo_fini,
  170. .ntfy = g84_fifo_chan_ntfy,
  171. .engine_ctor = gk104_fifo_gpfifo_engine_ctor,
  172. .engine_dtor = gk104_fifo_gpfifo_engine_dtor,
  173. .engine_init = gk104_fifo_gpfifo_engine_init,
  174. .engine_fini = gk104_fifo_gpfifo_engine_fini,
  175. };
  176. int
  177. gk104_fifo_gpfifo_new(struct nvkm_fifo *base, const struct nvkm_oclass *oclass,
  178. void *data, u32 size, struct nvkm_object **pobject)
  179. {
  180. union {
  181. struct kepler_channel_gpfifo_a_v0 v0;
  182. } *args = data;
  183. struct gk104_fifo *fifo = gk104_fifo(base);
  184. struct nvkm_device *device = fifo->base.engine.subdev.device;
  185. struct nvkm_object *parent = oclass->parent;
  186. struct gk104_fifo_chan *chan;
  187. u64 usermem, ioffset, ilength;
  188. u32 engines;
  189. int ret, i;
  190. nvif_ioctl(parent, "create channel gpfifo size %d\n", size);
  191. if (nvif_unpack(args->v0, 0, 0, false)) {
  192. nvif_ioctl(parent, "create channel gpfifo vers %d vm %llx "
  193. "ioffset %016llx ilength %08x engine %08x\n",
  194. args->v0.version, args->v0.vm, args->v0.ioffset,
  195. args->v0.ilength, args->v0.engine);
  196. } else
  197. return ret;
  198. /* determine which downstream engines are present */
  199. for (i = 0, engines = 0; i < ARRAY_SIZE(fifo->engine); i++) {
  200. u64 subdevs = gk104_fifo_engine_subdev(i);
  201. if (!nvkm_device_engine(device, __ffs64(subdevs)))
  202. continue;
  203. engines |= (1 << i);
  204. }
  205. /* if this is an engine mask query, we're done */
  206. if (!args->v0.engine) {
  207. args->v0.engine = engines;
  208. return nvkm_object_new(oclass, NULL, 0, pobject);
  209. }
  210. /* check that we support a requested engine - note that the user
  211. * argument is a mask in order to allow the user to request (for
  212. * example) *any* copy engine, but doesn't matter which.
  213. */
  214. args->v0.engine &= engines;
  215. if (!args->v0.engine) {
  216. nvif_ioctl(parent, "no supported engine\n");
  217. return -ENODEV;
  218. }
  219. /* allocate the channel */
  220. if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
  221. return -ENOMEM;
  222. *pobject = &chan->base.object;
  223. chan->fifo = fifo;
  224. chan->engine = __ffs(args->v0.engine);
  225. INIT_LIST_HEAD(&chan->head);
  226. ret = nvkm_fifo_chan_ctor(&gk104_fifo_gpfifo_func, &fifo->base,
  227. 0x1000, 0x1000, true, args->v0.vm, 0,
  228. gk104_fifo_engine_subdev(chan->engine),
  229. 1, fifo->user.bar.offset, 0x200,
  230. oclass, &chan->base);
  231. if (ret)
  232. return ret;
  233. args->v0.chid = chan->base.chid;
  234. /* page directory */
  235. ret = nvkm_gpuobj_new(device, 0x10000, 0x1000, false, NULL, &chan->pgd);
  236. if (ret)
  237. return ret;
  238. nvkm_kmap(chan->base.inst);
  239. nvkm_wo32(chan->base.inst, 0x0200, lower_32_bits(chan->pgd->addr));
  240. nvkm_wo32(chan->base.inst, 0x0204, upper_32_bits(chan->pgd->addr));
  241. nvkm_wo32(chan->base.inst, 0x0208, 0xffffffff);
  242. nvkm_wo32(chan->base.inst, 0x020c, 0x000000ff);
  243. nvkm_done(chan->base.inst);
  244. ret = nvkm_vm_ref(chan->base.vm, &chan->vm, chan->pgd);
  245. if (ret)
  246. return ret;
  247. /* clear channel control registers */
  248. usermem = chan->base.chid * 0x200;
  249. ioffset = args->v0.ioffset;
  250. ilength = order_base_2(args->v0.ilength / 8);
  251. nvkm_kmap(fifo->user.mem);
  252. for (i = 0; i < 0x200; i += 4)
  253. nvkm_wo32(fifo->user.mem, usermem + i, 0x00000000);
  254. nvkm_done(fifo->user.mem);
  255. usermem = nvkm_memory_addr(fifo->user.mem) + usermem;
  256. /* RAMFC */
  257. nvkm_kmap(chan->base.inst);
  258. nvkm_wo32(chan->base.inst, 0x08, lower_32_bits(usermem));
  259. nvkm_wo32(chan->base.inst, 0x0c, upper_32_bits(usermem));
  260. nvkm_wo32(chan->base.inst, 0x10, 0x0000face);
  261. nvkm_wo32(chan->base.inst, 0x30, 0xfffff902);
  262. nvkm_wo32(chan->base.inst, 0x48, lower_32_bits(ioffset));
  263. nvkm_wo32(chan->base.inst, 0x4c, upper_32_bits(ioffset) |
  264. (ilength << 16));
  265. nvkm_wo32(chan->base.inst, 0x84, 0x20400000);
  266. nvkm_wo32(chan->base.inst, 0x94, 0x30000001);
  267. nvkm_wo32(chan->base.inst, 0x9c, 0x00000100);
  268. nvkm_wo32(chan->base.inst, 0xac, 0x0000001f);
  269. nvkm_wo32(chan->base.inst, 0xe8, chan->base.chid);
  270. nvkm_wo32(chan->base.inst, 0xb8, 0xf8000000);
  271. nvkm_wo32(chan->base.inst, 0xf8, 0x10003080); /* 0x002310 */
  272. nvkm_wo32(chan->base.inst, 0xfc, 0x10000010); /* 0x002350 */
  273. nvkm_done(chan->base.inst);
  274. return 0;
  275. }
  276. const struct nvkm_fifo_chan_oclass
  277. gk104_fifo_gpfifo_oclass = {
  278. .base.oclass = KEPLER_CHANNEL_GPFIFO_A,
  279. .base.minver = 0,
  280. .base.maxver = 0,
  281. .ctor = gk104_fifo_gpfifo_new,
  282. };