Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 2 | * |
| 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 Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 13 | #define pr_fmt(fmt) "scm-pas: " fmt |
| 14 | |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/string.h> |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 18 | #include <linux/clk.h> |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 19 | |
Vikram Mulukutla | ff23ab2 | 2013-05-30 15:01:36 -0700 | [diff] [blame] | 20 | #include <asm/cacheflush.h> |
| 21 | |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 22 | #include <mach/scm.h> |
| 23 | #include <mach/socinfo.h> |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 24 | #include <mach/msm_bus.h> |
| 25 | #include <mach/msm_bus_board.h> |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 26 | #include "scm-pas.h" |
| 27 | |
| 28 | #define PAS_INIT_IMAGE_CMD 1 |
Stephen Boyd | b16e016 | 2012-07-30 17:10:54 -0700 | [diff] [blame] | 29 | #define PAS_MEM_SETUP_CMD 2 |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 30 | #define PAS_AUTH_AND_RESET_CMD 5 |
| 31 | #define PAS_SHUTDOWN_CMD 6 |
| 32 | #define PAS_IS_SUPPORTED_CMD 7 |
| 33 | |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 34 | enum scm_clock_ids { |
| 35 | BUS_CLK = 0, |
| 36 | CORE_CLK, |
| 37 | IFACE_CLK, |
| 38 | CORE_CLK_SRC, |
| 39 | NUM_CLKS |
| 40 | }; |
| 41 | |
| 42 | static const char * const scm_clock_names[NUM_CLKS] = { |
| 43 | [BUS_CLK] = "bus_clk", |
| 44 | [CORE_CLK] = "core_clk", |
| 45 | [IFACE_CLK] = "iface_clk", |
| 46 | [CORE_CLK_SRC] = "core_clk_src", |
| 47 | }; |
| 48 | |
| 49 | static struct clk *scm_clocks[NUM_CLKS]; |
| 50 | |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 51 | static struct msm_bus_paths scm_pas_bw_tbl[] = { |
| 52 | { |
| 53 | .vectors = (struct msm_bus_vectors[]){ |
| 54 | { |
| 55 | .src = MSM_BUS_MASTER_SPS, |
| 56 | .dst = MSM_BUS_SLAVE_EBI_CH0, |
| 57 | }, |
| 58 | }, |
| 59 | .num_paths = 1, |
| 60 | }, |
| 61 | { |
| 62 | .vectors = (struct msm_bus_vectors[]){ |
| 63 | { |
| 64 | .src = MSM_BUS_MASTER_SPS, |
| 65 | .dst = MSM_BUS_SLAVE_EBI_CH0, |
| 66 | .ib = 492 * 8 * 1000000UL, |
| 67 | .ab = 492 * 8 * 100000UL, |
| 68 | }, |
| 69 | }, |
| 70 | .num_paths = 1, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | static struct msm_bus_scale_pdata scm_pas_bus_pdata = { |
| 75 | .usecase = scm_pas_bw_tbl, |
| 76 | .num_usecases = ARRAY_SIZE(scm_pas_bw_tbl), |
| 77 | .name = "scm_pas", |
| 78 | }; |
| 79 | |
| 80 | static uint32_t scm_perf_client; |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 81 | |
| 82 | static DEFINE_MUTEX(scm_pas_bw_mutex); |
| 83 | static int scm_pas_bw_count; |
| 84 | |
| 85 | static int scm_pas_enable_bw(void) |
| 86 | { |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 87 | int ret = 0, i; |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 88 | |
Matt Wagantall | 8873e20 | 2012-07-26 14:34:34 -0700 | [diff] [blame] | 89 | if (!scm_perf_client) |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | |
| 92 | mutex_lock(&scm_pas_bw_mutex); |
| 93 | if (!scm_pas_bw_count) { |
| 94 | ret = msm_bus_scale_client_update_request(scm_perf_client, 1); |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 95 | if (ret) |
| 96 | goto err_bus; |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 97 | scm_pas_bw_count++; |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 98 | } |
| 99 | for (i = 0; i < NUM_CLKS; i++) |
| 100 | if (clk_prepare_enable(scm_clocks[i])) |
| 101 | goto err_clk; |
| 102 | |
| 103 | mutex_unlock(&scm_pas_bw_mutex); |
| 104 | return ret; |
| 105 | |
| 106 | err_clk: |
| 107 | pr_err("clk prepare_enable failed (%s)\n", scm_clock_names[i]); |
| 108 | for (i--; i >= 0; i--) |
| 109 | clk_disable_unprepare(scm_clocks[i]); |
| 110 | |
| 111 | err_bus: |
| 112 | pr_err("bandwidth request failed (%d)\n", ret); |
| 113 | msm_bus_scale_client_update_request(scm_perf_client, 0); |
| 114 | |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 115 | mutex_unlock(&scm_pas_bw_mutex); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static void scm_pas_disable_bw(void) |
| 120 | { |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 121 | int i; |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 122 | mutex_lock(&scm_pas_bw_mutex); |
| 123 | if (scm_pas_bw_count-- == 1) { |
| 124 | msm_bus_scale_client_update_request(scm_perf_client, 0); |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 125 | } |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 126 | for (i = NUM_CLKS - 1; i >= 0; i--) |
| 127 | clk_disable_unprepare(scm_clocks[i]); |
| 128 | |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 129 | mutex_unlock(&scm_pas_bw_mutex); |
| 130 | } |
| 131 | |
Saravana Kannan | 8061321 | 2013-04-05 17:31:24 -0700 | [diff] [blame] | 132 | int pas_init_image(enum pas_id id, const u8 *metadata, size_t size) |
| 133 | { |
| 134 | int ret; |
| 135 | struct pas_init_image_req { |
| 136 | u32 proc; |
| 137 | u32 image_addr; |
| 138 | } request; |
| 139 | u32 scm_ret = 0; |
| 140 | void *mdata_buf; |
| 141 | |
| 142 | ret = scm_pas_enable_bw(); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | /* Make memory physically contiguous */ |
| 147 | mdata_buf = kmemdup(metadata, size, GFP_KERNEL); |
| 148 | |
| 149 | if (!mdata_buf) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | request.proc = id; |
| 153 | request.image_addr = virt_to_phys(mdata_buf); |
| 154 | |
Vikram Mulukutla | ff23ab2 | 2013-05-30 15:01:36 -0700 | [diff] [blame] | 155 | /* Flush metadata to ensure secure world doesn't read stale data */ |
| 156 | __cpuc_flush_dcache_area(mdata_buf, size); |
| 157 | outer_flush_range(request.image_addr, request.image_addr + size); |
| 158 | |
Saravana Kannan | 8061321 | 2013-04-05 17:31:24 -0700 | [diff] [blame] | 159 | ret = scm_call(SCM_SVC_PIL, PAS_INIT_IMAGE_CMD, &request, |
| 160 | sizeof(request), &scm_ret, sizeof(scm_ret)); |
| 161 | |
| 162 | kfree(mdata_buf); |
| 163 | scm_pas_disable_bw(); |
| 164 | |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | return scm_ret; |
| 168 | } |
| 169 | EXPORT_SYMBOL(pas_init_image); |
| 170 | |
| 171 | int pas_mem_setup(enum pas_id id, u32 start_addr, u32 len) |
| 172 | { |
| 173 | int ret; |
| 174 | struct pas_init_image_req { |
| 175 | u32 proc; |
| 176 | u32 start_addr; |
| 177 | u32 len; |
| 178 | } request; |
| 179 | u32 scm_ret = 0; |
| 180 | |
| 181 | request.proc = id; |
| 182 | request.start_addr = start_addr; |
| 183 | request.len = len; |
| 184 | |
| 185 | ret = scm_call(SCM_SVC_PIL, PAS_MEM_SETUP_CMD, &request, |
| 186 | sizeof(request), &scm_ret, sizeof(scm_ret)); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | return scm_ret; |
| 190 | } |
| 191 | EXPORT_SYMBOL(pas_mem_setup); |
| 192 | |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 193 | int pas_auth_and_reset(enum pas_id id) |
| 194 | { |
Saravana Kannan | 8061321 | 2013-04-05 17:31:24 -0700 | [diff] [blame] | 195 | int ret; |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 196 | u32 proc = id, scm_ret = 0; |
| 197 | |
Saravana Kannan | 8061321 | 2013-04-05 17:31:24 -0700 | [diff] [blame] | 198 | ret = scm_pas_enable_bw(); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 202 | ret = scm_call(SCM_SVC_PIL, PAS_AUTH_AND_RESET_CMD, &proc, |
| 203 | sizeof(proc), &scm_ret, sizeof(scm_ret)); |
| 204 | if (ret) |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 205 | scm_ret = ret; |
Saravana Kannan | 8061321 | 2013-04-05 17:31:24 -0700 | [diff] [blame] | 206 | |
| 207 | scm_pas_disable_bw(); |
Stephen Boyd | e44ec39 | 2011-08-29 12:03:24 -0700 | [diff] [blame] | 208 | |
| 209 | return scm_ret; |
| 210 | } |
| 211 | EXPORT_SYMBOL(pas_auth_and_reset); |
| 212 | |
| 213 | int pas_shutdown(enum pas_id id) |
| 214 | { |
| 215 | int ret; |
| 216 | u32 proc = id, scm_ret = 0; |
| 217 | |
| 218 | ret = scm_call(SCM_SVC_PIL, PAS_SHUTDOWN_CMD, &proc, sizeof(proc), |
| 219 | &scm_ret, sizeof(scm_ret)); |
| 220 | if (ret) |
| 221 | return ret; |
| 222 | |
| 223 | return scm_ret; |
| 224 | } |
| 225 | EXPORT_SYMBOL(pas_shutdown); |
| 226 | |
| 227 | static bool secure_pil = true; |
| 228 | module_param(secure_pil, bool, S_IRUGO); |
| 229 | MODULE_PARM_DESC(secure_pil, "Use secure PIL"); |
| 230 | |
| 231 | int pas_supported(enum pas_id id) |
| 232 | { |
| 233 | int ret; |
| 234 | u32 periph = id, ret_val = 0; |
| 235 | |
| 236 | if (!secure_pil) |
| 237 | return 0; |
| 238 | |
| 239 | /* |
| 240 | * 8660 SCM doesn't support querying secure PIL support so just return |
| 241 | * true if not overridden on the command line. |
| 242 | */ |
| 243 | if (cpu_is_msm8x60()) |
| 244 | return 1; |
| 245 | |
| 246 | if (scm_is_call_available(SCM_SVC_PIL, PAS_IS_SUPPORTED_CMD) <= 0) |
| 247 | return 0; |
| 248 | |
| 249 | ret = scm_call(SCM_SVC_PIL, PAS_IS_SUPPORTED_CMD, &periph, |
| 250 | sizeof(periph), &ret_val, sizeof(ret_val)); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
| 254 | return ret_val; |
| 255 | } |
| 256 | EXPORT_SYMBOL(pas_supported); |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 257 | |
| 258 | static int __init scm_pas_init(void) |
| 259 | { |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 260 | int i, rate; |
| 261 | for (i = 0; i < NUM_CLKS; i++) { |
| 262 | scm_clocks[i] = clk_get_sys("scm", scm_clock_names[i]); |
| 263 | if (IS_ERR(scm_clocks[i])) |
| 264 | scm_clocks[i] = NULL; |
| 265 | } |
| 266 | |
| 267 | /* Fail silently if this clock is not supported */ |
| 268 | rate = clk_round_rate(scm_clocks[CORE_CLK_SRC], 1); |
| 269 | clk_set_rate(scm_clocks[CORE_CLK_SRC], rate); |
| 270 | |
Vikram Mulukutla | 948945f | 2013-04-11 08:42:52 -0700 | [diff] [blame] | 271 | if (cpu_is_msm8974() || cpu_is_msm8226() || cpu_is_msm8610()) { |
Matt Wagantall | 8873e20 | 2012-07-26 14:34:34 -0700 | [diff] [blame] | 272 | scm_pas_bw_tbl[0].vectors[0].src = MSM_BUS_MASTER_CRYPTO_CORE0; |
| 273 | scm_pas_bw_tbl[1].vectors[0].src = MSM_BUS_MASTER_CRYPTO_CORE0; |
| 274 | } else { |
Patrick Daly | 7a75e26 | 2013-03-07 16:22:45 -0800 | [diff] [blame] | 275 | if (!IS_ERR(scm_clocks[BUS_CLK])) |
| 276 | clk_set_rate(scm_clocks[BUS_CLK], 64000000); |
| 277 | else |
Matt Wagantall | 8873e20 | 2012-07-26 14:34:34 -0700 | [diff] [blame] | 278 | pr_warn("unable to get bus clock\n"); |
Matt Wagantall | 8873e20 | 2012-07-26 14:34:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Syed Rameez Mustafa | 3c8353d | 2012-10-18 17:28:57 -0700 | [diff] [blame] | 281 | scm_perf_client = msm_bus_scale_register_client(&scm_pas_bus_pdata); |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 282 | if (!scm_perf_client) |
| 283 | pr_warn("unable to register bus client\n"); |
Matt Wagantall | 8873e20 | 2012-07-26 14:34:34 -0700 | [diff] [blame] | 284 | |
Stephen Boyd | ef5d1c4 | 2011-12-15 20:47:14 -0800 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | module_init(scm_pas_init); |