diff mbox

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

Message ID 1306333427-26186-7-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
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/virtio/console.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

Comments

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

>  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);

Why is the ioport registration done in two steps?

Wouldnt a better sequence be something like:

> +	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
--
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
Sasha Levin May 25, 2011, 3:35 p.m. UTC | #2
On Wed, 2011-05-25 at 16:42 +0200, Ingo Molnar wrote:
> * Sasha Levin <levinsasha928@gmail.com> wrote:
> 
> >  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);
> 
> Why is the ioport registration done in two steps?
> 
> Wouldnt a better sequence be something like:
> 
> > +	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.

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.
diff mbox

Patch

diff --git a/tools/kvm/virtio/console.c b/tools/kvm/virtio/console.c
index affff0b..eeb5dc9 100644
--- a/tools/kvm/virtio/console.c
+++ b/tools/kvm/virtio/console.c
@@ -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);
 }