Skip to contentSkip to conversation

x86: Change tesing logic of mtrr commit

PATCH In review Bin Meng 3 participants 12 messages Jul 31, 2023 – Aug 1, 2023

Subsystems (2)
lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v2 1/6 x86: Change tesing logic of mtrr commit

From: Bin Meng <[email protected]>

On Coral U-Boot SPL programs some MTRRs and FSPv2 in U-Boot proper needs to program MTRRs too. With current testing logic of mtrr commit in init_cache_f_r(), the mtrr commit is skipped which won't work as the queued mtrr requests include setup for DRAM regions.

Change the logic to allow such configuration.

---

Changes in v2:
- new patch: "x86: Change tesing logic of mtrr commit"

 arch/x86/lib/init_helpers.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
arch/x86/lib/init_helpers.c +3 -5
index f33194045f..a2535e4468 100644
@@ -14,15 +14,13 @@ DECLARE_GLOBAL_DATA_PTR;
 
 int init_cache_f_r(void)
 {
-	bool do_mtrr = CONFIG_IS_ENABLED(X86_32BIT_INIT) ||
-		 IS_ENABLED(CONFIG_FSP_VERSION2);
+	bool do_mtrr = CONFIG_IS_ENABLED(X86_32BIT_INIT);
 	int ret;
 
 	/*
 	 * Supported configurations:
 	 *
-	 * booting from slimbootloader - in that case the MTRRs are already set
-	 *	up
+	 * booting from slimbootloader - MTRRs are already set up
 	 * booting with FSPv1 - MTRRs are already set up
 	 * booting with FSPv2 - MTRRs must be set here
 	 * booting from coreboot - in this case there is no SPL, so we set up
@@ -30,7 +28,7 @@ int init_cache_f_r(void)
 	 * Note: if there is an SPL, then it has already set up MTRRs so we
 	 *	don't need to do that here
 	 */
-	do_mtrr &= !IS_ENABLED(CONFIG_SPL) &&
+	do_mtrr &= (!IS_ENABLED(CONFIG_SPL) || IS_ENABLED(CONFIG_FSP_VERSION2)) &&
 		!IS_ENABLED(CONFIG_FSP_VERSION1) &&
 		!IS_ENABLED(CONFIG_SYS_SLIMBOOTLOADER);
 
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 2/6 x86: fsp: Use mtrr_set_next_var() for graphics memory

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

---

(no changes since v1)

 arch/x86/lib/fsp/fsp_graphics.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/lib/fsp/fsp_graphics.c +1 -2
index 2bcc49f605..09d5da8c84 100644
@@ -110,8 +110,7 @@ static int fsp_video_probe(struct udevice *dev)
 	if (ret)
 		goto err;
 
-	mtrr_add_request(MTRR_TYPE_WRCOMB, vesa->phys_base_ptr, 256 << 20);
-	mtrr_commit(true);
+	mtrr_set_next_var(MTRR_TYPE_WRCOMB, vesa->phys_base_ptr, 256 << 20);
 
 	printf("%dx%dx%d @ %x\n", uc_priv->xsize, uc_priv->ysize,
 	       vesa->bits_per_pixel, vesa->phys_base_ptr);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 2/6] x86: fsp: Use mtrr_set_next_var() for graphics memory

Hi Simon,

Quoted 33 lines · Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

---

(no changes since v1)

 arch/x86/lib/fsp/fsp_graphics.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/lib/fsp/fsp_graphics.c +0 -0
index 2bcc49f605..09d5da8c84 100644

arch/x86/lib/fsp/fsp_graphics.c static int fsp_video_probe(struct udevice *dev)

Quoted 10 lines
arch/x86/lib/fsp/fsp_graphics.c +1 -3
@@ -110,8 +110,7 @@ static int fsp_video_probe(struct udevice *dev)
        if (ret)
                goto err;
 
-       mtrr_add_request(MTRR_TYPE_WRCOMB, vesa->phys_base_ptr, 256 << 20);
-       mtrr_commit(true);
+       mtrr_set_next_var(MTRR_TYPE_WRCOMB, vesa->phys_base_ptr, 256 << 20);
 
        printf("%dx%dx%d @ %x\n", uc_priv->xsize, uc_priv->ysize,
               vesa->bits_per_pixel, vesa->phys_base_ptr);
--

Would you give a Rb/Tb tag, if you will?

Regards,
Bin

Reviewed-by on v2 2/6
#3 lore raw

Re: [PATCH v2 2/6] x86: fsp: Use mtrr_set_next_var() for graphics memory

In reply to Bin Meng
Quoted 20 lines · Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

Quoted 7 lines

---

(no changes since v1)

 arch/x86/lib/fsp/fsp_graphics.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
author
#4 lore raw

PATCH v2 3/6 video: broadwell: Use mtrr_set_next_var() for graphics memory

In reply to Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

---

(no changes since v1)

 drivers/video/broadwell_igd.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/video/broadwell_igd.c +3 -7
index 6aa4e27071..83b6c908a8 100644
@@ -693,13 +693,9 @@ static int broadwell_igd_probe(struct udevice *dev)
 
 	/* Use write-combining for the graphics memory, 256MB */
 	fbbase = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base;
-	ret = mtrr_add_request(MTRR_TYPE_WRCOMB, fbbase, 256 << 20);
-	if (!ret)
-		ret = mtrr_commit(true);
-	if (ret && ret != -ENOSYS) {
-		printf("Failed to add MTRR: Display will be slow (err %d)\n",
-		       ret);
-	}
+	ret = mtrr_set_next_var(MTRR_TYPE_WRCOMB, fbbase, 256 << 20);
+	if (ret)
+		printf("Failed to add MTRR: Display will be slow (err %d)\n", ret);
 
 	debug("fb=%lx, size %x, display size=%d %d %d\n", plat->base,
 	      plat->size, uc_priv->xsize, uc_priv->ysize, uc_priv->bpix);
