irq.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /* $Id: irq.c,v 1.114 2002/01/11 08:45:38 davem Exp $
  2. * irq.c: UltraSparc IRQ handling/init/registry.
  3. *
  4. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
  7. */
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/signal.h>
  15. #include <linux/mm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/slab.h>
  18. #include <linux/random.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/bootmem.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/processor.h>
  26. #include <asm/atomic.h>
  27. #include <asm/system.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/sbus.h>
  31. #include <asm/iommu.h>
  32. #include <asm/upa.h>
  33. #include <asm/oplib.h>
  34. #include <asm/timer.h>
  35. #include <asm/smp.h>
  36. #include <asm/starfire.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/cache.h>
  39. #include <asm/cpudata.h>
  40. #include <asm/auxio.h>
  41. #include <asm/head.h>
  42. #ifdef CONFIG_SMP
  43. static void distribute_irqs(void);
  44. #endif
  45. /* UPA nodes send interrupt packet to UltraSparc with first data reg
  46. * value low 5 (7 on Starfire) bits holding the IRQ identifier being
  47. * delivered. We must translate this into a non-vector IRQ so we can
  48. * set the softint on this cpu.
  49. *
  50. * To make processing these packets efficient and race free we use
  51. * an array of irq buckets below. The interrupt vector handler in
  52. * entry.S feeds incoming packets into per-cpu pil-indexed lists.
  53. * The IVEC handler does not need to act atomically, the PIL dispatch
  54. * code uses CAS to get an atomic snapshot of the list and clear it
  55. * at the same time.
  56. */
  57. struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
  58. /* This has to be in the main kernel image, it cannot be
  59. * turned into per-cpu data. The reason is that the main
  60. * kernel image is locked into the TLB and this structure
  61. * is accessed from the vectored interrupt trap handler. If
  62. * access to this structure takes a TLB miss it could cause
  63. * the 5-level sparc v9 trap stack to overflow.
  64. */
  65. struct irq_work_struct {
  66. unsigned int irq_worklists[16];
  67. };
  68. struct irq_work_struct __irq_work[NR_CPUS];
  69. #define irq_work(__cpu, __pil) &(__irq_work[(__cpu)].irq_worklists[(__pil)])
  70. static struct irqaction *irq_action[NR_IRQS+1];
  71. /* This only synchronizes entities which modify IRQ handler
  72. * state and some selected user-level spots that want to
  73. * read things in the table. IRQ handler processing orders
  74. * its' accesses such that no locking is needed.
  75. */
  76. static DEFINE_SPINLOCK(irq_action_lock);
  77. static void register_irq_proc (unsigned int irq);
  78. /*
  79. * Upper 2b of irqaction->flags holds the ino.
  80. * irqaction->mask holds the smp affinity information.
  81. */
  82. #define put_ino_in_irqaction(action, irq) \
  83. action->flags &= 0xffffffffffffUL; \
  84. if (__bucket(irq) == &pil0_dummy_bucket) \
  85. action->flags |= 0xdeadUL << 48; \
  86. else \
  87. action->flags |= __irq_ino(irq) << 48;
  88. #define get_ino_in_irqaction(action) (action->flags >> 48)
  89. #define put_smpaff_in_irqaction(action, smpaff) (action)->mask = (smpaff)
  90. #define get_smpaff_in_irqaction(action) ((action)->mask)
  91. int show_interrupts(struct seq_file *p, void *v)
  92. {
  93. unsigned long flags;
  94. int i = *(loff_t *) v;
  95. struct irqaction *action;
  96. #ifdef CONFIG_SMP
  97. int j;
  98. #endif
  99. spin_lock_irqsave(&irq_action_lock, flags);
  100. if (i <= NR_IRQS) {
  101. if (!(action = *(i + irq_action)))
  102. goto out_unlock;
  103. seq_printf(p, "%3d: ", i);
  104. #ifndef CONFIG_SMP
  105. seq_printf(p, "%10u ", kstat_irqs(i));
  106. #else
  107. for (j = 0; j < NR_CPUS; j++) {
  108. if (!cpu_online(j))
  109. continue;
  110. seq_printf(p, "%10u ",
  111. kstat_cpu(j).irqs[i]);
  112. }
  113. #endif
  114. seq_printf(p, " %s:%lx", action->name,
  115. get_ino_in_irqaction(action));
  116. for (action = action->next; action; action = action->next) {
  117. seq_printf(p, ", %s:%lx", action->name,
  118. get_ino_in_irqaction(action));
  119. }
  120. seq_putc(p, '\n');
  121. }
  122. out_unlock:
  123. spin_unlock_irqrestore(&irq_action_lock, flags);
  124. return 0;
  125. }
  126. 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 = kmalloc(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. memset(bucket->irq_info, 0, sizeof(struct irq_desc));
  282. /* Ok, looks good, set it up. Don't touch the irq_chain or
  283. * the pending flag.
  284. */
  285. bucket->imap = imap;
  286. bucket->iclr = iclr;
  287. bucket->pil = pil;
  288. bucket->flags = 0;
  289. out:
  290. return __irq(bucket);
  291. }
  292. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino, int pil, unsigned char flags)
  293. {
  294. struct ino_bucket *bucket;
  295. unsigned long sysino;
  296. sysino = sun4v_devino_to_sysino(devhandle, devino);
  297. bucket = &ivector_table[sysino];
  298. /* Catch accidental accesses to these things. IMAP/ICLR handling
  299. * is done by hypervisor calls on sun4v platforms, not by direct
  300. * register accesses.
  301. *
  302. * But we need to make them look unique for the disable_irq() logic
  303. * in free_irq().
  304. */
  305. bucket->imap = ~0UL - sysino;
  306. bucket->iclr = ~0UL - sysino;
  307. bucket->pil = pil;
  308. bucket->flags = flags;
  309. bucket->irq_info = kmalloc(sizeof(struct irq_desc), GFP_ATOMIC);
  310. if (!bucket->irq_info) {
  311. prom_printf("IRQ: Error, kmalloc(irq_desc) failed.\n");
  312. prom_halt();
  313. }
  314. memset(bucket->irq_info, 0, sizeof(struct irq_desc));
  315. return __irq(bucket);
  316. }
  317. static void atomic_bucket_insert(struct ino_bucket *bucket)
  318. {
  319. unsigned long pstate;
  320. unsigned int *ent;
  321. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  322. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  323. : : "r" (pstate), "i" (PSTATE_IE));
  324. ent = irq_work(smp_processor_id(), bucket->pil);
  325. bucket->irq_chain = *ent;
  326. *ent = __irq(bucket);
  327. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  328. }
  329. static int check_irq_sharing(int pil, unsigned long irqflags)
  330. {
  331. struct irqaction *action, *tmp;
  332. action = *(irq_action + pil);
  333. if (action) {
  334. if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
  335. for (tmp = action; tmp->next; tmp = tmp->next)
  336. ;
  337. } else {
  338. return -EBUSY;
  339. }
  340. }
  341. return 0;
  342. }
  343. static void append_irq_action(int pil, struct irqaction *action)
  344. {
  345. struct irqaction **pp = irq_action + pil;
  346. while (*pp)
  347. pp = &((*pp)->next);
  348. *pp = action;
  349. }
  350. static struct irqaction *get_action_slot(struct ino_bucket *bucket)
  351. {
  352. struct irq_desc *desc = bucket->irq_info;
  353. int max_irq, i;
  354. max_irq = 1;
  355. if (bucket->flags & IBF_PCI)
  356. max_irq = MAX_IRQ_DESC_ACTION;
  357. for (i = 0; i < max_irq; i++) {
  358. struct irqaction *p = &desc->action[i];
  359. u32 mask = (1 << i);
  360. if (desc->action_active_mask & mask)
  361. continue;
  362. desc->action_active_mask |= mask;
  363. return p;
  364. }
  365. return NULL;
  366. }
  367. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  368. unsigned long irqflags, const char *name, void *dev_id)
  369. {
  370. struct irqaction *action;
  371. struct ino_bucket *bucket = __bucket(irq);
  372. unsigned long flags;
  373. int pending = 0;
  374. if (unlikely(!handler))
  375. return -EINVAL;
  376. if (unlikely(!bucket->irq_info))
  377. return -ENODEV;
  378. if ((bucket != &pil0_dummy_bucket) && (irqflags & SA_SAMPLE_RANDOM)) {
  379. /*
  380. * This function might sleep, we want to call it first,
  381. * outside of the atomic block. In SA_STATIC_ALLOC case,
  382. * random driver's kmalloc will fail, but it is safe.
  383. * If already initialized, random driver will not reinit.
  384. * Yes, this might clear the entropy pool if the wrong
  385. * driver is attempted to be loaded, without actually
  386. * installing a new handler, but is this really a problem,
  387. * only the sysadmin is able to do this.
  388. */
  389. rand_initialize_irq(irq);
  390. }
  391. spin_lock_irqsave(&irq_action_lock, flags);
  392. if (check_irq_sharing(bucket->pil, irqflags)) {
  393. spin_unlock_irqrestore(&irq_action_lock, flags);
  394. return -EBUSY;
  395. }
  396. action = get_action_slot(bucket);
  397. if (!action) {
  398. spin_unlock_irqrestore(&irq_action_lock, flags);
  399. return -ENOMEM;
  400. }
  401. bucket->flags |= IBF_ACTIVE;
  402. pending = 0;
  403. if (bucket != &pil0_dummy_bucket) {
  404. pending = bucket->pending;
  405. if (pending)
  406. bucket->pending = 0;
  407. }
  408. action->handler = handler;
  409. action->flags = irqflags;
  410. action->name = name;
  411. action->next = NULL;
  412. action->dev_id = dev_id;
  413. put_ino_in_irqaction(action, irq);
  414. put_smpaff_in_irqaction(action, CPU_MASK_NONE);
  415. append_irq_action(bucket->pil, action);
  416. enable_irq(irq);
  417. /* We ate the IVEC already, this makes sure it does not get lost. */
  418. if (pending) {
  419. atomic_bucket_insert(bucket);
  420. set_softint(1 << bucket->pil);
  421. }
  422. spin_unlock_irqrestore(&irq_action_lock, flags);
  423. if (bucket != &pil0_dummy_bucket)
  424. register_irq_proc(__irq_ino(irq));
  425. #ifdef CONFIG_SMP
  426. distribute_irqs();
  427. #endif
  428. return 0;
  429. }
  430. EXPORT_SYMBOL(request_irq);
  431. static struct irqaction *unlink_irq_action(unsigned int irq, void *dev_id)
  432. {
  433. struct ino_bucket *bucket = __bucket(irq);
  434. struct irqaction *action, **pp;
  435. pp = irq_action + bucket->pil;
  436. action = *pp;
  437. if (unlikely(!action))
  438. return NULL;
  439. if (unlikely(!action->handler)) {
  440. printk("Freeing free IRQ %d\n", bucket->pil);
  441. return NULL;
  442. }
  443. while (action && action->dev_id != dev_id) {
  444. pp = &action->next;
  445. action = *pp;
  446. }
  447. if (likely(action))
  448. *pp = action->next;
  449. return action;
  450. }
  451. void free_irq(unsigned int irq, void *dev_id)
  452. {
  453. struct irqaction *action;
  454. struct ino_bucket *bucket;
  455. unsigned long flags;
  456. spin_lock_irqsave(&irq_action_lock, flags);
  457. action = unlink_irq_action(irq, dev_id);
  458. spin_unlock_irqrestore(&irq_action_lock, flags);
  459. if (unlikely(!action))
  460. return;
  461. synchronize_irq(irq);
  462. spin_lock_irqsave(&irq_action_lock, flags);
  463. bucket = __bucket(irq);
  464. if (bucket != &pil0_dummy_bucket) {
  465. struct irq_desc *desc = bucket->irq_info;
  466. int ent, i;
  467. for (i = 0; i < MAX_IRQ_DESC_ACTION; i++) {
  468. struct irqaction *p = &desc->action[i];
  469. if (p == action) {
  470. desc->action_active_mask &= ~(1 << i);
  471. break;
  472. }
  473. }
  474. if (!desc->action_active_mask) {
  475. unsigned long imap = bucket->imap;
  476. /* This unique interrupt source is now inactive. */
  477. bucket->flags &= ~IBF_ACTIVE;
  478. /* See if any other buckets share this bucket's IMAP
  479. * and are still active.
  480. */
  481. for (ent = 0; ent < NUM_IVECS; ent++) {
  482. struct ino_bucket *bp = &ivector_table[ent];
  483. if (bp != bucket &&
  484. bp->imap == imap &&
  485. (bp->flags & IBF_ACTIVE) != 0)
  486. break;
  487. }
  488. /* Only disable when no other sub-irq levels of
  489. * the same IMAP are active.
  490. */
  491. if (ent == NUM_IVECS)
  492. disable_irq(irq);
  493. }
  494. }
  495. spin_unlock_irqrestore(&irq_action_lock, flags);
  496. }
  497. EXPORT_SYMBOL(free_irq);
  498. #ifdef CONFIG_SMP
  499. void synchronize_irq(unsigned int irq)
  500. {
  501. struct ino_bucket *bucket = __bucket(irq);
  502. #if 0
  503. /* The following is how I wish I could implement this.
  504. * Unfortunately the ICLR registers are read-only, you can
  505. * only write ICLR_foo values to them. To get the current
  506. * IRQ status you would need to get at the IRQ diag registers
  507. * in the PCI/SBUS controller and the layout of those vary
  508. * from one controller to the next, sigh... -DaveM
  509. */
  510. unsigned long iclr = bucket->iclr;
  511. while (1) {
  512. u32 tmp = upa_readl(iclr);
  513. if (tmp == ICLR_TRANSMIT ||
  514. tmp == ICLR_PENDING) {
  515. cpu_relax();
  516. continue;
  517. }
  518. break;
  519. }
  520. #else
  521. /* So we have to do this with a INPROGRESS bit just like x86. */
  522. while (bucket->flags & IBF_INPROGRESS)
  523. cpu_relax();
  524. #endif
  525. }
  526. #endif /* CONFIG_SMP */
  527. static void process_bucket(int irq, struct ino_bucket *bp, struct pt_regs *regs)
  528. {
  529. struct irq_desc *desc = bp->irq_info;
  530. unsigned char flags = bp->flags;
  531. u32 action_mask, i;
  532. int random;
  533. bp->flags |= IBF_INPROGRESS;
  534. if (unlikely(!(flags & IBF_ACTIVE))) {
  535. bp->pending = 1;
  536. goto out;
  537. }
  538. if (desc->pre_handler)
  539. desc->pre_handler(bp,
  540. desc->pre_handler_arg1,
  541. desc->pre_handler_arg2);
  542. action_mask = desc->action_active_mask;
  543. random = 0;
  544. for (i = 0; i < MAX_IRQ_DESC_ACTION; i++) {
  545. struct irqaction *p = &desc->action[i];
  546. u32 mask = (1 << i);
  547. if (!(action_mask & mask))
  548. continue;
  549. action_mask &= ~mask;
  550. if (p->handler(__irq(bp), p->dev_id, regs) == IRQ_HANDLED)
  551. random |= p->flags;
  552. if (!action_mask)
  553. break;
  554. }
  555. if (bp->pil != 0) {
  556. if (tlb_type == hypervisor) {
  557. unsigned int ino = __irq_ino(bp);
  558. int err;
  559. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  560. if (err != HV_EOK)
  561. printk("sun4v_intr_setstate(%x): "
  562. "err(%d)\n", ino, err);
  563. } else {
  564. upa_writel(ICLR_IDLE, bp->iclr);
  565. }
  566. /* Test and add entropy */
  567. if (random & SA_SAMPLE_RANDOM)
  568. add_interrupt_randomness(irq);
  569. }
  570. out:
  571. bp->flags &= ~IBF_INPROGRESS;
  572. }
  573. void handler_irq(int irq, struct pt_regs *regs)
  574. {
  575. struct ino_bucket *bp;
  576. int cpu = smp_processor_id();
  577. #ifndef CONFIG_SMP
  578. /*
  579. * Check for TICK_INT on level 14 softint.
  580. */
  581. {
  582. unsigned long clr_mask = 1 << irq;
  583. unsigned long tick_mask = tick_ops->softint_mask;
  584. if ((irq == 14) && (get_softint() & tick_mask)) {
  585. irq = 0;
  586. clr_mask = tick_mask;
  587. }
  588. clear_softint(clr_mask);
  589. }
  590. #else
  591. clear_softint(1 << irq);
  592. #endif
  593. irq_enter();
  594. kstat_this_cpu.irqs[irq]++;
  595. /* Sliiiick... */
  596. #ifndef CONFIG_SMP
  597. bp = ((irq != 0) ?
  598. __bucket(xchg32(irq_work(cpu, irq), 0)) :
  599. &pil0_dummy_bucket);
  600. #else
  601. bp = __bucket(xchg32(irq_work(cpu, irq), 0));
  602. #endif
  603. while (bp) {
  604. struct ino_bucket *nbp = __bucket(bp->irq_chain);
  605. bp->irq_chain = 0;
  606. process_bucket(irq, bp, regs);
  607. bp = nbp;
  608. }
  609. irq_exit();
  610. }
  611. #ifdef CONFIG_BLK_DEV_FD
  612. extern irqreturn_t floppy_interrupt(int, void *, struct pt_regs *);;
  613. /* XXX No easy way to include asm/floppy.h XXX */
  614. extern unsigned char *pdma_vaddr;
  615. extern unsigned long pdma_size;
  616. extern volatile int doing_pdma;
  617. extern unsigned long fdc_status;
  618. irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie, struct pt_regs *regs)
  619. {
  620. if (likely(doing_pdma)) {
  621. void __iomem *stat = (void __iomem *) fdc_status;
  622. unsigned char *vaddr = pdma_vaddr;
  623. unsigned long size = pdma_size;
  624. u8 val;
  625. while (size) {
  626. val = readb(stat);
  627. if (unlikely(!(val & 0x80))) {
  628. pdma_vaddr = vaddr;
  629. pdma_size = size;
  630. return IRQ_HANDLED;
  631. }
  632. if (unlikely(!(val & 0x20))) {
  633. pdma_vaddr = vaddr;
  634. pdma_size = size;
  635. doing_pdma = 0;
  636. goto main_interrupt;
  637. }
  638. if (val & 0x40) {
  639. /* read */
  640. *vaddr++ = readb(stat + 1);
  641. } else {
  642. unsigned char data = *vaddr++;
  643. /* write */
  644. writeb(data, stat + 1);
  645. }
  646. size--;
  647. }
  648. pdma_vaddr = vaddr;
  649. pdma_size = size;
  650. /* Send Terminal Count pulse to floppy controller. */
  651. val = readb(auxio_register);
  652. val |= AUXIO_AUX1_FTCNT;
  653. writeb(val, auxio_register);
  654. val &= ~AUXIO_AUX1_FTCNT;
  655. writeb(val, auxio_register);
  656. doing_pdma = 0;
  657. }
  658. main_interrupt:
  659. return floppy_interrupt(irq, dev_cookie, regs);
  660. }
  661. EXPORT_SYMBOL(sparc_floppy_irq);
  662. #endif
  663. /* We really don't need these at all on the Sparc. We only have
  664. * stubs here because they are exported to modules.
  665. */
  666. unsigned long probe_irq_on(void)
  667. {
  668. return 0;
  669. }
  670. EXPORT_SYMBOL(probe_irq_on);
  671. int probe_irq_off(unsigned long mask)
  672. {
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(probe_irq_off);
  676. #ifdef CONFIG_SMP
  677. static int retarget_one_irq(struct irqaction *p, int goal_cpu)
  678. {
  679. struct ino_bucket *bucket = get_ino_in_irqaction(p) + ivector_table;
  680. while (!cpu_online(goal_cpu)) {
  681. if (++goal_cpu >= NR_CPUS)
  682. goal_cpu = 0;
  683. }
  684. if (tlb_type == hypervisor) {
  685. unsigned int ino = __irq_ino(bucket);
  686. sun4v_intr_settarget(ino, goal_cpu);
  687. sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  688. } else {
  689. unsigned long imap = bucket->imap;
  690. unsigned int tid = sun4u_compute_tid(imap, goal_cpu);
  691. upa_writel(tid | IMAP_VALID, imap);
  692. }
  693. do {
  694. if (++goal_cpu >= NR_CPUS)
  695. goal_cpu = 0;
  696. } while (!cpu_online(goal_cpu));
  697. return goal_cpu;
  698. }
  699. /* Called from request_irq. */
  700. static void distribute_irqs(void)
  701. {
  702. unsigned long flags;
  703. int cpu, level;
  704. spin_lock_irqsave(&irq_action_lock, flags);
  705. cpu = 0;
  706. /*
  707. * Skip the timer at [0], and very rare error/power intrs at [15].
  708. * Also level [12], it causes problems on Ex000 systems.
  709. */
  710. for (level = 1; level < NR_IRQS; level++) {
  711. struct irqaction *p = irq_action[level];
  712. if (level == 12)
  713. continue;
  714. while(p) {
  715. cpu = retarget_one_irq(p, cpu);
  716. p = p->next;
  717. }
  718. }
  719. spin_unlock_irqrestore(&irq_action_lock, flags);
  720. }
  721. #endif
  722. struct sun5_timer {
  723. u64 count0;
  724. u64 limit0;
  725. u64 count1;
  726. u64 limit1;
  727. };
  728. static struct sun5_timer *prom_timers;
  729. static u64 prom_limit0, prom_limit1;
  730. static void map_prom_timers(void)
  731. {
  732. unsigned int addr[3];
  733. int tnode, err;
  734. /* PROM timer node hangs out in the top level of device siblings... */
  735. tnode = prom_finddevice("/counter-timer");
  736. /* Assume if node is not present, PROM uses different tick mechanism
  737. * which we should not care about.
  738. */
  739. if (tnode == 0 || tnode == -1) {
  740. prom_timers = (struct sun5_timer *) 0;
  741. return;
  742. }
  743. /* If PROM is really using this, it must be mapped by him. */
  744. err = prom_getproperty(tnode, "address", (char *)addr, sizeof(addr));
  745. if (err == -1) {
  746. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  747. prom_timers = (struct sun5_timer *) 0;
  748. return;
  749. }
  750. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  751. }
  752. static void kill_prom_timer(void)
  753. {
  754. if (!prom_timers)
  755. return;
  756. /* Save them away for later. */
  757. prom_limit0 = prom_timers->limit0;
  758. prom_limit1 = prom_timers->limit1;
  759. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  760. * We turn both off here just to be paranoid.
  761. */
  762. prom_timers->limit0 = 0;
  763. prom_timers->limit1 = 0;
  764. /* Wheee, eat the interrupt packet too... */
  765. __asm__ __volatile__(
  766. " mov 0x40, %%g2\n"
  767. " ldxa [%%g0] %0, %%g1\n"
  768. " ldxa [%%g2] %1, %%g1\n"
  769. " stxa %%g0, [%%g0] %0\n"
  770. " membar #Sync\n"
  771. : /* no outputs */
  772. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  773. : "g1", "g2");
  774. }
  775. void init_irqwork_curcpu(void)
  776. {
  777. int cpu = hard_smp_processor_id();
  778. memset(__irq_work + cpu, 0, sizeof(struct irq_work_struct));
  779. }
  780. static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
  781. {
  782. unsigned long num_entries = 128;
  783. unsigned long status;
  784. status = sun4v_cpu_qconf(type, paddr, num_entries);
  785. if (status != HV_EOK) {
  786. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  787. "err %lu\n", type, paddr, num_entries, status);
  788. prom_halt();
  789. }
  790. }
  791. static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
  792. {
  793. struct trap_per_cpu *tb = &trap_block[this_cpu];
  794. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
  795. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
  796. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
  797. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
  798. }
  799. static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
  800. {
  801. void *page;
  802. if (use_bootmem)
  803. page = alloc_bootmem_low_pages(PAGE_SIZE);
  804. else
  805. page = (void *) get_zeroed_page(GFP_ATOMIC);
  806. if (!page) {
  807. prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
  808. prom_halt();
  809. }
  810. *pa_ptr = __pa(page);
  811. }
  812. static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
  813. {
  814. void *page;
  815. if (use_bootmem)
  816. page = alloc_bootmem_low_pages(PAGE_SIZE);
  817. else
  818. page = (void *) get_zeroed_page(GFP_ATOMIC);
  819. if (!page) {
  820. prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
  821. prom_halt();
  822. }
  823. *pa_ptr = __pa(page);
  824. }
  825. static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
  826. {
  827. #ifdef CONFIG_SMP
  828. void *page;
  829. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  830. if (use_bootmem)
  831. page = alloc_bootmem_low_pages(PAGE_SIZE);
  832. else
  833. page = (void *) get_zeroed_page(GFP_ATOMIC);
  834. if (!page) {
  835. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  836. prom_halt();
  837. }
  838. tb->cpu_mondo_block_pa = __pa(page);
  839. tb->cpu_list_pa = __pa(page + 64);
  840. #endif
  841. }
  842. /* Allocate and register the mondo and error queues for this cpu. */
  843. void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
  844. {
  845. struct trap_per_cpu *tb = &trap_block[cpu];
  846. if (alloc) {
  847. alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
  848. alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
  849. alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
  850. alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
  851. alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
  852. alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
  853. init_cpu_send_mondo_info(tb, use_bootmem);
  854. }
  855. if (load) {
  856. if (cpu != hard_smp_processor_id()) {
  857. prom_printf("SUN4V: init mondo on cpu %d not %d\n",
  858. cpu, hard_smp_processor_id());
  859. prom_halt();
  860. }
  861. sun4v_register_mondo_queues(cpu);
  862. }
  863. }
  864. /* Only invoked on boot processor. */
  865. void __init init_IRQ(void)
  866. {
  867. map_prom_timers();
  868. kill_prom_timer();
  869. memset(&ivector_table[0], 0, sizeof(ivector_table));
  870. if (tlb_type == hypervisor)
  871. sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
  872. /* We need to clear any IRQ's pending in the soft interrupt
  873. * registers, a spurious one could be left around from the
  874. * PROM timer which we just disabled.
  875. */
  876. clear_softint(get_softint());
  877. /* Now that ivector table is initialized, it is safe
  878. * to receive IRQ vector traps. We will normally take
  879. * one or two right now, in case some device PROM used
  880. * to boot us wants to speak to us. We just ignore them.
  881. */
  882. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  883. "or %%g1, %0, %%g1\n\t"
  884. "wrpr %%g1, 0x0, %%pstate"
  885. : /* No outputs */
  886. : "i" (PSTATE_IE)
  887. : "g1");
  888. }
  889. static struct proc_dir_entry * root_irq_dir;
  890. static struct proc_dir_entry * irq_dir [NUM_IVECS];
  891. #ifdef CONFIG_SMP
  892. static int irq_affinity_read_proc (char *page, char **start, off_t off,
  893. int count, int *eof, void *data)
  894. {
  895. struct ino_bucket *bp = ivector_table + (long)data;
  896. struct irq_desc *desc = bp->irq_info;
  897. struct irqaction *ap = desc->action;
  898. cpumask_t mask;
  899. int len;
  900. mask = get_smpaff_in_irqaction(ap);
  901. if (cpus_empty(mask))
  902. mask = cpu_online_map;
  903. len = cpumask_scnprintf(page, count, mask);
  904. if (count - len < 2)
  905. return -EINVAL;
  906. len += sprintf(page + len, "\n");
  907. return len;
  908. }
  909. static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
  910. {
  911. struct ino_bucket *bp = ivector_table + irq;
  912. struct irq_desc *desc = bp->irq_info;
  913. struct irqaction *ap = desc->action;
  914. /* Users specify affinity in terms of hw cpu ids.
  915. * As soon as we do this, handler_irq() might see and take action.
  916. */
  917. put_smpaff_in_irqaction(ap, hw_aff);
  918. /* Migration is simply done by the next cpu to service this
  919. * interrupt.
  920. */
  921. }
  922. static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
  923. unsigned long count, void *data)
  924. {
  925. int irq = (long) data, full_count = count, err;
  926. cpumask_t new_value;
  927. err = cpumask_parse(buffer, count, new_value);
  928. /*
  929. * Do not allow disabling IRQs completely - it's a too easy
  930. * way to make the system unusable accidentally :-) At least
  931. * one online CPU still has to be targeted.
  932. */
  933. cpus_and(new_value, new_value, cpu_online_map);
  934. if (cpus_empty(new_value))
  935. return -EINVAL;
  936. set_intr_affinity(irq, new_value);
  937. return full_count;
  938. }
  939. #endif
  940. #define MAX_NAMELEN 10
  941. static void register_irq_proc (unsigned int irq)
  942. {
  943. char name [MAX_NAMELEN];
  944. if (!root_irq_dir || irq_dir[irq])
  945. return;
  946. memset(name, 0, MAX_NAMELEN);
  947. sprintf(name, "%x", irq);
  948. /* create /proc/irq/1234 */
  949. irq_dir[irq] = proc_mkdir(name, root_irq_dir);
  950. #ifdef CONFIG_SMP
  951. /* XXX SMP affinity not supported on starfire yet. */
  952. if (this_is_starfire == 0) {
  953. struct proc_dir_entry *entry;
  954. /* create /proc/irq/1234/smp_affinity */
  955. entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
  956. if (entry) {
  957. entry->nlink = 1;
  958. entry->data = (void *)(long)irq;
  959. entry->read_proc = irq_affinity_read_proc;
  960. entry->write_proc = irq_affinity_write_proc;
  961. }
  962. }
  963. #endif
  964. }
  965. void init_irq_proc (void)
  966. {
  967. /* create /proc/irq */
  968. root_irq_dir = proc_mkdir("irq", NULL);
  969. }