Newer
Older
#include "device.hh"
using namespace hw;
namespace pci {
pci_bar::pci_bar(pci_function* dev, u8 pos)
: _dev(dev), _pos(pos),
_addr_lo(0), _addr_hi(0), _addr_64(0), _addr_size(0),
_addr_mmio(mmio_nullptr),
_is_mmio(false), _is_64(false), _is_prefetchable(false)
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
{
init();
}
pci_bar::~pci_bar()
{
}
void pci_bar::init(void)
{
u32 val = _dev->pci_readl(_pos);
_is_mmio = ((val & PCI_BAR_MEMORY_INDICATOR_MASK) == PCI_BAR_MMIO);
if (_is_mmio) {
_addr_lo = val & PCI_BAR_MEM_ADDR_LO_MASK;
_is_64 = ((val & PCI_BAR_MEM_ADDR_SPACE_MASK)
== PCI_BAR_64BIT_ADDRESS);
_is_prefetchable = ((val & PCI_BAR_PREFETCHABLE_MASK)
== PCI_BAR_PREFETCHABLE);
if (_is_64) {
_addr_hi = _dev->pci_readl(_pos + 4);
}
} else {
_addr_lo = val & PCI_BAR_PIO_ADDR_MASK;
}
_addr_64 = ((u64)_addr_hi << 32) | (u64)(_addr_lo);
// Determine the bar size
test_bar_size();
}
void pci_bar::test_bar_size(void)
{
// Size test
_dev->pci_writel(_pos, 0xFFFFFFFF);
u32 lo = _dev->pci_readl(_pos);
// Restore
_dev->pci_writel(_pos, lo_orig);
if (is_pio()) {
lo &= PCI_BAR_PIO_ADDR_MASK;
} else {
lo &= PCI_BAR_MEM_ADDR_LO_MASK;
}
if (is_64()) {
u32 hi_orig = _dev->pci_readl(_pos+4);
_dev->pci_writel(_pos+4, 0xFFFFFFFF);
hi = _dev->pci_readl(_pos+4);
// Restore
_dev->pci_writel(_pos+4, hi_orig);
}
u64 bits = (u64)hi << 32 | lo;
_addr_size = ~bits + 1;
void pci_bar::map(void)
{
if (_is_mmio) {
_addr_mmio = mmio_map(get_addr64(), get_size());
}
}
void pci_bar::unmap(void)
{
if ((_is_mmio) && (_addr_mmio != mmio_nullptr)) {
mmio_unmap(_addr_mmio, get_size());
}
}
mmioaddr_t pci_bar::get_mmio(void)
{
return (_addr_mmio);
}
pci_function::pci_function(u8 bus, u8 device, u8 func)
: _bus(bus), _device(device), _func(func), _have_msix(false), _msix_enabled(false)
}
pci_function::~pci_function()
{
for (auto it = _bars.begin(); it != _bars.end(); it++) {
delete (it->second);
}
}
hw_device_id pci_function::get_id(void)
{
return (hw_device_id(_vendor_id, _device_id));
}
void pci_function::print(void)
{
dump_config();
}
void pci_function::reset(void)
{
// TODO: implement
}
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
bool pci_function::parse_pci_config(void)
{
_device_id = pci_readw(PCI_CFG_DEVICE_ID);
_vendor_id = pci_readw(PCI_CFG_VENDOR_ID);
_revision_id = pci_readb(PCI_CFG_REVISION_ID);
_header_type = pci_readb(PCI_CFG_HEADER_TYPE);
_base_class_code = pci_readb(PCI_CFG_CLASS_CODE0);
_sub_class_code = pci_readb(PCI_CFG_CLASS_CODE1);
_lower_class_code = pci_readb(PCI_CFG_CLASS_CODE2);
// Parse capabilities
bool parse_ok = parse_pci_capabilities();
return parse_ok;
}
bool pci_function::parse_pci_capabilities(void)
{
// Parse MSI-X
u8 off = find_capability(PCI_CAP_MSIX);
if (off != 0xFF) {
bool msi_ok = parse_pci_msix(off);
return (msi_ok);
}
return true;
}
bool pci_function::parse_pci_msix(u8 off)
{
// Used for parsing MSI-x
u32 val = 0;
// Location within the configuration space
_msix.msix_location = off;
_msix.msix_ctrl = pci_readw(off + PCIR_MSIX_CTRL);
_msix.msix_msgnum = (_msix.msix_ctrl & PCIM_MSIXCTRL_TABLE_SIZE) + 1;
val = pci_readl(off + PCIR_MSIX_TABLE);
_msix.msix_table_bar = val & PCIM_MSIX_BIR_MASK;
_msix.msix_table_offset = val & ~PCIM_MSIX_BIR_MASK;
val = pci_readl(off + PCIR_MSIX_PBA);
_msix.msix_pba_bar = val & PCIM_MSIX_BIR_MASK;
_msix.msix_pba_offset = val & ~PCIM_MSIX_BIR_MASK;
// We've found an MSI-x capability
_have_msix = true;
return true;
}
void pci_function::get_bdf(u8& bus, u8 &device, u8& func)
{
bus = _bus;
device = _device;
func = _func;
}
void pci_function::set_bdf(u8 bus, u8 device, u8 func)
{
_bus = bus;
_device = device;
_func = func;
}
u16 pci_function::get_vendor_id(void)
{
return (_vendor_id);
}
u16 pci_function::get_device_id(void)
{
return (_device_id);
}
u8 pci_function::get_revision_id(void)
{
return (_revision_id);
}
bool pci_function::is_device(void)
{
return (_header_type == PCI_HDR_TYPE_DEVICE);
}
bool pci_function::is_bridge(void)
{
return (_header_type == PCI_HDR_TYPE_BRIDGE);
}
bool pci_function::is_pccard(void)
{
return (_header_type == PCI_HDR_TYPE_PCCARD);
}
bool pci_function::is_device(u8 bus, u8 device, u8 function)
{
u8 header_type = read_pci_config_byte(bus, device, function,
PCI_CFG_HEADER_TYPE);
return (header_type == PCI_HDR_TYPE_DEVICE);
}
bool pci_function::is_bridge(u8 bus, u8 device, u8 function)
{
u8 header_type = read_pci_config_byte(bus, device, function,
PCI_CFG_HEADER_TYPE);
return (header_type == PCI_HDR_TYPE_BRIDGE);
}
bool pci_function::is_pccard(u8 bus, u8 device, u8 function)
{
u8 header_type = read_pci_config_byte(bus, device, function,
PCI_CFG_HEADER_TYPE);
return (header_type == PCI_HDR_TYPE_PCCARD);
}
// Command & Status
u16 pci_function::get_command(void)
{
return (pci_readw(PCI_CFG_COMMAND));
}
u16 pci_function::get_status(void)
{
return (pci_readw(PCI_CFG_STATUS));
}
void pci_function::set_command(u16 command)
{
pci_writew(PCI_CFG_COMMAND, command);
}
void pci_function::set_status(u16 status)
{
pci_writew(PCI_CFG_COMMAND, status);
}
bool pci_function::get_bus_master()
{
u16 command = get_command();
return (command & PCI_COMMAND_BUS_MASTER);
}
void pci_function::set_bus_master(bool master)
{
u16 command = get_command();
command =
(master) ?
command | PCI_COMMAND_BUS_MASTER :
command & ~PCI_COMMAND_BUS_MASTER;
set_command(command);
}
bool pci_function::is_intx_enabled(void)
{
u16 command = get_command();
return ((command & PCI_COMMAND_INTX_DISABLE) == 0);
}
void pci_function::enable_intx(void)
{
u16 command = get_command();
command &= ~PCI_COMMAND_INTX_DISABLE;
set_command(command);
}
void pci_function::disable_intx(void)
{
u16 command = get_command();
command |= PCI_COMMAND_INTX_DISABLE;
set_command(command);
}
u8 pci_function::get_interrupt_line(void)
{
return (pci_readb(PCI_CFG_INTERRUPT_LINE));
}
void pci_function::set_interrupt_line(u8 irq)
{
pci_writeb(PCI_CFG_INTERRUPT_LINE, irq);
}
u8 pci_function::get_interrupt_pin(void)
{
return (pci_readb(PCI_CFG_INTERRUPT_PIN));
}
bool pci_function::is_msix(void)
{
return (_have_msix);
}
unsigned pci_function::msix_get_num_entries(void)
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
{
if (!is_msix()) {
return (0);
}
return (_msix.msix_msgnum);
}
void pci_function::msix_mask_all(void)
{
if (!is_msix()) {
return;
}
u16 ctrl = msix_get_control();
ctrl |= PCIM_MSIXCTRL_FUNCTION_MASK;
msix_set_control(ctrl);
}
void pci_function::msix_unmask_all(void)
{
if (!is_msix()) {
return;
}
u16 ctrl = msix_get_control();
ctrl &= ~PCIM_MSIXCTRL_FUNCTION_MASK;
msix_set_control(ctrl);
}
bool pci_function::msix_mask_entry(int entry_id)
{
if (!is_msix()) {
return (false);
}
if (entry_id >= _msix.msix_msgnum) {
return (false);
}
mmioaddr_t entryaddr = msix_get_table() + (entry_id * MSIX_ENTRY_SIZE);
mmioaddr_t ctrl = entryaddr + (u8)MSIX_ENTRY_CONTROL;
u32 ctrl_data = mmio_getl(ctrl);
ctrl_data |= (1 << MSIX_ENTRY_CONTROL_MASK_BIT);
mmio_setl(ctrl, ctrl_data);
return (true);
}
bool pci_function::msix_unmask_entry(int entry_id)
{
if (!is_msix()) {
return (false);
}
if (entry_id >= _msix.msix_msgnum) {
return (false);
}
mmioaddr_t entryaddr = msix_get_table() + (entry_id * MSIX_ENTRY_SIZE);
mmioaddr_t ctrl = entryaddr + (u8)MSIX_ENTRY_CONTROL;
u32 ctrl_data = mmio_getl(ctrl);
ctrl_data &= ~(1 << MSIX_ENTRY_CONTROL_MASK_BIT);
mmio_setl(ctrl, ctrl_data);
return (true);
}
bool pci_function::msix_write_entry(int entry_id, u64 address, u32 data)
{
if (!is_msix()) {
return (false);
}
if (entry_id >= _msix.msix_msgnum) {
return (false);
}
mmioaddr_t entryaddr = msix_get_table() + (entry_id * MSIX_ENTRY_SIZE);
mmio_setq(entryaddr + (u8)MSIX_ENTRY_ADDR, address);
mmio_setl(entryaddr + (u8)MSIX_ENTRY_DATA, data);
return (true);
}
void pci_function::msix_enable(void)
{
if (!is_msix()) {
return;
}
// mmap the msix bar into memory
pci_bar* msix_bar = get_bar(_msix.msix_table_bar + 1);
if (msix_bar == nullptr) {
return;
}
msix_bar->map();
// Disabled intx assertions which is turned on by default
disable_intx();
// Only after enabling msix, the access to the pci bar is permitted
// so we enable it while masking all interrupts in the msix ctrl reg
u16 ctrl = msix_get_control();
ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
ctrl |= PCIM_MSIXCTRL_FUNCTION_MASK;
msix_set_control(ctrl);
// Mask all individual entries
for (int i=0; i<_msix.msix_msgnum; i++) {
msix_mask_entry(i);
}
// After all individual entries are masked,
// Unmask the main block
ctrl &= ~PCIM_MSIXCTRL_FUNCTION_MASK;
msix_set_control(ctrl);
_msix_enabled = true;
}
void pci_function::msix_disable(void)
{
if (!is_msix()) {
return;
}
u16 ctrl = msix_get_control();
ctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE;
msix_set_control(ctrl);
_msix_enabled = false;
}
void pci_function::msix_set_control(u16 ctrl)
{
pci_writew(_msix.msix_location + PCIR_MSIX_CTRL, ctrl);
}
u16 pci_function::msix_get_control(void)
{
return (pci_readw(_msix.msix_location + PCIR_MSIX_CTRL));
}
mmioaddr_t pci_function::msix_get_table(void)
{
pci_bar* msix_bar = get_bar(_msix.msix_table_bar + 1);
if (msix_bar == nullptr) {
return (mmio_nullptr);
}
return ( reinterpret_cast<mmioaddr_t>(msix_bar->get_mmio() +
_msix.msix_table_offset) );
}
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
u8 pci_function::pci_readb(u8 offset)
{
return read_pci_config_byte(_bus, _device, _func, offset);
}
u16 pci_function::pci_readw(u8 offset)
{
return read_pci_config_word(_bus, _device, _func, offset);
}
u32 pci_function::pci_readl(u8 offset)
{
return read_pci_config(_bus, _device, _func, offset);
}
void pci_function::pci_writeb(u8 offset, u8 val)
{
write_pci_config_byte(_bus, _device, _func, offset, val);
}
void pci_function::pci_writew(u8 offset, u16 val)
{
write_pci_config_word(_bus, _device, _func, offset, val);
}
void pci_function::pci_writel(u8 offset, u32 val)
{
write_pci_config(_bus, _device, _func, offset, val);
}
u8 pci_function::find_capability(u8 cap_id)
{
u8 capabilities_base = pci_readb(PCI_CAPABILITIES_PTR);
u8 off = capabilities_base;
u8 bad_offset = 0xFF;
u8 max_capabilities = 0xF0;
u8 ctr = 0;
while (off != 0) {
// Read capability
u8 capability = pci_readb(off + PCI_CAP_OFF_ID);
if (capability == cap_id) {
return (off);
}
ctr++;
if (ctr > max_capabilities) {
return (bad_offset);
}
// Next
off = pci_readb(off + PCI_CAP_OFF_NEXT);
}
return (bad_offset);
}
pci_bar * pci_function::get_bar(int idx)
{
auto it = _bars.find(idx);
if (it == _bars.end()) {
return (nullptr);
}
return (it->second);
}
void pci_function::add_bar(int idx, pci_bar * bar)
{
_bars.insert(std::make_pair(idx, bar));
}
void pci_function::dump_config(void)
{
debug(fmt("[%x:%x.%x] vid:id = %x:%x") %
(u16)_bus % (u16)_device % (u16)_func % _vendor_id % _device_id);
// PCI BARs
int bar_idx = 1;
pci_bar *bar = get_bar(bar_idx);
while (bar != nullptr) {
debug(fmt(" bar[%d]: %sbits addr=%x size=%x") % bar_idx %
(bar->is_64()?"64":"32") % bar->get_addr64() % bar->get_size());
bar = get_bar(++bar_idx);
}
debug(fmt(" IRQ = %d") % (u16)get_interrupt_line());
// MSI-x
if (_have_msix) {
debug(fmt(" Have MSI-X!"));
debug(fmt(" msix_location: %1%") % (u16)_msix.msix_location);
debug(fmt(" msix_ctrl: %1%") % _msix.msix_ctrl);
debug(fmt(" msix_msgnum: %1%") % _msix.msix_msgnum);
debug(
fmt(" msix_table_bar: %1%") % (u16)_msix.msix_table_bar);
debug(
fmt(" msix_table_offset: %1%")
% _msix.msix_table_offset);
debug(fmt(" msix_pba_bar: %1%") % (u16)_msix.msix_pba_bar);
debug(fmt(" msix_pba_offset: %1%") % _msix.msix_pba_offset);
}
}
}