irq_ia64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. static cpumask_t vector_allocation_domain(int cpu);
  54. /*
  55. * Legacy IRQ to IA-64 vector translation table.
  56. */
  57. __u8 isa_irq_to_vector_map[16] = {
  58. /* 8259 IRQ translation, first 16 entries */
  59. 0x2f, 0x20, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29,
  60. 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21
  61. };
  62. EXPORT_SYMBOL(isa_irq_to_vector_map);
  63. DEFINE_SPINLOCK(vector_lock);
  64. struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
  65. [0 ... NR_IRQS - 1] = {
  66. .vector = IRQ_VECTOR_UNASSIGNED,
  67. .domain = CPU_MASK_NONE
  68. }
  69. };
  70. DEFINE_PER_CPU(int[IA64_NUM_VECTORS], vector_irq) = {
  71. [0 ... IA64_NUM_VECTORS - 1] = IA64_SPURIOUS_INT_VECTOR
  72. };
  73. static cpumask_t vector_table[IA64_MAX_DEVICE_VECTORS] = {
  74. [0 ... IA64_MAX_DEVICE_VECTORS - 1] = CPU_MASK_NONE
  75. };
  76. static int irq_status[NR_IRQS] = {
  77. [0 ... NR_IRQS -1] = IRQ_UNUSED
  78. };
  79. int check_irq_used(int irq)
  80. {
  81. if (irq_status[irq] == IRQ_USED)
  82. return 1;
  83. return -1;
  84. }
  85. static void reserve_irq(unsigned int irq)
  86. {
  87. unsigned long flags;
  88. spin_lock_irqsave(&vector_lock, flags);
  89. irq_status[irq] = IRQ_RSVD;
  90. spin_unlock_irqrestore(&vector_lock, flags);
  91. }
  92. static inline int find_unassigned_irq(void)
  93. {
  94. int irq;
  95. for (irq = IA64_FIRST_DEVICE_VECTOR; irq < NR_IRQS; irq++)
  96. if (irq_status[irq] == IRQ_UNUSED)
  97. return irq;
  98. return -ENOSPC;
  99. }
  100. static inline int find_unassigned_vector(cpumask_t domain)
  101. {
  102. cpumask_t mask;
  103. int pos;
  104. cpus_and(mask, domain, cpu_online_map);
  105. if (cpus_empty(mask))
  106. return -EINVAL;
  107. for (pos = 0; pos < IA64_NUM_DEVICE_VECTORS; pos++) {
  108. cpus_and(mask, domain, vector_table[pos]);
  109. if (!cpus_empty(mask))
  110. continue;
  111. return IA64_FIRST_DEVICE_VECTOR + pos;
  112. }
  113. return -ENOSPC;
  114. }
  115. static int __bind_irq_vector(int irq, int vector, cpumask_t domain)
  116. {
  117. cpumask_t mask;
  118. int cpu, pos;
  119. struct irq_cfg *cfg = &irq_cfg[irq];
  120. cpus_and(mask, domain, cpu_online_map);
  121. if (cpus_empty(mask))
  122. return -EINVAL;
  123. if ((cfg->vector == vector) && cpus_equal(cfg->domain, domain))
  124. return 0;
  125. if (cfg->vector != IRQ_VECTOR_UNASSIGNED)
  126. return -EBUSY;
  127. for_each_cpu_mask(cpu, mask)
  128. per_cpu(vector_irq, cpu)[vector] = irq;
  129. cfg->vector = vector;
  130. cfg->domain = domain;
  131. irq_status[irq] = IRQ_USED;
  132. pos = vector - IA64_FIRST_DEVICE_VECTOR;
  133. cpus_or(vector_table[pos], vector_table[pos], domain);
  134. return 0;
  135. }
  136. int bind_irq_vector(int irq, int vector, cpumask_t domain)
  137. {
  138. unsigned long flags;
  139. int ret;
  140. spin_lock_irqsave(&vector_lock, flags);
  141. ret = __bind_irq_vector(irq, vector, domain);
  142. spin_unlock_irqrestore(&vector_lock, flags);
  143. return ret;
  144. }
  145. static void clear_irq_vector(int irq)
  146. {
  147. unsigned long flags;
  148. int vector, cpu, pos;
  149. cpumask_t mask;
  150. cpumask_t domain;
  151. struct irq_cfg *cfg = &irq_cfg[irq];
  152. spin_lock_irqsave(&vector_lock, flags);
  153. BUG_ON((unsigned)irq >= NR_IRQS);
  154. BUG_ON(cfg->vector == IRQ_VECTOR_UNASSIGNED);
  155. vector = cfg->vector;
  156. domain = cfg->domain;
  157. cpus_and(mask, cfg->domain, cpu_online_map);
  158. for_each_cpu_mask(cpu, mask)
  159. per_cpu(vector_irq, cpu)[vector] = IA64_SPURIOUS_INT_VECTOR;
  160. cfg->vector = IRQ_VECTOR_UNASSIGNED;
  161. cfg->domain = CPU_MASK_NONE;
  162. irq_status[irq] = IRQ_UNUSED;
  163. pos = vector - IA64_FIRST_DEVICE_VECTOR;
  164. cpus_andnot(vector_table[pos], vector_table[pos], domain);
  165. spin_unlock_irqrestore(&vector_lock, flags);
  166. }
  167. int
  168. assign_irq_vector (int irq)
  169. {
  170. unsigned long flags;
  171. int vector, cpu;
  172. cpumask_t domain;
  173. vector = -ENOSPC;
  174. spin_lock_irqsave(&vector_lock, flags);
  175. if (irq < 0) {
  176. goto out;
  177. }
  178. for_each_online_cpu(cpu) {
  179. domain = vector_allocation_domain(cpu);
  180. vector = find_unassigned_vector(domain);
  181. if (vector >= 0)
  182. break;
  183. }
  184. if (vector < 0)
  185. goto out;
  186. BUG_ON(__bind_irq_vector(irq, vector, domain));
  187. out:
  188. spin_unlock_irqrestore(&vector_lock, flags);
  189. return vector;
  190. }
  191. void
  192. free_irq_vector (int vector)
  193. {
  194. if (vector < IA64_FIRST_DEVICE_VECTOR ||
  195. vector > IA64_LAST_DEVICE_VECTOR)
  196. return;
  197. clear_irq_vector(vector);
  198. }
  199. int
  200. reserve_irq_vector (int vector)
  201. {
  202. if (vector < IA64_FIRST_DEVICE_VECTOR ||
  203. vector > IA64_LAST_DEVICE_VECTOR)
  204. return -EINVAL;
  205. return !!bind_irq_vector(vector, vector, CPU_MASK_ALL);
  206. }
  207. /*
  208. * Initialize vector_irq on a new cpu. This function must be called
  209. * with vector_lock held.
  210. */
  211. void __setup_vector_irq(int cpu)
  212. {
  213. int irq, vector;
  214. /* Clear vector_irq */
  215. for (vector = 0; vector < IA64_NUM_VECTORS; ++vector)
  216. per_cpu(vector_irq, cpu)[vector] = IA64_SPURIOUS_INT_VECTOR;
  217. /* Mark the inuse vectors */
  218. for (irq = 0; irq < NR_IRQS; ++irq) {
  219. if (!cpu_isset(cpu, irq_cfg[irq].domain))
  220. continue;
  221. vector = irq_to_vector(irq);
  222. per_cpu(vector_irq, cpu)[vector] = irq;
  223. }
  224. }
  225. static cpumask_t vector_allocation_domain(int cpu)
  226. {
  227. return CPU_MASK_ALL;
  228. }
  229. void destroy_and_reserve_irq(unsigned int irq)
  230. {
  231. dynamic_irq_cleanup(irq);
  232. clear_irq_vector(irq);
  233. reserve_irq(irq);
  234. }
  235. /*
  236. * Dynamic irq allocate and deallocation for MSI
  237. */
  238. int create_irq(void)
  239. {
  240. unsigned long flags;
  241. int irq, vector, cpu;
  242. cpumask_t domain;
  243. irq = vector = -ENOSPC;
  244. spin_lock_irqsave(&vector_lock, flags);
  245. for_each_online_cpu(cpu) {
  246. domain = vector_allocation_domain(cpu);
  247. vector = find_unassigned_vector(domain);
  248. if (vector >= 0)
  249. break;
  250. }
  251. if (vector < 0)
  252. goto out;
  253. irq = find_unassigned_irq();
  254. if (irq < 0)
  255. goto out;
  256. BUG_ON(__bind_irq_vector(irq, vector, domain));
  257. out:
  258. spin_unlock_irqrestore(&vector_lock, flags);
  259. if (irq >= 0)
  260. dynamic_irq_init(irq);
  261. return irq;
  262. }
  263. void destroy_irq(unsigned int irq)
  264. {
  265. dynamic_irq_cleanup(irq);
  266. clear_irq_vector(irq);
  267. }
  268. #ifdef CONFIG_SMP
  269. # define IS_RESCHEDULE(vec) (vec == IA64_IPI_RESCHEDULE)
  270. # define IS_LOCAL_TLB_FLUSH(vec) (vec == IA64_IPI_LOCAL_TLB_FLUSH)
  271. #else
  272. # define IS_RESCHEDULE(vec) (0)
  273. # define IS_LOCAL_TLB_FLUSH(vec) (0)
  274. #endif
  275. /*
  276. * That's where the IVT branches when we get an external
  277. * interrupt. This branches to the correct hardware IRQ handler via
  278. * function ptr.
  279. */
  280. void
  281. ia64_handle_irq (ia64_vector vector, struct pt_regs *regs)
  282. {
  283. struct pt_regs *old_regs = set_irq_regs(regs);
  284. unsigned long saved_tpr;
  285. #if IRQ_DEBUG
  286. {
  287. unsigned long bsp, sp;
  288. /*
  289. * Note: if the interrupt happened while executing in
  290. * the context switch routine (ia64_switch_to), we may
  291. * get a spurious stack overflow here. This is
  292. * because the register and the memory stack are not
  293. * switched atomically.
  294. */
  295. bsp = ia64_getreg(_IA64_REG_AR_BSP);
  296. sp = ia64_getreg(_IA64_REG_SP);
  297. if ((sp - bsp) < 1024) {
  298. static unsigned char count;
  299. static long last_time;
  300. if (jiffies - last_time > 5*HZ)
  301. count = 0;
  302. if (++count < 5) {
  303. last_time = jiffies;
  304. printk("ia64_handle_irq: DANGER: less than "
  305. "1KB of free stack space!!\n"
  306. "(bsp=0x%lx, sp=%lx)\n", bsp, sp);
  307. }
  308. }
  309. }
  310. #endif /* IRQ_DEBUG */
  311. /*
  312. * Always set TPR to limit maximum interrupt nesting depth to
  313. * 16 (without this, it would be ~240, which could easily lead
  314. * to kernel stack overflows).
  315. */
  316. irq_enter();
  317. saved_tpr = ia64_getreg(_IA64_REG_CR_TPR);
  318. ia64_srlz_d();
  319. while (vector != IA64_SPURIOUS_INT_VECTOR) {
  320. if (unlikely(IS_LOCAL_TLB_FLUSH(vector))) {
  321. smp_local_flush_tlb();
  322. kstat_this_cpu.irqs[vector]++;
  323. } else if (unlikely(IS_RESCHEDULE(vector)))
  324. kstat_this_cpu.irqs[vector]++;
  325. else {
  326. ia64_setreg(_IA64_REG_CR_TPR, vector);
  327. ia64_srlz_d();
  328. generic_handle_irq(local_vector_to_irq(vector));
  329. /*
  330. * Disable interrupts and send EOI:
  331. */
  332. local_irq_disable();
  333. ia64_setreg(_IA64_REG_CR_TPR, saved_tpr);
  334. }
  335. ia64_eoi();
  336. vector = ia64_get_ivr();
  337. }
  338. /*
  339. * This must be done *after* the ia64_eoi(). For example, the keyboard softirq
  340. * handler needs to be able to wait for further keyboard interrupts, which can't
  341. * come through until ia64_eoi() has been done.
  342. */
  343. irq_exit();
  344. set_irq_regs(old_regs);
  345. }
  346. #ifdef CONFIG_HOTPLUG_CPU
  347. /*
  348. * This function emulates a interrupt processing when a cpu is about to be
  349. * brought down.
  350. */
  351. void ia64_process_pending_intr(void)
  352. {
  353. ia64_vector vector;
  354. unsigned long saved_tpr;
  355. extern unsigned int vectors_in_migration[NR_IRQS];
  356. vector = ia64_get_ivr();
  357. irq_enter();
  358. saved_tpr = ia64_getreg(_IA64_REG_CR_TPR);
  359. ia64_srlz_d();
  360. /*
  361. * Perform normal interrupt style processing
  362. */
  363. while (vector != IA64_SPURIOUS_INT_VECTOR) {
  364. if (unlikely(IS_LOCAL_TLB_FLUSH(vector))) {
  365. smp_local_flush_tlb();
  366. kstat_this_cpu.irqs[vector]++;
  367. } else if (unlikely(IS_RESCHEDULE(vector)))
  368. kstat_this_cpu.irqs[vector]++;
  369. else {
  370. struct pt_regs *old_regs = set_irq_regs(NULL);
  371. ia64_setreg(_IA64_REG_CR_TPR, vector);
  372. ia64_srlz_d();
  373. /*
  374. * Now try calling normal ia64_handle_irq as it would have got called
  375. * from a real intr handler. Try passing null for pt_regs, hopefully
  376. * it will work. I hope it works!.
  377. * Probably could shared code.
  378. */
  379. vectors_in_migration[local_vector_to_irq(vector)]=0;
  380. generic_handle_irq(local_vector_to_irq(vector));
  381. set_irq_regs(old_regs);
  382. /*
  383. * Disable interrupts and send EOI
  384. */
  385. local_irq_disable();
  386. ia64_setreg(_IA64_REG_CR_TPR, saved_tpr);
  387. }
  388. ia64_eoi();
  389. vector = ia64_get_ivr();
  390. }
  391. irq_exit();
  392. }
  393. #endif
  394. #ifdef CONFIG_SMP
  395. static irqreturn_t dummy_handler (int irq, void *dev_id)
  396. {
  397. BUG();
  398. }
  399. extern irqreturn_t handle_IPI (int irq, void *dev_id);
  400. static struct irqaction ipi_irqaction = {
  401. .handler = handle_IPI,
  402. .flags = IRQF_DISABLED,
  403. .name = "IPI"
  404. };
  405. static struct irqaction resched_irqaction = {
  406. .handler = dummy_handler,
  407. .flags = IRQF_DISABLED,
  408. .name = "resched"
  409. };
  410. static struct irqaction tlb_irqaction = {
  411. .handler = dummy_handler,
  412. .flags = IRQF_DISABLED,
  413. .name = "tlb_flush"
  414. };
  415. #endif
  416. void
  417. register_percpu_irq (ia64_vector vec, struct irqaction *action)
  418. {
  419. irq_desc_t *desc;
  420. unsigned int irq;
  421. irq = vec;
  422. BUG_ON(bind_irq_vector(irq, vec, CPU_MASK_ALL));
  423. desc = irq_desc + irq;
  424. desc->status |= IRQ_PER_CPU;
  425. desc->chip = &irq_type_ia64_lsapic;
  426. if (action)
  427. setup_irq(irq, action);
  428. }
  429. void __init
  430. init_IRQ (void)
  431. {
  432. register_percpu_irq(IA64_SPURIOUS_INT_VECTOR, NULL);
  433. #ifdef CONFIG_SMP
  434. register_percpu_irq(IA64_IPI_VECTOR, &ipi_irqaction);
  435. register_percpu_irq(IA64_IPI_RESCHEDULE, &resched_irqaction);
  436. register_percpu_irq(IA64_IPI_LOCAL_TLB_FLUSH, &tlb_irqaction);
  437. #endif
  438. #ifdef CONFIG_PERFMON
  439. pfm_init_percpu();
  440. #endif
  441. platform_irq_init();
  442. }
  443. void
  444. ia64_send_ipi (int cpu, int vector, int delivery_mode, int redirect)
  445. {
  446. void __iomem *ipi_addr;
  447. unsigned long ipi_data;
  448. unsigned long phys_cpu_id;
  449. #ifdef CONFIG_SMP
  450. phys_cpu_id = cpu_physical_id(cpu);
  451. #else
  452. phys_cpu_id = (ia64_getreg(_IA64_REG_CR_LID) >> 16) & 0xffff;
  453. #endif
  454. /*
  455. * cpu number is in 8bit ID and 8bit EID
  456. */
  457. ipi_data = (delivery_mode << 8) | (vector & 0xff);
  458. ipi_addr = ipi_base_addr + ((phys_cpu_id << 4) | ((redirect & 1) << 3));
  459. writeq(ipi_data, ipi_addr);
  460. }