irq.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /* $Id: irq.c,v 1.114 2002/01/11 08:45:38 davem Exp $
  2. * irq.c: UltraSparc IRQ handling/init/registry.
  3. *
  4. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
  7. */
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/signal.h>
  15. #include <linux/mm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/slab.h>
  18. #include <linux/random.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. #include <asm/ptrace.h>
  24. #include <asm/processor.h>
  25. #include <asm/atomic.h>
  26. #include <asm/system.h>
  27. #include <asm/irq.h>
  28. #include <asm/io.h>
  29. #include <asm/sbus.h>
  30. #include <asm/iommu.h>
  31. #include <asm/upa.h>
  32. #include <asm/oplib.h>
  33. #include <asm/timer.h>
  34. #include <asm/smp.h>
  35. #include <asm/starfire.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/cache.h>
  38. #include <asm/cpudata.h>
  39. #include <asm/auxio.h>
  40. #include <asm/head.h>
  41. #ifdef CONFIG_SMP
  42. static void distribute_irqs(void);
  43. #endif
  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. struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
  57. /* This has to be in the main kernel image, it cannot be
  58. * turned into per-cpu data. The reason is that the main
  59. * kernel image is locked into the TLB and this structure
  60. * is accessed from the vectored interrupt trap handler. If
  61. * access to this structure takes a TLB miss it could cause
  62. * the 5-level sparc v9 trap stack to overflow.
  63. */
  64. struct irq_work_struct {
  65. unsigned int irq_worklists[16];
  66. };
  67. struct irq_work_struct __irq_work[NR_CPUS];
  68. #define irq_work(__cpu, __pil) &(__irq_work[(__cpu)].irq_worklists[(__pil)])
  69. static struct irqaction *irq_action[NR_IRQS+1];
  70. /* This only synchronizes entities which modify IRQ handler
  71. * state and some selected user-level spots that want to
  72. * read things in the table. IRQ handler processing orders
  73. * its' accesses such that no locking is needed.
  74. */
  75. static DEFINE_SPINLOCK(irq_action_lock);
  76. static void register_irq_proc (unsigned int irq);
  77. /*
  78. * Upper 2b of irqaction->flags holds the ino.
  79. * irqaction->mask holds the smp affinity information.
  80. */
  81. #define put_ino_in_irqaction(action, irq) \
  82. action->flags &= 0xffffffffffffUL; \
  83. if (__bucket(irq) == &pil0_dummy_bucket) \
  84. action->flags |= 0xdeadUL << 48; \
  85. else \
  86. action->flags |= __irq_ino(irq) << 48;
  87. #define get_ino_in_irqaction(action) (action->flags >> 48)
  88. #define put_smpaff_in_irqaction(action, smpaff) (action)->mask = (smpaff)
  89. #define get_smpaff_in_irqaction(action) ((action)->mask)
  90. int show_interrupts(struct seq_file *p, void *v)
  91. {
  92. unsigned long flags;
  93. int i = *(loff_t *) v;
  94. struct irqaction *action;
  95. #ifdef CONFIG_SMP
  96. int j;
  97. #endif
  98. spin_lock_irqsave(&irq_action_lock, flags);
  99. if (i <= NR_IRQS) {
  100. if (!(action = *(i + irq_action)))
  101. goto out_unlock;
  102. seq_printf(p, "%3d: ", i);
  103. #ifndef CONFIG_SMP
  104. seq_printf(p, "%10u ", kstat_irqs(i));
  105. #else
  106. for (j = 0; j < NR_CPUS; j++) {
  107. if (!cpu_online(j))
  108. continue;
  109. seq_printf(p, "%10u ",
  110. kstat_cpu(j).irqs[i]);
  111. }
  112. #endif
  113. seq_printf(p, " %s:%lx", action->name,
  114. get_ino_in_irqaction(action));
  115. for (action = action->next; action; action = action->next) {
  116. seq_printf(p, ", %s:%lx", action->name,
  117. get_ino_in_irqaction(action));
  118. }
  119. seq_putc(p, '\n');
  120. }
  121. out_unlock:
  122. spin_unlock_irqrestore(&irq_action_lock, flags);
  123. return 0;
  124. }
  125. /* Now these are always passed a true fully specified sun4u INO. */
  126. void enable_irq(unsigned int irq)
  127. {
  128. struct ino_bucket *bucket = __bucket(irq);
  129. unsigned long imap;
  130. unsigned long tid;
  131. imap = bucket->imap;
  132. if (imap == 0UL)
  133. return;
  134. preempt_disable();
  135. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  136. unsigned long ver;
  137. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  138. if ((ver >> 32) == __JALAPENO_ID ||
  139. (ver >> 32) == __SERRANO_ID) {
  140. /* We set it to our JBUS ID. */
  141. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  142. : "=r" (tid)
  143. : "i" (ASI_JBUS_CONFIG));
  144. tid = ((tid & (0x1fUL<<17)) << 9);
  145. tid &= IMAP_TID_JBUS;
  146. } else {
  147. /* We set it to our Safari AID. */
  148. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  149. : "=r" (tid)
  150. : "i" (ASI_SAFARI_CONFIG));
  151. tid = ((tid & (0x3ffUL<<17)) << 9);
  152. tid &= IMAP_AID_SAFARI;
  153. }
  154. } else if (this_is_starfire == 0) {
  155. /* We set it to our UPA MID. */
  156. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  157. : "=r" (tid)
  158. : "i" (ASI_UPA_CONFIG));
  159. tid = ((tid & UPA_CONFIG_MID) << 9);
  160. tid &= IMAP_TID_UPA;
  161. } else {
  162. tid = (starfire_translate(imap, smp_processor_id()) << 26);
  163. tid &= IMAP_TID_UPA;
  164. }
  165. /* NOTE NOTE NOTE, IGN and INO are read-only, IGN is a product
  166. * of this SYSIO's preconfigured IGN in the SYSIO Control
  167. * Register, the hardware just mirrors that value here.
  168. * However for Graphics and UPA Slave devices the full
  169. * IMAP_INR field can be set by the programmer here.
  170. *
  171. * Things like FFB can now be handled via the new IRQ mechanism.
  172. */
  173. upa_writel(tid | IMAP_VALID, imap);
  174. preempt_enable();
  175. }
  176. /* This now gets passed true ino's as well. */
  177. void disable_irq(unsigned int irq)
  178. {
  179. struct ino_bucket *bucket = __bucket(irq);
  180. unsigned long imap;
  181. imap = bucket->imap;
  182. if (imap != 0UL) {
  183. u32 tmp;
  184. /* NOTE: We do not want to futz with the IRQ clear registers
  185. * and move the state to IDLE, the SCSI code does call
  186. * disable_irq() to assure atomicity in the queue cmd
  187. * SCSI adapter driver code. Thus we'd lose interrupts.
  188. */
  189. tmp = upa_readl(imap);
  190. tmp &= ~IMAP_VALID;
  191. upa_writel(tmp, imap);
  192. }
  193. }
  194. /* The timer is the one "weird" interrupt which is generated by
  195. * the CPU %tick register and not by some normal vectored interrupt
  196. * source. To handle this special case, we use this dummy INO bucket.
  197. */
  198. static struct irq_desc pil0_dummy_desc;
  199. static struct ino_bucket pil0_dummy_bucket = {
  200. .irq_info = &pil0_dummy_desc,
  201. };
  202. static void build_irq_error(const char *msg, unsigned int ino, int pil, int inofixup,
  203. unsigned long iclr, unsigned long imap,
  204. struct ino_bucket *bucket)
  205. {
  206. prom_printf("IRQ: INO %04x (%d:%016lx:%016lx) --> "
  207. "(%d:%d:%016lx:%016lx), halting...\n",
  208. ino, bucket->pil, bucket->iclr, bucket->imap,
  209. pil, inofixup, iclr, imap);
  210. prom_halt();
  211. }
  212. unsigned int build_irq(int pil, int inofixup, unsigned long iclr, unsigned long imap)
  213. {
  214. struct ino_bucket *bucket;
  215. int ino;
  216. if (pil == 0) {
  217. if (iclr != 0UL || imap != 0UL) {
  218. prom_printf("Invalid dummy bucket for PIL0 (%lx:%lx)\n",
  219. iclr, imap);
  220. prom_halt();
  221. }
  222. return __irq(&pil0_dummy_bucket);
  223. }
  224. /* RULE: Both must be specified in all other cases. */
  225. if (iclr == 0UL || imap == 0UL) {
  226. prom_printf("Invalid build_irq %d %d %016lx %016lx\n",
  227. pil, inofixup, iclr, imap);
  228. prom_halt();
  229. }
  230. ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  231. if (ino > NUM_IVECS) {
  232. prom_printf("Invalid INO %04x (%d:%d:%016lx:%016lx)\n",
  233. ino, pil, inofixup, iclr, imap);
  234. prom_halt();
  235. }
  236. bucket = &ivector_table[ino];
  237. if (bucket->flags & IBF_ACTIVE)
  238. build_irq_error("IRQ: Trying to build active INO bucket.\n",
  239. ino, pil, inofixup, iclr, imap, bucket);
  240. if (bucket->irq_info) {
  241. if (bucket->imap != imap || bucket->iclr != iclr)
  242. build_irq_error("IRQ: Trying to reinit INO bucket.\n",
  243. ino, pil, inofixup, iclr, imap, bucket);
  244. goto out;
  245. }
  246. bucket->irq_info = kmalloc(sizeof(struct irq_desc), GFP_ATOMIC);
  247. if (!bucket->irq_info) {
  248. prom_printf("IRQ: Error, kmalloc(irq_desc) failed.\n");
  249. prom_halt();
  250. }
  251. memset(bucket->irq_info, 0, sizeof(struct irq_desc));
  252. /* Ok, looks good, set it up. Don't touch the irq_chain or
  253. * the pending flag.
  254. */
  255. bucket->imap = imap;
  256. bucket->iclr = iclr;
  257. bucket->pil = pil;
  258. bucket->flags = 0;
  259. out:
  260. return __irq(bucket);
  261. }
  262. static void atomic_bucket_insert(struct ino_bucket *bucket)
  263. {
  264. unsigned long pstate;
  265. unsigned int *ent;
  266. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  267. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  268. : : "r" (pstate), "i" (PSTATE_IE));
  269. ent = irq_work(smp_processor_id(), bucket->pil);
  270. bucket->irq_chain = *ent;
  271. *ent = __irq(bucket);
  272. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  273. }
  274. static int check_irq_sharing(int pil, unsigned long irqflags)
  275. {
  276. struct irqaction *action, *tmp;
  277. action = *(irq_action + pil);
  278. if (action) {
  279. if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
  280. for (tmp = action; tmp->next; tmp = tmp->next)
  281. ;
  282. } else {
  283. return -EBUSY;
  284. }
  285. }
  286. return 0;
  287. }
  288. static void append_irq_action(int pil, struct irqaction *action)
  289. {
  290. struct irqaction **pp = irq_action + pil;
  291. while (*pp)
  292. pp = &((*pp)->next);
  293. *pp = action;
  294. }
  295. static struct irqaction *get_action_slot(struct ino_bucket *bucket)
  296. {
  297. struct irq_desc *desc = bucket->irq_info;
  298. int max_irq, i;
  299. max_irq = 1;
  300. if (bucket->flags & IBF_PCI)
  301. max_irq = MAX_IRQ_DESC_ACTION;
  302. for (i = 0; i < max_irq; i++) {
  303. struct irqaction *p = &desc->action[i];
  304. u32 mask = (1 << i);
  305. if (desc->action_active_mask & mask)
  306. continue;
  307. desc->action_active_mask |= mask;
  308. return p;
  309. }
  310. return NULL;
  311. }
  312. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  313. unsigned long irqflags, const char *name, void *dev_id)
  314. {
  315. struct irqaction *action;
  316. struct ino_bucket *bucket = __bucket(irq);
  317. unsigned long flags;
  318. int pending = 0;
  319. if (unlikely(!handler))
  320. return -EINVAL;
  321. if (unlikely(!bucket->irq_info))
  322. return -ENODEV;
  323. if ((bucket != &pil0_dummy_bucket) && (irqflags & SA_SAMPLE_RANDOM)) {
  324. /*
  325. * This function might sleep, we want to call it first,
  326. * outside of the atomic block. In SA_STATIC_ALLOC case,
  327. * random driver's kmalloc will fail, but it is safe.
  328. * If already initialized, random driver will not reinit.
  329. * Yes, this might clear the entropy pool if the wrong
  330. * driver is attempted to be loaded, without actually
  331. * installing a new handler, but is this really a problem,
  332. * only the sysadmin is able to do this.
  333. */
  334. rand_initialize_irq(irq);
  335. }
  336. spin_lock_irqsave(&irq_action_lock, flags);
  337. if (check_irq_sharing(bucket->pil, irqflags)) {
  338. spin_unlock_irqrestore(&irq_action_lock, flags);
  339. return -EBUSY;
  340. }
  341. action = get_action_slot(bucket);
  342. if (!action) {
  343. spin_unlock_irqrestore(&irq_action_lock, flags);
  344. return -ENOMEM;
  345. }
  346. bucket->flags |= IBF_ACTIVE;
  347. pending = 0;
  348. if (bucket != &pil0_dummy_bucket) {
  349. pending = bucket->pending;
  350. if (pending)
  351. bucket->pending = 0;
  352. }
  353. action->handler = handler;
  354. action->flags = irqflags;
  355. action->name = name;
  356. action->next = NULL;
  357. action->dev_id = dev_id;
  358. put_ino_in_irqaction(action, irq);
  359. put_smpaff_in_irqaction(action, CPU_MASK_NONE);
  360. append_irq_action(bucket->pil, action);
  361. enable_irq(irq);
  362. /* We ate the IVEC already, this makes sure it does not get lost. */
  363. if (pending) {
  364. atomic_bucket_insert(bucket);
  365. set_softint(1 << bucket->pil);
  366. }
  367. spin_unlock_irqrestore(&irq_action_lock, flags);
  368. if (bucket != &pil0_dummy_bucket)
  369. register_irq_proc(__irq_ino(irq));
  370. #ifdef CONFIG_SMP
  371. distribute_irqs();
  372. #endif
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(request_irq);
  376. static struct irqaction *unlink_irq_action(unsigned int irq, void *dev_id)
  377. {
  378. struct ino_bucket *bucket = __bucket(irq);
  379. struct irqaction *action, **pp;
  380. pp = irq_action + bucket->pil;
  381. action = *pp;
  382. if (unlikely(!action))
  383. return NULL;
  384. if (unlikely(!action->handler)) {
  385. printk("Freeing free IRQ %d\n", bucket->pil);
  386. return NULL;
  387. }
  388. while (action && action->dev_id != dev_id) {
  389. pp = &action->next;
  390. action = *pp;
  391. }
  392. if (likely(action))
  393. *pp = action->next;
  394. return action;
  395. }
  396. void free_irq(unsigned int irq, void *dev_id)
  397. {
  398. struct irqaction *action;
  399. struct ino_bucket *bucket;
  400. unsigned long flags;
  401. spin_lock_irqsave(&irq_action_lock, flags);
  402. action = unlink_irq_action(irq, dev_id);
  403. spin_unlock_irqrestore(&irq_action_lock, flags);
  404. if (unlikely(!action))
  405. return;
  406. synchronize_irq(irq);
  407. spin_lock_irqsave(&irq_action_lock, flags);
  408. bucket = __bucket(irq);
  409. if (bucket != &pil0_dummy_bucket) {
  410. struct irq_desc *desc = bucket->irq_info;
  411. unsigned long imap = bucket->imap;
  412. int ent, i;
  413. for (i = 0; i < MAX_IRQ_DESC_ACTION; i++) {
  414. struct irqaction *p = &desc->action[i];
  415. if (p == action) {
  416. desc->action_active_mask &= ~(1 << i);
  417. break;
  418. }
  419. }
  420. if (!desc->action_active_mask) {
  421. /* This unique interrupt source is now inactive. */
  422. bucket->flags &= ~IBF_ACTIVE;
  423. /* See if any other buckets share this bucket's IMAP
  424. * and are still active.
  425. */
  426. for (ent = 0; ent < NUM_IVECS; ent++) {
  427. struct ino_bucket *bp = &ivector_table[ent];
  428. if (bp != bucket &&
  429. bp->imap == imap &&
  430. (bp->flags & IBF_ACTIVE) != 0)
  431. break;
  432. }
  433. /* Only disable when no other sub-irq levels of
  434. * the same IMAP are active.
  435. */
  436. if (ent == NUM_IVECS)
  437. disable_irq(irq);
  438. }
  439. }
  440. spin_unlock_irqrestore(&irq_action_lock, flags);
  441. }
  442. EXPORT_SYMBOL(free_irq);
  443. #ifdef CONFIG_SMP
  444. void synchronize_irq(unsigned int irq)
  445. {
  446. struct ino_bucket *bucket = __bucket(irq);
  447. #if 0
  448. /* The following is how I wish I could implement this.
  449. * Unfortunately the ICLR registers are read-only, you can
  450. * only write ICLR_foo values to them. To get the current
  451. * IRQ status you would need to get at the IRQ diag registers
  452. * in the PCI/SBUS controller and the layout of those vary
  453. * from one controller to the next, sigh... -DaveM
  454. */
  455. unsigned long iclr = bucket->iclr;
  456. while (1) {
  457. u32 tmp = upa_readl(iclr);
  458. if (tmp == ICLR_TRANSMIT ||
  459. tmp == ICLR_PENDING) {
  460. cpu_relax();
  461. continue;
  462. }
  463. break;
  464. }
  465. #else
  466. /* So we have to do this with a INPROGRESS bit just like x86. */
  467. while (bucket->flags & IBF_INPROGRESS)
  468. cpu_relax();
  469. #endif
  470. }
  471. #endif /* CONFIG_SMP */
  472. static void process_bucket(int irq, struct ino_bucket *bp, struct pt_regs *regs)
  473. {
  474. struct irq_desc *desc = bp->irq_info;
  475. unsigned char flags = bp->flags;
  476. u32 action_mask, i;
  477. int random;
  478. bp->flags |= IBF_INPROGRESS;
  479. if (unlikely(!(flags & IBF_ACTIVE))) {
  480. bp->pending = 1;
  481. goto out;
  482. }
  483. if (desc->pre_handler)
  484. desc->pre_handler(bp,
  485. desc->pre_handler_arg1,
  486. desc->pre_handler_arg2);
  487. action_mask = desc->action_active_mask;
  488. random = 0;
  489. for (i = 0; i < MAX_IRQ_DESC_ACTION; i++) {
  490. struct irqaction *p = &desc->action[i];
  491. u32 mask = (1 << i);
  492. if (!(action_mask & mask))
  493. continue;
  494. action_mask &= ~mask;
  495. if (p->handler(__irq(bp), p->dev_id, regs) == IRQ_HANDLED)
  496. random |= p->flags;
  497. if (!action_mask)
  498. break;
  499. }
  500. if (bp->pil != 0) {
  501. upa_writel(ICLR_IDLE, bp->iclr);
  502. /* Test and add entropy */
  503. if (random & SA_SAMPLE_RANDOM)
  504. add_interrupt_randomness(irq);
  505. }
  506. out:
  507. bp->flags &= ~IBF_INPROGRESS;
  508. }
  509. void handler_irq(int irq, struct pt_regs *regs)
  510. {
  511. struct ino_bucket *bp;
  512. int cpu = smp_processor_id();
  513. #ifndef CONFIG_SMP
  514. /*
  515. * Check for TICK_INT on level 14 softint.
  516. */
  517. {
  518. unsigned long clr_mask = 1 << irq;
  519. unsigned long tick_mask = tick_ops->softint_mask;
  520. if ((irq == 14) && (get_softint() & tick_mask)) {
  521. irq = 0;
  522. clr_mask = tick_mask;
  523. }
  524. clear_softint(clr_mask);
  525. }
  526. #else
  527. clear_softint(1 << irq);
  528. #endif
  529. irq_enter();
  530. kstat_this_cpu.irqs[irq]++;
  531. /* Sliiiick... */
  532. #ifndef CONFIG_SMP
  533. bp = ((irq != 0) ?
  534. __bucket(xchg32(irq_work(cpu, irq), 0)) :
  535. &pil0_dummy_bucket);
  536. #else
  537. bp = __bucket(xchg32(irq_work(cpu, irq), 0));
  538. #endif
  539. while (bp) {
  540. struct ino_bucket *nbp = __bucket(bp->irq_chain);
  541. bp->irq_chain = 0;
  542. process_bucket(irq, bp, regs);
  543. bp = nbp;
  544. }
  545. irq_exit();
  546. }
  547. #ifdef CONFIG_BLK_DEV_FD
  548. extern irqreturn_t floppy_interrupt(int, void *, struct pt_regs *);;
  549. /* XXX No easy way to include asm/floppy.h XXX */
  550. extern unsigned char *pdma_vaddr;
  551. extern unsigned long pdma_size;
  552. extern volatile int doing_pdma;
  553. extern unsigned long fdc_status;
  554. irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie, struct pt_regs *regs)
  555. {
  556. if (likely(doing_pdma)) {
  557. void __iomem *stat = (void __iomem *) fdc_status;
  558. unsigned char *vaddr = pdma_vaddr;
  559. unsigned long size = pdma_size;
  560. u8 val;
  561. while (size) {
  562. val = readb(stat);
  563. if (unlikely(!(val & 0x80))) {
  564. pdma_vaddr = vaddr;
  565. pdma_size = size;
  566. return IRQ_HANDLED;
  567. }
  568. if (unlikely(!(val & 0x20))) {
  569. pdma_vaddr = vaddr;
  570. pdma_size = size;
  571. doing_pdma = 0;
  572. goto main_interrupt;
  573. }
  574. if (val & 0x40) {
  575. /* read */
  576. *vaddr++ = readb(stat + 1);
  577. } else {
  578. unsigned char data = *vaddr++;
  579. /* write */
  580. writeb(data, stat + 1);
  581. }
  582. size--;
  583. }
  584. pdma_vaddr = vaddr;
  585. pdma_size = size;
  586. /* Send Terminal Count pulse to floppy controller. */
  587. val = readb(auxio_register);
  588. val |= AUXIO_AUX1_FTCNT;
  589. writeb(val, auxio_register);
  590. val &= ~AUXIO_AUX1_FTCNT;
  591. writeb(val, auxio_register);
  592. doing_pdma = 0;
  593. }
  594. main_interrupt:
  595. return floppy_interrupt(irq, dev_cookie, regs);
  596. }
  597. EXPORT_SYMBOL(sparc_floppy_irq);
  598. #endif
  599. /* We really don't need these at all on the Sparc. We only have
  600. * stubs here because they are exported to modules.
  601. */
  602. unsigned long probe_irq_on(void)
  603. {
  604. return 0;
  605. }
  606. EXPORT_SYMBOL(probe_irq_on);
  607. int probe_irq_off(unsigned long mask)
  608. {
  609. return 0;
  610. }
  611. EXPORT_SYMBOL(probe_irq_off);
  612. #ifdef CONFIG_SMP
  613. static int retarget_one_irq(struct irqaction *p, int goal_cpu)
  614. {
  615. struct ino_bucket *bucket = get_ino_in_irqaction(p) + ivector_table;
  616. unsigned long imap = bucket->imap;
  617. unsigned int tid;
  618. while (!cpu_online(goal_cpu)) {
  619. if (++goal_cpu >= NR_CPUS)
  620. goal_cpu = 0;
  621. }
  622. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  623. tid = goal_cpu << 26;
  624. tid &= IMAP_AID_SAFARI;
  625. } else if (this_is_starfire == 0) {
  626. tid = goal_cpu << 26;
  627. tid &= IMAP_TID_UPA;
  628. } else {
  629. tid = (starfire_translate(imap, goal_cpu) << 26);
  630. tid &= IMAP_TID_UPA;
  631. }
  632. upa_writel(tid | IMAP_VALID, imap);
  633. do {
  634. if (++goal_cpu >= NR_CPUS)
  635. goal_cpu = 0;
  636. } while (!cpu_online(goal_cpu));
  637. return goal_cpu;
  638. }
  639. /* Called from request_irq. */
  640. static void distribute_irqs(void)
  641. {
  642. unsigned long flags;
  643. int cpu, level;
  644. spin_lock_irqsave(&irq_action_lock, flags);
  645. cpu = 0;
  646. /*
  647. * Skip the timer at [0], and very rare error/power intrs at [15].
  648. * Also level [12], it causes problems on Ex000 systems.
  649. */
  650. for (level = 1; level < NR_IRQS; level++) {
  651. struct irqaction *p = irq_action[level];
  652. if (level == 12)
  653. continue;
  654. while(p) {
  655. cpu = retarget_one_irq(p, cpu);
  656. p = p->next;
  657. }
  658. }
  659. spin_unlock_irqrestore(&irq_action_lock, flags);
  660. }
  661. #endif
  662. struct sun5_timer {
  663. u64 count0;
  664. u64 limit0;
  665. u64 count1;
  666. u64 limit1;
  667. };
  668. static struct sun5_timer *prom_timers;
  669. static u64 prom_limit0, prom_limit1;
  670. static void map_prom_timers(void)
  671. {
  672. unsigned int addr[3];
  673. int tnode, err;
  674. /* PROM timer node hangs out in the top level of device siblings... */
  675. tnode = prom_finddevice("/counter-timer");
  676. /* Assume if node is not present, PROM uses different tick mechanism
  677. * which we should not care about.
  678. */
  679. if (tnode == 0 || tnode == -1) {
  680. prom_timers = (struct sun5_timer *) 0;
  681. return;
  682. }
  683. /* If PROM is really using this, it must be mapped by him. */
  684. err = prom_getproperty(tnode, "address", (char *)addr, sizeof(addr));
  685. if (err == -1) {
  686. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  687. prom_timers = (struct sun5_timer *) 0;
  688. return;
  689. }
  690. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  691. }
  692. static void kill_prom_timer(void)
  693. {
  694. if (!prom_timers)
  695. return;
  696. /* Save them away for later. */
  697. prom_limit0 = prom_timers->limit0;
  698. prom_limit1 = prom_timers->limit1;
  699. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  700. * We turn both off here just to be paranoid.
  701. */
  702. prom_timers->limit0 = 0;
  703. prom_timers->limit1 = 0;
  704. /* Wheee, eat the interrupt packet too... */
  705. __asm__ __volatile__(
  706. " mov 0x40, %%g2\n"
  707. " ldxa [%%g0] %0, %%g1\n"
  708. " ldxa [%%g2] %1, %%g1\n"
  709. " stxa %%g0, [%%g0] %0\n"
  710. " membar #Sync\n"
  711. : /* no outputs */
  712. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  713. : "g1", "g2");
  714. }
  715. void init_irqwork_curcpu(void)
  716. {
  717. int cpu = hard_smp_processor_id();
  718. memset(__irq_work + cpu, 0, sizeof(struct irq_work_struct));
  719. }
  720. static void __cpuinit init_one_mondo(unsigned long *pa_ptr, unsigned long type)
  721. {
  722. register unsigned long func __asm__("%o0");
  723. register unsigned long arg0 __asm__("%o1");
  724. register unsigned long arg1 __asm__("%o2");
  725. register unsigned long arg2 __asm__("%o3");
  726. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  727. if (!page) {
  728. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  729. prom_halt();
  730. }
  731. *pa_ptr = __pa(page);
  732. func = HV_FAST_CPU_QCONF;
  733. arg0 = type;
  734. arg1 = *pa_ptr;
  735. arg2 = 128; /* XXX Implied by Niagara queue offsets. XXX */
  736. __asm__ __volatile__("ta %8"
  737. : "=&r" (func), "=&r" (arg0),
  738. "=&r" (arg1), "=&r" (arg2)
  739. : "0" (func), "1" (arg0),
  740. "2" (arg1), "3" (arg2),
  741. "i" (HV_FAST_TRAP));
  742. if (func != HV_EOK) {
  743. prom_printf("SUN4V: cpu_qconf(%lu) failed with error %lu\n",
  744. type, func);
  745. prom_halt();
  746. }
  747. }
  748. static void __cpuinit init_one_kbuf(unsigned long *pa_ptr)
  749. {
  750. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  751. if (!page) {
  752. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  753. prom_halt();
  754. }
  755. *pa_ptr = __pa(page);
  756. }
  757. /* Allocate and init the mondo and error queues for this cpu. */
  758. void __cpuinit sun4v_init_mondo_queues(void)
  759. {
  760. int cpu = hard_smp_processor_id();
  761. struct trap_per_cpu *tb = &trap_block[cpu];
  762. init_one_mondo(&tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
  763. init_one_mondo(&tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
  764. init_one_mondo(&tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
  765. init_one_kbuf(&tb->resum_kernel_buf_pa);
  766. init_one_mondo(&tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
  767. init_one_kbuf(&tb->nonresum_kernel_buf_pa);
  768. }
  769. /* Only invoked on boot processor. */
  770. void __init init_IRQ(void)
  771. {
  772. map_prom_timers();
  773. kill_prom_timer();
  774. memset(&ivector_table[0], 0, sizeof(ivector_table));
  775. if (tlb_type == hypervisor)
  776. sun4v_init_mondo_queues();
  777. /* We need to clear any IRQ's pending in the soft interrupt
  778. * registers, a spurious one could be left around from the
  779. * PROM timer which we just disabled.
  780. */
  781. clear_softint(get_softint());
  782. /* Now that ivector table is initialized, it is safe
  783. * to receive IRQ vector traps. We will normally take
  784. * one or two right now, in case some device PROM used
  785. * to boot us wants to speak to us. We just ignore them.
  786. */
  787. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  788. "or %%g1, %0, %%g1\n\t"
  789. "wrpr %%g1, 0x0, %%pstate"
  790. : /* No outputs */
  791. : "i" (PSTATE_IE)
  792. : "g1");
  793. }
  794. static struct proc_dir_entry * root_irq_dir;
  795. static struct proc_dir_entry * irq_dir [NUM_IVECS];
  796. #ifdef CONFIG_SMP
  797. static int irq_affinity_read_proc (char *page, char **start, off_t off,
  798. int count, int *eof, void *data)
  799. {
  800. struct ino_bucket *bp = ivector_table + (long)data;
  801. struct irq_desc *desc = bp->irq_info;
  802. struct irqaction *ap = desc->action;
  803. cpumask_t mask;
  804. int len;
  805. mask = get_smpaff_in_irqaction(ap);
  806. if (cpus_empty(mask))
  807. mask = cpu_online_map;
  808. len = cpumask_scnprintf(page, count, mask);
  809. if (count - len < 2)
  810. return -EINVAL;
  811. len += sprintf(page + len, "\n");
  812. return len;
  813. }
  814. static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
  815. {
  816. struct ino_bucket *bp = ivector_table + irq;
  817. struct irq_desc *desc = bp->irq_info;
  818. struct irqaction *ap = desc->action;
  819. /* Users specify affinity in terms of hw cpu ids.
  820. * As soon as we do this, handler_irq() might see and take action.
  821. */
  822. put_smpaff_in_irqaction(ap, hw_aff);
  823. /* Migration is simply done by the next cpu to service this
  824. * interrupt.
  825. */
  826. }
  827. static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
  828. unsigned long count, void *data)
  829. {
  830. int irq = (long) data, full_count = count, err;
  831. cpumask_t new_value;
  832. err = cpumask_parse(buffer, count, new_value);
  833. /*
  834. * Do not allow disabling IRQs completely - it's a too easy
  835. * way to make the system unusable accidentally :-) At least
  836. * one online CPU still has to be targeted.
  837. */
  838. cpus_and(new_value, new_value, cpu_online_map);
  839. if (cpus_empty(new_value))
  840. return -EINVAL;
  841. set_intr_affinity(irq, new_value);
  842. return full_count;
  843. }
  844. #endif
  845. #define MAX_NAMELEN 10
  846. static void register_irq_proc (unsigned int irq)
  847. {
  848. char name [MAX_NAMELEN];
  849. if (!root_irq_dir || irq_dir[irq])
  850. return;
  851. memset(name, 0, MAX_NAMELEN);
  852. sprintf(name, "%x", irq);
  853. /* create /proc/irq/1234 */
  854. irq_dir[irq] = proc_mkdir(name, root_irq_dir);
  855. #ifdef CONFIG_SMP
  856. /* XXX SMP affinity not supported on starfire yet. */
  857. if (this_is_starfire == 0) {
  858. struct proc_dir_entry *entry;
  859. /* create /proc/irq/1234/smp_affinity */
  860. entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
  861. if (entry) {
  862. entry->nlink = 1;
  863. entry->data = (void *)(long)irq;
  864. entry->read_proc = irq_affinity_read_proc;
  865. entry->write_proc = irq_affinity_write_proc;
  866. }
  867. }
  868. #endif
  869. }
  870. void init_irq_proc (void)
  871. {
  872. /* create /proc/irq */
  873. root_irq_dir = proc_mkdir("irq", NULL);
  874. }