/
iopcidevice_helpers.cpp
229 lines (190 loc) · 6.91 KB
/
iopcidevice_helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* kextgizmos IOPCIDevice helpers
Dual-licensed under the MIT and zLib licenses.
Copyright 2018 Phillip & Laura Dennis-Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copyright (c) 2018 Phillip & Laura Dennis-Jordan
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use
of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject
to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is not
required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "iopcidevice_helpers.hpp"
#if DEBUG
#include "ioreturn_strings.h"
#endif
#include <IOKit/pci/IOPCIDevice.h>
// TODO: move these to a logging gizmo
#define ANSI_ESCAPE_RESET "\x1b[0m"
#define ANSI_ESCAPE_DARKGREY "\x1b[90m"
#define ANSI_ESCAPE_RED "\x1b[31m"
#define LogWithLocation(fmt, ...) ({ kprintf( ANSI_ESCAPE_DARKGREY "%s:" ANSI_ESCAPE_RESET " " fmt , /*__PRETTY_FUNCTION__*/ __func__ , ## __VA_ARGS__); })
#define LogWarning(fmt, ...) LogWithLocation(ANSI_ESCAPE_RED "Warning: " ANSI_ESCAPE_RESET fmt, ## __VA_ARGS__)
#if DEBUG
#define LogVerbose(fmt, ...) LogWithLocation(fmt, ## __VA_ARGS__)
#endif
// Returns 0 if the device does not support PCI capabilities, or if the config space layout is bad
uint8_t djt_iopcidevice_first_capability_offset(IOPCIDevice* dev)
{
uint16_t status_reg = dev->configRead16(kIOPCIConfigStatus);
if (0 == (status_reg & kIOPCIStatusCapabilities))
return 0;
uint8_t first_capability_offset = dev->configRead8(kIOPCIConfigCapabilitiesPtr);
if (first_capability_offset < 0x40 || (first_capability_offset % 4 != 0))
{
LogVerbose("Bad value 0x%02x found in capabilities pointer register (should be 0x40-0xfc)\n", first_capability_offset);
first_capability_offset = 0;
}
return first_capability_offset;
}
// Returns 0 if the previous capability was the last one in the chain. NB does not defend against malformed infinite-loop lists.
uint8_t djt_iopcidevice_next_capability_offset(IOPCIDevice* dev, uint8_t prev_capability_offset)
{
assert(prev_capability_offset >= 0x40);
return dev->configRead8(prev_capability_offset + 1);
}
/* reg can be in range kIOPCIConfigBaseAddress0..kIOPCIConfigBaseAddress5 inclusive
* Returns one of:
* kIOPCIIOSpace
* kIOPCI32BitMemorySpace
* kIOPCI64BitMemorySpace
* -1 (error)
*/
int djt_iopcidevice_memory_range_type(IOPCIDevice* dev, UInt8 reg)
{
if (reg < kIOPCIConfigBaseAddress0 || reg > kIOPCIConfigBaseAddress5 || reg % 4 != 0)
return -1;
if (reg != kIOPCIConfigBaseAddress0)
{
// registers other than 0 can be second half of 64-bit BAR, so check that's not the case
uint32_t even_bar_val = dev->configRead32(reg - 4);
if ((even_bar_val & 0x1) == 0 && (even_bar_val & 0x6) == 0x02)
return -1;
}
uint32_t bar_val = dev->configRead32(reg);
if ((bar_val & 0x1) != 0)
return kIOPCIIOSpace;
else
{
uint32_t bar_mem_type = (bar_val & 0x6) >> 1;
switch (bar_mem_type)
{
case 0x00:
return kIOPCI32BitMemorySpace;
case 0x02:
if (reg == kIOPCIConfigBaseAddress5)
return -1; // 64-bit in BAR5 does not make any sense
return kIOPCI64BitMemorySpace;
}
return -1;
}
}
uint8_t djt_iopcidevice_register_for_range_index(uint8_t index)
{
assert(index <= 5);
return kIOPCIConfigBaseAddress0 + (index * (kIOPCIConfigBaseAddress1 - kIOPCIConfigBaseAddress0));
}
djt_pci_interrupt_index_ranges djt_iopcidevice_find_interrupt_ranges(IOPCIDevice* pci_device)
{
djt_pci_interrupt_index_ranges ranges = { -1, -1, -1, -1 };
bool pin_started = false;
bool pin_done = false;
bool msi_started = false;
bool msi_done = false;
int32_t intr_index = 0;
// keep trying interrupt source indices until we run out
while (intr_index <= INT32_MAX)
{
int intr_type = 0;
IOReturn ret = pci_device->getInterruptType(intr_index, &intr_type);
LogVerbose("Index %d type: 0x%x -> 0x%x (%s)\n", intr_index, intr_type, ret, djt_ioreturn_string(ret));
if (ret != kIOReturnSuccess)
break;
if (intr_type & kIOInterruptTypePCIMessaged)
{
// found MSI interrupt source
if (msi_done)
{
LogWarning("New unexpected MSI range found starting at index %d, existing range %d..%d (inclusive)\n",
intr_index, ranges.msi_start, ranges.msi_end);
}
else
{
if (!msi_started)
{
if (pin_started)
{
pin_started = false;
pin_done = true;
}
msi_started = true;
ranges.msi_start = intr_index;
}
ranges.msi_end = intr_index + 1;
}
}
else
{
// found traditional IRQ interrupt source
if (pin_done)
{
LogWarning("New unexpected IRQ range found starting at index %d, existing range %d..%d (inclusive)\n",
intr_index, ranges.irq_pin_start, ranges.irq_pin_end);
}
else
{
if (!pin_started)
{
if (msi_started)
{
msi_started = false;
msi_done = true;
}
pin_started = true;
ranges.irq_pin_start = intr_index;
}
ranges.irq_pin_end = intr_index + 1;
}
}
++intr_index;
}
return ranges;
}
void djt_kprint_pci_config_space(IOPCIDevice* pci_dev, const char* line_prefix_str)
{
uint8_t config_space[256];
for (unsigned i = 0; i < 256; ++i)
{
config_space[i] = pci_dev->configRead8(i);
}
for (unsigned i = 0; i < 16; ++i)
{
unsigned offset = i * 16;
const uint8_t* b = config_space + offset;
kprintf("%s[%3u]: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
line_prefix_str, offset,
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
}
}