irq.c 27 KB

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