vector.c 32 KB

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