Skip to content

Commit 20c3133

Browse files
authored
Merge pull request #2341 from torgeiru/driver-testing-enhancement
Driver testing enhancement and minor refactoring
2 parents cce171b + bf1e988 commit 20c3133

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

api/hw/pci_device.hpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,31 @@
2222
#include <vector>
2323
#include <unordered_map>
2424

25-
#define PCI_CAP_ID_AF 0x13 /* PCI Advanced Features */
26-
#define PCI_CAP_ID_MAX PCI_CAP_ID_AF
27-
#define PCI_EXT_CAP_ID_PASID 0x1B /* Process Address Space ID */
28-
#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID
25+
/* PCI Register Config Space */
26+
#define PCI_DEV_VEND_REG 0x00 /* for the 32 bit read of dev/vend */
27+
#define PCI_VENDID_REG 0x00
28+
#define PCI_DEVID_REG 0x02
29+
#define PCI_CMD_REG 0x04
30+
#define PCI_STATUS_REG 0x06
31+
#define PCI_REVID_REG 0x08
32+
#define PCI_PROGIF_REG 0x09
33+
#define PCI_SUBCLASS_REG 0x0a
34+
#define PCI_CLASS_REG 0x0b
35+
#define PCI_CLSZ_REG 0x0c
36+
#define PCI_LATTIM_REG 0x0d
37+
#define PCI_HEADER_REG 0x0e
38+
#define PCI_BIST_REG 0x0f
39+
#define PCI_CAPABILITY_REG 0x34
40+
41+
#define PCI_COMMAND_IO 0x01
42+
#define PCI_COMMAND_MEM 0x02
43+
#define PCI_COMMAND_MASTER 0x04
44+
45+
#define PCI_CAP_ID_VNDR 0x09
46+
#define PCI_CAP_ID_AF 0x13 /* PCI Advanced Features */
47+
#define PCI_CAP_ID_MAX PCI_CAP_ID_AF
48+
#define PCI_EXT_CAP_ID_PASID 0x1B /* Process Address Space ID */
49+
#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID
2950

3051
namespace PCI {
3152

api/hw/pci_manager.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@
2525

2626
namespace hw {
2727

28+
struct pcidev_info {
29+
const uintptr_t pci_addr;
30+
uint32_t vendor;
31+
hw::PCI_Device::class_revision_t dev_class;
32+
};
33+
2834
class PCI_manager {
2935
public:
3036
// a <...> driver is constructed from a PCI device,
3137
// and returns a unique_ptr to itself
3238
using NIC_driver = delegate< std::unique_ptr<hw::Nic> (PCI_Device&, uint16_t) >;
3339
using Device_vector = std::vector<const hw::PCI_Device*>;
40+
using Devinfo_vector = std::vector<pcidev_info>;
3441
static void register_nic(uint16_t, uint16_t, NIC_driver);
3542

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

3946
static void init();
4047
static void init_devices(uint8_t classcode);
41-
static Device_vector devices();
48+
/* Returns devices that were attempted to be initialized */
49+
static Device_vector devices();
50+
/* Returns all PCI device information except PCI bridges.
51+
Useful for testing driver code outside of src. */
52+
static const Devinfo_vector &devinfos();
4253

4354
private:
4455
static void scan_bus(int bus);

src/hw/pci_device.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,6 @@
2121
#include <hw/pci_device.hpp>
2222
#include <hw/msi.hpp>
2323

24-
/* PCI Register Config Space */
25-
#define PCI_DEV_VEND_REG 0x00 /* for the 32 bit read of dev/vend */
26-
#define PCI_VENDID_REG 0x00
27-
#define PCI_DEVID_REG 0x02
28-
#define PCI_CMD_REG 0x04
29-
#define PCI_STATUS_REG 0x06
30-
#define PCI_REVID_REG 0x08
31-
#define PCI_PROGIF_REG 0x09
32-
#define PCI_SUBCLASS_REG 0x0a
33-
#define PCI_CLASS_REG 0x0b
34-
#define PCI_CLSZ_REG 0x0c
35-
#define PCI_LATTIM_REG 0x0d
36-
#define PCI_HEADER_REG 0x0e
37-
#define PCI_BIST_REG 0x0f
38-
39-
#define PCI_COMMAND_IO 0x01
40-
#define PCI_COMMAND_MEM 0x02
41-
#define PCI_COMMAND_MASTER 0x04
42-
4324
namespace hw {
4425

4526
static constexpr std::array<const char*,3> bridge_subclasses {
@@ -190,7 +171,7 @@ namespace hw {
190171
//printf("read16 %#x status %#x\n", PCI_STATUS_REG, status);
191172
if ((status & 0x10) == 0) return;
192173
// this offset works for non-cardbus bridges
193-
uint32_t offset = 0x34;
174+
uint32_t offset = PCI_CAPABILITY_REG;
194175
// read first capability
195176
offset = read16(offset) & 0xff;
196177
offset &= ~0x3; // lower 2 bits reserved

src/hw/pci_manager.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ using Driver_entry = std::pair<uint32_t, Driver>;
3030
template <typename Driver>
3131
using fixed_factory_t = std::vector<Driver_entry<Driver>>;
3232

33-
struct pcidev_info {
34-
const uintptr_t pci_addr;
35-
uint32_t vendor;
36-
hw::PCI_Device::class_revision_t dev_class;
37-
};
3833
static std::vector<pcidev_info> devinfos_;
39-
4034
static std::vector<hw::PCI_Device> devices_;
4135
static std::vector<Driver_entry<PCI_manager::NIC_driver>> nic_fact;
4236
static std::vector<Driver_entry<PCI_manager::BLK_driver>> blk_fact;
@@ -75,6 +69,10 @@ PCI_manager::Device_vector PCI_manager::devices () {
7569
return device_vec;
7670
}
7771

72+
const PCI_manager::Devinfo_vector& PCI_manager::devinfos() {
73+
return devinfos_;
74+
}
75+
7876
void PCI_manager::scan_bus(const int bus)
7977
{
8078
INFO2("|");

0 commit comments

Comments
 (0)