xref: /kvmtool/pci.c (revision 598419d58cd07f632cf661b1ae20cb9fcf7853f5)
160742802SPekka Enberg #include "kvm/pci.h"
260742802SPekka Enberg #include "kvm/ioport.h"
376f9c841SCyrill Gorcunov #include "kvm/util.h"
460742802SPekka Enberg 
560742802SPekka Enberg #include <stdint.h>
660742802SPekka Enberg 
74402a581SPekka Enberg static struct pci_config_address pci_config_address;
860742802SPekka Enberg 
9305b72ceSCyrill Gorcunov static bool pci_config_address_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
1060742802SPekka Enberg {
114402a581SPekka Enberg 	struct pci_config_address *addr = data;
1260742802SPekka Enberg 
13dad5bc09SCyrill Gorcunov 	pci_config_address	= *addr;
1460742802SPekka Enberg 
1560742802SPekka Enberg 	return true;
1660742802SPekka Enberg }
1760742802SPekka Enberg 
18305b72ceSCyrill Gorcunov static bool pci_config_address_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
1960742802SPekka Enberg {
204402a581SPekka Enberg 	struct pci_config_address *addr = data;
2160742802SPekka Enberg 
22305b72ceSCyrill Gorcunov 	*addr		=  pci_config_address;
2360742802SPekka Enberg 
2460742802SPekka Enberg 	return true;
2560742802SPekka Enberg }
2660742802SPekka Enberg 
27305b72ceSCyrill Gorcunov static struct ioport_operations pci_config_address_ops = {
28305b72ceSCyrill Gorcunov 	.io_in		= pci_config_address_in,
29305b72ceSCyrill Gorcunov 	.io_out		= pci_config_address_out,
3060742802SPekka Enberg };
3160742802SPekka Enberg 
32305b72ceSCyrill Gorcunov static bool pci_config_data_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
3360742802SPekka Enberg {
3460742802SPekka Enberg 	return true;
3560742802SPekka Enberg }
3660742802SPekka Enberg 
3776f9c841SCyrill Gorcunov #define PCI_VENDOR_ID_REDHAT_QUMRANET	0x1af4
3876f9c841SCyrill Gorcunov #define PCI_DEVICE_ID_VIRTIO_BLK	0x1001
3976f9c841SCyrill Gorcunov 
4076f9c841SCyrill Gorcunov static struct pci_device_header virtio_device = {
4176f9c841SCyrill Gorcunov 	.vendor_id		= PCI_VENDOR_ID_REDHAT_QUMRANET,
4276f9c841SCyrill Gorcunov 	.device_id		= PCI_DEVICE_ID_VIRTIO_BLK,
4376f9c841SCyrill Gorcunov 	.header_type		= PCI_HEADER_TYPE_NORMAL,
4476f9c841SCyrill Gorcunov };
4576f9c841SCyrill Gorcunov 
4676f9c841SCyrill Gorcunov static bool pci_device_matches(uint8_t bus_number, uint8_t device_number, uint8_t function_number)
4776f9c841SCyrill Gorcunov {
4876f9c841SCyrill Gorcunov 	if (pci_config_address.bus_number != bus_number)
4976f9c841SCyrill Gorcunov 		return false;
5076f9c841SCyrill Gorcunov 
5176f9c841SCyrill Gorcunov 	if (pci_config_address.device_number != device_number)
5276f9c841SCyrill Gorcunov 		return false;
5376f9c841SCyrill Gorcunov 
5476f9c841SCyrill Gorcunov 	return pci_config_address.function_number == function_number;
5576f9c841SCyrill Gorcunov }
5676f9c841SCyrill Gorcunov 
57305b72ceSCyrill Gorcunov static bool pci_config_data_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
5860742802SPekka Enberg {
59e498ea08SPekka Enberg 	if (pci_device_matches(0, 1, 0)) {
60*598419d5SPekka Enberg 		unsigned long offset;
61*598419d5SPekka Enberg 
62*598419d5SPekka Enberg 		offset		= pci_config_address.register_number << 2;
63*598419d5SPekka Enberg 		if (offset < sizeof(struct pci_device_header)) {
64e498ea08SPekka Enberg 			void *p = &virtio_device;
654c797ebcSCyrill Gorcunov 
66*598419d5SPekka Enberg 			memcpy(data, p + (pci_config_address.register_number << 2), size);
67*598419d5SPekka Enberg 		} else
68*598419d5SPekka Enberg 			memset(data, 0x00, size);
69e498ea08SPekka Enberg 	} else
70e498ea08SPekka Enberg 		memset(data, 0xff, size);
714c797ebcSCyrill Gorcunov 
7260742802SPekka Enberg 	return true;
7360742802SPekka Enberg }
7460742802SPekka Enberg 
75305b72ceSCyrill Gorcunov static struct ioport_operations pci_config_data_ops = {
76305b72ceSCyrill Gorcunov 	.io_in		= pci_config_data_in,
77305b72ceSCyrill Gorcunov 	.io_out		= pci_config_data_out,
7860742802SPekka Enberg };
7960742802SPekka Enberg 
8060742802SPekka Enberg void pci__init(void)
8160742802SPekka Enberg {
82305b72ceSCyrill Gorcunov 	ioport__register(PCI_CONFIG_DATA,    &pci_config_data_ops);
83305b72ceSCyrill Gorcunov 	ioport__register(PCI_CONFIG_ADDRESS, &pci_config_address_ops);
8460742802SPekka Enberg }
85