apic.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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/system.h>
  13. #include <asm/msr.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. #ifdef CONFIG_SMP
  43. extern void __inquire_remote_apic(int apicid);
  44. #else /* CONFIG_SMP */
  45. static inline void __inquire_remote_apic(int apicid)
  46. {
  47. }
  48. #endif /* CONFIG_SMP */
  49. static inline void default_inquire_remote_apic(int apicid)
  50. {
  51. if (apic_verbosity >= APIC_DEBUG)
  52. __inquire_remote_apic(apicid);
  53. }
  54. /*
  55. * With 82489DX we can't rely on apic feature bit
  56. * retrieved via cpuid but still have to deal with
  57. * such an apic chip so we assume that SMP configuration
  58. * is found from MP table (64bit case uses ACPI mostly
  59. * which set smp presence flag as well so we are safe
  60. * to use this helper too).
  61. */
  62. static inline bool apic_from_smp_config(void)
  63. {
  64. return smp_found_config && !disable_apic;
  65. }
  66. /*
  67. * Basic functions accessing APICs.
  68. */
  69. #ifdef CONFIG_PARAVIRT
  70. #include <asm/paravirt.h>
  71. #endif
  72. #ifdef CONFIG_X86_64
  73. extern int is_vsmp_box(void);
  74. #else
  75. static inline int is_vsmp_box(void)
  76. {
  77. return 0;
  78. }
  79. #endif
  80. extern void xapic_wait_icr_idle(void);
  81. extern u32 safe_xapic_wait_icr_idle(void);
  82. extern void xapic_icr_write(u32, u32);
  83. extern int setup_profiling_timer(unsigned int);
  84. static inline void native_apic_mem_write(u32 reg, u32 v)
  85. {
  86. volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg);
  87. alternative_io("movl %0, %1", "xchgl %0, %1", X86_FEATURE_11AP,
  88. ASM_OUTPUT2("=r" (v), "=m" (*addr)),
  89. ASM_OUTPUT2("0" (v), "m" (*addr)));
  90. }
  91. static inline u32 native_apic_mem_read(u32 reg)
  92. {
  93. return *((volatile u32 *)(APIC_BASE + reg));
  94. }
  95. extern void native_apic_wait_icr_idle(void);
  96. extern u32 native_safe_apic_wait_icr_idle(void);
  97. extern void native_apic_icr_write(u32 low, u32 id);
  98. extern u64 native_apic_icr_read(void);
  99. extern int x2apic_mode;
  100. #ifdef CONFIG_X86_X2APIC
  101. /*
  102. * Make previous memory operations globally visible before
  103. * sending the IPI through x2apic wrmsr. We need a serializing instruction or
  104. * mfence for this.
  105. */
  106. static inline void x2apic_wrmsr_fence(void)
  107. {
  108. asm volatile("mfence" : : : "memory");
  109. }
  110. static inline void native_apic_msr_write(u32 reg, u32 v)
  111. {
  112. if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
  113. reg == APIC_LVR)
  114. return;
  115. wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
  116. }
  117. static inline u32 native_apic_msr_read(u32 reg)
  118. {
  119. u64 msr;
  120. if (reg == APIC_DFR)
  121. return -1;
  122. rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
  123. return (u32)msr;
  124. }
  125. static inline void native_x2apic_wait_icr_idle(void)
  126. {
  127. /* no need to wait for icr idle in x2apic */
  128. return;
  129. }
  130. static inline u32 native_safe_x2apic_wait_icr_idle(void)
  131. {
  132. /* no need to wait for icr idle in x2apic */
  133. return 0;
  134. }
  135. static inline void native_x2apic_icr_write(u32 low, u32 id)
  136. {
  137. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  138. }
  139. static inline u64 native_x2apic_icr_read(void)
  140. {
  141. unsigned long val;
  142. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  143. return val;
  144. }
  145. extern int x2apic_phys;
  146. extern void check_x2apic(void);
  147. extern void enable_x2apic(void);
  148. extern void x2apic_icr_write(u32 low, u32 id);
  149. static inline int x2apic_enabled(void)
  150. {
  151. u64 msr;
  152. if (!cpu_has_x2apic)
  153. return 0;
  154. rdmsrl(MSR_IA32_APICBASE, msr);
  155. if (msr & X2APIC_ENABLE)
  156. return 1;
  157. return 0;
  158. }
  159. #define x2apic_supported() (cpu_has_x2apic)
  160. static inline void x2apic_force_phys(void)
  161. {
  162. x2apic_phys = 1;
  163. }
  164. #else
  165. static inline void check_x2apic(void)
  166. {
  167. }
  168. static inline void enable_x2apic(void)
  169. {
  170. }
  171. static inline int x2apic_enabled(void)
  172. {
  173. return 0;
  174. }
  175. static inline void x2apic_force_phys(void)
  176. {
  177. }
  178. #define x2apic_preenabled 0
  179. #define x2apic_supported() 0
  180. #endif
  181. extern void enable_IR_x2apic(void);
  182. extern int get_physical_broadcast(void);
  183. extern int lapic_get_maxlvt(void);
  184. extern void clear_local_APIC(void);
  185. extern void connect_bsp_APIC(void);
  186. extern void disconnect_bsp_APIC(int virt_wire_setup);
  187. extern void disable_local_APIC(void);
  188. extern void lapic_shutdown(void);
  189. extern int verify_local_APIC(void);
  190. extern void sync_Arb_IDs(void);
  191. extern void init_bsp_APIC(void);
  192. extern void setup_local_APIC(void);
  193. extern void end_local_APIC_setup(void);
  194. extern void bsp_end_local_APIC_setup(void);
  195. extern void init_apic_mappings(void);
  196. void register_lapic_address(unsigned long address);
  197. extern void setup_boot_APIC_clock(void);
  198. extern void setup_secondary_APIC_clock(void);
  199. extern int APIC_init_uniprocessor(void);
  200. extern int apic_force_enable(unsigned long addr);
  201. /*
  202. * On 32bit this is mach-xxx local
  203. */
  204. #ifdef CONFIG_X86_64
  205. extern int apic_is_clustered_box(void);
  206. #else
  207. static inline int apic_is_clustered_box(void)
  208. {
  209. return 0;
  210. }
  211. #endif
  212. extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
  213. #else /* !CONFIG_X86_LOCAL_APIC */
  214. static inline void lapic_shutdown(void) { }
  215. #define local_apic_timer_c2_ok 1
  216. static inline void init_apic_mappings(void) { }
  217. static inline void disable_local_APIC(void) { }
  218. # define setup_boot_APIC_clock x86_init_noop
  219. # define setup_secondary_APIC_clock x86_init_noop
  220. #endif /* !CONFIG_X86_LOCAL_APIC */
  221. #ifdef CONFIG_X86_64
  222. #define SET_APIC_ID(x) (apic->set_apic_id(x))
  223. #else
  224. #endif
  225. /*
  226. * Copyright 2004 James Cleverdon, IBM.
  227. * Subject to the GNU Public License, v.2
  228. *
  229. * Generic APIC sub-arch data struct.
  230. *
  231. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  232. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  233. * James Cleverdon.
  234. */
  235. struct apic {
  236. char *name;
  237. int (*probe)(void);
  238. int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
  239. int (*apic_id_registered)(void);
  240. u32 irq_delivery_mode;
  241. u32 irq_dest_mode;
  242. const struct cpumask *(*target_cpus)(void);
  243. int disable_esr;
  244. int dest_logical;
  245. unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid);
  246. unsigned long (*check_apicid_present)(int apicid);
  247. void (*vector_allocation_domain)(int cpu, struct cpumask *retmask);
  248. void (*init_apic_ldr)(void);
  249. void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
  250. void (*setup_apic_routing)(void);
  251. int (*multi_timer_check)(int apic, int irq);
  252. int (*cpu_present_to_apicid)(int mps_cpu);
  253. void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
  254. void (*setup_portio_remap)(void);
  255. int (*check_phys_apicid_present)(int phys_apicid);
  256. void (*enable_apic_mode)(void);
  257. int (*phys_pkg_id)(int cpuid_apic, int index_msb);
  258. /*
  259. * When one of the next two hooks returns 1 the apic
  260. * is switched to this. Essentially they are additional
  261. * probe functions:
  262. */
  263. int (*mps_oem_check)(struct mpc_table *mpc, char *oem, char *productid);
  264. unsigned int (*get_apic_id)(unsigned long x);
  265. unsigned long (*set_apic_id)(unsigned int id);
  266. unsigned long apic_id_mask;
  267. unsigned int (*cpu_mask_to_apicid)(const struct cpumask *cpumask);
  268. unsigned int (*cpu_mask_to_apicid_and)(const struct cpumask *cpumask,
  269. const struct cpumask *andmask);
  270. /* ipi */
  271. void (*send_IPI_mask)(const struct cpumask *mask, int vector);
  272. void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
  273. int vector);
  274. void (*send_IPI_allbutself)(int vector);
  275. void (*send_IPI_all)(int vector);
  276. void (*send_IPI_self)(int vector);
  277. /* wakeup_secondary_cpu */
  278. int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
  279. int trampoline_phys_low;
  280. int trampoline_phys_high;
  281. void (*wait_for_init_deassert)(atomic_t *deassert);
  282. void (*smp_callin_clear_local_apic)(void);
  283. void (*inquire_remote_apic)(int apicid);
  284. /* apic ops */
  285. u32 (*read)(u32 reg);
  286. void (*write)(u32 reg, u32 v);
  287. u64 (*icr_read)(void);
  288. void (*icr_write)(u32 low, u32 high);
  289. void (*wait_icr_idle)(void);
  290. u32 (*safe_wait_icr_idle)(void);
  291. #ifdef CONFIG_X86_32
  292. /*
  293. * Called very early during boot from get_smp_config(). It should
  294. * return the logical apicid. x86_[bios]_cpu_to_apicid is
  295. * initialized before this function is called.
  296. *
  297. * If logical apicid can't be determined that early, the function
  298. * may return BAD_APICID. Logical apicid will be configured after
  299. * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity
  300. * won't be applied properly during early boot in this case.
  301. */
  302. int (*x86_32_early_logical_apicid)(int cpu);
  303. /*
  304. * Optional method called from setup_local_APIC() after logical
  305. * apicid is guaranteed to be known to initialize apicid -> node
  306. * mapping if NUMA initialization hasn't done so already. Don't
  307. * add new users.
  308. */
  309. int (*x86_32_numa_cpu_node)(int cpu);
  310. #endif
  311. };
  312. /*
  313. * Pointer to the local APIC driver in use on this system (there's
  314. * always just one such driver in use - the kernel decides via an
  315. * early probing process which one it picks - and then sticks to it):
  316. */
  317. extern struct apic *apic;
  318. /*
  319. * APIC drivers are probed based on how they are listed in the .apicdrivers
  320. * section. So the order is important and enforced by the ordering
  321. * of different apic driver files in the Makefile.
  322. *
  323. * For the files having two apic drivers, we use apic_drivers()
  324. * to enforce the order with in them.
  325. */
  326. #define apic_driver(sym) \
  327. static struct apic *__apicdrivers_##sym __used \
  328. __aligned(sizeof(struct apic *)) \
  329. __section(.apicdrivers) = { &sym }
  330. #define apic_drivers(sym1, sym2) \
  331. static struct apic *__apicdrivers_##sym1##sym2[2] __used \
  332. __aligned(sizeof(struct apic *)) \
  333. __section(.apicdrivers) = { &sym1, &sym2 }
  334. extern struct apic *__apicdrivers[], *__apicdrivers_end[];
  335. /*
  336. * APIC functionality to boot other CPUs - only used on SMP:
  337. */
  338. #ifdef CONFIG_SMP
  339. extern atomic_t init_deasserted;
  340. extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
  341. #endif
  342. #ifdef CONFIG_X86_LOCAL_APIC
  343. static inline u32 apic_read(u32 reg)
  344. {
  345. return apic->read(reg);
  346. }
  347. static inline void apic_write(u32 reg, u32 val)
  348. {
  349. apic->write(reg, val);
  350. }
  351. static inline u64 apic_icr_read(void)
  352. {
  353. return apic->icr_read();
  354. }
  355. static inline void apic_icr_write(u32 low, u32 high)
  356. {
  357. apic->icr_write(low, high);
  358. }
  359. static inline void apic_wait_icr_idle(void)
  360. {
  361. apic->wait_icr_idle();
  362. }
  363. static inline u32 safe_apic_wait_icr_idle(void)
  364. {
  365. return apic->safe_wait_icr_idle();
  366. }
  367. #else /* CONFIG_X86_LOCAL_APIC */
  368. static inline u32 apic_read(u32 reg) { return 0; }
  369. static inline void apic_write(u32 reg, u32 val) { }
  370. static inline u64 apic_icr_read(void) { return 0; }
  371. static inline void apic_icr_write(u32 low, u32 high) { }
  372. static inline void apic_wait_icr_idle(void) { }
  373. static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
  374. #endif /* CONFIG_X86_LOCAL_APIC */
  375. static inline void ack_APIC_irq(void)
  376. {
  377. /*
  378. * ack_APIC_irq() actually gets compiled as a single instruction
  379. * ... yummie.
  380. */
  381. /* Docs say use 0 for future compatibility */
  382. apic_write(APIC_EOI, 0);
  383. }
  384. static inline unsigned default_get_apic_id(unsigned long x)
  385. {
  386. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  387. if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
  388. return (x >> 24) & 0xFF;
  389. else
  390. return (x >> 24) & 0x0F;
  391. }
  392. /*
  393. * Warm reset vector default position:
  394. */
  395. #define DEFAULT_TRAMPOLINE_PHYS_LOW 0x467
  396. #define DEFAULT_TRAMPOLINE_PHYS_HIGH 0x469
  397. #ifdef CONFIG_X86_64
  398. extern int default_acpi_madt_oem_check(char *, char *);
  399. extern void apic_send_IPI_self(int vector);
  400. DECLARE_PER_CPU(int, x2apic_extra_bits);
  401. extern int default_cpu_present_to_apicid(int mps_cpu);
  402. extern int default_check_phys_apicid_present(int phys_apicid);
  403. #endif
  404. static inline void default_wait_for_init_deassert(atomic_t *deassert)
  405. {
  406. while (!atomic_read(deassert))
  407. cpu_relax();
  408. return;
  409. }
  410. extern void generic_bigsmp_probe(void);
  411. #ifdef CONFIG_X86_LOCAL_APIC
  412. #include <asm/smp.h>
  413. #define APIC_DFR_VALUE (APIC_DFR_FLAT)
  414. static inline const struct cpumask *default_target_cpus(void)
  415. {
  416. #ifdef CONFIG_SMP
  417. return cpu_online_mask;
  418. #else
  419. return cpumask_of(0);
  420. #endif
  421. }
  422. DECLARE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid);
  423. static inline unsigned int read_apic_id(void)
  424. {
  425. unsigned int reg;
  426. reg = apic_read(APIC_ID);
  427. return apic->get_apic_id(reg);
  428. }
  429. extern void default_setup_apic_routing(void);
  430. extern struct apic apic_noop;
  431. #ifdef CONFIG_X86_32
  432. static inline int noop_x86_32_early_logical_apicid(int cpu)
  433. {
  434. return BAD_APICID;
  435. }
  436. /*
  437. * Set up the logical destination ID.
  438. *
  439. * Intel recommends to set DFR, LDR and TPR before enabling
  440. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  441. * document number 292116). So here it goes...
  442. */
  443. extern void default_init_apic_ldr(void);
  444. static inline int default_apic_id_registered(void)
  445. {
  446. return physid_isset(read_apic_id(), phys_cpu_present_map);
  447. }
  448. static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
  449. {
  450. return cpuid_apic >> index_msb;
  451. }
  452. #endif
  453. static inline unsigned int
  454. default_cpu_mask_to_apicid(const struct cpumask *cpumask)
  455. {
  456. return cpumask_bits(cpumask)[0] & APIC_ALL_CPUS;
  457. }
  458. static inline unsigned int
  459. default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  460. const struct cpumask *andmask)
  461. {
  462. unsigned long mask1 = cpumask_bits(cpumask)[0];
  463. unsigned long mask2 = cpumask_bits(andmask)[0];
  464. unsigned long mask3 = cpumask_bits(cpu_online_mask)[0];
  465. return (unsigned int)(mask1 & mask2 & mask3);
  466. }
  467. static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid)
  468. {
  469. return physid_isset(apicid, *map);
  470. }
  471. static inline unsigned long default_check_apicid_present(int bit)
  472. {
  473. return physid_isset(bit, phys_cpu_present_map);
  474. }
  475. static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
  476. {
  477. *retmap = *phys_map;
  478. }
  479. static inline int __default_cpu_present_to_apicid(int mps_cpu)
  480. {
  481. if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
  482. return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
  483. else
  484. return BAD_APICID;
  485. }
  486. static inline int
  487. __default_check_phys_apicid_present(int phys_apicid)
  488. {
  489. return physid_isset(phys_apicid, phys_cpu_present_map);
  490. }
  491. #ifdef CONFIG_X86_32
  492. static inline int default_cpu_present_to_apicid(int mps_cpu)
  493. {
  494. return __default_cpu_present_to_apicid(mps_cpu);
  495. }
  496. static inline int
  497. default_check_phys_apicid_present(int phys_apicid)
  498. {
  499. return __default_check_phys_apicid_present(phys_apicid);
  500. }
  501. #else
  502. extern int default_cpu_present_to_apicid(int mps_cpu);
  503. extern int default_check_phys_apicid_present(int phys_apicid);
  504. #endif
  505. #endif /* CONFIG_X86_LOCAL_APIC */
  506. #endif /* _ASM_X86_APIC_H */