apic.h 13 KB

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