Skip to contentSkip to conversation

kvm tools: Add optional parameter used in ioport callbacks

PATCH Superseded Sasha Levin 2 participants 13 messages May 25, 2011 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/9 kvm tools: Add optional parameter used in ioport callbacks

Allow specifying an optional parameter when registering an ioport range. The callback functions provided by the registering module will be called with the same parameter.

This may be used to keep context during callbacks on IO operations.

---
 tools/kvm/include/kvm/ioport.h |    3 ++
 tools/kvm/ioport.c             |   54 +++++++++++++++++++++++++++++----------
 2 files changed, 43 insertions(+), 14 deletions(-)
tools/kvm/include/kvm/ioport.h +3 -0
index 8253938..2a8d74d 100644
@@ -25,11 +25,14 @@ struct kvm;
 struct ioport_operations {
 	bool (*io_in)(struct kvm *kvm, u16 port, void *data, int size, u32 count);
 	bool (*io_out)(struct kvm *kvm, u16 port, void *data, int size, u32 count);
+	bool (*io_in_param)(struct kvm *kvm, u16 port, void *data, int size, u32 count, void *param);
+	bool (*io_out_param)(struct kvm *kvm, u16 port, void *data, int size, u32 count, void *param);
 };
 
 void ioport__setup_legacy(void);
 
 void ioport__register(u16 port, struct ioport_operations *ops, int count);
