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