vector.c 25 KB

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