apic.h 16 KB

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