blob: f48b538a58e09a42b7a762db5ca3ce70db146616 [file] [log] [blame]
Patrick Daly7a75e262013-03-07 16:22:45 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Stephen Boyde44ec392011-08-29 12:03:24 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Stephen Boydef5d1c42011-12-15 20:47:14 -080013#define pr_fmt(fmt) "scm-pas: " fmt
14
Stephen Boyde44ec392011-08-29 12:03:24 -070015#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/string.h>
Stephen Boydef5d1c42011-12-15 20:47:14 -080018#include <linux/clk.h>
Stephen Boyde44ec392011-08-29 12:03:24 -070019
20#include <mach/scm.h>
21#include <mach/socinfo.h>
Stephen Boydef5d1c42011-12-15 20:47:14 -080022#include <mach/msm_bus.h>
23#include <mach/msm_bus_board.h>
Stephen Boyde44ec392011-08-29 12:03:24 -070024#include "scm-pas.h"
25
26#define PAS_INIT_IMAGE_CMD 1
Stephen Boydb16e0162012-07-30 17:10:54 -070027#define PAS_MEM_SETUP_CMD 2
Stephen Boyde44ec392011-08-29 12:03:24 -070028#define PAS_AUTH_AND_RESET_CMD 5
29#define PAS_SHUTDOWN_CMD 6
30#define PAS_IS_SUPPORTED_CMD 7
31
Patrick Daly7a75e262013-03-07 16:22:45 -080032enum scm_clock_ids {
33 BUS_CLK = 0,
34 CORE_CLK,
35 IFACE_CLK,
36 CORE_CLK_SRC,
37 NUM_CLKS
38};
39
40static const char * const scm_clock_names[NUM_CLKS] = {
41 [BUS_CLK] = "bus_clk",
42 [CORE_CLK] = "core_clk",
43 [IFACE_CLK] = "iface_clk",
44 [CORE_CLK_SRC] = "core_clk_src",
45};
46
47static struct clk *scm_clocks[NUM_CLKS];
48
Stephen Boydef5d1c42011-12-15 20:47:14 -080049static struct msm_bus_paths scm_pas_bw_tbl[] = {
50 {
51 .vectors = (struct msm_bus_vectors[]){
52 {
53 .src = MSM_BUS_MASTER_SPS,
54 .dst = MSM_BUS_SLAVE_EBI_CH0,
55 },
56 },
57 .num_paths = 1,
58 },
59 {
60 .vectors = (struct msm_bus_vectors[]){
61 {
62 .src = MSM_BUS_MASTER_SPS,
63 .dst = MSM_BUS_SLAVE_EBI_CH0,
64 .ib = 492 * 8 * 1000000UL,
65 .ab = 492 * 8 * 100000UL,
66 },
67 },
68 .num_paths = 1,
69 },
70};
71
72static struct msm_bus_scale_pdata scm_pas_bus_pdata = {
73 .usecase = scm_pas_bw_tbl,
74 .num_usecases = ARRAY_SIZE(scm_pas_bw_tbl),
75 .name = "scm_pas",
76};
77
78static uint32_t scm_perf_client;
Stephen Boydef5d1c42011-12-15 20:47:14 -080079
80static DEFINE_MUTEX(scm_pas_bw_mutex);
81static int scm_pas_bw_count;
82
83static int scm_pas_enable_bw(void)
84{
Patrick Daly7a75e262013-03-07 16:22:45 -080085 int ret = 0, i;
Stephen Boydef5d1c42011-12-15 20:47:14 -080086
Matt Wagantall8873e202012-07-26 14:34:34 -070087 if (!scm_perf_client)
Stephen Boydef5d1c42011-12-15 20:47:14 -080088 return -EINVAL;
89
90 mutex_lock(&scm_pas_bw_mutex);
91 if (!scm_pas_bw_count) {
92 ret = msm_bus_scale_client_update_request(scm_perf_client, 1);
Patrick Daly7a75e262013-03-07 16:22:45 -080093 if (ret)
94 goto err_bus;
Stephen Boydef5d1c42011-12-15 20:47:14 -080095 scm_pas_bw_count++;
Patrick Daly7a75e262013-03-07 16:22:45 -080096 }
97 for (i = 0; i < NUM_CLKS; i++)
98 if (clk_prepare_enable(scm_clocks[i]))
99 goto err_clk;
100
101 mutex_unlock(&scm_pas_bw_mutex);
102 return ret;
103
104err_clk:
105 pr_err("clk prepare_enable failed (%s)\n", scm_clock_names[i]);
106 for (i--; i >= 0; i--)
107 clk_disable_unprepare(scm_clocks[i]);
108
109err_bus:
110 pr_err("bandwidth request failed (%d)\n", ret);
111 msm_bus_scale_client_update_request(scm_perf_client, 0);
112
Stephen Boydef5d1c42011-12-15 20:47:14 -0800113 mutex_unlock(&scm_pas_bw_mutex);
114 return ret;
115}
116
117static void scm_pas_disable_bw(void)
118{
Patrick Daly7a75e262013-03-07 16:22:45 -0800119 int i;
Stephen Boydef5d1c42011-12-15 20:47:14 -0800120 mutex_lock(&scm_pas_bw_mutex);
121 if (scm_pas_bw_count-- == 1) {
122 msm_bus_scale_client_update_request(scm_perf_client, 0);
Stephen Boydef5d1c42011-12-15 20:47:14 -0800123 }
Patrick Daly7a75e262013-03-07 16:22:45 -0800124 for (i = NUM_CLKS - 1; i >= 0; i--)
125 clk_disable_unprepare(scm_clocks[i]);
126
Stephen Boydef5d1c42011-12-15 20:47:14 -0800127 mutex_unlock(&scm_pas_bw_mutex);
128}
129
Saravana Kannan80613212013-04-05 17:31:24 -0700130int pas_init_image(enum pas_id id, const u8 *metadata, size_t size)
131{
132 int ret;
133 struct pas_init_image_req {
134 u32 proc;
135 u32 image_addr;
136 } request;
137 u32 scm_ret = 0;
138 void *mdata_buf;
139
140 ret = scm_pas_enable_bw();
141 if (ret)
142 return ret;
143
144 /* Make memory physically contiguous */
145 mdata_buf = kmemdup(metadata, size, GFP_KERNEL);
146
147 if (!mdata_buf)
148 return -ENOMEM;
149
150 request.proc = id;
151 request.image_addr = virt_to_phys(mdata_buf);
152
153 ret = scm_call(SCM_SVC_PIL, PAS_INIT_IMAGE_CMD, &request,
154 sizeof(request), &scm_ret, sizeof(scm_ret));
155
156 kfree(mdata_buf);
157 scm_pas_disable_bw();
158
159 if (ret)
160 return ret;
161 return scm_ret;
162}
163EXPORT_SYMBOL(pas_init_image);
164
165int pas_mem_setup(enum pas_id id, u32 start_addr, u32 len)
166{
167 int ret;
168 struct pas_init_image_req {
169 u32 proc;
170 u32 start_addr;
171 u32 len;
172 } request;
173 u32 scm_ret = 0;
174
175 request.proc = id;
176 request.start_addr = start_addr;
177 request.len = len;
178
179 ret = scm_call(SCM_SVC_PIL, PAS_MEM_SETUP_CMD, &request,
180 sizeof(request), &scm_ret, sizeof(scm_ret));
181 if (ret)
182 return ret;
183 return scm_ret;
184}
185EXPORT_SYMBOL(pas_mem_setup);
186
Stephen Boyde44ec392011-08-29 12:03:24 -0700187int pas_auth_and_reset(enum pas_id id)
188{
Saravana Kannan80613212013-04-05 17:31:24 -0700189 int ret;
Stephen Boyde44ec392011-08-29 12:03:24 -0700190 u32 proc = id, scm_ret = 0;
191
Saravana Kannan80613212013-04-05 17:31:24 -0700192 ret = scm_pas_enable_bw();
193 if (ret)
194 return ret;
195
Stephen Boyde44ec392011-08-29 12:03:24 -0700196 ret = scm_call(SCM_SVC_PIL, PAS_AUTH_AND_RESET_CMD, &proc,
197 sizeof(proc), &scm_ret, sizeof(scm_ret));
198 if (ret)
Stephen Boydef5d1c42011-12-15 20:47:14 -0800199 scm_ret = ret;
Saravana Kannan80613212013-04-05 17:31:24 -0700200
201 scm_pas_disable_bw();
Stephen Boyde44ec392011-08-29 12:03:24 -0700202
203 return scm_ret;
204}
205EXPORT_SYMBOL(pas_auth_and_reset);
206
207int pas_shutdown(enum pas_id id)
208{
209 int ret;
210 u32 proc = id, scm_ret = 0;
211
212 ret = scm_call(SCM_SVC_PIL, PAS_SHUTDOWN_CMD, &proc, sizeof(proc),
213 &scm_ret, sizeof(scm_ret));
214 if (ret)
215 return ret;
216
217 return scm_ret;
218}
219EXPORT_SYMBOL(pas_shutdown);
220
221static bool secure_pil = true;
222module_param(secure_pil, bool, S_IRUGO);
223MODULE_PARM_DESC(secure_pil, "Use secure PIL");
224
225int pas_supported(enum pas_id id)
226{
227 int ret;
228 u32 periph = id, ret_val = 0;
229
230 if (!secure_pil)
231 return 0;
232
233 /*
234 * 8660 SCM doesn't support querying secure PIL support so just return
235 * true if not overridden on the command line.
236 */
237 if (cpu_is_msm8x60())
238 return 1;
239
240 if (scm_is_call_available(SCM_SVC_PIL, PAS_IS_SUPPORTED_CMD) <= 0)
241 return 0;
242
243 ret = scm_call(SCM_SVC_PIL, PAS_IS_SUPPORTED_CMD, &periph,
244 sizeof(periph), &ret_val, sizeof(ret_val));
245 if (ret)
246 return ret;
247
248 return ret_val;
249}
250EXPORT_SYMBOL(pas_supported);
Stephen Boydef5d1c42011-12-15 20:47:14 -0800251
252static int __init scm_pas_init(void)
253{
Patrick Daly7a75e262013-03-07 16:22:45 -0800254 int i, rate;
255 for (i = 0; i < NUM_CLKS; i++) {
256 scm_clocks[i] = clk_get_sys("scm", scm_clock_names[i]);
257 if (IS_ERR(scm_clocks[i]))
258 scm_clocks[i] = NULL;
259 }
260
261 /* Fail silently if this clock is not supported */
262 rate = clk_round_rate(scm_clocks[CORE_CLK_SRC], 1);
263 clk_set_rate(scm_clocks[CORE_CLK_SRC], rate);
264
Vikram Mulukutla948945f2013-04-11 08:42:52 -0700265 if (cpu_is_msm8974() || cpu_is_msm8226() || cpu_is_msm8610()) {
Matt Wagantall8873e202012-07-26 14:34:34 -0700266 scm_pas_bw_tbl[0].vectors[0].src = MSM_BUS_MASTER_CRYPTO_CORE0;
267 scm_pas_bw_tbl[1].vectors[0].src = MSM_BUS_MASTER_CRYPTO_CORE0;
268 } else {
Patrick Daly7a75e262013-03-07 16:22:45 -0800269 if (!IS_ERR(scm_clocks[BUS_CLK]))
270 clk_set_rate(scm_clocks[BUS_CLK], 64000000);
271 else
Matt Wagantall8873e202012-07-26 14:34:34 -0700272 pr_warn("unable to get bus clock\n");
Matt Wagantall8873e202012-07-26 14:34:34 -0700273 }
274
Syed Rameez Mustafa3c8353d2012-10-18 17:28:57 -0700275 scm_perf_client = msm_bus_scale_register_client(&scm_pas_bus_pdata);
Stephen Boydef5d1c42011-12-15 20:47:14 -0800276 if (!scm_perf_client)
277 pr_warn("unable to register bus client\n");
Matt Wagantall8873e202012-07-26 14:34:34 -0700278
Stephen Boydef5d1c42011-12-15 20:47:14 -0800279 return 0;
280}
281module_init(scm_pas_init);