irq.c 23 KB

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