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 );
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 ) \
69static _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 ) \
87static 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 */
106static __unused uint64_t i386_readq ( volatile uint64_t *io_addr ) {
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 */
127static __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
160#ifdef __x86_64__
163#else
166#endif
167PROVIDE_IOAPI ( x86, ioread8, x86_ioread8 );
168PROVIDE_IOAPI ( x86, ioread16, x86_ioread16 );
169PROVIDE_IOAPI ( x86, ioread32, x86_ioread32 );
170PROVIDE_IOAPI ( x86, iowrite8, x86_iowrite8 );
171PROVIDE_IOAPI ( x86, iowrite16, x86_iowrite16 );
172PROVIDE_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:573
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
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:127
static __unused uint64_t i386_readq(volatile uint64_t *io_addr)
Read 64-bit qword from memory-mapped device.
Definition x86_io.c:106
#define X86_IOREADX(_api_func, _suffix, _type)
Read from I/O-mapped or memory-mapped device.
Definition x86_io.c:68
#define X86_IOWRITEX(_api_func, _suffix, _type)
Write to I/O-mapped or memory-mapped device.
Definition x86_io.c:86
iPXE I/O API for x86