vector.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /*
  2. * Local APIC related interfaces to support IOAPIC, MSI, 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/seq_file.h>
  15. #include <linux/init.h>
  16. #include <linux/compiler.h>
  17. #include <linux/slab.h>
  18. #include <asm/irqdomain.h>
  19. #include <asm/hw_irq.h>
  20. #include <asm/apic.h>
  21. #include <asm/i8259.h>
  22. #include <asm/desc.h>
  23. #include <asm/irq_remapping.h>
  24. #include <asm/trace/irq_vectors.h>
  25. struct apic_chip_data {
  26. struct irq_cfg hw_irq_cfg;
  27. unsigned int vector;
  28. unsigned int prev_vector;
  29. unsigned int cpu;
  30. unsigned int prev_cpu;
  31. unsigned int irq;
  32. struct hlist_node clist;
  33. unsigned int move_in_progress : 1,
  34. is_managed : 1,
  35. can_reserve : 1,
  36. has_reserved : 1;
  37. };
  38. struct irq_domain *x86_vector_domain;
  39. EXPORT_SYMBOL_GPL(x86_vector_domain);
  40. static DEFINE_RAW_SPINLOCK(vector_lock);
  41. static cpumask_var_t vector_searchmask;
  42. static struct irq_chip lapic_controller;
  43. static struct irq_matrix *vector_matrix;
  44. #ifdef CONFIG_SMP
  45. static DEFINE_PER_CPU(struct hlist_head, cleanup_list);
  46. #endif
  47. void lock_vector_lock(void)
  48. {
  49. /* Used to the online set of cpus does not change
  50. * during assign_irq_vector.
  51. */
  52. raw_spin_lock(&vector_lock);
  53. }
  54. void unlock_vector_lock(void)
  55. {
  56. raw_spin_unlock(&vector_lock);
  57. }
  58. void init_irq_alloc_info(struct irq_alloc_info *info,
  59. const struct cpumask *mask)
  60. {
  61. memset(info, 0, sizeof(*info));
  62. info->mask = mask;
  63. }
  64. void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
  65. {
  66. if (src)
  67. *dst = *src;
  68. else
  69. memset(dst, 0, sizeof(*dst));
  70. }
  71. static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
  72. {
  73. if (!irqd)
  74. return NULL;
  75. while (irqd->parent_data)
  76. irqd = irqd->parent_data;
  77. return irqd->chip_data;
  78. }
  79. struct irq_cfg *irqd_cfg(struct irq_data *irqd)
  80. {
  81. struct apic_chip_data *apicd = apic_chip_data(irqd);
  82. return apicd ? &apicd->hw_irq_cfg : NULL;
  83. }
  84. EXPORT_SYMBOL_GPL(irqd_cfg);
  85. struct irq_cfg *irq_cfg(unsigned int irq)
  86. {
  87. return irqd_cfg(irq_get_irq_data(irq));
  88. }
  89. static struct apic_chip_data *alloc_apic_chip_data(int node)
  90. {
  91. struct apic_chip_data *apicd;
  92. apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
  93. if (apicd)
  94. INIT_HLIST_NODE(&apicd->clist);
  95. return apicd;
  96. }
  97. static void free_apic_chip_data(struct apic_chip_data *apicd)
  98. {
  99. kfree(apicd);
  100. }
  101. static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector,
  102. unsigned int cpu)
  103. {
  104. struct apic_chip_data *apicd = apic_chip_data(irqd);
  105. lockdep_assert_held(&vector_lock);
  106. apicd->hw_irq_cfg.vector = vector;
  107. apicd->hw_irq_cfg.dest_apicid = apic->calc_dest_apicid(cpu);
  108. irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
  109. trace_vector_config(irqd->irq, vector, cpu,
  110. apicd->hw_irq_cfg.dest_apicid);
  111. }
  112. static void apic_update_vector(struct irq_data *irqd, unsigned int newvec,
  113. unsigned int newcpu)
  114. {
  115. struct apic_chip_data *apicd = apic_chip_data(irqd);
  116. struct irq_desc *desc = irq_data_to_desc(irqd);
  117. lockdep_assert_held(&vector_lock);
  118. trace_vector_update(irqd->irq, newvec, newcpu, apicd->vector,
  119. apicd->cpu);
  120. /* Setup the vector move, if required */
  121. if (apicd->vector && cpu_online(apicd->cpu)) {
  122. apicd->move_in_progress = true;
  123. apicd->prev_vector = apicd->vector;
  124. apicd->prev_cpu = apicd->cpu;
  125. } else {
  126. apicd->prev_vector = 0;
  127. }
  128. apicd->vector = newvec;
  129. apicd->cpu = newcpu;
  130. BUG_ON(!IS_ERR_OR_NULL(per_cpu(vector_irq, newcpu)[newvec]));
  131. per_cpu(vector_irq, newcpu)[newvec] = desc;
  132. }
  133. static void vector_assign_managed_shutdown(struct irq_data *irqd)
  134. {
  135. unsigned int cpu = cpumask_first(cpu_online_mask);
  136. apic_update_irq_cfg(irqd, MANAGED_IRQ_SHUTDOWN_VECTOR, cpu);
  137. }
  138. static int reserve_managed_vector(struct irq_data *irqd)
  139. {
  140. const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
  141. struct apic_chip_data *apicd = apic_chip_data(irqd);
  142. unsigned long flags;
  143. int ret;
  144. raw_spin_lock_irqsave(&vector_lock, flags);
  145. apicd->is_managed = true;
  146. ret = irq_matrix_reserve_managed(vector_matrix, affmsk);
  147. raw_spin_unlock_irqrestore(&vector_lock, flags);
  148. trace_vector_reserve_managed(irqd->irq, ret);
  149. return ret;
  150. }
  151. static void reserve_irq_vector_locked(struct irq_data *irqd)
  152. {
  153. struct apic_chip_data *apicd = apic_chip_data(irqd);
  154. irq_matrix_reserve(vector_matrix);
  155. apicd->can_reserve = true;
  156. apicd->has_reserved = true;
  157. trace_vector_reserve(irqd->irq, 0);
  158. vector_assign_managed_shutdown(irqd);
  159. }
  160. static int reserve_irq_vector(struct irq_data *irqd)
  161. {
  162. unsigned long flags;
  163. raw_spin_lock_irqsave(&vector_lock, flags);
  164. reserve_irq_vector_locked(irqd);
  165. raw_spin_unlock_irqrestore(&vector_lock, flags);
  166. return 0;
  167. }
  168. static int allocate_vector(struct irq_data *irqd, const struct cpumask *dest)
  169. {
  170. struct apic_chip_data *apicd = apic_chip_data(irqd);
  171. bool resvd = apicd->has_reserved;
  172. unsigned int cpu = apicd->cpu;
  173. int vector = apicd->vector;
  174. lockdep_assert_held(&vector_lock);
  175. /*
  176. * If the current target CPU is online and in the new requested
  177. * affinity mask, there is no point in moving the interrupt from
  178. * one CPU to another.
  179. */
  180. if (vector && cpu_online(cpu) && cpumask_test_cpu(cpu, dest))
  181. return 0;
  182. vector = irq_matrix_alloc(vector_matrix, dest, resvd, &cpu);
  183. if (vector > 0)
  184. apic_update_vector(irqd, vector, cpu);
  185. trace_vector_alloc(irqd->irq, vector, resvd, vector);
  186. return vector;
  187. }
  188. static int assign_vector_locked(struct irq_data *irqd,
  189. const struct cpumask *dest)
  190. {
  191. struct apic_chip_data *apicd = apic_chip_data(irqd);
  192. int vector = allocate_vector(irqd, dest);
  193. if (vector < 0)
  194. return vector;
  195. apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu);
  196. return 0;
  197. }
  198. static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest)
  199. {
  200. unsigned long flags;
  201. int ret;
  202. raw_spin_lock_irqsave(&vector_lock, flags);
  203. cpumask_and(vector_searchmask, dest, cpu_online_mask);
  204. ret = assign_vector_locked(irqd, vector_searchmask);
  205. raw_spin_unlock_irqrestore(&vector_lock, flags);
  206. return ret;
  207. }
  208. static int assign_irq_vector_any_locked(struct irq_data *irqd)
  209. {
  210. /* Get the affinity mask - either irq_default_affinity or (user) set */
  211. const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
  212. int node = irq_data_get_node(irqd);
  213. if (node == NUMA_NO_NODE)
  214. goto all;
  215. /* Try the intersection of @affmsk and node mask */
  216. cpumask_and(vector_searchmask, cpumask_of_node(node), affmsk);
  217. if (!assign_vector_locked(irqd, vector_searchmask))
  218. return 0;
  219. /* Try the node mask */
  220. if (!assign_vector_locked(irqd, cpumask_of_node(node)))
  221. return 0;
  222. all:
  223. /* Try the full affinity mask */
  224. cpumask_and(vector_searchmask, affmsk, cpu_online_mask);
  225. if (!assign_vector_locked(irqd, vector_searchmask))
  226. return 0;
  227. /* Try the full online mask */
  228. return assign_vector_locked(irqd, cpu_online_mask);
  229. }
  230. static int
  231. assign_irq_vector_policy(struct irq_data *irqd, struct irq_alloc_info *info)
  232. {
  233. if (irqd_affinity_is_managed(irqd))
  234. return reserve_managed_vector(irqd);
  235. if (info->mask)
  236. return assign_irq_vector(irqd, info->mask);
  237. /*
  238. * Make only a global reservation with no guarantee. A real vector
  239. * is associated at activation time.
  240. */
  241. return reserve_irq_vector(irqd);
  242. }
  243. static int
  244. assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest)
  245. {
  246. const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
  247. struct apic_chip_data *apicd = apic_chip_data(irqd);
  248. int vector, cpu;
  249. cpumask_and(vector_searchmask, vector_searchmask, affmsk);
  250. cpu = cpumask_first(vector_searchmask);
  251. if (cpu >= nr_cpu_ids)
  252. return -EINVAL;
  253. /* set_affinity might call here for nothing */
  254. if (apicd->vector && cpumask_test_cpu(apicd->cpu, vector_searchmask))
  255. return 0;
  256. vector = irq_matrix_alloc_managed(vector_matrix, cpu);
  257. trace_vector_alloc_managed(irqd->irq, vector, vector);
  258. if (vector < 0)
  259. return vector;
  260. apic_update_vector(irqd, vector, cpu);
  261. apic_update_irq_cfg(irqd, vector, cpu);
  262. return 0;
  263. }
  264. static void clear_irq_vector(struct irq_data *irqd)
  265. {
  266. struct apic_chip_data *apicd = apic_chip_data(irqd);
  267. bool managed = irqd_affinity_is_managed(irqd);
  268. unsigned int vector = apicd->vector;
  269. lockdep_assert_held(&vector_lock);
  270. if (!vector)
  271. return;
  272. trace_vector_clear(irqd->irq, vector, apicd->cpu, apicd->prev_vector,
  273. apicd->prev_cpu);
  274. per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_UNUSED;
  275. irq_matrix_free(vector_matrix, apicd->cpu, vector, managed);
  276. apicd->vector = 0;
  277. /* Clean up move in progress */
  278. vector = apicd->prev_vector;
  279. if (!vector)
  280. return;
  281. per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_UNUSED;
  282. irq_matrix_free(vector_matrix, apicd->prev_cpu, vector, managed);
  283. apicd->prev_vector = 0;
  284. apicd->move_in_progress = 0;
  285. hlist_del_init(&apicd->clist);
  286. }
  287. static void x86_vector_deactivate(struct irq_domain *dom, struct irq_data *irqd)
  288. {
  289. struct apic_chip_data *apicd = apic_chip_data(irqd);
  290. unsigned long flags;
  291. trace_vector_deactivate(irqd->irq, apicd->is_managed,
  292. apicd->can_reserve, false);
  293. /* Regular fixed assigned interrupt */
  294. if (!apicd->is_managed && !apicd->can_reserve)
  295. return;
  296. /* If the interrupt has a global reservation, nothing to do */
  297. if (apicd->has_reserved)
  298. return;
  299. raw_spin_lock_irqsave(&vector_lock, flags);
  300. clear_irq_vector(irqd);
  301. if (apicd->can_reserve)
  302. reserve_irq_vector_locked(irqd);
  303. else
  304. vector_assign_managed_shutdown(irqd);
  305. raw_spin_unlock_irqrestore(&vector_lock, flags);
  306. }
  307. static int activate_reserved(struct irq_data *irqd)
  308. {
  309. struct apic_chip_data *apicd = apic_chip_data(irqd);
  310. int ret;
  311. ret = assign_irq_vector_any_locked(irqd);
  312. if (!ret)
  313. apicd->has_reserved = false;
  314. return ret;
  315. }
  316. static int activate_managed(struct irq_data *irqd)
  317. {
  318. const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
  319. int ret;
  320. cpumask_and(vector_searchmask, dest, cpu_online_mask);
  321. if (WARN_ON_ONCE(cpumask_empty(vector_searchmask))) {
  322. /* Something in the core code broke! Survive gracefully */
  323. pr_err("Managed startup for irq %u, but no CPU\n", irqd->irq);
  324. return EINVAL;
  325. }
  326. ret = assign_managed_vector(irqd, vector_searchmask);
  327. /*
  328. * This should not happen. The vector reservation got buggered. Handle
  329. * it gracefully.
  330. */
  331. if (WARN_ON_ONCE(ret < 0)) {
  332. pr_err("Managed startup irq %u, no vector available\n",
  333. irqd->irq);
  334. }
  335. return ret;
  336. }
  337. static int x86_vector_activate(struct irq_domain *dom, struct irq_data *irqd,
  338. bool early)
  339. {
  340. struct apic_chip_data *apicd = apic_chip_data(irqd);
  341. unsigned long flags;
  342. int ret = 0;
  343. trace_vector_activate(irqd->irq, apicd->is_managed,
  344. apicd->can_reserve, early);
  345. /* Nothing to do for fixed assigned vectors */
  346. if (!apicd->can_reserve && !apicd->is_managed)
  347. return 0;
  348. raw_spin_lock_irqsave(&vector_lock, flags);
  349. if (early || irqd_is_managed_and_shutdown(irqd))
  350. vector_assign_managed_shutdown(irqd);
  351. else if (apicd->is_managed)
  352. ret = activate_managed(irqd);
  353. else if (apicd->has_reserved)
  354. ret = activate_reserved(irqd);
  355. raw_spin_unlock_irqrestore(&vector_lock, flags);
  356. return ret;
  357. }
  358. static void vector_free_reserved_and_managed(struct irq_data *irqd)
  359. {
  360. const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
  361. struct apic_chip_data *apicd = apic_chip_data(irqd);
  362. trace_vector_teardown(irqd->irq, apicd->is_managed,
  363. apicd->has_reserved);
  364. if (apicd->has_reserved)
  365. irq_matrix_remove_reserved(vector_matrix);
  366. if (apicd->is_managed)
  367. irq_matrix_remove_managed(vector_matrix, dest);
  368. }
  369. static void x86_vector_free_irqs(struct irq_domain *domain,
  370. unsigned int virq, unsigned int nr_irqs)
  371. {
  372. struct apic_chip_data *apicd;
  373. struct irq_data *irqd;
  374. unsigned long flags;
  375. int i;
  376. for (i = 0; i < nr_irqs; i++) {
  377. irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
  378. if (irqd && irqd->chip_data) {
  379. raw_spin_lock_irqsave(&vector_lock, flags);
  380. clear_irq_vector(irqd);
  381. vector_free_reserved_and_managed(irqd);
  382. apicd = irqd->chip_data;
  383. irq_domain_reset_irq_data(irqd);
  384. raw_spin_unlock_irqrestore(&vector_lock, flags);
  385. free_apic_chip_data(apicd);
  386. }
  387. }
  388. }
  389. static bool vector_configure_legacy(unsigned int virq, struct irq_data *irqd,
  390. struct apic_chip_data *apicd)
  391. {
  392. unsigned long flags;
  393. bool realloc = false;
  394. apicd->vector = ISA_IRQ_VECTOR(virq);
  395. apicd->cpu = 0;
  396. raw_spin_lock_irqsave(&vector_lock, flags);
  397. /*
  398. * If the interrupt is activated, then it must stay at this vector
  399. * position. That's usually the timer interrupt (0).
  400. */
  401. if (irqd_is_activated(irqd)) {
  402. trace_vector_setup(virq, true, 0);
  403. apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu);
  404. } else {
  405. /* Release the vector */
  406. apicd->can_reserve = true;
  407. clear_irq_vector(irqd);
  408. realloc = true;
  409. }
  410. raw_spin_unlock_irqrestore(&vector_lock, flags);
  411. return realloc;
  412. }
  413. static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
  414. unsigned int nr_irqs, void *arg)
  415. {
  416. struct irq_alloc_info *info = arg;
  417. struct apic_chip_data *apicd;
  418. struct irq_data *irqd;
  419. int i, err, node;
  420. if (disable_apic)
  421. return -ENXIO;
  422. /* Currently vector allocator can't guarantee contiguous allocations */
  423. if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
  424. return -ENOSYS;
  425. for (i = 0; i < nr_irqs; i++) {
  426. irqd = irq_domain_get_irq_data(domain, virq + i);
  427. BUG_ON(!irqd);
  428. node = irq_data_get_node(irqd);
  429. WARN_ON_ONCE(irqd->chip_data);
  430. apicd = alloc_apic_chip_data(node);
  431. if (!apicd) {
  432. err = -ENOMEM;
  433. goto error;
  434. }
  435. apicd->irq = virq + i;
  436. irqd->chip = &lapic_controller;
  437. irqd->chip_data = apicd;
  438. irqd->hwirq = virq + i;
  439. irqd_set_single_target(irqd);
  440. /*
  441. * Legacy vectors are already assigned when the IOAPIC
  442. * takes them over. They stay on the same vector. This is
  443. * required for check_timer() to work correctly as it might
  444. * switch back to legacy mode. Only update the hardware
  445. * config.
  446. */
  447. if (info->flags & X86_IRQ_ALLOC_LEGACY) {
  448. if (!vector_configure_legacy(virq + i, irqd, apicd))
  449. continue;
  450. }
  451. err = assign_irq_vector_policy(irqd, info);
  452. trace_vector_setup(virq + i, false, err);
  453. if (err)
  454. goto error;
  455. }
  456. return 0;
  457. error:
  458. x86_vector_free_irqs(domain, virq, i + 1);
  459. return err;
  460. }
  461. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  462. void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
  463. struct irq_data *irqd, int ind)
  464. {
  465. unsigned int cpu, vector, prev_cpu, prev_vector;
  466. struct apic_chip_data *apicd;
  467. unsigned long flags;
  468. int irq;
  469. if (!irqd) {
  470. irq_matrix_debug_show(m, vector_matrix, ind);
  471. return;
  472. }
  473. irq = irqd->irq;
  474. if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
  475. seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
  476. seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
  477. return;
  478. }
  479. apicd = irqd->chip_data;
  480. if (!apicd) {
  481. seq_printf(m, "%*sVector: Not assigned\n", ind, "");
  482. return;
  483. }
  484. raw_spin_lock_irqsave(&vector_lock, flags);
  485. cpu = apicd->cpu;
  486. vector = apicd->vector;
  487. prev_cpu = apicd->prev_cpu;
  488. prev_vector = apicd->prev_vector;
  489. raw_spin_unlock_irqrestore(&vector_lock, flags);
  490. seq_printf(m, "%*sVector: %5u\n", ind, "", vector);
  491. seq_printf(m, "%*sTarget: %5u\n", ind, "", cpu);
  492. if (prev_vector) {
  493. seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", prev_vector);
  494. seq_printf(m, "%*sPrevious target: %5u\n", ind, "", prev_cpu);
  495. }
  496. }
  497. #endif
  498. static const struct irq_domain_ops x86_vector_domain_ops = {
  499. .alloc = x86_vector_alloc_irqs,
  500. .free = x86_vector_free_irqs,
  501. .activate = x86_vector_activate,
  502. .deactivate = x86_vector_deactivate,
  503. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  504. .debug_show = x86_vector_debug_show,
  505. #endif
  506. };
  507. int __init arch_probe_nr_irqs(void)
  508. {
  509. int nr;
  510. if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
  511. nr_irqs = NR_VECTORS * nr_cpu_ids;
  512. nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
  513. #if defined(CONFIG_PCI_MSI)
  514. /*
  515. * for MSI and HT dyn irq
  516. */
  517. if (gsi_top <= NR_IRQS_LEGACY)
  518. nr += 8 * nr_cpu_ids;
  519. else
  520. nr += gsi_top * 16;
  521. #endif
  522. if (nr < nr_irqs)
  523. nr_irqs = nr;
  524. /*
  525. * We don't know if PIC is present at this point so we need to do
  526. * probe() to get the right number of legacy IRQs.
  527. */
  528. return legacy_pic->probe();
  529. }
  530. void lapic_assign_legacy_vector(unsigned int irq, bool replace)
  531. {
  532. /*
  533. * Use assign system here so it wont get accounted as allocated
  534. * and moveable in the cpu hotplug check and it prevents managed
  535. * irq reservation from touching it.
  536. */
  537. irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
  538. }
  539. void __init lapic_assign_system_vectors(void)
  540. {
  541. unsigned int i, vector = 0;
  542. for_each_set_bit_from(vector, system_vectors, NR_VECTORS)
  543. irq_matrix_assign_system(vector_matrix, vector, false);
  544. if (nr_legacy_irqs() > 1)
  545. lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
  546. /* System vectors are reserved, online it */
  547. irq_matrix_online(vector_matrix);
  548. /* Mark the preallocated legacy interrupts */
  549. for (i = 0; i < nr_legacy_irqs(); i++) {
  550. if (i != PIC_CASCADE_IR)
  551. irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
  552. }
  553. }
  554. int __init arch_early_irq_init(void)
  555. {
  556. struct fwnode_handle *fn;
  557. fn = irq_domain_alloc_named_fwnode("VECTOR");
  558. BUG_ON(!fn);
  559. x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
  560. NULL);
  561. BUG_ON(x86_vector_domain == NULL);
  562. irq_domain_free_fwnode(fn);
  563. irq_set_default_host(x86_vector_domain);
  564. arch_init_msi_domain(x86_vector_domain);
  565. BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
  566. /*
  567. * Allocate the vector matrix allocator data structure and limit the
  568. * search area.
  569. */
  570. vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
  571. FIRST_SYSTEM_VECTOR);
  572. BUG_ON(!vector_matrix);
  573. return arch_early_ioapic_init();
  574. }
  575. #ifdef CONFIG_SMP
  576. static struct irq_desc *__setup_vector_irq(int vector)
  577. {
  578. int isairq = vector - ISA_IRQ_VECTOR(0);
  579. /* Check whether the irq is in the legacy space */
  580. if (isairq < 0 || isairq >= nr_legacy_irqs())
  581. return VECTOR_UNUSED;
  582. /* Check whether the irq is handled by the IOAPIC */
  583. if (test_bit(isairq, &io_apic_irqs))
  584. return VECTOR_UNUSED;
  585. return irq_to_desc(isairq);
  586. }
  587. /* Online the local APIC infrastructure and initialize the vectors */
  588. void lapic_online(void)
  589. {
  590. unsigned int vector;
  591. lockdep_assert_held(&vector_lock);
  592. /* Online the vector matrix array for this CPU */
  593. irq_matrix_online(vector_matrix);
  594. /*
  595. * The interrupt affinity logic never targets interrupts to offline
  596. * CPUs. The exception are the legacy PIC interrupts. In general
  597. * they are only targeted to CPU0, but depending on the platform
  598. * they can be distributed to any online CPU in hardware. The
  599. * kernel has no influence on that. So all active legacy vectors
  600. * must be installed on all CPUs. All non legacy interrupts can be
  601. * cleared.
  602. */
  603. for (vector = 0; vector < NR_VECTORS; vector++)
  604. this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
  605. }
  606. void lapic_offline(void)
  607. {
  608. lock_vector_lock();
  609. irq_matrix_offline(vector_matrix);
  610. unlock_vector_lock();
  611. }
  612. static int apic_set_affinity(struct irq_data *irqd,
  613. const struct cpumask *dest, bool force)
  614. {
  615. struct apic_chip_data *apicd = apic_chip_data(irqd);
  616. int err;
  617. /*
  618. * Core code can call here for inactive interrupts. For inactive
  619. * interrupts which use managed or reservation mode there is no
  620. * point in going through the vector assignment right now as the
  621. * activation will assign a vector which fits the destination
  622. * cpumask. Let the core code store the destination mask and be
  623. * done with it.
  624. */
  625. if (!irqd_is_activated(irqd) &&
  626. (apicd->is_managed || apicd->can_reserve))
  627. return IRQ_SET_MASK_OK;
  628. raw_spin_lock(&vector_lock);
  629. cpumask_and(vector_searchmask, dest, cpu_online_mask);
  630. if (irqd_affinity_is_managed(irqd))
  631. err = assign_managed_vector(irqd, vector_searchmask);
  632. else
  633. err = assign_vector_locked(irqd, vector_searchmask);
  634. raw_spin_unlock(&vector_lock);
  635. return err ? err : IRQ_SET_MASK_OK;
  636. }
  637. #else
  638. # define apic_set_affinity NULL
  639. #endif
  640. static int apic_retrigger_irq(struct irq_data *irqd)
  641. {
  642. struct apic_chip_data *apicd = apic_chip_data(irqd);
  643. unsigned long flags;
  644. raw_spin_lock_irqsave(&vector_lock, flags);
  645. apic->send_IPI(apicd->cpu, apicd->vector);
  646. raw_spin_unlock_irqrestore(&vector_lock, flags);
  647. return 1;
  648. }
  649. void apic_ack_edge(struct irq_data *irqd)
  650. {
  651. irq_complete_move(irqd_cfg(irqd));
  652. irq_move_irq(irqd);
  653. ack_APIC_irq();
  654. }
  655. static struct irq_chip lapic_controller = {
  656. .name = "APIC",
  657. .irq_ack = apic_ack_edge,
  658. .irq_set_affinity = apic_set_affinity,
  659. .irq_retrigger = apic_retrigger_irq,
  660. };
  661. #ifdef CONFIG_SMP
  662. static void free_moved_vector(struct apic_chip_data *apicd)
  663. {
  664. unsigned int vector = apicd->prev_vector;
  665. unsigned int cpu = apicd->prev_cpu;
  666. bool managed = apicd->is_managed;
  667. /*
  668. * This should never happen. Managed interrupts are not
  669. * migrated except on CPU down, which does not involve the
  670. * cleanup vector. But try to keep the accounting correct
  671. * nevertheless.
  672. */
  673. WARN_ON_ONCE(managed);
  674. trace_vector_free_moved(apicd->irq, cpu, vector, managed);
  675. irq_matrix_free(vector_matrix, cpu, vector, managed);
  676. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
  677. hlist_del_init(&apicd->clist);
  678. apicd->prev_vector = 0;
  679. apicd->move_in_progress = 0;
  680. }
  681. asmlinkage __visible void __irq_entry smp_irq_move_cleanup_interrupt(void)
  682. {
  683. struct hlist_head *clhead = this_cpu_ptr(&cleanup_list);
  684. struct apic_chip_data *apicd;
  685. struct hlist_node *tmp;
  686. entering_ack_irq();
  687. /* Prevent vectors vanishing under us */
  688. raw_spin_lock(&vector_lock);
  689. hlist_for_each_entry_safe(apicd, tmp, clhead, clist) {
  690. unsigned int irr, vector = apicd->prev_vector;
  691. /*
  692. * Paranoia: Check if the vector that needs to be cleaned
  693. * up is registered at the APICs IRR. If so, then this is
  694. * not the best time to clean it up. Clean it up in the
  695. * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
  696. * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
  697. * priority external vector, so on return from this
  698. * interrupt the device interrupt will happen first.
  699. */
  700. irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
  701. if (irr & (1U << (vector % 32))) {
  702. apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
  703. continue;
  704. }
  705. free_moved_vector(apicd);
  706. }
  707. raw_spin_unlock(&vector_lock);
  708. exiting_irq();
  709. }
  710. static void __send_cleanup_vector(struct apic_chip_data *apicd)
  711. {
  712. unsigned int cpu;
  713. raw_spin_lock(&vector_lock);
  714. apicd->move_in_progress = 0;
  715. cpu = apicd->prev_cpu;
  716. if (cpu_online(cpu)) {
  717. hlist_add_head(&apicd->clist, per_cpu_ptr(&cleanup_list, cpu));
  718. apic->send_IPI(cpu, IRQ_MOVE_CLEANUP_VECTOR);
  719. } else {
  720. apicd->prev_vector = 0;
  721. }
  722. raw_spin_unlock(&vector_lock);
  723. }
  724. void send_cleanup_vector(struct irq_cfg *cfg)
  725. {
  726. struct apic_chip_data *apicd;
  727. apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
  728. if (apicd->move_in_progress)
  729. __send_cleanup_vector(apicd);
  730. }
  731. static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
  732. {
  733. struct apic_chip_data *apicd;
  734. apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
  735. if (likely(!apicd->move_in_progress))
  736. return;
  737. if (vector == apicd->vector && apicd->cpu == smp_processor_id())
  738. __send_cleanup_vector(apicd);
  739. }
  740. void irq_complete_move(struct irq_cfg *cfg)
  741. {
  742. __irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
  743. }
  744. /*
  745. * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
  746. */
  747. void irq_force_complete_move(struct irq_desc *desc)
  748. {
  749. struct apic_chip_data *apicd;
  750. struct irq_data *irqd;
  751. unsigned int vector;
  752. /*
  753. * The function is called for all descriptors regardless of which
  754. * irqdomain they belong to. For example if an IRQ is provided by
  755. * an irq_chip as part of a GPIO driver, the chip data for that
  756. * descriptor is specific to the irq_chip in question.
  757. *
  758. * Check first that the chip_data is what we expect
  759. * (apic_chip_data) before touching it any further.
  760. */
  761. irqd = irq_domain_get_irq_data(x86_vector_domain,
  762. irq_desc_get_irq(desc));
  763. if (!irqd)
  764. return;
  765. raw_spin_lock(&vector_lock);
  766. apicd = apic_chip_data(irqd);
  767. if (!apicd)
  768. goto unlock;
  769. /*
  770. * If prev_vector is empty, no action required.
  771. */
  772. vector = apicd->prev_vector;
  773. if (!vector)
  774. goto unlock;
  775. /*
  776. * This is tricky. If the cleanup of the old vector has not been
  777. * done yet, then the following setaffinity call will fail with
  778. * -EBUSY. This can leave the interrupt in a stale state.
  779. *
  780. * All CPUs are stuck in stop machine with interrupts disabled so
  781. * calling __irq_complete_move() would be completely pointless.
  782. *
  783. * 1) The interrupt is in move_in_progress state. That means that we
  784. * have not seen an interrupt since the io_apic was reprogrammed to
  785. * the new vector.
  786. *
  787. * 2) The interrupt has fired on the new vector, but the cleanup IPIs
  788. * have not been processed yet.
  789. */
  790. if (apicd->move_in_progress) {
  791. /*
  792. * In theory there is a race:
  793. *
  794. * set_ioapic(new_vector) <-- Interrupt is raised before update
  795. * is effective, i.e. it's raised on
  796. * the old vector.
  797. *
  798. * So if the target cpu cannot handle that interrupt before
  799. * the old vector is cleaned up, we get a spurious interrupt
  800. * and in the worst case the ioapic irq line becomes stale.
  801. *
  802. * But in case of cpu hotplug this should be a non issue
  803. * because if the affinity update happens right before all
  804. * cpus rendevouz in stop machine, there is no way that the
  805. * interrupt can be blocked on the target cpu because all cpus
  806. * loops first with interrupts enabled in stop machine, so the
  807. * old vector is not yet cleaned up when the interrupt fires.
  808. *
  809. * So the only way to run into this issue is if the delivery
  810. * of the interrupt on the apic/system bus would be delayed
  811. * beyond the point where the target cpu disables interrupts
  812. * in stop machine. I doubt that it can happen, but at least
  813. * there is a theroretical chance. Virtualization might be
  814. * able to expose this, but AFAICT the IOAPIC emulation is not
  815. * as stupid as the real hardware.
  816. *
  817. * Anyway, there is nothing we can do about that at this point
  818. * w/o refactoring the whole fixup_irq() business completely.
  819. * We print at least the irq number and the old vector number,
  820. * so we have the necessary information when a problem in that
  821. * area arises.
  822. */
  823. pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
  824. irqd->irq, vector);
  825. }
  826. free_moved_vector(apicd);
  827. unlock:
  828. raw_spin_unlock(&vector_lock);
  829. }
  830. #ifdef CONFIG_HOTPLUG_CPU
  831. /*
  832. * Note, this is not accurate accounting, but at least good enough to
  833. * prevent that the actual interrupt move will run out of vectors.
  834. */
  835. int lapic_can_unplug_cpu(void)
  836. {
  837. unsigned int rsvd, avl, tomove, cpu = smp_processor_id();
  838. int ret = 0;
  839. raw_spin_lock(&vector_lock);
  840. tomove = irq_matrix_allocated(vector_matrix);
  841. avl = irq_matrix_available(vector_matrix, true);
  842. if (avl < tomove) {
  843. pr_warn("CPU %u has %u vectors, %u available. Cannot disable CPU\n",
  844. cpu, tomove, avl);
  845. ret = -ENOSPC;
  846. goto out;
  847. }
  848. rsvd = irq_matrix_reserved(vector_matrix);
  849. if (avl < rsvd) {
  850. pr_warn("Reserved vectors %u > available %u. IRQ request may fail\n",
  851. rsvd, avl);
  852. }
  853. out:
  854. raw_spin_unlock(&vector_lock);
  855. return ret;
  856. }
  857. #endif /* HOTPLUG_CPU */
  858. #endif /* SMP */
  859. static void __init print_APIC_field(int base)
  860. {
  861. int i;
  862. printk(KERN_DEBUG);
  863. for (i = 0; i < 8; i++)
  864. pr_cont("%08x", apic_read(base + i*0x10));
  865. pr_cont("\n");
  866. }
  867. static void __init print_local_APIC(void *dummy)
  868. {
  869. unsigned int i, v, ver, maxlvt;
  870. u64 icr;
  871. pr_debug("printing local APIC contents on CPU#%d/%d:\n",
  872. smp_processor_id(), hard_smp_processor_id());
  873. v = apic_read(APIC_ID);
  874. pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
  875. v = apic_read(APIC_LVR);
  876. pr_info("... APIC VERSION: %08x\n", v);
  877. ver = GET_APIC_VERSION(v);
  878. maxlvt = lapic_get_maxlvt();
  879. v = apic_read(APIC_TASKPRI);
  880. pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  881. /* !82489DX */
  882. if (APIC_INTEGRATED(ver)) {
  883. if (!APIC_XAPIC(ver)) {
  884. v = apic_read(APIC_ARBPRI);
  885. pr_debug("... APIC ARBPRI: %08x (%02x)\n",
  886. v, v & APIC_ARBPRI_MASK);
  887. }
  888. v = apic_read(APIC_PROCPRI);
  889. pr_debug("... APIC PROCPRI: %08x\n", v);
  890. }
  891. /*
  892. * Remote read supported only in the 82489DX and local APIC for
  893. * Pentium processors.
  894. */
  895. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  896. v = apic_read(APIC_RRR);
  897. pr_debug("... APIC RRR: %08x\n", v);
  898. }
  899. v = apic_read(APIC_LDR);
  900. pr_debug("... APIC LDR: %08x\n", v);
  901. if (!x2apic_enabled()) {
  902. v = apic_read(APIC_DFR);
  903. pr_debug("... APIC DFR: %08x\n", v);
  904. }
  905. v = apic_read(APIC_SPIV);
  906. pr_debug("... APIC SPIV: %08x\n", v);
  907. pr_debug("... APIC ISR field:\n");
  908. print_APIC_field(APIC_ISR);
  909. pr_debug("... APIC TMR field:\n");
  910. print_APIC_field(APIC_TMR);
  911. pr_debug("... APIC IRR field:\n");
  912. print_APIC_field(APIC_IRR);
  913. /* !82489DX */
  914. if (APIC_INTEGRATED(ver)) {
  915. /* Due to the Pentium erratum 3AP. */
  916. if (maxlvt > 3)
  917. apic_write(APIC_ESR, 0);
  918. v = apic_read(APIC_ESR);
  919. pr_debug("... APIC ESR: %08x\n", v);
  920. }
  921. icr = apic_icr_read();
  922. pr_debug("... APIC ICR: %08x\n", (u32)icr);
  923. pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
  924. v = apic_read(APIC_LVTT);
  925. pr_debug("... APIC LVTT: %08x\n", v);
  926. if (maxlvt > 3) {
  927. /* PC is LVT#4. */
  928. v = apic_read(APIC_LVTPC);
  929. pr_debug("... APIC LVTPC: %08x\n", v);
  930. }
  931. v = apic_read(APIC_LVT0);
  932. pr_debug("... APIC LVT0: %08x\n", v);
  933. v = apic_read(APIC_LVT1);
  934. pr_debug("... APIC LVT1: %08x\n", v);
  935. if (maxlvt > 2) {
  936. /* ERR is LVT#3. */
  937. v = apic_read(APIC_LVTERR);
  938. pr_debug("... APIC LVTERR: %08x\n", v);
  939. }
  940. v = apic_read(APIC_TMICT);
  941. pr_debug("... APIC TMICT: %08x\n", v);
  942. v = apic_read(APIC_TMCCT);
  943. pr_debug("... APIC TMCCT: %08x\n", v);
  944. v = apic_read(APIC_TDCR);
  945. pr_debug("... APIC TDCR: %08x\n", v);
  946. if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
  947. v = apic_read(APIC_EFEAT);
  948. maxlvt = (v >> 16) & 0xff;
  949. pr_debug("... APIC EFEAT: %08x\n", v);
  950. v = apic_read(APIC_ECTRL);
  951. pr_debug("... APIC ECTRL: %08x\n", v);
  952. for (i = 0; i < maxlvt; i++) {
  953. v = apic_read(APIC_EILVTn(i));
  954. pr_debug("... APIC EILVT%d: %08x\n", i, v);
  955. }
  956. }
  957. pr_cont("\n");
  958. }
  959. static void __init print_local_APICs(int maxcpu)
  960. {
  961. int cpu;
  962. if (!maxcpu)
  963. return;
  964. preempt_disable();
  965. for_each_online_cpu(cpu) {
  966. if (cpu >= maxcpu)
  967. break;
  968. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  969. }
  970. preempt_enable();
  971. }
  972. static void __init print_PIC(void)
  973. {
  974. unsigned int v;
  975. unsigned long flags;
  976. if (!nr_legacy_irqs())
  977. return;
  978. pr_debug("\nprinting PIC contents\n");
  979. raw_spin_lock_irqsave(&i8259A_lock, flags);
  980. v = inb(0xa1) << 8 | inb(0x21);
  981. pr_debug("... PIC IMR: %04x\n", v);
  982. v = inb(0xa0) << 8 | inb(0x20);
  983. pr_debug("... PIC IRR: %04x\n", v);
  984. outb(0x0b, 0xa0);
  985. outb(0x0b, 0x20);
  986. v = inb(0xa0) << 8 | inb(0x20);
  987. outb(0x0a, 0xa0);
  988. outb(0x0a, 0x20);
  989. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  990. pr_debug("... PIC ISR: %04x\n", v);
  991. v = inb(0x4d1) << 8 | inb(0x4d0);
  992. pr_debug("... PIC ELCR: %04x\n", v);
  993. }
  994. static int show_lapic __initdata = 1;
  995. static __init int setup_show_lapic(char *arg)
  996. {
  997. int num = -1;
  998. if (strcmp(arg, "all") == 0) {
  999. show_lapic = CONFIG_NR_CPUS;
  1000. } else {
  1001. get_option(&arg, &num);
  1002. if (num >= 0)
  1003. show_lapic = num;
  1004. }
  1005. return 1;
  1006. }
  1007. __setup("show_lapic=", setup_show_lapic);
  1008. static int __init print_ICs(void)
  1009. {
  1010. if (apic_verbosity == APIC_QUIET)
  1011. return 0;
  1012. print_PIC();
  1013. /* don't print out if apic is not there */
  1014. if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
  1015. return 0;
  1016. print_local_APICs(show_lapic);
  1017. print_IO_APICs();
  1018. return 0;
  1019. }
  1020. late_initcall(print_ICs);