lapic.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * From Coreboot file of same name
  3. *
  4. * Copyright (C) 2014 Google, Inc
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #ifndef _ARCH_ASM_LAPIC_H
  9. #define _ARCH_ASM_LAPIC_H
  10. #include <asm/io.h>
  11. #include <asm/lapic_def.h>
  12. #include <asm/msr.h>
  13. #include <asm/processor.h>
  14. /* See if I need to initialize the local apic */
  15. #if CONFIG_SMP || CONFIG_IOAPIC
  16. # define NEED_LAPIC 1
  17. #else
  18. # define NEED_LAPIC 0
  19. #endif
  20. static inline __attribute__((always_inline))
  21. unsigned long lapic_read(unsigned long reg)
  22. {
  23. return readl(LAPIC_DEFAULT_BASE + reg);
  24. }
  25. static inline __attribute__((always_inline))
  26. void lapic_write(unsigned long reg, unsigned long val)
  27. {
  28. writel(val, LAPIC_DEFAULT_BASE + reg);
  29. }
  30. static inline __attribute__((always_inline)) void lapic_wait_icr_idle(void)
  31. {
  32. do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY);
  33. }
  34. static inline void enable_lapic(void)
  35. {
  36. msr_t msr;
  37. msr = msr_read(LAPIC_BASE_MSR);
  38. msr.hi &= 0xffffff00;
  39. msr.lo |= LAPIC_BASE_MSR_ENABLE;
  40. msr.lo &= ~LAPIC_BASE_MSR_ADDR_MASK;
  41. msr.lo |= LAPIC_DEFAULT_BASE;
  42. msr_write(LAPIC_BASE_MSR, msr);
  43. }
  44. static inline void disable_lapic(void)
  45. {
  46. msr_t msr;
  47. msr = msr_read(LAPIC_BASE_MSR);
  48. msr.lo &= ~(1 << 11);
  49. msr_write(LAPIC_BASE_MSR, msr);
  50. }
  51. static inline __attribute__((always_inline)) unsigned long lapicid(void)
  52. {
  53. return lapic_read(LAPIC_ID) >> 24;
  54. }
  55. #if !CONFIG_AP_IN_SIPI_WAIT
  56. /* If we need to go back to sipi wait, we use the long non-inlined version of
  57. * this function in lapic_cpu_init.c
  58. */
  59. static inline __attribute__((always_inline)) void stop_this_cpu(void)
  60. {
  61. /* Called by an AP when it is ready to halt and wait for a new task */
  62. for (;;)
  63. cpu_hlt();
  64. }
  65. #else
  66. void stop_this_cpu(void);
  67. #endif
  68. #define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
  69. sizeof(*(ptr))))
  70. struct __xchg_dummy { unsigned long a[100]; };
  71. #define __xg(x) ((struct __xchg_dummy *)(x))
  72. /*
  73. * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
  74. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  75. * but generally the primitive is invalid, *ptr is output argument. --ANK
  76. */
  77. static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
  78. int size)
  79. {
  80. switch (size) {
  81. case 1:
  82. __asm__ __volatile__("xchgb %b0,%1"
  83. : "=q" (x)
  84. : "m" (*__xg(ptr)), "0" (x)
  85. : "memory");
  86. break;
  87. case 2:
  88. __asm__ __volatile__("xchgw %w0,%1"
  89. : "=r" (x)
  90. : "m" (*__xg(ptr)), "0" (x)
  91. : "memory");
  92. break;
  93. case 4:
  94. __asm__ __volatile__("xchgl %0,%1"
  95. : "=r" (x)
  96. : "m" (*__xg(ptr)), "0" (x)
  97. : "memory");
  98. break;
  99. }
  100. return x;
  101. }
  102. static inline void lapic_write_atomic(unsigned long reg, unsigned long v)
  103. {
  104. (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE + reg), v);
  105. }
  106. #ifdef X86_GOOD_APIC
  107. # define FORCE_READ_AROUND_WRITE 0
  108. # define lapic_read_around(x) lapic_read(x)
  109. # define lapic_write_around(x, y) lapic_write((x), (y))
  110. #else
  111. # define FORCE_READ_AROUND_WRITE 1
  112. # define lapic_read_around(x) lapic_read(x)
  113. # define lapic_write_around(x, y) lapic_write_atomic((x), (y))
  114. #endif
  115. static inline int lapic_remote_read(int apicid, int reg, unsigned long *pvalue)
  116. {
  117. int timeout;
  118. unsigned long status;
  119. int result;
  120. lapic_wait_icr_idle();
  121. lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
  122. lapic_write_around(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4));
  123. timeout = 0;
  124. do {
  125. status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK;
  126. } while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000);
  127. result = -1;
  128. if (status == LAPIC_ICR_RR_VALID) {
  129. *pvalue = lapic_read(LAPIC_RRR);
  130. result = 0;
  131. }
  132. return result;
  133. }
  134. void lapic_setup(void);
  135. #if CONFIG_SMP
  136. struct device;
  137. int start_cpu(struct device *cpu);
  138. #endif /* CONFIG_SMP */
  139. int boot_cpu(void);
  140. /**
  141. * struct x86_cpu_priv - Information about a single CPU
  142. *
  143. * @apic_id: Advanced Programmable Interrupt Controller Identifier, which is
  144. * just a number representing the CPU core
  145. *
  146. * TODO: Move this to driver model once lifecycle is understood
  147. */
  148. struct x86_cpu_priv {
  149. int apic_id;
  150. int start_err;
  151. };
  152. #endif