PCI: enumeration code cleanup

dev
Nigel Barink 2021-11-28 21:07:05 +01:00
parent 5089da5e9e
commit 08b97af863
2 changed files with 99 additions and 32 deletions

View File

@ -95,17 +95,30 @@ const char* GetClassCodeName (uint64_t ClassCode ) {
}
const char* getVendor( uint64_t VendorID){
const char* getVendor( uint32_t VendorID){
switch (VendorID)
{
case 0x8086:
return "Intel Corporation";
break;
default:
return "Vendor Unkown";
break;
case 0x8086:
return "Intel Corporation";
break;
case 0x10DE:
return "NVIDIA Corporation";
break;
case 0x1022:
return "Advanced Micro Devices, Inc.[AMD]";
break;
case 0x1002:
return "Advanced Micor Devices, Inc.[AMD/ATI]";
break;
default:
return "Vendor Unkown";
break;
}
}
@ -136,8 +149,9 @@ uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offs
uint8_t GetHeaderType( PCIBusAddress& PCIDeviceAddress ){
uint32_t header_information = ConfigReadWord(PCIDeviceAddress , 0xC);
return (uint8_t) (
(header_information >> 16) //Get higher half
& 0x00FF ); // Select the last two bytes
((header_information >> 16) //Get higher half
& 0x00FF) // Select the last two bytes
& 0x7F ); // Mask bit 7 as it indicates if the device is a mulit function device!
}
uint16_t GetClassCodes( PCIBusAddress& PCIDeviceAddress ){
@ -146,6 +160,39 @@ uint16_t GetClassCodes( PCIBusAddress& PCIDeviceAddress ){
}
bool IsMultiFunctionDevice(PCIBusAddress& PCIDeviceAddress){
uint32_t header_information = ConfigReadWord(PCIDeviceAddress, 0xC);
return (((header_information>>16)
& 0x80)
>> 7 );
}
void PrintPCIDeviceInfo (PCIBusAddress& PCIDeviceAddress)
{
uint32_t DeviceID = (GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) >> 16);
uint32_t VendorID = GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) & 0xFFFF;
printf("Device found!\n");
printf("Bus: %d, Device: %d, function: %d \n", PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function);
printf("DeviceID: 0x%x, Vendor: %s\n",
DeviceID
, getVendor(VendorID) );
uint8_t header_type = GetHeaderType(PCIDeviceAddress);
printf( "Header type: 0x%x\n", header_type);
uint16_t deviceClasses = GetClassCodes(PCIDeviceAddress);
printf("class: %s, subClass: %d\n\n", GetClassCodeName((deviceClasses >>8)), deviceClasses & 0xFF);
}
void PCI_Enumerate(){
int devicesFound = 0;
// loop through all possible busses, devices and their functions;
@ -159,33 +206,35 @@ void PCI_Enumerate(){
int function = 0;
//uint64_t DeviceIdentify = ConfigReadWord(bus, device, function,0x0);
uint32_t VendorID = GetDevice(bus, device, function) & 0xFFFF;
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
if( DeviceID != 0xFFFF){
printf("Device found!\n");
printf("Bus: %d, Device: %d, function: %d \n", bus, device, function);
printf("DeviceID: 0x%x, VendorID: %s\n", DeviceID, getVendor(VendorID) );
// iterate over the functions if it is a multi function device!
if( false ){
for ( function ++ ; function < 8; function++)
{
}
}
PCIBusAddress busAddress =
PCIBusAddress{bus, device, function };
uint8_t header_type = GetHeaderType(busAddress);
printf( "Header type: 0x%x\n", header_type);
PrintPCIDeviceInfo(busAddress);
// iterate over the functions if it is a multi function device!
if( IsMultiFunctionDevice(busAddress) ){
printf("Multi function device! \n");
printf("Check remaining Functions\n");
for ( function = 1 ; function < 8; function++)
{
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
if( DeviceID != 0xFFFF){
PCIBusAddress busAddress2 = PCIBusAddress{bus, device, function};
PrintPCIDeviceInfo(busAddress2);
devicesFound++;
}
}
}
uint16_t deviceClasses = GetClassCodes(busAddress);
printf(" class: %s, subClass: %d\n\n",
(deviceClasses >>8) > 0x13 ? "Unknown": GetClassCodeName((deviceClasses >>8)), deviceClasses & 0xFF);
devicesFound++;
}

View File

@ -1,6 +1,5 @@
#pragma once
#include <stdint.h>
/*
* PCI devices API
*/
@ -8,14 +7,14 @@ struct PCIBusAddress{
int bus ;
int device ;
int function[8];
int function;
uint32_t getAddress( int deviceFunction = 0 ){
uint32_t getAddress( ){
return ((uint32_t) 1 << 31) |
((uint32_t) bus << 16) |
((uint32_t) device << 11)|
((uint32_t) function[deviceFunction] << 8) |
((uint32_t) function << 8) |
0x0000;
};
@ -27,10 +26,29 @@ class PCIDevice {
~PCIDevice();
PCIBusAddress const PCIAddress();
inline const char* getDeviceString(){
return "Not implemented"; //GetClassCodeName(deviceclass);
}
inline const char* getVendorString(){
return "Not implemented"; // getVendor(VendorID);
}
inline void setVendorID (uint16_t id) {
this->VendorID = id;
}
private:
int bus;
int device;
int function;
uint16_t VendorID;
uint16_t DeviceID;
uint8_t deviceclass;
uint8_t devicesubclass;
int headerType;
};