irq.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  758. {
  759. #ifdef CONFIG_SMP
  760. unsigned long page;
  761. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  762. page = get_zeroed_page(GFP_ATOMIC);
  763. if (!page) {
  764. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  765. prom_halt();
  766. }
  767. tb->cpu_mondo_block_pa = __pa(page);
  768. tb->cpu_list_pa = __pa(page + 64);
  769. #endif
  770. }
  771. /* Allocate and init the mondo and error queues for this cpu. */
  772. void __cpuinit sun4v_init_mondo_queues(void)
  773. {
  774. int cpu = hard_smp_processor_id();
  775. struct trap_per_cpu *tb = &trap_block[cpu];
  776. init_one_mondo(&tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
  777. init_one_mondo(&tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
  778. init_one_mondo(&tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
  779. init_one_kbuf(&tb->resum_kernel_buf_pa);
  780. init_one_mondo(&tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
  781. init_one_kbuf(&tb->nonresum_kernel_buf_pa);
  782. init_cpu_send_mondo_info(tb);
  783. }
  784. /* Only invoked on boot processor. */
  785. void __init init_IRQ(void)
  786. {
  787. map_prom_timers();
  788. kill_prom_timer();
  789. memset(&ivector_table[0], 0, sizeof(ivector_table));
  790. if (tlb_type == hypervisor)
  791. sun4v_init_mondo_queues();
  792. /* We need to clear any IRQ's pending in the soft interrupt
  793. * registers, a spurious one could be left around from the
  794. * PROM timer which we just disabled.
  795. */
  796. clear_softint(get_softint());
  797. /* Now that ivector table is initialized, it is safe
  798. * to receive IRQ vector traps. We will normally take
  799. * one or two right now, in case some device PROM used
  800. * to boot us wants to speak to us. We just ignore them.
  801. */
  802. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  803. "or %%g1, %0, %%g1\n\t"
  804. "wrpr %%g1, 0x0, %%pstate"
  805. : /* No outputs */
  806. : "i" (PSTATE_IE)
  807. : "g1");
  808. }
  809. static struct proc_dir_entry * root_irq_dir;
  810. static struct proc_dir_entry * irq_dir [NUM_IVECS];
  811. #ifdef CONFIG_SMP
  812. static int irq_affinity_read_proc (char *page, char **start, off_t off,
  813. int count, int *eof, void *data)
  814. {
  815. struct ino_bucket *bp = ivector_table + (long)data;
  816. struct irq_desc *desc = bp->irq_info;
  817. struct irqaction *ap = desc->action;
  818. cpumask_t mask;
  819. int len;
  820. mask = get_smpaff_in_irqaction(ap);
  821. if (cpus_empty(mask))
  822. mask = cpu_online_map;
  823. len = cpumask_scnprintf(page, count, mask);
  824. if (count - len < 2)
  825. return -EINVAL;
  826. len += sprintf(page + len, "\n");
  827. return len;
  828. }
  829. static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
  830. {
  831. struct ino_bucket *bp = ivector_table + irq;
  832. struct irq_desc *desc = bp->irq_info;
  833. struct irqaction *ap = desc->action;
  834. /* Users specify affinity in terms of hw cpu ids.
  835. * As soon as we do this, handler_irq() might see and take action.
  836. */
  837. put_smpaff_in_irqaction(ap, hw_aff);
  838. /* Migration is simply done by the next cpu to service this
  839. * interrupt.
  840. */
  841. }
  842. static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
  843. unsigned long count, void *data)
  844. {
  845. int irq = (long) data, full_count = count, err;
  846. cpumask_t new_value;
  847. err = cpumask_parse(buffer, count, new_value);
  848. /*
  849. * Do not allow disabling IRQs completely - it's a too easy
  850. * way to make the system unusable accidentally :-) At least
  851. * one online CPU still has to be targeted.
  852. */
  853. cpus_and(new_value, new_value, cpu_online_map);
  854. if (cpus_empty(new_value))
  855. return -EINVAL;
  856. set_intr_affinity(irq, new_value);
  857. return full_count;
  858. }
  859. #endif
  860. #define MAX_NAMELEN 10
  861. static void register_irq_proc (unsigned int irq)
  862. {
  863. char name [MAX_NAMELEN];
  864. if (!root_irq_dir || irq_dir[irq])
  865. return;
  866. memset(name, 0, MAX_NAMELEN);
  867. sprintf(name, "%x", irq);
  868. /* create /proc/irq/1234 */
  869. irq_dir[irq] = proc_mkdir(name, root_irq_dir);
  870. #ifdef CONFIG_SMP
  871. /* XXX SMP affinity not supported on starfire yet. */
  872. if (this_is_starfire == 0) {
  873. struct proc_dir_entry *entry;
  874. /* create /proc/irq/1234/smp_affinity */
  875. entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
  876. if (entry) {
  877. entry->nlink = 1;
  878. entry->data = (void *)(long)irq;
  879. entry->read_proc = irq_affinity_read_proc;
  880. entry->write_proc = irq_affinity_write_proc;
  881. }
  882. }
  883. #endif
  884. }
  885. void init_irq_proc (void)
  886. {
  887. /* create /proc/irq */
  888. root_irq_dir = proc_mkdir("irq", NULL);
  889. }