-
Notifications
You must be signed in to change notification settings - Fork 0
/
pci.c
119 lines (84 loc) · 2.73 KB
/
pci.c
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
#include "kvm/pci.h"
#include "kvm/ioport.h"
#include "kvm/util.h"
#include <assert.h>
#include <stdint.h>
#define PCI_MAX_DEVICES 256
static struct pci_device_header *pci_devices[PCI_MAX_DEVICES];
static struct pci_config_address pci_config_address;
static void *pci_config_address_ptr(int16_t port)
{
unsigned long offset;
void *base;
offset = port - PCI_CONFIG_ADDRESS;
base = &pci_config_address;
return base + offset;
}
static bool pci_config_address_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
{
void *p = pci_config_address_ptr(port);
memcpy(p, data, size);
return true;
}
static bool pci_config_address_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
{
void *p = pci_config_address_ptr(port);
memcpy(data, p, size);
return true;
}
static struct ioport_operations pci_config_address_ops = {
.io_in = pci_config_address_in,
.io_out = pci_config_address_out,
};
static bool pci_config_data_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
{
return true;
}
static bool pci_device_exists(uint8_t bus_number, uint8_t device_number, uint8_t function_number)
{
struct pci_device_header *dev;
if (pci_config_address.function_number != function_number)
return false;
if (pci_config_address.device_number != device_number)
return false;
if (device_number >= PCI_MAX_DEVICES)
return false;
dev = pci_devices[device_number];
return dev != NULL;
}
static bool pci_config_data_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
{
unsigned long start;
uint8_t dev_num;
/*
* If someone accesses PCI configuration space offsets that are not
* aligned to 4 bytes, it uses ioports to signify that.
*/
start = port - PCI_CONFIG_DATA;
dev_num = pci_config_address.device_number;
if (pci_device_exists (0, dev_num, 0)) {
unsigned long offset;
offset = start + (pci_config_address.register_number << 2);
if (offset < sizeof(struct pci_device_header)) {
void *p = pci_devices[dev_num];
memcpy(data, p + offset, size);
} else
memset(data, 0x00, size);
} else
memset(data, 0xff, size);
return true;
}
static struct ioport_operations pci_config_data_ops = {
.io_in = pci_config_data_in,
.io_out = pci_config_data_out,
};
void pci__register(struct pci_device_header *dev, uint8_t dev_num)
{
assert(dev_num < PCI_MAX_DEVICES);
pci_devices[dev_num] = dev;
}
void pci__init(void)
{
ioport__register(PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4);
ioport__register(PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4);
}