apic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #ifndef _ASM_X86_APIC_H
  2. #define _ASM_X86_APIC_H
  3. #include <linux/cpumask.h>
  4. #include <linux/delay.h>
  5. #include <linux/pm.h>
  6. #include <asm/alternative.h>
  7. #include <asm/cpufeature.h>
  8. #include <asm/processor.h>
  9. #include <asm/apicdef.h>
  10. #include <asm/atomic.h>
  11. #include <asm/fixmap.h>
  12. #include <asm/mpspec.h>
  13. #include <asm/system.h>
  14. #include <asm/msr.h>
  15. #define ARCH_APICTIMER_STOPS_ON_C3 1
  16. /*
  17. * Debugging macros
  18. */
  19. #define APIC_QUIET 0
  20. #define APIC_VERBOSE 1
  21. #define APIC_DEBUG 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. #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. #else
  73. #define setup_boot_clock setup_boot_APIC_clock
  74. #define setup_secondary_clock setup_secondary_APIC_clock
  75. #endif
  76. #ifdef CONFIG_X86_64
  77. extern int is_vsmp_box(void);
  78. #else
  79. static inline int is_vsmp_box(void)
  80. {
  81. return 0;
  82. }
  83. #endif
  84. extern void xapic_wait_icr_idle(void);
  85. extern u32 safe_xapic_wait_icr_idle(void);
  86. extern void xapic_icr_write(u32, u32);
  87. extern int setup_profiling_timer(unsigned int);
  88. static inline void native_apic_mem_write(u32 reg, u32 v)
  89. {
  90. volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg);
  91. alternative_io("movl %0, %1", "xchgl %0, %1", X86_FEATURE_11AP,
  92. ASM_OUTPUT2("=r" (v), "=m" (*addr)),
  93. ASM_OUTPUT2("0" (v), "m" (*addr)));
  94. }
  95. static inline u32 native_apic_mem_read(u32 reg)
  96. {
  97. return *((volatile u32 *)(APIC_BASE + reg));
  98. }
  99. extern void native_apic_wait_icr_idle(void);
  100. extern u32 native_safe_apic_wait_icr_idle(void);
  101. extern void native_apic_icr_write(u32 low, u32 id);
  102. extern u64 native_apic_icr_read(void);
  103. extern int x2apic_mode;
  104. #ifdef CONFIG_X86_X2APIC
  105. /*
  106. * Make previous memory operations globally visible before
  107. * sending the IPI through x2apic wrmsr. We need a serializing instruction or
  108. * mfence for this.
  109. */
  110. static inline void x2apic_wrmsr_fence(void)
  111. {
  112. asm volatile("mfence" : : : "memory");
  113. }
  114. static inline void native_apic_msr_write(u32 reg, u32 v)
  115. {
  116. if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
  117. reg == APIC_LVR)
  118. return;
  119. wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
  120. }
  121. static inline u32 native_apic_msr_read(u32 reg)
  122. {
  123. u32 low, high;
  124. if (reg == APIC_DFR)
  125. return -1;
  126. rdmsr(APIC_BASE_MSR + (reg >> 4), low, high);
  127. return low;
  128. }
  129. static inline void native_x2apic_wait_icr_idle(void)
  130. {
  131. /* no need to wait for icr idle in x2apic */
  132. return;
  133. }
  134. static inline u32 native_safe_x2apic_wait_icr_idle(void)
  135. {
  136. /* no need to wait for icr idle in x2apic */
  137. return 0;
  138. }
  139. static inline void native_x2apic_icr_write(u32 low, u32 id)
  140. {
  141. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  142. }
  143. static inline u64 native_x2apic_icr_read(void)
  144. {
  145. unsigned long val;
  146. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  147. return val;
  148. }
  149. extern int x2apic_phys;
  150. extern void check_x2apic(void);
  151. extern void enable_x2apic(void);
  152. extern void x2apic_icr_write(u32 low, u32 id);
  153. static inline int x2apic_enabled(void)
  154. {
  155. int msr, msr2;
  156. if (!cpu_has_x2apic)
  157. return 0;
  158. rdmsr(MSR_IA32_APICBASE, msr, msr2);
  159. if (msr & X2APIC_ENABLE)
  160. return 1;
  161. return 0;
  162. }
  163. #define x2apic_supported() (cpu_has_x2apic)
  164. static inline void x2apic_force_phys(void)
  165. {
  166. x2apic_phys = 1;
  167. }
  168. #else
  169. static inline void check_x2apic(void)
  170. {
  171. }
  172. static inline void enable_x2apic(void)
  173. {
  174. }
  175. static inline int x2apic_enabled(void)
  176. {
  177. return 0;
  178. }
  179. static inline void x2apic_force_phys(void)
  180. {
  181. }
  182. #define x2apic_preenabled 0
  183. #define x2apic_supported() 0
  184. #endif
  185. extern void enable_IR_x2apic(void);
  186. extern int get_physical_broadcast(void);
  187. extern void apic_disable(void);
  188. extern int lapic_get_maxlvt(void);
  189. extern void clear_local_APIC(void);
  190. extern void connect_bsp_APIC(void);
  191. extern void disconnect_bsp_APIC(int virt_wire_setup);
  192. extern void disable_local_APIC(void);
  193. extern void lapic_shutdown(void);
  194. extern int verify_local_APIC(void);
  195. extern void cache_APIC_registers(void);
  196. extern void sync_Arb_IDs(void);
  197. extern void init_bsp_APIC(void);
  198. extern void setup_local_APIC(void);
  199. extern void end_local_APIC_setup(void);
  200. extern void init_apic_mappings(void);
  201. extern void setup_boot_APIC_clock(void);
  202. extern void setup_secondary_APIC_clock(void);
  203. extern int APIC_init_uniprocessor(void);
  204. extern void enable_NMI_through_LVT0(void);
  205. /*
  206. * On 32bit this is mach-xxx local
  207. */
  208. #ifdef CONFIG_X86_64
  209. extern void early_init_lapic_mapping(void);
  210. extern int apic_is_clustered_box(void);
  211. #else
  212. static inline int apic_is_clustered_box(void)
  213. {
  214. return 0;
  215. }
  216. #endif
  217. extern u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask);
  218. extern u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask);
  219. #else /* !CONFIG_X86_LOCAL_APIC */
  220. static inline void lapic_shutdown(void) { }
  221. #define local_apic_timer_c2_ok 1
  222. static inline void init_apic_mappings(void) { }
  223. static inline void disable_local_APIC(void) { }
  224. static inline void apic_disable(void) { }
  225. #endif /* !CONFIG_X86_LOCAL_APIC */
  226. #ifdef CONFIG_X86_64
  227. #define SET_APIC_ID(x) (apic->set_apic_id(x))
  228. #else
  229. #endif
  230. /*
  231. * Copyright 2004 James Cleverdon, IBM.
  232. * Subject to the GNU Public License, v.2
  233. *
  234. * Generic APIC sub-arch data struct.
  235. *
  236. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  237. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  238. * James Cleverdon.
  239. */
  240. struct apic {
  241. char *name;
  242. int (*probe)(void);
  243. int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
  244. int (*apic_id_registered)(void);
  245. u32 irq_delivery_mode;
  246. u32 irq_dest_mode;
  247. const struct cpumask *(*target_cpus)(void);
  248. int disable_esr;
  249. int dest_logical;
  250. unsigned long (*check_apicid_used)(physid_mask_t bitmap, int apicid);
  251. unsigned long (*check_apicid_present)(int apicid);
  252. void (*vector_allocation_domain)(int cpu, struct cpumask *retmask);
  253. void (*init_apic_ldr)(void);
  254. physid_mask_t (*ioapic_phys_id_map)(physid_mask_t map);
  255. void (*setup_apic_routing)(void);
  256. int (*multi_timer_check)(int apic, int irq);
  257. int (*apicid_to_node)(int logical_apicid);
  258. int (*cpu_to_logical_apicid)(int cpu);
  259. int (*cpu_present_to_apicid)(int mps_cpu);
  260. physid_mask_t (*apicid_to_cpu_present)(int phys_apicid);
  261. void (*setup_portio_remap)(void);
  262. int (*check_phys_apicid_present)(int boot_cpu_physical_apicid);
  263. void (*enable_apic_mode)(void);
  264. int (*phys_pkg_id)(int cpuid_apic, int index_msb);
  265. /*
  266. * When one of the next two hooks returns 1 the apic
  267. * is switched to this. Essentially they are additional
  268. * probe functions:
  269. */
  270. int (*mps_oem_check)(struct mpc_table *mpc, char *oem, char *productid);
  271. unsigned int (*get_apic_id)(unsigned long x);
  272. unsigned long (*set_apic_id)(unsigned int id);
  273. unsigned long apic_id_mask;
  274. unsigned int (*cpu_mask_to_apicid)(const struct cpumask *cpumask);
  275. unsigned int (*cpu_mask_to_apicid_and)(const struct cpumask *cpumask,
  276. const struct cpumask *andmask);
  277. /* ipi */
  278. void (*send_IPI_mask)(const struct cpumask *mask, int vector);
  279. void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
  280. int vector);
  281. void (*send_IPI_allbutself)(int vector);
  282. void (*send_IPI_all)(int vector);
  283. void (*send_IPI_self)(int vector);
  284. /* wakeup_secondary_cpu */
  285. int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
  286. int trampoline_phys_low;
  287. int trampoline_phys_high;
  288. void (*wait_for_init_deassert)(atomic_t *deassert);
  289. void (*smp_callin_clear_local_apic)(void);
  290. void (*inquire_remote_apic)(int apicid);
  291. /* apic ops */
  292. u32 (*read)(u32 reg);
  293. void (*write)(u32 reg, u32 v);
  294. u64 (*icr_read)(void);
  295. void (*icr_write)(u32 low, u32 high);
  296. void (*wait_icr_idle)(void);
  297. u32 (*safe_wait_icr_idle)(void);
  298. };
  299. /*
  300. * Pointer to the local APIC driver in use on this system (there's
  301. * always just one such driver in use - the kernel decides via an
  302. * early probing process which one it picks - and then sticks to it):
  303. */
  304. extern struct apic *apic;
  305. /*
  306. * APIC functionality to boot other CPUs - only used on SMP:
  307. */
  308. #ifdef CONFIG_SMP
  309. extern atomic_t init_deasserted;
  310. extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
  311. #endif
  312. static inline u32 apic_read(u32 reg)
  313. {
  314. return apic->read(reg);
  315. }
  316. static inline void apic_write(u32 reg, u32 val)
  317. {
  318. apic->write(reg, val);
  319. }
  320. static inline u64 apic_icr_read(void)
  321. {
  322. return apic->icr_read();
  323. }
  324. static inline void apic_icr_write(u32 low, u32 high)
  325. {
  326. apic->icr_write(low, high);
  327. }
  328. static inline void apic_wait_icr_idle(void)
  329. {
  330. apic->wait_icr_idle();
  331. }
  332. static inline u32 safe_apic_wait_icr_idle(void)
  333. {
  334. return apic->safe_wait_icr_idle();
  335. }
  336. static inline void ack_APIC_irq(void)
  337. {
  338. #ifdef CONFIG_X86_LOCAL_APIC
  339. /*
  340. * ack_APIC_irq() actually gets compiled as a single instruction
  341. * ... yummie.
  342. */
  343. /* Docs say use 0 for future compatibility */
  344. apic_write(APIC_EOI, 0);
  345. #endif
  346. }
  347. static inline unsigned default_get_apic_id(unsigned long x)
  348. {
  349. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  350. if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
  351. return (x >> 24) & 0xFF;
  352. else
  353. return (x >> 24) & 0x0F;
  354. }
  355. /*
  356. * Warm reset vector default position:
  357. */
  358. #define DEFAULT_TRAMPOLINE_PHYS_LOW 0x467
  359. #define DEFAULT_TRAMPOLINE_PHYS_HIGH 0x469
  360. #ifdef CONFIG_X86_64
  361. extern struct apic apic_flat;
  362. extern struct apic apic_physflat;
  363. extern struct apic apic_x2apic_cluster;
  364. extern struct apic apic_x2apic_phys;
  365. extern int default_acpi_madt_oem_check(char *, char *);
  366. extern void apic_send_IPI_self(int vector);
  367. extern struct apic apic_x2apic_uv_x;
  368. DECLARE_PER_CPU(int, x2apic_extra_bits);
  369. extern int default_cpu_present_to_apicid(int mps_cpu);
  370. extern int default_check_phys_apicid_present(int boot_cpu_physical_apicid);
  371. #endif
  372. static inline void default_wait_for_init_deassert(atomic_t *deassert)
  373. {
  374. while (!atomic_read(deassert))
  375. cpu_relax();
  376. return;
  377. }
  378. extern void generic_bigsmp_probe(void);
  379. #ifdef CONFIG_X86_LOCAL_APIC
  380. #include <asm/smp.h>
  381. #define APIC_DFR_VALUE (APIC_DFR_FLAT)
  382. static inline const struct cpumask *default_target_cpus(void)
  383. {
  384. #ifdef CONFIG_SMP
  385. return cpu_online_mask;
  386. #else
  387. return cpumask_of(0);
  388. #endif
  389. }
  390. DECLARE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid);
  391. static inline unsigned int read_apic_id(void)
  392. {
  393. unsigned int reg;
  394. reg = apic_read(APIC_ID);
  395. return apic->get_apic_id(reg);
  396. }
  397. extern void default_setup_apic_routing(void);
  398. #ifdef CONFIG_X86_32
  399. extern struct apic apic_default;
  400. /*
  401. * Set up the logical destination ID.
  402. *
  403. * Intel recommends to set DFR, LDR and TPR before enabling
  404. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  405. * document number 292116). So here it goes...
  406. */
  407. extern void default_init_apic_ldr(void);
  408. static inline int default_apic_id_registered(void)
  409. {
  410. return physid_isset(read_apic_id(), phys_cpu_present_map);
  411. }
  412. static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
  413. {
  414. return cpuid_apic >> index_msb;
  415. }
  416. extern int default_apicid_to_node(int logical_apicid);
  417. #endif
  418. static inline unsigned int
  419. default_cpu_mask_to_apicid(const struct cpumask *cpumask)
  420. {
  421. return cpumask_bits(cpumask)[0] & APIC_ALL_CPUS;
  422. }
  423. static inline unsigned int
  424. default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  425. const struct cpumask *andmask)
  426. {
  427. unsigned long mask1 = cpumask_bits(cpumask)[0];
  428. unsigned long mask2 = cpumask_bits(andmask)[0];
  429. unsigned long mask3 = cpumask_bits(cpu_online_mask)[0];
  430. return (unsigned int)(mask1 & mask2 & mask3);
  431. }
  432. static inline unsigned long default_check_apicid_used(physid_mask_t bitmap, int apicid)
  433. {
  434. return physid_isset(apicid, bitmap);
  435. }
  436. static inline unsigned long default_check_apicid_present(int bit)
  437. {
  438. return physid_isset(bit, phys_cpu_present_map);
  439. }
  440. static inline physid_mask_t default_ioapic_phys_id_map(physid_mask_t phys_map)
  441. {
  442. return phys_map;
  443. }
  444. /* Mapping from cpu number to logical apicid */
  445. static inline int default_cpu_to_logical_apicid(int cpu)
  446. {
  447. return 1 << cpu;
  448. }
  449. static inline int __default_cpu_present_to_apicid(int mps_cpu)
  450. {
  451. if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
  452. return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
  453. else
  454. return BAD_APICID;
  455. }
  456. static inline int
  457. __default_check_phys_apicid_present(int boot_cpu_physical_apicid)
  458. {
  459. return physid_isset(boot_cpu_physical_apicid, phys_cpu_present_map);
  460. }
  461. #ifdef CONFIG_X86_32
  462. static inline int default_cpu_present_to_apicid(int mps_cpu)
  463. {
  464. return __default_cpu_present_to_apicid(mps_cpu);
  465. }
  466. static inline int
  467. default_check_phys_apicid_present(int boot_cpu_physical_apicid)
  468. {
  469. return __default_check_phys_apicid_present(boot_cpu_physical_apicid);
  470. }
  471. #else
  472. extern int default_cpu_present_to_apicid(int mps_cpu);
  473. extern int default_check_phys_apicid_present(int boot_cpu_physical_apicid);
  474. #endif
  475. static inline physid_mask_t default_apicid_to_cpu_present(int phys_apicid)
  476. {
  477. return physid_mask_of_physid(phys_apicid);
  478. }
  479. #endif /* CONFIG_X86_LOCAL_APIC */
  480. #ifdef CONFIG_X86_32
  481. extern u8 cpu_2_logical_apicid[NR_CPUS];
  482. #endif
  483. #endif /* _ASM_X86_APIC_H */