irq.c 26 KB

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