// SPDX-License-Identifier: GPL-2.0-only1// Copyright (C) 2013 Broadcom Corporation2#include <linux/smp.h>3#include <linux/io.h>4#include <linux/ioport.h>56#include <asm/cacheflush.h>7#include <linux/of_address.h>89#include "bcm_kona_smc.h"1011static u32 bcm_smc_buffer_phys; /* physical address */12static void __iomem *bcm_smc_buffer; /* virtual address */1314struct bcm_kona_smc_data {15unsigned service_id;16unsigned arg0;17unsigned arg1;18unsigned arg2;19unsigned arg3;20unsigned result;21};2223static const struct of_device_id bcm_kona_smc_ids[] __initconst = {24{.compatible = "brcm,kona-smc"},25{.compatible = "bcm,kona-smc"}, /* deprecated name */26{},27};2829/* Map in the args buffer area */30int __init bcm_kona_smc_init(void)31{32struct device_node *node;33struct resource res;34int ret;3536/* Read buffer addr and size from the device tree node */37node = of_find_matching_node(NULL, bcm_kona_smc_ids);38if (!node)39return -ENODEV;4041ret = of_address_to_resource(node, 0, &res);42of_node_put(node);43if (ret)44return -EINVAL;4546bcm_smc_buffer = ioremap(res.start, resource_size(&res));47if (!bcm_smc_buffer)48return -ENOMEM;49bcm_smc_buffer_phys = res.start;5051pr_info("Kona Secure API initialized\n");5253return 0;54}5556/*57* int bcm_kona_do_smc(u32 service_id, u32 buffer_addr)58*59* Only core 0 can run the secure monitor code. If an "smc" request60* is initiated on a different core it must be redirected to core 061* for execution. We rely on the caller to handle this.62*63* Each "smc" request supplies a service id and the address of a64* buffer containing parameters related to the service to be65* performed. A flags value defines the behavior of the level 266* cache and interrupt handling while the secure monitor executes.67*68* Parameters to the "smc" request are passed in r4-r6 as follows:69* r4 service id70* r5 flags (SEC_ROM_*)71* r6 physical address of buffer with other parameters72*73* Execution of an "smc" request produces two distinct results.74*75* First, the secure monitor call itself (regardless of the specific76* service request) can succeed, or can produce an error. When an77* "smc" request completes this value is found in r12; it should78* always be SEC_EXIT_NORMAL.79*80* In addition, the particular service performed produces a result.81* The values that should be expected depend on the service. We82* therefore return this value to the caller, so it can handle the83* request result appropriately. This result value is found in r084* when the "smc" request completes.85*/86static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys)87{88register u32 ip asm("ip"); /* Also called r12 */89register u32 r0 asm("r0");90register u32 r4 asm("r4");91register u32 r5 asm("r5");92register u32 r6 asm("r6");9394r4 = service_id;95r5 = 0x3; /* Keep IRQ and FIQ off in SM */96r6 = buffer_phys;9798asm volatile (99/* Make sure we got the registers we want */100__asmeq("%0", "ip")101__asmeq("%1", "r0")102__asmeq("%2", "r4")103__asmeq("%3", "r5")104__asmeq("%4", "r6")105".arch_extension sec\n"106" smc #0\n"107: "=r" (ip), "=r" (r0)108: "r" (r4), "r" (r5), "r" (r6)109: "r1", "r2", "r3", "r7", "lr");110111BUG_ON(ip != SEC_EXIT_NORMAL);112113return r0;114}115116/* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */117static void __bcm_kona_smc(void *info)118{119struct bcm_kona_smc_data *data = info;120u32 __iomem *args = bcm_smc_buffer;121122BUG_ON(smp_processor_id() != 0);123BUG_ON(!args);124125/* Copy the four 32 bit argument values into the bounce area */126writel_relaxed(data->arg0, args++);127writel_relaxed(data->arg1, args++);128writel_relaxed(data->arg2, args++);129writel(data->arg3, args);130131/* Flush caches for input data passed to Secure Monitor */132flush_cache_all();133134/* Trap into Secure Monitor and record the request result */135data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys);136}137138unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1,139unsigned arg2, unsigned arg3)140{141struct bcm_kona_smc_data data;142143data.service_id = service_id;144data.arg0 = arg0;145data.arg1 = arg1;146data.arg2 = arg2;147data.arg3 = arg3;148data.result = 0;149150/*151* Due to a limitation of the secure monitor, we must use the SMP152* infrastructure to forward all secure monitor calls to Core 0.153*/154smp_call_function_single(0, __bcm_kona_smc, &data, 1);155156return data.result;157}158159160