intr.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * XXX Until more hardware is supported, the model used in this implementation
  19. * remains a very simple one, where interrupt controllers are global to the
  20. * system, and each interrupt is targeted at a single processor.
  21. *
  22. * Shared interrupts are supported.
  23. */
  24. #include <errno.h>
  25. #include <stdalign.h>
  26. #include <stdbool.h>
  27. #include <stddef.h>
  28. #include <kern/atomic.h>
  29. #include <kern/kmem.h>
  30. #include <kern/init.h>
  31. #include <kern/intr.h>
  32. #include <kern/list.h>
  33. #include <kern/log.h>
  34. #include <kern/macros.h>
  35. #include <kern/panic.h>
  36. #include <kern/spinlock.h>
  37. #include <kern/thread.h>
  38. #include <machine/boot.h>
  39. #include <machine/cpu.h>
  40. struct intr_handler
  41. {
  42. __cacheline_aligned struct list node;
  43. intr_handler_fn_t fn;
  44. void *arg;
  45. };
  46. /*
  47. * Interrupt controller.
  48. *
  49. * All members are currently read-only once all controllers have been
  50. * registered.
  51. */
  52. struct intr_ctl
  53. {
  54. struct list node;
  55. const struct intr_ops *ops;
  56. void *priv;
  57. uint32_t first_intr;
  58. uint32_t last_intr;
  59. };
  60. /*
  61. * Interrupt table entry, one per vector.
  62. *
  63. * Interrupts must be disabled when accessing an entry.
  64. *
  65. * Each interrupt can be routed to one processor at most. Make each entry
  66. * span a cache line to avoid false sharing.
  67. */
  68. struct intr_entry
  69. {
  70. __cacheline_aligned struct spinlock lock;
  71. struct intr_ctl *ctl;
  72. uint32_t cpu;
  73. struct list handlers;
  74. };
  75. // Interrupt table.
  76. static struct intr_entry intr_table[CPU_INTR_TABLE_SIZE];
  77. // List of registered controllers.
  78. static struct list intr_ctls;
  79. static struct kmem_cache intr_handler_cache;
  80. static uint32_t
  81. intr_select_cpu (void)
  82. {
  83. /*
  84. * TODO Interrupt routing.
  85. * Although the interface supports it, there are currently problems
  86. * with the I/O APIC that need to be solved first.
  87. */
  88. return (0);
  89. }
  90. static int
  91. intr_handler_create (struct intr_handler **handlerp,
  92. intr_handler_fn_t fn, void *arg)
  93. {
  94. struct intr_handler *handler = kmem_cache_alloc (&intr_handler_cache);
  95. if (! handler)
  96. return (ENOMEM);
  97. handler->fn = fn;
  98. handler->arg = arg;
  99. *handlerp = handler;
  100. return (0);
  101. }
  102. static void
  103. intr_handler_destroy (struct intr_handler *handler)
  104. {
  105. kmem_cache_free (&intr_handler_cache, handler);
  106. }
  107. static bool
  108. intr_handler_match (const struct intr_handler *handler, intr_handler_fn_t fn)
  109. {
  110. return (handler->fn == fn);
  111. }
  112. static int
  113. intr_handler_run (struct intr_handler *handler)
  114. {
  115. return (handler->fn (handler->arg));
  116. }
  117. static struct intr_ctl* __init
  118. intr_ctl_create (const struct intr_ops *ops, void *priv,
  119. uint32_t first_intr, uint32_t last_intr)
  120. {
  121. assert (ops);
  122. assert (first_intr < last_intr);
  123. struct intr_ctl *ctl = kmem_alloc (sizeof (*ctl));
  124. if (! ctl)
  125. panic ("intr: unable to allocate memory for controller");
  126. ctl->ops = ops;
  127. ctl->priv = priv;
  128. ctl->first_intr = first_intr;
  129. ctl->last_intr = last_intr;
  130. return (ctl);
  131. }
  132. static bool
  133. intr_ctl_has_intr (const struct intr_ctl *ctl, uint32_t intr)
  134. {
  135. return (intr >= ctl->first_intr && intr <= ctl->last_intr);
  136. }
  137. static void
  138. intr_ctl_enable (struct intr_ctl *ctl, uint32_t intr, uint32_t cpu)
  139. {
  140. ctl->ops->enable (ctl->priv, intr, cpu);
  141. }
  142. static void
  143. intr_ctl_disable (struct intr_ctl *ctl, uint32_t intr)
  144. {
  145. ctl->ops->disable (ctl->priv, intr);
  146. }
  147. static void
  148. intr_ctl_eoi (struct intr_ctl *ctl, uint32_t intr)
  149. {
  150. ctl->ops->eoi (ctl->priv, intr);
  151. }
  152. static struct intr_ctl*
  153. intr_lookup_ctl (uint32_t intr)
  154. {
  155. struct intr_ctl *ctl;
  156. list_for_each_entry (&intr_ctls, ctl, node)
  157. if (intr_ctl_has_intr (ctl, intr))
  158. return (ctl);
  159. return (NULL);
  160. }
  161. static void __init
  162. intr_entry_init (struct intr_entry *entry)
  163. {
  164. spinlock_init (&entry->lock);
  165. list_init (&entry->handlers);
  166. }
  167. static bool
  168. intr_entry_empty (const struct intr_entry *entry)
  169. {
  170. return (list_empty (&entry->handlers));
  171. }
  172. static uint32_t
  173. intr_entry_get_intr (const struct intr_entry *entry)
  174. {
  175. size_t id = entry - intr_table;
  176. assert (id < ARRAY_SIZE (intr_table));
  177. return (id);
  178. }
  179. static void
  180. intr_entry_enable (struct intr_entry *entry, struct intr_ctl *ctl, uint32_t cpu)
  181. {
  182. entry->ctl = ctl;
  183. entry->cpu = cpu;
  184. intr_ctl_enable (entry->ctl, intr_entry_get_intr (entry), cpu);
  185. }
  186. static void
  187. intr_entry_disable (struct intr_entry *entry)
  188. {
  189. intr_ctl_disable (entry->ctl, intr_entry_get_intr (entry));
  190. }
  191. static struct intr_handler*
  192. intr_entry_lookup_handler (const struct intr_entry *entry, intr_handler_fn_t fn)
  193. {
  194. struct intr_handler *handler;
  195. list_for_each_entry (&entry->handlers, handler, node)
  196. if (intr_handler_match (handler, fn))
  197. return (handler);
  198. return (NULL);
  199. }
  200. static int
  201. intr_entry_add (struct intr_entry *entry, struct intr_handler *handler)
  202. {
  203. SPINLOCK_INTR_GUARD (&entry->lock);
  204. if (intr_entry_empty (entry))
  205. {
  206. _Auto ctl = intr_lookup_ctl (intr_entry_get_intr (entry));
  207. if (! ctl)
  208. return (ENODEV);
  209. intr_entry_enable (entry, ctl, intr_select_cpu ());
  210. }
  211. list_insert_tail (&entry->handlers, &handler->node);
  212. return (0);
  213. }
  214. static struct intr_handler*
  215. intr_entry_remove (struct intr_entry *entry, intr_handler_fn_t fn)
  216. {
  217. SPINLOCK_INTR_GUARD (&entry->lock);
  218. _Auto handler = intr_entry_lookup_handler (entry, fn);
  219. if (! handler)
  220. return (NULL);
  221. list_remove (&handler->node);
  222. if (intr_entry_empty (entry))
  223. intr_entry_disable (entry);
  224. return (handler);
  225. }
  226. static void
  227. intr_entry_eoi (struct intr_entry *entry, uint32_t intr)
  228. {
  229. assert (entry->ctl != NULL);
  230. intr_ctl_eoi (entry->ctl, intr);
  231. }
  232. static struct intr_entry*
  233. intr_get_entry (uint32_t intr)
  234. {
  235. assert (intr < ARRAY_SIZE (intr_table));
  236. return (&intr_table[intr]);
  237. }
  238. static int __init
  239. intr_bootstrap (void)
  240. {
  241. list_init (&intr_ctls);
  242. kmem_cache_init (&intr_handler_cache, "intr_handler",
  243. sizeof (struct intr_handler), alignof (struct intr_handler),
  244. NULL, 0);
  245. for (size_t i = 0; i < ARRAY_SIZE (intr_table); i++)
  246. intr_entry_init (intr_get_entry (i));
  247. return (0);
  248. }
  249. INIT_OP_DEFINE (intr_bootstrap,
  250. INIT_OP_DEP (kmem_setup, true),
  251. INIT_OP_DEP (log_setup, true),
  252. INIT_OP_DEP (spinlock_setup, true),
  253. INIT_OP_DEP (thread_bootstrap, true));
  254. static int __init
  255. intr_setup (void)
  256. {
  257. return (0);
  258. }
  259. INIT_OP_DEFINE (intr_setup,
  260. INIT_OP_DEP (boot_setup_intr, true),
  261. INIT_OP_DEP (intr_bootstrap, true));
  262. static void __init
  263. intr_check_range (uint32_t first_intr, uint32_t last_intr)
  264. {
  265. struct intr_ctl *ctl;
  266. list_for_each_entry (&intr_ctls, ctl, node)
  267. for (uint32_t i = first_intr; i <= last_intr; i++)
  268. if (intr_ctl_has_intr (ctl, i))
  269. panic ("intr: controller range conflict");
  270. }
  271. void __init
  272. intr_register_ctl (const struct intr_ops *ops, void *priv,
  273. uint32_t first_intr, uint32_t last_intr)
  274. {
  275. _Auto ctl = intr_ctl_create (ops, priv, first_intr, last_intr);
  276. intr_check_range (first_intr, last_intr);
  277. list_insert_tail (&intr_ctls, &ctl->node);
  278. }
  279. int
  280. intr_register (uint32_t intr, intr_handler_fn_t fn, void *arg)
  281. {
  282. struct intr_handler *handler;
  283. int error = intr_handler_create (&handler, fn, arg);
  284. if (error)
  285. return (error);
  286. error = intr_entry_add (intr_get_entry (intr), handler);
  287. if (error)
  288. intr_handler_destroy (handler);
  289. return (error);
  290. }
  291. void
  292. intr_unregister (uint32_t intr, intr_handler_fn_t fn)
  293. {
  294. _Auto handler = intr_entry_remove (intr_get_entry (intr), fn);
  295. if (! handler)
  296. {
  297. log_warning ("intr: attempting to unregister unknown handler");
  298. return;
  299. }
  300. intr_handler_destroy (handler);
  301. }
  302. void
  303. intr_handle (uint32_t intr)
  304. {
  305. assert (thread_check_intr_context ());
  306. _Auto entry = intr_get_entry (intr);
  307. SPINLOCK_GUARD (&entry->lock);
  308. if (intr_entry_empty (entry))
  309. {
  310. log_warning ("intr: spurious interrupt %u", intr);
  311. return;
  312. }
  313. struct intr_handler *handler;
  314. list_for_each_entry (&entry->handlers, handler, node)
  315. if (intr_handler_run (handler) == 0)
  316. break;
  317. intr_entry_eoi (entry, intr);
  318. }