irq_64.c 25 KB

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