irq_64.c 25 KB

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