X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=Kernel%2Fdrv%2Fpci.c;h=f75b5acb708698c6ded7129d061438165146d886;hb=504e0f27f9240f083cefdc9f8dd7fefe0af544cc;hp=df5c11444cb51c3448090732bb5199a77eeedd0a;hpb=df1d534cfe822903fc38e1cc13c4b18941c91908;p=tpg%2Facess2.git diff --git a/Kernel/drv/pci.c b/Kernel/drv/pci.c index df5c1144..f75b5acb 100644 --- a/Kernel/drv/pci.c +++ b/Kernel/drv/pci.c @@ -28,7 +28,7 @@ typedef struct sPCIDevice }; Uint16 oc; }; - Uint16 revision; + Uint8 revision, progif; Uint32 ConfigCache[256/4]; char Name[8]; tVFS_Node Node; @@ -42,25 +42,10 @@ typedef struct sPCIDevice int PCI_Install(char **Arguments); int PCI_ScanBus(int ID, int bFill); -char *PCI_ReadDirRoot(tVFS_Node *node, int pos); -tVFS_Node *PCI_FindDirRoot(tVFS_Node *node, const char *filename); -Uint64 PCI_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer); - -#if 0 - int PCI_CountDevices(Uint16 vendor, Uint16 device, Uint16 fcn); - int PCI_GetDevice(Uint16 vendor, Uint16 device, Uint16 fcn, int idx); - int PCI_GetDeviceByClass(Uint16 class, Uint16 mask, int prev); -Uint8 PCI_GetIRQ(int id); -Uint32 PCI_GetBAR0(int id); -Uint32 PCI_GetBAR1(int id); -Uint32 PCI_GetBAR2(int id); -Uint32 PCI_GetBAR3(int id); -Uint32 PCI_GetBAR4(int id); -Uint32 PCI_GetBAR5(int id); -Uint16 PCI_AssignPort(int id, int bar, int count); -#endif - - int PCI_EnumDevice(Uint16 bus, Uint16 dev, Uint16 fcn, tPCIDevice *info); +char *PCI_int_ReadDirRoot(tVFS_Node *node, int pos); +tVFS_Node *PCI_int_FindDirRoot(tVFS_Node *node, const char *filename); +Uint64 PCI_int_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer); + int PCI_int_EnumDevice(Uint16 bus, Uint16 dev, Uint16 fcn, tPCIDevice *info); // === GLOBALS === MODULE_DEFINE(0, 0x0100, PCI, PCI_Install, NULL, NULL); @@ -75,8 +60,8 @@ tDevFS_Driver gPCI_DriverStruct = { .Size = -1, .NumACLs = 1, .ACLs = &gVFS_ACL_EveryoneRX, - .ReadDir = PCI_ReadDirRoot, - .FindDir = PCI_FindDirRoot + .ReadDir = PCI_int_ReadDirRoot, + .FindDir = PCI_int_FindDirRoot } }; Uint32 *gaPCI_PortBitmap = NULL; @@ -159,14 +144,15 @@ int PCI_ScanBus(int BusID, int bFill) for( fcn = 0; fcn < 8; fcn++ ) // Max 8 functions per device { // Check if the device/function exists - if(!PCI_EnumDevice(BusID, dev, fcn, &devInfo)) + if(!PCI_int_EnumDevice(BusID, dev, fcn, &devInfo)) continue; if(devInfo.oc == PCI_OC_PCIBRIDGE) { #if LIST_DEVICES - Log_Log("PCI", "Bridge @ %i,%i:%i (0x%x:0x%x)", - BusID, dev, fcn, devInfo.vendor, devInfo.device); + if( !bFill ) + Log_Log("PCI", "Bridge @ %i,%i:%i (0x%x:0x%x)", + BusID, dev, fcn, devInfo.vendor, devInfo.device); #endif //TODO: Handle PCI-PCI Bridges //PCI_ScanBus(devInfo.???, bFill); @@ -175,8 +161,9 @@ int PCI_ScanBus(int BusID, int bFill) else { #if LIST_DEVICES - Log_Log("PCI", "Device %i,%i:%i %04x => 0x%04x:0x%04x", - BusID, dev, fcn, devInfo.oc, devInfo.vendor, devInfo.device); + if( !bFill ) + Log_Log("PCI", "Device %i,%i:%i %04x => 0x%04x:0x%04x", + BusID, dev, fcn, devInfo.oc, devInfo.vendor, devInfo.device); #endif } @@ -196,10 +183,9 @@ int PCI_ScanBus(int BusID, int bFill) } /** - * \fn char *PCI_ReadDirRoot(tVFS_Node *Node, int Pos) * \brief Read from Root of PCI Driver */ -char *PCI_ReadDirRoot(tVFS_Node *Node, int Pos) +char *PCI_int_ReadDirRoot(tVFS_Node *Node, int Pos) { ENTER("pNode iPos", Node, Pos); if(Pos < 0 || Pos >= giPCI_DeviceCount) { @@ -211,9 +197,8 @@ char *PCI_ReadDirRoot(tVFS_Node *Node, int Pos) return strdup( gPCI_Devices[Pos].Name ); } /** - * \fn tVFS_Node *PCI_FindDirRoot(tVFS_Node *node, const char *filename) */ -tVFS_Node *PCI_FindDirRoot(tVFS_Node *node, const char *filename) +tVFS_Node *PCI_int_FindDirRoot(tVFS_Node *node, const char *filename) { int bus,slot,fcn; int i; @@ -251,9 +236,8 @@ tVFS_Node *PCI_FindDirRoot(tVFS_Node *node, const char *filename) } /** - * \fn Uint64 PCI_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer) */ -Uint64 PCI_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer) +Uint64 PCI_int_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer) { if( pos + length > 256 ) return 0; @@ -267,41 +251,35 @@ Uint64 PCI_ReadDevice(tVFS_Node *node, Uint64 pos, Uint64 length, void *buffer) // --- Kernel Code Interface --- /** - \fn int PCI_CountDevices(Uint16 vendor, Uint16 device, Uint16 fcn) - \brief Counts the devices with the specified codes - \param vendor Vendor ID - \param device Device ID - \param fcn Function ID -*/ -int PCI_CountDevices(Uint16 vendor, Uint16 device, Uint16 fcn) + * \brief Counts the devices with the specified codes + * \param vendor Vendor ID + * \param device Device ID + */ +int PCI_CountDevices(Uint16 vendor, Uint16 device) { int i, ret=0; for(i=0;i= giPCI_DeviceCount) - return 0; - return gPCI_Devices[id].ConfigCache[15]; - //return PCI_CfgReadByte( gPCI_Devices[id].bus, gPCI_Devices[id].slot, gPCI_Devices[id].fcn, 0x3C); + tPCIDevice *dev = &gPCI_Devices[ID]; + if(ID < 0 || ID >= giPCI_DeviceCount) return 1; + + if(Vendor) *Vendor = dev->vendor; + if(Device) *Device = dev->device; + if(Class) *Class = dev->oc; + return 0; } -/** - \fn Uint32 PCI_GetBAR0(int id) -*/ -Uint32 PCI_GetBAR0(int id) +int PCI_GetDeviceVersion(tPCIDev ID, Uint8 *Revision, Uint8 *ProgIF) { - if(id < 0 || id >= giPCI_DeviceCount) - return 0; - return gPCI_Devices[id].ConfigCache[4]; + tPCIDevice *dev = &gPCI_Devices[ID]; + if(ID < 0 || ID >= giPCI_DeviceCount) return 1; + + if(Revision) *Revision = dev->revision; + if(ProgIF) *ProgIF = dev->progif; + return 0; } -/** - \fn Uint32 PCI_GetBAR1(int id) -*/ -Uint32 PCI_GetBAR1(int id) +int PCI_GetDeviceSubsys(tPCIDev ID, Uint16 *SubsystemVendor, Uint16 *SubsystemID) { - if(id < 0 || id >= giPCI_DeviceCount) - return 0; - return gPCI_Devices[id].ConfigCache[5]; + tPCIDevice *dev = &gPCI_Devices[ID]; + if(ID < 0 || ID >= giPCI_DeviceCount) return 1; + + if(SubsystemVendor) *SubsystemVendor = dev->ConfigCache[0x2c/4] & 0xFFFF; + if(SubsystemID) *SubsystemID = dev->ConfigCache[0x2c/4] >> 16; + + return 0; } -/** - \fn Uint32 PCI_GetBAR2(int id) -*/ -Uint32 PCI_GetBAR2(int id) +Uint32 PCI_ConfigRead(tPCIDev ID, int Offset, int Size) { - if(id < 0 || id >= giPCI_DeviceCount) + tPCIDevice *dev; + Uint32 dword; + if( ID < 0 || ID >= giPCI_DeviceCount ) return 0; + if( Offset < 0 || Offset > 256 ) return 0; + + // TODO: Should I support non-aligned reads? + if( Offset & (Size - 1) ) return 0; + + dev = &gPCI_Devices[ID]; + + dword = PCI_CfgReadDWord(dev->bus, dev->slot, dev->fcn, Offset / 4); + gPCI_Devices[ID].ConfigCache[Offset/4] = dword; + switch( Size ) + { + case 1: return (dword >> (8 * (Offset&3))) & 0xFF; + case 2: return (dword >> (8 * (Offset&2))) & 0xFFFF; + case 4: return dword; + default: return 0; - return gPCI_Devices[id].ConfigCache[6]; + } } -/** - \fn Uint32 PCI_GetBAR3(int id) -*/ -Uint32 PCI_GetBAR3(int id) +void PCI_ConfigWrite(tPCIDev ID, int Offset, int Size, Uint32 Value) { - if(id < 0 || id >= giPCI_DeviceCount) - return 0; - return gPCI_Devices[id].ConfigCache[7]; + tPCIDevice *dev; + Uint32 dword; + int shift; + if( ID < 0 || ID >= giPCI_DeviceCount ) return ; + if( Offset < 0 || Offset > 256 ) return ; + + dev = &gPCI_Devices[ID]; + + if(Size != 4) + dword = PCI_CfgReadDWord(dev->bus, dev->slot, dev->fcn, Offset/4); + switch(Size) + { + case 1: + shift = (Offset&3)*8; + dword &= ~(0xFF << shift); + dword |= Value << shift; + break; + case 2: + shift = (Offset&2)*8; + dword &= ~(0xFFFF << shift); + dword |= Value << shift; + break; + case 4: + dword = Value; + break; + default: + return; + } + PCI_CfgWriteDWord(dev->bus, dev->slot, dev->fcn, Offset/4, dword); } /** - \fn Uint32 PCI_GetBAR4(int id) -*/ -Uint32 PCI_GetBAR4(int id) + * \brief Get the IRQ assigned to a device + */ +Uint8 PCI_GetIRQ(tPCIDev id) { if(id < 0 || id >= giPCI_DeviceCount) return 0; - return gPCI_Devices[id].ConfigCache[8]; + return gPCI_Devices[id].ConfigCache[15] & 0xFF; + //return PCI_CfgReadByte( gPCI_Devices[id].bus, gPCI_Devices[id].slot, gPCI_Devices[id].fcn, 0x3C); } /** - \fn Uint32 PCI_GetBAR5(int id) -*/ -Uint32 PCI_GetBAR5(int id) + * \brief Read the a BAR (base address register) from the PCI config space + */ +Uint32 PCI_GetBAR(tPCIDev id, int BARNum) { if(id < 0 || id >= giPCI_DeviceCount) return 0; - return gPCI_Devices[id].ConfigCache[9]; -} - -Uint16 PCI_AssignPort(int id, int bar, int count) -{ - Uint16 portVals; - int gran=0; - int i, j; - tPCIDevice *dev; - - //LogF("PCI_AssignPort: (id=%i,bar=%i,count=%i)\n", id, bar, count); - - if(id < 0 || id >= giPCI_DeviceCount) return 0; - if(bar < 0 || bar > 5) return 0; - - dev = &gPCI_Devices[id]; - - PCI_CfgWriteDWord( dev->bus, dev->slot, dev->fcn, 0x10+bar*4, -1 ); - portVals = PCI_CfgReadDWord( dev->bus, dev->slot, dev->fcn, 0x10+bar*4 ); - dev->ConfigCache[4+bar] = portVals; - //LogF(" PCI_AssignPort: portVals = 0x%x\n", portVals); - - // Check for IO port - if( !(portVals & 1) ) return 0; - - // Mask out final bit - portVals &= ~1; - - // Get Granuality - #if ARCHDIR_IS_x86 || ARCHDIR_IS_x86_64 - __asm__ __volatile__ ("bsf %%eax, %%ecx" : "=c" (gran) : "a" (portVals) ); - gran = 1 << gran; - #else - { - for(gran = 1; gran && !(portVals & gran); gran <<= 1); - } - #endif - //LogF(" PCI_AssignPort: gran = 0x%x\n", gran); - - // Find free space - portVals = 0; - for( i = 0; i < 1<<16; i += gran ) - { - for( j = 0; j < count; j ++ ) - { - if( gaPCI_PortBitmap[ (i+j)>>5 ] & 1 << ((i+j)&0x1F) ) - break; - } - if(j == count) { - portVals = i; - break; - } - } - - if(portVals) - { - for( j = 0; j < count; j ++ ) - { - if( gaPCI_PortBitmap[ (portVals+j)>>5 ] |= 1 << ((portVals+j)&0x1F) ) - break; - } - PCI_CfgWriteDWord( dev->bus, dev->slot, dev->fcn, 0x10+bar*4, portVals|1 ); - dev->ConfigCache[4+bar] = portVals|1; - } - - // Return - //LogF("PCI_AssignPort: RETURN 0x%x\n", portVals); - return portVals; + if(BARNum < 0 || BARNum >= 6) + return 0; + return gPCI_Devices[id].ConfigCache[4+BARNum]; } /** - * \fn int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) * \brief Get device information for a slot/function */ -int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) +int PCI_int_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) { Uint16 vendor; int i; @@ -518,7 +468,7 @@ int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) info->Node.NumACLs = 1; info->Node.ACLs = &gVFS_ACL_EveryoneRO; - info->Node.Read = PCI_ReadDevice; + info->Node.Read = PCI_int_ReadDevice; return 1; } @@ -528,6 +478,10 @@ int PCI_EnumDevice(Uint16 bus, Uint16 slot, Uint16 fcn, tPCIDevice *info) EXPORT(PCI_CountDevices); EXPORT(PCI_GetDevice); EXPORT(PCI_GetDeviceByClass); -EXPORT(PCI_AssignPort); +EXPORT(PCI_GetDeviceInfo); +EXPORT(PCI_GetDeviceVersion); +EXPORT(PCI_GetDeviceSubsys); +//EXPORT(PCI_AssignPort); +EXPORT(PCI_GetBAR); EXPORT(PCI_GetIRQ); //*/