xics.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * arch/powerpc/platforms/pseries/xics.c
  3. *
  4. * Copyright 2000 IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/threads.h>
  13. #include <linux/kernel.h>
  14. #include <linux/irq.h>
  15. #include <linux/smp.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/init.h>
  18. #include <linux/radix-tree.h>
  19. #include <linux/cpu.h>
  20. #include <linux/msi.h>
  21. #include <linux/of.h>
  22. #include <asm/firmware.h>
  23. #include <asm/io.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/smp.h>
  26. #include <asm/rtas.h>
  27. #include <asm/hvcall.h>
  28. #include <asm/machdep.h>
  29. #include "xics.h"
  30. #include "plpar_wrappers.h"
  31. static struct irq_host *xics_host;
  32. #define XICS_IPI 2
  33. #define XICS_IRQ_SPURIOUS 0
  34. /* Want a priority other than 0. Various HW issues require this. */
  35. #define DEFAULT_PRIORITY 5
  36. /*
  37. * Mark IPIs as higher priority so we can take them inside interrupts that
  38. * arent marked IRQF_DISABLED
  39. */
  40. #define IPI_PRIORITY 4
  41. static unsigned int default_server = 0xFF;
  42. static unsigned int default_distrib_server = 0;
  43. static unsigned int interrupt_server_size = 8;
  44. /* RTAS service tokens */
  45. static int ibm_get_xive;
  46. static int ibm_set_xive;
  47. static int ibm_int_on;
  48. static int ibm_int_off;
  49. /* Direct hardware low level accessors */
  50. /* The part of the interrupt presentation layer that we care about */
  51. struct xics_ipl {
  52. union {
  53. u32 word;
  54. u8 bytes[4];
  55. } xirr_poll;
  56. union {
  57. u32 word;
  58. u8 bytes[4];
  59. } xirr;
  60. u32 dummy;
  61. union {
  62. u32 word;
  63. u8 bytes[4];
  64. } qirr;
  65. };
  66. static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
  67. static inline unsigned int direct_xirr_info_get(void)
  68. {
  69. int cpu = smp_processor_id();
  70. return in_be32(&xics_per_cpu[cpu]->xirr.word);
  71. }
  72. static inline void direct_xirr_info_set(unsigned int value)
  73. {
  74. int cpu = smp_processor_id();
  75. out_be32(&xics_per_cpu[cpu]->xirr.word, value);
  76. }
  77. static inline void direct_cppr_info(u8 value)
  78. {
  79. int cpu = smp_processor_id();
  80. out_8(&xics_per_cpu[cpu]->xirr.bytes[0], value);
  81. }
  82. static inline void direct_qirr_info(int n_cpu, u8 value)
  83. {
  84. out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
  85. }
  86. /* LPAR low level accessors */
  87. static inline unsigned int lpar_xirr_info_get(void)
  88. {
  89. unsigned long lpar_rc;
  90. unsigned long return_value;
  91. lpar_rc = plpar_xirr(&return_value);
  92. if (lpar_rc != H_SUCCESS)
  93. panic(" bad return code xirr - rc = %lx \n", lpar_rc);
  94. return (unsigned int)return_value;
  95. }
  96. static inline void lpar_xirr_info_set(unsigned int value)
  97. {
  98. unsigned long lpar_rc;
  99. lpar_rc = plpar_eoi(value);
  100. if (lpar_rc != H_SUCCESS)
  101. panic("bad return code EOI - rc = %ld, value=%x\n", lpar_rc,
  102. value);
  103. }
  104. static inline void lpar_cppr_info(u8 value)
  105. {
  106. unsigned long lpar_rc;
  107. lpar_rc = plpar_cppr(value);
  108. if (lpar_rc != H_SUCCESS)
  109. panic("bad return code cppr - rc = %lx\n", lpar_rc);
  110. }
  111. static inline void lpar_qirr_info(int n_cpu , u8 value)
  112. {
  113. unsigned long lpar_rc;
  114. lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
  115. if (lpar_rc != H_SUCCESS)
  116. panic("bad return code qirr - rc = %lx\n", lpar_rc);
  117. }
  118. /* Interface to generic irq subsystem */
  119. #ifdef CONFIG_SMP
  120. static int get_irq_server(unsigned int virq, unsigned int strict_check)
  121. {
  122. int server;
  123. /* For the moment only implement delivery to all cpus or one cpu */
  124. cpumask_t cpumask;
  125. cpumask_t tmp = CPU_MASK_NONE;
  126. cpumask_copy(&cpumask, irq_desc[virq].affinity);
  127. if (!distribute_irqs)
  128. return default_server;
  129. if (!cpus_equal(cpumask, CPU_MASK_ALL)) {
  130. cpus_and(tmp, cpu_online_map, cpumask);
  131. server = first_cpu(tmp);
  132. if (server < NR_CPUS)
  133. return get_hard_smp_processor_id(server);
  134. if (strict_check)
  135. return -1;
  136. }
  137. if (cpus_equal(cpu_online_map, cpu_present_map))
  138. return default_distrib_server;
  139. return default_server;
  140. }
  141. #else
  142. static int get_irq_server(unsigned int virq, unsigned int strict_check)
  143. {
  144. return default_server;
  145. }
  146. #endif
  147. static void xics_unmask_irq(unsigned int virq)
  148. {
  149. unsigned int irq;
  150. int call_status;
  151. int server;
  152. pr_devel("xics: unmask virq %d\n", virq);
  153. irq = (unsigned int)irq_map[virq].hwirq;
  154. pr_devel(" -> map to hwirq 0x%x\n", irq);
  155. if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
  156. return;
  157. server = get_irq_server(virq, 0);
  158. call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
  159. DEFAULT_PRIORITY);
  160. if (call_status != 0) {
  161. printk(KERN_ERR
  162. "%s: ibm_set_xive irq %u server %x returned %d\n",
  163. __func__, irq, server, call_status);
  164. return;
  165. }
  166. /* Now unmask the interrupt (often a no-op) */
  167. call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
  168. if (call_status != 0) {
  169. printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n",
  170. __func__, irq, call_status);
  171. return;
  172. }
  173. }
  174. static unsigned int xics_startup(unsigned int virq)
  175. {
  176. /*
  177. * The generic MSI code returns with the interrupt disabled on the
  178. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  179. * at that level, so we do it here by hand.
  180. */
  181. if (irq_to_desc(virq)->msi_desc)
  182. unmask_msi_irq(virq);
  183. /* unmask it */
  184. xics_unmask_irq(virq);
  185. return 0;
  186. }
  187. static void xics_mask_real_irq(unsigned int irq)
  188. {
  189. int call_status;
  190. if (irq == XICS_IPI)
  191. return;
  192. call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
  193. if (call_status != 0) {
  194. printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n",
  195. __func__, irq, call_status);
  196. return;
  197. }
  198. /* Have to set XIVE to 0xff to be able to remove a slot */
  199. call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq,
  200. default_server, 0xff);
  201. if (call_status != 0) {
  202. printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n",
  203. __func__, irq, call_status);
  204. return;
  205. }
  206. }
  207. static void xics_mask_irq(unsigned int virq)
  208. {
  209. unsigned int irq;
  210. pr_devel("xics: mask virq %d\n", virq);
  211. irq = (unsigned int)irq_map[virq].hwirq;
  212. if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
  213. return;
  214. xics_mask_real_irq(irq);
  215. }
  216. static void xics_mask_unknown_vec(unsigned int vec)
  217. {
  218. printk(KERN_ERR "Interrupt %u (real) is invalid, disabling it.\n", vec);
  219. xics_mask_real_irq(vec);
  220. }
  221. static inline unsigned int xics_xirr_vector(unsigned int xirr)
  222. {
  223. /*
  224. * The top byte is the old cppr, to be restored on EOI.
  225. * The remaining 24 bits are the vector.
  226. */
  227. return xirr & 0x00ffffff;
  228. }
  229. static unsigned int xics_get_irq_direct(void)
  230. {
  231. unsigned int xirr = direct_xirr_info_get();
  232. unsigned int vec = xics_xirr_vector(xirr);
  233. unsigned int irq;
  234. if (vec == XICS_IRQ_SPURIOUS)
  235. return NO_IRQ;
  236. irq = irq_radix_revmap_lookup(xics_host, vec);
  237. if (likely(irq != NO_IRQ))
  238. return irq;
  239. /* We don't have a linux mapping, so have rtas mask it. */
  240. xics_mask_unknown_vec(vec);
  241. /* We might learn about it later, so EOI it */
  242. direct_xirr_info_set(xirr);
  243. return NO_IRQ;
  244. }
  245. static unsigned int xics_get_irq_lpar(void)
  246. {
  247. unsigned int xirr = lpar_xirr_info_get();
  248. unsigned int vec = xics_xirr_vector(xirr);
  249. unsigned int irq;
  250. if (vec == XICS_IRQ_SPURIOUS)
  251. return NO_IRQ;
  252. irq = irq_radix_revmap_lookup(xics_host, vec);
  253. if (likely(irq != NO_IRQ))
  254. return irq;
  255. /* We don't have a linux mapping, so have RTAS mask it. */
  256. xics_mask_unknown_vec(vec);
  257. /* We might learn about it later, so EOI it */
  258. lpar_xirr_info_set(xirr);
  259. return NO_IRQ;
  260. }
  261. static void xics_eoi_direct(unsigned int virq)
  262. {
  263. unsigned int irq = (unsigned int)irq_map[virq].hwirq;
  264. iosync();
  265. direct_xirr_info_set((0xff << 24) | irq);
  266. }
  267. static void xics_eoi_lpar(unsigned int virq)
  268. {
  269. unsigned int irq = (unsigned int)irq_map[virq].hwirq;
  270. iosync();
  271. lpar_xirr_info_set((0xff << 24) | irq);
  272. }
  273. static int xics_set_affinity(unsigned int virq, const struct cpumask *cpumask)
  274. {
  275. unsigned int irq;
  276. int status;
  277. int xics_status[2];
  278. int irq_server;
  279. irq = (unsigned int)irq_map[virq].hwirq;
  280. if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
  281. return -1;
  282. status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
  283. if (status) {
  284. printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n",
  285. __func__, irq, status);
  286. return -1;
  287. }
  288. /*
  289. * For the moment only implement delivery to all cpus or one cpu.
  290. * Get current irq_server for the given irq
  291. */
  292. irq_server = get_irq_server(virq, 1);
  293. if (irq_server == -1) {
  294. char cpulist[128];
  295. cpumask_scnprintf(cpulist, sizeof(cpulist), cpumask);
  296. printk(KERN_WARNING
  297. "%s: No online cpus in the mask %s for irq %d\n",
  298. __func__, cpulist, virq);
  299. return -1;
  300. }
  301. status = rtas_call(ibm_set_xive, 3, 1, NULL,
  302. irq, irq_server, xics_status[1]);
  303. if (status) {
  304. printk(KERN_ERR "%s: ibm,set-xive irq=%u returns %d\n",
  305. __func__, irq, status);
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. static struct irq_chip xics_pic_direct = {
  311. .typename = " XICS ",
  312. .startup = xics_startup,
  313. .mask = xics_mask_irq,
  314. .unmask = xics_unmask_irq,
  315. .eoi = xics_eoi_direct,
  316. .set_affinity = xics_set_affinity
  317. };
  318. static struct irq_chip xics_pic_lpar = {
  319. .typename = " XICS ",
  320. .startup = xics_startup,
  321. .mask = xics_mask_irq,
  322. .unmask = xics_unmask_irq,
  323. .eoi = xics_eoi_lpar,
  324. .set_affinity = xics_set_affinity
  325. };
  326. /* Interface to arch irq controller subsystem layer */
  327. /* Points to the irq_chip we're actually using */
  328. static struct irq_chip *xics_irq_chip;
  329. static int xics_host_match(struct irq_host *h, struct device_node *node)
  330. {
  331. /* IBM machines have interrupt parents of various funky types for things
  332. * like vdevices, events, etc... The trick we use here is to match
  333. * everything here except the legacy 8259 which is compatible "chrp,iic"
  334. */
  335. return !of_device_is_compatible(node, "chrp,iic");
  336. }
  337. static int xics_host_map(struct irq_host *h, unsigned int virq,
  338. irq_hw_number_t hw)
  339. {
  340. pr_devel("xics: map virq %d, hwirq 0x%lx\n", virq, hw);
  341. /* Insert the interrupt mapping into the radix tree for fast lookup */
  342. irq_radix_revmap_insert(xics_host, virq, hw);
  343. get_irq_desc(virq)->status |= IRQ_LEVEL;
  344. set_irq_chip_and_handler(virq, xics_irq_chip, handle_fasteoi_irq);
  345. return 0;
  346. }
  347. static int xics_host_xlate(struct irq_host *h, struct device_node *ct,
  348. u32 *intspec, unsigned int intsize,
  349. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  350. {
  351. /* Current xics implementation translates everything
  352. * to level. It is not technically right for MSIs but this
  353. * is irrelevant at this point. We might get smarter in the future
  354. */
  355. *out_hwirq = intspec[0];
  356. *out_flags = IRQ_TYPE_LEVEL_LOW;
  357. return 0;
  358. }
  359. static struct irq_host_ops xics_host_ops = {
  360. .match = xics_host_match,
  361. .map = xics_host_map,
  362. .xlate = xics_host_xlate,
  363. };
  364. static void __init xics_init_host(void)
  365. {
  366. if (firmware_has_feature(FW_FEATURE_LPAR))
  367. xics_irq_chip = &xics_pic_lpar;
  368. else
  369. xics_irq_chip = &xics_pic_direct;
  370. xics_host = irq_alloc_host(NULL, IRQ_HOST_MAP_TREE, 0, &xics_host_ops,
  371. XICS_IRQ_SPURIOUS);
  372. BUG_ON(xics_host == NULL);
  373. irq_set_default_host(xics_host);
  374. }
  375. /* Inter-processor interrupt support */
  376. #ifdef CONFIG_SMP
  377. /*
  378. * XICS only has a single IPI, so encode the messages per CPU
  379. */
  380. struct xics_ipi_struct {
  381. unsigned long value;
  382. } ____cacheline_aligned;
  383. static struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
  384. static inline void smp_xics_do_message(int cpu, int msg)
  385. {
  386. set_bit(msg, &xics_ipi_message[cpu].value);
  387. mb();
  388. if (firmware_has_feature(FW_FEATURE_LPAR))
  389. lpar_qirr_info(cpu, IPI_PRIORITY);
  390. else
  391. direct_qirr_info(cpu, IPI_PRIORITY);
  392. }
  393. void smp_xics_message_pass(int target, int msg)
  394. {
  395. unsigned int i;
  396. if (target < NR_CPUS) {
  397. smp_xics_do_message(target, msg);
  398. } else {
  399. for_each_online_cpu(i) {
  400. if (target == MSG_ALL_BUT_SELF
  401. && i == smp_processor_id())
  402. continue;
  403. smp_xics_do_message(i, msg);
  404. }
  405. }
  406. }
  407. static irqreturn_t xics_ipi_dispatch(int cpu)
  408. {
  409. WARN_ON(cpu_is_offline(cpu));
  410. mb(); /* order mmio clearing qirr */
  411. while (xics_ipi_message[cpu].value) {
  412. if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
  413. &xics_ipi_message[cpu].value)) {
  414. smp_message_recv(PPC_MSG_CALL_FUNCTION);
  415. }
  416. if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
  417. &xics_ipi_message[cpu].value)) {
  418. smp_message_recv(PPC_MSG_RESCHEDULE);
  419. }
  420. if (test_and_clear_bit(PPC_MSG_CALL_FUNC_SINGLE,
  421. &xics_ipi_message[cpu].value)) {
  422. smp_message_recv(PPC_MSG_CALL_FUNC_SINGLE);
  423. }
  424. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  425. if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
  426. &xics_ipi_message[cpu].value)) {
  427. smp_message_recv(PPC_MSG_DEBUGGER_BREAK);
  428. }
  429. #endif
  430. }
  431. return IRQ_HANDLED;
  432. }
  433. static irqreturn_t xics_ipi_action_direct(int irq, void *dev_id)
  434. {
  435. int cpu = smp_processor_id();
  436. direct_qirr_info(cpu, 0xff);
  437. return xics_ipi_dispatch(cpu);
  438. }
  439. static irqreturn_t xics_ipi_action_lpar(int irq, void *dev_id)
  440. {
  441. int cpu = smp_processor_id();
  442. lpar_qirr_info(cpu, 0xff);
  443. return xics_ipi_dispatch(cpu);
  444. }
  445. static void xics_request_ipi(void)
  446. {
  447. unsigned int ipi;
  448. int rc;
  449. ipi = irq_create_mapping(xics_host, XICS_IPI);
  450. BUG_ON(ipi == NO_IRQ);
  451. /*
  452. * IPIs are marked IRQF_DISABLED as they must run with irqs
  453. * disabled
  454. */
  455. set_irq_handler(ipi, handle_percpu_irq);
  456. if (firmware_has_feature(FW_FEATURE_LPAR))
  457. rc = request_irq(ipi, xics_ipi_action_lpar,
  458. IRQF_DISABLED|IRQF_PERCPU, "IPI", NULL);
  459. else
  460. rc = request_irq(ipi, xics_ipi_action_direct,
  461. IRQF_DISABLED|IRQF_PERCPU, "IPI", NULL);
  462. BUG_ON(rc);
  463. }
  464. int __init smp_xics_probe(void)
  465. {
  466. xics_request_ipi();
  467. return cpus_weight(cpu_possible_map);
  468. }
  469. #endif /* CONFIG_SMP */
  470. /* Initialization */
  471. static void xics_update_irq_servers(void)
  472. {
  473. int i, j;
  474. struct device_node *np;
  475. u32 ilen;
  476. const u32 *ireg;
  477. u32 hcpuid;
  478. /* Find the server numbers for the boot cpu. */
  479. np = of_get_cpu_node(boot_cpuid, NULL);
  480. BUG_ON(!np);
  481. ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
  482. if (!ireg) {
  483. of_node_put(np);
  484. return;
  485. }
  486. i = ilen / sizeof(int);
  487. hcpuid = get_hard_smp_processor_id(boot_cpuid);
  488. /* Global interrupt distribution server is specified in the last
  489. * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
  490. * entry fom this property for current boot cpu id and use it as
  491. * default distribution server
  492. */
  493. for (j = 0; j < i; j += 2) {
  494. if (ireg[j] == hcpuid) {
  495. default_server = hcpuid;
  496. default_distrib_server = ireg[j+1];
  497. }
  498. }
  499. of_node_put(np);
  500. }
  501. static void __init xics_map_one_cpu(int hw_id, unsigned long addr,
  502. unsigned long size)
  503. {
  504. int i;
  505. /* This may look gross but it's good enough for now, we don't quite
  506. * have a hard -> linux processor id matching.
  507. */
  508. for_each_possible_cpu(i) {
  509. if (!cpu_present(i))
  510. continue;
  511. if (hw_id == get_hard_smp_processor_id(i)) {
  512. xics_per_cpu[i] = ioremap(addr, size);
  513. return;
  514. }
  515. }
  516. }
  517. static void __init xics_init_one_node(struct device_node *np,
  518. unsigned int *indx)
  519. {
  520. unsigned int ilen;
  521. const u32 *ireg;
  522. /* This code does the theorically broken assumption that the interrupt
  523. * server numbers are the same as the hard CPU numbers.
  524. * This happens to be the case so far but we are playing with fire...
  525. * should be fixed one of these days. -BenH.
  526. */
  527. ireg = of_get_property(np, "ibm,interrupt-server-ranges", NULL);
  528. /* Do that ever happen ? we'll know soon enough... but even good'old
  529. * f80 does have that property ..
  530. */
  531. WARN_ON(ireg == NULL);
  532. if (ireg) {
  533. /*
  534. * set node starting index for this node
  535. */
  536. *indx = *ireg;
  537. }
  538. ireg = of_get_property(np, "reg", &ilen);
  539. if (!ireg)
  540. panic("xics_init_IRQ: can't find interrupt reg property");
  541. while (ilen >= (4 * sizeof(u32))) {
  542. unsigned long addr, size;
  543. /* XXX Use proper OF parsing code here !!! */
  544. addr = (unsigned long)*ireg++ << 32;
  545. ilen -= sizeof(u32);
  546. addr |= *ireg++;
  547. ilen -= sizeof(u32);
  548. size = (unsigned long)*ireg++ << 32;
  549. ilen -= sizeof(u32);
  550. size |= *ireg++;
  551. ilen -= sizeof(u32);
  552. xics_map_one_cpu(*indx, addr, size);
  553. (*indx)++;
  554. }
  555. }
  556. void __init xics_init_IRQ(void)
  557. {
  558. struct device_node *np;
  559. u32 indx = 0;
  560. int found = 0;
  561. const u32 *isize;
  562. ppc64_boot_msg(0x20, "XICS Init");
  563. ibm_get_xive = rtas_token("ibm,get-xive");
  564. ibm_set_xive = rtas_token("ibm,set-xive");
  565. ibm_int_on = rtas_token("ibm,int-on");
  566. ibm_int_off = rtas_token("ibm,int-off");
  567. for_each_node_by_type(np, "PowerPC-External-Interrupt-Presentation") {
  568. found = 1;
  569. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  570. of_node_put(np);
  571. break;
  572. }
  573. xics_init_one_node(np, &indx);
  574. }
  575. if (found == 0)
  576. return;
  577. /* get the bit size of server numbers */
  578. found = 0;
  579. for_each_compatible_node(np, NULL, "ibm,ppc-xics") {
  580. isize = of_get_property(np, "ibm,interrupt-server#-size", NULL);
  581. if (!isize)
  582. continue;
  583. if (!found) {
  584. interrupt_server_size = *isize;
  585. found = 1;
  586. } else if (*isize != interrupt_server_size) {
  587. printk(KERN_WARNING "XICS: "
  588. "mismatched ibm,interrupt-server#-size\n");
  589. interrupt_server_size = max(*isize,
  590. interrupt_server_size);
  591. }
  592. }
  593. xics_update_irq_servers();
  594. xics_init_host();
  595. if (firmware_has_feature(FW_FEATURE_LPAR))
  596. ppc_md.get_irq = xics_get_irq_lpar;
  597. else
  598. ppc_md.get_irq = xics_get_irq_direct;
  599. xics_setup_cpu();
  600. ppc64_boot_msg(0x21, "XICS Done");
  601. }
  602. /* Cpu startup, shutdown, and hotplug */
  603. static void xics_set_cpu_priority(unsigned char cppr)
  604. {
  605. if (firmware_has_feature(FW_FEATURE_LPAR))
  606. lpar_cppr_info(cppr);
  607. else
  608. direct_cppr_info(cppr);
  609. iosync();
  610. }
  611. /* Have the calling processor join or leave the specified global queue */
  612. static void xics_set_cpu_giq(unsigned int gserver, unsigned int join)
  613. {
  614. int index;
  615. int status;
  616. if (!rtas_indicator_present(GLOBAL_INTERRUPT_QUEUE, NULL))
  617. return;
  618. index = (1UL << interrupt_server_size) - 1 - gserver;
  619. status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE, index, join);
  620. WARN(status < 0, "set-indicator(%d, %d, %u) returned %d\n",
  621. GLOBAL_INTERRUPT_QUEUE, index, join, status);
  622. }
  623. void xics_setup_cpu(void)
  624. {
  625. xics_set_cpu_priority(0xff);
  626. xics_set_cpu_giq(default_distrib_server, 1);
  627. }
  628. void xics_teardown_cpu(void)
  629. {
  630. int cpu = smp_processor_id();
  631. xics_set_cpu_priority(0);
  632. /* Clear any pending IPI request */
  633. if (firmware_has_feature(FW_FEATURE_LPAR))
  634. lpar_qirr_info(cpu, 0xff);
  635. else
  636. direct_qirr_info(cpu, 0xff);
  637. }
  638. void xics_kexec_teardown_cpu(int secondary)
  639. {
  640. xics_teardown_cpu();
  641. /*
  642. * we take the ipi irq but and never return so we
  643. * need to EOI the IPI, but want to leave our priority 0
  644. *
  645. * should we check all the other interrupts too?
  646. * should we be flagging idle loop instead?
  647. * or creating some task to be scheduled?
  648. */
  649. if (firmware_has_feature(FW_FEATURE_LPAR))
  650. lpar_xirr_info_set((0x00 << 24) | XICS_IPI);
  651. else
  652. direct_xirr_info_set((0x00 << 24) | XICS_IPI);
  653. /*
  654. * Some machines need to have at least one cpu in the GIQ,
  655. * so leave the master cpu in the group.
  656. */
  657. if (secondary)
  658. xics_set_cpu_giq(default_distrib_server, 0);
  659. }
  660. #ifdef CONFIG_HOTPLUG_CPU
  661. /* Interrupts are disabled. */
  662. void xics_migrate_irqs_away(void)
  663. {
  664. int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();
  665. unsigned int irq, virq;
  666. /* If we used to be the default server, move to the new "boot_cpuid" */
  667. if (hw_cpu == default_server)
  668. xics_update_irq_servers();
  669. /* Reject any interrupt that was queued to us... */
  670. xics_set_cpu_priority(0);
  671. /* Remove ourselves from the global interrupt queue */
  672. xics_set_cpu_giq(default_distrib_server, 0);
  673. /* Allow IPIs again... */
  674. xics_set_cpu_priority(DEFAULT_PRIORITY);
  675. for_each_irq(virq) {
  676. struct irq_desc *desc;
  677. int xics_status[2];
  678. int status;
  679. unsigned long flags;
  680. /* We cant set affinity on ISA interrupts */
  681. if (virq < NUM_ISA_INTERRUPTS)
  682. continue;
  683. if (irq_map[virq].host != xics_host)
  684. continue;
  685. irq = (unsigned int)irq_map[virq].hwirq;
  686. /* We need to get IPIs still. */
  687. if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
  688. continue;
  689. desc = get_irq_desc(virq);
  690. /* We only need to migrate enabled IRQS */
  691. if (desc == NULL || desc->chip == NULL
  692. || desc->action == NULL
  693. || desc->chip->set_affinity == NULL)
  694. continue;
  695. spin_lock_irqsave(&desc->lock, flags);
  696. status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
  697. if (status) {
  698. printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n",
  699. __func__, irq, status);
  700. goto unlock;
  701. }
  702. /*
  703. * We only support delivery to all cpus or to one cpu.
  704. * The irq has to be migrated only in the single cpu
  705. * case.
  706. */
  707. if (xics_status[0] != hw_cpu)
  708. goto unlock;
  709. printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
  710. virq, cpu);
  711. /* Reset affinity to all cpus */
  712. cpumask_setall(irq_desc[virq].affinity);
  713. desc->chip->set_affinity(virq, cpu_all_mask);
  714. unlock:
  715. spin_unlock_irqrestore(&desc->lock, flags);
  716. }
  717. }
  718. #endif