irq.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 (tlb_type == hypervisor) {
  525. unsigned int ino = __irq_ino(bp);
  526. int err;
  527. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  528. if (err != HV_EOK)
  529. printk("sun4v_intr_setstate(%x): "
  530. "err(%d)\n", ino, err);
  531. } else {
  532. upa_writel(ICLR_IDLE, bp->iclr);
  533. }
  534. /* Test and add entropy */
  535. if (random & SA_SAMPLE_RANDOM)
  536. add_interrupt_randomness(bp->pil);
  537. out:
  538. bp->flags &= ~IBF_INPROGRESS;
  539. }
  540. #ifndef CONFIG_SMP
  541. extern irqreturn_t timer_interrupt(int, void *, struct pt_regs *);
  542. void timer_irq(int irq, struct pt_regs *regs)
  543. {
  544. unsigned long clr_mask = 1 << irq;
  545. unsigned long tick_mask = tick_ops->softint_mask;
  546. if (get_softint() & tick_mask) {
  547. irq = 0;
  548. clr_mask = tick_mask;
  549. }
  550. clear_softint(clr_mask);
  551. irq_enter();
  552. kstat_this_cpu.irqs[irq]++;
  553. timer_interrupt(irq, NULL, regs);
  554. irq_exit();
  555. }
  556. #endif
  557. void handler_irq(int irq, struct pt_regs *regs)
  558. {
  559. struct ino_bucket *bp;
  560. int cpu = smp_processor_id();
  561. /* XXX at this point we should be able to assert that
  562. * XXX irq is PIL_DEVICE_IRQ...
  563. */
  564. clear_softint(1 << irq);
  565. irq_enter();
  566. /* Sliiiick... */
  567. bp = __bucket(xchg32(irq_work(cpu), 0));
  568. while (bp) {
  569. struct ino_bucket *nbp = __bucket(bp->irq_chain);
  570. kstat_this_cpu.irqs[bp->pil]++;
  571. bp->irq_chain = 0;
  572. process_bucket(bp, regs);
  573. bp = nbp;
  574. }
  575. irq_exit();
  576. }
  577. #ifdef CONFIG_BLK_DEV_FD
  578. extern irqreturn_t floppy_interrupt(int, void *, struct pt_regs *);
  579. /* XXX No easy way to include asm/floppy.h XXX */
  580. extern unsigned char *pdma_vaddr;
  581. extern unsigned long pdma_size;
  582. extern volatile int doing_pdma;
  583. extern unsigned long fdc_status;
  584. irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie, struct pt_regs *regs)
  585. {
  586. if (likely(doing_pdma)) {
  587. void __iomem *stat = (void __iomem *) fdc_status;
  588. unsigned char *vaddr = pdma_vaddr;
  589. unsigned long size = pdma_size;
  590. u8 val;
  591. while (size) {
  592. val = readb(stat);
  593. if (unlikely(!(val & 0x80))) {
  594. pdma_vaddr = vaddr;
  595. pdma_size = size;
  596. return IRQ_HANDLED;
  597. }
  598. if (unlikely(!(val & 0x20))) {
  599. pdma_vaddr = vaddr;
  600. pdma_size = size;
  601. doing_pdma = 0;
  602. goto main_interrupt;
  603. }
  604. if (val & 0x40) {
  605. /* read */
  606. *vaddr++ = readb(stat + 1);
  607. } else {
  608. unsigned char data = *vaddr++;
  609. /* write */
  610. writeb(data, stat + 1);
  611. }
  612. size--;
  613. }
  614. pdma_vaddr = vaddr;
  615. pdma_size = size;
  616. /* Send Terminal Count pulse to floppy controller. */
  617. val = readb(auxio_register);
  618. val |= AUXIO_AUX1_FTCNT;
  619. writeb(val, auxio_register);
  620. val &= ~AUXIO_AUX1_FTCNT;
  621. writeb(val, auxio_register);
  622. doing_pdma = 0;
  623. }
  624. main_interrupt:
  625. return floppy_interrupt(irq, dev_cookie, regs);
  626. }
  627. EXPORT_SYMBOL(sparc_floppy_irq);
  628. #endif
  629. /* We really don't need these at all on the Sparc. We only have
  630. * stubs here because they are exported to modules.
  631. */
  632. unsigned long probe_irq_on(void)
  633. {
  634. return 0;
  635. }
  636. EXPORT_SYMBOL(probe_irq_on);
  637. int probe_irq_off(unsigned long mask)
  638. {
  639. return 0;
  640. }
  641. EXPORT_SYMBOL(probe_irq_off);
  642. #ifdef CONFIG_SMP
  643. static int retarget_one_irq(struct irqaction *p, int goal_cpu)
  644. {
  645. struct ino_bucket *bucket = get_ino_in_irqaction(p) + ivector_table;
  646. while (!cpu_online(goal_cpu)) {
  647. if (++goal_cpu >= NR_CPUS)
  648. goal_cpu = 0;
  649. }
  650. if (tlb_type == hypervisor) {
  651. unsigned int ino = __irq_ino(bucket);
  652. sun4v_intr_settarget(ino, goal_cpu);
  653. sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  654. } else {
  655. unsigned long imap = bucket->imap;
  656. unsigned int tid = sun4u_compute_tid(imap, goal_cpu);
  657. upa_writel(tid | IMAP_VALID, imap);
  658. }
  659. do {
  660. if (++goal_cpu >= NR_CPUS)
  661. goal_cpu = 0;
  662. } while (!cpu_online(goal_cpu));
  663. return goal_cpu;
  664. }
  665. /* Called from request_irq. */
  666. static void distribute_irqs(void)
  667. {
  668. unsigned long flags;
  669. int cpu, level;
  670. spin_lock_irqsave(&irq_action_lock, flags);
  671. cpu = 0;
  672. /*
  673. * Skip the timer at [0], and very rare error/power intrs at [15].
  674. * Also level [12], it causes problems on Ex000 systems.
  675. */
  676. for (level = 1; level < NR_IRQS; level++) {
  677. struct irqaction *p = irq_action[level];
  678. if (level == 12)
  679. continue;
  680. while(p) {
  681. cpu = retarget_one_irq(p, cpu);
  682. p = p->next;
  683. }
  684. }
  685. spin_unlock_irqrestore(&irq_action_lock, flags);
  686. }
  687. #endif
  688. struct sun5_timer {
  689. u64 count0;
  690. u64 limit0;
  691. u64 count1;
  692. u64 limit1;
  693. };
  694. static struct sun5_timer *prom_timers;
  695. static u64 prom_limit0, prom_limit1;
  696. static void map_prom_timers(void)
  697. {
  698. unsigned int addr[3];
  699. int tnode, err;
  700. /* PROM timer node hangs out in the top level of device siblings... */
  701. tnode = prom_finddevice("/counter-timer");
  702. /* Assume if node is not present, PROM uses different tick mechanism
  703. * which we should not care about.
  704. */
  705. if (tnode == 0 || tnode == -1) {
  706. prom_timers = (struct sun5_timer *) 0;
  707. return;
  708. }
  709. /* If PROM is really using this, it must be mapped by him. */
  710. err = prom_getproperty(tnode, "address", (char *)addr, sizeof(addr));
  711. if (err == -1) {
  712. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  713. prom_timers = (struct sun5_timer *) 0;
  714. return;
  715. }
  716. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  717. }
  718. static void kill_prom_timer(void)
  719. {
  720. if (!prom_timers)
  721. return;
  722. /* Save them away for later. */
  723. prom_limit0 = prom_timers->limit0;
  724. prom_limit1 = prom_timers->limit1;
  725. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  726. * We turn both off here just to be paranoid.
  727. */
  728. prom_timers->limit0 = 0;
  729. prom_timers->limit1 = 0;
  730. /* Wheee, eat the interrupt packet too... */
  731. __asm__ __volatile__(
  732. " mov 0x40, %%g2\n"
  733. " ldxa [%%g0] %0, %%g1\n"
  734. " ldxa [%%g2] %1, %%g1\n"
  735. " stxa %%g0, [%%g0] %0\n"
  736. " membar #Sync\n"
  737. : /* no outputs */
  738. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  739. : "g1", "g2");
  740. }
  741. void init_irqwork_curcpu(void)
  742. {
  743. int cpu = hard_smp_processor_id();
  744. trap_block[cpu].irq_worklist = 0;
  745. }
  746. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
  747. {
  748. unsigned long num_entries = 128;
  749. unsigned long status;
  750. status = sun4v_cpu_qconf(type, paddr, num_entries);
  751. if (status != HV_EOK) {
  752. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  753. "err %lu\n", type, paddr, num_entries, status);
  754. prom_halt();
  755. }
  756. }
  757. static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
  758. {
  759. struct trap_per_cpu *tb = &trap_block[this_cpu];
  760. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
  761. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
  762. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
  763. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
  764. }
  765. static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
  766. {
  767. void *page;
  768. if (use_bootmem)
  769. page = alloc_bootmem_low_pages(PAGE_SIZE);
  770. else
  771. page = (void *) get_zeroed_page(GFP_ATOMIC);
  772. if (!page) {
  773. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  774. prom_halt();
  775. }
  776. *pa_ptr = __pa(page);
  777. }
  778. static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
  779. {
  780. void *page;
  781. if (use_bootmem)
  782. page = alloc_bootmem_low_pages(PAGE_SIZE);
  783. else
  784. page = (void *) get_zeroed_page(GFP_ATOMIC);
  785. if (!page) {
  786. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  787. prom_halt();
  788. }
  789. *pa_ptr = __pa(page);
  790. }
  791. static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
  792. {
  793. #ifdef CONFIG_SMP
  794. void *page;
  795. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  796. if (use_bootmem)
  797. page = alloc_bootmem_low_pages(PAGE_SIZE);
  798. else
  799. page = (void *) get_zeroed_page(GFP_ATOMIC);
  800. if (!page) {
  801. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  802. prom_halt();
  803. }
  804. tb->cpu_mondo_block_pa = __pa(page);
  805. tb->cpu_list_pa = __pa(page + 64);
  806. #endif
  807. }
  808. /* Allocate and register the mondo and error queues for this cpu. */
  809. void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
  810. {
  811. struct trap_per_cpu *tb = &trap_block[cpu];
  812. if (alloc) {
  813. alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
  814. alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
  815. alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
  816. alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
  817. alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
  818. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
  819. init_cpu_send_mondo_info(tb, use_bootmem);
  820. }
  821. if (load) {
  822. if (cpu != hard_smp_processor_id()) {
  823. prom_printf("SUN4V: init mondo on cpu %d not %d\n",
  824. cpu, hard_smp_processor_id());
  825. prom_halt();
  826. }
  827. sun4v_register_mondo_queues(cpu);
  828. }
  829. }
  830. /* Only invoked on boot processor. */
  831. void __init init_IRQ(void)
  832. {
  833. map_prom_timers();
  834. kill_prom_timer();
  835. memset(&ivector_table[0], 0, sizeof(ivector_table));
  836. if (tlb_type == hypervisor)
  837. sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
  838. /* We need to clear any IRQ's pending in the soft interrupt
  839. * registers, a spurious one could be left around from the
  840. * PROM timer which we just disabled.
  841. */
  842. clear_softint(get_softint());
  843. /* Now that ivector table is initialized, it is safe
  844. * to receive IRQ vector traps. We will normally take
  845. * one or two right now, in case some device PROM used
  846. * to boot us wants to speak to us. We just ignore them.
  847. */
  848. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  849. "or %%g1, %0, %%g1\n\t"
  850. "wrpr %%g1, 0x0, %%pstate"
  851. : /* No outputs */
  852. : "i" (PSTATE_IE)
  853. : "g1");
  854. }
  855. static struct proc_dir_entry * root_irq_dir;
  856. static struct proc_dir_entry * irq_dir [NUM_IVECS];
  857. #ifdef CONFIG_SMP
  858. static int irq_affinity_read_proc (char *page, char **start, off_t off,
  859. int count, int *eof, void *data)
  860. {
  861. struct ino_bucket *bp = ivector_table + (long)data;
  862. struct irq_desc *desc = bp->irq_info;
  863. struct irqaction *ap = desc->action;
  864. cpumask_t mask;
  865. int len;
  866. mask = get_smpaff_in_irqaction(ap);
  867. if (cpus_empty(mask))
  868. mask = cpu_online_map;
  869. len = cpumask_scnprintf(page, count, mask);
  870. if (count - len < 2)
  871. return -EINVAL;
  872. len += sprintf(page + len, "\n");
  873. return len;
  874. }
  875. static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
  876. {
  877. struct ino_bucket *bp = ivector_table + irq;
  878. struct irq_desc *desc = bp->irq_info;
  879. struct irqaction *ap = desc->action;
  880. /* Users specify affinity in terms of hw cpu ids.
  881. * As soon as we do this, handler_irq() might see and take action.
  882. */
  883. put_smpaff_in_irqaction(ap, hw_aff);
  884. /* Migration is simply done by the next cpu to service this
  885. * interrupt.
  886. */
  887. }
  888. static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
  889. unsigned long count, void *data)
  890. {
  891. int irq = (long) data, full_count = count, err;
  892. cpumask_t new_value;
  893. err = cpumask_parse(buffer, count, new_value);
  894. /*
  895. * Do not allow disabling IRQs completely - it's a too easy
  896. * way to make the system unusable accidentally :-) At least
  897. * one online CPU still has to be targeted.
  898. */
  899. cpus_and(new_value, new_value, cpu_online_map);
  900. if (cpus_empty(new_value))
  901. return -EINVAL;
  902. set_intr_affinity(irq, new_value);
  903. return full_count;
  904. }
  905. #endif
  906. #define MAX_NAMELEN 10
  907. static void register_irq_proc (unsigned int irq)
  908. {
  909. char name [MAX_NAMELEN];
  910. if (!root_irq_dir || irq_dir[irq])
  911. return;
  912. memset(name, 0, MAX_NAMELEN);
  913. sprintf(name, "%x", irq);
  914. /* create /proc/irq/1234 */
  915. irq_dir[irq] = proc_mkdir(name, root_irq_dir);
  916. #ifdef CONFIG_SMP
  917. /* XXX SMP affinity not supported on starfire yet. */
  918. if (this_is_starfire == 0) {
  919. struct proc_dir_entry *entry;
  920. /* create /proc/irq/1234/smp_affinity */
  921. entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
  922. if (entry) {
  923. entry->nlink = 1;
  924. entry->data = (void *)(long)irq;
  925. entry->read_proc = irq_affinity_read_proc;
  926. entry->write_proc = irq_affinity_write_proc;
  927. }
  928. }
  929. #endif
  930. }
  931. void init_irq_proc (void)
  932. {
  933. /* create /proc/irq */
  934. root_irq_dir = proc_mkdir("irq", NULL);
  935. }