From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Gibson Subject: Re: [kvm-unit-tests PATCH 11/14] powerpc/ppc64: add rtas_power_off Date: Tue, 4 Aug 2015 14:11:30 +1000 Message-ID: <20150804041130.GF3080@voom.redhat.com> References: <1438612891-3718-1-git-send-email-drjones@redhat.com> <1438612891-3718-12-git-send-email-drjones@redhat.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="eVzOFob/8UvintSX" Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, dgibson@redhat.com, agraf@suse.de, thuth@redhat.com, lvivier@redhat.com, pbonzini@redhat.com To: Andrew Jones Return-path: Received: from ozlabs.org ([103.22.144.67]:42317 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752179AbbHDEUq (ORCPT ); Tue, 4 Aug 2015 00:20:46 -0400 Content-Disposition: inline In-Reply-To: <1438612891-3718-12-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: --eVzOFob/8UvintSX Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Aug 03, 2015 at 04:41:28PM +0200, Andrew Jones wrote: > Add enough RTAS support to support power-off, and apply it to > exit(). >=20 > Signed-off-by: Andrew Jones > --- > lib/powerpc/asm/rtas.h | 27 ++++++++++++++++ > lib/powerpc/io.c | 3 ++ > lib/powerpc/rtas.c | 84 +++++++++++++++++++++++++++++++++++++++++++= ++++++ > lib/ppc64/asm/rtas.h | 1 + > powerpc/Makefile.common | 1 + > powerpc/cstart64.S | 9 ++++++ > 6 files changed, 125 insertions(+) > create mode 100644 lib/powerpc/asm/rtas.h > create mode 100644 lib/powerpc/rtas.c > create mode 100644 lib/ppc64/asm/rtas.h >=20 > diff --git a/lib/powerpc/asm/rtas.h b/lib/powerpc/asm/rtas.h > new file mode 100644 > index 0000000000000..53ce126c69a42 > --- /dev/null > +++ b/lib/powerpc/asm/rtas.h > @@ -0,0 +1,27 @@ > +#ifndef _ASMPOWERPC_RTAS_H_ > +#define _ASMPOWERPC_RTAS_H_ > +/* > + * Copyright (C) 2015, Red Hat Inc, Andrew Jones > + * > + * This work is licensed under the terms of the GNU LGPL, version 2. > + */ > +#include > + > +#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 void enter_rtas(unsigned long args); > +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 a7eaafeca9205..25cdd0ad961ee 100644 > --- a/lib/powerpc/io.c > +++ b/lib/powerpc/io.c > @@ -7,6 +7,7 @@ > */ > #include > #include > +#include > =20 > extern void halt(int code); > extern void putchar(int c); > @@ -15,6 +16,7 @@ static struct spinlock uart_lock; > =20 > void io_init(void) > { > + rtas_init(); > } > =20 > void puts(const char *s) > @@ -27,5 +29,6 @@ void puts(const char *s) > =20 > void exit(int code) > { > + rtas_power_off(); > halt(code); > } > diff --git a/lib/powerpc/rtas.c b/lib/powerpc/rtas.c > new file mode 100644 > index 0000000000000..39f7d4428ee77 > --- /dev/null > +++ b/lib/powerpc/rtas.c > @@ -0,0 +1,84 @@ > +/* > + * powerpc RTAS > + * > + * Copyright (C) 2015, Red Hat Inc, Andrew Jones > + * > + * This work is licensed under the terms of the GNU LGPL, version 2. > + */ > +#include > +#include > +#include > +#include > +#include > +#include > + > +static struct spinlock lock; > +struct rtas_args rtas_args; > +static int rtas_node; > + > +void rtas_init(void) > +{ > + if (!dt_available()) { > + printf("%s: No device tree!\n", __func__); > + abort(); > + } > + > + rtas_node =3D fdt_path_offset(dt_fdt(), "/rtas"); > + if (rtas_node < 0) { > + printf("%s: /rtas node: %s\n", __func__, > + fdt_strerror(rtas_node)); > + abort(); > + } > +} > + > +int rtas_token(const char *service) > +{ > + const struct fdt_property *prop; > + u32 *token; > + > + prop =3D fdt_get_property(dt_fdt(), rtas_node, service, NULL); Oh.. one other thing here. This is only safe if you never alter the device tree blob you're given (or at least, not after rtas_init()). That may well be the case, but I don't know your code well enough to be sure. Otherwise, the rtas node could get moved by other dt changes, meaning the offset stored in rtas_node is no longer valid. > + if (prop) { > + token =3D (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(&lock); > + > + rtas_args.token =3D cpu_to_be32(token); > + rtas_args.nargs =3D cpu_to_be32(nargs); > + rtas_args.nret =3D cpu_to_be32(nret); > + rtas_args.rets =3D &rtas_args.args[nargs]; > + > + va_start(list, outputs); > + for (i =3D 0; i < nargs; ++i) > + rtas_args.args[i] =3D cpu_to_be32(va_arg(list, u32)); > + va_end(list); > + > + for (i =3D 0; i < nret; ++i) > + rtas_args.rets[i] =3D 0; > + > + enter_rtas(__pa(&rtas_args)); > + > + if (nret > 1 && outputs !=3D NULL) > + for (i =3D 0; i < nret - 1; ++i) > + outputs[i] =3D be32_to_cpu(rtas_args.rets[i + 1]); > + > + ret =3D nret > 0 ? be32_to_cpu(rtas_args.rets[0]) : 0; > + > + spin_unlock(&lock); > + return ret; > +} > + > +void rtas_power_off(void) > +{ > + printf("RTAS power-off returned %d\n", > + rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1)); > + assert(0); > +} > 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 b130342dee60e..f4eff7f8eccb4 100644 > --- a/powerpc/Makefile.common > +++ b/powerpc/Makefile.common > @@ -39,6 +39,7 @@ cflatobjs +=3D lib/powerpc/io.o > cflatobjs +=3D lib/powerpc/setup.o > cflatobjs +=3D lib/powerpc/mmu.o > cflatobjs +=3D lib/powerpc/smp.o > +cflatobjs +=3D lib/powerpc/rtas.o > =20 > libgcc :=3D $(shell $(CC) $(machine) --print-libgcc-file-name) > start_addr :=3D $(shell printf "%x\n" $$(( $(phys_base) + $(kernel_offse= t) ))) > diff --git a/powerpc/cstart64.S b/powerpc/cstart64.S > index 8edaaa6e251fc..1c48083efa33c 100644 > --- a/powerpc/cstart64.S > +++ b/powerpc/cstart64.S > @@ -8,6 +8,7 @@ > =20 > #define HVSC .long 0x44000022 > #define H_PUT_TERM_CHAR 0x58 > +#define KVMPPC_H_RTAS 0xf000 > =20 > #define LOAD_REG_IMMEDIATE(reg,expr) \ > lis reg,(expr)@highest; \ > @@ -85,3 +86,11 @@ putchar: > li r5, 1 /* sending just 1 byte */ > HVSC > blr > + > +.globl enter_rtas > +enter_rtas: > + mr r4, r3 > + lis r3, KVMPPC_H_RTAS@h > + ori r3, r3, KVMPPC_H_RTAS@l > + HVSC > + blr --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --eVzOFob/8UvintSX Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJVwDtyAAoJEGw4ysog2bOS+JUP/A+wRO1qkbtT35kCx/4Ls3LR OoBL4H6NIUb7zFHW4YjXHLaDjUBGtdAI2LqvvUyPbd06U1kFsj07MTRHKTyfGZAd WOQDV6GAUoY6Cu54EJoD/4FtQNnRgyOGFDBvX6/qMDbS8XWPWUGbbp9nQMaD0P90 G1HAFJcGXVADZLN3GSdFgX02QJBfcfJRH56Ye1bfJpecZPrAtwA8G1Xy4tlFje4v q+AaXy5j/2W5Q/y4D+mz7t9RTG/mDCSDca9tpBhszu912An/xtWBf2JGs56bE771 EZ3O1x2U2Wc+odQq59js1azq+bPEQFy6pvj38pULup41SwbqzpX2pCx9FHm42wLQ /T+S4WLjgHz87HU0daL2N83eDuQW0A9KSz/cM6igkrVLrKwei8qfgq/+NgXjSDh0 qrZK2hiQGIVd/y17f/DTf+83pEhFJyzKT9FJKfgTZ3FwTf2/Yv5pL36VQnJSSgOx x+1zNqhLIVuBcAkrl4n/nbi5nQocJjPFmSFv6ODsOJdtikq3occd5lh4xkOmZnh0 nwZSx9ZuEZ820ckcDSzO7jbvzzs7gC4Ab6otlSGqf1ZF3133c62FTFxIKZP6/Quj kD5HIgl33BC8Zwo3wGJHti4z1CokEnF8MPYwA6KnuAJ8TY/S2iutWMuIUOLUC9OF hUiPwB1Fmlexf22ZTupL =YzPa -----END PGP SIGNATURE----- --eVzOFob/8UvintSX--