diff --git a/Source/Mosa.BareMetal.HelloWorld/Apps/ShowISA.cs b/Source/Mosa.BareMetal.HelloWorld/Apps/ShowISA.cs index a587cef34d..8c0df84ce7 100644 --- a/Source/Mosa.BareMetal.HelloWorld/Apps/ShowISA.cs +++ b/Source/Mosa.BareMetal.HelloWorld/Apps/ShowISA.cs @@ -2,6 +2,7 @@ using System; using Mosa.DeviceDriver.ISA; +using Mosa.DeviceSystem.Framework; namespace Mosa.BareMetal.HelloWorld.Apps; @@ -15,7 +16,7 @@ public void Execute() { Console.Write("> Probing for ISA devices..."); - var isaDevices = Program.DeviceService.GetChildrenOf(Program.DeviceService.GetFirstDevice()); + var isaDevices = Program.DeviceService.GetAllDevices(DeviceBusType.ISA); Console.WriteLine("[Completed: " + isaDevices.Count + " found]"); foreach (var device in isaDevices) diff --git a/Source/Mosa.BareMetal.HelloWorld/Apps/ShowPCI.cs b/Source/Mosa.BareMetal.HelloWorld/Apps/ShowPCI.cs index ba2aa4561d..3d369d4cfc 100644 --- a/Source/Mosa.BareMetal.HelloWorld/Apps/ShowPCI.cs +++ b/Source/Mosa.BareMetal.HelloWorld/Apps/ShowPCI.cs @@ -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; @@ -14,7 +15,7 @@ public class ShowPCI : IApp public void Execute() { Console.Write("> Probing for PCI devices..."); - var devices = Program.DeviceService.GetDevices(); + var devices = Program.DeviceService.GetAllDevices(DeviceBusType.PCI); Console.WriteLine("[Completed: " + devices.Count + " found]"); foreach (var device in devices) @@ -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(); } } diff --git a/Source/Mosa.DeviceDriver/ISA/ISABus.cs b/Source/Mosa.DeviceDriver/ISA/ISABus.cs deleted file mode 100644 index f826f8b44e..0000000000 --- a/Source/Mosa.DeviceDriver/ISA/ISABus.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) MOSA Project. Licensed under the New BSD License. - -using System.Collections.Generic; -using Mosa.DeviceSystem.Framework; -using Mosa.DeviceSystem.Framework.ISA; -using Mosa.DeviceSystem.HardwareAbstraction; -using Mosa.Runtime; - -namespace Mosa.DeviceDriver.ISA; - -/// -/// A device driver whose sole purpose is to initialize all ISA devices in the system. -/// -public class ISABus : BaseDeviceDriver -{ - public override void Initialize() => Device.Name = "ISABus"; - - public override void Probe() => Device.Status = DeviceStatus.Available; - - public override void Start() - { - Device.Status = DeviceStatus.Online; - StartDevices(); - } - - public override bool OnInterrupt() => true; - - private void StartDevices() - { - HAL.DebugWriteLine("ISABus:StartDevices()"); - - var drivers = DeviceService.GetDeviceDrivers(DeviceBusType.ISA); - - foreach (var driver in drivers) - { - if (driver is not ISADeviceDriverRegistryEntry entry) - continue; - - HAL.DebugWrite(" > ISA Driver: "); - HAL.DebugWriteLine(entry.Name); - StartDevice(entry); - } - - HAL.DebugWriteLine("ISABus:StartDevices() [Exit]"); - } - - private void StartDevice(ISADeviceDriverRegistryEntry driverEntry) - { - var ioPortRegions = new List(); - var memoryRegions = new List(); - - ioPortRegions.Add(new IOPortRegion(driverEntry.BasePort, driverEntry.PortRange)); - - if (driverEntry.AltBasePort != 0x00) - ioPortRegions.Add(new IOPortRegion(driverEntry.AltBasePort, driverEntry.AltPortRange)); - - if (driverEntry.BaseAddress != 0x00) - memoryRegions.Add(new AddressRegion(new Pointer(driverEntry.BaseAddress), driverEntry.AddressRange)); - - var hardwareResources = new HardwareResources(ioPortRegions, memoryRegions, driverEntry.IRQ); - DeviceService.Initialize(driverEntry, Device, true, null, hardwareResources); - } -} diff --git a/Source/Mosa.DeviceDriver/PCI/VirtIO/VirtIODevice.cs b/Source/Mosa.DeviceDriver/PCI/VirtIO/VirtIODevice.cs index 6fb6c5cc89..83a34c5e99 100644 --- a/Source/Mosa.DeviceDriver/PCI/VirtIO/VirtIODevice.cs +++ b/Source/Mosa.DeviceDriver/PCI/VirtIO/VirtIODevice.cs @@ -46,7 +46,7 @@ public VirtIODevice(Device device) return; } - pciController = (IPCIController)device.Parent.Parent.DeviceDriver; + pciController = pciDevice.Controller; foreach (var capability in pciDevice.Capabilities) { diff --git a/Source/Mosa.DeviceDriver/Setup.cs b/Source/Mosa.DeviceDriver/Setup.cs index 966a909081..61b9a5b5c4 100644 --- a/Source/Mosa.DeviceDriver/Setup.cs +++ b/Source/Mosa.DeviceDriver/Setup.cs @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -147,7 +142,6 @@ public static class Setup { Name = "Intel440FX", Platform = PlatformArchitecture.X86AndX64, - BusType = DeviceBusType.PCI, VendorID = 0x8086, DeviceID = 0x1237, ClassCode = 0x06, @@ -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, @@ -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, @@ -182,7 +174,6 @@ public static class Setup { Name = "IntelGPIOController", Platform = PlatformArchitecture.X86AndX64, - BusType = DeviceBusType.PCI, VendorID = 0x8086, DeviceID = 0x0934, ClassCode = 0X0C, @@ -197,7 +188,6 @@ public static class Setup { Name = "IntelHSUART", Platform = PlatformArchitecture.X86AndX64, - BusType = DeviceBusType.PCI, VendorID = 0x8086, DeviceID = 0x0936, ClassCode = 0X07, @@ -212,7 +202,6 @@ public static class Setup { Name = "PCIIDEInterface", Platform = PlatformArchitecture.X86AndX64, - BusType = DeviceBusType.PCI, VendorID = 0x8086, DeviceID = 0x7010, ClassCode = 0X01, @@ -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, @@ -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, diff --git a/Source/Mosa.DeviceSystem/Framework/Device.cs b/Source/Mosa.DeviceSystem/Framework/Device.cs index e94dc394c7..2b5d4dec13 100644 --- a/Source/Mosa.DeviceSystem/Framework/Device.cs +++ b/Source/Mosa.DeviceSystem/Framework/Device.cs @@ -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; } diff --git a/Source/Mosa.DeviceSystem/Framework/PCI/PCIField.cs b/Source/Mosa.DeviceSystem/Framework/PCI/PCIField.cs index 4f53facd21..3895a2d905 100644 --- a/Source/Mosa.DeviceSystem/Framework/PCI/PCIField.cs +++ b/Source/Mosa.DeviceSystem/Framework/PCI/PCIField.cs @@ -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 } diff --git a/Source/Mosa.DeviceSystem/PCI/PCIDevice.cs b/Source/Mosa.DeviceSystem/PCI/PCIDevice.cs index 2d038634bf..60af79135a 100644 --- a/Source/Mosa.DeviceSystem/PCI/PCIDevice.cs +++ b/Source/Mosa.DeviceSystem/PCI/PCIDevice.cs @@ -13,8 +13,6 @@ namespace Mosa.DeviceSystem.PCI; /// public class PCIDevice : BaseDeviceDriver { - private IPCIController pciController; - #region Properties public byte Bus { get; } @@ -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) @@ -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(); @@ -111,14 +120,14 @@ public override void Initialize() if ((StatusRegister & (byte)PCIStatus.Capability) != 0) { var capabilities = new List(); - 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(); diff --git a/Source/Mosa.DeviceSystem/Services/DeviceService.cs b/Source/Mosa.DeviceSystem/Services/DeviceService.cs index 1a59217f5a..dcc25f3deb 100644 --- a/Source/Mosa.DeviceSystem/Services/DeviceService.cs +++ b/Source/Mosa.DeviceSystem/Services/DeviceService.cs @@ -88,7 +88,7 @@ protected override void Initialize() { var entry = (GenericDeviceDriverRegistryEntry)driver; - HAL.DebugWriteLine(" > Generic Driver: "); + HAL.DebugWriteLine(" > Generic Device: "); HAL.DebugWriteLine(entry.Name); var ioPortRegions = new List(); @@ -101,19 +101,20 @@ protected override void Initialize() HAL.DebugWriteLine("DeviceService::Initialize() [Exit]"); } - public Device Initialize(DeviceDriverRegistryEntry deviceDriverRegistryEntry, Device parent, bool autoStart = true, BaseDeviceConfiguration configuration = null, HardwareResources resources = null) + public Device Initialize(DeviceDriverRegistryEntry deviceDriverRegistryEntry, Device parent, bool autoStart = true, BaseDeviceConfiguration configuration = null, HardwareResources resources = null, DeviceBusType fallbackBusType = DeviceBusType.None) { - var deviceDriver = deviceDriverRegistryEntry.Factory(); + var deviceDriver = deviceDriverRegistryEntry?.Factory(); - return Initialize(deviceDriver, parent, autoStart, configuration, resources, deviceDriverRegistryEntry); + return Initialize(deviceDriver, parent, autoStart, configuration, resources, deviceDriverRegistryEntry, fallbackBusType); } - public Device Initialize(BaseDeviceDriver deviceDriver, Device parent, bool autoStart = true, BaseDeviceConfiguration configuration = null, HardwareResources resources = null, DeviceDriverRegistryEntry deviceDriverRegistryEntry = null) + public Device Initialize(BaseDeviceDriver deviceDriver, Device parent, bool autoStart = true, BaseDeviceConfiguration configuration = null, HardwareResources resources = null, DeviceDriverRegistryEntry deviceDriverRegistryEntry = null, DeviceBusType fallbackBusType = DeviceBusType.None) { - HAL.DebugWriteLine("DeviceService:Initialize()"); + HAL.DebugWriteLine("DeviceService::Initialize(BaseDeviceDriver)"); var device = new Device { + BusType = deviceDriverRegistryEntry?.BusType ?? fallbackBusType, DeviceDriver = deviceDriver, DeviceDriverRegistryEntry = deviceDriverRegistryEntry, Status = DeviceStatus.Initializing, @@ -126,7 +127,7 @@ public Device Initialize(BaseDeviceDriver deviceDriver, Device parent, bool auto if (autoStart) StartDevice(device); - HAL.DebugWriteLine("DeviceService:Initialize() [Exit]"); + HAL.DebugWriteLine("DeviceService::Initialize(BaseDeviceDriver) [Exit]"); return device; } @@ -137,7 +138,7 @@ public Device Initialize(BaseDeviceDriver deviceDriver, Device parent, bool auto /// The device. private void StartDevice(Device device) { - HAL.DebugWriteLine("DeviceService:StartDevice()"); + HAL.DebugWriteLine("DeviceService::StartDevice()"); lock (sync) { @@ -147,22 +148,22 @@ private void StartDevice(Device device) } device.Status = DeviceStatus.Initializing; - device.DeviceDriver.Setup(device); + device.DeviceDriver?.Setup(device); if (device.Status == DeviceStatus.Initializing) { - device.DeviceDriver.Initialize(); + device.DeviceDriver?.Initialize(); HAL.DebugWrite(" # Initialized: "); HAL.DebugWriteLine(device.Name); if (device.Status == DeviceStatus.Initializing) { - device.DeviceDriver.Probe(); + device.DeviceDriver?.Probe(); if (device.Status == DeviceStatus.Available) { - device.DeviceDriver.Start(); + device.DeviceDriver?.Start(); AddInterruptHandler(device); } @@ -171,7 +172,7 @@ private void StartDevice(Device device) ServiceManager.AddEvent(new ServiceEvent(ServiceEventType.Start, device)); - HAL.DebugWriteLine("DeviceService:StartDevice():Exit"); + HAL.DebugWriteLine("DeviceService::StartDevice():Exit"); } #endregion Initialize Devices Drivers @@ -280,6 +281,20 @@ public List GetAllDevices() } } + public List GetAllDevices(DeviceBusType busType) + { + lock (sync) + { + var list = new List(devices.Count); + + foreach (var device in devices) + if (device.BusType == busType) + list.Add(device); + + return list; + } + } + public bool CheckExists(Device parent, ulong componentID) { lock (sync) diff --git a/Source/Mosa.DeviceSystem/Services/ISADeviceService.cs b/Source/Mosa.DeviceSystem/Services/ISADeviceService.cs new file mode 100644 index 0000000000..84d173c583 --- /dev/null +++ b/Source/Mosa.DeviceSystem/Services/ISADeviceService.cs @@ -0,0 +1,47 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +using System.Collections.Generic; +using Mosa.DeviceSystem.Framework; +using Mosa.DeviceSystem.Framework.ISA; +using Mosa.DeviceSystem.HardwareAbstraction; +using Mosa.Runtime; + +namespace Mosa.DeviceSystem.Services; + +/// +/// Initializes and starts all ISA devices in the system. +/// +public class ISADeviceService : BaseService +{ + protected override void Initialize() + { + HAL.DebugWriteLine("ISADeviceService::Initialize()"); + + var deviceService = ServiceManager.GetFirstService(); + var drivers = deviceService.GetDeviceDrivers(DeviceBusType.ISA); + + foreach (var driver in drivers) + { + var entry = (ISADeviceDriverRegistryEntry)driver; + + HAL.DebugWriteLine(" > ISA Device: "); + HAL.DebugWriteLine(entry.Name); + + var ioPortRegions = new List(); + var memoryRegions = new List(); + + ioPortRegions.Add(new IOPortRegion(entry.BasePort, entry.PortRange)); + + if (entry.AltBasePort != 0x00) + ioPortRegions.Add(new IOPortRegion(entry.AltBasePort, entry.AltPortRange)); + + if (entry.BaseAddress != 0x00) + memoryRegions.Add(new AddressRegion(new Pointer(entry.BaseAddress), entry.AddressRange)); + + var hardwareResources = new HardwareResources(ioPortRegions, memoryRegions, entry.IRQ); + deviceService.Initialize(entry, null, true, null, hardwareResources, DeviceBusType.ISA); + } + + HAL.DebugWriteLine("ISADeviceService::Initialize() [Exit]"); + } +} diff --git a/Source/Mosa.DeviceSystem/Services/PCIDeviceService.cs b/Source/Mosa.DeviceSystem/Services/PCIDeviceService.cs index a1927be28f..96d311a257 100644 --- a/Source/Mosa.DeviceSystem/Services/PCIDeviceService.cs +++ b/Source/Mosa.DeviceSystem/Services/PCIDeviceService.cs @@ -1,10 +1,10 @@ // Copyright (c) MOSA Project. Licensed under the New BSD License. using System.Collections.Generic; -using System.Runtime.CompilerServices; using Mosa.DeviceSystem.Framework; using Mosa.DeviceSystem.Framework.PCI; using Mosa.DeviceSystem.HardwareAbstraction; +using Mosa.DeviceSystem.Misc; using Mosa.DeviceSystem.PCI; namespace Mosa.DeviceSystem.Services; @@ -16,23 +16,25 @@ public class PCIDeviceService : BaseService { private DeviceService deviceService; - protected override void Initialize() => deviceService = ServiceManager.GetFirstService(); - - [MethodImpl(MethodImplOptions.NoInlining)] - public override void PostEvent(ServiceEvent serviceEvent) + protected override void Initialize() { - var device = MatchEvent(serviceEvent, ServiceEventType.Start); - if (device == null) + HAL.DebugWriteLine("PCIDeviceService::Initialize()"); + + deviceService = ServiceManager.GetFirstService(); + + var controllerDevice = deviceService.GetFirstDevice(); + if (controllerDevice?.DeviceDriver is not IPCIController pciController) return; - var pciController = device.DeviceDriver as IPCIController; for (byte bus = 0; bus < byte.MaxValue; bus++) for (byte slot = 0; slot < 16; slot++) for (byte function = 0; function < 7; function++) - CreatePCIDevice(bus, slot, function, device, pciController); + CreateDevice(bus, slot, function, controllerDevice, pciController); + + HAL.DebugWriteLine("PCIDeviceService::Initialize() [Exit]"); } - private void CreatePCIDevice(byte bus, byte slot, byte function, Device device, IPCIController pciController) + private void CreateDevice(byte bus, byte slot, byte function, Device device, IPCIController pciController) { var pciDevice = new PCIDevice(bus, slot, function); var value = pciController.ReadConfig32(pciDevice, 0); @@ -62,10 +64,6 @@ private void CreatePCIDevice(byte bus, byte slot, byte function, Device device, matchPriority = priority; } - // No driver found - if (matchedDriver == null) - return; - StartDevice(matchedDriver, parentDevice, pciDevice); } @@ -88,10 +86,21 @@ private void StartDevice(PCIDeviceDriverRegistryEntry driver, Device device, PCI var hardwareResources = new HardwareResources(ioPortRegions, memoryRegions, pciDevice.IRQ); - HAL.DebugWriteLine(" > PCI Driver: "); + // No driver was found previously + if (driver == null) + { + HAL.DebugWriteLine(" > Unknown PCI Device: "); + HAL.DebugWriteLine(pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x")); + + // It must be set to auto start, or else the device isn't registered in the framework + deviceService.Initialize(null, device, true, null, hardwareResources, DeviceBusType.PCI); + return; + } + + HAL.DebugWriteLine(" > PCI Device: "); HAL.DebugWriteLine(driver.Name); - deviceService.Initialize(driver, device, driver.AutoStart, null, hardwareResources); + deviceService.Initialize(driver, device, driver.AutoStart, null, hardwareResources, DeviceBusType.PCI); } private static bool HasFlag(PCIField list, PCIField match) => (int)(list & match) != 0; diff --git a/Source/Mosa.DeviceSystem/Services/ServiceManager.cs b/Source/Mosa.DeviceSystem/Services/ServiceManager.cs index 7c5e8a9281..10905e488b 100644 --- a/Source/Mosa.DeviceSystem/Services/ServiceManager.cs +++ b/Source/Mosa.DeviceSystem/Services/ServiceManager.cs @@ -20,26 +20,26 @@ public sealed class ServiceManager public void AddService(BaseService service) { - HAL.DebugWriteLine("ServiceManager:AddService()"); + HAL.DebugWriteLine("ServiceManager::AddService()"); lock (lockServices) Services.Add(service); service.Start(this); - HAL.DebugWriteLine("ServiceManager:AddService() [Exit]"); + HAL.DebugWriteLine("ServiceManager::AddService() [Exit]"); } public void AddEvent(ServiceEvent serviceEvent) { - HAL.DebugWriteLine("ServiceManager:AddEvent()"); + HAL.DebugWriteLine("ServiceManager::AddEvent()"); lock (lockEvents) Events.Add(serviceEvent); SendEvents(); - HAL.DebugWriteLine("ServiceManager:AddEvent() [Exit]"); + HAL.DebugWriteLine("ServiceManager::AddEvent() [Exit]"); } public List GetService() where T : BaseService @@ -80,7 +80,7 @@ public List GetAllServices() [MethodImpl(MethodImplOptions.NoInlining)] private void SendEvents() { - HAL.DebugWriteLine("ServiceManager:SendEvents()"); + HAL.DebugWriteLine("ServiceManager::SendEvents()"); while (true) { @@ -98,13 +98,13 @@ private void SendEvents() DispatchEvents(serviceEvent); } - HAL.DebugWriteLine("ServiceManager:SendEvents() [Exit]"); + HAL.DebugWriteLine("ServiceManager::SendEvents() [Exit]"); } [MethodImpl(MethodImplOptions.NoInlining)] private void DispatchEvents(ServiceEvent serviceEvent) { - HAL.DebugWriteLine("ServiceManager:DispatchEvents()"); + HAL.DebugWriteLine("ServiceManager::DispatchEvents()"); var i = 0; @@ -125,6 +125,6 @@ private void DispatchEvents(ServiceEvent serviceEvent) service.PostEvent(serviceEvent); } - HAL.DebugWriteLine("ServiceManager:DispatchEvents() [Exit]"); + HAL.DebugWriteLine("ServiceManager::DispatchEvents() [Exit]"); } } diff --git a/Source/Mosa.Kernel.BareMetal/Startup.cs b/Source/Mosa.Kernel.BareMetal/Startup.cs index 3c0a727404..6a0be9cb90 100644 --- a/Source/Mosa.Kernel.BareMetal/Startup.cs +++ b/Source/Mosa.Kernel.BareMetal/Startup.cs @@ -149,6 +149,7 @@ public static void EntryPoint() var serviceManager = new ServiceManager(); var diskDeviceService = new DiskDeviceService(); var partitionService = new PartitionService(); + var isaDeviceService = new ISADeviceService(); var pciDeviceService = new PCIDeviceService(); var pcService = new PCService(); Kernel.ServiceManager = serviceManager; @@ -174,21 +175,20 @@ public static void EntryPoint() Console.WriteLine(" [Completed]"); Console.ForegroundColor = ConsoleColor.LightGreen; - Console.Write("> PCI Device Service..."); - serviceManager.AddService(pciDeviceService); + Console.Write("> ISA Device Service..."); + serviceManager.AddService(isaDeviceService); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(" [Completed]"); Console.ForegroundColor = ConsoleColor.LightGreen; - Console.Write("> PC Service..."); - serviceManager.AddService(pcService); + Console.Write("> PCI Device Service..."); + serviceManager.AddService(pciDeviceService); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(" [Completed]"); - // Future: We shouldn't be directly initializing ISA devices on non-x86 platforms (even if it doesn't do anything on those) Console.ForegroundColor = ConsoleColor.LightGreen; - Console.Write("> ISABus..."); - deviceService.Initialize(new ISABus(), null); + Console.Write("> PC Service..."); + serviceManager.AddService(pcService); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(" [Completed]");