HPET.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. // if (current_pcb->pid == 2)
  67. // kwarn("timer_jiffies = %ld video_refresh_expire_jiffies=%ld", timer_jiffies, video_refresh_expire_jiffies);
  68. // 当时间到了,或进程发生切换时,刷新帧缓冲区
  69. if (timer_jiffies >= video_refresh_expire_jiffies || (video_last_refresh_pid != current_pcb->pid))
  70. {
  71. raise_softirq(VIDEO_REFRESH_SIRQ);
  72. }
  73. sched_update_jiffies();
  74. break;
  75. default:
  76. kwarn("Unsupported HPET irq: %d.", number);
  77. break;
  78. }
  79. }
  80. /**
  81. * @brief 测量apic定时器频率的中断回调函数
  82. *
  83. */
  84. void HPET_measure_apic_timer_handler()
  85. {
  86. // 停止apic定时器
  87. // 写入每1ms的ticks
  88. apic_timer_stop();
  89. apic_timer_ticksIn1ms = 0xFFFFFFFF - apic_timer_get_current();
  90. measure_apic_timer_flag = true;
  91. }
  92. /**
  93. * @brief 测定apic定时器的频率
  94. *
  95. */
  96. void HPET_measure_apic_timer_freq()
  97. {
  98. kinfo("Measuring local APIC timer's frequency...");
  99. const uint64_t interval = 1; // 测量1毫秒内的计数
  100. struct apic_IO_APIC_RTE_entry entry;
  101. // 使用I/O APIC 的IRQ2接收hpet定时器0的中断
  102. apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
  103. // 计算HPET0间隔多少个时钟周期触发一次中断
  104. uint64_t clks_to_intr = 0.001 * interval * HPET_freq;
  105. // kdebug("clks_to_intr=%#ld", clks_to_intr);
  106. if (clks_to_intr <= 0 || clks_to_intr > (HPET_freq * 8))
  107. {
  108. kBUG("HPET0: Numof clocks to generate interrupt is INVALID! value=%lld", clks_to_intr);
  109. while (1)
  110. hlt();
  111. }
  112. *(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
  113. io_mfence();
  114. *(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x0044; // 设置定时器0为非周期,边沿触发,默认投递到IO APIC的2号引脚
  115. io_mfence();
  116. *(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = clks_to_intr;
  117. io_mfence();
  118. measure_apic_timer_flag = false;
  119. // 注册中断
  120. irq_register(34, &entry, &HPET_measure_apic_timer_handler, 0, &HPET_intr_controller, "HPET0 measure");
  121. // 设置div16
  122. apic_timer_set_div(0x3);
  123. apic_timer_stop();
  124. // 设置初始计数
  125. apic_timer_set_init_cnt(0xFFFFFFFF);
  126. // 启动apic定时器
  127. apic_timer_set_LVT(151, APIC_LVT_Timer_One_Shot);
  128. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 3; // 置位旧设备中断路由兼容标志位、定时器组使能标志位,开始计时
  129. io_mfence();
  130. while (measure_apic_timer_flag == false)
  131. ;
  132. irq_unregister(34);
  133. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 0; // 停用HPET定时器
  134. io_mfence();
  135. kinfo("Local APIC timer's freq: %d ticks/ms.", apic_timer_ticksIn1ms);
  136. }
  137. /**
  138. * @brief 启用HPET周期中断(5ms)
  139. *
  140. */
  141. void HPET_enable()
  142. {
  143. struct apic_IO_APIC_RTE_entry entry;
  144. // 使用I/O APIC 的IRQ2接收hpet定时器0的中断
  145. apic_make_rte_entry(&entry, 34, IO_APIC_FIXED, DEST_PHYSICAL, IDLE, POLARITY_HIGH, IRR_RESET, EDGE_TRIGGER, MASKED, 0);
  146. // 计算HPET0间隔多少个时钟周期触发一次中断
  147. uint64_t clks_to_intr = 0.001 * HPET0_INTERVAL * HPET_freq;
  148. // kdebug("clks_to_intr=%#ld", clks_to_intr);
  149. if (clks_to_intr <= 0 || clks_to_intr > (HPET_freq * 8))
  150. {
  151. kBUG("HPET0: Numof clocks to generate interrupt is INVALID! value=%lld", clks_to_intr);
  152. while (1)
  153. hlt();
  154. }
  155. // 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);
  156. *(uint64_t *)(HPET_REG_BASE + MAIN_CNT) = 0;
  157. io_mfence();
  158. *(uint64_t *)(HPET_REG_BASE + TIM0_CONF) = 0x004c; // 设置定时器0为周期定时,边沿触发,默认投递到IO APIC的2号引脚(看conf寄存器的高32bit,哪一位被置1,则可以投递到哪一个I/O apic引脚)
  159. io_mfence();
  160. *(uint64_t *)(HPET_REG_BASE + TIM0_COMP) = clks_to_intr; // 5ms触发一次中断
  161. io_mfence();
  162. // kdebug("[HPET0] conf register after modify=%#018lx", ((*(uint64_t *)(HPET_REG_BASE + TIM0_CONF))));
  163. // kdebug("[HPET1] conf register =%#018lx", ((*(uint64_t *)(HPET_REG_BASE + TIM1_CONF))));
  164. rtc_get_cmos_time(&rtc_now);
  165. kinfo("HPET0 enabled.");
  166. *(uint64_t *)(HPET_REG_BASE + GEN_CONF) = 3; // 置位旧设备中断路由兼容标志位、定时器组使能标志位
  167. io_mfence();
  168. // 注册中断
  169. irq_register(34, &entry, &HPET_handler, 0, &HPET_intr_controller, "HPET0");
  170. }
  171. int HPET_init()
  172. {
  173. kinfo("Initializing HPET...");
  174. // 从acpi获取hpet结构体
  175. ul hpet_table_addr = 0;
  176. acpi_iter_SDT(acpi_get_HPET, &hpet_table_addr);
  177. // ACPI表没有HPET,尝试读HPTC
  178. if (hpet_table_addr == 0)
  179. {
  180. kwarn("ACPI: HPET Table Not Found On This Computer!");
  181. if (RCBA_vaddr != 0)
  182. {
  183. kerror("NO HPET found on this computer!");
  184. uint32_t *hptc = (uint32_t *)(RCBA_vaddr + 0x3404UL);
  185. // enable HPET
  186. io_mfence();
  187. // 读取HPET配置寄存器地址
  188. switch ((*hptc) & 0x3)
  189. {
  190. case 0:
  191. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed00000;
  192. break;
  193. case 1:
  194. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed01000;
  195. break;
  196. case 2:
  197. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed02000;
  198. break;
  199. case 3:
  200. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed03000;
  201. break;
  202. default:
  203. break;
  204. }
  205. // enable HPET
  206. *hptc = 0x80;
  207. io_mfence();
  208. }
  209. else
  210. {
  211. // 没有RCBA寄存器,采用默认值
  212. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + 0xfed00000;
  213. kwarn("There is no RCBA register on this computer, and HPET regs base use default value.");
  214. }
  215. }
  216. else // ACPI表中有HPET表
  217. {
  218. hpet_table = (struct acpi_HPET_description_table_t *)hpet_table_addr;
  219. // kdebug("hpet_table_addr=%#018lx", hpet_table_addr);
  220. // 由于这段内存与io/apic的映射在同一物理页内,因此不需要重复映射
  221. HPET_REG_BASE = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + hpet_table->address;
  222. }
  223. // 读取计时精度并计算频率
  224. uint64_t tmp;
  225. tmp = *(uint64_t *)(HPET_REG_BASE + GCAP_ID);
  226. HPET_COUNTER_CLK_PERIOD = (tmp >> 32) & 0xffffffff;
  227. HPET_freq = 1.0 * 1e15 / HPET_COUNTER_CLK_PERIOD;
  228. HPET_NUM_TIM_CAP = (tmp >> 8) & 0x1f; // 读取计时器数量
  229. kinfo("Total HPET timers: %d", HPET_NUM_TIM_CAP);
  230. kinfo("HPET driver Initialized.");
  231. // kinfo("HPET CLK_PERIOD=%#03lx Frequency=%f", HPET_COUNTER_CLK_PERIOD, (double)HPET_freq);
  232. // kdebug("HPET_freq=%ld", (long)HPET_freq);
  233. // kdebug("HPET_freq=%lf", HPET_freq);
  234. }