1 /* 2 * Copyright 2006, Marcus Overhagen. All rights reserved. 3 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 4 * Copyright 2003, Marcus Overhagen. All rights reserved. 5 * 6 * Distributed under the terms of the MIT License. 7 */ 8 #ifndef __PCI_H__ 9 #define __PCI_H__ 10 11 #include <PCI.h> 12 13 #ifdef __cplusplus 14 #include <VectorMap.h> 15 #endif 16 17 #include "pci_controller.h" 18 19 20 #define TRACE_PCI 21 #ifndef TRACE_PCI 22 # define TRACE(x) 23 #else 24 # define TRACE(x) dprintf x 25 #endif 26 27 28 #ifdef __cplusplus 29 30 struct PCIDev; 31 32 struct PCIBus { 33 PCIBus *next; 34 PCIDev *parent; 35 PCIDev *child; 36 int domain; 37 uint8 bus; 38 }; 39 40 struct PCIDev { 41 PCIDev *next; 42 PCIBus *parent; 43 PCIBus *child; 44 int domain; 45 uint8 bus; 46 uint8 dev; 47 uint8 func; 48 pci_info info; 49 }; 50 51 52 struct domain_data 53 { 54 // These two are set in PCI::AddController: 55 pci_controller * controller; 56 void * controller_cookie; 57 58 // All the rest is set in PCI::InitDomainData 59 int max_bus_devices; 60 }; 61 62 63 class PCI { 64 public: 65 PCI(); 66 ~PCI(); 67 68 void InitDomainData(); 69 void InitBus(); 70 71 status_t AddController(pci_controller *controller, void *controller_cookie); 72 73 status_t GetNthPciInfo(long index, pci_info *outInfo); 74 75 status_t ReadPciConfig(int domain, uint8 bus, uint8 device, uint8 function, 76 uint8 offset, uint8 size, uint32 *value); 77 78 uint32 ReadPciConfig(int domain, uint8 bus, uint8 device, uint8 function, 79 uint8 offset, uint8 size); 80 81 status_t WritePciConfig(int domain, uint8 bus, uint8 device, uint8 function, 82 uint8 offset, uint8 size, uint32 value); 83 84 status_t ResolveVirtualBus(uint8 virtualBus, int *domain, uint8 *bus); 85 86 private: 87 88 void EnumerateBus(int domain, uint8 bus, uint8 *subordinate_bus = NULL); 89 90 void FixupDevices(int domain, uint8 bus); 91 92 void DiscoverBus(PCIBus *bus); 93 void DiscoverDevice(PCIBus *bus, uint8 dev, uint8 func); 94 95 PCIDev *CreateDevice(PCIBus *parent, uint8 dev, uint8 func); 96 PCIBus *CreateBus(PCIDev *parent, int domain, uint8 bus); 97 98 status_t GetNthPciInfo(PCIBus *bus, long *curindex, long wantindex, pci_info *outInfo); 99 void ReadPciBasicInfo(PCIDev *dev); 100 void ReadPciHeaderInfo(PCIDev *dev); 101 102 void RefreshDeviceInfo(PCIBus *bus); 103 104 uint32 BarSize(uint32 bits, uint32 mask); 105 void GetBarInfo(PCIDev *dev, uint8 offset, uint32 *address, uint32 *size = 0, uint8 *flags = 0); 106 void GetRomBarInfo(PCIDev *dev, uint8 offset, uint32 *address, uint32 *size = 0, uint8 *flags = 0); 107 108 109 domain_data * GetDomainData(int domain); 110 111 status_t CreateVirtualBus(int domain, uint8 bus, uint8 *virtualBus); 112 113 private: 114 PCIBus * fRootBus; 115 116 enum { MAX_PCI_DOMAINS = 8 }; 117 118 domain_data fDomainData[MAX_PCI_DOMAINS]; 119 int fDomainCount; 120 bool fBusEnumeration; 121 122 typedef VectorMap<uint8, uint16> VirtualBusMap; 123 124 VirtualBusMap fVirtualBusMap; 125 int fNextVirtualBus; 126 }; 127 128 #endif // __cplusplus 129 130 131 #ifdef __cplusplus 132 extern "C" { 133 #endif 134 135 status_t pci_init(void); 136 void pci_uninit(void); 137 138 long pci_get_nth_pci_info(long index, pci_info *outInfo); 139 140 uint32 pci_read_config(uint8 virtualBus, uint8 device, uint8 function, uint8 offset, uint8 size); 141 void pci_write_config(uint8 virtualBus, uint8 device, uint8 function, uint8 offset, uint8 size, uint32 value); 142 143 void __pci_resolve_virtual_bus(uint8 virtualBus, int *domain, uint8 *bus); 144 145 #ifdef __cplusplus 146 } 147 #endif 148 149 #endif /* __PCI_H__ */ 150