From patchwork Tue Jan 28 14:21:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 860407 Delivered-To: patch@linaro.org Received: by 2002:a5d:6b8c:0:b0:385:e875:8a9e with SMTP id n12csp375933wrx; Tue, 28 Jan 2025 06:23:27 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCV7x960iQwwePJfxfrincGBy7zRgODkB3S6VrggTydAZZZO7HKak+mF6jWUtTwBZ9JA2gDeTw==@linaro.org X-Google-Smtp-Source: AGHT+IH2663m2d2zJtFH3Zr6wcxXrR49SmP9CbLBjUZyElTkJ6zx2T7sLYXSDBEKaPbxSuyTNR1J X-Received: by 2002:a05:620a:4107:b0:7af:cf07:905b with SMTP id af79cd13be357-7be631e521dmr6134877185a.2.1738074207324; Tue, 28 Jan 2025 06:23:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1738074207; cv=none; d=google.com; s=arc-20240605; b=MVkc57lZ/fn2HGs/pDZ/5NrfwcfBn2uKrqToSSFcYB0xkIfYEpJObu1fHCvfY6kS18 IBpE56kZSPCcH6iP9NlhoOjvKkgMFwO1WorknpmktfciTJX+/RZD5HJuMtv/TLFlzs+n M/FTw8oUCJy7JKN86fISg7uTqKu4c8YKkiM9JOw4C3GEjuSi8nEn0IGERTthu3V3ahE6 F1HjZMo1CD2Y4opjim/rdj/lvMxfOWbPb4W69ahGay/p8pHfbrpuyjyiSG0CmZsw+/48 WCbF5KLCzhiQtYS8wsYReYYyqfAc3XnbRIVPS7WUvx+QcAEZIkm/apJciJemheuSiokK /zCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+boILYhYAtgeHGjq7jZm3ySgVoHBXMK8/hh/+aqTVRc=; fh=U6T2d4RqSCGJNf6LOIblgdoe4xaHu2Qwae+meUOhpik=; b=BxEaCnZzzvxH39GmtVrx4k9KC8k1cluMrtT3wyUak9+9IBoYLiijxNExBCtDIcjuhG Ra3Xcg4BJi6W+UBSovfl7efbMu0/8I71LLbZGqCS9ffAC2WaY687SSSOYSu58oV76urV j2JiI6IynCyTfGU+vvsMk6zfDiC8lnMH6vVEWb1rmHWOK8BI7GF+vK+YYELvt3sASKXI fuQ+eC8xPUTgX9bYVPTZqQT5qxhE0hOzCQa47fk4jsPAGJs7bMdNgInpVM3XfggRwkEH yrc7B4vx3FOtjrsr/V0rVmTj12vMXVIu2z1wZj8rasFK+EZnU1dqP0TlWYiEdHOd8Qhr DOFw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="zDKJ/uzz"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7be9aee0163si1260904185a.247.2025.01.28.06.23.27 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 28 Jan 2025 06:23:27 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="zDKJ/uzz"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tcmTw-0007sL-L2; Tue, 28 Jan 2025 09:22:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tcmTs-0007pl-Pc for qemu-devel@nongnu.org; Tue, 28 Jan 2025 09:22:20 -0500 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tcmTq-0002LJ-Ob for qemu-devel@nongnu.org; Tue, 28 Jan 2025 09:22:20 -0500 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-43624b2d453so63131265e9.2 for ; Tue, 28 Jan 2025 06:22:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1738074135; x=1738678935; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+boILYhYAtgeHGjq7jZm3ySgVoHBXMK8/hh/+aqTVRc=; b=zDKJ/uzzSbnVMhEOyWrbWTzi4EhJrFG1NPvkd+qVf5cp8ivj+C6fplNYZlCvoj3+iX BrkUg9rSTy6zQODNdtrU7UIIHijXiaJfKp1orT0nTU7prNKr/ZDD/Oa8JW99/f2x8PfC pHQjcxv2d0ZbIdzjX0AiaO64AZgA+uKvoWsM3RTZAdEwkZPGp03U21bhswQNaS22EJQJ AK1RewFv5Pa3xenVm13SCiqp3rahKzebYBUrshAmxVF15PVQIjcESCQBwgcm9lBDnoES LKNLYnk9XCoIqnNfalYZWbIhusdcQD/hiaAMdi6IvOYMnomAjAQI7k9eP/tKRgt9f7FO 2Pww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738074135; x=1738678935; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+boILYhYAtgeHGjq7jZm3ySgVoHBXMK8/hh/+aqTVRc=; b=UKil+ei9nhFmvQF5InLCPKQlPkyUOJkktMCa86WqvBosqZ1oa12523MYWH/nZbiM09 feEnD5bDpBEb91WqtIRvyozCKkH7uV+fu7dkmKQpK17BaU0Yc/VzUp3wQC0Fvlu0f1AL 4FBMAACAJ3Wjyan5YaN44IAEBoacM7U5qTrn0PjjOna7Sl4TW5LmnuqbWn3ANAUcMF0l Sr2lStwfGHowAnO5GYzTrj32ptEkiD7MpZa/b7Bdxd862QKaMc84rtRlxMCQ3FcelDsF 1BgBbi9m59UUPYV5AoDMlSyb6U0/NII2DXD2FXkhmkmbaCPxD+5XEQeeqph1/mvYP9TP 87cQ== X-Gm-Message-State: AOJu0YzUqRA5fitYnDcLZD1hJCqqk3FPYw9fcJSgI/OdQ4iQOfXegMzC OeCgyRP1+yENqQFXvv5oU2bxBBUxEjX5KAECAraQajqMyOEbvlLENdG++q5UCtd3IYSLms7O+is leVE= X-Gm-Gg: ASbGncsyVqMCB1URD9zqaR3vFbdCKPVOLa0t2IFuCwUYlpuwq7WySQuhKf5p60Oo9YU VFxQuBwohq6uYke8DR4UM+w0RwJeQveL+3nTm4EebREKBdpGWWB30/gWI6Obvm9n5IQvags+Vth ta6pptbZHSLla9/AR2BoV1kfpFe0MzXmiXdBW24lUP8QUanftTME4R1bnQLwmiEPafXDaPi6xFy Z2fycsC11uOALXDpogtZ3iNtRxUUGYWmFrmiKsa69ZLG9W/isYzu+1N+hnbtzXqRoOJCNj8YOZq YM43UQ2dQPvdi8zirkYPd2jG08t3V4302P/3Qc8sCxxPbLwwWZSyYiBLJfFAG2ubfQ== X-Received: by 2002:a05:600c:1386:b0:434:f739:7cd9 with SMTP id 5b1f17b1804b1-438913cf349mr381680345e9.9.1738074135029; Tue, 28 Jan 2025 06:22:15 -0800 (PST) Received: from localhost.localdomain (88-187-86-199.subs.proxad.net. [88.187.86.199]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438bd4857b8sm168810805e9.15.2025.01.28.06.22.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 28 Jan 2025 06:22:14 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Peter Maydell , Yanan Wang , Eduardo Habkost , Harsh Prateek Bora , kvm@vger.kernel.org, Zhao Liu , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Markus Armbruster , Igor Mammedov , Marcel Apfelbaum , Peter Xu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Paolo Bonzini , Richard Henderson Subject: [RFC PATCH 4/9] hw/qdev: Introduce DeviceClass::[un]wire() handlers Date: Tue, 28 Jan 2025 15:21:47 +0100 Message-ID: <20250128142152.9889-5-philmd@linaro.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250128142152.9889-1-philmd@linaro.org> References: <20250128142152.9889-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=philmd@linaro.org; helo=mail-wm1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We are trying to understand what means "a qdev is realized". One explanation was "the device is guest visible"; however many devices are realized before being mapped, thus are not "guest visible". Some devices map / wire their IRQs before being realized (such ISA devices). There is a need for devices to be "automatically" mapped/wired (see [2]) such CLI-created devices, but this apply generically to dynamic machines. Currently the device creation steps are expected to roughly be: (external use) (QDev core) (Device Impl) ~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~ INIT enter -----> +----------------------+ | Allocate state | +----------------------+ -----> +---------------------+ | INIT children | | | | Alias children properties | | | Expose properties | INIT exit +---------------------+ <----------------------------------- +----------------+ | set properties | | | | set ClkIn | +----------------+ REALIZE enter ----------------------------------> +----------------------+ | Use config properties| | | | Realize children | | | | Init GPIOs/IRQs | | | | Init MemoryRegions | +----------------------+ REALIZE exit <----------------------------------- ---- "realized" / "guest visible" +-----------------+ | Explicit wiring:| | IRQs | | I/O / Mem | | ClkOut | +-----------------+ RESET enter ---------------------------------> +----------------------+ | Reset default values | +----------------------+ But as mentioned, various devices "wire" parts before they exit the "realize" step. In order to clarify, I'm trying to enforce what can be done *before* and *after* realization. *after* a device is expected to be stable (no more configurable) and fully usable. To be able to use internal/auto wiring (such ISA devices) and keep the current external/explicit wiring, I propose to add an extra "internal wiring" step, happening after the REALIZE step as: (external use) (QDev core) (Device Impl) ~~~~~~~~~~~~ ~~~~~~~~~ ~~~~~~~~~~~ INIT enter -----> +----------------------+ | Allocate state | +----------------------+ -----> +---------------------+ | INIT children | | | | Alias children properties | | | Expose properties | INIT exit +---------------------+ <----------------------------------- +----------------+ | set properties | | | | set ClkIn | +----------------+ REALIZE enter ----------------------------------> +----------------------+ | Use config properties| | | | Realize children | | | | Init GPIOs/IRQs | | | | Init MemoryRegions | +----------------------+ REALIZE exit <--- +----------------------+ | Internal auto wiring | | IRQs | (i.e. ISA bus) | I/O / Mem | | ClkOut | +----------------------+ <--- ---- "realized" +-----------------+ | External wiring:| | IRQs | | I/O / Mem | | ClkOut | +-----------------+ RESET enter ---- "guest visible" ---------------------------------> +----------------------+ | Reset default values | +----------------------+ The "realized" point is not changed. "guest visible" concept only occurs *after* wiring, just before the reset phase. This change introduces the DeviceClass::wire handler within qdev core realization code. Signed-off-by: Philippe Mathieu-Daudé --- include/hw/qdev-core.h | 7 +++++++ hw/core/qdev.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 530f3da7021..021bb7afdc0 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -102,7 +102,12 @@ typedef int (*DeviceSyncConfig)(DeviceState *dev, Error **errp); * @props: Properties accessing state fields. * @realize: Callback function invoked when the #DeviceState:realized * property is changed to %true. + * @wire: Callback function called after @realize to connect IRQs, + * clocks and map memories. Can not fail. + * @unwire: Callback function to undo @wire. Called before @unrealize. + * Can not fail. * @unrealize: Callback function invoked when the #DeviceState:realized + * property is changed to %false. Can not fail. * property is changed to %false. * @sync_config: Callback function invoked when QMP command device-sync-config * is called. Should synchronize device configuration from host to guest part @@ -171,6 +176,8 @@ struct DeviceClass { */ DeviceReset legacy_reset; DeviceRealize realize; + void (*wire)(DeviceState *dev); + void (*unwire)(DeviceState *dev); DeviceUnrealize unrealize; DeviceSyncConfig sync_config; diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 82bbdcb654e..38449255365 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -554,6 +554,15 @@ static void device_set_realized(Object *obj, bool value, Error **errp) } } + if (dc->wire) { + if (!dc->unwire) { + warn_report_once("wire() without unwire() for type '%s'", + object_get_typename(OBJECT(dev))); + } + dc->wire(dev); + } + + /* At this point the device is "guest visible". */ qatomic_store_release(&dev->realized, value); } else if (!value && dev->realized) { @@ -573,6 +582,15 @@ static void device_set_realized(Object *obj, bool value, Error **errp) */ smp_wmb(); + if (dc->unwire) { + if (!dc->wire) { + error_report("disconnect() without connect() for type '%s'", + object_get_typename(OBJECT(dev))); + abort(); + } + dc->unwire(dev); + } + QLIST_FOREACH(bus, &dev->child_bus, sibling) { qbus_unrealize(bus); } @@ -585,8 +603,8 @@ static void device_set_realized(Object *obj, bool value, Error **errp) dev->pending_deleted_event = true; DEVICE_LISTENER_CALL(unrealize, Reverse, dev); } - assert(local_err == NULL); + return; child_realize_fail: