vector.c 31 KB

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