irq.c 28 KB

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