Permalink
Browse files

Files for part 10, https://www.youtube.com/watch?v=GE7iO2vlLD4

  • Loading branch information...
Viktor Engelmann Viktor Engelmann
Viktor Engelmann authored and Viktor Engelmann committed Jul 4, 2016
1 parent 349d2f9 commit 740241ab47a8111018b7b27b44c8613f5d6fa3c6
Showing with 196 additions and 0 deletions.
  1. +1 −0 include/common/types.h
  2. +60 −0 include/hardwarecommunication/pci.h
  3. +1 −0 makefile
  4. +131 −0 src/hardwarecommunication/pci.cpp
  5. +3 −0 src/kernel.cpp
View
@@ -15,6 +15,7 @@ namespace myos
typedef long long int int64_t;
typedef unsigned long long int uint64_t;
typedef const char* string;
}
}
@@ -0,0 +1,60 @@
#ifndef __MYOS__HARDWARECOMMUNICATION__PCI_H
#define __MYOS__HARDWARECOMMUNICATION__PCI_H
#include <hardwarecommunication/port.h>
#include <drivers/driver.h>
#include <common/types.h>
#include <hardwarecommunication/interrupts.h>
namespace myos
{
namespace hardwarecommunication
{
class PeripheralComponentInterconnectDeviceDescriptor
{
public:
myos::common::uint32_t portBase;
myos::common::uint32_t interrupt;
myos::common::uint16_t bus;
myos::common::uint16_t device;
myos::common::uint16_t function;
myos::common::uint16_t vendor_id;
myos::common::uint16_t device_id;
myos::common::uint8_t class_id;
myos::common::uint8_t subclass_id;
myos::common::uint8_t interface_id;
myos::common::uint8_t revision;
PeripheralComponentInterconnectDeviceDescriptor();
~PeripheralComponentInterconnectDeviceDescriptor();
};
class PeripheralComponentInterconnectController
{
Port32Bit dataPort;
Port32Bit commandPort;
public:
PeripheralComponentInterconnectController();
~PeripheralComponentInterconnectController();
myos::common::uint32_t Read(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function, myos::common::uint32_t registeroffset);
void Write(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function, myos::common::uint32_t registeroffset, myos::common::uint32_t value);
bool DeviceHasFunctions(myos::common::uint16_t bus, myos::common::uint16_t device);
void SelectDrivers(myos::drivers::DriverManager* driverManager);
PeripheralComponentInterconnectDeviceDescriptor GetDeviceDescriptor(myos::common::uint16_t bus, myos::common::uint16_t device, myos::common::uint16_t function);
};
}
}
#endif
View
@@ -12,6 +12,7 @@ objects = obj/loader.o \
obj/hardwarecommunication/port.o \
obj/hardwarecommunication/interruptstubs.o \
obj/hardwarecommunication/interrupts.o \
obj/hardwarecommunication/pci.o \
obj/drivers/keyboard.o \
obj/drivers/mouse.o \
obj/kernel.o
@@ -0,0 +1,131 @@
#include <hardwarecommunication/pci.h>
using namespace myos::common;
using namespace myos::drivers;
using namespace myos::hardwarecommunication;
PeripheralComponentInterconnectDeviceDescriptor::PeripheralComponentInterconnectDeviceDescriptor()
{
}
PeripheralComponentInterconnectDeviceDescriptor::~PeripheralComponentInterconnectDeviceDescriptor()
{
}
PeripheralComponentInterconnectController::PeripheralComponentInterconnectController()
: dataPort(0xCFC),
commandPort(0xCF8)
{
}
PeripheralComponentInterconnectController::~PeripheralComponentInterconnectController()
{
}
uint32_t PeripheralComponentInterconnectController::Read(uint16_t bus, uint16_t device, uint16_t function, uint32_t registeroffset)
{
uint32_t id =
0x1 << 31
| ((bus & 0xFF) << 16)
| ((device & 0x1F) << 11)
| ((function & 0x07) << 8)
| (registeroffset & 0xFC);
commandPort.Write(id);
uint32_t result = dataPort.Read();
return result >> (8* (registeroffset % 4));
}
void PeripheralComponentInterconnectController::Write(uint16_t bus, uint16_t device, uint16_t function, uint32_t registeroffset, uint32_t value)
{
uint32_t id =
0x1 << 31
| ((bus & 0xFF) << 16)
| ((device & 0x1F) << 11)
| ((function & 0x07) << 8)
| (registeroffset & 0xFC);
commandPort.Write(id);
dataPort.Write(value);
}
bool PeripheralComponentInterconnectController::DeviceHasFunctions(common::uint16_t bus, common::uint16_t device)
{
return Read(bus, device, 0, 0x0E) & (1<<7);
}
void printf(char* str);
void printfHex(uint8_t);
void PeripheralComponentInterconnectController::SelectDrivers(DriverManager* driverManager)
{
for(int bus = 0; bus < 8; bus++)
{
for(int device = 0; device < 32; device++)
{
int numFunctions = DeviceHasFunctions(bus, device) ? 8 : 1;
for(int function = 0; function < numFunctions; function++)
{
PeripheralComponentInterconnectDeviceDescriptor dev = GetDeviceDescriptor(bus, device, function);
if(dev.vendor_id == 0x0000 || dev.vendor_id == 0xFFFF)
break;
printf("PCI BUS ");
printfHex(bus & 0xFF);
printf(", DEVICE ");
printfHex(device & 0xFF);
printf(", FUNCTION ");
printfHex(function & 0xFF);
printf(" = VENDOR ");
printfHex((dev.vendor_id & 0xFF00) >> 8);
printfHex(dev.vendor_id & 0xFF);
printf(", DEVICE ");
printfHex((dev.device_id & 0xFF00) >> 8);
printfHex(dev.device_id & 0xFF);
printf("\n");
}
}
}
}
PeripheralComponentInterconnectDeviceDescriptor PeripheralComponentInterconnectController::GetDeviceDescriptor(uint16_t bus, uint16_t device, uint16_t function)
{
PeripheralComponentInterconnectDeviceDescriptor result;
result.bus = bus;
result.device = device;
result.function = function;
result.vendor_id = Read(bus, device, function, 0x00);
result.device_id = Read(bus, device, function, 0x02);
result.class_id = Read(bus, device, function, 0x0b);
result.subclass_id = Read(bus, device, function, 0x0a);
result.interface_id = Read(bus, device, function, 0x09);
result.revision = Read(bus, device, function, 0x08);
result.interrupt = Read(bus, device, function, 0x3c);
return result;
}
View
@@ -2,6 +2,7 @@
#include <common/types.h>
#include <gdt.h>
#include <hardwarecommunication/interrupts.h>
#include <hardwarecommunication/pci.h>
#include <drivers/driver.h>
#include <drivers/keyboard.h>
#include <drivers/mouse.h>
@@ -147,6 +148,8 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
MouseDriver mouse(&interrupts, &mousehandler);
drvManager.AddDriver(&mouse);
PeripheralComponentInterconnectController PCIController;
PCIController.SelectDrivers(&drvManager);
printf("Initializing Hardware, Stage 2\n");
drvManager.ActivateAll();

0 comments on commit 740241a

Please sign in to comment.