1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (C) 2013 Broadcom Corporation 3 #include <linux/smp.h> 4 #include <linux/io.h> 5 #include <linux/ioport.h> 6 7 #include <asm/cacheflush.h> 8 #include <linux/of_address.h> 9 10 #include "bcm_kona_smc.h" 11 12 static u32 bcm_smc_buffer_phys; /* physical address */ 13 static void __iomem *bcm_smc_buffer; /* virtual address */ 14 15 struct bcm_kona_smc_data { 16 unsigned service_id; 17 unsigned arg0; 18 unsigned arg1; 19 unsigned arg2; 20 unsigned arg3; 21 unsigned result; 22 }; 23 24 static const struct of_device_id bcm_kona_smc_ids[] __initconst = { 25 {.compatible = "brcm,kona-smc"}, 26 {.compatible = "bcm,kona-smc"}, /* deprecated name */ 27 {}, 28 }; 29 30 /* Map in the args buffer area */ 31 int __init bcm_kona_smc_init(void) 32 { 33 struct device_node *node; 34 struct resource res; 35 int ret; 36 37 /* Read buffer addr and size from the device tree node */ 38 node = of_find_matching_node(NULL, bcm_kona_smc_ids); 39 if (!node) 40 return -ENODEV; 41 42 ret = of_address_to_resource(node, 0, &res); 43 of_node_put(node); 44 if (ret) 45 return -EINVAL; 46 47 bcm_smc_buffer = ioremap(res.start, resource_size(&res)); 48 if (!bcm_smc_buffer) 49 return -ENOMEM; 50 bcm_smc_buffer_phys = res.start; 51 52 pr_info("Kona Secure API initialized\n"); 53 54 return 0; 55 } 56 57 /* 58 * int bcm_kona_do_smc(u32 service_id, u32 buffer_addr) 59 * 60 * Only core 0 can run the secure monitor code. If an "smc" request 61 * is initiated on a different core it must be redirected to core 0 62 * for execution. We rely on the caller to handle this. 63 * 64 * Each "smc" request supplies a service id and the address of a 65 * buffer containing parameters related to the service to be 66 * performed. A flags value defines the behavior of the level 2 67 * cache and interrupt handling while the secure monitor executes. 68 * 69 * Parameters to the "smc" request are passed in r4-r6 as follows: 70 * r4 service id 71 * r5 flags (SEC_ROM_*) 72 * r6 physical address of buffer with other parameters 73 * 74 * Execution of an "smc" request produces two distinct results. 75 * 76 * First, the secure monitor call itself (regardless of the specific 77 * service request) can succeed, or can produce an error. When an 78 * "smc" request completes this value is found in r12; it should 79 * always be SEC_EXIT_NORMAL. 80 * 81 * In addition, the particular service performed produces a result. 82 * The values that should be expected depend on the service. We 83 * therefore return this value to the caller, so it can handle the 84 * request result appropriately. This result value is found in r0 85 * when the "smc" request completes. 86 */ 87 static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys) 88 { 89 register u32 ip asm("ip"); /* Also called r12 */ 90 register u32 r0 asm("r0"); 91 register u32 r4 asm("r4"); 92 register u32 r5 asm("r5"); 93 register u32 r6 asm("r6"); 94 95 r4 = service_id; 96 r5 = 0x3; /* Keep IRQ and FIQ off in SM */ 97 r6 = buffer_phys; 98 99 asm volatile ( 100 /* Make sure we got the registers we want */ 101 __asmeq("%0", "ip") 102 __asmeq("%1", "r0") 103 __asmeq("%2", "r4") 104 __asmeq("%3", "r5") 105 __asmeq("%4", "r6") 106 ".arch_extension sec\n" 107 " smc #0\n" 108 : "=r" (ip), "=r" (r0) 109 : "r" (r4), "r" (r5), "r" (r6) 110 : "r1", "r2", "r3", "r7", "lr"); 111 112 BUG_ON(ip != SEC_EXIT_NORMAL); 113 114 return r0; 115 } 116 117 /* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */ 118 static void __bcm_kona_smc(void *info) 119 { 120 struct bcm_kona_smc_data *data = info; 121 u32 __iomem *args = bcm_smc_buffer; 122 123 BUG_ON(smp_processor_id() != 0); 124 BUG_ON(!args); 125 126 /* Copy the four 32 bit argument values into the bounce area */ 127 writel_relaxed(data->arg0, args++); 128 writel_relaxed(data->arg1, args++); 129 writel_relaxed(data->arg2, args++); 130 writel(data->arg3, args); 131 132 /* Flush caches for input data passed to Secure Monitor */ 133 flush_cache_all(); 134 135 /* Trap into Secure Monitor and record the request result */ 136 data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys); 137 } 138 139 unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1, 140 unsigned arg2, unsigned arg3) 141 { 142 struct bcm_kona_smc_data data; 143 144 data.service_id = service_id; 145 data.arg0 = arg0; 146 data.arg1 = arg1; 147 data.arg2 = arg2; 148 data.arg3 = arg3; 149 data.result = 0; 150 151 /* 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