irq_64.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. }
  168. return 0;
  169. }
  170. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  171. {
  172. unsigned int tid;
  173. if (this_is_starfire) {
  174. tid = starfire_translate(imap, cpuid);
  175. tid <<= IMAP_TID_SHIFT;
  176. tid &= IMAP_TID_UPA;
  177. } else {
  178. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  179. unsigned long ver;
  180. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  181. if ((ver >> 32UL) == __JALAPENO_ID ||
  182. (ver >> 32UL) == __SERRANO_ID) {
  183. tid = cpuid << IMAP_TID_SHIFT;
  184. tid &= IMAP_TID_JBUS;
  185. } else {
  186. unsigned int a = cpuid & 0x1f;
  187. unsigned int n = (cpuid >> 5) & 0x1f;
  188. tid = ((a << IMAP_AID_SHIFT) |
  189. (n << IMAP_NID_SHIFT));
  190. tid &= (IMAP_AID_SAFARI |
  191. IMAP_NID_SAFARI);;
  192. }
  193. } else {
  194. tid = cpuid << IMAP_TID_SHIFT;
  195. tid &= IMAP_TID_UPA;
  196. }
  197. }
  198. return tid;
  199. }
  200. struct irq_handler_data {
  201. unsigned long iclr;
  202. unsigned long imap;
  203. void (*pre_handler)(unsigned int, void *, void *);
  204. void *arg1;
  205. void *arg2;
  206. };
  207. #ifdef CONFIG_SMP
  208. static int irq_choose_cpu(unsigned int virt_irq)
  209. {
  210. cpumask_t mask = irq_desc[virt_irq].affinity;
  211. int cpuid;
  212. if (cpus_equal(mask, CPU_MASK_ALL)) {
  213. static int irq_rover;
  214. static DEFINE_SPINLOCK(irq_rover_lock);
  215. unsigned long flags;
  216. /* Round-robin distribution... */
  217. do_round_robin:
  218. spin_lock_irqsave(&irq_rover_lock, flags);
  219. while (!cpu_online(irq_rover)) {
  220. if (++irq_rover >= NR_CPUS)
  221. irq_rover = 0;
  222. }
  223. cpuid = irq_rover;
  224. do {
  225. if (++irq_rover >= NR_CPUS)
  226. irq_rover = 0;
  227. } while (!cpu_online(irq_rover));
  228. spin_unlock_irqrestore(&irq_rover_lock, flags);
  229. } else {
  230. cpumask_t tmp;
  231. cpus_and(tmp, cpu_online_map, mask);
  232. if (cpus_empty(tmp))
  233. goto do_round_robin;
  234. cpuid = first_cpu(tmp);
  235. }
  236. return cpuid;
  237. }
  238. #else
  239. static int irq_choose_cpu(unsigned int virt_irq)
  240. {
  241. return real_hard_smp_processor_id();
  242. }
  243. #endif
  244. static void sun4u_irq_enable(unsigned int virt_irq)
  245. {
  246. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  247. if (likely(data)) {
  248. unsigned long cpuid, imap, val;
  249. unsigned int tid;
  250. cpuid = irq_choose_cpu(virt_irq);
  251. imap = data->imap;
  252. tid = sun4u_compute_tid(imap, cpuid);
  253. val = upa_readq(imap);
  254. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  255. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  256. val |= tid | IMAP_VALID;
  257. upa_writeq(val, imap);
  258. upa_writeq(ICLR_IDLE, data->iclr);
  259. }
  260. }
  261. static void sun4u_set_affinity(unsigned int virt_irq,
  262. const struct cpumask *mask)
  263. {
  264. sun4u_irq_enable(virt_irq);
  265. }
  266. static void sun4u_irq_disable(unsigned int virt_irq)
  267. {
  268. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  269. if (likely(data)) {
  270. unsigned long imap = data->imap;
  271. unsigned long tmp = upa_readq(imap);
  272. tmp &= ~IMAP_VALID;
  273. upa_writeq(tmp, imap);
  274. }
  275. }
  276. static void sun4u_irq_eoi(unsigned int virt_irq)
  277. {
  278. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  279. struct irq_desc *desc = irq_desc + virt_irq;
  280. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  281. return;
  282. if (likely(data))
  283. upa_writeq(ICLR_IDLE, data->iclr);
  284. }
  285. static void sun4v_irq_enable(unsigned int virt_irq)
  286. {
  287. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  288. unsigned long cpuid = irq_choose_cpu(virt_irq);
  289. int err;
  290. err = sun4v_intr_settarget(ino, cpuid);
  291. if (err != HV_EOK)
  292. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  293. "err(%d)\n", ino, cpuid, err);
  294. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  295. if (err != HV_EOK)
  296. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  297. "err(%d)\n", ino, err);
  298. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  299. if (err != HV_EOK)
  300. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  301. ino, err);
  302. }
  303. static void sun4v_set_affinity(unsigned int virt_irq,
  304. const struct cpumask *mask)
  305. {
  306. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  307. unsigned long cpuid = irq_choose_cpu(virt_irq);
  308. int err;
  309. err = sun4v_intr_settarget(ino, cpuid);
  310. if (err != HV_EOK)
  311. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  312. "err(%d)\n", ino, cpuid, err);
  313. }
  314. static void sun4v_irq_disable(unsigned int virt_irq)
  315. {
  316. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  317. int err;
  318. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  319. if (err != HV_EOK)
  320. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  321. "err(%d)\n", ino, err);
  322. }
  323. static void sun4v_irq_eoi(unsigned int virt_irq)
  324. {
  325. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  326. struct irq_desc *desc = irq_desc + virt_irq;
  327. int err;
  328. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  329. return;
  330. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  331. if (err != HV_EOK)
  332. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  333. "err(%d)\n", ino, err);
  334. }
  335. static void sun4v_virq_enable(unsigned int virt_irq)
  336. {
  337. unsigned long cpuid, dev_handle, dev_ino;
  338. int err;
  339. cpuid = irq_choose_cpu(virt_irq);
  340. dev_handle = virt_irq_table[virt_irq].dev_handle;
  341. dev_ino = virt_irq_table[virt_irq].dev_ino;
  342. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  343. if (err != HV_EOK)
  344. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  345. "err(%d)\n",
  346. dev_handle, dev_ino, cpuid, err);
  347. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  348. HV_INTR_STATE_IDLE);
  349. if (err != HV_EOK)
  350. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  351. "HV_INTR_STATE_IDLE): err(%d)\n",
  352. dev_handle, dev_ino, err);
  353. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  354. HV_INTR_ENABLED);
  355. if (err != HV_EOK)
  356. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  357. "HV_INTR_ENABLED): err(%d)\n",
  358. dev_handle, dev_ino, err);
  359. }
  360. static void sun4v_virt_set_affinity(unsigned int virt_irq,
  361. const struct cpumask *mask)
  362. {
  363. unsigned long cpuid, dev_handle, dev_ino;
  364. int err;
  365. cpuid = irq_choose_cpu(virt_irq);
  366. dev_handle = virt_irq_table[virt_irq].dev_handle;
  367. dev_ino = virt_irq_table[virt_irq].dev_ino;
  368. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  369. if (err != HV_EOK)
  370. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  371. "err(%d)\n",
  372. dev_handle, dev_ino, cpuid, err);
  373. }
  374. static void sun4v_virq_disable(unsigned int virt_irq)
  375. {
  376. unsigned long dev_handle, dev_ino;
  377. int err;
  378. dev_handle = virt_irq_table[virt_irq].dev_handle;
  379. dev_ino = virt_irq_table[virt_irq].dev_ino;
  380. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  381. HV_INTR_DISABLED);
  382. if (err != HV_EOK)
  383. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  384. "HV_INTR_DISABLED): err(%d)\n",
  385. dev_handle, dev_ino, err);
  386. }
  387. static void sun4v_virq_eoi(unsigned int virt_irq)
  388. {
  389. struct irq_desc *desc = irq_desc + virt_irq;
  390. unsigned long dev_handle, dev_ino;
  391. int err;
  392. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  393. return;
  394. dev_handle = virt_irq_table[virt_irq].dev_handle;
  395. dev_ino = virt_irq_table[virt_irq].dev_ino;
  396. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  397. HV_INTR_STATE_IDLE);
  398. if (err != HV_EOK)
  399. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  400. "HV_INTR_STATE_IDLE): err(%d)\n",
  401. dev_handle, dev_ino, err);
  402. }
  403. static struct irq_chip sun4u_irq = {
  404. .typename = "sun4u",
  405. .enable = sun4u_irq_enable,
  406. .disable = sun4u_irq_disable,
  407. .eoi = sun4u_irq_eoi,
  408. .set_affinity = sun4u_set_affinity,
  409. };
  410. static struct irq_chip sun4v_irq = {
  411. .typename = "sun4v",
  412. .enable = sun4v_irq_enable,
  413. .disable = sun4v_irq_disable,
  414. .eoi = sun4v_irq_eoi,
  415. .set_affinity = sun4v_set_affinity,
  416. };
  417. static struct irq_chip sun4v_virq = {
  418. .typename = "vsun4v",
  419. .enable = sun4v_virq_enable,
  420. .disable = sun4v_virq_disable,
  421. .eoi = sun4v_virq_eoi,
  422. .set_affinity = sun4v_virt_set_affinity,
  423. };
  424. static void pre_flow_handler(unsigned int virt_irq,
  425. struct irq_desc *desc)
  426. {
  427. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  428. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  429. data->pre_handler(ino, data->arg1, data->arg2);
  430. handle_fasteoi_irq(virt_irq, desc);
  431. }
  432. void irq_install_pre_handler(int virt_irq,
  433. void (*func)(unsigned int, void *, void *),
  434. void *arg1, void *arg2)
  435. {
  436. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  437. struct irq_desc *desc = irq_desc + virt_irq;
  438. data->pre_handler = func;
  439. data->arg1 = arg1;
  440. data->arg2 = arg2;
  441. desc->handle_irq = pre_flow_handler;
  442. }
  443. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  444. {
  445. struct ino_bucket *bucket;
  446. struct irq_handler_data *data;
  447. unsigned int virt_irq;
  448. int ino;
  449. BUG_ON(tlb_type == hypervisor);
  450. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  451. bucket = &ivector_table[ino];
  452. virt_irq = bucket_get_virt_irq(__pa(bucket));
  453. if (!virt_irq) {
  454. virt_irq = virt_irq_alloc(0, ino);
  455. bucket_set_virt_irq(__pa(bucket), virt_irq);
  456. set_irq_chip_and_handler_name(virt_irq,
  457. &sun4u_irq,
  458. handle_fasteoi_irq,
  459. "IVEC");
  460. }
  461. data = get_irq_chip_data(virt_irq);
  462. if (unlikely(data))
  463. goto out;
  464. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  465. if (unlikely(!data)) {
  466. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  467. prom_halt();
  468. }
  469. set_irq_chip_data(virt_irq, data);
  470. data->imap = imap;
  471. data->iclr = iclr;
  472. out:
  473. return virt_irq;
  474. }
  475. static unsigned int sun4v_build_common(unsigned long sysino,
  476. struct irq_chip *chip)
  477. {
  478. struct ino_bucket *bucket;
  479. struct irq_handler_data *data;
  480. unsigned int virt_irq;
  481. BUG_ON(tlb_type != hypervisor);
  482. bucket = &ivector_table[sysino];
  483. virt_irq = bucket_get_virt_irq(__pa(bucket));
  484. if (!virt_irq) {
  485. virt_irq = virt_irq_alloc(0, sysino);
  486. bucket_set_virt_irq(__pa(bucket), virt_irq);
  487. set_irq_chip_and_handler_name(virt_irq, chip,
  488. handle_fasteoi_irq,
  489. "IVEC");
  490. }
  491. data = get_irq_chip_data(virt_irq);
  492. if (unlikely(data))
  493. goto out;
  494. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  495. if (unlikely(!data)) {
  496. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  497. prom_halt();
  498. }
  499. set_irq_chip_data(virt_irq, data);
  500. /* Catch accidental accesses to these things. IMAP/ICLR handling
  501. * is done by hypervisor calls on sun4v platforms, not by direct
  502. * register accesses.
  503. */
  504. data->imap = ~0UL;
  505. data->iclr = ~0UL;
  506. out:
  507. return virt_irq;
  508. }
  509. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  510. {
  511. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  512. return sun4v_build_common(sysino, &sun4v_irq);
  513. }
  514. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  515. {
  516. struct irq_handler_data *data;
  517. unsigned long hv_err, cookie;
  518. struct ino_bucket *bucket;
  519. struct irq_desc *desc;
  520. unsigned int virt_irq;
  521. bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
  522. if (unlikely(!bucket))
  523. return 0;
  524. __flush_dcache_range((unsigned long) bucket,
  525. ((unsigned long) bucket +
  526. sizeof(struct ino_bucket)));
  527. virt_irq = virt_irq_alloc(devhandle, devino);
  528. bucket_set_virt_irq(__pa(bucket), virt_irq);
  529. set_irq_chip_and_handler_name(virt_irq, &sun4v_virq,
  530. handle_fasteoi_irq,
  531. "IVEC");
  532. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  533. if (unlikely(!data))
  534. return 0;
  535. /* In order to make the LDC channel startup sequence easier,
  536. * especially wrt. locking, we do not let request_irq() enable
  537. * the interrupt.
  538. */
  539. desc = irq_desc + virt_irq;
  540. desc->status |= IRQ_NOAUTOEN;
  541. set_irq_chip_data(virt_irq, data);
  542. /* Catch accidental accesses to these things. IMAP/ICLR handling
  543. * is done by hypervisor calls on sun4v platforms, not by direct
  544. * register accesses.
  545. */
  546. data->imap = ~0UL;
  547. data->iclr = ~0UL;
  548. cookie = ~__pa(bucket);
  549. hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
  550. if (hv_err) {
  551. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  552. "err=%lu\n", devhandle, devino, hv_err);
  553. prom_halt();
  554. }
  555. return virt_irq;
  556. }
  557. void ack_bad_irq(unsigned int virt_irq)
  558. {
  559. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  560. if (!ino)
  561. ino = 0xdeadbeef;
  562. printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
  563. ino, virt_irq);
  564. }
  565. void *hardirq_stack[NR_CPUS];
  566. void *softirq_stack[NR_CPUS];
  567. static __attribute__((always_inline)) void *set_hardirq_stack(void)
  568. {
  569. void *orig_sp, *sp = hardirq_stack[smp_processor_id()];
  570. __asm__ __volatile__("mov %%sp, %0" : "=r" (orig_sp));
  571. if (orig_sp < sp ||
  572. orig_sp > (sp + THREAD_SIZE)) {
  573. sp += THREAD_SIZE - 192 - STACK_BIAS;
  574. __asm__ __volatile__("mov %0, %%sp" : : "r" (sp));
  575. }
  576. return orig_sp;
  577. }
  578. static __attribute__((always_inline)) void restore_hardirq_stack(void *orig_sp)
  579. {
  580. __asm__ __volatile__("mov %0, %%sp" : : "r" (orig_sp));
  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. void *orig_sp;
  587. clear_softint(1 << irq);
  588. old_regs = set_irq_regs(regs);
  589. irq_enter();
  590. /* Grab an atomic snapshot of the pending IVECs. */
  591. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  592. "wrpr %0, %3, %%pstate\n\t"
  593. "ldx [%2], %1\n\t"
  594. "stx %%g0, [%2]\n\t"
  595. "wrpr %0, 0x0, %%pstate\n\t"
  596. : "=&r" (pstate), "=&r" (bucket_pa)
  597. : "r" (irq_work_pa(smp_processor_id())),
  598. "i" (PSTATE_IE)
  599. : "memory");
  600. orig_sp = set_hardirq_stack();
  601. while (bucket_pa) {
  602. struct irq_desc *desc;
  603. unsigned long next_pa;
  604. unsigned int virt_irq;
  605. next_pa = bucket_get_chain_pa(bucket_pa);
  606. virt_irq = bucket_get_virt_irq(bucket_pa);
  607. bucket_clear_chain_pa(bucket_pa);
  608. desc = irq_desc + virt_irq;
  609. desc->handle_irq(virt_irq, desc);
  610. bucket_pa = next_pa;
  611. }
  612. restore_hardirq_stack(orig_sp);
  613. irq_exit();
  614. set_irq_regs(old_regs);
  615. }
  616. void do_softirq(void)
  617. {
  618. unsigned long flags;
  619. if (in_interrupt())
  620. return;
  621. local_irq_save(flags);
  622. if (local_softirq_pending()) {
  623. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  624. sp += THREAD_SIZE - 192 - STACK_BIAS;
  625. __asm__ __volatile__("mov %%sp, %0\n\t"
  626. "mov %1, %%sp"
  627. : "=&r" (orig_sp)
  628. : "r" (sp));
  629. __do_softirq();
  630. __asm__ __volatile__("mov %0, %%sp"
  631. : : "r" (orig_sp));
  632. }
  633. local_irq_restore(flags);
  634. }
  635. static void unhandled_perf_irq(struct pt_regs *regs)
  636. {
  637. unsigned long pcr, pic;
  638. read_pcr(pcr);
  639. read_pic(pic);
  640. write_pcr(0);
  641. printk(KERN_EMERG "CPU %d: Got unexpected perf counter IRQ.\n",
  642. smp_processor_id());
  643. printk(KERN_EMERG "CPU %d: PCR[%016lx] PIC[%016lx]\n",
  644. smp_processor_id(), pcr, pic);
  645. }
  646. /* Almost a direct copy of the powerpc PMC code. */
  647. static DEFINE_SPINLOCK(perf_irq_lock);
  648. static void *perf_irq_owner_caller; /* mostly for debugging */
  649. static void (*perf_irq)(struct pt_regs *regs) = unhandled_perf_irq;
  650. /* Invoked from level 15 PIL handler in trap table. */
  651. void perfctr_irq(int irq, struct pt_regs *regs)
  652. {
  653. clear_softint(1 << irq);
  654. perf_irq(regs);
  655. }
  656. int register_perfctr_intr(void (*handler)(struct pt_regs *))
  657. {
  658. int ret;
  659. if (!handler)
  660. return -EINVAL;
  661. spin_lock(&perf_irq_lock);
  662. if (perf_irq != unhandled_perf_irq) {
  663. printk(KERN_WARNING "register_perfctr_intr: "
  664. "perf IRQ busy (reserved by caller %p)\n",
  665. perf_irq_owner_caller);
  666. ret = -EBUSY;
  667. goto out;
  668. }
  669. perf_irq_owner_caller = __builtin_return_address(0);
  670. perf_irq = handler;
  671. ret = 0;
  672. out:
  673. spin_unlock(&perf_irq_lock);
  674. return ret;
  675. }
  676. EXPORT_SYMBOL_GPL(register_perfctr_intr);
  677. void release_perfctr_intr(void (*handler)(struct pt_regs *))
  678. {
  679. spin_lock(&perf_irq_lock);
  680. perf_irq_owner_caller = NULL;
  681. perf_irq = unhandled_perf_irq;
  682. spin_unlock(&perf_irq_lock);
  683. }
  684. EXPORT_SYMBOL_GPL(release_perfctr_intr);
  685. #ifdef CONFIG_HOTPLUG_CPU
  686. void fixup_irqs(void)
  687. {
  688. unsigned int irq;
  689. for (irq = 0; irq < NR_IRQS; irq++) {
  690. unsigned long flags;
  691. spin_lock_irqsave(&irq_desc[irq].lock, flags);
  692. if (irq_desc[irq].action &&
  693. !(irq_desc[irq].status & IRQ_PER_CPU)) {
  694. if (irq_desc[irq].chip->set_affinity)
  695. irq_desc[irq].chip->set_affinity(irq,
  696. &irq_desc[irq].affinity);
  697. }
  698. spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
  699. }
  700. tick_ops->disable_irq();
  701. }
  702. #endif
  703. struct sun5_timer {
  704. u64 count0;
  705. u64 limit0;
  706. u64 count1;
  707. u64 limit1;
  708. };
  709. static struct sun5_timer *prom_timers;
  710. static u64 prom_limit0, prom_limit1;
  711. static void map_prom_timers(void)
  712. {
  713. struct device_node *dp;
  714. const unsigned int *addr;
  715. /* PROM timer node hangs out in the top level of device siblings... */
  716. dp = of_find_node_by_path("/");
  717. dp = dp->child;
  718. while (dp) {
  719. if (!strcmp(dp->name, "counter-timer"))
  720. break;
  721. dp = dp->sibling;
  722. }
  723. /* Assume if node is not present, PROM uses different tick mechanism
  724. * which we should not care about.
  725. */
  726. if (!dp) {
  727. prom_timers = (struct sun5_timer *) 0;
  728. return;
  729. }
  730. /* If PROM is really using this, it must be mapped by him. */
  731. addr = of_get_property(dp, "address", NULL);
  732. if (!addr) {
  733. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  734. prom_timers = (struct sun5_timer *) 0;
  735. return;
  736. }
  737. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  738. }
  739. static void kill_prom_timer(void)
  740. {
  741. if (!prom_timers)
  742. return;
  743. /* Save them away for later. */
  744. prom_limit0 = prom_timers->limit0;
  745. prom_limit1 = prom_timers->limit1;
  746. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  747. * We turn both off here just to be paranoid.
  748. */
  749. prom_timers->limit0 = 0;
  750. prom_timers->limit1 = 0;
  751. /* Wheee, eat the interrupt packet too... */
  752. __asm__ __volatile__(
  753. " mov 0x40, %%g2\n"
  754. " ldxa [%%g0] %0, %%g1\n"
  755. " ldxa [%%g2] %1, %%g1\n"
  756. " stxa %%g0, [%%g0] %0\n"
  757. " membar #Sync\n"
  758. : /* no outputs */
  759. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  760. : "g1", "g2");
  761. }
  762. void notrace init_irqwork_curcpu(void)
  763. {
  764. int cpu = hard_smp_processor_id();
  765. trap_block[cpu].irq_worklist_pa = 0UL;
  766. }
  767. /* Please be very careful with register_one_mondo() and
  768. * sun4v_register_mondo_queues().
  769. *
  770. * On SMP this gets invoked from the CPU trampoline before
  771. * the cpu has fully taken over the trap table from OBP,
  772. * and it's kernel stack + %g6 thread register state is
  773. * not fully cooked yet.
  774. *
  775. * Therefore you cannot make any OBP calls, not even prom_printf,
  776. * from these two routines.
  777. */
  778. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  779. {
  780. unsigned long num_entries = (qmask + 1) / 64;
  781. unsigned long status;
  782. status = sun4v_cpu_qconf(type, paddr, num_entries);
  783. if (status != HV_EOK) {
  784. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  785. "err %lu\n", type, paddr, num_entries, status);
  786. prom_halt();
  787. }
  788. }
  789. void __cpuinit notrace sun4v_register_mondo_queues(int this_cpu)
  790. {
  791. struct trap_per_cpu *tb = &trap_block[this_cpu];
  792. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  793. tb->cpu_mondo_qmask);
  794. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  795. tb->dev_mondo_qmask);
  796. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  797. tb->resum_qmask);
  798. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  799. tb->nonresum_qmask);
  800. }
  801. static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
  802. {
  803. unsigned long size = PAGE_ALIGN(qmask + 1);
  804. void *p = __alloc_bootmem(size, size, 0);
  805. if (!p) {
  806. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  807. prom_halt();
  808. }
  809. *pa_ptr = __pa(p);
  810. }
  811. static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
  812. {
  813. unsigned long size = PAGE_ALIGN(qmask + 1);
  814. void *p = __alloc_bootmem(size, size, 0);
  815. if (!p) {
  816. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  817. prom_halt();
  818. }
  819. *pa_ptr = __pa(p);
  820. }
  821. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  822. {
  823. #ifdef CONFIG_SMP
  824. void *page;
  825. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  826. page = alloc_bootmem_pages(PAGE_SIZE);
  827. if (!page) {
  828. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  829. prom_halt();
  830. }
  831. tb->cpu_mondo_block_pa = __pa(page);
  832. tb->cpu_list_pa = __pa(page + 64);
  833. #endif
  834. }
  835. /* Allocate mondo and error queues for all possible cpus. */
  836. static void __init sun4v_init_mondo_queues(void)
  837. {
  838. int cpu;
  839. for_each_possible_cpu(cpu) {
  840. struct trap_per_cpu *tb = &trap_block[cpu];
  841. alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  842. alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  843. alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
  844. alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  845. alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  846. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
  847. tb->nonresum_qmask);
  848. }
  849. }
  850. static void __init init_send_mondo_info(void)
  851. {
  852. int cpu;
  853. for_each_possible_cpu(cpu) {
  854. struct trap_per_cpu *tb = &trap_block[cpu];
  855. init_cpu_send_mondo_info(tb);
  856. }
  857. }
  858. static struct irqaction timer_irq_action = {
  859. .name = "timer",
  860. };
  861. /* Only invoked on boot processor. */
  862. void __init init_IRQ(void)
  863. {
  864. unsigned long size;
  865. map_prom_timers();
  866. kill_prom_timer();
  867. size = sizeof(struct ino_bucket) * NUM_IVECS;
  868. ivector_table = alloc_bootmem(size);
  869. if (!ivector_table) {
  870. prom_printf("Fatal error, cannot allocate ivector_table\n");
  871. prom_halt();
  872. }
  873. __flush_dcache_range((unsigned long) ivector_table,
  874. ((unsigned long) ivector_table) + size);
  875. ivector_table_pa = __pa(ivector_table);
  876. if (tlb_type == hypervisor)
  877. sun4v_init_mondo_queues();
  878. init_send_mondo_info();
  879. if (tlb_type == hypervisor) {
  880. /* Load up the boot cpu's entries. */
  881. sun4v_register_mondo_queues(hard_smp_processor_id());
  882. }
  883. /* We need to clear any IRQ's pending in the soft interrupt
  884. * registers, a spurious one could be left around from the
  885. * PROM timer which we just disabled.
  886. */
  887. clear_softint(get_softint());
  888. /* Now that ivector table is initialized, it is safe
  889. * to receive IRQ vector traps. We will normally take
  890. * one or two right now, in case some device PROM used
  891. * to boot us wants to speak to us. We just ignore them.
  892. */
  893. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  894. "or %%g1, %0, %%g1\n\t"
  895. "wrpr %%g1, 0x0, %%pstate"
  896. : /* No outputs */
  897. : "i" (PSTATE_IE)
  898. : "g1");
  899. irq_desc[0].action = &timer_irq_action;
  900. }