vector.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * Local APIC related interfaces to support IOAPIC, MSI, HT_IRQ etc.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
  5. * Moved from arch/x86/kernel/apic/io_apic.c.
  6. * Jiang Liu <jiang.liu@linux.intel.com>
  7. * Enable support of hierarchical irqdomains
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/interrupt.h>
  14. #include <linux/init.h>
  15. #include <linux/compiler.h>
  16. #include <linux/slab.h>
  17. #include <asm/irqdomain.h>
  18. #include <asm/hw_irq.h>
  19. #include <asm/apic.h>
  20. #include <asm/i8259.h>
  21. #include <asm/desc.h>
  22. #include <asm/irq_remapping.h>
  23. struct apic_chip_data {
  24. struct irq_cfg cfg;
  25. cpumask_var_t domain;
  26. cpumask_var_t old_domain;
  27. u8 move_in_progress : 1;
  28. };
  29. struct irq_domain *x86_vector_domain;
  30. EXPORT_SYMBOL_GPL(x86_vector_domain);
  31. static DEFINE_RAW_SPINLOCK(vector_lock);
  32. static cpumask_var_t vector_cpumask, vector_searchmask, searched_cpumask;
  33. static struct irq_chip lapic_controller;
  34. #ifdef CONFIG_X86_IO_APIC
  35. static struct apic_chip_data *legacy_irq_data[NR_IRQS_LEGACY];
  36. #endif
  37. void lock_vector_lock(void)
  38. {
  39. /* Used to the online set of cpus does not change
  40. * during assign_irq_vector.
  41. */
  42. raw_spin_lock(&vector_lock);
  43. }
  44. void unlock_vector_lock(void)
  45. {
  46. raw_spin_unlock(&vector_lock);
  47. }
  48. static struct apic_chip_data *apic_chip_data(struct irq_data *irq_data)
  49. {
  50. if (!irq_data)
  51. return NULL;
  52. while (irq_data->parent_data)
  53. irq_data = irq_data->parent_data;
  54. return irq_data->chip_data;
  55. }
  56. struct irq_cfg *irqd_cfg(struct irq_data *irq_data)
  57. {
  58. struct apic_chip_data *data = apic_chip_data(irq_data);
  59. return data ? &data->cfg : NULL;
  60. }
  61. EXPORT_SYMBOL_GPL(irqd_cfg);
  62. struct irq_cfg *irq_cfg(unsigned int irq)
  63. {
  64. return irqd_cfg(irq_get_irq_data(irq));
  65. }
  66. static struct apic_chip_data *alloc_apic_chip_data(int node)
  67. {
  68. struct apic_chip_data *data;
  69. data = kzalloc_node(sizeof(*data), GFP_KERNEL, node);
  70. if (!data)
  71. return NULL;
  72. if (!zalloc_cpumask_var_node(&data->domain, GFP_KERNEL, node))
  73. goto out_data;
  74. if (!zalloc_cpumask_var_node(&data->old_domain, GFP_KERNEL, node))
  75. goto out_domain;
  76. return data;
  77. out_domain:
  78. free_cpumask_var(data->domain);
  79. out_data:
  80. kfree(data);
  81. return NULL;
  82. }
  83. static void free_apic_chip_data(struct apic_chip_data *data)
  84. {
  85. if (data) {
  86. free_cpumask_var(data->domain);
  87. free_cpumask_var(data->old_domain);
  88. kfree(data);
  89. }
  90. }
  91. static int __assign_irq_vector(int irq, struct apic_chip_data *d,
  92. const struct cpumask *mask,
  93. struct irq_data *irqdata)
  94. {
  95. /*
  96. * NOTE! The local APIC isn't very good at handling
  97. * multiple interrupts at the same interrupt level.
  98. * As the interrupt level is determined by taking the
  99. * vector number and shifting that right by 4, we
  100. * want to spread these out a bit so that they don't
  101. * all fall in the same interrupt level.
  102. *
  103. * Also, we've got to be careful not to trash gate
  104. * 0x80, because int 0x80 is hm, kind of importantish. ;)
  105. */
  106. static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
  107. static int current_offset = VECTOR_OFFSET_START % 16;
  108. int cpu, vector;
  109. /*
  110. * If there is still a move in progress or the previous move has not
  111. * been cleaned up completely, tell the caller to come back later.
  112. */
  113. if (d->move_in_progress ||
  114. cpumask_intersects(d->old_domain, cpu_online_mask))
  115. return -EBUSY;
  116. /* Only try and allocate irqs on cpus that are present */
  117. cpumask_clear(d->old_domain);
  118. cpumask_clear(searched_cpumask);
  119. cpu = cpumask_first_and(mask, cpu_online_mask);
  120. while (cpu < nr_cpu_ids) {
  121. int new_cpu, offset;
  122. /* Get the possible target cpus for @mask/@cpu from the apic */
  123. apic->vector_allocation_domain(cpu, vector_cpumask, mask);
  124. /*
  125. * Clear the offline cpus from @vector_cpumask for searching
  126. * and verify whether the result overlaps with @mask. If true,
  127. * then the call to apic->cpu_mask_to_apicid() will
  128. * succeed as well. If not, no point in trying to find a
  129. * vector in this mask.
  130. */
  131. cpumask_and(vector_searchmask, vector_cpumask, cpu_online_mask);
  132. if (!cpumask_intersects(vector_searchmask, mask))
  133. goto next_cpu;
  134. if (cpumask_subset(vector_cpumask, d->domain)) {
  135. if (cpumask_equal(vector_cpumask, d->domain))
  136. goto success;
  137. /*
  138. * Mark the cpus which are not longer in the mask for
  139. * cleanup.
  140. */
  141. cpumask_andnot(d->old_domain, d->domain, vector_cpumask);
  142. vector = d->cfg.vector;
  143. goto update;
  144. }
  145. vector = current_vector;
  146. offset = current_offset;
  147. next:
  148. vector += 16;
  149. if (vector >= first_system_vector) {
  150. offset = (offset + 1) % 16;
  151. vector = FIRST_EXTERNAL_VECTOR + offset;
  152. }
  153. /* If the search wrapped around, try the next cpu */
  154. if (unlikely(current_vector == vector))
  155. goto next_cpu;
  156. if (test_bit(vector, used_vectors))
  157. goto next;
  158. for_each_cpu(new_cpu, vector_searchmask) {
  159. if (!IS_ERR_OR_NULL(per_cpu(vector_irq, new_cpu)[vector]))
  160. goto next;
  161. }
  162. /* Found one! */
  163. current_vector = vector;
  164. current_offset = offset;
  165. /* Schedule the old vector for cleanup on all cpus */
  166. if (d->cfg.vector)
  167. cpumask_copy(d->old_domain, d->domain);
  168. for_each_cpu(new_cpu, vector_searchmask)
  169. per_cpu(vector_irq, new_cpu)[vector] = irq_to_desc(irq);
  170. goto update;
  171. next_cpu:
  172. /*
  173. * We exclude the current @vector_cpumask from the requested
  174. * @mask and try again with the next online cpu in the
  175. * result. We cannot modify @mask, so we use @vector_cpumask
  176. * as a temporary buffer here as it will be reassigned when
  177. * calling apic->vector_allocation_domain() above.
  178. */
  179. cpumask_or(searched_cpumask, searched_cpumask, vector_cpumask);
  180. cpumask_andnot(vector_cpumask, mask, searched_cpumask);
  181. cpu = cpumask_first_and(vector_cpumask, cpu_online_mask);
  182. continue;
  183. }
  184. return -ENOSPC;
  185. update:
  186. /*
  187. * Exclude offline cpus from the cleanup mask and set the
  188. * move_in_progress flag when the result is not empty.
  189. */
  190. cpumask_and(d->old_domain, d->old_domain, cpu_online_mask);
  191. d->move_in_progress = !cpumask_empty(d->old_domain);
  192. d->cfg.old_vector = d->move_in_progress ? d->cfg.vector : 0;
  193. d->cfg.vector = vector;
  194. cpumask_copy(d->domain, vector_cpumask);
  195. success:
  196. /*
  197. * Cache destination APIC IDs into cfg->dest_apicid. This cannot fail
  198. * as we already established, that mask & d->domain & cpu_online_mask
  199. * is not empty.
  200. *
  201. * vector_searchmask is a subset of d->domain and has the offline
  202. * cpus masked out.
  203. */
  204. cpumask_and(vector_searchmask, vector_searchmask, mask);
  205. BUG_ON(apic->cpu_mask_to_apicid(vector_searchmask, irqdata,
  206. &d->cfg.dest_apicid));
  207. return 0;
  208. }
  209. static int assign_irq_vector(int irq, struct apic_chip_data *data,
  210. const struct cpumask *mask,
  211. struct irq_data *irqdata)
  212. {
  213. int err;
  214. unsigned long flags;
  215. raw_spin_lock_irqsave(&vector_lock, flags);
  216. err = __assign_irq_vector(irq, data, mask, irqdata);
  217. raw_spin_unlock_irqrestore(&vector_lock, flags);
  218. return err;
  219. }
  220. static int assign_irq_vector_policy(int irq, int node,
  221. struct apic_chip_data *data,
  222. struct irq_alloc_info *info,
  223. struct irq_data *irqdata)
  224. {
  225. if (info && info->mask)
  226. return assign_irq_vector(irq, data, info->mask, irqdata);
  227. if (node != NUMA_NO_NODE &&
  228. assign_irq_vector(irq, data, cpumask_of_node(node), irqdata) == 0)
  229. return 0;
  230. return assign_irq_vector(irq, data, apic->target_cpus(), irqdata);
  231. }
  232. static void clear_irq_vector(int irq, struct apic_chip_data *data)
  233. {
  234. struct irq_desc *desc;
  235. int cpu, vector;
  236. if (!data->cfg.vector)
  237. return;
  238. vector = data->cfg.vector;
  239. for_each_cpu_and(cpu, data->domain, cpu_online_mask)
  240. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
  241. data->cfg.vector = 0;
  242. cpumask_clear(data->domain);
  243. /*
  244. * If move is in progress or the old_domain mask is not empty,
  245. * i.e. the cleanup IPI has not been processed yet, we need to remove
  246. * the old references to desc from all cpus vector tables.
  247. */
  248. if (!data->move_in_progress && cpumask_empty(data->old_domain))
  249. return;
  250. desc = irq_to_desc(irq);
  251. for_each_cpu_and(cpu, data->old_domain, cpu_online_mask) {
  252. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
  253. vector++) {
  254. if (per_cpu(vector_irq, cpu)[vector] != desc)
  255. continue;
  256. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
  257. break;
  258. }
  259. }
  260. data->move_in_progress = 0;
  261. }
  262. void init_irq_alloc_info(struct irq_alloc_info *info,
  263. const struct cpumask *mask)
  264. {
  265. memset(info, 0, sizeof(*info));
  266. info->mask = mask;
  267. }
  268. void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
  269. {
  270. if (src)
  271. *dst = *src;
  272. else
  273. memset(dst, 0, sizeof(*dst));
  274. }
  275. static void x86_vector_free_irqs(struct irq_domain *domain,
  276. unsigned int virq, unsigned int nr_irqs)
  277. {
  278. struct apic_chip_data *apic_data;
  279. struct irq_data *irq_data;
  280. unsigned long flags;
  281. int i;
  282. for (i = 0; i < nr_irqs; i++) {
  283. irq_data = irq_domain_get_irq_data(x86_vector_domain, virq + i);
  284. if (irq_data && irq_data->chip_data) {
  285. raw_spin_lock_irqsave(&vector_lock, flags);
  286. clear_irq_vector(virq + i, irq_data->chip_data);
  287. apic_data = irq_data->chip_data;
  288. irq_domain_reset_irq_data(irq_data);
  289. raw_spin_unlock_irqrestore(&vector_lock, flags);
  290. free_apic_chip_data(apic_data);
  291. #ifdef CONFIG_X86_IO_APIC
  292. if (virq + i < nr_legacy_irqs())
  293. legacy_irq_data[virq + i] = NULL;
  294. #endif
  295. }
  296. }
  297. }
  298. static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
  299. unsigned int nr_irqs, void *arg)
  300. {
  301. struct irq_alloc_info *info = arg;
  302. struct apic_chip_data *data;
  303. struct irq_data *irq_data;
  304. int i, err, node;
  305. if (disable_apic)
  306. return -ENXIO;
  307. /* Currently vector allocator can't guarantee contiguous allocations */
  308. if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
  309. return -ENOSYS;
  310. for (i = 0; i < nr_irqs; i++) {
  311. irq_data = irq_domain_get_irq_data(domain, virq + i);
  312. BUG_ON(!irq_data);
  313. node = irq_data_get_node(irq_data);
  314. #ifdef CONFIG_X86_IO_APIC
  315. if (virq + i < nr_legacy_irqs() && legacy_irq_data[virq + i])
  316. data = legacy_irq_data[virq + i];
  317. else
  318. #endif
  319. data = alloc_apic_chip_data(node);
  320. if (!data) {
  321. err = -ENOMEM;
  322. goto error;
  323. }
  324. irq_data->chip = &lapic_controller;
  325. irq_data->chip_data = data;
  326. irq_data->hwirq = virq + i;
  327. err = assign_irq_vector_policy(virq + i, node, data, info,
  328. irq_data);
  329. if (err)
  330. goto error;
  331. /*
  332. * If the apic destination mode is physical, then the
  333. * effective affinity is restricted to a single target
  334. * CPU. Mark the interrupt accordingly.
  335. */
  336. if (!apic->irq_dest_mode)
  337. irqd_set_single_target(irq_data);
  338. }
  339. return 0;
  340. error:
  341. x86_vector_free_irqs(domain, virq, i + 1);
  342. return err;
  343. }
  344. static const struct irq_domain_ops x86_vector_domain_ops = {
  345. .alloc = x86_vector_alloc_irqs,
  346. .free = x86_vector_free_irqs,
  347. };
  348. int __init arch_probe_nr_irqs(void)
  349. {
  350. int nr;
  351. if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
  352. nr_irqs = NR_VECTORS * nr_cpu_ids;
  353. nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
  354. #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
  355. /*
  356. * for MSI and HT dyn irq
  357. */
  358. if (gsi_top <= NR_IRQS_LEGACY)
  359. nr += 8 * nr_cpu_ids;
  360. else
  361. nr += gsi_top * 16;
  362. #endif
  363. if (nr < nr_irqs)
  364. nr_irqs = nr;
  365. /*
  366. * We don't know if PIC is present at this point so we need to do
  367. * probe() to get the right number of legacy IRQs.
  368. */
  369. return legacy_pic->probe();
  370. }
  371. #ifdef CONFIG_X86_IO_APIC
  372. static void __init init_legacy_irqs(void)
  373. {
  374. int i, node = cpu_to_node(0);
  375. struct apic_chip_data *data;
  376. /*
  377. * For legacy IRQ's, start with assigning irq0 to irq15 to
  378. * ISA_IRQ_VECTOR(i) for all cpu's.
  379. */
  380. for (i = 0; i < nr_legacy_irqs(); i++) {
  381. data = legacy_irq_data[i] = alloc_apic_chip_data(node);
  382. BUG_ON(!data);
  383. data->cfg.vector = ISA_IRQ_VECTOR(i);
  384. cpumask_setall(data->domain);
  385. irq_set_chip_data(i, data);
  386. }
  387. }
  388. #else
  389. static inline void init_legacy_irqs(void) { }
  390. #endif
  391. int __init arch_early_irq_init(void)
  392. {
  393. struct fwnode_handle *fn;
  394. init_legacy_irqs();
  395. fn = irq_domain_alloc_named_fwnode("VECTOR");
  396. BUG_ON(!fn);
  397. x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
  398. NULL);
  399. BUG_ON(x86_vector_domain == NULL);
  400. irq_domain_free_fwnode(fn);
  401. irq_set_default_host(x86_vector_domain);
  402. arch_init_msi_domain(x86_vector_domain);
  403. arch_init_htirq_domain(x86_vector_domain);
  404. BUG_ON(!alloc_cpumask_var(&vector_cpumask, GFP_KERNEL));
  405. BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
  406. BUG_ON(!alloc_cpumask_var(&searched_cpumask, GFP_KERNEL));
  407. return arch_early_ioapic_init();
  408. }
  409. /* Initialize vector_irq on a new cpu */
  410. static void __setup_vector_irq(int cpu)
  411. {
  412. struct apic_chip_data *data;
  413. struct irq_desc *desc;
  414. int irq, vector;
  415. /* Mark the inuse vectors */
  416. for_each_irq_desc(irq, desc) {
  417. struct irq_data *idata = irq_desc_get_irq_data(desc);
  418. data = apic_chip_data(idata);
  419. if (!data || !cpumask_test_cpu(cpu, data->domain))
  420. continue;
  421. vector = data->cfg.vector;
  422. per_cpu(vector_irq, cpu)[vector] = desc;
  423. }
  424. /* Mark the free vectors */
  425. for (vector = 0; vector < NR_VECTORS; ++vector) {
  426. desc = per_cpu(vector_irq, cpu)[vector];
  427. if (IS_ERR_OR_NULL(desc))
  428. continue;
  429. data = apic_chip_data(irq_desc_get_irq_data(desc));
  430. if (!cpumask_test_cpu(cpu, data->domain))
  431. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
  432. }
  433. }
  434. /*
  435. * Setup the vector to irq mappings. Must be called with vector_lock held.
  436. */
  437. void setup_vector_irq(int cpu)
  438. {
  439. int irq;
  440. lockdep_assert_held(&vector_lock);
  441. /*
  442. * On most of the platforms, legacy PIC delivers the interrupts on the
  443. * boot cpu. But there are certain platforms where PIC interrupts are
  444. * delivered to multiple cpu's. If the legacy IRQ is handled by the
  445. * legacy PIC, for the new cpu that is coming online, setup the static
  446. * legacy vector to irq mapping:
  447. */
  448. for (irq = 0; irq < nr_legacy_irqs(); irq++)
  449. per_cpu(vector_irq, cpu)[ISA_IRQ_VECTOR(irq)] = irq_to_desc(irq);
  450. __setup_vector_irq(cpu);
  451. }
  452. static int apic_retrigger_irq(struct irq_data *irq_data)
  453. {
  454. struct apic_chip_data *data = apic_chip_data(irq_data);
  455. unsigned long flags;
  456. int cpu;
  457. raw_spin_lock_irqsave(&vector_lock, flags);
  458. cpu = cpumask_first_and(data->domain, cpu_online_mask);
  459. apic->send_IPI_mask(cpumask_of(cpu), data->cfg.vector);
  460. raw_spin_unlock_irqrestore(&vector_lock, flags);
  461. return 1;
  462. }
  463. void apic_ack_edge(struct irq_data *data)
  464. {
  465. irq_complete_move(irqd_cfg(data));
  466. irq_move_irq(data);
  467. ack_APIC_irq();
  468. }
  469. static int apic_set_affinity(struct irq_data *irq_data,
  470. const struct cpumask *dest, bool force)
  471. {
  472. struct apic_chip_data *data = irq_data->chip_data;
  473. int err, irq = irq_data->irq;
  474. if (!IS_ENABLED(CONFIG_SMP))
  475. return -EPERM;
  476. if (!cpumask_intersects(dest, cpu_online_mask))
  477. return -EINVAL;
  478. err = assign_irq_vector(irq, data, dest, irq_data);
  479. return err ? err : IRQ_SET_MASK_OK;
  480. }
  481. static struct irq_chip lapic_controller = {
  482. .name = "APIC",
  483. .irq_ack = apic_ack_edge,
  484. .irq_set_affinity = apic_set_affinity,
  485. .irq_retrigger = apic_retrigger_irq,
  486. };
  487. #ifdef CONFIG_SMP
  488. static void __send_cleanup_vector(struct apic_chip_data *data)
  489. {
  490. raw_spin_lock(&vector_lock);
  491. cpumask_and(data->old_domain, data->old_domain, cpu_online_mask);
  492. data->move_in_progress = 0;
  493. if (!cpumask_empty(data->old_domain))
  494. apic->send_IPI_mask(data->old_domain, IRQ_MOVE_CLEANUP_VECTOR);
  495. raw_spin_unlock(&vector_lock);
  496. }
  497. void send_cleanup_vector(struct irq_cfg *cfg)
  498. {
  499. struct apic_chip_data *data;
  500. data = container_of(cfg, struct apic_chip_data, cfg);
  501. if (data->move_in_progress)
  502. __send_cleanup_vector(data);
  503. }
  504. asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
  505. {
  506. unsigned vector, me;
  507. entering_ack_irq();
  508. /* Prevent vectors vanishing under us */
  509. raw_spin_lock(&vector_lock);
  510. me = smp_processor_id();
  511. for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
  512. struct apic_chip_data *data;
  513. struct irq_desc *desc;
  514. unsigned int irr;
  515. retry:
  516. desc = __this_cpu_read(vector_irq[vector]);
  517. if (IS_ERR_OR_NULL(desc))
  518. continue;
  519. if (!raw_spin_trylock(&desc->lock)) {
  520. raw_spin_unlock(&vector_lock);
  521. cpu_relax();
  522. raw_spin_lock(&vector_lock);
  523. goto retry;
  524. }
  525. data = apic_chip_data(irq_desc_get_irq_data(desc));
  526. if (!data)
  527. goto unlock;
  528. /*
  529. * Nothing to cleanup if irq migration is in progress
  530. * or this cpu is not set in the cleanup mask.
  531. */
  532. if (data->move_in_progress ||
  533. !cpumask_test_cpu(me, data->old_domain))
  534. goto unlock;
  535. /*
  536. * We have two cases to handle here:
  537. * 1) vector is unchanged but the target mask got reduced
  538. * 2) vector and the target mask has changed
  539. *
  540. * #1 is obvious, but in #2 we have two vectors with the same
  541. * irq descriptor: the old and the new vector. So we need to
  542. * make sure that we only cleanup the old vector. The new
  543. * vector has the current @vector number in the config and
  544. * this cpu is part of the target mask. We better leave that
  545. * one alone.
  546. */
  547. if (vector == data->cfg.vector &&
  548. cpumask_test_cpu(me, data->domain))
  549. goto unlock;
  550. irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
  551. /*
  552. * Check if the vector that needs to be cleanedup is
  553. * registered at the cpu's IRR. If so, then this is not
  554. * the best time to clean it up. Lets clean it up in the
  555. * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
  556. * to myself.
  557. */
  558. if (irr & (1 << (vector % 32))) {
  559. apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
  560. goto unlock;
  561. }
  562. __this_cpu_write(vector_irq[vector], VECTOR_UNUSED);
  563. cpumask_clear_cpu(me, data->old_domain);
  564. unlock:
  565. raw_spin_unlock(&desc->lock);
  566. }
  567. raw_spin_unlock(&vector_lock);
  568. exiting_irq();
  569. }
  570. static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
  571. {
  572. unsigned me;
  573. struct apic_chip_data *data;
  574. data = container_of(cfg, struct apic_chip_data, cfg);
  575. if (likely(!data->move_in_progress))
  576. return;
  577. me = smp_processor_id();
  578. if (vector == data->cfg.vector && cpumask_test_cpu(me, data->domain))
  579. __send_cleanup_vector(data);
  580. }
  581. void irq_complete_move(struct irq_cfg *cfg)
  582. {
  583. __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
  584. }
  585. /*
  586. * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
  587. */
  588. void irq_force_complete_move(struct irq_desc *desc)
  589. {
  590. struct irq_data *irqdata;
  591. struct apic_chip_data *data;
  592. struct irq_cfg *cfg;
  593. unsigned int cpu;
  594. /*
  595. * The function is called for all descriptors regardless of which
  596. * irqdomain they belong to. For example if an IRQ is provided by
  597. * an irq_chip as part of a GPIO driver, the chip data for that
  598. * descriptor is specific to the irq_chip in question.
  599. *
  600. * Check first that the chip_data is what we expect
  601. * (apic_chip_data) before touching it any further.
  602. */
  603. irqdata = irq_domain_get_irq_data(x86_vector_domain,
  604. irq_desc_get_irq(desc));
  605. if (!irqdata)
  606. return;
  607. data = apic_chip_data(irqdata);
  608. cfg = data ? &data->cfg : NULL;
  609. if (!cfg)
  610. return;
  611. /*
  612. * This is tricky. If the cleanup of @data->old_domain has not been
  613. * done yet, then the following setaffinity call will fail with
  614. * -EBUSY. This can leave the interrupt in a stale state.
  615. *
  616. * All CPUs are stuck in stop machine with interrupts disabled so
  617. * calling __irq_complete_move() would be completely pointless.
  618. */
  619. raw_spin_lock(&vector_lock);
  620. /*
  621. * Clean out all offline cpus (including the outgoing one) from the
  622. * old_domain mask.
  623. */
  624. cpumask_and(data->old_domain, data->old_domain, cpu_online_mask);
  625. /*
  626. * If move_in_progress is cleared and the old_domain mask is empty,
  627. * then there is nothing to cleanup. fixup_irqs() will take care of
  628. * the stale vectors on the outgoing cpu.
  629. */
  630. if (!data->move_in_progress && cpumask_empty(data->old_domain)) {
  631. raw_spin_unlock(&vector_lock);
  632. return;
  633. }
  634. /*
  635. * 1) The interrupt is in move_in_progress state. That means that we
  636. * have not seen an interrupt since the io_apic was reprogrammed to
  637. * the new vector.
  638. *
  639. * 2) The interrupt has fired on the new vector, but the cleanup IPIs
  640. * have not been processed yet.
  641. */
  642. if (data->move_in_progress) {
  643. /*
  644. * In theory there is a race:
  645. *
  646. * set_ioapic(new_vector) <-- Interrupt is raised before update
  647. * is effective, i.e. it's raised on
  648. * the old vector.
  649. *
  650. * So if the target cpu cannot handle that interrupt before
  651. * the old vector is cleaned up, we get a spurious interrupt
  652. * and in the worst case the ioapic irq line becomes stale.
  653. *
  654. * But in case of cpu hotplug this should be a non issue
  655. * because if the affinity update happens right before all
  656. * cpus rendevouz in stop machine, there is no way that the
  657. * interrupt can be blocked on the target cpu because all cpus
  658. * loops first with interrupts enabled in stop machine, so the
  659. * old vector is not yet cleaned up when the interrupt fires.
  660. *
  661. * So the only way to run into this issue is if the delivery
  662. * of the interrupt on the apic/system bus would be delayed
  663. * beyond the point where the target cpu disables interrupts
  664. * in stop machine. I doubt that it can happen, but at least
  665. * there is a theroretical chance. Virtualization might be
  666. * able to expose this, but AFAICT the IOAPIC emulation is not
  667. * as stupid as the real hardware.
  668. *
  669. * Anyway, there is nothing we can do about that at this point
  670. * w/o refactoring the whole fixup_irq() business completely.
  671. * We print at least the irq number and the old vector number,
  672. * so we have the necessary information when a problem in that
  673. * area arises.
  674. */
  675. pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
  676. irqdata->irq, cfg->old_vector);
  677. }
  678. /*
  679. * If old_domain is not empty, then other cpus still have the irq
  680. * descriptor set in their vector array. Clean it up.
  681. */
  682. for_each_cpu(cpu, data->old_domain)
  683. per_cpu(vector_irq, cpu)[cfg->old_vector] = VECTOR_UNUSED;
  684. /* Cleanup the left overs of the (half finished) move */
  685. cpumask_clear(data->old_domain);
  686. data->move_in_progress = 0;
  687. raw_spin_unlock(&vector_lock);
  688. }
  689. #endif
  690. static void __init print_APIC_field(int base)
  691. {
  692. int i;
  693. printk(KERN_DEBUG);
  694. for (i = 0; i < 8; i++)
  695. pr_cont("%08x", apic_read(base + i*0x10));
  696. pr_cont("\n");
  697. }
  698. static void __init print_local_APIC(void *dummy)
  699. {
  700. unsigned int i, v, ver, maxlvt;
  701. u64 icr;
  702. pr_debug("printing local APIC contents on CPU#%d/%d:\n",
  703. smp_processor_id(), hard_smp_processor_id());
  704. v = apic_read(APIC_ID);
  705. pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
  706. v = apic_read(APIC_LVR);
  707. pr_info("... APIC VERSION: %08x\n", v);
  708. ver = GET_APIC_VERSION(v);
  709. maxlvt = lapic_get_maxlvt();
  710. v = apic_read(APIC_TASKPRI);
  711. pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  712. /* !82489DX */
  713. if (APIC_INTEGRATED(ver)) {
  714. if (!APIC_XAPIC(ver)) {
  715. v = apic_read(APIC_ARBPRI);
  716. pr_debug("... APIC ARBPRI: %08x (%02x)\n",
  717. v, v & APIC_ARBPRI_MASK);
  718. }
  719. v = apic_read(APIC_PROCPRI);
  720. pr_debug("... APIC PROCPRI: %08x\n", v);
  721. }
  722. /*
  723. * Remote read supported only in the 82489DX and local APIC for
  724. * Pentium processors.
  725. */
  726. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  727. v = apic_read(APIC_RRR);
  728. pr_debug("... APIC RRR: %08x\n", v);
  729. }
  730. v = apic_read(APIC_LDR);
  731. pr_debug("... APIC LDR: %08x\n", v);
  732. if (!x2apic_enabled()) {
  733. v = apic_read(APIC_DFR);
  734. pr_debug("... APIC DFR: %08x\n", v);
  735. }
  736. v = apic_read(APIC_SPIV);
  737. pr_debug("... APIC SPIV: %08x\n", v);
  738. pr_debug("... APIC ISR field:\n");
  739. print_APIC_field(APIC_ISR);
  740. pr_debug("... APIC TMR field:\n");
  741. print_APIC_field(APIC_TMR);
  742. pr_debug("... APIC IRR field:\n");
  743. print_APIC_field(APIC_IRR);
  744. /* !82489DX */
  745. if (APIC_INTEGRATED(ver)) {
  746. /* Due to the Pentium erratum 3AP. */
  747. if (maxlvt > 3)
  748. apic_write(APIC_ESR, 0);
  749. v = apic_read(APIC_ESR);
  750. pr_debug("... APIC ESR: %08x\n", v);
  751. }
  752. icr = apic_icr_read();
  753. pr_debug("... APIC ICR: %08x\n", (u32)icr);
  754. pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
  755. v = apic_read(APIC_LVTT);
  756. pr_debug("... APIC LVTT: %08x\n", v);
  757. if (maxlvt > 3) {
  758. /* PC is LVT#4. */
  759. v = apic_read(APIC_LVTPC);
  760. pr_debug("... APIC LVTPC: %08x\n", v);
  761. }
  762. v = apic_read(APIC_LVT0);
  763. pr_debug("... APIC LVT0: %08x\n", v);
  764. v = apic_read(APIC_LVT1);
  765. pr_debug("... APIC LVT1: %08x\n", v);
  766. if (maxlvt > 2) {
  767. /* ERR is LVT#3. */
  768. v = apic_read(APIC_LVTERR);
  769. pr_debug("... APIC LVTERR: %08x\n", v);
  770. }
  771. v = apic_read(APIC_TMICT);
  772. pr_debug("... APIC TMICT: %08x\n", v);
  773. v = apic_read(APIC_TMCCT);
  774. pr_debug("... APIC TMCCT: %08x\n", v);
  775. v = apic_read(APIC_TDCR);
  776. pr_debug("... APIC TDCR: %08x\n", v);
  777. if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
  778. v = apic_read(APIC_EFEAT);
  779. maxlvt = (v >> 16) & 0xff;
  780. pr_debug("... APIC EFEAT: %08x\n", v);
  781. v = apic_read(APIC_ECTRL);
  782. pr_debug("... APIC ECTRL: %08x\n", v);
  783. for (i = 0; i < maxlvt; i++) {
  784. v = apic_read(APIC_EILVTn(i));
  785. pr_debug("... APIC EILVT%d: %08x\n", i, v);
  786. }
  787. }
  788. pr_cont("\n");
  789. }
  790. static void __init print_local_APICs(int maxcpu)
  791. {
  792. int cpu;
  793. if (!maxcpu)
  794. return;
  795. preempt_disable();
  796. for_each_online_cpu(cpu) {
  797. if (cpu >= maxcpu)
  798. break;
  799. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  800. }
  801. preempt_enable();
  802. }
  803. static void __init print_PIC(void)
  804. {
  805. unsigned int v;
  806. unsigned long flags;
  807. if (!nr_legacy_irqs())
  808. return;
  809. pr_debug("\nprinting PIC contents\n");
  810. raw_spin_lock_irqsave(&i8259A_lock, flags);
  811. v = inb(0xa1) << 8 | inb(0x21);
  812. pr_debug("... PIC IMR: %04x\n", v);
  813. v = inb(0xa0) << 8 | inb(0x20);
  814. pr_debug("... PIC IRR: %04x\n", v);
  815. outb(0x0b, 0xa0);
  816. outb(0x0b, 0x20);
  817. v = inb(0xa0) << 8 | inb(0x20);
  818. outb(0x0a, 0xa0);
  819. outb(0x0a, 0x20);
  820. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  821. pr_debug("... PIC ISR: %04x\n", v);
  822. v = inb(0x4d1) << 8 | inb(0x4d0);
  823. pr_debug("... PIC ELCR: %04x\n", v);
  824. }
  825. static int show_lapic __initdata = 1;
  826. static __init int setup_show_lapic(char *arg)
  827. {
  828. int num = -1;
  829. if (strcmp(arg, "all") == 0) {
  830. show_lapic = CONFIG_NR_CPUS;
  831. } else {
  832. get_option(&arg, &num);
  833. if (num >= 0)
  834. show_lapic = num;
  835. }
  836. return 1;
  837. }
  838. __setup("show_lapic=", setup_show_lapic);
  839. static int __init print_ICs(void)
  840. {
  841. if (apic_verbosity == APIC_QUIET)
  842. return 0;
  843. print_PIC();
  844. /* don't print out if apic is not there */
  845. if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
  846. return 0;
  847. print_local_APICs(show_lapic);
  848. print_IO_APICs();
  849. return 0;
  850. }
  851. late_initcall(print_ICs);