HPET.c 9.0 KB

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