apic.h 13 KB

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