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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <ipxe/io.h>
28#include <ipxe/x86_io.h>
29
30/** @file
31 *
32 * iPXE I/O API for x86
33 *
34 */
35
36/** Threshold for port I/O-mapped addresses
37 *
38 * On x86, port I/O instructions (inb/outb/etc) can take only an 8-bit
39 * or 16-bit address (in %dx). All I/O ports must therefore have a
40 * value in the first 64kB of the address space.
41 *
42 * Virtual addresses below 64kB can never be MMIO addresses:
43 *
44 * - In the UEFI memory model and x86_64 BIOS memory model, virtual
45 * addresses below 64kB are identity-mapped to the corresponding
46 * physical address. Since the first 64kB of address space is
47 * always RAM, no MMIO device can exist within this region.
48 *
49 * - In the i386 BIOS memory model, virtual addresses below 64kB cover
50 * the iPXE binary itself (which starts at address zero). Since the
51 * size of .textdata can never realistically be below 64kB (not
52 * least since the heap alone is 512kB), and since iPXE is placed
53 * into RAM as a contiguous block, no MMIO device can exist within
54 * this region.
55 *
56 * We therefore know that any (virtual) address returned by ioremap()
57 * must be outside the first 64kB of the address space. We can
58 * therefore use this as a threshold to determine whether a given
59 * address is a port I/O address or an MMIO address.
60 */
61#define PIO_THRESHOLD 0x10000
62
63/**
64 * Read from I/O-mapped or memory-mapped device
65 *
66 * @v io_addr I/O address
67 * @ret data Value read
68 */
69#define X86_IOREADX( _api_func, _suffix, _type ) \
70static _type x86_ ## _api_func ( volatile _type *io_addr ) { \
71 if ( ( ( intptr_t ) io_addr ) < PIO_THRESHOLD ) { \
72 return in ## _suffix ( io_addr ); \
73 } else { \
74 return read ## _suffix ( io_addr ); \
75 } \
76}
80
81/**
82 * Write to I/O-mapped or memory-mapped device
83 *
84 * @v data Value to write
85 * @v io_addr I/O address
86 */
87#define X86_IOWRITEX( _api_func, _suffix, _type ) \
88static void x86_ ## _api_func ( _type data, volatile _type *io_addr ) { \
89 if ( ( ( intptr_t ) io_addr ) < PIO_THRESHOLD ) { \
90 out ## _suffix ( data, io_addr ); \
91 } else { \
92 write ## _suffix ( data, io_addr ); \
93 } \
94}
98
99/**
100 * Read 64-bit qword from memory-mapped device
101 *
102 * @v io_addr I/O address
103 * @ret data Value read
104 *
105 * This routine uses MMX instructions.
106 */
107static __unused uint64_t i386_readq ( volatile uint64_t *io_addr ) {
109 __asm__ __volatile__ ( "pushl %%edx\n\t"
110 "pushl %%eax\n\t"
111 "movq (%1), %%mm0\n\t"
112 "movq %%mm0, (%%esp)\n\t"
113 "popl %%eax\n\t"
114 "popl %%edx\n\t"
115 "emms\n\t"
116 : "=A" ( data ) : "r" ( io_addr ) );
117 return data;
118}
119
120/**
121 * Write 64-bit qword to memory-mapped device
122 *
123 * @v data Value to write
124 * @v io_addr I/O address
125 *
126 * This routine uses MMX instructions.
127 */
128static __unused void i386_writeq ( uint64_t data, volatile uint64_t *io_addr ) {
129 __asm__ __volatile__ ( "pushl %%edx\n\t"
130 "pushl %%eax\n\t"
131 "movq (%%esp), %%mm0\n\t"
132 "movq %%mm0, (%1)\n\t"
133 "popl %%eax\n\t"
134 "popl %%edx\n\t"
135 "emms\n\t"
136 : : "A" ( data ), "r" ( io_addr ) );
137}
138
161#ifdef __x86_64__
164#else
167#endif
168PROVIDE_IOAPI ( x86, ioread8, x86_ioread8 );
169PROVIDE_IOAPI ( x86, ioread16, x86_ioread16 );
170PROVIDE_IOAPI ( x86, ioread32, x86_ioread32 );
171PROVIDE_IOAPI ( x86, iowrite8, x86_iowrite8 );
172PROVIDE_IOAPI ( x86, iowrite16, x86_iowrite16 );
173PROVIDE_IOAPI ( x86, iowrite32, x86_iowrite32 );
__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 short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned long long uint64_t
Definition stdint.h:13
unsigned char uint8_t
Definition stdint.h:10
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
iPXE I/O API
#define inw(io_addr)
Definition io.h:292
#define outb(data, io_addr)
Definition io.h:310
#define ioread16(io_addr)
Definition io.h:350
void mb(void)
Memory barrier.
#define outw(data, io_addr)
Definition io.h:320
unsigned long phys_to_bus(unsigned long phys_addr)
Convert physical address to a bus address.
#define inl(io_addr)
Definition io.h:301
#define insw(io_addr, data, count)
Definition io.h:412
#define outl(data, io_addr)
Definition io.h:330
void iodelay(void)
Slow down I/O.
#define outsl(io_addr, data, count)
Definition io.h:459
#define inb(io_addr)
Definition io.h:283
#define insb(io_addr, data, count)
Definition io.h:401
#define PROVIDE_IOAPI(_subsys, _api_func, _func)
Provide an I/O API implementation.
Definition io.h:50
unsigned long bus_to_phys(unsigned long bus_addr)
Convert bus address to a physical address.
#define readq(io_addr)
Definition io.h:234
#define insl(io_addr, data, count)
Definition io.h:423
#define outsw(io_addr, data, count)
Definition io.h:447
#define outsb(io_addr, data, count)
Definition io.h:435
#define ioread32(io_addr)
Definition io.h:360
#define iowrite8(data, io_addr)
Definition io.h:370
#define iowrite16(data, io_addr)
Definition io.h:380
#define writeq(data, io_addr)
Definition io.h:273
#define ioread8(io_addr)
Definition io.h:340
#define PROVIDE_IOAPI_INLINE(_subsys, _api_func)
Provide a static inline I/O API implementation.
Definition io.h:59
#define iowrite32(data, io_addr)
Definition io.h:390
__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")
uint8_t l
Definition registers.h:1
#define readl
Definition w89c840.c:157
#define writel
Definition w89c840.c:160
#define writew
Definition w89c840.c:159
#define readb
Definition w89c840.c:155
#define writeb
Definition w89c840.c:158
#define readw
Definition w89c840.c:156
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:128
static __unused uint64_t i386_readq(volatile uint64_t *io_addr)
Read 64-bit qword from memory-mapped device.
Definition x86_io.c:107
#define X86_IOREADX(_api_func, _suffix, _type)
Read from I/O-mapped or memory-mapped device.
Definition x86_io.c:69
#define X86_IOWRITEX(_api_func, _suffix, _type)
Write to I/O-mapped or memory-mapped device.
Definition x86_io.c:87
iPXE I/O API for x86