diff mbox series

[U-Boot,062/126] x86: Add a function to set variable MTRRs

Message ID 20190925145750.200592-63-sjg@chromium.org
State Accepted
Delegated to: Bin Meng
Headers show
Series x86: Add initial support for apollolake | expand

Commit Message

Simon Glass Sept. 25, 2019, 2:56 p.m. UTC
Normally U-Boot handles MTRRs through an add/commit process which
overwrites all MTRRs. But in very early boot it is not desirable to clear
the existing MTRRs since they may be in use and it can cause a hang.

Add a new mtrr_set_next_var() function which sets up the next available
MTRR to the required region.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/x86/cpu/mtrr.c         | 38 +++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/mtrr.h | 12 ++++++++++++
 2 files changed, 50 insertions(+)

Comments

Bin Meng Oct. 7, 2019, 1:53 p.m. UTC | #1
On Wed, Sep 25, 2019 at 10:58 PM Simon Glass <sjg@chromium.org> wrote:
>
> Normally U-Boot handles MTRRs through an add/commit process which
> overwrites all MTRRs. But in very early boot it is not desirable to clear
> the existing MTRRs since they may be in use and it can cause a hang.
>
> Add a new mtrr_set_next_var() function which sets up the next available
> MTRR to the required region.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  arch/x86/cpu/mtrr.c         | 38 +++++++++++++++++++++++++++++++++++++
>  arch/x86/include/asm/mtrr.h | 12 ++++++++++++
>  2 files changed, 50 insertions(+)
>
> diff --git a/arch/x86/cpu/mtrr.c b/arch/x86/cpu/mtrr.c
> index 6218d149e30..1a7c292fa8b 100644
> --- a/arch/x86/cpu/mtrr.c
> +++ b/arch/x86/cpu/mtrr.c
> @@ -112,3 +112,41 @@ int mtrr_add_request(int type, uint64_t start, uint64_t size)
>
>         return 0;
>  }
> +
> +static int get_var_mtrr_count(void)
> +{
> +       return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
> +}
> +
> +static int get_free_var_mtrr(void)
> +{
> +       struct msr_t maskm;
> +       int vcnt;
> +       int i;
> +
> +       vcnt = get_var_mtrr_count();
> +
> +       /* Identify the first var mtrr which is not valid */
> +       for (i = 0; i < vcnt; i++) {
> +               maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
> +               if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
> +                       return i;
> +       }
> +
> +       /* No free var mtrr */
> +       return -ENOSPC;
> +}
> +
> +int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
> +{
> +       int mtrr;
> +
> +       mtrr = get_free_var_mtrr();
> +       if (mtrr < 0)
> +               return mtrr;
> +
> +       set_var_mtrr(mtrr, MTRR_TYPE_WRPROT, start, size);

I think MTRR_TYPE_WRPROT should be parameter 'type'

> +       debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
> +
> +       return 0;
> +}
> diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h
> index 6f29e75ce65..672617256e9 100644
> --- a/arch/x86/include/asm/mtrr.h
> +++ b/arch/x86/include/asm/mtrr.h
> @@ -117,6 +117,18 @@ int mtrr_add_request(int type, uint64_t start, uint64_t size);
>   */
>  int mtrr_commit(bool do_caches);
>
> +/**
> + * mtrr_set_next_var() - set up a variable MTRR
> + *
> + * This finds the first free variable MTRR and sets to the given area
> + *
> + * @type:      Requested type (MTRR_TYPE_)
> + * @start:     Start address
> + * @size:      Size
> + * @return 0 on success, -ENOSPC if there are no more MTRRs
> + */
> +int mtrr_set_next_var(uint type, uint64_t base, uint64_t size);
> +
>  #endif
>
>  #if ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE - 1)) != 0)
> --

