irq.c 28 KB

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