irq.c 24 KB

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