icp-native.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2011 IBM Corporation.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/kernel.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/smp.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/cpu.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/module.h>
  17. #include <asm/io.h>
  18. #include <asm/smp.h>
  19. #include <asm/irq.h>
  20. #include <asm/errno.h>
  21. #include <asm/xics.h>
  22. #include <asm/kvm_ppc.h>
  23. #include <asm/dbell.h>
  24. struct icp_ipl {
  25. union {
  26. u32 word;
  27. u8 bytes[4];
  28. } xirr_poll;
  29. union {
  30. u32 word;
  31. u8 bytes[4];
  32. } xirr;
  33. u32 dummy;
  34. union {
  35. u32 word;
  36. u8 bytes[4];
  37. } qirr;
  38. u32 link_a;
  39. u32 link_b;
  40. u32 link_c;
  41. };
  42. static struct icp_ipl __iomem *icp_native_regs[NR_CPUS];
  43. static inline unsigned int icp_native_get_xirr(void)
  44. {
  45. int cpu = smp_processor_id();
  46. unsigned int xirr;
  47. /* Handled an interrupt latched by KVM */
  48. xirr = kvmppc_get_xics_latch();
  49. if (xirr)
  50. return xirr;
  51. return in_be32(&icp_native_regs[cpu]->xirr.word);
  52. }
  53. static inline void icp_native_set_xirr(unsigned int value)
  54. {
  55. int cpu = smp_processor_id();
  56. out_be32(&icp_native_regs[cpu]->xirr.word, value);
  57. }
  58. static inline void icp_native_set_cppr(u8 value)
  59. {
  60. int cpu = smp_processor_id();
  61. out_8(&icp_native_regs[cpu]->xirr.bytes[0], value);
  62. }
  63. static inline void icp_native_set_qirr(int n_cpu, u8 value)
  64. {
  65. out_8(&icp_native_regs[n_cpu]->qirr.bytes[0], value);
  66. }
  67. static void icp_native_set_cpu_priority(unsigned char cppr)
  68. {
  69. xics_set_base_cppr(cppr);
  70. icp_native_set_cppr(cppr);
  71. iosync();
  72. }
  73. void icp_native_eoi(struct irq_data *d)
  74. {
  75. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  76. iosync();
  77. icp_native_set_xirr((xics_pop_cppr() << 24) | hw_irq);
  78. }
  79. static void icp_native_teardown_cpu(void)
  80. {
  81. int cpu = smp_processor_id();
  82. /* Clear any pending IPI */
  83. icp_native_set_qirr(cpu, 0xff);
  84. }
  85. static void icp_native_flush_ipi(void)
  86. {
  87. /* We take the ipi irq but and never return so we
  88. * need to EOI the IPI, but want to leave our priority 0
  89. *
  90. * should we check all the other interrupts too?
  91. * should we be flagging idle loop instead?
  92. * or creating some task to be scheduled?
  93. */
  94. icp_native_set_xirr((0x00 << 24) | XICS_IPI);
  95. }
  96. static unsigned int icp_native_get_irq(void)
  97. {
  98. unsigned int xirr = icp_native_get_xirr();
  99. unsigned int vec = xirr & 0x00ffffff;
  100. unsigned int irq;
  101. if (vec == XICS_IRQ_SPURIOUS)
  102. return 0;
  103. irq = irq_find_mapping(xics_host, vec);
  104. if (likely(irq)) {
  105. xics_push_cppr(vec);
  106. return irq;
  107. }
  108. /* We don't have a linux mapping, so have rtas mask it. */
  109. xics_mask_unknown_vec(vec);
  110. /* We might learn about it later, so EOI it */
  111. icp_native_set_xirr(xirr);
  112. return 0;
  113. }
  114. #ifdef CONFIG_SMP
  115. static void icp_native_cause_ipi(int cpu)
  116. {
  117. kvmppc_set_host_ipi(cpu);
  118. icp_native_set_qirr(cpu, IPI_PRIORITY);
  119. }
  120. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  121. void icp_native_cause_ipi_rm(int cpu)
  122. {
  123. /*
  124. * Currently not used to send IPIs to another CPU
  125. * on the same core. Only caller is KVM real mode.
  126. * Need the physical address of the XICS to be
  127. * previously saved in kvm_hstate in the paca.
  128. */
  129. void __iomem *xics_phys;
  130. /*
  131. * Just like the cause_ipi functions, it is required to
  132. * include a full barrier before causing the IPI.
  133. */
  134. xics_phys = paca_ptrs[cpu]->kvm_hstate.xics_phys;
  135. mb();
  136. __raw_rm_writeb(IPI_PRIORITY, xics_phys + XICS_MFRR);
  137. }
  138. #endif
  139. /*
  140. * Called when an interrupt is received on an off-line CPU to
  141. * clear the interrupt, so that the CPU can go back to nap mode.
  142. */
  143. void icp_native_flush_interrupt(void)
  144. {
  145. unsigned int xirr = icp_native_get_xirr();
  146. unsigned int vec = xirr & 0x00ffffff;
  147. if (vec == XICS_IRQ_SPURIOUS)
  148. return;
  149. if (vec == XICS_IPI) {
  150. /* Clear pending IPI */
  151. int cpu = smp_processor_id();
  152. kvmppc_clear_host_ipi(cpu);
  153. icp_native_set_qirr(cpu, 0xff);
  154. } else {
  155. pr_err("XICS: hw interrupt 0x%x to offline cpu, disabling\n",
  156. vec);
  157. xics_mask_unknown_vec(vec);
  158. }
  159. /* EOI the interrupt */
  160. icp_native_set_xirr(xirr);
  161. }
  162. void xics_wake_cpu(int cpu)
  163. {
  164. icp_native_set_qirr(cpu, IPI_PRIORITY);
  165. }
  166. EXPORT_SYMBOL_GPL(xics_wake_cpu);
  167. static irqreturn_t icp_native_ipi_action(int irq, void *dev_id)
  168. {
  169. int cpu = smp_processor_id();
  170. kvmppc_clear_host_ipi(cpu);
  171. icp_native_set_qirr(cpu, 0xff);
  172. return smp_ipi_demux();
  173. }
  174. #endif /* CONFIG_SMP */
  175. static int __init icp_native_map_one_cpu(int hw_id, unsigned long addr,
  176. unsigned long size)
  177. {
  178. char *rname;
  179. int i, cpu = -1;
  180. /* This may look gross but it's good enough for now, we don't quite
  181. * have a hard -> linux processor id matching.
  182. */
  183. for_each_possible_cpu(i) {
  184. if (!cpu_present(i))
  185. continue;
  186. if (hw_id == get_hard_smp_processor_id(i)) {
  187. cpu = i;
  188. break;
  189. }
  190. }
  191. /* Fail, skip that CPU. Don't print, it's normal, some XICS come up
  192. * with way more entries in there than you have CPUs
  193. */
  194. if (cpu == -1)
  195. return 0;
  196. rname = kasprintf(GFP_KERNEL, "CPU %d [0x%x] Interrupt Presentation",
  197. cpu, hw_id);
  198. if (!request_mem_region(addr, size, rname)) {
  199. pr_warn("icp_native: Could not reserve ICP MMIO for CPU %d, interrupt server #0x%x\n",
  200. cpu, hw_id);
  201. return -EBUSY;
  202. }
  203. icp_native_regs[cpu] = ioremap(addr, size);
  204. kvmppc_set_xics_phys(cpu, addr);
  205. if (!icp_native_regs[cpu]) {
  206. pr_warn("icp_native: Failed ioremap for CPU %d, interrupt server #0x%x, addr %#lx\n",
  207. cpu, hw_id, addr);
  208. release_mem_region(addr, size);
  209. return -ENOMEM;
  210. }
  211. return 0;
  212. }
  213. static int __init icp_native_init_one_node(struct device_node *np,
  214. unsigned int *indx)
  215. {
  216. unsigned int ilen;
  217. const __be32 *ireg;
  218. int i;
  219. int reg_tuple_size;
  220. int num_servers = 0;
  221. /* This code does the theorically broken assumption that the interrupt
  222. * server numbers are the same as the hard CPU numbers.
  223. * This happens to be the case so far but we are playing with fire...
  224. * should be fixed one of these days. -BenH.
  225. */
  226. ireg = of_get_property(np, "ibm,interrupt-server-ranges", &ilen);
  227. /* Do that ever happen ? we'll know soon enough... but even good'old
  228. * f80 does have that property ..
  229. */
  230. WARN_ON((ireg == NULL) || (ilen != 2*sizeof(u32)));
  231. if (ireg) {
  232. *indx = of_read_number(ireg, 1);
  233. if (ilen >= 2*sizeof(u32))
  234. num_servers = of_read_number(ireg + 1, 1);
  235. }
  236. ireg = of_get_property(np, "reg", &ilen);
  237. if (!ireg) {
  238. pr_err("icp_native: Can't find interrupt reg property");
  239. return -1;
  240. }
  241. reg_tuple_size = (of_n_addr_cells(np) + of_n_size_cells(np)) * 4;
  242. if (((ilen % reg_tuple_size) != 0)
  243. || (num_servers && (num_servers != (ilen / reg_tuple_size)))) {
  244. pr_err("icp_native: ICP reg len (%d) != num servers (%d)",
  245. ilen / reg_tuple_size, num_servers);
  246. return -1;
  247. }
  248. for (i = 0; i < (ilen / reg_tuple_size); i++) {
  249. struct resource r;
  250. int err;
  251. err = of_address_to_resource(np, i, &r);
  252. if (err) {
  253. pr_err("icp_native: Could not translate ICP MMIO"
  254. " for interrupt server 0x%x (%d)\n", *indx, err);
  255. return -1;
  256. }
  257. if (icp_native_map_one_cpu(*indx, r.start, resource_size(&r)))
  258. return -1;
  259. (*indx)++;
  260. }
  261. return 0;
  262. }
  263. static const struct icp_ops icp_native_ops = {
  264. .get_irq = icp_native_get_irq,
  265. .eoi = icp_native_eoi,
  266. .set_priority = icp_native_set_cpu_priority,
  267. .teardown_cpu = icp_native_teardown_cpu,
  268. .flush_ipi = icp_native_flush_ipi,
  269. #ifdef CONFIG_SMP
  270. .ipi_action = icp_native_ipi_action,
  271. .cause_ipi = icp_native_cause_ipi,
  272. #endif
  273. };
  274. int __init icp_native_init(void)
  275. {
  276. struct device_node *np;
  277. u32 indx = 0;
  278. int found = 0;
  279. for_each_compatible_node(np, NULL, "ibm,ppc-xicp")
  280. if (icp_native_init_one_node(np, &indx) == 0)
  281. found = 1;
  282. if (!found) {
  283. for_each_node_by_type(np,
  284. "PowerPC-External-Interrupt-Presentation") {
  285. if (icp_native_init_one_node(np, &indx) == 0)
  286. found = 1;
  287. }
  288. }
  289. if (found == 0)
  290. return -ENODEV;
  291. icp_ops = &icp_native_ops;
  292. return 0;
  293. }