pci update. busses enumerated properly (tho there is a bug atm (isn't detect pci-bridges correctly for some reason)

This commit is contained in:
smremde_cp 2008-03-08 11:06:32 +00:00
parent e418dc4251
commit 79debc9c81
8 changed files with 285 additions and 206 deletions

View file

@ -8,148 +8,44 @@ namespace Cosmos.Hardware.PC.Bus
public class PCIBus : Cosmos.Hardware.Bus.PCIBus
{
protected const ushort ConfigAddr = 0xCF8;
protected const ushort ConfigData = 0xCFC;
public static List<PCIDevice> Devices = new List<PCIDevice>();
static public void Init()
{
var xDeviceIDs = new DeviceIDs();
Console.WriteLine("PCI Devices");
#if DEBUG
Console.WriteLine("PCI Devices:");
Console.WriteLine();
#endif
byte xMaxBus = GetMaxBus();
for (byte xBus = 0; xBus <= xMaxBus; xBus++)
{
for (byte xSlot = 0; xSlot < 32; xSlot++)
{
byte xMaxFunctions = 1;
EnumerateBus(0, ref Devices);
for (byte xFunction = 0; xFunction < xMaxFunctions; xFunction++)
{
PCIDevice xPCIDevice = new PCIDevice(xBus, xSlot, xFunction);
if (xPCIDevice.DeviceExists)
{
string xVendor = xDeviceIDs.FindVendor(xPCIDevice.VendorID);
Console.Write(xPCIDevice.Bus.ToString());
Console.Write("-");
Console.Write(xPCIDevice.Slot.ToString());
Console.Write("-");
Console.Write(xPCIDevice.Function.ToString());
Console.Write(" ");
Console.Write(xVendor);
Console.Write(": ");
Console.Write(ToHex(xPCIDevice.DeviceID, 4));
Console.Write(" Type: ");
Console.Write(xPCIDevice.HeaderType.ToString());
Console.Write(" Class: ");
Console.Write(xPCIDevice.GetClassInfo());
Console.WriteLine();
Console.Write("Memory requirements: ");
xPCIDevice.BaseAddress0 = 0xffffffff;
Console.Write(ToHex(xPCIDevice.BaseAddress0, 8));
Console.Write(",");
xPCIDevice.BaseAddress1 = 0xffffffff;
Console.Write(ToHex(xPCIDevice.BaseAddress1, 8));
Console.Write(",");
xPCIDevice.BaseAddress2 = 0xffffffff;
Console.Write(ToHex(xPCIDevice.BaseAddress2, 8));
Console.Write(",");
xPCIDevice.BaseAddress3 = 0xffffffff;
Console.Write(ToHex(xPCIDevice.BaseAddress3, 8));
Console.Write(",");
xPCIDevice.BaseAddress4 = 0xffffffff;
Console.Write(ToHex(xPCIDevice.BaseAddress4, 8));
Console.Write(",");
xPCIDevice.BaseAddress5 = 0xffffffff;
Console.Write(ToHex(xPCIDevice.BaseAddress5, 8));
Console.WriteLine();
if (xPCIDevice.IsMultiFunction)
xMaxFunctions = 8;
}
}
}
}
Console.WriteLine("Done");
}
//private static char[] hex = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'e', 'd', 'f' };
private static string ToHex(UInt32 num, int length)
private static void EnumerateBus(byte Bus, ref List<PCIDevice> Devices)
{
char[] ret = new char[length];
UInt32 cpy = num;
for (int index = length - 1; index >= 0; index--)
Console.WriteLine("EnumerateBus(" + Bus + ")");
for (byte xSlot = 0; xSlot < 32; xSlot++)
{
ret[index] = hex(cpy & 0xf);
cpy = cpy / 16;
byte xMaxFunctions = 1;
for (byte xFunction = 0; xFunction < xMaxFunctions; xFunction++)
{
PCIDevice xPCIDevice = PCIDevice.GetPCIDevice(Bus, xSlot, xFunction);
if (xPCIDevice != null)
{
Devices.Add(xPCIDevice);
if (xPCIDevice is PCIDeviceBridge)
EnumerateBus(((PCIDeviceBridge)xPCIDevice).SecondaryBus, ref Devices);
if (xPCIDevice.IsMultiFunction)
xMaxFunctions = 8;
}
}
}
return "0x" + new string(ret);
}
private static char hex(uint p)
{
switch (p)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'a';
case 11:
return 'b';
case 12:
return 'c';
case 13:
return 'd';
case 14:
return 'e';
case 15:
return 'f';
}
return ' ';
}
private static byte GetMaxBus()
{
/*
union REGS r;
r.h.ah = PCI_FUNCTION_ID; //0xb1
r.h.al = PCI_BIOS_PRESENT; //0x01
int86(0x1a, &r, &r);
GetMaxBus = r.w.cx;
*/
return 255;
}
}
@ -188,15 +84,14 @@ GetMaxBus = r.w.cx;
}
[Flags]
public enum PCIHeaderType : byte
{
Normal = 0,
Bridge = 1,
Cardbus = 2,
Multifunction = 0x10
Cardbus = 2
}
[Flags]
public enum PCIBist : byte
{
@ -206,10 +101,83 @@ GetMaxBus = r.w.cx;
}
public class PCIDevice
public class PCIDeviceCardBus : PCIDevice
{
private static string[] classtext = new string[]
public PCIDeviceCardBus(byte bus, byte slot, byte function)
: base (bus,slot,function)
{
}
public override int GetNumberOfBaseAddresses()
{
return 6;
}
}
public class PCIDeviceBridge : PCIDevice
{
public PCIDeviceBridge(byte bus, byte slot, byte function)
: base (bus,slot,function)
{
}
public override int GetNumberOfBaseAddresses()
{
return 2;
}
public byte PrimaryBus { get { return Read8(0x18); } set { Write8(0x18, value); } }
public byte SecondaryBus { get { return Read8(0x19); } set { Write8(0x19, value); } }
public byte SubordinateBus { get { return Read8(0x1a); } set { Write8(0x1a, value); } }
public byte SecondaryLatencyTime { get { return Read8(0x1b); } set { Write8(0x1b, value); } }
}
public class PCIDeviceNormal : PCIDevice
{
public PCIDeviceNormal(byte bus, byte slot, byte function)
: base (bus,slot,function)
{
}
public override int GetNumberOfBaseAddresses()
{
return 6;
}
public UInt32 BaseAddress2 { get { return Read32(0x18); } set { Write32(0x18, value); } }
public UInt32 BaseAddress3 { get { return Read32(0x1a); } set { Write32(0x1a, value); } }
public UInt32 BaseAddress4 { get { return Read32(0x20); } set { Write32(0x20, value); } }
public UInt32 BaseAddress5 { get { return Read32(0x24); } set { Write32(0x24, value); } }
}
public abstract class PCIDevice
{
public abstract int GetNumberOfBaseAddresses();
public static PCIDevice GetPCIDevice(byte bus, byte slot, byte function)
{
PCIDeviceNormal test = new PCIDeviceNormal(bus,slot,function);
if (!test.DeviceExists)
return null;
if (test.HeaderType == PCIHeaderType.Normal)
return test;
if (test.HeaderType == PCIHeaderType.Cardbus)
return new PCIDeviceCardBus(bus,slot,function);
if (test.HeaderType == PCIHeaderType.Bridge)
return new PCIDeviceBridge(bus,slot,function);
return null;
}
private static string[] classtext = new string[]
{
"pre pci 2.0", // 00
"disk", // 01
@ -228,7 +196,7 @@ GetMaxBus = r.w.cx;
private static string[][] subclasstext = new string[][]
{
new string[] {},
new string[] { "VGA Device", "non VGA device"},
new string[] { "SCSI" ,"IDE" , "floppy","IPI","RAID", "other" },
new string[] { "Ethernet", "TokenRing", "FDDI" , "ATM" , "other" },
new string[] { "VGA", "SuperVGA","XGA", "other"},
@ -243,15 +211,18 @@ GetMaxBus = r.w.cx;
new string[] { "Firewire", "ACCESS.bus" , "SSA", "USB" ,"Fiber Channel" , "other"},
};
public string GetClassInfo()
{
int cc = ClassCode;
if (cc >= classtext.Length)
return "unknown class / subclass";
int sc = SubClass;
if (cc >= classtext.Length)
return "unknown class (" + cc.ToString() + ") / subclass (" + sc.ToString() + ")";
int sc = SubClass;
if (sc >= subclasstext[cc].Length)
return String.Concat(classtext[cc], " / unknown subclass");
return String.Concat(classtext[cc], " / unknown subclass (", sc.ToString(), ")");
return String.Concat( classtext[cc] , " / " , subclasstext[cc][sc]);
}
@ -268,7 +239,7 @@ GetMaxBus = r.w.cx;
private const UInt32 PCI_BASE_ADDRESS_IO_MASK = ~(UInt32)0x03;
public PCIDevice(byte bus, byte slot, byte function)
protected PCIDevice(byte bus, byte slot, byte function)
{
this.Bus = bus;
this.Slot = slot;
@ -280,7 +251,7 @@ GetMaxBus = r.w.cx;
public byte Function { get; private set; }
public bool DeviceExists { get { return VendorID != 0xFFFF && VendorID != 0x0; } }
public bool IsMultiFunction { get { return (HeaderType & PCIHeaderType.Multifunction) != 0; } }
public bool IsMultiFunction { get { return (Read8(0x0e) & 0xf0) != 0; } }
public UInt32 VendorID { get { return Read16(0x0); } }
public UInt16 DeviceID { get { return Read16(0x2); } }
@ -295,26 +266,21 @@ GetMaxBus = r.w.cx;
public byte CacheLineSize { get { return Read8(0x0c); } set { Write8(0x0c, value); } }
public byte LatencyTimer { get { return Read8(0x0d); } set { Write8(0x0d, value); } }
public PCIHeaderType HeaderType { get { return (PCIHeaderType)Read8(0x0e); } set { Write8(0x0e, (byte)value); } }
public PCIHeaderType HeaderType { get { return (PCIHeaderType)(Read8(0x0e) & 0xf); } }
public PCIBist Bist { get { return (PCIBist)Read8(0x0f); } set { Write8(0x0f, (byte)value); } }
public UInt32 GetBaseAddress(byte index)
{
return Read32((byte)(0x10b + index << 2));
return Read32((byte)(0x10 + index * 4));
}
public void SetBaseAddress(byte index, UInt32 value)
{
Write32((byte)(0x10b + index << 2), value);
Write32((byte)(0x10 + index *4), value);
}
public UInt32 BaseAddress0 { get { return Read32(0x10); } set { Write32(0x10, value); } }
public UInt32 BaseAddress1 { get { return Read32(0x14); } set { Write32(0x14, value); } }
public UInt32 BaseAddress2 { get { return Read32(0x18); } set { Write32(0x18, value); } }
public UInt32 BaseAddress3 { get { return Read32(0x1a); } set { Write32(0x1a, value); } }
public UInt32 BaseAddress4 { get { return Read32(0x20); } set { Write32(0x20, value); } }
public UInt32 BaseAddress5 { get { return Read32(0x24); } set { Write32(0x24, value); } }
// more registers need to be filled in
public byte InterruptLine { get { return Read8(0x3c); } set { Write8(0x3c, value); } }
public byte InterruptPin { get { return Read8(0x3d); } set { Write8(0x3d, value); } }
public byte MinGNT { get { return Read8(0x3e); } set { Write8(0x3e, value); } }
@ -525,6 +491,6 @@ GetMaxBus = r.w.cx;
{
CPUBus.Write32(ConfigAddr, GetAddress(aRegister));
CPUBus.Write8(ConfigData, value);
}
}
}
}

