irq.c 28 KB

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