apic.h 8.6 KB

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