irq_64.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /* irq.c: UltraSparc IRQ handling/init/registry.
  2. *
  3. * Copyright (C) 1997, 2007, 2008 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/linkage.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/ftrace.h>
  23. #include <linux/irq.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/iommu.h>
  31. #include <asm/upa.h>
  32. #include <asm/oplib.h>
  33. #include <asm/prom.h>
  34. #include <asm/timer.h>
  35. #include <asm/smp.h>
  36. #include <asm/starfire.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/cache.h>
  39. #include <asm/cpudata.h>
  40. #include <asm/auxio.h>
  41. #include <asm/head.h>
  42. #include <asm/hypervisor.h>
  43. #include <asm/cacheflush.h>
  44. #include "entry.h"
  45. #include "cpumap.h"
  46. #define NUM_IVECS (IMAP_INR + 1)
  47. struct ino_bucket *ivector_table;
  48. unsigned long ivector_table_pa;
  49. /* On several sun4u processors, it is illegal to mix bypass and
  50. * non-bypass accesses. Therefore we access all INO buckets
  51. * using bypass accesses only.
  52. */
  53. static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
  54. {
  55. unsigned long ret;
  56. __asm__ __volatile__("ldxa [%1] %2, %0"
  57. : "=&r" (ret)
  58. : "r" (bucket_pa +
  59. offsetof(struct ino_bucket,
  60. __irq_chain_pa)),
  61. "i" (ASI_PHYS_USE_EC));
  62. return ret;
  63. }
  64. static void bucket_clear_chain_pa(unsigned long bucket_pa)
  65. {
  66. __asm__ __volatile__("stxa %%g0, [%0] %1"
  67. : /* no outputs */
  68. : "r" (bucket_pa +
  69. offsetof(struct ino_bucket,
  70. __irq_chain_pa)),
  71. "i" (ASI_PHYS_USE_EC));
  72. }
  73. static unsigned int bucket_get_virt_irq(unsigned long bucket_pa)
  74. {
  75. unsigned int ret;
  76. __asm__ __volatile__("lduwa [%1] %2, %0"
  77. : "=&r" (ret)
  78. : "r" (bucket_pa +
  79. offsetof(struct ino_bucket,
  80. __virt_irq)),
  81. "i" (ASI_PHYS_USE_EC));
  82. return ret;
  83. }
  84. static void bucket_set_virt_irq(unsigned long bucket_pa,
  85. unsigned int virt_irq)
  86. {
  87. __asm__ __volatile__("stwa %0, [%1] %2"
  88. : /* no outputs */
  89. : "r" (virt_irq),
  90. "r" (bucket_pa +
  91. offsetof(struct ino_bucket,
  92. __virt_irq)),
  93. "i" (ASI_PHYS_USE_EC));
  94. }
  95. #define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
  96. static struct {
  97. unsigned int dev_handle;
  98. unsigned int dev_ino;
  99. unsigned int in_use;
  100. } virt_irq_table[NR_IRQS];
  101. static DEFINE_SPINLOCK(virt_irq_alloc_lock);
  102. unsigned char virt_irq_alloc(unsigned int dev_handle,
  103. unsigned int dev_ino)
  104. {
  105. unsigned long flags;
  106. unsigned char ent;
  107. BUILD_BUG_ON(NR_IRQS >= 256);
  108. spin_lock_irqsave(&virt_irq_alloc_lock, flags);
  109. for (ent = 1; ent < NR_IRQS; ent++) {
  110. if (!virt_irq_table[ent].in_use)
  111. break;
  112. }
  113. if (ent >= NR_IRQS) {
  114. printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
  115. ent = 0;
  116. } else {
  117. virt_irq_table[ent].dev_handle = dev_handle;
  118. virt_irq_table[ent].dev_ino = dev_ino;
  119. virt_irq_table[ent].in_use = 1;
  120. }
  121. spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
  122. return ent;
  123. }
  124. #ifdef CONFIG_PCI_MSI
  125. void virt_irq_free(unsigned int virt_irq)
  126. {
  127. unsigned long flags;
  128. if (virt_irq >= NR_IRQS)
  129. return;
  130. spin_lock_irqsave(&virt_irq_alloc_lock, flags);
  131. virt_irq_table[virt_irq].in_use = 0;
  132. spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
  133. }
  134. #endif
  135. /*
  136. * /proc/interrupts printing:
  137. */
  138. int show_interrupts(struct seq_file *p, void *v)
  139. {
  140. int i = *(loff_t *) v, j;
  141. struct irqaction * action;
  142. unsigned long flags;
  143. if (i == 0) {
  144. seq_printf(p, " ");
  145. for_each_online_cpu(j)
  146. seq_printf(p, "CPU%d ",j);
  147. seq_putc(p, '\n');
  148. }
  149. if (i < NR_IRQS) {
  150. raw_spin_lock_irqsave(&irq_desc[i].lock, flags);
  151. action = irq_desc[i].action;
  152. if (!action)
  153. goto skip;
  154. seq_printf(p, "%3d: ",i);
  155. #ifndef CONFIG_SMP
  156. seq_printf(p, "%10u ", kstat_irqs(i));
  157. #else
  158. for_each_online_cpu(j)
  159. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  160. #endif
  161. seq_printf(p, " %9s", irq_desc[i].chip->name);
  162. seq_printf(p, " %s", action->name);
  163. for (action=action->next; action; action = action->next)
  164. seq_printf(p, ", %s", action->name);
  165. seq_putc(p, '\n');
  166. skip:
  167. raw_spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  168. } else if (i == NR_IRQS) {
  169. seq_printf(p, "NMI: ");
  170. for_each_online_cpu(j)
  171. seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
  172. seq_printf(p, " Non-maskable interrupts\n");
  173. }
  174. return 0;
  175. }
  176. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  177. {
  178. unsigned int tid;
  179. if (this_is_starfire) {
  180. tid = starfire_translate(imap, cpuid);
  181. tid <<= IMAP_TID_SHIFT;
  182. tid &= IMAP_TID_UPA;
  183. } else {
  184. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  185. unsigned long ver;
  186. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  187. if ((ver >> 32UL) == __JALAPENO_ID ||
  188. (ver >> 32UL) == __SERRANO_ID) {
  189. tid = cpuid << IMAP_TID_SHIFT;
  190. tid &= IMAP_TID_JBUS;
  191. } else {
  192. unsigned int a = cpuid & 0x1f;
  193. unsigned int n = (cpuid >> 5) & 0x1f;
  194. tid = ((a << IMAP_AID_SHIFT) |
  195. (n << IMAP_NID_SHIFT));
  196. tid &= (IMAP_AID_SAFARI |
  197. IMAP_NID_SAFARI);
  198. }
  199. } else {
  200. tid = cpuid << IMAP_TID_SHIFT;
  201. tid &= IMAP_TID_UPA;
  202. }
  203. }
  204. return tid;
  205. }
  206. struct irq_handler_data {
  207. unsigned long iclr;
  208. unsigned long imap;
  209. void (*pre_handler)(unsigned int, void *, void *);
  210. void *arg1;
  211. void *arg2;
  212. };
  213. #ifdef CONFIG_SMP
  214. static int irq_choose_cpu(unsigned int virt_irq, const struct cpumask *affinity)
  215. {
  216. cpumask_t mask;
  217. int cpuid;
  218. cpumask_copy(&mask, affinity);
  219. if (cpus_equal(mask, cpu_online_map)) {
  220. cpuid = map_to_cpu(virt_irq);
  221. } else {
  222. cpumask_t tmp;
  223. cpus_and(tmp, cpu_online_map, mask);
  224. cpuid = cpus_empty(tmp) ? map_to_cpu(virt_irq) : first_cpu(tmp);
  225. }
  226. return cpuid;
  227. }
  228. #else
  229. #define irq_choose_cpu(virt_irq, affinity) \
  230. real_hard_smp_processor_id()
  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. irq_desc[virt_irq].affinity);
  240. imap = data->imap;
  241. tid = sun4u_compute_tid(imap, cpuid);
  242. val = upa_readq(imap);
  243. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  244. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  245. val |= tid | IMAP_VALID;
  246. upa_writeq(val, imap);
  247. upa_writeq(ICLR_IDLE, data->iclr);
  248. }
  249. }
  250. static int sun4u_set_affinity(unsigned int virt_irq,
  251. const struct cpumask *mask)
  252. {
  253. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  254. if (likely(data)) {
  255. unsigned long cpuid, imap, val;
  256. unsigned int tid;
  257. cpuid = irq_choose_cpu(virt_irq, mask);
  258. imap = data->imap;
  259. tid = sun4u_compute_tid(imap, cpuid);
  260. val = upa_readq(imap);
  261. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  262. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  263. val |= tid | IMAP_VALID;
  264. upa_writeq(val, imap);
  265. upa_writeq(ICLR_IDLE, data->iclr);
  266. }
  267. return 0;
  268. }
  269. /* Don't do anything. The desc->status check for IRQ_DISABLED in
  270. * handler_irq() will skip the handler call and that will leave the
  271. * interrupt in the sent state. The next ->enable() call will hit the
  272. * ICLR register to reset the state machine.
  273. *
  274. * This scheme is necessary, instead of clearing the Valid bit in the
  275. * IMAP register, to handle the case of IMAP registers being shared by
  276. * multiple INOs (and thus ICLR registers). Since we use a different
  277. * virtual IRQ for each shared IMAP instance, the generic code thinks
  278. * there is only one user so it prematurely calls ->disable() on
  279. * free_irq().
  280. *
  281. * We have to provide an explicit ->disable() method instead of using
  282. * NULL to get the default. The reason is that if the generic code
  283. * sees that, it also hooks up a default ->shutdown method which
  284. * invokes ->mask() which we do not want. See irq_chip_set_defaults().
  285. */
  286. static void sun4u_irq_disable(unsigned int virt_irq)
  287. {
  288. }
  289. static void sun4u_irq_eoi(unsigned int virt_irq)
  290. {
  291. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  292. struct irq_desc *desc = irq_desc + virt_irq;
  293. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  294. return;
  295. if (likely(data))
  296. upa_writeq(ICLR_IDLE, data->iclr);
  297. }
  298. static void sun4v_irq_enable(unsigned int virt_irq)
  299. {
  300. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  301. unsigned long cpuid = irq_choose_cpu(virt_irq,
  302. irq_desc[virt_irq].affinity);
  303. int err;
  304. err = sun4v_intr_settarget(ino, cpuid);
  305. if (err != HV_EOK)
  306. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  307. "err(%d)\n", ino, cpuid, err);
  308. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  309. if (err != HV_EOK)
  310. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  311. "err(%d)\n", ino, err);
  312. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  313. if (err != HV_EOK)
  314. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  315. ino, err);
  316. }
  317. static int sun4v_set_affinity(unsigned int virt_irq,
  318. const struct cpumask *mask)
  319. {
  320. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  321. unsigned long cpuid = irq_choose_cpu(virt_irq, mask);
  322. int err;
  323. err = sun4v_intr_settarget(ino, cpuid);
  324. if (err != HV_EOK)
  325. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  326. "err(%d)\n", ino, cpuid, err);
  327. return 0;
  328. }
  329. static void sun4v_irq_disable(unsigned int virt_irq)
  330. {
  331. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  332. int err;
  333. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  334. if (err != HV_EOK)
  335. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  336. "err(%d)\n", ino, err);
  337. }
  338. static void sun4v_irq_eoi(unsigned int virt_irq)
  339. {
  340. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  341. struct irq_desc *desc = irq_desc + virt_irq;
  342. int err;
  343. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  344. return;
  345. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  346. if (err != HV_EOK)
  347. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  348. "err(%d)\n", ino, err);
  349. }
  350. static void sun4v_virq_enable(unsigned int virt_irq)
  351. {
  352. unsigned long cpuid, dev_handle, dev_ino;
  353. int err;
  354. cpuid = irq_choose_cpu(virt_irq, irq_desc[virt_irq].affinity);
  355. dev_handle = virt_irq_table[virt_irq].dev_handle;
  356. dev_ino = virt_irq_table[virt_irq].dev_ino;
  357. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  358. if (err != HV_EOK)
  359. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  360. "err(%d)\n",
  361. dev_handle, dev_ino, cpuid, err);
  362. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  363. HV_INTR_STATE_IDLE);
  364. if (err != HV_EOK)
  365. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  366. "HV_INTR_STATE_IDLE): err(%d)\n",
  367. dev_handle, dev_ino, err);
  368. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  369. HV_INTR_ENABLED);
  370. if (err != HV_EOK)
  371. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  372. "HV_INTR_ENABLED): err(%d)\n",
  373. dev_handle, dev_ino, err);
  374. }
  375. static int sun4v_virt_set_affinity(unsigned int virt_irq,
  376. const struct cpumask *mask)
  377. {
  378. unsigned long cpuid, dev_handle, dev_ino;
  379. int err;
  380. cpuid = irq_choose_cpu(virt_irq, mask);
  381. dev_handle = virt_irq_table[virt_irq].dev_handle;
  382. dev_ino = virt_irq_table[virt_irq].dev_ino;
  383. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  384. if (err != HV_EOK)
  385. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  386. "err(%d)\n",
  387. dev_handle, dev_ino, cpuid, err);
  388. return 0;
  389. }
  390. static void sun4v_virq_disable(unsigned int virt_irq)
  391. {
  392. unsigned long dev_handle, dev_ino;
  393. int err;
  394. dev_handle = virt_irq_table[virt_irq].dev_handle;
  395. dev_ino = virt_irq_table[virt_irq].dev_ino;
  396. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  397. HV_INTR_DISABLED);
  398. if (err != HV_EOK)
  399. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  400. "HV_INTR_DISABLED): err(%d)\n",
  401. dev_handle, dev_ino, err);
  402. }
  403. static void sun4v_virq_eoi(unsigned int virt_irq)
  404. {
  405. struct irq_desc *desc = irq_desc + virt_irq;
  406. unsigned long dev_handle, dev_ino;
  407. int err;
  408. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  409. return;
  410. dev_handle = virt_irq_table[virt_irq].dev_handle;
  411. dev_ino = virt_irq_table[virt_irq].dev_ino;
  412. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  413. HV_INTR_STATE_IDLE);
  414. if (err != HV_EOK)
  415. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  416. "HV_INTR_STATE_IDLE): err(%d)\n",
  417. dev_handle, dev_ino, err);
  418. }
  419. static struct irq_chip sun4u_irq = {
  420. .name = "sun4u",
  421. .enable = sun4u_irq_enable,
  422. .disable = sun4u_irq_disable,
  423. .eoi = sun4u_irq_eoi,
  424. .set_affinity = sun4u_set_affinity,
  425. };
  426. static struct irq_chip sun4v_irq = {
  427. .name = "sun4v",
  428. .enable = sun4v_irq_enable,
  429. .disable = sun4v_irq_disable,
  430. .eoi = sun4v_irq_eoi,
  431. .set_affinity = sun4v_set_affinity,
  432. };
  433. static struct irq_chip sun4v_virq = {
  434. .name = "vsun4v",
  435. .enable = sun4v_virq_enable,
  436. .disable = sun4v_virq_disable,
  437. .eoi = sun4v_virq_eoi,
  438. .set_affinity = sun4v_virt_set_affinity,
  439. };
  440. static void pre_flow_handler(unsigned int virt_irq,
  441. struct irq_desc *desc)
  442. {
  443. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  444. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  445. data->pre_handler(ino, data->arg1, data->arg2);
  446. handle_fasteoi_irq(virt_irq, desc);
  447. }
  448. void irq_install_pre_handler(int virt_irq,
  449. void (*func)(unsigned int, void *, void *),
  450. void *arg1, void *arg2)
  451. {
  452. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  453. struct irq_desc *desc = irq_desc + virt_irq;
  454. data->pre_handler = func;
  455. data->arg1 = arg1;
  456. data->arg2 = arg2;
  457. desc->handle_irq = pre_flow_handler;
  458. }
  459. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  460. {
  461. struct ino_bucket *bucket;
  462. struct irq_handler_data *data;
  463. unsigned int virt_irq;
  464. int ino;
  465. BUG_ON(tlb_type == hypervisor);
  466. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  467. bucket = &ivector_table[ino];
  468. virt_irq = bucket_get_virt_irq(__pa(bucket));
  469. if (!virt_irq) {
  470. virt_irq = virt_irq_alloc(0, ino);
  471. bucket_set_virt_irq(__pa(bucket), virt_irq);
  472. set_irq_chip_and_handler_name(virt_irq,
  473. &sun4u_irq,
  474. handle_fasteoi_irq,
  475. "IVEC");
  476. }
  477. data = get_irq_chip_data(virt_irq);
  478. if (unlikely(data))
  479. goto out;
  480. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  481. if (unlikely(!data)) {
  482. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  483. prom_halt();
  484. }
  485. set_irq_chip_data(virt_irq, data);
  486. data->imap = imap;
  487. data->iclr = iclr;
  488. out:
  489. return virt_irq;
  490. }
  491. static unsigned int sun4v_build_common(unsigned long sysino,
  492. struct irq_chip *chip)
  493. {
  494. struct ino_bucket *bucket;
  495. struct irq_handler_data *data;
  496. unsigned int virt_irq;
  497. BUG_ON(tlb_type != hypervisor);
  498. bucket = &ivector_table[sysino];
  499. virt_irq = bucket_get_virt_irq(__pa(bucket));
  500. if (!virt_irq) {
  501. virt_irq = virt_irq_alloc(0, sysino);
  502. bucket_set_virt_irq(__pa(bucket), virt_irq);
  503. set_irq_chip_and_handler_name(virt_irq, chip,
  504. handle_fasteoi_irq,
  505. "IVEC");
  506. }
  507. data = get_irq_chip_data(virt_irq);
  508. if (unlikely(data))
  509. goto out;
  510. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  511. if (unlikely(!data)) {
  512. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  513. prom_halt();
  514. }
  515. set_irq_chip_data(virt_irq, data);
  516. /* Catch accidental accesses to these things. IMAP/ICLR handling
  517. * is done by hypervisor calls on sun4v platforms, not by direct
  518. * register accesses.
  519. */
  520. data->imap = ~0UL;
  521. data->iclr = ~0UL;
  522. out:
  523. return virt_irq;
  524. }
  525. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  526. {
  527. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  528. return sun4v_build_common(sysino, &sun4v_irq);
  529. }
  530. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  531. {
  532. struct irq_handler_data *data;
  533. unsigned long hv_err, cookie;
  534. struct ino_bucket *bucket;
  535. struct irq_desc *desc;
  536. unsigned int virt_irq;
  537. bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
  538. if (unlikely(!bucket))
  539. return 0;
  540. __flush_dcache_range((unsigned long) bucket,
  541. ((unsigned long) bucket +
  542. sizeof(struct ino_bucket)));
  543. virt_irq = virt_irq_alloc(devhandle, devino);
  544. bucket_set_virt_irq(__pa(bucket), virt_irq);
  545. set_irq_chip_and_handler_name(virt_irq, &sun4v_virq,
  546. handle_fasteoi_irq,
  547. "IVEC");
  548. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  549. if (unlikely(!data))
  550. return 0;
  551. /* In order to make the LDC channel startup sequence easier,
  552. * especially wrt. locking, we do not let request_irq() enable
  553. * the interrupt.
  554. */
  555. desc = irq_desc + virt_irq;
  556. desc->status |= IRQ_NOAUTOEN;
  557. set_irq_chip_data(virt_irq, data);
  558. /* Catch accidental accesses to these things. IMAP/ICLR handling
  559. * is done by hypervisor calls on sun4v platforms, not by direct
  560. * register accesses.
  561. */
  562. data->imap = ~0UL;
  563. data->iclr = ~0UL;
  564. cookie = ~__pa(bucket);
  565. hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
  566. if (hv_err) {
  567. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  568. "err=%lu\n", devhandle, devino, hv_err);
  569. prom_halt();
  570. }
  571. return virt_irq;
  572. }
  573. void ack_bad_irq(unsigned int virt_irq)
  574. {
  575. unsigned int ino = virt_irq_table[virt_irq].dev_ino;
  576. if (!ino)
  577. ino = 0xdeadbeef;
  578. printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
  579. ino, virt_irq);
  580. }
  581. void *hardirq_stack[NR_CPUS];
  582. void *softirq_stack[NR_CPUS];
  583. static __attribute__((always_inline)) void *set_hardirq_stack(void)
  584. {
  585. void *orig_sp, *sp = hardirq_stack[smp_processor_id()];
  586. __asm__ __volatile__("mov %%sp, %0" : "=r" (orig_sp));
  587. if (orig_sp < sp ||
  588. orig_sp > (sp + THREAD_SIZE)) {
  589. sp += THREAD_SIZE - 192 - STACK_BIAS;
  590. __asm__ __volatile__("mov %0, %%sp" : : "r" (sp));
  591. }
  592. return orig_sp;
  593. }
  594. static __attribute__((always_inline)) void restore_hardirq_stack(void *orig_sp)
  595. {
  596. __asm__ __volatile__("mov %0, %%sp" : : "r" (orig_sp));
  597. }
  598. void __irq_entry handler_irq(int irq, struct pt_regs *regs)
  599. {
  600. unsigned long pstate, bucket_pa;
  601. struct pt_regs *old_regs;
  602. void *orig_sp;
  603. clear_softint(1 << irq);
  604. old_regs = set_irq_regs(regs);
  605. irq_enter();
  606. /* Grab an atomic snapshot of the pending IVECs. */
  607. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  608. "wrpr %0, %3, %%pstate\n\t"
  609. "ldx [%2], %1\n\t"
  610. "stx %%g0, [%2]\n\t"
  611. "wrpr %0, 0x0, %%pstate\n\t"
  612. : "=&r" (pstate), "=&r" (bucket_pa)
  613. : "r" (irq_work_pa(smp_processor_id())),
  614. "i" (PSTATE_IE)
  615. : "memory");
  616. orig_sp = set_hardirq_stack();
  617. while (bucket_pa) {
  618. struct irq_desc *desc;
  619. unsigned long next_pa;
  620. unsigned int virt_irq;
  621. next_pa = bucket_get_chain_pa(bucket_pa);
  622. virt_irq = bucket_get_virt_irq(bucket_pa);
  623. bucket_clear_chain_pa(bucket_pa);
  624. desc = irq_desc + virt_irq;
  625. if (!(desc->status & IRQ_DISABLED))
  626. desc->handle_irq(virt_irq, desc);
  627. bucket_pa = next_pa;
  628. }
  629. restore_hardirq_stack(orig_sp);
  630. irq_exit();
  631. set_irq_regs(old_regs);
  632. }
  633. void do_softirq(void)
  634. {
  635. unsigned long flags;
  636. if (in_interrupt())
  637. return;
  638. local_irq_save(flags);
  639. if (local_softirq_pending()) {
  640. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  641. sp += THREAD_SIZE - 192 - STACK_BIAS;
  642. __asm__ __volatile__("mov %%sp, %0\n\t"
  643. "mov %1, %%sp"
  644. : "=&r" (orig_sp)
  645. : "r" (sp));
  646. __do_softirq();
  647. __asm__ __volatile__("mov %0, %%sp"
  648. : : "r" (orig_sp));
  649. }
  650. local_irq_restore(flags);
  651. }
  652. #ifdef CONFIG_HOTPLUG_CPU
  653. void fixup_irqs(void)
  654. {
  655. unsigned int irq;
  656. for (irq = 0; irq < NR_IRQS; irq++) {
  657. unsigned long flags;
  658. raw_spin_lock_irqsave(&irq_desc[irq].lock, flags);
  659. if (irq_desc[irq].action &&
  660. !(irq_desc[irq].status & IRQ_PER_CPU)) {
  661. if (irq_desc[irq].chip->set_affinity)
  662. irq_desc[irq].chip->set_affinity(irq,
  663. irq_desc[irq].affinity);
  664. }
  665. raw_spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
  666. }
  667. tick_ops->disable_irq();
  668. }
  669. #endif
  670. struct sun5_timer {
  671. u64 count0;
  672. u64 limit0;
  673. u64 count1;
  674. u64 limit1;
  675. };
  676. static struct sun5_timer *prom_timers;
  677. static u64 prom_limit0, prom_limit1;
  678. static void map_prom_timers(void)
  679. {
  680. struct device_node *dp;
  681. const unsigned int *addr;
  682. /* PROM timer node hangs out in the top level of device siblings... */
  683. dp = of_find_node_by_path("/");
  684. dp = dp->child;
  685. while (dp) {
  686. if (!strcmp(dp->name, "counter-timer"))
  687. break;
  688. dp = dp->sibling;
  689. }
  690. /* Assume if node is not present, PROM uses different tick mechanism
  691. * which we should not care about.
  692. */
  693. if (!dp) {
  694. prom_timers = (struct sun5_timer *) 0;
  695. return;
  696. }
  697. /* If PROM is really using this, it must be mapped by him. */
  698. addr = of_get_property(dp, "address", NULL);
  699. if (!addr) {
  700. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  701. prom_timers = (struct sun5_timer *) 0;
  702. return;
  703. }
  704. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  705. }
  706. static void kill_prom_timer(void)
  707. {
  708. if (!prom_timers)
  709. return;
  710. /* Save them away for later. */
  711. prom_limit0 = prom_timers->limit0;
  712. prom_limit1 = prom_timers->limit1;
  713. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  714. * We turn both off here just to be paranoid.
  715. */
  716. prom_timers->limit0 = 0;
  717. prom_timers->limit1 = 0;
  718. /* Wheee, eat the interrupt packet too... */
  719. __asm__ __volatile__(
  720. " mov 0x40, %%g2\n"
  721. " ldxa [%%g0] %0, %%g1\n"
  722. " ldxa [%%g2] %1, %%g1\n"
  723. " stxa %%g0, [%%g0] %0\n"
  724. " membar #Sync\n"
  725. : /* no outputs */
  726. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  727. : "g1", "g2");
  728. }
  729. void notrace init_irqwork_curcpu(void)
  730. {
  731. int cpu = hard_smp_processor_id();
  732. trap_block[cpu].irq_worklist_pa = 0UL;
  733. }
  734. /* Please be very careful with register_one_mondo() and
  735. * sun4v_register_mondo_queues().
  736. *
  737. * On SMP this gets invoked from the CPU trampoline before
  738. * the cpu has fully taken over the trap table from OBP,
  739. * and it's kernel stack + %g6 thread register state is
  740. * not fully cooked yet.
  741. *
  742. * Therefore you cannot make any OBP calls, not even prom_printf,
  743. * from these two routines.
  744. */
  745. static void __cpuinit notrace register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  746. {
  747. unsigned long num_entries = (qmask + 1) / 64;
  748. unsigned long status;
  749. status = sun4v_cpu_qconf(type, paddr, num_entries);
  750. if (status != HV_EOK) {
  751. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  752. "err %lu\n", type, paddr, num_entries, status);
  753. prom_halt();
  754. }
  755. }
  756. void __cpuinit notrace sun4v_register_mondo_queues(int this_cpu)
  757. {
  758. struct trap_per_cpu *tb = &trap_block[this_cpu];
  759. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  760. tb->cpu_mondo_qmask);
  761. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  762. tb->dev_mondo_qmask);
  763. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  764. tb->resum_qmask);
  765. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  766. tb->nonresum_qmask);
  767. }
  768. /* Each queue region must be a power of 2 multiple of 64 bytes in
  769. * size. The base real address must be aligned to the size of the
  770. * region. Thus, an 8KB queue must be 8KB aligned, for example.
  771. */
  772. static void __init alloc_one_queue(unsigned long *pa_ptr, unsigned long qmask)
  773. {
  774. unsigned long size = PAGE_ALIGN(qmask + 1);
  775. unsigned long order = get_order(size);
  776. unsigned long p;
  777. p = __get_free_pages(GFP_KERNEL, order);
  778. if (!p) {
  779. prom_printf("SUN4V: Error, cannot allocate queue.\n");
  780. prom_halt();
  781. }
  782. *pa_ptr = __pa(p);
  783. }
  784. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  785. {
  786. #ifdef CONFIG_SMP
  787. unsigned long page;
  788. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  789. page = get_zeroed_page(GFP_KERNEL);
  790. if (!page) {
  791. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  792. prom_halt();
  793. }
  794. tb->cpu_mondo_block_pa = __pa(page);
  795. tb->cpu_list_pa = __pa(page + 64);
  796. #endif
  797. }
  798. /* Allocate mondo and error queues for all possible cpus. */
  799. static void __init sun4v_init_mondo_queues(void)
  800. {
  801. int cpu;
  802. for_each_possible_cpu(cpu) {
  803. struct trap_per_cpu *tb = &trap_block[cpu];
  804. alloc_one_queue(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  805. alloc_one_queue(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  806. alloc_one_queue(&tb->resum_mondo_pa, tb->resum_qmask);
  807. alloc_one_queue(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  808. alloc_one_queue(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  809. alloc_one_queue(&tb->nonresum_kernel_buf_pa,
  810. tb->nonresum_qmask);
  811. }
  812. }
  813. static void __init init_send_mondo_info(void)
  814. {
  815. int cpu;
  816. for_each_possible_cpu(cpu) {
  817. struct trap_per_cpu *tb = &trap_block[cpu];
  818. init_cpu_send_mondo_info(tb);
  819. }
  820. }
  821. static struct irqaction timer_irq_action = {
  822. .name = "timer",
  823. };
  824. /* Only invoked on boot processor. */
  825. void __init init_IRQ(void)
  826. {
  827. unsigned long size;
  828. map_prom_timers();
  829. kill_prom_timer();
  830. size = sizeof(struct ino_bucket) * NUM_IVECS;
  831. ivector_table = kzalloc(size, GFP_KERNEL);
  832. if (!ivector_table) {
  833. prom_printf("Fatal error, cannot allocate ivector_table\n");
  834. prom_halt();
  835. }
  836. __flush_dcache_range((unsigned long) ivector_table,
  837. ((unsigned long) ivector_table) + size);
  838. ivector_table_pa = __pa(ivector_table);
  839. if (tlb_type == hypervisor)
  840. sun4v_init_mondo_queues();
  841. init_send_mondo_info();
  842. if (tlb_type == hypervisor) {
  843. /* Load up the boot cpu's entries. */
  844. sun4v_register_mondo_queues(hard_smp_processor_id());
  845. }
  846. /* We need to clear any IRQ's pending in the soft interrupt
  847. * registers, a spurious one could be left around from the
  848. * PROM timer which we just disabled.
  849. */
  850. clear_softint(get_softint());
  851. /* Now that ivector table is initialized, it is safe
  852. * to receive IRQ vector traps. We will normally take
  853. * one or two right now, in case some device PROM used
  854. * to boot us wants to speak to us. We just ignore them.
  855. */
  856. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  857. "or %%g1, %0, %%g1\n\t"
  858. "wrpr %%g1, 0x0, %%pstate"
  859. : /* No outputs */
  860. : "i" (PSTATE_IE)
  861. : "g1");
  862. irq_desc[0].action = &timer_irq_action;
  863. }