xics-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright 2011 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. */
  10. #include <linux/types.h>
  11. #include <linux/threads.h>
  12. #include <linux/kernel.h>
  13. #include <linux/irq.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/smp.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/init.h>
  19. #include <linux/cpu.h>
  20. #include <linux/of.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/delay.h>
  24. #include <asm/prom.h>
  25. #include <asm/io.h>
  26. #include <asm/smp.h>
  27. #include <asm/machdep.h>
  28. #include <asm/irq.h>
  29. #include <asm/errno.h>
  30. #include <asm/rtas.h>
  31. #include <asm/xics.h>
  32. #include <asm/firmware.h>
  33. /* Globals common to all ICP/ICS implementations */
  34. const struct icp_ops *icp_ops;
  35. unsigned int xics_default_server = 0xff;
  36. unsigned int xics_default_distrib_server = 0;
  37. unsigned int xics_interrupt_server_size = 8;
  38. DEFINE_PER_CPU(struct xics_cppr, xics_cppr);
  39. struct irq_domain *xics_host;
  40. static LIST_HEAD(ics_list);
  41. void xics_update_irq_servers(void)
  42. {
  43. int i, j;
  44. struct device_node *np;
  45. u32 ilen;
  46. const __be32 *ireg;
  47. u32 hcpuid;
  48. /* Find the server numbers for the boot cpu. */
  49. np = of_get_cpu_node(boot_cpuid, NULL);
  50. BUG_ON(!np);
  51. hcpuid = get_hard_smp_processor_id(boot_cpuid);
  52. xics_default_server = xics_default_distrib_server = hcpuid;
  53. pr_devel("xics: xics_default_server = 0x%x\n", xics_default_server);
  54. ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
  55. if (!ireg) {
  56. of_node_put(np);
  57. return;
  58. }
  59. i = ilen / sizeof(int);
  60. /* Global interrupt distribution server is specified in the last
  61. * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
  62. * entry fom this property for current boot cpu id and use it as
  63. * default distribution server
  64. */
  65. for (j = 0; j < i; j += 2) {
  66. if (be32_to_cpu(ireg[j]) == hcpuid) {
  67. xics_default_distrib_server = be32_to_cpu(ireg[j+1]);
  68. break;
  69. }
  70. }
  71. pr_devel("xics: xics_default_distrib_server = 0x%x\n",
  72. xics_default_distrib_server);
  73. of_node_put(np);
  74. }
  75. /* GIQ stuff, currently only supported on RTAS setups, will have
  76. * to be sorted properly for bare metal
  77. */
  78. void xics_set_cpu_giq(unsigned int gserver, unsigned int join)
  79. {
  80. #ifdef CONFIG_PPC_RTAS
  81. int index;
  82. int status;
  83. if (!rtas_indicator_present(GLOBAL_INTERRUPT_QUEUE, NULL))
  84. return;
  85. index = (1UL << xics_interrupt_server_size) - 1 - gserver;
  86. status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE, index, join);
  87. WARN(status < 0, "set-indicator(%d, %d, %u) returned %d\n",
  88. GLOBAL_INTERRUPT_QUEUE, index, join, status);
  89. #endif
  90. }
  91. void xics_setup_cpu(void)
  92. {
  93. icp_ops->set_priority(LOWEST_PRIORITY);
  94. xics_set_cpu_giq(xics_default_distrib_server, 1);
  95. }
  96. void xics_mask_unknown_vec(unsigned int vec)
  97. {
  98. struct ics *ics;
  99. pr_err("Interrupt 0x%x (real) is invalid, disabling it.\n", vec);
  100. list_for_each_entry(ics, &ics_list, link)
  101. ics->mask_unknown(ics, vec);
  102. }
  103. #ifdef CONFIG_SMP
  104. static void xics_request_ipi(void)
  105. {
  106. unsigned int ipi;
  107. ipi = irq_create_mapping(xics_host, XICS_IPI);
  108. BUG_ON(!ipi);
  109. /*
  110. * IPIs are marked IRQF_PERCPU. The handler was set in map.
  111. */
  112. BUG_ON(request_irq(ipi, icp_ops->ipi_action,
  113. IRQF_PERCPU | IRQF_NO_THREAD, "IPI", NULL));
  114. }
  115. void __init xics_smp_probe(void)
  116. {
  117. /* Setup cause_ipi callback based on which ICP is used */
  118. smp_ops->cause_ipi = icp_ops->cause_ipi;
  119. /* Register all the IPIs */
  120. xics_request_ipi();
  121. }
  122. #endif /* CONFIG_SMP */
  123. void xics_teardown_cpu(void)
  124. {
  125. struct xics_cppr *os_cppr = this_cpu_ptr(&xics_cppr);
  126. /*
  127. * we have to reset the cppr index to 0 because we're
  128. * not going to return from the IPI
  129. */
  130. os_cppr->index = 0;
  131. icp_ops->set_priority(0);
  132. icp_ops->teardown_cpu();
  133. }
  134. void xics_kexec_teardown_cpu(int secondary)
  135. {
  136. xics_teardown_cpu();
  137. icp_ops->flush_ipi();
  138. /*
  139. * Some machines need to have at least one cpu in the GIQ,
  140. * so leave the master cpu in the group.
  141. */
  142. if (secondary)
  143. xics_set_cpu_giq(xics_default_distrib_server, 0);
  144. }
  145. #ifdef CONFIG_HOTPLUG_CPU
  146. /* Interrupts are disabled. */
  147. void xics_migrate_irqs_away(void)
  148. {
  149. int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();
  150. unsigned int irq, virq;
  151. struct irq_desc *desc;
  152. /* If we used to be the default server, move to the new "boot_cpuid" */
  153. if (hw_cpu == xics_default_server)
  154. xics_update_irq_servers();
  155. /* Reject any interrupt that was queued to us... */
  156. icp_ops->set_priority(0);
  157. /* Remove ourselves from the global interrupt queue */
  158. xics_set_cpu_giq(xics_default_distrib_server, 0);
  159. for_each_irq_desc(virq, desc) {
  160. struct irq_chip *chip;
  161. long server;
  162. unsigned long flags;
  163. struct ics *ics;
  164. /* We can't set affinity on ISA interrupts */
  165. if (virq < NUM_ISA_INTERRUPTS)
  166. continue;
  167. /* We only need to migrate enabled IRQS */
  168. if (!desc->action)
  169. continue;
  170. if (desc->irq_data.domain != xics_host)
  171. continue;
  172. irq = desc->irq_data.hwirq;
  173. /* We need to get IPIs still. */
  174. if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
  175. continue;
  176. chip = irq_desc_get_chip(desc);
  177. if (!chip || !chip->irq_set_affinity)
  178. continue;
  179. raw_spin_lock_irqsave(&desc->lock, flags);
  180. /* Locate interrupt server */
  181. server = -1;
  182. ics = irq_desc_get_chip_data(desc);
  183. if (ics)
  184. server = ics->get_server(ics, irq);
  185. if (server < 0) {
  186. printk(KERN_ERR "%s: Can't find server for irq %d\n",
  187. __func__, irq);
  188. goto unlock;
  189. }
  190. /* We only support delivery to all cpus or to one cpu.
  191. * The irq has to be migrated only in the single cpu
  192. * case.
  193. */
  194. if (server != hw_cpu)
  195. goto unlock;
  196. /* This is expected during cpu offline. */
  197. if (cpu_online(cpu))
  198. pr_warning("IRQ %u affinity broken off cpu %u\n",
  199. virq, cpu);
  200. /* Reset affinity to all cpus */
  201. raw_spin_unlock_irqrestore(&desc->lock, flags);
  202. irq_set_affinity(virq, cpu_all_mask);
  203. continue;
  204. unlock:
  205. raw_spin_unlock_irqrestore(&desc->lock, flags);
  206. }
  207. /* Allow "sufficient" time to drop any inflight IRQ's */
  208. mdelay(5);
  209. /*
  210. * Allow IPIs again. This is done at the very end, after migrating all
  211. * interrupts, the expectation is that we'll only get woken up by an IPI
  212. * interrupt beyond this point, but leave externals masked just to be
  213. * safe. If we're using icp-opal this may actually allow all
  214. * interrupts anyway, but that should be OK.
  215. */
  216. icp_ops->set_priority(DEFAULT_PRIORITY);
  217. }
  218. #endif /* CONFIG_HOTPLUG_CPU */
  219. #ifdef CONFIG_SMP
  220. /*
  221. * For the moment we only implement delivery to all cpus or one cpu.
  222. *
  223. * If the requested affinity is cpu_all_mask, we set global affinity.
  224. * If not we set it to the first cpu in the mask, even if multiple cpus
  225. * are set. This is so things like irqbalance (which set core and package
  226. * wide affinities) do the right thing.
  227. *
  228. * We need to fix this to implement support for the links
  229. */
  230. int xics_get_irq_server(unsigned int virq, const struct cpumask *cpumask,
  231. unsigned int strict_check)
  232. {
  233. if (!distribute_irqs)
  234. return xics_default_server;
  235. if (!cpumask_subset(cpu_possible_mask, cpumask)) {
  236. int server = cpumask_first_and(cpu_online_mask, cpumask);
  237. if (server < nr_cpu_ids)
  238. return get_hard_smp_processor_id(server);
  239. if (strict_check)
  240. return -1;
  241. }
  242. /*
  243. * Workaround issue with some versions of JS20 firmware that
  244. * deliver interrupts to cpus which haven't been started. This
  245. * happens when using the maxcpus= boot option.
  246. */
  247. if (cpumask_equal(cpu_online_mask, cpu_present_mask))
  248. return xics_default_distrib_server;
  249. return xics_default_server;
  250. }
  251. #endif /* CONFIG_SMP */
  252. static int xics_host_match(struct irq_domain *h, struct device_node *node,
  253. enum irq_domain_bus_token bus_token)
  254. {
  255. struct ics *ics;
  256. list_for_each_entry(ics, &ics_list, link)
  257. if (ics->host_match(ics, node))
  258. return 1;
  259. return 0;
  260. }
  261. /* Dummies */
  262. static void xics_ipi_unmask(struct irq_data *d) { }
  263. static void xics_ipi_mask(struct irq_data *d) { }
  264. static struct irq_chip xics_ipi_chip = {
  265. .name = "XICS",
  266. .irq_eoi = NULL, /* Patched at init time */
  267. .irq_mask = xics_ipi_mask,
  268. .irq_unmask = xics_ipi_unmask,
  269. };
  270. static int xics_host_map(struct irq_domain *h, unsigned int virq,
  271. irq_hw_number_t hw)
  272. {
  273. struct ics *ics;
  274. pr_devel("xics: map virq %d, hwirq 0x%lx\n", virq, hw);
  275. /*
  276. * Mark interrupts as edge sensitive by default so that resend
  277. * actually works. The device-tree parsing will turn the LSIs
  278. * back to level.
  279. */
  280. irq_clear_status_flags(virq, IRQ_LEVEL);
  281. /* Don't call into ICS for IPIs */
  282. if (hw == XICS_IPI) {
  283. irq_set_chip_and_handler(virq, &xics_ipi_chip,
  284. handle_percpu_irq);
  285. return 0;
  286. }
  287. /* Let the ICS setup the chip data */
  288. list_for_each_entry(ics, &ics_list, link)
  289. if (ics->map(ics, virq) == 0)
  290. return 0;
  291. return -EINVAL;
  292. }
  293. static int xics_host_xlate(struct irq_domain *h, struct device_node *ct,
  294. const u32 *intspec, unsigned int intsize,
  295. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  296. {
  297. *out_hwirq = intspec[0];
  298. /*
  299. * If intsize is at least 2, we look for the type in the second cell,
  300. * we assume the LSB indicates a level interrupt.
  301. */
  302. if (intsize > 1) {
  303. if (intspec[1] & 1)
  304. *out_flags = IRQ_TYPE_LEVEL_LOW;
  305. else
  306. *out_flags = IRQ_TYPE_EDGE_RISING;
  307. } else
  308. *out_flags = IRQ_TYPE_LEVEL_LOW;
  309. return 0;
  310. }
  311. int xics_set_irq_type(struct irq_data *d, unsigned int flow_type)
  312. {
  313. /*
  314. * We only support these. This has really no effect other than setting
  315. * the corresponding descriptor bits mind you but those will in turn
  316. * affect the resend function when re-enabling an edge interrupt.
  317. *
  318. * Set set the default to edge as explained in map().
  319. */
  320. if (flow_type == IRQ_TYPE_DEFAULT || flow_type == IRQ_TYPE_NONE)
  321. flow_type = IRQ_TYPE_EDGE_RISING;
  322. if (flow_type != IRQ_TYPE_EDGE_RISING &&
  323. flow_type != IRQ_TYPE_LEVEL_LOW)
  324. return -EINVAL;
  325. irqd_set_trigger_type(d, flow_type);
  326. return IRQ_SET_MASK_OK_NOCOPY;
  327. }
  328. int xics_retrigger(struct irq_data *data)
  329. {
  330. /*
  331. * We need to push a dummy CPPR when retriggering, since the subsequent
  332. * EOI will try to pop it. Passing 0 works, as the function hard codes
  333. * the priority value anyway.
  334. */
  335. xics_push_cppr(0);
  336. /* Tell the core to do a soft retrigger */
  337. return 0;
  338. }
  339. static const struct irq_domain_ops xics_host_ops = {
  340. .match = xics_host_match,
  341. .map = xics_host_map,
  342. .xlate = xics_host_xlate,
  343. };
  344. static void __init xics_init_host(void)
  345. {
  346. xics_host = irq_domain_add_tree(NULL, &xics_host_ops, NULL);
  347. BUG_ON(xics_host == NULL);
  348. irq_set_default_host(xics_host);
  349. }
  350. void __init xics_register_ics(struct ics *ics)
  351. {
  352. list_add(&ics->link, &ics_list);
  353. }
  354. static void __init xics_get_server_size(void)
  355. {
  356. struct device_node *np;
  357. const __be32 *isize;
  358. /* We fetch the interrupt server size from the first ICS node
  359. * we find if any
  360. */
  361. np = of_find_compatible_node(NULL, NULL, "ibm,ppc-xics");
  362. if (!np)
  363. return;
  364. isize = of_get_property(np, "ibm,interrupt-server#-size", NULL);
  365. if (!isize)
  366. return;
  367. xics_interrupt_server_size = be32_to_cpu(*isize);
  368. of_node_put(np);
  369. }
  370. void __init xics_init(void)
  371. {
  372. int rc = -1;
  373. /* Fist locate ICP */
  374. if (firmware_has_feature(FW_FEATURE_LPAR))
  375. rc = icp_hv_init();
  376. if (rc < 0) {
  377. rc = icp_native_init();
  378. if (rc == -ENODEV)
  379. rc = icp_opal_init();
  380. }
  381. if (rc < 0) {
  382. pr_warning("XICS: Cannot find a Presentation Controller !\n");
  383. return;
  384. }
  385. /* Copy get_irq callback over to ppc_md */
  386. ppc_md.get_irq = icp_ops->get_irq;
  387. /* Patch up IPI chip EOI */
  388. xics_ipi_chip.irq_eoi = icp_ops->eoi;
  389. /* Now locate ICS */
  390. rc = ics_rtas_init();
  391. if (rc < 0)
  392. rc = ics_opal_init();
  393. if (rc < 0)
  394. pr_warning("XICS: Cannot find a Source Controller !\n");
  395. /* Initialize common bits */
  396. xics_get_server_size();
  397. xics_update_irq_servers();
  398. xics_init_host();
  399. xics_setup_cpu();
  400. }