irq.c 25 KB

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