1 #include "kvm/pci.h" 2 #include "kvm/ioport.h" 3 #include "kvm/util.h" 4 #include "kvm/kvm.h" 5 6 #include <assert.h> 7 8 #define PCI_MAX_DEVICES 256 9 #define PCI_IO_SIZE 0x100 10 #define PCI_BAR_OFFSET(b) (offsetof(struct pci_device_header, bar[b])) 11 12 static struct pci_device_header *pci_devices[PCI_MAX_DEVICES]; 13 14 static struct pci_config_address pci_config_address; 15 16 /* This is within our PCI gap - in an unused area */ 17 static u32 io_space_blocks = KVM_32BIT_GAP_START + 0x1000000; 18 19 u32 pci_get_io_space_block(void) 20 { 21 u32 block = io_space_blocks; 22 io_space_blocks += PCI_IO_SIZE; 23 24 return block; 25 } 26 27 static void *pci_config_address_ptr(u16 port) 28 { 29 unsigned long offset; 30 void *base; 31 32 offset = port - PCI_CONFIG_ADDRESS; 33 base = &pci_config_address; 34 35 return base + offset; 36 } 37 38 static bool pci_config_address_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count) 39 { 40 void *p = pci_config_address_ptr(port); 41 42 memcpy(p, data, size); 43 44 return true; 45 } 46 47 static bool pci_config_address_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count) 48 { 49 void *p = pci_config_address_ptr(port); 50 51 memcpy(data, p, size); 52 53 return true; 54 } 55 56 static struct ioport_operations pci_config_address_ops = { 57 .io_in = pci_config_address_in, 58 .io_out = pci_config_address_out, 59 }; 60 61 static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_number) 62 { 63 struct pci_device_header *dev; 64 65 if (pci_config_address.bus_number != bus_number) 66 return false; 67 68 if (pci_config_address.function_number != function_number) 69 return false; 70 71 if (device_number >= PCI_MAX_DEVICES) 72 return false; 73 74 dev = pci_devices[device_number]; 75 76 return dev != NULL; 77 } 78 79 static bool pci_config_data_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count) 80 { 81 unsigned long start; 82 u8 dev_num; 83 84 /* 85 * If someone accesses PCI configuration space offsets that are not 86 * aligned to 4 bytes, it uses ioports to signify that. 87 */ 88 start = port - PCI_CONFIG_DATA; 89 90 dev_num = pci_config_address.device_number; 91 92 if (pci_device_exists(0, dev_num, 0)) { 93 unsigned long offset; 94 95 offset = start + (pci_config_address.register_number << 2); 96 if (offset < sizeof(struct pci_device_header)) { 97 void *p = pci_devices[dev_num]; 98 u32 sz = PCI_IO_SIZE; 99 100 /* 101 * If the kernel masks the BAR it would expect to find the 102 * size of the BAR there next time it reads from it. 103 * When the kernel got the size it would write the address 104 * back. 105 */ 106 if (*(u32 *)(p + offset)) { 107 /* See if kernel tries to mask one of the BARs */ 108 if ((offset >= PCI_BAR_OFFSET(0)) && 109 (offset <= PCI_BAR_OFFSET(6))) 110 memcpy(p + offset, &sz, sizeof(sz)); 111 else 112 memcpy(p + offset, data, size); 113 } 114 } 115 } 116 117 return true; 118 } 119 120 static bool pci_config_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size, u32 count) 121 { 122 unsigned long start; 123 u8 dev_num; 124 125 /* 126 * If someone accesses PCI configuration space offsets that are not 127 * aligned to 4 bytes, it uses ioports to signify that. 128 */ 129 start = port - PCI_CONFIG_DATA; 130 131 dev_num = pci_config_address.device_number; 132 133 if (pci_device_exists(0, dev_num, 0)) { 134 unsigned long offset; 135 136 offset = start + (pci_config_address.register_number << 2); 137 if (offset < sizeof(struct pci_device_header)) { 138 void *p = pci_devices[dev_num]; 139 140 memcpy(data, p + offset, size); 141 } else 142 memset(data, 0x00, size); 143 } else 144 memset(data, 0xff, size); 145 146 return true; 147 } 148 149 static struct ioport_operations pci_config_data_ops = { 150 .io_in = pci_config_data_in, 151 .io_out = pci_config_data_out, 152 }; 153 154 void pci__register(struct pci_device_header *dev, u8 dev_num) 155 { 156 assert(dev_num < PCI_MAX_DEVICES); 157 158 pci_devices[dev_num] = dev; 159 } 160 161 void pci__init(void) 162 { 163 ioport__register(PCI_CONFIG_DATA + 0, &pci_config_data_ops, 4, NULL); 164 ioport__register(PCI_CONFIG_ADDRESS + 0, &pci_config_address_ops, 4, NULL); 165 } 166