xref: /linux/arch/arm/mach-bcm/bcm_kona_smc.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
152e6676eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
252e6676eSThomas Gleixner // Copyright (C) 2013 Broadcom Corporation
3b8eb35fdSChristian Daudt #include <linux/smp.h>
4b8eb35fdSChristian Daudt #include <linux/io.h>
5b8eb35fdSChristian Daudt #include <linux/ioport.h>
6b8eb35fdSChristian Daudt 
7b8eb35fdSChristian Daudt #include <asm/cacheflush.h>
8b8eb35fdSChristian Daudt #include <linux/of_address.h>
9b8eb35fdSChristian Daudt 
10b8eb35fdSChristian Daudt #include "bcm_kona_smc.h"
11b8eb35fdSChristian Daudt 
12c64756ccSAlex Elder static u32		bcm_smc_buffer_phys;	/* physical address */
13c64756ccSAlex Elder static void __iomem	*bcm_smc_buffer;	/* virtual address */
14b8eb35fdSChristian Daudt 
15b8eb35fdSChristian Daudt struct bcm_kona_smc_data {
16b8eb35fdSChristian Daudt 	unsigned service_id;
17b8eb35fdSChristian Daudt 	unsigned arg0;
18b8eb35fdSChristian Daudt 	unsigned arg1;
19b8eb35fdSChristian Daudt 	unsigned arg2;
20b8eb35fdSChristian Daudt 	unsigned arg3;
216c90f108SAlex Elder 	unsigned result;
22b8eb35fdSChristian Daudt };
23b8eb35fdSChristian Daudt 
240527873bSArnd Bergmann static const struct of_device_id bcm_kona_smc_ids[] __initconst = {
25aea237bfSChristian Daudt 	{.compatible = "brcm,kona-smc"},
26aea237bfSChristian Daudt 	{.compatible = "bcm,kona-smc"}, /* deprecated name */
27b8eb35fdSChristian Daudt 	{},
28b8eb35fdSChristian Daudt };
29b8eb35fdSChristian Daudt 
30c64756ccSAlex Elder /* Map in the args buffer area */
bcm_kona_smc_init(void)313a76b351SChristian Daudt int __init bcm_kona_smc_init(void)
32b8eb35fdSChristian Daudt {
33b8eb35fdSChristian Daudt 	struct device_node *node;
34*3c01b054SRob Herring 	struct resource res;
35*3c01b054SRob Herring 	int ret;
36b8eb35fdSChristian Daudt 
37b8eb35fdSChristian Daudt 	/* Read buffer addr and size from the device tree node */
38b8eb35fdSChristian Daudt 	node = of_find_matching_node(NULL, bcm_kona_smc_ids);
393a76b351SChristian Daudt 	if (!node)
403a76b351SChristian Daudt 		return -ENODEV;
41b8eb35fdSChristian Daudt 
42*3c01b054SRob Herring 	ret = of_address_to_resource(node, 0, &res);
43cb23389aSMiaoqian Lin 	of_node_put(node);
44*3c01b054SRob Herring 	if (ret)
455c4cee2fSAlex Elder 		return -EINVAL;
465c4cee2fSAlex Elder 
47*3c01b054SRob Herring 	bcm_smc_buffer = ioremap(res.start, resource_size(&res));
48c64756ccSAlex Elder 	if (!bcm_smc_buffer)
495c4cee2fSAlex Elder 		return -ENOMEM;
50*3c01b054SRob Herring 	bcm_smc_buffer_phys = res.start;
51b8eb35fdSChristian Daudt 
523a76b351SChristian Daudt 	pr_info("Kona Secure API initialized\n");
533a76b351SChristian Daudt 
543a76b351SChristian Daudt 	return 0;
55b8eb35fdSChristian Daudt }
56b8eb35fdSChristian Daudt 
578b9c550eSAlex Elder /*
58ed24f446SAlex Elder  * int bcm_kona_do_smc(u32 service_id, u32 buffer_addr)
59ed24f446SAlex Elder  *
60ed24f446SAlex Elder  * Only core 0 can run the secure monitor code.  If an "smc" request
61ed24f446SAlex Elder  * is initiated on a different core it must be redirected to core 0
62ed24f446SAlex Elder  * for execution.  We rely on the caller to handle this.
63ed24f446SAlex Elder  *
64ed24f446SAlex Elder  * Each "smc" request supplies a service id and the address of a
65ed24f446SAlex Elder  * buffer containing parameters related to the service to be
66ed24f446SAlex Elder  * performed.  A flags value defines the behavior of the level 2
67ed24f446SAlex Elder  * cache and interrupt handling while the secure monitor executes.
68ed24f446SAlex Elder  *
69ed24f446SAlex Elder  * Parameters to the "smc" request are passed in r4-r6 as follows:
70ed24f446SAlex Elder  *     r4	service id
71ed24f446SAlex Elder  *     r5	flags (SEC_ROM_*)
72ed24f446SAlex Elder  *     r6	physical address of buffer with other parameters
73ed24f446SAlex Elder  *
74ed24f446SAlex Elder  * Execution of an "smc" request produces two distinct results.
75ed24f446SAlex Elder  *
76ed24f446SAlex Elder  * First, the secure monitor call itself (regardless of the specific
77ed24f446SAlex Elder  * service request) can succeed, or can produce an error.  When an
78ed24f446SAlex Elder  * "smc" request completes this value is found in r12; it should
79ed24f446SAlex Elder  * always be SEC_EXIT_NORMAL.
80ed24f446SAlex Elder  *
81ed24f446SAlex Elder  * In addition, the particular service performed produces a result.
82ed24f446SAlex Elder  * The values that should be expected depend on the service.  We
83ed24f446SAlex Elder  * therefore return this value to the caller, so it can handle the
84ed24f446SAlex Elder  * request result appropriately.  This result value is found in r0
85ed24f446SAlex Elder  * when the "smc" request completes.
868b9c550eSAlex Elder  */
bcm_kona_do_smc(u32 service_id,u32 buffer_phys)878b9c550eSAlex Elder static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys)
888b9c550eSAlex Elder {
898b9c550eSAlex Elder 	register u32 ip asm("ip");	/* Also called r12 */
908b9c550eSAlex Elder 	register u32 r0 asm("r0");
918b9c550eSAlex Elder 	register u32 r4 asm("r4");
928b9c550eSAlex Elder 	register u32 r5 asm("r5");
938b9c550eSAlex Elder 	register u32 r6 asm("r6");
948b9c550eSAlex Elder 
958b9c550eSAlex Elder 	r4 = service_id;
968b9c550eSAlex Elder 	r5 = 0x3;		/* Keep IRQ and FIQ off in SM */
978b9c550eSAlex Elder 	r6 = buffer_phys;
988b9c550eSAlex Elder 
998b9c550eSAlex Elder 	asm volatile (
1008b9c550eSAlex Elder 		/* Make sure we got the registers we want */
1018b9c550eSAlex Elder 		__asmeq("%0", "ip")
1028b9c550eSAlex Elder 		__asmeq("%1", "r0")
1038b9c550eSAlex Elder 		__asmeq("%2", "r4")
1048b9c550eSAlex Elder 		__asmeq("%3", "r5")
1058b9c550eSAlex Elder 		__asmeq("%4", "r6")
1068b9c550eSAlex Elder 		".arch_extension sec\n"
1078b9c550eSAlex Elder 		"	smc    #0\n"
1088b9c550eSAlex Elder 		: "=r" (ip), "=r" (r0)
1098b9c550eSAlex Elder 		: "r" (r4), "r" (r5), "r" (r6)
1108b9c550eSAlex Elder 		: "r1", "r2", "r3", "r7", "lr");
1118b9c550eSAlex Elder 
1128b9c550eSAlex Elder 	BUG_ON(ip != SEC_EXIT_NORMAL);
1138b9c550eSAlex Elder 
1148b9c550eSAlex Elder 	return r0;
1158b9c550eSAlex Elder }
1168b9c550eSAlex Elder 
117b8eb35fdSChristian Daudt /* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */
__bcm_kona_smc(void * info)118b8eb35fdSChristian Daudt static void __bcm_kona_smc(void *info)
119b8eb35fdSChristian Daudt {
120b8eb35fdSChristian Daudt 	struct bcm_kona_smc_data *data = info;
121b47879aaSBen Dooks 	u32 __iomem *args = bcm_smc_buffer;
122b8eb35fdSChristian Daudt 
123b8eb35fdSChristian Daudt 	BUG_ON(smp_processor_id() != 0);
124c64756ccSAlex Elder 	BUG_ON(!args);
125b8eb35fdSChristian Daudt 
126e80eef33SAlex Elder 	/* Copy the four 32 bit argument values into the bounce area */
127e80eef33SAlex Elder 	writel_relaxed(data->arg0, args++);
128e80eef33SAlex Elder 	writel_relaxed(data->arg1, args++);
129e80eef33SAlex Elder 	writel_relaxed(data->arg2, args++);
130e80eef33SAlex Elder 	writel(data->arg3, args);
131b8eb35fdSChristian Daudt 
132b8eb35fdSChristian Daudt 	/* Flush caches for input data passed to Secure Monitor */
133b8eb35fdSChristian Daudt 	flush_cache_all();
134b8eb35fdSChristian Daudt 
1356c90f108SAlex Elder 	/* Trap into Secure Monitor and record the request result */
1368b9c550eSAlex Elder 	data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys);
137b8eb35fdSChristian Daudt }
138b8eb35fdSChristian Daudt 
bcm_kona_smc(unsigned service_id,unsigned arg0,unsigned arg1,unsigned arg2,unsigned arg3)139b8eb35fdSChristian Daudt unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1,
140b8eb35fdSChristian Daudt 		  unsigned arg2, unsigned arg3)
141b8eb35fdSChristian Daudt {
142b8eb35fdSChristian Daudt 	struct bcm_kona_smc_data data;
143b8eb35fdSChristian Daudt 
144b8eb35fdSChristian Daudt 	data.service_id = service_id;
145b8eb35fdSChristian Daudt 	data.arg0 = arg0;
146b8eb35fdSChristian Daudt 	data.arg1 = arg1;
147b8eb35fdSChristian Daudt 	data.arg2 = arg2;
148b8eb35fdSChristian Daudt 	data.arg3 = arg3;
1496c90f108SAlex Elder 	data.result = 0;
150b8eb35fdSChristian Daudt 
151b8eb35fdSChristian Daudt 	/*
152b8eb35fdSChristian Daudt 	 * Due to a limitation of the secure monitor, we must use the SMP
153b8eb35fdSChristian Daudt 	 * infrastructure to forward all secure monitor calls to Core 0.
154b8eb35fdSChristian Daudt 	 */
15535138d52SAlex Elder 	smp_call_function_single(0, __bcm_kona_smc, &data, 1);
156b8eb35fdSChristian Daudt 
1576c90f108SAlex Elder 	return data.result;
158b8eb35fdSChristian Daudt }
159