diff mbox

[kvm-unit-tests,v6,17/18] powerpc/ppc64: add RTAS support

Message ID 1455897503-11835-18-git-send-email-drjones@redhat.com
State Superseded
Headers show

Commit Message

Andrew Jones Feb. 19, 2016, 3:58 p.m. UTC
Add enough RTAS support to start adding RTAS commands. Just add
power-off for now.

Signed-off-by: Andrew Jones <drjones@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Tested-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
 lib/powerpc/asm/rtas.h  |  26 +++++++++
 lib/powerpc/io.c        |   2 +
 lib/powerpc/rtas.c      | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ppc64/asm/rtas.h    |   1 +
 powerpc/Makefile.common |   1 +
 5 files changed, 169 insertions(+)
 create mode 100644 lib/powerpc/asm/rtas.h
 create mode 100644 lib/powerpc/rtas.c
 create mode 100644 lib/ppc64/asm/rtas.h

Comments

Andrew Jones Feb. 19, 2016, 4:10 p.m. UTC | #1
On Fri, Feb 19, 2016 at 04:58:22PM +0100, Andrew Jones wrote:
> Add enough RTAS support to start adding RTAS commands. Just add
> power-off for now.
> 
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> Reviewed-by: Thomas Huth <thuth@redhat.com>
> Tested-by: Laurent Vivier <lvivier@redhat.com>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

I've left the r-b's on, but there was a decent amount of code movement
to better handle rtas blob endianness. Please take a look.

