From d8392851d2d5cfe0a7f79c2dab29219f59affa60 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sat, 13 Jul 2013 12:42:40 +0200 Subject: [PATCH] [linux] Add support for accessing PCI configuration space via /proc/bus/pci Signed-off-by: Michael Brown --- src/arch/x86/core/linux/linux_api.c | 4 + src/config/defaults/linux.h | 1 + src/include/ipxe/errfile.h | 1 + src/include/ipxe/linux.h | 8 ++ src/include/ipxe/linux/linux_pci.h | 130 +++++++++++++++++++ src/include/ipxe/pci_io.h | 1 + src/include/linux_api.h | 2 + src/interface/linux/linux_pci.c | 185 ++++++++++++++++++++++++++++ 8 files changed, 332 insertions(+) create mode 100644 src/include/ipxe/linux/linux_pci.h create mode 100644 src/interface/linux/linux_pci.c diff --git a/src/arch/x86/core/linux/linux_api.c b/src/arch/x86/core/linux/linux_api.c index c8a09b7d8..0bed9fd57 100644 --- a/src/arch/x86/core/linux/linux_api.c +++ b/src/arch/x86/core/linux/linux_api.c @@ -37,6 +37,10 @@ int linux_close ( int fd ) { return linux_syscall ( __NR_close, fd ); } +off_t linux_lseek ( int fd, off_t offset, int whence ) { + return linux_syscall ( __NR_lseek, fd, offset, whence ); +} + __kernel_ssize_t linux_read ( int fd, void *buf, __kernel_size_t count ) { return linux_syscall ( __NR_read, fd, buf, count ); } diff --git a/src/config/defaults/linux.h b/src/config/defaults/linux.h index 666db6b82..2b565943f 100644 --- a/src/config/defaults/linux.h +++ b/src/config/defaults/linux.h @@ -17,6 +17,7 @@ #define ENTROPY_LINUX #define TIME_LINUX #define REBOOT_NULL +#define PCIAPI_LINUX #define DRIVERS_LINUX diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h index 67edcc931..2b9d16ca0 100644 --- a/src/include/ipxe/errfile.h +++ b/src/include/ipxe/errfile.h @@ -275,6 +275,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); #define ERRFILE_efi_init ( ERRFILE_OTHER | 0x00390000 ) #define ERRFILE_efi_timer ( ERRFILE_OTHER | 0x003a0000 ) #define ERRFILE_efi_umalloc ( ERRFILE_OTHER | 0x003b0000 ) +#define ERRFILE_linux_pci ( ERRFILE_OTHER | 0x003c0000 ) /** @} */ diff --git a/src/include/ipxe/linux.h b/src/include/ipxe/linux.h index dac508ea6..a01ace3de 100644 --- a/src/include/ipxe/linux.h +++ b/src/include/ipxe/linux.h @@ -30,6 +30,14 @@ FILE_LICENCE(GPL2_OR_LATER); #include #include +/** + * Convert a Linux error number to an iPXE status code + * + * @v errno Linux error number + * @ret rc iPXE status code (before negation) + */ +#define ELINUX( errno ) EPLATFORM ( EINFO_EPLATFORM, errno ) + /** A linux device */ struct linux_device { /** Generic device */ diff --git a/src/include/ipxe/linux/linux_pci.h b/src/include/ipxe/linux/linux_pci.h new file mode 100644 index 000000000..439166733 --- /dev/null +++ b/src/include/ipxe/linux/linux_pci.h @@ -0,0 +1,130 @@ +#ifndef _IPXE_LINUX_PCI_H +#define _IPXE_LINUX_PCI_H + +/** @file + * + * iPXE PCI API for Linux + * + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#ifdef PCIAPI_LINUX +#define PCIAPI_PREFIX_linux +#else +#define PCIAPI_PREFIX_linux __linux_ +#endif + +struct pci_device; + +extern int linux_pci_read ( struct pci_device *pci, unsigned long where, + unsigned long *value, size_t len ); +extern int linux_pci_write ( struct pci_device *pci, unsigned long where, + unsigned long value, size_t len ); + +/** + * Read byte from PCI configuration space + * + * @v pci PCI device + * @v where Location within PCI configuration space + * @v value Value read + * @ret rc Return status code + */ +static inline __always_inline int +PCIAPI_INLINE ( linux, pci_read_config_byte ) ( struct pci_device *pci, + unsigned int where, + uint8_t *value ) { + int rc; + unsigned long tmp; + + rc = linux_pci_read ( pci, where, &tmp, sizeof ( *value ) ); + *value = tmp; + return rc; +} + +/** + * Read word from PCI configuration space + * + * @v pci PCI device + * @v where Location within PCI configuration space + * @v value Value read + * @ret rc Return status code + */ +static inline __always_inline int +PCIAPI_INLINE ( linux, pci_read_config_word ) ( struct pci_device *pci, + unsigned int where, + uint16_t *value ) { + int rc; + unsigned long tmp; + + rc = linux_pci_read ( pci, where, &tmp, sizeof ( *value ) ); + *value = tmp; + return rc; +} + +/** + * Read dword from PCI configuration space + * + * @v pci PCI device + * @v where Location within PCI configuration space + * @v value Value read + * @ret rc Return status code + */ +static inline __always_inline int +PCIAPI_INLINE ( linux, pci_read_config_dword ) ( struct pci_device *pci, + unsigned int where, + uint32_t *value ) { + int rc; + unsigned long tmp; + + rc = linux_pci_read ( pci, where, &tmp, sizeof ( *value ) ); + *value = tmp; + return rc; +} + +/** + * Write byte to PCI configuration space + * + * @v pci PCI device + * @v where Location within PCI configuration space + * @v value Value to be written + * @ret rc Return status code + */ +static inline __always_inline int +PCIAPI_INLINE ( linux, pci_write_config_byte ) ( struct pci_device *pci, + unsigned int where, + uint8_t value ) { + return linux_pci_write ( pci, where, value, sizeof ( value ) ); +} + +/** + * Write word to PCI configuration space + * + * @v pci PCI device + * @v where Location within PCI configuration space + * @v value Value to be written + * @ret rc Return status code + */ +static inline __always_inline int +PCIAPI_INLINE ( linux, pci_write_config_word ) ( struct pci_device *pci, + unsigned int where, + uint16_t value ) { + return linux_pci_write ( pci, where, value, sizeof ( value ) ); +} + +/** + * Write dword to PCI configuration space + * + * @v pci PCI device + * @v where Location within PCI configuration space + * @v value Value to be written + * @ret rc Return status code + */ +static inline __always_inline int +PCIAPI_INLINE ( linux, pci_write_config_dword ) ( struct pci_device *pci, + unsigned int where, + uint32_t value ) { + return linux_pci_write ( pci, where, value, sizeof ( value ) ); +} + +#endif /* _IPXE_LINUX_PCI_H */ diff --git a/src/include/ipxe/pci_io.h b/src/include/ipxe/pci_io.h index 7368cf48b..781b77fe1 100644 --- a/src/include/ipxe/pci_io.h +++ b/src/include/ipxe/pci_io.h @@ -44,6 +44,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); /* Include all architecture-independent I/O API headers */ #include +#include /* Include all architecture-dependent I/O API headers */ #include diff --git a/src/include/linux_api.h b/src/include/linux_api.h index 94dc991f0..28a3cda3b 100644 --- a/src/include/linux_api.h +++ b/src/include/linux_api.h @@ -47,11 +47,13 @@ typedef __kernel_loff_t loff_t; typedef unsigned long nfds_t; typedef uint32_t useconds_t; #define MAP_FAILED ( ( void * ) -1 ) +#define SEEK_SET 0 extern long linux_syscall ( int number, ... ); extern int linux_open ( const char *pathname, int flags ); extern int linux_close ( int fd ); +extern off_t linux_lseek ( int fd, off_t offset, int whence ); extern __kernel_ssize_t linux_read ( int fd, void *buf, __kernel_size_t count ); extern __kernel_ssize_t linux_write ( int fd, const void *buf, __kernel_size_t count ); diff --git a/src/interface/linux/linux_pci.c b/src/interface/linux/linux_pci.c new file mode 100644 index 000000000..cbd825c18 --- /dev/null +++ b/src/interface/linux/linux_pci.c @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2013 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include +#include +#include + +/** @file + * + * iPXE PCI API for Linux + * + */ + +/** + * Open PCI configuration space + * + * @v pci PCI device + * @v flags Access mode flags + * @v where Address within configuration space + * @ret fd File handle, or negative error + */ +static int linux_pci_open ( struct pci_device *pci, int flags, + unsigned long where ) { + char filename[ 22 /* "/proc/bus/pci/xx/xx.x" + NUL */ ]; + int fd; + int rc; + + /* Construct filename */ + snprintf ( filename, sizeof ( filename ), "/proc/bus/pci/%02x/%02x.%x", + PCI_BUS ( pci->busdevfn ), PCI_SLOT ( pci->busdevfn ), + PCI_FUNC ( pci->busdevfn ) ); + + /* Open file */ + fd = linux_open ( filename, flags ); + if ( fd < 0 ) { + DBGC ( pci, "PCI could not open %s: %s\n", filename, + linux_strerror ( linux_errno ) ); + rc = -ELINUX ( linux_errno ); + goto err_open; + } + + /* Seek to location */ + if ( linux_lseek ( fd, where, SEEK_SET ) < 0 ) { + DBGC ( pci, "PCI could not seek to %s offset %#02lx: %s\n", + filename, where, linux_strerror ( linux_errno ) ); + rc = -ELINUX ( linux_errno ); + goto err_seek; + } + + return fd; + + err_seek: + linux_close ( fd ); + err_open: + return rc; +} + +/** + * Read from PCI configuration space + * + * @v pci PCI device + * @v where Address within configuration space + * @v value Data buffer + * @v len Length to read + * @ret rc Return status code + */ +int linux_pci_read ( struct pci_device *pci, unsigned long where, + unsigned long *value, size_t len ) { + uint32_t tmp = 0; + int fd; + int check_len; + int rc; + + /* Return "missing device" in case of error */ + *value = -1UL; + + /* Open configuration space */ + fd = linux_pci_open ( pci, O_RDONLY, where ); + if ( fd < 0 ) { + rc = fd; + goto err_open; + } + + /* Read value */ + check_len = linux_read ( fd, &tmp, len ); + if ( check_len < 0 ) { + DBGC ( pci, "PCI could not read from " PCI_FMT " %#02lx+%#zx: " + "%s\n", PCI_ARGS ( pci ), where, len, + linux_strerror ( linux_errno ) ); + rc = -ELINUX ( linux_errno ); + goto err_read; + } + if ( ( size_t ) check_len != len ) { + DBGC ( pci, "PCI read only %#x bytes from " PCI_FMT + " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ), + where, len ); + rc = -EIO; + goto err_read; + } + + /* Return value */ + *value = le32_to_cpu ( tmp ); + + /* Success */ + rc = 0; + + err_read: + linux_close ( fd ); + err_open: + return rc; +} + +/** + * Write to PCI configuration space + * + * @v pci PCI device + * @v where Address within configuration space + * @v value Value to write + * @v len Length of value + * @ret rc Return status code + */ +int linux_pci_write ( struct pci_device *pci, unsigned long where, + unsigned long value, size_t len ) { + uint32_t tmp; + int fd; + int check_len; + int rc; + + /* Open configuration space */ + fd = linux_pci_open ( pci, O_WRONLY, where ); + if ( fd < 0 ) { + rc = fd; + goto err_open; + } + + /* Prepare value for writing */ + tmp = cpu_to_le32 ( value ); + assert ( len <= sizeof ( tmp ) ); + + /* Write value */ + check_len = linux_write ( fd, &tmp, len ); + if ( check_len < 0 ) { + DBGC ( pci, "PCI could not write to " PCI_FMT " %#02lx+%#zx: " + "%s\n", PCI_ARGS ( pci ), where, len, + linux_strerror ( linux_errno ) ); + rc = -ELINUX ( linux_errno ); + goto err_write; + } + if ( ( size_t ) check_len != len ) { + DBGC ( pci, "PCI wrote only %#x bytes to " PCI_FMT + " %#02lx+%#zx\n", check_len, PCI_ARGS ( pci ), + where, len ); + rc = -EIO; + goto err_write; + } + + /* Success */ + rc = 0; + + err_write: + linux_close ( fd ); + err_open: + return rc; +}