Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Paul Gortmaker | dea85242 | 2016-07-04 11:01:56 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm SCM driver |
| 4 | * |
| 5 | * Copyright (c) 2010,2015, The Linux Foundation. All rights reserved. |
Lina Iyer | 2ce76a6 | 2015-03-02 16:30:29 -0700 | [diff] [blame] | 6 | * Copyright (C) 2015 Linaro Ltd. |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 7 | */ |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 8 | #include <linux/platform_device.h> |
Paul Gortmaker | dea85242 | 2016-07-04 11:01:56 -0400 | [diff] [blame] | 9 | #include <linux/init.h> |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 10 | #include <linux/cpumask.h> |
| 11 | #include <linux/export.h> |
Stephen Boyd | 6e37ccf | 2019-05-17 14:09:21 -0700 | [diff] [blame] | 12 | #include <linux/dma-direct.h> |
Bjorn Andersson | f01e90f | 2015-09-23 12:56:12 -0700 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 15 | #include <linux/types.h> |
Kumar Gala | 916f743d | 2015-02-26 15:49:09 -0600 | [diff] [blame] | 16 | #include <linux/qcom_scm.h> |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 17 | #include <linux/of.h> |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 18 | #include <linux/of_address.h> |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/clk.h> |
Bjorn Andersson | dd4fe5b | 2016-06-17 10:40:43 -0700 | [diff] [blame] | 21 | #include <linux/reset-controller.h> |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 22 | |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 23 | #include "qcom_scm.h" |
Lina Iyer | a353e4a | 2015-03-02 16:30:28 -0700 | [diff] [blame] | 24 | |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 25 | static bool download_mode = IS_ENABLED(CONFIG_QCOM_SCM_DOWNLOAD_MODE_DEFAULT); |
| 26 | module_param(download_mode, bool, 0); |
| 27 | |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 28 | #define SCM_HAS_CORE_CLK BIT(0) |
| 29 | #define SCM_HAS_IFACE_CLK BIT(1) |
| 30 | #define SCM_HAS_BUS_CLK BIT(2) |
| 31 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 32 | struct qcom_scm { |
| 33 | struct device *dev; |
| 34 | struct clk *core_clk; |
| 35 | struct clk *iface_clk; |
| 36 | struct clk *bus_clk; |
Bjorn Andersson | dd4fe5b | 2016-06-17 10:40:43 -0700 | [diff] [blame] | 37 | struct reset_controller_dev reset; |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 38 | |
| 39 | u64 dload_mode_addr; |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 40 | }; |
| 41 | |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 42 | struct qcom_scm_current_perm_info { |
| 43 | __le32 vmid; |
| 44 | __le32 perm; |
| 45 | __le64 ctx; |
| 46 | __le32 ctx_size; |
| 47 | __le32 unused; |
| 48 | }; |
| 49 | |
| 50 | struct qcom_scm_mem_map_info { |
| 51 | __le64 mem_addr; |
| 52 | __le64 mem_size; |
| 53 | }; |
| 54 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 55 | static struct qcom_scm *__scm; |
| 56 | |
| 57 | static int qcom_scm_clk_enable(void) |
| 58 | { |
| 59 | int ret; |
| 60 | |
| 61 | ret = clk_prepare_enable(__scm->core_clk); |
| 62 | if (ret) |
| 63 | goto bail; |
| 64 | |
| 65 | ret = clk_prepare_enable(__scm->iface_clk); |
| 66 | if (ret) |
| 67 | goto disable_core; |
| 68 | |
| 69 | ret = clk_prepare_enable(__scm->bus_clk); |
| 70 | if (ret) |
| 71 | goto disable_iface; |
| 72 | |
| 73 | return 0; |
| 74 | |
| 75 | disable_iface: |
| 76 | clk_disable_unprepare(__scm->iface_clk); |
| 77 | disable_core: |
| 78 | clk_disable_unprepare(__scm->core_clk); |
| 79 | bail: |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static void qcom_scm_clk_disable(void) |
| 84 | { |
| 85 | clk_disable_unprepare(__scm->core_clk); |
| 86 | clk_disable_unprepare(__scm->iface_clk); |
| 87 | clk_disable_unprepare(__scm->bus_clk); |
| 88 | } |
| 89 | |
Lina Iyer | a353e4a | 2015-03-02 16:30:28 -0700 | [diff] [blame] | 90 | /** |
| 91 | * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus |
| 92 | * @entry: Entry point function for the cpus |
| 93 | * @cpus: The cpumask of cpus that will use the entry point |
| 94 | * |
| 95 | * Set the cold boot address of the cpus. Any cpu outside the supported |
| 96 | * range would be removed from the cpu present mask. |
| 97 | */ |
| 98 | int qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus) |
| 99 | { |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 100 | return __qcom_scm_set_cold_boot_addr(entry, cpus); |
Lina Iyer | a353e4a | 2015-03-02 16:30:28 -0700 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr); |
Lina Iyer | 2ce76a6 | 2015-03-02 16:30:29 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus |
| 106 | * @entry: Entry point function for the cpus |
| 107 | * @cpus: The cpumask of cpus that will use the entry point |
| 108 | * |
| 109 | * Set the Linux entry point for the SCM to transfer control to when coming |
| 110 | * out of a power down. CPU power down may be executed on cpuidle or hotplug. |
| 111 | */ |
| 112 | int qcom_scm_set_warm_boot_addr(void *entry, const cpumask_t *cpus) |
| 113 | { |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 114 | return __qcom_scm_set_warm_boot_addr(__scm->dev, entry, cpus); |
Lina Iyer | 2ce76a6 | 2015-03-02 16:30:29 -0700 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL(qcom_scm_set_warm_boot_addr); |
Lina Iyer | 767b023 | 2015-03-02 16:30:30 -0700 | [diff] [blame] | 117 | |
Lina Iyer | 767b023 | 2015-03-02 16:30:30 -0700 | [diff] [blame] | 118 | /** |
| 119 | * qcom_scm_cpu_power_down() - Power down the cpu |
| 120 | * @flags - Flags to flush cache |
| 121 | * |
| 122 | * This is an end point to power down cpu. If there was a pending interrupt, |
| 123 | * the control would return from this function, otherwise, the cpu jumps to the |
| 124 | * warm boot entry point set for this cpu upon reset. |
| 125 | */ |
| 126 | void qcom_scm_cpu_power_down(u32 flags) |
| 127 | { |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 128 | __qcom_scm_cpu_power_down(flags); |
Lina Iyer | 767b023 | 2015-03-02 16:30:30 -0700 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL(qcom_scm_cpu_power_down); |
jilai wang | 9626b69 | 2015-04-10 16:15:59 -0400 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * qcom_scm_hdcp_available() - Check if secure environment supports HDCP. |
| 134 | * |
| 135 | * Return true if HDCP is supported, false if not. |
| 136 | */ |
| 137 | bool qcom_scm_hdcp_available(void) |
| 138 | { |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 139 | int ret = qcom_scm_clk_enable(); |
| 140 | |
| 141 | if (ret) |
| 142 | return ret; |
jilai wang | 9626b69 | 2015-04-10 16:15:59 -0400 | [diff] [blame] | 143 | |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 144 | ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP, |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 145 | QCOM_SCM_CMD_HDCP); |
jilai wang | 9626b69 | 2015-04-10 16:15:59 -0400 | [diff] [blame] | 146 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 147 | qcom_scm_clk_disable(); |
| 148 | |
| 149 | return ret > 0 ? true : false; |
jilai wang | 9626b69 | 2015-04-10 16:15:59 -0400 | [diff] [blame] | 150 | } |
| 151 | EXPORT_SYMBOL(qcom_scm_hdcp_available); |
| 152 | |
| 153 | /** |
| 154 | * qcom_scm_hdcp_req() - Send HDCP request. |
| 155 | * @req: HDCP request array |
| 156 | * @req_cnt: HDCP request array count |
| 157 | * @resp: response buffer passed to SCM |
| 158 | * |
| 159 | * Write HDCP register(s) through SCM. |
| 160 | */ |
| 161 | int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp) |
| 162 | { |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 163 | int ret = qcom_scm_clk_enable(); |
| 164 | |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 168 | ret = __qcom_scm_hdcp_req(__scm->dev, req, req_cnt, resp); |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 169 | qcom_scm_clk_disable(); |
| 170 | return ret; |
jilai wang | 9626b69 | 2015-04-10 16:15:59 -0400 | [diff] [blame] | 171 | } |
| 172 | EXPORT_SYMBOL(qcom_scm_hdcp_req); |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 173 | |
Bjorn Andersson | f01e90f | 2015-09-23 12:56:12 -0700 | [diff] [blame] | 174 | /** |
| 175 | * qcom_scm_pas_supported() - Check if the peripheral authentication service is |
| 176 | * available for the given peripherial |
| 177 | * @peripheral: peripheral id |
| 178 | * |
| 179 | * Returns true if PAS is supported for this peripheral, otherwise false. |
| 180 | */ |
| 181 | bool qcom_scm_pas_supported(u32 peripheral) |
| 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL, |
| 186 | QCOM_SCM_PAS_IS_SUPPORTED_CMD); |
| 187 | if (ret <= 0) |
| 188 | return false; |
| 189 | |
| 190 | return __qcom_scm_pas_supported(__scm->dev, peripheral); |
| 191 | } |
| 192 | EXPORT_SYMBOL(qcom_scm_pas_supported); |
| 193 | |
| 194 | /** |
Rob Clark | b0a1614 | 2019-08-23 05:16:33 -0700 | [diff] [blame^] | 195 | * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available |
| 196 | */ |
| 197 | bool qcom_scm_ocmem_lock_available(void) |
| 198 | { |
| 199 | return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_OCMEM_SVC, |
| 200 | QCOM_SCM_OCMEM_LOCK_CMD); |
| 201 | } |
| 202 | EXPORT_SYMBOL(qcom_scm_ocmem_lock_available); |
| 203 | |
| 204 | /** |
| 205 | * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM |
| 206 | * region to the specified initiator |
| 207 | * |
| 208 | * @id: tz initiator id |
| 209 | * @offset: OCMEM offset |
| 210 | * @size: OCMEM size |
| 211 | * @mode: access mode (WIDE/NARROW) |
| 212 | */ |
| 213 | int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size, |
| 214 | u32 mode) |
| 215 | { |
| 216 | return __qcom_scm_ocmem_lock(__scm->dev, id, offset, size, mode); |
| 217 | } |
| 218 | EXPORT_SYMBOL(qcom_scm_ocmem_lock); |
| 219 | |
| 220 | /** |
| 221 | * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM |
| 222 | * region from the specified initiator |
| 223 | * |
| 224 | * @id: tz initiator id |
| 225 | * @offset: OCMEM offset |
| 226 | * @size: OCMEM size |
| 227 | */ |
| 228 | int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size) |
| 229 | { |
| 230 | return __qcom_scm_ocmem_unlock(__scm->dev, id, offset, size); |
| 231 | } |
| 232 | EXPORT_SYMBOL(qcom_scm_ocmem_unlock); |
| 233 | |
| 234 | /** |
Bjorn Andersson | f01e90f | 2015-09-23 12:56:12 -0700 | [diff] [blame] | 235 | * qcom_scm_pas_init_image() - Initialize peripheral authentication service |
| 236 | * state machine for a given peripheral, using the |
| 237 | * metadata |
| 238 | * @peripheral: peripheral id |
| 239 | * @metadata: pointer to memory containing ELF header, program header table |
| 240 | * and optional blob of data used for authenticating the metadata |
| 241 | * and the rest of the firmware |
| 242 | * @size: size of the metadata |
| 243 | * |
| 244 | * Returns 0 on success. |
| 245 | */ |
| 246 | int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size) |
| 247 | { |
| 248 | dma_addr_t mdata_phys; |
| 249 | void *mdata_buf; |
| 250 | int ret; |
| 251 | |
| 252 | /* |
| 253 | * During the scm call memory protection will be enabled for the meta |
| 254 | * data blob, so make sure it's physically contiguous, 4K aligned and |
| 255 | * non-cachable to avoid XPU violations. |
| 256 | */ |
| 257 | mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys, |
| 258 | GFP_KERNEL); |
| 259 | if (!mdata_buf) { |
| 260 | dev_err(__scm->dev, "Allocation of metadata buffer failed.\n"); |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | memcpy(mdata_buf, metadata, size); |
| 264 | |
| 265 | ret = qcom_scm_clk_enable(); |
| 266 | if (ret) |
| 267 | goto free_metadata; |
| 268 | |
| 269 | ret = __qcom_scm_pas_init_image(__scm->dev, peripheral, mdata_phys); |
| 270 | |
| 271 | qcom_scm_clk_disable(); |
| 272 | |
| 273 | free_metadata: |
| 274 | dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | EXPORT_SYMBOL(qcom_scm_pas_init_image); |
| 279 | |
| 280 | /** |
| 281 | * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral |
| 282 | * for firmware loading |
| 283 | * @peripheral: peripheral id |
| 284 | * @addr: start address of memory area to prepare |
| 285 | * @size: size of the memory area to prepare |
| 286 | * |
| 287 | * Returns 0 on success. |
| 288 | */ |
| 289 | int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size) |
| 290 | { |
| 291 | int ret; |
| 292 | |
| 293 | ret = qcom_scm_clk_enable(); |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
| 297 | ret = __qcom_scm_pas_mem_setup(__scm->dev, peripheral, addr, size); |
| 298 | qcom_scm_clk_disable(); |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | EXPORT_SYMBOL(qcom_scm_pas_mem_setup); |
| 303 | |
| 304 | /** |
| 305 | * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware |
| 306 | * and reset the remote processor |
| 307 | * @peripheral: peripheral id |
| 308 | * |
| 309 | * Return 0 on success. |
| 310 | */ |
| 311 | int qcom_scm_pas_auth_and_reset(u32 peripheral) |
| 312 | { |
| 313 | int ret; |
| 314 | |
| 315 | ret = qcom_scm_clk_enable(); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
| 319 | ret = __qcom_scm_pas_auth_and_reset(__scm->dev, peripheral); |
| 320 | qcom_scm_clk_disable(); |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | EXPORT_SYMBOL(qcom_scm_pas_auth_and_reset); |
| 325 | |
| 326 | /** |
| 327 | * qcom_scm_pas_shutdown() - Shut down the remote processor |
| 328 | * @peripheral: peripheral id |
| 329 | * |
| 330 | * Returns 0 on success. |
| 331 | */ |
| 332 | int qcom_scm_pas_shutdown(u32 peripheral) |
| 333 | { |
| 334 | int ret; |
| 335 | |
| 336 | ret = qcom_scm_clk_enable(); |
| 337 | if (ret) |
| 338 | return ret; |
| 339 | |
| 340 | ret = __qcom_scm_pas_shutdown(__scm->dev, peripheral); |
| 341 | qcom_scm_clk_disable(); |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | EXPORT_SYMBOL(qcom_scm_pas_shutdown); |
| 346 | |
Bjorn Andersson | dd4fe5b | 2016-06-17 10:40:43 -0700 | [diff] [blame] | 347 | static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev, |
| 348 | unsigned long idx) |
| 349 | { |
| 350 | if (idx != 0) |
| 351 | return -EINVAL; |
| 352 | |
| 353 | return __qcom_scm_pas_mss_reset(__scm->dev, 1); |
| 354 | } |
| 355 | |
| 356 | static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev, |
| 357 | unsigned long idx) |
| 358 | { |
| 359 | if (idx != 0) |
| 360 | return -EINVAL; |
| 361 | |
| 362 | return __qcom_scm_pas_mss_reset(__scm->dev, 0); |
| 363 | } |
| 364 | |
| 365 | static const struct reset_control_ops qcom_scm_pas_reset_ops = { |
| 366 | .assert = qcom_scm_pas_reset_assert, |
| 367 | .deassert = qcom_scm_pas_reset_deassert, |
| 368 | }; |
| 369 | |
Rob Clark | a2c680c | 2017-03-14 11:18:03 -0400 | [diff] [blame] | 370 | int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare) |
| 371 | { |
| 372 | return __qcom_scm_restore_sec_cfg(__scm->dev, device_id, spare); |
| 373 | } |
| 374 | EXPORT_SYMBOL(qcom_scm_restore_sec_cfg); |
| 375 | |
Stanimir Varbanov | b182cc4 | 2017-03-14 11:18:04 -0400 | [diff] [blame] | 376 | int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size) |
| 377 | { |
| 378 | return __qcom_scm_iommu_secure_ptbl_size(__scm->dev, spare, size); |
| 379 | } |
| 380 | EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_size); |
| 381 | |
| 382 | int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare) |
| 383 | { |
| 384 | return __qcom_scm_iommu_secure_ptbl_init(__scm->dev, addr, size, spare); |
| 385 | } |
| 386 | EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init); |
| 387 | |
Bjorn Andersson | 4e659db | 2017-08-14 15:46:17 -0700 | [diff] [blame] | 388 | int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val) |
| 389 | { |
| 390 | return __qcom_scm_io_readl(__scm->dev, addr, val); |
| 391 | } |
| 392 | EXPORT_SYMBOL(qcom_scm_io_readl); |
| 393 | |
| 394 | int qcom_scm_io_writel(phys_addr_t addr, unsigned int val) |
| 395 | { |
| 396 | return __qcom_scm_io_writel(__scm->dev, addr, val); |
| 397 | } |
| 398 | EXPORT_SYMBOL(qcom_scm_io_writel); |
| 399 | |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 400 | static void qcom_scm_set_download_mode(bool enable) |
| 401 | { |
| 402 | bool avail; |
| 403 | int ret = 0; |
| 404 | |
| 405 | avail = __qcom_scm_is_call_available(__scm->dev, |
| 406 | QCOM_SCM_SVC_BOOT, |
| 407 | QCOM_SCM_SET_DLOAD_MODE); |
| 408 | if (avail) { |
| 409 | ret = __qcom_scm_set_dload_mode(__scm->dev, enable); |
| 410 | } else if (__scm->dload_mode_addr) { |
| 411 | ret = __qcom_scm_io_writel(__scm->dev, __scm->dload_mode_addr, |
| 412 | enable ? QCOM_SCM_SET_DLOAD_MODE : 0); |
| 413 | } else { |
| 414 | dev_err(__scm->dev, |
| 415 | "No available mechanism for setting download mode\n"); |
| 416 | } |
| 417 | |
| 418 | if (ret) |
| 419 | dev_err(__scm->dev, "failed to set download mode: %d\n", ret); |
| 420 | } |
| 421 | |
| 422 | static int qcom_scm_find_dload_address(struct device *dev, u64 *addr) |
| 423 | { |
| 424 | struct device_node *tcsr; |
| 425 | struct device_node *np = dev->of_node; |
| 426 | struct resource res; |
| 427 | u32 offset; |
| 428 | int ret; |
| 429 | |
| 430 | tcsr = of_parse_phandle(np, "qcom,dload-mode", 0); |
| 431 | if (!tcsr) |
| 432 | return 0; |
| 433 | |
| 434 | ret = of_address_to_resource(tcsr, 0, &res); |
| 435 | of_node_put(tcsr); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | |
| 439 | ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset); |
| 440 | if (ret < 0) |
| 441 | return ret; |
| 442 | |
| 443 | *addr = res.start + offset; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Andy Gross | 72d4341 | 2016-06-29 15:28:29 -0500 | [diff] [blame] | 448 | /** |
| 449 | * qcom_scm_is_available() - Checks if SCM is available |
| 450 | */ |
| 451 | bool qcom_scm_is_available(void) |
| 452 | { |
| 453 | return !!__scm; |
| 454 | } |
| 455 | EXPORT_SYMBOL(qcom_scm_is_available); |
Bjorn Andersson | dd4fe5b | 2016-06-17 10:40:43 -0700 | [diff] [blame] | 456 | |
Andy Gross | a811b42 | 2017-01-16 23:24:15 -0600 | [diff] [blame] | 457 | int qcom_scm_set_remote_state(u32 state, u32 id) |
| 458 | { |
| 459 | return __qcom_scm_set_remote_state(__scm->dev, state, id); |
| 460 | } |
| 461 | EXPORT_SYMBOL(qcom_scm_set_remote_state); |
| 462 | |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 463 | /** |
| 464 | * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership |
| 465 | * @mem_addr: mem region whose ownership need to be reassigned |
| 466 | * @mem_sz: size of the region. |
| 467 | * @srcvm: vmid for current set of owners, each set bit in |
| 468 | * flag indicate a unique owner |
Stephen Boyd | c8b08fc | 2019-05-17 14:09:23 -0700 | [diff] [blame] | 469 | * @newvm: array having new owners and corresponding permission |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 470 | * flags |
| 471 | * @dest_cnt: number of owners in next set. |
| 472 | * |
Stephen Boyd | c8b08fc | 2019-05-17 14:09:23 -0700 | [diff] [blame] | 473 | * Return negative errno on failure or 0 on success with @srcvm updated. |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 474 | */ |
| 475 | int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz, |
| 476 | unsigned int *srcvm, |
Stephen Boyd | af311ff | 2019-05-17 14:09:22 -0700 | [diff] [blame] | 477 | const struct qcom_scm_vmperm *newvm, |
| 478 | unsigned int dest_cnt) |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 479 | { |
| 480 | struct qcom_scm_current_perm_info *destvm; |
| 481 | struct qcom_scm_mem_map_info *mem_to_map; |
| 482 | phys_addr_t mem_to_map_phys; |
| 483 | phys_addr_t dest_phys; |
| 484 | phys_addr_t ptr_phys; |
Stephen Boyd | 6e37ccf | 2019-05-17 14:09:21 -0700 | [diff] [blame] | 485 | dma_addr_t ptr_dma; |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 486 | size_t mem_to_map_sz; |
| 487 | size_t dest_sz; |
| 488 | size_t src_sz; |
| 489 | size_t ptr_sz; |
| 490 | int next_vm; |
| 491 | __le32 *src; |
| 492 | void *ptr; |
Stephen Boyd | af311ff | 2019-05-17 14:09:22 -0700 | [diff] [blame] | 493 | int ret, i, b; |
| 494 | unsigned long srcvm_bits = *srcvm; |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 495 | |
Stephen Boyd | af311ff | 2019-05-17 14:09:22 -0700 | [diff] [blame] | 496 | src_sz = hweight_long(srcvm_bits) * sizeof(*src); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 497 | mem_to_map_sz = sizeof(*mem_to_map); |
| 498 | dest_sz = dest_cnt * sizeof(*destvm); |
| 499 | ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) + |
| 500 | ALIGN(dest_sz, SZ_64); |
| 501 | |
Stephen Boyd | 6e37ccf | 2019-05-17 14:09:21 -0700 | [diff] [blame] | 502 | ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_dma, GFP_KERNEL); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 503 | if (!ptr) |
| 504 | return -ENOMEM; |
Stephen Boyd | 6e37ccf | 2019-05-17 14:09:21 -0700 | [diff] [blame] | 505 | ptr_phys = dma_to_phys(__scm->dev, ptr_dma); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 506 | |
| 507 | /* Fill source vmid detail */ |
| 508 | src = ptr; |
Stephen Boyd | af311ff | 2019-05-17 14:09:22 -0700 | [diff] [blame] | 509 | i = 0; |
| 510 | for_each_set_bit(b, &srcvm_bits, BITS_PER_LONG) |
| 511 | src[i++] = cpu_to_le32(b); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 512 | |
| 513 | /* Fill details of mem buff to map */ |
| 514 | mem_to_map = ptr + ALIGN(src_sz, SZ_64); |
| 515 | mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64); |
Stephen Boyd | af311ff | 2019-05-17 14:09:22 -0700 | [diff] [blame] | 516 | mem_to_map->mem_addr = cpu_to_le64(mem_addr); |
| 517 | mem_to_map->mem_size = cpu_to_le64(mem_sz); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 518 | |
| 519 | next_vm = 0; |
| 520 | /* Fill details of next vmid detail */ |
| 521 | destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64); |
| 522 | dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64); |
Stephen Boyd | af311ff | 2019-05-17 14:09:22 -0700 | [diff] [blame] | 523 | for (i = 0; i < dest_cnt; i++, destvm++, newvm++) { |
| 524 | destvm->vmid = cpu_to_le32(newvm->vmid); |
| 525 | destvm->perm = cpu_to_le32(newvm->perm); |
| 526 | destvm->ctx = 0; |
| 527 | destvm->ctx_size = 0; |
| 528 | next_vm |= BIT(newvm->vmid); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz, |
| 532 | ptr_phys, src_sz, dest_phys, dest_sz); |
Stephen Boyd | 6e37ccf | 2019-05-17 14:09:21 -0700 | [diff] [blame] | 533 | dma_free_coherent(__scm->dev, ptr_sz, ptr, ptr_dma); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 534 | if (ret) { |
| 535 | dev_err(__scm->dev, |
Stephen Boyd | c8b08fc | 2019-05-17 14:09:23 -0700 | [diff] [blame] | 536 | "Assign memory protection call failed %d\n", ret); |
Avaneesh Kumar Dwivedi | d82bd35 | 2017-10-24 21:22:24 +0530 | [diff] [blame] | 537 | return -EINVAL; |
| 538 | } |
| 539 | |
| 540 | *srcvm = next_vm; |
| 541 | return 0; |
| 542 | } |
| 543 | EXPORT_SYMBOL(qcom_scm_assign_mem); |
| 544 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 545 | static int qcom_scm_probe(struct platform_device *pdev) |
| 546 | { |
| 547 | struct qcom_scm *scm; |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 548 | unsigned long clks; |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 549 | int ret; |
| 550 | |
| 551 | scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL); |
| 552 | if (!scm) |
| 553 | return -ENOMEM; |
| 554 | |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 555 | ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr); |
| 556 | if (ret < 0) |
| 557 | return ret; |
| 558 | |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 559 | clks = (unsigned long)of_device_get_match_data(&pdev->dev); |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 560 | |
| 561 | scm->core_clk = devm_clk_get(&pdev->dev, "core"); |
| 562 | if (IS_ERR(scm->core_clk)) { |
| 563 | if (PTR_ERR(scm->core_clk) == -EPROBE_DEFER) |
| 564 | return PTR_ERR(scm->core_clk); |
| 565 | |
| 566 | if (clks & SCM_HAS_CORE_CLK) { |
| 567 | dev_err(&pdev->dev, "failed to acquire core clk\n"); |
spjoshi@codeaurora.org | ed19b86 | 2016-11-15 17:19:25 -0800 | [diff] [blame] | 568 | return PTR_ERR(scm->core_clk); |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 569 | } |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 570 | |
| 571 | scm->core_clk = NULL; |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 572 | } |
| 573 | |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 574 | scm->iface_clk = devm_clk_get(&pdev->dev, "iface"); |
| 575 | if (IS_ERR(scm->iface_clk)) { |
| 576 | if (PTR_ERR(scm->iface_clk) == -EPROBE_DEFER) |
| 577 | return PTR_ERR(scm->iface_clk); |
| 578 | |
| 579 | if (clks & SCM_HAS_IFACE_CLK) { |
| 580 | dev_err(&pdev->dev, "failed to acquire iface clk\n"); |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 581 | return PTR_ERR(scm->iface_clk); |
| 582 | } |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 583 | |
| 584 | scm->iface_clk = NULL; |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 585 | } |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 586 | |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 587 | scm->bus_clk = devm_clk_get(&pdev->dev, "bus"); |
| 588 | if (IS_ERR(scm->bus_clk)) { |
| 589 | if (PTR_ERR(scm->bus_clk) == -EPROBE_DEFER) |
| 590 | return PTR_ERR(scm->bus_clk); |
| 591 | |
| 592 | if (clks & SCM_HAS_BUS_CLK) { |
| 593 | dev_err(&pdev->dev, "failed to acquire bus clk\n"); |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 594 | return PTR_ERR(scm->bus_clk); |
| 595 | } |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 596 | |
| 597 | scm->bus_clk = NULL; |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 598 | } |
| 599 | |
Bjorn Andersson | dd4fe5b | 2016-06-17 10:40:43 -0700 | [diff] [blame] | 600 | scm->reset.ops = &qcom_scm_pas_reset_ops; |
| 601 | scm->reset.nr_resets = 1; |
| 602 | scm->reset.of_node = pdev->dev.of_node; |
Wei Yongjun | bd4760c | 2016-08-28 16:29:10 +0000 | [diff] [blame] | 603 | ret = devm_reset_controller_register(&pdev->dev, &scm->reset); |
| 604 | if (ret) |
| 605 | return ret; |
Bjorn Andersson | dd4fe5b | 2016-06-17 10:40:43 -0700 | [diff] [blame] | 606 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 607 | /* vote for max clk rate for highest performance */ |
| 608 | ret = clk_set_rate(scm->core_clk, INT_MAX); |
| 609 | if (ret) |
| 610 | return ret; |
| 611 | |
| 612 | __scm = scm; |
| 613 | __scm->dev = &pdev->dev; |
| 614 | |
Kumar Gala | 6b1751a | 2016-06-03 18:25:26 -0500 | [diff] [blame] | 615 | __qcom_scm_init(); |
| 616 | |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 617 | /* |
| 618 | * If requested enable "download mode", from this point on warmboot |
| 619 | * will cause the the boot stages to enter download mode, unless |
| 620 | * disabled below by a clean shutdown/reboot. |
| 621 | */ |
| 622 | if (download_mode) |
| 623 | qcom_scm_set_download_mode(true); |
| 624 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 628 | static void qcom_scm_shutdown(struct platform_device *pdev) |
| 629 | { |
| 630 | /* Clean shutdown, disable download mode to allow normal restart */ |
| 631 | if (download_mode) |
| 632 | qcom_scm_set_download_mode(false); |
| 633 | } |
| 634 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 635 | static const struct of_device_id qcom_scm_dt_match[] = { |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 636 | { .compatible = "qcom,scm-apq8064", |
Andy Gross | b58a2d3 | 2017-01-11 16:58:03 -0600 | [diff] [blame] | 637 | /* FIXME: This should have .data = (void *) SCM_HAS_CORE_CLK */ |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 638 | }, |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 639 | { .compatible = "qcom,scm-apq8084", .data = (void *)(SCM_HAS_CORE_CLK | |
| 640 | SCM_HAS_IFACE_CLK | |
| 641 | SCM_HAS_BUS_CLK) |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 642 | }, |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 643 | { .compatible = "qcom,scm-ipq4019" }, |
| 644 | { .compatible = "qcom,scm-msm8660", .data = (void *) SCM_HAS_CORE_CLK }, |
| 645 | { .compatible = "qcom,scm-msm8960", .data = (void *) SCM_HAS_CORE_CLK }, |
| 646 | { .compatible = "qcom,scm-msm8916", .data = (void *)(SCM_HAS_CORE_CLK | |
| 647 | SCM_HAS_IFACE_CLK | |
| 648 | SCM_HAS_BUS_CLK) |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 649 | }, |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 650 | { .compatible = "qcom,scm-msm8974", .data = (void *)(SCM_HAS_CORE_CLK | |
| 651 | SCM_HAS_IFACE_CLK | |
| 652 | SCM_HAS_BUS_CLK) |
spjoshi@codeaurora.org | ab0822d | 2016-11-15 17:19:24 -0800 | [diff] [blame] | 653 | }, |
Bjorn Andersson | 60cd420 | 2018-08-29 16:15:04 -0700 | [diff] [blame] | 654 | { .compatible = "qcom,scm-msm8996" }, |
| 655 | { .compatible = "qcom,scm" }, |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 656 | {} |
| 657 | }; |
| 658 | |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 659 | static struct platform_driver qcom_scm_driver = { |
| 660 | .driver = { |
| 661 | .name = "qcom_scm", |
| 662 | .of_match_table = qcom_scm_dt_match, |
| 663 | }, |
| 664 | .probe = qcom_scm_probe, |
Bjorn Andersson | 8c1b7dc | 2017-08-14 15:46:18 -0700 | [diff] [blame] | 665 | .shutdown = qcom_scm_shutdown, |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 666 | }; |
| 667 | |
| 668 | static int __init qcom_scm_init(void) |
| 669 | { |
Andy Gross | d0f6fa7 | 2016-06-03 18:25:22 -0500 | [diff] [blame] | 670 | return platform_driver_register(&qcom_scm_driver); |
| 671 | } |
Andy Gross | 6c8e99d | 2016-07-01 23:04:03 -0500 | [diff] [blame] | 672 | subsys_initcall(qcom_scm_init); |