View file

@ -83,9 +83,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestSuite", "TestSuite", "{
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestKernel", "TestKernel\TestKernel.csproj", "{8F2D5231-CDE5-48FA-9D26-D4305B01FD3C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteveKernel", "SteveKernel\SteveKernel.csproj", "{8C566493-DBA5-45DD-93D4-89D4486CAAB6}"
EndProject
Global
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 34
SccNumberOfProjects = 35
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
SccTeamFoundationServer = https://tfs04.codeplex.com/
SccLocalPath0 = .
@ -221,6 +223,10 @@ Global
SccProjectTopLevelParentUniqueName33 = Cosmos.sln
SccProjectName33 = TestKernel
SccLocalPath33 = TestKernel
SccProjectUniqueName34 = SteveKernel\\SteveKernel.csproj
SccProjectTopLevelParentUniqueName34 = Cosmos.sln
SccProjectName34 = SteveKernel
SccLocalPath34 = SteveKernel
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -356,6 +362,10 @@ Global
{8F2D5231-CDE5-48FA-9D26-D4305B01FD3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F2D5231-CDE5-48FA-9D26-D4305B01FD3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F2D5231-CDE5-48FA-9D26-D4305B01FD3C}.Release|Any CPU.Build.0 = Release|Any CPU
{8C566493-DBA5-45DD-93D4-89D4486CAAB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C566493-DBA5-45DD-93D4-89D4486CAAB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C566493-DBA5-45DD-93D4-89D4486CAAB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C566493-DBA5-45DD-93D4-89D4486CAAB6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -396,6 +406,7 @@ Global
{B69B89D4-54FC-41DF-B5D7-EE674CF60343} = {EBF602FE-8AFC-408D-A9F1-560C35651090}
{AEC28E7E-1ABB-4ABC-8CE1-B74D9DC9B80E} = {EBF602FE-8AFC-408D-A9F1-560C35651090}
{7EB6E2FF-74C8-4F59-9923-EB8EF1DE098E} = {EBF602FE-8AFC-408D-A9F1-560C35651090}
{8C566493-DBA5-45DD-93D4-89D4486CAAB6} = {EBF602FE-8AFC-408D-A9F1-560C35651090}
{8F2D5231-CDE5-48FA-9D26-D4305B01FD3C} = {7EB6E2FF-74C8-4F59-9923-EB8EF1DE098E}
EndGlobalSection
EndGlobal

View file

@ -25,6 +25,10 @@ namespace Cosmos.Shell.Console.Commands {
public abstract void Execute(string param);
public abstract void Help();
public virtual void Help()
{
System.Console.WriteLine(Name);
System.Console.WriteLine(" " + Summary);
}
}
}

View file

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Text;
using Cosmos.Hardware.PC.Bus;
namespace Cosmos.Shell.Console.Commands
{
public class LspciCommand : CommandBase
{
public override string Name
{
get { return "lspci"; }
}
public override string Summary
{
get { return "Lists pci devices."; }
}
public override void Execute(string param)
{
var xDeviceIDs = new Cosmos.Hardware.Bus.PCIBus.DeviceIDs();
foreach (PCIDevice xPCIDevice in Cosmos.Hardware.PC.Bus.PCIBus.Devices.ToArray())
{
string xVendor = xDeviceIDs.FindVendor(xPCIDevice.VendorID);
if (xVendor == default(string))
xVendor = ToHex(xPCIDevice.VendorID, 4);
System.Console.Write(xPCIDevice.Bus + "-" + xPCIDevice.Slot + "-" + xPCIDevice.Function);
System.Console.Write(" " + xVendor + ":" + ToHex(xPCIDevice.DeviceID, 4));
System.Console.WriteLine(" Type: " + xPCIDevice.HeaderType);
// /*Enum.GetName(typeof(PCIHeaderType), */ xPCIDevice.HeaderType/*) */);
// Console.WriteLine(" Status: " + xPCIDevice.Status + " " +
// /*Enum.GetName(typeof(PCIStatus), */xPCIDevice.Status/*) */);
// Console.WriteLine(" Command: " + xPCIDevice.Command + " " +
// /*Enum.GetName(typeof(PCICommand), */xPCIDevice.Command /* ) */);
System.Console.Write(" Class [" + ToHex((UInt32)((xPCIDevice.ClassCode << 8) | xPCIDevice.SubClass), 4) + "] " + xPCIDevice.GetClassInfo());
System.Console.WriteLine();
System.Console.Write(" Memory: ");
for (byte i = 0; i < xPCIDevice.GetNumberOfBaseAddresses(); i++)
{
System.Console.Write(ToHex(xPCIDevice.GetBaseAddress(i), 8));
System.Console.Write(" ");
}
System.Console.WriteLine();
System.Console.Write(" Flags: ");
for (byte i = 0; i < xPCIDevice.GetNumberOfBaseAddresses(); i++)
{
UInt32 addr = xPCIDevice.GetBaseAddress(i);
xPCIDevice.SetBaseAddress(i, 0xffffffff);
System.Console.Write(ToHex(xPCIDevice.GetBaseAddress(i), 8));
xPCIDevice.SetBaseAddress(i, addr);
System.Console.Write(" ");
}
System.Console.WriteLine();
}
}
static string ToHex(UInt32 num, int length)
{
char[] ret = new char[length];
UInt32 cpy = num;
for (int index = length - 1; index >= 0; index--)
{
ret[index] = hex(cpy & 0xf);
cpy = cpy / 16;
}
return "0x" + new string(ret);
}
private static char hex(uint p)
{
switch (p)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'a';
case 11:
return 'b';
case 12:
return 'c';
case 13:
return 'd';
case 14:
return 'e';
case 15:
return 'f';
}
return ' ';
}
public override void Help()
{
System.Console.WriteLine(Name);
System.Console.WriteLine(" " + Summary);
}
}
}

View file

@ -48,6 +48,7 @@
<ItemGroup>
<Compile Include="Commands\BreakCommand.cs" />
<Compile Include="Commands\ClsCommand.cs" />
<Compile Include="Commands\LspciCommand.cs" />
<Compile Include="Commands\DeviceCommand.cs" />
<Compile Include="Commands\DirCommand.cs" />
<Compile Include="Commands\EchoCommand.cs" />

View file

@ -34,6 +34,10 @@ namespace Cosmos.Shell.Console {
_commands.Add(new Commands.TypeCommand());
_commands.Add(new Commands.VersionCommand());
_commands.Add(new Commands.DeviceCommand());
_commands.Add(new Commands.LspciCommand());
// TODO: this shouldn't go here:
//Hardware.PC.Bus.PCIBus.Init();
while (running) {
System.Console.Write("Running = ");

View file

@ -17,65 +17,16 @@ namespace SteveKernel
private static string[] classtext = new string[]
{
"pre pci 2.0", // 00
"disk", // 01
"network", // 02
"display", // 03
"multimedia", // 04
"memory", // 05
"bridge", // 06
"communication", // 07
"system peripheral",// 08
"input", // 09
"docking station", // 0A
"CPU", // 0B
"serial bus", // 0C
};
private static string[][] subclasstext = new string[][]
{
new string[] {},
new string[] { "SCSI" ,"IDE" , "floppy","IPI","RAID", "other" },
new string[] { "Ethernet", "TokenRing", "FDDI" , "ATM" , "other" },
new string[] { "VGA", "SuperVGA","XGA", "other"},
new string[] { "video" ,"audio", "other"},
new string[] { "RAM", "Flash memory" , "other"},
new string[] { "CPU/PCI" ,"PCI/ISA" , "PCI/EISA" , "PCI/MCA","PCI/PCI" , "PCI/PCMCIA", "PCI/NuBus", "PCI/CardBus", "other"},
new string[] { "serial", "parallel", "other"},
new string[] { "PIC", "DMAC" , "timer" ,"RTC", "other"},
new string[] { "keyboard","digitizer","mouse", "other" },
new string[] { "generic" , "other" },
new string[] { "386", "486","Pentium" , "P6" ,"Alpha","coproc","other" },
new string[] { "Firewire", "ACCESS.bus" , "SSA", "USB" ,"Fiber Channel" , "other"},
};
// Main entry point of the kernel
public static void Init()
{
Console.WriteLine("Done booting");
// all work fine
Console.Write("pre pci 2.0 = '"); Console.Write(classtext[0]); Console.WriteLine("'");
Console.Write("disk = '"); Console.Write(classtext[1]); Console.WriteLine("'");
Console.Write("network = '"); Console.Write(classtext[2]); Console.WriteLine("'");
Console.Write("display = '"); Console.Write(classtext[3]); Console.WriteLine("'");
Console.Write("multimedia = '"); Console.Write(classtext[4]);Console.WriteLine("'");
Console.Write("memory = '"); Console.Write(classtext[5]);Console.WriteLine("'");
Console.Write("bridge = '"); Console.Write(classtext[6]);Console.WriteLine("'");
Console.Write("communication = '"); Console.Write(classtext[7]);Console.WriteLine("'");
Console.Write("system peripheral = '"); Console.Write(classtext[8]);Console.WriteLine("'");
Console.Write("input = '"); Console.Write(classtext[9]);Console.WriteLine("'");
Console.Write("docking station = '"); Console.Write(classtext[10]);Console.WriteLine("'");
Console.Write("CPU = '"); Console.Write(classtext[11]); Console.WriteLine("'");
Console.Write("serial bus = '"); Console.Write(classtext[12]); Console.WriteLine("'");
// doesnt wrok
Console.Write("network / Ethernet = '");
Console.Write(classtext[2] + " / " + subclasstext[2][0]);
Console.WriteLine("'");
Cosmos.Hardware.PC.Bus.PCIBus.Init();
while (true)
;

View file

@ -48,6 +48,16 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cosmos.Hardware.PC\Cosmos.Hardware.PC.csproj">
<Project>{B024FADF-EF04-4602-A0F4-49016D68B2AF}</Project>
<Name>Cosmos.Hardware.PC</Name>
</ProjectReference>
<ProjectReference Include="..\Cosmos\Cosmos.Hardware\Cosmos.Hardware.csproj">
<Project>{CE50FE98-9AC4-4B4D-ADC7-31F6DCD28755}</Project>
<Name>Cosmos.Hardware</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.