irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2023-2024, Ventana Micro Systems Inc
  4. * Author: Sunil V L <sunilvl@ventanamicro.com>
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/sort.h>
  8. #include <linux/irq.h>
  9. #include "init.h"
  10. #define RISCV_ACPI_INTC_FLAG_PENDING BIT(0)
  11. struct riscv_ext_intc_list {
  12. acpi_handle handle;
  13. u32 gsi_base;
  14. u32 nr_irqs;
  15. u32 nr_idcs;
  16. u32 id;
  17. u32 type;
  18. u32 flag;
  19. struct list_head list;
  20. };
  21. struct acpi_irq_dep_ctx {
  22. int rc;
  23. unsigned int index;
  24. acpi_handle handle;
  25. };
  26. LIST_HEAD(ext_intc_list);
  27. static int irqchip_cmp_func(const void *in0, const void *in1)
  28. {
  29. struct acpi_probe_entry *elem0 = (struct acpi_probe_entry *)in0;
  30. struct acpi_probe_entry *elem1 = (struct acpi_probe_entry *)in1;
  31. return (elem0->type > elem1->type) - (elem0->type < elem1->type);
  32. }
  33. /*
  34. * On RISC-V, RINTC structures in MADT should be probed before any other
  35. * interrupt controller structures and IMSIC before APLIC. The interrupt
  36. * controller subtypes in MADT of ACPI spec for RISC-V are defined in
  37. * the incremental order like RINTC(24)->IMSIC(25)->APLIC(26)->PLIC(27).
  38. * Hence, simply sorting the subtypes in incremental order will
  39. * establish the required order.
  40. */
  41. void arch_sort_irqchip_probe(struct acpi_probe_entry *ap_head, int nr)
  42. {
  43. struct acpi_probe_entry *ape = ap_head;
  44. if (nr == 1 || !ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id))
  45. return;
  46. sort(ape, nr, sizeof(*ape), irqchip_cmp_func, NULL);
  47. }
  48. static acpi_status riscv_acpi_update_gsi_handle(u32 gsi_base, acpi_handle handle)
  49. {
  50. struct riscv_ext_intc_list *ext_intc_element;
  51. struct list_head *i, *tmp;
  52. list_for_each_safe(i, tmp, &ext_intc_list) {
  53. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  54. if (gsi_base == ext_intc_element->gsi_base) {
  55. ext_intc_element->handle = handle;
  56. return AE_OK;
  57. }
  58. }
  59. return AE_NOT_FOUND;
  60. }
  61. int riscv_acpi_update_gsi_range(u32 gsi_base, u32 nr_irqs)
  62. {
  63. struct riscv_ext_intc_list *ext_intc_element;
  64. list_for_each_entry(ext_intc_element, &ext_intc_list, list) {
  65. if (gsi_base == ext_intc_element->gsi_base &&
  66. (ext_intc_element->flag & RISCV_ACPI_INTC_FLAG_PENDING)) {
  67. ext_intc_element->nr_irqs = nr_irqs;
  68. ext_intc_element->flag &= ~RISCV_ACPI_INTC_FLAG_PENDING;
  69. return 0;
  70. }
  71. }
  72. return -ENODEV;
  73. }
  74. int riscv_acpi_get_gsi_info(struct fwnode_handle *fwnode, u32 *gsi_base,
  75. u32 *id, u32 *nr_irqs, u32 *nr_idcs)
  76. {
  77. struct riscv_ext_intc_list *ext_intc_element;
  78. struct list_head *i;
  79. list_for_each(i, &ext_intc_list) {
  80. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  81. if (ext_intc_element->handle == ACPI_HANDLE_FWNODE(fwnode)) {
  82. *gsi_base = ext_intc_element->gsi_base;
  83. *id = ext_intc_element->id;
  84. *nr_irqs = ext_intc_element->nr_irqs;
  85. if (nr_idcs)
  86. *nr_idcs = ext_intc_element->nr_idcs;
  87. return 0;
  88. }
  89. }
  90. return -ENODEV;
  91. }
  92. struct fwnode_handle *riscv_acpi_get_gsi_domain_id(u32 gsi)
  93. {
  94. struct riscv_ext_intc_list *ext_intc_element;
  95. struct acpi_device *adev;
  96. struct list_head *i;
  97. list_for_each(i, &ext_intc_list) {
  98. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  99. if (gsi >= ext_intc_element->gsi_base &&
  100. gsi < (ext_intc_element->gsi_base + ext_intc_element->nr_irqs)) {
  101. adev = acpi_fetch_acpi_dev(ext_intc_element->handle);
  102. if (!adev)
  103. return NULL;
  104. return acpi_fwnode_handle(adev);
  105. }
  106. }
  107. return NULL;
  108. }
  109. static int __init riscv_acpi_register_ext_intc(u32 gsi_base, u32 nr_irqs, u32 nr_idcs,
  110. u32 id, u32 type)
  111. {
  112. struct riscv_ext_intc_list *ext_intc_element, *node, *prev;
  113. ext_intc_element = kzalloc_obj(*ext_intc_element);
  114. if (!ext_intc_element)
  115. return -ENOMEM;
  116. ext_intc_element->gsi_base = gsi_base;
  117. /* If nr_irqs is zero, indicate it in flag and set to max range possible */
  118. if (nr_irqs) {
  119. ext_intc_element->nr_irqs = nr_irqs;
  120. } else {
  121. ext_intc_element->flag |= RISCV_ACPI_INTC_FLAG_PENDING;
  122. ext_intc_element->nr_irqs = U32_MAX - ext_intc_element->gsi_base;
  123. }
  124. ext_intc_element->nr_idcs = nr_idcs;
  125. ext_intc_element->id = id;
  126. list_for_each_entry(node, &ext_intc_list, list) {
  127. if (node->gsi_base < ext_intc_element->gsi_base)
  128. break;
  129. }
  130. /* Adjust the previous node's GSI range if that has pending registration */
  131. prev = list_prev_entry(node, list);
  132. if (!list_entry_is_head(prev, &ext_intc_list, list)) {
  133. if (prev->flag & RISCV_ACPI_INTC_FLAG_PENDING)
  134. prev->nr_irqs = ext_intc_element->gsi_base - prev->gsi_base;
  135. }
  136. list_add_tail(&ext_intc_element->list, &node->list);
  137. return 0;
  138. }
  139. static acpi_status __init riscv_acpi_create_gsi_map_smsi(acpi_handle handle, u32 level,
  140. void *context, void **return_value)
  141. {
  142. acpi_status status;
  143. u64 gbase;
  144. if (!acpi_has_method(handle, "_GSB")) {
  145. acpi_handle_err(handle, "_GSB method not found\n");
  146. return AE_ERROR;
  147. }
  148. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gbase);
  149. if (ACPI_FAILURE(status)) {
  150. acpi_handle_err(handle, "failed to evaluate _GSB method\n");
  151. return status;
  152. }
  153. riscv_acpi_register_ext_intc(gbase, 0, 0, 0, ACPI_RISCV_IRQCHIP_SMSI);
  154. status = riscv_acpi_update_gsi_handle((u32)gbase, handle);
  155. if (ACPI_FAILURE(status)) {
  156. acpi_handle_err(handle, "failed to find the GSI mapping entry\n");
  157. return status;
  158. }
  159. return AE_OK;
  160. }
  161. static acpi_status __init riscv_acpi_create_gsi_map(acpi_handle handle, u32 level,
  162. void *context, void **return_value)
  163. {
  164. acpi_status status;
  165. u64 gbase;
  166. if (!acpi_has_method(handle, "_GSB")) {
  167. acpi_handle_err(handle, "_GSB method not found\n");
  168. return AE_ERROR;
  169. }
  170. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gbase);
  171. if (ACPI_FAILURE(status)) {
  172. acpi_handle_err(handle, "failed to evaluate _GSB method\n");
  173. return status;
  174. }
  175. status = riscv_acpi_update_gsi_handle((u32)gbase, handle);
  176. if (ACPI_FAILURE(status)) {
  177. acpi_handle_err(handle, "failed to find the GSI mapping entry\n");
  178. return status;
  179. }
  180. return AE_OK;
  181. }
  182. static int __init riscv_acpi_aplic_parse_madt(union acpi_subtable_headers *header,
  183. const unsigned long end)
  184. {
  185. struct acpi_madt_aplic *aplic = (struct acpi_madt_aplic *)header;
  186. return riscv_acpi_register_ext_intc(aplic->gsi_base, aplic->num_sources, aplic->num_idcs,
  187. aplic->id, ACPI_RISCV_IRQCHIP_APLIC);
  188. }
  189. static int __init riscv_acpi_plic_parse_madt(union acpi_subtable_headers *header,
  190. const unsigned long end)
  191. {
  192. struct acpi_madt_plic *plic = (struct acpi_madt_plic *)header;
  193. return riscv_acpi_register_ext_intc(plic->gsi_base, plic->num_irqs, 0,
  194. plic->id, ACPI_RISCV_IRQCHIP_PLIC);
  195. }
  196. void __init riscv_acpi_init_gsi_mapping(void)
  197. {
  198. /* There can be either PLIC or APLIC */
  199. if (acpi_table_parse_madt(ACPI_MADT_TYPE_PLIC, riscv_acpi_plic_parse_madt, 0) > 0) {
  200. acpi_get_devices("RSCV0001", riscv_acpi_create_gsi_map, NULL, NULL);
  201. return;
  202. }
  203. if (acpi_table_parse_madt(ACPI_MADT_TYPE_APLIC, riscv_acpi_aplic_parse_madt, 0) > 0)
  204. acpi_get_devices("RSCV0002", riscv_acpi_create_gsi_map, NULL, NULL);
  205. /* Unlike PLIC/APLIC, SYSMSI doesn't have MADT */
  206. acpi_get_devices("RSCV0006", riscv_acpi_create_gsi_map_smsi, NULL, NULL);
  207. }
  208. static acpi_handle riscv_acpi_get_gsi_handle(u32 gsi)
  209. {
  210. struct riscv_ext_intc_list *ext_intc_element;
  211. struct list_head *i;
  212. list_for_each(i, &ext_intc_list) {
  213. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  214. if (gsi >= ext_intc_element->gsi_base &&
  215. gsi < (ext_intc_element->gsi_base + ext_intc_element->nr_irqs))
  216. return ext_intc_element->handle;
  217. }
  218. return NULL;
  219. }
  220. static acpi_status riscv_acpi_irq_get_parent(struct acpi_resource *ares, void *context)
  221. {
  222. struct acpi_irq_dep_ctx *ctx = context;
  223. struct acpi_resource_irq *irq;
  224. struct acpi_resource_extended_irq *eirq;
  225. switch (ares->type) {
  226. case ACPI_RESOURCE_TYPE_IRQ:
  227. irq = &ares->data.irq;
  228. if (ctx->index >= irq->interrupt_count) {
  229. ctx->index -= irq->interrupt_count;
  230. return AE_OK;
  231. }
  232. ctx->handle = riscv_acpi_get_gsi_handle(irq->interrupts[ctx->index]);
  233. return AE_CTRL_TERMINATE;
  234. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  235. eirq = &ares->data.extended_irq;
  236. if (eirq->producer_consumer == ACPI_PRODUCER)
  237. return AE_OK;
  238. if (ctx->index >= eirq->interrupt_count) {
  239. ctx->index -= eirq->interrupt_count;
  240. return AE_OK;
  241. }
  242. /* Support GSIs only */
  243. if (eirq->resource_source.string_length)
  244. return AE_OK;
  245. ctx->handle = riscv_acpi_get_gsi_handle(eirq->interrupts[ctx->index]);
  246. return AE_CTRL_TERMINATE;
  247. }
  248. return AE_OK;
  249. }
  250. static int riscv_acpi_irq_get_dep(acpi_handle handle, unsigned int index, acpi_handle *gsi_handle)
  251. {
  252. struct acpi_irq_dep_ctx ctx = {-EINVAL, index, NULL};
  253. if (!gsi_handle)
  254. return 0;
  255. acpi_walk_resources(handle, METHOD_NAME__CRS, riscv_acpi_irq_get_parent, &ctx);
  256. *gsi_handle = ctx.handle;
  257. if (*gsi_handle)
  258. return 1;
  259. return 0;
  260. }
  261. static u32 riscv_acpi_add_prt_dep(acpi_handle handle)
  262. {
  263. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  264. struct acpi_pci_routing_table *entry;
  265. struct acpi_handle_list dep_devices;
  266. acpi_handle gsi_handle;
  267. acpi_handle link_handle;
  268. acpi_status status;
  269. u32 count = 0;
  270. status = acpi_get_irq_routing_table(handle, &buffer);
  271. if (ACPI_FAILURE(status)) {
  272. acpi_handle_err(handle, "failed to get IRQ routing table\n");
  273. kfree(buffer.pointer);
  274. return 0;
  275. }
  276. entry = buffer.pointer;
  277. while (entry && (entry->length > 0)) {
  278. if (entry->source[0]) {
  279. acpi_get_handle(handle, entry->source, &link_handle);
  280. dep_devices.count = 1;
  281. dep_devices.handles = kzalloc_objs(*dep_devices.handles,
  282. 1);
  283. if (!dep_devices.handles) {
  284. acpi_handle_err(handle, "failed to allocate memory\n");
  285. continue;
  286. }
  287. dep_devices.handles[0] = link_handle;
  288. count += acpi_scan_add_dep(handle, &dep_devices);
  289. } else {
  290. gsi_handle = riscv_acpi_get_gsi_handle(entry->source_index);
  291. dep_devices.count = 1;
  292. dep_devices.handles = kzalloc_objs(*dep_devices.handles,
  293. 1);
  294. if (!dep_devices.handles) {
  295. acpi_handle_err(handle, "failed to allocate memory\n");
  296. continue;
  297. }
  298. dep_devices.handles[0] = gsi_handle;
  299. count += acpi_scan_add_dep(handle, &dep_devices);
  300. }
  301. entry = (struct acpi_pci_routing_table *)
  302. ((unsigned long)entry + entry->length);
  303. }
  304. kfree(buffer.pointer);
  305. return count;
  306. }
  307. static u32 riscv_acpi_add_irq_dep(acpi_handle handle)
  308. {
  309. struct acpi_handle_list dep_devices;
  310. acpi_handle gsi_handle;
  311. u32 count = 0;
  312. int i;
  313. for (i = 0;
  314. riscv_acpi_irq_get_dep(handle, i, &gsi_handle);
  315. i++) {
  316. dep_devices.count = 1;
  317. dep_devices.handles = kzalloc_objs(*dep_devices.handles, 1);
  318. if (!dep_devices.handles) {
  319. acpi_handle_err(handle, "failed to allocate memory\n");
  320. continue;
  321. }
  322. dep_devices.handles[0] = gsi_handle;
  323. count += acpi_scan_add_dep(handle, &dep_devices);
  324. }
  325. return count;
  326. }
  327. u32 arch_acpi_add_auto_dep(acpi_handle handle)
  328. {
  329. if (acpi_has_method(handle, "_PRT"))
  330. return riscv_acpi_add_prt_dep(handle);
  331. return riscv_acpi_add_irq_dep(handle);
  332. }