From patchwork Mon Sep 1 07:53:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 36335 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f200.google.com (mail-qc0-f200.google.com [209.85.216.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8AF5D20C7F for ; Mon, 1 Sep 2014 07:54:51 +0000 (UTC) Received: by mail-qc0-f200.google.com with SMTP id w7sf17826084qcr.7 for ; Mon, 01 Sep 2014 00:54:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:date :message-id:in-reply-to:references:cc:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=S+ONkO3qtMq/pIrYy6qxkloXffKvNnpBN90H/E+5bc8=; b=GShjj0oRtwUuJm/fFfwEF6w7ddnJXi1RmZhubYDmLM+kxes8CDERGJw60bGdVdOX2O kEmH0eqOlouMg6bcZEthKQEvWWZocsvxExdmaD3QuUq5aJzrODNQDwAXRBCnfgHRWCL5 QsRH4IVI9nwhjDxM1Omi9iL5Yxade6cNKvE+VpbZpq5uf/hhZjGwNLwvY2SQd6flhKau LQ7f4YyeNPo1Rjjx/gtVYNdp25UWZG7P0MZ1dCW2Q2WwC/XWqToWS/Y5ytoMeqqZ8zVI wle+FuJ4iKwCdzmdOaKinzO+zYKP2W8X3Ho9VneUwtxhf3VRGYLtE+q/nBjjBPBE9FFI 2b2g== X-Gm-Message-State: ALoCoQlrbrC+7CrI0RuatkptfIvw7wXRcozeYX1I/dRx7vtDylU5LruxO/ElMz7yBx1YUB0z5cHo X-Received: by 10.236.112.234 with SMTP id y70mr14963301yhg.32.1409558091336; Mon, 01 Sep 2014 00:54:51 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.30.245 with SMTP id d108ls1109176qgd.68.gmail; Mon, 01 Sep 2014 00:54:51 -0700 (PDT) X-Received: by 10.52.185.193 with SMTP id fe1mr20894249vdc.31.1409558091249; Mon, 01 Sep 2014 00:54:51 -0700 (PDT) Received: from mail-vc0-f175.google.com (mail-vc0-f175.google.com [209.85.220.175]) by mx.google.com with ESMTPS id t9si30795vcy.1.2014.09.01.00.54.51 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 01 Sep 2014 00:54:51 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.175 as permitted sender) client-ip=209.85.220.175; Received: by mail-vc0-f175.google.com with SMTP id lf12so5098652vcb.34 for ; Mon, 01 Sep 2014 00:54:51 -0700 (PDT) X-Received: by 10.52.94.108 with SMTP id db12mr20246749vdb.8.1409558091134; Mon, 01 Sep 2014 00:54:51 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.45.67 with SMTP id uj3csp350194vcb; Mon, 1 Sep 2014 00:54:50 -0700 (PDT) X-Received: by 10.140.107.198 with SMTP id h64mr39607444qgf.42.1409558090237; Mon, 01 Sep 2014 00:54:50 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [208.118.235.17]) by mx.google.com with ESMTPS id h10si9634570qgf.118.2014.09.01.00.54.50 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 01 Sep 2014 00:54:50 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; Received: from localhost ([::1]:52604 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOMRu-0002Wj-0j for patch@linaro.org; Mon, 01 Sep 2014 03:54:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51731) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOMQn-0001GL-Cb for qemu-devel@nongnu.org; Mon, 01 Sep 2014 03:53:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XOMQe-0006nx-Q6 for qemu-devel@nongnu.org; Mon, 01 Sep 2014 03:53:41 -0400 Received: from mail-we0-f182.google.com ([74.125.82.182]:52784) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOMQe-0006nn-HT for qemu-devel@nongnu.org; Mon, 01 Sep 2014 03:53:32 -0400 Received: by mail-we0-f182.google.com with SMTP id w62so4989014wes.41 for ; Mon, 01 Sep 2014 00:53:31 -0700 (PDT) X-Received: by 10.180.19.35 with SMTP id b3mr19593959wie.52.1409558011774; Mon, 01 Sep 2014 00:53:31 -0700 (PDT) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id gh1sm23183323wib.18.2014.09.01.00.53.30 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 01 Sep 2014 00:53:31 -0700 (PDT) From: Ard Biesheuvel To: qemu-devel@nongnu.org, peter.maydell@linaro.org, rob.herring@linaro.org Date: Mon, 1 Sep 2014 09:53:20 +0200 Message-Id: <1409558001-12148-5-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1409558001-12148-1-git-send-email-ard.biesheuvel@linaro.org> References: <1409558001-12148-1-git-send-email-ard.biesheuvel@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 74.125.82.182 Cc: Ard Biesheuvel Subject: [Qemu-devel] [PATCH 4/5] target-arm: add emulation of PSCI calls for system emulation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.175 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 From: Rob Herring Add support for handling PSCI calls in system emulation. Both version 0.1 and 0.2 of the PSCI spec are supported. Platforms can enable support by setting "psci-method" QOM property on the cpus to SMC or HVC emulation and having PSCI binding in their dtb. Signed-off-by: Rob Herring Signed-off-by: Ard Biesheuvel --- target-arm/Makefile.objs | 1 + target-arm/cpu-qom.h | 6 ++ target-arm/cpu.c | 10 +++- target-arm/cpu.h | 6 ++ target-arm/helper.c | 12 ++++ target-arm/psci.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 target-arm/psci.c diff --git a/target-arm/Makefile.objs b/target-arm/Makefile.objs index dcd167e0d880..deda9f49fec3 100644 --- a/target-arm/Makefile.objs +++ b/target-arm/Makefile.objs @@ -7,5 +7,6 @@ obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o obj-y += translate.o op_helper.o helper.o cpu.o obj-y += neon_helper.o iwmmxt_helper.o obj-y += gdbstub.o +obj-y += psci.o obj-$(TARGET_AARCH64) += cpu64.o translate-a64.o helper-a64.o gdbstub64.o obj-y += crypto_helper.o diff --git a/target-arm/cpu-qom.h b/target-arm/cpu-qom.h index bcdd1e0edb55..b1c96b2d194e 100644 --- a/target-arm/cpu-qom.h +++ b/target-arm/cpu-qom.h @@ -100,6 +100,11 @@ typedef struct ARMCPU { bool start_powered_off; bool powered_off; + /* PSCI emulation state + * 0 - disabled, 1 - smc, 2 - hvc + */ + uint32_t psci_method; + /* [QEMU_]KVM_ARM_TARGET_* constant for this CPU, or * QEMU_KVM_ARM_TARGET_NONE if the kernel doesn't support this CPU type. */ @@ -191,6 +196,7 @@ extern const struct VMStateDescription vmstate_arm_cpu; void register_cp_regs_for_features(ARMCPU *cpu); void init_cpreg_list(ARMCPU *cpu); +bool arm_handle_psci(CPUState *cs); bool arm_cpu_do_hvc(CPUState *cs); bool arm_cpu_do_smc(CPUState *cs); diff --git a/target-arm/cpu.c b/target-arm/cpu.c index 633a533af716..cbd56be5149b 100644 --- a/target-arm/cpu.c +++ b/target-arm/cpu.c @@ -261,9 +261,12 @@ static void arm_cpu_initfn(Object *obj) cpu->psci_version = 1; /* By default assume PSCI v0.1 */ cpu->kvm_target = QEMU_KVM_ARM_TARGET_NONE; - if (tcg_enabled() && !inited) { - inited = true; - arm_translate_init(); + if (tcg_enabled()) { + cpu->psci_version = 2; /* TCG implements PSCI 0.2 */ + if (!inited) { + inited = true; + arm_translate_init(); + } } } @@ -1017,6 +1020,7 @@ static const ARMCPUInfo arm_cpus[] = { static Property arm_cpu_properties[] = { DEFINE_PROP_BOOL("start-powered-off", ARMCPU, start_powered_off, false), + DEFINE_PROP_UINT32("psci-method", ARMCPU, psci_method, 0), DEFINE_PROP_UINT32("midr", ARMCPU, midr, 0), DEFINE_PROP_END_OF_LIST() }; diff --git a/target-arm/cpu.h b/target-arm/cpu.h index 4c336b342553..645d97357f28 100644 --- a/target-arm/cpu.h +++ b/target-arm/cpu.h @@ -1362,4 +1362,10 @@ static inline void arm_cpu_set_pc(CPUState *cs, vaddr value) } +enum { + QEMU_PSCI_METHOD_DISABLED = 0, + QEMU_PSCI_METHOD_SMC = 1, + QEMU_PSCI_METHOD_HVC = 2, +}; + #endif diff --git a/target-arm/helper.c b/target-arm/helper.c index 3e29d08ae182..ea8d85bd8d53 100644 --- a/target-arm/helper.c +++ b/target-arm/helper.c @@ -3499,11 +3499,23 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs) bool arm_cpu_do_hvc(CPUState *cs) { + ARMCPU *cpu = ARM_CPU(cs); + + if (cpu->psci_method == QEMU_PSCI_METHOD_HVC) { + return arm_handle_psci(cs); + } + return false; } bool arm_cpu_do_smc(CPUState *cs) { + ARMCPU *cpu = ARM_CPU(cs); + + if (cpu->psci_method == QEMU_PSCI_METHOD_SMC) { + return arm_handle_psci(cs); + } + return false; } diff --git a/target-arm/psci.c b/target-arm/psci.c new file mode 100644 index 000000000000..b7ca3e0b1113 --- /dev/null +++ b/target-arm/psci.c @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2014 - Linaro + * Author: Rob Herring + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include +#include +#include +#include +#include + +#if !defined(CONFIG_USER_ONLY) + +bool arm_handle_psci(CPUState *cs) +{ + ARMCPU *cpu = ARM_CPU(cs); + CPUARMState *env = &cpu->env; + uint64_t param[4]; + uint64_t context_id, mpidr; + target_ulong entry; + int32_t ret = 0; + int i; + + for (i = 0; i < 4; i++) { + param[i] = is_a64(env) ? env->xregs[i] : env->regs[i]; + } + + if ((param[0] & PSCI_0_2_64BIT) && !is_a64(env)) { + ret = PSCI_RET_INVALID_PARAMS; + goto err; + } + + switch (param[0]) { + case PSCI_0_2_FN_PSCI_VERSION: + ret = PSCI_VERSION_MAJOR(0) | PSCI_VERSION_MINOR(2); + break; + case PSCI_0_2_FN_MIGRATE_INFO_TYPE: + ret = PSCI_0_2_TOS_MP; /* No trusted OS */ + break; + case PSCI_0_2_FN_AFFINITY_INFO: + case PSCI_0_2_FN64_AFFINITY_INFO: + mpidr = param[1]; + + switch (param[2]) { + case 0: + cs = qemu_get_cpu(mpidr & 0xff); + if (!cs) { + ret = PSCI_RET_INVALID_PARAMS; + break; + } + cpu = ARM_CPU(cs); + ret = cpu->powered_off ? 1 : 0; + break; + default: + /* Everything above affinity level 0 is always on. */ + ret = 0; + } + break; + case PSCI_0_2_FN_SYSTEM_RESET: + qemu_system_reset_request(); + break; + case PSCI_0_2_FN_SYSTEM_OFF: + qemu_system_shutdown_request(); + break; + case QEMU_PSCI_0_1_FN_CPU_ON: + case PSCI_0_2_FN_CPU_ON: + case PSCI_0_2_FN64_CPU_ON: + mpidr = param[1]; + entry = param[2]; + context_id = param[3]; + + /* change to the cpu we are powering up */ + cs = qemu_get_cpu(mpidr & 0xff); + if (!cs) { + ret = PSCI_RET_INVALID_PARAMS; + break; + } + cpu = ARM_CPU(cs); + + if (!cpu->powered_off) { + ret = PSCI_RET_ALREADY_ON; + break; + } + + /* Initialize the cpu we are turning on */ + cpu_reset(cs); + arm_cpu_set_pc(cs, entry); + cpu->powered_off = false; + cs->interrupt_request |= CPU_INTERRUPT_EXITTB; + + /* Set the context_id in r0/x0 */ + if (is_a64(env)) { + cpu->env.xregs[0] = context_id; + } else { + cpu->env.regs[0] = context_id; + } + + ret = 0; + break; + case QEMU_PSCI_0_1_FN_CPU_OFF: + case PSCI_0_2_FN_CPU_OFF: + cpu->powered_off = true; + cs->exit_request = 1; + cs->halted = 1; + + /* CPU_OFF should never return, but if it does return an error */ + ret = PSCI_RET_DENIED; + break; + case QEMU_PSCI_0_1_FN_CPU_SUSPEND: + case PSCI_0_2_FN_CPU_SUSPEND: + case PSCI_0_2_FN64_CPU_SUSPEND: + /* Affinity levels are not supported in QEMU */ + if (param[1] & 0xfffe0000) { + ret = PSCI_RET_INVALID_PARAMS; + break; + } + /* Powerdown is not supported, we always go into WFI */ + cs->halted = 1; + cs->exit_request = 1; + + /* Return success when we wakeup */ + ret = 0; + break; + case QEMU_PSCI_0_1_FN_MIGRATE: + case PSCI_0_2_FN_MIGRATE: + ret = PSCI_RET_NOT_SUPPORTED; + break; + default: + return false; + } + +err: + if (is_a64(env)) { + env->xregs[0] = ret; + } else { + env->regs[0] = ret; + } + return true; +} +#endif