cpu: make kvm-stub.o a part of CPU library

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v2:
  remove unnecessary kvm-stub.o from "System emulator target" section
(cherry picked from commit 4604df6ec87fba3247db0fa3c4c5dc9e07e571b9)
cpu: call cpu_synchronize_post_init() from CPUClass.realize() if hotplugged

... to synchronize CPU state to KVM

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v2:
  * linking kvm-stub.o to *-user target moved in separate patch
(cherry picked from commit cb5f4c6a786cab4e15e8f481a334baab49d1ca9a)
introduce resume_vcpu(), for single CPU

... also add stub for it, to make possible to use it in qom/cpu.c,
which is shared with user emulators.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
Note:
  it will be re-used from qom/cpu.c by hotpluged CPU
(cherry picked from commit ad1d68872dade8870dd74a2e0635f45ae40aa676)
cpu: resume CPU from CPUClass.cpu_common_realizefn() when it is hot-plugged

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v2:
 * remove unnecessary now sysemu/cpus.h header
 * move out introducing resume_vcpu() into a separate patch
(cherry picked from commit 58fd5a774515be939ee5d51dbda9af9406d6e6e3)
introduce CPU hot-plug notifier

hot-added CPU will be distributed to acpi_piix4 and rtc_cmos

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
---
v2:
  * move notifier to qom/cpu.c and call it from CPUClass.realize() on hotplug
  * remove get_firmware_id() since it belong to other patch
(cherry picked from commit a4a7d309b671ebd8cfd25848be8c295b65fb8978)
target-i386: pc: update rtc_cmos on CPU hot-plug

... so that on reboot BIOS could read current available CPU count

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
(cherry picked from commit 95ddaaf55429863297e9e39ca8d96e223e564f98)
cpu: introduce get_arch_id() method and override it for target-i386

get_arch_id() adds possibility for generic code to get guest visible
CPU ID without accessing CPUArchState. If target doesn't override it,
it will return cpu_index.

Override it on target-i386 to return APIC ID.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 * it will be used later by new cpu_exists() generic function and
   acpi_piix.
 * s/cpu_firmware_id/cpu_arch_id/
(cherry picked from commit dfb91b03439d129d6a37274473e4747dec878e4c)
cpu: add helper cpu_exists(), to check if CPU with specified id exists

... it should be used only on slow path since it does recursive search
    on /machine QOM tree for objects of TYPE_CPU

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
---
v2:
  * s/get_firmware_id()/get_arch_id()/ rebase fixup
  * remove check for get_arch_id being NULL, since it's always defined
(cherry picked from commit 1846755cb53ad9eccc2170fb125d424fb84a593a)
acpi_piix4: add infrastructure to send CPU hot-plug GPE to guest

* introduce processor status bitmask visible to guest at 0xaf00 addr,
  where ACPI asl code expects it
* set bit corresponding to APIC ID in processor status bitmask on
  receiving CPU hot-plug notification
* trigger CPU hot-plug SCI, to notify guest about CPU hot-plug event

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v3:
  * s/get_firmware_id()/get_arch_id()/ due rebase
  * s/cpu_add_notifier/cpu_added_notifier/
v2:
  * use CPUClass.get_firmware_id() to make code target independent
  * bump up vmstate_acpi version
(cherry picked from commit 2ba7051594c1332e6acdb49509640e5eb26b3903)
target-i386: introduce apic-id property

... and use it from board level to set APIC ID for CPUs
it creates.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
---
Note:
  * pc_new_cpu() function will be reused later in CPU hot-plug hook.

v3:
  * user error_propagate() in property setter
v2:
  * use generic cpu_exists() instead of custom one
  * make apic-id dynamic property, so it won't be possible to use it
    as global property, since it's instance specific
(cherry picked from commit 3363b7f5e64f4812600c9c640b6a18b13b05ff45)
introduce ICC bus/device/bridge

... to provide hotplug-able bus.

* icc-bridge will serve as a parent for icc-bus and provide
  mmio mapping services to child icc-devices.
* icc-device will replace SysBusDevice as a parent of APIC
  and IOAPIC devices.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v2:
  * Rebase on top the last HW reorganization series.
  * Move hw/icc_bus.c into hw/cpu/ and hw/icc_bus.h include/hw/i386/
(cherry picked from commit afebc13fcf87a057675bfbf096b856b35c901ea7)
target-i386: cpu: attach ICC bus to CPU on its creation

... during startup,  so it would be possible to unplug it later
and set bus_type to TYPE_ICC_BUS for X86CPU type to make device_add
attach hotplugged CPU to ICC bus.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
(cherry picked from commit 9ede53124a7c2dc506ff74d34063d81bdf938ea4)
target-i386: replace MSI_SPACE_SIZE with APIC_SPACE_SIZE

... and put APIC_SPACE_SIZE in public header so that it could be
reused later elsewhere.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
(cherry picked from commit b4e318ffe6ccc851e4238d75911ebd9afcd92505)
target-i386: move APIC to ICC bus

... to allow it to be hotplugged

 * map APIC's mmio at board level if it is present
 * do not register mmio region for each APIC, since
   only one is used/mapped
target-i386: move IOAPIC to ICC bus

 * inherit IOAPICs from ICCDevice and attach them to ICC bus
 * map IOAPIC's mmio at board level via indirect icc-bridge
   mmio region that provides address space to IOAPIC via
   icc-bus

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
 * do not create IOAPIC in icc-bridge and do not make it QOM child
   of icc-bridge, just attach it to icc-bus.
(cherry picked from commit 1df59952c489dd653a6aa41ef12653bef0c1c221)
add cpu-add qmp command and implement CPU hot-add for target-i386

... via current_machine->cpu_hot_add() hook called by cpu-set QMP command,
for x86 target.

cpu-add's "id" argument is a CPU thread number in a range [0..max-cpus - 1)

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v5:
  * accept id=[0..max_cpus) range in cpu-add command
v4:
  * merge "qmp: add cpu-add qmp command" & "target-i386: implement CPU hot-add" patches
  * move notifier call to CPUCLass.realize()
  * add hook cpu_hot_add to QEMUMachine
  * make QEMUMachineInitArgs global and keep default cpu_model there

v3:
  * it appears that 'online/offline' in cpu-set are confusing people
    with what command actually does and users might have to distinguish
    if 'offline' is not implemented by parsing error message. To simplify
    things replace cpu-set with cpu-add command to show more clear what
    command does and just add cpu-del when CPU remove is implemented.

v2:
  * s/cpu_set/cpu-set/
  * qmp doc style fix
  * use bool type instead of opencodding online/offline string
     suggested-by: Eric Blake <eblake@redhat.com>
(cherry picked from commit af13d9ff5f2941b5bd106c71b2922f98b4891113)