HPET.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "HPET.h"
  2. #include <common/kprint.h>
  3. #include <mm/mm.h>
  4. #include <driver/interrupt/apic/apic.h>
  5. #include <exception/softirq.h>
  6. #include <driver/timers/timer.h>
  7. #include <process/process.h>
  8. #include <sched/sched.h>
  9. static struct acpi_HPET_description_table_t *hpet_table;
  10. static uint64_t HPET_REG_BASE = 0;
  11. static uint32_t HPET_COUNTER_CLK_PERIOD = 0; // 主计数器时间精度(单位:飞秒)
  12. static double HPET_freq = 0; // 主计时器频率
  13. static uint8_t HPET_NUM_TIM_CAP = 0; // 定时器数量
  14. extern struct rtc_time_t rtc_now; // 导出全局墙上时钟
  15. enum
  16. {
  17. GCAP_ID = 0x00,
  18. GEN_CONF = 0x10,
  19. GINTR_STA = 0x20,
  20. MAIN_CNT = 0xf0,
  21. TIM0_CONF = 0x100,
  22. TIM0_COMP = 0x108,
  23. TIM1_CONF = 0x120,
  24. TIM1_COMP = 0x128,
  25. TIM2_CONF = 0x140,
  26. TIM2_COMP = 0x148,
  27. TIM3_CONF = 0x160,
  28. TIM3_COMP = 0x168,
  29. TIM4_CONF = 0x180,
  30. TIM4_COMP = 0x188,
  31. TIM5_CONF = 0x1a0,
  32. TIM5_COMP = 0x1a8,
  33. TIM6_CONF = 0x1c0,
  34. TIM6_COMP = 0x1c8,
  35. TIM7_CONF = 0x1e0,
  36. TIM7_COMP = 0x1e8,
  37. };
  38. hardware_intr_controller HPET_intr_controller =
  39. {
  40. .enable = apic_ioapic_enable,
  41. .disable = apic_ioapic_disable,
  42. .install = apic_ioapic_install,
  43. .uninstall = apic_ioapic_uninstall,
  44. .ack = apic_ioapic_edge_ack,
  45. };
  46. void HPET_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
  47. {
  48. //printk("(HPET)");
  49. switch (param)
  50. {
  51. case 0: // 定时器0中断
  52. ++timer_jiffies;
  53. // 若当前时间比定时任务的时间间隔大,则进入中断下半部
  54. if (container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list)->expire_jiffies <= timer_jiffies)
  55. set_softirq_status(TIMER_SIRQ);
  56. switch (current_pcb->priority)
  57. {
  58. case 0:
  59. case 1:
  60. --sched_cfs_ready_queue.cpu_exec_proc_jiffies;
  61. ++current_pcb->virtual_runtime;
  62. break;
  63. case 2:
  64. default:
  65. sched_cfs_ready_queue.cpu_exec_proc_jiffies -= 2;
  66. current_pcb->virtual_runtime += 2;
  67. break;
  68. }
  69. if (sched_cfs_ready_queue.cpu_exec_proc_jiffies <= 0)
  70. {
  71. current_pcb->flags |= PROC_NEED_SCHED;
  72. }
  73. break;
  74. default:
  75. kwarn("Unsupported HPET irq: %d.", number);
  76. break;
  77. }
  78. }
  79. int HPET_init()
  80. {
  81. kinfo("Initializing HPET...");
  82. // 从acpi获取hpet结构体
  83. ul hpet_table_addr = 0;
  84. acpi_iter_SDT(acpi_get_HPET, &hpet_table_addr);
  85. // ACPI表没有HPET,尝试读HPTC
  86. if (hpet_table_addr == 0)
  87. {
  88. kwarn("ACPI: HPET Table Not Found On This Computer!");
  89. if (RCBA_vaddr != 0)
  90. {
  91. kerror("NO HPET found on this computer!");
  92. uint32_t *hptc = (uint32_t *)(RCBA_vaddr + 0x3404UL);
  93. // enable HPET
  94. io_mfence();
  95. // 读取HPET配置寄存器地址
  96. switch ((*hptc) & 0x3)
  97. {
  98. case 0:
  99. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed00000;
  100. break;
  101. case 1:
  102. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed01000;
  103. break;
  104. case 2:
  105. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed02000;
  106. break;
  107. case 3:
  108. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed03000;
  109. break;
  110. default:
  111. break;
  112. }
  113. // enable HPET
  114. *hptc = 0x80;
  115. io_mfence();
  116. }
  117. else
  118. {
  119. // 没有RCBA寄存器,采用默认值
  120. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed00000;
  121. kwarn("There is no RCBA register on this computer, and HPET regs base use default value.");
  122. }
  123. }
  124. else // ACPI表中有HPET表
  125. {
  126. hpet_table = (struct acpi_HPET_description_table_t *)hpet_table_addr;
  127. kdebug("hpet_table_addr=%#018lx", hpet_table_addr);
  128. // 由于这段内存与io/apic的映射在同一物理页内,因此不需要重复映射
  129. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + hpet_table->address;
  130. }
  131. // 读取计时精度并计算频率
  132. uint64_t tmp;
  133. tmp = *(uint64_t *)(HPET_REG_BASE + GCAP_ID);
  134. HPET_COUNTER_CLK_PERIOD = (tmp >> 32) & 0xffffffff;
  135. HPET_freq = 1.0 * 1e15 / HPET_COUNTER_CLK_PERIOD;
  136. HPET_NUM_TIM_CAP = (tmp >> 8) & 0x1f; // 读取计时器数量
  137. // kinfo("HPET CLK_PERIOD=%#03lx Frequency=%f", HPET_COUNTER_CLK_PERIOD, (double)HPET_freq);
  138. struct apic_IO_APIC_RTE_entry entry;
  139. // 使用I/O APIC 的IRQ2接收hpet定时器0的中断
  140. apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
  141. *(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
  142. io_mfence();
  143. *(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x004c; // 设置定时器0为周期定时,边沿触发,投递到IO APIC的2号引脚(这里有点绕,写的是8259的引脚号,但是因为禁用了8259,因此会被路由到IO APIC的2号引脚)
  144. io_mfence();
  145. *(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = HPET_freq; // 1s触发一次中断
  146. io_mfence();
  147. rtc_get_cmos_time(&rtc_now);
  148. kinfo("HPET Initialized.");
  149. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 3; // 置位旧设备中断路由兼容标志位、定时器组使能标志位
  150. io_mfence();
  151. // 注册中断
  152. irq_register(34, &entry, &HPET_handler, 0, &HPET_intr_controller, "HPET0");
  153. }