1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2013 Broadcom Corporation
5 #include <linux/ioport.h>
7 #include <asm/cacheflush.h>
8 #include <linux/of_address.h>
10 #include "bcm_kona_smc.h"
12 static u32 bcm_smc_buffer_phys; /* physical address */
13 static void __iomem *bcm_smc_buffer; /* virtual address */
15 struct bcm_kona_smc_data {
24 static const struct of_device_id bcm_kona_smc_ids[] __initconst = {
25 {.compatible = "brcm,kona-smc"},
26 {.compatible = "bcm,kona-smc"}, /* deprecated name */
30 /* Map in the args buffer area */
31 int __init bcm_kona_smc_init(void)
33 struct device_node *node;
37 /* Read buffer addr and size from the device tree node */
38 node = of_find_matching_node(NULL, bcm_kona_smc_ids);
42 ret = of_address_to_resource(node, 0, &res);
47 bcm_smc_buffer = ioremap(res.start, resource_size(&res));
50 bcm_smc_buffer_phys = res.start;
52 pr_info("Kona Secure API initialized\n");
58 * int bcm_kona_do_smc(u32 service_id, u32 buffer_addr)
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.
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.
69 * Parameters to the "smc" request are passed in r4-r6 as follows:
71 * r5 flags (SEC_ROM_*)
72 * r6 physical address of buffer with other parameters
74 * Execution of an "smc" request produces two distinct results.
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.
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.
87 static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys)
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");
96 r5 = 0x3; /* Keep IRQ and FIQ off in SM */
100 /* Make sure we got the registers we want */
106 ".arch_extension sec\n"
108 : "=r" (ip), "=r" (r0)
109 : "r" (r4), "r" (r5), "r" (r6)
110 : "r1", "r2", "r3", "r7", "lr");
112 BUG_ON(ip != SEC_EXIT_NORMAL);
117 /* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */
118 static void __bcm_kona_smc(void *info)
120 struct bcm_kona_smc_data *data = info;
121 u32 __iomem *args = bcm_smc_buffer;
123 BUG_ON(smp_processor_id() != 0);
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);
132 /* Flush caches for input data passed to Secure Monitor */
135 /* Trap into Secure Monitor and record the request result */
136 data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys);
139 unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1,
140 unsigned arg2, unsigned arg3)
142 struct bcm_kona_smc_data data;
144 data.service_id = service_id;
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.
155 smp_call_function_single(0, __bcm_kona_smc, &data, 1);