irq.c 28 KB

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