From patchwork Fri Sep 5 12:24:56 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 36842 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ig0-f197.google.com (mail-ig0-f197.google.com [209.85.213.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 408C3202A1 for ; Fri, 5 Sep 2014 12:28:08 +0000 (UTC) Received: by mail-ig0-f197.google.com with SMTP id r2sf55015297igi.8 for ; Fri, 05 Sep 2014 05:28:07 -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=tmf2YCeTFer78Zn703sB44reUHyN1F3+gRVu/UND8Hw=; b=YYejKKsRItRUTQ1LSuyo3vSe0srJyW4CvfscBXK/m9Z09bKTJfSAAd6Y7ZMOYF1y8N x37IYbfR4deNpKGGsiyEXaJJe9xjF1Ne3HLDrmJCEBNUgNNSzbx5eiNJ1ksTvc8j8AQ8 DzTKh0C2Ll0iT6goiZA4XUQoXp22ubSiwi8BY62euwKyruIrbD/2KzyFkr1yBm7DRonm 2eEBDH8TOHALpj1rZ3qJ8+kHMx18XwWeuNcX0hNBsaB5GQ3k+fbjuygeaVkiKi67vf8x 0HhzJEUr+pSlLjJxorMCmuvDazdGRIZ/vpLBQW/2Zep6yCfVHBj+i2T/I/Virx3bDkNu pDHQ== X-Gm-Message-State: ALoCoQnk8hZEjEASENSzQHdrVSY9X/gIhHkhbZG7ZfghclTfyrMhG4NfLxKql0g8FY9nCEtHE9gm X-Received: by 10.182.91.69 with SMTP id cc5mr6798048obb.33.1409920087704; Fri, 05 Sep 2014 05:28:07 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.30.55 with SMTP id c52ls739141qgc.8.gmail; Fri, 05 Sep 2014 05:28:07 -0700 (PDT) X-Received: by 10.221.66.194 with SMTP id xr2mr365598vcb.65.1409920087522; Fri, 05 Sep 2014 05:28:07 -0700 (PDT) Received: from mail-vc0-f172.google.com (mail-vc0-f172.google.com [209.85.220.172]) by mx.google.com with ESMTPS id f2si405843vcy.28.2014.09.05.05.28.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 05 Sep 2014 05:28:07 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) client-ip=209.85.220.172; Received: by mail-vc0-f172.google.com with SMTP id le20so856142vcb.31 for ; Fri, 05 Sep 2014 05:28:07 -0700 (PDT) X-Received: by 10.52.120.51 with SMTP id kz19mr125031vdb.95.1409920087443; Fri, 05 Sep 2014 05:28:07 -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 uj3csp75023vcb; Fri, 5 Sep 2014 05:28:06 -0700 (PDT) X-Received: by 10.229.33.202 with SMTP id i10mr18113055qcd.2.1409920086690; Fri, 05 Sep 2014 05:28:06 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id k47si1370305qgd.59.2014.09.05.05.28.06 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 05 Sep 2014 05:28:06 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Received: from localhost ([::1]:57495 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPscY-0003TE-2K for patch@linaro.org; Fri, 05 Sep 2014 08:28:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54784) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPsZn-0008G3-RP for qemu-devel@nongnu.org; Fri, 05 Sep 2014 08:25:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XPsZg-0006Q5-Ux for qemu-devel@nongnu.org; Fri, 05 Sep 2014 08:25:15 -0400 Received: from mail-wi0-f170.google.com ([209.85.212.170]:42753) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPsZg-0006Ps-Mp for qemu-devel@nongnu.org; Fri, 05 Sep 2014 08:25:08 -0400 Received: by mail-wi0-f170.google.com with SMTP id cc10so691142wib.3 for ; Fri, 05 Sep 2014 05:25:07 -0700 (PDT) X-Received: by 10.181.5.3 with SMTP id ci3mr3280241wid.57.1409919907339; Fri, 05 Sep 2014 05:25:07 -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 mv14sm1807558wic.20.2014.09.05.05.25.06 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 05 Sep 2014 05:25:06 -0700 (PDT) From: Ard Biesheuvel To: peter.maydell@linaro.org, christoffer.dall@linaro.org, qemu-devel@nongnu.org Date: Fri, 5 Sep 2014 14:24:56 +0200 Message-Id: <1409919897-360-6-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1409919897-360-1-git-send-email-ard.biesheuvel@linaro.org> References: <1409919897-360-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: 209.85.212.170 Cc: Rob Herring , Ard Biesheuvel Subject: [Qemu-devel] [PATCH v3 5/6] 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.172 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 | 171 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 target-arm/psci.c diff --git a/target-arm/Makefile.objs b/target-arm/Makefile.objs index dcd167e0d880..9460b409a5a1 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-$(CONFIG_SOFTMMU) += 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 104cc67e82d2..469fefb3fec8 100644 --- a/target-arm/cpu-qom.h +++ b/target-arm/cpu-qom.h @@ -101,6 +101,11 @@ typedef struct ARMCPU { /* CPU currently in PSCI powered-off state */ 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. */ @@ -192,6 +197,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 b4c06c17cf87..df094fdf5359 100644 --- a/target-arm/cpu.c +++ b/target-arm/cpu.c @@ -268,9 +268,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(); + } } } @@ -1024,6 +1027,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 d235929f4c12..2624117e58d3 100644 --- a/target-arm/cpu.h +++ b/target-arm/cpu.h @@ -1350,4 +1350,10 @@ static inline void cpu_pc_from_tb(CPUARMState *env, TranslationBlock *tb) } } +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 440ee07a2ff8..021f8ed2c45f 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..78efa06b1b00 --- /dev/null +++ b/target-arm/psci.c @@ -0,0 +1,171 @@ +/* + * 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 + +bool arm_handle_psci(CPUState *cs) +{ + /* + * This function partially implements the logic for dispatching Power State + * Coordination Interface (PSCI) calls (as described in ARM DEN 0022B.b), + * to the extent required for bringing up and taking down secondary cores, + * and for handling reset and poweroff requests. + * Additional information about the calling convention used is available in + * the document 'SMC Calling Convention' (ARM DEN 0028) + */ + 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++) { + /* + * All PSCI functions take explicit 32-bit or native int sized + * arguments so we can simply zero-extend all arguments regardless + * of which exact function we are about to call. + */ + param[i] = is_a64(env) ? env->xregs[i] : env->regs[i]; + } + + if ((param[0] & QEMU_PSCI_0_2_64BIT) && !is_a64(env)) { + ret = QEMU_PSCI_RET_INVALID_PARAMS; + goto err; + } + + switch (param[0]) { + CPUState *target_cpu_state; + ARMCPU *target_cpu; + CPUClass *target_cpu_class; + + case QEMU_PSCI_0_2_FN_PSCI_VERSION: + ret = QEMU_PSCI_0_2_RET_VERSION_0_2; + break; + case QEMU_PSCI_0_2_FN_MIGRATE_INFO_TYPE: + ret = QEMU_PSCI_0_2_RET_TOS_MIGRATION_NOT_REQUIRED; /* No trusted OS */ + break; + case QEMU_PSCI_0_2_FN_AFFINITY_INFO: + case QEMU_PSCI_0_2_FN64_AFFINITY_INFO: + mpidr = param[1]; + + switch (param[2]) { + case 0: + target_cpu_state = qemu_get_cpu(mpidr & 0xff); + if (!target_cpu_state) { + ret = QEMU_PSCI_RET_INVALID_PARAMS; + break; + } + target_cpu = ARM_CPU(target_cpu_state); + ret = target_cpu->powered_off ? 1 : 0; + break; + default: + /* Everything above affinity level 0 is always on. */ + ret = 0; + } + break; + case QEMU_PSCI_0_2_FN_SYSTEM_RESET: + qemu_system_reset_request(); + break; + case QEMU_PSCI_0_2_FN_SYSTEM_OFF: + qemu_system_shutdown_request(); + break; + case QEMU_PSCI_0_1_FN_CPU_ON: + case QEMU_PSCI_0_2_FN_CPU_ON: + case QEMU_PSCI_0_2_FN64_CPU_ON: + mpidr = param[1]; + entry = param[2]; + context_id = param[3]; + + /* change to the cpu we are powering up */ + target_cpu_state = qemu_get_cpu(mpidr & 0xff); + if (!target_cpu_state) { + ret = QEMU_PSCI_RET_INVALID_PARAMS; + break; + } + target_cpu = ARM_CPU(target_cpu_state); + if (!target_cpu->powered_off) { + ret = QEMU_PSCI_RET_ALREADY_ON; + break; + } + target_cpu_class = CPU_GET_CLASS(target_cpu); + + /* Initialize the cpu we are turning on */ + cpu_reset(target_cpu_state); + target_cpu_class->set_pc(target_cpu_state, entry); + target_cpu->powered_off = false; + target_cpu_state->interrupt_request |= CPU_INTERRUPT_EXITTB; + + /* + * The PSCI spec mandates that newly brought up CPUs enter the + * exception level of the caller in the same execution mode as + * the caller, with context_id in x0/r0, respectively. + */ + if (is_a64(env)) { + target_cpu->env.aarch64 = 1; + target_cpu->env.xregs[0] = context_id; + } else { + target_cpu->env.aarch64 = 0; + target_cpu->env.regs[0] = context_id; + } + + ret = 0; + break; + case QEMU_PSCI_0_1_FN_CPU_OFF: + case QEMU_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 = QEMU_PSCI_RET_DENIED; + break; + case QEMU_PSCI_0_1_FN_CPU_SUSPEND: + case QEMU_PSCI_0_2_FN_CPU_SUSPEND: + case QEMU_PSCI_0_2_FN64_CPU_SUSPEND: + /* Affinity levels are not supported in QEMU */ + if (param[1] & 0xfffe0000) { + ret = QEMU_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 QEMU_PSCI_0_2_FN_MIGRATE: + ret = QEMU_PSCI_RET_NOT_SUPPORTED; + break; + default: + return false; + } + +err: + if (is_a64(env)) { + env->xregs[0] = ret; + } else { + env->regs[0] = ret; + } + return true; +}