HPET.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "HPET.h"
  2. #include <common/kprint.h>
  3. #include <mm/mm.h>
  4. #include <driver/interrupt/apic/apic.h>
  5. static struct acpi_HPET_description_table_t *hpet_table;
  6. static uint64_t HPET_REG_BASE = 0;
  7. static uint32_t HPET_COUNTER_CLK_PERIOD = 0; // 主计数器时间精度(单位:飞秒)
  8. static double HPET_freq = 0; // 主计时器频率
  9. static uint8_t HPET_NUM_TIM_CAP = 0; // 定时器数量
  10. extern struct rtc_time_t rtc_now; // 导出全局墙上时钟
  11. enum
  12. {
  13. GCAP_ID = 0x00,
  14. GEN_CONF = 0x10,
  15. GINTR_STA = 0x20,
  16. MAIN_CNT = 0xf0,
  17. TIM0_CONF = 0x100,
  18. TIM0_COMP = 0x108,
  19. TIM1_CONF = 0x120,
  20. TIM1_COMP = 0x128,
  21. TIM2_CONF = 0x140,
  22. TIM2_COMP = 0x148,
  23. TIM3_CONF = 0x160,
  24. TIM3_COMP = 0x168,
  25. TIM4_CONF = 0x180,
  26. TIM4_COMP = 0x188,
  27. TIM5_CONF = 0x1a0,
  28. TIM5_COMP = 0x1a8,
  29. TIM6_CONF = 0x1c0,
  30. TIM6_COMP = 0x1c8,
  31. TIM7_CONF = 0x1e0,
  32. TIM7_COMP = 0x1e8,
  33. };
  34. hardware_intr_controller HPET_intr_controller =
  35. {
  36. .enable = apic_ioapic_enable,
  37. .disable = apic_ioapic_disable,
  38. .install = apic_ioapic_install,
  39. .uninstall = apic_ioapic_uninstall,
  40. .ack = apic_ioapic_edge_ack,
  41. };
  42. void HPET_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
  43. {
  44. //printk_color(ORANGE, BLACK, "(HPET)");
  45. switch (param)
  46. {
  47. case 0:
  48. rtc_get_cmos_time(&rtc_now);
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. int HPET_init()
  55. {
  56. // 从acpi获取hpet结构体
  57. ul hpet_table_addr = 0;
  58. acpi_iter_SDT(acpi_get_HPET, &hpet_table_addr);
  59. if (hpet_table_addr == 0)
  60. {
  61. kerror("HPET Not Found On This Computer!");
  62. return E_HPET_INIT_FAILED;
  63. }
  64. hpet_table = (struct acpi_HPET_description_table_t *)hpet_table_addr;
  65. // 由于这段内存与io/apic的映射在同一物理页内,因此不需要重复映射
  66. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + hpet_table->address;
  67. // 读取计时精度并计算频率
  68. uint64_t tmp;
  69. tmp = *(uint64_t *)(HPET_REG_BASE + GCAP_ID);
  70. HPET_COUNTER_CLK_PERIOD = (tmp >> 32) & 0xffffffff;
  71. HPET_freq = 1.0 * 1e15 / HPET_COUNTER_CLK_PERIOD;
  72. HPET_NUM_TIM_CAP = (tmp >> 8) & 0x1f; // 读取计时器数量
  73. kinfo("HPET CLK_PERIOD=%#03lx Frequency=%f", HPET_COUNTER_CLK_PERIOD, HPET_freq);
  74. struct apic_IO_APIC_RTE_entry entry;
  75. // 使用I/O APIC 的IRQ2接收hpet定时器0的中断
  76. apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
  77. // 注册中断
  78. irq_register(34, &entry, &HPET_handler, 0, &HPET_intr_controller, "HPET0");
  79. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 3; // 置位旧设备中断路由兼容标志位、定时器组使能标志位
  80. io_mfence();
  81. *(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x004c; // 设置定时器0为周期定时,边沿触发,投递到IO APIC的2号引脚(这里有点绕,写的是8259的引脚号,但是因为禁用了8259,因此会被路由到IO APIC的2号引脚)
  82. io_mfence();
  83. *(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = HPET_freq; // 1s触发一次中断
  84. io_mfence();
  85. rtc_get_cmos_time(&rtc_now);
  86. *(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
  87. io_mfence();
  88. }