*spapr_events: re-use EPOW event infrastructure for hotplug events

This extends the data structures currently used to report EPOW events to
gets via the check-exception RTAS interfaces to also include event types
for hotplug/unplug events.

This is currently undocumented and being finalized for inclusion in PAPR
specification, but we implement this here as an extension for guest
userspace tools to implement (existing guest kernels simply log these
events via a sysfs interface that's read by rtas_errd).

(need to introduce additional event to differentiate being guest-managed
 (sysfs rescan interface) and qemu/host-managed (rpaphp interface) BAR
 assignments)

Signed-off-by: Nathan Fontenot <nfont@linux.vnet.ibm.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
spapr_events: event-scan RTAS interface

We don't actually rely on this interface to surface hotplug events, and
instead rely on the similar-but-interrupt-driven check-exception RTAS
interface used for EPOW events. However, the existence of this interface
is needed to ensure guest kernels initialize the event-reporting
interfaces which will in turn be used by userspace tools to handle these
events, so we implement this interface as a stub.

Signed-off-by: Tyrel Datwyler <tyreld@linux.vnet.ibm.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
spapr_pci: emit hotplug add/remove events during hotplug

This uses extension of existing EPOW interrupt/event mechanism
to notify userspace tools like librtas/drmgr to handle
in-guest configuration/cleanup operations in response to
device_add/device_del.

Userspace tools that don't implement this extension will need
to be run manually in response/advance of device_add/device_del,
respectively.

Signed-off-by: Tyrel Datwyler <tyreld@linux.vnet.ibm.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
*spapr-pci: increase number of MSI capable devices per PHB

This increases the existing limit from 32 to 256 devices as
real applications may exceed it and 256 seems now way too big.

(make commit msg clearer, determine if this is really needed for
 the initial series)

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit cfa5294952b657ade415ab15250bc5f9f203dc29)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
*Hotplug of multiple disks fails due to MSI vector quota check.
Number of MSI vectors default to 8 allowing only 4 devices.
This happens on RHEL6.5 guest. RHEL7 and SLES11 guests fallback
to INTX.

One way to workaround the issue is to increase total MSIs,
so that MSI quota check allows us to hotplug multiple disks.

(authorship needs to be attributed to Badari. include in follow-up
 series or the initial patchset?)

(make commit msg clearer)

(should this match 256 from Alexey's prior patch?)

(maybe do all the MSI fixes in a separate series)

Signed-off-by: Badari Pulavarty <pbadari@us.ibm.com>
(cherry picked from commit d906be2b96dd98ba6cafdd9d7b0235857ef7d5fc)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
xics: add flags for interrupts

We will need soon an "allocated" flag for every interrupt to support
interrupt configuration change which may happen during migration.

This replaces a separate lslsi[] array with a byte in the ICSIRQState
struct and defines "LSI" and "MSI" flags. Neither of these flags set
signals that the descriptor is not in use.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit b015a456a381638c8a0209de95cb336b067b1bb4)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
xics: add find_server

PAPR allows having multiple interrupr servers. However now we
only support one.

This adds a server lookup function and makes use of it.

At the moment no change is expected.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit 7893f138ee44ef92c0d5f28c167d1468d6507d55)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
xics: add pre_load() hook to ICSStateClass

This adds a hook which will be used to reset ICS state before incoming
migration.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit fc26b34c2b21dc0425f32c83a6105ad31dc30032)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
spapr: move interrupt allocator to xics

The current allocator returns IRQ numbers from a pool and
does not support IRQs reuse in any form as it did not keep
track of what it previously returned. However migration
may change interrupts for devices depending on theis order
in the command line.

This adds an allocator to XICS and next patch will remove
the old one from SPAPR code.

The new allocator uses new flags field of the ICS IRQ array
which potentially allows reuse of IRQs.

This removes IRQ allocator from SPAPR code.

This makes use of new IRQ allocator API provided by XICS.

This allows IRQ override during migration. As long as devices
do not cache qemu_irq and use it just to pulse IRQ, we are fine.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit 19ca655416adc2c57557bfebbe6a0dde3e9e9843)

Conflicts:
	hw/ppc/spapr.c

*manually removed spapr_allocate_irq* funcs due to chunk being
 misidentified

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
*xics: enable interrupt configuration reset on migration

Interrupt numbers migrate along with other properties so
the initial QEMU setup will be reset by migration. Since
XICS migrates as well and this includes IRQ map with all
the flags saying which ones are already used, all we need
is just to reset the XICS IRQ array on the destination.
So does the patch.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit e753beb6e334ea067d505c63c12af37efaf35e24)

Conflicts:
	hw/intc/xics.c

*frobisher
*adopted ppc-next func signature in place of xics_common_cpu_setup

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
xics_kvm: Fix IRQ level for LSI and MSI

The b015a456a381638c8a0209de95cb336b067b1bb4 patch converted @islsi
array to @flags field and once the convertion was made wrong.

This fixes ics_kvm_set_irq() to use (again) level interrupts
for LSI and edge-triggered interrupts for MSI.

Fixed BZ 108182.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit 38639d077573cedca5e864d8effd88dcc133d1c6)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
xics: Remove obsolete xics_set_irq_type

Since IRQ allocation code is completely in XICS now,
there is no need in public function xics_set_irq_type() so
remove it.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit ce460690f4287b76e4f2ace494425dfeff6ed384)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
xics: Sync with latest version

This is reworked version of XICS patchset posted for beta3 which appears
to be very broken.

No change in behavior is expected but the next patch which enables
IRQ reuse for MSI depends on it.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit a11d05eb2f0f2db35fcb281fe6e276d7b1cd5d8c)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
spapr_pci: Use XICS interrupt allocator and do not cache interrupts in PHB

Currently SPAPR PHB keeps track of all allocated MSI/MISX interrupt as
XICS used to be unable to reuse interrupts which becomes a problem for
dynamic MSI reconfiguration which is happening on guest driver reload or
PCI hot (un)plug. Another problem is that PHB has a limit of devices
supporting MSI/MSIX (SPAPR_MSIX_MAX_DEVS=32) and there is no good reason
for that.

This makes use of new XICS capability to reuse interrupts.

This removes cached MSI configuration from SPAPR PHB so the first IRQ number
of a device is stored in MSI/MSIX config space so there is no need to store
this anywhere else. From now, SPAPR PHB only keeps flags telling what type
of interrupt which device has configured as we cannot look at if MSI/MSIX
is enabled in the config space - the configuration happens before actual
enablement.

This removes a limit for the maximum number of MSIX-enabled devices per PHB,
now XICS and PCI bus capacity are the only limitation.

This changes migration stream as it fixes vmstate_spapr_pci_msi::name which was
wrong since the beginning.

This fixed traces to be more informative.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
(cherry picked from commit 151bad9fb212a2b47c1207c4ffd5725707ca2219)

Conflicts:
	hw/ppc/spapr_pci.c
	trace-events

*frobisher
*manually merged in hunks due to duplicate hunks from 0190491 and
 context mismatches due to rtas error code macros

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>