base.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "priv.h"
  25. #include "chan.h"
  26. #include <core/client.h>
  27. #include <core/gpuobj.h>
  28. #include <core/notify.h>
  29. #include <nvif/event.h>
  30. #include <nvif/unpack.h>
  31. void
  32. nvkm_fifo_recover_chan(struct nvkm_fifo *fifo, int chid)
  33. {
  34. unsigned long flags;
  35. if (WARN_ON(!fifo->func->recover_chan))
  36. return;
  37. spin_lock_irqsave(&fifo->lock, flags);
  38. fifo->func->recover_chan(fifo, chid);
  39. spin_unlock_irqrestore(&fifo->lock, flags);
  40. }
  41. void
  42. nvkm_fifo_pause(struct nvkm_fifo *fifo, unsigned long *flags)
  43. {
  44. return fifo->func->pause(fifo, flags);
  45. }
  46. void
  47. nvkm_fifo_start(struct nvkm_fifo *fifo, unsigned long *flags)
  48. {
  49. return fifo->func->start(fifo, flags);
  50. }
  51. void
  52. nvkm_fifo_chan_put(struct nvkm_fifo *fifo, unsigned long flags,
  53. struct nvkm_fifo_chan **pchan)
  54. {
  55. struct nvkm_fifo_chan *chan = *pchan;
  56. if (likely(chan)) {
  57. *pchan = NULL;
  58. spin_unlock_irqrestore(&fifo->lock, flags);
  59. }
  60. }
  61. struct nvkm_fifo_chan *
  62. nvkm_fifo_chan_inst_locked(struct nvkm_fifo *fifo, u64 inst)
  63. {
  64. struct nvkm_fifo_chan *chan;
  65. list_for_each_entry(chan, &fifo->chan, head) {
  66. if (chan->inst->addr == inst) {
  67. list_del(&chan->head);
  68. list_add(&chan->head, &fifo->chan);
  69. return chan;
  70. }
  71. }
  72. return NULL;
  73. }
  74. struct nvkm_fifo_chan *
  75. nvkm_fifo_chan_inst(struct nvkm_fifo *fifo, u64 inst, unsigned long *rflags)
  76. {
  77. struct nvkm_fifo_chan *chan;
  78. unsigned long flags;
  79. spin_lock_irqsave(&fifo->lock, flags);
  80. if ((chan = nvkm_fifo_chan_inst_locked(fifo, inst))) {
  81. *rflags = flags;
  82. return chan;
  83. }
  84. spin_unlock_irqrestore(&fifo->lock, flags);
  85. return NULL;
  86. }
  87. struct nvkm_fifo_chan *
  88. nvkm_fifo_chan_chid(struct nvkm_fifo *fifo, int chid, unsigned long *rflags)
  89. {
  90. struct nvkm_fifo_chan *chan;
  91. unsigned long flags;
  92. spin_lock_irqsave(&fifo->lock, flags);
  93. list_for_each_entry(chan, &fifo->chan, head) {
  94. if (chan->chid == chid) {
  95. list_del(&chan->head);
  96. list_add(&chan->head, &fifo->chan);
  97. *rflags = flags;
  98. return chan;
  99. }
  100. }
  101. spin_unlock_irqrestore(&fifo->lock, flags);
  102. return NULL;
  103. }
  104. void
  105. nvkm_fifo_kevent(struct nvkm_fifo *fifo, int chid)
  106. {
  107. nvkm_event_send(&fifo->kevent, 1, chid, NULL, 0);
  108. }
  109. static int
  110. nvkm_fifo_kevent_ctor(struct nvkm_object *object, void *data, u32 size,
  111. struct nvkm_notify *notify)
  112. {
  113. struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
  114. if (size == 0) {
  115. notify->size = 0;
  116. notify->types = 1;
  117. notify->index = chan->chid;
  118. return 0;
  119. }
  120. return -ENOSYS;
  121. }
  122. static const struct nvkm_event_func
  123. nvkm_fifo_kevent_func = {
  124. .ctor = nvkm_fifo_kevent_ctor,
  125. };
  126. static int
  127. nvkm_fifo_cevent_ctor(struct nvkm_object *object, void *data, u32 size,
  128. struct nvkm_notify *notify)
  129. {
  130. if (size == 0) {
  131. notify->size = 0;
  132. notify->types = 1;
  133. notify->index = 0;
  134. return 0;
  135. }
  136. return -ENOSYS;
  137. }
  138. static const struct nvkm_event_func
  139. nvkm_fifo_cevent_func = {
  140. .ctor = nvkm_fifo_cevent_ctor,
  141. };
  142. void
  143. nvkm_fifo_cevent(struct nvkm_fifo *fifo)
  144. {
  145. nvkm_event_send(&fifo->cevent, 1, 0, NULL, 0);
  146. }
  147. static void
  148. nvkm_fifo_uevent_fini(struct nvkm_event *event, int type, int index)
  149. {
  150. struct nvkm_fifo *fifo = container_of(event, typeof(*fifo), uevent);
  151. fifo->func->uevent_fini(fifo);
  152. }
  153. static void
  154. nvkm_fifo_uevent_init(struct nvkm_event *event, int type, int index)
  155. {
  156. struct nvkm_fifo *fifo = container_of(event, typeof(*fifo), uevent);
  157. fifo->func->uevent_init(fifo);
  158. }
  159. static int
  160. nvkm_fifo_uevent_ctor(struct nvkm_object *object, void *data, u32 size,
  161. struct nvkm_notify *notify)
  162. {
  163. union {
  164. struct nvif_notify_uevent_req none;
  165. } *req = data;
  166. int ret = -ENOSYS;
  167. if (!(ret = nvif_unvers(ret, &data, &size, req->none))) {
  168. notify->size = sizeof(struct nvif_notify_uevent_rep);
  169. notify->types = 1;
  170. notify->index = 0;
  171. }
  172. return ret;
  173. }
  174. static const struct nvkm_event_func
  175. nvkm_fifo_uevent_func = {
  176. .ctor = nvkm_fifo_uevent_ctor,
  177. .init = nvkm_fifo_uevent_init,
  178. .fini = nvkm_fifo_uevent_fini,
  179. };
  180. void
  181. nvkm_fifo_uevent(struct nvkm_fifo *fifo)
  182. {
  183. struct nvif_notify_uevent_rep rep = {
  184. };
  185. nvkm_event_send(&fifo->uevent, 1, 0, &rep, sizeof(rep));
  186. }
  187. static int
  188. nvkm_fifo_class_new(struct nvkm_device *device,
  189. const struct nvkm_oclass *oclass, void *data, u32 size,
  190. struct nvkm_object **pobject)
  191. {
  192. const struct nvkm_fifo_chan_oclass *sclass = oclass->engn;
  193. struct nvkm_fifo *fifo = nvkm_fifo(oclass->engine);
  194. return sclass->ctor(fifo, oclass, data, size, pobject);
  195. }
  196. static const struct nvkm_device_oclass
  197. nvkm_fifo_class = {
  198. .ctor = nvkm_fifo_class_new,
  199. };
  200. static int
  201. nvkm_fifo_class_get(struct nvkm_oclass *oclass, int index,
  202. const struct nvkm_device_oclass **class)
  203. {
  204. struct nvkm_fifo *fifo = nvkm_fifo(oclass->engine);
  205. const struct nvkm_fifo_chan_oclass *sclass;
  206. int c = 0;
  207. if (fifo->func->class_get) {
  208. int ret = fifo->func->class_get(fifo, index, &sclass);
  209. if (ret == 0) {
  210. oclass->base = sclass->base;
  211. oclass->engn = sclass;
  212. *class = &nvkm_fifo_class;
  213. return 0;
  214. }
  215. return ret;
  216. }
  217. while ((sclass = fifo->func->chan[c])) {
  218. if (c++ == index) {
  219. oclass->base = sclass->base;
  220. oclass->engn = sclass;
  221. *class = &nvkm_fifo_class;
  222. return 0;
  223. }
  224. }
  225. return c;
  226. }
  227. static void
  228. nvkm_fifo_intr(struct nvkm_engine *engine)
  229. {
  230. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  231. fifo->func->intr(fifo);
  232. }
  233. static int
  234. nvkm_fifo_fini(struct nvkm_engine *engine, bool suspend)
  235. {
  236. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  237. if (fifo->func->fini)
  238. fifo->func->fini(fifo);
  239. return 0;
  240. }
  241. static int
  242. nvkm_fifo_oneinit(struct nvkm_engine *engine)
  243. {
  244. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  245. if (fifo->func->oneinit)
  246. return fifo->func->oneinit(fifo);
  247. return 0;
  248. }
  249. static int
  250. nvkm_fifo_init(struct nvkm_engine *engine)
  251. {
  252. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  253. fifo->func->init(fifo);
  254. return 0;
  255. }
  256. static void *
  257. nvkm_fifo_dtor(struct nvkm_engine *engine)
  258. {
  259. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  260. void *data = fifo;
  261. if (fifo->func->dtor)
  262. data = fifo->func->dtor(fifo);
  263. nvkm_event_fini(&fifo->kevent);
  264. nvkm_event_fini(&fifo->cevent);
  265. nvkm_event_fini(&fifo->uevent);
  266. return data;
  267. }
  268. static const struct nvkm_engine_func
  269. nvkm_fifo = {
  270. .dtor = nvkm_fifo_dtor,
  271. .oneinit = nvkm_fifo_oneinit,
  272. .init = nvkm_fifo_init,
  273. .fini = nvkm_fifo_fini,
  274. .intr = nvkm_fifo_intr,
  275. .base.sclass = nvkm_fifo_class_get,
  276. };
  277. int
  278. nvkm_fifo_ctor(const struct nvkm_fifo_func *func, struct nvkm_device *device,
  279. int index, int nr, struct nvkm_fifo *fifo)
  280. {
  281. int ret;
  282. fifo->func = func;
  283. INIT_LIST_HEAD(&fifo->chan);
  284. spin_lock_init(&fifo->lock);
  285. if (WARN_ON(fifo->nr > NVKM_FIFO_CHID_NR))
  286. fifo->nr = NVKM_FIFO_CHID_NR;
  287. else
  288. fifo->nr = nr;
  289. bitmap_clear(fifo->mask, 0, fifo->nr);
  290. ret = nvkm_engine_ctor(&nvkm_fifo, device, index, true, &fifo->engine);
  291. if (ret)
  292. return ret;
  293. if (func->uevent_init) {
  294. ret = nvkm_event_init(&nvkm_fifo_uevent_func, 1, 1,
  295. &fifo->uevent);
  296. if (ret)
  297. return ret;
  298. }
  299. ret = nvkm_event_init(&nvkm_fifo_cevent_func, 1, 1, &fifo->cevent);
  300. if (ret)
  301. return ret;
  302. return nvkm_event_init(&nvkm_fifo_kevent_func, 1, nr, &fifo->kevent);
  303. }