1152942Sdavidxu// SPDX-License-Identifier: GPL-2.0-only 2152942Sdavidxu// Copyright (C) 2013 Broadcom Corporation 3152942Sdavidxu#include <linux/smp.h> 4152942Sdavidxu#include <linux/io.h> 5152942Sdavidxu#include <linux/ioport.h> 6152942Sdavidxu 7152942Sdavidxu#include <asm/cacheflush.h> 8152942Sdavidxu#include <linux/of_address.h> 9152942Sdavidxu 10152942Sdavidxu#include "bcm_kona_smc.h" 11152942Sdavidxu 12152942Sdavidxustatic u32 bcm_smc_buffer_phys; /* physical address */ 13152942Sdavidxustatic void __iomem *bcm_smc_buffer; /* virtual address */ 14152942Sdavidxu 15152942Sdavidxustruct bcm_kona_smc_data { 16152942Sdavidxu unsigned service_id; 17152942Sdavidxu unsigned arg0; 18152942Sdavidxu unsigned arg1; 19152942Sdavidxu unsigned arg2; 20152942Sdavidxu unsigned arg3; 21152942Sdavidxu unsigned result; 22152942Sdavidxu}; 23152942Sdavidxu 24152942Sdavidxustatic const struct of_device_id bcm_kona_smc_ids[] __initconst = { 25152942Sdavidxu {.compatible = "brcm,kona-smc"}, 26152942Sdavidxu {.compatible = "bcm,kona-smc"}, /* deprecated name */ 27152942Sdavidxu {}, 28176661Sphilip}; 29176661Sphilip 30176661Sphilip/* Map in the args buffer area */ 31176661Sphilipint __init bcm_kona_smc_init(void) 32176661Sphilip{ 33176661Sphilip struct device_node *node; 34176661Sphilip struct resource res; 35176661Sphilip int ret; 36176661Sphilip 37176443Sphilip /* Read buffer addr and size from the device tree node */ 38152942Sdavidxu node = of_find_matching_node(NULL, bcm_kona_smc_ids); 39152942Sdavidxu if (!node) 40152942Sdavidxu return -ENODEV; 41152942Sdavidxu 42152942Sdavidxu ret = of_address_to_resource(node, 0, &res); 43152942Sdavidxu of_node_put(node); 44152942Sdavidxu if (ret) 45152942Sdavidxu return -EINVAL; 46152942Sdavidxu 47170904Sdavidxu bcm_smc_buffer = ioremap(res.start, resource_size(&res)); 48152942Sdavidxu if (!bcm_smc_buffer) 49152942Sdavidxu return -ENOMEM; 50152942Sdavidxu bcm_smc_buffer_phys = res.start; 51152942Sdavidxu 52152942Sdavidxu pr_info("Kona Secure API initialized\n"); 53152942Sdavidxu 54152942Sdavidxu return 0; 55153018Sdavidxu} 56152942Sdavidxu 57153036Sdavidxu/* 58153036Sdavidxu * int bcm_kona_do_smc(u32 service_id, u32 buffer_addr) 59153036Sdavidxu * 60153036Sdavidxu * Only core 0 can run the secure monitor code. If an "smc" request 61153036Sdavidxu * is initiated on a different core it must be redirected to core 0 62152942Sdavidxu * for execution. We rely on the caller to handle this. 63152942Sdavidxu * 64153036Sdavidxu * Each "smc" request supplies a service id and the address of a 65153036Sdavidxu * buffer containing parameters related to the service to be 66152942Sdavidxu * performed. A flags value defines the behavior of the level 2 67152942Sdavidxu * cache and interrupt handling while the secure monitor executes. 68152942Sdavidxu * 69153101Sdavidxu * Parameters to the "smc" request are passed in r4-r6 as follows: 70153101Sdavidxu * r4 service id 71153101Sdavidxu * r5 flags (SEC_ROM_*) 72153101Sdavidxu * r6 physical address of buffer with other parameters 73153101Sdavidxu * 74153109Sdavidxu * Execution of an "smc" request produces two distinct results. 75153109Sdavidxu * 76153109Sdavidxu * First, the secure monitor call itself (regardless of the specific 77153101Sdavidxu * service request) can succeed, or can produce an error. When an 78153101Sdavidxu * "smc" request completes this value is found in r12; it should 79153101Sdavidxu * always be SEC_EXIT_NORMAL. 80153109Sdavidxu * 81153109Sdavidxu * In addition, the particular service performed produces a result. 82153109Sdavidxu * The values that should be expected depend on the service. We 83153109Sdavidxu * therefore return this value to the caller, so it can handle the 84153109Sdavidxu * request result appropriately. This result value is found in r0 85153109Sdavidxu * when the "smc" request completes. 86153109Sdavidxu */ 87153101Sdavidxustatic int bcm_kona_do_smc(u32 service_id, u32 buffer_phys) 88153161Sdavidxu{ 89153161Sdavidxu register u32 ip asm("ip"); /* Also called r12 */ 90153109Sdavidxu register u32 r0 asm("r0"); 91153109Sdavidxu register u32 r4 asm("r4"); 92153101Sdavidxu register u32 r5 asm("r5"); 93153036Sdavidxu register u32 r6 asm("r6"); 94153036Sdavidxu 95153036Sdavidxu r4 = service_id; 96152942Sdavidxu r5 = 0x3; /* Keep IRQ and FIQ off in SM */ 97152942Sdavidxu r6 = buffer_phys; 98152942Sdavidxu 99152942Sdavidxu asm volatile ( 100152942Sdavidxu /* Make sure we got the registers we want */ 101153036Sdavidxu __asmeq("%0", "ip") 102153036Sdavidxu __asmeq("%1", "r0") 103152942Sdavidxu __asmeq("%2", "r4") 104152942Sdavidxu __asmeq("%3", "r5") 105152942Sdavidxu __asmeq("%4", "r6") 106152942Sdavidxu ".arch_extension sec\n" 107152942Sdavidxu " smc #0\n" 108152942Sdavidxu : "=r" (ip), "=r" (r0) 109152942Sdavidxu : "r" (r4), "r" (r5), "r" (r6) 110152942Sdavidxu : "r1", "r2", "r3", "r7", "lr"); 111152942Sdavidxu 112152942Sdavidxu BUG_ON(ip != SEC_EXIT_NORMAL); 113153036Sdavidxu 114152942Sdavidxu return r0; 115152942Sdavidxu} 116152942Sdavidxu 117153018Sdavidxu/* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */ 118152942Sdavidxustatic void __bcm_kona_smc(void *info) 119152942Sdavidxu{ 120152942Sdavidxu struct bcm_kona_smc_data *data = info; 121152942Sdavidxu u32 __iomem *args = bcm_smc_buffer; 122152942Sdavidxu 123152942Sdavidxu BUG_ON(smp_processor_id() != 0); 124152942Sdavidxu BUG_ON(!args); 125153036Sdavidxu 126152942Sdavidxu /* Copy the four 32 bit argument values into the bounce area */ 127152942Sdavidxu writel_relaxed(data->arg0, args++); 128152942Sdavidxu writel_relaxed(data->arg1, args++); 129152942Sdavidxu writel_relaxed(data->arg2, args++); 130153370Sdavidxu writel(data->arg3, args); 131153370Sdavidxu 132152942Sdavidxu /* Flush caches for input data passed to Secure Monitor */ 133152942Sdavidxu flush_cache_all(); 134152942Sdavidxu 135153036Sdavidxu /* Trap into Secure Monitor and record the request result */ 136152942Sdavidxu data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys); 137152942Sdavidxu} 138153036Sdavidxu 139153036Sdavidxuunsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1, 140153036Sdavidxu unsigned arg2, unsigned arg3) 141152942Sdavidxu{ 142176443Sphilip struct bcm_kona_smc_data data; 143176443Sphilip 144176661Sphilip data.service_id = service_id; 145176661Sphilip data.arg0 = arg0; 146176661Sphilip data.arg1 = arg1; 147176661Sphilip data.arg2 = arg2; 148176661Sphilip data.arg3 = arg3; 149176661Sphilip data.result = 0; 150176661Sphilip 151208914Suqs /* 152 * Due to a limitation of the secure monitor, we must use the SMP 153 * infrastructure to forward all secure monitor calls to Core 0. 154 */ 155 smp_call_function_single(0, __bcm_kona_smc, &data, 1); 156 157 return data.result; 158} 159