Processor Counter Monitor
pci.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 // Copyright (c) 2009-2018, Intel Corporation
3 // written by Roman Dementiev
4 // Pat Fay
5 // Jim Harris (FreeBSD)
6 
7 
8 #ifndef CPUCounters_PCI_H
9 #define CPUCounters_PCI_H
10 
16 #include "types.h"
17 
18 #ifdef _MSC_VER
19 #include "windows.h"
20 #else
21 #include <unistd.h>
22 #endif
23 
24 #ifdef __APPLE__
25 #include "PCIDriverInterface.h"
26 #endif
27 
28 #include <vector>
29 
30 namespace pcm {
31 
32 class PciHandle
33 {
34 #ifdef _MSC_VER
35  HANDLE hDriver;
36 #else
37  int32 fd;
38 #endif
39 
40  uint32 bus;
41  uint32 device;
42  uint32 function;
43 #ifdef _MSC_VER
44  DWORD pciAddress;
45 #endif
46 
47  friend class PciHandleM;
48  friend class PciHandleMM;
49 
50  PciHandle(); // forbidden
51  PciHandle(const PciHandle &); // forbidden
52  PciHandle & operator = (const PciHandle &); // forbidden
53 
54 public:
55  PciHandle(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
56 
57  static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
58 
59  int32 read32(uint64 offset, uint32 * value);
60  int32 write32(uint64 offset, uint32 value);
61 
62  int32 read64(uint64 offset, uint64 * value);
63 
64  virtual ~PciHandle();
65 
66 protected:
67  static int openMcfgTable();
68 };
69 
70 #ifdef _MSC_VER
71 typedef PciHandle PciHandleType;
72 #elif __APPLE__
73 // This may need to change if it can be implemented for OSX
74 typedef PciHandle PciHandleType;
75 #elif defined(__FreeBSD__) || defined(__DragonFly__)
76 typedef PciHandle PciHandleType;
77 #else
78 
79 // read/write PCI config space using physical memory
81 {
82 #ifdef _MSC_VER
83 
84 #else
85  int32 fd;
86 #endif
87 
88  uint32 bus;
89  uint32 device;
90  uint32 function;
91  uint64 base_addr;
92 
93  PciHandleM() = delete; // forbidden
94  PciHandleM(PciHandleM &) = delete; // forbidden
95  PciHandleM & operator = (PciHandleM &) = delete; // forbidden
96 
97 public:
98  PciHandleM(uint32 bus_, uint32 device_, uint32 function_);
99 
100  static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
101 
102  int32 read32(uint64 offset, uint32 * value);
103  int32 write32(uint64 offset, uint32 value);
104 
105  int32 read64(uint64 offset, uint64 * value);
106 
107  virtual ~PciHandleM();
108 };
109 
110 #ifndef _MSC_VER
111 
112 // read/write PCI config space using physical memory using mmapped file I/O
114 {
115  int32 fd;
116  char * mmapAddr;
117 
118  uint32 bus;
119  uint32 device;
120  uint32 function;
121  uint64 base_addr;
122 
123 #ifdef __linux__
124  static MCFGHeader mcfgHeader;
125  static std::vector<MCFGRecord> mcfgRecords;
126  static void readMCFG();
127 #endif
128 
129  PciHandleMM(); // forbidden
130  PciHandleMM(PciHandleM &); // forbidden
131 
132 public:
133  PciHandleMM(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
134 
135  static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
136 
137  int32 read32(uint64 offset, uint32 * value);
138  int32 write32(uint64 offset, uint32 value);
139 
140  int32 read64(uint64 offset, uint64 * value);
141 
142  virtual ~PciHandleMM();
143 
144 #ifdef __linux__
145  static const std::vector<MCFGRecord> & getMCFGRecords();
146 #endif
147 };
148 
149 #ifdef PCM_USE_PCI_MM_LINUX
150 #define PciHandleType PciHandleMM
151 #else
152 #define PciHandleType PciHandle
153 #endif
154 
155 #endif // _MSC_VER
156 
157 #endif
158 
159 } // namespace pcm
160 
161 #endif
Internal type and constant definitions.
Definition: pci.h:32
Definition: bw.cpp:12
Definition: types.h:1277
Definition: pci.h:80
Definition: pci.h:113