iosapic.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * I/O SAPIC support.
  4. *
  5. * Copyright (C) 1999 Intel Corp.
  6. * Copyright (C) 1999 Asit Mallick <asit.k.mallick@intel.com>
  7. * Copyright (C) 2000-2002 J.I. Lee <jung-ik.lee@intel.com>
  8. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co.
  9. * David Mosberger-Tang <davidm@hpl.hp.com>
  10. * Copyright (C) 1999 VA Linux Systems
  11. * Copyright (C) 1999,2000 Walt Drummond <drummond@valinux.com>
  12. *
  13. * 00/04/19 D. Mosberger Rewritten to mirror more closely the x86 I/O
  14. * APIC code. In particular, we now have separate
  15. * handlers for edge and level triggered
  16. * interrupts.
  17. * 00/10/27 Asit Mallick, Goutham Rao <goutham.rao@intel.com> IRQ vector
  18. * allocation PCI to vector mapping, shared PCI
  19. * interrupts.
  20. * 00/10/27 D. Mosberger Document things a bit more to make them more
  21. * understandable. Clean up much of the old
  22. * IOSAPIC cruft.
  23. * 01/07/27 J.I. Lee PCI irq routing, Platform/Legacy interrupts
  24. * and fixes for ACPI S5(SoftOff) support.
  25. * 02/01/23 J.I. Lee iosapic pgm fixes for PCI irq routing from _PRT
  26. * 02/01/07 E. Focht <efocht@ess.nec.de> Redirectable interrupt
  27. * vectors in iosapic_set_affinity(),
  28. * initializations for /proc/irq/#/smp_affinity
  29. * 02/04/02 P. Diefenbaugh Cleaned up ACPI PCI IRQ routing.
  30. * 02/04/18 J.I. Lee bug fix in iosapic_init_pci_irq
  31. * 02/04/30 J.I. Lee bug fix in find_iosapic to fix ACPI PCI IRQ to
  32. * IOSAPIC mapping error
  33. * 02/07/29 T. Kochi Allocate interrupt vectors dynamically
  34. * 02/08/04 T. Kochi Cleaned up terminology (irq, global system
  35. * interrupt, vector, etc.)
  36. * 02/09/20 D. Mosberger Simplified by taking advantage of ACPI's
  37. * pci_irq code.
  38. * 03/02/19 B. Helgaas Make pcat_compat system-wide, not per-IOSAPIC.
  39. * Remove iosapic_address & gsi_base from
  40. * external interfaces. Rationalize
  41. * __init/__devinit attributes.
  42. * 04/12/04 Ashok Raj <ashok.raj@intel.com> Intel Corporation 2004
  43. * Updated to work with irq migration necessary
  44. * for CPU Hotplug
  45. */
  46. /*
  47. * Here is what the interrupt logic between a PCI device and the kernel looks
  48. * like:
  49. *
  50. * (1) A PCI device raises one of the four interrupt pins (INTA, INTB, INTC,
  51. * INTD). The device is uniquely identified by its bus-, and slot-number
  52. * (the function number does not matter here because all functions share
  53. * the same interrupt lines).
  54. *
  55. * (2) The motherboard routes the interrupt line to a pin on a IOSAPIC
  56. * controller. Multiple interrupt lines may have to share the same
  57. * IOSAPIC pin (if they're level triggered and use the same polarity).
  58. * Each interrupt line has a unique Global System Interrupt (GSI) number
  59. * which can be calculated as the sum of the controller's base GSI number
  60. * and the IOSAPIC pin number to which the line connects.
  61. *
  62. * (3) The IOSAPIC uses an internal routing table entries (RTEs) to map the
  63. * IOSAPIC pin into the IA-64 interrupt vector. This interrupt vector is then
  64. * sent to the CPU.
  65. *
  66. * (4) The kernel recognizes an interrupt as an IRQ. The IRQ interface is
  67. * used as architecture-independent interrupt handling mechanism in Linux.
  68. * As an IRQ is a number, we have to have
  69. * IA-64 interrupt vector number <-> IRQ number mapping. On smaller
  70. * systems, we use one-to-one mapping between IA-64 vector and IRQ. A
  71. * platform can implement platform_irq_to_vector(irq) and
  72. * platform_local_vector_to_irq(vector) APIs to differentiate the mapping.
  73. * Please see also arch/ia64/include/asm/hw_irq.h for those APIs.
  74. *
  75. * To sum up, there are three levels of mappings involved:
  76. *
  77. * PCI pin -> global system interrupt (GSI) -> IA-64 vector <-> IRQ
  78. *
  79. * Note: The term "IRQ" is loosely used everywhere in Linux kernel to
  80. * describe interrupts. Now we use "IRQ" only for Linux IRQ's. ISA IRQ
  81. * (isa_irq) is the only exception in this source code.
  82. */
  83. #include <linux/acpi.h>
  84. #include <linux/init.h>
  85. #include <linux/irq.h>
  86. #include <linux/kernel.h>
  87. #include <linux/list.h>
  88. #include <linux/pci.h>
  89. #include <linux/slab.h>
  90. #include <linux/smp.h>
  91. #include <linux/string.h>
  92. #include <linux/bootmem.h>
  93. #include <asm/delay.h>
  94. #include <asm/hw_irq.h>
  95. #include <asm/io.h>
  96. #include <asm/iosapic.h>
  97. #include <asm/machvec.h>
  98. #include <asm/processor.h>
  99. #include <asm/ptrace.h>
  100. #undef DEBUG_INTERRUPT_ROUTING
  101. #ifdef DEBUG_INTERRUPT_ROUTING
  102. #define DBG(fmt...) printk(fmt)
  103. #else
  104. #define DBG(fmt...)
  105. #endif
  106. static DEFINE_SPINLOCK(iosapic_lock);
  107. /*
  108. * These tables map IA-64 vectors to the IOSAPIC pin that generates this
  109. * vector.
  110. */
  111. #define NO_REF_RTE 0
  112. static struct iosapic {
  113. char __iomem *addr; /* base address of IOSAPIC */
  114. unsigned int gsi_base; /* GSI base */
  115. unsigned short num_rte; /* # of RTEs on this IOSAPIC */
  116. int rtes_inuse; /* # of RTEs in use on this IOSAPIC */
  117. #ifdef CONFIG_NUMA
  118. unsigned short node; /* numa node association via pxm */
  119. #endif
  120. spinlock_t lock; /* lock for indirect reg access */
  121. } iosapic_lists[NR_IOSAPICS];
  122. struct iosapic_rte_info {
  123. struct list_head rte_list; /* RTEs sharing the same vector */
  124. char rte_index; /* IOSAPIC RTE index */
  125. int refcnt; /* reference counter */
  126. struct iosapic *iosapic;
  127. } ____cacheline_aligned;
  128. static struct iosapic_intr_info {
  129. struct list_head rtes; /* RTEs using this vector (empty =>
  130. * not an IOSAPIC interrupt) */
  131. int count; /* # of registered RTEs */
  132. u32 low32; /* current value of low word of
  133. * Redirection table entry */
  134. unsigned int dest; /* destination CPU physical ID */
  135. unsigned char dmode : 3; /* delivery mode (see iosapic.h) */
  136. unsigned char polarity: 1; /* interrupt polarity
  137. * (see iosapic.h) */
  138. unsigned char trigger : 1; /* trigger mode (see iosapic.h) */
  139. } iosapic_intr_info[NR_IRQS];
  140. static unsigned char pcat_compat; /* 8259 compatibility flag */
  141. static inline void
  142. iosapic_write(struct iosapic *iosapic, unsigned int reg, u32 val)
  143. {
  144. unsigned long flags;
  145. spin_lock_irqsave(&iosapic->lock, flags);
  146. __iosapic_write(iosapic->addr, reg, val);
  147. spin_unlock_irqrestore(&iosapic->lock, flags);
  148. }
  149. /*
  150. * Find an IOSAPIC associated with a GSI
  151. */
  152. static inline int
  153. find_iosapic (unsigned int gsi)
  154. {
  155. int i;
  156. for (i = 0; i < NR_IOSAPICS; i++) {
  157. if ((unsigned) (gsi - iosapic_lists[i].gsi_base) <
  158. iosapic_lists[i].num_rte)
  159. return i;
  160. }
  161. return -1;
  162. }
  163. static inline int __gsi_to_irq(unsigned int gsi)
  164. {
  165. int irq;
  166. struct iosapic_intr_info *info;
  167. struct iosapic_rte_info *rte;
  168. for (irq = 0; irq < NR_IRQS; irq++) {
  169. info = &iosapic_intr_info[irq];
  170. list_for_each_entry(rte, &info->rtes, rte_list)
  171. if (rte->iosapic->gsi_base + rte->rte_index == gsi)
  172. return irq;
  173. }
  174. return -1;
  175. }
  176. int
  177. gsi_to_irq (unsigned int gsi)
  178. {
  179. unsigned long flags;
  180. int irq;
  181. spin_lock_irqsave(&iosapic_lock, flags);
  182. irq = __gsi_to_irq(gsi);
  183. spin_unlock_irqrestore(&iosapic_lock, flags);
  184. return irq;
  185. }
  186. static struct iosapic_rte_info *find_rte(unsigned int irq, unsigned int gsi)
  187. {
  188. struct iosapic_rte_info *rte;
  189. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list)
  190. if (rte->iosapic->gsi_base + rte->rte_index == gsi)
  191. return rte;
  192. return NULL;
  193. }
  194. static void
  195. set_rte (unsigned int gsi, unsigned int irq, unsigned int dest, int mask)
  196. {
  197. unsigned long pol, trigger, dmode;
  198. u32 low32, high32;
  199. int rte_index;
  200. char redir;
  201. struct iosapic_rte_info *rte;
  202. ia64_vector vector = irq_to_vector(irq);
  203. DBG(KERN_DEBUG"IOSAPIC: routing vector %d to 0x%x\n", vector, dest);
  204. rte = find_rte(irq, gsi);
  205. if (!rte)
  206. return; /* not an IOSAPIC interrupt */
  207. rte_index = rte->rte_index;
  208. pol = iosapic_intr_info[irq].polarity;
  209. trigger = iosapic_intr_info[irq].trigger;
  210. dmode = iosapic_intr_info[irq].dmode;
  211. redir = (dmode == IOSAPIC_LOWEST_PRIORITY) ? 1 : 0;
  212. #ifdef CONFIG_SMP
  213. set_irq_affinity_info(irq, (int)(dest & 0xffff), redir);
  214. #endif
  215. low32 = ((pol << IOSAPIC_POLARITY_SHIFT) |
  216. (trigger << IOSAPIC_TRIGGER_SHIFT) |
  217. (dmode << IOSAPIC_DELIVERY_SHIFT) |
  218. ((mask ? 1 : 0) << IOSAPIC_MASK_SHIFT) |
  219. vector);
  220. /* dest contains both id and eid */
  221. high32 = (dest << IOSAPIC_DEST_SHIFT);
  222. iosapic_write(rte->iosapic, IOSAPIC_RTE_HIGH(rte_index), high32);
  223. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  224. iosapic_intr_info[irq].low32 = low32;
  225. iosapic_intr_info[irq].dest = dest;
  226. }
  227. static void
  228. iosapic_nop (struct irq_data *data)
  229. {
  230. /* do nothing... */
  231. }
  232. #ifdef CONFIG_KEXEC
  233. void
  234. kexec_disable_iosapic(void)
  235. {
  236. struct iosapic_intr_info *info;
  237. struct iosapic_rte_info *rte;
  238. ia64_vector vec;
  239. int irq;
  240. for (irq = 0; irq < NR_IRQS; irq++) {
  241. info = &iosapic_intr_info[irq];
  242. vec = irq_to_vector(irq);
  243. list_for_each_entry(rte, &info->rtes,
  244. rte_list) {
  245. iosapic_write(rte->iosapic,
  246. IOSAPIC_RTE_LOW(rte->rte_index),
  247. IOSAPIC_MASK|vec);
  248. iosapic_eoi(rte->iosapic->addr, vec);
  249. }
  250. }
  251. }
  252. #endif
  253. static void
  254. mask_irq (struct irq_data *data)
  255. {
  256. unsigned int irq = data->irq;
  257. u32 low32;
  258. int rte_index;
  259. struct iosapic_rte_info *rte;
  260. if (!iosapic_intr_info[irq].count)
  261. return; /* not an IOSAPIC interrupt! */
  262. /* set only the mask bit */
  263. low32 = iosapic_intr_info[irq].low32 |= IOSAPIC_MASK;
  264. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  265. rte_index = rte->rte_index;
  266. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  267. }
  268. }
  269. static void
  270. unmask_irq (struct irq_data *data)
  271. {
  272. unsigned int irq = data->irq;
  273. u32 low32;
  274. int rte_index;
  275. struct iosapic_rte_info *rte;
  276. if (!iosapic_intr_info[irq].count)
  277. return; /* not an IOSAPIC interrupt! */
  278. low32 = iosapic_intr_info[irq].low32 &= ~IOSAPIC_MASK;
  279. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  280. rte_index = rte->rte_index;
  281. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  282. }
  283. }
  284. static int
  285. iosapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
  286. bool force)
  287. {
  288. #ifdef CONFIG_SMP
  289. unsigned int irq = data->irq;
  290. u32 high32, low32;
  291. int cpu, dest, rte_index;
  292. int redir = (irq & IA64_IRQ_REDIRECTED) ? 1 : 0;
  293. struct iosapic_rte_info *rte;
  294. struct iosapic *iosapic;
  295. irq &= (~IA64_IRQ_REDIRECTED);
  296. cpu = cpumask_first_and(cpu_online_mask, mask);
  297. if (cpu >= nr_cpu_ids)
  298. return -1;
  299. if (irq_prepare_move(irq, cpu))
  300. return -1;
  301. dest = cpu_physical_id(cpu);
  302. if (!iosapic_intr_info[irq].count)
  303. return -1; /* not an IOSAPIC interrupt */
  304. set_irq_affinity_info(irq, dest, redir);
  305. /* dest contains both id and eid */
  306. high32 = dest << IOSAPIC_DEST_SHIFT;
  307. low32 = iosapic_intr_info[irq].low32 & ~(7 << IOSAPIC_DELIVERY_SHIFT);
  308. if (redir)
  309. /* change delivery mode to lowest priority */
  310. low32 |= (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
  311. else
  312. /* change delivery mode to fixed */
  313. low32 |= (IOSAPIC_FIXED << IOSAPIC_DELIVERY_SHIFT);
  314. low32 &= IOSAPIC_VECTOR_MASK;
  315. low32 |= irq_to_vector(irq);
  316. iosapic_intr_info[irq].low32 = low32;
  317. iosapic_intr_info[irq].dest = dest;
  318. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list) {
  319. iosapic = rte->iosapic;
  320. rte_index = rte->rte_index;
  321. iosapic_write(iosapic, IOSAPIC_RTE_HIGH(rte_index), high32);
  322. iosapic_write(iosapic, IOSAPIC_RTE_LOW(rte_index), low32);
  323. }
  324. #endif
  325. return 0;
  326. }
  327. /*
  328. * Handlers for level-triggered interrupts.
  329. */
  330. static unsigned int
  331. iosapic_startup_level_irq (struct irq_data *data)
  332. {
  333. unmask_irq(data);
  334. return 0;
  335. }
  336. static void
  337. iosapic_unmask_level_irq (struct irq_data *data)
  338. {
  339. unsigned int irq = data->irq;
  340. ia64_vector vec = irq_to_vector(irq);
  341. struct iosapic_rte_info *rte;
  342. int do_unmask_irq = 0;
  343. irq_complete_move(irq);
  344. if (unlikely(irqd_is_setaffinity_pending(data))) {
  345. do_unmask_irq = 1;
  346. mask_irq(data);
  347. } else
  348. unmask_irq(data);
  349. list_for_each_entry(rte, &iosapic_intr_info[irq].rtes, rte_list)
  350. iosapic_eoi(rte->iosapic->addr, vec);
  351. if (unlikely(do_unmask_irq)) {
  352. irq_move_masked_irq(data);
  353. unmask_irq(data);
  354. }
  355. }
  356. #define iosapic_shutdown_level_irq mask_irq
  357. #define iosapic_enable_level_irq unmask_irq
  358. #define iosapic_disable_level_irq mask_irq
  359. #define iosapic_ack_level_irq iosapic_nop
  360. static struct irq_chip irq_type_iosapic_level = {
  361. .name = "IO-SAPIC-level",
  362. .irq_startup = iosapic_startup_level_irq,
  363. .irq_shutdown = iosapic_shutdown_level_irq,
  364. .irq_enable = iosapic_enable_level_irq,
  365. .irq_disable = iosapic_disable_level_irq,
  366. .irq_ack = iosapic_ack_level_irq,
  367. .irq_mask = mask_irq,
  368. .irq_unmask = iosapic_unmask_level_irq,
  369. .irq_set_affinity = iosapic_set_affinity
  370. };
  371. /*
  372. * Handlers for edge-triggered interrupts.
  373. */
  374. static unsigned int
  375. iosapic_startup_edge_irq (struct irq_data *data)
  376. {
  377. unmask_irq(data);
  378. /*
  379. * IOSAPIC simply drops interrupts pended while the
  380. * corresponding pin was masked, so we can't know if an
  381. * interrupt is pending already. Let's hope not...
  382. */
  383. return 0;
  384. }
  385. static void
  386. iosapic_ack_edge_irq (struct irq_data *data)
  387. {
  388. irq_complete_move(data->irq);
  389. irq_move_irq(data);
  390. }
  391. #define iosapic_enable_edge_irq unmask_irq
  392. #define iosapic_disable_edge_irq iosapic_nop
  393. static struct irq_chip irq_type_iosapic_edge = {
  394. .name = "IO-SAPIC-edge",
  395. .irq_startup = iosapic_startup_edge_irq,
  396. .irq_shutdown = iosapic_disable_edge_irq,
  397. .irq_enable = iosapic_enable_edge_irq,
  398. .irq_disable = iosapic_disable_edge_irq,
  399. .irq_ack = iosapic_ack_edge_irq,
  400. .irq_mask = mask_irq,
  401. .irq_unmask = unmask_irq,
  402. .irq_set_affinity = iosapic_set_affinity
  403. };
  404. static unsigned int
  405. iosapic_version (char __iomem *addr)
  406. {
  407. /*
  408. * IOSAPIC Version Register return 32 bit structure like:
  409. * {
  410. * unsigned int version : 8;
  411. * unsigned int reserved1 : 8;
  412. * unsigned int max_redir : 8;
  413. * unsigned int reserved2 : 8;
  414. * }
  415. */
  416. return __iosapic_read(addr, IOSAPIC_VERSION);
  417. }
  418. static int iosapic_find_sharable_irq(unsigned long trigger, unsigned long pol)
  419. {
  420. int i, irq = -ENOSPC, min_count = -1;
  421. struct iosapic_intr_info *info;
  422. /*
  423. * shared vectors for edge-triggered interrupts are not
  424. * supported yet
  425. */
  426. if (trigger == IOSAPIC_EDGE)
  427. return -EINVAL;
  428. for (i = 0; i < NR_IRQS; i++) {
  429. info = &iosapic_intr_info[i];
  430. if (info->trigger == trigger && info->polarity == pol &&
  431. (info->dmode == IOSAPIC_FIXED ||
  432. info->dmode == IOSAPIC_LOWEST_PRIORITY) &&
  433. can_request_irq(i, IRQF_SHARED)) {
  434. if (min_count == -1 || info->count < min_count) {
  435. irq = i;
  436. min_count = info->count;
  437. }
  438. }
  439. }
  440. return irq;
  441. }
  442. /*
  443. * if the given vector is already owned by other,
  444. * assign a new vector for the other and make the vector available
  445. */
  446. static void __init
  447. iosapic_reassign_vector (int irq)
  448. {
  449. int new_irq;
  450. if (iosapic_intr_info[irq].count) {
  451. new_irq = create_irq();
  452. if (new_irq < 0)
  453. panic("%s: out of interrupt vectors!\n", __func__);
  454. printk(KERN_INFO "Reassigning vector %d to %d\n",
  455. irq_to_vector(irq), irq_to_vector(new_irq));
  456. memcpy(&iosapic_intr_info[new_irq], &iosapic_intr_info[irq],
  457. sizeof(struct iosapic_intr_info));
  458. INIT_LIST_HEAD(&iosapic_intr_info[new_irq].rtes);
  459. list_move(iosapic_intr_info[irq].rtes.next,
  460. &iosapic_intr_info[new_irq].rtes);
  461. memset(&iosapic_intr_info[irq], 0,
  462. sizeof(struct iosapic_intr_info));
  463. iosapic_intr_info[irq].low32 = IOSAPIC_MASK;
  464. INIT_LIST_HEAD(&iosapic_intr_info[irq].rtes);
  465. }
  466. }
  467. static inline int irq_is_shared (int irq)
  468. {
  469. return (iosapic_intr_info[irq].count > 1);
  470. }
  471. struct irq_chip*
  472. ia64_native_iosapic_get_irq_chip(unsigned long trigger)
  473. {
  474. if (trigger == IOSAPIC_EDGE)
  475. return &irq_type_iosapic_edge;
  476. else
  477. return &irq_type_iosapic_level;
  478. }
  479. static int
  480. register_intr (unsigned int gsi, int irq, unsigned char delivery,
  481. unsigned long polarity, unsigned long trigger)
  482. {
  483. struct irq_chip *chip, *irq_type;
  484. int index;
  485. struct iosapic_rte_info *rte;
  486. index = find_iosapic(gsi);
  487. if (index < 0) {
  488. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  489. __func__, gsi);
  490. return -ENODEV;
  491. }
  492. rte = find_rte(irq, gsi);
  493. if (!rte) {
  494. rte = kzalloc(sizeof (*rte), GFP_ATOMIC);
  495. if (!rte) {
  496. printk(KERN_WARNING "%s: cannot allocate memory\n",
  497. __func__);
  498. return -ENOMEM;
  499. }
  500. rte->iosapic = &iosapic_lists[index];
  501. rte->rte_index = gsi - rte->iosapic->gsi_base;
  502. rte->refcnt++;
  503. list_add_tail(&rte->rte_list, &iosapic_intr_info[irq].rtes);
  504. iosapic_intr_info[irq].count++;
  505. iosapic_lists[index].rtes_inuse++;
  506. }
  507. else if (rte->refcnt == NO_REF_RTE) {
  508. struct iosapic_intr_info *info = &iosapic_intr_info[irq];
  509. if (info->count > 0 &&
  510. (info->trigger != trigger || info->polarity != polarity)){
  511. printk (KERN_WARNING
  512. "%s: cannot override the interrupt\n",
  513. __func__);
  514. return -EINVAL;
  515. }
  516. rte->refcnt++;
  517. iosapic_intr_info[irq].count++;
  518. iosapic_lists[index].rtes_inuse++;
  519. }
  520. iosapic_intr_info[irq].polarity = polarity;
  521. iosapic_intr_info[irq].dmode = delivery;
  522. iosapic_intr_info[irq].trigger = trigger;
  523. irq_type = iosapic_get_irq_chip(trigger);
  524. chip = irq_get_chip(irq);
  525. if (irq_type != NULL && chip != irq_type) {
  526. if (chip != &no_irq_chip)
  527. printk(KERN_WARNING
  528. "%s: changing vector %d from %s to %s\n",
  529. __func__, irq_to_vector(irq),
  530. chip->name, irq_type->name);
  531. chip = irq_type;
  532. }
  533. irq_set_chip_handler_name_locked(irq_get_irq_data(irq), chip,
  534. trigger == IOSAPIC_EDGE ? handle_edge_irq : handle_level_irq,
  535. NULL);
  536. return 0;
  537. }
  538. static unsigned int
  539. get_target_cpu (unsigned int gsi, int irq)
  540. {
  541. #ifdef CONFIG_SMP
  542. static int cpu = -1;
  543. extern int cpe_vector;
  544. cpumask_t domain = irq_to_domain(irq);
  545. /*
  546. * In case of vector shared by multiple RTEs, all RTEs that
  547. * share the vector need to use the same destination CPU.
  548. */
  549. if (iosapic_intr_info[irq].count)
  550. return iosapic_intr_info[irq].dest;
  551. /*
  552. * If the platform supports redirection via XTP, let it
  553. * distribute interrupts.
  554. */
  555. if (smp_int_redirect & SMP_IRQ_REDIRECTION)
  556. return cpu_physical_id(smp_processor_id());
  557. /*
  558. * Some interrupts (ACPI SCI, for instance) are registered
  559. * before the BSP is marked as online.
  560. */
  561. if (!cpu_online(smp_processor_id()))
  562. return cpu_physical_id(smp_processor_id());
  563. #ifdef CONFIG_ACPI
  564. if (cpe_vector > 0 && irq_to_vector(irq) == IA64_CPEP_VECTOR)
  565. return get_cpei_target_cpu();
  566. #endif
  567. #ifdef CONFIG_NUMA
  568. {
  569. int num_cpus, cpu_index, iosapic_index, numa_cpu, i = 0;
  570. const struct cpumask *cpu_mask;
  571. iosapic_index = find_iosapic(gsi);
  572. if (iosapic_index < 0 ||
  573. iosapic_lists[iosapic_index].node == MAX_NUMNODES)
  574. goto skip_numa_setup;
  575. cpu_mask = cpumask_of_node(iosapic_lists[iosapic_index].node);
  576. num_cpus = 0;
  577. for_each_cpu_and(numa_cpu, cpu_mask, &domain) {
  578. if (cpu_online(numa_cpu))
  579. num_cpus++;
  580. }
  581. if (!num_cpus)
  582. goto skip_numa_setup;
  583. /* Use irq assignment to distribute across cpus in node */
  584. cpu_index = irq % num_cpus;
  585. for_each_cpu_and(numa_cpu, cpu_mask, &domain)
  586. if (cpu_online(numa_cpu) && i++ >= cpu_index)
  587. break;
  588. if (numa_cpu < nr_cpu_ids)
  589. return cpu_physical_id(numa_cpu);
  590. }
  591. skip_numa_setup:
  592. #endif
  593. /*
  594. * Otherwise, round-robin interrupt vectors across all the
  595. * processors. (It'd be nice if we could be smarter in the
  596. * case of NUMA.)
  597. */
  598. do {
  599. if (++cpu >= nr_cpu_ids)
  600. cpu = 0;
  601. } while (!cpu_online(cpu) || !cpumask_test_cpu(cpu, &domain));
  602. return cpu_physical_id(cpu);
  603. #else /* CONFIG_SMP */
  604. return cpu_physical_id(smp_processor_id());
  605. #endif
  606. }
  607. static inline unsigned char choose_dmode(void)
  608. {
  609. #ifdef CONFIG_SMP
  610. if (smp_int_redirect & SMP_IRQ_REDIRECTION)
  611. return IOSAPIC_LOWEST_PRIORITY;
  612. #endif
  613. return IOSAPIC_FIXED;
  614. }
  615. /*
  616. * ACPI can describe IOSAPIC interrupts via static tables and namespace
  617. * methods. This provides an interface to register those interrupts and
  618. * program the IOSAPIC RTE.
  619. */
  620. int
  621. iosapic_register_intr (unsigned int gsi,
  622. unsigned long polarity, unsigned long trigger)
  623. {
  624. int irq, mask = 1, err;
  625. unsigned int dest;
  626. unsigned long flags;
  627. struct iosapic_rte_info *rte;
  628. u32 low32;
  629. unsigned char dmode;
  630. struct irq_desc *desc;
  631. /*
  632. * If this GSI has already been registered (i.e., it's a
  633. * shared interrupt, or we lost a race to register it),
  634. * don't touch the RTE.
  635. */
  636. spin_lock_irqsave(&iosapic_lock, flags);
  637. irq = __gsi_to_irq(gsi);
  638. if (irq > 0) {
  639. rte = find_rte(irq, gsi);
  640. if(iosapic_intr_info[irq].count == 0) {
  641. assign_irq_vector(irq);
  642. irq_init_desc(irq);
  643. } else if (rte->refcnt != NO_REF_RTE) {
  644. rte->refcnt++;
  645. goto unlock_iosapic_lock;
  646. }
  647. } else
  648. irq = create_irq();
  649. /* If vector is running out, we try to find a sharable vector */
  650. if (irq < 0) {
  651. irq = iosapic_find_sharable_irq(trigger, polarity);
  652. if (irq < 0)
  653. goto unlock_iosapic_lock;
  654. }
  655. desc = irq_to_desc(irq);
  656. raw_spin_lock(&desc->lock);
  657. dest = get_target_cpu(gsi, irq);
  658. dmode = choose_dmode();
  659. err = register_intr(gsi, irq, dmode, polarity, trigger);
  660. if (err < 0) {
  661. raw_spin_unlock(&desc->lock);
  662. irq = err;
  663. goto unlock_iosapic_lock;
  664. }
  665. /*
  666. * If the vector is shared and already unmasked for other
  667. * interrupt sources, don't mask it.
  668. */
  669. low32 = iosapic_intr_info[irq].low32;
  670. if (irq_is_shared(irq) && !(low32 & IOSAPIC_MASK))
  671. mask = 0;
  672. set_rte(gsi, irq, dest, mask);
  673. printk(KERN_INFO "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d\n",
  674. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  675. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  676. cpu_logical_id(dest), dest, irq_to_vector(irq));
  677. raw_spin_unlock(&desc->lock);
  678. unlock_iosapic_lock:
  679. spin_unlock_irqrestore(&iosapic_lock, flags);
  680. return irq;
  681. }
  682. void
  683. iosapic_unregister_intr (unsigned int gsi)
  684. {
  685. unsigned long flags;
  686. int irq, index;
  687. u32 low32;
  688. unsigned long trigger, polarity;
  689. unsigned int dest;
  690. struct iosapic_rte_info *rte;
  691. /*
  692. * If the irq associated with the gsi is not found,
  693. * iosapic_unregister_intr() is unbalanced. We need to check
  694. * this again after getting locks.
  695. */
  696. irq = gsi_to_irq(gsi);
  697. if (irq < 0) {
  698. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  699. gsi);
  700. WARN_ON(1);
  701. return;
  702. }
  703. spin_lock_irqsave(&iosapic_lock, flags);
  704. if ((rte = find_rte(irq, gsi)) == NULL) {
  705. printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n",
  706. gsi);
  707. WARN_ON(1);
  708. goto out;
  709. }
  710. if (--rte->refcnt > 0)
  711. goto out;
  712. rte->refcnt = NO_REF_RTE;
  713. /* Mask the interrupt */
  714. low32 = iosapic_intr_info[irq].low32 | IOSAPIC_MASK;
  715. iosapic_write(rte->iosapic, IOSAPIC_RTE_LOW(rte->rte_index), low32);
  716. iosapic_intr_info[irq].count--;
  717. index = find_iosapic(gsi);
  718. iosapic_lists[index].rtes_inuse--;
  719. WARN_ON(iosapic_lists[index].rtes_inuse < 0);
  720. trigger = iosapic_intr_info[irq].trigger;
  721. polarity = iosapic_intr_info[irq].polarity;
  722. dest = iosapic_intr_info[irq].dest;
  723. printk(KERN_INFO
  724. "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d unregistered\n",
  725. gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  726. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  727. cpu_logical_id(dest), dest, irq_to_vector(irq));
  728. if (iosapic_intr_info[irq].count == 0) {
  729. #ifdef CONFIG_SMP
  730. /* Clear affinity */
  731. cpumask_setall(irq_get_affinity_mask(irq));
  732. #endif
  733. /* Clear the interrupt information */
  734. iosapic_intr_info[irq].dest = 0;
  735. iosapic_intr_info[irq].dmode = 0;
  736. iosapic_intr_info[irq].polarity = 0;
  737. iosapic_intr_info[irq].trigger = 0;
  738. iosapic_intr_info[irq].low32 |= IOSAPIC_MASK;
  739. /* Destroy and reserve IRQ */
  740. destroy_and_reserve_irq(irq);
  741. }
  742. out:
  743. spin_unlock_irqrestore(&iosapic_lock, flags);
  744. }
  745. /*
  746. * ACPI calls this when it finds an entry for a platform interrupt.
  747. */
  748. int __init
  749. iosapic_register_platform_intr (u32 int_type, unsigned int gsi,
  750. int iosapic_vector, u16 eid, u16 id,
  751. unsigned long polarity, unsigned long trigger)
  752. {
  753. static const char * const name[] = {"unknown", "PMI", "INIT", "CPEI"};
  754. unsigned char delivery;
  755. int irq, vector, mask = 0;
  756. unsigned int dest = ((id << 8) | eid) & 0xffff;
  757. switch (int_type) {
  758. case ACPI_INTERRUPT_PMI:
  759. irq = vector = iosapic_vector;
  760. bind_irq_vector(irq, vector, CPU_MASK_ALL);
  761. /*
  762. * since PMI vector is alloc'd by FW(ACPI) not by kernel,
  763. * we need to make sure the vector is available
  764. */
  765. iosapic_reassign_vector(irq);
  766. delivery = IOSAPIC_PMI;
  767. break;
  768. case ACPI_INTERRUPT_INIT:
  769. irq = create_irq();
  770. if (irq < 0)
  771. panic("%s: out of interrupt vectors!\n", __func__);
  772. vector = irq_to_vector(irq);
  773. delivery = IOSAPIC_INIT;
  774. break;
  775. case ACPI_INTERRUPT_CPEI:
  776. irq = vector = IA64_CPE_VECTOR;
  777. BUG_ON(bind_irq_vector(irq, vector, CPU_MASK_ALL));
  778. delivery = IOSAPIC_FIXED;
  779. mask = 1;
  780. break;
  781. default:
  782. printk(KERN_ERR "%s: invalid int type 0x%x\n", __func__,
  783. int_type);
  784. return -1;
  785. }
  786. register_intr(gsi, irq, delivery, polarity, trigger);
  787. printk(KERN_INFO
  788. "PLATFORM int %s (0x%x): GSI %u (%s, %s) -> CPU %d (0x%04x)"
  789. " vector %d\n",
  790. int_type < ARRAY_SIZE(name) ? name[int_type] : "unknown",
  791. int_type, gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
  792. (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
  793. cpu_logical_id(dest), dest, vector);
  794. set_rte(gsi, irq, dest, mask);
  795. return vector;
  796. }
  797. /*
  798. * ACPI calls this when it finds an entry for a legacy ISA IRQ override.
  799. */
  800. void iosapic_override_isa_irq(unsigned int isa_irq, unsigned int gsi,
  801. unsigned long polarity, unsigned long trigger)
  802. {
  803. int vector, irq;
  804. unsigned int dest = cpu_physical_id(smp_processor_id());
  805. unsigned char dmode;
  806. irq = vector = isa_irq_to_vector(isa_irq);
  807. BUG_ON(bind_irq_vector(irq, vector, CPU_MASK_ALL));
  808. dmode = choose_dmode();
  809. register_intr(gsi, irq, dmode, polarity, trigger);
  810. DBG("ISA: IRQ %u -> GSI %u (%s,%s) -> CPU %d (0x%04x) vector %d\n",
  811. isa_irq, gsi, trigger == IOSAPIC_EDGE ? "edge" : "level",
  812. polarity == IOSAPIC_POL_HIGH ? "high" : "low",
  813. cpu_logical_id(dest), dest, vector);
  814. set_rte(gsi, irq, dest, 1);
  815. }
  816. void __init
  817. ia64_native_iosapic_pcat_compat_init(void)
  818. {
  819. if (pcat_compat) {
  820. /*
  821. * Disable the compatibility mode interrupts (8259 style),
  822. * needs IN/OUT support enabled.
  823. */
  824. printk(KERN_INFO
  825. "%s: Disabling PC-AT compatible 8259 interrupts\n",
  826. __func__);
  827. outb(0xff, 0xA1);
  828. outb(0xff, 0x21);
  829. }
  830. }
  831. void __init
  832. iosapic_system_init (int system_pcat_compat)
  833. {
  834. int irq;
  835. for (irq = 0; irq < NR_IRQS; ++irq) {
  836. iosapic_intr_info[irq].low32 = IOSAPIC_MASK;
  837. /* mark as unused */
  838. INIT_LIST_HEAD(&iosapic_intr_info[irq].rtes);
  839. iosapic_intr_info[irq].count = 0;
  840. }
  841. pcat_compat = system_pcat_compat;
  842. if (pcat_compat)
  843. iosapic_pcat_compat_init();
  844. }
  845. static inline int
  846. iosapic_alloc (void)
  847. {
  848. int index;
  849. for (index = 0; index < NR_IOSAPICS; index++)
  850. if (!iosapic_lists[index].addr)
  851. return index;
  852. printk(KERN_WARNING "%s: failed to allocate iosapic\n", __func__);
  853. return -1;
  854. }
  855. static inline void
  856. iosapic_free (int index)
  857. {
  858. memset(&iosapic_lists[index], 0, sizeof(iosapic_lists[0]));
  859. }
  860. static inline int
  861. iosapic_check_gsi_range (unsigned int gsi_base, unsigned int ver)
  862. {
  863. int index;
  864. unsigned int gsi_end, base, end;
  865. /* check gsi range */
  866. gsi_end = gsi_base + ((ver >> 16) & 0xff);
  867. for (index = 0; index < NR_IOSAPICS; index++) {
  868. if (!iosapic_lists[index].addr)
  869. continue;
  870. base = iosapic_lists[index].gsi_base;
  871. end = base + iosapic_lists[index].num_rte - 1;
  872. if (gsi_end < base || end < gsi_base)
  873. continue; /* OK */
  874. return -EBUSY;
  875. }
  876. return 0;
  877. }
  878. static int
  879. iosapic_delete_rte(unsigned int irq, unsigned int gsi)
  880. {
  881. struct iosapic_rte_info *rte, *temp;
  882. list_for_each_entry_safe(rte, temp, &iosapic_intr_info[irq].rtes,
  883. rte_list) {
  884. if (rte->iosapic->gsi_base + rte->rte_index == gsi) {
  885. if (rte->refcnt)
  886. return -EBUSY;
  887. list_del(&rte->rte_list);
  888. kfree(rte);
  889. return 0;
  890. }
  891. }
  892. return -EINVAL;
  893. }
  894. int iosapic_init(unsigned long phys_addr, unsigned int gsi_base)
  895. {
  896. int num_rte, err, index;
  897. unsigned int isa_irq, ver;
  898. char __iomem *addr;
  899. unsigned long flags;
  900. spin_lock_irqsave(&iosapic_lock, flags);
  901. index = find_iosapic(gsi_base);
  902. if (index >= 0) {
  903. spin_unlock_irqrestore(&iosapic_lock, flags);
  904. return -EBUSY;
  905. }
  906. addr = ioremap(phys_addr, 0);
  907. if (addr == NULL) {
  908. spin_unlock_irqrestore(&iosapic_lock, flags);
  909. return -ENOMEM;
  910. }
  911. ver = iosapic_version(addr);
  912. if ((err = iosapic_check_gsi_range(gsi_base, ver))) {
  913. iounmap(addr);
  914. spin_unlock_irqrestore(&iosapic_lock, flags);
  915. return err;
  916. }
  917. /*
  918. * The MAX_REDIR register holds the highest input pin number
  919. * (starting from 0). We add 1 so that we can use it for
  920. * number of pins (= RTEs)
  921. */
  922. num_rte = ((ver >> 16) & 0xff) + 1;
  923. index = iosapic_alloc();
  924. iosapic_lists[index].addr = addr;
  925. iosapic_lists[index].gsi_base = gsi_base;
  926. iosapic_lists[index].num_rte = num_rte;
  927. #ifdef CONFIG_NUMA
  928. iosapic_lists[index].node = MAX_NUMNODES;
  929. #endif
  930. spin_lock_init(&iosapic_lists[index].lock);
  931. spin_unlock_irqrestore(&iosapic_lock, flags);
  932. if ((gsi_base == 0) && pcat_compat) {
  933. /*
  934. * Map the legacy ISA devices into the IOSAPIC data. Some of
  935. * these may get reprogrammed later on with data from the ACPI
  936. * Interrupt Source Override table.
  937. */
  938. for (isa_irq = 0; isa_irq < 16; ++isa_irq)
  939. iosapic_override_isa_irq(isa_irq, isa_irq,
  940. IOSAPIC_POL_HIGH,
  941. IOSAPIC_EDGE);
  942. }
  943. return 0;
  944. }
  945. int iosapic_remove(unsigned int gsi_base)
  946. {
  947. int i, irq, index, err = 0;
  948. unsigned long flags;
  949. spin_lock_irqsave(&iosapic_lock, flags);
  950. index = find_iosapic(gsi_base);
  951. if (index < 0) {
  952. printk(KERN_WARNING "%s: No IOSAPIC for GSI base %u\n",
  953. __func__, gsi_base);
  954. goto out;
  955. }
  956. if (iosapic_lists[index].rtes_inuse) {
  957. err = -EBUSY;
  958. printk(KERN_WARNING "%s: IOSAPIC for GSI base %u is busy\n",
  959. __func__, gsi_base);
  960. goto out;
  961. }
  962. for (i = gsi_base; i < gsi_base + iosapic_lists[index].num_rte; i++) {
  963. irq = __gsi_to_irq(i);
  964. if (irq < 0)
  965. continue;
  966. err = iosapic_delete_rte(irq, i);
  967. if (err)
  968. goto out;
  969. }
  970. iounmap(iosapic_lists[index].addr);
  971. iosapic_free(index);
  972. out:
  973. spin_unlock_irqrestore(&iosapic_lock, flags);
  974. return err;
  975. }
  976. #ifdef CONFIG_NUMA
  977. void map_iosapic_to_node(unsigned int gsi_base, int node)
  978. {
  979. int index;
  980. index = find_iosapic(gsi_base);
  981. if (index < 0) {
  982. printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
  983. __func__, gsi_base);
  984. return;
  985. }
  986. iosapic_lists[index].node = node;
  987. return;
  988. }
  989. #endif