+void ioport__register_param(u16 port, struct ioport_operations *ops, int count, void *param);
 
 static inline u8 ioport__read8(u8 *data)
 {
tools/kvm/ioport.c +40 -14

82 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3
#1 lore raw

PATCH 2/9 kvm tools: Add basic ioport dynamic allocation

Add a very simple allocation of ioports.

This prevents the need to coordinate ioports between different modules.

---
 tools/kvm/include/kvm/ioport.h |    5 +++++
 tools/kvm/ioport.c             |    7 +++++++
 2 files changed, 12 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/ioport.h +5 -0
index 2a8d74d..bc7ea02 100644
@@ -7,6 +7,9 @@
 
 /* some ports we reserve for own use */
 #define IOPORT_DBG			0xe0
+#define IOPORT_START			0x6200
+#define IOPORT_SIZE			0x400
+
 #define IOPORT_VESA			0xa200
 #define IOPORT_VESA_SIZE		256
 #define IOPORT_VIRTIO_P9		0xb200	/* Virtio 9P device */
@@ -34,6 +37,8 @@ void ioport__setup_legacy(void);
 void ioport__register(u16 port, struct ioport_operations *ops, int count);
 void ioport__register_param(u16 port, struct ioport_operations *ops, int count, void *param);
 
+u16 ioport__find_free_range(void);
+
 static inline u8 ioport__read8(u8 *data)
 {
 	return *data;
tools/kvm/ioport.c +7 -0
index 159d089..0db5a87 100644
@@ -126,6 +126,13 @@ static void ioport_error(u16 port, void *data, int direction, int size, u32 coun
 	fprintf(stderr, "IO error: %s port=%x, size=%d, count=%u\n", to_direction(direction), port, size, count);
 }
 
+u16 ioport__find_free_range(void)
+{
+	static u16 cur_loc;
+
+	return IOPORT_START + (cur_loc++ * IOPORT_SIZE);
+}
+
 bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int size, u32 count)
 {
 	struct ioport_operations *ops;
Signature · 1 line
1.7.5.rc3
#3 lore raw

PATCH 3/9 kvm tools: Use ioport context to control blk devices

In reply to Sasha Levin

Since ioports now has the ability to pass context to its callbacks, we can implement multiple blk devices more efficiently.

We can get a ptr to the 'current' blk dev on each ioport call, which means that we don't need to keep track of the blk device allocation and ioport distribution within the module.

The advantages are easier management of multiple blk devices and removal of any hardcoded limits to the amount of possible blk devices.

---
 tools/kvm/include/kvm/ioport.h |    2 -
 tools/kvm/virtio/blk.c         |   75 ++++++++++++++-------------------------
 2 files changed, 27 insertions(+), 50 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -2
index bc7ea02..2fe751c 100644
@@ -14,8 +14,6 @@
 #define IOPORT_VESA_SIZE		256
 #define IOPORT_VIRTIO_P9		0xb200	/* Virtio 9P device */
 #define IOPORT_VIRTIO_P9_SIZE		256
-#define IOPORT_VIRTIO_BLK		0xc200	/* Virtio block device */
-#define IOPORT_VIRTIO_BLK_SIZE		0x200
 #define IOPORT_VIRTIO_CONSOLE		0xd200	/* Virtio console device */
 #define IOPORT_VIRTIO_CONSOLE_SIZE	256
 #define IOPORT_VIRTIO_NET		0xe200	/* Virtio network device */
tools/kvm/virtio/blk.c +27 -48

161 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3
#4 lore raw

PATCH 4/9 kvm tools: Add support for multiple virtio-rng devices

In reply to Sasha Levin

Since multiple hardware rng devices of the same type are currently unsupported by the kernel, this serves more as an example of a basic virtio driver under kvm tools and can be used to debug the PCI layer.

---
 tools/kvm/include/kvm/ioport.h        |    2 -
 tools/kvm/include/kvm/parse-options.h |    9 +++
 tools/kvm/include/kvm/virtio-rng.h    |    1 +
 tools/kvm/kvm-run.c                   |    8 ++-
 tools/kvm/virtio/rng.c                |  128 ++++++++++++++++++++++-----------
 5 files changed, 102 insertions(+), 46 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -2
index 2fe751c..709e18e 100644
@@ -18,8 +18,6 @@
 #define IOPORT_VIRTIO_CONSOLE_SIZE	256
 #define IOPORT_VIRTIO_NET		0xe200	/* Virtio network device */
 #define IOPORT_VIRTIO_NET_SIZE		256
-#define IOPORT_VIRTIO_RNG		0xf200	/* Virtio network device */
-#define IOPORT_VIRTIO_RNG_SIZE		256
 
 struct kvm;
 
tools/kvm/include/kvm/parse-options.h +9 -0
index 2d5c99e..6bf9a1d 100644
@@ -132,6 +132,15 @@ intptr_t defval;
 	.help = (h)                         \
 }
 
+#define OPT_INCR(s, l, v, h)                \
+{                                           \
+	.type = OPTION_INCR,	            \
+	.short_name = (s),                  \
+	.long_name = (l),                   \
+	.value = check_vtype(v, int *),     \
+	.help = (h)                         \
+}
+
 #define OPT_GROUP(h)                        \
 {                                           \
 	.type = OPTION_GROUP,               \
tools/kvm/include/kvm/virtio-rng.h +1 -0
index 7015c1f..c0a413b 100644
@@ -4,5 +4,6 @@
 struct kvm;
 
 void virtio_rng__init(struct kvm *kvm);
+void virtio_rng__delete_all(struct kvm *kvm);
 
 #endif /* KVM__RNG_VIRTIO_H */
tools/kvm/kvm-run.c +5 -3
index adbb25b..76b5782 100644
@@ -52,6 +52,7 @@ static __thread struct kvm_cpu *current_kvm_cpu;
 
 static u64 ram_size;
 static u8  image_count;
+static int virtio_rng;
 static const char *kernel_cmdline;
 static const char *kernel_filename;
 static const char *vmlinux_filename;
@@ -66,7 +67,6 @@ static const char *script;
 static const char *virtio_9p_dir;
 static bool single_step;
 static bool readonly_image[MAX_DISK_IMAGES];
-static bool virtio_rng;
 static bool vnc;
 extern bool ioport_debug;
 extern int  active_console;
@@ -107,7 +107,7 @@ static const struct option options[] = {
 	OPT_CALLBACK('d', "disk", NULL, "image", "Disk image", img_name_parser),
 	OPT_STRING('\0', "console", &console, "serial or virtio",
 			"Console to use"),
-	OPT_BOOLEAN('\0', "rng", &virtio_rng,
+	OPT_INCR('\0', "rng", &virtio_rng,
 			"Enable virtio Random Number Generator"),
 	OPT_STRING('\0', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
 	OPT_STRING('\0', "virtio-9p", &virtio_9p_dir, "root dir",
@@ -570,7 +570,8 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		virtio_console__init(kvm);
 
 	if (virtio_rng)
-		virtio_rng__init(kvm);
+		while (virtio_rng--)
+			virtio_rng__init(kvm);
 
 	if (!network)
 		network = DEFAULT_NETWORK;
@@ -631,6 +632,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	}
 
 	virtio_blk__delete_all(kvm);
+	virtio_rng__delete_all(kvm);
 
 	disk_image__close_all(kvm->disks, image_count);
 	kvm__delete(kvm);
tools/kvm/virtio/rng.c +87 -41

246 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3
#6 lore raw

PATCH 6/9 kvm tools: Use dynamic IO port allocation in 9p driver

In reply to Sasha Levin
---
 tools/kvm/virtio/9p.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
tools/kvm/virtio/9p.c +8 -4
index e307592..f8cb96d 100644
@@ -50,7 +50,6 @@ static struct pci_device_header virtio_p9_pci_device = {
 	.class			= 0x010000,
 	.subsys_vendor_id	= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
 	.subsys_id		= VIRTIO_ID_9P,
-	.bar[0]			= IOPORT_VIRTIO_P9 | PCI_BASE_ADDRESS_SPACE_IO,
 };
 
 struct p9_dev {
@@ -59,6 +58,7 @@ struct p9_dev {
 	u16			config_vector;
 	u32			features;
 	struct virtio_9p_config	*config;
+	u16			base_addr;
 
 	/* virtio queue */
 	u16			queue_selector;
@@ -96,7 +96,7 @@ static bool virtio_p9_pci_io_in(struct kvm *kvm, u16 port, void *data, int size,
 	unsigned long offset;
 	bool ret = true;
 
-	offset = port - IOPORT_VIRTIO_P9;
+	offset = port - p9dev.base_addr;
 
 	switch (offset) {
 	case VIRTIO_PCI_HOST_FEATURES:
@@ -584,7 +584,7 @@ static bool virtio_p9_pci_io_out(struct kvm *kvm, u16 port, void *data, int size
 	unsigned long offset;
 	bool ret = true;
 
-	offset		= port - IOPORT_VIRTIO_P9;
+	offset = port - p9dev.base_addr;
 
 	switch (offset) {
 	case VIRTIO_MSI_QUEUE_VECTOR:
@@ -636,6 +636,7 @@ void virtio_9p__init(struct kvm *kvm, const char *root)
 {
 	u8 pin, line, dev;
 	u32 i, root_len;
+	u16 p9_base_addr;
 
 	p9dev.config = calloc(1, sizeof(*p9dev.config) + sizeof(VIRTIO_P9_TAG));
 	if (p9dev.config == NULL)
@@ -662,7 +663,10 @@ void virtio_9p__init(struct kvm *kvm, const char *root)
 
 	virtio_p9_pci_device.irq_pin	= pin;
 	virtio_p9_pci_device.irq_line	= line;
+	p9_base_addr			= ioport__find_free_range();
+	virtio_p9_pci_device.bar[0]	= p9_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+	p9dev.base_addr			= p9_base_addr;
 	pci__register(&virtio_p9_pci_device, dev);
 
-	ioport__register(IOPORT_VIRTIO_P9, &virtio_p9_io_ops, IOPORT_VIRTIO_P9_SIZE);
+	ioport__register(p9_base_addr, &virtio_p9_io_ops, IOPORT_SIZE);
 }
Signature · 1 line
1.7.5.rc3
#7 lore raw

PATCH 7/9 kvm tools: Use dynamic IO port allocation in virtio-console driver

In reply to Sasha Levin
---
 tools/kvm/virtio/console.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
tools/kvm/virtio/console.c +8 -4
index affff0b..eeb5dc9 100644
@@ -36,7 +36,6 @@ static struct pci_device_header virtio_console_pci_device = {
 	.class			= 0x078000,
 	.subsys_vendor_id	= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
 	.subsys_id		= VIRTIO_ID_CONSOLE,
-	.bar[0]			= IOPORT_VIRTIO_CONSOLE | PCI_BASE_ADDRESS_SPACE_IO,
 };
 
 struct con_dev {
@@ -50,6 +49,7 @@ struct con_dev {
 	u8				status;
 	u8				isr;
 	u16				queue_selector;
+	u16				base_addr;
 
 	void				*jobs[VIRTIO_CONSOLE_NUM_QUEUES];
 };
@@ -113,7 +113,7 @@ static bool virtio_console_pci_io_device_specific_in(void *data, unsigned long o
 
 static bool virtio_console_pci_io_in(struct kvm *kvm, u16 port, void *data, int size, u32 count)
 {
-	unsigned long offset = port - IOPORT_VIRTIO_CONSOLE;
+	unsigned long offset = port - cdev.base_addr;
 	bool ret = true;
 
 	mutex_lock(&cdev.mutex);
@@ -181,7 +181,7 @@ static void virtio_console_handle_callback(struct kvm *kvm, void *param)
 
 static bool virtio_console_pci_io_out(struct kvm *kvm, u16 port, void *data, int size, u32 count)
 {
-	unsigned long offset = port - IOPORT_VIRTIO_CONSOLE;
+	unsigned long offset = port - cdev.base_addr;
 	bool ret = true;
 
 	mutex_lock(&cdev.mutex);
@@ -243,12 +243,16 @@ static struct ioport_operations virtio_console_io_ops = {
 void virtio_console__init(struct kvm *kvm)
 {
 	u8 dev, line, pin;
+	u16 console_base_addr;
 
 	if (irq__register_device(VIRTIO_ID_CONSOLE, &dev, &pin, &line) < 0)
 		return;
 
 	virtio_console_pci_device.irq_pin	= pin;
 	virtio_console_pci_device.irq_line	= line;
+	console_base_addr			= ioport__find_free_range();
+	virtio_console_pci_device.bar[0]	= console_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+	cdev.base_addr				= console_base_addr;
 	pci__register(&virtio_console_pci_device, dev);
-	ioport__register(IOPORT_VIRTIO_CONSOLE, &virtio_console_io_ops, IOPORT_VIRTIO_CONSOLE_SIZE);
+	ioport__register(console_base_addr, &virtio_console_io_ops, IOPORT_SIZE);
 }
Signature · 1 line
1.7.5.rc3
#10 lore raw

PATCH 8/9 kvm tools: Use dynamic IO port allocation in virtio-net driver

In reply to Sasha Levin
---
 tools/kvm/virtio/net.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)
tools/kvm/virtio/net.c +9 -4
index 649bc0f..765ae56d 100644
@@ -37,7 +37,6 @@ static struct pci_device_header pci_header = {
 	.class				= 0x020000,
 	.subsys_vendor_id		= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
 	.subsys_id			= VIRTIO_ID_NET,
-	.bar[0]				= IOPORT_VIRTIO_NET | PCI_BASE_ADDRESS_SPACE_IO,
 };
 
 struct net_device {
@@ -51,6 +50,7 @@ struct net_device {
 	u8				status;
 	u8				isr;
 	u16				queue_selector;
+	u16				base_addr;
 
 	pthread_t			io_rx_thread;
 	pthread_mutex_t			io_rx_lock;
@@ -166,7 +166,7 @@ static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offse
 
 static bool virtio_net_pci_io_in(struct kvm *kvm, u16 port, void *data, int size, u32 count)
 {
-	unsigned long	offset	= port - IOPORT_VIRTIO_NET;
+	unsigned long	offset	= port - ndev.base_addr;
 	bool		ret	= true;
 
 	mutex_lock(&ndev.mutex);
@@ -230,7 +230,7 @@ static void virtio_net_handle_callback(struct kvm *kvm, u16 queue_index)
 
 static bool virtio_net_pci_io_out(struct kvm *kvm, u16 port, void *data, int size, u32 count)
 {
-	unsigned long	offset		= port - IOPORT_VIRTIO_NET;
+	unsigned long	offset		= port - ndev.base_addr;
 	bool		ret		= true;
 
 	mutex_lock(&ndev.mutex);
@@ -387,14 +387,19 @@ void virtio_net__init(const struct virtio_net_parameters *params)
 {
 	if (virtio_net__tap_init(params)) {
 		u8 dev, line, pin;
+		u16 net_base_addr;
 
 		if (irq__register_device(VIRTIO_ID_NET, &dev, &pin, &line) < 0)
 			return;
 
 		pci_header.irq_pin	= pin;
 		pci_header.irq_line	= line;
+		net_base_addr		= ioport__find_free_range();
+		pci_header.bar[0]	= net_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+		ndev.base_addr		= net_base_addr;
+
 		pci__register(&pci_header, dev);
-		ioport__register(IOPORT_VIRTIO_NET, &virtio_net_io_ops, IOPORT_VIRTIO_NET_SIZE);
+		ioport__register(net_base_addr, &virtio_net_io_ops, IOPORT_SIZE);
 
 		virtio_net__io_thread_init(params->kvm);
 	}
Signature · 1 line
1.7.5.rc3
#11 lore raw

PATCH 9/9 kvm tools: Remove static ioport allocations

In reply to Sasha Levin
---
 tools/kvm/include/kvm/ioport.h |    9 ---------
 1 files changed, 0 insertions(+), 9 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -9
index 709e18e..cc57cdd 100644
@@ -10,15 +10,6 @@
 #define IOPORT_START			0x6200
 #define IOPORT_SIZE			0x400
 
-#define IOPORT_VESA			0xa200
-#define IOPORT_VESA_SIZE		256
-#define IOPORT_VIRTIO_P9		0xb200	/* Virtio 9P device */
-#define IOPORT_VIRTIO_P9_SIZE		256
-#define IOPORT_VIRTIO_CONSOLE		0xd200	/* Virtio console device */
-#define IOPORT_VIRTIO_CONSOLE_SIZE	256
-#define IOPORT_VIRTIO_NET		0xe200	/* Virtio network device */
-#define IOPORT_VIRTIO_NET_SIZE		256
-
 struct kvm;
 
 struct ioport_operations {
Signature · 1 line
1.7.5.rc3
#5 lore raw

PATCH 5/9 kvm tools: Use dynamic IO port allocation in vesa driver

In reply to Sasha Levin
---
 tools/kvm/hw/vesa.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
tools/kvm/hw/vesa.c +4 -3
index 6ab07ee..eb3ee65 100644
@@ -49,7 +49,6 @@ static struct pci_device_header vesa_pci_device = {
 	.class			= 0x030000,
 	.subsys_vendor_id	= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
 	.subsys_id		= PCI_SUBSYSTEM_ID_VESA,
-	.bar[0]			= IOPORT_VESA   | PCI_BASE_ADDRESS_SPACE_IO,
 	.bar[1]			= VESA_MEM_ADDR | PCI_BASE_ADDRESS_SPACE_MEMORY,
 };
 
@@ -66,16 +65,18 @@ void vesa__init(struct kvm *kvm)
 {
 	u8 dev, line, pin;
 	pthread_t thread;
+	u16 vesa_base_addr;
 
 	if (irq__register_device(PCI_DEVICE_ID_VESA, &dev, &pin, &line) < 0)
 		return;
 
 	vesa_pci_device.irq_pin		= pin;
 	vesa_pci_device.irq_line	= line;
-
+	vesa_base_addr			= ioport__find_free_range();
+	vesa_pci_device.bar[0]		= vesa_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
 	pci__register(&vesa_pci_device, dev);
 
-	ioport__register(IOPORT_VESA, &vesa_io_ops, IOPORT_VESA_SIZE);
+	ioport__register(vesa_base_addr, &vesa_io_ops, IOPORT_SIZE);
 
 	kvm__register_mmio(VESA_MEM_ADDR, VESA_MEM_SIZE, &vesa_mmio_callback);
 
Signature · 1 line
1.7.5.rc3
#2 lore raw

Re: [PATCH 2/9] kvm tools: Add basic ioport dynamic allocation

In reply to Sasha Levin

tools/kvm/ioport.c static void ioport_error(u16 port, void *data, int direction, int size, u32 coun · line 131

Quoted 3 lines · Sasha Levin
tools/kvm/ioport.c +3 -0
+u16 ioport__find_free_range(void)
+{
+	static u16 cur_loc;

Please don't put statics inside function bodies! (even if they are only used within a single function)

I had to look three times to discover that it's really a global variable. These should be where other global variables are, or should be put before the function, in plain sight.

Also, if you do that i'd suggest a more descriptive name - free_io_port_idx perhaps?

tools/kvm/ioport.c static void ioport_error(u16 port, void *data, int direction, int size, u32 coun · line 133

Quoted 1 line
tools/kvm/ioport.c +1 -0
+	return IOPORT_START + (cur_loc++ * IOPORT_SIZE);

So this is SMP unsafe really. While ioport registrations are currently only used from initdev() functions and are thus serialized, it's not completely unfeasible that we would want to have async initcalls like the kernel does, to improve bootup/startup performance!

So it would be nice to make this all SMP safe. A single mutex would suffice i suspect.

Thanks,

Ingo

#8 lore raw

Re: [PATCH 7/9] kvm tools: Use dynamic IO port allocation in virtio-console driver

In reply to Sasha Levin
Quoted 6 lines · Sasha Levin

void virtio_console__init(struct kvm *kvm)
{

tools/kvm/virtio/console.c +1 -0
 	u8 dev, line, pin;
+	u16 console_base_addr;
 
 	if (irq__register_device(VIRTIO_ID_CONSOLE, &dev, &pin, &line) < 0)

tools/kvm/virtio/console.c static struct ioport_operations virtio_console_io_ops = { · line 257

Quoted 10 lines
tools/kvm/virtio/console.c +4 -1
 		return;
 
 	virtio_console_pci_device.irq_pin	= pin;
 	virtio_console_pci_device.irq_line	= line;
+	console_base_addr			= ioport__find_free_range();
+	virtio_console_pci_device.bar[0]	= console_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+	cdev.base_addr				= console_base_addr;
 	pci__register(&virtio_console_pci_device, dev);
-	ioport__register(IOPORT_VIRTIO_CONSOLE, &virtio_console_io_ops, IOPORT_VIRTIO_CONSOLE_SIZE);
+	ioport__register(console_base_addr, &virtio_console_io_ops, IOPORT_SIZE);

Why is the ioport registration done in two steps?

Wouldnt a better sequence be something like:

tools/kvm/virtio/console.c static struct ioport_operations virtio_console_io_ops = { · line 256

Quoted 5 lines
tools/kvm/virtio/console.c +3 -0
+	console_base_addr			= ioport__register(&virtio_console_io_ops, IOPORT_SIZE);
 
+	virtio_console_pci_device.bar[0]	= console_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+	cdev.base_addr				= console_base_addr;
 	pci__register(&virtio_console_pci_device, dev);

I.e. first register the ioport range - this would also get a free range for you, and then register the PCI driver?

Or something even more compact could be done i suspect - all of the drivers seem to be using the same registration sequence.

Thanks,

Ingo

#12 lore raw

Re: [PATCH 9/9] kvm tools: Remove static ioport allocations

In reply to Sasha Levin
Quoted 11 lines · Sasha Levin
---
 tools/kvm/include/kvm/ioport.h |    9 ---------
 1 files changed, 0 insertions(+), 9 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -0
index 709e18e..cc57cdd 100644
@@ -10,15 +10,6 @@
 #define IOPORT_START			0x6200

tools/kvm/include/kvm/ioport.h line 20

Quoted 10 lines
tools/kvm/include/kvm/ioport.h +0 -8
 #define IOPORT_SIZE			0x400
 
-#define IOPORT_VESA			0xa200
-#define IOPORT_VESA_SIZE		256
-#define IOPORT_VIRTIO_P9		0xb200	/* Virtio 9P device */
-#define IOPORT_VIRTIO_P9_SIZE		256
-#define IOPORT_VIRTIO_CONSOLE		0xd200	/* Virtio console device */
-#define IOPORT_VIRTIO_CONSOLE_SIZE	256
-#define IOPORT_VIRTIO_NET		0xe200	/* Virtio network device */
-#define IOPORT_VIRTIO_NET_SIZE		256

Btw., nice end result :-)

Thanks,

Ingo

#9 lore raw

Re: [PATCH 7/9] kvm tools: Use dynamic IO port allocation in virtio-console driver

In reply to Ingo Molnar
Quoted 32 lines · Ingo Molnar
Quoted 6 lines · Sasha Levin

void virtio_console__init(struct kvm *kvm)
{

tools/kvm/virtio/console.c +1 -0
 	u8 dev, line, pin;
+	u16 console_base_addr;
 
 	if (irq__register_device(VIRTIO_ID_CONSOLE, &dev, &pin, &line) < 0)

tools/kvm/virtio/console.c static struct ioport_operations virtio_console_io_ops = { · line 257

Quoted 10 lines
tools/kvm/virtio/console.c +4 -1
 		return;
 
 	virtio_console_pci_device.irq_pin	= pin;
 	virtio_console_pci_device.irq_line	= line;
+	console_base_addr			= ioport__find_free_range();
+	virtio_console_pci_device.bar[0]	= console_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+	cdev.base_addr				= console_base_addr;
 	pci__register(&virtio_console_pci_device, dev);
-	ioport__register(IOPORT_VIRTIO_CONSOLE, &virtio_console_io_ops, IOPORT_VIRTIO_CONSOLE_SIZE);
+	ioport__register(console_base_addr, &virtio_console_io_ops, IOPORT_SIZE);

Why is the ioport registration done in two steps?

Wouldnt a better sequence be something like:

tools/kvm/virtio/console.c static struct ioport_operations virtio_console_io_ops = { · line 256

Quoted 5 lines
tools/kvm/virtio/console.c +3 -0
+	console_base_addr			= ioport__register(&virtio_console_io_ops, IOPORT_SIZE);
 
+	virtio_console_pci_device.bar[0]	= console_base_addr | PCI_BASE_ADDRESS_SPACE_IO;
+	cdev.base_addr				= console_base_addr;
 	pci__register(&virtio_console_pci_device, dev);

I.e. first register the ioport range - this would also get a free range for you, and then register the PCI driver?

Quoted 2 lines

Or something even more compact could be done i suspect - all of the drivers seem to be using the same registration sequence.

Some drivers need explicit IO ports, such as the serial driver.

It was a question of whether I create another registration function (which may be as simple as calling 'ioport__find_free_range()' before calling the real registration) but it would lead to two 'registration' functions behaving very differently - one returning a new port and one that just does nothing except registering.

Signature · 1 line
Sasha.