From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC5943E4C61 for ; Mon, 27 Apr 2026 17:32:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311135; cv=none; b=JBWtMppXqwnm34DKcR2akWf3wRD5wdQzjLwywN3Gbq+1X2er8V1DVvvFifi3mPMLH5ZG5hGOafRDZlxfduGmHFcyGl7L+Y9oIfk3in7sCenyPFVEAgDRaW9EcwcVoJbDTMNbH3VR9WSGls8/UD2J82kr+1UGGGcZRxV/Thq9K8M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311135; c=relaxed/simple; bh=NcY3/HYuL0HhUfal/SPd7W7TNW+k4WOElEB6PI8qU9k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NnsDFBP/4d4Zr1VliFPTg77CrlPcSeBBe/hyGvomRPZzRJ0mXZt+sG22wqG0sSBI1lFmAVw7sU8jb7VkbLVeT32Mjnuu7UO+maM2DNKSSgxvacmraw0g+QFDWcutyrhG+erABSt84ZNYAE7WxKML7AHao1yGbs1B17tAHeglUu8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KoCp6MI7; arc=none smtp.client-ip=209.85.215.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KoCp6MI7" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-c6e2355739dso4178496a12.2 for ; Mon, 27 Apr 2026 10:32:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777311133; x=1777915933; darn=vger.kernel.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=cgov5pKPFmVmTdXq6mInFjiLm1BGL5Qb5uWZW3rhPvs=; b=KoCp6MI7ihQ1bwIDS2DeLHV+yWIY4JZ/rqwzpvmZDjqm4t5Rpq3WepKiM5nEMUKOCc Gvon44fm1rt09i57qnvNTZYS00uXn5yNfGNvUUcMhemj+vz9VsA52LF9iMjZSMQTisS+ 0YydOI7aRr0+kmsxqpw5ZE//PxmcP7orWoDDtxSKCG4zc2jpDo2XX59uzSJur8qfTkAr pT3arB77BN45ee5ulbMNWWQCt+Wshh7rBgozcKycp84ANZRuOFt/sq09bd2kuuMGbY8I 3GcCgbyXoKjmx8OpmHkh5C2KTx/OwFiAlTpRBUDdDpYeENMLNsW45JGX3ROHOPawmR4M 0Ecw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777311133; x=1777915933; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cgov5pKPFmVmTdXq6mInFjiLm1BGL5Qb5uWZW3rhPvs=; b=RuMpyMVaE5/Mu+jwfFSX/z3kRQryASCPoM3uxAWhvHLC3MUL0KUj8N4eHziTpaD3aR F//nCXPY5sCMcQP/1f/gh/xX8ZEwW9Gpk3zHY9HOxox6sz3CztNxdyrnay4qbCM4Hfty c5yJYL75e60DNj20wNsh8m2ZAxAW3PHe92BtBknGpmfNWxj4RwDSFPVOKOkSGFfrxZXE jtqaGiXV4Jk7n7Rx2lL1++7h/8crzCM50yOu2t8z+VROOV4JN23wMTF7/mCt3rxc48oj VKtLI/UTl9VQ8q8DaoOAzy5vGQADs4O/aUNsxyR0F1QbzM2VukHaKfOfLbZy+q5EvmR9 10Yw== X-Forwarded-Encrypted: i=1; AFNElJ8FlpGF/68j6Q3/ipPqe4jyFzAIMFlO+wbqMYjaIdvZ370DS3z1uq8+KJ3xNn176KBoMolWoeKWpzUfEYA=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7KzSPSSYm/V/iPebK2LnVV/lcrJsChMJfmqdSayPLjVbzz/YU vvMqkF7TyBv8C68Q/2M8xZFQ9PaRXRA7T8l6X5lN87dHx74HdmORh6P0 X-Gm-Gg: AeBDiet0vJNgR+pQm5nFdL2Gw6LbZrSQ7t485r+NHjC8bbYFXCVRvuRwuM7h5Q+8ihF Oy8Dk1u+T3/5zhW/uCdu/nPm/wYGUiUWtjVa79elsm/USpA+3IKWZV30+qiktfVlICYQSlKIBp1 Nf0QgDL2v+Q3Y/r0BsxYlLi3gg+LRGa+EMPyhr62QSHChUUP6hPvQ83X4YxbyzonfIZVuSQx6Li ucRPMkgCbE5oGxMpaZdWY6TIbw+KI1Ll8p0+2iGglRS+ZIcR0O7t/ZoNY2KluHokzkTtDliz3E9 7fphDCFHohnI26Y+4Uy2bX3RWLsM1v5xul8dez4VWMb5cVzGF0He1sL2eHDqjYp2Sb0iq4iKaRd RexFYOPCJv9sJeHLzDMAG95zXWQ+VaxPVm/lkJiRHYZjA6NbNvPpsNkQwc5cjk45ccit2DUql+E MBLKHQXez2+M5q8UfM0Ce2iy15DjiJB2YwiCICMZwQ2KEShjx6ToQuzHpC7kU2S6xJUTPD60bw8 dUmzBpIBxcvdlXpw3EGDvJZrv6suOHVryzZTw== X-Received: by 2002:a05:6a21:3393:b0:3a2:bd1d:d684 with SMTP id adf61e73a8af0-3a398e05ec1mr359533637.33.1777311132902; Mon, 27 Apr 2026 10:32:12 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7977031729sm26246246a12.25.2026.04.27.10.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 10:32:12 -0700 (PDT) From: Sungho Bae To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [RFC PATCH v6 4/4] virtio-mmio: wire up noirq system sleep PM callbacks Date: Tue, 28 Apr 2026 02:30:45 +0900 Message-Id: <20260427173045.28652-5-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260427173045.28652-1-baver.bae@gmail.com> References: <20260427173045.28652-1-baver.bae@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sungho Bae Add noirq system-sleep PM support to the virtio-mmio transport. This change wires noirq freeze/restore callbacks into virtio-mmio and hooks queue reset/reactivation into the transport config ops so virtqueues can be reinitialized and reused across suspend/resume. For legacy (v1) devices, keep GUEST_PAGE_SIZE programming aligned with the noirq restore path while avoiding duplicate programming in normal restore. This enables virtio-mmio based devices to participate safely in the noirq PM phase, which is required for early-restore users. Signed-off-by: Sungho Bae --- drivers/virtio/virtio_mmio.c | 136 ++++++++++++++++++++++++----------- 1 file changed, 96 insertions(+), 40 deletions(-) diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c index 595c2274fbb5..71d13cb57c43 100644 --- a/drivers/virtio/virtio_mmio.c +++ b/drivers/virtio/virtio_mmio.c @@ -336,6 +336,77 @@ static void vm_del_vqs(struct virtio_device *vdev) free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev); } +static int vm_active_vq(struct virtio_device *vdev, struct virtqueue *vq) +{ + struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); + int q_num = virtqueue_get_vring_size(vq); + + writel(q_num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM); + if (vm_dev->version == 1) { + u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT; + + /* + * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something + * that doesn't fit in 32bit, fail the setup rather than + * pretending to be successful. + */ + if (q_pfn >> 32) { + dev_err(&vdev->dev, + "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n", + 0x1ULL << (32 + PAGE_SHIFT - 30)); + return -E2BIG; + } + + writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); + writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); + } else { + u64 addr; + + addr = virtqueue_get_desc_addr(vq); + writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW); + writel((u32)(addr >> 32), + vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); + + addr = virtqueue_get_avail_addr(vq); + writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); + writel((u32)(addr >> 32), + vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); + + addr = virtqueue_get_used_addr(vq); + writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW); + writel((u32)(addr >> 32), + vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH); + + writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); + } + + return 0; +} + +static int vm_reset_vqs(struct virtio_device *vdev) +{ + struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); + struct virtqueue *vq; + int err; + + virtio_device_for_each_vq(vdev, vq) { + /* Re-initialize vring state */ + err = virtqueue_reinit_vring(vq); + if (err < 0) + return err; + + /* Select the queue we're interested in */ + writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); + + /* Activate the queue */ + err = vm_active_vq(vdev, vq); + if (err < 0) + return err; + } + + return 0; +} + static void vm_synchronize_cbs(struct virtio_device *vdev) { struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); @@ -388,45 +459,9 @@ static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int in vq->num_max = num; /* Activate the queue */ - writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM); - if (vm_dev->version == 1) { - u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT; - - /* - * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something - * that doesn't fit in 32bit, fail the setup rather than - * pretending to be successful. - */ - if (q_pfn >> 32) { - dev_err(&vdev->dev, - "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n", - 0x1ULL << (32 + PAGE_SHIFT - 30)); - err = -E2BIG; - goto error_bad_pfn; - } - - writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); - writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); - } else { - u64 addr; - - addr = virtqueue_get_desc_addr(vq); - writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW); - writel((u32)(addr >> 32), - vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); - - addr = virtqueue_get_avail_addr(vq); - writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); - writel((u32)(addr >> 32), - vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); - - addr = virtqueue_get_used_addr(vq); - writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW); - writel((u32)(addr >> 32), - vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH); - - writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); - } + err = vm_active_vq(vdev, vq); + if (err < 0) + goto error_bad_pfn; return vq; @@ -528,11 +563,13 @@ static const struct virtio_config_ops virtio_mmio_config_ops = { .reset = vm_reset, .find_vqs = vm_find_vqs, .del_vqs = vm_del_vqs, + .reset_vqs = vm_reset_vqs, .get_features = vm_get_features, .finalize_features = vm_finalize_features, .bus_name = vm_bus_name, .get_shm_region = vm_get_shm_region, .synchronize_cbs = vm_synchronize_cbs, + .noirq_safe = true, }; #ifdef CONFIG_PM_SLEEP @@ -547,14 +584,33 @@ static int virtio_mmio_restore(struct device *dev) { struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); - if (vm_dev->version == 1) + if (vm_dev->version == 1 && !vm_dev->vdev.noirq_restore_done) writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); return virtio_device_restore(&vm_dev->vdev); } +static int virtio_mmio_freeze_noirq(struct device *dev) +{ + struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); + + return virtio_device_freeze_noirq(&vm_dev->vdev); +} + +static int virtio_mmio_restore_noirq(struct device *dev) +{ + struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); + + if (vm_dev->version == 1) + writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); + + return virtio_device_restore_noirq(&vm_dev->vdev); +} + static const struct dev_pm_ops virtio_mmio_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore) + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze_noirq, + virtio_mmio_restore_noirq) }; #endif -- 2.43.0