iPXE
x86_io.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <ipxe/io.h>
27 #include <ipxe/x86_io.h>
28 
29 /** @file
30  *
31  * iPXE I/O API for x86
32  *
33  */
34 
35 /** Threshold for port I/O-mapped addresses
36  *
37  * On x86, port I/O instructions (inb/outb/etc) can take only an 8-bit
38  * or 16-bit address (in %dx). All I/O ports must therefore have a
39  * value in the first 64kB of the address space.
40  *
41  * Virtual addresses below 64kB can never be MMIO addresses:
42  *
43  * - In the UEFI memory model and x86_64 BIOS memory model, virtual
44  * addresses below 64kB are identity-mapped to the corresponding
45  * physical address. Since the first 64kB of address space is
46  * always RAM, no MMIO device can exist within this region.
47  *
48  * - In the i386 BIOS memory model, virtual addresses below 64kB cover
49  * the iPXE binary itself (which starts at address zero). Since the
50  * size of .textdata can never realistically be below 64kB (not
51  * least since the heap alone is 512kB), and since iPXE is placed
52  * into RAM as a contiguous block, no MMIO device can exist within
53  * this region.
54  *
55  * We therefore know that any (virtual) address returned by ioremap()
56  * must be outside the first 64kB of the address space. We can
57  * therefore use this as a threshold to determine whether a given
58  * address is a port I/O address or an MMIO address.
59  */
60 #define PIO_THRESHOLD 0x10000
61 
62 /**
63  * Read from I/O-mapped or memory-mapped device
64  *
65  * @v io_addr I/O address
66  * @ret data Value read
67  */
68 #define X86_IOREADX( _api_func, _suffix, _type ) \
69 static _type x86_ ## _api_func ( volatile _type *io_addr ) { \
70  if ( ( ( intptr_t ) io_addr ) < PIO_THRESHOLD ) { \
71  return in ## _suffix ( io_addr ); \
72  } else { \
73  return read ## _suffix ( io_addr ); \
74  } \
75 }
79 
80 /**
81  * Write to I/O-mapped or memory-mapped device
82  *
83  * @v data Value to write
84  * @v io_addr I/O address
85  */
86 #define X86_IOWRITEX( _api_func, _suffix, _type ) \
87 static void x86_ ## _api_func ( _type data, volatile _type *io_addr ) { \
88  if ( ( ( intptr_t ) io_addr ) < PIO_THRESHOLD ) { \
89  out ## _suffix ( data, io_addr ); \
90  } else { \
91  write ## _suffix ( data, io_addr ); \
92  } \
93 }
97 
98 /**
99  * Read 64-bit qword from memory-mapped device
100  *
101  * @v io_addr I/O address
102  * @ret data Value read
103  *
104  * This routine uses MMX instructions.
105  */
106 static __unused uint64_t i386_readq ( volatile uint64_t *io_addr ) {
107  uint64_t data;
108  __asm__ __volatile__ ( "pushl %%edx\n\t"
109  "pushl %%eax\n\t"
110  "movq (%1), %%mm0\n\t"
111  "movq %%mm0, (%%esp)\n\t"
112  "popl %%eax\n\t"
113  "popl %%edx\n\t"
114  "emms\n\t"
115  : "=A" ( data ) : "r" ( io_addr ) );
116  return data;
117 }
118 
119 /**
120  * Write 64-bit qword to memory-mapped device
121  *
122  * @v data Value to write
123  * @v io_addr I/O address
124  *
125  * This routine uses MMX instructions.
126  */
127 static __unused void i386_writeq ( uint64_t data, volatile uint64_t *io_addr ) {
128  __asm__ __volatile__ ( "pushl %%edx\n\t"
129  "pushl %%eax\n\t"
130  "movq (%%esp), %%mm0\n\t"
131  "movq %%mm0, (%1)\n\t"
132  "popl %%eax\n\t"
133  "popl %%edx\n\t"
134  "emms\n\t"
135  : : "A" ( data ), "r" ( io_addr ) );
136 }
137 
140 PROVIDE_IOAPI_INLINE ( x86, readb );
141 PROVIDE_IOAPI_INLINE ( x86, readw );
142 PROVIDE_IOAPI_INLINE ( x86, readl );
146 PROVIDE_IOAPI_INLINE ( x86, inb );
147 PROVIDE_IOAPI_INLINE ( x86, inw );
148 PROVIDE_IOAPI_INLINE ( x86, inl );
149 PROVIDE_IOAPI_INLINE ( x86, outb );
150 PROVIDE_IOAPI_INLINE ( x86, outw );
151 PROVIDE_IOAPI_INLINE ( x86, outl );
152 PROVIDE_IOAPI_INLINE ( x86, insb );
153 PROVIDE_IOAPI_INLINE ( x86, insw );
154 PROVIDE_IOAPI_INLINE ( x86, insl );
155 PROVIDE_IOAPI_INLINE ( x86, outsb );
156 PROVIDE_IOAPI_INLINE ( x86, outsw );
157 PROVIDE_IOAPI_INLINE ( x86, outsl );
159 PROVIDE_IOAPI_INLINE ( x86, mb );
160 #ifdef __x86_64__
161 PROVIDE_IOAPI_INLINE ( x86, readq );
163 #else
164 PROVIDE_IOAPI ( x86, readq, i386_readq );
166 #endif
167 PROVIDE_IOAPI ( x86, ioread8, x86_ioread8 );
168 PROVIDE_IOAPI ( x86, ioread16, x86_ioread16 );
169 PROVIDE_IOAPI ( x86, ioread32, x86_ioread32 );
170 PROVIDE_IOAPI ( x86, iowrite8, x86_iowrite8 );
171 PROVIDE_IOAPI ( x86, iowrite16, x86_iowrite16 );
172 PROVIDE_IOAPI ( x86, iowrite32, x86_iowrite32 );
iPXE I/O API
unsigned short uint16_t
Definition: stdint.h:11
uint8_t l
Definition: registers.h:15
uint8_t readb(volatile uint8_t *io_addr)
Read byte from memory-mapped device.
uint16_t inw(volatile uint16_t *io_addr)
Read 16-bit word from I/O-mapped device.
#define outw(data, io_addr)
Definition: io.h:319
uint64_t readq(volatile uint64_t *io_addr)
Read 64-bit qword from memory-mapped device.
uint16_t readw(volatile uint16_t *io_addr)
Read 16-bit word from memory-mapped device.
uint32_t readl(volatile uint32_t *io_addr)
Read 32-bit dword from memory-mapped device.
void outsw(volatile uint16_t *io_addr, const uint16_t *data, unsigned int count)
Write 16-bit words to I/O-mapped device.
unsigned long long uint64_t
Definition: stdint.h:13
void insb(volatile uint8_t *io_addr, uint8_t *data, unsigned int count)
Read bytes from I/O-mapped device.
uint8_t ioread8(volatile uint8_t *io_addr)
Read byte from I/O-mapped or memory-mapped device.
void writeb(uint8_t data, volatile uint8_t *io_addr)
Write byte to memory-mapped device.
PROVIDE_IOAPI_INLINE(x86, phys_to_bus)
uint32_t ioread32(volatile uint32_t *io_addr)
Read 32-bit dword from I/O-mapped or memory-mapped device.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
void writel(uint32_t data, volatile uint32_t *io_addr)
Write 32-bit dword to memory-mapped device.
static __unused void i386_writeq(uint64_t data, volatile uint64_t *io_addr)
Write 64-bit qword to memory-mapped device.
Definition: x86_io.c:127
iPXE I/O API for x86
void insl(volatile uint32_t *io_addr, uint32_t *data, unsigned int count)
Read 32-bit words from I/O-mapped device.
void iowrite16(uint16_t data, volatile uint16_t *io_addr)
Write 16-bit word to I/O-mapped or memory-mapped device.
void iowrite8(uint8_t data, volatile uint8_t *io_addr)
Write byte to I/O-mapped or memory-mapped device.
#define outl(data, io_addr)
Definition: io.h:329
__asm__ __volatile__("call *%9" :"=a"(result), "=c"(discard_ecx), "=d"(discard_edx) :"d"(0), "a"(code), "b"(0), "c"(in_phys), "D"(0), "S"(out_phys), "m"(hypercall))
unsigned char uint8_t
Definition: stdint.h:10
void insw(volatile uint16_t *io_addr, uint16_t *data, unsigned int count)
Read 16-bit words from I/O-mapped device.
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
unsigned int uint32_t
Definition: stdint.h:12
unsigned long phys_to_bus(unsigned long phys_addr)
Convert physical address to a bus address.
void outsb(volatile uint8_t *io_addr, const uint8_t *data, unsigned int count)
Write bytes to I/O-mapped device.
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define outb(data, io_addr)
Definition: io.h:309
#define writew
Definition: w89c840.c:159
#define X86_IOWRITEX(_api_func, _suffix, _type)
Write to I/O-mapped or memory-mapped device.
Definition: x86_io.c:86
uint32_t inl(volatile uint32_t *io_addr)
Read 32-bit dword from I/O-mapped device.
static __unused uint64_t i386_readq(volatile uint64_t *io_addr)
Read 64-bit qword from memory-mapped device.
Definition: x86_io.c:106
uint8_t data[48]
Additional event data.
Definition: ena.h:22
void outsl(volatile uint32_t *io_addr, const uint32_t *data, unsigned int count)
Write 32-bit words to I/O-mapped device.
void mb(void)
Memory barrier.
#define X86_IOREADX(_api_func, _suffix, _type)
Read from I/O-mapped or memory-mapped device.
Definition: x86_io.c:68
void iodelay(void)
Slow down I/O.
unsigned long bus_to_phys(unsigned long bus_addr)
Convert bus address to a physical address.
void writeq(uint64_t data, volatile uint64_t *io_addr)
Write 64-bit qword to memory-mapped device.
void iowrite32(uint32_t data, volatile uint32_t *io_addr)
Write 32-bit dword to I/O-mapped or memory-mapped device.
uint16_t ioread16(volatile uint16_t *io_addr)
Read 16-bit word from I/O-mapped or memory-mapped device.
PROVIDE_IOAPI(x86, readq, i386_readq)