Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 0 additions & 92 deletions src/pci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,98 +481,6 @@ int PciHandle::openMcfgTable() {
return handle;
}

#ifndef PCM_USE_PCI_MM_LINUX

PciHandleM::PciHandleM(uint32 bus_, uint32 device_, uint32 function_) :
fd(-1),
bus(bus_),
device(device_),
function(function_),
base_addr(0)
{
int handle = ::open("/dev/mem", O_RDWR);
if (handle < 0) throw std::exception();
fd = handle;

int mcfg_handle = PciHandle::openMcfgTable();
if (mcfg_handle < 0) throw std::runtime_error("Cannot open any of /[pcm]/sys/firmware/acpi/tables/MCFG* files!");

int32 result = ::pread(mcfg_handle, (void *)&base_addr, sizeof(uint64), 44);

if (result != sizeof(uint64))
{
::close(mcfg_handle);
throw std::exception();
}

unsigned char max_bus = 0;

result = ::pread(mcfg_handle, (void *)&max_bus, sizeof(unsigned char), 55);

::close(mcfg_handle);
if (result != sizeof(unsigned char))
{
throw std::exception();
}

if (bus > (unsigned)max_bus)
{
std::cout << "WARNING: Requested bus number " << bus << " is larger than the max bus number " << (unsigned)max_bus << "\n";
throw std::exception();
}

// std::cout << "PCI config base addr: "<< std::hex << base_addr<< "\n" << std::dec;

base_addr += (bus * 1024ULL * 1024ULL + device * 32ULL * 1024ULL + function * 4ULL * 1024ULL);
}


bool PciHandleM::exists(uint32 /*groupnr_*/, uint32 /* bus_*/, uint32 /* device_ */, uint32 /* function_ */)
{
int handle = ::open("/dev/mem", O_RDWR);

if (handle < 0) {
perror("error opening /dev/mem");
return false;
}

::close(handle);

handle = PciHandle::openMcfgTable();
if (handle < 0) {
return false;
}

::close(handle);

return true;
}

int32 PciHandleM::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandleM::read32", false, offset);
return ::pread(fd, (void *)value, sizeof(uint32), offset + base_addr);
}

int32 PciHandleM::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandleM::write32", false, offset);
return ::pwrite(fd, (const void *)&value, sizeof(uint32), offset + base_addr);
}

int32 PciHandleM::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandleM::read64", false, offset);
return ::pread(fd, (void *)value, sizeof(uint64), offset + base_addr);
}

PciHandleM::~PciHandleM()
{
if (fd >= 0) ::close(fd);
}

#endif // PCM_USE_PCI_MM_LINUX

// mmapped I/O version

MCFGHeader PciHandleMM::mcfgHeader;
Expand Down
44 changes: 3 additions & 41 deletions src/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class PciHandle
DWORD pciAddress;
#endif

friend class PciHandleM;
friend class PciHandleMM;

PciHandle(); // forbidden
Expand Down Expand Up @@ -79,40 +78,7 @@ typedef PciHandle PciHandleType;
typedef PciHandle PciHandleType;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
typedef PciHandle PciHandleType;
#else

// read/write PCI config space using physical memory
class PciHandleM
{
#ifdef _MSC_VER

#else
int32 fd;
#endif

uint32 bus;
uint32 device;
uint32 function;
uint64 base_addr;

PciHandleM() = delete; // forbidden
PciHandleM(PciHandleM &) = delete; // forbidden
PciHandleM & operator = (PciHandleM &) = delete; // forbidden

public:
PciHandleM(uint32 bus_, uint32 device_, uint32 function_);

static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);

int32 read32(uint64 offset, uint32 * value);
int32 write32(uint64 offset, uint32 value);

int32 read64(uint64 offset, uint64 * value);

virtual ~PciHandleM();
};

#ifndef _MSC_VER
#elif defined(__linux__)

// read/write PCI config space using physical memory using mmapped file I/O
class PciHandleMM
Expand All @@ -125,11 +91,9 @@ class PciHandleMM
uint32 function;
uint64 base_addr;

#ifdef __linux__
static MCFGHeader mcfgHeader;
static std::vector<MCFGRecord> mcfgRecords;
static void readMCFG();
#endif

PciHandleMM() = delete; // forbidden
PciHandleMM(const PciHandleMM &) = delete; // forbidden
Expand All @@ -147,9 +111,7 @@ class PciHandleMM

virtual ~PciHandleMM();

#ifdef __linux__
static const std::vector<MCFGRecord> & getMCFGRecords();
#endif
};

#ifdef PCM_USE_PCI_MM_LINUX
Expand All @@ -158,8 +120,8 @@ class PciHandleMM
#define PciHandleType PciHandle
#endif

#endif // _MSC_VER

#else
#error "Platform not supported"
#endif

template <class F>
Expand Down