irq.c 26 KB

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