Thanks,
drew
--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Gibson Feb. 22, 2016, 3:26 a.m. UTC | #2
On Fri, Feb 19, 2016 at 04:58:22PM +0100, Andrew Jones wrote:
> Add enough RTAS support to start adding RTAS commands. Just add
> power-off for now.
> 
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> Reviewed-by: Thomas Huth <thuth@redhat.com>
> Tested-by: Laurent Vivier <lvivier@redhat.com>
> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  lib/powerpc/asm/rtas.h  |  26 +++++++++
>  lib/powerpc/io.c        |   2 +
>  lib/powerpc/rtas.c      | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
>  lib/ppc64/asm/rtas.h    |   1 +
>  powerpc/Makefile.common |   1 +
>  5 files changed, 169 insertions(+)
>  create mode 100644 lib/powerpc/asm/rtas.h
>  create mode 100644 lib/powerpc/rtas.c
>  create mode 100644 lib/ppc64/asm/rtas.h
> 
> diff --git a/lib/powerpc/asm/rtas.h b/lib/powerpc/asm/rtas.h
> new file mode 100644
> index 0000000000000..522225bcb6de3
> --- /dev/null
> +++ b/lib/powerpc/asm/rtas.h
> @@ -0,0 +1,26 @@
> +#ifndef _ASMPOWERPC_RTAS_H_
> +#define _ASMPOWERPC_RTAS_H_
> +/*
> + * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> + *
> + * This work is licensed under the terms of the GNU LGPL, version 2.
> + */
> +#include <libcflat.h>
> +
> +#define RTAS_UNKNOWN_SERVICE	(-1)
> +
> +struct rtas_args {
> +	u32 token;
> +	u32 nargs;
> +	u32 nret;
> +	u32 args[16];
> +	u32 *rets;
> +};
> +
> +extern void rtas_init(void);
> +extern int rtas_token(const char *service);
> +extern int rtas_call(int token, int nargs, int nret, int *outputs, ...);
> +
> +extern void rtas_power_off(void);
> +
> +#endif /* _ASMPOWERPC_RTAS_H_ */
> diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
> index ef90946fb1131..c4c61ea8ac1cf 100644
> --- a/lib/powerpc/io.c
> +++ b/lib/powerpc/io.c
> @@ -7,6 +7,7 @@
>   */
>  #include <libcflat.h>
>  #include <asm/spinlock.h>
> +#include <asm/rtas.h>
>  
>  extern void halt(int code);
>  extern void putchar(int c);
> @@ -15,6 +16,7 @@ static struct spinlock print_lock;
>  
>  void io_init(void)
>  {
> +	rtas_init();
>  }
>  
>  void puts(const char *s)
> diff --git a/lib/powerpc/rtas.c b/lib/powerpc/rtas.c
> new file mode 100644
> index 0000000000000..464d066d3c220
> --- /dev/null
> +++ b/lib/powerpc/rtas.c
> @@ -0,0 +1,139 @@
> +/*
> + * powerpc RTAS
> + *
> + * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> + *
> + * This work is licensed under the terms of the GNU LGPL, version 2.
> + */
> +#include <libcflat.h>
> +#include <libfdt/libfdt.h>
> +#include <devicetree.h>
> +#include <asm/spinlock.h>
> +#include <asm/hcall.h>
> +#include <asm/io.h>
> +#include <asm/rtas.h>
> +
> +#define RTAS_BLOB_WORDS 8
> +
> +#if defined(__powerpc64__)
> +struct func_ptr {
> +	unsigned long ptr;
> +	unsigned long toc;
> +};
> +static struct func_ptr rtas_entry_funcptr;
> +#endif
> +static u32 rtas_blob[RTAS_BLOB_WORDS];
> +typedef void (*rtas_entry_t)(unsigned long);
> +static rtas_entry_t enter_rtas;
> +static struct rtas_args rtas_args;
> +static struct spinlock rtas_lock;
> +
> +static int rtas_node(void)
> +{
> +	int node = fdt_path_offset(dt_fdt(), "/rtas");
> +
> +	if (node < 0) {
> +		printf("%s: /rtas: %s\n", __func__, fdt_strerror(node));
> +		abort();
> +	}
> +
> +	return node;
> +}
> +
> +void rtas_init(void)
> +{
> +	bool broken_sc1 = hcall_have_broken_sc1();
> +	int node = rtas_node(), len, words, i;
> +	const struct fdt_property *prop;
> +	u32 *data, *insns;
> +
> +	if (!dt_available()) {
> +		printf("%s: No device tree!\n", __func__);
> +		abort();
> +	}
> +
> +	prop = fdt_get_property(dt_fdt(), node,
> +				"linux,rtas-entry", &len);
> +	if (!prop) {
> +		printf("%s: /rtas/linux,rtas-entry: %s\n",
> +				__func__, fdt_strerror(len));
> +		abort();
> +	}
> +
> +	data = (u32 *)prop->data;
> +	insns = (u32 *)(unsigned long)fdt32_to_cpu(*data);
> +
> +	prop = fdt_get_property(dt_fdt(), node, "rtas-size", &len);
> +	if (!prop) {
> +		printf("%s: /rtas/rtas-size: %s\n",
> +				__func__, fdt_strerror(len));
> +		abort();
> +	}
> +	data = (u32 *)prop->data;
> +	words = (int)fdt32_to_cpu(*data)/4;
> +	assert(words <= RTAS_BLOB_WORDS);
> +
> +	for (i = 0; i < words; ++i) {
> +		rtas_blob[i] = be32_to_cpu(insns[i]);
> +		if (broken_sc1 && rtas_blob[i] == SC1)
> +			rtas_blob[i] = SC1_REPLACEMENT;
> +	}

Hmm.. in general it's not guaranteed that you can copy around the RTAS
blob and have it still work.  But.. you're working essentially at the
same level as SLOF here, rather than a "normal" OS and I think SLOF
must make the same assumption.  So I guess it's ok.

> +#if defined(__powerpc64__)

Technically this ifdef should be based on the ABI version, rather than
__powerpc64__, but I'm not sure if there are standard #defines for that.

> +	rtas_entry_funcptr.ptr = (unsigned long)rtas_blob;
> +	enter_rtas = (rtas_entry_t)&rtas_entry_funcptr;
> +#else
> +	enter_rtas = (rtas_entry_t)rtas_blob;
> +#endif

I think you may want to have an actual coded enter_rtas function,
rather than just constructing a function pointer around the raw blob.
For one thing it should reduce the ugly ABI version #ifdefs.  More
importantly though, you can use it to set up the correct MSR state for
entry into RTAS (IR, DR, EE off, big-endian, ..).  At the moment all
your code runs in a suitable MSR state to call directly into RTAS, but
that will probably change fairly soon.


> +}
> +
> +int rtas_token(const char *service)
> +{
> +	const struct fdt_property *prop;
> +	u32 *token;
> +
> +	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
> +	if (prop) {
> +		token = (u32 *)prop->data;
> +		return fdt32_to_cpu(*token);
> +	}
> +	return RTAS_UNKNOWN_SERVICE;
> +}
> +
> +int rtas_call(int token, int nargs, int nret, int *outputs, ...)
> +{
> +	va_list list;
> +	int ret, i;
> +
> +	spin_lock(&rtas_lock);
> +
> +	rtas_args.token = cpu_to_be32(token);
> +	rtas_args.nargs = cpu_to_be32(nargs);
> +	rtas_args.nret = cpu_to_be32(nret);
> +	rtas_args.rets = &rtas_args.args[nargs];
> +
> +	va_start(list, outputs);
> +	for (i = 0; i < nargs; ++i)
> +		rtas_args.args[i] = cpu_to_be32(va_arg(list, u32));
> +	va_end(list);
> +
> +	for (i = 0; i < nret; ++i)
> +		rtas_args.rets[i] = 0;
> +
> +	enter_rtas(__pa(&rtas_args));
> +
> +	if (nret > 1 && outputs != NULL)
> +		for (i = 0; i < nret - 1; ++i)
> +			outputs[i] = be32_to_cpu(rtas_args.rets[i + 1]);
> +
> +	ret = nret > 0 ? be32_to_cpu(rtas_args.rets[0]) : 0;
> +
> +	spin_unlock(&rtas_lock);
> +	return ret;
> +}
> +
> +void rtas_power_off(void)
> +{
> +	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
> +	printf("RTAS power-off returned %d\n", ret);

abort here?

> +}
> diff --git a/lib/ppc64/asm/rtas.h b/lib/ppc64/asm/rtas.h
> new file mode 100644
> index 0000000000000..fe77f635cd860
> --- /dev/null
> +++ b/lib/ppc64/asm/rtas.h
> @@ -0,0 +1 @@
> +#include "../../powerpc/asm/rtas.h"
> diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
> index 07ba135f77110..cad728ecbe269 100644
> --- a/powerpc/Makefile.common
> +++ b/powerpc/Makefile.common
> @@ -28,6 +28,7 @@ cflatobjs += lib/devicetree.o
>  cflatobjs += lib/powerpc/io.o
>  cflatobjs += lib/powerpc/hcall.o
>  cflatobjs += lib/powerpc/setup.o
> +cflatobjs += lib/powerpc/rtas.o
>  
>  libgcc := $(shell $(CC) $(machine) --print-libgcc-file-name)
>
Thomas Huth Feb. 22, 2016, 7:39 a.m. UTC | #3
On 22.02.2016 04:26, David Gibson wrote:
> On Fri, Feb 19, 2016 at 04:58:22PM +0100, Andrew Jones wrote:
>> Add enough RTAS support to start adding RTAS commands. Just add
>> power-off for now.
>>
>> Signed-off-by: Andrew Jones <drjones@redhat.com>
>> Reviewed-by: Thomas Huth <thuth@redhat.com>
>> Tested-by: Laurent Vivier <lvivier@redhat.com>
>> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
>> ---
>>  lib/powerpc/asm/rtas.h  |  26 +++++++++
>>  lib/powerpc/io.c        |   2 +
>>  lib/powerpc/rtas.c      | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
>>  lib/ppc64/asm/rtas.h    |   1 +
>>  powerpc/Makefile.common |   1 +
>>  5 files changed, 169 insertions(+)
>>  create mode 100644 lib/powerpc/asm/rtas.h
>>  create mode 100644 lib/powerpc/rtas.c
>>  create mode 100644 lib/ppc64/asm/rtas.h
...
>> +
>> +void rtas_power_off(void)
>> +{
>> +	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
>> +	printf("RTAS power-off returned %d\n", ret);
> 
> abort here?

No, that does not work ... there was an assert() in an earlier version
of the patch here, and that did not work: assert() (and abort()) call
exit() in kvm-unit-test land, and exit again calls rtas_power_off()
again, so you end up with and endless recursive loop.
But exit() calls halt() after calling rtas_power_off(), so it's ok if
that function simply returns here.

 Thomas
David Gibson Feb. 22, 2016, 7:56 a.m. UTC | #4
On Mon, Feb 22, 2016 at 08:39:48AM +0100, Thomas Huth wrote:
> On 22.02.2016 04:26, David Gibson wrote:
> > On Fri, Feb 19, 2016 at 04:58:22PM +0100, Andrew Jones wrote:
> >> Add enough RTAS support to start adding RTAS commands. Just add
> >> power-off for now.
> >>
> >> Signed-off-by: Andrew Jones <drjones@redhat.com>
> >> Reviewed-by: Thomas Huth <thuth@redhat.com>
> >> Tested-by: Laurent Vivier <lvivier@redhat.com>
> >> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> >> ---
> >>  lib/powerpc/asm/rtas.h  |  26 +++++++++
> >>  lib/powerpc/io.c        |   2 +
> >>  lib/powerpc/rtas.c      | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
> >>  lib/ppc64/asm/rtas.h    |   1 +
> >>  powerpc/Makefile.common |   1 +
> >>  5 files changed, 169 insertions(+)
> >>  create mode 100644 lib/powerpc/asm/rtas.h
> >>  create mode 100644 lib/powerpc/rtas.c
> >>  create mode 100644 lib/ppc64/asm/rtas.h
> ...
> >> +
> >> +void rtas_power_off(void)
> >> +{
> >> +	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
> >> +	printf("RTAS power-off returned %d\n", ret);
> > 
> > abort here?
> 
> No, that does not work ... there was an assert() in an earlier version
> of the patch here, and that did not work: assert() (and abort()) call
> exit() in kvm-unit-test land, and exit again calls rtas_power_off()
> again, so you end up with and endless recursive loop.
> But exit() calls halt() after calling rtas_power_off(), so it's ok if
> that function simply returns here.

Ah, ok.
Andrew Jones Feb. 23, 2016, 3:03 p.m. UTC | #5
On Mon, Feb 22, 2016 at 02:26:28PM +1100, David Gibson wrote:
> On Fri, Feb 19, 2016 at 04:58:22PM +0100, Andrew Jones wrote:
> > Add enough RTAS support to start adding RTAS commands. Just add
> > power-off for now.
> > 
> > Signed-off-by: Andrew Jones <drjones@redhat.com>
> > Reviewed-by: Thomas Huth <thuth@redhat.com>
> > Tested-by: Laurent Vivier <lvivier@redhat.com>
> > Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  lib/powerpc/asm/rtas.h  |  26 +++++++++
> >  lib/powerpc/io.c        |   2 +
> >  lib/powerpc/rtas.c      | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
> >  lib/ppc64/asm/rtas.h    |   1 +
> >  powerpc/Makefile.common |   1 +
> >  5 files changed, 169 insertions(+)
> >  create mode 100644 lib/powerpc/asm/rtas.h
> >  create mode 100644 lib/powerpc/rtas.c
> >  create mode 100644 lib/ppc64/asm/rtas.h
> > 
> > diff --git a/lib/powerpc/asm/rtas.h b/lib/powerpc/asm/rtas.h
> > new file mode 100644
> > index 0000000000000..522225bcb6de3
> > --- /dev/null
> > +++ b/lib/powerpc/asm/rtas.h
> > @@ -0,0 +1,26 @@
> > +#ifndef _ASMPOWERPC_RTAS_H_
> > +#define _ASMPOWERPC_RTAS_H_
> > +/*
> > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> > + *
> > + * This work is licensed under the terms of the GNU LGPL, version 2.
> > + */
> > +#include <libcflat.h>
> > +
> > +#define RTAS_UNKNOWN_SERVICE	(-1)
> > +
> > +struct rtas_args {
> > +	u32 token;
> > +	u32 nargs;
> > +	u32 nret;
> > +	u32 args[16];
> > +	u32 *rets;
> > +};
> > +
> > +extern void rtas_init(void);
> > +extern int rtas_token(const char *service);
> > +extern int rtas_call(int token, int nargs, int nret, int *outputs, ...);
> > +
> > +extern void rtas_power_off(void);
> > +
> > +#endif /* _ASMPOWERPC_RTAS_H_ */
> > diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
> > index ef90946fb1131..c4c61ea8ac1cf 100644
> > --- a/lib/powerpc/io.c
> > +++ b/lib/powerpc/io.c
> > @@ -7,6 +7,7 @@
> >   */
> >  #include <libcflat.h>
> >  #include <asm/spinlock.h>
> > +#include <asm/rtas.h>
> >  
> >  extern void halt(int code);
> >  extern void putchar(int c);
> > @@ -15,6 +16,7 @@ static struct spinlock print_lock;
> >  
> >  void io_init(void)
> >  {
> > +	rtas_init();
> >  }
> >  
> >  void puts(const char *s)
> > diff --git a/lib/powerpc/rtas.c b/lib/powerpc/rtas.c
> > new file mode 100644
> > index 0000000000000..464d066d3c220
> > --- /dev/null
> > +++ b/lib/powerpc/rtas.c
> > @@ -0,0 +1,139 @@
> > +/*
> > + * powerpc RTAS
> > + *
> > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> > + *
> > + * This work is licensed under the terms of the GNU LGPL, version 2.
> > + */
> > +#include <libcflat.h>
> > +#include <libfdt/libfdt.h>
> > +#include <devicetree.h>
> > +#include <asm/spinlock.h>
> > +#include <asm/hcall.h>
> > +#include <asm/io.h>
> > +#include <asm/rtas.h>
> > +
> > +#define RTAS_BLOB_WORDS 8
> > +
> > +#if defined(__powerpc64__)
> > +struct func_ptr {
> > +	unsigned long ptr;
> > +	unsigned long toc;
> > +};
> > +static struct func_ptr rtas_entry_funcptr;
> > +#endif
> > +static u32 rtas_blob[RTAS_BLOB_WORDS];
> > +typedef void (*rtas_entry_t)(unsigned long);
> > +static rtas_entry_t enter_rtas;
> > +static struct rtas_args rtas_args;
> > +static struct spinlock rtas_lock;
> > +
> > +static int rtas_node(void)
> > +{
> > +	int node = fdt_path_offset(dt_fdt(), "/rtas");
> > +
> > +	if (node < 0) {
> > +		printf("%s: /rtas: %s\n", __func__, fdt_strerror(node));
> > +		abort();
> > +	}
> > +
> > +	return node;
> > +}
> > +
> > +void rtas_init(void)
> > +{
> > +	bool broken_sc1 = hcall_have_broken_sc1();
> > +	int node = rtas_node(), len, words, i;
> > +	const struct fdt_property *prop;
> > +	u32 *data, *insns;
> > +
> > +	if (!dt_available()) {
> > +		printf("%s: No device tree!\n", __func__);
> > +		abort();
> > +	}
> > +
> > +	prop = fdt_get_property(dt_fdt(), node,
> > +				"linux,rtas-entry", &len);
> > +	if (!prop) {
> > +		printf("%s: /rtas/linux,rtas-entry: %s\n",
> > +				__func__, fdt_strerror(len));
> > +		abort();
> > +	}
> > +
> > +	data = (u32 *)prop->data;
> > +	insns = (u32 *)(unsigned long)fdt32_to_cpu(*data);
> > +
> > +	prop = fdt_get_property(dt_fdt(), node, "rtas-size", &len);
> > +	if (!prop) {
> > +		printf("%s: /rtas/rtas-size: %s\n",
> > +				__func__, fdt_strerror(len));
> > +		abort();
> > +	}
> > +	data = (u32 *)prop->data;
> > +	words = (int)fdt32_to_cpu(*data)/4;
> > +	assert(words <= RTAS_BLOB_WORDS);
> > +
> > +	for (i = 0; i < words; ++i) {
> > +		rtas_blob[i] = be32_to_cpu(insns[i]);
> > +		if (broken_sc1 && rtas_blob[i] == SC1)
> > +			rtas_blob[i] = SC1_REPLACEMENT;
> > +	}
> 
> Hmm.. in general it's not guaranteed that you can copy around the RTAS
> blob and have it still work.  But.. you're working essentially at the
> same level as SLOF here, rather than a "normal" OS and I think SLOF
> must make the same assumption.  So I guess it's ok.

I can edit the instructions in place, like I was doing before, as now...

> 
> > +#if defined(__powerpc64__)
> 
> Technically this ifdef should be based on the ABI version, rather than
> __powerpc64__, but I'm not sure if there are standard #defines for that.
> 
> > +	rtas_entry_funcptr.ptr = (unsigned long)rtas_blob;
> > +	enter_rtas = (rtas_entry_t)&rtas_entry_funcptr;
> > +#else
> > +	enter_rtas = (rtas_entry_t)rtas_blob;
> > +#endif
> 
> I think you may want to have an actual coded enter_rtas function,
> rather than just constructing a function pointer around the raw blob.
> For one thing it should reduce the ugly ABI version #ifdefs.  More
> importantly though, you can use it to set up the correct MSR state for
> entry into RTAS (IR, DR, EE off, big-endian, ..).  At the moment all
> your code runs in a suitable MSR state to call directly into RTAS, but
> that will probably change fairly soon.

...I see I don't need to edit all instructions wrt to endianness. The
"call RTAS with BE state", even if running LE, was something I had
missed, which means the above code (converting the BE blob to cpu
endian) was the wrong thing to do. I've now added an assembler enter_rtas
function, which doesn't do anything special yet, but I think I know what
needs to be done with it, at least for LE enablement.

Thanks,
drew
--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Gibson Feb. 24, 2016, 12:30 a.m. UTC | #6
On Tue, Feb 23, 2016 at 04:03:55PM +0100, Andrew Jones wrote:
> On Mon, Feb 22, 2016 at 02:26:28PM +1100, David Gibson wrote:
> > On Fri, Feb 19, 2016 at 04:58:22PM +0100, Andrew Jones wrote:
> > > Add enough RTAS support to start adding RTAS commands. Just add
> > > power-off for now.
> > > 
> > > Signed-off-by: Andrew Jones <drjones@redhat.com>
> > > Reviewed-by: Thomas Huth <thuth@redhat.com>
> > > Tested-by: Laurent Vivier <lvivier@redhat.com>
> > > Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> > > ---
> > >  lib/powerpc/asm/rtas.h  |  26 +++++++++
> > >  lib/powerpc/io.c        |   2 +
> > >  lib/powerpc/rtas.c      | 139 ++++++++++++++++++++++++++++++++++++++++++++++++
> > >  lib/ppc64/asm/rtas.h    |   1 +
> > >  powerpc/Makefile.common |   1 +
> > >  5 files changed, 169 insertions(+)
> > >  create mode 100644 lib/powerpc/asm/rtas.h
> > >  create mode 100644 lib/powerpc/rtas.c
> > >  create mode 100644 lib/ppc64/asm/rtas.h
> > > 
> > > diff --git a/lib/powerpc/asm/rtas.h b/lib/powerpc/asm/rtas.h
> > > new file mode 100644
> > > index 0000000000000..522225bcb6de3
> > > --- /dev/null
> > > +++ b/lib/powerpc/asm/rtas.h
> > > @@ -0,0 +1,26 @@
> > > +#ifndef _ASMPOWERPC_RTAS_H_
> > > +#define _ASMPOWERPC_RTAS_H_
> > > +/*
> > > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> > > + *
> > > + * This work is licensed under the terms of the GNU LGPL, version 2.
> > > + */
> > > +#include <libcflat.h>
> > > +
> > > +#define RTAS_UNKNOWN_SERVICE	(-1)
> > > +
> > > +struct rtas_args {
> > > +	u32 token;
> > > +	u32 nargs;
> > > +	u32 nret;
> > > +	u32 args[16];
> > > +	u32 *rets;
> > > +};
> > > +
> > > +extern void rtas_init(void);
> > > +extern int rtas_token(const char *service);
> > > +extern int rtas_call(int token, int nargs, int nret, int *outputs, ...);
> > > +
> > > +extern void rtas_power_off(void);
> > > +
> > > +#endif /* _ASMPOWERPC_RTAS_H_ */
> > > diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
> > > index ef90946fb1131..c4c61ea8ac1cf 100644
> > > --- a/lib/powerpc/io.c
> > > +++ b/lib/powerpc/io.c
> > > @@ -7,6 +7,7 @@
> > >   */
> > >  #include <libcflat.h>
> > >  #include <asm/spinlock.h>
> > > +#include <asm/rtas.h>
> > >  
> > >  extern void halt(int code);
> > >  extern void putchar(int c);
> > > @@ -15,6 +16,7 @@ static struct spinlock print_lock;
> > >  
> > >  void io_init(void)
> > >  {
> > > +	rtas_init();
> > >  }
> > >  
> > >  void puts(const char *s)
> > > diff --git a/lib/powerpc/rtas.c b/lib/powerpc/rtas.c
> > > new file mode 100644
> > > index 0000000000000..464d066d3c220
> > > --- /dev/null
> > > +++ b/lib/powerpc/rtas.c
> > > @@ -0,0 +1,139 @@
> > > +/*
> > > + * powerpc RTAS
> > > + *
> > > + * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
> > > + *
> > > + * This work is licensed under the terms of the GNU LGPL, version 2.
> > > + */
> > > +#include <libcflat.h>
> > > +#include <libfdt/libfdt.h>
> > > +#include <devicetree.h>
> > > +#include <asm/spinlock.h>
> > > +#include <asm/hcall.h>
> > > +#include <asm/io.h>
> > > +#include <asm/rtas.h>
> > > +
> > > +#define RTAS_BLOB_WORDS 8
> > > +
> > > +#if defined(__powerpc64__)
> > > +struct func_ptr {
> > > +	unsigned long ptr;
> > > +	unsigned long toc;
> > > +};
> > > +static struct func_ptr rtas_entry_funcptr;
> > > +#endif
> > > +static u32 rtas_blob[RTAS_BLOB_WORDS];
> > > +typedef void (*rtas_entry_t)(unsigned long);
> > > +static rtas_entry_t enter_rtas;
> > > +static struct rtas_args rtas_args;
> > > +static struct spinlock rtas_lock;
> > > +
> > > +static int rtas_node(void)
> > > +{
> > > +	int node = fdt_path_offset(dt_fdt(), "/rtas");
> > > +
> > > +	if (node < 0) {
> > > +		printf("%s: /rtas: %s\n", __func__, fdt_strerror(node));
> > > +		abort();
> > > +	}
> > > +
> > > +	return node;
> > > +}
> > > +
> > > +void rtas_init(void)
> > > +{
> > > +	bool broken_sc1 = hcall_have_broken_sc1();
> > > +	int node = rtas_node(), len, words, i;
> > > +	const struct fdt_property *prop;
> > > +	u32 *data, *insns;
> > > +
> > > +	if (!dt_available()) {
> > > +		printf("%s: No device tree!\n", __func__);
> > > +		abort();
> > > +	}
> > > +
> > > +	prop = fdt_get_property(dt_fdt(), node,
> > > +				"linux,rtas-entry", &len);
> > > +	if (!prop) {
> > > +		printf("%s: /rtas/linux,rtas-entry: %s\n",
> > > +				__func__, fdt_strerror(len));
> > > +		abort();
> > > +	}
> > > +
> > > +	data = (u32 *)prop->data;
> > > +	insns = (u32 *)(unsigned long)fdt32_to_cpu(*data);
> > > +
> > > +	prop = fdt_get_property(dt_fdt(), node, "rtas-size", &len);
> > > +	if (!prop) {
> > > +		printf("%s: /rtas/rtas-size: %s\n",
> > > +				__func__, fdt_strerror(len));
> > > +		abort();
> > > +	}
> > > +	data = (u32 *)prop->data;
> > > +	words = (int)fdt32_to_cpu(*data)/4;
> > > +	assert(words <= RTAS_BLOB_WORDS);
> > > +
> > > +	for (i = 0; i < words; ++i) {
> > > +		rtas_blob[i] = be32_to_cpu(insns[i]);
> > > +		if (broken_sc1 && rtas_blob[i] == SC1)
> > > +			rtas_blob[i] = SC1_REPLACEMENT;
> > > +	}
> > 
> > Hmm.. in general it's not guaranteed that you can copy around the RTAS
> > blob and have it still work.  But.. you're working essentially at the
> > same level as SLOF here, rather than a "normal" OS and I think SLOF
> > must make the same assumption.  So I guess it's ok.
> 
> I can edit the instructions in place, like I was doing before, as now...

I don't think it really matters.

> > 
> > > +#if defined(__powerpc64__)
> > 
> > Technically this ifdef should be based on the ABI version, rather than
> > __powerpc64__, but I'm not sure if there are standard #defines for that.
> > 
> > > +	rtas_entry_funcptr.ptr = (unsigned long)rtas_blob;
> > > +	enter_rtas = (rtas_entry_t)&rtas_entry_funcptr;
> > > +#else
> > > +	enter_rtas = (rtas_entry_t)rtas_blob;
> > > +#endif
> > 
> > I think you may want to have an actual coded enter_rtas function,
> > rather than just constructing a function pointer around the raw blob.
> > For one thing it should reduce the ugly ABI version #ifdefs.  More
> > importantly though, you can use it to set up the correct MSR state for
> > entry into RTAS (IR, DR, EE off, big-endian, ..).  At the moment all
> > your code runs in a suitable MSR state to call directly into RTAS, but
> > that will probably change fairly soon.
> 
> ...I see I don't need to edit all instructions wrt to endianness. The
> "call RTAS with BE state", even if running LE, was something I had
> missed, which means the above code (converting the BE blob to cpu
> endian) was the wrong thing to do. I've now added an assembler enter_rtas
> function, which doesn't do anything special yet, but I think I know what
> needs to be done with it, at least for LE enablement.

Right.  The RTAS blob needs to be entered in BE mode, and technically
in 32-bit mode as well, although the one we use in KVM will work fine
in 64-bit mode as well.  That does also mean it can technically
clobber the upper halves of a lot of registers - see the kernel
enter_rtas().
diff mbox

Patch

diff --git a/lib/powerpc/asm/rtas.h b/lib/powerpc/asm/rtas.h
new file mode 100644
index 0000000000000..522225bcb6de3
--- /dev/null
+++ b/lib/powerpc/asm/rtas.h
@@ -0,0 +1,26 @@ 
+#ifndef _ASMPOWERPC_RTAS_H_
+#define _ASMPOWERPC_RTAS_H_
+/*
+ * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include <libcflat.h>
+
+#define RTAS_UNKNOWN_SERVICE	(-1)
+
+struct rtas_args {
+	u32 token;
+	u32 nargs;
+	u32 nret;
+	u32 args[16];
+	u32 *rets;
+};
+
+extern void rtas_init(void);
+extern int rtas_token(const char *service);
+extern int rtas_call(int token, int nargs, int nret, int *outputs, ...);
+
+extern void rtas_power_off(void);
+
+#endif /* _ASMPOWERPC_RTAS_H_ */
diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
index ef90946fb1131..c4c61ea8ac1cf 100644
--- a/lib/powerpc/io.c
+++ b/lib/powerpc/io.c
@@ -7,6 +7,7 @@ 
  */
 #include <libcflat.h>
 #include <asm/spinlock.h>
+#include <asm/rtas.h>
 
 extern void halt(int code);
 extern void putchar(int c);
@@ -15,6 +16,7 @@  static struct spinlock print_lock;
 
 void io_init(void)
 {
+	rtas_init();
 }
 
 void puts(const char *s)
diff --git a/lib/powerpc/rtas.c b/lib/powerpc/rtas.c
new file mode 100644
index 0000000000000..464d066d3c220
--- /dev/null
+++ b/lib/powerpc/rtas.c
@@ -0,0 +1,139 @@ 
+/*
+ * powerpc RTAS
+ *
+ * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include <libcflat.h>
+#include <libfdt/libfdt.h>
+#include <devicetree.h>
+#include <asm/spinlock.h>
+#include <asm/hcall.h>
+#include <asm/io.h>
+#include <asm/rtas.h>
+
+#define RTAS_BLOB_WORDS 8
+
+#if defined(__powerpc64__)
+struct func_ptr {
+	unsigned long ptr;
+	unsigned long toc;
+};
+static struct func_ptr rtas_entry_funcptr;
+#endif
+static u32 rtas_blob[RTAS_BLOB_WORDS];
+typedef void (*rtas_entry_t)(unsigned long);
+static rtas_entry_t enter_rtas;
+static struct rtas_args rtas_args;
+static struct spinlock rtas_lock;
+
+static int rtas_node(void)
+{
+	int node = fdt_path_offset(dt_fdt(), "/rtas");
+
+	if (node < 0) {
+		printf("%s: /rtas: %s\n", __func__, fdt_strerror(node));
+		abort();
+	}
+
+	return node;
+}
+
+void rtas_init(void)
+{
+	bool broken_sc1 = hcall_have_broken_sc1();
+	int node = rtas_node(), len, words, i;
+	const struct fdt_property *prop;
+	u32 *data, *insns;
+
+	if (!dt_available()) {
+		printf("%s: No device tree!\n", __func__);
+		abort();
+	}
+
+	prop = fdt_get_property(dt_fdt(), node,
+				"linux,rtas-entry", &len);
+	if (!prop) {
+		printf("%s: /rtas/linux,rtas-entry: %s\n",
+				__func__, fdt_strerror(len));
+		abort();
+	}
+
+	data = (u32 *)prop->data;
+	insns = (u32 *)(unsigned long)fdt32_to_cpu(*data);
+
+	prop = fdt_get_property(dt_fdt(), node, "rtas-size", &len);
+	if (!prop) {
+		printf("%s: /rtas/rtas-size: %s\n",
+				__func__, fdt_strerror(len));
+		abort();
+	}
+	data = (u32 *)prop->data;
+	words = (int)fdt32_to_cpu(*data)/4;
+	assert(words <= RTAS_BLOB_WORDS);
+
+	for (i = 0; i < words; ++i) {
+		rtas_blob[i] = be32_to_cpu(insns[i]);
+		if (broken_sc1 && rtas_blob[i] == SC1)
+			rtas_blob[i] = SC1_REPLACEMENT;
+	}
+
+#if defined(__powerpc64__)
+	rtas_entry_funcptr.ptr = (unsigned long)rtas_blob;
+	enter_rtas = (rtas_entry_t)&rtas_entry_funcptr;
+#else
+	enter_rtas = (rtas_entry_t)rtas_blob;
+#endif
+}
+
+int rtas_token(const char *service)
+{
+	const struct fdt_property *prop;
+	u32 *token;
+
+	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
+	if (prop) {
+		token = (u32 *)prop->data;
+		return fdt32_to_cpu(*token);
+	}
+	return RTAS_UNKNOWN_SERVICE;
+}
+
+int rtas_call(int token, int nargs, int nret, int *outputs, ...)
+{
+	va_list list;
+	int ret, i;
+
+	spin_lock(&rtas_lock);
+
+	rtas_args.token = cpu_to_be32(token);
+	rtas_args.nargs = cpu_to_be32(nargs);
+	rtas_args.nret = cpu_to_be32(nret);
+	rtas_args.rets = &rtas_args.args[nargs];
+
+	va_start(list, outputs);
+	for (i = 0; i < nargs; ++i)
+		rtas_args.args[i] = cpu_to_be32(va_arg(list, u32));
+	va_end(list);
+
+	for (i = 0; i < nret; ++i)
+		rtas_args.rets[i] = 0;
+
+	enter_rtas(__pa(&rtas_args));
+
+	if (nret > 1 && outputs != NULL)
+		for (i = 0; i < nret - 1; ++i)
+			outputs[i] = be32_to_cpu(rtas_args.rets[i + 1]);
+
+	ret = nret > 0 ? be32_to_cpu(rtas_args.rets[0]) : 0;
+
+	spin_unlock(&rtas_lock);
+	return ret;
+}
+
+void rtas_power_off(void)
+{
+	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	printf("RTAS power-off returned %d\n", ret);
+}
diff --git a/lib/ppc64/asm/rtas.h b/lib/ppc64/asm/rtas.h
new file mode 100644
index 0000000000000..fe77f635cd860
--- /dev/null
+++ b/lib/ppc64/asm/rtas.h
@@ -0,0 +1 @@ 
+#include "../../powerpc/asm/rtas.h"
diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
index 07ba135f77110..cad728ecbe269 100644
--- a/powerpc/Makefile.common
+++ b/powerpc/Makefile.common
@@ -28,6 +28,7 @@  cflatobjs += lib/devicetree.o
 cflatobjs += lib/powerpc/io.o
 cflatobjs += lib/powerpc/hcall.o
 cflatobjs += lib/powerpc/setup.o
+cflatobjs += lib/powerpc/rtas.o
 
 libgcc := $(shell $(CC) $(machine) --print-libgcc-file-name)