Processor Counter Monitor
pci.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009-2018, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9  * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 // written by Roman Dementiev
14 // Pat Fay
15 // Jim Harris (FreeBSD)
16 
17 
18 #ifndef CPUCounters_PCI_H
19 #define CPUCounters_PCI_H
20 
26 #include "types.h"
27 
28 #ifdef _MSC_VER
29 #include "windows.h"
30 #else
31 #include <unistd.h>
32 #endif
33 
34 #ifdef __APPLE__
35 #include "PCIDriverInterface.h"
36 #endif
37 
38 #include <vector>
39 
40 class PciHandle
41 {
42 #ifdef _MSC_VER
43  HANDLE hDriver;
44 #else
45  int32 fd;
46 #endif
47 
48  uint32 bus;
49  uint32 device;
50  uint32 function;
51 #ifdef _MSC_VER
52  DWORD pciAddress;
53 #endif
54 
55  friend class PciHandleM;
56  friend class PciHandleMM;
57 
58  PciHandle(); // forbidden
59  PciHandle(const PciHandle &); // forbidden
60  PciHandle & operator = (const PciHandle &); // forbidden
61 
62 public:
63  PciHandle(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
64 
65  static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
66 
67  int32 read32(uint64 offset, uint32 * value);
68  int32 write32(uint64 offset, uint32 value);
69 
70  int32 read64(uint64 offset, uint64 * value);
71 
72  virtual ~PciHandle();
73 
74 protected:
75  static int openMcfgTable();
76 };
77 
78 #ifdef _MSC_VER
79 typedef PciHandle PciHandleType;
80 #elif __APPLE__
81 // This may need to change if it can be implemented for OSX
82 typedef PciHandle PciHandleType;
83 #elif defined(__FreeBSD__) || defined(__DragonFly__)
84 typedef PciHandle PciHandleType;
85 #else
86 
87 // read/write PCI config space using physical memory
89 {
90 #ifdef _MSC_VER
91 
92 #else
93  int32 fd;
94 #endif
95 
96  uint32 bus;
97  uint32 device;
98  uint32 function;
99  uint64 base_addr;
100 
101  PciHandleM(); // forbidden
102  PciHandleM(PciHandleM &); // forbidden
103 
104 public:
105  PciHandleM(uint32 bus_, uint32 device_, uint32 function_);
106 
107  static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
108 
109  int32 read32(uint64 offset, uint32 * value);
110  int32 write32(uint64 offset, uint32 value);
111 
112  int32 read64(uint64 offset, uint64 * value);
113 
114  virtual ~PciHandleM();
115 };
116 
117 #ifndef _MSC_VER
118 
119 // read/write PCI config space using physical memory using mmaped file I/O
121 {
122  int32 fd;
123  char * mmapAddr;
124 
125  uint32 bus;
126  uint32 device;
127  uint32 function;
128  uint64 base_addr;
129 
130 #ifdef __linux__
131  static MCFGHeader mcfgHeader;
132  static std::vector<MCFGRecord> mcfgRecords;
133  static void readMCFG();
134 #endif
135 
136  PciHandleMM(); // forbidden
137  PciHandleMM(PciHandleM &); // forbidden
138 
139 public:
140  PciHandleMM(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
141 
142  static bool exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 function_);
143 
144  int32 read32(uint64 offset, uint32 * value);
145  int32 write32(uint64 offset, uint32 value);
146 
147  int32 read64(uint64 offset, uint64 * value);
148 
149  virtual ~PciHandleMM();
150 
151 #ifdef __linux__
152  static const std::vector<MCFGRecord> & getMCFGRecords();
153 #endif
154 };
155 
156 #ifdef PCM_USE_PCI_MM_LINUX
157 #define PciHandleType PciHandleMM
158 #else
159 #define PciHandleType PciHandle
160 #endif
161 
162 #endif // _MSC_VER
163 
164 #endif
165 
166 
167 #endif
Internal type and constant definitions.
Definition: pci.h:88
static int openMcfgTable()
Definition: pci.cpp:440
Definition: types.h:965
Definition: pci.h:120
Definition: pci.h:40