irq.c 24 KB

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