irq_ia64.c 13 KB

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