apic.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "apic.h"
  2. #include "../../../common/kprint.h"
  3. #include "../../../common/printk.h"
  4. #include "../../../common/cpu.h"
  5. #include "../../../common/glib.h"
  6. #include "../../../exception/gate.h"
  7. // 导出定义在irq.c中的中段门表
  8. extern void (*interrupt_table[24])(void);
  9. bool flag_support_apic = false;
  10. bool flag_support_x2apic = false;
  11. uint local_apic_version;
  12. uint local_apic_max_LVT_entries;
  13. /**
  14. * @brief 初始化io_apic
  15. *
  16. */
  17. void apic_io_apic_init()
  18. {
  19. // 初始化中断门, 中断使用第二个ist
  20. for (int i = 32; i <= 55; ++i)
  21. set_intr_gate(i, 2, interrupt_table[i - 32]);
  22. // 屏蔽类8259A芯片
  23. io_out8(0x21, 0xff);
  24. io_out8(0xa1, 0xff);
  25. kdebug("8259A Masked.");
  26. apic_local_apic_init();
  27. sti();
  28. }
  29. /**
  30. * @brief 初始化local apic
  31. *
  32. */
  33. void apic_local_apic_init()
  34. {
  35. uint a, b, c, d;
  36. cpu_cpuid(1, 0, &a, &b, &c, &d);
  37. kdebug("CPUID 0x01, eax:%#010lx, ebx:%#010lx, ecx:%#010lx, edx:%#010lx", a, b, c, d);
  38. // 判断是否支持APIC和xAPIC
  39. if ((1 << 9) & d)
  40. {
  41. flag_support_apic = true;
  42. kdebug("This computer support APIC&xAPIC");
  43. }
  44. else
  45. {
  46. flag_support_apic = false;
  47. kerror("This computer does not support APIC&xAPIC");
  48. while (1)
  49. ;
  50. }
  51. // 判断是否支持x2APIC
  52. if ((1 << 21) & c)
  53. {
  54. flag_support_x2apic = true;
  55. kdebug("This computer support x2APIC");
  56. }
  57. else
  58. {
  59. kerror("This computer does not support x2APIC");
  60. }
  61. uint eax, edx;
  62. // 启用xAPIC 和x2APIC
  63. __asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
  64. "rdmsr \n\t"
  65. "bts $10, %%rax \n\t"
  66. "bts $11, %%rax \n\t"
  67. "wrmsr \n\t"
  68. "movq $0x1b, %%rcx \n\t"
  69. "rdmsr \n\t"
  70. : "=a"(eax), "=d"(edx)::"memory");
  71. kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
  72. // 检测是否成功启用xAPIC和x2APIC
  73. if (eax & 0xc00)
  74. kinfo("xAPIC & x2APIC enabled!");
  75. // 设置SVR寄存器,开启local APIC、禁止EOI广播
  76. __asm__ __volatile__("movq 0x80f, %%rcx \n\t"
  77. "rdmsr \n\t"
  78. "bts $8, %%rax \n\t"
  79. "bts $12, %%rax \n\t"
  80. "movq 0x80f, %%rcx \n\t"
  81. "wrmsr \n\t"
  82. "movq $0x80f , %%rcx \n\t"
  83. "rdmsr \n\t"
  84. : "=a"(eax), "=d"(edx)::"memory", "rcx");
  85. /*
  86. //enable SVR[8]
  87. __asm__ __volatile__( "movq $0x80f, %%rcx \n\t"
  88. "rdmsr \n\t"
  89. "bts $8, %%rax \n\t"
  90. "bts $12,%%rax\n\t"
  91. "wrmsr \n\t"
  92. "movq $0x80f, %%rcx \n\t"
  93. "rdmsr \n\t"
  94. :"=a"(eax),"=d"(edx)
  95. :
  96. :"memory");
  97. */
  98. kdebug("After setting SVR: edx=%#010x, eax=%#010x", edx, eax);
  99. if (eax & 0x100)
  100. kinfo("APIC Software Enabled.");
  101. if (eax & 0x1000)
  102. kinfo("EOI-Broadcast Suppression Enabled.");
  103. // 获取Local APIC的基础信息 (参见英特尔开发手册Vol3A 10-39)
  104. // Table 10-6. Local APIC Register Address Map Supported by x2APIC
  105. // 获取 Local APIC ID
  106. // 0x802处是x2APIC ID 位宽32bits 的 Local APIC ID register
  107. __asm__ __volatile__("movq $0x802, %%rcx \n\t"
  108. "rdmsr \n\t"
  109. : "=a"(eax), "=d"(edx)::"memory");
  110. kdebug("get Local APIC ID: edx=%#010x, eax=%#010x", edx, eax);
  111. // 获取Local APIC Version
  112. // 0x803处是 Local APIC Version register
  113. __asm__ __volatile__("movq $0x803, %%rcx \n\t"
  114. "rdmsr \n\t"
  115. : "=a"(eax), "=d"(edx)::"memory");
  116. local_apic_max_LVT_entries = ((eax >> 16) & 0xff) + 1;
  117. local_apic_version = eax & 0xff;
  118. kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version, local_apic_max_LVT_entries, (eax >> 24) & 0x1);
  119. if ((eax & 0xff) < 0x10)
  120. {
  121. kdebug("82489DX discrete APIC");
  122. }
  123. else if (((eax & 0xff) >= 0x10) && ((eax & 0xff) <= 0x15))
  124. kdebug("Integrated APIC.");
  125. // 由于尚未配置LVT对应的处理程序,因此先屏蔽所有的LVT
  126. __asm__ __volatile__(
  127. "movq $0x82f, %%rcx \n\t" // CMCI
  128. "wrmsr \n\t"
  129. "movq $0x832, %%rcx \n\t" // Timer
  130. "wrmsr \n\t"
  131. "movq $0x833, %%rcx \n\t" // Thermal Monitor
  132. "wrmsr \n\t"
  133. "movq $0x834, %%rcx \n\t" // Performance Counter
  134. "wrmsr \n\t"
  135. "movq $0x835, %%rcx \n\t" // LINT0
  136. "wrmsr \n\t"
  137. "movq $0x836, %%rcx \n\t" // LINT1
  138. "wrmsr \n\t"
  139. "movq $0x837, %%rcx \n\t" // Error
  140. "wrmsr \n\t"
  141. :
  142. : "a"(0x10000), "d"(0x00)
  143. : "memory");
  144. kdebug("All LVT Masked");
  145. // 获取TPR寄存器的值
  146. __asm__ __volatile__("movq $0x808, %%rcx \n\t"
  147. "rdmsr \n\t"
  148. : "=a"(eax), "=d"(edx)::"memory");
  149. kdebug("LVT_TPR=%#010x", eax);
  150. // 获取PPR寄存器的值
  151. __asm__ __volatile__("movq $0x80a, %%rcx \n\t"
  152. "rdmsr \n\t"
  153. : "=a"(eax), "=d"(edx)::"memory");
  154. kdebug("LVT_PPR=%#010x", eax);
  155. }
  156. /**
  157. * @brief 初始化apic控制器
  158. *
  159. */
  160. void apic_init()
  161. {
  162. apic_io_apic_init();
  163. }
  164. /**
  165. * @brief 中断服务程序
  166. *
  167. * @param rsp 中断栈指针
  168. * @param number 中断号
  169. */
  170. void do_IRQ(struct pt_regs *rsp, ul number)
  171. {
  172. }