irq.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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 <asm/ptrace.h>
  24. #include <asm/processor.h>
  25. #include <asm/atomic.h>
  26. #include <asm/system.h>
  27. #include <asm/irq.h>
  28. #include <asm/sbus.h>
  29. #include <asm/iommu.h>
  30. #include <asm/upa.h>
  31. #include <asm/oplib.h>
  32. #include <asm/timer.h>
  33. #include <asm/smp.h>
  34. #include <asm/starfire.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/cache.h>
  37. #include <asm/cpudata.h>
  38. #ifdef CONFIG_SMP
  39. static void distribute_irqs(void);
  40. #endif
  41. /* UPA nodes send interrupt packet to UltraSparc with first data reg
  42. * value low 5 (7 on Starfire) bits holding the IRQ identifier being
  43. * delivered. We must translate this into a non-vector IRQ so we can
  44. * set the softint on this cpu.
  45. *
  46. * To make processing these packets efficient and race free we use
  47. * an array of irq buckets below. The interrupt vector handler in
  48. * entry.S feeds incoming packets into per-cpu pil-indexed lists.
  49. * The IVEC handler does not need to act atomically, the PIL dispatch
  50. * code uses CAS to get an atomic snapshot of the list and clear it
  51. * at the same time.
  52. */
  53. struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
  54. /* This has to be in the main kernel image, it cannot be
  55. * turned into per-cpu data. The reason is that the main
  56. * kernel image is locked into the TLB and this structure
  57. * is accessed from the vectored interrupt trap handler. If
  58. * access to this structure takes a TLB miss it could cause
  59. * the 5-level sparc v9 trap stack to overflow.
  60. */
  61. struct irq_work_struct {
  62. unsigned int irq_worklists[16];
  63. };
  64. struct irq_work_struct __irq_work[NR_CPUS];
  65. #define irq_work(__cpu, __pil) &(__irq_work[(__cpu)].irq_worklists[(__pil)])
  66. #ifdef CONFIG_PCI
  67. /* This is a table of physical addresses used to deal with IBF_DMA_SYNC.
  68. * It is used for PCI only to synchronize DMA transfers with IRQ delivery
  69. * for devices behind busses other than APB on Sabre systems.
  70. *
  71. * Currently these physical addresses are just config space accesses
  72. * to the command register for that device.
  73. */
  74. unsigned long pci_dma_wsync;
  75. unsigned long dma_sync_reg_table[256];
  76. unsigned char dma_sync_reg_table_entry = 0;
  77. #endif
  78. /* This is based upon code in the 32-bit Sparc kernel written mostly by
  79. * David Redman (djhr@tadpole.co.uk).
  80. */
  81. #define MAX_STATIC_ALLOC 4
  82. static struct irqaction static_irqaction[MAX_STATIC_ALLOC];
  83. static int static_irq_count;
  84. /* This is exported so that fast IRQ handlers can get at it... -DaveM */
  85. struct irqaction *irq_action[NR_IRQS+1] = {
  86. NULL, NULL, NULL, NULL, NULL, NULL , NULL, NULL,
  87. NULL, NULL, NULL, NULL, NULL, NULL , NULL, NULL
  88. };
  89. /* This only synchronizes entities which modify IRQ handler
  90. * state and some selected user-level spots that want to
  91. * read things in the table. IRQ handler processing orders
  92. * its' accesses such that no locking is needed.
  93. */
  94. static DEFINE_SPINLOCK(irq_action_lock);
  95. static void register_irq_proc (unsigned int irq);
  96. /*
  97. * Upper 2b of irqaction->flags holds the ino.
  98. * irqaction->mask holds the smp affinity information.
  99. */
  100. #define put_ino_in_irqaction(action, irq) \
  101. action->flags &= 0xffffffffffffUL; \
  102. if (__bucket(irq) == &pil0_dummy_bucket) \
  103. action->flags |= 0xdeadUL << 48; \
  104. else \
  105. action->flags |= __irq_ino(irq) << 48;
  106. #define get_ino_in_irqaction(action) (action->flags >> 48)
  107. #define put_smpaff_in_irqaction(action, smpaff) (action)->mask = (smpaff)
  108. #define get_smpaff_in_irqaction(action) ((action)->mask)
  109. int show_interrupts(struct seq_file *p, void *v)
  110. {
  111. unsigned long flags;
  112. int i = *(loff_t *) v;
  113. struct irqaction *action;
  114. #ifdef CONFIG_SMP
  115. int j;
  116. #endif
  117. spin_lock_irqsave(&irq_action_lock, flags);
  118. if (i <= NR_IRQS) {
  119. if (!(action = *(i + irq_action)))
  120. goto out_unlock;
  121. seq_printf(p, "%3d: ", i);
  122. #ifndef CONFIG_SMP
  123. seq_printf(p, "%10u ", kstat_irqs(i));
  124. #else
  125. for (j = 0; j < NR_CPUS; j++) {
  126. if (!cpu_online(j))
  127. continue;
  128. seq_printf(p, "%10u ",
  129. kstat_cpu(j).irqs[i]);
  130. }
  131. #endif
  132. seq_printf(p, " %s:%lx", action->name,
  133. get_ino_in_irqaction(action));
  134. for (action = action->next; action; action = action->next) {
  135. seq_printf(p, ", %s:%lx", action->name,
  136. get_ino_in_irqaction(action));
  137. }
  138. seq_putc(p, '\n');
  139. }
  140. out_unlock:
  141. spin_unlock_irqrestore(&irq_action_lock, flags);
  142. return 0;
  143. }
  144. /* Now these are always passed a true fully specified sun4u INO. */
  145. void enable_irq(unsigned int irq)
  146. {
  147. struct ino_bucket *bucket = __bucket(irq);
  148. unsigned long imap;
  149. unsigned long tid;
  150. imap = bucket->imap;
  151. if (imap == 0UL)
  152. return;
  153. preempt_disable();
  154. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  155. unsigned long ver;
  156. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  157. if ((ver >> 32) == 0x003e0016) {
  158. /* We set it to our JBUS ID. */
  159. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  160. : "=r" (tid)
  161. : "i" (ASI_JBUS_CONFIG));
  162. tid = ((tid & (0x1fUL<<17)) << 9);
  163. tid &= IMAP_TID_JBUS;
  164. } else {
  165. /* We set it to our Safari AID. */
  166. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  167. : "=r" (tid)
  168. : "i" (ASI_SAFARI_CONFIG));
  169. tid = ((tid & (0x3ffUL<<17)) << 9);
  170. tid &= IMAP_AID_SAFARI;
  171. }
  172. } else if (this_is_starfire == 0) {
  173. /* We set it to our UPA MID. */
  174. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  175. : "=r" (tid)
  176. : "i" (ASI_UPA_CONFIG));
  177. tid = ((tid & UPA_CONFIG_MID) << 9);
  178. tid &= IMAP_TID_UPA;
  179. } else {
  180. tid = (starfire_translate(imap, smp_processor_id()) << 26);
  181. tid &= IMAP_TID_UPA;
  182. }
  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 mechanism.
  190. */
  191. upa_writel(tid | IMAP_VALID, imap);
  192. preempt_enable();
  193. }
  194. /* This now gets passed true ino's as well. */
  195. void disable_irq(unsigned int irq)
  196. {
  197. struct ino_bucket *bucket = __bucket(irq);
  198. unsigned long imap;
  199. imap = bucket->imap;
  200. if (imap != 0UL) {
  201. u32 tmp;
  202. /* NOTE: We do not want to futz with the IRQ clear registers
  203. * and move the state to IDLE, the SCSI code does call
  204. * disable_irq() to assure atomicity in the queue cmd
  205. * SCSI adapter driver code. Thus we'd lose interrupts.
  206. */
  207. tmp = upa_readl(imap);
  208. tmp &= ~IMAP_VALID;
  209. upa_writel(tmp, imap);
  210. }
  211. }
  212. /* The timer is the one "weird" interrupt which is generated by
  213. * the CPU %tick register and not by some normal vectored interrupt
  214. * source. To handle this special case, we use this dummy INO bucket.
  215. */
  216. static struct ino_bucket pil0_dummy_bucket = {
  217. 0, /* irq_chain */
  218. 0, /* pil */
  219. 0, /* pending */
  220. 0, /* flags */
  221. 0, /* __unused */
  222. NULL, /* irq_info */
  223. 0UL, /* iclr */
  224. 0UL, /* imap */
  225. };
  226. unsigned int build_irq(int pil, int inofixup, unsigned long iclr, unsigned long imap)
  227. {
  228. struct ino_bucket *bucket;
  229. int ino;
  230. if (pil == 0) {
  231. if (iclr != 0UL || imap != 0UL) {
  232. prom_printf("Invalid dummy bucket for PIL0 (%lx:%lx)\n",
  233. iclr, imap);
  234. prom_halt();
  235. }
  236. return __irq(&pil0_dummy_bucket);
  237. }
  238. /* RULE: Both must be specified in all other cases. */
  239. if (iclr == 0UL || imap == 0UL) {
  240. prom_printf("Invalid build_irq %d %d %016lx %016lx\n",
  241. pil, inofixup, iclr, imap);
  242. prom_halt();
  243. }
  244. ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  245. if (ino > NUM_IVECS) {
  246. prom_printf("Invalid INO %04x (%d:%d:%016lx:%016lx)\n",
  247. ino, pil, inofixup, iclr, imap);
  248. prom_halt();
  249. }
  250. /* Ok, looks good, set it up. Don't touch the irq_chain or
  251. * the pending flag.
  252. */
  253. bucket = &ivector_table[ino];
  254. if ((bucket->flags & IBF_ACTIVE) ||
  255. (bucket->irq_info != NULL)) {
  256. /* This is a gross fatal error if it happens here. */
  257. prom_printf("IRQ: Trying to reinit INO bucket, fatal error.\n");
  258. prom_printf("IRQ: Request INO %04x (%d:%d:%016lx:%016lx)\n",
  259. ino, pil, inofixup, iclr, imap);
  260. prom_printf("IRQ: Existing (%d:%016lx:%016lx)\n",
  261. bucket->pil, bucket->iclr, bucket->imap);
  262. prom_printf("IRQ: Cannot continue, halting...\n");
  263. prom_halt();
  264. }
  265. bucket->imap = imap;
  266. bucket->iclr = iclr;
  267. bucket->pil = pil;
  268. bucket->flags = 0;
  269. bucket->irq_info = NULL;
  270. return __irq(bucket);
  271. }
  272. static void atomic_bucket_insert(struct ino_bucket *bucket)
  273. {
  274. unsigned long pstate;
  275. unsigned int *ent;
  276. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  277. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  278. : : "r" (pstate), "i" (PSTATE_IE));
  279. ent = irq_work(smp_processor_id(), bucket->pil);
  280. bucket->irq_chain = *ent;
  281. *ent = __irq(bucket);
  282. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  283. }
  284. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  285. unsigned long irqflags, const char *name, void *dev_id)
  286. {
  287. struct irqaction *action, *tmp = NULL;
  288. struct ino_bucket *bucket = __bucket(irq);
  289. unsigned long flags;
  290. int pending = 0;
  291. if ((bucket != &pil0_dummy_bucket) &&
  292. (bucket < &ivector_table[0] ||
  293. bucket >= &ivector_table[NUM_IVECS])) {
  294. unsigned int *caller;
  295. __asm__ __volatile__("mov %%i7, %0" : "=r" (caller));
  296. printk(KERN_CRIT "request_irq: Old style IRQ registry attempt "
  297. "from %p, irq %08x.\n", caller, irq);
  298. return -EINVAL;
  299. }
  300. if (!handler)
  301. return -EINVAL;
  302. if ((bucket != &pil0_dummy_bucket) && (irqflags & SA_SAMPLE_RANDOM)) {
  303. /*
  304. * This function might sleep, we want to call it first,
  305. * outside of the atomic block. In SA_STATIC_ALLOC case,
  306. * random driver's kmalloc will fail, but it is safe.
  307. * If already initialized, random driver will not reinit.
  308. * Yes, this might clear the entropy pool if the wrong
  309. * driver is attempted to be loaded, without actually
  310. * installing a new handler, but is this really a problem,
  311. * only the sysadmin is able to do this.
  312. */
  313. rand_initialize_irq(irq);
  314. }
  315. spin_lock_irqsave(&irq_action_lock, flags);
  316. action = *(bucket->pil + irq_action);
  317. if (action) {
  318. if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ))
  319. for (tmp = action; tmp->next; tmp = tmp->next)
  320. ;
  321. else {
  322. spin_unlock_irqrestore(&irq_action_lock, flags);
  323. return -EBUSY;
  324. }
  325. action = NULL; /* Or else! */
  326. }
  327. /* If this is flagged as statically allocated then we use our
  328. * private struct which is never freed.
  329. */
  330. if (irqflags & SA_STATIC_ALLOC) {
  331. if (static_irq_count < MAX_STATIC_ALLOC)
  332. action = &static_irqaction[static_irq_count++];
  333. else
  334. printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed "
  335. "using kmalloc\n", irq, name);
  336. }
  337. if (action == NULL)
  338. action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
  339. GFP_ATOMIC);
  340. if (!action) {
  341. spin_unlock_irqrestore(&irq_action_lock, flags);
  342. return -ENOMEM;
  343. }
  344. if (bucket == &pil0_dummy_bucket) {
  345. bucket->irq_info = action;
  346. bucket->flags |= IBF_ACTIVE;
  347. } else {
  348. if ((bucket->flags & IBF_ACTIVE) != 0) {
  349. void *orig = bucket->irq_info;
  350. void **vector = NULL;
  351. if ((bucket->flags & IBF_PCI) == 0) {
  352. printk("IRQ: Trying to share non-PCI bucket.\n");
  353. goto free_and_ebusy;
  354. }
  355. if ((bucket->flags & IBF_MULTI) == 0) {
  356. vector = kmalloc(sizeof(void *) * 4, GFP_ATOMIC);
  357. if (vector == NULL)
  358. goto free_and_enomem;
  359. /* We might have slept. */
  360. if ((bucket->flags & IBF_MULTI) != 0) {
  361. int ent;
  362. kfree(vector);
  363. vector = (void **)bucket->irq_info;
  364. for(ent = 0; ent < 4; ent++) {
  365. if (vector[ent] == NULL) {
  366. vector[ent] = action;
  367. break;
  368. }
  369. }
  370. if (ent == 4)
  371. goto free_and_ebusy;
  372. } else {
  373. vector[0] = orig;
  374. vector[1] = action;
  375. vector[2] = NULL;
  376. vector[3] = NULL;
  377. bucket->irq_info = vector;
  378. bucket->flags |= IBF_MULTI;
  379. }
  380. } else {
  381. int ent;
  382. vector = (void **)orig;
  383. for (ent = 0; ent < 4; ent++) {
  384. if (vector[ent] == NULL) {
  385. vector[ent] = action;
  386. break;
  387. }
  388. }
  389. if (ent == 4)
  390. goto free_and_ebusy;
  391. }
  392. } else {
  393. bucket->irq_info = action;
  394. bucket->flags |= IBF_ACTIVE;
  395. }
  396. pending = bucket->pending;
  397. if (pending)
  398. bucket->pending = 0;
  399. }
  400. action->handler = handler;
  401. action->flags = irqflags;
  402. action->name = name;
  403. action->next = NULL;
  404. action->dev_id = dev_id;
  405. put_ino_in_irqaction(action, irq);
  406. put_smpaff_in_irqaction(action, CPU_MASK_NONE);
  407. if (tmp)
  408. tmp->next = action;
  409. else
  410. *(bucket->pil + irq_action) = action;
  411. enable_irq(irq);
  412. /* We ate the IVEC already, this makes sure it does not get lost. */
  413. if (pending) {
  414. atomic_bucket_insert(bucket);
  415. set_softint(1 << bucket->pil);
  416. }
  417. spin_unlock_irqrestore(&irq_action_lock, flags);
  418. if ((bucket != &pil0_dummy_bucket) && (!(irqflags & SA_STATIC_ALLOC)))
  419. register_irq_proc(__irq_ino(irq));
  420. #ifdef CONFIG_SMP
  421. distribute_irqs();
  422. #endif
  423. return 0;
  424. free_and_ebusy:
  425. kfree(action);
  426. spin_unlock_irqrestore(&irq_action_lock, flags);
  427. return -EBUSY;
  428. free_and_enomem:
  429. kfree(action);
  430. spin_unlock_irqrestore(&irq_action_lock, flags);
  431. return -ENOMEM;
  432. }
  433. EXPORT_SYMBOL(request_irq);
  434. void free_irq(unsigned int irq, void *dev_id)
  435. {
  436. struct irqaction *action;
  437. struct irqaction *tmp = NULL;
  438. unsigned long flags;
  439. struct ino_bucket *bucket = __bucket(irq), *bp;
  440. if ((bucket != &pil0_dummy_bucket) &&
  441. (bucket < &ivector_table[0] ||
  442. bucket >= &ivector_table[NUM_IVECS])) {
  443. unsigned int *caller;
  444. __asm__ __volatile__("mov %%i7, %0" : "=r" (caller));
  445. printk(KERN_CRIT "free_irq: Old style IRQ removal attempt "
  446. "from %p, irq %08x.\n", caller, irq);
  447. return;
  448. }
  449. spin_lock_irqsave(&irq_action_lock, flags);
  450. action = *(bucket->pil + irq_action);
  451. if (!action->handler) {
  452. printk("Freeing free IRQ %d\n", bucket->pil);
  453. return;
  454. }
  455. if (dev_id) {
  456. for ( ; action; action = action->next) {
  457. if (action->dev_id == dev_id)
  458. break;
  459. tmp = action;
  460. }
  461. if (!action) {
  462. printk("Trying to free free shared IRQ %d\n", bucket->pil);
  463. spin_unlock_irqrestore(&irq_action_lock, flags);
  464. return;
  465. }
  466. } else if (action->flags & SA_SHIRQ) {
  467. printk("Trying to free shared IRQ %d with NULL device ID\n", bucket->pil);
  468. spin_unlock_irqrestore(&irq_action_lock, flags);
  469. return;
  470. }
  471. if (action->flags & SA_STATIC_ALLOC) {
  472. printk("Attempt to free statically allocated IRQ %d (%s)\n",
  473. bucket->pil, action->name);
  474. spin_unlock_irqrestore(&irq_action_lock, flags);
  475. return;
  476. }
  477. if (action && tmp)
  478. tmp->next = action->next;
  479. else
  480. *(bucket->pil + irq_action) = action->next;
  481. spin_unlock_irqrestore(&irq_action_lock, flags);
  482. synchronize_irq(irq);
  483. spin_lock_irqsave(&irq_action_lock, flags);
  484. if (bucket != &pil0_dummy_bucket) {
  485. unsigned long imap = bucket->imap;
  486. void **vector, *orig;
  487. int ent;
  488. orig = bucket->irq_info;
  489. vector = (void **)orig;
  490. if ((bucket->flags & IBF_MULTI) != 0) {
  491. int other = 0;
  492. void *orphan = NULL;
  493. for (ent = 0; ent < 4; ent++) {
  494. if (vector[ent] == action)
  495. vector[ent] = NULL;
  496. else if (vector[ent] != NULL) {
  497. orphan = vector[ent];
  498. other++;
  499. }
  500. }
  501. /* Only free when no other shared irq
  502. * uses this bucket.
  503. */
  504. if (other) {
  505. if (other == 1) {
  506. /* Convert back to non-shared bucket. */
  507. bucket->irq_info = orphan;
  508. bucket->flags &= ~(IBF_MULTI);
  509. kfree(vector);
  510. }
  511. goto out;
  512. }
  513. } else {
  514. bucket->irq_info = NULL;
  515. }
  516. /* This unique interrupt source is now inactive. */
  517. bucket->flags &= ~IBF_ACTIVE;
  518. /* See if any other buckets share this bucket's IMAP
  519. * and are still active.
  520. */
  521. for (ent = 0; ent < NUM_IVECS; ent++) {
  522. bp = &ivector_table[ent];
  523. if (bp != bucket &&
  524. bp->imap == imap &&
  525. (bp->flags & IBF_ACTIVE) != 0)
  526. break;
  527. }
  528. /* Only disable when no other sub-irq levels of
  529. * the same IMAP are active.
  530. */
  531. if (ent == NUM_IVECS)
  532. disable_irq(irq);
  533. }
  534. out:
  535. kfree(action);
  536. spin_unlock_irqrestore(&irq_action_lock, flags);
  537. }
  538. EXPORT_SYMBOL(free_irq);
  539. #ifdef CONFIG_SMP
  540. void synchronize_irq(unsigned int irq)
  541. {
  542. struct ino_bucket *bucket = __bucket(irq);
  543. #if 0
  544. /* The following is how I wish I could implement this.
  545. * Unfortunately the ICLR registers are read-only, you can
  546. * only write ICLR_foo values to them. To get the current
  547. * IRQ status you would need to get at the IRQ diag registers
  548. * in the PCI/SBUS controller and the layout of those vary
  549. * from one controller to the next, sigh... -DaveM
  550. */
  551. unsigned long iclr = bucket->iclr;
  552. while (1) {
  553. u32 tmp = upa_readl(iclr);
  554. if (tmp == ICLR_TRANSMIT ||
  555. tmp == ICLR_PENDING) {
  556. cpu_relax();
  557. continue;
  558. }
  559. break;
  560. }
  561. #else
  562. /* So we have to do this with a INPROGRESS bit just like x86. */
  563. while (bucket->flags & IBF_INPROGRESS)
  564. cpu_relax();
  565. #endif
  566. }
  567. #endif /* CONFIG_SMP */
  568. void catch_disabled_ivec(struct pt_regs *regs)
  569. {
  570. int cpu = smp_processor_id();
  571. struct ino_bucket *bucket = __bucket(*irq_work(cpu, 0));
  572. /* We can actually see this on Ultra/PCI PCI cards, which are bridges
  573. * to other devices. Here a single IMAP enabled potentially multiple
  574. * unique interrupt sources (which each do have a unique ICLR register.
  575. *
  576. * So what we do is just register that the IVEC arrived, when registered
  577. * for real the request_irq() code will check the bit and signal
  578. * a local CPU interrupt for it.
  579. */
  580. #if 0
  581. printk("IVEC: Spurious interrupt vector (%x) received at (%016lx)\n",
  582. bucket - &ivector_table[0], regs->tpc);
  583. #endif
  584. *irq_work(cpu, 0) = 0;
  585. bucket->pending = 1;
  586. }
  587. /* Tune this... */
  588. #define FORWARD_VOLUME 12
  589. #ifdef CONFIG_SMP
  590. static inline void redirect_intr(int cpu, struct ino_bucket *bp)
  591. {
  592. /* Ok, here is what is going on:
  593. * 1) Retargeting IRQs on Starfire is very
  594. * expensive so just forget about it on them.
  595. * 2) Moving around very high priority interrupts
  596. * is a losing game.
  597. * 3) If the current cpu is idle, interrupts are
  598. * useful work, so keep them here. But do not
  599. * pass to our neighbour if he is not very idle.
  600. * 4) If sysadmin explicitly asks for directed intrs,
  601. * Just Do It.
  602. */
  603. struct irqaction *ap = bp->irq_info;
  604. cpumask_t cpu_mask;
  605. unsigned int buddy, ticks;
  606. cpu_mask = get_smpaff_in_irqaction(ap);
  607. cpus_and(cpu_mask, cpu_mask, cpu_online_map);
  608. if (cpus_empty(cpu_mask))
  609. cpu_mask = cpu_online_map;
  610. if (this_is_starfire != 0 ||
  611. bp->pil >= 10 || current->pid == 0)
  612. goto out;
  613. /* 'cpu' is the MID (ie. UPAID), calculate the MID
  614. * of our buddy.
  615. */
  616. buddy = cpu + 1;
  617. if (buddy >= NR_CPUS)
  618. buddy = 0;
  619. ticks = 0;
  620. while (!cpu_isset(buddy, cpu_mask)) {
  621. if (++buddy >= NR_CPUS)
  622. buddy = 0;
  623. if (++ticks > NR_CPUS) {
  624. put_smpaff_in_irqaction(ap, CPU_MASK_NONE);
  625. goto out;
  626. }
  627. }
  628. if (buddy == cpu)
  629. goto out;
  630. /* Voo-doo programming. */
  631. if (cpu_data(buddy).idle_volume < FORWARD_VOLUME)
  632. goto out;
  633. /* This just so happens to be correct on Cheetah
  634. * at the moment.
  635. */
  636. buddy <<= 26;
  637. /* Push it to our buddy. */
  638. upa_writel(buddy | IMAP_VALID, bp->imap);
  639. out:
  640. return;
  641. }
  642. #endif
  643. void handler_irq(int irq, struct pt_regs *regs)
  644. {
  645. struct ino_bucket *bp, *nbp;
  646. int cpu = smp_processor_id();
  647. #ifndef CONFIG_SMP
  648. /*
  649. * Check for TICK_INT on level 14 softint.
  650. */
  651. {
  652. unsigned long clr_mask = 1 << irq;
  653. unsigned long tick_mask = tick_ops->softint_mask;
  654. if ((irq == 14) && (get_softint() & tick_mask)) {
  655. irq = 0;
  656. clr_mask = tick_mask;
  657. }
  658. clear_softint(clr_mask);
  659. }
  660. #else
  661. int should_forward = 0;
  662. clear_softint(1 << irq);
  663. #endif
  664. irq_enter();
  665. kstat_this_cpu.irqs[irq]++;
  666. /* Sliiiick... */
  667. #ifndef CONFIG_SMP
  668. bp = ((irq != 0) ?
  669. __bucket(xchg32(irq_work(cpu, irq), 0)) :
  670. &pil0_dummy_bucket);
  671. #else
  672. bp = __bucket(xchg32(irq_work(cpu, irq), 0));
  673. #endif
  674. for ( ; bp != NULL; bp = nbp) {
  675. unsigned char flags = bp->flags;
  676. unsigned char random = 0;
  677. nbp = __bucket(bp->irq_chain);
  678. bp->irq_chain = 0;
  679. bp->flags |= IBF_INPROGRESS;
  680. if ((flags & IBF_ACTIVE) != 0) {
  681. #ifdef CONFIG_PCI
  682. if ((flags & IBF_DMA_SYNC) != 0) {
  683. upa_readl(dma_sync_reg_table[bp->synctab_ent]);
  684. upa_readq(pci_dma_wsync);
  685. }
  686. #endif
  687. if ((flags & IBF_MULTI) == 0) {
  688. struct irqaction *ap = bp->irq_info;
  689. int ret;
  690. ret = ap->handler(__irq(bp), ap->dev_id, regs);
  691. if (ret == IRQ_HANDLED)
  692. random |= ap->flags;
  693. } else {
  694. void **vector = (void **)bp->irq_info;
  695. int ent;
  696. for (ent = 0; ent < 4; ent++) {
  697. struct irqaction *ap = vector[ent];
  698. if (ap != NULL) {
  699. int ret;
  700. ret = ap->handler(__irq(bp),
  701. ap->dev_id,
  702. regs);
  703. if (ret == IRQ_HANDLED)
  704. random |= ap->flags;
  705. }
  706. }
  707. }
  708. /* Only the dummy bucket lacks IMAP/ICLR. */
  709. if (bp->pil != 0) {
  710. #ifdef CONFIG_SMP
  711. if (should_forward) {
  712. redirect_intr(cpu, bp);
  713. should_forward = 0;
  714. }
  715. #endif
  716. upa_writel(ICLR_IDLE, bp->iclr);
  717. /* Test and add entropy */
  718. if (random & SA_SAMPLE_RANDOM)
  719. add_interrupt_randomness(irq);
  720. }
  721. } else
  722. bp->pending = 1;
  723. bp->flags &= ~IBF_INPROGRESS;
  724. }
  725. irq_exit();
  726. }
  727. #ifdef CONFIG_BLK_DEV_FD
  728. extern void floppy_interrupt(int irq, void *dev_cookie, struct pt_regs *regs);
  729. void sparc_floppy_irq(int irq, void *dev_cookie, struct pt_regs *regs)
  730. {
  731. struct irqaction *action = *(irq + irq_action);
  732. struct ino_bucket *bucket;
  733. int cpu = smp_processor_id();
  734. irq_enter();
  735. kstat_this_cpu.irqs[irq]++;
  736. *(irq_work(cpu, irq)) = 0;
  737. bucket = get_ino_in_irqaction(action) + ivector_table;
  738. bucket->flags |= IBF_INPROGRESS;
  739. floppy_interrupt(irq, dev_cookie, regs);
  740. upa_writel(ICLR_IDLE, bucket->iclr);
  741. bucket->flags &= ~IBF_INPROGRESS;
  742. irq_exit();
  743. }
  744. #endif
  745. /* The following assumes that the branch lies before the place we
  746. * are branching to. This is the case for a trap vector...
  747. * You have been warned.
  748. */
  749. #define SPARC_BRANCH(dest_addr, inst_addr) \
  750. (0x10800000 | ((((dest_addr)-(inst_addr))>>2)&0x3fffff))
  751. #define SPARC_NOP (0x01000000)
  752. static void install_fast_irq(unsigned int cpu_irq,
  753. irqreturn_t (*handler)(int, void *, struct pt_regs *))
  754. {
  755. extern unsigned long sparc64_ttable_tl0;
  756. unsigned long ttent = (unsigned long) &sparc64_ttable_tl0;
  757. unsigned int *insns;
  758. ttent += 0x820;
  759. ttent += (cpu_irq - 1) << 5;
  760. insns = (unsigned int *) ttent;
  761. insns[0] = SPARC_BRANCH(((unsigned long) handler),
  762. ((unsigned long)&insns[0]));
  763. insns[1] = SPARC_NOP;
  764. __asm__ __volatile__("membar #StoreStore; flush %0" : : "r" (ttent));
  765. }
  766. int request_fast_irq(unsigned int irq,
  767. irqreturn_t (*handler)(int, void *, struct pt_regs *),
  768. unsigned long irqflags, const char *name, void *dev_id)
  769. {
  770. struct irqaction *action;
  771. struct ino_bucket *bucket = __bucket(irq);
  772. unsigned long flags;
  773. /* No pil0 dummy buckets allowed here. */
  774. if (bucket < &ivector_table[0] ||
  775. bucket >= &ivector_table[NUM_IVECS]) {
  776. unsigned int *caller;
  777. __asm__ __volatile__("mov %%i7, %0" : "=r" (caller));
  778. printk(KERN_CRIT "request_fast_irq: Old style IRQ registry attempt "
  779. "from %p, irq %08x.\n", caller, irq);
  780. return -EINVAL;
  781. }
  782. if (!handler)
  783. return -EINVAL;
  784. if ((bucket->pil == 0) || (bucket->pil == 14)) {
  785. printk("request_fast_irq: Trying to register shared IRQ 0 or 14.\n");
  786. return -EBUSY;
  787. }
  788. spin_lock_irqsave(&irq_action_lock, flags);
  789. action = *(bucket->pil + irq_action);
  790. if (action) {
  791. if (action->flags & SA_SHIRQ)
  792. panic("Trying to register fast irq when already shared.\n");
  793. if (irqflags & SA_SHIRQ)
  794. panic("Trying to register fast irq as shared.\n");
  795. printk("request_fast_irq: Trying to register yet already owned.\n");
  796. spin_unlock_irqrestore(&irq_action_lock, flags);
  797. return -EBUSY;
  798. }
  799. /*
  800. * We do not check for SA_SAMPLE_RANDOM in this path. Neither do we
  801. * support smp intr affinity in this path.
  802. */
  803. if (irqflags & SA_STATIC_ALLOC) {
  804. if (static_irq_count < MAX_STATIC_ALLOC)
  805. action = &static_irqaction[static_irq_count++];
  806. else
  807. printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed "
  808. "using kmalloc\n", bucket->pil, name);
  809. }
  810. if (action == NULL)
  811. action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
  812. GFP_ATOMIC);
  813. if (!action) {
  814. spin_unlock_irqrestore(&irq_action_lock, flags);
  815. return -ENOMEM;
  816. }
  817. install_fast_irq(bucket->pil, handler);
  818. bucket->irq_info = action;
  819. bucket->flags |= IBF_ACTIVE;
  820. action->handler = handler;
  821. action->flags = irqflags;
  822. action->dev_id = NULL;
  823. action->name = name;
  824. action->next = NULL;
  825. put_ino_in_irqaction(action, irq);
  826. put_smpaff_in_irqaction(action, CPU_MASK_NONE);
  827. *(bucket->pil + irq_action) = action;
  828. enable_irq(irq);
  829. spin_unlock_irqrestore(&irq_action_lock, flags);
  830. #ifdef CONFIG_SMP
  831. distribute_irqs();
  832. #endif
  833. return 0;
  834. }
  835. /* We really don't need these at all on the Sparc. We only have
  836. * stubs here because they are exported to modules.
  837. */
  838. unsigned long probe_irq_on(void)
  839. {
  840. return 0;
  841. }
  842. EXPORT_SYMBOL(probe_irq_on);
  843. int probe_irq_off(unsigned long mask)
  844. {
  845. return 0;
  846. }
  847. EXPORT_SYMBOL(probe_irq_off);
  848. #ifdef CONFIG_SMP
  849. static int retarget_one_irq(struct irqaction *p, int goal_cpu)
  850. {
  851. struct ino_bucket *bucket = get_ino_in_irqaction(p) + ivector_table;
  852. unsigned long imap = bucket->imap;
  853. unsigned int tid;
  854. while (!cpu_online(goal_cpu)) {
  855. if (++goal_cpu >= NR_CPUS)
  856. goal_cpu = 0;
  857. }
  858. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  859. tid = goal_cpu << 26;
  860. tid &= IMAP_AID_SAFARI;
  861. } else if (this_is_starfire == 0) {
  862. tid = goal_cpu << 26;
  863. tid &= IMAP_TID_UPA;
  864. } else {
  865. tid = (starfire_translate(imap, goal_cpu) << 26);
  866. tid &= IMAP_TID_UPA;
  867. }
  868. upa_writel(tid | IMAP_VALID, imap);
  869. do {
  870. if (++goal_cpu >= NR_CPUS)
  871. goal_cpu = 0;
  872. } while (!cpu_online(goal_cpu));
  873. return goal_cpu;
  874. }
  875. /* Called from request_irq. */
  876. static void distribute_irqs(void)
  877. {
  878. unsigned long flags;
  879. int cpu, level;
  880. spin_lock_irqsave(&irq_action_lock, flags);
  881. cpu = 0;
  882. /*
  883. * Skip the timer at [0], and very rare error/power intrs at [15].
  884. * Also level [12], it causes problems on Ex000 systems.
  885. */
  886. for (level = 1; level < NR_IRQS; level++) {
  887. struct irqaction *p = irq_action[level];
  888. if (level == 12) continue;
  889. while(p) {
  890. cpu = retarget_one_irq(p, cpu);
  891. p = p->next;
  892. }
  893. }
  894. spin_unlock_irqrestore(&irq_action_lock, flags);
  895. }
  896. #endif
  897. struct sun5_timer *prom_timers;
  898. static u64 prom_limit0, prom_limit1;
  899. static void map_prom_timers(void)
  900. {
  901. unsigned int addr[3];
  902. int tnode, err;
  903. /* PROM timer node hangs out in the top level of device siblings... */
  904. tnode = prom_finddevice("/counter-timer");
  905. /* Assume if node is not present, PROM uses different tick mechanism
  906. * which we should not care about.
  907. */
  908. if (tnode == 0 || tnode == -1) {
  909. prom_timers = (struct sun5_timer *) 0;
  910. return;
  911. }
  912. /* If PROM is really using this, it must be mapped by him. */
  913. err = prom_getproperty(tnode, "address", (char *)addr, sizeof(addr));
  914. if (err == -1) {
  915. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  916. prom_timers = (struct sun5_timer *) 0;
  917. return;
  918. }
  919. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  920. }
  921. static void kill_prom_timer(void)
  922. {
  923. if (!prom_timers)
  924. return;
  925. /* Save them away for later. */
  926. prom_limit0 = prom_timers->limit0;
  927. prom_limit1 = prom_timers->limit1;
  928. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  929. * We turn both off here just to be paranoid.
  930. */
  931. prom_timers->limit0 = 0;
  932. prom_timers->limit1 = 0;
  933. /* Wheee, eat the interrupt packet too... */
  934. __asm__ __volatile__(
  935. " mov 0x40, %%g2\n"
  936. " ldxa [%%g0] %0, %%g1\n"
  937. " ldxa [%%g2] %1, %%g1\n"
  938. " stxa %%g0, [%%g0] %0\n"
  939. " membar #Sync\n"
  940. : /* no outputs */
  941. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  942. : "g1", "g2");
  943. }
  944. void enable_prom_timer(void)
  945. {
  946. if (!prom_timers)
  947. return;
  948. /* Set it to whatever was there before. */
  949. prom_timers->limit1 = prom_limit1;
  950. prom_timers->count1 = 0;
  951. prom_timers->limit0 = prom_limit0;
  952. prom_timers->count0 = 0;
  953. }
  954. void init_irqwork_curcpu(void)
  955. {
  956. register struct irq_work_struct *workp asm("o2");
  957. register unsigned long tmp asm("o3");
  958. int cpu = hard_smp_processor_id();
  959. memset(__irq_work + cpu, 0, sizeof(*workp));
  960. /* Make sure we are called with PSTATE_IE disabled. */
  961. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  962. : "=r" (tmp));
  963. if (tmp & PSTATE_IE) {
  964. prom_printf("BUG: init_irqwork_curcpu() called with "
  965. "PSTATE_IE enabled, bailing.\n");
  966. __asm__ __volatile__("mov %%i7, %0\n\t"
  967. : "=r" (tmp));
  968. prom_printf("BUG: Called from %lx\n", tmp);
  969. prom_halt();
  970. }
  971. /* Set interrupt globals. */
  972. workp = &__irq_work[cpu];
  973. __asm__ __volatile__(
  974. "rdpr %%pstate, %0\n\t"
  975. "wrpr %0, %1, %%pstate\n\t"
  976. "mov %2, %%g6\n\t"
  977. "wrpr %0, 0x0, %%pstate\n\t"
  978. : "=&r" (tmp)
  979. : "i" (PSTATE_IG), "r" (workp));
  980. }
  981. /* Only invoked on boot processor. */
  982. void __init init_IRQ(void)
  983. {
  984. map_prom_timers();
  985. kill_prom_timer();
  986. memset(&ivector_table[0], 0, sizeof(ivector_table));
  987. /* We need to clear any IRQ's pending in the soft interrupt
  988. * registers, a spurious one could be left around from the
  989. * PROM timer which we just disabled.
  990. */
  991. clear_softint(get_softint());
  992. /* Now that ivector table is initialized, it is safe
  993. * to receive IRQ vector traps. We will normally take
  994. * one or two right now, in case some device PROM used
  995. * to boot us wants to speak to us. We just ignore them.
  996. */
  997. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  998. "or %%g1, %0, %%g1\n\t"
  999. "wrpr %%g1, 0x0, %%pstate"
  1000. : /* No outputs */
  1001. : "i" (PSTATE_IE)
  1002. : "g1");
  1003. }
  1004. static struct proc_dir_entry * root_irq_dir;
  1005. static struct proc_dir_entry * irq_dir [NUM_IVECS];
  1006. #ifdef CONFIG_SMP
  1007. static int irq_affinity_read_proc (char *page, char **start, off_t off,
  1008. int count, int *eof, void *data)
  1009. {
  1010. struct ino_bucket *bp = ivector_table + (long)data;
  1011. struct irqaction *ap = bp->irq_info;
  1012. cpumask_t mask;
  1013. int len;
  1014. mask = get_smpaff_in_irqaction(ap);
  1015. if (cpus_empty(mask))
  1016. mask = cpu_online_map;
  1017. len = cpumask_scnprintf(page, count, mask);
  1018. if (count - len < 2)
  1019. return -EINVAL;
  1020. len += sprintf(page + len, "\n");
  1021. return len;
  1022. }
  1023. static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
  1024. {
  1025. struct ino_bucket *bp = ivector_table + irq;
  1026. /* Users specify affinity in terms of hw cpu ids.
  1027. * As soon as we do this, handler_irq() might see and take action.
  1028. */
  1029. put_smpaff_in_irqaction((struct irqaction *)bp->irq_info, hw_aff);
  1030. /* Migration is simply done by the next cpu to service this
  1031. * interrupt.
  1032. */
  1033. }
  1034. static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
  1035. unsigned long count, void *data)
  1036. {
  1037. int irq = (long) data, full_count = count, err;
  1038. cpumask_t new_value;
  1039. err = cpumask_parse(buffer, count, new_value);
  1040. /*
  1041. * Do not allow disabling IRQs completely - it's a too easy
  1042. * way to make the system unusable accidentally :-) At least
  1043. * one online CPU still has to be targeted.
  1044. */
  1045. cpus_and(new_value, new_value, cpu_online_map);
  1046. if (cpus_empty(new_value))
  1047. return -EINVAL;
  1048. set_intr_affinity(irq, new_value);
  1049. return full_count;
  1050. }
  1051. #endif
  1052. #define MAX_NAMELEN 10
  1053. static void register_irq_proc (unsigned int irq)
  1054. {
  1055. char name [MAX_NAMELEN];
  1056. if (!root_irq_dir || irq_dir[irq])
  1057. return;
  1058. memset(name, 0, MAX_NAMELEN);
  1059. sprintf(name, "%x", irq);
  1060. /* create /proc/irq/1234 */
  1061. irq_dir[irq] = proc_mkdir(name, root_irq_dir);
  1062. #ifdef CONFIG_SMP
  1063. /* XXX SMP affinity not supported on starfire yet. */
  1064. if (this_is_starfire == 0) {
  1065. struct proc_dir_entry *entry;
  1066. /* create /proc/irq/1234/smp_affinity */
  1067. entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
  1068. if (entry) {
  1069. entry->nlink = 1;
  1070. entry->data = (void *)(long)irq;
  1071. entry->read_proc = irq_affinity_read_proc;
  1072. entry->write_proc = irq_affinity_write_proc;
  1073. }
  1074. }
  1075. #endif
  1076. }
  1077. void init_irq_proc (void)
  1078. {
  1079. /* create /proc/irq */
  1080. root_irq_dir = proc_mkdir("irq", NULL);
  1081. }