irq_ia64.c 14 KB

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