apic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include "../../acpi/acpi.h"
  8. // 导出定义在irq.c中的中段门表
  9. extern void (*interrupt_table[24])(void);
  10. bool flag_support_apic = false;
  11. bool flag_support_x2apic = false;
  12. uint local_apic_version;
  13. uint local_apic_max_LVT_entries;
  14. static struct acpi_Multiple_APIC_Description_Table_t *madt;
  15. static struct acpi_IO_APIC_Structure_t *io_apic_ICS;
  16. /**
  17. * @brief 初始化io_apic
  18. *
  19. */
  20. void apic_io_apic_init()
  21. {
  22. ul madt_addr;
  23. kdebug("madt_addr = %#018lx", (ul)madt_addr);
  24. acpi_iter_SDT(acpi_get_MADT, &madt_addr);
  25. madt = (struct acpi_Multiple_APIC_Description_Table_t *)madt_addr;
  26. kdebug("MADT->local intr controller addr=%#018lx", madt->Local_Interrupt_Controller_Address);
  27. kdebug("MADT->length= %d bytes", madt->header.Length);
  28. // 寻找io apic的ICS
  29. void *ent = (void *)(madt_addr) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
  30. struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
  31. while (header->length > 2)
  32. {
  33. header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
  34. if (header->type == 1)
  35. {
  36. struct acpi_IO_APIC_Structure_t *t = (struct acpi_IO_APIC_Structure_t *)ent;
  37. kdebug("IO apic addr = %#018lx", t->IO_APIC_Address);
  38. io_apic_ICS = t;
  39. break;
  40. }
  41. ent += header->length;
  42. }
  43. kdebug("Global_System_Interrupt_Base=%d", io_apic_ICS->Global_System_Interrupt_Base);
  44. apic_ioapic_map.addr_phys = io_apic_ICS->IO_APIC_Address;
  45. apic_ioapic_map.virtual_index_addr = (unsigned char *)APIC_IO_APIC_VIRT_BASE_ADDR;
  46. apic_ioapic_map.virtual_data_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x10);
  47. apic_ioapic_map.virtual_EOI_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x40);
  48. // 填写页表,完成地址映射
  49. mm_map_phys_addr(apic_ioapic_map.virtual_index_addr, apic_ioapic_map.addr_phys, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
  50. // 设置IO APIC ID 为0x0f000000
  51. *apic_ioapic_map.virtual_index_addr = 0x00;
  52. io_mfence();
  53. *apic_ioapic_map.virtual_data_addr = 0x0f000000;
  54. io_mfence();
  55. kdebug("IOAPIC Version:%#010x", ((*apic_ioapic_map.virtual_data_addr) >> 24) & 0xf);
  56. io_mfence();
  57. // 获取IO APIC Version
  58. *apic_ioapic_map.virtual_index_addr = 0x01;
  59. io_mfence();
  60. kdebug("IO APIC Version=%d, Max Redirection Entries=%d", *apic_ioapic_map.virtual_data_addr & 0xff, (((*apic_ioapic_map.virtual_data_addr) >> 16) & 0xff) + 1);
  61. // 初始化RTE表项,将所有RTE表项屏蔽
  62. for (int i = 0x10; i < 0x40; i += 2)
  63. {
  64. // 以0x20为起始中断向量号,初始化RTE
  65. apic_ioapic_write_rte(i, 0x10020 + ((i - 0x10) >> 1));
  66. }
  67. // 开启键盘中断,中断向量号为0x21,物理模式,投递至BSP处理器
  68. apic_ioapic_write_rte(0x12, 0x21);
  69. // 不需要手动启动IO APIC,只要初始化了RTE寄存器之后,io apic就会自动启用了。
  70. // 而且不是每台电脑都有RCBA寄存器,因此不需要手动启用IO APIC
  71. /*
  72. // get RCBA address
  73. io_out32(0xcf8, 0x8000f8f0);
  74. uint x = io_in32(0xcfc);
  75. uint *p;
  76. printk_color(RED, BLACK, "Get RCBA Address:%#010x\n", x);
  77. x = x & 0xffffc000UL;
  78. printk_color(RED, BLACK, "Get RCBA Address:%#010x\n", x);
  79. // get OIC address
  80. if (x > 0xfec00000 && x < 0xfee00000)
  81. {
  82. p = (unsigned int *)(x + 0x31feUL+SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE);
  83. }
  84. // enable IOAPIC
  85. x = (*p & 0xffffff00) | 0x100;
  86. io_mfence();
  87. *p = x;
  88. io_mfence();
  89. */
  90. }
  91. /**
  92. * @brief 初始化local apic
  93. *
  94. */
  95. void apic_local_apic_init()
  96. {
  97. uint a, b, c, d;
  98. cpu_cpuid(1, 0, &a, &b, &c, &d);
  99. kdebug("CPUID 0x01, eax:%#010lx, ebx:%#010lx, ecx:%#010lx, edx:%#010lx", a, b, c, d);
  100. // 判断是否支持APIC和xAPIC
  101. if ((1 << 9) & d)
  102. {
  103. flag_support_apic = true;
  104. kdebug("This computer support APIC&xAPIC");
  105. }
  106. else
  107. {
  108. flag_support_apic = false;
  109. kerror("This computer does not support APIC&xAPIC");
  110. while (1)
  111. ;
  112. }
  113. // 判断是否支持x2APIC
  114. if ((1 << 21) & c)
  115. {
  116. flag_support_x2apic = true;
  117. kdebug("This computer support x2APIC");
  118. }
  119. else
  120. {
  121. kerror("This computer does not support x2APIC");
  122. }
  123. uint eax, edx;
  124. // 启用xAPIC 和x2APIC
  125. __asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
  126. "rdmsr \n\t"
  127. "bts $10, %%rax \n\t"
  128. "bts $11, %%rax \n\t"
  129. "wrmsr \n\t"
  130. "movq $0x1b, %%rcx \n\t"
  131. "rdmsr \n\t"
  132. : "=a"(eax), "=d"(edx)::"memory");
  133. kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
  134. // 检测是否成功启用xAPIC和x2APIC
  135. if (eax & 0xc00)
  136. kinfo("xAPIC & x2APIC enabled!");
  137. // 设置SVR寄存器,开启local APIC、禁止EOI广播
  138. __asm__ __volatile__("movq 0x80f, %%rcx \n\t"
  139. "rdmsr \n\t"
  140. "bts $8, %%rax \n\t"
  141. "bts $12, %%rax \n\t"
  142. "movq 0x80f, %%rcx \n\t"
  143. "wrmsr \n\t"
  144. "movq $0x80f , %%rcx \n\t"
  145. "rdmsr \n\t"
  146. : "=a"(eax), "=d"(edx)::"memory", "rcx");
  147. /*
  148. //enable SVR[8]
  149. __asm__ __volatile__( "movq $0x80f, %%rcx \n\t"
  150. "rdmsr \n\t"
  151. "bts $8, %%rax \n\t"
  152. "bts $12,%%rax\n\t"
  153. "wrmsr \n\t"
  154. "movq $0x80f, %%rcx \n\t"
  155. "rdmsr \n\t"
  156. :"=a"(eax),"=d"(edx)
  157. :
  158. :"memory");
  159. */
  160. kdebug("After setting SVR: edx=%#010x, eax=%#010x", edx, eax);
  161. if (eax & 0x100)
  162. kinfo("APIC Software Enabled.");
  163. if (eax & 0x1000)
  164. kinfo("EOI-Broadcast Suppression Enabled.");
  165. // 获取Local APIC的基础信息 (参见英特尔开发手册Vol3A 10-39)
  166. // Table 10-6. Local APIC Register Address Map Supported by x2APIC
  167. // 获取 Local APIC ID
  168. // 0x802处是x2APIC ID 位宽32bits 的 Local APIC ID register
  169. __asm__ __volatile__("movq $0x802, %%rcx \n\t"
  170. "rdmsr \n\t"
  171. : "=a"(eax), "=d"(edx)::"memory");
  172. kdebug("get Local APIC ID: edx=%#010x, eax=%#010x", edx, eax);
  173. // 获取Local APIC Version
  174. // 0x803处是 Local APIC Version register
  175. __asm__ __volatile__("movq $0x803, %%rcx \n\t"
  176. "rdmsr \n\t"
  177. : "=a"(eax), "=d"(edx)::"memory");
  178. local_apic_max_LVT_entries = ((eax >> 16) & 0xff) + 1;
  179. local_apic_version = eax & 0xff;
  180. 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);
  181. if ((eax & 0xff) < 0x10)
  182. {
  183. kdebug("82489DX discrete APIC");
  184. }
  185. else if (((eax & 0xff) >= 0x10) && ((eax & 0xff) <= 0x15))
  186. kdebug("Integrated APIC.");
  187. // 由于尚未配置LVT对应的处理程序,因此先屏蔽所有的LVT
  188. __asm__ __volatile__(
  189. "movq $0x82f, %%rcx \n\t" // CMCI
  190. "wrmsr \n\t"
  191. "movq $0x832, %%rcx \n\t" // Timer
  192. "wrmsr \n\t"
  193. "movq $0x833, %%rcx \n\t" // Thermal Monitor
  194. "wrmsr \n\t"
  195. "movq $0x834, %%rcx \n\t" // Performance Counter
  196. "wrmsr \n\t"
  197. "movq $0x835, %%rcx \n\t" // LINT0
  198. "wrmsr \n\t"
  199. "movq $0x836, %%rcx \n\t" // LINT1
  200. "wrmsr \n\t"
  201. "movq $0x837, %%rcx \n\t" // Error
  202. "wrmsr \n\t"
  203. :
  204. : "a"(0x10000), "d"(0x00)
  205. : "memory");
  206. kdebug("All LVT Masked");
  207. // 获取TPR寄存器的值
  208. __asm__ __volatile__("movq $0x808, %%rcx \n\t"
  209. "rdmsr \n\t"
  210. : "=a"(eax), "=d"(edx)::"memory");
  211. kdebug("LVT_TPR=%#010x", eax);
  212. // 获取PPR寄存器的值
  213. __asm__ __volatile__("movq $0x80a, %%rcx \n\t"
  214. "rdmsr \n\t"
  215. : "=a"(eax), "=d"(edx)::"memory");
  216. kdebug("LVT_PPR=%#010x", eax);
  217. // 映射Local APIC 寄存器地址
  218. mm_map_phys_addr(APIC_LOCAL_APIC_VIRT_BASE_ADDR, 0xfee00000, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
  219. }
  220. /**
  221. * @brief 初始化apic控制器
  222. *
  223. */
  224. void apic_init()
  225. {
  226. // 初始化中断门, 中断使用第二个ist
  227. for (int i = 32; i <= 55; ++i)
  228. set_intr_gate(i, 2, interrupt_table[i - 32]);
  229. // 屏蔽类8259A芯片
  230. io_out8(0x21, 0xff);
  231. io_out8(0xa1, 0xff);
  232. kdebug("8259A Masked.");
  233. // enable IMCR
  234. io_out8(0x22, 0x70);
  235. io_out8(0x23, 0x01);
  236. apic_local_apic_init();
  237. apic_io_apic_init();
  238. sti();
  239. }
  240. /**
  241. * @brief 中断服务程序
  242. *
  243. * @param rsp 中断栈指针
  244. * @param number 中断向量号
  245. */
  246. void do_IRQ(struct pt_regs *rsp, ul number)
  247. {
  248. unsigned char x = io_in8(0x60);
  249. irq_desc_t *irq = &interrupt_desc[number - 32];
  250. // 执行中断上半部处理程序
  251. if (irq->handler != NULL)
  252. irq->handler(number, irq->parameter, rsp);
  253. else
  254. kwarn("Intr vector [%d] does not have a handler!");
  255. // 向中断控制器发送应答消息
  256. if (irq->controller != NULL && irq->controller->ack != NULL)
  257. irq->controller->ack(number);
  258. // 向EOI寄存器写入0x00表示结束中断
  259. io_mfence();
  260. uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
  261. *eoi = 0x00;
  262. io_mfence();
  263. }
  264. /**
  265. * @brief 读取RTE寄存器
  266. * 由于RTE位宽为64位而IO window寄存器只有32位,因此需要两次读取
  267. * @param index 索引值
  268. * @return ul
  269. */
  270. ul apic_ioapic_read_rte(unsigned char index)
  271. {
  272. // 由于处理器的乱序执行的问题,需要加入内存屏障以保证结果的正确性。
  273. ul ret;
  274. // 先读取高32bit
  275. *apic_ioapic_map.virtual_index_addr = index + 1;
  276. io_mfence();
  277. ret = *apic_ioapic_map.virtual_data_addr;
  278. ret <<= 32;
  279. io_mfence();
  280. // 读取低32bit
  281. *apic_ioapic_map.virtual_index_addr = index;
  282. io_mfence();
  283. ret |= *apic_ioapic_map.virtual_data_addr;
  284. io_mfence();
  285. return ret;
  286. }
  287. /**
  288. * @brief 写入RTE寄存器
  289. *
  290. * @param index 索引值
  291. * @param value 要写入的值
  292. */
  293. void apic_ioapic_write_rte(unsigned char index, ul value)
  294. {
  295. // 先写入低32bit
  296. *apic_ioapic_map.virtual_index_addr = index;
  297. io_mfence();
  298. *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
  299. io_mfence();
  300. // 再写入高32bit
  301. value >>= 32;
  302. io_mfence();
  303. *apic_ioapic_map.virtual_index_addr = index + 1;
  304. io_mfence();
  305. *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
  306. io_mfence();
  307. }
  308. // =========== 中断控制操作接口 ============
  309. void apic_ioapic_enable(ul irq_num)
  310. {
  311. ul index = 0x10 + ((irq_num - 32) << 1);
  312. ul value = apic_ioapic_read_rte(index);
  313. value &= (~0x10000UL);
  314. apic_ioapic_write_rte(index, value);
  315. }
  316. void apic_ioapic_disable(ul irq_num)
  317. {
  318. ul index = 0x10 + ((irq_num - 32) << 1);
  319. ul value = apic_ioapic_read_rte(index);
  320. value |= (0x10000UL);
  321. apic_ioapic_write_rte(index, value);
  322. }
  323. ul apic_ioapic_install(ul irq_num, void *arg)
  324. {
  325. struct apic_IO_APIC_RTE_entry *entry = (struct apic_IO_APIC_RTE_entry *)arg;
  326. // RTE表项值写入对应的RTE寄存器
  327. apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), *(ul *)entry);
  328. return 0;
  329. }
  330. void apic_ioapic_uninstall(ul irq_num)
  331. {
  332. // 将对应的RTE表项设置为屏蔽状态
  333. apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), 0x10000UL);
  334. }
  335. void apic_ioapic_level_ack(ul irq_num) // 电平触发
  336. {
  337. // 向EOI寄存器写入0x00表示结束中断
  338. uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
  339. *eoi = 0x00;
  340. *apic_ioapic_map.virtual_EOI_addr = irq_num;
  341. }
  342. void apic_ioapic_edge_ack(ul irq_num) // 边沿触发
  343. {
  344. // 向EOI寄存器写入0x00表示结束中断
  345. uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
  346. *eoi = 0x00;
  347. }