base.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_pause(struct nvkm_fifo *fifo, unsigned long *flags)
  33. {
  34. return fifo->func->pause(fifo, flags);
  35. }
  36. void
  37. nvkm_fifo_start(struct nvkm_fifo *fifo, unsigned long *flags)
  38. {
  39. return fifo->func->start(fifo, flags);
  40. }
  41. void
  42. nvkm_fifo_chan_put(struct nvkm_fifo *fifo, unsigned long flags,
  43. struct nvkm_fifo_chan **pchan)
  44. {
  45. struct nvkm_fifo_chan *chan = *pchan;
  46. if (likely(chan)) {
  47. *pchan = NULL;
  48. spin_unlock_irqrestore(&fifo->lock, flags);
  49. }
  50. }
  51. struct nvkm_fifo_chan *
  52. nvkm_fifo_chan_inst(struct nvkm_fifo *fifo, u64 inst, unsigned long *rflags)
  53. {
  54. struct nvkm_fifo_chan *chan;
  55. unsigned long flags;
  56. spin_lock_irqsave(&fifo->lock, flags);
  57. list_for_each_entry(chan, &fifo->chan, head) {
  58. if (chan->inst->addr == inst) {
  59. list_del(&chan->head);
  60. list_add(&chan->head, &fifo->chan);
  61. *rflags = flags;
  62. return chan;
  63. }
  64. }
  65. spin_unlock_irqrestore(&fifo->lock, flags);
  66. return NULL;
  67. }
  68. struct nvkm_fifo_chan *
  69. nvkm_fifo_chan_chid(struct nvkm_fifo *fifo, int chid, unsigned long *rflags)
  70. {
  71. struct nvkm_fifo_chan *chan;
  72. unsigned long flags;
  73. spin_lock_irqsave(&fifo->lock, flags);
  74. list_for_each_entry(chan, &fifo->chan, head) {
  75. if (chan->chid == chid) {
  76. list_del(&chan->head);
  77. list_add(&chan->head, &fifo->chan);
  78. *rflags = flags;
  79. return chan;
  80. }
  81. }
  82. spin_unlock_irqrestore(&fifo->lock, flags);
  83. return NULL;
  84. }
  85. static int
  86. nvkm_fifo_event_ctor(struct nvkm_object *object, void *data, u32 size,
  87. struct nvkm_notify *notify)
  88. {
  89. if (size == 0) {
  90. notify->size = 0;
  91. notify->types = 1;
  92. notify->index = 0;
  93. return 0;
  94. }
  95. return -ENOSYS;
  96. }
  97. static const struct nvkm_event_func
  98. nvkm_fifo_event_func = {
  99. .ctor = nvkm_fifo_event_ctor,
  100. };
  101. static void
  102. nvkm_fifo_uevent_fini(struct nvkm_event *event, int type, int index)
  103. {
  104. struct nvkm_fifo *fifo = container_of(event, typeof(*fifo), uevent);
  105. fifo->func->uevent_fini(fifo);
  106. }
  107. static void
  108. nvkm_fifo_uevent_init(struct nvkm_event *event, int type, int index)
  109. {
  110. struct nvkm_fifo *fifo = container_of(event, typeof(*fifo), uevent);
  111. fifo->func->uevent_init(fifo);
  112. }
  113. static int
  114. nvkm_fifo_uevent_ctor(struct nvkm_object *object, void *data, u32 size,
  115. struct nvkm_notify *notify)
  116. {
  117. union {
  118. struct nvif_notify_uevent_req none;
  119. } *req = data;
  120. int ret;
  121. if (nvif_unvers(req->none)) {
  122. notify->size = sizeof(struct nvif_notify_uevent_rep);
  123. notify->types = 1;
  124. notify->index = 0;
  125. }
  126. return ret;
  127. }
  128. static const struct nvkm_event_func
  129. nvkm_fifo_uevent_func = {
  130. .ctor = nvkm_fifo_uevent_ctor,
  131. .init = nvkm_fifo_uevent_init,
  132. .fini = nvkm_fifo_uevent_fini,
  133. };
  134. void
  135. nvkm_fifo_uevent(struct nvkm_fifo *fifo)
  136. {
  137. struct nvif_notify_uevent_rep rep = {
  138. };
  139. nvkm_event_send(&fifo->uevent, 1, 0, &rep, sizeof(rep));
  140. }
  141. static int
  142. nvkm_fifo_class_new(struct nvkm_device *device,
  143. const struct nvkm_oclass *oclass, void *data, u32 size,
  144. struct nvkm_object **pobject)
  145. {
  146. const struct nvkm_fifo_chan_oclass *sclass = oclass->engn;
  147. struct nvkm_fifo *fifo = nvkm_fifo(oclass->engine);
  148. return sclass->ctor(fifo, oclass, data, size, pobject);
  149. }
  150. static const struct nvkm_device_oclass
  151. nvkm_fifo_class = {
  152. .ctor = nvkm_fifo_class_new,
  153. };
  154. static int
  155. nvkm_fifo_class_get(struct nvkm_oclass *oclass, int index,
  156. const struct nvkm_device_oclass **class)
  157. {
  158. struct nvkm_fifo *fifo = nvkm_fifo(oclass->engine);
  159. const struct nvkm_fifo_chan_oclass *sclass;
  160. int c = 0;
  161. while ((sclass = fifo->func->chan[c])) {
  162. if (c++ == index) {
  163. oclass->base = sclass->base;
  164. oclass->engn = sclass;
  165. *class = &nvkm_fifo_class;
  166. return 0;
  167. }
  168. }
  169. return c;
  170. }
  171. static void
  172. nvkm_fifo_intr(struct nvkm_engine *engine)
  173. {
  174. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  175. fifo->func->intr(fifo);
  176. }
  177. static int
  178. nvkm_fifo_fini(struct nvkm_engine *engine, bool suspend)
  179. {
  180. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  181. if (fifo->func->fini)
  182. fifo->func->fini(fifo);
  183. return 0;
  184. }
  185. static int
  186. nvkm_fifo_oneinit(struct nvkm_engine *engine)
  187. {
  188. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  189. if (fifo->func->oneinit)
  190. return fifo->func->oneinit(fifo);
  191. return 0;
  192. }
  193. static int
  194. nvkm_fifo_init(struct nvkm_engine *engine)
  195. {
  196. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  197. fifo->func->init(fifo);
  198. return 0;
  199. }
  200. static void *
  201. nvkm_fifo_dtor(struct nvkm_engine *engine)
  202. {
  203. struct nvkm_fifo *fifo = nvkm_fifo(engine);
  204. void *data = fifo;
  205. if (fifo->func->dtor)
  206. data = fifo->func->dtor(fifo);
  207. nvkm_event_fini(&fifo->cevent);
  208. nvkm_event_fini(&fifo->uevent);
  209. return data;
  210. }
  211. static const struct nvkm_engine_func
  212. nvkm_fifo = {
  213. .dtor = nvkm_fifo_dtor,
  214. .oneinit = nvkm_fifo_oneinit,
  215. .init = nvkm_fifo_init,
  216. .fini = nvkm_fifo_fini,
  217. .intr = nvkm_fifo_intr,
  218. .base.sclass = nvkm_fifo_class_get,
  219. };
  220. int
  221. nvkm_fifo_ctor(const struct nvkm_fifo_func *func, struct nvkm_device *device,
  222. int index, int nr, struct nvkm_fifo *fifo)
  223. {
  224. int ret;
  225. fifo->func = func;
  226. INIT_LIST_HEAD(&fifo->chan);
  227. spin_lock_init(&fifo->lock);
  228. if (WARN_ON(fifo->nr > NVKM_FIFO_CHID_NR))
  229. fifo->nr = NVKM_FIFO_CHID_NR;
  230. else
  231. fifo->nr = nr;
  232. bitmap_clear(fifo->mask, 0, fifo->nr);
  233. ret = nvkm_engine_ctor(&nvkm_fifo, device, index, 0x00000100,
  234. true, &fifo->engine);
  235. if (ret)
  236. return ret;
  237. if (func->uevent_init) {
  238. ret = nvkm_event_init(&nvkm_fifo_uevent_func, 1, 1,
  239. &fifo->uevent);
  240. if (ret)
  241. return ret;
  242. }
  243. return nvkm_event_init(&nvkm_fifo_event_func, 1, 1, &fifo->cevent);
  244. }