irq_ia64.c 13 KB

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