From patchwork Tue Feb 28 14:55:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 94628 Delivered-To: patches@linaro.org Received: by 10.140.20.113 with SMTP id 104csp1355725qgi; Tue, 28 Feb 2017 06:55:18 -0800 (PST) X-Received: by 10.99.104.199 with SMTP id d190mr2953022pgc.59.1488293718822; Tue, 28 Feb 2017 06:55:18 -0800 (PST) Return-Path: Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by mx.google.com with ESMTPS id r5si1982310pgh.151.2017.02.28.06.55.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Feb 2017 06:55:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) client-ip=2001:8b0:1d0::2; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1cijBI-0003ID-U9; Tue, 28 Feb 2017 14:55:12 +0000 From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: patches@linaro.org, Clement Deschamps , Paolo Bonzini Subject: [PATCH 1/4] qdev: Have qdev_set_parent_bus() handle devices already on a bus Date: Tue, 28 Feb 2017 14:55:08 +0000 Message-Id: <1488293711-14195-2-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488293711-14195-1-git-send-email-peter.maydell@linaro.org> References: <1488293711-14195-1-git-send-email-peter.maydell@linaro.org> Instead of qdev_set_parent_bus() silently doing the wrong thing if it's handed a device that's already on a bus, have it remove the device from the old bus and add it to the new one. This is useful for the raspi2 sdcard. Signed-off-by: Peter Maydell --- hw/core/qdev.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) -- 2.7.4 Reviewed-by: Igor Mammedov diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 06ba02e..923e626 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -102,9 +102,23 @@ static void bus_add_child(BusState *bus, DeviceState *child) void qdev_set_parent_bus(DeviceState *dev, BusState *bus) { + bool replugging = dev->parent_bus != NULL; + + if (replugging) { + /* Keep a reference to the device while it's not plugged into + * any bus, to avoid it potentially evaporating when it is + * dereffed in bus_remove_child(). + */ + object_ref(OBJECT(dev)); + bus_remove_child(dev->parent_bus, dev); + object_unref(OBJECT(dev->parent_bus)); + } dev->parent_bus = bus; object_ref(OBJECT(bus)); bus_add_child(bus, dev); + if (replugging) { + object_unref(OBJECT(dev)); + } } /* Create a new device. This only initializes the device state