1 /*
2 * The PCI Library -- Compiler-specific wrappers around x86 I/O port access instructions
3 *
4 * Copyright (c) 2023 Pali Rohár <[email protected]>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL v2+
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #if defined(__GNUC__)
12
13 static inline unsigned char
intel_inb(unsigned short int port)14 intel_inb(unsigned short int port)
15 {
16 unsigned char value;
17 asm volatile ("inb %w1, %0" : "=a" (value) : "Nd" (port));
18 return value;
19 }
20
21 static inline unsigned short int
intel_inw(unsigned short int port)22 intel_inw(unsigned short int port)
23 {
24 unsigned short value;
25 asm volatile ("inw %w1, %0" : "=a" (value) : "Nd" (port));
26 return value;
27 }
28
29 static inline unsigned int
intel_inl(unsigned short int port)30 intel_inl(unsigned short int port)
31 {
32 u32 value;
33 asm volatile ("inl %w1, %0" : "=a" (value) : "Nd" (port));
34 return value;
35 }
36
37 static inline void
intel_outb(unsigned char value,unsigned short int port)38 intel_outb(unsigned char value, unsigned short int port)
39 {
40 asm volatile ("outb %b0, %w1" : : "a" (value), "Nd" (port));
41 }
42
43 static inline void
intel_outw(unsigned short int value,unsigned short int port)44 intel_outw(unsigned short int value, unsigned short int port)
45 {
46 asm volatile ("outw %w0, %w1" : : "a" (value), "Nd" (port));
47 }
48
49 static inline void
intel_outl(u32 value,unsigned short int port)50 intel_outl(u32 value, unsigned short int port)
51 {
52 asm volatile ("outl %0, %w1" : : "a" (value), "Nd" (port));
53 }
54
55 #elif defined(_MSC_VER)
56
57 #pragma intrinsic(_outp)
58 #pragma intrinsic(_outpw)
59 #pragma intrinsic(_outpd)
60 #pragma intrinsic(_inp)
61 #pragma intrinsic(_inpw)
62 #pragma intrinsic(_inpd)
63
64 #define intel_outb(x, y) _outp(y, x)
65 #define intel_outw(x, y) _outpw(y, x)
66 #define intel_outl(x, y) _outpd(y, x)
67 #define intel_inb(x) _inp(x)
68 #define intel_inw(x) _inpw(x)
69 #define intel_inl(x) _inpd(x)
70
71 #else
72
73 #error Do not know how to access I/O ports on this compiler
74
75 #endif
76