irq.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /****************************************************************************
  2. * arch/x86_64/include/intel64/irq.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /* This file should never be included directly but, rather, only indirectly
  21. * through nuttx/irq.h
  22. */
  23. #ifndef __ARCH_X86_64_INCLUDE_INTEL64_IRQ_H
  24. #define __ARCH_X86_64_INCLUDE_INTEL64_IRQ_H
  25. /****************************************************************************
  26. * Included Files
  27. ****************************************************************************/
  28. #ifndef __ASSEMBLY__
  29. # include <stdint.h>
  30. # include <stdbool.h>
  31. # include <arch/arch.h>
  32. # include <semaphore.h>
  33. # include <time.h>
  34. # include <debug.h>
  35. # include <nuttx/config.h>
  36. #endif
  37. /****************************************************************************
  38. * Pre-processor Definitions
  39. ****************************************************************************/
  40. /* ISR and IRQ numbers */
  41. #define ISR0 0 /* Division by zero exception */
  42. #define ISR1 1 /* Debug exception */
  43. #define ISR2 2 /* Non maskable interrupt */
  44. #define ISR3 3 /* Breakpoint exception */
  45. #define ISR4 4 /* 'Into detected overflow' */
  46. #define ISR5 5 /* Out of bounds exception */
  47. #define ISR6 6 /* Invalid opcode exception */
  48. #define ISR7 7 /* No coprocessor exception */
  49. #define ISR8 8 /* Double fault (pushes an error code) */
  50. #define ISR9 9 /* Coprocessor segment overrun */
  51. #define ISR10 10 /* Bad TSS (pushes an error code) */
  52. #define ISR11 11 /* Segment not present (pushes an error code) */
  53. #define ISR12 12 /* Stack fault (pushes an error code) */
  54. #define ISR13 13 /* General protection fault (pushes an error code) */
  55. #define ISR14 14 /* Page fault (pushes an error code) */
  56. #define ISR15 15 /* Unknown interrupt exception */
  57. #define ISR16 16 /* Coprocessor fault */
  58. #define ISR17 17 /* Alignment check exception */
  59. #define ISR18 18 /* Machine check exception */
  60. #define ISR19 19 /* SIMD Float-Point Exception*/
  61. #define ISR20 20 /* Virtualization Exception */
  62. #define ISR21 21 /* Reserved */
  63. #define ISR22 22 /* Reserved */
  64. #define ISR23 23 /* Reserved */
  65. #define ISR24 24 /* Reserved */
  66. #define ISR25 25 /* Reserved */
  67. #define ISR26 26 /* Reserved */
  68. #define ISR27 27 /* Reserved */
  69. #define ISR28 28 /* Reserved */
  70. #define ISR29 29 /* Reserved */
  71. #define ISR30 30 /* Security Exception */
  72. #define ISR31 31 /* Reserved */
  73. #define IRQ0 32 /* System timer (cannot be changed) */
  74. #define IRQ1 33 /* Keyboard controller (cannot be changed) */
  75. #define IRQ2 34 /* Cascaded signals from IRQs 8~15 */
  76. #define IRQ3 35 /* Serial port controller for COM2/4 */
  77. #define IRQ4 36 /* serial port controller for COM1/3 */
  78. #define IRQ5 37 /* LPT port 2 or sound card */
  79. #define IRQ6 38 /* Floppy disk controller */
  80. #define IRQ7 39 /* LPT port 1 or sound card */
  81. #define IRQ8 40 /* Real time clock (RTC) */
  82. #define IRQ9 41 /* Open interrupt/available or SCSI host adapter */
  83. #define IRQ10 42 /* Open interrupt/available or SCSI or NIC */
  84. #define IRQ11 43 /* Open interrupt/available or SCSI or NIC */
  85. #define IRQ12 44 /* Mouse on PS/2 connector */
  86. #define IRQ13 45 /* Math coprocessor */
  87. #define IRQ14 46 /* Primary ATA channel */
  88. #define IRQ15 47 /* Secondary ATA channel */
  89. #define IRQ_ERROR 51 /* APIC Error */
  90. #define IRQ_SPURIOUS 0xff /* Spurious Interrupts */
  91. #define NR_IRQS 48
  92. /* Common register save structure created by up_saveusercontext() and by
  93. * ISR/IRQ interrupt processing.
  94. */
  95. #define XCPTCONTEXT_XMM_AREA_SIZE 512
  96. #define XMMAREA_OFFSET XCPTCONTEXT_XMM_AREA_SIZE / 8
  97. /* Data segments */
  98. #define REG_ALIGN (0 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  99. #define REG_FS (1 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  100. #define REG_GS (2 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  101. #define REG_ES (3 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  102. #define REG_DS (4 + XMMAREA_OFFSET) /* Data segment selector */
  103. /* Remaining regs */
  104. #define REG_RAX (5 + XMMAREA_OFFSET) /* " " "" " " */
  105. #define REG_RBX (6 + XMMAREA_OFFSET) /* " " "" " " */
  106. #define REG_RBP (7 + XMMAREA_OFFSET) /* " " "" " " */
  107. #define REG_R10 (8 + XMMAREA_OFFSET) /* " " "" " " */
  108. #define REG_R11 (9 + XMMAREA_OFFSET) /* " " "" " " */
  109. #define REG_R12 (10 + XMMAREA_OFFSET) /* " " "" " " */
  110. #define REG_R13 (11 + XMMAREA_OFFSET) /* " " "" " " */
  111. #define REG_R14 (12 + XMMAREA_OFFSET) /* " " "" " " */
  112. #define REG_R15 (13 + XMMAREA_OFFSET) /* " " "" " " */
  113. /* ABI calling convention */
  114. #define REG_R9 (14 + XMMAREA_OFFSET) /* " " "" " " */
  115. #define REG_R8 (15 + XMMAREA_OFFSET) /* " " "" " " */
  116. #define REG_RCX (16 + XMMAREA_OFFSET) /* " " "" " " */
  117. #define REG_RDX (17 + XMMAREA_OFFSET) /* " " "" " " */
  118. #define REG_RSI (18 + XMMAREA_OFFSET) /* " " "" " " */
  119. #define REG_RDI (19 + XMMAREA_OFFSET) /* " " "" " " */
  120. /* IRQ saved */
  121. #define REG_ERRCODE (20 + XMMAREA_OFFSET) /* Error code */
  122. #define REG_RIP (21 + XMMAREA_OFFSET) /* Pushed by process on interrupt processing */
  123. #define REG_CS (22 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  124. #define REG_RFLAGS (23 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  125. #define REG_RSP (24 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  126. #define REG_SS (25 + XMMAREA_OFFSET) /* " " "" " " "" " " " " */
  127. /* NOTE 2: This is not really state data. Rather, this is just a convenient
  128. * way to pass parameters from the interrupt handler to C code.
  129. */
  130. #define XCPTCONTEXT_REGS (26 + XCPTCONTEXT_XMM_AREA_SIZE / 8)
  131. #define XCPTCONTEXT_SIZE (8 * XCPTCONTEXT_REGS + XCPTCONTEXT_XMM_AREA_SIZE)
  132. /****************************************************************************
  133. * Public Types
  134. ****************************************************************************/
  135. #ifndef __ASSEMBLY__
  136. enum ioapic_trigger_mode
  137. {
  138. TRIGGER_RISING_EDGE = 0,
  139. TRIGGER_FALLING_EDGE = (1 << 13),
  140. TRIGGER_LEVEL_ACTIVE_HIGH = 1 << 15,
  141. TRIGGER_LEVEL_ACTIVE_LOW = (1 << 15) | (1 << 13),
  142. };
  143. /* This struct defines the way the registers are stored */
  144. struct xcptcontext
  145. {
  146. /* The following function pointer is non-zero if there are pending signals
  147. * to be processed.
  148. */
  149. #ifndef CONFIG_DISABLE_SIGNALS
  150. void *sigdeliver; /* Actual type is sig_deliver_t */
  151. /* These are saved copies of instruction pointer and EFLAGS used during
  152. * signal processing.
  153. */
  154. uint64_t saved_rip;
  155. uint64_t saved_rflags;
  156. uint64_t saved_rsp;
  157. #endif
  158. /* Register save area */
  159. uint64_t regs[XCPTCONTEXT_REGS] __attribute__((aligned (16)));
  160. };
  161. #endif
  162. /****************************************************************************
  163. * Inline functions
  164. ****************************************************************************/
  165. #ifndef __ASSEMBLY__
  166. /* Name: up_irq_save, up_irq_restore, and friends.
  167. *
  168. * NOTE: This function should never be called from application code and,
  169. * as a general rule unless you really know what you are doing, this
  170. * function should not be called directly from operation system code either:
  171. * Typically, the wrapper functions, enter_critical_section() and
  172. * leave_critical section(), are probably what you really want.
  173. */
  174. /* Get the current FLAGS register contents */
  175. static inline irqstate_t irqflags()
  176. {
  177. irqstate_t flags;
  178. asm volatile(
  179. "\tpushfq\n"
  180. "\tpopq %0\n"
  181. : "=rm" (flags)
  182. :
  183. : "memory");
  184. return flags;
  185. }
  186. /* Get a sample of the FLAGS register, determine if interrupts are disabled.
  187. * If the X86_FLAGS_IF is cleared by cli, then interrupts are disabled. If
  188. * if the X86_FLAGS_IF is set by sti, then interrupts are enable.
  189. */
  190. static inline bool up_irq_disabled(irqstate_t flags)
  191. {
  192. return ((flags & X86_64_RFLAGS_IF) == 0);
  193. }
  194. static inline bool up_irq_enabled(irqstate_t flags)
  195. {
  196. return ((flags & X86_64_RFLAGS_IF) != 0);
  197. }
  198. /* Disable interrupts unconditionally */
  199. static inline void up_irq_disable(void)
  200. {
  201. asm volatile("cli": : :"memory");
  202. }
  203. /* Enable interrupts unconditionally */
  204. static inline void up_irq_enable(void)
  205. {
  206. asm volatile("sti": : :"memory");
  207. }
  208. /* Disable interrupts, but return previous interrupt state */
  209. static inline irqstate_t up_irq_save(void)
  210. {
  211. irqstate_t flags = irqflags();
  212. up_irq_disable();
  213. return flags;
  214. }
  215. /* Conditionally disable interrupts */
  216. static inline void up_irq_restore(irqstate_t flags)
  217. {
  218. if (up_irq_enabled(flags))
  219. {
  220. up_irq_enable();
  221. }
  222. }
  223. static inline unsigned int up_apic_cpu_id(void)
  224. {
  225. return read_msr(MSR_X2APIC_ID);
  226. }
  227. /****************************************************************************
  228. * Public Data
  229. ****************************************************************************/
  230. /****************************************************************************
  231. * Public Function Prototypes
  232. ****************************************************************************/
  233. void up_ioapic_pin_set_vector(unsigned int pin,
  234. enum ioapic_trigger_mode trigger_mode,
  235. unsigned int vector);
  236. #ifdef __cplusplus
  237. #define EXTERN extern "C"
  238. extern "C"
  239. {
  240. #else
  241. #define EXTERN extern
  242. #endif
  243. #undef EXTERN
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* __ASSEMBLY__ */
  248. #endif /* __ARCH_X86_INCLUDE_I486_IRQ_H */