diff mbox

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

Message ID 1306333427-26186-2-git-send-email-levinsasha928@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sasha Levin May 25, 2011, 2:23 p.m. UTC
Add a very simple allocation of ioports.

This prevents the need to coordinate ioports between different
modules.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/include/kvm/ioport.h |    5 +++++
 tools/kvm/ioport.c             |    7 +++++++
 2 files changed, 12 insertions(+), 0 deletions(-)

Comments

Ingo Molnar May 25, 2011, 2:34 p.m. UTC | #1
* Sasha Levin <levinsasha928@gmail.com> wrote:

> +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?

> +	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
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/tools/kvm/include/kvm/ioport.h b/tools/kvm/include/kvm/ioport.h
index 2a8d74d..bc7ea02 100644
--- a/tools/kvm/include/kvm/ioport.h
+++ b/tools/kvm/include/kvm/ioport.h
@@ -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;
diff --git a/tools/kvm/ioport.c b/tools/kvm/ioport.c
index 159d089..0db5a87 100644
--- a/tools/kvm/ioport.c
+++ b/tools/kvm/ioport.c
@@ -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;