HPET.c 9.3 KB

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