xics.c 21 KB

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