apic.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #ifndef _ASM_X86_APIC_H
  2. #define _ASM_X86_APIC_H
  3. #include <linux/cpumask.h>
  4. #include <linux/pm.h>
  5. #include <asm/alternative.h>
  6. #include <asm/cpufeature.h>
  7. #include <asm/processor.h>
  8. #include <asm/apicdef.h>
  9. #include <linux/atomic.h>
  10. #include <asm/fixmap.h>
  11. #include <asm/mpspec.h>
  12. #include <asm/msr.h>
  13. #include <asm/idle.h>
  14. #define ARCH_APICTIMER_STOPS_ON_C3 1
  15. /*
  16. * Debugging macros
  17. */
  18. #define APIC_QUIET 0
  19. #define APIC_VERBOSE 1
  20. #define APIC_DEBUG 2
  21. /*
  22. * Define the default level of output to be very little
  23. * This can be turned up by using apic=verbose for more
  24. * information and apic=debug for _lots_ of information.
  25. * apic_verbosity is defined in apic.c
  26. */
  27. #define apic_printk(v, s, a...) do { \
  28. if ((v) <= apic_verbosity) \
  29. printk(s, ##a); \
  30. } while (0)
  31. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32)
  32. extern void generic_apic_probe(void);
  33. #else
  34. static inline void generic_apic_probe(void)
  35. {
  36. }
  37. #endif
  38. #ifdef CONFIG_X86_LOCAL_APIC
  39. extern unsigned int apic_verbosity;
  40. extern int local_apic_timer_c2_ok;
  41. extern int disable_apic;
  42. extern unsigned int lapic_timer_frequency;
  43. #ifdef CONFIG_SMP
  44. extern void __inquire_remote_apic(int apicid);
  45. #else /* CONFIG_SMP */
  46. static inline void __inquire_remote_apic(int apicid)
  47. {
  48. }
  49. #endif /* CONFIG_SMP */
  50. static inline void default_inquire_remote_apic(int apicid)
  51. {
  52. if (apic_verbosity >= APIC_DEBUG)
  53. __inquire_remote_apic(apicid);
  54. }
  55. /*
  56. * With 82489DX we can't rely on apic feature bit
  57. * retrieved via cpuid but still have to deal with
  58. * such an apic chip so we assume that SMP configuration
  59. * is found from MP table (64bit case uses ACPI mostly
  60. * which set smp presence flag as well so we are safe
  61. * to use this helper too).
  62. */
  63. static inline bool apic_from_smp_config(void)
  64. {
  65. return smp_found_config && !disable_apic;
  66. }
  67. /*
  68. * Basic functions accessing APICs.
  69. */
  70. #ifdef CONFIG_PARAVIRT
  71. #include <asm/paravirt.h>
  72. #endif
  73. extern int setup_profiling_timer(unsigned int);
  74. static inline void native_apic_mem_write(u32 reg, u32 v)
  75. {
  76. volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg);
  77. alternative_io("movl %0, %P1", "xchgl %0, %P1", X86_BUG_11AP,
  78. ASM_OUTPUT2("=r" (v), "=m" (*addr)),
  79. ASM_OUTPUT2("0" (v), "m" (*addr)));
  80. }
  81. static inline u32 native_apic_mem_read(u32 reg)
  82. {
  83. return *((volatile u32 *)(APIC_BASE + reg));
  84. }
  85. extern void native_apic_wait_icr_idle(void);
  86. extern u32 native_safe_apic_wait_icr_idle(void);
  87. extern void native_apic_icr_write(u32 low, u32 id);
  88. extern u64 native_apic_icr_read(void);
  89. static inline bool apic_is_x2apic_enabled(void)
  90. {
  91. u64 msr;
  92. if (rdmsrl_safe(MSR_IA32_APICBASE, &msr))
  93. return false;
  94. return msr & X2APIC_ENABLE;
  95. }
  96. extern void enable_IR_x2apic(void);
  97. extern int get_physical_broadcast(void);
  98. extern int lapic_get_maxlvt(void);
  99. extern void clear_local_APIC(void);
  100. extern void disconnect_bsp_APIC(int virt_wire_setup);
  101. extern void disable_local_APIC(void);
  102. extern void lapic_shutdown(void);
  103. extern void sync_Arb_IDs(void);
  104. extern void init_bsp_APIC(void);
  105. extern void setup_local_APIC(void);
  106. extern void init_apic_mappings(void);
  107. void register_lapic_address(unsigned long address);
  108. extern void setup_boot_APIC_clock(void);
  109. extern void setup_secondary_APIC_clock(void);
  110. extern int APIC_init_uniprocessor(void);
  111. #ifdef CONFIG_X86_64
  112. static inline int apic_force_enable(unsigned long addr)
  113. {
  114. return -1;
  115. }
  116. #else
  117. extern int apic_force_enable(unsigned long addr);
  118. #endif
  119. extern int apic_bsp_setup(bool upmode);
  120. extern void apic_ap_setup(void);
  121. /*
  122. * On 32bit this is mach-xxx local
  123. */
  124. #ifdef CONFIG_X86_64
  125. extern int apic_is_clustered_box(void);
  126. #else
  127. static inline int apic_is_clustered_box(void)
  128. {
  129. return 0;
  130. }
  131. #endif
  132. extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
  133. #else /* !CONFIG_X86_LOCAL_APIC */
  134. static inline void lapic_shutdown(void) { }
  135. #define local_apic_timer_c2_ok 1
  136. static inline void init_apic_mappings(void) { }
  137. static inline void disable_local_APIC(void) { }
  138. # define setup_boot_APIC_clock x86_init_noop
  139. # define setup_secondary_APIC_clock x86_init_noop
  140. #endif /* !CONFIG_X86_LOCAL_APIC */
  141. #ifdef CONFIG_X86_X2APIC
  142. /*
  143. * Make previous memory operations globally visible before
  144. * sending the IPI through x2apic wrmsr. We need a serializing instruction or
  145. * mfence for this.
  146. */
  147. static inline void x2apic_wrmsr_fence(void)
  148. {
  149. asm volatile("mfence" : : : "memory");
  150. }
  151. static inline void native_apic_msr_write(u32 reg, u32 v)
  152. {
  153. if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
  154. reg == APIC_LVR)
  155. return;
  156. wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
  157. }
  158. static inline void native_apic_msr_eoi_write(u32 reg, u32 v)
  159. {
  160. wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0);
  161. }
  162. static inline u32 native_apic_msr_read(u32 reg)
  163. {
  164. u64 msr;
  165. if (reg == APIC_DFR)
  166. return -1;
  167. rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
  168. return (u32)msr;
  169. }
  170. static inline void native_x2apic_wait_icr_idle(void)
  171. {
  172. /* no need to wait for icr idle in x2apic */
  173. return;
  174. }
  175. static inline u32 native_safe_x2apic_wait_icr_idle(void)
  176. {
  177. /* no need to wait for icr idle in x2apic */
  178. return 0;
  179. }
  180. static inline void native_x2apic_icr_write(u32 low, u32 id)
  181. {
  182. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  183. }
  184. static inline u64 native_x2apic_icr_read(void)
  185. {
  186. unsigned long val;
  187. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  188. return val;
  189. }
  190. extern int x2apic_mode;
  191. extern int x2apic_phys;
  192. extern void __init check_x2apic(void);
  193. extern void x2apic_setup(void);
  194. static inline int x2apic_enabled(void)
  195. {
  196. return cpu_has_x2apic && apic_is_x2apic_enabled();
  197. }
  198. #define x2apic_supported() (cpu_has_x2apic)
  199. #else /* !CONFIG_X86_X2APIC */
  200. static inline void check_x2apic(void) { }
  201. static inline void x2apic_setup(void) { }
  202. static inline int x2apic_enabled(void) { return 0; }
  203. #define x2apic_mode (0)
  204. #define x2apic_supported() (0)
  205. #endif /* !CONFIG_X86_X2APIC */
  206. #ifdef CONFIG_X86_64
  207. #define SET_APIC_ID(x) (apic->set_apic_id(x))
  208. #else
  209. #endif
  210. /*
  211. * Copyright 2004 James Cleverdon, IBM.
  212. * Subject to the GNU Public License, v.2
  213. *
  214. * Generic APIC sub-arch data struct.
  215. *
  216. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  217. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  218. * James Cleverdon.
  219. */
  220. struct apic {
  221. char *name;
  222. int (*probe)(void);
  223. int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
  224. int (*apic_id_valid)(int apicid);
  225. int (*apic_id_registered)(void);
  226. u32 irq_delivery_mode;
  227. u32 irq_dest_mode;
  228. const struct cpumask *(*target_cpus)(void);
  229. int disable_esr;
  230. int dest_logical;
  231. unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid);
  232. void (*vector_allocation_domain)(int cpu, struct cpumask *retmask,
  233. const struct cpumask *mask);
  234. void (*init_apic_ldr)(void);
  235. void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
  236. void (*setup_apic_routing)(void);
  237. int (*cpu_present_to_apicid)(int mps_cpu);
  238. void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
  239. int (*check_phys_apicid_present)(int phys_apicid);
  240. int (*phys_pkg_id)(int cpuid_apic, int index_msb);
  241. unsigned int (*get_apic_id)(unsigned long x);
  242. unsigned long (*set_apic_id)(unsigned int id);
  243. unsigned long apic_id_mask;
  244. int (*cpu_mask_to_apicid_and)(const struct cpumask *cpumask,
  245. const struct cpumask *andmask,
  246. unsigned int *apicid);
  247. /* ipi */
  248. void (*send_IPI_mask)(const struct cpumask *mask, int vector);
  249. void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
  250. int vector);
  251. void (*send_IPI_allbutself)(int vector);
  252. void (*send_IPI_all)(int vector);
  253. void (*send_IPI_self)(int vector);
  254. /* wakeup_secondary_cpu */
  255. int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
  256. void (*inquire_remote_apic)(int apicid);
  257. /* apic ops */
  258. u32 (*read)(u32 reg);
  259. void (*write)(u32 reg, u32 v);
  260. /*
  261. * ->eoi_write() has the same signature as ->write().
  262. *
  263. * Drivers can support both ->eoi_write() and ->write() by passing the same
  264. * callback value. Kernel can override ->eoi_write() and fall back
  265. * on write for EOI.
  266. */
  267. void (*eoi_write)(u32 reg, u32 v);
  268. u64 (*icr_read)(void);
  269. void (*icr_write)(u32 low, u32 high);
  270. void (*wait_icr_idle)(void);
  271. u32 (*safe_wait_icr_idle)(void);
  272. #ifdef CONFIG_X86_32
  273. /*
  274. * Called very early during boot from get_smp_config(). It should
  275. * return the logical apicid. x86_[bios]_cpu_to_apicid is
  276. * initialized before this function is called.
  277. *
  278. * If logical apicid can't be determined that early, the function
  279. * may return BAD_APICID. Logical apicid will be configured after
  280. * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity
  281. * won't be applied properly during early boot in this case.
  282. */
  283. int (*x86_32_early_logical_apicid)(int cpu);
  284. #endif
  285. };
  286. /*
  287. * Pointer to the local APIC driver in use on this system (there's
  288. * always just one such driver in use - the kernel decides via an
  289. * early probing process which one it picks - and then sticks to it):
  290. */
  291. extern struct apic *apic;
  292. /*
  293. * APIC drivers are probed based on how they are listed in the .apicdrivers
  294. * section. So the order is important and enforced by the ordering
  295. * of different apic driver files in the Makefile.
  296. *
  297. * For the files having two apic drivers, we use apic_drivers()
  298. * to enforce the order with in them.
  299. */
  300. #define apic_driver(sym) \
  301. static const struct apic *__apicdrivers_##sym __used \
  302. __aligned(sizeof(struct apic *)) \
  303. __section(.apicdrivers) = { &sym }
  304. #define apic_drivers(sym1, sym2) \
  305. static struct apic *__apicdrivers_##sym1##sym2[2] __used \
  306. __aligned(sizeof(struct apic *)) \
  307. __section(.apicdrivers) = { &sym1, &sym2 }
  308. extern struct apic *__apicdrivers[], *__apicdrivers_end[];
  309. /*
  310. * APIC functionality to boot other CPUs - only used on SMP:
  311. */
  312. #ifdef CONFIG_SMP
  313. extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
  314. #endif
  315. #ifdef CONFIG_X86_LOCAL_APIC
  316. static inline u32 apic_read(u32 reg)
  317. {
  318. return apic->read(reg);
  319. }
  320. static inline void apic_write(u32 reg, u32 val)
  321. {
  322. apic->write(reg, val);
  323. }
  324. static inline void apic_eoi(void)
  325. {
  326. apic->eoi_write(APIC_EOI, APIC_EOI_ACK);
  327. }
  328. static inline u64 apic_icr_read(void)
  329. {
  330. return apic->icr_read();
  331. }
  332. static inline void apic_icr_write(u32 low, u32 high)
  333. {
  334. apic->icr_write(low, high);
  335. }
  336. static inline void apic_wait_icr_idle(void)
  337. {
  338. apic->wait_icr_idle();
  339. }
  340. static inline u32 safe_apic_wait_icr_idle(void)
  341. {
  342. return apic->safe_wait_icr_idle();
  343. }
  344. extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v));
  345. #else /* CONFIG_X86_LOCAL_APIC */
  346. static inline u32 apic_read(u32 reg) { return 0; }
  347. static inline void apic_write(u32 reg, u32 val) { }
  348. static inline void apic_eoi(void) { }
  349. static inline u64 apic_icr_read(void) { return 0; }
  350. static inline void apic_icr_write(u32 low, u32 high) { }
  351. static inline void apic_wait_icr_idle(void) { }
  352. static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
  353. static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {}
  354. #endif /* CONFIG_X86_LOCAL_APIC */
  355. static inline void ack_APIC_irq(void)
  356. {
  357. /*
  358. * ack_APIC_irq() actually gets compiled as a single instruction
  359. * ... yummie.
  360. */
  361. apic_eoi();
  362. }
  363. static inline unsigned default_get_apic_id(unsigned long x)
  364. {
  365. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  366. if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
  367. return (x >> 24) & 0xFF;
  368. else
  369. return (x >> 24) & 0x0F;
  370. }
  371. /*
  372. * Warm reset vector position:
  373. */
  374. #define TRAMPOLINE_PHYS_LOW 0x467
  375. #define TRAMPOLINE_PHYS_HIGH 0x469
  376. #ifdef CONFIG_X86_64
  377. extern void apic_send_IPI_self(int vector);
  378. DECLARE_PER_CPU(int, x2apic_extra_bits);
  379. extern int default_cpu_present_to_apicid(int mps_cpu);
  380. extern int default_check_phys_apicid_present(int phys_apicid);
  381. #endif
  382. extern void generic_bigsmp_probe(void);
  383. #ifdef CONFIG_X86_LOCAL_APIC
  384. #include <asm/smp.h>
  385. #define APIC_DFR_VALUE (APIC_DFR_FLAT)
  386. static inline const struct cpumask *default_target_cpus(void)
  387. {
  388. #ifdef CONFIG_SMP
  389. return cpu_online_mask;
  390. #else
  391. return cpumask_of(0);
  392. #endif
  393. }
  394. static inline const struct cpumask *online_target_cpus(void)
  395. {
  396. return cpu_online_mask;
  397. }
  398. DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid);
  399. static inline unsigned int read_apic_id(void)
  400. {
  401. unsigned int reg;
  402. reg = apic_read(APIC_ID);
  403. return apic->get_apic_id(reg);
  404. }
  405. static inline int default_apic_id_valid(int apicid)
  406. {
  407. return (apicid < 255);
  408. }
  409. extern int default_acpi_madt_oem_check(char *, char *);
  410. extern void default_setup_apic_routing(void);
  411. extern struct apic apic_noop;
  412. #ifdef CONFIG_X86_32
  413. static inline int noop_x86_32_early_logical_apicid(int cpu)
  414. {
  415. return BAD_APICID;
  416. }
  417. /*
  418. * Set up the logical destination ID.
  419. *
  420. * Intel recommends to set DFR, LDR and TPR before enabling
  421. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  422. * document number 292116). So here it goes...
  423. */
  424. extern void default_init_apic_ldr(void);
  425. static inline int default_apic_id_registered(void)
  426. {
  427. return physid_isset(read_apic_id(), phys_cpu_present_map);
  428. }
  429. static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
  430. {
  431. return cpuid_apic >> index_msb;
  432. }
  433. #endif
  434. static inline int
  435. flat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  436. const struct cpumask *andmask,
  437. unsigned int *apicid)
  438. {
  439. unsigned long cpu_mask = cpumask_bits(cpumask)[0] &
  440. cpumask_bits(andmask)[0] &
  441. cpumask_bits(cpu_online_mask)[0] &
  442. APIC_ALL_CPUS;
  443. if (likely(cpu_mask)) {
  444. *apicid = (unsigned int)cpu_mask;
  445. return 0;
  446. } else {
  447. return -EINVAL;
  448. }
  449. }
  450. extern int
  451. default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  452. const struct cpumask *andmask,
  453. unsigned int *apicid);
  454. static inline void
  455. flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
  456. const struct cpumask *mask)
  457. {
  458. /* Careful. Some cpus do not strictly honor the set of cpus
  459. * specified in the interrupt destination when using lowest
  460. * priority interrupt delivery mode.
  461. *
  462. * In particular there was a hyperthreading cpu observed to
  463. * deliver interrupts to the wrong hyperthread when only one
  464. * hyperthread was specified in the interrupt desitination.
  465. */
  466. cpumask_clear(retmask);
  467. cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
  468. }
  469. static inline void
  470. default_vector_allocation_domain(int cpu, struct cpumask *retmask,
  471. const struct cpumask *mask)
  472. {
  473. cpumask_copy(retmask, cpumask_of(cpu));
  474. }
  475. static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid)
  476. {
  477. return physid_isset(apicid, *map);
  478. }
  479. static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
  480. {
  481. *retmap = *phys_map;
  482. }
  483. static inline int __default_cpu_present_to_apicid(int mps_cpu)
  484. {
  485. if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
  486. return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
  487. else
  488. return BAD_APICID;
  489. }
  490. static inline int
  491. __default_check_phys_apicid_present(int phys_apicid)
  492. {
  493. return physid_isset(phys_apicid, phys_cpu_present_map);
  494. }
  495. #ifdef CONFIG_X86_32
  496. static inline int default_cpu_present_to_apicid(int mps_cpu)
  497. {
  498. return __default_cpu_present_to_apicid(mps_cpu);
  499. }
  500. static inline int
  501. default_check_phys_apicid_present(int phys_apicid)
  502. {
  503. return __default_check_phys_apicid_present(phys_apicid);
  504. }
  505. #else
  506. extern int default_cpu_present_to_apicid(int mps_cpu);
  507. extern int default_check_phys_apicid_present(int phys_apicid);
  508. #endif
  509. #endif /* CONFIG_X86_LOCAL_APIC */
  510. extern void irq_enter(void);
  511. extern void irq_exit(void);
  512. static inline void entering_irq(void)
  513. {
  514. irq_enter();
  515. exit_idle();
  516. }
  517. static inline void entering_ack_irq(void)
  518. {
  519. ack_APIC_irq();
  520. entering_irq();
  521. }
  522. static inline void ipi_entering_ack_irq(void)
  523. {
  524. ack_APIC_irq();
  525. irq_enter();
  526. }
  527. static inline void exiting_irq(void)
  528. {
  529. irq_exit();
  530. }
  531. static inline void exiting_ack_irq(void)
  532. {
  533. irq_exit();
  534. /* Ack only at the end to avoid potential reentry */
  535. ack_APIC_irq();
  536. }
  537. extern void ioapic_zap_locks(void);
  538. #endif /* _ASM_X86_APIC_H */