Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions api/hw/pci_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,31 @@
#include <vector>
#include <unordered_map>

#define PCI_CAP_ID_AF 0x13 /* PCI Advanced Features */
#define PCI_CAP_ID_MAX PCI_CAP_ID_AF
#define PCI_EXT_CAP_ID_PASID 0x1B /* Process Address Space ID */
#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID
/* PCI Register Config Space */
#define PCI_DEV_VEND_REG 0x00 /* for the 32 bit read of dev/vend */
#define PCI_VENDID_REG 0x00
#define PCI_DEVID_REG 0x02
#define PCI_CMD_REG 0x04
#define PCI_STATUS_REG 0x06
#define PCI_REVID_REG 0x08
#define PCI_PROGIF_REG 0x09
#define PCI_SUBCLASS_REG 0x0a
#define PCI_CLASS_REG 0x0b
#define PCI_CLSZ_REG 0x0c
#define PCI_LATTIM_REG 0x0d
#define PCI_HEADER_REG 0x0e
#define PCI_BIST_REG 0x0f
#define PCI_CAPABILITY_REG 0x34

#define PCI_COMMAND_IO 0x01
#define PCI_COMMAND_MEM 0x02
#define PCI_COMMAND_MASTER 0x04

#define PCI_CAP_ID_VNDR 0x09
#define PCI_CAP_ID_AF 0x13 /* PCI Advanced Features */
#define PCI_CAP_ID_MAX PCI_CAP_ID_AF
#define PCI_EXT_CAP_ID_PASID 0x1B /* Process Address Space ID */
#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID

namespace PCI {

Expand Down
13 changes: 12 additions & 1 deletion api/hw/pci_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@

namespace hw {

struct pcidev_info {
const uintptr_t pci_addr;
uint32_t vendor;
hw::PCI_Device::class_revision_t dev_class;
};

class PCI_manager {
public:
// a <...> driver is constructed from a PCI device,
// and returns a unique_ptr to itself
using NIC_driver = delegate< std::unique_ptr<hw::Nic> (PCI_Device&, uint16_t) >;
using Device_vector = std::vector<const hw::PCI_Device*>;
using Devinfo_vector = std::vector<pcidev_info>;
static void register_nic(uint16_t, uint16_t, NIC_driver);

using BLK_driver = delegate< std::unique_ptr<hw::Block_device> (PCI_Device&) >;
static void register_blk(uint16_t, uint16_t, BLK_driver);

static void init();
static void init_devices(uint8_t classcode);
static Device_vector devices();
/* Returns devices that were attempted to be initialized */
static Device_vector devices();
/* Returns all PCI device information except PCI bridges.
Useful for testing driver code outside of src. */
static const Devinfo_vector &devinfos();

private:
static void scan_bus(int bus);
Expand Down
21 changes: 1 addition & 20 deletions src/hw/pci_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,6 @@
#include <hw/pci_device.hpp>
#include <hw/msi.hpp>

/* PCI Register Config Space */
#define PCI_DEV_VEND_REG 0x00 /* for the 32 bit read of dev/vend */
#define PCI_VENDID_REG 0x00
#define PCI_DEVID_REG 0x02
#define PCI_CMD_REG 0x04
#define PCI_STATUS_REG 0x06
#define PCI_REVID_REG 0x08
#define PCI_PROGIF_REG 0x09
#define PCI_SUBCLASS_REG 0x0a
#define PCI_CLASS_REG 0x0b
#define PCI_CLSZ_REG 0x0c
#define PCI_LATTIM_REG 0x0d
#define PCI_HEADER_REG 0x0e
#define PCI_BIST_REG 0x0f

#define PCI_COMMAND_IO 0x01
#define PCI_COMMAND_MEM 0x02
#define PCI_COMMAND_MASTER 0x04

namespace hw {

static constexpr std::array<const char*,3> bridge_subclasses {
Expand Down Expand Up @@ -190,7 +171,7 @@ namespace hw {
//printf("read16 %#x status %#x\n", PCI_STATUS_REG, status);
if ((status & 0x10) == 0) return;
// this offset works for non-cardbus bridges
uint32_t offset = 0x34;
uint32_t offset = PCI_CAPABILITY_REG;
// read first capability
offset = read16(offset) & 0xff;
offset &= ~0x3; // lower 2 bits reserved
Expand Down
10 changes: 4 additions & 6 deletions src/hw/pci_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ using Driver_entry = std::pair<uint32_t, Driver>;
template <typename Driver>
using fixed_factory_t = std::vector<Driver_entry<Driver>>;

struct pcidev_info {
const uintptr_t pci_addr;
uint32_t vendor;
hw::PCI_Device::class_revision_t dev_class;
};
static std::vector<pcidev_info> devinfos_;

static std::vector<hw::PCI_Device> devices_;
static std::vector<Driver_entry<PCI_manager::NIC_driver>> nic_fact;
static std::vector<Driver_entry<PCI_manager::BLK_driver>> blk_fact;
Expand Down Expand Up @@ -75,6 +69,10 @@ PCI_manager::Device_vector PCI_manager::devices () {
return device_vec;
}

const PCI_manager::Devinfo_vector& PCI_manager::devinfos() {
return devinfos_;
}

void PCI_manager::scan_bus(const int bus)
{
INFO2("|");
Expand Down