irq.c 23 KB

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