Commit 976e661e authored by Niclas Zeising's avatar Niclas Zeising
Browse files

freebsd_pci: Make this work on sparc64

Make the FreeBSD libpciaccess routines work on FreeBSD.  The FreeBSD
sparc64 architecture needs special treatment, similar to the
PCI_MAGIC_IO_RANGE code.  However, PCI_MAGIC_IO_RANGE has been gone for
some time.  Give this a chance to work on sparc64 anyway.

This code has been in the FreeBSD ports tree for some time.
parent a1b528c4
...@@ -77,7 +77,9 @@ pci_system_init( void ) ...@@ -77,7 +77,9 @@ pci_system_init( void )
void void
pci_system_init_dev_mem(int fd) pci_system_init_dev_mem(int fd)
{ {
#ifdef __OpenBSD__ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
pci_system_freebsd_init_dev_mem(fd);
#elif defined(__OpenBSD__)
pci_system_openbsd_init_dev_mem(fd); pci_system_openbsd_init_dev_mem(fd);
#endif #endif
} }
......
...@@ -82,6 +82,10 @@ ...@@ -82,6 +82,10 @@
#define PCIM_BAR_MEM_SPACE 0 #define PCIM_BAR_MEM_SPACE 0
#define PCIM_BAR_IO_SPACE 1 #define PCIM_BAR_IO_SPACE 1
#if defined(__sparc64__)
static int screenfd;
#endif
/** /**
* FreeBSD private pci_system structure that extends the base pci_system * FreeBSD private pci_system structure that extends the base pci_system
* structure. * structure.
...@@ -113,12 +117,18 @@ pci_device_freebsd_map_range( struct pci_device *dev, ...@@ -113,12 +117,18 @@ pci_device_freebsd_map_range( struct pci_device *dev,
{ {
const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0) const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0)
? (PROT_READ | PROT_WRITE) : PROT_READ; ? (PROT_READ | PROT_WRITE) : PROT_READ;
#if !defined(__sparc64__)
struct mem_range_desc mrd; struct mem_range_desc mrd;
struct mem_range_op mro; struct mem_range_op mro;
#endif
int fd, err = 0; int fd, err = 0;
#if defined(__sparc64__)
fd = screenfd;
#else
fd = open("/dev/mem", O_RDWR | O_CLOEXEC); fd = open("/dev/mem", O_RDWR | O_CLOEXEC);
#endif
if (fd == -1) if (fd == -1)
return errno; return errno;
...@@ -128,6 +138,7 @@ pci_device_freebsd_map_range( struct pci_device *dev, ...@@ -128,6 +138,7 @@ pci_device_freebsd_map_range( struct pci_device *dev,
err = errno; err = errno;
} }
#if !defined(__sparc64__)
mrd.mr_base = map->base; mrd.mr_base = map->base;
mrd.mr_len = map->size; mrd.mr_len = map->size;
strncpy(mrd.mr_owner, "pciaccess", sizeof(mrd.mr_owner)); strncpy(mrd.mr_owner, "pciaccess", sizeof(mrd.mr_owner));
...@@ -148,6 +159,7 @@ pci_device_freebsd_map_range( struct pci_device *dev, ...@@ -148,6 +159,7 @@ pci_device_freebsd_map_range( struct pci_device *dev,
} }
close(fd); close(fd);
#endif
return err; return err;
} }
...@@ -156,6 +168,7 @@ static int ...@@ -156,6 +168,7 @@ static int
pci_device_freebsd_unmap_range( struct pci_device *dev, pci_device_freebsd_unmap_range( struct pci_device *dev,
struct pci_device_mapping *map ) struct pci_device_mapping *map )
{ {
#if !defined(__sparc64__)
struct mem_range_desc mrd; struct mem_range_desc mrd;
struct mem_range_op mro; struct mem_range_op mro;
int fd; int fd;
...@@ -181,6 +194,7 @@ pci_device_freebsd_unmap_range( struct pci_device *dev, ...@@ -181,6 +194,7 @@ pci_device_freebsd_unmap_range( struct pci_device *dev,
fprintf(stderr, "Failed to open /dev/mem\n"); fprintf(stderr, "Failed to open /dev/mem\n");
} }
} }
#endif
return pci_device_generic_unmap_range(dev, map); return pci_device_generic_unmap_range(dev, map);
} }
...@@ -299,7 +313,11 @@ pci_device_freebsd_read_rom( struct pci_device * dev, void * buffer ) ...@@ -299,7 +313,11 @@ pci_device_freebsd_read_rom( struct pci_device * dev, void * buffer )
} }
printf("Using rom_base = 0x%lx\n", (long)rom_base); printf("Using rom_base = 0x%lx\n", (long)rom_base);
#if defined(__sparc64__)
memfd = screenfd;
#else
memfd = open( "/dev/mem", O_RDONLY | O_CLOEXEC ); memfd = open( "/dev/mem", O_RDONLY | O_CLOEXEC );
#endif
if ( memfd == -1 ) if ( memfd == -1 )
return errno; return errno;
...@@ -312,7 +330,9 @@ pci_device_freebsd_read_rom( struct pci_device * dev, void * buffer ) ...@@ -312,7 +330,9 @@ pci_device_freebsd_read_rom( struct pci_device * dev, void * buffer )
memcpy( buffer, bios, dev->rom_size ); memcpy( buffer, bios, dev->rom_size );
munmap( bios, dev->rom_size ); munmap( bios, dev->rom_size );
#if !defined(__sparc64__)
close( memfd ); close( memfd );
#endif
if (pci_rom) { if (pci_rom) {
pci_device_cfg_write_u32( dev, PCIR_BIOS, rom ); pci_device_cfg_write_u32( dev, PCIR_BIOS, rom );
...@@ -570,9 +590,9 @@ pci_device_freebsd_open_legacy_io( struct pci_io_handle *ret, ...@@ -570,9 +590,9 @@ pci_device_freebsd_open_legacy_io( struct pci_io_handle *ret,
struct pci_device *dev, pciaddr_t base, struct pci_device *dev, pciaddr_t base,
pciaddr_t size ) pciaddr_t size )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
ret->memory = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ret->memory = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
aperturefd, PCI_MAGIC_IO_RANGE + base ); screenfd, base );
if ( ret->memory == MAP_FAILED ) if ( ret->memory == MAP_FAILED )
return NULL; return NULL;
#else #else
...@@ -601,14 +621,16 @@ static void ...@@ -601,14 +621,16 @@ static void
pci_device_freebsd_close_io( struct pci_device *dev, pci_device_freebsd_close_io( struct pci_device *dev,
struct pci_io_handle *handle ) struct pci_io_handle *handle )
{ {
#if !defined(__sparc64__)
if ( handle->fd > -1 ) if ( handle->fd > -1 )
close( handle->fd ); close( handle->fd );
#endif
} }
static uint32_t static uint32_t
pci_device_freebsd_read32( struct pci_io_handle *handle, uint32_t reg ) pci_device_freebsd_read32( struct pci_io_handle *handle, uint32_t reg )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
return *(uint32_t *)((uintptr_t)handle->memory + reg); return *(uint32_t *)((uintptr_t)handle->memory + reg);
#elif defined(__i386__) || defined(__amd64__) #elif defined(__i386__) || defined(__amd64__)
return inl( handle->base + reg ); return inl( handle->base + reg );
...@@ -623,7 +645,7 @@ pci_device_freebsd_read32( struct pci_io_handle *handle, uint32_t reg ) ...@@ -623,7 +645,7 @@ pci_device_freebsd_read32( struct pci_io_handle *handle, uint32_t reg )
static uint16_t static uint16_t
pci_device_freebsd_read16( struct pci_io_handle *handle, uint32_t reg ) pci_device_freebsd_read16( struct pci_io_handle *handle, uint32_t reg )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
return *(uint16_t *)((uintptr_t)handle->memory + reg); return *(uint16_t *)((uintptr_t)handle->memory + reg);
#elif defined(__i386__) || defined(__amd64__) #elif defined(__i386__) || defined(__amd64__)
return inw( handle->base + reg ); return inw( handle->base + reg );
...@@ -638,7 +660,7 @@ pci_device_freebsd_read16( struct pci_io_handle *handle, uint32_t reg ) ...@@ -638,7 +660,7 @@ pci_device_freebsd_read16( struct pci_io_handle *handle, uint32_t reg )
static uint8_t static uint8_t
pci_device_freebsd_read8( struct pci_io_handle *handle, uint32_t reg ) pci_device_freebsd_read8( struct pci_io_handle *handle, uint32_t reg )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
return *(uint8_t *)((uintptr_t)handle->memory + reg); return *(uint8_t *)((uintptr_t)handle->memory + reg);
#elif defined(__i386__) || defined(__amd64__) #elif defined(__i386__) || defined(__amd64__)
return inb( handle->base + reg ); return inb( handle->base + reg );
...@@ -654,7 +676,7 @@ static void ...@@ -654,7 +676,7 @@ static void
pci_device_freebsd_write32( struct pci_io_handle *handle, uint32_t reg, pci_device_freebsd_write32( struct pci_io_handle *handle, uint32_t reg,
uint32_t data ) uint32_t data )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
*(uint32_t *)((uintptr_t)handle->memory + reg) = data; *(uint32_t *)((uintptr_t)handle->memory + reg) = data;
#elif defined(__i386__) || defined(__amd64__) #elif defined(__i386__) || defined(__amd64__)
outl( handle->base + reg, data ); outl( handle->base + reg, data );
...@@ -669,7 +691,7 @@ static void ...@@ -669,7 +691,7 @@ static void
pci_device_freebsd_write16( struct pci_io_handle *handle, uint32_t reg, pci_device_freebsd_write16( struct pci_io_handle *handle, uint32_t reg,
uint16_t data ) uint16_t data )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
*(uint16_t *)((uintptr_t)handle->memory + reg) = data; *(uint16_t *)((uintptr_t)handle->memory + reg) = data;
#elif defined(__i386__) || defined(__amd64__) #elif defined(__i386__) || defined(__amd64__)
outw( handle->base + reg, data ); outw( handle->base + reg, data );
...@@ -684,7 +706,7 @@ static void ...@@ -684,7 +706,7 @@ static void
pci_device_freebsd_write8( struct pci_io_handle *handle, uint32_t reg, pci_device_freebsd_write8( struct pci_io_handle *handle, uint32_t reg,
uint8_t data ) uint8_t data )
{ {
#if defined(PCI_MAGIC_IO_RANGE) #if defined(__sparc64__)
*(uint8_t *)((uintptr_t)handle->memory + reg) = data; *(uint8_t *)((uintptr_t)handle->memory + reg) = data;
#elif defined(__i386__) || defined(__amd64__) #elif defined(__i386__) || defined(__amd64__)
outb( handle->base + reg, data ); outb( handle->base + reg, data );
...@@ -814,3 +836,11 @@ pci_system_freebsd_create( void ) ...@@ -814,3 +836,11 @@ pci_system_freebsd_create( void )
return 0; return 0;
} }
void
pci_system_freebsd_init_dev_mem(int fd)
{
#if defined(__sparc64__)
screenfd = fd;
#endif
}
...@@ -185,6 +185,7 @@ extern struct pci_system * pci_sys; ...@@ -185,6 +185,7 @@ extern struct pci_system * pci_sys;
extern int pci_system_linux_sysfs_create( void ); extern int pci_system_linux_sysfs_create( void );
extern int pci_system_freebsd_create( void ); extern int pci_system_freebsd_create( void );
extern void pci_system_freebsd_init_dev_mem( int );
extern int pci_system_netbsd_create( void ); extern int pci_system_netbsd_create( void );
extern int pci_system_openbsd_create( void ); extern int pci_system_openbsd_create( void );
extern void pci_system_openbsd_init_dev_mem( int ); extern void pci_system_openbsd_init_dev_mem( int );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment