xics-common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <asm/prom.h>
  24. #include <asm/io.h>
  25. #include <asm/smp.h>
  26. #include <asm/machdep.h>
  27. #include <asm/irq.h>
  28. #include <asm/errno.h>
  29. #include <asm/rtas.h>
  30. #include <asm/xics.h>
  31. #include <asm/firmware.h>
  32. /* Globals common to all ICP/ICS implementations */
  33. const struct icp_ops *icp_ops;
  34. unsigned int xics_default_server = 0xff;
  35. unsigned int xics_default_distrib_server = 0;
  36. unsigned int xics_interrupt_server_size = 8;
  37. DEFINE_PER_CPU(struct xics_cppr, xics_cppr);
  38. struct irq_host *xics_host;
  39. static LIST_HEAD(ics_list);
  40. void xics_update_irq_servers(void)
  41. {
  42. int i, j;
  43. struct device_node *np;
  44. u32 ilen;
  45. const u32 *ireg;
  46. u32 hcpuid;
  47. /* Find the server numbers for the boot cpu. */
  48. np = of_get_cpu_node(boot_cpuid, NULL);
  49. BUG_ON(!np);
  50. hcpuid = get_hard_smp_processor_id(boot_cpuid);
  51. xics_default_server = xics_default_distrib_server = hcpuid;
  52. pr_devel("xics: xics_default_server = 0x%x\n", xics_default_server);
  53. ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
  54. if (!ireg) {
  55. of_node_put(np);
  56. return;
  57. }
  58. i = ilen / sizeof(int);
  59. /* Global interrupt distribution server is specified in the last
  60. * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
  61. * entry fom this property for current boot cpu id and use it as
  62. * default distribution server
  63. */
  64. for (j = 0; j < i; j += 2) {
  65. if (ireg[j] == hcpuid) {
  66. xics_default_distrib_server = ireg[j+1];
  67. break;
  68. }
  69. }
  70. pr_devel("xics: xics_default_distrib_server = 0x%x\n",
  71. xics_default_distrib_server);
  72. of_node_put(np);
  73. }
  74. /* GIQ stuff, currently only supported on RTAS setups, will have
  75. * to be sorted properly for bare metal
  76. */
  77. void xics_set_cpu_giq(unsigned int gserver, unsigned int join)
  78. {
  79. #ifdef CONFIG_PPC_RTAS
  80. int index;
  81. int status;
  82. if (!rtas_indicator_present(GLOBAL_INTERRUPT_QUEUE, NULL))
  83. return;
  84. index = (1UL << xics_interrupt_server_size) - 1 - gserver;
  85. status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE, index, join);
  86. WARN(status < 0, "set-indicator(%d, %d, %u) returned %d\n",
  87. GLOBAL_INTERRUPT_QUEUE, index, join, status);
  88. #endif
  89. }
  90. void xics_setup_cpu(void)
  91. {
  92. icp_ops->set_priority(LOWEST_PRIORITY);
  93. xics_set_cpu_giq(xics_default_distrib_server, 1);
  94. }
  95. void xics_mask_unknown_vec(unsigned int vec)
  96. {
  97. struct ics *ics;
  98. pr_err("Interrupt 0x%x (real) is invalid, disabling it.\n", vec);
  99. list_for_each_entry(ics, &ics_list, link)
  100. ics->mask_unknown(ics, vec);
  101. }
  102. #ifdef CONFIG_SMP
  103. DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, xics_ipi_message);
  104. irqreturn_t xics_ipi_dispatch(int cpu)
  105. {
  106. unsigned long *tgt = &per_cpu(xics_ipi_message, cpu);
  107. mb(); /* order mmio clearing qirr */
  108. while (*tgt) {
  109. if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION, tgt)) {
  110. smp_message_recv(PPC_MSG_CALL_FUNCTION);
  111. }
  112. if (test_and_clear_bit(PPC_MSG_RESCHEDULE, tgt)) {
  113. smp_message_recv(PPC_MSG_RESCHEDULE);
  114. }
  115. if (test_and_clear_bit(PPC_MSG_CALL_FUNC_SINGLE, tgt)) {
  116. smp_message_recv(PPC_MSG_CALL_FUNC_SINGLE);
  117. }
  118. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  119. if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK, tgt)) {
  120. smp_message_recv(PPC_MSG_DEBUGGER_BREAK);
  121. }
  122. #endif
  123. }
  124. return IRQ_HANDLED;
  125. }
  126. static void xics_request_ipi(void)
  127. {
  128. unsigned int ipi;
  129. ipi = irq_create_mapping(xics_host, XICS_IPI);
  130. BUG_ON(ipi == NO_IRQ);
  131. /*
  132. * IPIs are marked IRQF_DISABLED as they must run with irqs
  133. * disabled
  134. */
  135. irq_set_handler(ipi, handle_percpu_irq);
  136. BUG_ON(request_irq(ipi, icp_ops->ipi_action,
  137. IRQF_DISABLED|IRQF_PERCPU, "IPI", NULL));
  138. }
  139. int __init xics_smp_probe(void)
  140. {
  141. /* Setup message_pass callback based on which ICP is used */
  142. smp_ops->message_pass = icp_ops->message_pass;
  143. /* Register all the IPIs */
  144. xics_request_ipi();
  145. return cpumask_weight(cpu_possible_mask);
  146. }
  147. #endif /* CONFIG_SMP */
  148. void xics_teardown_cpu(void)
  149. {
  150. struct xics_cppr *os_cppr = &__get_cpu_var(xics_cppr);
  151. /*
  152. * we have to reset the cppr index to 0 because we're
  153. * not going to return from the IPI
  154. */
  155. os_cppr->index = 0;
  156. icp_ops->set_priority(0);
  157. icp_ops->teardown_cpu();
  158. }
  159. void xics_kexec_teardown_cpu(int secondary)
  160. {
  161. xics_teardown_cpu();
  162. icp_ops->flush_ipi();
  163. /*
  164. * Some machines need to have at least one cpu in the GIQ,
  165. * so leave the master cpu in the group.
  166. */
  167. if (secondary)
  168. xics_set_cpu_giq(xics_default_distrib_server, 0);
  169. }
  170. #ifdef CONFIG_HOTPLUG_CPU
  171. /* Interrupts are disabled. */
  172. void xics_migrate_irqs_away(void)
  173. {
  174. int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();
  175. unsigned int irq, virq;
  176. /* If we used to be the default server, move to the new "boot_cpuid" */
  177. if (hw_cpu == xics_default_server)
  178. xics_update_irq_servers();
  179. /* Reject any interrupt that was queued to us... */
  180. icp_ops->set_priority(0);
  181. /* Remove ourselves from the global interrupt queue */
  182. xics_set_cpu_giq(xics_default_distrib_server, 0);
  183. /* Allow IPIs again... */
  184. icp_ops->set_priority(DEFAULT_PRIORITY);
  185. for_each_irq(virq) {
  186. struct irq_desc *desc;
  187. struct irq_chip *chip;
  188. long server;
  189. unsigned long flags;
  190. struct ics *ics;
  191. /* We can't set affinity on ISA interrupts */
  192. if (virq < NUM_ISA_INTERRUPTS)
  193. continue;
  194. if (virq_to_host(virq) != xics_host)
  195. continue;
  196. irq = (unsigned int)virq_to_hw(virq);
  197. /* We need to get IPIs still. */
  198. if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
  199. continue;
  200. desc = irq_to_desc(virq);
  201. /* We only need to migrate enabled IRQS */
  202. if (!desc || !desc->action)
  203. continue;
  204. chip = irq_desc_get_chip(desc);
  205. if (!chip || !chip->irq_set_affinity)
  206. continue;
  207. raw_spin_lock_irqsave(&desc->lock, flags);
  208. /* Locate interrupt server */
  209. server = -1;
  210. ics = irq_get_chip_data(virq);
  211. if (ics)
  212. server = ics->get_server(ics, irq);
  213. if (server < 0) {
  214. printk(KERN_ERR "%s: Can't find server for irq %d\n",
  215. __func__, irq);
  216. goto unlock;
  217. }
  218. /* We only support delivery to all cpus or to one cpu.
  219. * The irq has to be migrated only in the single cpu
  220. * case.
  221. */
  222. if (server != hw_cpu)
  223. goto unlock;
  224. /* This is expected during cpu offline. */
  225. if (cpu_online(cpu))
  226. pr_warning("IRQ %u affinity broken off cpu %u\n",
  227. virq, cpu);
  228. /* Reset affinity to all cpus */
  229. raw_spin_unlock_irqrestore(&desc->lock, flags);
  230. irq_set_affinity(virq, cpu_all_mask);
  231. continue;
  232. unlock:
  233. raw_spin_unlock_irqrestore(&desc->lock, flags);
  234. }
  235. }
  236. #endif /* CONFIG_HOTPLUG_CPU */
  237. #ifdef CONFIG_SMP
  238. /*
  239. * For the moment we only implement delivery to all cpus or one cpu.
  240. *
  241. * If the requested affinity is cpu_all_mask, we set global affinity.
  242. * If not we set it to the first cpu in the mask, even if multiple cpus
  243. * are set. This is so things like irqbalance (which set core and package
  244. * wide affinities) do the right thing.
  245. *
  246. * We need to fix this to implement support for the links
  247. */
  248. int xics_get_irq_server(unsigned int virq, const struct cpumask *cpumask,
  249. unsigned int strict_check)
  250. {
  251. if (!distribute_irqs)
  252. return xics_default_server;
  253. if (!cpumask_subset(cpu_possible_mask, cpumask)) {
  254. int server = cpumask_first_and(cpu_online_mask, cpumask);
  255. if (server < nr_cpu_ids)
  256. return get_hard_smp_processor_id(server);
  257. if (strict_check)
  258. return -1;
  259. }
  260. /*
  261. * Workaround issue with some versions of JS20 firmware that
  262. * deliver interrupts to cpus which haven't been started. This
  263. * happens when using the maxcpus= boot option.
  264. */
  265. if (cpumask_equal(cpu_online_mask, cpu_present_mask))
  266. return xics_default_distrib_server;
  267. return xics_default_server;
  268. }
  269. #endif /* CONFIG_SMP */
  270. static int xics_host_match(struct irq_host *h, struct device_node *node)
  271. {
  272. struct ics *ics;
  273. list_for_each_entry(ics, &ics_list, link)
  274. if (ics->host_match(ics, node))
  275. return 1;
  276. return 0;
  277. }
  278. /* Dummies */
  279. static void xics_ipi_unmask(struct irq_data *d) { }
  280. static void xics_ipi_mask(struct irq_data *d) { }
  281. static struct irq_chip xics_ipi_chip = {
  282. .name = "XICS",
  283. .irq_eoi = NULL, /* Patched at init time */
  284. .irq_mask = xics_ipi_mask,
  285. .irq_unmask = xics_ipi_unmask,
  286. };
  287. static int xics_host_map(struct irq_host *h, unsigned int virq,
  288. irq_hw_number_t hw)
  289. {
  290. struct ics *ics;
  291. pr_devel("xics: map virq %d, hwirq 0x%lx\n", virq, hw);
  292. /* Insert the interrupt mapping into the radix tree for fast lookup */
  293. irq_radix_revmap_insert(xics_host, virq, hw);
  294. /* They aren't all level sensitive but we just don't really know */
  295. irq_set_status_flags(virq, IRQ_LEVEL);
  296. /* Don't call into ICS for IPIs */
  297. if (hw == XICS_IPI) {
  298. irq_set_chip_and_handler(virq, &xics_ipi_chip,
  299. handle_fasteoi_irq);
  300. return 0;
  301. }
  302. /* Let the ICS setup the chip data */
  303. list_for_each_entry(ics, &ics_list, link)
  304. if (ics->map(ics, virq) == 0)
  305. break;
  306. return 0;
  307. }
  308. static int xics_host_xlate(struct irq_host *h, struct device_node *ct,
  309. const u32 *intspec, unsigned int intsize,
  310. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  311. {
  312. /* Current xics implementation translates everything
  313. * to level. It is not technically right for MSIs but this
  314. * is irrelevant at this point. We might get smarter in the future
  315. */
  316. *out_hwirq = intspec[0];
  317. *out_flags = IRQ_TYPE_LEVEL_LOW;
  318. return 0;
  319. }
  320. static struct irq_host_ops xics_host_ops = {
  321. .match = xics_host_match,
  322. .map = xics_host_map,
  323. .xlate = xics_host_xlate,
  324. };
  325. static void __init xics_init_host(void)
  326. {
  327. xics_host = irq_alloc_host(NULL, IRQ_HOST_MAP_TREE, 0, &xics_host_ops,
  328. XICS_IRQ_SPURIOUS);
  329. BUG_ON(xics_host == NULL);
  330. irq_set_default_host(xics_host);
  331. }
  332. void __init xics_register_ics(struct ics *ics)
  333. {
  334. list_add(&ics->link, &ics_list);
  335. }
  336. static void __init xics_get_server_size(void)
  337. {
  338. struct device_node *np;
  339. const u32 *isize;
  340. /* We fetch the interrupt server size from the first ICS node
  341. * we find if any
  342. */
  343. np = of_find_compatible_node(NULL, NULL, "ibm,ppc-xics");
  344. if (!np)
  345. return;
  346. isize = of_get_property(np, "ibm,interrupt-server#-size", NULL);
  347. if (!isize)
  348. return;
  349. xics_interrupt_server_size = *isize;
  350. of_node_put(np);
  351. }
  352. void __init xics_init(void)
  353. {
  354. int rc = -1;
  355. /* Fist locate ICP */
  356. #ifdef CONFIG_PPC_ICP_HV
  357. if (firmware_has_feature(FW_FEATURE_LPAR))
  358. rc = icp_hv_init();
  359. #endif
  360. #ifdef CONFIG_PPC_ICP_NATIVE
  361. if (rc < 0)
  362. rc = icp_native_init();
  363. #endif
  364. if (rc < 0) {
  365. pr_warning("XICS: Cannot find a Presentation Controller !\n");
  366. return;
  367. }
  368. /* Copy get_irq callback over to ppc_md */
  369. ppc_md.get_irq = icp_ops->get_irq;
  370. /* Patch up IPI chip EOI */
  371. xics_ipi_chip.irq_eoi = icp_ops->eoi;
  372. /* Now locate ICS */
  373. #ifdef CONFIG_PPC_ICS_RTAS
  374. rc = ics_rtas_init();
  375. #endif
  376. if (rc < 0)
  377. pr_warning("XICS: Cannot find a Source Controller !\n");
  378. /* Initialize common bits */
  379. xics_get_server_size();
  380. xics_update_irq_servers();
  381. xics_init_host();
  382. xics_setup_cpu();
  383. }