apic.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #pragma once
  2. #include "../../../common/asm.h"
  3. #include "../../../process/ptrace.h"
  4. #include "../../../exception/irq.h"
  5. #include "../../../mm/mm.h"
  6. #define APIC_IO_APIC_VIRT_BASE_ADDR SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + IO_APIC_MAPPING_OFFSET
  7. #define APIC_LOCAL_APIC_VIRT_BASE_ADDR SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + LOCAL_APIC_MAPPING_OFFSET
  8. // ======== local apic 寄存器地址偏移量表 =======
  9. // 0x00~0x10 Reserved.
  10. #define LOCAL_APIC_OFFSET_Local_APIC_ID 0x20
  11. #define LOCAL_APIC_OFFSET_Local_APIC_Version 0x30
  12. // 0x40~0x70 Reserved.
  13. #define LOCAL_APIC_OFFSET_Local_APIC_TPR 0x80
  14. #define LOCAL_APIC_OFFSET_Local_APIC_APR 0x90
  15. #define LOCAL_APIC_OFFSET_Local_APIC_PPR 0xa0
  16. #define LOCAL_APIC_OFFSET_Local_APIC_EOI 0xb0
  17. #define LOCAL_APIC_OFFSET_Local_APIC_RRD 0xc0
  18. #define LOCAL_APIC_OFFSET_Local_APIC_LDR 0xd0
  19. #define LOCAL_APIC_OFFSET_Local_APIC_DFR 0xe0
  20. #define LOCAL_APIC_OFFSET_Local_APIC_SVR 0xf0
  21. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_31_0 0x100
  22. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_63_32 0x110
  23. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_95_64 0x120
  24. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_127_96 0x130
  25. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_159_128 0x140
  26. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_191_160 0x150
  27. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_223_192 0x160
  28. #define LOCAL_APIC_OFFSET_Local_APIC_ISR_255_224 0x170
  29. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_31_0 0x180
  30. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_63_32 0x190
  31. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_95_64 0x1a0
  32. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_127_96 0x1b0
  33. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_159_128 0x1c0
  34. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_191_160 0x1d0
  35. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_223_192 0x1e0
  36. #define LOCAL_APIC_OFFSET_Local_APIC_TMR_255_224 0x1f0
  37. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_31_0 0x200
  38. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_63_32 0x210
  39. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_95_64 0x220
  40. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_127_96 0x230
  41. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_159_128 0x240
  42. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_191_160 0x250
  43. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_223_192 0x260
  44. #define LOCAL_APIC_OFFSET_Local_APIC_IRR_255_224 0x270
  45. #define LOCAL_APIC_OFFSET_Local_APIC_ESR 0x280
  46. // 0x290~0x2e0 Reserved.
  47. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI 0x2f0
  48. #define LOCAL_APIC_OFFSET_Local_APIC_ICR_31_0 0x300
  49. #define LOCAL_APIC_OFFSET_Local_APIC_ICR_63_32 0x310
  50. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER 0x320
  51. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_THERMAL 0x330
  52. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_PERFORMANCE_MONITOR 0x340
  53. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT0 0x350
  54. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT1 0x360
  55. #define LOCAL_APIC_OFFSET_Local_APIC_LVT_ERROR 0x370
  56. // 初始计数寄存器(定时器专用)
  57. #define LOCAL_APIC_OFFSET_Local_APIC_INITIAL_COUNT_REG 0x380
  58. // 当前计数寄存器(定时器专用)
  59. #define LOCAL_APIC_OFFSET_Local_APIC_CURRENT_COUNT_REG 0x390
  60. // 0x3A0~0x3D0 Reserved.
  61. // 分频配置寄存器(定时器专用)
  62. #define LOCAL_APIC_OFFSET_Local_APIC_CLKDIV 0x3e0
  63. /*
  64. 1: LVT CMCI
  65. 2: LVT Timer
  66. 3: LVT Thermal Monitor
  67. 4: LVT Performace Counter
  68. 5: LVT LINT0
  69. 6: LVT LINT1
  70. 7: LVT Error
  71. */
  72. /**
  73. * LVT表项
  74. * */
  75. struct apic_LVT
  76. {
  77. uint vector : 8, // 0-7位全部置为1
  78. delivery_mode : 3, // 第[10:8]位置为100, 表示NMI
  79. reserved_1 : 1, // 第11位保留
  80. delivery_status : 1, // 第12位,投递状态 -> 发送挂起
  81. polarity : 1, // 第13位,电平触发极性 存在于LINT0,LINT1
  82. remote_IRR : 1, // 第14位,远程IRR标志位(只读) 存在于LINT0,LINT1
  83. trigger_mode : 1, // 第15位,触发模式(0位边沿触发,1为电平触发) 存在于LINT0,LINT1
  84. mask : 1, // 第16位,屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
  85. timer_mode : 2, // 第[18:17]位,定时模式。(00:一次性定时, 01:周期性定时, 10:指定TSC值计数), 存在于定时器寄存器
  86. reserved_2 : 13; // [31:19]位保留
  87. } __attribute((packed)); // 取消结构体的align
  88. /*
  89. ICR
  90. */
  91. struct INT_CMD_REG
  92. {
  93. unsigned int vector : 8, // 0~7
  94. deliver_mode : 3, // 8~10
  95. dest_mode : 1, // 11
  96. deliver_status : 1, // 12
  97. res_1 : 1, // 13
  98. level : 1, // 14
  99. trigger : 1, // 15
  100. res_2 : 2, // 16~17
  101. dest_shorthand : 2, // 18~19
  102. res_3 : 12; // 20~31
  103. union
  104. {
  105. struct
  106. {
  107. unsigned int res_4 : 24, // 32~55
  108. dest_field : 8; // 56~63
  109. } apic_destination;
  110. unsigned int x2apic_destination; // 32~63
  111. } destination;
  112. } __attribute__((packed));
  113. /**
  114. * @brief I/O APIC 的中断定向寄存器的结构体
  115. *
  116. */
  117. struct apic_IO_APIC_RTE_entry
  118. {
  119. unsigned int vector : 8, // 0~7
  120. deliver_mode : 3, // [10:8] 投递模式默认为NMI
  121. dest_mode : 1, // 11 目标模式(0位物理模式,1为逻辑模式)
  122. deliver_status : 1, // 12 投递状态
  123. polarity : 1, // 13 电平触发极性
  124. remote_IRR : 1, // 14 远程IRR标志位(只读)
  125. trigger_mode : 1, // 15 触发模式(0位边沿触发,1为电平触发)
  126. mask : 1, // 16 屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
  127. reserved : 19; // [31:17]位保留
  128. union
  129. {
  130. // 物理模式
  131. struct
  132. {
  133. unsigned int reserved1 : 24, // [55:32] 保留
  134. phy_dest : 4, // [59:56] APIC ID
  135. reserved2 : 4; // [63:60] 保留
  136. } physical;
  137. // 逻辑模式
  138. struct
  139. {
  140. unsigned int reserved1 : 24, // [55:32] 保留
  141. logical_dest : 8; // [63:56] 自定义APIC ID
  142. } logical;
  143. } destination;
  144. } __attribute__((packed));
  145. // ========== APIC的寄存器的参数定义 ==============
  146. // 投递模式
  147. #define LOCAL_APIC_FIXED 0
  148. #define IO_APIC_FIXED 0
  149. #define ICR_APIC_FIXED 0
  150. #define IO_APIC_Lowest_Priority 1
  151. #define ICR_Lowest_Priority 1
  152. #define LOCAL_APIC_SMI 2
  153. #define APIC_SMI 2
  154. #define ICR_SMI 2
  155. #define LOCAL_APIC_NMI 4
  156. #define APIC_NMI 4
  157. #define ICR_NMI 4
  158. #define LOCAL_APIC_INIT 5
  159. #define APIC_INIT 5
  160. #define ICR_INIT 5
  161. #define ICR_Start_up 6
  162. #define IO_APIC_ExtINT 7
  163. // 时钟模式
  164. #define APIC_LVT_Timer_One_Shot 0
  165. #define APIC_LVT_Timer_Periodic 1
  166. #define APIC_LVT_Timer_TSC_Deadline 2
  167. // 屏蔽
  168. #define UNMASKED 0
  169. #define MASKED 1
  170. // 触发模式
  171. #define EDGE_TRIGGER 0 // 边沿触发
  172. #define Level_TRIGGER 1 // 电平触发
  173. // 投递模式
  174. #define IDLE 0 // 挂起
  175. #define SEND_PENDING 1 // 发送等待
  176. // destination shorthand
  177. #define ICR_No_Shorthand 0
  178. #define ICR_Self 1
  179. #define ICR_ALL_INCLUDE_Self 2
  180. #define ICR_ALL_EXCLUDE_Self 3
  181. // 投递目标模式
  182. #define DEST_PHYSICAL 0 // 物理模式
  183. #define DEST_LOGIC 1 // 逻辑模式
  184. // level
  185. #define ICR_LEVEL_DE_ASSERT 0
  186. #define ICR_LEVEL_ASSERT 1
  187. // 远程IRR标志位, 在处理Local APIC标志位时置位,在收到处理器发来的EOI命令时复位
  188. #define IRR_RESET 0
  189. #define IRR_ACCEPT 1
  190. // 电平触发极性
  191. #define POLARITY_HIGH 0
  192. #define POLARITY_LOW 1
  193. struct apic_IO_APIC_map
  194. {
  195. // 间接访问寄存器的物理基地址
  196. uint addr_phys;
  197. // 索引寄存器虚拟地址
  198. unsigned char *virtual_index_addr;
  199. // 数据寄存器虚拟地址
  200. uint *virtual_data_addr;
  201. // EOI寄存器虚拟地址
  202. uint *virtual_EOI_addr;
  203. } apic_ioapic_map;
  204. /**
  205. * @brief 中断服务程序
  206. *
  207. * @param rsp 中断栈指针
  208. * @param number 中断向量号
  209. */
  210. void do_IRQ(struct pt_regs *rsp, ul number);
  211. /**
  212. * @brief 读取RTE寄存器
  213. *
  214. * @param index 索引值
  215. * @return ul
  216. */
  217. ul apic_ioapic_read_rte(unsigned char index);
  218. /**
  219. * @brief 写入RTE寄存器
  220. *
  221. * @param index 索引值
  222. * @param value 要写入的值
  223. */
  224. void apic_ioapic_write_rte(unsigned char index, ul value);
  225. /**
  226. * @brief 初始化apic控制器
  227. *
  228. */
  229. void apic_init();
  230. // =========== 中断控制操作接口 ============
  231. void apic_ioapic_enable(ul irq_num);
  232. void apic_ioapic_disable(ul irq_num);
  233. ul apic_ioapic_install(ul irq_num, void *arg);
  234. void apic_ioapic_uninstall(ul irq_num);
  235. void apic_ioapic_level_ack(ul irq_num); // 电平触发
  236. void apic_ioapic_edge_ack(ul irq_num); // 边沿触发