lapic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Local APIC driver. The MMIO window must already be mapped by smp.c. */
  2. #include "lapic.h"
  3. #include "paging.h"
  4. #include "timer.h"
  5. #define LAPIC_ID 0x020
  6. #define LAPIC_TPR 0x080
  7. #define LAPIC_EOI 0x0B0
  8. #define LAPIC_SVR 0x0F0
  9. #define LAPIC_ICR_LO 0x300
  10. #define LAPIC_ICR_HI 0x310
  11. #define LAPIC_LVT_TIMER 0x320
  12. #define LAPIC_LVT_LINT0 0x350
  13. #define LAPIC_LVT_LINT1 0x360
  14. #define LAPIC_TIMER_INIT 0x380
  15. #define LAPIC_TIMER_CUR 0x390
  16. #define LAPIC_TIMER_DIV 0x3E0
  17. #define IA32_APIC_BASE_MSR 0x1B
  18. static volatile unsigned int *lapic_mmio;
  19. static unsigned int lapic_phys;
  20. static unsigned int lapic_per_ms;
  21. static inline unsigned int lapic_read(unsigned int off)
  22. {
  23. return lapic_mmio[off / 4];
  24. }
  25. static inline void lapic_write(unsigned int off, unsigned int value)
  26. {
  27. lapic_mmio[off / 4] = value;
  28. }
  29. static inline unsigned long long rdmsr(unsigned int msr)
  30. {
  31. unsigned int lo, hi;
  32. __asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr));
  33. return ((unsigned long long)hi << 32) | lo;
  34. }
  35. static inline void wrmsr(unsigned int msr, unsigned long long value)
  36. {
  37. unsigned int lo = (unsigned int)value;
  38. unsigned int hi = (unsigned int)(value >> 32);
  39. __asm__ volatile ("wrmsr" : : "c"(msr), "a"(lo), "d"(hi));
  40. }
  41. void lapic_set_base(unsigned int base)
  42. {
  43. lapic_phys = base;
  44. lapic_mmio = ioremap(base, 0x1000, IOREMAP_MMIO);
  45. }
  46. unsigned int lapic_base(void)
  47. {
  48. return lapic_phys;
  49. }
  50. static void lapic_enable(void)
  51. {
  52. unsigned long long msr = rdmsr(IA32_APIC_BASE_MSR);
  53. unsigned int base = (unsigned int)(msr & 0xFFFFF000u);
  54. unsigned int keep = (unsigned int)(msr & 0x100u); /* BSP bit */
  55. if (base != lapic_phys) {
  56. msr = (msr & ~0xFFFFF000ull) | lapic_phys;
  57. wrmsr(IA32_APIC_BASE_MSR, msr);
  58. }
  59. /* Keep the BSP bit, clear x2APIC (we drive the xAPIC MMIO window), and
  60. * set the global enable bit. */
  61. msr = (msr & ~0xF00ull) | keep | 0x800ull;
  62. wrmsr(IA32_APIC_BASE_MSR, msr);
  63. }
  64. static int lapic_wait_icr(void)
  65. {
  66. unsigned int n;
  67. for (n = 0; n < 1000000u; n++) {
  68. if (!(lapic_read(LAPIC_ICR_LO) & 0x1000u))
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. void lapic_eoi(void)
  74. {
  75. if (lapic_mmio)
  76. lapic_write(LAPIC_EOI, 0);
  77. }
  78. unsigned int lapic_id(void)
  79. {
  80. return lapic_mmio ? (lapic_read(LAPIC_ID) >> 24) : 0xFFFFFFFFu;
  81. }
  82. int lapic_init_bsp(void)
  83. {
  84. if (!lapic_mmio)
  85. return 0;
  86. lapic_enable();
  87. lapic_write(LAPIC_TPR, 0);
  88. /* Spurious vector: the handler must not send a PIC EOI. */
  89. lapic_write(LAPIC_SVR, 0x1FF);
  90. /* Keep the 8259 PIC on LINT0 (ExtINT); LINT1 stays NMI. */
  91. lapic_write(LAPIC_LVT_LINT0, 0x700);
  92. lapic_write(LAPIC_LVT_LINT1, 0x400);
  93. lapic_timer_stop();
  94. return 1;
  95. }
  96. int lapic_init_ap(void)
  97. {
  98. if (!lapic_mmio)
  99. return 0;
  100. lapic_enable();
  101. lapic_write(LAPIC_TPR, 0);
  102. lapic_write(LAPIC_SVR, 0x1FF);
  103. lapic_write(LAPIC_LVT_LINT0, 0x10000); /* masked */
  104. lapic_write(LAPIC_LVT_LINT1, 0x10000);
  105. lapic_timer_stop();
  106. return 1;
  107. }
  108. void lapic_send_ipi(unsigned int apic_id, unsigned int vector)
  109. {
  110. if (!lapic_mmio)
  111. return;
  112. lapic_wait_icr();
  113. lapic_write(LAPIC_ICR_HI, (apic_id & 0xFFu) << 24);
  114. lapic_write(LAPIC_ICR_LO, (vector & 0xFFu) | 0x00000000u);
  115. lapic_wait_icr();
  116. }
  117. void lapic_send_init(unsigned int apic_id)
  118. {
  119. if (!lapic_mmio)
  120. return;
  121. lapic_wait_icr();
  122. lapic_write(LAPIC_ICR_HI, (apic_id & 0xFFu) << 24);
  123. /* delivery mode INIT (5), level assert. */
  124. lapic_write(LAPIC_ICR_LO, 0x00004500u);
  125. lapic_wait_icr();
  126. }
  127. void lapic_send_sipi(unsigned int apic_id, unsigned int vector)
  128. {
  129. if (!lapic_mmio)
  130. return;
  131. lapic_wait_icr();
  132. lapic_write(LAPIC_ICR_HI, (apic_id & 0xFFu) << 24);
  133. /* delivery mode startup (6); vector is the real-mode page number. */
  134. lapic_write(LAPIC_ICR_LO, 0x00004600u | (vector & 0xFFu));
  135. lapic_wait_icr();
  136. }
  137. int lapic_timer_start(unsigned int vector)
  138. {
  139. unsigned int t0, count, per_ms;
  140. if (!lapic_mmio)
  141. return 0;
  142. lapic_write(LAPIC_TIMER_DIV, 0x3); /* divide by 16 */
  143. lapic_write(LAPIC_LVT_TIMER, 0x10000); /* masked while calibrating */
  144. lapic_write(LAPIC_TIMER_INIT, 0xFFFFFFFFu);
  145. t0 = get_ticks_ms();
  146. while ((unsigned int)(get_ticks_ms() - t0) < 10)
  147. __asm__ volatile ("pause");
  148. count = lapic_read(LAPIC_TIMER_CUR);
  149. per_ms = (0xFFFFFFFFu - count) / 10u;
  150. if (per_ms == 0)
  151. return 0;
  152. lapic_write(LAPIC_LVT_TIMER, (vector & 0xFFu) | 0x00020000u);
  153. lapic_per_ms = per_ms;
  154. lapic_write(LAPIC_TIMER_INIT, per_ms);
  155. return 1;
  156. }
  157. void lapic_timer_set_ms(unsigned int ms)
  158. {
  159. unsigned int init;
  160. if (!lapic_mmio || !lapic_per_ms)
  161. return;
  162. if (ms == 0)
  163. ms = 1;
  164. init = lapic_per_ms * ms;
  165. if (init)
  166. lapic_write(LAPIC_TIMER_INIT, init);
  167. }
  168. void lapic_timer_stop(void)
  169. {
  170. if (lapic_mmio)
  171. lapic_write(LAPIC_LVT_TIMER, 0x10000);
  172. }