From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web12.36953.1629192223280318773 for ; Tue, 17 Aug 2021 02:23:43 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hAUCfeXq; spf=pass (domain: redhat.com, ip: 170.10.133.124, mailfrom: kraxel@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1629192222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8Vx77UMi0tusTRcaHfTqx0ybMVK2MGsUMxtoQ/NdT64=; b=hAUCfeXqHYQ2MtvWYO+f2FhUWhEPquJtJy+7t2OqmlbWDR6yZ0vuEuCayvKEGh9hh4toae JYhElvj3pDdlGjh+QameAwrhU4+6UyEFwMcch2VqmLMHdsQBWPc7jiCIIFmypz1ntoC4gA MhrEM2mwOwsnBqJEuMHSKOf7Hzh2jco= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-292-s-nl_YzYNkuzHSZZpH3qkQ-1; Tue, 17 Aug 2021 05:23:41 -0400 X-MC-Unique: s-nl_YzYNkuzHSZZpH3qkQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9337D87D551 for ; Tue, 17 Aug 2021 09:23:40 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.193.216]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 62E3960FB8; Tue, 17 Aug 2021 09:23:36 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 623F51800905; Tue, 17 Aug 2021 11:23:29 +0200 (CEST) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Gerd Hoffmann , Philippe Mathieu-Daude Subject: [PATCH v3 2/4] OvmfPkg/VirtioMmioDeviceLib: Add virtio 1.0 support. Date: Tue, 17 Aug 2021 11:23:27 +0200 Message-Id: <20210817092329.808150-3-kraxel@redhat.com> In-Reply-To: <20210817092329.808150-1-kraxel@redhat.com> References: <20210817092329.808150-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kraxel@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" Add support for virtio 1.0 to the mmio transport. virtio 0.9.5 uses page size, page frame number and a fixed layout for the ring. virtio 1.0 uses the physical addresses for base address, used bits and available bits instead. The ring layout is not changed, so a 0.9.5 compatible layout is used in both 0.9.5 and 1.0 mode to to keep the code differences as small as possible. Signed-off-by: Gerd Hoffmann Reviewed-by: Philippe Mathieu-Daude --- .../VirtioMmioDeviceLib/VirtioMmioDevice.h | 4 +++ .../VirtioMmioDeviceLib/VirtioMmioDevice.c | 17 +++++++--- .../VirtioMmioDeviceFunctions.c | 31 +++++++++++++++++-- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.h b/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.h index ab53b90d51c9..0c2f99633c46 100644 --- a/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.h +++ b/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.h @@ -23,9 +23,13 @@ #include #define VIRTIO_MMIO_DEVICE_SIGNATURE SIGNATURE_32 ('V', 'M', 'I', 'O') +#define VIRTIO_MMIO_DEVICE_VERSION_0_95 1 +#define VIRTIO_MMIO_DEVICE_VERSION_1_00 2 + typedef struct { UINT32 Signature; + UINT32 Version; VIRTIO_DEVICE_PROTOCOL VirtioDevice; PHYSICAL_ADDRESS BaseAddress; } VIRTIO_MMIO_DEVICE; diff --git a/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.c b/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.c index 6dbbba008c75..a97ef9352d0f 100644 --- a/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.c +++ b/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.c @@ -58,7 +58,6 @@ VirtioMmioInit ( ) { UINT32 MagicValue; - UINT32 Version; // // Initialize VirtIo Mmio Device @@ -66,7 +65,6 @@ VirtioMmioInit ( CopyMem (&Device->VirtioDevice, &mMmioDeviceProtocolTemplate, sizeof (VIRTIO_DEVICE_PROTOCOL)); Device->BaseAddress = BaseAddress; - Device->VirtioDevice.Revision = VIRTIO_SPEC_REVISION (0, 9, 5); Device->VirtioDevice.SubSystemDeviceId = MmioRead32 (BaseAddress + VIRTIO_MMIO_OFFSET_DEVICE_ID); @@ -78,8 +76,19 @@ VirtioMmioInit ( return EFI_UNSUPPORTED; } - Version = VIRTIO_CFG_READ (Device, VIRTIO_MMIO_OFFSET_VERSION); - if (Version != 1) { + Device->Version = VIRTIO_CFG_READ (Device, VIRTIO_MMIO_OFFSET_VERSION); + switch (Device->Version) { + case VIRTIO_MMIO_DEVICE_VERSION_0_95: + DEBUG ((DEBUG_INFO, "%a virtio 0.9.5, id %d\n", __FUNCTION__, + Device->VirtioDevice.SubSystemDeviceId)); + Device->VirtioDevice.Revision = VIRTIO_SPEC_REVISION (0, 9, 5); + break; + case VIRTIO_MMIO_DEVICE_VERSION_1_00: + DEBUG ((DEBUG_INFO, "%a virtio 1.0, id %d\n", __FUNCTION__, + Device->VirtioDevice.SubSystemDeviceId)); + Device->VirtioDevice.Revision = VIRTIO_SPEC_REVISION (1, 0, 0); + break; + default: return EFI_UNSUPPORTED; } diff --git a/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDeviceFunctions.c b/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDeviceFunctions.c index b0d75fb1dd24..a0ee8e5f3c86 100644 --- a/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDeviceFunctions.c +++ b/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDeviceFunctions.c @@ -151,7 +151,9 @@ VirtioMmioSetPageSize ( Device = VIRTIO_MMIO_DEVICE_FROM_VIRTIO_DEVICE (This); - VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_GUEST_PAGE_SIZE, PageSize); + if (Device->Version == VIRTIO_MMIO_DEVICE_VERSION_0_95) { + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_GUEST_PAGE_SIZE, PageSize); + } return EFI_SUCCESS; } @@ -181,13 +183,36 @@ VirtioMmioSetQueueAddress ( ) { VIRTIO_MMIO_DEVICE *Device; + UINT64 Address; ASSERT (RingBaseShift == 0); Device = VIRTIO_MMIO_DEVICE_FROM_VIRTIO_DEVICE (This); - VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_PFN, - (UINT32)((UINTN)Ring->Base >> EFI_PAGE_SHIFT)); + if (Device->Version == VIRTIO_MMIO_DEVICE_VERSION_0_95) { + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_PFN, + (UINT32)((UINTN)Ring->Base >> EFI_PAGE_SHIFT)); + } else { + Address = (UINT64)Ring->Base; + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_DESC_LO, + (UINT32)Address); + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_DESC_HI, + (UINT32)RShiftU64(Address, 32)); + + Address = (UINT64)Ring->Avail.Flags; + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_AVAIL_LO, + (UINT32)Address); + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_AVAIL_HI, + (UINT32)RShiftU64(Address, 32)); + + Address = (UINT64)Ring->Used.Flags; + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_USED_LO, + (UINT32)Address); + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_USED_HI, + (UINT32)RShiftU64(Address, 32)); + + VIRTIO_CFG_WRITE (Device, VIRTIO_MMIO_OFFSET_QUEUE_READY, 1); + } return EFI_SUCCESS; } -- 2.31.1