irq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /* $Id: irq.c,v 1.114 2002/01/11 08:45:38 davem Exp $
  2. * irq.c: UltraSparc IRQ handling/init/registry.
  3. *
  4. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/signal.h>
  14. #include <linux/mm.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/slab.h>
  17. #include <linux/random.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/irq.h>
  24. #include <linux/msi.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/processor.h>
  27. #include <asm/atomic.h>
  28. #include <asm/system.h>
  29. #include <asm/irq.h>
  30. #include <asm/io.h>
  31. #include <asm/sbus.h>
  32. #include <asm/iommu.h>
  33. #include <asm/upa.h>
  34. #include <asm/oplib.h>
  35. #include <asm/prom.h>
  36. #include <asm/timer.h>
  37. #include <asm/smp.h>
  38. #include <asm/starfire.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/cache.h>
  41. #include <asm/cpudata.h>
  42. #include <asm/auxio.h>
  43. #include <asm/head.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. extern unsigned long real_hard_smp_processor_id(void);
  150. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  151. {
  152. unsigned int tid;
  153. if (this_is_starfire) {
  154. tid = starfire_translate(imap, cpuid);
  155. tid <<= IMAP_TID_SHIFT;
  156. tid &= IMAP_TID_UPA;
  157. } else {
  158. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  159. unsigned long ver;
  160. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  161. if ((ver >> 32UL) == __JALAPENO_ID ||
  162. (ver >> 32UL) == __SERRANO_ID) {
  163. tid = cpuid << IMAP_TID_SHIFT;
  164. tid &= IMAP_TID_JBUS;
  165. } else {
  166. unsigned int a = cpuid & 0x1f;
  167. unsigned int n = (cpuid >> 5) & 0x1f;
  168. tid = ((a << IMAP_AID_SHIFT) |
  169. (n << IMAP_NID_SHIFT));
  170. tid &= (IMAP_AID_SAFARI |
  171. IMAP_NID_SAFARI);;
  172. }
  173. } else {
  174. tid = cpuid << IMAP_TID_SHIFT;
  175. tid &= IMAP_TID_UPA;
  176. }
  177. }
  178. return tid;
  179. }
  180. struct irq_handler_data {
  181. unsigned long iclr;
  182. unsigned long imap;
  183. void (*pre_handler)(unsigned int, void *, void *);
  184. void *pre_handler_arg1;
  185. void *pre_handler_arg2;
  186. };
  187. static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
  188. {
  189. unsigned int real_irq = virt_to_real_irq(virt_irq);
  190. struct ino_bucket *bucket = NULL;
  191. if (likely(real_irq))
  192. bucket = __bucket(real_irq);
  193. return bucket;
  194. }
  195. #ifdef CONFIG_SMP
  196. static int irq_choose_cpu(unsigned int virt_irq)
  197. {
  198. cpumask_t mask = irq_desc[virt_irq].affinity;
  199. int cpuid;
  200. if (cpus_equal(mask, CPU_MASK_ALL)) {
  201. static int irq_rover;
  202. static DEFINE_SPINLOCK(irq_rover_lock);
  203. unsigned long flags;
  204. /* Round-robin distribution... */
  205. do_round_robin:
  206. spin_lock_irqsave(&irq_rover_lock, flags);
  207. while (!cpu_online(irq_rover)) {
  208. if (++irq_rover >= NR_CPUS)
  209. irq_rover = 0;
  210. }
  211. cpuid = irq_rover;
  212. do {
  213. if (++irq_rover >= NR_CPUS)
  214. irq_rover = 0;
  215. } while (!cpu_online(irq_rover));
  216. spin_unlock_irqrestore(&irq_rover_lock, flags);
  217. } else {
  218. cpumask_t tmp;
  219. cpus_and(tmp, cpu_online_map, mask);
  220. if (cpus_empty(tmp))
  221. goto do_round_robin;
  222. cpuid = first_cpu(tmp);
  223. }
  224. return cpuid;
  225. }
  226. #else
  227. static int irq_choose_cpu(unsigned int virt_irq)
  228. {
  229. return real_hard_smp_processor_id();
  230. }
  231. #endif
  232. static void sun4u_irq_enable(unsigned int virt_irq)
  233. {
  234. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  235. if (likely(data)) {
  236. unsigned long cpuid, imap, val;
  237. unsigned int tid;
  238. cpuid = irq_choose_cpu(virt_irq);
  239. imap = data->imap;
  240. tid = sun4u_compute_tid(imap, cpuid);
  241. val = upa_readq(imap);
  242. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  243. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  244. val |= tid | IMAP_VALID;
  245. upa_writeq(val, imap);
  246. }
  247. }
  248. static void sun4u_irq_disable(unsigned int virt_irq)
  249. {
  250. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  251. if (likely(data)) {
  252. unsigned long imap = data->imap;
  253. u32 tmp = upa_readq(imap);
  254. tmp &= ~IMAP_VALID;
  255. upa_writeq(tmp, imap);
  256. }
  257. }
  258. static void sun4u_irq_end(unsigned int virt_irq)
  259. {
  260. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  261. if (likely(data))
  262. upa_writeq(ICLR_IDLE, data->iclr);
  263. }
  264. static void sun4v_irq_enable(unsigned int virt_irq)
  265. {
  266. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  267. unsigned int ino = bucket - &ivector_table[0];
  268. if (likely(bucket)) {
  269. unsigned long cpuid;
  270. int err;
  271. cpuid = irq_choose_cpu(virt_irq);
  272. err = sun4v_intr_settarget(ino, cpuid);
  273. if (err != HV_EOK)
  274. printk("sun4v_intr_settarget(%x,%lu): err(%d)\n",
  275. ino, cpuid, err);
  276. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  277. if (err != HV_EOK)
  278. printk("sun4v_intr_setenabled(%x): err(%d)\n",
  279. ino, err);
  280. }
  281. }
  282. static void sun4v_irq_disable(unsigned int virt_irq)
  283. {
  284. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  285. unsigned int ino = bucket - &ivector_table[0];
  286. if (likely(bucket)) {
  287. int err;
  288. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  289. if (err != HV_EOK)
  290. printk("sun4v_intr_setenabled(%x): "
  291. "err(%d)\n", ino, err);
  292. }
  293. }
  294. #ifdef CONFIG_PCI_MSI
  295. static void sun4v_msi_enable(unsigned int virt_irq)
  296. {
  297. sun4v_irq_enable(virt_irq);
  298. unmask_msi_irq(virt_irq);
  299. }
  300. static void sun4v_msi_disable(unsigned int virt_irq)
  301. {
  302. mask_msi_irq(virt_irq);
  303. sun4v_irq_disable(virt_irq);
  304. }
  305. #endif
  306. static void sun4v_irq_end(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_setstate(ino, HV_INTR_STATE_IDLE);
  313. if (err != HV_EOK)
  314. printk("sun4v_intr_setstate(%x): "
  315. "err(%d)\n", ino, err);
  316. }
  317. }
  318. static void run_pre_handler(unsigned int virt_irq)
  319. {
  320. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  321. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  322. if (likely(data->pre_handler)) {
  323. data->pre_handler(__irq_ino(__irq(bucket)),
  324. data->pre_handler_arg1,
  325. data->pre_handler_arg2);
  326. }
  327. }
  328. static struct irq_chip sun4u_irq = {
  329. .typename = "sun4u",
  330. .enable = sun4u_irq_enable,
  331. .disable = sun4u_irq_disable,
  332. .end = sun4u_irq_end,
  333. };
  334. static struct irq_chip sun4u_irq_ack = {
  335. .typename = "sun4u+ack",
  336. .enable = sun4u_irq_enable,
  337. .disable = sun4u_irq_disable,
  338. .ack = run_pre_handler,
  339. .end = sun4u_irq_end,
  340. };
  341. static struct irq_chip sun4v_irq = {
  342. .typename = "sun4v",
  343. .enable = sun4v_irq_enable,
  344. .disable = sun4v_irq_disable,
  345. .end = sun4v_irq_end,
  346. };
  347. static struct irq_chip sun4v_irq_ack = {
  348. .typename = "sun4v+ack",
  349. .enable = sun4v_irq_enable,
  350. .disable = sun4v_irq_disable,
  351. .ack = run_pre_handler,
  352. .end = sun4v_irq_end,
  353. };
  354. #ifdef CONFIG_PCI_MSI
  355. static struct irq_chip sun4v_msi = {
  356. .typename = "sun4v+msi",
  357. .mask = mask_msi_irq,
  358. .unmask = unmask_msi_irq,
  359. .enable = sun4v_msi_enable,
  360. .disable = sun4v_msi_disable,
  361. .ack = run_pre_handler,
  362. .end = sun4v_irq_end,
  363. };
  364. #endif
  365. void irq_install_pre_handler(int virt_irq,
  366. void (*func)(unsigned int, void *, void *),
  367. void *arg1, void *arg2)
  368. {
  369. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  370. struct irq_chip *chip;
  371. data->pre_handler = func;
  372. data->pre_handler_arg1 = arg1;
  373. data->pre_handler_arg2 = arg2;
  374. chip = get_irq_chip(virt_irq);
  375. if (chip == &sun4u_irq_ack ||
  376. chip == &sun4v_irq_ack
  377. #ifdef CONFIG_PCI_MSI
  378. || chip == &sun4v_msi
  379. #endif
  380. )
  381. return;
  382. chip = (chip == &sun4u_irq ?
  383. &sun4u_irq_ack : &sun4v_irq_ack);
  384. set_irq_chip(virt_irq, chip);
  385. }
  386. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  387. {
  388. struct ino_bucket *bucket;
  389. struct irq_handler_data *data;
  390. int ino;
  391. BUG_ON(tlb_type == hypervisor);
  392. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  393. bucket = &ivector_table[ino];
  394. if (!bucket->virt_irq) {
  395. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  396. set_irq_chip(bucket->virt_irq, &sun4u_irq);
  397. }
  398. data = get_irq_chip_data(bucket->virt_irq);
  399. if (unlikely(data))
  400. goto out;
  401. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  402. if (unlikely(!data)) {
  403. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  404. prom_halt();
  405. }
  406. set_irq_chip_data(bucket->virt_irq, data);
  407. data->imap = imap;
  408. data->iclr = iclr;
  409. out:
  410. return bucket->virt_irq;
  411. }
  412. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  413. {
  414. struct ino_bucket *bucket;
  415. struct irq_handler_data *data;
  416. unsigned long sysino;
  417. BUG_ON(tlb_type != hypervisor);
  418. sysino = sun4v_devino_to_sysino(devhandle, devino);
  419. bucket = &ivector_table[sysino];
  420. if (!bucket->virt_irq) {
  421. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  422. set_irq_chip(bucket->virt_irq, &sun4v_irq);
  423. }
  424. data = get_irq_chip_data(bucket->virt_irq);
  425. if (unlikely(data))
  426. goto out;
  427. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  428. if (unlikely(!data)) {
  429. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  430. prom_halt();
  431. }
  432. set_irq_chip_data(bucket->virt_irq, data);
  433. /* Catch accidental accesses to these things. IMAP/ICLR handling
  434. * is done by hypervisor calls on sun4v platforms, not by direct
  435. * register accesses.
  436. */
  437. data->imap = ~0UL;
  438. data->iclr = ~0UL;
  439. out:
  440. return bucket->virt_irq;
  441. }
  442. #ifdef CONFIG_PCI_MSI
  443. unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
  444. unsigned int msi_start, unsigned int msi_end)
  445. {
  446. struct ino_bucket *bucket;
  447. struct irq_handler_data *data;
  448. unsigned long sysino;
  449. unsigned int devino;
  450. BUG_ON(tlb_type != hypervisor);
  451. /* Find a free devino in the given range. */
  452. for (devino = msi_start; devino < msi_end; devino++) {
  453. sysino = sun4v_devino_to_sysino(devhandle, devino);
  454. bucket = &ivector_table[sysino];
  455. if (!bucket->virt_irq)
  456. break;
  457. }
  458. if (devino >= msi_end)
  459. return 0;
  460. sysino = sun4v_devino_to_sysino(devhandle, devino);
  461. bucket = &ivector_table[sysino];
  462. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  463. *virt_irq_p = bucket->virt_irq;
  464. set_irq_chip(bucket->virt_irq, &sun4v_msi);
  465. data = get_irq_chip_data(bucket->virt_irq);
  466. if (unlikely(data))
  467. return devino;
  468. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  469. if (unlikely(!data)) {
  470. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  471. prom_halt();
  472. }
  473. set_irq_chip_data(bucket->virt_irq, data);
  474. data->imap = ~0UL;
  475. data->iclr = ~0UL;
  476. return devino;
  477. }
  478. void sun4v_destroy_msi(unsigned int virt_irq)
  479. {
  480. virt_irq_free(virt_irq);
  481. }
  482. #endif
  483. void ack_bad_irq(unsigned int virt_irq)
  484. {
  485. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  486. unsigned int ino = 0xdeadbeef;
  487. if (bucket)
  488. ino = bucket - &ivector_table[0];
  489. printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
  490. ino, virt_irq);
  491. }
  492. void handler_irq(int irq, struct pt_regs *regs)
  493. {
  494. struct ino_bucket *bucket;
  495. struct pt_regs *old_regs;
  496. clear_softint(1 << irq);
  497. old_regs = set_irq_regs(regs);
  498. irq_enter();
  499. /* Sliiiick... */
  500. bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
  501. while (bucket) {
  502. struct ino_bucket *next = __bucket(bucket->irq_chain);
  503. bucket->irq_chain = 0;
  504. __do_IRQ(bucket->virt_irq);
  505. bucket = next;
  506. }
  507. irq_exit();
  508. set_irq_regs(old_regs);
  509. }
  510. struct sun5_timer {
  511. u64 count0;
  512. u64 limit0;
  513. u64 count1;
  514. u64 limit1;
  515. };
  516. static struct sun5_timer *prom_timers;
  517. static u64 prom_limit0, prom_limit1;
  518. static void map_prom_timers(void)
  519. {
  520. struct device_node *dp;
  521. const unsigned int *addr;
  522. /* PROM timer node hangs out in the top level of device siblings... */
  523. dp = of_find_node_by_path("/");
  524. dp = dp->child;
  525. while (dp) {
  526. if (!strcmp(dp->name, "counter-timer"))
  527. break;
  528. dp = dp->sibling;
  529. }
  530. /* Assume if node is not present, PROM uses different tick mechanism
  531. * which we should not care about.
  532. */
  533. if (!dp) {
  534. prom_timers = (struct sun5_timer *) 0;
  535. return;
  536. }
  537. /* If PROM is really using this, it must be mapped by him. */
  538. addr = of_get_property(dp, "address", NULL);
  539. if (!addr) {
  540. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  541. prom_timers = (struct sun5_timer *) 0;
  542. return;
  543. }
  544. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  545. }
  546. static void kill_prom_timer(void)
  547. {
  548. if (!prom_timers)
  549. return;
  550. /* Save them away for later. */
  551. prom_limit0 = prom_timers->limit0;
  552. prom_limit1 = prom_timers->limit1;
  553. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  554. * We turn both off here just to be paranoid.
  555. */
  556. prom_timers->limit0 = 0;
  557. prom_timers->limit1 = 0;
  558. /* Wheee, eat the interrupt packet too... */
  559. __asm__ __volatile__(
  560. " mov 0x40, %%g2\n"
  561. " ldxa [%%g0] %0, %%g1\n"
  562. " ldxa [%%g2] %1, %%g1\n"
  563. " stxa %%g0, [%%g0] %0\n"
  564. " membar #Sync\n"
  565. : /* no outputs */
  566. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  567. : "g1", "g2");
  568. }
  569. void init_irqwork_curcpu(void)
  570. {
  571. int cpu = hard_smp_processor_id();
  572. trap_block[cpu].irq_worklist = 0;
  573. }
  574. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
  575. {
  576. unsigned long num_entries = 128;
  577. unsigned long status;
  578. status = sun4v_cpu_qconf(type, paddr, num_entries);
  579. if (status != HV_EOK) {
  580. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  581. "err %lu\n", type, paddr, num_entries, status);
  582. prom_halt();
  583. }
  584. }
  585. static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
  586. {
  587. struct trap_per_cpu *tb = &trap_block[this_cpu];
  588. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
  589. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
  590. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
  591. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
  592. }
  593. static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
  594. {
  595. void *page;
  596. if (use_bootmem)
  597. page = alloc_bootmem_low_pages(PAGE_SIZE);
  598. else
  599. page = (void *) get_zeroed_page(GFP_ATOMIC);
  600. if (!page) {
  601. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  602. prom_halt();
  603. }
  604. *pa_ptr = __pa(page);
  605. }
  606. static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
  607. {
  608. void *page;
  609. if (use_bootmem)
  610. page = alloc_bootmem_low_pages(PAGE_SIZE);
  611. else
  612. page = (void *) get_zeroed_page(GFP_ATOMIC);
  613. if (!page) {
  614. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  615. prom_halt();
  616. }
  617. *pa_ptr = __pa(page);
  618. }
  619. static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
  620. {
  621. #ifdef CONFIG_SMP
  622. void *page;
  623. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  624. if (use_bootmem)
  625. page = alloc_bootmem_low_pages(PAGE_SIZE);
  626. else
  627. page = (void *) get_zeroed_page(GFP_ATOMIC);
  628. if (!page) {
  629. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  630. prom_halt();
  631. }
  632. tb->cpu_mondo_block_pa = __pa(page);
  633. tb->cpu_list_pa = __pa(page + 64);
  634. #endif
  635. }
  636. /* Allocate and register the mondo and error queues for this cpu. */
  637. void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
  638. {
  639. struct trap_per_cpu *tb = &trap_block[cpu];
  640. if (alloc) {
  641. alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
  642. alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
  643. alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
  644. alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
  645. alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
  646. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
  647. init_cpu_send_mondo_info(tb, use_bootmem);
  648. }
  649. if (load) {
  650. if (cpu != hard_smp_processor_id()) {
  651. prom_printf("SUN4V: init mondo on cpu %d not %d\n",
  652. cpu, hard_smp_processor_id());
  653. prom_halt();
  654. }
  655. sun4v_register_mondo_queues(cpu);
  656. }
  657. }
  658. static struct irqaction timer_irq_action = {
  659. .name = "timer",
  660. };
  661. /* Only invoked on boot processor. */
  662. void __init init_IRQ(void)
  663. {
  664. map_prom_timers();
  665. kill_prom_timer();
  666. memset(&ivector_table[0], 0, sizeof(ivector_table));
  667. if (tlb_type == hypervisor)
  668. sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
  669. /* We need to clear any IRQ's pending in the soft interrupt
  670. * registers, a spurious one could be left around from the
  671. * PROM timer which we just disabled.
  672. */
  673. clear_softint(get_softint());
  674. /* Now that ivector table is initialized, it is safe
  675. * to receive IRQ vector traps. We will normally take
  676. * one or two right now, in case some device PROM used
  677. * to boot us wants to speak to us. We just ignore them.
  678. */
  679. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  680. "or %%g1, %0, %%g1\n\t"
  681. "wrpr %%g1, 0x0, %%pstate"
  682. : /* No outputs */
  683. : "i" (PSTATE_IE)
  684. : "g1");
  685. irq_desc[0].action = &timer_irq_action;
  686. }