Other than above,

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Bin Meng Oct. 7, 2019, 2:09 p.m. UTC | #2
On Mon, Oct 7, 2019 at 9:53 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> On Wed, Sep 25, 2019 at 10:58 PM Simon Glass <sjg@chromium.org> wrote:
> >
> > Normally U-Boot handles MTRRs through an add/commit process which
> > overwrites all MTRRs. But in very early boot it is not desirable to clear
> > the existing MTRRs since they may be in use and it can cause a hang.
> >
> > Add a new mtrr_set_next_var() function which sets up the next available
> > MTRR to the required region.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > ---
> >
> >  arch/x86/cpu/mtrr.c         | 38 +++++++++++++++++++++++++++++++++++++
> >  arch/x86/include/asm/mtrr.h | 12 ++++++++++++
> >  2 files changed, 50 insertions(+)
> >
> > diff --git a/arch/x86/cpu/mtrr.c b/arch/x86/cpu/mtrr.c
> > index 6218d149e30..1a7c292fa8b 100644
> > --- a/arch/x86/cpu/mtrr.c
> > +++ b/arch/x86/cpu/mtrr.c
> > @@ -112,3 +112,41 @@ int mtrr_add_request(int type, uint64_t start, uint64_t size)
> >
> >         return 0;
> >  }
> > +
> > +static int get_var_mtrr_count(void)
> > +{
> > +       return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
> > +}
> > +
> > +static int get_free_var_mtrr(void)
> > +{
> > +       struct msr_t maskm;
> > +       int vcnt;
> > +       int i;
> > +
> > +       vcnt = get_var_mtrr_count();
> > +
> > +       /* Identify the first var mtrr which is not valid */
> > +       for (i = 0; i < vcnt; i++) {
> > +               maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
> > +               if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
> > +                       return i;
> > +       }
> > +
> > +       /* No free var mtrr */
> > +       return -ENOSPC;
> > +}
> > +
> > +int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
> > +{
> > +       int mtrr;
> > +
> > +       mtrr = get_free_var_mtrr();
> > +       if (mtrr < 0)
> > +               return mtrr;
> > +
> > +       set_var_mtrr(mtrr, MTRR_TYPE_WRPROT, start, size);
>
> I think MTRR_TYPE_WRPROT should be parameter 'type'

Fixed this, and

>
> > +       debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
> > +
> > +       return 0;
> > +}
> > diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h
> > index 6f29e75ce65..672617256e9 100644
> > --- a/arch/x86/include/asm/mtrr.h
> > +++ b/arch/x86/include/asm/mtrr.h
> > @@ -117,6 +117,18 @@ int mtrr_add_request(int type, uint64_t start, uint64_t size);
> >   */
> >  int mtrr_commit(bool do_caches);
> >
> > +/**
> > + * mtrr_set_next_var() - set up a variable MTRR
> > + *
> > + * This finds the first free variable MTRR and sets to the given area
> > + *
> > + * @type:      Requested type (MTRR_TYPE_)
> > + * @start:     Start address
> > + * @size:      Size
> > + * @return 0 on success, -ENOSPC if there are no more MTRRs
> > + */
> > +int mtrr_set_next_var(uint type, uint64_t base, uint64_t size);
> > +
> >  #endif
> >
> >  #if ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE - 1)) != 0)
> > --
>
> Other than above,
>
> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

applied to u-boot-x86/next, thanks!
diff mbox series

Patch

diff --git a/arch/x86/cpu/mtrr.c b/arch/x86/cpu/mtrr.c
index 6218d149e30..1a7c292fa8b 100644
--- a/arch/x86/cpu/mtrr.c
+++ b/arch/x86/cpu/mtrr.c
@@ -112,3 +112,41 @@  int mtrr_add_request(int type, uint64_t start, uint64_t size)
 
 	return 0;
 }
+
+static int get_var_mtrr_count(void)
+{
+	return msr_read(MSR_MTRR_CAP_MSR).lo & MSR_MTRR_CAP_VCNT;
+}
+
+static int get_free_var_mtrr(void)
+{
+	struct msr_t maskm;
+	int vcnt;
+	int i;
+
+	vcnt = get_var_mtrr_count();
+
+	/* Identify the first var mtrr which is not valid */
+	for (i = 0; i < vcnt; i++) {
+		maskm = msr_read(MTRR_PHYS_MASK_MSR(i));
+		if ((maskm.lo & MTRR_PHYS_MASK_VALID) == 0)
+			return i;
+	}
+
+	/* No free var mtrr */
+	return -ENOSPC;
+}
+
+int mtrr_set_next_var(uint type, uint64_t start, uint64_t size)
+{
+	int mtrr;
+
+	mtrr = get_free_var_mtrr();
+	if (mtrr < 0)
+		return mtrr;
+
+	set_var_mtrr(mtrr, MTRR_TYPE_WRPROT, start, size);
+	debug("MTRR %x: start=%x, size=%x\n", mtrr, (uint)start, (uint)size);
+
+	return 0;
+}
diff --git a/arch/x86/include/asm/mtrr.h b/arch/x86/include/asm/mtrr.h
index 6f29e75ce65..672617256e9 100644
--- a/arch/x86/include/asm/mtrr.h
+++ b/arch/x86/include/asm/mtrr.h
@@ -117,6 +117,18 @@  int mtrr_add_request(int type, uint64_t start, uint64_t size);
  */
 int mtrr_commit(bool do_caches);
 
+/**
+ * mtrr_set_next_var() - set up a variable MTRR
+ *
+ * This finds the first free variable MTRR and sets to the given area
+ *
+ * @type:	Requested type (MTRR_TYPE_)
+ * @start:	Start address
+ * @size:	Size
+ * @return 0 on success, -ENOSPC if there are no more MTRRs
+ */
+int mtrr_set_next_var(uint type, uint64_t base, uint64_t size);
+
 #endif
 
 #if ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE - 1)) != 0)