cpu/virtio: turn virtio_is_big_endian() into a CPU object method

Legacy virtio devices are target endian, which means it depends on the CPU.
If we want to support targets that can change endianness (modern PPC and ARM
for the moment), we need to add a per-CPU class method to be called from
the virtio code: .has_big_endian_virtio. The name looks like a software
boundary violation, but please consider it as a hint for people to
avoid using it anywhere but in the virtio code... and FWIW, the violation
comes from the virtio specification^Wmess itself !

The base class implementation returns what we have today: it is based
on TARGET_WORDS_BIGENDIAN.

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio: add endian-ambivalent support to VirtIODevice

Some CPU families can dynamically change their endianness. This means we can
have little endian ppc or big endian arm guests for example. This has an
impact on legacy virtio data structures since they are target endian.
We hence introduce a new property to track the endianness of each virtio
device. The lifecycle is has follow:

	virtio_init()	set endianness to default (TARGET_WORDS_BIGENDIAN)
	     |
	virtio_reset()	set endiannes according to current_cpu
	     :
	     :
	virtio_reset()	set endiannes according to current_cpu (may change)
	     :
	     :
	virtio_save()   save endianness

	virtio_init()	set endianness to default (TARGET_WORDS_BIGENDIAN)
	     |
	virtio_load()	set endianness according to saved state
	     :
	     :

It is reasonnably assumed that endianness won't change while the device
is in use.

<disclaimer>
We should save the endianness property in the migration stream. Since it
only affects PPC and ARM targets, we should put it in a subsection. That
implies porting the whole virtio migration code to VMState... :-\

In the meantime, we try to guess endianness at load time with an ugly
heuristic involving available ring indexes.

An alternative hack for PPC is to assume that all CPUs have the same
endianness, and so have all virtio devices. Since CPU state is restored
first, we can randomly choose to rely on first_cpu to get the endianness.
</disclaimer>

Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio: memory accessors for endian-ambivalent targets

This is the virtio-access.h header file taken from Rusty's "endian-ambivalent
targets using legacy virtio" patch. It introduces helpers that should be used
when accessing vring data or by drivers for data that contains headers.
The virtio config space is also target endian, but the current code already
handles that with the virtio_is_big_endian() helper. There is no obvious
benefit at using the virtio accessors in this case.

Now we have two distinct paths: a fast inline one for fixed endian targets,
and a slow out-of-line one for targets that define the new TARGET_BIENDIAN
macro.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
[ relicensed virtio-access.h to GPLv2+ on Rusty's request,
  pass &address_space_memory to physical memory accessors,
  per-device endianness,
  virtio tswap16 and tswap64 helpers,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Cc: Cédric Le Goater <clg@fr.ibm.com>
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio: allow byte swapping for vring

Quoting original text from Rusty: "This is based on a simpler patch by Anthony
Liguouri".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
[ add VirtIODevice * argument to most helpers,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
Reviewed-by: Thomas Huth <thuth@linux.vnet.ibm.com>
virtio-net: use virtio wrappers to access headers

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
[ pass VirtIODevice * to memory accessors,
  TCP checksums fix by Cédric Le Goater,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Cc: Cédric Le Goater <clg@fr.ibm.com>
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio-balloon: use virtio wrappers to access page frame numbers

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
[ pass VirtIODevice * to memory accessors,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio-blk: use virtio wrappers to access headers

Note that st*_raw and ld*_raw are effectively replaced by st*_p and ld*_p.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
[ pass VirtIODevice * to memory accessors,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio-scsi: use virtio wrappers to access headers

Note that st*_raw and ld*_raw are effectively replaced by st*_p and ld*_p.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
[ pass VirtIODevice * to memory accessors,
  fix missing tswap32 in virtio_scsi_push_event() by Cédric Le Goater,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Cc: Cédric Le Goater <clg@fr.ibm.com>
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio-serial-bus: use virtio wrappers to access headers

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
[ pass VirtIODevice * to memory accessors,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
virtio-9p: use virtio wrappers to access headers

Note that st*_raw and ld*_raw are effectively replaced by st*_p and ld*_p.

Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
target-ppc: ppc64 can be either endian

This patch implements CPUClass::has_big_endian_virtio() for POWER7, POWER7+
and POWER8 families. In this case, the virtio device endianness depends on a
"system" endianness that is tracked with the ILE bit of the LPCR special
control register (endianness to enforce when entering an interrupt handler).

Since other PowerPC families may handle endianness differently, we first
introduce a PowerPCCPUClass::has_big_endian_interrupts() method that we
can call from the CPUClass::has_big_endian_virtio() implementation for
POWER7 and friends.

We also define TARGET_BIENDIAN to select appropriate virtio accessors.

Suggested-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
[ original hook superseded by CPU method,
  Greg Kurz <gkurz@linux.vnet.ibm.com> ]
Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
Reviewed-by: Alexander Graf <agraf@suse.de>