irq.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. u32 msi;
  190. };
  191. void sparc64_set_msi(unsigned int virt_irq, u32 msi)
  192. {
  193. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  194. if (data)
  195. data->msi = msi;
  196. }
  197. u32 sparc64_get_msi(unsigned int virt_irq)
  198. {
  199. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  200. if (data)
  201. return data->msi;
  202. return 0xffffffff;
  203. }
  204. static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
  205. {
  206. unsigned int real_irq = virt_to_real_irq(virt_irq);
  207. struct ino_bucket *bucket = NULL;
  208. if (likely(real_irq))
  209. bucket = __bucket(real_irq);
  210. return bucket;
  211. }
  212. #ifdef CONFIG_SMP
  213. static int irq_choose_cpu(unsigned int virt_irq)
  214. {
  215. cpumask_t mask = irq_desc[virt_irq].affinity;
  216. int cpuid;
  217. if (cpus_equal(mask, CPU_MASK_ALL)) {
  218. static int irq_rover;
  219. static DEFINE_SPINLOCK(irq_rover_lock);
  220. unsigned long flags;
  221. /* Round-robin distribution... */
  222. do_round_robin:
  223. spin_lock_irqsave(&irq_rover_lock, flags);
  224. while (!cpu_online(irq_rover)) {
  225. if (++irq_rover >= NR_CPUS)
  226. irq_rover = 0;
  227. }
  228. cpuid = irq_rover;
  229. do {
  230. if (++irq_rover >= NR_CPUS)
  231. irq_rover = 0;
  232. } while (!cpu_online(irq_rover));
  233. spin_unlock_irqrestore(&irq_rover_lock, flags);
  234. } else {
  235. cpumask_t tmp;
  236. cpus_and(tmp, cpu_online_map, mask);
  237. if (cpus_empty(tmp))
  238. goto do_round_robin;
  239. cpuid = first_cpu(tmp);
  240. }
  241. return cpuid;
  242. }
  243. #else
  244. static int irq_choose_cpu(unsigned int virt_irq)
  245. {
  246. return real_hard_smp_processor_id();
  247. }
  248. #endif
  249. static void sun4u_irq_enable(unsigned int virt_irq)
  250. {
  251. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  252. if (likely(data)) {
  253. unsigned long cpuid, imap, val;
  254. unsigned int tid;
  255. cpuid = irq_choose_cpu(virt_irq);
  256. imap = data->imap;
  257. tid = sun4u_compute_tid(imap, cpuid);
  258. val = upa_readq(imap);
  259. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  260. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  261. val |= tid | IMAP_VALID;
  262. upa_writeq(val, imap);
  263. }
  264. }
  265. static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
  266. {
  267. sun4u_irq_enable(virt_irq);
  268. }
  269. static void sun4u_irq_disable(unsigned int virt_irq)
  270. {
  271. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  272. if (likely(data)) {
  273. unsigned long imap = data->imap;
  274. unsigned long tmp = upa_readq(imap);
  275. tmp &= ~IMAP_VALID;
  276. upa_writeq(tmp, imap);
  277. }
  278. }
  279. static void sun4u_irq_end(unsigned int virt_irq)
  280. {
  281. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  282. struct irq_desc *desc = irq_desc + virt_irq;
  283. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  284. return;
  285. if (likely(data))
  286. upa_writeq(ICLR_IDLE, data->iclr);
  287. }
  288. static void sun4v_irq_enable(unsigned int virt_irq)
  289. {
  290. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  291. unsigned int ino = bucket - &ivector_table[0];
  292. if (likely(bucket)) {
  293. unsigned long cpuid;
  294. int err;
  295. cpuid = irq_choose_cpu(virt_irq);
  296. err = sun4v_intr_settarget(ino, cpuid);
  297. if (err != HV_EOK)
  298. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  299. "err(%d)\n", ino, cpuid, err);
  300. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  301. if (err != HV_EOK)
  302. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  303. "err(%d)\n", ino, err);
  304. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  305. if (err != HV_EOK)
  306. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  307. ino, err);
  308. }
  309. }
  310. static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
  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. unsigned long cpuid;
  316. int err;
  317. cpuid = irq_choose_cpu(virt_irq);
  318. err = sun4v_intr_settarget(ino, cpuid);
  319. if (err != HV_EOK)
  320. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  321. "err(%d)\n", ino, cpuid, err);
  322. }
  323. }
  324. static void sun4v_irq_disable(unsigned int virt_irq)
  325. {
  326. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  327. unsigned int ino = bucket - &ivector_table[0];
  328. if (likely(bucket)) {
  329. int err;
  330. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  331. if (err != HV_EOK)
  332. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  333. "err(%d)\n", ino, err);
  334. }
  335. }
  336. #ifdef CONFIG_PCI_MSI
  337. static void sun4u_msi_enable(unsigned int virt_irq)
  338. {
  339. sun4u_irq_enable(virt_irq);
  340. unmask_msi_irq(virt_irq);
  341. }
  342. static void sun4u_msi_disable(unsigned int virt_irq)
  343. {
  344. mask_msi_irq(virt_irq);
  345. sun4u_irq_disable(virt_irq);
  346. }
  347. static void sun4v_msi_enable(unsigned int virt_irq)
  348. {
  349. sun4v_irq_enable(virt_irq);
  350. unmask_msi_irq(virt_irq);
  351. }
  352. static void sun4v_msi_disable(unsigned int virt_irq)
  353. {
  354. mask_msi_irq(virt_irq);
  355. sun4v_irq_disable(virt_irq);
  356. }
  357. #endif
  358. static void sun4v_irq_end(unsigned int virt_irq)
  359. {
  360. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  361. unsigned int ino = bucket - &ivector_table[0];
  362. struct irq_desc *desc = irq_desc + virt_irq;
  363. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  364. return;
  365. if (likely(bucket)) {
  366. int err;
  367. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  368. if (err != HV_EOK)
  369. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  370. "err(%d)\n", ino, err);
  371. }
  372. }
  373. static void sun4v_virq_enable(unsigned int virt_irq)
  374. {
  375. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  376. if (likely(bucket)) {
  377. unsigned long cpuid, dev_handle, dev_ino;
  378. int err;
  379. cpuid = irq_choose_cpu(virt_irq);
  380. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  381. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  382. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  383. if (err != HV_EOK)
  384. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  385. "err(%d)\n",
  386. dev_handle, dev_ino, cpuid, err);
  387. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  388. HV_INTR_STATE_IDLE);
  389. if (err != HV_EOK)
  390. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  391. "HV_INTR_STATE_IDLE): err(%d)\n",
  392. dev_handle, dev_ino, err);
  393. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  394. HV_INTR_ENABLED);
  395. if (err != HV_EOK)
  396. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  397. "HV_INTR_ENABLED): err(%d)\n",
  398. dev_handle, dev_ino, err);
  399. }
  400. }
  401. static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
  402. {
  403. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  404. if (likely(bucket)) {
  405. unsigned long cpuid, dev_handle, dev_ino;
  406. int err;
  407. cpuid = irq_choose_cpu(virt_irq);
  408. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  409. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  410. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  411. if (err != HV_EOK)
  412. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  413. "err(%d)\n",
  414. dev_handle, dev_ino, cpuid, err);
  415. }
  416. }
  417. static void sun4v_virq_disable(unsigned int virt_irq)
  418. {
  419. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  420. if (likely(bucket)) {
  421. unsigned long dev_handle, dev_ino;
  422. int err;
  423. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  424. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  425. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  426. HV_INTR_DISABLED);
  427. if (err != HV_EOK)
  428. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  429. "HV_INTR_DISABLED): err(%d)\n",
  430. dev_handle, dev_ino, err);
  431. }
  432. }
  433. static void sun4v_virq_end(unsigned int virt_irq)
  434. {
  435. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  436. struct irq_desc *desc = irq_desc + virt_irq;
  437. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  438. return;
  439. if (likely(bucket)) {
  440. unsigned long dev_handle, dev_ino;
  441. int err;
  442. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  443. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  444. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  445. HV_INTR_STATE_IDLE);
  446. if (err != HV_EOK)
  447. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  448. "HV_INTR_STATE_IDLE): err(%d)\n",
  449. dev_handle, dev_ino, err);
  450. }
  451. }
  452. static void run_pre_handler(unsigned int virt_irq)
  453. {
  454. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  455. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  456. if (likely(data->pre_handler)) {
  457. data->pre_handler(__irq_ino(__irq(bucket)),
  458. data->pre_handler_arg1,
  459. data->pre_handler_arg2);
  460. }
  461. }
  462. static struct irq_chip sun4u_irq = {
  463. .typename = "sun4u",
  464. .enable = sun4u_irq_enable,
  465. .disable = sun4u_irq_disable,
  466. .end = sun4u_irq_end,
  467. .set_affinity = sun4u_set_affinity,
  468. };
  469. static struct irq_chip sun4u_irq_ack = {
  470. .typename = "sun4u+ack",
  471. .enable = sun4u_irq_enable,
  472. .disable = sun4u_irq_disable,
  473. .ack = run_pre_handler,
  474. .end = sun4u_irq_end,
  475. .set_affinity = sun4u_set_affinity,
  476. };
  477. static struct irq_chip sun4v_irq = {
  478. .typename = "sun4v",
  479. .enable = sun4v_irq_enable,
  480. .disable = sun4v_irq_disable,
  481. .end = sun4v_irq_end,
  482. .set_affinity = sun4v_set_affinity,
  483. };
  484. static struct irq_chip sun4v_irq_ack = {
  485. .typename = "sun4v+ack",
  486. .enable = sun4v_irq_enable,
  487. .disable = sun4v_irq_disable,
  488. .ack = run_pre_handler,
  489. .end = sun4v_irq_end,
  490. .set_affinity = sun4v_set_affinity,
  491. };
  492. #ifdef CONFIG_PCI_MSI
  493. static struct irq_chip sun4u_msi = {
  494. .typename = "sun4u+msi",
  495. .mask = mask_msi_irq,
  496. .unmask = unmask_msi_irq,
  497. .enable = sun4u_msi_enable,
  498. .disable = sun4u_msi_disable,
  499. .ack = run_pre_handler,
  500. .end = sun4u_irq_end,
  501. .set_affinity = sun4u_set_affinity,
  502. };
  503. static struct irq_chip sun4v_msi = {
  504. .typename = "sun4v+msi",
  505. .mask = mask_msi_irq,
  506. .unmask = unmask_msi_irq,
  507. .enable = sun4v_msi_enable,
  508. .disable = sun4v_msi_disable,
  509. .ack = run_pre_handler,
  510. .end = sun4v_irq_end,
  511. .set_affinity = sun4v_set_affinity,
  512. };
  513. #endif
  514. static struct irq_chip sun4v_virq = {
  515. .typename = "vsun4v",
  516. .enable = sun4v_virq_enable,
  517. .disable = sun4v_virq_disable,
  518. .end = sun4v_virq_end,
  519. .set_affinity = sun4v_virt_set_affinity,
  520. };
  521. static struct irq_chip sun4v_virq_ack = {
  522. .typename = "vsun4v+ack",
  523. .enable = sun4v_virq_enable,
  524. .disable = sun4v_virq_disable,
  525. .ack = run_pre_handler,
  526. .end = sun4v_virq_end,
  527. .set_affinity = sun4v_virt_set_affinity,
  528. };
  529. void irq_install_pre_handler(int virt_irq,
  530. void (*func)(unsigned int, void *, void *),
  531. void *arg1, void *arg2)
  532. {
  533. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  534. struct irq_chip *chip;
  535. data->pre_handler = func;
  536. data->pre_handler_arg1 = arg1;
  537. data->pre_handler_arg2 = arg2;
  538. chip = get_irq_chip(virt_irq);
  539. if (chip == &sun4u_irq_ack ||
  540. chip == &sun4v_irq_ack ||
  541. chip == &sun4v_virq_ack
  542. #ifdef CONFIG_PCI_MSI
  543. || chip == &sun4u_msi
  544. || chip == &sun4v_msi
  545. #endif
  546. )
  547. return;
  548. chip = (chip == &sun4u_irq ?
  549. &sun4u_irq_ack :
  550. (chip == &sun4v_irq ?
  551. &sun4v_irq_ack : &sun4v_virq_ack));
  552. set_irq_chip(virt_irq, chip);
  553. }
  554. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  555. {
  556. struct ino_bucket *bucket;
  557. struct irq_handler_data *data;
  558. int ino;
  559. BUG_ON(tlb_type == hypervisor);
  560. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  561. bucket = &ivector_table[ino];
  562. if (!bucket->virt_irq) {
  563. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  564. set_irq_chip(bucket->virt_irq, &sun4u_irq);
  565. }
  566. data = get_irq_chip_data(bucket->virt_irq);
  567. if (unlikely(data))
  568. goto out;
  569. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  570. if (unlikely(!data)) {
  571. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  572. prom_halt();
  573. }
  574. set_irq_chip_data(bucket->virt_irq, data);
  575. data->imap = imap;
  576. data->iclr = iclr;
  577. out:
  578. return bucket->virt_irq;
  579. }
  580. static unsigned int sun4v_build_common(unsigned long sysino,
  581. struct irq_chip *chip)
  582. {
  583. struct ino_bucket *bucket;
  584. struct irq_handler_data *data;
  585. BUG_ON(tlb_type != hypervisor);
  586. bucket = &ivector_table[sysino];
  587. if (!bucket->virt_irq) {
  588. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  589. set_irq_chip(bucket->virt_irq, chip);
  590. }
  591. data = get_irq_chip_data(bucket->virt_irq);
  592. if (unlikely(data))
  593. goto out;
  594. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  595. if (unlikely(!data)) {
  596. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  597. prom_halt();
  598. }
  599. set_irq_chip_data(bucket->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. out:
  607. return bucket->virt_irq;
  608. }
  609. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  610. {
  611. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  612. return sun4v_build_common(sysino, &sun4v_irq);
  613. }
  614. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  615. {
  616. unsigned long sysino, hv_err;
  617. unsigned int virq;
  618. BUG_ON(devhandle & devino);
  619. sysino = devhandle | devino;
  620. BUG_ON(sysino & ~(IMAP_IGN | IMAP_INO));
  621. hv_err = sun4v_vintr_set_cookie(devhandle, devino, sysino);
  622. if (hv_err) {
  623. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  624. "err=%lu\n", devhandle, devino, hv_err);
  625. prom_halt();
  626. }
  627. virq = sun4v_build_common(sysino, &sun4v_virq);
  628. virt_to_real_irq_table[virq].dev_handle = devhandle;
  629. virt_to_real_irq_table[virq].dev_ino = devino;
  630. return virq;
  631. }
  632. #ifdef CONFIG_PCI_MSI
  633. unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
  634. unsigned int msi_start, unsigned int msi_end)
  635. {
  636. struct ino_bucket *bucket;
  637. struct irq_handler_data *data;
  638. unsigned long sysino;
  639. unsigned int devino;
  640. BUG_ON(tlb_type != hypervisor);
  641. /* Find a free devino in the given range. */
  642. for (devino = msi_start; devino < msi_end; devino++) {
  643. sysino = sun4v_devino_to_sysino(devhandle, devino);
  644. bucket = &ivector_table[sysino];
  645. if (!bucket->virt_irq)
  646. break;
  647. }
  648. if (devino >= msi_end)
  649. return -ENOSPC;
  650. sysino = sun4v_devino_to_sysino(devhandle, devino);
  651. bucket = &ivector_table[sysino];
  652. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  653. *virt_irq_p = bucket->virt_irq;
  654. set_irq_chip(bucket->virt_irq, &sun4v_msi);
  655. data = get_irq_chip_data(bucket->virt_irq);
  656. if (unlikely(data))
  657. return devino;
  658. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  659. if (unlikely(!data)) {
  660. virt_irq_free(*virt_irq_p);
  661. return -ENOMEM;
  662. }
  663. set_irq_chip_data(bucket->virt_irq, data);
  664. data->imap = ~0UL;
  665. data->iclr = ~0UL;
  666. return devino;
  667. }
  668. void sun4v_destroy_msi(unsigned int virt_irq)
  669. {
  670. virt_irq_free(virt_irq);
  671. }
  672. unsigned int sun4u_build_msi(u32 portid, unsigned int *virt_irq_p,
  673. unsigned int msi_start, unsigned int msi_end,
  674. unsigned long imap_base, unsigned long iclr_base)
  675. {
  676. struct ino_bucket *bucket;
  677. struct irq_handler_data *data;
  678. unsigned long sysino;
  679. unsigned int devino;
  680. /* Find a free devino in the given range. */
  681. for (devino = msi_start; devino < msi_end; devino++) {
  682. sysino = (portid << 6) | devino;
  683. bucket = &ivector_table[sysino];
  684. if (!bucket->virt_irq)
  685. break;
  686. }
  687. if (devino >= msi_end)
  688. return -ENOSPC;
  689. sysino = (portid << 6) | devino;
  690. bucket = &ivector_table[sysino];
  691. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  692. *virt_irq_p = bucket->virt_irq;
  693. set_irq_chip(bucket->virt_irq, &sun4u_msi);
  694. data = get_irq_chip_data(bucket->virt_irq);
  695. if (unlikely(data))
  696. return devino;
  697. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  698. if (unlikely(!data)) {
  699. virt_irq_free(*virt_irq_p);
  700. return -ENOMEM;
  701. }
  702. set_irq_chip_data(bucket->virt_irq, data);
  703. data->imap = (imap_base + (devino * 0x8UL));
  704. data->iclr = (iclr_base + (devino * 0x8UL));
  705. return devino;
  706. }
  707. void sun4u_destroy_msi(unsigned int virt_irq)
  708. {
  709. virt_irq_free(virt_irq);
  710. }
  711. #endif
  712. void ack_bad_irq(unsigned int virt_irq)
  713. {
  714. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  715. unsigned int ino = 0xdeadbeef;
  716. if (bucket)
  717. ino = bucket - &ivector_table[0];
  718. printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
  719. ino, virt_irq);
  720. }
  721. void handler_irq(int irq, struct pt_regs *regs)
  722. {
  723. struct ino_bucket *bucket;
  724. struct pt_regs *old_regs;
  725. clear_softint(1 << irq);
  726. old_regs = set_irq_regs(regs);
  727. irq_enter();
  728. /* Sliiiick... */
  729. bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
  730. while (bucket) {
  731. struct ino_bucket *next = __bucket(bucket->irq_chain);
  732. bucket->irq_chain = 0;
  733. __do_IRQ(bucket->virt_irq);
  734. bucket = next;
  735. }
  736. irq_exit();
  737. set_irq_regs(old_regs);
  738. }
  739. #ifdef CONFIG_HOTPLUG_CPU
  740. void fixup_irqs(void)
  741. {
  742. unsigned int irq;
  743. for (irq = 0; irq < NR_IRQS; irq++) {
  744. unsigned long flags;
  745. spin_lock_irqsave(&irq_desc[irq].lock, flags);
  746. if (irq_desc[irq].action &&
  747. !(irq_desc[irq].status & IRQ_PER_CPU)) {
  748. if (irq_desc[irq].chip->set_affinity)
  749. irq_desc[irq].chip->set_affinity(irq,
  750. irq_desc[irq].affinity);
  751. }
  752. spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
  753. }
  754. }
  755. #endif
  756. struct sun5_timer {
  757. u64 count0;
  758. u64 limit0;
  759. u64 count1;
  760. u64 limit1;
  761. };
  762. static struct sun5_timer *prom_timers;
  763. static u64 prom_limit0, prom_limit1;
  764. static void map_prom_timers(void)
  765. {
  766. struct device_node *dp;
  767. const unsigned int *addr;
  768. /* PROM timer node hangs out in the top level of device siblings... */
  769. dp = of_find_node_by_path("/");
  770. dp = dp->child;
  771. while (dp) {
  772. if (!strcmp(dp->name, "counter-timer"))
  773. break;
  774. dp = dp->sibling;
  775. }
  776. /* Assume if node is not present, PROM uses different tick mechanism
  777. * which we should not care about.
  778. */
  779. if (!dp) {
  780. prom_timers = (struct sun5_timer *) 0;
  781. return;
  782. }
  783. /* If PROM is really using this, it must be mapped by him. */
  784. addr = of_get_property(dp, "address", NULL);
  785. if (!addr) {
  786. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  787. prom_timers = (struct sun5_timer *) 0;
  788. return;
  789. }
  790. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  791. }
  792. static void kill_prom_timer(void)
  793. {
  794. if (!prom_timers)
  795. return;
  796. /* Save them away for later. */
  797. prom_limit0 = prom_timers->limit0;
  798. prom_limit1 = prom_timers->limit1;
  799. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  800. * We turn both off here just to be paranoid.
  801. */
  802. prom_timers->limit0 = 0;
  803. prom_timers->limit1 = 0;
  804. /* Wheee, eat the interrupt packet too... */
  805. __asm__ __volatile__(
  806. " mov 0x40, %%g2\n"
  807. " ldxa [%%g0] %0, %%g1\n"
  808. " ldxa [%%g2] %1, %%g1\n"
  809. " stxa %%g0, [%%g0] %0\n"
  810. " membar #Sync\n"
  811. : /* no outputs */
  812. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  813. : "g1", "g2");
  814. }
  815. void init_irqwork_curcpu(void)
  816. {
  817. int cpu = hard_smp_processor_id();
  818. trap_block[cpu].irq_worklist = 0;
  819. }
  820. /* Please be very careful with register_one_mondo() and
  821. * sun4v_register_mondo_queues().
  822. *
  823. * On SMP this gets invoked from the CPU trampoline before
  824. * the cpu has fully taken over the trap table from OBP,
  825. * and it's kernel stack + %g6 thread register state is
  826. * not fully cooked yet.
  827. *
  828. * Therefore you cannot make any OBP calls, not even prom_printf,
  829. * from these two routines.
  830. */
  831. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  832. {
  833. unsigned long num_entries = (qmask + 1) / 64;
  834. unsigned long status;
  835. status = sun4v_cpu_qconf(type, paddr, num_entries);
  836. if (status != HV_EOK) {
  837. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  838. "err %lu\n", type, paddr, num_entries, status);
  839. prom_halt();
  840. }
  841. }
  842. void __cpuinit sun4v_register_mondo_queues(int this_cpu)
  843. {
  844. struct trap_per_cpu *tb = &trap_block[this_cpu];
  845. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  846. tb->cpu_mondo_qmask);
  847. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  848. tb->dev_mondo_qmask);
  849. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  850. tb->resum_qmask);
  851. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  852. tb->nonresum_qmask);
  853. }
  854. static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
  855. {
  856. unsigned long size = PAGE_ALIGN(qmask + 1);
  857. void *p = __alloc_bootmem_low(size, size, 0);
  858. if (!p) {
  859. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  860. prom_halt();
  861. }
  862. *pa_ptr = __pa(p);
  863. }
  864. static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
  865. {
  866. unsigned long size = PAGE_ALIGN(qmask + 1);
  867. void *p = __alloc_bootmem_low(size, size, 0);
  868. if (!p) {
  869. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  870. prom_halt();
  871. }
  872. *pa_ptr = __pa(p);
  873. }
  874. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  875. {
  876. #ifdef CONFIG_SMP
  877. void *page;
  878. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  879. page = alloc_bootmem_low_pages(PAGE_SIZE);
  880. if (!page) {
  881. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  882. prom_halt();
  883. }
  884. tb->cpu_mondo_block_pa = __pa(page);
  885. tb->cpu_list_pa = __pa(page + 64);
  886. #endif
  887. }
  888. /* Allocate mondo and error queues for all possible cpus. */
  889. static void __init sun4v_init_mondo_queues(void)
  890. {
  891. int cpu;
  892. for_each_possible_cpu(cpu) {
  893. struct trap_per_cpu *tb = &trap_block[cpu];
  894. alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  895. alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  896. alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
  897. alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  898. alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  899. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
  900. tb->nonresum_qmask);
  901. init_cpu_send_mondo_info(tb);
  902. }
  903. /* Load up the boot cpu's entries. */
  904. sun4v_register_mondo_queues(hard_smp_processor_id());
  905. }
  906. static struct irqaction timer_irq_action = {
  907. .name = "timer",
  908. };
  909. /* Only invoked on boot processor. */
  910. void __init init_IRQ(void)
  911. {
  912. map_prom_timers();
  913. kill_prom_timer();
  914. memset(&ivector_table[0], 0, sizeof(ivector_table));
  915. if (tlb_type == hypervisor)
  916. sun4v_init_mondo_queues();
  917. /* We need to clear any IRQ's pending in the soft interrupt
  918. * registers, a spurious one could be left around from the
  919. * PROM timer which we just disabled.
  920. */
  921. clear_softint(get_softint());
  922. /* Now that ivector table is initialized, it is safe
  923. * to receive IRQ vector traps. We will normally take
  924. * one or two right now, in case some device PROM used
  925. * to boot us wants to speak to us. We just ignore them.
  926. */
  927. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  928. "or %%g1, %0, %%g1\n\t"
  929. "wrpr %%g1, 0x0, %%pstate"
  930. : /* No outputs */
  931. : "i" (PSTATE_IE)
  932. : "g1");
  933. irq_desc[0].action = &timer_irq_action;
  934. }