2 * Copyright (C) 2013 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/smp.h>
17 #include <linux/ioport.h>
19 #include <asm/cacheflush.h>
20 #include <linux/of_address.h>
22 #include "bcm_kona_smc.h"
24 static u32 bcm_smc_buffer_phys; /* physical address */
25 static void __iomem *bcm_smc_buffer; /* virtual address */
27 struct bcm_kona_smc_data {
36 static const struct of_device_id bcm_kona_smc_ids[] __initconst = {
37 {.compatible = "brcm,kona-smc"},
38 {.compatible = "bcm,kona-smc"}, /* deprecated name */
42 /* Map in the args buffer area */
43 int __init bcm_kona_smc_init(void)
45 struct device_node *node;
46 const __be32 *prop_val;
48 unsigned long buffer_size;
51 /* Read buffer addr and size from the device tree node */
52 node = of_find_matching_node(NULL, bcm_kona_smc_ids);
56 prop_val = of_get_address(node, 0, &prop_size, NULL);
61 /* We assume space for four 32-bit arguments */
62 if (prop_size < 4 * sizeof(u32) || prop_size > (u64)ULONG_MAX)
64 buffer_size = (unsigned long)prop_size;
66 buffer_phys = be32_to_cpup(prop_val);
70 bcm_smc_buffer = ioremap(buffer_phys, buffer_size);
73 bcm_smc_buffer_phys = buffer_phys;
75 pr_info("Kona Secure API initialized\n");
81 * int bcm_kona_do_smc(u32 service_id, u32 buffer_addr)
83 * Only core 0 can run the secure monitor code. If an "smc" request
84 * is initiated on a different core it must be redirected to core 0
85 * for execution. We rely on the caller to handle this.
87 * Each "smc" request supplies a service id and the address of a
88 * buffer containing parameters related to the service to be
89 * performed. A flags value defines the behavior of the level 2
90 * cache and interrupt handling while the secure monitor executes.
92 * Parameters to the "smc" request are passed in r4-r6 as follows:
94 * r5 flags (SEC_ROM_*)
95 * r6 physical address of buffer with other parameters
97 * Execution of an "smc" request produces two distinct results.
99 * First, the secure monitor call itself (regardless of the specific
100 * service request) can succeed, or can produce an error. When an
101 * "smc" request completes this value is found in r12; it should
102 * always be SEC_EXIT_NORMAL.
104 * In addition, the particular service performed produces a result.
105 * The values that should be expected depend on the service. We
106 * therefore return this value to the caller, so it can handle the
107 * request result appropriately. This result value is found in r0
108 * when the "smc" request completes.
110 static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys)
112 register u32 ip asm("ip"); /* Also called r12 */
113 register u32 r0 asm("r0");
114 register u32 r4 asm("r4");
115 register u32 r5 asm("r5");
116 register u32 r6 asm("r6");
119 r5 = 0x3; /* Keep IRQ and FIQ off in SM */
123 /* Make sure we got the registers we want */
129 ".arch_extension sec\n"
131 : "=r" (ip), "=r" (r0)
132 : "r" (r4), "r" (r5), "r" (r6)
133 : "r1", "r2", "r3", "r7", "lr");
135 BUG_ON(ip != SEC_EXIT_NORMAL);
140 /* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */
141 static void __bcm_kona_smc(void *info)
143 struct bcm_kona_smc_data *data = info;
144 u32 __iomem *args = bcm_smc_buffer;
146 BUG_ON(smp_processor_id() != 0);
149 /* Copy the four 32 bit argument values into the bounce area */
150 writel_relaxed(data->arg0, args++);
151 writel_relaxed(data->arg1, args++);
152 writel_relaxed(data->arg2, args++);
153 writel(data->arg3, args);
155 /* Flush caches for input data passed to Secure Monitor */
158 /* Trap into Secure Monitor and record the request result */
159 data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys);
162 unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1,
163 unsigned arg2, unsigned arg3)
165 struct bcm_kona_smc_data data;
167 data.service_id = service_id;
175 * Due to a limitation of the secure monitor, we must use the SMP
176 * infrastructure to forward all secure monitor calls to Core 0.
178 smp_call_function_single(0, __bcm_kona_smc, &data, 1);