irq_64.c 26 KB

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