xics.c 22 KB

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