xics.c 21 KB

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