From 889211b18b8d0acc814fbbe01b986f07b229a8c9 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Thu, 5 May 2016 17:14:37 +0200 Subject: apic: move MAX_APICS check to 'apic' class MAX_APICS is only used by child 'apic' class and not by its parent TYPE_APIC_COMMON or any other derived class. Move check into end user 'apic' class so it won't get in the way of other APIC implementations if they support more then MAX_APICS. Signed-off-by: Igor Mammedov Reviewed-by: Radim Krčmář Reviewed-by: Michael S. Tsirkin Signed-off-by: Eduardo Habkost --- hw/intc/apic.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'hw/intc/apic.c') diff --git a/hw/intc/apic.c b/hw/intc/apic.c index e1ab9354c6..b0d237b945 100644 --- a/hw/intc/apic.c +++ b/hw/intc/apic.c @@ -28,7 +28,9 @@ #include "trace.h" #include "hw/i386/pc.h" #include "hw/i386/apic-msidef.h" +#include "qapi/error.h" +#define MAX_APICS 255 #define MAX_APIC_WORDS 8 #define SYNC_FROM_VAPIC 0x1 @@ -869,6 +871,14 @@ static const MemoryRegionOps apic_io_ops = { static void apic_realize(DeviceState *dev, Error **errp) { APICCommonState *s = APIC_COMMON(dev); + static int apic_no; + + if (apic_no >= MAX_APICS) { + error_setg(errp, "%s initialization failed.", + object_get_typename(OBJECT(dev))); + return; + } + s->idx = apic_no++; memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi", APIC_SPACE_SIZE); -- cgit v1.2.3-55-g7522 From 1dfe3282cf851dce186ab15b07225e5d8588b63f Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 24 Jun 2016 16:27:00 +0200 Subject: apic: Drop APICCommonState.idx and use APIC ID as index in local_apics[] local_apics[] is sized to contain all APIC ID supported in xAPIC mode, so use APIC ID as index in it instead of constantly increasing counter idx. Fixes error "apic initialization failed" when a CPU hotplugged and unplugged more times than there are free slots in local_apics[]. Signed-off-by: Igor Mammedov Reviewed-by: Radim Krčmář Reviewed-by: Michael S. Tsirkin Signed-off-by: Eduardo Habkost --- hw/intc/apic.c | 16 +++++++--------- include/hw/i386/apic_internal.h | 1 - 2 files changed, 7 insertions(+), 10 deletions(-) (limited to 'hw/intc/apic.c') diff --git a/hw/intc/apic.c b/hw/intc/apic.c index b0d237b945..f473572fb4 100644 --- a/hw/intc/apic.c +++ b/hw/intc/apic.c @@ -421,7 +421,7 @@ static int apic_find_dest(uint8_t dest) int i; if (apic && apic->id == dest) - return dest; /* shortcut in case apic->id == apic->idx */ + return dest; /* shortcut in case apic->id == local_apics[dest]->id */ for (i = 0; i < MAX_APICS; i++) { apic = local_apics[i]; @@ -504,14 +504,14 @@ static void apic_deliver(DeviceState *dev, uint8_t dest, uint8_t dest_mode, break; case 1: memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask)); - apic_set_bit(deliver_bitmask, s->idx); + apic_set_bit(deliver_bitmask, s->id); break; case 2: memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); break; case 3: memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); - apic_reset_bit(deliver_bitmask, s->idx); + apic_reset_bit(deliver_bitmask, s->id); break; } @@ -871,20 +871,18 @@ static const MemoryRegionOps apic_io_ops = { static void apic_realize(DeviceState *dev, Error **errp) { APICCommonState *s = APIC_COMMON(dev); - static int apic_no; - if (apic_no >= MAX_APICS) { - error_setg(errp, "%s initialization failed.", - object_get_typename(OBJECT(dev))); + if (s->id >= MAX_APICS) { + error_setg(errp, "%s initialization failed. APIC ID %d is invalid", + object_get_typename(OBJECT(dev)), s->id); return; } - s->idx = apic_no++; memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi", APIC_SPACE_SIZE); s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s); - local_apics[s->idx] = s; + local_apics[s->id] = s; msi_nonbroken = true; } diff --git a/include/hw/i386/apic_internal.h b/include/hw/i386/apic_internal.h index 67348e9571..8330592638 100644 --- a/include/hw/i386/apic_internal.h +++ b/include/hw/i386/apic_internal.h @@ -174,7 +174,6 @@ struct APICCommonState { uint32_t initial_count; int64_t initial_count_load_time; int64_t next_time; - int idx; /* not actually common, used only by 'apic' derived class */ QEMUTimer *timer; int64_t timer_expiry; int sipi_vector; -- cgit v1.2.3-55-g7522 From 9c156f9de52b75510d3951dfede2cf96773b6626 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Thu, 23 Jun 2016 17:30:14 +0200 Subject: (kvm)apic: Add unrealize callbacks Callbacks will do necessary cleanups before APIC device is deleted Signed-off-by: Chen Fan Signed-off-by: Gu Zheng Signed-off-by: Zhu Guihua Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Eduardo Habkost --- hw/i386/kvm/apic.c | 5 +++++ hw/intc/apic.c | 10 ++++++++++ hw/intc/apic_common.c | 13 +++++++++++++ include/hw/i386/apic_internal.h | 1 + 4 files changed, 29 insertions(+) (limited to 'hw/intc/apic.c') diff --git a/hw/i386/kvm/apic.c b/hw/i386/kvm/apic.c index 1f87e73d4d..2bd0de82b4 100644 --- a/hw/i386/kvm/apic.c +++ b/hw/i386/kvm/apic.c @@ -192,11 +192,16 @@ static void kvm_apic_realize(DeviceState *dev, Error **errp) } } +static void kvm_apic_unrealize(DeviceState *dev, Error **errp) +{ +} + static void kvm_apic_class_init(ObjectClass *klass, void *data) { APICCommonClass *k = APIC_COMMON_CLASS(klass); k->realize = kvm_apic_realize; + k->unrealize = kvm_apic_unrealize; k->reset = kvm_apic_reset; k->set_base = kvm_apic_set_base; k->set_tpr = kvm_apic_set_tpr; diff --git a/hw/intc/apic.c b/hw/intc/apic.c index f473572fb4..45887d99c0 100644 --- a/hw/intc/apic.c +++ b/hw/intc/apic.c @@ -887,11 +887,21 @@ static void apic_realize(DeviceState *dev, Error **errp) msi_nonbroken = true; } +static void apic_unrealize(DeviceState *dev, Error **errp) +{ + APICCommonState *s = APIC_COMMON(dev); + + timer_del(s->timer); + timer_free(s->timer); + local_apics[s->id] = NULL; +} + static void apic_class_init(ObjectClass *klass, void *data) { APICCommonClass *k = APIC_COMMON_CLASS(klass); k->realize = apic_realize; + k->unrealize = apic_unrealize; k->set_base = apic_set_base; k->set_tpr = apic_set_tpr; k->get_tpr = apic_get_tpr; diff --git a/hw/intc/apic_common.c b/hw/intc/apic_common.c index fd425d1d3c..0bb9ad51fa 100644 --- a/hw/intc/apic_common.c +++ b/hw/intc/apic_common.c @@ -315,6 +315,18 @@ static void apic_common_realize(DeviceState *dev, Error **errp) } +static void apic_common_unrealize(DeviceState *dev, Error **errp) +{ + APICCommonState *s = APIC_COMMON(dev); + APICCommonClass *info = APIC_COMMON_GET_CLASS(s); + + info->unrealize(dev, errp); + + if (apic_report_tpr_access && info->enable_tpr_reporting) { + info->enable_tpr_reporting(s, false); + } +} + static int apic_pre_load(void *opaque) { APICCommonState *s = APIC_COMMON(opaque); @@ -421,6 +433,7 @@ static void apic_common_class_init(ObjectClass *klass, void *data) dc->reset = apic_reset_common; dc->props = apic_properties_common; dc->realize = apic_common_realize; + dc->unrealize = apic_common_unrealize; /* * Reason: APIC and CPU need to be wired up by * x86_cpu_apic_create() diff --git a/include/hw/i386/apic_internal.h b/include/hw/i386/apic_internal.h index 8330592638..e549a62e7f 100644 --- a/include/hw/i386/apic_internal.h +++ b/include/hw/i386/apic_internal.h @@ -136,6 +136,7 @@ typedef struct APICCommonClass DeviceClass parent_class; DeviceRealize realize; + DeviceUnrealize unrealize; void (*set_base)(APICCommonState *s, uint64_t val); void (*set_tpr)(APICCommonState *s, uint8_t val); uint8_t (*get_tpr)(APICCommonState *s); -- cgit v1.2.3-55-g7522