apic.c 15 KB

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