irq.c 20 KB

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