irq_ia64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * linux/arch/ia64/kernel/irq_ia64.c
  3. *
  4. * Copyright (C) 1998-2001 Hewlett-Packard Co
  5. * Stephane Eranian <eranian@hpl.hp.com>
  6. * David Mosberger-Tang <davidm@hpl.hp.com>
  7. *
  8. * 6/10/99: Updated to bring in sync with x86 version to facilitate
  9. * support for SMP and different interrupt controllers.
  10. *
  11. * 09/15/00 Goutham Rao <goutham.rao@intel.com> Implemented pci_irq_to_vector
  12. * PCI to vector allocation routine.
  13. * 04/14/2004 Ashok Raj <ashok.raj@intel.com>
  14. * Added CPU Hotplug handling for IPF.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/ioport.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/random.h> /* for rand_initialize_irq() */
  26. #include <linux/signal.h>
  27. #include <linux/smp.h>
  28. #include <linux/threads.h>
  29. #include <linux/bitops.h>
  30. #include <linux/irq.h>
  31. #include <asm/delay.h>
  32. #include <asm/intrinsics.h>
  33. #include <asm/io.h>
  34. #include <asm/hw_irq.h>
  35. #include <asm/machvec.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/system.h>
  38. #include <asm/tlbflush.h>
  39. #ifdef CONFIG_PERFMON
  40. # include <asm/perfmon.h>
  41. #endif
  42. #define IRQ_DEBUG 0
  43. #define IRQ_VECTOR_UNASSIGNED (0)
  44. #define IRQ_UNUSED (0)
  45. #define IRQ_USED (1)
  46. #define IRQ_RSVD (2)
  47. /* These can be overridden in platform_irq_init */
  48. int ia64_first_device_vector = IA64_DEF_FIRST_DEVICE_VECTOR;
  49. int ia64_last_device_vector = IA64_DEF_LAST_DEVICE_VECTOR;
  50. /* default base addr of IPI table */
  51. void __iomem *ipi_base_addr = ((void __iomem *)
  52. (__IA64_UNCACHED_OFFSET | IA64_IPI_DEFAULT_BASE_ADDR));
  53. /*
  54. * Legacy IRQ to IA-64 vector translation table.
  55. */
  56. __u8 isa_irq_to_vector_map[16] = {
  57. /* 8259 IRQ translation, first 16 entries */
  58. 0x2f, 0x20, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29,
  59. 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21
  60. };
  61. EXPORT_SYMBOL(isa_irq_to_vector_map);
  62. DEFINE_SPINLOCK(vector_lock);
  63. struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  64. [0 ... NR_IRQS - 1] = { .vector = IRQ_VECTOR_UNASSIGNED }
  65. };
  66. DEFINE_PER_CPU(int[IA64_NUM_VECTORS], vector_irq) = {
  67. [0 ... IA64_NUM_VECTORS - 1] = IA64_SPURIOUS_INT_VECTOR
  68. };
  69. static int irq_status[NR_IRQS] = {
  70. [0 ... NR_IRQS -1] = IRQ_UNUSED
  71. };
  72. int check_irq_used(int irq)
  73. {
  74. if (irq_status[irq] == IRQ_USED)
  75. return 1;
  76. return -1;
  77. }
  78. static void reserve_irq(unsigned int irq)
  79. {
  80. unsigned long flags;
  81. spin_lock_irqsave(&vector_lock, flags);
  82. irq_status[irq] = IRQ_RSVD;
  83. spin_unlock_irqrestore(&vector_lock, flags);
  84. }
  85. static inline int find_unassigned_irq(void)
  86. {
  87. int irq;
  88. for (irq = IA64_FIRST_DEVICE_VECTOR; irq < NR_IRQS; irq++)
  89. if (irq_status[irq] == IRQ_UNUSED)
  90. return irq;
  91. return -ENOSPC;
  92. }
  93. static inline int find_unassigned_vector(void)
  94. {
  95. int vector;
  96. for (vector = IA64_FIRST_DEVICE_VECTOR;
  97. vector <= IA64_LAST_DEVICE_VECTOR; vector++)
  98. if (__get_cpu_var(vector_irq[vector]) == IA64_SPURIOUS_INT_VECTOR)
  99. return vector;
  100. return -ENOSPC;
  101. }
  102. static int __bind_irq_vector(int irq, int vector)
  103. {
  104. int cpu;
  105. if (irq_to_vector(irq) == vector)
  106. return 0;
  107. if (irq_to_vector(irq) != IRQ_VECTOR_UNASSIGNED)
  108. return -EBUSY;
  109. for_each_online_cpu(cpu)
  110. per_cpu(vector_irq, cpu)[vector] = irq;
  111. irq_cfg[irq].vector = vector;
  112. irq_status[irq] = IRQ_USED;
  113. return 0;
  114. }
  115. int bind_irq_vector(int irq, int vector)
  116. {
  117. unsigned long flags;
  118. int ret;
  119. spin_lock_irqsave(&vector_lock, flags);
  120. ret = __bind_irq_vector(irq, vector);
  121. spin_unlock_irqrestore(&vector_lock, flags);
  122. return ret;
  123. }
  124. static void clear_irq_vector(int irq)
  125. {
  126. unsigned long flags;
  127. int vector, cpu;
  128. spin_lock_irqsave(&vector_lock, flags);
  129. BUG_ON((unsigned)irq >= NR_IRQS);
  130. BUG_ON(irq_cfg[irq].vector == IRQ_VECTOR_UNASSIGNED);
  131. vector = irq_cfg[irq].vector;
  132. for_each_online_cpu(cpu)
  133. per_cpu(vector_irq, cpu)[vector] = IA64_SPURIOUS_INT_VECTOR;
  134. irq_cfg[irq].vector = IRQ_VECTOR_UNASSIGNED;
  135. irq_status[irq] = IRQ_UNUSED;
  136. spin_unlock_irqrestore(&vector_lock, flags);
  137. }
  138. int
  139. assign_irq_vector (int irq)
  140. {
  141. unsigned long flags;
  142. int vector = -ENOSPC;
  143. if (irq < 0) {
  144. goto out;
  145. }
  146. spin_lock_irqsave(&vector_lock, flags);
  147. vector = find_unassigned_vector();
  148. if (vector < 0)
  149. goto out;
  150. BUG_ON(__bind_irq_vector(irq, vector));
  151. spin_unlock_irqrestore(&vector_lock, flags);
  152. out:
  153. return vector;
  154. }
  155. void
  156. free_irq_vector (int vector)
  157. {
  158. if (vector < IA64_FIRST_DEVICE_VECTOR ||
  159. vector > IA64_LAST_DEVICE_VECTOR)
  160. return;
  161. clear_irq_vector(vector);
  162. }
  163. int
  164. reserve_irq_vector (int vector)
  165. {
  166. if (vector < IA64_FIRST_DEVICE_VECTOR ||
  167. vector > IA64_LAST_DEVICE_VECTOR)
  168. return -EINVAL;
  169. return !!bind_irq_vector(vector, vector);
  170. }
  171. /*
  172. * Initialize vector_irq on a new cpu. This function must be called
  173. * with vector_lock held.
  174. */
  175. void __setup_vector_irq(int cpu)
  176. {
  177. int irq, vector;
  178. /* Clear vector_irq */
  179. for (vector = 0; vector < IA64_NUM_VECTORS; ++vector)
  180. per_cpu(vector_irq, cpu)[vector] = IA64_SPURIOUS_INT_VECTOR;
  181. /* Mark the inuse vectors */
  182. for (irq = 0; irq < NR_IRQS; ++irq) {
  183. if ((vector = irq_to_vector(irq)) != IRQ_VECTOR_UNASSIGNED)
  184. per_cpu(vector_irq, cpu)[vector] = irq;
  185. }
  186. }
  187. void destroy_and_reserve_irq(unsigned int irq)
  188. {
  189. dynamic_irq_cleanup(irq);
  190. clear_irq_vector(irq);
  191. reserve_irq(irq);
  192. }
  193. /*
  194. * Dynamic irq allocate and deallocation for MSI
  195. */
  196. int create_irq(void)
  197. {
  198. unsigned long flags;
  199. int irq, vector;
  200. irq = -ENOSPC;
  201. spin_lock_irqsave(&vector_lock, flags);
  202. vector = find_unassigned_vector();
  203. if (vector < 0)
  204. goto out;
  205. irq = find_unassigned_irq();
  206. if (irq < 0)
  207. goto out;
  208. BUG_ON(__bind_irq_vector(irq, vector));
  209. out:
  210. spin_unlock_irqrestore(&vector_lock, flags);
  211. if (irq >= 0)
  212. dynamic_irq_init(irq);
  213. return irq;
  214. }
  215. void destroy_irq(unsigned int irq)
  216. {
  217. dynamic_irq_cleanup(irq);
  218. clear_irq_vector(irq);
  219. }
  220. #ifdef CONFIG_SMP
  221. # define IS_RESCHEDULE(vec) (vec == IA64_IPI_RESCHEDULE)
  222. # define IS_LOCAL_TLB_FLUSH(vec) (vec == IA64_IPI_LOCAL_TLB_FLUSH)
  223. #else
  224. # define IS_RESCHEDULE(vec) (0)
  225. # define IS_LOCAL_TLB_FLUSH(vec) (0)
  226. #endif
  227. /*
  228. * That's where the IVT branches when we get an external
  229. * interrupt. This branches to the correct hardware IRQ handler via
  230. * function ptr.
  231. */
  232. void
  233. ia64_handle_irq (ia64_vector vector, struct pt_regs *regs)
  234. {
  235. struct pt_regs *old_regs = set_irq_regs(regs);
  236. unsigned long saved_tpr;
  237. #if IRQ_DEBUG
  238. {
  239. unsigned long bsp, sp;
  240. /*
  241. * Note: if the interrupt happened while executing in
  242. * the context switch routine (ia64_switch_to), we may
  243. * get a spurious stack overflow here. This is
  244. * because the register and the memory stack are not
  245. * switched atomically.
  246. */
  247. bsp = ia64_getreg(_IA64_REG_AR_BSP);
  248. sp = ia64_getreg(_IA64_REG_SP);
  249. if ((sp - bsp) < 1024) {
  250. static unsigned char count;
  251. static long last_time;
  252. if (jiffies - last_time > 5*HZ)
  253. count = 0;
  254. if (++count < 5) {
  255. last_time = jiffies;
  256. printk("ia64_handle_irq: DANGER: less than "
  257. "1KB of free stack space!!\n"
  258. "(bsp=0x%lx, sp=%lx)\n", bsp, sp);
  259. }
  260. }
  261. }
  262. #endif /* IRQ_DEBUG */
  263. /*
  264. * Always set TPR to limit maximum interrupt nesting depth to
  265. * 16 (without this, it would be ~240, which could easily lead
  266. * to kernel stack overflows).
  267. */
  268. irq_enter();
  269. saved_tpr = ia64_getreg(_IA64_REG_CR_TPR);
  270. ia64_srlz_d();
  271. while (vector != IA64_SPURIOUS_INT_VECTOR) {
  272. if (unlikely(IS_LOCAL_TLB_FLUSH(vector))) {
  273. smp_local_flush_tlb();
  274. kstat_this_cpu.irqs[vector]++;
  275. } else if (unlikely(IS_RESCHEDULE(vector)))
  276. kstat_this_cpu.irqs[vector]++;
  277. else {
  278. ia64_setreg(_IA64_REG_CR_TPR, vector);
  279. ia64_srlz_d();
  280. generic_handle_irq(local_vector_to_irq(vector));
  281. /*
  282. * Disable interrupts and send EOI:
  283. */
  284. local_irq_disable();
  285. ia64_setreg(_IA64_REG_CR_TPR, saved_tpr);
  286. }
  287. ia64_eoi();
  288. vector = ia64_get_ivr();
  289. }
  290. /*
  291. * This must be done *after* the ia64_eoi(). For example, the keyboard softirq
  292. * handler needs to be able to wait for further keyboard interrupts, which can't
  293. * come through until ia64_eoi() has been done.
  294. */
  295. irq_exit();
  296. set_irq_regs(old_regs);
  297. }
  298. #ifdef CONFIG_HOTPLUG_CPU
  299. /*
  300. * This function emulates a interrupt processing when a cpu is about to be
  301. * brought down.
  302. */
  303. void ia64_process_pending_intr(void)
  304. {
  305. ia64_vector vector;
  306. unsigned long saved_tpr;
  307. extern unsigned int vectors_in_migration[NR_IRQS];
  308. vector = ia64_get_ivr();
  309. irq_enter();
  310. saved_tpr = ia64_getreg(_IA64_REG_CR_TPR);
  311. ia64_srlz_d();
  312. /*
  313. * Perform normal interrupt style processing
  314. */
  315. while (vector != IA64_SPURIOUS_INT_VECTOR) {
  316. if (unlikely(IS_LOCAL_TLB_FLUSH(vector))) {
  317. smp_local_flush_tlb();
  318. kstat_this_cpu.irqs[vector]++;
  319. } else if (unlikely(IS_RESCHEDULE(vector)))
  320. kstat_this_cpu.irqs[vector]++;
  321. else {
  322. struct pt_regs *old_regs = set_irq_regs(NULL);
  323. ia64_setreg(_IA64_REG_CR_TPR, vector);
  324. ia64_srlz_d();
  325. /*
  326. * Now try calling normal ia64_handle_irq as it would have got called
  327. * from a real intr handler. Try passing null for pt_regs, hopefully
  328. * it will work. I hope it works!.
  329. * Probably could shared code.
  330. */
  331. vectors_in_migration[local_vector_to_irq(vector)]=0;
  332. generic_handle_irq(local_vector_to_irq(vector));
  333. set_irq_regs(old_regs);
  334. /*
  335. * Disable interrupts and send EOI
  336. */
  337. local_irq_disable();
  338. ia64_setreg(_IA64_REG_CR_TPR, saved_tpr);
  339. }
  340. ia64_eoi();
  341. vector = ia64_get_ivr();
  342. }
  343. irq_exit();
  344. }
  345. #endif
  346. #ifdef CONFIG_SMP
  347. static irqreturn_t dummy_handler (int irq, void *dev_id)
  348. {
  349. BUG();
  350. }
  351. extern irqreturn_t handle_IPI (int irq, void *dev_id);
  352. static struct irqaction ipi_irqaction = {
  353. .handler = handle_IPI,
  354. .flags = IRQF_DISABLED,
  355. .name = "IPI"
  356. };
  357. static struct irqaction resched_irqaction = {
  358. .handler = dummy_handler,
  359. .flags = IRQF_DISABLED,
  360. .name = "resched"
  361. };
  362. static struct irqaction tlb_irqaction = {
  363. .handler = dummy_handler,
  364. .flags = IRQF_DISABLED,
  365. .name = "tlb_flush"
  366. };
  367. #endif
  368. void
  369. register_percpu_irq (ia64_vector vec, struct irqaction *action)
  370. {
  371. irq_desc_t *desc;
  372. unsigned int irq;
  373. irq = vec;
  374. BUG_ON(bind_irq_vector(irq, vec));
  375. desc = irq_desc + irq;
  376. desc->status |= IRQ_PER_CPU;
  377. desc->chip = &irq_type_ia64_lsapic;
  378. if (action)
  379. setup_irq(irq, action);
  380. }
  381. void __init
  382. init_IRQ (void)
  383. {
  384. register_percpu_irq(IA64_SPURIOUS_INT_VECTOR, NULL);
  385. #ifdef CONFIG_SMP
  386. register_percpu_irq(IA64_IPI_VECTOR, &ipi_irqaction);
  387. register_percpu_irq(IA64_IPI_RESCHEDULE, &resched_irqaction);
  388. register_percpu_irq(IA64_IPI_LOCAL_TLB_FLUSH, &tlb_irqaction);
  389. #endif
  390. #ifdef CONFIG_PERFMON
  391. pfm_init_percpu();
  392. #endif
  393. platform_irq_init();
  394. }
  395. void
  396. ia64_send_ipi (int cpu, int vector, int delivery_mode, int redirect)
  397. {
  398. void __iomem *ipi_addr;
  399. unsigned long ipi_data;
  400. unsigned long phys_cpu_id;
  401. #ifdef CONFIG_SMP
  402. phys_cpu_id = cpu_physical_id(cpu);
  403. #else
  404. phys_cpu_id = (ia64_getreg(_IA64_REG_CR_LID) >> 16) & 0xffff;
  405. #endif
  406. /*
  407. * cpu number is in 8bit ID and 8bit EID
  408. */
  409. ipi_data = (delivery_mode << 8) | (vector & 0xff);
  410. ipi_addr = ipi_base_addr + ((phys_cpu_id << 4) | ((redirect & 1) << 3));
  411. writeq(ipi_data, ipi_addr);
  412. }