loapic.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* loapic.h - public LOAPIC APIs */
  2. /*
  3. * Copyright (c) 2015 Wind River Systems, Inc.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #ifndef ZEPHYR_INCLUDE_DRIVERS_LOAPIC_H_
  8. #define ZEPHYR_INCLUDE_DRIVERS_LOAPIC_H_
  9. #include <arch/cpu.h>
  10. #include <arch/x86/msr.h>
  11. #include <sys/device_mmio.h>
  12. /* Local APIC Register Offset */
  13. #define LOAPIC_ID 0x020 /* Local APIC ID Reg */
  14. #define LOAPIC_VER 0x030 /* Local APIC Version Reg */
  15. #define LOAPIC_TPR 0x080 /* Task Priority Reg */
  16. #define LOAPIC_APR 0x090 /* Arbitration Priority Reg */
  17. #define LOAPIC_PPR 0x0a0 /* Processor Priority Reg */
  18. #define LOAPIC_EOI 0x0b0 /* EOI Reg */
  19. #define LOAPIC_LDR 0x0d0 /* Logical Destination Reg */
  20. #define LOAPIC_DFR 0x0e0 /* Destination Format Reg */
  21. #define LOAPIC_SVR 0x0f0 /* Spurious Interrupt Reg */
  22. #define LOAPIC_ISR 0x100 /* In-service Reg */
  23. #define LOAPIC_TMR 0x180 /* Trigger Mode Reg */
  24. #define LOAPIC_IRR 0x200 /* Interrupt Request Reg */
  25. #define LOAPIC_ESR 0x280 /* Error Status Reg */
  26. #define LOAPIC_ICRLO 0x300 /* Interrupt Command Reg */
  27. #define LOAPIC_ICRHI 0x310 /* Interrupt Command Reg */
  28. #define LOAPIC_TIMER 0x320 /* LVT (Timer) */
  29. #define LOAPIC_THERMAL 0x330 /* LVT (Thermal) */
  30. #define LOAPIC_PMC 0x340 /* LVT (PMC) */
  31. #define LOAPIC_LINT0 0x350 /* LVT (LINT0) */
  32. #define LOAPIC_LINT1 0x360 /* LVT (LINT1) */
  33. #define LOAPIC_ERROR 0x370 /* LVT (ERROR) */
  34. #define LOAPIC_TIMER_ICR 0x380 /* Timer Initial Count Reg */
  35. #define LOAPIC_TIMER_CCR 0x390 /* Timer Current Count Reg */
  36. #define LOAPIC_TIMER_CONFIG 0x3e0 /* Timer Divide Config Reg */
  37. #define LOAPIC_ICR_BUSY 0x00001000 /* delivery status: 1 = busy */
  38. #define LOAPIC_ICR_IPI_OTHERS 0x000C4000U /* normal IPI to other CPUs */
  39. #define LOAPIC_ICR_IPI_INIT 0x00004500U
  40. #define LOAPIC_ICR_IPI_STARTUP 0x00004600U
  41. #define LOAPIC_LVT_MASKED 0x00010000 /* mask */
  42. #ifndef _ASMLANGUAGE
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. extern uint32_t z_loapic_irq_base(void);
  47. extern void z_loapic_enable(unsigned char cpu_number);
  48. extern void z_loapic_int_vec_set(unsigned int irq, unsigned int vector);
  49. extern void z_loapic_irq_enable(unsigned int irq);
  50. extern void z_loapic_irq_disable(unsigned int irq);
  51. /**
  52. * @brief Read 64-bit value from the local APIC in x2APIC mode.
  53. *
  54. * @param reg the LOAPIC register number to read (LOAPIC_*)
  55. */
  56. static inline uint64_t x86_read_x2apic(unsigned int reg)
  57. {
  58. reg >>= 4;
  59. return z_x86_msr_read(X86_X2APIC_BASE_MSR + reg);
  60. }
  61. /* Defined in intc_loapic.c */
  62. #ifdef DEVICE_MMIO_IS_IN_RAM
  63. extern mm_reg_t z_loapic_regs;
  64. #endif
  65. /**
  66. * @brief Read 32-bit value from the local APIC in xAPIC (MMIO) mode.
  67. *
  68. * @param reg the LOAPIC register number to read (LOAPIC_*)
  69. */
  70. static inline uint32_t x86_read_xapic(unsigned int reg)
  71. {
  72. mm_reg_t base;
  73. #ifdef DEVICE_MMIO_IS_IN_RAM
  74. base = z_loapic_regs;
  75. #else
  76. base = CONFIG_LOAPIC_BASE_ADDRESS;
  77. #endif
  78. return sys_read32(base + reg);
  79. }
  80. /**
  81. * @brief Read value from the local APIC using the default mode.
  82. *
  83. * Returns a 32-bit value read from the local APIC, using the access
  84. * method determined by CONFIG_X2APIC (either xAPIC or x2APIC). Note
  85. * that 64-bit reads are only allowed in x2APIC mode and can only be
  86. * done by calling x86_read_x2apic() directly. (This is intentional.)
  87. *
  88. * @param reg the LOAPIC register number to read (LOAPIC_*)
  89. */
  90. static inline uint32_t x86_read_loapic(unsigned int reg)
  91. {
  92. #ifdef CONFIG_X2APIC
  93. return x86_read_x2apic(reg);
  94. #else
  95. return x86_read_xapic(reg);
  96. #endif
  97. }
  98. /**
  99. * @brief Write 64-bit value to the local APIC in x2APIC mode.
  100. *
  101. * @param reg the LOAPIC register number to write (one of LOAPIC_*)
  102. * @param val 64-bit value to write
  103. */
  104. static inline void x86_write_x2apic(unsigned int reg, uint64_t val)
  105. {
  106. reg >>= 4;
  107. z_x86_msr_write(X86_X2APIC_BASE_MSR + reg, val);
  108. }
  109. /**
  110. * @brief Write 32-bit value to the local APIC in xAPIC (MMIO) mode.
  111. *
  112. * @param reg the LOAPIC register number to write (one of LOAPIC_*)
  113. * @param val 32-bit value to write
  114. */
  115. static inline void x86_write_xapic(unsigned int reg, uint32_t val)
  116. {
  117. mm_reg_t base;
  118. #ifdef DEVICE_MMIO_IS_IN_RAM
  119. base = z_loapic_regs;
  120. #else
  121. base = CONFIG_LOAPIC_BASE_ADDRESS;
  122. #endif
  123. sys_write32(val, base + reg);
  124. }
  125. /**
  126. * @brief Write 32-bit value to the local APIC using the default mode.
  127. *
  128. * Write a 32-bit value to the local APIC, using the access method
  129. * determined by CONFIG_X2APIC (either xAPIC or x2APIC). Note that
  130. * 64-bit writes are only available in x2APIC mode and can only be
  131. * done by calling x86_write_x2apic() directly. (This is intentional.)
  132. *
  133. * @param reg the LOAPIC register number to write (one of LOAPIC_*)
  134. * @param val 32-bit value to write
  135. */
  136. static inline void x86_write_loapic(unsigned int reg, uint32_t val)
  137. {
  138. #ifdef CONFIG_X2APIC
  139. x86_write_x2apic(reg, val);
  140. #else
  141. x86_write_xapic(reg, val);
  142. #endif
  143. }
  144. /**
  145. * @brief Send an IPI.
  146. *
  147. * @param apic_id If applicable, the target CPU APIC ID (0 otherwise).
  148. * @param ipi Type of IPI: one of the LOAPIC_ICR_IPI_* constants.
  149. * @param vector If applicable, the target vector (0 otherwise).
  150. */
  151. static inline void z_loapic_ipi(uint8_t apic_id, uint32_t ipi, uint8_t vector)
  152. {
  153. ipi |= vector;
  154. #ifndef CONFIG_X2APIC
  155. /*
  156. * Legacy xAPIC mode: first wait for any previous IPI to be delivered.
  157. */
  158. while (x86_read_xapic(LOAPIC_ICRLO) & LOAPIC_ICR_BUSY) {
  159. }
  160. x86_write_xapic(LOAPIC_ICRHI, apic_id << 24);
  161. x86_write_xapic(LOAPIC_ICRLO, ipi);
  162. #else
  163. /*
  164. * x2APIC mode is greatly simplified: one write, no delivery status.
  165. */
  166. x86_write_x2apic(LOAPIC_ICRLO, (((uint64_t) apic_id) << 32) | ipi);
  167. #endif
  168. }
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif /* _ASMLANGUAGE */
  173. #endif /* ZEPHYR_INCLUDE_DRIVERS_LOAPIC_H_ */