io.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /****************************************************************************
  2. * arch/x86_64/include/intel64/io.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 arch/io.h
  22. */
  23. #ifndef __ARCH_X86_64_INCLUDE_INTEL64_IO_H
  24. #define __ARCH_X86_64_INCLUDE_INTEL64_IO_H
  25. /****************************************************************************
  26. * Included Files
  27. ****************************************************************************/
  28. #include <stdint.h>
  29. #include <arch/arch.h>
  30. /****************************************************************************
  31. * Pre-processor Definitions
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Public Types
  35. ****************************************************************************/
  36. #ifndef __ASSEMBLY__
  37. /****************************************************************************
  38. * Inline functions
  39. ****************************************************************************/
  40. /* Standard x86 Port I/O */
  41. static inline void outb(uint8_t regval, uint16_t port)
  42. {
  43. asm volatile(
  44. "\toutb %0,%1\n"
  45. :
  46. : "a" (regval), "dN" (port)
  47. );
  48. }
  49. static inline uint8_t inb(uint16_t port)
  50. {
  51. uint8_t regval;
  52. asm volatile(
  53. "\tinb %1,%0\n"
  54. : "=a" (regval)
  55. : "dN" (port)
  56. );
  57. return regval;
  58. }
  59. static inline void outw(uint16_t regval, uint16_t port)
  60. {
  61. asm volatile(
  62. "\toutw %0,%1\n"
  63. :
  64. : "a" (regval), "dN" (port)
  65. );
  66. }
  67. static inline uint16_t inw(uint16_t port)
  68. {
  69. uint16_t regval;
  70. asm volatile(
  71. "\tinw %1,%0\n"
  72. : "=a" (regval)
  73. : "dN" (port)
  74. );
  75. return regval;
  76. }
  77. static inline void outl(uint32_t regval, uint16_t port)
  78. {
  79. asm volatile(
  80. "\toutl %0,%1\n"
  81. :
  82. : "a" (regval), "dN" (port)
  83. );
  84. }
  85. static inline uint32_t inl(uint16_t port)
  86. {
  87. uint32_t regval;
  88. asm volatile(
  89. "\tinl %1,%0\n"
  90. : "=a" (regval)
  91. : "dN" (port)
  92. );
  93. return regval;
  94. }
  95. /* MMIO */
  96. static inline uint8_t mmio_read8(void *address)
  97. {
  98. return *(volatile uint8_t *)address;
  99. }
  100. static inline uint16_t mmio_read16(void *address)
  101. {
  102. return *(volatile uint16_t *)address;
  103. }
  104. static inline uint32_t mmio_read32(void *address)
  105. {
  106. uint32_t value;
  107. /* Assembly-encoded to match the hypervisor MMIO parser support */
  108. asm volatile("movl (%1),%0" : "=r" (value) : "r" (address));
  109. return value;
  110. }
  111. static inline uint64_t mmio_read64(void *address)
  112. {
  113. return *(volatile uint64_t *)address;
  114. }
  115. static inline void mmio_write8(void *address, uint8_t value)
  116. {
  117. *(volatile uint8_t *)address = value;
  118. }
  119. static inline void mmio_write16(void *address, uint16_t value)
  120. {
  121. *(volatile uint16_t *)address = value;
  122. }
  123. static inline void mmio_write32(void *address, uint32_t value)
  124. {
  125. /* Assembly-encoded to match the hypervisor MMIO parser support */
  126. asm volatile("movl %0,(%1)" : : "r" (value), "r" (address));
  127. }
  128. static inline void mmio_write64(void *address, uint64_t value)
  129. {
  130. *(volatile uint64_t *)address = value;
  131. }
  132. static inline void up_trash_cpu(void)
  133. {
  134. for (; ; )
  135. {
  136. asm volatile ("cli;hlt;");
  137. }
  138. asm("ud2":::"memory");
  139. }
  140. static inline void up_invalid_TLB(uintptr_t start, uintptr_t end)
  141. {
  142. uintptr_t i;
  143. start = start & PAGE_MASK;
  144. end = (end + PAGE_SIZE - 1) & PAGE_MASK;
  145. for (i = start; i < end; i += PAGE_SIZE)
  146. {
  147. asm("invlpg %0;":: "m"(i):"memory");
  148. }
  149. }
  150. /****************************************************************************
  151. * Public Data
  152. ****************************************************************************/
  153. /****************************************************************************
  154. * Public Function Prototypes
  155. ****************************************************************************/
  156. #ifdef __cplusplus
  157. #define EXTERN extern "C"
  158. extern "C"
  159. {
  160. #else
  161. #define EXTERN extern
  162. #endif
  163. #undef EXTERN
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif /* __ASSEMBLY__ */
  168. #endif /* __ARCH_X86_64_INCLUDE_INTEL64_IO_H */