irq_64.c 25 KB

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