vector.c 35 KB

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