HPET.c 6.3 KB

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