From: "Corvin Köhne" <C.Koehne@beckhoff.com>
To: "tomitamoeko@gmail.com" <tomitamoeko@gmail.com>,
"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Cc: "clg@redhat.com" <clg@redhat.com>,
"alex.williamson@redhat.com" <alex.williamson@redhat.com>
Subject: Re: [PATCH v2 3/9] vfio/igd: canonicalize memory size calculations
Date: Tue, 3 Dec 2024 16:12:44 +0000 [thread overview]
Message-ID: <61ee6c4f27b62891a9d045b07821d03a8659f35e.camel@beckhoff.com> (raw)
In-Reply-To: <20241203133548.38252-4-tomitamoeko@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 6906 bytes --]
On Tue, 2024-12-03 at 21:35 +0800, Tomita Moeko wrote:
> CAUTION: External Email!!
> Add helper functions igd_gtt_memory_size() and igd_stolen_size() for
> calculating GTT stolen memory and Data stolen memory size in bytes,
> and use macros to replace the hardware-related magic numbers for
> better readability.
>
> Signed-off-by: Tomita Moeko <tomitamoeko@gmail.com>
> ---
> hw/vfio/igd.c | 99 ++++++++++++++++++++++++++++-----------------------
> 1 file changed, 55 insertions(+), 44 deletions(-)
>
> diff --git a/hw/vfio/igd.c b/hw/vfio/igd.c
> index 2ede72d243..b5bfdc6580 100644
> --- a/hw/vfio/igd.c
> +++ b/hw/vfio/igd.c
> @@ -106,6 +106,51 @@ typedef struct VFIOIGDQuirk {
> #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */
> #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and later
> */
>
> +#define IGD_GMCH_GEN6_GMS_SHIFT 3 /* SNB_GMCH in i915 */
> +#define IGD_GMCH_GEN6_GMS_MASK 0x1f
> +#define IGD_GMCH_GEN6_GGMS_SHIFT 8
> +#define IGD_GMCH_GEN6_GGMS_MASK 0x3
> +#define IGD_GMCH_GEN8_GMS_SHIFT 8 /* BDW_GMCH in i915 */
> +#define IGD_GMCH_GEN8_GMS_MASK 0xff
> +#define IGD_GMCH_GEN8_GGMS_SHIFT 6
> +#define IGD_GMCH_GEN8_GGMS_MASK 0x3
> +
> +static uint64_t igd_gtt_memory_size(int gen, uint16_t gmch)
> +{
> + uint64_t ggms;
> +
> + if (gen < 8) {
> + ggms = (gmch >> IGD_GMCH_GEN6_GGMS_SHIFT) & IGD_GMCH_GEN6_GGMS_MASK;
> + } else {
> + ggms = (gmch >> IGD_GMCH_GEN8_GGMS_SHIFT) & IGD_GMCH_GEN8_GGMS_MASK;
> + ggms *= 2;
> + }
> +
> + return ggms * MiB;
> +}
> +
> +static uint64_t igd_stolen_memory_size(int gen, uint32_t gmch)
> +{
> + uint64_t gms;
> +
> + if (gen < 8) {
> + gms = (gmch >> IGD_GMCH_GEN6_GMS_SHIFT) & IGD_GMCH_GEN6_GMS_MASK;
> + } else {
> + gms = (gmch >> IGD_GMCH_GEN8_GMS_SHIFT) & IGD_GMCH_GEN8_GMS_MASK;
> + }
> +
> + if (gen < 9) {
> + return gms * 32 * MiB;
> + } else {
> + if (gms < 0xf0) {
> + return gms * 32 * MiB;
> + } else {
> + return (gms - 0xf0 + 1) * 4 * MiB;
> + }
> + }
> +
> + return 0;
> +}
>
> /*
> * The rather short list of registers that we copy from the host devices.
> @@ -254,17 +299,10 @@ static int vfio_pci_igd_lpc_init(VFIOPCIDevice *vdev,
> static int vfio_igd_gtt_max(VFIOPCIDevice *vdev)
> {
> uint32_t gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH,
> sizeof(gmch));
> - int ggms, gen = igd_gen(vdev);
> -
> - gmch = vfio_pci_read_config(&vdev->pdev, IGD_GMCH, sizeof(gmch));
> - ggms = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
> - if (gen >= 8) {
> - ggms = 1 << ggms;
> - }
> -
> - ggms *= MiB;
> + int gen = igd_gen(vdev);
> + uint64_t ggms_size = igd_gtt_memory_size(gen, gmch);
>
> - return (ggms / (4 * KiB)) * (gen < 8 ? 4 : 8);
> + return (ggms_size / (4 * KiB)) * (gen < 8 ? 4 : 8);
> }
>
> /*
> @@ -471,30 +509,6 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int
> nr)
> QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
> }
>
> -static int igd_get_stolen_mb(int gen, uint32_t gmch)
> -{
> - int gms;
> -
> - if (gen < 8) {
> - gms = (gmch >> 3) & 0x1f;
> - } else {
> - gms = (gmch >> 8) & 0xff;
> - }
> -
> - if (gen < 9) {
> - if (gms > 0x10) {
> - error_report("Unsupported IGD GMS value 0x%x", gms);
> - return 0;
> - }
> - return gms * 32;
> - } else {
> - if (gms < 0xf0)
> - return gms * 32;
> - else
> - return (gms - 0xf0) * 4 + 4;
> - }
> -}
> -
> void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr)
> {
> g_autofree struct vfio_region_info *rom = NULL;
> @@ -504,7 +518,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int
> nr)
> VFIOQuirk *quirk;
> VFIOIGDQuirk *igd;
> PCIDevice *lpc_bridge;
> - int i, ret, ggms_mb, gms_mb = 0, gen;
> + int i, ret, gen;
> + uint64_t ggms_size, gms_size;
> uint64_t *bdsm_size;
> uint32_t gmch;
> uint16_t cmd_orig, cmd;
> @@ -666,13 +681,8 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int
> nr)
>
> QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, quirk, next);
>
> - /* Determine the size of stolen memory needed for GTT */
> - ggms_mb = (gmch >> (gen < 8 ? 8 : 6)) & 0x3;
> - if (gen >= 8) {
> - ggms_mb = 1 << ggms_mb;
> - }
> -
> - gms_mb = igd_get_stolen_mb(gen, gmch);
> + ggms_size = igd_gtt_memory_size(gen, gmch);
> + gms_size = igd_stolen_memory_size(gen, gmch);
>
> /*
> * Request reserved memory for stolen memory via fw_cfg. VM firmware
> @@ -683,7 +693,7 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int
> nr)
> * config offset 0x5C.
> */
> bdsm_size = g_malloc(sizeof(*bdsm_size));
> - *bdsm_size = cpu_to_le64((ggms_mb + gms_mb) * MiB);
> + *bdsm_size = cpu_to_le64(ggms_size + gms_size);
> fw_cfg_add_file(fw_cfg_find(), "etc/igd-bdsm-size",
> bdsm_size, sizeof(*bdsm_size));
>
> @@ -734,5 +744,6 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int
> nr)
> vdev-
> >https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAADIUcZasZ36mwAAABM0
> 01Yig6LNmON7mS202pDuIRRNT-
> tIucgsQHKYe8WitBfoANcxBM2L6i4Sg4fLLMkXL_LDVUSEswEqsunuGsAr4DjgOogXtNMivhsyeaj9
> xYl9AgydV4QqrKMV29P7y3uAuqQcYz1GacVJRg1 );
> }
>
> - trace_vfio_pci_igd_bdsm_enabled(vdev-
> >https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAADIUcZasZ36mwAAABM0
> 01Yig6LNmON7mS202pDuIRRNT-
> tIucgsQHKYe8WitBfoANcxBM2L6i4Sg4fLLMkXL_LDVUSEswEqsunuGsAr4DjgOogXtNMivhsyeaj9
> xYl9AgydV4QqrKMV29P7y3uAuqQcYz1GacVJRg1 , ggms_mb + gms_mb);
> + trace_vfio_pci_igd_bdsm_enabled(vdev-
> >https://nospamproxywebp.beckhoff.com/enQsig/link?id=BAgAAAADIUcZasZ36mwAAABM0
> 01Yig6LNmON7mS202pDuIRRNT-
> tIucgsQHKYe8WitBfoANcxBM2L6i4Sg4fLLMkXL_LDVUSEswEqsunuGsAr4DjgOogXtNMivhsyeaj9
> xYl9AgydV4QqrKMV29P7y3uAuqQcYz1GacVJRg1 ,
> + (ggms_size + gms_size) / MiB);
> }
Reviewed-by: Corvin Köhne <c.koehne@beckhoff.com>
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-12-03 16:13 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-03 13:35 [PATCH v2 0/9] vfio/igd: Enable legacy mode on more devices Tomita Moeko
2024-12-03 13:35 ` [PATCH v2 1/9] vfio/igd: remove unsupported device ids Tomita Moeko
2024-12-03 13:35 ` [PATCH v2 2/9] vfio/igd: align generation with i915 kernel driver Tomita Moeko
2024-12-03 16:07 ` Corvin Köhne
2024-12-04 22:36 ` Alex Williamson
2024-12-05 9:26 ` Tomita Moeko
2024-12-03 13:35 ` [PATCH v2 3/9] vfio/igd: canonicalize memory size calculations Tomita Moeko
2024-12-03 16:12 ` Corvin Köhne [this message]
2024-12-04 22:35 ` Alex Williamson
2024-12-05 10:13 ` Tomita Moeko
2024-12-03 13:35 ` [PATCH v2 4/9] vfio/igd: add Gemini Lake and Comet Lake device ids Tomita Moeko
2024-12-03 16:15 ` Corvin Köhne
2024-12-03 13:35 ` [PATCH v2 5/9] vfio/igd: add Alder/Raptor/Rocket/Ice/Jasper " Tomita Moeko
2024-12-03 16:18 ` Corvin Köhne
2024-12-03 13:35 ` [PATCH v2 6/9] vfio/igd: add macro for declaring mirrored registers Tomita Moeko
2024-12-03 16:22 ` Corvin Köhne
2024-12-04 22:35 ` Alex Williamson
2024-12-03 13:35 ` [PATCH v2 7/9] vfio/igd: emulate GGC register in mmio bar0 Tomita Moeko
2024-12-04 22:35 ` Alex Williamson
2024-12-03 13:35 ` [PATCH v2 8/9] vfio/igd: emulate BDSM in mmio bar0 for gen 6-10 devices Tomita Moeko
2024-12-03 16:24 ` Corvin Köhne
2024-12-03 13:35 ` [PATCH v2 9/9] vfio/igd: add x-igd-gms option back to set DSM region size for guest Tomita Moeko
2024-12-03 16:30 ` Corvin Köhne
2024-12-04 22:35 ` Alex Williamson
2024-12-03 20:12 ` [PATCH v2 0/9] vfio/igd: Enable legacy mode on more devices Alex Williamson
2024-12-04 15:08 ` Tomita Moeko
2024-12-29 16:43 ` Tomita Moeko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=61ee6c4f27b62891a9d045b07821d03a8659f35e.camel@beckhoff.com \
--to=c.koehne@beckhoff.com \
--cc=alex.williamson@redhat.com \
--cc=clg@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=tomitamoeko@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).