irq.c 28 KB

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