From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 3368548C8A5 for ; Wed, 6 May 2026 16:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084664; cv=none; b=E3a0c46Dr48DI8zIjTgZuFY8mLdsvX8vZDrQqWHWHbtHi2sn867GIKy8hPUBsb28aHFp6/0kcfixzyaHq2I/EsM8WTmh9ToOV3Mi7ZasDFOjjgTeHuGFakTyBjUernZvLdda56r3s83L+TRZ2Qbw1fpkt+CVTEfosGv1ZCfIu6c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084664; c=relaxed/simple; bh=IolL4RY97C9MSpLuk3s2ulTCNVbomPxJq0SB6QX/VVE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rAM/myxgMg+rmBM98UPfiN/zE/tGoHh/pHol/ssj+OJ/mfWZ7pdcKfoUaI6HDREguYN6RWRFHvbEX9wDwGjPgA0qJNZy7rjlYTX3zs4kDAQKujCNvB4aqcLEZP8dhvEplZAk0Q46LEEebOaqZVbV2jbBF+HY3Qcip+fKkZUNdwk= 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=m02nBy6H; arc=none smtp.client-ip=209.85.210.179 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="m02nBy6H" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-82f8b60e485so3200686b3a.0 for ; Wed, 06 May 2026 09:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778084662; x=1778689462; 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=GLZ6Q/p8ThasrMA2tuzRiOEHpqfxlyjFwiRgvxlFlJI=; b=m02nBy6H9FHuDpsj6hEjva+fvvJ+x8HKVXbiMhC4fMmjbtKYgBK655dZzsD9kwOQu4 nCqyU7LIVoTh+PsKC7LhzcWbFxqZ2aiRvalgyeJVoeJ3zsr1H2R1XeQ/1Vf47PQg1fP/ g8rBbT95ddyGBcG66k2fyVPGXAT+F5GZH3rKNP2TkKjbufG5LGD7W1q4J4DiehgRsa5Q ypi9gLtWIne4UaTknIZnlDFxfGDYnkxi2LiCXGiDYAbKt6XrkM8ofxQIVKv89cU5vBT8 /3hR/5QG8JmreoqKLsAO70J/2cCkX3eeQBc0qMdWj/rfvs8k5OpcKSCuBSL53CtB3HE7 oIEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778084662; x=1778689462; 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=GLZ6Q/p8ThasrMA2tuzRiOEHpqfxlyjFwiRgvxlFlJI=; b=fMqqBxLbPPiZnMzlcgfncaRFf8/wzr7QaG6HFSkqBQ/mvO1WQ7YeS+rny+4l4V6flq 1ZVdTNJ/WjI/N8PELjztxqpOWAc2tl94KofOzSBA+nelE0IYSLTPJmXJAAeMq68n7ENv 87THApK366Yu3X0o+qsNw9R5SZpn/lqEjGyxVwCCC4Ww7EKtM0XO4zLDSj4IRQC6OPlt QuNhzNz8AAGEH6CSCA+fS/fvIkKNyz6aol5hoW3LxhYbTpc6PT4KsmSrAs8wNS8B7VBJ SnDo1fXZlwLnhbg+zXIh5TbHJZaES2wU3sGfSRumwG5UJgpnTFw0GlBFWQ2+zc5q917e FSgg== X-Forwarded-Encrypted: i=1; AFNElJ/O5JX/SARDjhlasO7Sd2QhSVcwohP6D6pE/850D1fY2gEu1q1tfbP0c9AIX2LdrPfxbUk0RUEXnXESb2M=@vger.kernel.org X-Gm-Message-State: AOJu0YyE0m2ei3biJMaEmKKmJxW0OK1s8qhpzd6QkGtyJj5aAISNy99Q H+ndL0NrWcnMvPPAO72RQI43dE+Yy4gOiEB3yWomfPvErFYshfSaO3lg X-Gm-Gg: AeBDiet7f1SHt8NEnhUrwCJg7eqTacRvzPNM+EwCqgAteOhXqt/KAlQNKYdCquC+fk5 Y1aV3j8kDOElXwskAxXyTPQB1c8QkD2KIurhxHHWO080liQX2JqRi/3veMOpXSO9blRIjHp01YL iChpWvJw3FpOB8WJgH89C2ZtGsSeo/GXE+TwgQ9kJbuPgELU6G7KNrDdusSCWQScZ7JTr1b3gqo JD4kZrlQCDJuMliMMENFSjuRwG69PWQDQanmO5lfU8ll7EoJcMCybNz2QUZsMhcTREcgfS3Kj60 bYEc7JvPRyXaO62+EmkxTrLkzQ6KVpJPd+yDGmmJVBKdXp9x5yJ2v17CST+GQresuDQdSEX/xgz A0PH8pbpg/Dcxmaw8wtNp2Jm7bbqWxpoMCnwwHsYOvQyUEw4Ldpxco3ZEng1ZoR6kCZ2Xrlt3FR B3+p13u+txEfhLwD4uQZybb0mwHczybZ/J+BTzhcXA2bOYWD9kzcss9pUe/fqdATEcnre6ek8ss QWvQc6d8Ki9Knq+dKud9FFiENCJAsDRhVwDPueSrRsI62j3 X-Received: by 2002:a05:6a00:4655:b0:82f:38:a5b2 with SMTP id d2e1a72fcca58-83a5dd5582bmr3835165b3a.40.1778084662312; Wed, 06 May 2026 09:24:22 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8629sm5980337b3a.36.2026.05.06.09.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 09:24:21 -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 v8 4/4] virtio-mmio: wire up noirq system sleep PM callbacks Date: Thu, 7 May 2026 01:22:54 +0900 Message-Id: <20260506162254.25576-5-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260506162254.25576-1-baver.bae@gmail.com> References: <20260506162254.25576-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 | 137 +++++++++++++++++++++++++---------- 1 file changed, 97 insertions(+), 40 deletions(-) diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c index 595c2274fbb5..4f4836103b88 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 @@ -546,15 +583,35 @@ static int virtio_mmio_freeze(struct device *dev) static int virtio_mmio_restore(struct device *dev) { struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev); + struct virtio_driver *drv = drv_to_virtio(vm_dev->vdev.dev.driver); - if (vm_dev->version == 1) + if (vm_dev->version == 1 && (!drv || !drv->restore_noirq)) 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