Author: stefanct Date: Sat Dec 29 20:26:55 2012 New Revision: 1638 URL: http://flashrom.org/trac/flashrom/changeset/1638
Log: Simplify hwaccess.c.
Previously the code was focused on architectures which led to lots of duplicate code and spread the information regarding differences between the architectures accross the file.
With this patch there is a single function header for any function and the differentiation between architectures (and OS where needed) happens in one place for each function. Also, this patch adds simple defines to bundle often used arch and os checks. A central check for unknown architectures and OSes has been added on top.
Signed-off-by: Peter Lemenkov lemenkov@gmail.com Signed-off-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at Acked-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at
Modified: trunk/hwaccess.c
Modified: trunk/hwaccess.c ============================================================================== --- trunk/hwaccess.c Sat Dec 29 16:04:20 2012 (r1637) +++ trunk/hwaccess.c Sat Dec 29 20:26:55 2012 (r1638) @@ -18,6 +18,20 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+#define IS_X86 (defined(__i386__) || defined(__x86_64__) || defined(__amd64__)) +#define IS_MIPS (defined (__mips) || defined (__mips__) || defined (__MIPS__) || defined (mips)) +#define IS_PPC (defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)) +#define IS_ARM (defined (__arm__) || defined (_ARM)) +#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM) +#error Unknown architecture +#endif + +#define IS_BSD (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)) +#define IS_LINUX (defined(__gnu_linux__) || defined(__linux__)) +#if !(IS_BSD || IS_LINUX || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun)) +#error "Unknown operating system" +#endif + #include <stdint.h> #include <string.h> #include <stdlib.h> @@ -32,30 +46,39 @@ #include "flash.h" #include "hwaccess.h"
-#if defined(__i386__) || defined(__x86_64__) +#if IS_X86 && IS_BSD +int io_fd; +#endif
-/* sync primitive is not needed because x86 uses uncached accesses - * which have a strongly ordered memory model. +/* Prevent reordering and/or merging of reads/writes to hardware. + * Such reordering and/or merging would break device accesses which depend on the exact access order. */ static inline void sync_primitive(void) { -} - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -int io_fd; +/* This is needed only on PowerPC because... + * - x86 uses uncached accesses which have a strongly ordered memory model and + * - MIPS uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model + * - ARM uses a strongly ordered memory model for device memories. + */ +#if IS_PPC + asm("eieio" : : : "memory"); #endif +}
-int release_io_perms(void *p) +static int release_io_perms(void *p) { +#if IS_X86 #if defined(__DJGPP__) || defined(__LIBPAYLOAD__) -#else -#if defined (__sun) && (defined(__i386) || defined(__amd64)) + /* Nothing to release */ +#elif defined (__sun) sysi86(SI86V86, V86SC_IOPL, 0); -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__) +#elif IS_BSD close(io_fd); -#else +#elif IS_LINUX iopl(0); #endif +#else +/* PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM. */ #endif return 0; } @@ -63,81 +86,32 @@ /* Get I/O permissions with automatic permission release on shutdown. */ int rget_io_perms(void) { +#if IS_X86 #if defined(__DJGPP__) || defined(__LIBPAYLOAD__) /* We have full permissions by default. */ - return 0; -#else -#if defined (__sun) && (defined(__i386) || defined(__amd64)) +#elif defined (__sun) if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__) +#elif IS_BSD if ((io_fd = open("/dev/io", O_RDWR)) < 0) { -#else +#elif IS_LINUX if (iopl(3) != 0) { #endif msg_perr("ERROR: Could not get I/O privileges (%s).\n" "You need to be root.\n", strerror(errno)); #if defined (__OpenBSD__) - msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " - "and reboot, or reboot into \n"); + msg_perr("Please set securelevel=-1 in /etc/rc.securelevel and reboot, or reboot into \n"); msg_perr("single user mode.\n"); #endif return 1; } else { register_shutdown(release_io_perms, NULL); } - return 0; +#else +/* PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM. */ #endif -} - -#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) - -static inline void sync_primitive(void) -{ - /* Prevent reordering and/or merging of reads/writes to hardware. - * Such reordering and/or merging would break device accesses which - * depend on the exact access order. - */ - ___asm___ volatile ("eieio" : : : "memory"); -} - -/* PCI port I/O is not yet implemented on PowerPC. */ -int rget_io_perms(void) -{ return 0; }
-#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips) - -/* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses - * in mode 2 which has a strongly ordered memory model. - */ -static inline void sync_primitive(void) -{ -} - -/* PCI port I/O is not yet implemented on MIPS. */ -int rget_io_perms(void) -{ - return 0; -} - -#elif defined (__arm__) - -static inline void sync_primitive(void) -{ -} - -int rget_io_perms(void) -{ - return 0; -} - -#else - -#error Unknown architecture - -#endif - void mmio_writeb(uint8_t val, void *addr) { *(volatile uint8_t *) addr = val;