io.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * SerialICE
  3. *
  4. * Copyright (C) 2009 coresystems GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc.
  18. */
  19. #ifndef IO_H
  20. #define IO_H
  21. /* Memory functions */
  22. static inline u8 read8(unsigned long addr)
  23. {
  24. return *((volatile u8 *)(addr));
  25. }
  26. static inline u16 read16(unsigned long addr)
  27. {
  28. return *((volatile u16 *)(addr));
  29. }
  30. static inline u32 read32(unsigned long addr)
  31. {
  32. return *((volatile u32 *)(addr));
  33. }
  34. static inline void write8(unsigned long addr, u8 value)
  35. {
  36. *((volatile u8 *)(addr)) = value;
  37. }
  38. static inline void write16(unsigned long addr, u16 value)
  39. {
  40. *((volatile u16 *)(addr)) = value;
  41. }
  42. static inline void write32(unsigned long addr, u32 value)
  43. {
  44. *((volatile u32 *)(addr)) = value;
  45. }
  46. /* IO functions */
  47. #if defined( __ROMCC__ ) && !defined (__GNUC__)
  48. static inline void outb(u8 value, u16 port)
  49. {
  50. __builtin_outb(value, port);
  51. }
  52. static inline void outw(u16 value, u16 port)
  53. {
  54. __builtin_outw(value, port);
  55. }
  56. static inline void outl(u32 value, u16 port)
  57. {
  58. __builtin_outl(value, port);
  59. }
  60. static inline u8 inb(u16 port)
  61. {
  62. return __builtin_inb(port);
  63. }
  64. static inline u16 inw(u16 port)
  65. {
  66. return __builtin_inw(port);
  67. }
  68. static inline u32 inl(u16 port)
  69. {
  70. return __builtin_inl(port);
  71. }
  72. #else
  73. static inline void outb(u8 value, u16 port)
  74. {
  75. __asm__ __volatile__("outb %b0, %w1"::"a"(value), "Nd"(port));
  76. }
  77. static inline void outw(u16 value, u16 port)
  78. {
  79. __asm__ __volatile__("outw %w0, %w1"::"a"(value), "Nd"(port));
  80. }
  81. static inline void outl(u32 value, u16 port)
  82. {
  83. __asm__ __volatile__("outl %0, %w1"::"a"(value), "Nd"(port));
  84. }
  85. static inline u8 inb(u16 port)
  86. {
  87. u8 value;
  88. __asm__ __volatile__("inb %w1, %b0":"=a"(value): "Nd"(port));
  89. return value;
  90. }
  91. static inline u16 inw(u16 port)
  92. {
  93. u16 value;
  94. __asm__ __volatile__("inw %w1, %w0":"=a"(value): "Nd"(port));
  95. return value;
  96. }
  97. static inline u32 inl(u16 port)
  98. {
  99. u32 value;
  100. __asm__ __volatile__("inl %w1, %0":"=a"(value): "Nd"(port));
  101. return value;
  102. }
  103. #endif /* __ROMCC__ && !__GNUC__ */
  104. /* MSR functions */
  105. typedef struct { u32 lo, hi; } msr_t;
  106. static inline msr_t rdmsr(u32 index, u32 key)
  107. {
  108. msr_t result;
  109. __asm__ __volatile__ (
  110. "rdmsr"
  111. : "=a" (result.lo), "=d" (result.hi)
  112. : "c" (index), "D" (key)
  113. );
  114. return result;
  115. }
  116. static inline void wrmsr(u32 index, msr_t msr, u32 key)
  117. {
  118. __asm__ __volatile__ (
  119. "wrmsr"
  120. : /* No outputs */
  121. : "c" (index), "a" (msr.lo), "d" (msr.hi), "D" (key)
  122. );
  123. }
  124. /* CPUID functions */
  125. static inline u32 cpuid_eax(u32 op, u32 op2)
  126. {
  127. u32 eax;
  128. __asm__("cpuid"
  129. : "=a" (eax)
  130. : "a" (op), "c" (op2)
  131. : "ebx", "edx" );
  132. return eax;
  133. }
  134. static inline u32 cpuid_ebx(u32 op, u32 op2)
  135. {
  136. u32 eax, ebx;
  137. __asm__("cpuid"
  138. : "=b" (ebx)
  139. : "a" (op), "c" (op2)
  140. : "edx");
  141. return ebx;
  142. }
  143. static inline u32 cpuid_ecx(u32 op, u32 op2)
  144. {
  145. u32 eax, ecx;
  146. __asm__("cpuid"
  147. : "=c" (ecx)
  148. : "a" (op), "c" (op2)
  149. : "ebx", "edx" );
  150. return ecx;
  151. }
  152. static inline u32 cpuid_edx(u32 op, u32 op2)
  153. {
  154. u32 eax, edx;
  155. __asm__("cpuid"
  156. : "=d" (edx)
  157. : "a" (op), "c" (op2)
  158. : "ebx");
  159. return edx;
  160. }
  161. #endif