irq.c 24 KB

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