lapic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * From coreboot file of same name
  3. *
  4. * Copyright (C) 2008-2009 coresystems GmbH
  5. * Copyright (C) 2014 Google, Inc
  6. *
  7. * SPDX-License-Identifier: GPL-2.0
  8. */
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <asm/lapic.h>
  12. #include <asm/msr.h>
  13. #include <asm/msr-index.h>
  14. #include <asm/post.h>
  15. unsigned long lapic_read(unsigned long reg)
  16. {
  17. return readl(LAPIC_DEFAULT_BASE + reg);
  18. }
  19. #define xchg(ptr, v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), \
  20. sizeof(*(ptr))))
  21. struct __xchg_dummy { unsigned long a[100]; };
  22. #define __xg(x) ((struct __xchg_dummy *)(x))
  23. /*
  24. * Note: no "lock" prefix even on SMP. xchg always implies lock anyway.
  25. *
  26. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  27. * but generally the primitive is invalid, *ptr is output argument.
  28. */
  29. static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
  30. int size)
  31. {
  32. switch (size) {
  33. case 1:
  34. __asm__ __volatile__("xchgb %b0,%1"
  35. : "=q" (x)
  36. : "m" (*__xg(ptr)), "0" (x)
  37. : "memory");
  38. break;
  39. case 2:
  40. __asm__ __volatile__("xchgw %w0,%1"
  41. : "=r" (x)
  42. : "m" (*__xg(ptr)), "0" (x)
  43. : "memory");
  44. break;
  45. case 4:
  46. __asm__ __volatile__("xchgl %0,%1"
  47. : "=r" (x)
  48. : "m" (*__xg(ptr)), "0" (x)
  49. : "memory");
  50. break;
  51. }
  52. return x;
  53. }
  54. void lapic_write(unsigned long reg, unsigned long v)
  55. {
  56. (void)xchg((volatile unsigned long *)(LAPIC_DEFAULT_BASE + reg), v);
  57. }
  58. void enable_lapic(void)
  59. {
  60. msr_t msr;
  61. msr = msr_read(MSR_IA32_APICBASE);
  62. msr.hi &= 0xffffff00;
  63. msr.lo |= MSR_IA32_APICBASE_ENABLE;
  64. msr.lo &= ~MSR_IA32_APICBASE_BASE;
  65. msr.lo |= LAPIC_DEFAULT_BASE;
  66. msr_write(MSR_IA32_APICBASE, msr);
  67. }
  68. void disable_lapic(void)
  69. {
  70. msr_t msr;
  71. msr = msr_read(MSR_IA32_APICBASE);
  72. msr.lo &= ~MSR_IA32_APICBASE_ENABLE;
  73. msr_write(MSR_IA32_APICBASE, msr);
  74. }
  75. unsigned long lapicid(void)
  76. {
  77. return lapic_read(LAPIC_ID) >> 24;
  78. }
  79. static void lapic_wait_icr_idle(void)
  80. {
  81. do { } while (lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY);
  82. }
  83. int lapic_remote_read(int apicid, int reg, unsigned long *pvalue)
  84. {
  85. int timeout;
  86. unsigned long status;
  87. int result;
  88. lapic_wait_icr_idle();
  89. lapic_write(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
  90. lapic_write(LAPIC_ICR, LAPIC_DM_REMRD | (reg >> 4));
  91. timeout = 0;
  92. do {
  93. status = lapic_read(LAPIC_ICR) & LAPIC_ICR_RR_MASK;
  94. } while (status == LAPIC_ICR_RR_INPROG && timeout++ < 1000);
  95. result = -1;
  96. if (status == LAPIC_ICR_RR_VALID) {
  97. *pvalue = lapic_read(LAPIC_RRR);
  98. result = 0;
  99. }
  100. return result;
  101. }
  102. void lapic_setup(void)
  103. {
  104. #ifdef CONFIG_SMP
  105. /* Only Pentium Pro and later have those MSR stuff */
  106. debug("Setting up local apic: ");
  107. /* Enable the local apic */
  108. enable_lapic();
  109. /* Set Task Priority to 'accept all' */
  110. lapic_write(LAPIC_TASKPRI,
  111. lapic_read(LAPIC_TASKPRI) & ~LAPIC_TPRI_MASK);
  112. /* Put the local apic in virtual wire mode */
  113. lapic_write(LAPIC_SPIV, (lapic_read(LAPIC_SPIV) &
  114. ~(LAPIC_VECTOR_MASK)) | LAPIC_SPIV_ENABLE);
  115. lapic_write(LAPIC_LVT0, (lapic_read(LAPIC_LVT0) &
  116. ~(LAPIC_LVT_MASKED | LAPIC_LVT_LEVEL_TRIGGER |
  117. LAPIC_LVT_REMOTE_IRR | LAPIC_INPUT_POLARITY |
  118. LAPIC_SEND_PENDING | LAPIC_LVT_RESERVED_1 |
  119. LAPIC_DELIVERY_MODE_MASK)) |
  120. (LAPIC_LVT_REMOTE_IRR | LAPIC_SEND_PENDING |
  121. LAPIC_DELIVERY_MODE_EXTINT));
  122. lapic_write(LAPIC_LVT1, (lapic_read(LAPIC_LVT1) &
  123. ~(LAPIC_LVT_MASKED | LAPIC_LVT_LEVEL_TRIGGER |
  124. LAPIC_LVT_REMOTE_IRR | LAPIC_INPUT_POLARITY |
  125. LAPIC_SEND_PENDING | LAPIC_LVT_RESERVED_1 |
  126. LAPIC_DELIVERY_MODE_MASK)) |
  127. (LAPIC_LVT_REMOTE_IRR | LAPIC_SEND_PENDING |
  128. LAPIC_DELIVERY_MODE_NMI));
  129. debug("apic_id: 0x%02lx, ", lapicid());
  130. #else /* !CONFIG_SMP */
  131. /* Only Pentium Pro and later have those MSR stuff */
  132. debug("Disabling local apic: ");
  133. disable_lapic();
  134. #endif /* CONFIG_SMP */
  135. debug("done.\n");
  136. post_code(POST_LAPIC);
  137. }