intr.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Tegra host1x Interrupt Management
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <linux/irq.h>
  22. #include <trace/events/host1x.h>
  23. #include "channel.h"
  24. #include "dev.h"
  25. #include "intr.h"
  26. /* Wait list management */
  27. enum waitlist_state {
  28. WLS_PENDING,
  29. WLS_REMOVED,
  30. WLS_CANCELLED,
  31. WLS_HANDLED
  32. };
  33. static void waiter_release(struct kref *kref)
  34. {
  35. kfree(container_of(kref, struct host1x_waitlist, refcount));
  36. }
  37. /*
  38. * add a waiter to a waiter queue, sorted by threshold
  39. * returns true if it was added at the head of the queue
  40. */
  41. static bool add_waiter_to_queue(struct host1x_waitlist *waiter,
  42. struct list_head *queue)
  43. {
  44. struct host1x_waitlist *pos;
  45. u32 thresh = waiter->thresh;
  46. list_for_each_entry_reverse(pos, queue, list)
  47. if ((s32)(pos->thresh - thresh) <= 0) {
  48. list_add(&waiter->list, &pos->list);
  49. return false;
  50. }
  51. list_add(&waiter->list, queue);
  52. return true;
  53. }
  54. /*
  55. * run through a waiter queue for a single sync point ID
  56. * and gather all completed waiters into lists by actions
  57. */
  58. static void remove_completed_waiters(struct list_head *head, u32 sync,
  59. struct list_head completed[HOST1X_INTR_ACTION_COUNT])
  60. {
  61. struct list_head *dest;
  62. struct host1x_waitlist *waiter, *next, *prev;
  63. list_for_each_entry_safe(waiter, next, head, list) {
  64. if ((s32)(waiter->thresh - sync) > 0)
  65. break;
  66. dest = completed + waiter->action;
  67. /* consolidate submit cleanups */
  68. if (waiter->action == HOST1X_INTR_ACTION_SUBMIT_COMPLETE &&
  69. !list_empty(dest)) {
  70. prev = list_entry(dest->prev,
  71. struct host1x_waitlist, list);
  72. if (prev->data == waiter->data) {
  73. prev->count++;
  74. dest = NULL;
  75. }
  76. }
  77. /* PENDING->REMOVED or CANCELLED->HANDLED */
  78. if (atomic_inc_return(&waiter->state) == WLS_HANDLED || !dest) {
  79. list_del(&waiter->list);
  80. kref_put(&waiter->refcount, waiter_release);
  81. } else
  82. list_move_tail(&waiter->list, dest);
  83. }
  84. }
  85. static void reset_threshold_interrupt(struct host1x *host,
  86. struct list_head *head,
  87. unsigned int id)
  88. {
  89. u32 thresh =
  90. list_first_entry(head, struct host1x_waitlist, list)->thresh;
  91. host1x_hw_intr_set_syncpt_threshold(host, id, thresh);
  92. host1x_hw_intr_enable_syncpt_intr(host, id);
  93. }
  94. static void action_submit_complete(struct host1x_waitlist *waiter)
  95. {
  96. struct host1x_channel *channel = waiter->data;
  97. host1x_cdma_update(&channel->cdma);
  98. /* Add nr_completed to trace */
  99. trace_host1x_channel_submit_complete(dev_name(channel->dev),
  100. waiter->count, waiter->thresh);
  101. }
  102. static void action_wakeup(struct host1x_waitlist *waiter)
  103. {
  104. wait_queue_head_t *wq = waiter->data;
  105. wake_up(wq);
  106. }
  107. static void action_wakeup_interruptible(struct host1x_waitlist *waiter)
  108. {
  109. wait_queue_head_t *wq = waiter->data;
  110. wake_up_interruptible(wq);
  111. }
  112. typedef void (*action_handler)(struct host1x_waitlist *waiter);
  113. static action_handler action_handlers[HOST1X_INTR_ACTION_COUNT] = {
  114. action_submit_complete,
  115. action_wakeup,
  116. action_wakeup_interruptible,
  117. };
  118. static void run_handlers(struct list_head completed[HOST1X_INTR_ACTION_COUNT])
  119. {
  120. struct list_head *head = completed;
  121. int i;
  122. for (i = 0; i < HOST1X_INTR_ACTION_COUNT; ++i, ++head) {
  123. action_handler handler = action_handlers[i];
  124. struct host1x_waitlist *waiter, *next;
  125. list_for_each_entry_safe(waiter, next, head, list) {
  126. list_del(&waiter->list);
  127. handler(waiter);
  128. WARN_ON(atomic_xchg(&waiter->state, WLS_HANDLED) !=
  129. WLS_REMOVED);
  130. kref_put(&waiter->refcount, waiter_release);
  131. }
  132. }
  133. }
  134. /*
  135. * Remove & handle all waiters that have completed for the given syncpt
  136. */
  137. static int process_wait_list(struct host1x *host,
  138. struct host1x_syncpt *syncpt,
  139. u32 threshold)
  140. {
  141. struct list_head completed[HOST1X_INTR_ACTION_COUNT];
  142. unsigned int i;
  143. int empty;
  144. for (i = 0; i < HOST1X_INTR_ACTION_COUNT; ++i)
  145. INIT_LIST_HEAD(completed + i);
  146. spin_lock(&syncpt->intr.lock);
  147. remove_completed_waiters(&syncpt->intr.wait_head, threshold,
  148. completed);
  149. empty = list_empty(&syncpt->intr.wait_head);
  150. if (empty)
  151. host1x_hw_intr_disable_syncpt_intr(host, syncpt->id);
  152. else
  153. reset_threshold_interrupt(host, &syncpt->intr.wait_head,
  154. syncpt->id);
  155. spin_unlock(&syncpt->intr.lock);
  156. run_handlers(completed);
  157. return empty;
  158. }
  159. /*
  160. * Sync point threshold interrupt service thread function
  161. * Handles sync point threshold triggers, in thread context
  162. */
  163. static void syncpt_thresh_work(struct work_struct *work)
  164. {
  165. struct host1x_syncpt_intr *syncpt_intr =
  166. container_of(work, struct host1x_syncpt_intr, work);
  167. struct host1x_syncpt *syncpt =
  168. container_of(syncpt_intr, struct host1x_syncpt, intr);
  169. unsigned int id = syncpt->id;
  170. struct host1x *host = syncpt->host;
  171. (void)process_wait_list(host, syncpt,
  172. host1x_syncpt_load(host->syncpt + id));
  173. }
  174. int host1x_intr_add_action(struct host1x *host, u32 id, u32 thresh,
  175. enum host1x_intr_action action, void *data,
  176. struct host1x_waitlist *waiter, void **ref)
  177. {
  178. struct host1x_syncpt *syncpt;
  179. int queue_was_empty;
  180. if (waiter == NULL) {
  181. pr_warn("%s: NULL waiter\n", __func__);
  182. return -EINVAL;
  183. }
  184. /* initialize a new waiter */
  185. INIT_LIST_HEAD(&waiter->list);
  186. kref_init(&waiter->refcount);
  187. if (ref)
  188. kref_get(&waiter->refcount);
  189. waiter->thresh = thresh;
  190. waiter->action = action;
  191. atomic_set(&waiter->state, WLS_PENDING);
  192. waiter->data = data;
  193. waiter->count = 1;
  194. syncpt = host->syncpt + id;
  195. spin_lock(&syncpt->intr.lock);
  196. queue_was_empty = list_empty(&syncpt->intr.wait_head);
  197. if (add_waiter_to_queue(waiter, &syncpt->intr.wait_head)) {
  198. /* added at head of list - new threshold value */
  199. host1x_hw_intr_set_syncpt_threshold(host, id, thresh);
  200. /* added as first waiter - enable interrupt */
  201. if (queue_was_empty)
  202. host1x_hw_intr_enable_syncpt_intr(host, id);
  203. }
  204. spin_unlock(&syncpt->intr.lock);
  205. if (ref)
  206. *ref = waiter;
  207. return 0;
  208. }
  209. void host1x_intr_put_ref(struct host1x *host, u32 id, void *ref)
  210. {
  211. struct host1x_waitlist *waiter = ref;
  212. struct host1x_syncpt *syncpt;
  213. while (atomic_cmpxchg(&waiter->state, WLS_PENDING, WLS_CANCELLED) ==
  214. WLS_REMOVED)
  215. schedule();
  216. syncpt = host->syncpt + id;
  217. (void)process_wait_list(host, syncpt,
  218. host1x_syncpt_load(host->syncpt + id));
  219. kref_put(&waiter->refcount, waiter_release);
  220. }
  221. int host1x_intr_init(struct host1x *host, unsigned int irq_sync)
  222. {
  223. unsigned int id;
  224. u32 nb_pts = host1x_syncpt_nb_pts(host);
  225. mutex_init(&host->intr_mutex);
  226. host->intr_syncpt_irq = irq_sync;
  227. host->intr_wq = create_workqueue("host_syncpt");
  228. if (!host->intr_wq)
  229. return -ENOMEM;
  230. for (id = 0; id < nb_pts; ++id) {
  231. struct host1x_syncpt *syncpt = host->syncpt + id;
  232. spin_lock_init(&syncpt->intr.lock);
  233. INIT_LIST_HEAD(&syncpt->intr.wait_head);
  234. snprintf(syncpt->intr.thresh_irq_name,
  235. sizeof(syncpt->intr.thresh_irq_name),
  236. "host1x_sp_%02d", id);
  237. }
  238. host1x_intr_start(host);
  239. return 0;
  240. }
  241. void host1x_intr_deinit(struct host1x *host)
  242. {
  243. host1x_intr_stop(host);
  244. destroy_workqueue(host->intr_wq);
  245. }
  246. void host1x_intr_start(struct host1x *host)
  247. {
  248. u32 hz = clk_get_rate(host->clk);
  249. int err;
  250. mutex_lock(&host->intr_mutex);
  251. err = host1x_hw_intr_init_host_sync(host, DIV_ROUND_UP(hz, 1000000),
  252. syncpt_thresh_work);
  253. if (err) {
  254. mutex_unlock(&host->intr_mutex);
  255. return;
  256. }
  257. mutex_unlock(&host->intr_mutex);
  258. }
  259. void host1x_intr_stop(struct host1x *host)
  260. {
  261. unsigned int id;
  262. struct host1x_syncpt *syncpt = host->syncpt;
  263. u32 nb_pts = host1x_syncpt_nb_pts(host);
  264. mutex_lock(&host->intr_mutex);
  265. host1x_hw_intr_disable_all_syncpt_intrs(host);
  266. for (id = 0; id < nb_pts; ++id) {
  267. struct host1x_waitlist *waiter, *next;
  268. list_for_each_entry_safe(waiter, next,
  269. &syncpt[id].intr.wait_head, list) {
  270. if (atomic_cmpxchg(&waiter->state,
  271. WLS_CANCELLED, WLS_HANDLED) == WLS_CANCELLED) {
  272. list_del(&waiter->list);
  273. kref_put(&waiter->refcount, waiter_release);
  274. }
  275. }
  276. if (!list_empty(&syncpt[id].intr.wait_head)) {
  277. /* output diagnostics */
  278. mutex_unlock(&host->intr_mutex);
  279. pr_warn("%s cannot stop syncpt intr id=%d\n",
  280. __func__, id);
  281. return;
  282. }
  283. }
  284. host1x_hw_intr_free_syncpt_irq(host);
  285. mutex_unlock(&host->intr_mutex);
  286. }