irq.c 24 KB

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