irq_64.c 25 KB

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