irq-riscv-imsic-platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Western Digital Corporation or its affiliates.
  4. * Copyright (C) 2022 Ventana Micro Systems Inc.
  5. */
  6. #define pr_fmt(fmt) "riscv-imsic: " fmt
  7. #include <linux/acpi.h>
  8. #include <linux/bitmap.h>
  9. #include <linux/cpu.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/module.h>
  16. #include <linux/msi.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/smp.h>
  21. #include <linux/irqchip/irq-msi-lib.h>
  22. #include "irq-riscv-imsic-state.h"
  23. static bool imsic_cpu_page_phys(unsigned int cpu, unsigned int guest_index,
  24. phys_addr_t *out_msi_pa)
  25. {
  26. struct imsic_global_config *global;
  27. struct imsic_local_config *local;
  28. global = &imsic->global;
  29. local = per_cpu_ptr(global->local, cpu);
  30. if (BIT(global->guest_index_bits) <= guest_index)
  31. return false;
  32. if (out_msi_pa)
  33. *out_msi_pa = local->msi_pa + (guest_index * IMSIC_MMIO_PAGE_SZ);
  34. return true;
  35. }
  36. static void imsic_irq_mask(struct irq_data *d)
  37. {
  38. imsic_vector_mask(irq_data_get_irq_chip_data(d));
  39. }
  40. static void imsic_irq_unmask(struct irq_data *d)
  41. {
  42. imsic_vector_unmask(irq_data_get_irq_chip_data(d));
  43. }
  44. static int imsic_irq_retrigger(struct irq_data *d)
  45. {
  46. struct imsic_vector *vec = irq_data_get_irq_chip_data(d);
  47. struct imsic_local_config *local;
  48. if (WARN_ON(!vec))
  49. return -ENOENT;
  50. local = per_cpu_ptr(imsic->global.local, vec->cpu);
  51. writel_relaxed(vec->local_id, local->msi_va);
  52. return 0;
  53. }
  54. static void imsic_irq_ack(struct irq_data *d)
  55. {
  56. irq_move_irq(d);
  57. }
  58. static void imsic_irq_compose_vector_msg(struct imsic_vector *vec, struct msi_msg *msg)
  59. {
  60. phys_addr_t msi_addr;
  61. if (WARN_ON(!vec))
  62. return;
  63. if (WARN_ON(!imsic_cpu_page_phys(vec->cpu, 0, &msi_addr)))
  64. return;
  65. msg->address_hi = upper_32_bits(msi_addr);
  66. msg->address_lo = lower_32_bits(msi_addr);
  67. msg->data = vec->local_id;
  68. }
  69. static void imsic_irq_compose_msg(struct irq_data *d, struct msi_msg *msg)
  70. {
  71. imsic_irq_compose_vector_msg(irq_data_get_irq_chip_data(d), msg);
  72. }
  73. #ifdef CONFIG_SMP
  74. static void imsic_msi_update_msg(struct irq_data *d, struct imsic_vector *vec)
  75. {
  76. struct msi_msg msg = { };
  77. imsic_irq_compose_vector_msg(vec, &msg);
  78. irq_data_get_irq_chip(d)->irq_write_msi_msg(d, &msg);
  79. }
  80. static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  81. bool force)
  82. {
  83. struct imsic_vector *old_vec, *new_vec;
  84. struct imsic_vector tmp_vec;
  85. /*
  86. * Requirements for the downstream irqdomains (or devices):
  87. *
  88. * 1) Downstream irqdomains (or devices) with atomic MSI update can
  89. * happily do imsic_irq_set_affinity() in the process-context on
  90. * any CPU so the irqchip of such irqdomains must not set the
  91. * IRQCHIP_MOVE_DEFERRED flag.
  92. *
  93. * 2) Downstream irqdomains (or devices) with non-atomic MSI update
  94. * must use imsic_irq_set_affinity() in nterrupt-context upon
  95. * the next device interrupt so the irqchip of such irqdomains
  96. * must set the IRQCHIP_MOVE_DEFERRED flag.
  97. */
  98. old_vec = irq_data_get_irq_chip_data(d);
  99. if (WARN_ON(!old_vec))
  100. return -ENOENT;
  101. /* If old vector cpu belongs to the target cpumask then do nothing */
  102. if (cpumask_test_cpu(old_vec->cpu, mask_val))
  103. return IRQ_SET_MASK_OK_DONE;
  104. /* If move is already in-flight then return failure */
  105. if (imsic_vector_get_move(old_vec))
  106. return -EBUSY;
  107. /* Get a new vector on the desired set of CPUs */
  108. new_vec = imsic_vector_alloc(old_vec->irq, mask_val);
  109. if (!new_vec)
  110. return -ENOSPC;
  111. /*
  112. * Device having non-atomic MSI update might see an intermediate
  113. * state when changing target IMSIC vector from one CPU to another.
  114. *
  115. * To avoid losing interrupt to such intermediate state, do the
  116. * following (just like x86 APIC):
  117. *
  118. * 1) First write a temporary IMSIC vector to the device which
  119. * has MSI address same as the old IMSIC vector but MSI data
  120. * matches the new IMSIC vector.
  121. *
  122. * 2) Next write the new IMSIC vector to the device.
  123. *
  124. * Based on the above, __imsic_local_sync() must check pending
  125. * status of both old MSI data and new MSI data on the old CPU.
  126. */
  127. if (!irq_can_move_in_process_context(d) &&
  128. new_vec->local_id != old_vec->local_id) {
  129. /* Setup temporary vector */
  130. tmp_vec.cpu = old_vec->cpu;
  131. tmp_vec.local_id = new_vec->local_id;
  132. /* Point device to the temporary vector */
  133. imsic_msi_update_msg(irq_get_irq_data(d->irq), &tmp_vec);
  134. }
  135. /* Point device to the new vector */
  136. imsic_msi_update_msg(irq_get_irq_data(d->irq), new_vec);
  137. /* Update irq descriptors with the new vector */
  138. d->chip_data = new_vec;
  139. /* Update effective affinity */
  140. irq_data_update_effective_affinity(d, cpumask_of(new_vec->cpu));
  141. /* Move state of the old vector to the new vector */
  142. imsic_vector_move(old_vec, new_vec);
  143. return IRQ_SET_MASK_OK_DONE;
  144. }
  145. static void imsic_irq_force_complete_move(struct irq_data *d)
  146. {
  147. struct imsic_vector *mvec, *vec = irq_data_get_irq_chip_data(d);
  148. unsigned int cpu = smp_processor_id();
  149. if (WARN_ON(!vec))
  150. return;
  151. /* Do nothing if there is no in-flight move */
  152. mvec = imsic_vector_get_move(vec);
  153. if (!mvec)
  154. return;
  155. /* Do nothing if the old IMSIC vector does not belong to current CPU */
  156. if (mvec->cpu != cpu)
  157. return;
  158. /*
  159. * The best we can do is force cleanup the old IMSIC vector.
  160. *
  161. * The challenges over here are same as x86 vector domain so
  162. * refer to the comments in irq_force_complete_move() function
  163. * implemented at arch/x86/kernel/apic/vector.c.
  164. */
  165. /* Force cleanup in-flight move */
  166. pr_info("IRQ fixup: irq %d move in progress, old vector cpu %d local_id %d\n",
  167. d->irq, mvec->cpu, mvec->local_id);
  168. imsic_vector_force_move_cleanup(vec);
  169. }
  170. #endif
  171. static struct irq_chip imsic_irq_base_chip = {
  172. .name = "IMSIC",
  173. .irq_mask = imsic_irq_mask,
  174. .irq_unmask = imsic_irq_unmask,
  175. #ifdef CONFIG_SMP
  176. .irq_set_affinity = imsic_irq_set_affinity,
  177. .irq_force_complete_move = imsic_irq_force_complete_move,
  178. #endif
  179. .irq_retrigger = imsic_irq_retrigger,
  180. .irq_ack = imsic_irq_ack,
  181. .irq_compose_msi_msg = imsic_irq_compose_msg,
  182. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND,
  183. };
  184. static int imsic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  185. unsigned int nr_irqs, void *args)
  186. {
  187. struct imsic_vector *vec;
  188. /* Multi-MSI is not supported yet. */
  189. if (nr_irqs > 1)
  190. return -EOPNOTSUPP;
  191. vec = imsic_vector_alloc(virq, cpu_online_mask);
  192. if (!vec)
  193. return -ENOSPC;
  194. irq_domain_set_info(domain, virq, virq, &imsic_irq_base_chip, vec,
  195. handle_edge_irq, NULL, NULL);
  196. irq_set_noprobe(virq);
  197. irq_set_affinity(virq, cpu_online_mask);
  198. irq_data_update_effective_affinity(irq_get_irq_data(virq), cpumask_of(vec->cpu));
  199. return 0;
  200. }
  201. static void imsic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  202. unsigned int nr_irqs)
  203. {
  204. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  205. imsic_vector_free(irq_data_get_irq_chip_data(d));
  206. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  207. }
  208. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  209. static void imsic_irq_debug_show(struct seq_file *m, struct irq_domain *d,
  210. struct irq_data *irqd, int ind)
  211. {
  212. if (!irqd) {
  213. imsic_vector_debug_show_summary(m, ind);
  214. return;
  215. }
  216. imsic_vector_debug_show(m, irq_data_get_irq_chip_data(irqd), ind);
  217. }
  218. #endif
  219. static const struct irq_domain_ops imsic_base_domain_ops = {
  220. .alloc = imsic_irq_domain_alloc,
  221. .free = imsic_irq_domain_free,
  222. .select = msi_lib_irq_domain_select,
  223. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  224. .debug_show = imsic_irq_debug_show,
  225. #endif
  226. };
  227. static bool imsic_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
  228. struct irq_domain *real_parent, struct msi_domain_info *info)
  229. {
  230. if (!msi_lib_init_dev_msi_info(dev, domain, real_parent, info))
  231. return false;
  232. switch (info->bus_token) {
  233. case DOMAIN_BUS_PCI_DEVICE_MSI:
  234. case DOMAIN_BUS_PCI_DEVICE_MSIX:
  235. info->chip->flags |= IRQCHIP_MOVE_DEFERRED;
  236. break;
  237. default:
  238. break;
  239. }
  240. return true;
  241. }
  242. static const struct msi_parent_ops imsic_msi_parent_ops = {
  243. .supported_flags = MSI_GENERIC_FLAGS_MASK |
  244. MSI_FLAG_PCI_MSIX,
  245. .required_flags = MSI_FLAG_USE_DEF_DOM_OPS |
  246. MSI_FLAG_USE_DEF_CHIP_OPS |
  247. MSI_FLAG_PCI_MSI_MASK_PARENT,
  248. .chip_flags = MSI_CHIP_FLAG_SET_ACK,
  249. .bus_select_token = DOMAIN_BUS_NEXUS,
  250. .bus_select_mask = MATCH_PCI_MSI | MATCH_PLATFORM_MSI,
  251. .init_dev_msi_info = imsic_init_dev_msi_info,
  252. };
  253. int imsic_irqdomain_init(void)
  254. {
  255. struct irq_domain_info info = {
  256. .ops = &imsic_base_domain_ops,
  257. .host_data = imsic,
  258. };
  259. struct imsic_global_config *global;
  260. if (!imsic || !imsic->fwnode) {
  261. pr_err("early driver not probed\n");
  262. return -ENODEV;
  263. }
  264. if (imsic->base_domain) {
  265. pr_err("%pfwP: irq domain already created\n", imsic->fwnode);
  266. return -ENODEV;
  267. }
  268. /* Create Base IRQ domain */
  269. info.fwnode = imsic->fwnode,
  270. imsic->base_domain = msi_create_parent_irq_domain(&info, &imsic_msi_parent_ops);
  271. if (!imsic->base_domain) {
  272. pr_err("%pfwP: failed to create IMSIC base domain\n", imsic->fwnode);
  273. return -ENOMEM;
  274. }
  275. global = &imsic->global;
  276. pr_info("%pfwP: hart-index-bits: %d, guest-index-bits: %d\n",
  277. imsic->fwnode, global->hart_index_bits, global->guest_index_bits);
  278. pr_info("%pfwP: group-index-bits: %d, group-index-shift: %d\n",
  279. imsic->fwnode, global->group_index_bits, global->group_index_shift);
  280. pr_info("%pfwP: per-CPU IDs %d at base address %pa\n",
  281. imsic->fwnode, global->nr_ids, &global->base_addr);
  282. pr_info("%pfwP: total %d interrupts available\n",
  283. imsic->fwnode, num_possible_cpus() * (global->nr_ids - 1));
  284. return 0;
  285. }
  286. static int imsic_platform_probe_common(struct fwnode_handle *fwnode)
  287. {
  288. if (imsic && imsic->fwnode != fwnode) {
  289. pr_err("%pfwP: fwnode mismatch\n", fwnode);
  290. return -ENODEV;
  291. }
  292. return imsic_irqdomain_init();
  293. }
  294. static int imsic_platform_dt_probe(struct platform_device *pdev)
  295. {
  296. return imsic_platform_probe_common(pdev->dev.fwnode);
  297. }
  298. #ifdef CONFIG_ACPI
  299. /*
  300. * On ACPI based systems, PCI enumeration happens early during boot in
  301. * acpi_scan_init(). PCI enumeration expects MSI domain setup before
  302. * it calls pci_set_msi_domain(). Hence, unlike in DT where
  303. * imsic-platform drive probe happens late during boot, ACPI based
  304. * systems need to setup the MSI domain early.
  305. */
  306. int imsic_platform_acpi_probe(struct fwnode_handle *fwnode)
  307. {
  308. return imsic_platform_probe_common(fwnode);
  309. }
  310. #endif
  311. static const struct of_device_id imsic_platform_match[] = {
  312. { .compatible = "riscv,imsics" },
  313. {}
  314. };
  315. static struct platform_driver imsic_platform_driver = {
  316. .driver = {
  317. .name = "riscv-imsic",
  318. .of_match_table = imsic_platform_match,
  319. },
  320. .probe = imsic_platform_dt_probe,
  321. };
  322. builtin_platform_driver(imsic_platform_driver);