vector.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Local APIC related interfaces to support IOAPIC, MSI, etc.
  4. *
  5. * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
  6. * Moved from arch/x86/kernel/apic/io_apic.c.
  7. * Jiang Liu <jiang.liu@linux.intel.com>
  8. * Enable support of hierarchical irqdomains
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/init.h>
  14. #include <linux/compiler.h>
  15. #include <linux/slab.h>
  16. #include <asm/irqdomain.h>
  17. #include <asm/hw_irq.h>
  18. #include <asm/traps.h>
  19. #include <asm/apic.h>
  20. #include <asm/i8259.h>
  21. #include <asm/desc.h>
  22. #include <asm/irq_remapping.h>
  23. #include <asm/trace/irq_vectors.h>
  24. struct apic_chip_data {
  25. struct irq_cfg hw_irq_cfg;
  26. unsigned int vector;
  27. unsigned int prev_vector;
  28. unsigned int cpu;
  29. unsigned int prev_cpu;
  30. unsigned int irq;
  31. struct hlist_node clist;
  32. unsigned int move_in_progress : 1,
  33. is_managed : 1,
  34. can_reserve : 1,
  35. has_reserved : 1;
  36. };
  37. struct irq_domain *x86_vector_domain;
  38. EXPORT_SYMBOL_GPL(x86_vector_domain);
  39. static DEFINE_RAW_SPINLOCK(vector_lock);
  40. static cpumask_var_t vector_searchmask;
  41. static struct irq_chip lapic_controller;
  42. static struct irq_matrix *vector_matrix;
  43. #ifdef CONFIG_SMP
  44. static void vector_cleanup_callback(struct timer_list *tmr);
  45. struct vector_cleanup {
  46. struct hlist_head head;
  47. struct timer_list timer;
  48. };
  49. static DEFINE_PER_CPU(struct vector_cleanup, vector_cleanup) = {
  50. .head = HLIST_HEAD_INIT,
  51. .timer = __TIMER_INITIALIZER(vector_cleanup_callback, TIMER_PINNED),
  52. };
  53. #endif
  54. void lock_vector_lock(void)
  55. {
  56. /* Used to the online set of cpus does not change
  57. * during assign_irq_vector.
  58. */
  59. raw_spin_lock(&vector_lock);
  60. }
  61. void unlock_vector_lock(void)
  62. {
  63. raw_spin_unlock(&vector_lock);
  64. }
  65. void init_irq_alloc_info(struct irq_alloc_info *info,
  66. const struct cpumask *mask)
  67. {
  68. memset(info, 0, sizeof(*info));
  69. info->mask = mask;
  70. }
  71. void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
  72. {
  73. if (src)
  74. *dst = *src;
  75. else
  76. memset(dst, 0, sizeof(*dst));
  77. }
  78. static struct apic_chip_data *apic_chip_data(struct irq_data *irqd)
  79. {
  80. if (!irqd)
  81. return NULL;
  82. while (irqd->parent_data)
  83. irqd = irqd->parent_data;
  84. return irqd->chip_data;
  85. }
  86. struct irq_cfg *irqd_cfg(struct irq_data *irqd)
  87. {
  88. struct apic_chip_data *apicd = apic_chip_data(irqd);
  89. return apicd ? &apicd->hw_irq_cfg : NULL;
  90. }
  91. EXPORT_SYMBOL_GPL(irqd_cfg);
  92. struct irq_cfg *irq_cfg(unsigned int irq)
  93. {
  94. return irqd_cfg(irq_get_irq_data(irq));
  95. }
  96. static struct apic_chip_data *alloc_apic_chip_data(int node)
  97. {
  98. struct apic_chip_data *apicd;
  99. apicd = kzalloc_node(sizeof(*apicd), GFP_KERNEL, node);
  100. if (apicd)
  101. INIT_HLIST_NODE(&apicd->clist);
  102. return apicd;
  103. }
  104. static void free_apic_chip_data(struct apic_chip_data *apicd)
  105. {
  106. kfree(apicd);
  107. }
  108. static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector,
  109. unsigned int cpu)
  110. {
  111. struct apic_chip_data *apicd = apic_chip_data(irqd);
  112. lockdep_assert_held(&vector_lock);
  113. apicd->hw_irq_cfg.vector = vector;
  114. apicd->hw_irq_cfg.dest_apicid = apic->calc_dest_apicid(cpu);
  115. irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
  116. trace_vector_config(irqd->irq, vector, cpu,
  117. apicd->hw_irq_cfg.dest_apicid);
  118. }
  119. static void apic_update_vector(struct irq_data *irqd, unsigned int newvec,
  120. unsigned int newcpu)
  121. {
  122. struct apic_chip_data *apicd = apic_chip_data(irqd);
  123. struct irq_desc *desc = irq_data_to_desc(irqd);
  124. bool managed = irqd_affinity_is_managed(irqd);
  125. lockdep_assert_held(&vector_lock);
  126. trace_vector_update(irqd->irq, newvec, newcpu, apicd->vector,
  127. apicd->cpu);
  128. /*
  129. * If there is no vector associated or if the associated vector is
  130. * the shutdown vector, which is associated to make PCI/MSI
  131. * shutdown mode work, then there is nothing to release. Clear out
  132. * prev_vector for this and the offlined target case.
  133. */
  134. apicd->prev_vector = 0;
  135. if (!apicd->vector || apicd->vector == MANAGED_IRQ_SHUTDOWN_VECTOR)
  136. goto setnew;
  137. /*
  138. * If the target CPU of the previous vector is online, then mark
  139. * the vector as move in progress and store it for cleanup when the
  140. * first interrupt on the new vector arrives. If the target CPU is
  141. * offline then the regular release mechanism via the cleanup
  142. * vector is not possible and the vector can be immediately freed
  143. * in the underlying matrix allocator.
  144. */
  145. if (cpu_online(apicd->cpu)) {
  146. apicd->move_in_progress = true;
  147. apicd->prev_vector = apicd->vector;
  148. apicd->prev_cpu = apicd->cpu;
  149. WARN_ON_ONCE(apicd->cpu == newcpu);
  150. } else {
  151. irq_matrix_free(vector_matrix, apicd->cpu, apicd->vector,
  152. managed);
  153. }
  154. setnew:
  155. apicd->vector = newvec;
  156. apicd->cpu = newcpu;
  157. BUG_ON(!IS_ERR_OR_NULL(per_cpu(vector_irq, newcpu)[newvec]));
  158. per_cpu(vector_irq, newcpu)[newvec] = desc;
  159. }
  160. static void vector_assign_managed_shutdown(struct irq_data *irqd)
  161. {
  162. unsigned int cpu = cpumask_first(cpu_online_mask);
  163. apic_update_irq_cfg(irqd, MANAGED_IRQ_SHUTDOWN_VECTOR, cpu);
  164. }
  165. static int reserve_managed_vector(struct irq_data *irqd)
  166. {
  167. const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
  168. struct apic_chip_data *apicd = apic_chip_data(irqd);
  169. unsigned long flags;
  170. int ret;
  171. raw_spin_lock_irqsave(&vector_lock, flags);
  172. apicd->is_managed = true;
  173. ret = irq_matrix_reserve_managed(vector_matrix, affmsk);
  174. raw_spin_unlock_irqrestore(&vector_lock, flags);
  175. trace_vector_reserve_managed(irqd->irq, ret);
  176. return ret;
  177. }
  178. static void reserve_irq_vector_locked(struct irq_data *irqd)
  179. {
  180. struct apic_chip_data *apicd = apic_chip_data(irqd);
  181. irq_matrix_reserve(vector_matrix);
  182. apicd->can_reserve = true;
  183. apicd->has_reserved = true;
  184. irqd_set_can_reserve(irqd);
  185. trace_vector_reserve(irqd->irq, 0);
  186. vector_assign_managed_shutdown(irqd);
  187. }
  188. static int reserve_irq_vector(struct irq_data *irqd)
  189. {
  190. unsigned long flags;
  191. raw_spin_lock_irqsave(&vector_lock, flags);
  192. reserve_irq_vector_locked(irqd);
  193. raw_spin_unlock_irqrestore(&vector_lock, flags);
  194. return 0;
  195. }
  196. static int
  197. assign_vector_locked(struct irq_data *irqd, const struct cpumask *dest)
  198. {
  199. struct apic_chip_data *apicd = apic_chip_data(irqd);
  200. bool resvd = apicd->has_reserved;
  201. unsigned int cpu = apicd->cpu;
  202. int vector = apicd->vector;
  203. lockdep_assert_held(&vector_lock);
  204. /*
  205. * If the current target CPU is online and in the new requested
  206. * affinity mask, there is no point in moving the interrupt from
  207. * one CPU to another.
  208. */
  209. if (vector && cpu_online(cpu) && cpumask_test_cpu(cpu, dest))
  210. return 0;
  211. /*
  212. * Careful here. @apicd might either have move_in_progress set or
  213. * be enqueued for cleanup. Assigning a new vector would either
  214. * leave a stale vector on some CPU around or in case of a pending
  215. * cleanup corrupt the hlist.
  216. */
  217. if (apicd->move_in_progress || !hlist_unhashed(&apicd->clist))
  218. return -EBUSY;
  219. vector = irq_matrix_alloc(vector_matrix, dest, resvd, &cpu);
  220. trace_vector_alloc(irqd->irq, vector, resvd, vector);
  221. if (vector < 0)
  222. return vector;
  223. apic_update_vector(irqd, vector, cpu);
  224. apic_update_irq_cfg(irqd, vector, cpu);
  225. return 0;
  226. }
  227. static int assign_irq_vector(struct irq_data *irqd, const struct cpumask *dest)
  228. {
  229. unsigned long flags;
  230. int ret;
  231. raw_spin_lock_irqsave(&vector_lock, flags);
  232. cpumask_and(vector_searchmask, dest, cpu_online_mask);
  233. ret = assign_vector_locked(irqd, vector_searchmask);
  234. raw_spin_unlock_irqrestore(&vector_lock, flags);
  235. return ret;
  236. }
  237. static int assign_irq_vector_any_locked(struct irq_data *irqd)
  238. {
  239. /* Get the affinity mask - either irq_default_affinity or (user) set */
  240. const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
  241. int node = irq_data_get_node(irqd);
  242. if (node != NUMA_NO_NODE) {
  243. /* Try the intersection of @affmsk and node mask */
  244. cpumask_and(vector_searchmask, cpumask_of_node(node), affmsk);
  245. if (!assign_vector_locked(irqd, vector_searchmask))
  246. return 0;
  247. }
  248. /* Try the full affinity mask */
  249. cpumask_and(vector_searchmask, affmsk, cpu_online_mask);
  250. if (!assign_vector_locked(irqd, vector_searchmask))
  251. return 0;
  252. if (node != NUMA_NO_NODE) {
  253. /* Try the node mask */
  254. if (!assign_vector_locked(irqd, cpumask_of_node(node)))
  255. return 0;
  256. }
  257. /* Try the full online mask */
  258. return assign_vector_locked(irqd, cpu_online_mask);
  259. }
  260. static int
  261. assign_irq_vector_policy(struct irq_data *irqd, struct irq_alloc_info *info)
  262. {
  263. if (irqd_affinity_is_managed(irqd))
  264. return reserve_managed_vector(irqd);
  265. if (info->mask)
  266. return assign_irq_vector(irqd, info->mask);
  267. /*
  268. * Make only a global reservation with no guarantee. A real vector
  269. * is associated at activation time.
  270. */
  271. return reserve_irq_vector(irqd);
  272. }
  273. static int
  274. assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest)
  275. {
  276. const struct cpumask *affmsk = irq_data_get_affinity_mask(irqd);
  277. struct apic_chip_data *apicd = apic_chip_data(irqd);
  278. int vector, cpu;
  279. cpumask_and(vector_searchmask, dest, affmsk);
  280. /* set_affinity might call here for nothing */
  281. if (apicd->vector && cpumask_test_cpu(apicd->cpu, vector_searchmask))
  282. return 0;
  283. vector = irq_matrix_alloc_managed(vector_matrix, vector_searchmask,
  284. &cpu);
  285. trace_vector_alloc_managed(irqd->irq, vector, vector);
  286. if (vector < 0)
  287. return vector;
  288. apic_update_vector(irqd, vector, cpu);
  289. apic_update_irq_cfg(irqd, vector, cpu);
  290. return 0;
  291. }
  292. static void clear_irq_vector(struct irq_data *irqd)
  293. {
  294. struct apic_chip_data *apicd = apic_chip_data(irqd);
  295. bool managed = irqd_affinity_is_managed(irqd);
  296. unsigned int vector = apicd->vector;
  297. lockdep_assert_held(&vector_lock);
  298. if (!vector)
  299. return;
  300. trace_vector_clear(irqd->irq, vector, apicd->cpu, apicd->prev_vector,
  301. apicd->prev_cpu);
  302. per_cpu(vector_irq, apicd->cpu)[vector] = VECTOR_SHUTDOWN;
  303. irq_matrix_free(vector_matrix, apicd->cpu, vector, managed);
  304. apicd->vector = 0;
  305. /* Clean up move in progress */
  306. vector = apicd->prev_vector;
  307. if (!vector)
  308. return;
  309. per_cpu(vector_irq, apicd->prev_cpu)[vector] = VECTOR_SHUTDOWN;
  310. irq_matrix_free(vector_matrix, apicd->prev_cpu, vector, managed);
  311. apicd->prev_vector = 0;
  312. apicd->move_in_progress = 0;
  313. hlist_del_init(&apicd->clist);
  314. }
  315. static void x86_vector_deactivate(struct irq_domain *dom, struct irq_data *irqd)
  316. {
  317. struct apic_chip_data *apicd = apic_chip_data(irqd);
  318. unsigned long flags;
  319. trace_vector_deactivate(irqd->irq, apicd->is_managed,
  320. apicd->can_reserve, false);
  321. /* Regular fixed assigned interrupt */
  322. if (!apicd->is_managed && !apicd->can_reserve)
  323. return;
  324. /* If the interrupt has a global reservation, nothing to do */
  325. if (apicd->has_reserved)
  326. return;
  327. raw_spin_lock_irqsave(&vector_lock, flags);
  328. clear_irq_vector(irqd);
  329. if (apicd->can_reserve)
  330. reserve_irq_vector_locked(irqd);
  331. else
  332. vector_assign_managed_shutdown(irqd);
  333. raw_spin_unlock_irqrestore(&vector_lock, flags);
  334. }
  335. static int activate_reserved(struct irq_data *irqd)
  336. {
  337. struct apic_chip_data *apicd = apic_chip_data(irqd);
  338. int ret;
  339. ret = assign_irq_vector_any_locked(irqd);
  340. if (!ret) {
  341. apicd->has_reserved = false;
  342. /*
  343. * Core might have disabled reservation mode after
  344. * allocating the irq descriptor. Ideally this should
  345. * happen before allocation time, but that would require
  346. * completely convoluted ways of transporting that
  347. * information.
  348. */
  349. if (!irqd_can_reserve(irqd))
  350. apicd->can_reserve = false;
  351. }
  352. /*
  353. * Check to ensure that the effective affinity mask is a subset
  354. * the user supplied affinity mask, and warn the user if it is not
  355. */
  356. if (!cpumask_subset(irq_data_get_effective_affinity_mask(irqd),
  357. irq_data_get_affinity_mask(irqd))) {
  358. pr_warn("irq %u: Affinity broken due to vector space exhaustion.\n",
  359. irqd->irq);
  360. }
  361. return ret;
  362. }
  363. static int activate_managed(struct irq_data *irqd)
  364. {
  365. const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
  366. int ret;
  367. cpumask_and(vector_searchmask, dest, cpu_online_mask);
  368. if (WARN_ON_ONCE(cpumask_empty(vector_searchmask))) {
  369. /* Something in the core code broke! Survive gracefully */
  370. pr_err("Managed startup for irq %u, but no CPU\n", irqd->irq);
  371. return -EINVAL;
  372. }
  373. ret = assign_managed_vector(irqd, vector_searchmask);
  374. /*
  375. * This should not happen. The vector reservation got buggered. Handle
  376. * it gracefully.
  377. */
  378. if (WARN_ON_ONCE(ret < 0)) {
  379. pr_err("Managed startup irq %u, no vector available\n",
  380. irqd->irq);
  381. }
  382. return ret;
  383. }
  384. static int x86_vector_activate(struct irq_domain *dom, struct irq_data *irqd,
  385. bool reserve)
  386. {
  387. struct apic_chip_data *apicd = apic_chip_data(irqd);
  388. unsigned long flags;
  389. int ret = 0;
  390. trace_vector_activate(irqd->irq, apicd->is_managed,
  391. apicd->can_reserve, reserve);
  392. raw_spin_lock_irqsave(&vector_lock, flags);
  393. if (!apicd->can_reserve && !apicd->is_managed)
  394. assign_irq_vector_any_locked(irqd);
  395. else if (reserve || irqd_is_managed_and_shutdown(irqd))
  396. vector_assign_managed_shutdown(irqd);
  397. else if (apicd->is_managed)
  398. ret = activate_managed(irqd);
  399. else if (apicd->has_reserved)
  400. ret = activate_reserved(irqd);
  401. raw_spin_unlock_irqrestore(&vector_lock, flags);
  402. return ret;
  403. }
  404. static void vector_free_reserved_and_managed(struct irq_data *irqd)
  405. {
  406. const struct cpumask *dest = irq_data_get_affinity_mask(irqd);
  407. struct apic_chip_data *apicd = apic_chip_data(irqd);
  408. trace_vector_teardown(irqd->irq, apicd->is_managed,
  409. apicd->has_reserved);
  410. if (apicd->has_reserved)
  411. irq_matrix_remove_reserved(vector_matrix);
  412. if (apicd->is_managed)
  413. irq_matrix_remove_managed(vector_matrix, dest);
  414. }
  415. static void x86_vector_free_irqs(struct irq_domain *domain,
  416. unsigned int virq, unsigned int nr_irqs)
  417. {
  418. struct apic_chip_data *apicd;
  419. struct irq_data *irqd;
  420. unsigned long flags;
  421. int i;
  422. for (i = 0; i < nr_irqs; i++) {
  423. irqd = irq_domain_get_irq_data(x86_vector_domain, virq + i);
  424. if (irqd && irqd->chip_data) {
  425. raw_spin_lock_irqsave(&vector_lock, flags);
  426. clear_irq_vector(irqd);
  427. vector_free_reserved_and_managed(irqd);
  428. apicd = irqd->chip_data;
  429. irq_domain_reset_irq_data(irqd);
  430. raw_spin_unlock_irqrestore(&vector_lock, flags);
  431. free_apic_chip_data(apicd);
  432. }
  433. }
  434. }
  435. static bool vector_configure_legacy(unsigned int virq, struct irq_data *irqd,
  436. struct apic_chip_data *apicd)
  437. {
  438. unsigned long flags;
  439. bool realloc = false;
  440. apicd->vector = ISA_IRQ_VECTOR(virq);
  441. apicd->cpu = 0;
  442. raw_spin_lock_irqsave(&vector_lock, flags);
  443. /*
  444. * If the interrupt is activated, then it must stay at this vector
  445. * position. That's usually the timer interrupt (0).
  446. */
  447. if (irqd_is_activated(irqd)) {
  448. trace_vector_setup(virq, true, 0);
  449. apic_update_irq_cfg(irqd, apicd->vector, apicd->cpu);
  450. } else {
  451. /* Release the vector */
  452. apicd->can_reserve = true;
  453. irqd_set_can_reserve(irqd);
  454. clear_irq_vector(irqd);
  455. realloc = true;
  456. }
  457. raw_spin_unlock_irqrestore(&vector_lock, flags);
  458. return realloc;
  459. }
  460. static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
  461. unsigned int nr_irqs, void *arg)
  462. {
  463. struct irq_alloc_info *info = arg;
  464. struct apic_chip_data *apicd;
  465. struct irq_data *irqd;
  466. int i, err, node;
  467. if (apic_is_disabled)
  468. return -ENXIO;
  469. /*
  470. * Catch any attempt to touch the cascade interrupt on a PIC
  471. * equipped system.
  472. */
  473. if (WARN_ON_ONCE(info->flags & X86_IRQ_ALLOC_LEGACY &&
  474. virq == PIC_CASCADE_IR))
  475. return -EINVAL;
  476. for (i = 0; i < nr_irqs; i++) {
  477. irqd = irq_domain_get_irq_data(domain, virq + i);
  478. BUG_ON(!irqd);
  479. node = irq_data_get_node(irqd);
  480. WARN_ON_ONCE(irqd->chip_data);
  481. apicd = alloc_apic_chip_data(node);
  482. if (!apicd) {
  483. err = -ENOMEM;
  484. goto error;
  485. }
  486. apicd->irq = virq + i;
  487. irqd->chip = &lapic_controller;
  488. irqd->chip_data = apicd;
  489. irqd->hwirq = virq + i;
  490. irqd_set_single_target(irqd);
  491. /*
  492. * Prevent that any of these interrupts is invoked in
  493. * non interrupt context via e.g. generic_handle_irq()
  494. * as that can corrupt the affinity move state.
  495. */
  496. irqd_set_handle_enforce_irqctx(irqd);
  497. /* Don't invoke affinity setter on deactivated interrupts */
  498. irqd_set_affinity_on_activate(irqd);
  499. /*
  500. * Legacy vectors are already assigned when the IOAPIC
  501. * takes them over. They stay on the same vector. This is
  502. * required for check_timer() to work correctly as it might
  503. * switch back to legacy mode. Only update the hardware
  504. * config.
  505. */
  506. if (info->flags & X86_IRQ_ALLOC_LEGACY) {
  507. if (!vector_configure_legacy(virq + i, irqd, apicd))
  508. continue;
  509. }
  510. err = assign_irq_vector_policy(irqd, info);
  511. trace_vector_setup(virq + i, false, err);
  512. if (err) {
  513. irqd->chip_data = NULL;
  514. free_apic_chip_data(apicd);
  515. goto error;
  516. }
  517. }
  518. return 0;
  519. error:
  520. x86_vector_free_irqs(domain, virq, i);
  521. return err;
  522. }
  523. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  524. static void x86_vector_debug_show(struct seq_file *m, struct irq_domain *d,
  525. struct irq_data *irqd, int ind)
  526. {
  527. struct apic_chip_data apicd;
  528. unsigned long flags;
  529. int irq;
  530. if (!irqd) {
  531. irq_matrix_debug_show(m, vector_matrix, ind);
  532. return;
  533. }
  534. irq = irqd->irq;
  535. if (irq < nr_legacy_irqs() && !test_bit(irq, &io_apic_irqs)) {
  536. seq_printf(m, "%*sVector: %5d\n", ind, "", ISA_IRQ_VECTOR(irq));
  537. seq_printf(m, "%*sTarget: Legacy PIC all CPUs\n", ind, "");
  538. return;
  539. }
  540. if (!irqd->chip_data) {
  541. seq_printf(m, "%*sVector: Not assigned\n", ind, "");
  542. return;
  543. }
  544. raw_spin_lock_irqsave(&vector_lock, flags);
  545. memcpy(&apicd, irqd->chip_data, sizeof(apicd));
  546. raw_spin_unlock_irqrestore(&vector_lock, flags);
  547. seq_printf(m, "%*sVector: %5u\n", ind, "", apicd.vector);
  548. seq_printf(m, "%*sTarget: %5u\n", ind, "", apicd.cpu);
  549. if (apicd.prev_vector) {
  550. seq_printf(m, "%*sPrevious vector: %5u\n", ind, "", apicd.prev_vector);
  551. seq_printf(m, "%*sPrevious target: %5u\n", ind, "", apicd.prev_cpu);
  552. }
  553. seq_printf(m, "%*smove_in_progress: %u\n", ind, "", apicd.move_in_progress ? 1 : 0);
  554. seq_printf(m, "%*sis_managed: %u\n", ind, "", apicd.is_managed ? 1 : 0);
  555. seq_printf(m, "%*scan_reserve: %u\n", ind, "", apicd.can_reserve ? 1 : 0);
  556. seq_printf(m, "%*shas_reserved: %u\n", ind, "", apicd.has_reserved ? 1 : 0);
  557. seq_printf(m, "%*scleanup_pending: %u\n", ind, "", !hlist_unhashed(&apicd.clist));
  558. }
  559. #endif
  560. int x86_fwspec_is_ioapic(struct irq_fwspec *fwspec)
  561. {
  562. if (fwspec->param_count != 1)
  563. return 0;
  564. if (is_fwnode_irqchip(fwspec->fwnode)) {
  565. const char *fwname = fwnode_get_name(fwspec->fwnode);
  566. return fwname && !strncmp(fwname, "IO-APIC-", 8) &&
  567. simple_strtol(fwname+8, NULL, 10) == fwspec->param[0];
  568. }
  569. return to_of_node(fwspec->fwnode) &&
  570. of_device_is_compatible(to_of_node(fwspec->fwnode),
  571. "intel,ce4100-ioapic");
  572. }
  573. int x86_fwspec_is_hpet(struct irq_fwspec *fwspec)
  574. {
  575. if (fwspec->param_count != 1)
  576. return 0;
  577. if (is_fwnode_irqchip(fwspec->fwnode)) {
  578. const char *fwname = fwnode_get_name(fwspec->fwnode);
  579. return fwname && !strncmp(fwname, "HPET-MSI-", 9) &&
  580. simple_strtol(fwname+9, NULL, 10) == fwspec->param[0];
  581. }
  582. return 0;
  583. }
  584. static int x86_vector_select(struct irq_domain *d, struct irq_fwspec *fwspec,
  585. enum irq_domain_bus_token bus_token)
  586. {
  587. /*
  588. * HPET and I/OAPIC cannot be parented in the vector domain
  589. * if IRQ remapping is enabled. APIC IDs above 15 bits are
  590. * only permitted if IRQ remapping is enabled, so check that.
  591. */
  592. if (apic_id_valid(32768))
  593. return 0;
  594. return x86_fwspec_is_ioapic(fwspec) || x86_fwspec_is_hpet(fwspec);
  595. }
  596. static const struct irq_domain_ops x86_vector_domain_ops = {
  597. .select = x86_vector_select,
  598. .alloc = x86_vector_alloc_irqs,
  599. .free = x86_vector_free_irqs,
  600. .activate = x86_vector_activate,
  601. .deactivate = x86_vector_deactivate,
  602. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  603. .debug_show = x86_vector_debug_show,
  604. #endif
  605. };
  606. int __init arch_probe_nr_irqs(void)
  607. {
  608. int nr;
  609. if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
  610. nr_irqs = NR_VECTORS * nr_cpu_ids;
  611. nr = (gsi_top + nr_legacy_irqs()) + 8 * nr_cpu_ids;
  612. #if defined(CONFIG_PCI_MSI)
  613. /*
  614. * for MSI and HT dyn irq
  615. */
  616. if (gsi_top <= NR_IRQS_LEGACY)
  617. nr += 8 * nr_cpu_ids;
  618. else
  619. nr += gsi_top * 16;
  620. #endif
  621. if (nr < nr_irqs)
  622. nr_irqs = nr;
  623. /*
  624. * We don't know if PIC is present at this point so we need to do
  625. * probe() to get the right number of legacy IRQs.
  626. */
  627. return legacy_pic->probe();
  628. }
  629. void lapic_assign_legacy_vector(unsigned int irq, bool replace)
  630. {
  631. /*
  632. * Use assign system here so it won't get accounted as allocated
  633. * and movable in the cpu hotplug check and it prevents managed
  634. * irq reservation from touching it.
  635. */
  636. irq_matrix_assign_system(vector_matrix, ISA_IRQ_VECTOR(irq), replace);
  637. }
  638. void __init lapic_update_legacy_vectors(void)
  639. {
  640. unsigned int i;
  641. if (IS_ENABLED(CONFIG_X86_IO_APIC) && nr_ioapics > 0)
  642. return;
  643. /*
  644. * If the IO/APIC is disabled via config, kernel command line or
  645. * lack of enumeration then all legacy interrupts are routed
  646. * through the PIC. Make sure that they are marked as legacy
  647. * vectors. PIC_CASCADE_IRQ has already been marked in
  648. * lapic_assign_system_vectors().
  649. */
  650. for (i = 0; i < nr_legacy_irqs(); i++) {
  651. if (i != PIC_CASCADE_IR)
  652. lapic_assign_legacy_vector(i, true);
  653. }
  654. }
  655. void __init lapic_assign_system_vectors(void)
  656. {
  657. unsigned int i, vector;
  658. for_each_set_bit(vector, system_vectors, NR_VECTORS)
  659. irq_matrix_assign_system(vector_matrix, vector, false);
  660. if (nr_legacy_irqs() > 1)
  661. lapic_assign_legacy_vector(PIC_CASCADE_IR, false);
  662. /* System vectors are reserved, online it */
  663. irq_matrix_online(vector_matrix);
  664. /* Mark the preallocated legacy interrupts */
  665. for (i = 0; i < nr_legacy_irqs(); i++) {
  666. /*
  667. * Don't touch the cascade interrupt. It's unusable
  668. * on PIC equipped machines. See the large comment
  669. * in the IO/APIC code.
  670. */
  671. if (i != PIC_CASCADE_IR)
  672. irq_matrix_assign(vector_matrix, ISA_IRQ_VECTOR(i));
  673. }
  674. }
  675. int __init arch_early_irq_init(void)
  676. {
  677. struct fwnode_handle *fn;
  678. fn = irq_domain_alloc_named_fwnode("VECTOR");
  679. BUG_ON(!fn);
  680. x86_vector_domain = irq_domain_create_tree(fn, &x86_vector_domain_ops,
  681. NULL);
  682. BUG_ON(x86_vector_domain == NULL);
  683. irq_set_default_host(x86_vector_domain);
  684. BUG_ON(!alloc_cpumask_var(&vector_searchmask, GFP_KERNEL));
  685. /*
  686. * Allocate the vector matrix allocator data structure and limit the
  687. * search area.
  688. */
  689. vector_matrix = irq_alloc_matrix(NR_VECTORS, FIRST_EXTERNAL_VECTOR,
  690. FIRST_SYSTEM_VECTOR);
  691. BUG_ON(!vector_matrix);
  692. return arch_early_ioapic_init();
  693. }
  694. #ifdef CONFIG_SMP
  695. static struct irq_desc *__setup_vector_irq(int vector)
  696. {
  697. int isairq = vector - ISA_IRQ_VECTOR(0);
  698. /* Check whether the irq is in the legacy space */
  699. if (isairq < 0 || isairq >= nr_legacy_irqs())
  700. return VECTOR_UNUSED;
  701. /* Check whether the irq is handled by the IOAPIC */
  702. if (test_bit(isairq, &io_apic_irqs))
  703. return VECTOR_UNUSED;
  704. return irq_to_desc(isairq);
  705. }
  706. /* Online the local APIC infrastructure and initialize the vectors */
  707. void lapic_online(void)
  708. {
  709. unsigned int vector;
  710. lockdep_assert_held(&vector_lock);
  711. /* Online the vector matrix array for this CPU */
  712. irq_matrix_online(vector_matrix);
  713. /*
  714. * The interrupt affinity logic never targets interrupts to offline
  715. * CPUs. The exception are the legacy PIC interrupts. In general
  716. * they are only targeted to CPU0, but depending on the platform
  717. * they can be distributed to any online CPU in hardware. The
  718. * kernel has no influence on that. So all active legacy vectors
  719. * must be installed on all CPUs. All non legacy interrupts can be
  720. * cleared.
  721. */
  722. for (vector = 0; vector < NR_VECTORS; vector++)
  723. this_cpu_write(vector_irq[vector], __setup_vector_irq(vector));
  724. }
  725. static void __vector_cleanup(struct vector_cleanup *cl, bool check_irr);
  726. void lapic_offline(void)
  727. {
  728. struct vector_cleanup *cl = this_cpu_ptr(&vector_cleanup);
  729. lock_vector_lock();
  730. /* In case the vector cleanup timer has not expired */
  731. __vector_cleanup(cl, false);
  732. irq_matrix_offline(vector_matrix);
  733. WARN_ON_ONCE(try_to_del_timer_sync(&cl->timer) < 0);
  734. WARN_ON_ONCE(!hlist_empty(&cl->head));
  735. unlock_vector_lock();
  736. }
  737. static int apic_set_affinity(struct irq_data *irqd,
  738. const struct cpumask *dest, bool force)
  739. {
  740. int err;
  741. if (WARN_ON_ONCE(!irqd_is_activated(irqd)))
  742. return -EIO;
  743. raw_spin_lock(&vector_lock);
  744. cpumask_and(vector_searchmask, dest, cpu_online_mask);
  745. if (irqd_affinity_is_managed(irqd))
  746. err = assign_managed_vector(irqd, vector_searchmask);
  747. else
  748. err = assign_vector_locked(irqd, vector_searchmask);
  749. raw_spin_unlock(&vector_lock);
  750. return err ? err : IRQ_SET_MASK_OK;
  751. }
  752. #else
  753. # define apic_set_affinity NULL
  754. #endif
  755. static int apic_retrigger_irq(struct irq_data *irqd)
  756. {
  757. struct apic_chip_data *apicd = apic_chip_data(irqd);
  758. unsigned long flags;
  759. raw_spin_lock_irqsave(&vector_lock, flags);
  760. __apic_send_IPI(apicd->cpu, apicd->vector);
  761. raw_spin_unlock_irqrestore(&vector_lock, flags);
  762. return 1;
  763. }
  764. void apic_ack_irq(struct irq_data *irqd)
  765. {
  766. irq_move_irq(irqd);
  767. apic_eoi();
  768. }
  769. void apic_ack_edge(struct irq_data *irqd)
  770. {
  771. irq_complete_move(irqd_cfg(irqd));
  772. apic_ack_irq(irqd);
  773. }
  774. static void x86_vector_msi_compose_msg(struct irq_data *data,
  775. struct msi_msg *msg)
  776. {
  777. __irq_msi_compose_msg(irqd_cfg(data), msg, false);
  778. }
  779. static struct irq_chip lapic_controller = {
  780. .name = "APIC",
  781. .irq_ack = apic_ack_edge,
  782. .irq_set_affinity = apic_set_affinity,
  783. .irq_compose_msi_msg = x86_vector_msi_compose_msg,
  784. .irq_retrigger = apic_retrigger_irq,
  785. };
  786. #ifdef CONFIG_SMP
  787. static void free_moved_vector(struct apic_chip_data *apicd)
  788. {
  789. unsigned int vector = apicd->prev_vector;
  790. unsigned int cpu = apicd->prev_cpu;
  791. bool managed = apicd->is_managed;
  792. /*
  793. * Managed interrupts are usually not migrated away
  794. * from an online CPU, but CPU isolation 'managed_irq'
  795. * can make that happen.
  796. * 1) Activation does not take the isolation into account
  797. * to keep the code simple
  798. * 2) Migration away from an isolated CPU can happen when
  799. * a non-isolated CPU which is in the calculated
  800. * affinity mask comes online.
  801. */
  802. trace_vector_free_moved(apicd->irq, cpu, vector, managed);
  803. irq_matrix_free(vector_matrix, cpu, vector, managed);
  804. per_cpu(vector_irq, cpu)[vector] = VECTOR_UNUSED;
  805. hlist_del_init(&apicd->clist);
  806. apicd->prev_vector = 0;
  807. apicd->move_in_progress = 0;
  808. }
  809. static void __vector_cleanup(struct vector_cleanup *cl, bool check_irr)
  810. {
  811. struct apic_chip_data *apicd;
  812. struct hlist_node *tmp;
  813. bool rearm = false;
  814. lockdep_assert_held(&vector_lock);
  815. hlist_for_each_entry_safe(apicd, tmp, &cl->head, clist) {
  816. unsigned int vector = apicd->prev_vector;
  817. /*
  818. * Paranoia: Check if the vector that needs to be cleaned
  819. * up is registered at the APICs IRR. That's clearly a
  820. * hardware issue if the vector arrived on the old target
  821. * _after_ interrupts were disabled above. Keep @apicd
  822. * on the list and schedule the timer again to give the CPU
  823. * a chance to handle the pending interrupt.
  824. *
  825. * Do not check IRR when called from lapic_offline(), because
  826. * fixup_irqs() was just called to scan IRR for set bits and
  827. * forward them to new destination CPUs via IPIs.
  828. */
  829. if (check_irr && is_vector_pending(vector)) {
  830. pr_warn_once("Moved interrupt pending in old target APIC %u\n", apicd->irq);
  831. rearm = true;
  832. continue;
  833. }
  834. free_moved_vector(apicd);
  835. }
  836. /*
  837. * Must happen under vector_lock to make the timer_pending() check
  838. * in __vector_schedule_cleanup() race free against the rearm here.
  839. */
  840. if (rearm)
  841. mod_timer(&cl->timer, jiffies + 1);
  842. }
  843. static void vector_cleanup_callback(struct timer_list *tmr)
  844. {
  845. struct vector_cleanup *cl = container_of(tmr, typeof(*cl), timer);
  846. /* Prevent vectors vanishing under us */
  847. raw_spin_lock_irq(&vector_lock);
  848. __vector_cleanup(cl, true);
  849. raw_spin_unlock_irq(&vector_lock);
  850. }
  851. static void __vector_schedule_cleanup(struct apic_chip_data *apicd)
  852. {
  853. unsigned int cpu = apicd->prev_cpu;
  854. raw_spin_lock(&vector_lock);
  855. apicd->move_in_progress = 0;
  856. if (cpu_online(cpu)) {
  857. struct vector_cleanup *cl = per_cpu_ptr(&vector_cleanup, cpu);
  858. hlist_add_head(&apicd->clist, &cl->head);
  859. /*
  860. * The lockless timer_pending() check is safe here. If it
  861. * returns true, then the callback will observe this new
  862. * apic data in the hlist as everything is serialized by
  863. * vector lock.
  864. *
  865. * If it returns false then the timer is either not armed
  866. * or the other CPU executes the callback, which again
  867. * would be blocked on vector lock. Rearming it in the
  868. * latter case makes it fire for nothing.
  869. *
  870. * This is also safe against the callback rearming the timer
  871. * because that's serialized via vector lock too.
  872. */
  873. if (!timer_pending(&cl->timer)) {
  874. cl->timer.expires = jiffies + 1;
  875. add_timer_on(&cl->timer, cpu);
  876. }
  877. } else {
  878. pr_warn("IRQ %u schedule cleanup for offline CPU %u\n", apicd->irq, cpu);
  879. free_moved_vector(apicd);
  880. }
  881. raw_spin_unlock(&vector_lock);
  882. }
  883. void vector_schedule_cleanup(struct irq_cfg *cfg)
  884. {
  885. struct apic_chip_data *apicd;
  886. apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
  887. if (apicd->move_in_progress)
  888. __vector_schedule_cleanup(apicd);
  889. }
  890. void irq_complete_move(struct irq_cfg *cfg)
  891. {
  892. struct apic_chip_data *apicd;
  893. apicd = container_of(cfg, struct apic_chip_data, hw_irq_cfg);
  894. if (likely(!apicd->move_in_progress))
  895. return;
  896. /*
  897. * If the interrupt arrived on the new target CPU, cleanup the
  898. * vector on the old target CPU. A vector check is not required
  899. * because an interrupt can never move from one vector to another
  900. * on the same CPU.
  901. */
  902. if (apicd->cpu == smp_processor_id())
  903. __vector_schedule_cleanup(apicd);
  904. }
  905. /*
  906. * Called from fixup_irqs() with @desc->lock held and interrupts disabled.
  907. */
  908. void irq_force_complete_move(struct irq_desc *desc)
  909. {
  910. unsigned int cpu = smp_processor_id();
  911. struct apic_chip_data *apicd;
  912. struct irq_data *irqd;
  913. unsigned int vector;
  914. /*
  915. * The function is called for all descriptors regardless of which
  916. * irqdomain they belong to. For example if an IRQ is provided by
  917. * an irq_chip as part of a GPIO driver, the chip data for that
  918. * descriptor is specific to the irq_chip in question.
  919. *
  920. * Check first that the chip_data is what we expect
  921. * (apic_chip_data) before touching it any further.
  922. */
  923. irqd = irq_domain_get_irq_data(x86_vector_domain,
  924. irq_desc_get_irq(desc));
  925. if (!irqd)
  926. return;
  927. raw_spin_lock(&vector_lock);
  928. apicd = apic_chip_data(irqd);
  929. if (!apicd)
  930. goto unlock;
  931. /*
  932. * If prev_vector is empty or the descriptor is neither currently
  933. * nor previously on the outgoing CPU no action required.
  934. */
  935. vector = apicd->prev_vector;
  936. if (!vector || (apicd->cpu != cpu && apicd->prev_cpu != cpu))
  937. goto unlock;
  938. /*
  939. * This is tricky. If the cleanup of the old vector has not been
  940. * done yet, then the following setaffinity call will fail with
  941. * -EBUSY. This can leave the interrupt in a stale state.
  942. *
  943. * All CPUs are stuck in stop machine with interrupts disabled so
  944. * calling __irq_complete_move() would be completely pointless.
  945. *
  946. * 1) The interrupt is in move_in_progress state. That means that we
  947. * have not seen an interrupt since the io_apic was reprogrammed to
  948. * the new vector.
  949. *
  950. * 2) The interrupt has fired on the new vector, but the cleanup IPIs
  951. * have not been processed yet.
  952. */
  953. if (apicd->move_in_progress) {
  954. /*
  955. * In theory there is a race:
  956. *
  957. * set_ioapic(new_vector) <-- Interrupt is raised before update
  958. * is effective, i.e. it's raised on
  959. * the old vector.
  960. *
  961. * So if the target cpu cannot handle that interrupt before
  962. * the old vector is cleaned up, we get a spurious interrupt
  963. * and in the worst case the ioapic irq line becomes stale.
  964. *
  965. * But in case of cpu hotplug this should be a non issue
  966. * because if the affinity update happens right before all
  967. * cpus rendezvous in stop machine, there is no way that the
  968. * interrupt can be blocked on the target cpu because all cpus
  969. * loops first with interrupts enabled in stop machine, so the
  970. * old vector is not yet cleaned up when the interrupt fires.
  971. *
  972. * So the only way to run into this issue is if the delivery
  973. * of the interrupt on the apic/system bus would be delayed
  974. * beyond the point where the target cpu disables interrupts
  975. * in stop machine. I doubt that it can happen, but at least
  976. * there is a theoretical chance. Virtualization might be
  977. * able to expose this, but AFAICT the IOAPIC emulation is not
  978. * as stupid as the real hardware.
  979. *
  980. * Anyway, there is nothing we can do about that at this point
  981. * w/o refactoring the whole fixup_irq() business completely.
  982. * We print at least the irq number and the old vector number,
  983. * so we have the necessary information when a problem in that
  984. * area arises.
  985. */
  986. pr_warn("IRQ fixup: irq %d move in progress, old vector %d\n",
  987. irqd->irq, vector);
  988. }
  989. free_moved_vector(apicd);
  990. unlock:
  991. raw_spin_unlock(&vector_lock);
  992. }
  993. #ifdef CONFIG_HOTPLUG_CPU
  994. /*
  995. * Note, this is not accurate accounting, but at least good enough to
  996. * prevent that the actual interrupt move will run out of vectors.
  997. */
  998. int lapic_can_unplug_cpu(void)
  999. {
  1000. unsigned int rsvd, avl, tomove, cpu = smp_processor_id();
  1001. int ret = 0;
  1002. raw_spin_lock(&vector_lock);
  1003. tomove = irq_matrix_allocated(vector_matrix);
  1004. avl = irq_matrix_available(vector_matrix, true);
  1005. if (avl < tomove) {
  1006. pr_warn("CPU %u has %u vectors, %u available. Cannot disable CPU\n",
  1007. cpu, tomove, avl);
  1008. ret = -ENOSPC;
  1009. goto out;
  1010. }
  1011. rsvd = irq_matrix_reserved(vector_matrix);
  1012. if (avl < rsvd) {
  1013. pr_warn("Reserved vectors %u > available %u. IRQ request may fail\n",
  1014. rsvd, avl);
  1015. }
  1016. out:
  1017. raw_spin_unlock(&vector_lock);
  1018. return ret;
  1019. }
  1020. #endif /* HOTPLUG_CPU */
  1021. #endif /* SMP */
  1022. static void __init print_APIC_field(int base)
  1023. {
  1024. int i;
  1025. printk(KERN_DEBUG);
  1026. for (i = 0; i < 8; i++)
  1027. pr_cont("%08x", apic_read(base + i*0x10));
  1028. pr_cont("\n");
  1029. }
  1030. static void __init print_local_APIC(void *dummy)
  1031. {
  1032. unsigned int i, v, ver, maxlvt;
  1033. u64 icr;
  1034. pr_debug("printing local APIC contents on CPU#%d/%d:\n",
  1035. smp_processor_id(), read_apic_id());
  1036. v = apic_read(APIC_ID);
  1037. pr_info("... APIC ID: %08x (%01x)\n", v, read_apic_id());
  1038. v = apic_read(APIC_LVR);
  1039. pr_info("... APIC VERSION: %08x\n", v);
  1040. ver = GET_APIC_VERSION(v);
  1041. maxlvt = lapic_get_maxlvt();
  1042. v = apic_read(APIC_TASKPRI);
  1043. pr_debug("... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
  1044. /* !82489DX */
  1045. if (APIC_INTEGRATED(ver)) {
  1046. if (!APIC_XAPIC(ver)) {
  1047. v = apic_read(APIC_ARBPRI);
  1048. pr_debug("... APIC ARBPRI: %08x (%02x)\n",
  1049. v, v & APIC_ARBPRI_MASK);
  1050. }
  1051. v = apic_read(APIC_PROCPRI);
  1052. pr_debug("... APIC PROCPRI: %08x\n", v);
  1053. }
  1054. /*
  1055. * Remote read supported only in the 82489DX and local APIC for
  1056. * Pentium processors.
  1057. */
  1058. if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
  1059. v = apic_read(APIC_RRR);
  1060. pr_debug("... APIC RRR: %08x\n", v);
  1061. }
  1062. v = apic_read(APIC_LDR);
  1063. pr_debug("... APIC LDR: %08x\n", v);
  1064. if (!x2apic_enabled()) {
  1065. v = apic_read(APIC_DFR);
  1066. pr_debug("... APIC DFR: %08x\n", v);
  1067. }
  1068. v = apic_read(APIC_SPIV);
  1069. pr_debug("... APIC SPIV: %08x\n", v);
  1070. pr_debug("... APIC ISR field:\n");
  1071. print_APIC_field(APIC_ISR);
  1072. pr_debug("... APIC TMR field:\n");
  1073. print_APIC_field(APIC_TMR);
  1074. pr_debug("... APIC IRR field:\n");
  1075. print_APIC_field(APIC_IRR);
  1076. /* !82489DX */
  1077. if (APIC_INTEGRATED(ver)) {
  1078. /* Due to the Pentium erratum 3AP. */
  1079. if (maxlvt > 3)
  1080. apic_write(APIC_ESR, 0);
  1081. v = apic_read(APIC_ESR);
  1082. pr_debug("... APIC ESR: %08x\n", v);
  1083. }
  1084. icr = apic_icr_read();
  1085. pr_debug("... APIC ICR: %08x\n", (u32)icr);
  1086. pr_debug("... APIC ICR2: %08x\n", (u32)(icr >> 32));
  1087. v = apic_read(APIC_LVTT);
  1088. pr_debug("... APIC LVTT: %08x\n", v);
  1089. if (maxlvt > 3) {
  1090. /* PC is LVT#4. */
  1091. v = apic_read(APIC_LVTPC);
  1092. pr_debug("... APIC LVTPC: %08x\n", v);
  1093. }
  1094. v = apic_read(APIC_LVT0);
  1095. pr_debug("... APIC LVT0: %08x\n", v);
  1096. v = apic_read(APIC_LVT1);
  1097. pr_debug("... APIC LVT1: %08x\n", v);
  1098. if (maxlvt > 2) {
  1099. /* ERR is LVT#3. */
  1100. v = apic_read(APIC_LVTERR);
  1101. pr_debug("... APIC LVTERR: %08x\n", v);
  1102. }
  1103. v = apic_read(APIC_TMICT);
  1104. pr_debug("... APIC TMICT: %08x\n", v);
  1105. v = apic_read(APIC_TMCCT);
  1106. pr_debug("... APIC TMCCT: %08x\n", v);
  1107. v = apic_read(APIC_TDCR);
  1108. pr_debug("... APIC TDCR: %08x\n", v);
  1109. if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
  1110. v = apic_read(APIC_EFEAT);
  1111. maxlvt = (v >> 16) & 0xff;
  1112. pr_debug("... APIC EFEAT: %08x\n", v);
  1113. v = apic_read(APIC_ECTRL);
  1114. pr_debug("... APIC ECTRL: %08x\n", v);
  1115. for (i = 0; i < maxlvt; i++) {
  1116. v = apic_read(APIC_EILVTn(i));
  1117. pr_debug("... APIC EILVT%d: %08x\n", i, v);
  1118. }
  1119. }
  1120. pr_cont("\n");
  1121. }
  1122. static void __init print_local_APICs(int maxcpu)
  1123. {
  1124. int cpu;
  1125. if (!maxcpu)
  1126. return;
  1127. preempt_disable();
  1128. for_each_online_cpu(cpu) {
  1129. if (cpu >= maxcpu)
  1130. break;
  1131. smp_call_function_single(cpu, print_local_APIC, NULL, 1);
  1132. }
  1133. preempt_enable();
  1134. }
  1135. static void __init print_PIC(void)
  1136. {
  1137. unsigned int v;
  1138. unsigned long flags;
  1139. if (!nr_legacy_irqs())
  1140. return;
  1141. pr_debug("\nprinting PIC contents\n");
  1142. raw_spin_lock_irqsave(&i8259A_lock, flags);
  1143. v = inb(0xa1) << 8 | inb(0x21);
  1144. pr_debug("... PIC IMR: %04x\n", v);
  1145. v = inb(0xa0) << 8 | inb(0x20);
  1146. pr_debug("... PIC IRR: %04x\n", v);
  1147. outb(0x0b, 0xa0);
  1148. outb(0x0b, 0x20);
  1149. v = inb(0xa0) << 8 | inb(0x20);
  1150. outb(0x0a, 0xa0);
  1151. outb(0x0a, 0x20);
  1152. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  1153. pr_debug("... PIC ISR: %04x\n", v);
  1154. v = inb(PIC_ELCR2) << 8 | inb(PIC_ELCR1);
  1155. pr_debug("... PIC ELCR: %04x\n", v);
  1156. }
  1157. static int show_lapic __initdata = 1;
  1158. static __init int setup_show_lapic(char *arg)
  1159. {
  1160. int num = -1;
  1161. if (strcmp(arg, "all") == 0) {
  1162. show_lapic = CONFIG_NR_CPUS;
  1163. } else {
  1164. get_option(&arg, &num);
  1165. if (num >= 0)
  1166. show_lapic = num;
  1167. }
  1168. return 1;
  1169. }
  1170. __setup("show_lapic=", setup_show_lapic);
  1171. static int __init print_ICs(void)
  1172. {
  1173. if (apic_verbosity == APIC_QUIET)
  1174. return 0;
  1175. print_PIC();
  1176. /* don't print out if apic is not there */
  1177. if (!boot_cpu_has(X86_FEATURE_APIC) && !apic_from_smp_config())
  1178. return 0;
  1179. print_local_APICs(show_lapic);
  1180. print_IO_APICs();
  1181. return 0;
  1182. }
  1183. late_initcall(print_ICs);