Re: [Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-17 Thread Paolo Bonzini

Il 17/02/2014 09:35, Alex David ha scritto:

- Are there solutions that seems more adapted to my case ? Like using
USB-I2C bridge ?


From an upstream point of view, a host passthrough device pair (one 
object talking to /dev/i2c-N on the host, and one device per sensor 
talking to the other object) would be the best.


But if you can make I2C-over-parallel work, that would also be 
interesting.  And not much new code to write: all you need to parse the 
bitbanging I2C protocol is already in hw/i2c/bitbang_i2c.c.


Paolo



Re: [Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-17 Thread Alex David
Thanks for all your answers.

I understand that what I want to achieve seemed pretty confused. I will try
to clarify :

On real hardware, I have an I2C device used to get temperatures, pressure
etc... and it works on x86 and there were no QEMU virtualized hardware
corresponding.

I don't really need to simulate the I2C hardware in QEMU. Indeed, there are
few of them just sending regular i2c data. For some tests, I want to be
able to send/receive these ones from my daemon on the host. After some
researches, what I was thinking about was :

1) Use virtio-serial and write an I2C driver (guest kernel) that would
give/take data to/from i2c-1 and read/write to vportp0... Seemed a bit
ugly, so I wanted to try something else.

2) Write virtio-i2c (using i2c-driver and virtio kernel basics) that would
register, for example, i2c-1 and get/send data from my guest app, and use
virtio to send these data to host.

What I have done for now : I used virtio-serial / virtio-console in linux
kernel and inspired from virtio-pci to try to registers these "vport0pX" as
i2c-1, i2c-2 etc... and as i2c devices. I also wrote a virtio-i2c QEMU-side
to register as this hardware using virtio-i2c drivers. I think my
understanding of the architecture is probably not complete, as it seems
that this QEMU device doesn't automatically registers as a "virtio-i2c"
hardware that would launch my guest kernel driver.
My printk's in the "probe" are not printed. My driver is then never used.

