irq.c 23 KB

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