vector.c 32 KB

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