HPET.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "HPET.h"
  2. #include <common/kprint.h>
  3. #include <common/compiler.h>
  4. #include <mm/mm.h>
  5. #include <driver/interrupt/apic/apic.h>
  6. #include <exception/softirq.h>
  7. #include <time/timer.h>
  8. #include <process/process.h>
  9. #include <sched/sched.h>
  10. #include <smp/ipi.h>
  11. #include <driver/video/video.h>
  12. #include <driver/interrupt/apic/apic_timer.h>
  13. #include <process/spinlock.h>
  14. static struct acpi_HPET_description_table_t *hpet_table;
  15. static uint64_t HPET_REG_BASE = 0;
  16. static uint32_t HPET_COUNTER_CLK_PERIOD = 0; // 主计数器时间精度(单位:飞秒)
  17. static double HPET_freq = 0; // 主计时器频率
  18. static uint8_t HPET_NUM_TIM_CAP = 0; // 定时器数量
  19. static char measure_apic_timer_flag; // 初始化apic时钟时所用到的标志变量
  20. // 测定tsc频率的临时变量
  21. static uint64_t test_tsc_start = 0;
  22. static uint64_t test_tsc_end = 0;
  23. extern uint64_t Cpu_tsc_freq; // 导出自cpu.c
  24. extern struct rtc_time_t rtc_now; // 导出全局墙上时钟
  25. enum
  26. {
  27. GCAP_ID = 0x00,
  28. GEN_CONF = 0x10,
  29. GINTR_STA = 0x20,
  30. MAIN_CNT = 0xf0,
  31. TIM0_CONF = 0x100,
  32. TIM0_COMP = 0x108,
  33. TIM1_CONF = 0x120,
  34. TIM1_COMP = 0x128,
  35. TIM2_CONF = 0x140,
  36. TIM2_COMP = 0x148,
  37. TIM3_CONF = 0x160,
  38. TIM3_COMP = 0x168,
  39. TIM4_CONF = 0x180,
  40. TIM4_COMP = 0x188,
  41. TIM5_CONF = 0x1a0,
  42. TIM5_COMP = 0x1a8,
  43. TIM6_CONF = 0x1c0,
  44. TIM6_COMP = 0x1c8,
  45. TIM7_CONF = 0x1e0,
  46. TIM7_COMP = 0x1e8,
  47. };
  48. hardware_intr_controller HPET_intr_controller =
  49. {
  50. .enable = apic_ioapic_enable,
  51. .disable = apic_ioapic_disable,
  52. .install = apic_ioapic_install,
  53. .uninstall = apic_ioapic_uninstall,
  54. .ack = apic_ioapic_edge_ack,
  55. };
  56. void HPET_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
  57. {
  58. // printk("(HPET)");
  59. switch (param)
  60. {
  61. case 0: // 定时器0中断
  62. timer_jiffies += HPET0_INTERVAL;
  63. /*
  64. // 将HEPT中断消息转发到ap:1处理器
  65. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0xc8,
  66. ICR_APIC_FIXED, ICR_ALL_EXCLUDE_Self, true, 0);
  67. */
  68. // 若当前时间比定时任务的时间间隔大,则进入中断下半部
  69. if (container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list)->expire_jiffies <= timer_jiffies)
  70. raise_softirq(TIMER_SIRQ);
  71. // 当时间到了,或进程发生切换时,刷新帧缓冲区
  72. if (timer_jiffies >= video_refresh_expire_jiffies || (video_last_refresh_pid != current_pcb->pid))
  73. {
  74. raise_softirq(VIDEO_REFRESH_SIRQ);
  75. // 超过130ms仍未刷新完成,则重新发起刷新(防止由于进程异常退出导致的屏幕无法刷新)
  76. if (unlikely(timer_jiffies >= (video_refresh_expire_jiffies + (1 << 17))))
  77. {
  78. video_refresh_expire_jiffies = timer_jiffies + (1 << 20);
  79. clear_softirq_pending(VIDEO_REFRESH_SIRQ);
  80. }
  81. }
  82. break;
  83. default:
  84. kwarn("Unsupported HPET irq: %d.", number);
  85. break;
  86. }
  87. }
  88. /**
  89. * @brief 测定apic定时器以及tsc的频率的中断回调函数
  90. *
  91. */
  92. void HPET_measure_handler(uint64_t number, uint64_t param, struct pt_regs *regs)
  93. {
  94. test_tsc_end = rdtsc();
  95. // 停止apic定时器
  96. // 写入每1ms的ticks
  97. apic_timer_stop();
  98. apic_timer_ticks_result = 0xFFFFFFFF - apic_timer_get_current();
  99. measure_apic_timer_flag = true;
  100. }
  101. /**
  102. * @brief 测定apic定时器以及tsc的频率
  103. *
  104. */
  105. void HPET_measure_freq()
  106. {
  107. kinfo("Measuring local APIC timer's frequency...");
  108. const uint64_t interval = APIC_TIMER_INTERVAL; // 测量给定时间内的计数
  109. struct apic_IO_APIC_RTE_entry entry;
  110. // 使用I/O APIC 的IRQ2接收hpet定时器0的中断
  111. apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
  112. // 计算HPET0间隔多少个时钟周期触发一次中断
  113. uint64_t clks_to_intr = 0.001 * interval * HPET_freq;
  114. // kdebug("clks_to_intr=%#ld", clks_to_intr);
  115. if (clks_to_intr <= 0 || clks_to_intr > (HPET_freq * 8))
  116. {
  117. kBUG("HPET0: Numof clocks to generate interrupt is INVALID! value=%lld", clks_to_intr);
  118. while (1)
  119. hlt();
  120. }
  121. *(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
  122. io_mfence();
  123. *(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x0044; // 设置定时器0为非周期,边沿触发,默认投递到IO APIC的2号引脚
  124. io_mfence();
  125. *(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = clks_to_intr;
  126. io_mfence();
  127. measure_apic_timer_flag = false;
  128. // 注册中断
  129. irq_register(34, &entry, &HPET_measure_handler, 0, &HPET_intr_controller, "HPET0 measure");
  130. // 设置div16
  131. apic_timer_stop();
  132. apic_timer_set_div(APIC_TIMER_DIVISOR);
  133. // 设置初始计数
  134. apic_timer_set_init_cnt(0xFFFFFFFF);
  135. // 启动apic定时器
  136. apic_timer_set_LVT(151, 0, APIC_LVT_Timer_One_Shot);
  137. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 3; // 置位旧设备中断路由兼容标志位、定时器组使能标志位,开始计时
  138. // 顺便测定tsc频率
  139. test_tsc_start = rdtsc();
  140. io_mfence();
  141. while (measure_apic_timer_flag == false)
  142. ;
  143. irq_unregister(34);
  144. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 0; // 停用HPET定时器
  145. io_mfence();
  146. kinfo("Local APIC timer's freq: %d ticks/ms.", apic_timer_ticks_result);
  147. // 计算tsc频率
  148. Cpu_tsc_freq = (test_tsc_end - test_tsc_start) * (1000UL / interval);
  149. kinfo("TSC frequency: %ldMHz", Cpu_tsc_freq / 1000000);
  150. }
  151. /**
  152. * @brief 启用HPET周期中断(5ms)
  153. *
  154. */
  155. void HPET_enable()
  156. {
  157. struct apic_IO_APIC_RTE_entry entry;
  158. // 使用I/O APIC 的IRQ2接收hpet定时器0的中断
  159. apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
  160. // 计算HPET0间隔多少个时钟周期触发一次中断
  161. uint64_t clks_to_intr = 0.000001 * HPET0_INTERVAL * HPET_freq;
  162. // kdebug("clks_to_intr=%#ld", clks_to_intr);
  163. if (clks_to_intr <= 0 || clks_to_intr > (HPET_freq * 8))
  164. {
  165. kBUG("HPET0: Numof clocks to generate interrupt is INVALID! value=%lld", clks_to_intr);
  166. while (1)
  167. hlt();
  168. }
  169. // kdebug("[HPET0] conf register=%#018lx conf register[63:32]=%#06lx", (*(uint64_t *)(HPET_REG_BASE + TIM0_CONF)), ((*(uint64_t *)(HPET_REG_BASE + TIM0_CONF))>>32)&0xffffffff);
  170. *(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
  171. io_mfence();
  172. *(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x004c; // 设置定时器0为周期定时,边沿触发,默认投递到IO APIC的2号引脚(看conf寄存器的高32bit,哪一位被置1,则可以投递到哪一个I/O apic引脚)
  173. io_mfence();
  174. *(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = clks_to_intr;
  175. io_mfence();
  176. // kdebug("[HPET0] conf register after modify=%#018lx", ((*(uint64_t *)(HPET_REG_BASE + TIM0_CONF))));
  177. // kdebug("[HPET1] conf register =%#018lx", ((*(uint64_t *)(HPET_REG_BASE + TIM1_CONF))));
  178. rtc_get_cmos_time(&rtc_now);
  179. kinfo("HPET0 enabled.");
  180. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 3; // 置位旧设备中断路由兼容标志位、定时器组使能标志位
  181. io_mfence();
  182. // 注册中断
  183. irq_register(34, &entry, &HPET_handler, 0, &HPET_intr_controller, "HPET0");
  184. }
  185. int HPET_init()
  186. {
  187. kinfo("Initializing HPET...");
  188. // 从acpi获取hpet结构体
  189. ul hpet_table_addr = 0;
  190. acpi_iter_SDT(acpi_get_HPET, &hpet_table_addr);
  191. // ACPI表没有HPET,尝试读HPTC
  192. if (hpet_table_addr == 0)
  193. {
  194. kwarn("ACPI: HPET Table Not Found On This Computer!");
  195. if (RCBA_vaddr != 0)
  196. {
  197. kerror("NO HPET found on this computer!");
  198. uint32_t *hptc = (uint32_t *)(RCBA_vaddr + 0x3404UL);
  199. // enable HPET
  200. io_mfence();
  201. // 读取HPET配置寄存器地址
  202. switch ((*hptc) & 0x3)
  203. {
  204. case 0:
  205. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed00000;
  206. break;
  207. case 1:
  208. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed01000;
  209. break;
  210. case 2:
  211. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed02000;
  212. break;
  213. case 3:
  214. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed03000;
  215. break;
  216. default:
  217. break;
  218. }
  219. // enable HPET
  220. *hptc = 0x80;
  221. io_mfence();
  222. }
  223. else
  224. {
  225. // 没有RCBA寄存器,采用默认值
  226. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed00000;
  227. kwarn("There is no RCBA register on this computer, and HPET regs base use default value.");
  228. }
  229. }
  230. else // ACPI表中有HPET表
  231. {
  232. hpet_table = (struct acpi_HPET_description_table_t *)hpet_table_addr;
  233. // kdebug("hpet_table_addr=%#018lx", hpet_table_addr);
  234. // 由于这段内存与io/apic的映射在同一物理页内,因此不需要重复映射
  235. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + hpet_table->address;
  236. }
  237. // 读取计时精度并计算频率
  238. uint64_t tmp;
  239. tmp = *(uint64_t *)(HPET_REG_BASE + GCAP_ID);
  240. HPET_COUNTER_CLK_PERIOD = (tmp >> 32) & 0xffffffff;
  241. HPET_freq = 1.0 * 1e15 / HPET_COUNTER_CLK_PERIOD;
  242. HPET_NUM_TIM_CAP = (tmp >> 8) & 0x1f; // 读取计时器数量
  243. kinfo("Total HPET timers: %d", HPET_NUM_TIM_CAP);
  244. kinfo("HPET driver Initialized.");
  245. // kinfo("HPET CLK_PERIOD=%#03lx Frequency=%f", HPET_COUNTER_CLK_PERIOD, (double)HPET_freq);
  246. // kdebug("HPET_freq=%ld", (long)HPET_freq);
  247. // kdebug("HPET_freq=%lf", HPET_freq);
  248. }