irq_64.c 26 KB

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