vector.c 25 KB

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