irq.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /* irq.c: UltraSparc IRQ handling/init/registry.
  2. *
  3. * Copyright (C) 1997, 2007 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/ptrace.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/signal.h>
  13. #include <linux/mm.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/random.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/irq.h>
  23. #include <linux/msi.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/processor.h>
  26. #include <asm/atomic.h>
  27. #include <asm/system.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/sbus.h>
  31. #include <asm/iommu.h>
  32. #include <asm/upa.h>
  33. #include <asm/oplib.h>
  34. #include <asm/prom.h>
  35. #include <asm/timer.h>
  36. #include <asm/smp.h>
  37. #include <asm/starfire.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/cache.h>
  40. #include <asm/cpudata.h>
  41. #include <asm/auxio.h>
  42. #include <asm/head.h>
  43. #include <asm/hypervisor.h>
  44. /* UPA nodes send interrupt packet to UltraSparc with first data reg
  45. * value low 5 (7 on Starfire) bits holding the IRQ identifier being
  46. * delivered. We must translate this into a non-vector IRQ so we can
  47. * set the softint on this cpu.
  48. *
  49. * To make processing these packets efficient and race free we use
  50. * an array of irq buckets below. The interrupt vector handler in
  51. * entry.S feeds incoming packets into per-cpu pil-indexed lists.
  52. * The IVEC handler does not need to act atomically, the PIL dispatch
  53. * code uses CAS to get an atomic snapshot of the list and clear it
  54. * at the same time.
  55. *
  56. * If you make changes to ino_bucket, please update hand coded assembler
  57. * of the vectored interrupt trap handler(s) in entry.S and sun4v_ivec.S
  58. */
  59. struct ino_bucket {
  60. /* Next handler in per-CPU IRQ worklist. We know that
  61. * bucket pointers have the high 32-bits clear, so to
  62. * save space we only store the bits we need.
  63. */
  64. /*0x00*/unsigned int irq_chain;
  65. /* Virtual interrupt number assigned to this INO. */
  66. /*0x04*/unsigned int virt_irq;
  67. };
  68. #define NUM_IVECS (IMAP_INR + 1)
  69. struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
  70. #define __irq_ino(irq) \
  71. (((struct ino_bucket *)(unsigned long)(irq)) - &ivector_table[0])
  72. #define __bucket(irq) ((struct ino_bucket *)(unsigned long)(irq))
  73. #define __irq(bucket) ((unsigned int)(unsigned long)(bucket))
  74. /* This has to be in the main kernel image, it cannot be
  75. * turned into per-cpu data. The reason is that the main
  76. * kernel image is locked into the TLB and this structure
  77. * is accessed from the vectored interrupt trap handler. If
  78. * access to this structure takes a TLB miss it could cause
  79. * the 5-level sparc v9 trap stack to overflow.
  80. */
  81. #define irq_work(__cpu) &(trap_block[(__cpu)].irq_worklist)
  82. static struct {
  83. unsigned int irq;
  84. unsigned int dev_handle;
  85. unsigned int dev_ino;
  86. } virt_to_real_irq_table[NR_IRQS];
  87. static unsigned char virt_irq_alloc(unsigned int real_irq)
  88. {
  89. unsigned char ent;
  90. BUILD_BUG_ON(NR_IRQS >= 256);
  91. for (ent = 1; ent < NR_IRQS; ent++) {
  92. if (!virt_to_real_irq_table[ent].irq)
  93. break;
  94. }
  95. if (ent >= NR_IRQS) {
  96. printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
  97. return 0;
  98. }
  99. virt_to_real_irq_table[ent].irq = real_irq;
  100. return ent;
  101. }
  102. #ifdef CONFIG_PCI_MSI
  103. static void virt_irq_free(unsigned int virt_irq)
  104. {
  105. unsigned int real_irq;
  106. if (virt_irq >= NR_IRQS)
  107. return;
  108. real_irq = virt_to_real_irq_table[virt_irq].irq;
  109. virt_to_real_irq_table[virt_irq].irq = 0;
  110. __bucket(real_irq)->virt_irq = 0;
  111. }
  112. #endif
  113. static unsigned int virt_to_real_irq(unsigned char virt_irq)
  114. {
  115. return virt_to_real_irq_table[virt_irq].irq;
  116. }
  117. /*
  118. * /proc/interrupts printing:
  119. */
  120. int show_interrupts(struct seq_file *p, void *v)
  121. {
  122. int i = *(loff_t *) v, j;
  123. struct irqaction * action;
  124. unsigned long flags;
  125. if (i == 0) {
  126. seq_printf(p, " ");
  127. for_each_online_cpu(j)
  128. seq_printf(p, "CPU%d ",j);
  129. seq_putc(p, '\n');
  130. }
  131. if (i < NR_IRQS) {
  132. spin_lock_irqsave(&irq_desc[i].lock, flags);
  133. action = irq_desc[i].action;
  134. if (!action)
  135. goto skip;
  136. seq_printf(p, "%3d: ",i);
  137. #ifndef CONFIG_SMP
  138. seq_printf(p, "%10u ", kstat_irqs(i));
  139. #else
  140. for_each_online_cpu(j)
  141. seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
  142. #endif
  143. seq_printf(p, " %9s", irq_desc[i].chip->typename);
  144. seq_printf(p, " %s", action->name);
  145. for (action=action->next; action; action = action->next)
  146. seq_printf(p, ", %s", action->name);
  147. seq_putc(p, '\n');
  148. skip:
  149. spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  150. }
  151. return 0;
  152. }
  153. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  154. {
  155. unsigned int tid;
  156. if (this_is_starfire) {
  157. tid = starfire_translate(imap, cpuid);
  158. tid <<= IMAP_TID_SHIFT;
  159. tid &= IMAP_TID_UPA;
  160. } else {
  161. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  162. unsigned long ver;
  163. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  164. if ((ver >> 32UL) == __JALAPENO_ID ||
  165. (ver >> 32UL) == __SERRANO_ID) {
  166. tid = cpuid << IMAP_TID_SHIFT;
  167. tid &= IMAP_TID_JBUS;
  168. } else {
  169. unsigned int a = cpuid & 0x1f;
  170. unsigned int n = (cpuid >> 5) & 0x1f;
  171. tid = ((a << IMAP_AID_SHIFT) |
  172. (n << IMAP_NID_SHIFT));
  173. tid &= (IMAP_AID_SAFARI |
  174. IMAP_NID_SAFARI);;
  175. }
  176. } else {
  177. tid = cpuid << IMAP_TID_SHIFT;
  178. tid &= IMAP_TID_UPA;
  179. }
  180. }
  181. return tid;
  182. }
  183. struct irq_handler_data {
  184. unsigned long iclr;
  185. unsigned long imap;
  186. void (*pre_handler)(unsigned int, void *, void *);
  187. void *pre_handler_arg1;
  188. void *pre_handler_arg2;
  189. u32 msi;
  190. };
  191. void sparc64_set_msi(unsigned int virt_irq, u32 msi)
  192. {
  193. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  194. if (data)
  195. data->msi = msi;
  196. }
  197. u32 sparc64_get_msi(unsigned int virt_irq)
  198. {
  199. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  200. if (data)
  201. return data->msi;
  202. return 0xffffffff;
  203. }
  204. static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
  205. {
  206. unsigned int real_irq = virt_to_real_irq(virt_irq);
  207. struct ino_bucket *bucket = NULL;
  208. if (likely(real_irq))
  209. bucket = __bucket(real_irq);
  210. return bucket;
  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. }
  264. }
  265. static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
  266. {
  267. sun4u_irq_enable(virt_irq);
  268. }
  269. static void sun4u_irq_disable(unsigned int virt_irq)
  270. {
  271. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  272. if (likely(data)) {
  273. unsigned long imap = data->imap;
  274. unsigned long tmp = upa_readq(imap);
  275. tmp &= ~IMAP_VALID;
  276. upa_writeq(tmp, imap);
  277. }
  278. }
  279. static void sun4u_irq_end(unsigned int virt_irq)
  280. {
  281. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  282. struct irq_desc *desc = irq_desc + virt_irq;
  283. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  284. return;
  285. if (likely(data))
  286. upa_writeq(ICLR_IDLE, data->iclr);
  287. }
  288. static void sun4v_irq_enable(unsigned int virt_irq)
  289. {
  290. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  291. unsigned int ino = bucket - &ivector_table[0];
  292. if (likely(bucket)) {
  293. unsigned long cpuid;
  294. int err;
  295. cpuid = irq_choose_cpu(virt_irq);
  296. err = sun4v_intr_settarget(ino, cpuid);
  297. if (err != HV_EOK)
  298. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  299. "err(%d)\n", ino, cpuid, err);
  300. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  301. if (err != HV_EOK)
  302. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  303. "err(%d)\n", ino, err);
  304. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  305. if (err != HV_EOK)
  306. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  307. ino, err);
  308. }
  309. }
  310. static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
  311. {
  312. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  313. unsigned int ino = bucket - &ivector_table[0];
  314. if (likely(bucket)) {
  315. unsigned long cpuid;
  316. int err;
  317. cpuid = irq_choose_cpu(virt_irq);
  318. err = sun4v_intr_settarget(ino, cpuid);
  319. if (err != HV_EOK)
  320. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  321. "err(%d)\n", ino, cpuid, err);
  322. }
  323. }
  324. static void sun4v_irq_disable(unsigned int virt_irq)
  325. {
  326. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  327. unsigned int ino = bucket - &ivector_table[0];
  328. if (likely(bucket)) {
  329. int err;
  330. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  331. if (err != HV_EOK)
  332. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  333. "err(%d)\n", ino, err);
  334. }
  335. }
  336. #ifdef CONFIG_PCI_MSI
  337. static void sun4v_msi_enable(unsigned int virt_irq)
  338. {
  339. sun4v_irq_enable(virt_irq);
  340. unmask_msi_irq(virt_irq);
  341. }
  342. static void sun4v_msi_disable(unsigned int virt_irq)
  343. {
  344. mask_msi_irq(virt_irq);
  345. sun4v_irq_disable(virt_irq);
  346. }
  347. #endif
  348. static void sun4v_irq_end(unsigned int virt_irq)
  349. {
  350. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  351. unsigned int ino = bucket - &ivector_table[0];
  352. struct irq_desc *desc = irq_desc + virt_irq;
  353. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  354. return;
  355. if (likely(bucket)) {
  356. int err;
  357. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  358. if (err != HV_EOK)
  359. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  360. "err(%d)\n", ino, err);
  361. }
  362. }
  363. static void sun4v_virq_enable(unsigned int virt_irq)
  364. {
  365. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  366. if (likely(bucket)) {
  367. unsigned long cpuid, dev_handle, dev_ino;
  368. int err;
  369. cpuid = irq_choose_cpu(virt_irq);
  370. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  371. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  372. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  373. if (err != HV_EOK)
  374. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  375. "err(%d)\n",
  376. dev_handle, dev_ino, cpuid, err);
  377. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  378. HV_INTR_STATE_IDLE);
  379. if (err != HV_EOK)
  380. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  381. "HV_INTR_STATE_IDLE): err(%d)\n",
  382. dev_handle, dev_ino, err);
  383. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  384. HV_INTR_ENABLED);
  385. if (err != HV_EOK)
  386. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  387. "HV_INTR_ENABLED): err(%d)\n",
  388. dev_handle, dev_ino, err);
  389. }
  390. }
  391. static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
  392. {
  393. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  394. if (likely(bucket)) {
  395. unsigned long cpuid, dev_handle, dev_ino;
  396. int err;
  397. cpuid = irq_choose_cpu(virt_irq);
  398. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  399. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  400. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  401. if (err != HV_EOK)
  402. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  403. "err(%d)\n",
  404. dev_handle, dev_ino, cpuid, err);
  405. }
  406. }
  407. static void sun4v_virq_disable(unsigned int virt_irq)
  408. {
  409. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  410. if (likely(bucket)) {
  411. unsigned long dev_handle, dev_ino;
  412. int err;
  413. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  414. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  415. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  416. HV_INTR_DISABLED);
  417. if (err != HV_EOK)
  418. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  419. "HV_INTR_DISABLED): err(%d)\n",
  420. dev_handle, dev_ino, err);
  421. }
  422. }
  423. static void sun4v_virq_end(unsigned int virt_irq)
  424. {
  425. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  426. struct irq_desc *desc = irq_desc + virt_irq;
  427. if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  428. return;
  429. if (likely(bucket)) {
  430. unsigned long dev_handle, dev_ino;
  431. int err;
  432. dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
  433. dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
  434. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  435. HV_INTR_STATE_IDLE);
  436. if (err != HV_EOK)
  437. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  438. "HV_INTR_STATE_IDLE): err(%d)\n",
  439. dev_handle, dev_ino, err);
  440. }
  441. }
  442. static void run_pre_handler(unsigned int virt_irq)
  443. {
  444. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  445. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  446. if (likely(data->pre_handler)) {
  447. data->pre_handler(__irq_ino(__irq(bucket)),
  448. data->pre_handler_arg1,
  449. data->pre_handler_arg2);
  450. }
  451. }
  452. static struct irq_chip sun4u_irq = {
  453. .typename = "sun4u",
  454. .enable = sun4u_irq_enable,
  455. .disable = sun4u_irq_disable,
  456. .end = sun4u_irq_end,
  457. .set_affinity = sun4u_set_affinity,
  458. };
  459. static struct irq_chip sun4u_irq_ack = {
  460. .typename = "sun4u+ack",
  461. .enable = sun4u_irq_enable,
  462. .disable = sun4u_irq_disable,
  463. .ack = run_pre_handler,
  464. .end = sun4u_irq_end,
  465. .set_affinity = sun4u_set_affinity,
  466. };
  467. static struct irq_chip sun4v_irq = {
  468. .typename = "sun4v",
  469. .enable = sun4v_irq_enable,
  470. .disable = sun4v_irq_disable,
  471. .end = sun4v_irq_end,
  472. .set_affinity = sun4v_set_affinity,
  473. };
  474. static struct irq_chip sun4v_irq_ack = {
  475. .typename = "sun4v+ack",
  476. .enable = sun4v_irq_enable,
  477. .disable = sun4v_irq_disable,
  478. .ack = run_pre_handler,
  479. .end = sun4v_irq_end,
  480. .set_affinity = sun4v_set_affinity,
  481. };
  482. #ifdef CONFIG_PCI_MSI
  483. static struct irq_chip sun4v_msi = {
  484. .typename = "sun4v+msi",
  485. .mask = mask_msi_irq,
  486. .unmask = unmask_msi_irq,
  487. .enable = sun4v_msi_enable,
  488. .disable = sun4v_msi_disable,
  489. .ack = run_pre_handler,
  490. .end = sun4v_irq_end,
  491. .set_affinity = sun4v_set_affinity,
  492. };
  493. #endif
  494. static struct irq_chip sun4v_virq = {
  495. .typename = "vsun4v",
  496. .enable = sun4v_virq_enable,
  497. .disable = sun4v_virq_disable,
  498. .end = sun4v_virq_end,
  499. .set_affinity = sun4v_virt_set_affinity,
  500. };
  501. static struct irq_chip sun4v_virq_ack = {
  502. .typename = "vsun4v+ack",
  503. .enable = sun4v_virq_enable,
  504. .disable = sun4v_virq_disable,
  505. .ack = run_pre_handler,
  506. .end = sun4v_virq_end,
  507. .set_affinity = sun4v_virt_set_affinity,
  508. };
  509. void irq_install_pre_handler(int virt_irq,
  510. void (*func)(unsigned int, void *, void *),
  511. void *arg1, void *arg2)
  512. {
  513. struct irq_handler_data *data = get_irq_chip_data(virt_irq);
  514. struct irq_chip *chip;
  515. data->pre_handler = func;
  516. data->pre_handler_arg1 = arg1;
  517. data->pre_handler_arg2 = arg2;
  518. chip = get_irq_chip(virt_irq);
  519. if (chip == &sun4u_irq_ack ||
  520. chip == &sun4v_irq_ack ||
  521. chip == &sun4v_virq_ack
  522. #ifdef CONFIG_PCI_MSI
  523. || chip == &sun4v_msi
  524. #endif
  525. )
  526. return;
  527. chip = (chip == &sun4u_irq ?
  528. &sun4u_irq_ack :
  529. (chip == &sun4v_irq ?
  530. &sun4v_irq_ack : &sun4v_virq_ack));
  531. set_irq_chip(virt_irq, chip);
  532. }
  533. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  534. {
  535. struct ino_bucket *bucket;
  536. struct irq_handler_data *data;
  537. int ino;
  538. BUG_ON(tlb_type == hypervisor);
  539. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  540. bucket = &ivector_table[ino];
  541. if (!bucket->virt_irq) {
  542. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  543. set_irq_chip(bucket->virt_irq, &sun4u_irq);
  544. }
  545. data = get_irq_chip_data(bucket->virt_irq);
  546. if (unlikely(data))
  547. goto out;
  548. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  549. if (unlikely(!data)) {
  550. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  551. prom_halt();
  552. }
  553. set_irq_chip_data(bucket->virt_irq, data);
  554. data->imap = imap;
  555. data->iclr = iclr;
  556. out:
  557. return bucket->virt_irq;
  558. }
  559. static unsigned int sun4v_build_common(unsigned long sysino,
  560. struct irq_chip *chip)
  561. {
  562. struct ino_bucket *bucket;
  563. struct irq_handler_data *data;
  564. BUG_ON(tlb_type != hypervisor);
  565. bucket = &ivector_table[sysino];
  566. if (!bucket->virt_irq) {
  567. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  568. set_irq_chip(bucket->virt_irq, chip);
  569. }
  570. data = get_irq_chip_data(bucket->virt_irq);
  571. if (unlikely(data))
  572. goto out;
  573. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  574. if (unlikely(!data)) {
  575. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  576. prom_halt();
  577. }
  578. set_irq_chip_data(bucket->virt_irq, data);
  579. /* Catch accidental accesses to these things. IMAP/ICLR handling
  580. * is done by hypervisor calls on sun4v platforms, not by direct
  581. * register accesses.
  582. */
  583. data->imap = ~0UL;
  584. data->iclr = ~0UL;
  585. out:
  586. return bucket->virt_irq;
  587. }
  588. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  589. {
  590. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  591. return sun4v_build_common(sysino, &sun4v_irq);
  592. }
  593. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  594. {
  595. unsigned long sysino, hv_err;
  596. unsigned int virq;
  597. BUG_ON(devhandle & devino);
  598. sysino = devhandle | devino;
  599. BUG_ON(sysino & ~(IMAP_IGN | IMAP_INO));
  600. hv_err = sun4v_vintr_set_cookie(devhandle, devino, sysino);
  601. if (hv_err) {
  602. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  603. "err=%lu\n", devhandle, devino, hv_err);
  604. prom_halt();
  605. }
  606. virq = sun4v_build_common(sysino, &sun4v_virq);
  607. virt_to_real_irq_table[virq].dev_handle = devhandle;
  608. virt_to_real_irq_table[virq].dev_ino = devino;
  609. return virq;
  610. }
  611. #ifdef CONFIG_PCI_MSI
  612. unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
  613. unsigned int msi_start, unsigned int msi_end)
  614. {
  615. struct ino_bucket *bucket;
  616. struct irq_handler_data *data;
  617. unsigned long sysino;
  618. unsigned int devino;
  619. BUG_ON(tlb_type != hypervisor);
  620. /* Find a free devino in the given range. */
  621. for (devino = msi_start; devino < msi_end; devino++) {
  622. sysino = sun4v_devino_to_sysino(devhandle, devino);
  623. bucket = &ivector_table[sysino];
  624. if (!bucket->virt_irq)
  625. break;
  626. }
  627. if (devino >= msi_end)
  628. return -ENOSPC;
  629. sysino = sun4v_devino_to_sysino(devhandle, devino);
  630. bucket = &ivector_table[sysino];
  631. bucket->virt_irq = virt_irq_alloc(__irq(bucket));
  632. *virt_irq_p = bucket->virt_irq;
  633. set_irq_chip(bucket->virt_irq, &sun4v_msi);
  634. data = get_irq_chip_data(bucket->virt_irq);
  635. if (unlikely(data))
  636. return devino;
  637. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  638. if (unlikely(!data)) {
  639. virt_irq_free(*virt_irq_p);
  640. return -ENOMEM;
  641. }
  642. set_irq_chip_data(bucket->virt_irq, data);
  643. data->imap = ~0UL;
  644. data->iclr = ~0UL;
  645. return devino;
  646. }
  647. void sun4v_destroy_msi(unsigned int virt_irq)
  648. {
  649. virt_irq_free(virt_irq);
  650. }
  651. #endif
  652. void ack_bad_irq(unsigned int virt_irq)
  653. {
  654. struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
  655. unsigned int ino = 0xdeadbeef;
  656. if (bucket)
  657. ino = bucket - &ivector_table[0];
  658. printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
  659. ino, virt_irq);
  660. }
  661. void handler_irq(int irq, struct pt_regs *regs)
  662. {
  663. struct ino_bucket *bucket;
  664. struct pt_regs *old_regs;
  665. clear_softint(1 << irq);
  666. old_regs = set_irq_regs(regs);
  667. irq_enter();
  668. /* Sliiiick... */
  669. bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
  670. while (bucket) {
  671. struct ino_bucket *next = __bucket(bucket->irq_chain);
  672. bucket->irq_chain = 0;
  673. __do_IRQ(bucket->virt_irq);
  674. bucket = next;
  675. }
  676. irq_exit();
  677. set_irq_regs(old_regs);
  678. }
  679. #ifdef CONFIG_HOTPLUG_CPU
  680. void fixup_irqs(void)
  681. {
  682. unsigned int irq;
  683. for (irq = 0; irq < NR_IRQS; irq++) {
  684. unsigned long flags;
  685. spin_lock_irqsave(&irq_desc[irq].lock, flags);
  686. if (irq_desc[irq].action &&
  687. !(irq_desc[irq].status & IRQ_PER_CPU)) {
  688. if (irq_desc[irq].chip->set_affinity)
  689. irq_desc[irq].chip->set_affinity(irq,
  690. irq_desc[irq].affinity);
  691. }
  692. spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
  693. }
  694. }
  695. #endif
  696. struct sun5_timer {
  697. u64 count0;
  698. u64 limit0;
  699. u64 count1;
  700. u64 limit1;
  701. };
  702. static struct sun5_timer *prom_timers;
  703. static u64 prom_limit0, prom_limit1;
  704. static void map_prom_timers(void)
  705. {
  706. struct device_node *dp;
  707. const unsigned int *addr;
  708. /* PROM timer node hangs out in the top level of device siblings... */
  709. dp = of_find_node_by_path("/");
  710. dp = dp->child;
  711. while (dp) {
  712. if (!strcmp(dp->name, "counter-timer"))
  713. break;
  714. dp = dp->sibling;
  715. }
  716. /* Assume if node is not present, PROM uses different tick mechanism
  717. * which we should not care about.
  718. */
  719. if (!dp) {
  720. prom_timers = (struct sun5_timer *) 0;
  721. return;
  722. }
  723. /* If PROM is really using this, it must be mapped by him. */
  724. addr = of_get_property(dp, "address", NULL);
  725. if (!addr) {
  726. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  727. prom_timers = (struct sun5_timer *) 0;
  728. return;
  729. }
  730. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  731. }
  732. static void kill_prom_timer(void)
  733. {
  734. if (!prom_timers)
  735. return;
  736. /* Save them away for later. */
  737. prom_limit0 = prom_timers->limit0;
  738. prom_limit1 = prom_timers->limit1;
  739. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  740. * We turn both off here just to be paranoid.
  741. */
  742. prom_timers->limit0 = 0;
  743. prom_timers->limit1 = 0;
  744. /* Wheee, eat the interrupt packet too... */
  745. __asm__ __volatile__(
  746. " mov 0x40, %%g2\n"
  747. " ldxa [%%g0] %0, %%g1\n"
  748. " ldxa [%%g2] %1, %%g1\n"
  749. " stxa %%g0, [%%g0] %0\n"
  750. " membar #Sync\n"
  751. : /* no outputs */
  752. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  753. : "g1", "g2");
  754. }
  755. void init_irqwork_curcpu(void)
  756. {
  757. int cpu = hard_smp_processor_id();
  758. trap_block[cpu].irq_worklist = 0;
  759. }
  760. /* Please be very careful with register_one_mondo() and
  761. * sun4v_register_mondo_queues().
  762. *
  763. * On SMP this gets invoked from the CPU trampoline before
  764. * the cpu has fully taken over the trap table from OBP,
  765. * and it's kernel stack + %g6 thread register state is
  766. * not fully cooked yet.
  767. *
  768. * Therefore you cannot make any OBP calls, not even prom_printf,
  769. * from these two routines.
  770. */
  771. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  772. {
  773. unsigned long num_entries = (qmask + 1) / 64;
  774. unsigned long status;
  775. status = sun4v_cpu_qconf(type, paddr, num_entries);
  776. if (status != HV_EOK) {
  777. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  778. "err %lu\n", type, paddr, num_entries, status);
  779. prom_halt();
  780. }
  781. }
  782. void __cpuinit sun4v_register_mondo_queues(int this_cpu)
  783. {
  784. struct trap_per_cpu *tb = &trap_block[this_cpu];
  785. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  786. tb->cpu_mondo_qmask);
  787. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  788. tb->dev_mondo_qmask);
  789. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  790. tb->resum_qmask);
  791. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  792. tb->nonresum_qmask);
  793. }
  794. static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
  795. {
  796. unsigned long size = PAGE_ALIGN(qmask + 1);
  797. void *p = __alloc_bootmem_low(size, size, 0);
  798. if (!p) {
  799. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  800. prom_halt();
  801. }
  802. *pa_ptr = __pa(p);
  803. }
  804. static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
  805. {
  806. unsigned long size = PAGE_ALIGN(qmask + 1);
  807. void *p = __alloc_bootmem_low(size, size, 0);
  808. if (!p) {
  809. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  810. prom_halt();
  811. }
  812. *pa_ptr = __pa(p);
  813. }
  814. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  815. {
  816. #ifdef CONFIG_SMP
  817. void *page;
  818. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  819. page = alloc_bootmem_low_pages(PAGE_SIZE);
  820. if (!page) {
  821. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  822. prom_halt();
  823. }
  824. tb->cpu_mondo_block_pa = __pa(page);
  825. tb->cpu_list_pa = __pa(page + 64);
  826. #endif
  827. }
  828. /* Allocate mondo and error queues for all possible cpus. */
  829. static void __init sun4v_init_mondo_queues(void)
  830. {
  831. int cpu;
  832. for_each_possible_cpu(cpu) {
  833. struct trap_per_cpu *tb = &trap_block[cpu];
  834. alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  835. alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  836. alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
  837. alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  838. alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  839. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
  840. tb->nonresum_qmask);
  841. init_cpu_send_mondo_info(tb);
  842. }
  843. /* Load up the boot cpu's entries. */
  844. sun4v_register_mondo_queues(hard_smp_processor_id());
  845. }
  846. static struct irqaction timer_irq_action = {
  847. .name = "timer",
  848. };
  849. /* Only invoked on boot processor. */
  850. void __init init_IRQ(void)
  851. {
  852. map_prom_timers();
  853. kill_prom_timer();
  854. memset(&ivector_table[0], 0, sizeof(ivector_table));
  855. if (tlb_type == hypervisor)
  856. sun4v_init_mondo_queues();
  857. /* We need to clear any IRQ's pending in the soft interrupt
  858. * registers, a spurious one could be left around from the
  859. * PROM timer which we just disabled.
  860. */
  861. clear_softint(get_softint());
  862. /* Now that ivector table is initialized, it is safe
  863. * to receive IRQ vector traps. We will normally take
  864. * one or two right now, in case some device PROM used
  865. * to boot us wants to speak to us. We just ignore them.
  866. */
  867. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  868. "or %%g1, %0, %%g1\n\t"
  869. "wrpr %%g1, 0x0, %%pstate"
  870. : /* No outputs */
  871. : "i" (PSTATE_IE)
  872. : "g1");
  873. irq_desc[0].action = &timer_irq_action;
  874. }