Signature · 1 line
2.25.1
Reviewed-by on v2 3/6
#5 lore raw

Re: [PATCH v2 3/6] video: broadwell: Use mtrr_set_next_var() for graphics memory

Quoted 20 lines · Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

Quoted 7 lines

---

(no changes since v1)

 drivers/video/broadwell_igd.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
author
#6 lore raw

PATCH v2 4/6 video: ivybridge: Use mtrr_set_next_var() for graphics memory

In reply to Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

---

(no changes since v1)

 drivers/video/ivybridge_igd.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/video/ivybridge_igd.c +1 -2
index 9264dd6770..c2cc976618 100644
@@ -774,8 +774,7 @@ static int bd82x6x_video_probe(struct udevice *dev)
 
 	/* Use write-combining for the graphics memory, 256MB */
 	fbbase = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base;
-	mtrr_add_request(MTRR_TYPE_WRCOMB, fbbase, 256 << 20);
-	mtrr_commit(true);
+	mtrr_set_next_var(MTRR_TYPE_WRCOMB, fbbase, 256 << 20);
 
 	return 0;
 }
Signature · 1 line
2.25.1
Reviewed-by on v2 4/6
#7 lore raw

Re: [PATCH v2 4/6] video: ivybridge: Use mtrr_set_next_var() for graphics memory

Quoted 20 lines · Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

Quoted 7 lines

---

(no changes since v1)

 drivers/video/ivybridge_igd.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
author
#8 lore raw

PATCH v2 5/6 video: vesa: Use mtrr_set_next_var() for graphics memory

In reply to Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

---

(no changes since v1)

 drivers/video/vesa.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/video/vesa.c +1 -2
index cac3bb0c33..50912c5c8b 100644
@@ -23,8 +23,7 @@ static int vesa_video_probe(struct udevice *dev)
 
 	/* Use write-combining for the graphics memory, 256MB */
 	fbbase = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base;
-	mtrr_add_request(MTRR_TYPE_WRCOMB, fbbase, 256 << 20);
-	mtrr_commit(true);
+	mtrr_set_next_var(MTRR_TYPE_WRCOMB, fbbase, 256 << 20);
 
 	return 0;
 }
Signature · 1 line
2.25.1
Reviewed-by on v2 5/6
#9 lore raw

Re: [PATCH v2 5/6] video: vesa: Use mtrr_set_next_var() for graphics memory

Quoted 20 lines · Bin Meng

From: Bin Meng <[email protected]>

At present this uses mtrr_add_request() & mtrr_commit() combination to program the MTRR for graphics memory. This usage has two major issues as below:

- mtrr_commit() will re-initialize all MTRR registers from index 0, using the settings previously added by mtrr_add_request() and saved in gd->arch.mtrr_req[], which won't cause any issue but is unnecessary
- The way such combination works is based on the assumption that U-Boot has full control with MTRR programming (e.g.: U-Boot without any blob that does all low-level initialization on its own, or using FSP2 which does not touch MTRR), but this is not the case with FSP. FSP programs some MTRRs during its execution but U-Boot does not have the settings saved in gd->arch.mtrr_req[] and when doing mtrr_commit() it will corrupt what was already programmed previously.

Correct this to use mtrr_set_next_var() instead.

Quoted 7 lines

---

(no changes since v1)

 drivers/video/vesa.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
author
#10 lore raw

PATCH v2 6/6 x86: Return mtrr_add_request() to its old purpose

In reply to Bin Meng

From: Simon Glass <[email protected]>

This function used to be for adding a list of requests to be actioned on relocation. Revert it back to this purpose, to avoid problems with boards which need control of their MTRRs (i.e. those which don't use FSP).

The mtrr_set_next_var() function is available when the next free variable-MTRR must be set, so this can be used instead.

Fixes 3bcd6cf89ef ("x86: mtrr: Skip MSRs that were already programmed..")
Fixes 596bd0589ad ("x86: mtrr: Do not clear the unused ones..")

---

(no changes since v1)

 arch/x86/cpu/mtrr.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/cpu/mtrr.c +5 -1
index d57fcface0..9c24ae984e 100644
@@ -166,8 +166,12 @@ int mtrr_commit(bool do_caches)
 	debug("open done\n");
 	qsort(req, gd->arch.mtrr_req_count, sizeof(*req), h_comp_mtrr);
 	for (i = 0; i < gd->arch.mtrr_req_count; i++, req++)
-		mtrr_set_next_var(req->type, req->start, req->size);
+		set_var_mtrr(i, req->type, req->start, req->size);
 
+	/* Clear the ones that are unused */
+	debug("clear\n");
+	for (; i < mtrr_get_var_count(); i++)
+		wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
 	debug("close\n");
 	mtrr_close(&state, do_caches);
 	debug("mtrr done\n");
Signature · 1 line
2.25.1
#11 lore raw
In reply to Bin Meng
Quoted 12 lines · Bin Meng

From: Bin Meng <[email protected]>

On Coral U-Boot SPL programs some MTRRs and FSPv2 in U-Boot proper needs to program MTRRs too. With current testing logic of mtrr commit in init_cache_f_r(), the mtrr commit is skipped which won't work as the queued mtrr requests include setup for DRAM regions.

Change the logic to allow such configuration.

Quoted 8 lines

---

Changes in v2:
- new patch: "x86: Change tesing logic of mtrr commit"

 arch/x86/lib/init_helpers.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

This patch is superceded by
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

The rest of the patches are applied to u-boot-x86, thanks!

Regards,
Bin