Skip to content

Commit

Permalink
PCI framework refactor: part 2 (#1213)
Browse files Browse the repository at this point in the history
* PCI framework refactor: part 2

Signed-off-by: AnErrupTion <[email protected]>

* Device driver is always a PCIDevice

Signed-off-by: AnErrupTion <[email protected]>

* Make the Device class BusType aware

Signed-off-by: AnErrupTion <[email protected]>

---------

Signed-off-by: AnErrupTion <[email protected]>
  • Loading branch information
AnErrupTion authored Apr 7, 2024
1 parent 33c48a5 commit dfbf309
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 165 deletions.
3 changes: 2 additions & 1 deletion Source/Mosa.BareMetal.HelloWorld/Apps/ShowISA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using Mosa.DeviceDriver.ISA;
using Mosa.DeviceSystem.Framework;

namespace Mosa.BareMetal.HelloWorld.Apps;

Expand All @@ -15,7 +16,7 @@ public void Execute()
{
Console.Write("> Probing for ISA devices...");

var isaDevices = Program.DeviceService.GetChildrenOf(Program.DeviceService.GetFirstDevice<ISABus>());
var isaDevices = Program.DeviceService.GetAllDevices(DeviceBusType.ISA);
Console.WriteLine("[Completed: " + isaDevices.Count + " found]");

foreach (var device in isaDevices)
Expand Down
20 changes: 5 additions & 15 deletions Source/Mosa.BareMetal.HelloWorld/Apps/ShowPCI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System;
using Mosa.DeviceSystem.Framework;
using Mosa.DeviceSystem.PCI;

namespace Mosa.BareMetal.HelloWorld.Apps;
Expand All @@ -14,7 +15,7 @@ public class ShowPCI : IApp
public void Execute()
{
Console.Write("> Probing for PCI devices...");
var devices = Program.DeviceService.GetDevices<PCIDevice>();
var devices = Program.DeviceService.GetAllDevices(DeviceBusType.PCI);
Console.WriteLine("[Completed: " + devices.Count + " found]");

foreach (var device in devices)
Expand All @@ -23,21 +24,10 @@ public void Execute()
Program.Bullet(ConsoleColor.Yellow);
Console.Write(" ");

var pciDevice = device.DeviceDriver as PCIDevice;
Program.InBrackets(device.Name + ": " + pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x") + " " + pciDevice.SubSystemID.ToString("x") + ":" + pciDevice.SubSystemVendorID.ToString("x") + " (" + pciDevice.ClassCode.ToString("x") + ":" + pciDevice.SubClassCode.ToString("x") + ":" + pciDevice.ProgIF.ToString("x") + ":" + pciDevice.RevisionID.ToString("x") + ")", ConsoleColor.White, ConsoleColor.Green);

var children = Program.DeviceService.GetChildrenOf(device);

if (children.Count != 0)
{
var child = children[0];

Console.WriteLine();
Console.Write(" ");

Program.InBrackets(child.Name, ConsoleColor.White, ConsoleColor.Green);
}
var pciDevice = (PCIDevice)device.Parent.DeviceDriver;
var name = device.DeviceDriverRegistryEntry == null ? "UnknownPCIDevice" : device.Name;

Program.InBrackets(pciDevice.Device.Name + ": " + name + " " + pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x") + " " + pciDevice.SubSystemID.ToString("x") + ":" + pciDevice.SubSystemVendorID.ToString("x") + " (" + pciDevice.ClassCode.ToString("x") + ":" + pciDevice.SubClassCode.ToString("x") + ":" + pciDevice.ProgIF.ToString("x") + ":" + pciDevice.RevisionID.ToString("x") + ")", ConsoleColor.White, ConsoleColor.Green);
Console.WriteLine();
}
}
Expand Down
63 changes: 0 additions & 63 deletions Source/Mosa.DeviceDriver/ISA/ISABus.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Source/Mosa.DeviceDriver/PCI/VirtIO/VirtIODevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public VirtIODevice(Device device)
return;
}

pciController = (IPCIController)device.Parent.Parent.DeviceDriver;
pciController = pciDevice.Controller;

foreach (var capability in pciDevice.Capabilities)
{
Expand Down
13 changes: 0 additions & 13 deletions Source/Mosa.DeviceDriver/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public static class Setup
{
Name = "Intel4SeriesChipsetDRAMController",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x2E10,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -103,7 +102,6 @@ public static class Setup
{
Name = "Intel4SeriesChipsetIntegratedGraphicsController",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x2E10,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -114,7 +112,6 @@ public static class Setup
{
Name = "Intel4SeriesChipsetIntegratedGraphicsController2E13",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x2E13,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -125,7 +122,6 @@ public static class Setup
{
Name = "Intel4SeriesChipsetPCIExpressRootPort",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x2E10,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -136,7 +132,6 @@ public static class Setup
{
Name = "Intel4SeriesChipsetPCIExpressRootPort",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x2E10,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -147,7 +142,6 @@ public static class Setup
{
Name = "Intel440FX",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x1237,
ClassCode = 0x06,
Expand All @@ -160,7 +154,6 @@ public static class Setup
{
Name = "IntelPIIX3",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x7000,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -171,7 +164,6 @@ public static class Setup
{
Name = "IntelPIIX4",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x7113,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -182,7 +174,6 @@ public static class Setup
{
Name = "IntelGPIOController",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x0934,
ClassCode = 0X0C,
Expand All @@ -197,7 +188,6 @@ public static class Setup
{
Name = "IntelHSUART",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x0936,
ClassCode = 0X07,
Expand All @@ -212,7 +202,6 @@ public static class Setup
{
Name = "PCIIDEInterface",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x8086,
DeviceID = 0x7010,
ClassCode = 0X01,
Expand All @@ -226,7 +215,6 @@ public static class Setup
{
Name = "VirtIOGPU",
Platform = PlatformArchitecture.X86AndX64 | PlatformArchitecture.ARM32,
BusType = DeviceBusType.PCI,
VendorID = 0x1AF4,
DeviceID = 0x1050,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand All @@ -237,7 +225,6 @@ public static class Setup
{
Name = "VMwareSVGA2",
Platform = PlatformArchitecture.X86AndX64,
BusType = DeviceBusType.PCI,
VendorID = 0x15AD,
DeviceID = 0x0405,
PCIFields = PCIField.VendorID | PCIField.DeviceID,
Expand Down
2 changes: 2 additions & 0 deletions Source/Mosa.DeviceSystem/Framework/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Device
{
public string Name { get; set; }

public DeviceBusType BusType { get; set; }

public BaseDeviceDriver DeviceDriver { get; set; }

public DeviceStatus Status { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion Source/Mosa.DeviceSystem/Framework/PCI/PCIField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public enum PCIField : byte
RevisionID = 16,
ProgIF = 32,
ClassCode = 64,
SubClassCode = 128
SubClassCode = 128,
All = DeviceID | VendorID | SubSystemVendorID | SubSystemID | RevisionID | ProgIF | ClassCode | SubClassCode
}
63 changes: 36 additions & 27 deletions Source/Mosa.DeviceSystem/PCI/PCIDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Mosa.DeviceSystem.PCI;
/// </summary>
public class PCIDevice : BaseDeviceDriver
{
private IPCIController pciController;

#region Properties

public byte Bus { get; }
Expand All @@ -23,40 +21,42 @@ public class PCIDevice : BaseDeviceDriver

public byte Function { get; }

public IPCIController Controller { get; private set; }

public PCICapability[] Capabilities { get; private set; }

public ushort VendorID => pciController.ReadConfig16(this, PCIConfigurationHeader.VendorID);
public BaseAddress[] BaseAddresses { get; } = new BaseAddress[8];

public ushort VendorID { get; private set; }

public ushort DeviceID => pciController.ReadConfig16(this, PCIConfigurationHeader.DeviceID);
public ushort DeviceID { get; private set; }

public byte RevisionID => pciController.ReadConfig8(this, PCIConfigurationHeader.RevisionID);
public byte RevisionID { get; private set; }

public byte ClassCode => pciController.ReadConfig8(this, PCIConfigurationHeader.ClassCode);
public byte ClassCode { get; private set; }

public byte ProgIF => pciController.ReadConfig8(this, PCIConfigurationHeader.ProgrammingInterface);
public byte ProgIF { get; private set; }

public byte SubClassCode => pciController.ReadConfig8(this, PCIConfigurationHeader.SubClassCode);
public byte SubClassCode { get; private set; }

public ushort SubSystemVendorID => pciController.ReadConfig16(this, PCIConfigurationHeader.SubSystemVendorID);
public ushort SubSystemVendorID { get; private set; }

public ushort SubSystemID => pciController.ReadConfig16(this, PCIConfigurationHeader.SubSystemID);
public ushort SubSystemID { get; private set; }

public byte IRQ => pciController.ReadConfig8(this, PCIConfigurationHeader.InterruptLineRegister);
public byte IRQ { get; private set; }

public ushort StatusRegister
{
get => pciController.ReadConfig16(this, PCIConfigurationHeader.StatusRegister);
set => pciController.WriteConfig16(this, PCIConfigurationHeader.StatusRegister, value);
get => Controller.ReadConfig16(this, PCIConfigurationHeader.StatusRegister);
set => Controller.WriteConfig16(this, PCIConfigurationHeader.StatusRegister, value);
}

public ushort CommandRegister
{
get => pciController.ReadConfig16(this, PCIConfigurationHeader.CommandRegister);
set => pciController.WriteConfig16(this, PCIConfigurationHeader.CommandRegister, value);
get => Controller.ReadConfig16(this, PCIConfigurationHeader.CommandRegister);
set => Controller.WriteConfig16(this, PCIConfigurationHeader.CommandRegister, value);
}

public BaseAddress[] BaseAddresses { get; private set; }

#endregion Properties

public PCIDevice(byte bus, byte slot, byte function)
Expand All @@ -68,26 +68,35 @@ public PCIDevice(byte bus, byte slot, byte function)

public override void Initialize()
{
pciController = Device.Parent.DeviceDriver as IPCIController;
if (pciController == null)
Controller = Device.Parent.DeviceDriver as IPCIController;
if (Controller == null)
return;

VendorID = Controller.ReadConfig16(this, PCIConfigurationHeader.VendorID);
DeviceID = Controller.ReadConfig16(this, PCIConfigurationHeader.DeviceID);
RevisionID = Controller.ReadConfig8(this, PCIConfigurationHeader.RevisionID);
ClassCode = Controller.ReadConfig8(this, PCIConfigurationHeader.ClassCode);
ProgIF = Controller.ReadConfig8(this, PCIConfigurationHeader.ProgrammingInterface);
SubClassCode = Controller.ReadConfig8(this, PCIConfigurationHeader.SubClassCode);
SubSystemVendorID = Controller.ReadConfig16(this, PCIConfigurationHeader.SubSystemVendorID);
SubSystemID = Controller.ReadConfig16(this, PCIConfigurationHeader.SubSystemID);
IRQ = Controller.ReadConfig8(this, PCIConfigurationHeader.InterruptLineRegister);

Device.Name = $"{Device.Parent.Name}/{Bus}.{Slot}.{Function}";
BaseAddresses = new BaseAddress[8];

for (byte i = 0; i < 6; i++)
{
var bar = (byte)(PCIConfigurationHeader.BaseAddressRegisterBase + i * 4);

var address = pciController.ReadConfig32(this, bar);
var address = Controller.ReadConfig32(this, bar);
if (address == 0)
continue;

HAL.DisableAllInterrupts();

pciController.WriteConfig32(this, bar, 0xFFFFFFFF);
var mask = pciController.ReadConfig32(this, bar);
pciController.WriteConfig32(this, bar, address);
Controller.WriteConfig32(this, bar, 0xFFFFFFFF);
var mask = Controller.ReadConfig32(this, bar);
Controller.WriteConfig32(this, bar, address);

HAL.EnableAllInterrupts();

Expand All @@ -111,14 +120,14 @@ public override void Initialize()
if ((StatusRegister & (byte)PCIStatus.Capability) != 0)
{
var capabilities = new List<PCICapability>();
var ptr = pciController.ReadConfig8(this, PCIConfigurationHeader.CapabilitiesPointer);
var ptr = Controller.ReadConfig8(this, PCIConfigurationHeader.CapabilitiesPointer);

while (ptr != 0)
{
var capability = pciController.ReadConfig8(this, ptr);
var capability = Controller.ReadConfig8(this, ptr);
capabilities.Add(new PCICapability(capability, ptr));

ptr = pciController.ReadConfig8(this, (byte)(ptr + 1));
ptr = Controller.ReadConfig8(this, (byte)(ptr + 1));
}

Capabilities = capabilities.ToArray();
Expand Down
Loading

0 comments on commit dfbf309

Please sign in to comment.