irq_64.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /* irq.c: UltraSparc IRQ handling/init/registry.
  2. *
  3. * Copyright (C) 1997, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/linkage.h>
  9. #include <linux/ptrace.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/signal.h>
  13. #include <linux/mm.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/random.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/ftrace.h>
  22. #include <linux/irq.h>
  23. #include <linux/kmemleak.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/processor.h>
  26. #include <linux/atomic.h>
  27. #include <asm/system.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/iommu.h>
  31. #include <asm/upa.h>
  32. #include <asm/oplib.h>
  33. #include <asm/prom.h>
  34. #include <asm/timer.h>
  35. #include <asm/smp.h>
  36. #include <asm/starfire.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/cache.h>
  39. #include <asm/cpudata.h>
  40. #include <asm/auxio.h>
  41. #include <asm/head.h>
  42. #include <asm/hypervisor.h>
  43. #include <asm/cacheflush.h>
  44. #include "entry.h"
  45. #include "cpumap.h"
  46. #include "kstack.h"
  47. #define NUM_IVECS (IMAP_INR + 1)
  48. struct ino_bucket *ivector_table;
  49. unsigned long ivector_table_pa;
  50. /* On several sun4u processors, it is illegal to mix bypass and
  51. * non-bypass accesses. Therefore we access all INO buckets
  52. * using bypass accesses only.
  53. */
  54. static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
  55. {
  56. unsigned long ret;
  57. __asm__ __volatile__("ldxa [%1] %2, %0"
  58. : "=&r" (ret)
  59. : "r" (bucket_pa +
  60. offsetof(struct ino_bucket,
  61. __irq_chain_pa)),
  62. "i" (ASI_PHYS_USE_EC));
  63. return ret;
  64. }
  65. static void bucket_clear_chain_pa(unsigned long bucket_pa)
  66. {
  67. __asm__ __volatile__("stxa %%g0, [%0] %1"
  68. : /* no outputs */
  69. : "r" (bucket_pa +
  70. offsetof(struct ino_bucket,
  71. __irq_chain_pa)),
  72. "i" (ASI_PHYS_USE_EC));
  73. }
  74. static unsigned int bucket_get_irq(unsigned long bucket_pa)
  75. {
  76. unsigned int ret;
  77. __asm__ __volatile__("lduwa [%1] %2, %0"
  78. : "=&r" (ret)
  79. : "r" (bucket_pa +
  80. offsetof(struct ino_bucket,
  81. __irq)),
  82. "i" (ASI_PHYS_USE_EC));
  83. return ret;
  84. }
  85. static void bucket_set_irq(unsigned long bucket_pa, unsigned int irq)
  86. {
  87. __asm__ __volatile__("stwa %0, [%1] %2"
  88. : /* no outputs */
  89. : "r" (irq),
  90. "r" (bucket_pa +
  91. offsetof(struct ino_bucket,
  92. __irq)),
  93. "i" (ASI_PHYS_USE_EC));
  94. }
  95. #define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
  96. static struct {
  97. unsigned int dev_handle;
  98. unsigned int dev_ino;
  99. unsigned int in_use;
  100. } irq_table[NR_IRQS];
  101. static DEFINE_SPINLOCK(irq_alloc_lock);
  102. unsigned char irq_alloc(unsigned int dev_handle, unsigned int dev_ino)
  103. {
  104. unsigned long flags;
  105. unsigned char ent;
  106. BUILD_BUG_ON(NR_IRQS >= 256);
  107. spin_lock_irqsave(&irq_alloc_lock, flags);
  108. for (ent = 1; ent < NR_IRQS; ent++) {
  109. if (!irq_table[ent].in_use)
  110. break;
  111. }
  112. if (ent >= NR_IRQS) {
  113. printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
  114. ent = 0;
  115. } else {
  116. irq_table[ent].dev_handle = dev_handle;
  117. irq_table[ent].dev_ino = dev_ino;
  118. irq_table[ent].in_use = 1;
  119. }
  120. spin_unlock_irqrestore(&irq_alloc_lock, flags);
  121. return ent;
  122. }
  123. #ifdef CONFIG_PCI_MSI
  124. void irq_free(unsigned int irq)
  125. {
  126. unsigned long flags;
  127. if (irq >= NR_IRQS)
  128. return;
  129. spin_lock_irqsave(&irq_alloc_lock, flags);
  130. irq_table[irq].in_use = 0;
  131. spin_unlock_irqrestore(&irq_alloc_lock, flags);
  132. }
  133. #endif
  134. /*
  135. * /proc/interrupts printing:
  136. */
  137. int arch_show_interrupts(struct seq_file *p, int prec)
  138. {
  139. int j;
  140. seq_printf(p, "NMI: ");
  141. for_each_online_cpu(j)
  142. seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
  143. seq_printf(p, " Non-maskable interrupts\n");
  144. return 0;
  145. }
  146. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  147. {
  148. unsigned int tid;
  149. if (this_is_starfire) {
  150. tid = starfire_translate(imap, cpuid);
  151. tid <<= IMAP_TID_SHIFT;
  152. tid &= IMAP_TID_UPA;
  153. } else {
  154. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  155. unsigned long ver;
  156. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  157. if ((ver >> 32UL) == __JALAPENO_ID ||
  158. (ver >> 32UL) == __SERRANO_ID) {
  159. tid = cpuid << IMAP_TID_SHIFT;
  160. tid &= IMAP_TID_JBUS;
  161. } else {
  162. unsigned int a = cpuid & 0x1f;
  163. unsigned int n = (cpuid >> 5) & 0x1f;
  164. tid = ((a << IMAP_AID_SHIFT) |
  165. (n << IMAP_NID_SHIFT));
  166. tid &= (IMAP_AID_SAFARI |
  167. IMAP_NID_SAFARI);
  168. }
  169. } else {
  170. tid = cpuid << IMAP_TID_SHIFT;
  171. tid &= IMAP_TID_UPA;
  172. }
  173. }
  174. return tid;
  175. }
  176. struct irq_handler_data {
  177. unsigned long iclr;
  178. unsigned long imap;
  179. void (*pre_handler)(unsigned int, void *, void *);
  180. void *arg1;
  181. void *arg2;
  182. };
  183. #ifdef CONFIG_SMP
  184. static int irq_choose_cpu(unsigned int irq, const struct cpumask *affinity)
  185. {
  186. cpumask_t mask;
  187. int cpuid;
  188. cpumask_copy(&mask, affinity);
  189. if (cpumask_equal(&mask, cpu_online_mask)) {
  190. cpuid = map_to_cpu(irq);
  191. } else {
  192. cpumask_t tmp;
  193. cpumask_and(&tmp, cpu_online_mask, &mask);
  194. cpuid = cpumask_empty(&tmp) ? map_to_cpu(irq) : cpumask_first(&tmp);
  195. }
  196. return cpuid;
  197. }
  198. #else
  199. #define irq_choose_cpu(irq, affinity) \
  200. real_hard_smp_processor_id()
  201. #endif
  202. static void sun4u_irq_enable(struct irq_data *data)
  203. {
  204. struct irq_handler_data *handler_data = data->handler_data;
  205. if (likely(handler_data)) {
  206. unsigned long cpuid, imap, val;
  207. unsigned int tid;
  208. cpuid = irq_choose_cpu(data->irq, data->affinity);
  209. imap = handler_data->imap;
  210. tid = sun4u_compute_tid(imap, cpuid);
  211. val = upa_readq(imap);
  212. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  213. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  214. val |= tid | IMAP_VALID;
  215. upa_writeq(val, imap);
  216. upa_writeq(ICLR_IDLE, handler_data->iclr);
  217. }
  218. }
  219. static int sun4u_set_affinity(struct irq_data *data,
  220. const struct cpumask *mask, bool force)
  221. {
  222. struct irq_handler_data *handler_data = data->handler_data;
  223. if (likely(handler_data)) {
  224. unsigned long cpuid, imap, val;
  225. unsigned int tid;
  226. cpuid = irq_choose_cpu(data->irq, mask);
  227. imap = handler_data->imap;
  228. tid = sun4u_compute_tid(imap, cpuid);
  229. val = upa_readq(imap);
  230. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  231. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  232. val |= tid | IMAP_VALID;
  233. upa_writeq(val, imap);
  234. upa_writeq(ICLR_IDLE, handler_data->iclr);
  235. }
  236. return 0;
  237. }
  238. /* Don't do anything. The desc->status check for IRQ_DISABLED in
  239. * handler_irq() will skip the handler call and that will leave the
  240. * interrupt in the sent state. The next ->enable() call will hit the
  241. * ICLR register to reset the state machine.
  242. *
  243. * This scheme is necessary, instead of clearing the Valid bit in the
  244. * IMAP register, to handle the case of IMAP registers being shared by
  245. * multiple INOs (and thus ICLR registers). Since we use a different
  246. * virtual IRQ for each shared IMAP instance, the generic code thinks
  247. * there is only one user so it prematurely calls ->disable() on
  248. * free_irq().
  249. *
  250. * We have to provide an explicit ->disable() method instead of using
  251. * NULL to get the default. The reason is that if the generic code
  252. * sees that, it also hooks up a default ->shutdown method which
  253. * invokes ->mask() which we do not want. See irq_chip_set_defaults().
  254. */
  255. static void sun4u_irq_disable(struct irq_data *data)
  256. {
  257. }
  258. static void sun4u_irq_eoi(struct irq_data *data)
  259. {
  260. struct irq_handler_data *handler_data = data->handler_data;
  261. if (likely(handler_data))
  262. upa_writeq(ICLR_IDLE, handler_data->iclr);
  263. }
  264. static void sun4v_irq_enable(struct irq_data *data)
  265. {
  266. unsigned int ino = irq_table[data->irq].dev_ino;
  267. unsigned long cpuid = irq_choose_cpu(data->irq, data->affinity);
  268. int err;
  269. err = sun4v_intr_settarget(ino, cpuid);
  270. if (err != HV_EOK)
  271. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  272. "err(%d)\n", ino, cpuid, err);
  273. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  274. if (err != HV_EOK)
  275. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  276. "err(%d)\n", ino, err);
  277. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  278. if (err != HV_EOK)
  279. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  280. ino, err);
  281. }
  282. static int sun4v_set_affinity(struct irq_data *data,
  283. const struct cpumask *mask, bool force)
  284. {
  285. unsigned int ino = irq_table[data->irq].dev_ino;
  286. unsigned long cpuid = irq_choose_cpu(data->irq, mask);
  287. int err;
  288. err = sun4v_intr_settarget(ino, cpuid);
  289. if (err != HV_EOK)
  290. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  291. "err(%d)\n", ino, cpuid, err);
  292. return 0;
  293. }
  294. static void sun4v_irq_disable(struct irq_data *data)
  295. {
  296. unsigned int ino = irq_table[data->irq].dev_ino;
  297. int err;
  298. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  299. if (err != HV_EOK)
  300. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  301. "err(%d)\n", ino, err);
  302. }
  303. static void sun4v_irq_eoi(struct irq_data *data)
  304. {
  305. unsigned int ino = irq_table[data->irq].dev_ino;
  306. int err;
  307. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  308. if (err != HV_EOK)
  309. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  310. "err(%d)\n", ino, err);
  311. }
  312. static void sun4v_virq_enable(struct irq_data *data)
  313. {
  314. unsigned long cpuid, dev_handle, dev_ino;
  315. int err;
  316. cpuid = irq_choose_cpu(data->irq, data->affinity);
  317. dev_handle = irq_table[data->irq].dev_handle;
  318. dev_ino = irq_table[data->irq].dev_ino;
  319. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  320. if (err != HV_EOK)
  321. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  322. "err(%d)\n",
  323. dev_handle, dev_ino, cpuid, err);
  324. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  325. HV_INTR_STATE_IDLE);
  326. if (err != HV_EOK)
  327. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  328. "HV_INTR_STATE_IDLE): err(%d)\n",
  329. dev_handle, dev_ino, err);
  330. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  331. HV_INTR_ENABLED);
  332. if (err != HV_EOK)
  333. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  334. "HV_INTR_ENABLED): err(%d)\n",
  335. dev_handle, dev_ino, err);
  336. }
  337. static int sun4v_virt_set_affinity(struct irq_data *data,
  338. const struct cpumask *mask, bool force)
  339. {
  340. unsigned long cpuid, dev_handle, dev_ino;
  341. int err;
  342. cpuid = irq_choose_cpu(data->irq, mask);
  343. dev_handle = irq_table[data->irq].dev_handle;
  344. dev_ino = irq_table[data->irq].dev_ino;
  345. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  346. if (err != HV_EOK)
  347. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  348. "err(%d)\n",
  349. dev_handle, dev_ino, cpuid, err);
  350. return 0;
  351. }
  352. static void sun4v_virq_disable(struct irq_data *data)
  353. {
  354. unsigned long dev_handle, dev_ino;
  355. int err;
  356. dev_handle = irq_table[data->irq].dev_handle;
  357. dev_ino = irq_table[data->irq].dev_ino;
  358. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  359. HV_INTR_DISABLED);
  360. if (err != HV_EOK)
  361. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  362. "HV_INTR_DISABLED): err(%d)\n",
  363. dev_handle, dev_ino, err);
  364. }
  365. static void sun4v_virq_eoi(struct irq_data *data)
  366. {
  367. unsigned long dev_handle, dev_ino;
  368. int err;
  369. dev_handle = irq_table[data->irq].dev_handle;
  370. dev_ino = irq_table[data->irq].dev_ino;
  371. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  372. HV_INTR_STATE_IDLE);
  373. if (err != HV_EOK)
  374. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  375. "HV_INTR_STATE_IDLE): err(%d)\n",
  376. dev_handle, dev_ino, err);
  377. }
  378. static struct irq_chip sun4u_irq = {
  379. .name = "sun4u",
  380. .irq_enable = sun4u_irq_enable,
  381. .irq_disable = sun4u_irq_disable,
  382. .irq_eoi = sun4u_irq_eoi,
  383. .irq_set_affinity = sun4u_set_affinity,
  384. .flags = IRQCHIP_EOI_IF_HANDLED,
  385. };
  386. static struct irq_chip sun4v_irq = {
  387. .name = "sun4v",
  388. .irq_enable = sun4v_irq_enable,
  389. .irq_disable = sun4v_irq_disable,
  390. .irq_eoi = sun4v_irq_eoi,
  391. .irq_set_affinity = sun4v_set_affinity,
  392. .flags = IRQCHIP_EOI_IF_HANDLED,
  393. };
  394. static struct irq_chip sun4v_virq = {
  395. .name = "vsun4v",
  396. .irq_enable = sun4v_virq_enable,
  397. .irq_disable = sun4v_virq_disable,
  398. .irq_eoi = sun4v_virq_eoi,
  399. .irq_set_affinity = sun4v_virt_set_affinity,
  400. .flags = IRQCHIP_EOI_IF_HANDLED,
  401. };
  402. static void pre_flow_handler(struct irq_data *d)
  403. {
  404. struct irq_handler_data *handler_data = irq_data_get_irq_handler_data(d);
  405. unsigned int ino = irq_table[d->irq].dev_ino;
  406. handler_data->pre_handler(ino, handler_data->arg1, handler_data->arg2);
  407. }
  408. void irq_install_pre_handler(int irq,
  409. void (*func)(unsigned int, void *, void *),
  410. void *arg1, void *arg2)
  411. {
  412. struct irq_handler_data *handler_data = irq_get_handler_data(irq);
  413. handler_data->pre_handler = func;
  414. handler_data->arg1 = arg1;
  415. handler_data->arg2 = arg2;
  416. __irq_set_preflow_handler(irq, pre_flow_handler);
  417. }
  418. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  419. {
  420. struct ino_bucket *bucket;
  421. struct irq_handler_data *handler_data;
  422. unsigned int irq;
  423. int ino;
  424. BUG_ON(tlb_type == hypervisor);
  425. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  426. bucket = &ivector_table[ino];
  427. irq = bucket_get_irq(__pa(bucket));
  428. if (!irq) {
  429. irq = irq_alloc(0, ino);
  430. bucket_set_irq(__pa(bucket), irq);
  431. irq_set_chip_and_handler_name(irq, &sun4u_irq,
  432. handle_fasteoi_irq, "IVEC");
  433. }
  434. handler_data = irq_get_handler_data(irq);
  435. if (unlikely(handler_data))
  436. goto out;
  437. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  438. if (unlikely(!handler_data)) {
  439. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  440. prom_halt();
  441. }
  442. irq_set_handler_data(irq, handler_data);
  443. handler_data->imap = imap;
  444. handler_data->iclr = iclr;
  445. out:
  446. return irq;
  447. }
  448. static unsigned int sun4v_build_common(unsigned long sysino,
  449. struct irq_chip *chip)
  450. {
  451. struct ino_bucket *bucket;
  452. struct irq_handler_data *handler_data;
  453. unsigned int irq;
  454. BUG_ON(tlb_type != hypervisor);
  455. bucket = &ivector_table[sysino];
  456. irq = bucket_get_irq(__pa(bucket));
  457. if (!irq) {
  458. irq = irq_alloc(0, sysino);
  459. bucket_set_irq(__pa(bucket), irq);
  460. irq_set_chip_and_handler_name(irq, chip, handle_fasteoi_irq,
  461. "IVEC");
  462. }
  463. handler_data = irq_get_handler_data(irq);
  464. if (unlikely(handler_data))
  465. goto out;
  466. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  467. if (unlikely(!handler_data)) {
  468. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  469. prom_halt();
  470. }
  471. irq_set_handler_data(irq, handler_data);
  472. /* Catch accidental accesses to these things. IMAP/ICLR handling
  473. * is done by hypervisor calls on sun4v platforms, not by direct
  474. * register accesses.
  475. */
  476. handler_data->imap = ~0UL;
  477. handler_data->iclr = ~0UL;
  478. out:
  479. return irq;
  480. }
  481. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  482. {
  483. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  484. return sun4v_build_common(sysino, &sun4v_irq);
  485. }
  486. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  487. {
  488. struct irq_handler_data *handler_data;
  489. unsigned long hv_err, cookie;
  490. struct ino_bucket *bucket;
  491. unsigned int irq;
  492. bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
  493. if (unlikely(!bucket))
  494. return 0;
  495. /* The only reference we store to the IRQ bucket is
  496. * by physical address which kmemleak can't see, tell
  497. * it that this object explicitly is not a leak and
  498. * should be scanned.
  499. */
  500. kmemleak_not_leak(bucket);
  501. __flush_dcache_range((unsigned long) bucket,
  502. ((unsigned long) bucket +
  503. sizeof(struct ino_bucket)));
  504. irq = irq_alloc(devhandle, devino);
  505. bucket_set_irq(__pa(bucket), irq);
  506. irq_set_chip_and_handler_name(irq, &sun4v_virq, handle_fasteoi_irq,
  507. "IVEC");
  508. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  509. if (unlikely(!handler_data))
  510. return 0;
  511. /* In order to make the LDC channel startup sequence easier,
  512. * especially wrt. locking, we do not let request_irq() enable
  513. * the interrupt.
  514. */
  515. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  516. irq_set_handler_data(irq, handler_data);
  517. /* Catch accidental accesses to these things. IMAP/ICLR handling
  518. * is done by hypervisor calls on sun4v platforms, not by direct
  519. * register accesses.
  520. */
  521. handler_data->imap = ~0UL;
  522. handler_data->iclr = ~0UL;
  523. cookie = ~__pa(bucket);
  524. hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
  525. if (hv_err) {
  526. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  527. "err=%lu\n", devhandle, devino, hv_err);
  528. prom_halt();
  529. }
  530. return irq;
  531. }
  532. void ack_bad_irq(unsigned int irq)
  533. {
  534. unsigned int ino = irq_table[irq].dev_ino;
  535. if (!ino)
  536. ino = 0xdeadbeef;
  537. printk(KERN_CRIT "Unexpected IRQ from ino[%x] irq[%u]\n",
  538. ino, irq);
  539. }
  540. void *hardirq_stack[NR_CPUS];
  541. void *softirq_stack[NR_CPUS];
  542. void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  543. {
  544. unsigned long pstate, bucket_pa;
  545. struct pt_regs *old_regs;
  546. void *orig_sp;
  547. clear_softint(1 << pil);
  548. old_regs = set_irq_regs(regs);
  549. irq_enter();
  550. /* Grab an atomic snapshot of the pending IVECs. */
  551. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  552. "wrpr %0, %3, %%pstate\n\t"
  553. "ldx [%2], %1\n\t"
  554. "stx %%g0, [%2]\n\t"
  555. "wrpr %0, 0x0, %%pstate\n\t"
  556. : "=&r" (pstate), "=&r" (bucket_pa)
  557. : "r" (irq_work_pa(smp_processor_id())),
  558. "i" (PSTATE_IE)
  559. : "memory");
  560. orig_sp = set_hardirq_stack();
  561. while (bucket_pa) {
  562. unsigned long next_pa;
  563. unsigned int irq;
  564. next_pa = bucket_get_chain_pa(bucket_pa);
  565. irq = bucket_get_irq(bucket_pa);
  566. bucket_clear_chain_pa(bucket_pa);
  567. generic_handle_irq(irq);
  568. bucket_pa = next_pa;
  569. }
  570. restore_hardirq_stack(orig_sp);
  571. irq_exit();
  572. set_irq_regs(old_regs);
  573. }
  574. void do_softirq(void)
  575. {
  576. unsigned long flags;
  577. if (in_interrupt())
  578. return;
  579. local_irq_save(flags);
  580. if (local_softirq_pending()) {
  581. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  582. sp += THREAD_SIZE - 192 - STACK_BIAS;
  583. __asm__ __volatile__("mov %%sp, %0\n\t"
  584. "mov %1, %%sp"
  585. : "=&r" (orig_sp)
  586. : "r" (sp));
  587. __do_softirq();
  588. __asm__ __volatile__("mov %0, %%sp"
  589. : : "r" (orig_sp));
  590. }
  591. local_irq_restore(flags);
  592. }
  593. #ifdef CONFIG_HOTPLUG_CPU
  594. void fixup_irqs(void)
  595. {
  596. unsigned int irq;
  597. for (irq = 0; irq < NR_IRQS; irq++) {
  598. struct irq_desc *desc = irq_to_desc(irq);
  599. struct irq_data *data = irq_desc_get_irq_data(desc);
  600. unsigned long flags;
  601. raw_spin_lock_irqsave(&desc->lock, flags);
  602. if (desc->action && !irqd_is_per_cpu(data)) {
  603. if (data->chip->irq_set_affinity)
  604. data->chip->irq_set_affinity(data,
  605. data->affinity,
  606. false);
  607. }
  608. raw_spin_unlock_irqrestore(&desc->lock, flags);
  609. }
  610. tick_ops->disable_irq();
  611. }
  612. #endif
  613. struct sun5_timer {
  614. u64 count0;
  615. u64 limit0;
  616. u64 count1;
  617. u64 limit1;
  618. };
  619. static struct sun5_timer *prom_timers;
  620. static u64 prom_limit0, prom_limit1;
  621. static void map_prom_timers(void)
  622. {
  623. struct device_node *dp;
  624. const unsigned int *addr;
  625. /* PROM timer node hangs out in the top level of device siblings... */
  626. dp = of_find_node_by_path("/");
  627. dp = dp->child;
  628. while (dp) {
  629. if (!strcmp(dp->name, "counter-timer"))
  630. break;
  631. dp = dp->sibling;
  632. }
  633. /* Assume if node is not present, PROM uses different tick mechanism
  634. * which we should not care about.
  635. */
  636. if (!dp) {
  637. prom_timers = (struct sun5_timer *) 0;
  638. return;
  639. }
  640. /* If PROM is really using this, it must be mapped by him. */
  641. addr = of_get_property(dp, "address", NULL);
  642. if (!addr) {
  643. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  644. prom_timers = (struct sun5_timer *) 0;
  645. return;
  646. }
  647. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  648. }
  649. static void kill_prom_timer(void)
  650. {
  651. if (!prom_timers)
  652. return;
  653. /* Save them away for later. */
  654. prom_limit0 = prom_timers->limit0;
  655. prom_limit1 = prom_timers->limit1;
  656. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  657. * We turn both off here just to be paranoid.
  658. */
  659. prom_timers->limit0 = 0;
  660. prom_timers->limit1 = 0;
  661. /* Wheee, eat the interrupt packet too... */
  662. __asm__ __volatile__(
  663. " mov 0x40, %%g2\n"
  664. " ldxa [%%g0] %0, %%g1\n"
  665. " ldxa [%%g2] %1, %%g1\n"
  666. " stxa %%g0, [%%g0] %0\n"
  667. " membar #Sync\n"
  668. : /* no outputs */
  669. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  670. : "g1", "g2");
  671. }
  672. void notrace init_irqwork_curcpu(void)
  673. {
  674. int cpu = hard_smp_processor_id();
  675. trap_block[cpu].irq_worklist_pa = 0UL;
  676. }
  677. /* Please be very careful with register_one_mondo() and
  678. * sun4v_register_mondo_queues().
  679. *
  680. * On SMP this gets invoked from the CPU trampoline before
  681. * the cpu has fully taken over the trap table from OBP,
  682. * and it's kernel stack + %g6 thread register state is
  683. * not fully cooked yet.
  684. *
  685. * Therefore you cannot make any OBP calls, not even prom_printf,
  686. * from these two routines.
  687. */
  688. static void __cpuinit notrace register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  689. {
  690. unsigned long num_entries = (qmask + 1) / 64;
  691. unsigned long status;
  692. status = sun4v_cpu_qconf(type, paddr, num_entries);
  693. if (status != HV_EOK) {
  694. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  695. "err %lu\n", type, paddr, num_entries, status);
  696. prom_halt();
  697. }
  698. }
  699. void __cpuinit notrace sun4v_register_mondo_queues(int this_cpu)
  700. {
  701. struct trap_per_cpu *tb = &trap_block[this_cpu];
  702. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  703. tb->cpu_mondo_qmask);
  704. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  705. tb->dev_mondo_qmask);
  706. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  707. tb->resum_qmask);
  708. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  709. tb->nonresum_qmask);
  710. }
  711. /* Each queue region must be a power of 2 multiple of 64 bytes in
  712. * size. The base real address must be aligned to the size of the
  713. * region. Thus, an 8KB queue must be 8KB aligned, for example.
  714. */
  715. static void __init alloc_one_queue(unsigned long *pa_ptr, unsigned long qmask)
  716. {
  717. unsigned long size = PAGE_ALIGN(qmask + 1);
  718. unsigned long order = get_order(size);
  719. unsigned long p;
  720. p = __get_free_pages(GFP_KERNEL, order);
  721. if (!p) {
  722. prom_printf("SUN4V: Error, cannot allocate queue.\n");
  723. prom_halt();
  724. }
  725. *pa_ptr = __pa(p);
  726. }
  727. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  728. {
  729. #ifdef CONFIG_SMP
  730. unsigned long page;
  731. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  732. page = get_zeroed_page(GFP_KERNEL);
  733. if (!page) {
  734. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  735. prom_halt();
  736. }
  737. tb->cpu_mondo_block_pa = __pa(page);
  738. tb->cpu_list_pa = __pa(page + 64);
  739. #endif
  740. }
  741. /* Allocate mondo and error queues for all possible cpus. */
  742. static void __init sun4v_init_mondo_queues(void)
  743. {
  744. int cpu;
  745. for_each_possible_cpu(cpu) {
  746. struct trap_per_cpu *tb = &trap_block[cpu];
  747. alloc_one_queue(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  748. alloc_one_queue(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  749. alloc_one_queue(&tb->resum_mondo_pa, tb->resum_qmask);
  750. alloc_one_queue(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  751. alloc_one_queue(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  752. alloc_one_queue(&tb->nonresum_kernel_buf_pa,
  753. tb->nonresum_qmask);
  754. }
  755. }
  756. static void __init init_send_mondo_info(void)
  757. {
  758. int cpu;
  759. for_each_possible_cpu(cpu) {
  760. struct trap_per_cpu *tb = &trap_block[cpu];
  761. init_cpu_send_mondo_info(tb);
  762. }
  763. }
  764. static struct irqaction timer_irq_action = {
  765. .name = "timer",
  766. };
  767. /* Only invoked on boot processor. */
  768. void __init init_IRQ(void)
  769. {
  770. unsigned long size;
  771. map_prom_timers();
  772. kill_prom_timer();
  773. size = sizeof(struct ino_bucket) * NUM_IVECS;
  774. ivector_table = kzalloc(size, GFP_KERNEL);
  775. if (!ivector_table) {
  776. prom_printf("Fatal error, cannot allocate ivector_table\n");
  777. prom_halt();
  778. }
  779. __flush_dcache_range((unsigned long) ivector_table,
  780. ((unsigned long) ivector_table) + size);
  781. ivector_table_pa = __pa(ivector_table);
  782. if (tlb_type == hypervisor)
  783. sun4v_init_mondo_queues();
  784. init_send_mondo_info();
  785. if (tlb_type == hypervisor) {
  786. /* Load up the boot cpu's entries. */
  787. sun4v_register_mondo_queues(hard_smp_processor_id());
  788. }
  789. /* We need to clear any IRQ's pending in the soft interrupt
  790. * registers, a spurious one could be left around from the
  791. * PROM timer which we just disabled.
  792. */
  793. clear_softint(get_softint());
  794. /* Now that ivector table is initialized, it is safe
  795. * to receive IRQ vector traps. We will normally take
  796. * one or two right now, in case some device PROM used
  797. * to boot us wants to speak to us. We just ignore them.
  798. */
  799. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  800. "or %%g1, %0, %%g1\n\t"
  801. "wrpr %%g1, 0x0, %%pstate"
  802. : /* No outputs */
  803. : "i" (PSTATE_IE)
  804. : "g1");
  805. irq_to_desc(0)->action = &timer_irq_action;
  806. }