irq.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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/sbus.h>
  29. #include <asm/iommu.h>
  30. #include <asm/upa.h>
  31. #include <asm/oplib.h>
  32. #include <asm/timer.h>
  33. #include <asm/smp.h>
  34. #include <asm/starfire.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/cache.h>
  37. #include <asm/cpudata.h>
  38. #include <asm/auxio.h>
  39. #ifdef CONFIG_SMP
  40. static void distribute_irqs(void);
  41. #endif
  42. /* UPA nodes send interrupt packet to UltraSparc with first data reg
  43. * value low 5 (7 on Starfire) bits holding the IRQ identifier being
  44. * delivered. We must translate this into a non-vector IRQ so we can
  45. * set the softint on this cpu.
  46. *
  47. * To make processing these packets efficient and race free we use
  48. * an array of irq buckets below. The interrupt vector handler in
  49. * entry.S feeds incoming packets into per-cpu pil-indexed lists.
  50. * The IVEC handler does not need to act atomically, the PIL dispatch
  51. * code uses CAS to get an atomic snapshot of the list and clear it
  52. * at the same time.
  53. */
  54. struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
  55. /* This has to be in the main kernel image, it cannot be
  56. * turned into per-cpu data. The reason is that the main
  57. * kernel image is locked into the TLB and this structure
  58. * is accessed from the vectored interrupt trap handler. If
  59. * access to this structure takes a TLB miss it could cause
  60. * the 5-level sparc v9 trap stack to overflow.
  61. */
  62. struct irq_work_struct {
  63. unsigned int irq_worklists[16];
  64. };
  65. struct irq_work_struct __irq_work[NR_CPUS];
  66. #define irq_work(__cpu, __pil) &(__irq_work[(__cpu)].irq_worklists[(__pil)])
  67. #ifdef CONFIG_PCI
  68. /* This is a table of physical addresses used to deal with IBF_DMA_SYNC.
  69. * It is used for PCI only to synchronize DMA transfers with IRQ delivery
  70. * for devices behind busses other than APB on Sabre systems.
  71. *
  72. * Currently these physical addresses are just config space accesses
  73. * to the command register for that device.
  74. */
  75. unsigned long pci_dma_wsync;
  76. unsigned long dma_sync_reg_table[256];
  77. unsigned char dma_sync_reg_table_entry = 0;
  78. #endif
  79. /* This is based upon code in the 32-bit Sparc kernel written mostly by
  80. * David Redman (djhr@tadpole.co.uk).
  81. */
  82. #define MAX_STATIC_ALLOC 4
  83. static struct irqaction static_irqaction[MAX_STATIC_ALLOC];
  84. static int static_irq_count;
  85. /* This is exported so that fast IRQ handlers can get at it... -DaveM */
  86. struct irqaction *irq_action[NR_IRQS+1] = {
  87. NULL, NULL, NULL, NULL, NULL, NULL , NULL, NULL,
  88. NULL, NULL, NULL, NULL, NULL, NULL , NULL, NULL
  89. };
  90. /* This only synchronizes entities which modify IRQ handler
  91. * state and some selected user-level spots that want to
  92. * read things in the table. IRQ handler processing orders
  93. * its' accesses such that no locking is needed.
  94. */
  95. static DEFINE_SPINLOCK(irq_action_lock);
  96. static void register_irq_proc (unsigned int irq);
  97. /*
  98. * Upper 2b of irqaction->flags holds the ino.
  99. * irqaction->mask holds the smp affinity information.
  100. */
  101. #define put_ino_in_irqaction(action, irq) \
  102. action->flags &= 0xffffffffffffUL; \
  103. if (__bucket(irq) == &pil0_dummy_bucket) \
  104. action->flags |= 0xdeadUL << 48; \
  105. else \
  106. action->flags |= __irq_ino(irq) << 48;
  107. #define get_ino_in_irqaction(action) (action->flags >> 48)
  108. #define put_smpaff_in_irqaction(action, smpaff) (action)->mask = (smpaff)
  109. #define get_smpaff_in_irqaction(action) ((action)->mask)
  110. int show_interrupts(struct seq_file *p, void *v)
  111. {
  112. unsigned long flags;
  113. int i = *(loff_t *) v;
  114. struct irqaction *action;
  115. #ifdef CONFIG_SMP
  116. int j;
  117. #endif
  118. spin_lock_irqsave(&irq_action_lock, flags);
  119. if (i <= NR_IRQS) {
  120. if (!(action = *(i + irq_action)))
  121. goto out_unlock;
  122. seq_printf(p, "%3d: ", i);
  123. #ifndef CONFIG_SMP
  124. seq_printf(p, "%10u ", kstat_irqs(i));
  125. #else
  126. for (j = 0; j < NR_CPUS; j++) {
  127. if (!cpu_online(j))
  128. continue;
  129. seq_printf(p, "%10u ",
  130. kstat_cpu(j).irqs[i]);
  131. }
  132. #endif
  133. seq_printf(p, " %s:%lx", action->name,
  134. get_ino_in_irqaction(action));
  135. for (action = action->next; action; action = action->next) {
  136. seq_printf(p, ", %s:%lx", action->name,
  137. get_ino_in_irqaction(action));
  138. }
  139. seq_putc(p, '\n');
  140. }
  141. out_unlock:
  142. spin_unlock_irqrestore(&irq_action_lock, flags);
  143. return 0;
  144. }
  145. /* Now these are always passed a true fully specified sun4u INO. */
  146. void enable_irq(unsigned int irq)
  147. {
  148. struct ino_bucket *bucket = __bucket(irq);
  149. unsigned long imap;
  150. unsigned long tid;
  151. imap = bucket->imap;
  152. if (imap == 0UL)
  153. return;
  154. preempt_disable();
  155. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  156. unsigned long ver;
  157. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  158. if ((ver >> 32) == 0x003e0016) {
  159. /* We set it to our JBUS ID. */
  160. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  161. : "=r" (tid)
  162. : "i" (ASI_JBUS_CONFIG));
  163. tid = ((tid & (0x1fUL<<17)) << 9);
  164. tid &= IMAP_TID_JBUS;
  165. } else {
  166. /* We set it to our Safari AID. */
  167. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  168. : "=r" (tid)
  169. : "i" (ASI_SAFARI_CONFIG));
  170. tid = ((tid & (0x3ffUL<<17)) << 9);
  171. tid &= IMAP_AID_SAFARI;
  172. }
  173. } else if (this_is_starfire == 0) {
  174. /* We set it to our UPA MID. */
  175. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  176. : "=r" (tid)
  177. : "i" (ASI_UPA_CONFIG));
  178. tid = ((tid & UPA_CONFIG_MID) << 9);
  179. tid &= IMAP_TID_UPA;
  180. } else {
  181. tid = (starfire_translate(imap, smp_processor_id()) << 26);
  182. tid &= IMAP_TID_UPA;
  183. }
  184. /* NOTE NOTE NOTE, IGN and INO are read-only, IGN is a product
  185. * of this SYSIO's preconfigured IGN in the SYSIO Control
  186. * Register, the hardware just mirrors that value here.
  187. * However for Graphics and UPA Slave devices the full
  188. * IMAP_INR field can be set by the programmer here.
  189. *
  190. * Things like FFB can now be handled via the new IRQ mechanism.
  191. */
  192. upa_writel(tid | IMAP_VALID, imap);
  193. preempt_enable();
  194. }
  195. /* This now gets passed true ino's as well. */
  196. void disable_irq(unsigned int irq)
  197. {
  198. struct ino_bucket *bucket = __bucket(irq);
  199. unsigned long imap;
  200. imap = bucket->imap;
  201. if (imap != 0UL) {
  202. u32 tmp;
  203. /* NOTE: We do not want to futz with the IRQ clear registers
  204. * and move the state to IDLE, the SCSI code does call
  205. * disable_irq() to assure atomicity in the queue cmd
  206. * SCSI adapter driver code. Thus we'd lose interrupts.
  207. */
  208. tmp = upa_readl(imap);
  209. tmp &= ~IMAP_VALID;
  210. upa_writel(tmp, imap);
  211. }
  212. }
  213. /* The timer is the one "weird" interrupt which is generated by
  214. * the CPU %tick register and not by some normal vectored interrupt
  215. * source. To handle this special case, we use this dummy INO bucket.
  216. */
  217. static struct ino_bucket pil0_dummy_bucket = {
  218. 0, /* irq_chain */
  219. 0, /* pil */
  220. 0, /* pending */
  221. 0, /* flags */
  222. 0, /* __unused */
  223. NULL, /* irq_info */
  224. 0UL, /* iclr */
  225. 0UL, /* imap */
  226. };
  227. unsigned int build_irq(int pil, int inofixup, unsigned long iclr, unsigned long imap)
  228. {
  229. struct ino_bucket *bucket;
  230. int ino;
  231. if (pil == 0) {
  232. if (iclr != 0UL || imap != 0UL) {
  233. prom_printf("Invalid dummy bucket for PIL0 (%lx:%lx)\n",
  234. iclr, imap);
  235. prom_halt();
  236. }
  237. return __irq(&pil0_dummy_bucket);
  238. }
  239. /* RULE: Both must be specified in all other cases. */
  240. if (iclr == 0UL || imap == 0UL) {
  241. prom_printf("Invalid build_irq %d %d %016lx %016lx\n",
  242. pil, inofixup, iclr, imap);
  243. prom_halt();
  244. }
  245. ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  246. if (ino > NUM_IVECS) {
  247. prom_printf("Invalid INO %04x (%d:%d:%016lx:%016lx)\n",
  248. ino, pil, inofixup, iclr, imap);
  249. prom_halt();
  250. }
  251. /* Ok, looks good, set it up. Don't touch the irq_chain or
  252. * the pending flag.
  253. */
  254. bucket = &ivector_table[ino];
  255. if ((bucket->flags & IBF_ACTIVE) ||
  256. (bucket->irq_info != NULL)) {
  257. /* This is a gross fatal error if it happens here. */
  258. prom_printf("IRQ: Trying to reinit INO bucket, fatal error.\n");
  259. prom_printf("IRQ: Request INO %04x (%d:%d:%016lx:%016lx)\n",
  260. ino, pil, inofixup, iclr, imap);
  261. prom_printf("IRQ: Existing (%d:%016lx:%016lx)\n",
  262. bucket->pil, bucket->iclr, bucket->imap);
  263. prom_printf("IRQ: Cannot continue, halting...\n");
  264. prom_halt();
  265. }
  266. bucket->imap = imap;
  267. bucket->iclr = iclr;
  268. bucket->pil = pil;
  269. bucket->flags = 0;
  270. bucket->irq_info = NULL;
  271. return __irq(bucket);
  272. }
  273. static void atomic_bucket_insert(struct ino_bucket *bucket)
  274. {
  275. unsigned long pstate;
  276. unsigned int *ent;
  277. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  278. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  279. : : "r" (pstate), "i" (PSTATE_IE));
  280. ent = irq_work(smp_processor_id(), bucket->pil);
  281. bucket->irq_chain = *ent;
  282. *ent = __irq(bucket);
  283. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  284. }
  285. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  286. unsigned long irqflags, const char *name, void *dev_id)
  287. {
  288. struct irqaction *action, *tmp = NULL;
  289. struct ino_bucket *bucket = __bucket(irq);
  290. unsigned long flags;
  291. int pending = 0;
  292. if ((bucket != &pil0_dummy_bucket) &&
  293. (bucket < &ivector_table[0] ||
  294. bucket >= &ivector_table[NUM_IVECS])) {
  295. unsigned int *caller;
  296. __asm__ __volatile__("mov %%i7, %0" : "=r" (caller));
  297. printk(KERN_CRIT "request_irq: Old style IRQ registry attempt "
  298. "from %p, irq %08x.\n", caller, irq);
  299. return -EINVAL;
  300. }
  301. if (!handler)
  302. return -EINVAL;
  303. if ((bucket != &pil0_dummy_bucket) && (irqflags & SA_SAMPLE_RANDOM)) {
  304. /*
  305. * This function might sleep, we want to call it first,
  306. * outside of the atomic block. In SA_STATIC_ALLOC case,
  307. * random driver's kmalloc will fail, but it is safe.
  308. * If already initialized, random driver will not reinit.
  309. * Yes, this might clear the entropy pool if the wrong
  310. * driver is attempted to be loaded, without actually
  311. * installing a new handler, but is this really a problem,
  312. * only the sysadmin is able to do this.
  313. */
  314. rand_initialize_irq(irq);
  315. }
  316. spin_lock_irqsave(&irq_action_lock, flags);
  317. action = *(bucket->pil + irq_action);
  318. if (action) {
  319. if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ))
  320. for (tmp = action; tmp->next; tmp = tmp->next)
  321. ;
  322. else {
  323. spin_unlock_irqrestore(&irq_action_lock, flags);
  324. return -EBUSY;
  325. }
  326. action = NULL; /* Or else! */
  327. }
  328. /* If this is flagged as statically allocated then we use our
  329. * private struct which is never freed.
  330. */
  331. if (irqflags & SA_STATIC_ALLOC) {
  332. if (static_irq_count < MAX_STATIC_ALLOC)
  333. action = &static_irqaction[static_irq_count++];
  334. else
  335. printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed "
  336. "using kmalloc\n", irq, name);
  337. }
  338. if (action == NULL)
  339. action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
  340. GFP_ATOMIC);
  341. if (!action) {
  342. spin_unlock_irqrestore(&irq_action_lock, flags);
  343. return -ENOMEM;
  344. }
  345. if (bucket == &pil0_dummy_bucket) {
  346. bucket->irq_info = action;
  347. bucket->flags |= IBF_ACTIVE;
  348. } else {
  349. if ((bucket->flags & IBF_ACTIVE) != 0) {
  350. void *orig = bucket->irq_info;
  351. void **vector = NULL;
  352. if ((bucket->flags & IBF_PCI) == 0) {
  353. printk("IRQ: Trying to share non-PCI bucket.\n");
  354. goto free_and_ebusy;
  355. }
  356. if ((bucket->flags & IBF_MULTI) == 0) {
  357. vector = kmalloc(sizeof(void *) * 4, GFP_ATOMIC);
  358. if (vector == NULL)
  359. goto free_and_enomem;
  360. /* We might have slept. */
  361. if ((bucket->flags & IBF_MULTI) != 0) {
  362. int ent;
  363. kfree(vector);
  364. vector = (void **)bucket->irq_info;
  365. for(ent = 0; ent < 4; ent++) {
  366. if (vector[ent] == NULL) {
  367. vector[ent] = action;
  368. break;
  369. }
  370. }
  371. if (ent == 4)
  372. goto free_and_ebusy;
  373. } else {
  374. vector[0] = orig;
  375. vector[1] = action;
  376. vector[2] = NULL;
  377. vector[3] = NULL;
  378. bucket->irq_info = vector;
  379. bucket->flags |= IBF_MULTI;
  380. }
  381. } else {
  382. int ent;
  383. vector = (void **)orig;
  384. for (ent = 0; ent < 4; ent++) {
  385. if (vector[ent] == NULL) {
  386. vector[ent] = action;
  387. break;
  388. }
  389. }
  390. if (ent == 4)
  391. goto free_and_ebusy;
  392. }
  393. } else {
  394. bucket->irq_info = action;
  395. bucket->flags |= IBF_ACTIVE;
  396. }
  397. pending = bucket->pending;
  398. if (pending)
  399. bucket->pending = 0;
  400. }
  401. action->handler = handler;
  402. action->flags = irqflags;
  403. action->name = name;
  404. action->next = NULL;
  405. action->dev_id = dev_id;
  406. put_ino_in_irqaction(action, irq);
  407. put_smpaff_in_irqaction(action, CPU_MASK_NONE);
  408. if (tmp)
  409. tmp->next = action;
  410. else
  411. *(bucket->pil + irq_action) = action;
  412. enable_irq(irq);
  413. /* We ate the IVEC already, this makes sure it does not get lost. */
  414. if (pending) {
  415. atomic_bucket_insert(bucket);
  416. set_softint(1 << bucket->pil);
  417. }
  418. spin_unlock_irqrestore(&irq_action_lock, flags);
  419. if ((bucket != &pil0_dummy_bucket) && (!(irqflags & SA_STATIC_ALLOC)))
  420. register_irq_proc(__irq_ino(irq));
  421. #ifdef CONFIG_SMP
  422. distribute_irqs();
  423. #endif
  424. return 0;
  425. free_and_ebusy:
  426. kfree(action);
  427. spin_unlock_irqrestore(&irq_action_lock, flags);
  428. return -EBUSY;
  429. free_and_enomem:
  430. kfree(action);
  431. spin_unlock_irqrestore(&irq_action_lock, flags);
  432. return -ENOMEM;
  433. }
  434. EXPORT_SYMBOL(request_irq);
  435. void free_irq(unsigned int irq, void *dev_id)
  436. {
  437. struct irqaction *action;
  438. struct irqaction *tmp = NULL;
  439. unsigned long flags;
  440. struct ino_bucket *bucket = __bucket(irq), *bp;
  441. if ((bucket != &pil0_dummy_bucket) &&
  442. (bucket < &ivector_table[0] ||
  443. bucket >= &ivector_table[NUM_IVECS])) {
  444. unsigned int *caller;
  445. __asm__ __volatile__("mov %%i7, %0" : "=r" (caller));
  446. printk(KERN_CRIT "free_irq: Old style IRQ removal attempt "
  447. "from %p, irq %08x.\n", caller, irq);
  448. return;
  449. }
  450. spin_lock_irqsave(&irq_action_lock, flags);
  451. action = *(bucket->pil + irq_action);
  452. if (!action->handler) {
  453. printk("Freeing free IRQ %d\n", bucket->pil);
  454. return;
  455. }
  456. if (dev_id) {
  457. for ( ; action; action = action->next) {
  458. if (action->dev_id == dev_id)
  459. break;
  460. tmp = action;
  461. }
  462. if (!action) {
  463. printk("Trying to free free shared IRQ %d\n", bucket->pil);
  464. spin_unlock_irqrestore(&irq_action_lock, flags);
  465. return;
  466. }
  467. } else if (action->flags & SA_SHIRQ) {
  468. printk("Trying to free shared IRQ %d with NULL device ID\n", bucket->pil);
  469. spin_unlock_irqrestore(&irq_action_lock, flags);
  470. return;
  471. }
  472. if (action->flags & SA_STATIC_ALLOC) {
  473. printk("Attempt to free statically allocated IRQ %d (%s)\n",
  474. bucket->pil, action->name);
  475. spin_unlock_irqrestore(&irq_action_lock, flags);
  476. return;
  477. }
  478. if (action && tmp)
  479. tmp->next = action->next;
  480. else
  481. *(bucket->pil + irq_action) = action->next;
  482. spin_unlock_irqrestore(&irq_action_lock, flags);
  483. synchronize_irq(irq);
  484. spin_lock_irqsave(&irq_action_lock, flags);
  485. if (bucket != &pil0_dummy_bucket) {
  486. unsigned long imap = bucket->imap;
  487. void **vector, *orig;
  488. int ent;
  489. orig = bucket->irq_info;
  490. vector = (void **)orig;
  491. if ((bucket->flags & IBF_MULTI) != 0) {
  492. int other = 0;
  493. void *orphan = NULL;
  494. for (ent = 0; ent < 4; ent++) {
  495. if (vector[ent] == action)
  496. vector[ent] = NULL;
  497. else if (vector[ent] != NULL) {
  498. orphan = vector[ent];
  499. other++;
  500. }
  501. }
  502. /* Only free when no other shared irq
  503. * uses this bucket.
  504. */
  505. if (other) {
  506. if (other == 1) {
  507. /* Convert back to non-shared bucket. */
  508. bucket->irq_info = orphan;
  509. bucket->flags &= ~(IBF_MULTI);
  510. kfree(vector);
  511. }
  512. goto out;
  513. }
  514. } else {
  515. bucket->irq_info = NULL;
  516. }
  517. /* This unique interrupt source is now inactive. */
  518. bucket->flags &= ~IBF_ACTIVE;
  519. /* See if any other buckets share this bucket's IMAP
  520. * and are still active.
  521. */
  522. for (ent = 0; ent < NUM_IVECS; ent++) {
  523. bp = &ivector_table[ent];
  524. if (bp != bucket &&
  525. bp->imap == imap &&
  526. (bp->flags & IBF_ACTIVE) != 0)
  527. break;
  528. }
  529. /* Only disable when no other sub-irq levels of
  530. * the same IMAP are active.
  531. */
  532. if (ent == NUM_IVECS)
  533. disable_irq(irq);
  534. }
  535. out:
  536. kfree(action);
  537. spin_unlock_irqrestore(&irq_action_lock, flags);
  538. }
  539. EXPORT_SYMBOL(free_irq);
  540. #ifdef CONFIG_SMP
  541. void synchronize_irq(unsigned int irq)
  542. {
  543. struct ino_bucket *bucket = __bucket(irq);
  544. #if 0
  545. /* The following is how I wish I could implement this.
  546. * Unfortunately the ICLR registers are read-only, you can
  547. * only write ICLR_foo values to them. To get the current
  548. * IRQ status you would need to get at the IRQ diag registers
  549. * in the PCI/SBUS controller and the layout of those vary
  550. * from one controller to the next, sigh... -DaveM
  551. */
  552. unsigned long iclr = bucket->iclr;
  553. while (1) {
  554. u32 tmp = upa_readl(iclr);
  555. if (tmp == ICLR_TRANSMIT ||
  556. tmp == ICLR_PENDING) {
  557. cpu_relax();
  558. continue;
  559. }
  560. break;
  561. }
  562. #else
  563. /* So we have to do this with a INPROGRESS bit just like x86. */
  564. while (bucket->flags & IBF_INPROGRESS)
  565. cpu_relax();
  566. #endif
  567. }
  568. #endif /* CONFIG_SMP */
  569. void catch_disabled_ivec(struct pt_regs *regs)
  570. {
  571. int cpu = smp_processor_id();
  572. struct ino_bucket *bucket = __bucket(*irq_work(cpu, 0));
  573. /* We can actually see this on Ultra/PCI PCI cards, which are bridges
  574. * to other devices. Here a single IMAP enabled potentially multiple
  575. * unique interrupt sources (which each do have a unique ICLR register.
  576. *
  577. * So what we do is just register that the IVEC arrived, when registered
  578. * for real the request_irq() code will check the bit and signal
  579. * a local CPU interrupt for it.
  580. */
  581. #if 0
  582. printk("IVEC: Spurious interrupt vector (%x) received at (%016lx)\n",
  583. bucket - &ivector_table[0], regs->tpc);
  584. #endif
  585. *irq_work(cpu, 0) = 0;
  586. bucket->pending = 1;
  587. }
  588. /* Tune this... */
  589. #define FORWARD_VOLUME 12
  590. #ifdef CONFIG_SMP
  591. static inline void redirect_intr(int cpu, struct ino_bucket *bp)
  592. {
  593. /* Ok, here is what is going on:
  594. * 1) Retargeting IRQs on Starfire is very
  595. * expensive so just forget about it on them.
  596. * 2) Moving around very high priority interrupts
  597. * is a losing game.
  598. * 3) If the current cpu is idle, interrupts are
  599. * useful work, so keep them here. But do not
  600. * pass to our neighbour if he is not very idle.
  601. * 4) If sysadmin explicitly asks for directed intrs,
  602. * Just Do It.
  603. */
  604. struct irqaction *ap = bp->irq_info;
  605. cpumask_t cpu_mask;
  606. unsigned int buddy, ticks;
  607. cpu_mask = get_smpaff_in_irqaction(ap);
  608. cpus_and(cpu_mask, cpu_mask, cpu_online_map);
  609. if (cpus_empty(cpu_mask))
  610. cpu_mask = cpu_online_map;
  611. if (this_is_starfire != 0 ||
  612. bp->pil >= 10 || current->pid == 0)
  613. goto out;
  614. /* 'cpu' is the MID (ie. UPAID), calculate the MID
  615. * of our buddy.
  616. */
  617. buddy = cpu + 1;
  618. if (buddy >= NR_CPUS)
  619. buddy = 0;
  620. ticks = 0;
  621. while (!cpu_isset(buddy, cpu_mask)) {
  622. if (++buddy >= NR_CPUS)
  623. buddy = 0;
  624. if (++ticks > NR_CPUS) {
  625. put_smpaff_in_irqaction(ap, CPU_MASK_NONE);
  626. goto out;
  627. }
  628. }
  629. if (buddy == cpu)
  630. goto out;
  631. /* Voo-doo programming. */
  632. if (cpu_data(buddy).idle_volume < FORWARD_VOLUME)
  633. goto out;
  634. /* This just so happens to be correct on Cheetah
  635. * at the moment.
  636. */
  637. buddy <<= 26;
  638. /* Push it to our buddy. */
  639. upa_writel(buddy | IMAP_VALID, bp->imap);
  640. out:
  641. return;
  642. }
  643. #endif
  644. void handler_irq(int irq, struct pt_regs *regs)
  645. {
  646. struct ino_bucket *bp, *nbp;
  647. int cpu = smp_processor_id();
  648. #ifndef CONFIG_SMP
  649. /*
  650. * Check for TICK_INT on level 14 softint.
  651. */
  652. {
  653. unsigned long clr_mask = 1 << irq;
  654. unsigned long tick_mask = tick_ops->softint_mask;
  655. if ((irq == 14) && (get_softint() & tick_mask)) {
  656. irq = 0;
  657. clr_mask = tick_mask;
  658. }
  659. clear_softint(clr_mask);
  660. }
  661. #else
  662. int should_forward = 0;
  663. clear_softint(1 << irq);
  664. #endif
  665. irq_enter();
  666. kstat_this_cpu.irqs[irq]++;
  667. /* Sliiiick... */
  668. #ifndef CONFIG_SMP
  669. bp = ((irq != 0) ?
  670. __bucket(xchg32(irq_work(cpu, irq), 0)) :
  671. &pil0_dummy_bucket);
  672. #else
  673. bp = __bucket(xchg32(irq_work(cpu, irq), 0));
  674. #endif
  675. for ( ; bp != NULL; bp = nbp) {
  676. unsigned char flags = bp->flags;
  677. unsigned char random = 0;
  678. nbp = __bucket(bp->irq_chain);
  679. bp->irq_chain = 0;
  680. bp->flags |= IBF_INPROGRESS;
  681. if ((flags & IBF_ACTIVE) != 0) {
  682. #ifdef CONFIG_PCI
  683. if ((flags & IBF_DMA_SYNC) != 0) {
  684. upa_readl(dma_sync_reg_table[bp->synctab_ent]);
  685. upa_readq(pci_dma_wsync);
  686. }
  687. #endif
  688. if ((flags & IBF_MULTI) == 0) {
  689. struct irqaction *ap = bp->irq_info;
  690. int ret;
  691. ret = ap->handler(__irq(bp), ap->dev_id, regs);
  692. if (ret == IRQ_HANDLED)
  693. random |= ap->flags;
  694. } else {
  695. void **vector = (void **)bp->irq_info;
  696. int ent;
  697. for (ent = 0; ent < 4; ent++) {
  698. struct irqaction *ap = vector[ent];
  699. if (ap != NULL) {
  700. int ret;
  701. ret = ap->handler(__irq(bp),
  702. ap->dev_id,
  703. regs);
  704. if (ret == IRQ_HANDLED)
  705. random |= ap->flags;
  706. }
  707. }
  708. }
  709. /* Only the dummy bucket lacks IMAP/ICLR. */
  710. if (bp->pil != 0) {
  711. #ifdef CONFIG_SMP
  712. if (should_forward) {
  713. redirect_intr(cpu, bp);
  714. should_forward = 0;
  715. }
  716. #endif
  717. upa_writel(ICLR_IDLE, bp->iclr);
  718. /* Test and add entropy */
  719. if (random & SA_SAMPLE_RANDOM)
  720. add_interrupt_randomness(irq);
  721. }
  722. } else
  723. bp->pending = 1;
  724. bp->flags &= ~IBF_INPROGRESS;
  725. }
  726. irq_exit();
  727. }
  728. #ifdef CONFIG_BLK_DEV_FD
  729. extern irqreturn_t floppy_interrupt(int, void *, struct pt_regs *);;
  730. /* XXX No easy way to include asm/floppy.h XXX */
  731. extern unsigned char *pdma_vaddr;
  732. extern unsigned long pdma_size;
  733. extern volatile int doing_pdma;
  734. extern unsigned long fdc_status;
  735. irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie, struct pt_regs *regs)
  736. {
  737. if (likely(doing_pdma)) {
  738. void __iomem *stat = (void __iomem *) fdc_status;
  739. unsigned char *vaddr = pdma_vaddr;
  740. unsigned long size = pdma_size;
  741. u8 val;
  742. while (size) {
  743. val = readb(stat);
  744. if (unlikely(!(val & 0x80))) {
  745. pdma_vaddr = vaddr;
  746. pdma_size = size;
  747. return IRQ_HANDLED;
  748. }
  749. if (unlikely(!(val & 0x20))) {
  750. pdma_vaddr = vaddr;
  751. pdma_size = size;
  752. doing_pdma = 0;
  753. goto main_interrupt;
  754. }
  755. if (val & 0x40) {
  756. /* read */
  757. *vaddr++ = readb(stat + 1);
  758. } else {
  759. unsigned char data = *vaddr++;
  760. /* write */
  761. writeb(data, stat + 1);
  762. }
  763. size--;
  764. }
  765. pdma_vaddr = vaddr;
  766. pdma_size = size;
  767. /* Send Terminal Count pulse to floppy controller. */
  768. val = readb(auxio_register);
  769. val |= AUXIO_AUX1_FTCNT;
  770. writeb(val, auxio_register);
  771. val &= AUXIO_AUX1_FTCNT;
  772. writeb(val, auxio_register);
  773. doing_pdma = 0;
  774. }
  775. main_interrupt:
  776. return floppy_interrupt(irq, dev_cookie, regs);
  777. }
  778. EXPORT_SYMBOL(sparc_floppy_irq);
  779. #endif
  780. /* We really don't need these at all on the Sparc. We only have
  781. * stubs here because they are exported to modules.
  782. */
  783. unsigned long probe_irq_on(void)
  784. {
  785. return 0;
  786. }
  787. EXPORT_SYMBOL(probe_irq_on);
  788. int probe_irq_off(unsigned long mask)
  789. {
  790. return 0;
  791. }
  792. EXPORT_SYMBOL(probe_irq_off);
  793. #ifdef CONFIG_SMP
  794. static int retarget_one_irq(struct irqaction *p, int goal_cpu)
  795. {
  796. struct ino_bucket *bucket = get_ino_in_irqaction(p) + ivector_table;
  797. unsigned long imap = bucket->imap;
  798. unsigned int tid;
  799. while (!cpu_online(goal_cpu)) {
  800. if (++goal_cpu >= NR_CPUS)
  801. goal_cpu = 0;
  802. }
  803. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  804. tid = goal_cpu << 26;
  805. tid &= IMAP_AID_SAFARI;
  806. } else if (this_is_starfire == 0) {
  807. tid = goal_cpu << 26;
  808. tid &= IMAP_TID_UPA;
  809. } else {
  810. tid = (starfire_translate(imap, goal_cpu) << 26);
  811. tid &= IMAP_TID_UPA;
  812. }
  813. upa_writel(tid | IMAP_VALID, imap);
  814. do {
  815. if (++goal_cpu >= NR_CPUS)
  816. goal_cpu = 0;
  817. } while (!cpu_online(goal_cpu));
  818. return goal_cpu;
  819. }
  820. /* Called from request_irq. */
  821. static void distribute_irqs(void)
  822. {
  823. unsigned long flags;
  824. int cpu, level;
  825. spin_lock_irqsave(&irq_action_lock, flags);
  826. cpu = 0;
  827. /*
  828. * Skip the timer at [0], and very rare error/power intrs at [15].
  829. * Also level [12], it causes problems on Ex000 systems.
  830. */
  831. for (level = 1; level < NR_IRQS; level++) {
  832. struct irqaction *p = irq_action[level];
  833. if (level == 12) continue;
  834. while(p) {
  835. cpu = retarget_one_irq(p, cpu);
  836. p = p->next;
  837. }
  838. }
  839. spin_unlock_irqrestore(&irq_action_lock, flags);
  840. }
  841. #endif
  842. struct sun5_timer *prom_timers;
  843. static u64 prom_limit0, prom_limit1;
  844. static void map_prom_timers(void)
  845. {
  846. unsigned int addr[3];
  847. int tnode, err;
  848. /* PROM timer node hangs out in the top level of device siblings... */
  849. tnode = prom_finddevice("/counter-timer");
  850. /* Assume if node is not present, PROM uses different tick mechanism
  851. * which we should not care about.
  852. */
  853. if (tnode == 0 || tnode == -1) {
  854. prom_timers = (struct sun5_timer *) 0;
  855. return;
  856. }
  857. /* If PROM is really using this, it must be mapped by him. */
  858. err = prom_getproperty(tnode, "address", (char *)addr, sizeof(addr));
  859. if (err == -1) {
  860. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  861. prom_timers = (struct sun5_timer *) 0;
  862. return;
  863. }
  864. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  865. }
  866. static void kill_prom_timer(void)
  867. {
  868. if (!prom_timers)
  869. return;
  870. /* Save them away for later. */
  871. prom_limit0 = prom_timers->limit0;
  872. prom_limit1 = prom_timers->limit1;
  873. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  874. * We turn both off here just to be paranoid.
  875. */
  876. prom_timers->limit0 = 0;
  877. prom_timers->limit1 = 0;
  878. /* Wheee, eat the interrupt packet too... */
  879. __asm__ __volatile__(
  880. " mov 0x40, %%g2\n"
  881. " ldxa [%%g0] %0, %%g1\n"
  882. " ldxa [%%g2] %1, %%g1\n"
  883. " stxa %%g0, [%%g0] %0\n"
  884. " membar #Sync\n"
  885. : /* no outputs */
  886. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  887. : "g1", "g2");
  888. }
  889. void enable_prom_timer(void)
  890. {
  891. if (!prom_timers)
  892. return;
  893. /* Set it to whatever was there before. */
  894. prom_timers->limit1 = prom_limit1;
  895. prom_timers->count1 = 0;
  896. prom_timers->limit0 = prom_limit0;
  897. prom_timers->count0 = 0;
  898. }
  899. void init_irqwork_curcpu(void)
  900. {
  901. register struct irq_work_struct *workp asm("o2");
  902. register unsigned long tmp asm("o3");
  903. int cpu = hard_smp_processor_id();
  904. memset(__irq_work + cpu, 0, sizeof(*workp));
  905. /* Make sure we are called with PSTATE_IE disabled. */
  906. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  907. : "=r" (tmp));
  908. if (tmp & PSTATE_IE) {
  909. prom_printf("BUG: init_irqwork_curcpu() called with "
  910. "PSTATE_IE enabled, bailing.\n");
  911. __asm__ __volatile__("mov %%i7, %0\n\t"
  912. : "=r" (tmp));
  913. prom_printf("BUG: Called from %lx\n", tmp);
  914. prom_halt();
  915. }
  916. /* Set interrupt globals. */
  917. workp = &__irq_work[cpu];
  918. __asm__ __volatile__(
  919. "rdpr %%pstate, %0\n\t"
  920. "wrpr %0, %1, %%pstate\n\t"
  921. "mov %2, %%g6\n\t"
  922. "wrpr %0, 0x0, %%pstate\n\t"
  923. : "=&r" (tmp)
  924. : "i" (PSTATE_IG), "r" (workp));
  925. }
  926. /* Only invoked on boot processor. */
  927. void __init init_IRQ(void)
  928. {
  929. map_prom_timers();
  930. kill_prom_timer();
  931. memset(&ivector_table[0], 0, sizeof(ivector_table));
  932. /* We need to clear any IRQ's pending in the soft interrupt
  933. * registers, a spurious one could be left around from the
  934. * PROM timer which we just disabled.
  935. */
  936. clear_softint(get_softint());
  937. /* Now that ivector table is initialized, it is safe
  938. * to receive IRQ vector traps. We will normally take
  939. * one or two right now, in case some device PROM used
  940. * to boot us wants to speak to us. We just ignore them.
  941. */
  942. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  943. "or %%g1, %0, %%g1\n\t"
  944. "wrpr %%g1, 0x0, %%pstate"
  945. : /* No outputs */
  946. : "i" (PSTATE_IE)
  947. : "g1");
  948. }
  949. static struct proc_dir_entry * root_irq_dir;
  950. static struct proc_dir_entry * irq_dir [NUM_IVECS];
  951. #ifdef CONFIG_SMP
  952. static int irq_affinity_read_proc (char *page, char **start, off_t off,
  953. int count, int *eof, void *data)
  954. {
  955. struct ino_bucket *bp = ivector_table + (long)data;
  956. struct irqaction *ap = bp->irq_info;
  957. cpumask_t mask;
  958. int len;
  959. mask = get_smpaff_in_irqaction(ap);
  960. if (cpus_empty(mask))
  961. mask = cpu_online_map;
  962. len = cpumask_scnprintf(page, count, mask);
  963. if (count - len < 2)
  964. return -EINVAL;
  965. len += sprintf(page + len, "\n");
  966. return len;
  967. }
  968. static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
  969. {
  970. struct ino_bucket *bp = ivector_table + irq;
  971. /* Users specify affinity in terms of hw cpu ids.
  972. * As soon as we do this, handler_irq() might see and take action.
  973. */
  974. put_smpaff_in_irqaction((struct irqaction *)bp->irq_info, hw_aff);
  975. /* Migration is simply done by the next cpu to service this
  976. * interrupt.
  977. */
  978. }
  979. static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
  980. unsigned long count, void *data)
  981. {
  982. int irq = (long) data, full_count = count, err;
  983. cpumask_t new_value;
  984. err = cpumask_parse(buffer, count, new_value);
  985. /*
  986. * Do not allow disabling IRQs completely - it's a too easy
  987. * way to make the system unusable accidentally :-) At least
  988. * one online CPU still has to be targeted.
  989. */
  990. cpus_and(new_value, new_value, cpu_online_map);
  991. if (cpus_empty(new_value))
  992. return -EINVAL;
  993. set_intr_affinity(irq, new_value);
  994. return full_count;
  995. }
  996. #endif
  997. #define MAX_NAMELEN 10
  998. static void register_irq_proc (unsigned int irq)
  999. {
  1000. char name [MAX_NAMELEN];
  1001. if (!root_irq_dir || irq_dir[irq])
  1002. return;
  1003. memset(name, 0, MAX_NAMELEN);
  1004. sprintf(name, "%x", irq);
  1005. /* create /proc/irq/1234 */
  1006. irq_dir[irq] = proc_mkdir(name, root_irq_dir);
  1007. #ifdef CONFIG_SMP
  1008. /* XXX SMP affinity not supported on starfire yet. */
  1009. if (this_is_starfire == 0) {
  1010. struct proc_dir_entry *entry;
  1011. /* create /proc/irq/1234/smp_affinity */
  1012. entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
  1013. if (entry) {
  1014. entry->nlink = 1;
  1015. entry->data = (void *)(long)irq;
  1016. entry->read_proc = irq_affinity_read_proc;
  1017. entry->write_proc = irq_affinity_write_proc;
  1018. }
  1019. }
  1020. #endif
  1021. }
  1022. void init_irq_proc (void)
  1023. {
  1024. /* create /proc/irq */
  1025. root_irq_dir = proc_mkdir("irq", NULL);
  1026. }