My questions were then :
- My solution might be a bit complicated assuming I don't have that much
knowledge in the architecture (however I'm interested into learning...)
- Are there solutions that seems more adapted to my case ? Like using
USB-I2C bridge ?


2014-02-14 17:45 GMT+01:00 Paolo Bonzini :

> Il 14/02/2014 17:31, Andreas Färber ha scritto:
>
>  While that is certainly possible in case host passthrough was desired,
>> maybe virtio was mixed up with VFIO?
>>
>
> I don't think so, VFIO is mostly about IOMMUs and protecting from DMA.
>
> Paolo
>


Re: [Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-14 Thread Paolo Bonzini

Il 14/02/2014 17:31, Andreas Färber ha scritto:

While that is certainly possible in case host passthrough was desired,
maybe virtio was mixed up with VFIO?


I don't think so, VFIO is mostly about IOMMUs and protecting from DMA.

Paolo



Re: [Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-14 Thread Andreas Färber
Am 14.02.2014 16:58, schrieb Paolo Bonzini:
> Il 13/02/2014 14:26, Alex David ha scritto:
>> After reading code, documentation and available things, I've been trying
>> to write something like a "virtio-i2c" : I wrote a virtio-i2c module for
>> my kernel (I used some examples from virtio-pci and virtio-console), it
>> seems that it created a "i2c-1" device in /dev,
>>
>> My device that I launch with QEMU (-chardev
>> socket,path=/tmp/test0,server,nowait,id=bob -device
>> virtio-i2c,chardev=bob) doesn't seem to be recognized by the kernel
>> driver : my probe function doesn't run.
> 
> i2c is a bus, not directly a device.  Do you want to pass an entire
> adapter down to the guest?  Or just a slave?
> 
> QEMU has i2c emulation but it is bus-based, so you need one device per
> slave + 1 for the adapter. x86 already has an I2C bus (actually it's
> SMBus) that you may be able to use.  So you probably want something like
> 
> -object i2c-linux,file=/dev/i2c-1,id=i2c-backend
> -device i2c-host,backend=i2c-backend,hostaddr=0x40,address=0x40
> -device i2c-host,backend=i2c-backend,hostaddr=0x50,address=0x50
> 
> and so on.  i2c-linux would be the object issuing ioctls to /dev/i2c-1,
> multiplexing access to the device for all the i2c-host devices.

While that is certainly possible in case host passthrough was desired,
maybe virtio was mixed up with VFIO? For now I believe it is PCI-only
and there was work on platform devices at KVM Forum 2013; for s390x a
ccw backend may be needed, so maybe it's possible to extend vfio for I2C?

If however it's just about QEMU reading data from a daemon on the host,
a simple slave device might do and is then independent of host I2C devices.

Regards,
Andreas

-- 
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg



Re: [Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-14 Thread Paolo Bonzini

Il 13/02/2014 14:26, Alex David ha scritto:

After reading code, documentation and available things, I've been trying
to write something like a "virtio-i2c" : I wrote a virtio-i2c module for
my kernel (I used some examples from virtio-pci and virtio-console), it
seems that it created a "i2c-1" device in /dev,

My device that I launch with QEMU (-chardev
socket,path=/tmp/test0,server,nowait,id=bob -device
virtio-i2c,chardev=bob) doesn't seem to be recognized by the kernel
driver : my probe function doesn't run.


i2c is a bus, not directly a device.  Do you want to pass an entire 
adapter down to the guest?  Or just a slave?


QEMU has i2c emulation but it is bus-based, so you need one device per 
slave + 1 for the adapter. x86 already has an I2C bus (actually it's 
SMBus) that you may be able to use.  So you probably want something like


-object i2c-linux,file=/dev/i2c-1,id=i2c-backend
-device i2c-host,backend=i2c-backend,hostaddr=0x40,address=0x40
-device i2c-host,backend=i2c-backend,hostaddr=0x50,address=0x50

and so on.  i2c-linux would be the object issuing ioctls to /dev/i2c-1, 
multiplexing access to the device for all the i2c-host devices.


If the default I2C bus is not good, I suggest that you write QEMU code 
to emulate an USB-I2C bridge, rather than write a virtio one.  It is not 
a performance-intensive path in all likelihood, and you won't have to 
write driver code for the guest.


Another alternative is to support the kernel's I2C-over-parallel 
interface.  You can then write a character device backend that maps 
/dev/i2c-1 read/write/ioctl to the parallel port interface that the 
kernel expects use it with a parallel port device.  It would be used 
like this:


-chardev i2c-linux,file=/dev/i2c-1,id=i2c-backend
-device parallel,chrdev=i2c-backend

Paolo



Re: [Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-14 Thread Stefan Hajnoczi
On Thu, Feb 13, 2014 at 02:26:16PM +0100, Alex David wrote:
> I'm new to QEMU and kinda new to driver & QEMU programming in general, so
> please excuse my questions...
> 
> I want to develop a new QEMU i2c device (qemu x86), that would get/send
> data to an application running on the guest. Thing is : I need these data
> onto the host, as a daemon will send/get the same kind of data to the guest.
> 
> After reading code, documentation and available things, I've been trying to
> write something like a "virtio-i2c" : I wrote a virtio-i2c module for my
> kernel (I used some examples from virtio-pci and virtio-console), it seems
> that it created a "i2c-1" device in /dev,
> 
> My device that I launch with QEMU (-chardev
> socket,path=/tmp/test0,server,nowait,id=bob -device virtio-i2c,chardev=bob)
> doesn't seem to be recognized by the kernel driver : my probe function
> doesn't run.
> 
> I might have missed something : how does a kernel driver uses the "probe"
> function with a QEMU device ?

Virtio devices have a device ID which the Linux guest virtio bus code
uses to probe the right driver.

For example:
$ grep virt /lib/modules/3.12.9-301.fc20.x86_64/modules.alias
alias virtio:d0004v* virtio_rng
alias virtio:d0003v* virtio_console
alias virtio:d0002v* virtio_blk
alias virtio:d0008v* virtio_scsi
alias virtio:d0001v* virtio_net

This is how udev knows to load the right kernel modules.  If you
compiled and installed your kernel modules correctly then it should be
automatically loaded.

If you want to dig deeper into how Linux driver loading works, see
drivers/virtio/virtio.c:virtio_bus.

> Hoping that it doesn't look too confused...

I'm confused about what you're trying to achieve.  If you want
host<->guest communication then the starting point is virtio-serial.
Maybe even the QEMU Guest Agent which can use virtio-serial.

If you want "real" I2C you should probably not use virtio.

Stefan



[Qemu-devel] Trying to write a new device / virtio-i2c ?

2014-02-13 Thread Alex David
Hello,

I'm new to QEMU and kinda new to driver & QEMU programming in general, so
please excuse my questions...

I want to develop a new QEMU i2c device (qemu x86), that would get/send
data to an application running on the guest. Thing is : I need these data
onto the host, as a daemon will send/get the same kind of data to the guest.

After reading code, documentation and available things, I've been trying to
write something like a "virtio-i2c" : I wrote a virtio-i2c module for my
kernel (I used some examples from virtio-pci and virtio-console), it seems
that it created a "i2c-1" device in /dev,

My device that I launch with QEMU (-chardev
socket,path=/tmp/test0,server,nowait,id=bob -device virtio-i2c,chardev=bob)
doesn't seem to be recognized by the kernel driver : my probe function
doesn't run.

I might have missed something : how does a kernel driver uses the "probe"
function with a QEMU device ?

Hoping that it doesn't look too confused...