irq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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_irq_disable(unsigned int virt_irq)
  248. {
  249. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  250. if (likely(data)) {
  251. unsigned long imap = data->imap;
  252. u32 tmp = upa_readq(imap);
  253. tmp &= ~IMAP_VALID;
  254. upa_writeq(tmp, imap);
  255. }
  256. }
  257. static void sun4u_irq_end(unsigned int virt_irq)
  258. {
  259. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  260. if (likely(data))
  261. upa_writeq(ICLR_IDLE, data->iclr);
  262. }
  263. static void sun4v_irq_enable(unsigned int virt_irq)
  264. {
  265. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  266. unsigned int ino = bucket - &ivector_table[0];
  267. if (likely(bucket)) {
  268. unsigned long cpuid;
  269. int err;
  270. cpuid = irq_choose_cpu(virt_irq);
  271. err = sun4v_intr_settarget(ino, cpuid);
  272. if (err != HV_EOK)
  273. printk("sun4v_intr_settarget(%x,%lu): err(%d)\n",
  274. ino, cpuid, err);
  275. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  276. if (err != HV_EOK)
  277. printk("sun4v_intr_setenabled(%x): err(%d)\n",
  278. ino, err);
  279. }
  280. }
  281. static void sun4v_irq_disable(unsigned int virt_irq)
  282. {
  283. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  284. unsigned int ino = bucket - &ivector_table[0];
  285. if (likely(bucket)) {
  286. int err;
  287. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  288. if (err != HV_EOK)
  289. printk("sun4v_intr_setenabled(%x): "
  290. "err(%d)\n", ino, err);
  291. }
  292. }
  293. #ifdef CONFIG_PCI_MSI
  294. static void sun4v_msi_enable(unsigned int virt_irq)
  295. {
  296. sun4v_irq_enable(virt_irq);
  297. unmask_msi_irq(virt_irq);
  298. }
  299. static void sun4v_msi_disable(unsigned int virt_irq)
  300. {
  301. mask_msi_irq(virt_irq);
  302. sun4v_irq_disable(virt_irq);
  303. }
  304. #endif
  305. static void sun4v_irq_end(unsigned int virt_irq)
  306. {
  307. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  308. unsigned int ino = bucket - &ivector_table[0];
  309. if (likely(bucket)) {
  310. int err;
  311. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  312. if (err != HV_EOK)
  313. printk("sun4v_intr_setstate(%x): "
  314. "err(%d)\n", ino, err);
  315. }
  316. }
  317. static void sun4v_virq_enable(unsigned int virt_irq)
  318. {
  319. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  320. unsigned int ino = bucket - &ivector_table[0];
  321. if (likely(bucket)) {
  322. unsigned long cpuid, dev_handle, dev_ino;
  323. int err;
  324. cpuid = irq_choose_cpu(virt_irq);
  325. dev_handle = ino & IMAP_IGN;
  326. dev_ino = ino & IMAP_INO;
  327. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  328. if (err != HV_EOK)
  329. printk("sun4v_vintr_set_target(%lx,%lx,%lu): "
  330. "err(%d)\n",
  331. dev_handle, dev_ino, cpuid, err);
  332. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  333. HV_INTR_ENABLED);
  334. if (err != HV_EOK)
  335. printk("sun4v_vintr_set_state(%lx,%lx,"
  336. "HV_INTR_ENABLED): err(%d)\n",
  337. dev_handle, dev_ino, err);
  338. }
  339. }
  340. static void sun4v_virq_disable(unsigned int virt_irq)
  341. {
  342. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  343. unsigned int ino = bucket - &ivector_table[0];
  344. if (likely(bucket)) {
  345. unsigned long dev_handle, dev_ino;
  346. int err;
  347. dev_handle = ino & IMAP_IGN;
  348. dev_ino = ino & IMAP_INO;
  349. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  350. HV_INTR_DISABLED);
  351. if (err != HV_EOK)
  352. printk("sun4v_vintr_set_state(%lx,%lx,"
  353. "HV_INTR_DISABLED): err(%d)\n",
  354. dev_handle, dev_ino, err);
  355. }
  356. }
  357. static void sun4v_virq_end(unsigned int virt_irq)
  358. {
  359. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  360. unsigned int ino = bucket - &ivector_table[0];
  361. if (likely(bucket)) {
  362. unsigned long dev_handle, dev_ino;
  363. int err;
  364. dev_handle = ino & IMAP_IGN;
  365. dev_ino = ino & IMAP_INO;
  366. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  367. HV_INTR_STATE_IDLE);
  368. if (err != HV_EOK)
  369. printk("sun4v_vintr_set_state(%lx,%lx,"
  370. "HV_INTR_STATE_IDLE): err(%d)\n",
  371. dev_handle, dev_ino, err);
  372. }
  373. }
  374. static void run_pre_handler(unsigned int virt_irq)
  375. {
  376. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  377. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  378. if (likely(data->pre_handler)) {
  379. data->pre_handler(__irq_ino(__irq(bucket)),
  380. data->pre_handler_arg1,
  381. data->pre_handler_arg2);
  382. }
  383. }
  384. static struct irq_chip sun4u_irq = {
  385. .typename = "sun4u",
  386. .enable = sun4u_irq_enable,
  387. .disable = sun4u_irq_disable,
  388. .end = sun4u_irq_end,
  389. };
  390. static struct irq_chip sun4u_irq_ack = {
  391. .typename = "sun4u+ack",
  392. .enable = sun4u_irq_enable,
  393. .disable = sun4u_irq_disable,
  394. .ack = run_pre_handler,
  395. .end = sun4u_irq_end,
  396. };
  397. static struct irq_chip sun4v_irq = {
  398. .typename = "sun4v",
  399. .enable = sun4v_irq_enable,
  400. .disable = sun4v_irq_disable,
  401. .end = sun4v_irq_end,
  402. };
  403. static struct irq_chip sun4v_irq_ack = {
  404. .typename = "sun4v+ack",
  405. .enable = sun4v_irq_enable,
  406. .disable = sun4v_irq_disable,
  407. .ack = run_pre_handler,
  408. .end = sun4v_irq_end,
  409. };
  410. #ifdef CONFIG_PCI_MSI
  411. static struct irq_chip sun4v_msi = {
  412. .typename = "sun4v+msi",
  413. .mask = mask_msi_irq,
  414. .unmask = unmask_msi_irq,
  415. .enable = sun4v_msi_enable,
  416. .disable = sun4v_msi_disable,
  417. .ack = run_pre_handler,
  418. .end = sun4v_irq_end,
  419. };
  420. #endif
  421. static struct irq_chip sun4v_virq = {
  422. .typename = "vsun4v",
  423. .enable = sun4v_virq_enable,
  424. .disable = sun4v_virq_disable,
  425. .end = sun4v_virq_end,
  426. };
  427. static struct irq_chip sun4v_virq_ack = {
  428. .typename = "vsun4v+ack",
  429. .enable = sun4v_virq_enable,
  430. .disable = sun4v_virq_disable,
  431. .ack = run_pre_handler,
  432. .end = sun4v_virq_end,
  433. };
  434. void irq_install_pre_handler(int virt_irq,
  435. void (*func)(unsigned int, void *, void *),
  436. void *arg1, void *arg2)
  437. {
  438. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  439. struct irq_chip *chip;
  440. data->pre_handler = func;
  441. data->pre_handler_arg1 = arg1;
  442. data->pre_handler_arg2 = arg2;
  443. chip = get_irq_chip(virt_irq);
  444. if (chip == &sun4u_irq_ack ||
  445. chip == &sun4v_irq_ack ||
  446. chip == &sun4v_virq_ack
  447. #ifdef CONFIG_PCI_MSI
  448. || chip == &sun4v_msi
  449. #endif
  450. )
  451. return;
  452. chip = (chip == &sun4u_irq ?
  453. &sun4u_irq_ack :
  454. (chip == &sun4v_irq ?
  455. &sun4v_irq_ack : &sun4v_virq_ack));
  456. set_irq_chip(virt_irq, chip);
  457. }
  458. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  459. {
  460. struct ino_bucket *bucket;
  461. struct irq_handler_data *data;
  462. int ino;
  463. BUG_ON(tlb_type == hypervisor);
  464. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  465. bucket = &ivector_table[ino];
  466. if (!bucket->virt_irq) {
  467. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  468. set_irq_chip(bucket->virt_irq, &sun4u_irq);
  469. }
  470. data = get_irq_chip_data(bucket->virt_irq);
  471. if (unlikely(data))
  472. goto out;
  473. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  474. if (unlikely(!data)) {
  475. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  476. prom_halt();
  477. }
  478. set_irq_chip_data(bucket->virt_irq, data);
  479. data->imap = imap;
  480. data->iclr = iclr;
  481. out:
  482. return bucket->virt_irq;
  483. }
  484. static unsigned int sun4v_build_common(unsigned long sysino,
  485. struct irq_chip *chip)
  486. {
  487. struct ino_bucket *bucket;
  488. struct irq_handler_data *data;
  489. BUG_ON(tlb_type != hypervisor);
  490. bucket = &ivector_table[sysino];
  491. if (!bucket->virt_irq) {
  492. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  493. set_irq_chip(bucket->virt_irq, chip);
  494. }
  495. data = get_irq_chip_data(bucket->virt_irq);
  496. if (unlikely(data))
  497. goto out;
  498. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  499. if (unlikely(!data)) {
  500. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  501. prom_halt();
  502. }
  503. set_irq_chip_data(bucket->virt_irq, data);
  504. /* Catch accidental accesses to these things. IMAP/ICLR handling
  505. * is done by hypervisor calls on sun4v platforms, not by direct
  506. * register accesses.
  507. */
  508. data->imap = ~0UL;
  509. data->iclr = ~0UL;
  510. out:
  511. return bucket->virt_irq;
  512. }
  513. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  514. {
  515. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  516. return sun4v_build_common(sysino, &sun4v_irq);
  517. }
  518. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  519. {
  520. unsigned long sysino, hv_err;
  521. BUG_ON(devhandle & ~IMAP_IGN);
  522. BUG_ON(devino & ~IMAP_INO);
  523. sysino = devhandle | devino;
  524. hv_err = sun4v_vintr_set_cookie(devhandle, devino, sysino);
  525. if (hv_err) {
  526. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  527. "err=%lu\n", devhandle, devino, hv_err);
  528. prom_halt();
  529. }
  530. return sun4v_build_common(sysino, &sun4v_virq);
  531. }
  532. #ifdef CONFIG_PCI_MSI
  533. unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
  534. unsigned int msi_start, unsigned int msi_end)
  535. {
  536. struct ino_bucket *bucket;
  537. struct irq_handler_data *data;
  538. unsigned long sysino;
  539. unsigned int devino;
  540. BUG_ON(tlb_type != hypervisor);
  541. /* Find a free devino in the given range. */
  542. for (devino = msi_start; devino < msi_end; devino++) {
  543. sysino = sun4v_devino_to_sysino(devhandle, devino);
  544. bucket = &ivector_table[sysino];
  545. if (!bucket->virt_irq)
  546. break;
  547. }
  548. if (devino >= msi_end)
  549. return 0;
  550. sysino = sun4v_devino_to_sysino(devhandle, devino);
  551. bucket = &ivector_table[sysino];
  552. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  553. *virt_irq_p = bucket->virt_irq;
  554. set_irq_chip(bucket->virt_irq, &sun4v_msi);
  555. data = get_irq_chip_data(bucket->virt_irq);
  556. if (unlikely(data))
  557. return devino;
  558. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  559. if (unlikely(!data)) {
  560. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  561. prom_halt();
  562. }
  563. set_irq_chip_data(bucket->virt_irq, data);
  564. data->imap = ~0UL;
  565. data->iclr = ~0UL;
  566. return devino;
  567. }
  568. void sun4v_destroy_msi(unsigned int virt_irq)
  569. {
  570. virt_irq_free(virt_irq);
  571. }
  572. #endif
  573. void ack_bad_irq(unsigned int virt_irq)
  574. {
  575. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  576. unsigned int ino = 0xdeadbeef;
  577. if (bucket)
  578. ino = bucket - &ivector_table[0];
  579. printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
  580. ino, virt_irq);
  581. }
  582. void handler_irq(int irq, struct pt_regs *regs)
  583. {
  584. struct ino_bucket *bucket;
  585. struct pt_regs *old_regs;
  586. clear_softint(1 << irq);
  587. old_regs = set_irq_regs(regs);
  588. irq_enter();
  589. /* Sliiiick... */
  590. bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
  591. while (bucket) {
  592. struct ino_bucket *next = __bucket(bucket->irq_chain);
  593. bucket->irq_chain = 0;
  594. __do_IRQ(bucket->virt_irq);
  595. bucket = next;
  596. }
  597. irq_exit();
  598. set_irq_regs(old_regs);
  599. }
  600. struct sun5_timer {
  601. u64 count0;
  602. u64 limit0;
  603. u64 count1;
  604. u64 limit1;
  605. };
  606. static struct sun5_timer *prom_timers;
  607. static u64 prom_limit0, prom_limit1;
  608. static void map_prom_timers(void)
  609. {
  610. struct device_node *dp;
  611. const unsigned int *addr;
  612. /* PROM timer node hangs out in the top level of device siblings... */
  613. dp = of_find_node_by_path("/");
  614. dp = dp->child;
  615. while (dp) {
  616. if (!strcmp(dp->name, "counter-timer"))
  617. break;
  618. dp = dp->sibling;
  619. }
  620. /* Assume if node is not present, PROM uses different tick mechanism
  621. * which we should not care about.
  622. */
  623. if (!dp) {
  624. prom_timers = (struct sun5_timer *) 0;
  625. return;
  626. }
  627. /* If PROM is really using this, it must be mapped by him. */
  628. addr = of_get_property(dp, "address", NULL);
  629. if (!addr) {
  630. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  631. prom_timers = (struct sun5_timer *) 0;
  632. return;
  633. }
  634. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  635. }
  636. static void kill_prom_timer(void)
  637. {
  638. if (!prom_timers)
  639. return;
  640. /* Save them away for later. */
  641. prom_limit0 = prom_timers->limit0;
  642. prom_limit1 = prom_timers->limit1;
  643. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  644. * We turn both off here just to be paranoid.
  645. */
  646. prom_timers->limit0 = 0;
  647. prom_timers->limit1 = 0;
  648. /* Wheee, eat the interrupt packet too... */
  649. __asm__ __volatile__(
  650. " mov 0x40, %%g2\n"
  651. " ldxa [%%g0] %0, %%g1\n"
  652. " ldxa [%%g2] %1, %%g1\n"
  653. " stxa %%g0, [%%g0] %0\n"
  654. " membar #Sync\n"
  655. : /* no outputs */
  656. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  657. : "g1", "g2");
  658. }
  659. void init_irqwork_curcpu(void)
  660. {
  661. int cpu = hard_smp_processor_id();
  662. trap_block[cpu].irq_worklist = 0;
  663. }
  664. /* Please be very careful with register_one_mondo() and
  665. * sun4v_register_mondo_queues().
  666. *
  667. * On SMP this gets invoked from the CPU trampoline before
  668. * the cpu has fully taken over the trap table from OBP,
  669. * and it's kernel stack + %g6 thread register state is
  670. * not fully cooked yet.
  671. *
  672. * Therefore you cannot make any OBP calls, not even prom_printf,
  673. * from these two routines.
  674. */
  675. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  676. {
  677. unsigned long num_entries = (qmask + 1) / 64;
  678. unsigned long status;
  679. status = sun4v_cpu_qconf(type, paddr, num_entries);
  680. if (status != HV_EOK) {
  681. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  682. "err %lu\n", type, paddr, num_entries, status);
  683. prom_halt();
  684. }
  685. }
  686. static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
  687. {
  688. struct trap_per_cpu *tb = &trap_block[this_cpu];
  689. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  690. tb->cpu_mondo_qmask);
  691. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  692. tb->dev_mondo_qmask);
  693. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  694. tb->resum_qmask);
  695. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  696. tb->nonresum_qmask);
  697. }
  698. static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask, int use_bootmem)
  699. {
  700. unsigned long size = PAGE_ALIGN(qmask + 1);
  701. unsigned long order = get_order(size);
  702. void *p = NULL;
  703. if (use_bootmem) {
  704. p = __alloc_bootmem_low(size, size, 0);
  705. } else {
  706. struct page *page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, order);
  707. if (page)
  708. p = page_address(page);
  709. }
  710. if (!p) {
  711. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  712. prom_halt();
  713. }
  714. *pa_ptr = __pa(p);
  715. }
  716. static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask, int use_bootmem)
  717. {
  718. unsigned long size = PAGE_ALIGN(qmask + 1);
  719. unsigned long order = get_order(size);
  720. void *p = NULL;
  721. if (use_bootmem) {
  722. p = __alloc_bootmem_low(size, size, 0);
  723. } else {
  724. struct page *page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, order);
  725. if (page)
  726. p = page_address(page);
  727. }
  728. if (!p) {
  729. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  730. prom_halt();
  731. }
  732. *pa_ptr = __pa(p);
  733. }
  734. static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
  735. {
  736. #ifdef CONFIG_SMP
  737. void *page;
  738. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  739. if (use_bootmem)
  740. page = alloc_bootmem_low_pages(PAGE_SIZE);
  741. else
  742. page = (void *) get_zeroed_page(GFP_ATOMIC);
  743. if (!page) {
  744. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  745. prom_halt();
  746. }
  747. tb->cpu_mondo_block_pa = __pa(page);
  748. tb->cpu_list_pa = __pa(page + 64);
  749. #endif
  750. }
  751. /* Allocate and register the mondo and error queues for this cpu. */
  752. void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
  753. {
  754. struct trap_per_cpu *tb = &trap_block[cpu];
  755. if (alloc) {
  756. alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask, use_bootmem);
  757. alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask, use_bootmem);
  758. alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask, use_bootmem);
  759. alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask, use_bootmem);
  760. alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask, use_bootmem);
  761. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, tb->nonresum_qmask, use_bootmem);
  762. init_cpu_send_mondo_info(tb, use_bootmem);
  763. }
  764. if (load) {
  765. if (cpu != hard_smp_processor_id()) {
  766. prom_printf("SUN4V: init mondo on cpu %d not %d\n",
  767. cpu, hard_smp_processor_id());
  768. prom_halt();
  769. }
  770. sun4v_register_mondo_queues(cpu);
  771. }
  772. }
  773. static struct irqaction timer_irq_action = {
  774. .name = "timer",
  775. };
  776. /* Only invoked on boot processor. */
  777. void __init init_IRQ(void)
  778. {
  779. map_prom_timers();
  780. kill_prom_timer();
  781. memset(&ivector_table[0], 0, sizeof(ivector_table));
  782. if (tlb_type == hypervisor)
  783. sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
  784. /* We need to clear any IRQ's pending in the soft interrupt
  785. * registers, a spurious one could be left around from the
  786. * PROM timer which we just disabled.
  787. */
  788. clear_softint(get_softint());
  789. /* Now that ivector table is initialized, it is safe
  790. * to receive IRQ vector traps. We will normally take
  791. * one or two right now, in case some device PROM used
  792. * to boot us wants to speak to us. We just ignore them.
  793. */
  794. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  795. "or %%g1, %0, %%g1\n\t"
  796. "wrpr %%g1, 0x0, %%pstate"
  797. : /* No outputs */
  798. : "i" (PSTATE_IE)
  799. : "g1");
  800. irq_desc[0].action = &timer_irq_action;
  801. }