-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CGS Update (part 2) #3034
CGS Update (part 2) #3034
Changes from 17 commits
c4de75d
f6937c9
4b3ee4d
e47e504
5d9e075
2feb8c1
dda9df1
7bad71f
289270e
a3540b0
f064006
2036880
f1bedae
d290822
6f38e13
da5e7ac
b00577e
8d6fa08
c5b44ce
a770c34
155b893
f047cd9
98406c5
01342ae
7f19fbd
d8f0aad
d117063
98bb00f
3375aab
ac464df
b84f8a4
e80d1e0
e0e7f0b
e58fdc5
fb59494
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
using Cosmos.HAL.PCIE; | ||
using Cosmos.HAL; | ||
using System.Collections.Generic; | ||
using System; | ||
using Cosmos.Core; | ||
|
||
namespace Cosmos.HAL.PCIE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. namespace and directory dont match |
||
{ | ||
public class PCIDevice | ||
{ | ||
public ushort Bus; | ||
public ushort Slot; | ||
public ushort Function; | ||
public ushort VendorID; | ||
|
||
public ushort DeviceID; | ||
|
||
public byte ClassID; | ||
public byte SubClassID; | ||
public byte ProgIF; | ||
public byte IRQ; | ||
|
||
public uint Bar0; | ||
public uint Bar1; | ||
public uint Bar2; | ||
public uint Bar3; | ||
public uint Bar4; | ||
public uint Bar5; | ||
|
||
//PCI Express | ||
public ushort Segment; | ||
public bool IsPCIEDevice; | ||
|
||
public void WriteRegister(ushort Register, ushort Value) | ||
{ | ||
PCI.WriteRegister16(Bus, Slot, Function, (byte)Register, (ushort)(ReadRegister(Register) | Value)); | ||
} | ||
|
||
public ushort ReadRegister(ushort Register) | ||
{ | ||
return PCI.ReadRegister16(Bus, Slot, Function, (byte)Register); | ||
} | ||
} | ||
|
||
public static unsafe class PCI | ||
{ | ||
public static List<PCIDevice> Devices; | ||
|
||
public static PCIDevice GetDevice(ushort VendorID, ushort DeviceID) | ||
{ | ||
for (int i = 0; i < Devices.Count; i++) | ||
{ | ||
if ( | ||
Devices[i] != null && | ||
Devices[i].VendorID == VendorID && | ||
Devices[i].DeviceID == DeviceID | ||
) | ||
{ | ||
return Devices[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static PCIDevice GetDevice(byte ClassID, byte SubClassID, byte ProgIF) | ||
{ | ||
for (int i = 0; i < Devices.Count; i++) | ||
{ | ||
if ( | ||
Devices[i] != null && | ||
Devices[i].ClassID == ClassID && | ||
Devices[i].SubClassID == SubClassID && | ||
Devices[i].ProgIF == ProgIF | ||
) | ||
{ | ||
return Devices[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static void Initialise() | ||
{ | ||
Devices = new List<PCIDevice>(); | ||
if ((GetHeaderType(0x0, 0x0, 0x0) & 0x80) == 0) | ||
{ | ||
CheckBus(0); | ||
} | ||
else | ||
{ | ||
for (ushort fn = 0; fn < 8; fn++) | ||
{ | ||
if (GetVendorID(0x0, 0x0, fn) != 0xFFFF) | ||
break; | ||
|
||
CheckBus(fn); | ||
} | ||
} | ||
|
||
PCIExpress.Initialize(); | ||
|
||
Console.Write("[PCI] PCI Initialized. "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this writes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi, leaving them there is best practice because you will have feedback of the PCI and PCIE initialization There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we allow the user to enable this feedback when they want it but also give them a way to disable it? |
||
Console.Write(((ulong)Devices.Count).ToString()); | ||
Console.WriteLine(" Devices"); | ||
} | ||
|
||
public static void CheckBus(ushort Bus) | ||
{ | ||
for (ushort slot = 0; slot < 32; slot++) | ||
{ | ||
ushort vendorID = GetVendorID(Bus, slot, 0); | ||
if (vendorID == 0xFFFF) | ||
{ | ||
continue; | ||
} | ||
|
||
PCIDevice device = new PCIDevice(); | ||
device.Bus = Bus; | ||
device.Slot = slot; | ||
device.Function = 0; | ||
device.VendorID = vendorID; | ||
device.Segment = 0; | ||
device.IsPCIEDevice = false; | ||
|
||
device.Bar0 = ReadRegister32(device.Bus, device.Slot, device.Function, 0x10); | ||
device.Bar1 = ReadRegister32(device.Bus, device.Slot, device.Function, 0x14); | ||
device.Bar2 = ReadRegister32(device.Bus, device.Slot, device.Function, 0x18); | ||
device.Bar3 = ReadRegister32(device.Bus, device.Slot, device.Function, 0x1C); | ||
device.Bar4 = ReadRegister32(device.Bus, device.Slot, device.Function, 0x20); | ||
device.Bar5 = ReadRegister32(device.Bus, device.Slot, device.Function, 0x24); | ||
|
||
device.ClassID = ReadRegister8(device.Bus, device.Slot, device.Function, 11); | ||
device.SubClassID = ReadRegister8(device.Bus, device.Slot, device.Function, 10); | ||
device.ProgIF = ReadRegister8(device.Bus, device.Slot, device.Function, 9); | ||
device.IRQ = (byte)(0x20 + ReadRegister8(device.Bus, device.Slot, device.Function, 60)); | ||
|
||
device.DeviceID = ReadRegister16(device.Bus, device.Slot, device.Function, 2); | ||
|
||
//Console.WriteLine($"[PCI {device.Bus}:{device.Slot}:{device.Function}] {VendorID.GetName(device.VendorID)} {ClassID.GetName(device.ClassID)}"); | ||
|
||
Devices.Add(device); | ||
|
||
if (device.ClassID == 0x06 && device.SubClassID == 0x04) | ||
{ | ||
CheckBus(ReadRegister8(device.Bus, device.Slot, device.Function, 25)); | ||
} | ||
} | ||
} | ||
|
||
public static void WriteRegister32(ushort Bus, ushort Slot, ushort Function, byte aRegister, uint Value) | ||
{ | ||
uint xAddr = GetAddressBase(Bus, Slot, Function) | ((uint)(aRegister & 0xFC)); | ||
IOPort.Write32(0xCF8, xAddr); | ||
IOPort.Write32(0xCFC, Value); | ||
} | ||
|
||
public static uint ReadRegister32(ushort Bus, ushort Slot, ushort Function, byte aRegister) | ||
{ | ||
uint xAddr = PCI.GetAddressBase(Bus, Slot, Function) | ((uint)(aRegister & 0xFC)); | ||
IOPort.Write32(0xCF8, xAddr); | ||
return IOPort.Read32(0xCFC); | ||
} | ||
|
||
public static ushort ReadRegister16(ushort Bus, ushort Slot, ushort Function, byte aRegister) | ||
{ | ||
uint xAddr = PCI.GetAddressBase(Bus, Slot, Function) | ((uint)(aRegister & 0xFC)); | ||
IOPort.Write32(0xCF8, xAddr); | ||
return (ushort)(IOPort.Read32(0xCFC) >> ((aRegister % 4) * 8) & 0xFFFF); | ||
} | ||
|
||
public static byte ReadRegister8(ushort Bus, ushort Slot, ushort Function, byte aRegister) | ||
{ | ||
uint xAddr = PCI.GetAddressBase(Bus, Slot, Function) | ((uint)(aRegister & 0xFC)); | ||
IOPort.Write32(0xCF8, xAddr); | ||
return ((byte)(IOPort.Read32(0xCFC) >> ((aRegister % 4) * 8) & 0xFF)); | ||
} | ||
|
||
public static void WriteRegister16(ushort Bus, ushort Slot, ushort Function, byte aRegister, ushort Value) | ||
{ | ||
uint xAddr = GetAddressBase(Bus, Slot, Function) | ((uint)(aRegister & 0xFC)); | ||
IOPort.Write32(0xCF8, xAddr); | ||
IOPort.Write16(0xCFC, Value); | ||
} | ||
|
||
public static ushort GetVendorID(ushort Bus, ushort Slot, ushort Function) | ||
{ | ||
uint xAddr = GetAddressBase(Bus, Slot, Function) | 0x0 & 0xFC; | ||
IOPort.Write32(0xCF8, xAddr); | ||
return (ushort)(IOPort.Read32(0xCFC) >> ((0x0 % 4) * 8) & 0xFFFF); | ||
} | ||
|
||
public static ushort GetHeaderType(ushort Bus, ushort Slot, ushort Function) | ||
{ | ||
uint xAddr = GetAddressBase(Bus, Slot, Function) | 0xE & 0xFC; | ||
IOPort.Write32(0xCF8, xAddr); | ||
return (byte)(IOPort.Read32(0xCFC) >> ((0xE % 4) * 8) & 0xFF); | ||
} | ||
|
||
public static uint GetAddressBase(ushort Bus, uint Slot, uint Function) | ||
{ | ||
return (uint)(0x80000000 | (Bus << 16) | ((Slot & 0x1F) << 11) | ((Function & 0x07) << 8)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should no longer be an issue and can be checked