irq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. struct riscv_ext_intc_list {
  11. acpi_handle handle;
  12. u32 gsi_base;
  13. u32 nr_irqs;
  14. u32 nr_idcs;
  15. u32 id;
  16. u32 type;
  17. struct list_head list;
  18. };
  19. struct acpi_irq_dep_ctx {
  20. int rc;
  21. unsigned int index;
  22. acpi_handle handle;
  23. };
  24. LIST_HEAD(ext_intc_list);
  25. static int irqchip_cmp_func(const void *in0, const void *in1)
  26. {
  27. struct acpi_probe_entry *elem0 = (struct acpi_probe_entry *)in0;
  28. struct acpi_probe_entry *elem1 = (struct acpi_probe_entry *)in1;
  29. return (elem0->type > elem1->type) - (elem0->type < elem1->type);
  30. }
  31. /*
  32. * On RISC-V, RINTC structures in MADT should be probed before any other
  33. * interrupt controller structures and IMSIC before APLIC. The interrupt
  34. * controller subtypes in MADT of ACPI spec for RISC-V are defined in
  35. * the incremental order like RINTC(24)->IMSIC(25)->APLIC(26)->PLIC(27).
  36. * Hence, simply sorting the subtypes in incremental order will
  37. * establish the required order.
  38. */
  39. void arch_sort_irqchip_probe(struct acpi_probe_entry *ap_head, int nr)
  40. {
  41. struct acpi_probe_entry *ape = ap_head;
  42. if (nr == 1 || !ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id))
  43. return;
  44. sort(ape, nr, sizeof(*ape), irqchip_cmp_func, NULL);
  45. }
  46. static acpi_status riscv_acpi_update_gsi_handle(u32 gsi_base, acpi_handle handle)
  47. {
  48. struct riscv_ext_intc_list *ext_intc_element;
  49. struct list_head *i, *tmp;
  50. list_for_each_safe(i, tmp, &ext_intc_list) {
  51. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  52. if (gsi_base == ext_intc_element->gsi_base) {
  53. ext_intc_element->handle = handle;
  54. return AE_OK;
  55. }
  56. }
  57. return AE_NOT_FOUND;
  58. }
  59. int riscv_acpi_get_gsi_info(struct fwnode_handle *fwnode, u32 *gsi_base,
  60. u32 *id, u32 *nr_irqs, u32 *nr_idcs)
  61. {
  62. struct riscv_ext_intc_list *ext_intc_element;
  63. struct list_head *i;
  64. list_for_each(i, &ext_intc_list) {
  65. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  66. if (ext_intc_element->handle == ACPI_HANDLE_FWNODE(fwnode)) {
  67. *gsi_base = ext_intc_element->gsi_base;
  68. *id = ext_intc_element->id;
  69. *nr_irqs = ext_intc_element->nr_irqs;
  70. if (nr_idcs)
  71. *nr_idcs = ext_intc_element->nr_idcs;
  72. return 0;
  73. }
  74. }
  75. return -ENODEV;
  76. }
  77. struct fwnode_handle *riscv_acpi_get_gsi_domain_id(u32 gsi)
  78. {
  79. struct riscv_ext_intc_list *ext_intc_element;
  80. struct acpi_device *adev;
  81. struct list_head *i;
  82. list_for_each(i, &ext_intc_list) {
  83. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  84. if (gsi >= ext_intc_element->gsi_base &&
  85. gsi < (ext_intc_element->gsi_base + ext_intc_element->nr_irqs)) {
  86. adev = acpi_fetch_acpi_dev(ext_intc_element->handle);
  87. if (!adev)
  88. return NULL;
  89. return acpi_fwnode_handle(adev);
  90. }
  91. }
  92. return NULL;
  93. }
  94. static int __init riscv_acpi_register_ext_intc(u32 gsi_base, u32 nr_irqs, u32 nr_idcs,
  95. u32 id, u32 type)
  96. {
  97. struct riscv_ext_intc_list *ext_intc_element;
  98. ext_intc_element = kzalloc(sizeof(*ext_intc_element), GFP_KERNEL);
  99. if (!ext_intc_element)
  100. return -ENOMEM;
  101. ext_intc_element->gsi_base = gsi_base;
  102. ext_intc_element->nr_irqs = nr_irqs;
  103. ext_intc_element->nr_idcs = nr_idcs;
  104. ext_intc_element->id = id;
  105. list_add_tail(&ext_intc_element->list, &ext_intc_list);
  106. return 0;
  107. }
  108. static acpi_status __init riscv_acpi_create_gsi_map(acpi_handle handle, u32 level,
  109. void *context, void **return_value)
  110. {
  111. acpi_status status;
  112. u64 gbase;
  113. if (!acpi_has_method(handle, "_GSB")) {
  114. acpi_handle_err(handle, "_GSB method not found\n");
  115. return AE_ERROR;
  116. }
  117. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gbase);
  118. if (ACPI_FAILURE(status)) {
  119. acpi_handle_err(handle, "failed to evaluate _GSB method\n");
  120. return status;
  121. }
  122. status = riscv_acpi_update_gsi_handle((u32)gbase, handle);
  123. if (ACPI_FAILURE(status)) {
  124. acpi_handle_err(handle, "failed to find the GSI mapping entry\n");
  125. return status;
  126. }
  127. return AE_OK;
  128. }
  129. static int __init riscv_acpi_aplic_parse_madt(union acpi_subtable_headers *header,
  130. const unsigned long end)
  131. {
  132. struct acpi_madt_aplic *aplic = (struct acpi_madt_aplic *)header;
  133. return riscv_acpi_register_ext_intc(aplic->gsi_base, aplic->num_sources, aplic->num_idcs,
  134. aplic->id, ACPI_RISCV_IRQCHIP_APLIC);
  135. }
  136. static int __init riscv_acpi_plic_parse_madt(union acpi_subtable_headers *header,
  137. const unsigned long end)
  138. {
  139. struct acpi_madt_plic *plic = (struct acpi_madt_plic *)header;
  140. return riscv_acpi_register_ext_intc(plic->gsi_base, plic->num_irqs, 0,
  141. plic->id, ACPI_RISCV_IRQCHIP_PLIC);
  142. }
  143. void __init riscv_acpi_init_gsi_mapping(void)
  144. {
  145. /* There can be either PLIC or APLIC */
  146. if (acpi_table_parse_madt(ACPI_MADT_TYPE_PLIC, riscv_acpi_plic_parse_madt, 0) > 0) {
  147. acpi_get_devices("RSCV0001", riscv_acpi_create_gsi_map, NULL, NULL);
  148. return;
  149. }
  150. if (acpi_table_parse_madt(ACPI_MADT_TYPE_APLIC, riscv_acpi_aplic_parse_madt, 0) > 0)
  151. acpi_get_devices("RSCV0002", riscv_acpi_create_gsi_map, NULL, NULL);
  152. }
  153. static acpi_handle riscv_acpi_get_gsi_handle(u32 gsi)
  154. {
  155. struct riscv_ext_intc_list *ext_intc_element;
  156. struct list_head *i;
  157. list_for_each(i, &ext_intc_list) {
  158. ext_intc_element = list_entry(i, struct riscv_ext_intc_list, list);
  159. if (gsi >= ext_intc_element->gsi_base &&
  160. gsi < (ext_intc_element->gsi_base + ext_intc_element->nr_irqs))
  161. return ext_intc_element->handle;
  162. }
  163. return NULL;
  164. }
  165. static acpi_status riscv_acpi_irq_get_parent(struct acpi_resource *ares, void *context)
  166. {
  167. struct acpi_irq_dep_ctx *ctx = context;
  168. struct acpi_resource_irq *irq;
  169. struct acpi_resource_extended_irq *eirq;
  170. switch (ares->type) {
  171. case ACPI_RESOURCE_TYPE_IRQ:
  172. irq = &ares->data.irq;
  173. if (ctx->index >= irq->interrupt_count) {
  174. ctx->index -= irq->interrupt_count;
  175. return AE_OK;
  176. }
  177. ctx->handle = riscv_acpi_get_gsi_handle(irq->interrupts[ctx->index]);
  178. return AE_CTRL_TERMINATE;
  179. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  180. eirq = &ares->data.extended_irq;
  181. if (eirq->producer_consumer == ACPI_PRODUCER)
  182. return AE_OK;
  183. if (ctx->index >= eirq->interrupt_count) {
  184. ctx->index -= eirq->interrupt_count;
  185. return AE_OK;
  186. }
  187. /* Support GSIs only */
  188. if (eirq->resource_source.string_length)
  189. return AE_OK;
  190. ctx->handle = riscv_acpi_get_gsi_handle(eirq->interrupts[ctx->index]);
  191. return AE_CTRL_TERMINATE;
  192. }
  193. return AE_OK;
  194. }
  195. static int riscv_acpi_irq_get_dep(acpi_handle handle, unsigned int index, acpi_handle *gsi_handle)
  196. {
  197. struct acpi_irq_dep_ctx ctx = {-EINVAL, index, NULL};
  198. if (!gsi_handle)
  199. return 0;
  200. acpi_walk_resources(handle, METHOD_NAME__CRS, riscv_acpi_irq_get_parent, &ctx);
  201. *gsi_handle = ctx.handle;
  202. if (*gsi_handle)
  203. return 1;
  204. return 0;
  205. }
  206. static u32 riscv_acpi_add_prt_dep(acpi_handle handle)
  207. {
  208. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  209. struct acpi_pci_routing_table *entry;
  210. struct acpi_handle_list dep_devices;
  211. acpi_handle gsi_handle;
  212. acpi_handle link_handle;
  213. acpi_status status;
  214. u32 count = 0;
  215. status = acpi_get_irq_routing_table(handle, &buffer);
  216. if (ACPI_FAILURE(status)) {
  217. acpi_handle_err(handle, "failed to get IRQ routing table\n");
  218. kfree(buffer.pointer);
  219. return 0;
  220. }
  221. entry = buffer.pointer;
  222. while (entry && (entry->length > 0)) {
  223. if (entry->source[0]) {
  224. acpi_get_handle(handle, entry->source, &link_handle);
  225. dep_devices.count = 1;
  226. dep_devices.handles = kcalloc(1, sizeof(*dep_devices.handles), GFP_KERNEL);
  227. if (!dep_devices.handles) {
  228. acpi_handle_err(handle, "failed to allocate memory\n");
  229. continue;
  230. }
  231. dep_devices.handles[0] = link_handle;
  232. count += acpi_scan_add_dep(handle, &dep_devices);
  233. } else {
  234. gsi_handle = riscv_acpi_get_gsi_handle(entry->source_index);
  235. dep_devices.count = 1;
  236. dep_devices.handles = kcalloc(1, sizeof(*dep_devices.handles), GFP_KERNEL);
  237. if (!dep_devices.handles) {
  238. acpi_handle_err(handle, "failed to allocate memory\n");
  239. continue;
  240. }
  241. dep_devices.handles[0] = gsi_handle;
  242. count += acpi_scan_add_dep(handle, &dep_devices);
  243. }
  244. entry = (struct acpi_pci_routing_table *)
  245. ((unsigned long)entry + entry->length);
  246. }
  247. kfree(buffer.pointer);
  248. return count;
  249. }
  250. static u32 riscv_acpi_add_irq_dep(acpi_handle handle)
  251. {
  252. struct acpi_handle_list dep_devices;
  253. acpi_handle gsi_handle;
  254. u32 count = 0;
  255. int i;
  256. for (i = 0;
  257. riscv_acpi_irq_get_dep(handle, i, &gsi_handle);
  258. i++) {
  259. dep_devices.count = 1;
  260. dep_devices.handles = kcalloc(1, sizeof(*dep_devices.handles), GFP_KERNEL);
  261. if (!dep_devices.handles) {
  262. acpi_handle_err(handle, "failed to allocate memory\n");
  263. continue;
  264. }
  265. dep_devices.handles[0] = gsi_handle;
  266. count += acpi_scan_add_dep(handle, &dep_devices);
  267. }
  268. return count;
  269. }
  270. u32 arch_acpi_add_auto_dep(acpi_handle handle)
  271. {
  272. if (acpi_has_method(handle, "_PRT"))
  273. return riscv_acpi_add_prt_dep(handle);
  274. return riscv_acpi_add_irq_dep(handle);
  275. }