Commit | Line | Data |
---|---|---|
b8eb35fd CD |
1 | /* |
2 | * Copyright (C) 2013 Broadcom Corporation | |
3 | * | |
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. | |
7 | * | |
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. | |
12 | */ | |
13 | ||
14 | #include <stdarg.h> | |
15 | #include <linux/smp.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/ioport.h> | |
18 | ||
19 | #include <asm/cacheflush.h> | |
20 | #include <linux/of_address.h> | |
21 | ||
22 | #include "bcm_kona_smc.h" | |
23 | ||
c64756cc AE |
24 | static u32 bcm_smc_buffer_phys; /* physical address */ |
25 | static void __iomem *bcm_smc_buffer; /* virtual address */ | |
b8eb35fd CD |
26 | |
27 | struct bcm_kona_smc_data { | |
28 | unsigned service_id; | |
29 | unsigned arg0; | |
30 | unsigned arg1; | |
31 | unsigned arg2; | |
32 | unsigned arg3; | |
6c90f108 | 33 | unsigned result; |
b8eb35fd CD |
34 | }; |
35 | ||
36 | static const struct of_device_id bcm_kona_smc_ids[] __initconst = { | |
aea237bf CD |
37 | {.compatible = "brcm,kona-smc"}, |
38 | {.compatible = "bcm,kona-smc"}, /* deprecated name */ | |
b8eb35fd CD |
39 | {}, |
40 | }; | |
41 | ||
c64756cc | 42 | /* Map in the args buffer area */ |
3a76b351 | 43 | int __init bcm_kona_smc_init(void) |
b8eb35fd CD |
44 | { |
45 | struct device_node *node; | |
5c4cee2f | 46 | const __be32 *prop_val; |
c64756cc AE |
47 | u64 prop_size = 0; |
48 | unsigned long buffer_size; | |
49 | u32 buffer_phys; | |
b8eb35fd CD |
50 | |
51 | /* Read buffer addr and size from the device tree node */ | |
52 | node = of_find_matching_node(NULL, bcm_kona_smc_ids); | |
3a76b351 CD |
53 | if (!node) |
54 | return -ENODEV; | |
b8eb35fd | 55 | |
c64756cc | 56 | prop_val = of_get_address(node, 0, &prop_size, NULL); |
5c4cee2f AE |
57 | if (!prop_val) |
58 | return -EINVAL; | |
59 | ||
c64756cc AE |
60 | /* We assume space for four 32-bit arguments */ |
61 | if (prop_size < 4 * sizeof(u32) || prop_size > (u64)ULONG_MAX) | |
5c4cee2f | 62 | return -EINVAL; |
c64756cc | 63 | buffer_size = (unsigned long)prop_size; |
b8eb35fd | 64 | |
c64756cc AE |
65 | buffer_phys = be32_to_cpup(prop_val); |
66 | if (!buffer_phys) | |
67 | return -EINVAL; | |
b8eb35fd | 68 | |
c64756cc AE |
69 | bcm_smc_buffer = ioremap(buffer_phys, buffer_size); |
70 | if (!bcm_smc_buffer) | |
71 | return -ENOMEM; | |
72 | bcm_smc_buffer_phys = buffer_phys; | |
b8eb35fd | 73 | |
3a76b351 CD |
74 | pr_info("Kona Secure API initialized\n"); |
75 | ||
76 | return 0; | |
b8eb35fd CD |
77 | } |
78 | ||
79 | /* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */ | |
80 | static void __bcm_kona_smc(void *info) | |
81 | { | |
82 | struct bcm_kona_smc_data *data = info; | |
c64756cc | 83 | u32 *args = bcm_smc_buffer; |
b8eb35fd | 84 | |
b8eb35fd | 85 | BUG_ON(smp_processor_id() != 0); |
c64756cc | 86 | BUG_ON(!args); |
b8eb35fd | 87 | |
e80eef33 AE |
88 | /* Copy the four 32 bit argument values into the bounce area */ |
89 | writel_relaxed(data->arg0, args++); | |
90 | writel_relaxed(data->arg1, args++); | |
91 | writel_relaxed(data->arg2, args++); | |
92 | writel(data->arg3, args); | |
b8eb35fd CD |
93 | |
94 | /* Flush caches for input data passed to Secure Monitor */ | |
c64756cc | 95 | flush_cache_all(); |
b8eb35fd | 96 | |
6c90f108 AE |
97 | /* Trap into Secure Monitor and record the request result */ |
98 | data->result = bcm_kona_smc_asm(data->service_id, bcm_smc_buffer_phys); | |
b8eb35fd CD |
99 | } |
100 | ||
101 | unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1, | |
102 | unsigned arg2, unsigned arg3) | |
103 | { | |
104 | struct bcm_kona_smc_data data; | |
105 | ||
106 | data.service_id = service_id; | |
107 | data.arg0 = arg0; | |
108 | data.arg1 = arg1; | |
109 | data.arg2 = arg2; | |
110 | data.arg3 = arg3; | |
6c90f108 | 111 | data.result = 0; |
b8eb35fd CD |
112 | |
113 | /* | |
114 | * Due to a limitation of the secure monitor, we must use the SMP | |
115 | * infrastructure to forward all secure monitor calls to Core 0. | |
116 | */ | |
35138d52 | 117 | smp_call_function_single(0, __bcm_kona_smc, &data, 1); |
b8eb35fd | 118 | |
6c90f108 | 119 | return data.result; |
b8eb35fd | 120 | } |