Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1 | /*Qualcomm Secure Execution Environment Communicator (QSEECOM) driver |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2 | * |
Zhen Kong | 581df35 | 2017-04-28 16:13:57 +0800 | [diff] [blame] | 3 | * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved. |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #define pr_fmt(fmt) "QSEECOM: %s: " fmt, __func__ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/debugfs.h> |
| 23 | #include <linux/cdev.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/mutex.h> |
| 28 | #include <linux/io.h> |
Mitchel Humpherys | 4f8be2e | 2012-09-06 10:41:41 -0700 | [diff] [blame] | 29 | #include <linux/msm_ion.h> |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/clk.h> |
| 32 | #include <linux/qseecom.h> |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 33 | #include <linux/elf.h> |
| 34 | #include <linux/firmware.h> |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 35 | #include <linux/freezer.h> |
Mona Hossain | f1f2ed6 | 2012-11-15 19:51:33 -0800 | [diff] [blame] | 36 | #include <linux/scatterlist.h> |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 37 | #include <mach/board.h> |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 38 | #include <mach/msm_bus.h> |
| 39 | #include <mach/msm_bus_board.h> |
| 40 | #include <mach/scm.h> |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 41 | #include <mach/subsystem_restart.h> |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 42 | #include <mach/socinfo.h> |
Mona Hossain | 803c3d9 | 2012-11-21 13:33:42 -0800 | [diff] [blame] | 43 | #include <mach/qseecomi.h> |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 44 | #include <asm/cacheflush.h> |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 45 | #include "qseecom_legacy.h" |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 46 | #include "qseecom_kernel.h" |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 47 | |
Zhen Kong | e3674c6 | 2017-05-02 13:39:41 +0800 | [diff] [blame] | 48 | #define U32_MAX ((u32)~0U) |
| 49 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 50 | #define QSEECOM_DEV "qseecom" |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 51 | #define QSEOS_VERSION_14 0x14 |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 52 | #define QSEEE_VERSION_00 0x400000 |
Mona Hossain | 5b76a62 | 2012-11-15 20:09:08 -0800 | [diff] [blame] | 53 | #define QSEE_VERSION_01 0x401000 |
| 54 | #define QSEE_VERSION_02 0x402000 |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 55 | #define QSEE_VERSION_03 0x403000 |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 56 | #define QSEE_VERSION_04 0x404000 |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 57 | #define QSEE_VERSION_05 0x405000 |
| 58 | |
Mona Hossain | 5b76a62 | 2012-11-15 20:09:08 -0800 | [diff] [blame] | 59 | |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 60 | |
| 61 | #define QSEOS_CHECK_VERSION_CMD 0x00001803 |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 62 | |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 63 | #define QSEE_CE_CLK_100MHZ 100000000 |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 64 | |
Mona Hossain | 13dd892 | 2013-01-03 06:11:09 -0800 | [diff] [blame] | 65 | #define QSEECOM_MAX_SG_ENTRY 512 |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 66 | #define QSEECOM_INVALID_KEY_ID 0xff |
Mona Hossain | f1f2ed6 | 2012-11-15 19:51:33 -0800 | [diff] [blame] | 67 | |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 68 | /* Save partition image hash for authentication check */ |
| 69 | #define SCM_SAVE_PARTITION_HASH_ID 0x01 |
| 70 | |
| 71 | /* Check if enterprise security is activate */ |
| 72 | #define SCM_IS_ACTIVATED_ID 0x02 |
| 73 | |
Zhen Kong | 7812dc1 | 2013-07-09 17:12:55 -0700 | [diff] [blame] | 74 | #define RPMB_SERVICE 0x2000 |
| 75 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 76 | #define QSEECOM_SEND_CMD_CRYPTO_TIMEOUT 2000 |
| 77 | #define QSEECOM_LOAD_APP_CRYPTO_TIMEOUT 2000 |
| 78 | |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 79 | #define U32_MAX ((u32)~0U) |
| 80 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 81 | enum qseecom_clk_definitions { |
| 82 | CLK_DFAB = 0, |
| 83 | CLK_SFPB, |
| 84 | }; |
| 85 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 86 | enum qseecom_client_handle_type { |
AnilKumar Chimata | f732bbe | 2013-07-24 00:44:09 +0530 | [diff] [blame] | 87 | QSEECOM_CLIENT_APP = 1, |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 88 | QSEECOM_LISTENER_SERVICE, |
| 89 | QSEECOM_SECURE_SERVICE, |
| 90 | QSEECOM_GENERIC, |
AnilKumar Chimata | f732bbe | 2013-07-24 00:44:09 +0530 | [diff] [blame] | 91 | QSEECOM_UNAVAILABLE_CLIENT_APP, |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 94 | enum qseecom_ce_hw_instance { |
| 95 | CLK_QSEE = 0, |
| 96 | CLK_CE_DRV, |
| 97 | }; |
| 98 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 99 | static struct class *driver_class; |
| 100 | static dev_t qseecom_device_no; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 101 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 102 | static DEFINE_MUTEX(qsee_bw_mutex); |
| 103 | static DEFINE_MUTEX(app_access_lock); |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 104 | static DEFINE_MUTEX(clk_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 105 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 106 | struct qseecom_registered_listener_list { |
| 107 | struct list_head list; |
| 108 | struct qseecom_register_listener_req svc; |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 109 | uint32_t user_virt_sb_base; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 110 | u8 *sb_virt; |
| 111 | s32 sb_phys; |
| 112 | size_t sb_length; |
| 113 | struct ion_handle *ihandle; /* Retrieve phy addr */ |
| 114 | |
| 115 | wait_queue_head_t rcv_req_wq; |
| 116 | int rcv_req_flag; |
| 117 | }; |
| 118 | |
| 119 | struct qseecom_registered_app_list { |
| 120 | struct list_head list; |
| 121 | u32 app_id; |
| 122 | u32 ref_cnt; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 123 | char app_name[MAX_APP_NAME_SIZE]; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 126 | struct qseecom_registered_kclient_list { |
| 127 | struct list_head list; |
| 128 | struct qseecom_handle *handle; |
| 129 | }; |
| 130 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 131 | struct ce_hw_usage_info { |
| 132 | uint32_t qsee_ce_hw_instance; |
| 133 | uint32_t hlos_ce_hw_instance; |
| 134 | uint32_t disk_encrypt_pipe; |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 135 | uint32_t file_encrypt_pipe; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 138 | struct qseecom_clk { |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 139 | enum qseecom_ce_hw_instance instance; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 140 | struct clk *ce_core_clk; |
| 141 | struct clk *ce_clk; |
| 142 | struct clk *ce_core_src_clk; |
| 143 | struct clk *ce_bus_clk; |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 144 | uint32_t clk_access_cnt; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 147 | struct qseecom_control { |
| 148 | struct ion_client *ion_clnt; /* Ion client */ |
| 149 | struct list_head registered_listener_list_head; |
| 150 | spinlock_t registered_listener_list_lock; |
| 151 | |
| 152 | struct list_head registered_app_list_head; |
| 153 | spinlock_t registered_app_list_lock; |
| 154 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 155 | struct list_head registered_kclient_list_head; |
| 156 | spinlock_t registered_kclient_list_lock; |
| 157 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 158 | wait_queue_head_t send_resp_wq; |
| 159 | int send_resp_flag; |
| 160 | |
| 161 | uint32_t qseos_version; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 162 | uint32_t qsee_version; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 163 | struct device *pdev; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 164 | bool commonlib_loaded; |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 165 | struct ion_handle *cmnlib_ion_handle; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 166 | struct ce_hw_usage_info ce_info; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 167 | |
| 168 | int qsee_bw_count; |
| 169 | int qsee_sfpb_bw_count; |
| 170 | |
| 171 | uint32_t qsee_perf_client; |
| 172 | struct qseecom_clk qsee; |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 173 | struct qseecom_clk ce_drv; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 174 | |
| 175 | bool support_bus_scaling; |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 176 | bool support_fde; |
| 177 | bool support_pfe; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 178 | uint32_t cumulative_mode; |
| 179 | enum qseecom_bandwidth_request_mode current_mode; |
| 180 | struct timer_list bw_scale_down_timer; |
| 181 | struct work_struct bw_inactive_req_ws; |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 182 | struct cdev cdev; |
| 183 | bool timer_running; |
Zhen Kong | 56d6264 | 2015-03-10 16:29:53 -0700 | [diff] [blame] | 184 | bool appsbl_qseecom_support; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct qseecom_client_handle { |
| 188 | u32 app_id; |
| 189 | u8 *sb_virt; |
| 190 | s32 sb_phys; |
| 191 | uint32_t user_virt_sb_base; |
| 192 | size_t sb_length; |
| 193 | struct ion_handle *ihandle; /* Retrieve phy addr */ |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 194 | char app_name[MAX_APP_NAME_SIZE]; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | struct qseecom_listener_handle { |
| 198 | u32 id; |
| 199 | }; |
| 200 | |
| 201 | static struct qseecom_control qseecom; |
| 202 | |
| 203 | struct qseecom_dev_handle { |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 204 | enum qseecom_client_handle_type type; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 205 | union { |
| 206 | struct qseecom_client_handle client; |
| 207 | struct qseecom_listener_handle listener; |
| 208 | }; |
| 209 | bool released; |
| 210 | int abort; |
| 211 | wait_queue_head_t abort_wq; |
| 212 | atomic_t ioctl_count; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 213 | bool perf_enabled; |
| 214 | bool fast_load_enabled; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 215 | enum qseecom_bandwidth_request_mode mode; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 218 | enum qseecom_set_clear_key_flag { |
| 219 | QSEECOM_CLEAR_CE_KEY_CMD = 0, |
| 220 | QSEECOM_SET_CE_KEY_CMD, |
| 221 | }; |
| 222 | |
| 223 | struct qseecom_set_key_parameter { |
| 224 | uint32_t ce_hw; |
| 225 | uint32_t pipe; |
| 226 | uint32_t flags; |
| 227 | uint8_t key_id[QSEECOM_KEY_ID_SIZE]; |
| 228 | unsigned char hash32[QSEECOM_HASH_SIZE]; |
| 229 | enum qseecom_set_clear_key_flag set_clear_key_flag; |
| 230 | }; |
| 231 | |
Mona Hossain | f1f2ed6 | 2012-11-15 19:51:33 -0800 | [diff] [blame] | 232 | struct qseecom_sg_entry { |
| 233 | uint32_t phys_addr; |
| 234 | uint32_t len; |
| 235 | }; |
| 236 | |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 237 | struct qseecom_key_id_usage_desc { |
| 238 | uint8_t desc[QSEECOM_KEY_ID_SIZE]; |
| 239 | }; |
| 240 | |
| 241 | static struct qseecom_key_id_usage_desc key_id_array[] = { |
| 242 | { |
| 243 | .desc = "Undefined Usage Index", |
| 244 | }, |
| 245 | |
| 246 | { |
| 247 | .desc = "Full Disk Encryption", |
| 248 | }, |
| 249 | |
| 250 | { |
| 251 | .desc = "Per File Encryption", |
| 252 | }, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 255 | /* Function proto types */ |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 256 | static int qsee_vote_for_clock(struct qseecom_dev_handle *, int32_t); |
| 257 | static void qsee_disable_clock_vote(struct qseecom_dev_handle *, int32_t); |
Zhen Kong | 7812dc1 | 2013-07-09 17:12:55 -0700 | [diff] [blame] | 258 | static int __qseecom_enable_clk(enum qseecom_ce_hw_instance ce); |
| 259 | static void __qseecom_disable_clk(enum qseecom_ce_hw_instance ce); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 260 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 261 | static int __qseecom_is_svc_unique(struct qseecom_dev_handle *data, |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 262 | struct qseecom_register_listener_req *svc) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 263 | { |
| 264 | struct qseecom_registered_listener_list *ptr; |
| 265 | int unique = 1; |
| 266 | unsigned long flags; |
| 267 | |
| 268 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 269 | list_for_each_entry(ptr, &qseecom.registered_listener_list_head, list) { |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 270 | if (ptr->svc.listener_id == svc->listener_id) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 271 | pr_err("Service id: %u is already registered\n", |
| 272 | ptr->svc.listener_id); |
| 273 | unique = 0; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 278 | return unique; |
| 279 | } |
| 280 | |
| 281 | static struct qseecom_registered_listener_list *__qseecom_find_svc( |
| 282 | int32_t listener_id) |
| 283 | { |
| 284 | struct qseecom_registered_listener_list *entry = NULL; |
| 285 | unsigned long flags; |
| 286 | |
| 287 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 288 | list_for_each_entry(entry, &qseecom.registered_listener_list_head, list) |
| 289 | { |
| 290 | if (entry->svc.listener_id == listener_id) |
| 291 | break; |
| 292 | } |
| 293 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
AnilKumar Chimata | 69b857a | 2013-07-23 23:03:26 +0530 | [diff] [blame] | 294 | |
| 295 | if ((entry != NULL) && (entry->svc.listener_id != listener_id)) { |
| 296 | pr_err("Service id: %u is not found\n", listener_id); |
| 297 | return NULL; |
| 298 | } |
| 299 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 300 | return entry; |
| 301 | } |
| 302 | |
| 303 | static int __qseecom_set_sb_memory(struct qseecom_registered_listener_list *svc, |
| 304 | struct qseecom_dev_handle *handle, |
| 305 | struct qseecom_register_listener_req *listener) |
| 306 | { |
| 307 | int ret = 0; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 308 | struct qseecom_register_listener_ireq req; |
| 309 | struct qseecom_command_scm_resp resp; |
| 310 | ion_phys_addr_t pa; |
| 311 | |
| 312 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 313 | svc->ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
| 314 | listener->ifd_data_fd); |
Hariprasad Dhalinarasimha | 1401bc0 | 2014-02-18 13:46:37 -0800 | [diff] [blame] | 315 | if (IS_ERR_OR_NULL(svc->ihandle)) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 316 | pr_err("Ion client could not retrieve the handle\n"); |
| 317 | return -ENOMEM; |
| 318 | } |
| 319 | |
| 320 | /* Get the physical address of the ION BUF */ |
| 321 | ret = ion_phys(qseecom.ion_clnt, svc->ihandle, &pa, &svc->sb_length); |
Zhen Kong | 05ed446 | 2014-01-28 18:21:30 -0800 | [diff] [blame] | 322 | if (ret) { |
| 323 | pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n", |
| 324 | ret); |
| 325 | return ret; |
| 326 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 327 | /* Populate the structure for sending scm call to load image */ |
Mitchel Humpherys | 911b4b7 | 2012-09-12 14:42:50 -0700 | [diff] [blame] | 328 | svc->sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, svc->ihandle); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 329 | svc->sb_phys = pa; |
| 330 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 331 | req.qsee_cmd_id = QSEOS_REGISTER_LISTENER; |
| 332 | req.listener_id = svc->svc.listener_id; |
| 333 | req.sb_len = svc->sb_length; |
| 334 | req.sb_ptr = (void *)svc->sb_phys; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 335 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 336 | resp.result = QSEOS_RESULT_INCOMPLETE; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 337 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 338 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 339 | sizeof(req), &resp, sizeof(resp)); |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 340 | if (ret) { |
| 341 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 342 | return -EINVAL; |
| 343 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 344 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 345 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 346 | pr_err("Error SB registration req: resp.result = %d\n", |
| 347 | resp.result); |
| 348 | return -EPERM; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 349 | } |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int qseecom_register_listener(struct qseecom_dev_handle *data, |
| 354 | void __user *argp) |
| 355 | { |
| 356 | int ret = 0; |
| 357 | unsigned long flags; |
| 358 | struct qseecom_register_listener_req rcvd_lstnr; |
| 359 | struct qseecom_registered_listener_list *new_entry; |
| 360 | |
| 361 | ret = copy_from_user(&rcvd_lstnr, argp, sizeof(rcvd_lstnr)); |
| 362 | if (ret) { |
| 363 | pr_err("copy_from_user failed\n"); |
| 364 | return ret; |
| 365 | } |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 366 | if (!access_ok(VERIFY_WRITE, (void __user *)rcvd_lstnr.virt_sb_base, |
| 367 | rcvd_lstnr.sb_size)) |
| 368 | return -EFAULT; |
| 369 | |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 370 | data->listener.id = 0; |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 371 | if (!__qseecom_is_svc_unique(data, &rcvd_lstnr)) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 372 | pr_err("Service is not unique and is already registered\n"); |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 373 | data->released = true; |
| 374 | return -EBUSY; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL); |
| 378 | if (!new_entry) { |
| 379 | pr_err("kmalloc failed\n"); |
| 380 | return -ENOMEM; |
| 381 | } |
| 382 | memcpy(&new_entry->svc, &rcvd_lstnr, sizeof(rcvd_lstnr)); |
| 383 | new_entry->rcv_req_flag = 0; |
| 384 | |
| 385 | new_entry->svc.listener_id = rcvd_lstnr.listener_id; |
| 386 | new_entry->sb_length = rcvd_lstnr.sb_size; |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 387 | new_entry->user_virt_sb_base = rcvd_lstnr.virt_sb_base; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 388 | if (__qseecom_set_sb_memory(new_entry, data, &rcvd_lstnr)) { |
| 389 | pr_err("qseecom_set_sb_memoryfailed\n"); |
| 390 | kzfree(new_entry); |
| 391 | return -ENOMEM; |
| 392 | } |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 393 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 394 | data->listener.id = rcvd_lstnr.listener_id; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 395 | init_waitqueue_head(&new_entry->rcv_req_wq); |
| 396 | |
| 397 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 398 | list_add_tail(&new_entry->list, &qseecom.registered_listener_list_head); |
| 399 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 400 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static int qseecom_unregister_listener(struct qseecom_dev_handle *data) |
| 405 | { |
| 406 | int ret = 0; |
| 407 | unsigned long flags; |
| 408 | uint32_t unmap_mem = 0; |
| 409 | struct qseecom_register_listener_ireq req; |
| 410 | struct qseecom_registered_listener_list *ptr_svc = NULL; |
| 411 | struct qseecom_command_scm_resp resp; |
| 412 | struct ion_handle *ihandle = NULL; /* Retrieve phy addr */ |
| 413 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 414 | req.qsee_cmd_id = QSEOS_DEREGISTER_LISTENER; |
| 415 | req.listener_id = data->listener.id; |
| 416 | resp.result = QSEOS_RESULT_INCOMPLETE; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 417 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 418 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 419 | sizeof(req), &resp, sizeof(resp)); |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 420 | if (ret) { |
| 421 | pr_err("scm_call() failed with err: %d (lstnr id=%d)\n", |
| 422 | ret, data->listener.id); |
| 423 | return ret; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 424 | } |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 425 | |
| 426 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 427 | pr_err("Failed resp.result=%d,(lstnr id=%d)\n", |
| 428 | resp.result, data->listener.id); |
| 429 | return -EPERM; |
| 430 | } |
| 431 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 432 | data->abort = 1; |
| 433 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 434 | list_for_each_entry(ptr_svc, &qseecom.registered_listener_list_head, |
| 435 | list) { |
| 436 | if (ptr_svc->svc.listener_id == data->listener.id) { |
| 437 | wake_up_all(&ptr_svc->rcv_req_wq); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 442 | |
| 443 | while (atomic_read(&data->ioctl_count) > 1) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 444 | if (wait_event_freezable(data->abort_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 445 | atomic_read(&data->ioctl_count) <= 1)) { |
| 446 | pr_err("Interrupted from abort\n"); |
| 447 | ret = -ERESTARTSYS; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 453 | list_for_each_entry(ptr_svc, |
| 454 | &qseecom.registered_listener_list_head, |
| 455 | list) |
| 456 | { |
| 457 | if (ptr_svc->svc.listener_id == data->listener.id) { |
| 458 | if (ptr_svc->sb_virt) { |
| 459 | unmap_mem = 1; |
| 460 | ihandle = ptr_svc->ihandle; |
| 461 | } |
| 462 | list_del(&ptr_svc->list); |
| 463 | kzfree(ptr_svc); |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 468 | |
| 469 | /* Unmap the memory */ |
| 470 | if (unmap_mem) { |
| 471 | if (!IS_ERR_OR_NULL(ihandle)) { |
| 472 | ion_unmap_kernel(qseecom.ion_clnt, ihandle); |
| 473 | ion_free(qseecom.ion_clnt, ihandle); |
| 474 | } |
| 475 | } |
| 476 | data->released = true; |
| 477 | return ret; |
| 478 | } |
| 479 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 480 | static int __qseecom_set_msm_bus_request(uint32_t mode) |
| 481 | { |
| 482 | int ret = 0; |
| 483 | struct qseecom_clk *qclk; |
| 484 | |
| 485 | qclk = &qseecom.qsee; |
| 486 | if (qclk->ce_core_src_clk != NULL) { |
| 487 | if (mode == INACTIVE) { |
| 488 | __qseecom_disable_clk(CLK_QSEE); |
| 489 | } else { |
| 490 | ret = __qseecom_enable_clk(CLK_QSEE); |
| 491 | if (ret) |
| 492 | pr_err("CLK enabling failed (%d) MODE (%d)\n", |
| 493 | ret, mode); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if ((!ret) && (qseecom.current_mode != mode)) { |
| 498 | ret = msm_bus_scale_client_update_request( |
| 499 | qseecom.qsee_perf_client, mode); |
| 500 | if (ret) { |
| 501 | pr_err("Bandwidth req failed(%d) MODE (%d)\n", |
| 502 | ret, mode); |
| 503 | if (qclk->ce_core_src_clk != NULL) { |
| 504 | if (mode == INACTIVE) |
| 505 | __qseecom_enable_clk(CLK_QSEE); |
| 506 | else |
| 507 | __qseecom_disable_clk(CLK_QSEE); |
| 508 | } |
| 509 | } |
| 510 | qseecom.current_mode = mode; |
| 511 | } |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static void qseecom_bw_inactive_req_work(struct work_struct *work) |
| 516 | { |
| 517 | mutex_lock(&app_access_lock); |
| 518 | mutex_lock(&qsee_bw_mutex); |
| 519 | __qseecom_set_msm_bus_request(INACTIVE); |
| 520 | pr_debug("current_mode = %d, cumulative_mode = %d\n", |
| 521 | qseecom.current_mode, qseecom.cumulative_mode); |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 522 | qseecom.timer_running = false; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 523 | mutex_unlock(&qsee_bw_mutex); |
| 524 | mutex_unlock(&app_access_lock); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | static void qseecom_scale_bus_bandwidth_timer_callback(unsigned long data) |
| 529 | { |
| 530 | schedule_work(&qseecom.bw_inactive_req_ws); |
| 531 | return; |
| 532 | } |
| 533 | |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 534 | static int __qseecom_decrease_clk_ref_count(enum qseecom_ce_hw_instance ce) |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 535 | { |
| 536 | struct qseecom_clk *qclk; |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 537 | int ret = 0; |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 538 | mutex_lock(&clk_access_lock); |
| 539 | if (ce == CLK_QSEE) |
| 540 | qclk = &qseecom.qsee; |
| 541 | else |
| 542 | qclk = &qseecom.ce_drv; |
| 543 | |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 544 | if (qclk->clk_access_cnt > 2) { |
| 545 | pr_err("Invalid clock ref count %d\n", qclk->clk_access_cnt); |
| 546 | ret = -EINVAL; |
| 547 | goto err_dec_ref_cnt; |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 548 | } |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 549 | if (qclk->clk_access_cnt == 2) |
| 550 | qclk->clk_access_cnt--; |
| 551 | |
| 552 | err_dec_ref_cnt: |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 553 | mutex_unlock(&clk_access_lock); |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 554 | return ret; |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 558 | static int qseecom_scale_bus_bandwidth_timer(uint32_t mode) |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 559 | { |
| 560 | int32_t ret = 0; |
| 561 | int32_t request_mode = INACTIVE; |
| 562 | |
| 563 | mutex_lock(&qsee_bw_mutex); |
| 564 | if (mode == 0) { |
| 565 | if (qseecom.cumulative_mode > MEDIUM) |
| 566 | request_mode = HIGH; |
| 567 | else |
| 568 | request_mode = qseecom.cumulative_mode; |
| 569 | } else { |
| 570 | request_mode = mode; |
| 571 | } |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 572 | |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 573 | ret = __qseecom_set_msm_bus_request(request_mode); |
| 574 | if (ret) { |
| 575 | pr_err("set msm bus request failed (%d),request_mode (%d)\n", |
| 576 | ret, request_mode); |
| 577 | goto err_scale_timer; |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 578 | } |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 579 | |
| 580 | if (qseecom.timer_running) { |
| 581 | ret = __qseecom_decrease_clk_ref_count(CLK_QSEE); |
| 582 | if (ret) { |
| 583 | pr_err("Failed to decrease clk ref count.\n"); |
| 584 | goto err_scale_timer; |
| 585 | } |
| 586 | del_timer_sync(&(qseecom.bw_scale_down_timer)); |
| 587 | qseecom.timer_running = false; |
| 588 | } |
| 589 | err_scale_timer: |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 590 | mutex_unlock(&qsee_bw_mutex); |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | static int qseecom_unregister_bus_bandwidth_needs( |
| 596 | struct qseecom_dev_handle *data) |
| 597 | { |
| 598 | int32_t ret = 0; |
| 599 | |
| 600 | qseecom.cumulative_mode -= data->mode; |
| 601 | data->mode = INACTIVE; |
| 602 | |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | static int __qseecom_register_bus_bandwidth_needs( |
| 607 | struct qseecom_dev_handle *data, uint32_t request_mode) |
| 608 | { |
| 609 | int32_t ret = 0; |
| 610 | |
| 611 | if (data->mode == INACTIVE) { |
| 612 | qseecom.cumulative_mode += request_mode; |
| 613 | data->mode = request_mode; |
| 614 | } else { |
| 615 | if (data->mode != request_mode) { |
| 616 | qseecom.cumulative_mode -= data->mode; |
| 617 | qseecom.cumulative_mode += request_mode; |
| 618 | data->mode = request_mode; |
| 619 | } |
| 620 | } |
| 621 | return ret; |
| 622 | } |
| 623 | |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 624 | static int qseecom_perf_enable(struct qseecom_dev_handle *data) |
| 625 | { |
| 626 | int ret = 0; |
| 627 | ret = qsee_vote_for_clock(data, CLK_DFAB); |
| 628 | if (ret) { |
| 629 | pr_err("Failed to vote for DFAB clock with err %d\n", ret); |
| 630 | goto perf_enable_exit; |
| 631 | } |
| 632 | ret = qsee_vote_for_clock(data, CLK_SFPB); |
| 633 | if (ret) { |
| 634 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 635 | pr_err("Failed to vote for SFPB clock with err %d\n", ret); |
| 636 | goto perf_enable_exit; |
| 637 | } |
| 638 | |
| 639 | perf_enable_exit: |
| 640 | return ret; |
| 641 | } |
| 642 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 643 | static int qseecom_scale_bus_bandwidth(struct qseecom_dev_handle *data, |
| 644 | void __user *argp) |
| 645 | { |
| 646 | int32_t ret = 0; |
| 647 | int32_t req_mode; |
| 648 | |
| 649 | ret = copy_from_user(&req_mode, argp, sizeof(req_mode)); |
| 650 | if (ret) { |
| 651 | pr_err("copy_from_user failed\n"); |
| 652 | return ret; |
| 653 | } |
| 654 | if (req_mode > HIGH) { |
| 655 | pr_err("Invalid bandwidth mode (%d)\n", req_mode); |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 656 | return -EINVAL; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 657 | } |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 658 | |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 659 | /* |
| 660 | * Register bus bandwidth needs if bus scaling feature is enabled; |
| 661 | * otherwise, qseecom enable/disable clocks for the client directly. |
| 662 | */ |
| 663 | if (qseecom.support_bus_scaling) { |
| 664 | mutex_lock(&qsee_bw_mutex); |
| 665 | ret = __qseecom_register_bus_bandwidth_needs(data, req_mode); |
| 666 | mutex_unlock(&qsee_bw_mutex); |
| 667 | } else { |
| 668 | pr_debug("Bus scaling feature is NOT enabled\n"); |
| 669 | pr_debug("request bandwidth mode %d for the client\n", |
| 670 | req_mode); |
| 671 | if (req_mode != INACTIVE) { |
| 672 | ret = qseecom_perf_enable(data); |
| 673 | if (ret) |
| 674 | pr_err("Failed to vote for clock with err %d\n", |
| 675 | ret); |
| 676 | } else { |
| 677 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 678 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 679 | } |
| 680 | } |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 681 | return ret; |
| 682 | } |
| 683 | |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 684 | static void __qseecom_add_bw_scale_down_timer(uint32_t duration) |
| 685 | { |
| 686 | mutex_lock(&qsee_bw_mutex); |
| 687 | qseecom.bw_scale_down_timer.expires = jiffies + |
| 688 | msecs_to_jiffies(duration); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 689 | mod_timer(&(qseecom.bw_scale_down_timer), |
| 690 | qseecom.bw_scale_down_timer.expires); |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 691 | qseecom.timer_running = true; |
| 692 | mutex_unlock(&qsee_bw_mutex); |
| 693 | } |
| 694 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 695 | static void __qseecom_disable_clk_scale_down(struct qseecom_dev_handle *data) |
| 696 | { |
| 697 | if (!qseecom.support_bus_scaling) |
| 698 | qsee_disable_clock_vote(data, CLK_SFPB); |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 699 | else |
| 700 | __qseecom_add_bw_scale_down_timer( |
| 701 | QSEECOM_LOAD_APP_CRYPTO_TIMEOUT); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | |
| 705 | static int __qseecom_enable_clk_scale_up(struct qseecom_dev_handle *data) |
| 706 | { |
| 707 | int ret = 0; |
| 708 | if (qseecom.support_bus_scaling) { |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 709 | ret = qseecom_scale_bus_bandwidth_timer(MEDIUM); |
| 710 | if (ret) |
| 711 | pr_err("Failed to set bw MEDIUM.\n"); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 712 | } else { |
| 713 | ret = qsee_vote_for_clock(data, CLK_SFPB); |
| 714 | if (ret) |
| 715 | pr_err("Fail vote for clk SFPB ret %d\n", ret); |
| 716 | } |
| 717 | return ret; |
| 718 | } |
| 719 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 720 | static int qseecom_set_client_mem_param(struct qseecom_dev_handle *data, |
| 721 | void __user *argp) |
| 722 | { |
| 723 | ion_phys_addr_t pa; |
| 724 | int32_t ret; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 725 | struct qseecom_set_sb_mem_param_req req; |
| 726 | uint32_t len; |
| 727 | |
| 728 | /* Copy the relevant information needed for loading the image */ |
Hariprasad Dhalinarasimha | e201495 | 2013-10-01 18:25:21 -0700 | [diff] [blame] | 729 | if (copy_from_user(&req, (void __user *)argp, sizeof(req))) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 730 | return -EFAULT; |
| 731 | |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 732 | if ((req.ifd_data_fd <= 0) || (req.virt_sb_base == 0) || |
| 733 | (req.sb_len == 0)) { |
| 734 | pr_err("Inavlid input(s)ion_fd(%d), sb_len(%d), vaddr(0x%x)\n", |
| 735 | req.ifd_data_fd, req.sb_len, req.virt_sb_base); |
| 736 | return -EFAULT; |
| 737 | } |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 738 | if (!access_ok(VERIFY_WRITE, (void __user *)req.virt_sb_base, |
| 739 | req.sb_len)) |
| 740 | return -EFAULT; |
| 741 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 742 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 743 | data->client.ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
| 744 | req.ifd_data_fd); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 745 | if (IS_ERR_OR_NULL(data->client.ihandle)) { |
| 746 | pr_err("Ion client could not retrieve the handle\n"); |
| 747 | return -ENOMEM; |
| 748 | } |
| 749 | /* Get the physical address of the ION BUF */ |
| 750 | ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len); |
Zhen Kong | 05ed446 | 2014-01-28 18:21:30 -0800 | [diff] [blame] | 751 | if (ret) { |
| 752 | |
| 753 | pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n", |
| 754 | ret); |
| 755 | return ret; |
| 756 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 757 | /* Populate the structure for sending scm call to load image */ |
| 758 | data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, |
Mitchel Humpherys | 911b4b7 | 2012-09-12 14:42:50 -0700 | [diff] [blame] | 759 | data->client.ihandle); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 760 | data->client.sb_phys = pa; |
| 761 | data->client.sb_length = req.sb_len; |
| 762 | data->client.user_virt_sb_base = req.virt_sb_base; |
| 763 | return 0; |
| 764 | } |
| 765 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 766 | static int __qseecom_listener_has_sent_rsp(struct qseecom_dev_handle *data) |
| 767 | { |
| 768 | int ret; |
| 769 | ret = (qseecom.send_resp_flag != 0); |
| 770 | return ret || data->abort; |
| 771 | } |
| 772 | |
| 773 | static int __qseecom_process_incomplete_cmd(struct qseecom_dev_handle *data, |
| 774 | struct qseecom_command_scm_resp *resp) |
| 775 | { |
| 776 | int ret = 0; |
Mona Hossain | 0b3a279 | 2013-02-05 17:38:55 -0800 | [diff] [blame] | 777 | int rc = 0; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 778 | uint32_t lstnr; |
| 779 | unsigned long flags; |
| 780 | struct qseecom_client_listener_data_irsp send_data_rsp; |
| 781 | struct qseecom_registered_listener_list *ptr_svc = NULL; |
Mona Hossain | 91da2c5 | 2013-03-29 17:28:31 -0700 | [diff] [blame] | 782 | sigset_t new_sigset; |
| 783 | sigset_t old_sigset; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 784 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 785 | while (resp->result == QSEOS_RESULT_INCOMPLETE) { |
| 786 | lstnr = resp->data; |
| 787 | /* |
| 788 | * Wake up blocking lsitener service with the lstnr id |
| 789 | */ |
| 790 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, |
| 791 | flags); |
| 792 | list_for_each_entry(ptr_svc, |
| 793 | &qseecom.registered_listener_list_head, list) { |
| 794 | if (ptr_svc->svc.listener_id == lstnr) { |
| 795 | ptr_svc->rcv_req_flag = 1; |
| 796 | wake_up_interruptible(&ptr_svc->rcv_req_wq); |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, |
| 801 | flags); |
Zhen Kong | c4d4951 | 2013-10-03 13:47:23 -0700 | [diff] [blame] | 802 | |
| 803 | if (ptr_svc == NULL) { |
| 804 | pr_err("Listener Svc %d does not exist\n", lstnr); |
| 805 | return -EINVAL; |
| 806 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 807 | if (ptr_svc->svc.listener_id != lstnr) { |
| 808 | pr_warning("Service requested for does on exist\n"); |
| 809 | return -ERESTARTSYS; |
| 810 | } |
| 811 | pr_debug("waking up rcv_req_wq and " |
| 812 | "waiting for send_resp_wq\n"); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 813 | |
Mona Hossain | 91da2c5 | 2013-03-29 17:28:31 -0700 | [diff] [blame] | 814 | /* initialize the new signal mask with all signals*/ |
| 815 | sigfillset(&new_sigset); |
| 816 | /* block all signals */ |
| 817 | sigprocmask(SIG_SETMASK, &new_sigset, &old_sigset); |
| 818 | |
| 819 | do { |
| 820 | if (!wait_event_freezable(qseecom.send_resp_wq, |
| 821 | __qseecom_listener_has_sent_rsp(data))) |
| 822 | break; |
| 823 | } while (1); |
| 824 | |
| 825 | /* restore signal mask */ |
| 826 | sigprocmask(SIG_SETMASK, &old_sigset, NULL); |
| 827 | if (data->abort) { |
Mona Hossain | eaa69b7 | 2013-04-15 17:20:15 -0700 | [diff] [blame] | 828 | pr_err("Abort clnt %d waiting on lstnr svc %d, ret %d", |
| 829 | data->client.app_id, lstnr, ret); |
Mona Hossain | 91da2c5 | 2013-03-29 17:28:31 -0700 | [diff] [blame] | 830 | rc = -ENODEV; |
Mona Hossain | 0b3a279 | 2013-02-05 17:38:55 -0800 | [diff] [blame] | 831 | send_data_rsp.status = QSEOS_RESULT_FAILURE; |
| 832 | } else { |
| 833 | send_data_rsp.status = QSEOS_RESULT_SUCCESS; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 834 | } |
Mona Hossain | 0b3a279 | 2013-02-05 17:38:55 -0800 | [diff] [blame] | 835 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 836 | qseecom.send_resp_flag = 0; |
| 837 | send_data_rsp.qsee_cmd_id = QSEOS_LISTENER_DATA_RSP_COMMAND; |
| 838 | send_data_rsp.listener_id = lstnr ; |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 839 | if (ptr_svc) |
| 840 | msm_ion_do_cache_op(qseecom.ion_clnt, ptr_svc->ihandle, |
| 841 | ptr_svc->sb_virt, ptr_svc->sb_length, |
| 842 | ION_IOC_CLEAN_INV_CACHES); |
Zhen Kong | 7812dc1 | 2013-07-09 17:12:55 -0700 | [diff] [blame] | 843 | |
| 844 | if (lstnr == RPMB_SERVICE) |
| 845 | __qseecom_enable_clk(CLK_QSEE); |
| 846 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 847 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, |
| 848 | (const void *)&send_data_rsp, |
| 849 | sizeof(send_data_rsp), resp, |
| 850 | sizeof(*resp)); |
| 851 | if (ret) { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 852 | pr_err("scm_call() failed with err: %d (app_id = %d)\n", |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 853 | ret, data->client.app_id); |
Zhen Kong | 7812dc1 | 2013-07-09 17:12:55 -0700 | [diff] [blame] | 854 | if (lstnr == RPMB_SERVICE) |
| 855 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 856 | return ret; |
| 857 | } |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 858 | if ((resp->result != QSEOS_RESULT_SUCCESS) && |
| 859 | (resp->result != QSEOS_RESULT_INCOMPLETE)) { |
| 860 | pr_err("fail:resp res= %d,app_id = %d,lstr = %d\n", |
| 861 | resp->result, data->client.app_id, lstnr); |
| 862 | ret = -EINVAL; |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 863 | } |
Zhen Kong | 7812dc1 | 2013-07-09 17:12:55 -0700 | [diff] [blame] | 864 | if (lstnr == RPMB_SERVICE) |
| 865 | __qseecom_disable_clk(CLK_QSEE); |
| 866 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 867 | } |
Mona Hossain | 0b3a279 | 2013-02-05 17:38:55 -0800 | [diff] [blame] | 868 | if (rc) |
| 869 | return rc; |
| 870 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 871 | return ret; |
| 872 | } |
| 873 | |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 874 | static int __qseecom_check_app_exists(struct qseecom_check_app_ireq req) |
| 875 | { |
| 876 | int32_t ret; |
| 877 | struct qseecom_command_scm_resp resp; |
| 878 | |
| 879 | /* SCM_CALL to check if app_id for the mentioned app exists */ |
| 880 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 881 | sizeof(struct qseecom_check_app_ireq), |
| 882 | &resp, sizeof(resp)); |
| 883 | if (ret) { |
| 884 | pr_err("scm_call to check if app is already loaded failed\n"); |
| 885 | return -EINVAL; |
| 886 | } |
| 887 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 888 | if (resp.result == QSEOS_RESULT_FAILURE) { |
| 889 | return 0; |
| 890 | } else { |
| 891 | switch (resp.resp_type) { |
| 892 | /*qsee returned listener type response */ |
| 893 | case QSEOS_LISTENER_ID: |
| 894 | pr_err("resp type is of listener type instead of app"); |
| 895 | return -EINVAL; |
| 896 | break; |
| 897 | case QSEOS_APP_ID: |
| 898 | return resp.data; |
| 899 | default: |
| 900 | pr_err("invalid resp type (%d) from qsee", |
| 901 | resp.resp_type); |
| 902 | return -ENODEV; |
| 903 | break; |
| 904 | } |
| 905 | } |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 908 | static int qseecom_load_app(struct qseecom_dev_handle *data, void __user *argp) |
| 909 | { |
| 910 | struct qseecom_registered_app_list *entry = NULL; |
| 911 | unsigned long flags = 0; |
| 912 | u32 app_id = 0; |
| 913 | struct ion_handle *ihandle; /* Ion handle */ |
| 914 | struct qseecom_load_img_req load_img_req; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 915 | int32_t ret = 0; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 916 | ion_phys_addr_t pa = 0; |
| 917 | uint32_t len; |
| 918 | struct qseecom_command_scm_resp resp; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 919 | struct qseecom_check_app_ireq req; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 920 | struct qseecom_load_app_ireq load_req; |
Mallikarjuna Reddy Amireddy | 2329a5c | 2017-01-13 12:54:54 +0800 | [diff] [blame] | 921 | bool first_time = false; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 922 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 923 | /* Copy the relevant information needed for loading the image */ |
Hariprasad Dhalinarasimha | e201495 | 2013-10-01 18:25:21 -0700 | [diff] [blame] | 924 | if (copy_from_user(&load_img_req, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 925 | (void __user *)argp, |
| 926 | sizeof(struct qseecom_load_img_req))) { |
| 927 | pr_err("copy_from_user failed\n"); |
| 928 | return -EFAULT; |
| 929 | } |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 930 | |
| 931 | if (qseecom.support_bus_scaling) { |
| 932 | mutex_lock(&qsee_bw_mutex); |
| 933 | ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM); |
| 934 | mutex_unlock(&qsee_bw_mutex); |
| 935 | if (ret) |
| 936 | return ret; |
| 937 | } |
| 938 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 939 | /* Vote for the SFPB clock */ |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 940 | ret = __qseecom_enable_clk_scale_up(data); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 941 | if (ret) |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 942 | goto enable_clk_err; |
| 943 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 944 | req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND; |
Hariprasad Dhalinarasimha | 7a01882 | 2013-10-03 16:52:16 -0700 | [diff] [blame] | 945 | load_img_req.img_name[MAX_APP_NAME_SIZE-1] = '\0'; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 946 | strlcpy(req.app_name, load_img_req.img_name, MAX_APP_NAME_SIZE); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 947 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 948 | ret = __qseecom_check_app_exists(req); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 949 | if (ret < 0) |
| 950 | goto loadapp_err; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 951 | |
AnilKumar Chimata | 5370c7a | 2013-06-12 12:43:55 +0530 | [diff] [blame] | 952 | app_id = ret; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 953 | if (app_id) { |
Mona Hossain | 7c44320 | 2013-04-18 12:08:58 -0700 | [diff] [blame] | 954 | pr_debug("App id %d (%s) already exists\n", app_id, |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 955 | (char *)(req.app_name)); |
| 956 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 957 | list_for_each_entry(entry, |
| 958 | &qseecom.registered_app_list_head, list){ |
| 959 | if (entry->app_id == app_id) { |
| 960 | entry->ref_cnt++; |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | spin_unlock_irqrestore( |
| 965 | &qseecom.registered_app_list_lock, flags); |
Zhen Kong | 3a5eaac | 2014-06-12 12:16:12 -0700 | [diff] [blame] | 966 | ret = 0; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 967 | } else { |
Mallikarjuna Reddy Amireddy | 2329a5c | 2017-01-13 12:54:54 +0800 | [diff] [blame] | 968 | first_time = true; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 969 | pr_warn("App (%s) does'nt exist, loading apps for first time\n", |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 970 | (char *)(load_img_req.img_name)); |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 971 | /* Get the handle of the shared fd */ |
| 972 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 973 | load_img_req.ifd_data_fd); |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 974 | if (IS_ERR_OR_NULL(ihandle)) { |
| 975 | pr_err("Ion client could not retrieve the handle\n"); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 976 | ret = -ENOMEM; |
| 977 | goto loadapp_err; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 978 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 979 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 980 | /* Get the physical address of the ION BUF */ |
| 981 | ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len); |
Zhen Kong | 05ed446 | 2014-01-28 18:21:30 -0800 | [diff] [blame] | 982 | if (ret) { |
| 983 | pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n", |
| 984 | ret); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 985 | goto loadapp_err; |
Zhen Kong | 05ed446 | 2014-01-28 18:21:30 -0800 | [diff] [blame] | 986 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 987 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 988 | /* Populate the structure for sending scm call to load image */ |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 989 | strlcpy(load_req.app_name, load_img_req.img_name, |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 990 | MAX_APP_NAME_SIZE); |
| 991 | load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND; |
| 992 | load_req.mdt_len = load_img_req.mdt_len; |
| 993 | load_req.img_len = load_img_req.img_len; |
| 994 | load_req.phy_addr = pa; |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 995 | msm_ion_do_cache_op(qseecom.ion_clnt, ihandle, NULL, len, |
| 996 | ION_IOC_CLEAN_INV_CACHES); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 997 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 998 | /* SCM_CALL to load the app and get the app_id back */ |
| 999 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req, |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1000 | sizeof(struct qseecom_load_app_ireq), |
| 1001 | &resp, sizeof(resp)); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1002 | if (ret) { |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1003 | pr_err("scm_call to load app failed\n"); |
Hariprasad Dhalinarasimha | 56d3512 | 2013-02-28 17:55:51 -0800 | [diff] [blame] | 1004 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1005 | ion_free(qseecom.ion_clnt, ihandle); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1006 | ret = -EINVAL; |
| 1007 | goto loadapp_err; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | if (resp.result == QSEOS_RESULT_FAILURE) { |
| 1011 | pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n"); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1012 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1013 | ion_free(qseecom.ion_clnt, ihandle); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1014 | ret = -EFAULT; |
| 1015 | goto loadapp_err; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1016 | } |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1017 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1018 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 1019 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1020 | if (ret) { |
| 1021 | pr_err("process_incomplete_cmd failed err: %d\n", |
| 1022 | ret); |
| 1023 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1024 | ion_free(qseecom.ion_clnt, ihandle); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1025 | ret = -EFAULT; |
| 1026 | goto loadapp_err; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 1031 | pr_err("scm_call failed resp.result unknown, %d\n", |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1032 | resp.result); |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1033 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1034 | ion_free(qseecom.ion_clnt, ihandle); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1035 | ret = -EFAULT; |
| 1036 | goto loadapp_err; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | app_id = resp.data; |
| 1040 | |
| 1041 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 1042 | if (!entry) { |
| 1043 | pr_err("kmalloc failed\n"); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1044 | ret = -ENOMEM; |
| 1045 | goto loadapp_err; |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1046 | } |
| 1047 | entry->app_id = app_id; |
| 1048 | entry->ref_cnt = 1; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 1049 | strlcpy(entry->app_name, load_img_req.img_name, |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 1050 | MAX_APP_NAME_SIZE); |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1051 | /* Deallocate the handle */ |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1052 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1053 | ion_free(qseecom.ion_clnt, ihandle); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1054 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1055 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 1056 | list_add_tail(&entry->list, &qseecom.registered_app_list_head); |
| 1057 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, |
| 1058 | flags); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1059 | |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1060 | pr_warn("App with id %d (%s) now loaded\n", app_id, |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1061 | (char *)(load_img_req.img_name)); |
Mona Hossain | 436b75f | 2012-11-20 17:10:40 -0800 | [diff] [blame] | 1062 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1063 | data->client.app_id = app_id; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 1064 | strlcpy(data->client.app_name, load_img_req.img_name, |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 1065 | MAX_APP_NAME_SIZE); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1066 | load_img_req.app_id = app_id; |
| 1067 | if (copy_to_user(argp, &load_img_req, sizeof(load_img_req))) { |
| 1068 | pr_err("copy_to_user failed\n"); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1069 | ret = -EFAULT; |
Mallikarjuna Reddy Amireddy | 2329a5c | 2017-01-13 12:54:54 +0800 | [diff] [blame] | 1070 | if (first_time == true) { |
| 1071 | spin_lock_irqsave( |
| 1072 | &qseecom.registered_app_list_lock, flags); |
| 1073 | list_del(&entry->list); |
| 1074 | spin_unlock_irqrestore( |
| 1075 | &qseecom.registered_app_list_lock, flags); |
| 1076 | kzfree(entry); |
| 1077 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1078 | } |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1079 | |
| 1080 | loadapp_err: |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1081 | __qseecom_disable_clk_scale_down(data); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 1082 | enable_clk_err: |
| 1083 | if (qseecom.support_bus_scaling) { |
| 1084 | mutex_lock(&qsee_bw_mutex); |
| 1085 | qseecom_unregister_bus_bandwidth_needs(data); |
| 1086 | mutex_unlock(&qsee_bw_mutex); |
| 1087 | } |
| 1088 | return ret; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static int __qseecom_cleanup_app(struct qseecom_dev_handle *data) |
| 1092 | { |
| 1093 | wake_up_all(&qseecom.send_resp_wq); |
| 1094 | while (atomic_read(&data->ioctl_count) > 1) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 1095 | if (wait_event_freezable(data->abort_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1096 | atomic_read(&data->ioctl_count) <= 1)) { |
| 1097 | pr_err("Interrupted from abort\n"); |
| 1098 | return -ERESTARTSYS; |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | /* Set unload app */ |
| 1103 | return 1; |
| 1104 | } |
| 1105 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1106 | static int qseecom_unmap_ion_allocated_memory(struct qseecom_dev_handle *data) |
| 1107 | { |
| 1108 | int ret = 0; |
| 1109 | if (!IS_ERR_OR_NULL(data->client.ihandle)) { |
| 1110 | ion_unmap_kernel(qseecom.ion_clnt, data->client.ihandle); |
| 1111 | ion_free(qseecom.ion_clnt, data->client.ihandle); |
| 1112 | data->client.ihandle = NULL; |
| 1113 | } |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 1117 | static int qseecom_unload_app(struct qseecom_dev_handle *data, |
Hariprasad Dhalinarasimha | ba30614 | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 1118 | bool app_crash) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1119 | { |
| 1120 | unsigned long flags; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1121 | unsigned long flags1; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1122 | int ret = 0; |
| 1123 | struct qseecom_command_scm_resp resp; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1124 | struct qseecom_registered_app_list *ptr_app = NULL; |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 1125 | bool unload = false; |
| 1126 | bool found_app = false; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1127 | bool found_dead_app = false; |
| 1128 | |
| 1129 | if (!memcmp(data->client.app_name, "keymaste", strlen("keymaste"))) { |
Zhen Kong | 763307c | 2014-09-12 12:58:13 -0700 | [diff] [blame] | 1130 | pr_debug("Do not unload keymaster app from tz\n"); |
| 1131 | goto unload_exit; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1132 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1133 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 1134 | if (data->client.app_id > 0) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1135 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 1136 | list_for_each_entry(ptr_app, &qseecom.registered_app_list_head, |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 1137 | list) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1138 | if (ptr_app->app_id == data->client.app_id) { |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1139 | if (!memcmp((void *)ptr_app->app_name, |
| 1140 | (void *)data->client.app_name, |
| 1141 | strlen(data->client.app_name))) { |
| 1142 | found_app = true; |
| 1143 | if (app_crash || ptr_app->ref_cnt == 1) |
| 1144 | unload = true; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1145 | break; |
| 1146 | } else { |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1147 | found_dead_app = true; |
| 1148 | break; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, |
| 1153 | flags); |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1154 | if (found_app == false && found_dead_app == false) { |
| 1155 | pr_err("Cannot find app with id = %d (%s)\n", |
| 1156 | data->client.app_id, |
| 1157 | (char *)data->client.app_name); |
Mona Hossain | 1fb538f | 2012-08-30 16:19:38 -0700 | [diff] [blame] | 1158 | return -EINVAL; |
| 1159 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1162 | if (found_dead_app) { |
| 1163 | pr_warn("cleanup app_id %d(%s)\n", data->client.app_id, |
| 1164 | (char *)data->client.app_name); |
| 1165 | __qseecom_cleanup_app(data); |
| 1166 | } |
| 1167 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 1168 | if (unload) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1169 | struct qseecom_unload_app_ireq req; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1170 | /* Populate the structure for sending scm call to load image */ |
| 1171 | req.qsee_cmd_id = QSEOS_APP_SHUTDOWN_COMMAND; |
| 1172 | req.app_id = data->client.app_id; |
| 1173 | |
| 1174 | /* SCM_CALL to unload the app */ |
| 1175 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 1176 | sizeof(struct qseecom_unload_app_ireq), |
| 1177 | &resp, sizeof(resp)); |
| 1178 | if (ret) { |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 1179 | pr_err("scm_call to unload app (id = %d) failed\n", |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 1180 | req.app_id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1181 | return -EFAULT; |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 1182 | } else { |
| 1183 | pr_warn("App id %d now unloaded\n", req.app_id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1184 | } |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1185 | if (resp.result == QSEOS_RESULT_FAILURE) { |
| 1186 | pr_err("app (%d) unload_failed!!\n", |
| 1187 | data->client.app_id); |
| 1188 | return -EFAULT; |
| 1189 | } |
| 1190 | if (resp.result == QSEOS_RESULT_SUCCESS) |
AnilKumar Chimata | 58eddab | 2015-04-17 17:17:35 +0530 | [diff] [blame] | 1191 | pr_debug("App (%d) is unloaded!!\n", |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1192 | data->client.app_id); |
| 1193 | __qseecom_cleanup_app(data); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1194 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 1195 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1196 | if (ret) { |
| 1197 | pr_err("process_incomplete_cmd fail err: %d\n", |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 1198 | ret); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1199 | return ret; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1203 | |
| 1204 | if (found_app) { |
| 1205 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags1); |
| 1206 | if (app_crash) { |
| 1207 | ptr_app->ref_cnt = 0; |
| 1208 | pr_debug("app_crash: ref_count = 0\n"); |
| 1209 | } else { |
| 1210 | if (ptr_app->ref_cnt == 1) { |
| 1211 | ptr_app->ref_cnt = 0; |
AnilKumar Chimata | 58eddab | 2015-04-17 17:17:35 +0530 | [diff] [blame] | 1212 | pr_debug("ref_count set to 0\n"); |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1213 | } else { |
| 1214 | ptr_app->ref_cnt--; |
AnilKumar Chimata | 58eddab | 2015-04-17 17:17:35 +0530 | [diff] [blame] | 1215 | pr_debug("Can't unload app(%d) inuse\n", |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1216 | ptr_app->app_id); |
| 1217 | } |
| 1218 | } |
| 1219 | if (unload) { |
| 1220 | list_del(&ptr_app->list); |
| 1221 | kzfree(ptr_app); |
| 1222 | } |
| 1223 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, |
| 1224 | flags1); |
| 1225 | } |
Zhen Kong | 763307c | 2014-09-12 12:58:13 -0700 | [diff] [blame] | 1226 | unload_exit: |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1227 | qseecom_unmap_ion_allocated_memory(data); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1228 | data->released = true; |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | static uint32_t __qseecom_uvirt_to_kphys(struct qseecom_dev_handle *data, |
| 1233 | uint32_t virt) |
| 1234 | { |
| 1235 | return data->client.sb_phys + (virt - data->client.user_virt_sb_base); |
| 1236 | } |
| 1237 | |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 1238 | static uint32_t __qseecom_uvirt_to_kvirt(struct qseecom_dev_handle *data, |
| 1239 | uint32_t virt) |
| 1240 | { |
| 1241 | return (uint32_t)data->client.sb_virt + |
| 1242 | (virt - data->client.user_virt_sb_base); |
| 1243 | } |
| 1244 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1245 | int __qseecom_process_rpmb_svc_cmd(struct qseecom_dev_handle *data_ptr, |
| 1246 | struct qseecom_send_svc_cmd_req *req_ptr, |
| 1247 | struct qseecom_client_send_service_ireq *send_svc_ireq_ptr) |
| 1248 | { |
| 1249 | int ret = 0; |
Hariprasad Dhalinarasimha | fd86ecd | 2013-09-27 18:38:53 -0700 | [diff] [blame] | 1250 | void *req_buf = NULL; |
| 1251 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1252 | if ((req_ptr == NULL) || (send_svc_ireq_ptr == NULL)) { |
Jeron Susan | 84186d4 | 2016-11-03 10:02:25 +0800 | [diff] [blame] | 1253 | pr_err("Error with pointer: req_ptr = %pK, send_svc_ptr = %pK\n", |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1254 | req_ptr, send_svc_ireq_ptr); |
| 1255 | return -EINVAL; |
| 1256 | } |
Hariprasad Dhalinarasimha | fd86ecd | 2013-09-27 18:38:53 -0700 | [diff] [blame] | 1257 | |
Zhen Kong | 581df35 | 2017-04-28 16:13:57 +0800 | [diff] [blame] | 1258 | |
Hariprasad Dhalinarasimha | 4b61e2a | 2014-02-12 19:43:02 -0800 | [diff] [blame] | 1259 | /* Clients need to ensure req_buf is at base offset of shared buffer */ |
| 1260 | if ((uint32_t)req_ptr->cmd_req_buf != |
| 1261 | data_ptr->client.user_virt_sb_base) { |
| 1262 | pr_err("cmd buf not pointing to base offset of shared buffer\n"); |
Hariprasad Dhalinarasimha | fd86ecd | 2013-09-27 18:38:53 -0700 | [diff] [blame] | 1263 | return -EINVAL; |
| 1264 | } |
| 1265 | |
Zhen Kong | 581df35 | 2017-04-28 16:13:57 +0800 | [diff] [blame] | 1266 | if (data_ptr->client.sb_length < |
| 1267 | sizeof(struct qseecom_rpmb_provision_key)) { |
| 1268 | pr_err("shared buffer is too small to hold key type\n"); |
Hariprasad Dhalinarasimha | fd86ecd | 2013-09-27 18:38:53 -0700 | [diff] [blame] | 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | |
| 1272 | req_buf = data_ptr->client.sb_virt; |
| 1273 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1274 | send_svc_ireq_ptr->qsee_cmd_id = req_ptr->cmd_id; |
| 1275 | send_svc_ireq_ptr->key_type = |
Hariprasad Dhalinarasimha | fd86ecd | 2013-09-27 18:38:53 -0700 | [diff] [blame] | 1276 | ((struct qseecom_rpmb_provision_key *)req_buf)->key_type; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1277 | send_svc_ireq_ptr->req_len = req_ptr->cmd_req_len; |
| 1278 | send_svc_ireq_ptr->rsp_ptr = (void *)(__qseecom_uvirt_to_kphys(data_ptr, |
| 1279 | (uint32_t)req_ptr->resp_buf)); |
| 1280 | send_svc_ireq_ptr->rsp_len = req_ptr->resp_len; |
| 1281 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1282 | return ret; |
| 1283 | } |
| 1284 | |
Zhen Kong | 581df35 | 2017-04-28 16:13:57 +0800 | [diff] [blame] | 1285 | static int __validate_send_service_cmd_inputs(struct qseecom_dev_handle *data, |
| 1286 | struct qseecom_send_svc_cmd_req *req) |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1287 | { |
Zhen Kong | 581df35 | 2017-04-28 16:13:57 +0800 | [diff] [blame] | 1288 | if (!req || !req->resp_buf || !req->cmd_req_buf) { |
| 1289 | pr_err("req or cmd buffer or response buffer is null\n"); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1290 | return -EINVAL; |
| 1291 | } |
| 1292 | |
Hariprasad Dhalinarasimha | 4b61e2a | 2014-02-12 19:43:02 -0800 | [diff] [blame] | 1293 | if (data->client.sb_virt == NULL) { |
| 1294 | pr_err("sb_virt null\n"); |
| 1295 | return -EINVAL; |
| 1296 | } |
| 1297 | |
| 1298 | if (data->client.user_virt_sb_base == 0) { |
| 1299 | pr_err("user_virt_sb_base is null\n"); |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | |
| 1303 | if (data->client.sb_length == 0) { |
| 1304 | pr_err("sb_length is 0\n"); |
| 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | |
Zhen Kong | 581df35 | 2017-04-28 16:13:57 +0800 | [diff] [blame] | 1308 | if (((uintptr_t)req->cmd_req_buf < |
| 1309 | data->client.user_virt_sb_base) || |
| 1310 | ((uintptr_t)req->cmd_req_buf >= |
| 1311 | (data->client.user_virt_sb_base + data->client.sb_length))) { |
| 1312 | pr_err("cmd buffer address not within shared bufffer\n"); |
| 1313 | return -EINVAL; |
| 1314 | } |
| 1315 | if (((uintptr_t)req->resp_buf < |
| 1316 | data->client.user_virt_sb_base) || |
| 1317 | ((uintptr_t)req->resp_buf >= |
| 1318 | (data->client.user_virt_sb_base + data->client.sb_length))) { |
| 1319 | pr_err("response buffer address not within shared bufffer\n"); |
| 1320 | return -EINVAL; |
| 1321 | } |
| 1322 | if ((req->cmd_req_len == 0) || (req->resp_len == 0) || |
| 1323 | (req->cmd_req_len > data->client.sb_length) || |
| 1324 | (req->resp_len > data->client.sb_length)) { |
| 1325 | pr_err("cmd buf length or response buf length not valid\n"); |
| 1326 | return -EINVAL; |
| 1327 | } |
| 1328 | if (req->cmd_req_len > UINT_MAX - req->resp_len) { |
| 1329 | pr_err("Integer overflow detected in req_len & rsp_len\n"); |
| 1330 | return -EINVAL; |
| 1331 | } |
| 1332 | |
| 1333 | if ((req->cmd_req_len + req->resp_len) > data->client.sb_length) { |
| 1334 | pr_debug("Not enough memory to fit cmd_buf.\n"); |
| 1335 | pr_debug("resp_buf. Required: %u, Available: %zu\n", |
| 1336 | (req->cmd_req_len + req->resp_len), |
| 1337 | data->client.sb_length); |
| 1338 | return -ENOMEM; |
| 1339 | } |
| 1340 | if ((uintptr_t)req->cmd_req_buf > (ULONG_MAX - req->cmd_req_len)) { |
| 1341 | pr_err("Integer overflow in req_len & cmd_req_buf\n"); |
| 1342 | return -EINVAL; |
| 1343 | } |
| 1344 | if ((uintptr_t)req->resp_buf > (ULONG_MAX - req->resp_len)) { |
| 1345 | pr_err("Integer overflow in resp_len & resp_buf\n"); |
| 1346 | return -EINVAL; |
| 1347 | } |
| 1348 | if (data->client.user_virt_sb_base > |
| 1349 | (ULONG_MAX - data->client.sb_length)) { |
| 1350 | pr_err("Integer overflow in user_virt_sb_base & sb_length\n"); |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | if ((((uintptr_t)req->cmd_req_buf + req->cmd_req_len) > |
| 1354 | ((uintptr_t)data->client.user_virt_sb_base + |
| 1355 | data->client.sb_length)) || |
| 1356 | (((uintptr_t)req->resp_buf + req->resp_len) > |
| 1357 | ((uintptr_t)data->client.user_virt_sb_base + |
| 1358 | data->client.sb_length))) { |
| 1359 | pr_err("cmd buf or resp buf is out of shared buffer region\n"); |
| 1360 | return -EINVAL; |
| 1361 | } |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | static int qseecom_send_service_cmd(struct qseecom_dev_handle *data, |
| 1366 | void __user *argp) |
| 1367 | { |
| 1368 | int ret = 0; |
| 1369 | struct qseecom_client_send_service_ireq send_svc_ireq; |
| 1370 | struct qseecom_command_scm_resp resp; |
| 1371 | struct qseecom_send_svc_cmd_req req; |
| 1372 | |
| 1373 | /*struct qseecom_command_scm_resp resp;*/ |
| 1374 | |
| 1375 | if (copy_from_user(&req, |
| 1376 | (void __user *)argp, |
| 1377 | sizeof(req))) { |
| 1378 | pr_err("copy_from_user failed\n"); |
| 1379 | return -EFAULT; |
| 1380 | } |
| 1381 | |
| 1382 | if (__validate_send_service_cmd_inputs(data, &req)) |
| 1383 | return -EINVAL; |
| 1384 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1385 | data->type = QSEECOM_SECURE_SERVICE; |
| 1386 | |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1387 | switch (req.cmd_id) { |
Hariprasad Dhalinarasimha | b383224 | 2013-07-23 15:35:26 -0700 | [diff] [blame] | 1388 | case QSEOS_RPMB_PROVISION_KEY_COMMAND: |
| 1389 | case QSEOS_RPMB_ERASE_COMMAND: |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1390 | if (__qseecom_process_rpmb_svc_cmd(data, &req, |
| 1391 | &send_svc_ireq)) |
| 1392 | return -EINVAL; |
| 1393 | break; |
| 1394 | default: |
| 1395 | pr_err("Unsupported cmd_id %d\n", req.cmd_id); |
| 1396 | return -EINVAL; |
| 1397 | } |
AnilKumar Chimata | 80f1d1b | 2013-08-03 05:16:14 +0530 | [diff] [blame] | 1398 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1399 | if (qseecom.support_bus_scaling) { |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 1400 | ret = qseecom_scale_bus_bandwidth_timer(HIGH); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1401 | if (ret) { |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 1402 | pr_err("Fail to set bw HIGH\n"); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1403 | return ret; |
| 1404 | } |
| 1405 | } else { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 1406 | ret = qseecom_perf_enable(data); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1407 | if (ret) { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 1408 | pr_err("Failed to vote for clocks with err %d\n", ret); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1409 | goto exit; |
| 1410 | } |
AnilKumar Chimata | 80f1d1b | 2013-08-03 05:16:14 +0530 | [diff] [blame] | 1411 | } |
| 1412 | |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 1413 | msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle, |
| 1414 | data->client.sb_virt, data->client.sb_length, |
| 1415 | ION_IOC_CLEAN_INV_CACHES); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1416 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *) &send_svc_ireq, |
| 1417 | sizeof(send_svc_ireq), |
| 1418 | &resp, sizeof(resp)); |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 1419 | msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle, |
| 1420 | data->client.sb_virt, data->client.sb_length, |
| 1421 | ION_IOC_INV_CACHES); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1422 | if (ret) { |
| 1423 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1424 | if (!qseecom.support_bus_scaling) { |
| 1425 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 1426 | qsee_disable_clock_vote(data, CLK_SFPB); |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 1427 | } else { |
| 1428 | __qseecom_add_bw_scale_down_timer( |
| 1429 | QSEECOM_SEND_CMD_CRYPTO_TIMEOUT); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1430 | } |
| 1431 | goto exit; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | switch (resp.result) { |
| 1435 | case QSEOS_RESULT_SUCCESS: |
| 1436 | break; |
| 1437 | case QSEOS_RESULT_INCOMPLETE: |
| 1438 | pr_err("qseos_result_incomplete\n"); |
| 1439 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1440 | if (ret) { |
| 1441 | pr_err("process_incomplete_cmd fail: err: %d\n", |
| 1442 | ret); |
| 1443 | } |
| 1444 | break; |
| 1445 | case QSEOS_RESULT_FAILURE: |
| 1446 | pr_err("process_incomplete_cmd failed err: %d\n", ret); |
| 1447 | break; |
| 1448 | default: |
| 1449 | pr_err("Response result %d not supported\n", |
| 1450 | resp.result); |
| 1451 | ret = -EINVAL; |
| 1452 | break; |
| 1453 | } |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 1454 | if (!qseecom.support_bus_scaling) { |
| 1455 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 1456 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 1457 | } else { |
| 1458 | __qseecom_add_bw_scale_down_timer( |
| 1459 | QSEECOM_SEND_CMD_CRYPTO_TIMEOUT); |
| 1460 | } |
| 1461 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 1462 | exit: |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1463 | return ret; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1466 | static int __validate_send_cmd_inputs(struct qseecom_dev_handle *data, |
| 1467 | struct qseecom_send_cmd_req *req) |
| 1468 | { |
| 1469 | if (!data || !data->client.ihandle) { |
| 1470 | pr_err("Client or client handle is not initialized\n"); |
| 1471 | return -EINVAL; |
| 1472 | } |
| 1473 | if (((req->resp_buf == NULL) && (req->resp_len != 0)) || |
| 1474 | (req->cmd_req_buf == NULL)) { |
| 1475 | pr_err("cmd buffer or response buffer is null\n"); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | if (((uint32_t)req->cmd_req_buf < data->client.user_virt_sb_base) || |
| 1479 | ((uint32_t)req->cmd_req_buf >= (data->client.user_virt_sb_base + |
| 1480 | data->client.sb_length))) { |
| 1481 | pr_err("cmd buffer address not within shared bufffer\n"); |
| 1482 | return -EINVAL; |
| 1483 | } |
| 1484 | if (((uintptr_t)req->resp_buf < |
| 1485 | data->client.user_virt_sb_base) || |
| 1486 | ((uintptr_t)req->resp_buf >= |
| 1487 | (data->client.user_virt_sb_base + data->client.sb_length))) { |
| 1488 | pr_err("response buffer address not within shared bufffer\n"); |
| 1489 | return -EINVAL; |
| 1490 | } |
| 1491 | if ((req->cmd_req_len == 0) || |
| 1492 | (req->cmd_req_len > data->client.sb_length) || |
| 1493 | (req->resp_len > data->client.sb_length)) { |
| 1494 | pr_err("cmd buf length or response buf length not valid\n"); |
| 1495 | return -EINVAL; |
| 1496 | } |
| 1497 | if (req->cmd_req_len > UINT_MAX - req->resp_len) { |
| 1498 | pr_err("Integer overflow detected in req_len & rsp_len\n"); |
| 1499 | return -EINVAL; |
| 1500 | } |
| 1501 | |
| 1502 | if ((req->cmd_req_len + req->resp_len) > data->client.sb_length) { |
| 1503 | pr_debug("Not enough memory to fit cmd_buf.\n"); |
| 1504 | pr_debug("resp_buf. Required: %u, Available: %zu\n", |
| 1505 | (req->cmd_req_len + req->resp_len), |
| 1506 | data->client.sb_length); |
| 1507 | return -ENOMEM; |
| 1508 | } |
| 1509 | if ((uintptr_t)req->cmd_req_buf > (ULONG_MAX - req->cmd_req_len)) { |
| 1510 | pr_err("Integer overflow in req_len & cmd_req_buf\n"); |
| 1511 | return -EINVAL; |
| 1512 | } |
| 1513 | if ((uintptr_t)req->resp_buf > (ULONG_MAX - req->resp_len)) { |
| 1514 | pr_err("Integer overflow in resp_len & resp_buf\n"); |
| 1515 | return -EINVAL; |
| 1516 | } |
| 1517 | if (data->client.user_virt_sb_base > |
| 1518 | (ULONG_MAX - data->client.sb_length)) { |
| 1519 | pr_err("Integer overflow in user_virt_sb_base & sb_length\n"); |
| 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | if ((((uintptr_t)req->cmd_req_buf + req->cmd_req_len) > |
| 1523 | ((uintptr_t)data->client.user_virt_sb_base + |
| 1524 | data->client.sb_length)) || |
| 1525 | (((uintptr_t)req->resp_buf + req->resp_len) > |
| 1526 | ((uintptr_t)data->client.user_virt_sb_base + |
| 1527 | data->client.sb_length))) { |
| 1528 | pr_err("cmd buf or resp buf is out of shared buffer region\n"); |
| 1529 | return -EINVAL; |
| 1530 | } |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1534 | static int __qseecom_send_cmd(struct qseecom_dev_handle *data, |
| 1535 | struct qseecom_send_cmd_req *req) |
| 1536 | { |
| 1537 | int ret = 0; |
| 1538 | u32 reqd_len_sb_in = 0; |
| 1539 | struct qseecom_client_send_data_ireq send_data_req; |
| 1540 | struct qseecom_command_scm_resp resp; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1541 | unsigned long flags; |
| 1542 | struct qseecom_registered_app_list *ptr_app; |
| 1543 | bool found_app = false; |
William Clark | 3f378a4 | 2014-07-03 15:32:13 -0700 | [diff] [blame] | 1544 | int name_len = 0; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1545 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1546 | reqd_len_sb_in = req->cmd_req_len + req->resp_len; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1547 | /* find app_id & img_name from list */ |
| 1548 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 1549 | list_for_each_entry(ptr_app, &qseecom.registered_app_list_head, |
| 1550 | list) { |
William Clark | 3f378a4 | 2014-07-03 15:32:13 -0700 | [diff] [blame] | 1551 | name_len = min(strlen(data->client.app_name), |
| 1552 | strlen(ptr_app->app_name)); |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1553 | if ((ptr_app->app_id == data->client.app_id) && |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 1554 | (!memcmp(ptr_app->app_name, |
| 1555 | data->client.app_name, name_len))) { |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 1556 | found_app = true; |
| 1557 | break; |
| 1558 | } |
| 1559 | } |
| 1560 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags); |
| 1561 | |
| 1562 | if (!found_app) { |
| 1563 | pr_err("app_id %d (%s) is not found\n", data->client.app_id, |
| 1564 | (char *)data->client.app_name); |
| 1565 | return -EINVAL; |
| 1566 | } |
| 1567 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1568 | send_data_req.qsee_cmd_id = QSEOS_CLIENT_SEND_DATA_COMMAND; |
| 1569 | send_data_req.app_id = data->client.app_id; |
| 1570 | send_data_req.req_ptr = (void *)(__qseecom_uvirt_to_kphys(data, |
| 1571 | (uint32_t)req->cmd_req_buf)); |
| 1572 | send_data_req.req_len = req->cmd_req_len; |
| 1573 | send_data_req.rsp_ptr = (void *)(__qseecom_uvirt_to_kphys(data, |
| 1574 | (uint32_t)req->resp_buf)); |
| 1575 | send_data_req.rsp_len = req->resp_len; |
| 1576 | |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 1577 | msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle, |
| 1578 | data->client.sb_virt, |
Hariprasad Dhalinarasimha | 528400a | 2013-10-03 16:43:39 -0700 | [diff] [blame] | 1579 | reqd_len_sb_in, |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 1580 | ION_IOC_CLEAN_INV_CACHES); |
| 1581 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1582 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *) &send_data_req, |
| 1583 | sizeof(send_data_req), |
| 1584 | &resp, sizeof(resp)); |
| 1585 | if (ret) { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 1586 | pr_err("scm_call() failed with err: %d (app_id = %d)\n", |
| 1587 | ret, data->client.app_id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1588 | return ret; |
| 1589 | } |
| 1590 | |
| 1591 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 1592 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1593 | if (ret) { |
| 1594 | pr_err("process_incomplete_cmd failed err: %d\n", ret); |
| 1595 | return ret; |
| 1596 | } |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 1597 | } else { |
| 1598 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 1599 | pr_err("Response result %d not supported\n", |
| 1600 | resp.result); |
| 1601 | ret = -EINVAL; |
| 1602 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1603 | } |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 1604 | msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle, |
| 1605 | data->client.sb_virt, data->client.sb_length, |
| 1606 | ION_IOC_INV_CACHES); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1607 | return ret; |
| 1608 | } |
| 1609 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1610 | static int qseecom_send_cmd(struct qseecom_dev_handle *data, void __user *argp) |
| 1611 | { |
| 1612 | int ret = 0; |
| 1613 | struct qseecom_send_cmd_req req; |
| 1614 | |
| 1615 | ret = copy_from_user(&req, argp, sizeof(req)); |
| 1616 | if (ret) { |
| 1617 | pr_err("copy_from_user failed\n"); |
| 1618 | return ret; |
| 1619 | } |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1620 | |
| 1621 | if (__validate_send_cmd_inputs(data, &req)) |
| 1622 | return -EINVAL; |
| 1623 | |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 1624 | ret = __qseecom_send_cmd(data, &req); |
| 1625 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1626 | if (ret) |
| 1627 | return ret; |
| 1628 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1629 | return ret; |
| 1630 | } |
| 1631 | |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1632 | int __boundary_checks_offset(struct qseecom_send_modfd_cmd_req *cmd_req, |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1633 | struct qseecom_send_modfd_listener_resp *lstnr_resp, |
| 1634 | struct qseecom_dev_handle *data, bool listener_svc, |
| 1635 | int i) { |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1636 | |
| 1637 | if ((!listener_svc) && (cmd_req->ifd_data[i].fd > 0)) { |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1638 | if ((cmd_req->cmd_req_len < sizeof(uint32_t)) || |
| 1639 | (cmd_req->ifd_data[i].cmd_buf_offset > |
| 1640 | cmd_req->cmd_req_len - sizeof(uint32_t))) { |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1641 | pr_err("Invalid offset 0x%x\n", |
| 1642 | cmd_req->ifd_data[i].cmd_buf_offset); |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1643 | return -EINVAL; |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } else if ((listener_svc) && (lstnr_resp->ifd_data[i].fd > 0)) { |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1646 | if ((lstnr_resp->resp_len < sizeof(uint32_t)) || |
| 1647 | (lstnr_resp->ifd_data[i].cmd_buf_offset > |
| 1648 | lstnr_resp->resp_len - sizeof(uint32_t))) { |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1649 | pr_err("Invalid offset 0x%x\n", |
| 1650 | lstnr_resp->ifd_data[i].cmd_buf_offset); |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1651 | return -EINVAL; |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1652 | } |
| 1653 | } |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1654 | return 0; |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1657 | #define SG_ENTRY_SZ sizeof(struct qseecom_sg_entry) |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1658 | static int __qseecom_update_cmd_buf(void *msg, bool cleanup, |
| 1659 | struct qseecom_dev_handle *data, |
| 1660 | bool listener_svc) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1661 | { |
| 1662 | struct ion_handle *ihandle; |
| 1663 | char *field; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1664 | int ret = 0; |
| 1665 | int i = 0; |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 1666 | uint32_t len = 0; |
| 1667 | struct scatterlist *sg; |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1668 | struct qseecom_send_modfd_cmd_req *cmd_req = NULL; |
| 1669 | struct qseecom_send_modfd_listener_resp *lstnr_resp = NULL; |
| 1670 | struct qseecom_registered_listener_list *this_lstnr = NULL; |
| 1671 | |
| 1672 | if (msg == NULL) { |
| 1673 | pr_err("Invalid address\n"); |
| 1674 | return -EINVAL; |
| 1675 | } |
| 1676 | if (listener_svc) { |
| 1677 | lstnr_resp = (struct qseecom_send_modfd_listener_resp *)msg; |
| 1678 | this_lstnr = __qseecom_find_svc(data->listener.id); |
| 1679 | if (IS_ERR_OR_NULL(this_lstnr)) { |
| 1680 | pr_err("Invalid listener ID\n"); |
| 1681 | return -ENOMEM; |
| 1682 | } |
| 1683 | } else { |
| 1684 | cmd_req = (struct qseecom_send_modfd_cmd_req *)msg; |
| 1685 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1686 | |
| 1687 | for (i = 0; i < MAX_ION_FD; i++) { |
Mona Hossain | f1f2ed6 | 2012-11-15 19:51:33 -0800 | [diff] [blame] | 1688 | struct sg_table *sg_ptr = NULL; |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1689 | if ((!listener_svc) && (cmd_req->ifd_data[i].fd > 0)) { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1690 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1691 | cmd_req->ifd_data[i].fd); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1692 | if (IS_ERR_OR_NULL(ihandle)) { |
| 1693 | pr_err("Ion client can't retrieve the handle\n"); |
| 1694 | return -ENOMEM; |
| 1695 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1696 | field = (char *) cmd_req->cmd_req_buf + |
| 1697 | cmd_req->ifd_data[i].cmd_buf_offset; |
| 1698 | } else if ((listener_svc) && |
| 1699 | (lstnr_resp->ifd_data[i].fd > 0)) { |
| 1700 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
| 1701 | lstnr_resp->ifd_data[i].fd); |
| 1702 | if (IS_ERR_OR_NULL(ihandle)) { |
| 1703 | pr_err("Ion client can't retrieve the handle\n"); |
| 1704 | return -ENOMEM; |
Mona Hossain | f1f2ed6 | 2012-11-15 19:51:33 -0800 | [diff] [blame] | 1705 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1706 | field = lstnr_resp->resp_buf_ptr + |
| 1707 | lstnr_resp->ifd_data[i].cmd_buf_offset; |
| 1708 | } else { |
| 1709 | return ret; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1710 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1711 | /* Populate the cmd data structure with the phys_addr */ |
| 1712 | sg_ptr = ion_sg_table(qseecom.ion_clnt, ihandle); |
| 1713 | if (sg_ptr == NULL) { |
| 1714 | pr_err("IOn client could not retrieve sg table\n"); |
| 1715 | goto err; |
| 1716 | } |
| 1717 | if (sg_ptr->nents == 0) { |
| 1718 | pr_err("Num of scattered entries is 0\n"); |
| 1719 | goto err; |
| 1720 | } |
| 1721 | if (sg_ptr->nents > QSEECOM_MAX_SG_ENTRY) { |
| 1722 | pr_err("Num of scattered entries"); |
| 1723 | pr_err(" (%d) is greater than max supported %d\n", |
| 1724 | sg_ptr->nents, QSEECOM_MAX_SG_ENTRY); |
| 1725 | goto err; |
| 1726 | } |
| 1727 | sg = sg_ptr->sgl; |
| 1728 | if (sg_ptr->nents == 1) { |
| 1729 | uint32_t *update; |
| 1730 | update = (uint32_t *) field; |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1731 | |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1732 | if (__boundary_checks_offset(cmd_req, lstnr_resp, data, |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1733 | listener_svc, i)) |
| 1734 | goto err; |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1735 | if (cleanup) |
| 1736 | *update = 0; |
| 1737 | else |
| 1738 | *update = (uint32_t)sg_dma_address( |
| 1739 | sg_ptr->sgl); |
| 1740 | len += (uint32_t)sg->length; |
| 1741 | } else { |
| 1742 | struct qseecom_sg_entry *update; |
| 1743 | int j = 0; |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1744 | |
| 1745 | if ((!listener_svc) && (cmd_req->ifd_data[i].fd > 0)) { |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1746 | if ((cmd_req->cmd_req_len < |
| 1747 | SG_ENTRY_SZ * sg_ptr->nents) || |
| 1748 | (cmd_req->ifd_data[i].cmd_buf_offset > |
| 1749 | (cmd_req->cmd_req_len - |
| 1750 | SG_ENTRY_SZ * sg_ptr->nents))) { |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1751 | pr_err("Invalid offset = 0x%x\n", |
| 1752 | cmd_req->ifd_data[i]. |
| 1753 | cmd_buf_offset); |
| 1754 | goto err; |
| 1755 | } |
| 1756 | } else if ((listener_svc) && |
| 1757 | (lstnr_resp->ifd_data[i].fd > 0)) { |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1758 | if ((lstnr_resp->resp_len < |
| 1759 | SG_ENTRY_SZ * sg_ptr->nents) || |
| 1760 | (lstnr_resp->ifd_data[i].cmd_buf_offset > |
| 1761 | (lstnr_resp->resp_len - |
| 1762 | SG_ENTRY_SZ * sg_ptr->nents))) { |
Mona Hossain | b947069 | 2014-10-09 12:00:03 -0700 | [diff] [blame] | 1763 | pr_err("Invalid offset = 0x%x\n", |
| 1764 | lstnr_resp->ifd_data[i]. |
| 1765 | cmd_buf_offset); |
| 1766 | goto err; |
| 1767 | } |
| 1768 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1769 | update = (struct qseecom_sg_entry *) field; |
| 1770 | for (j = 0; j < sg_ptr->nents; j++) { |
| 1771 | if (cleanup) { |
| 1772 | update->phys_addr = 0; |
| 1773 | update->len = 0; |
| 1774 | } else { |
| 1775 | update->phys_addr = (uint32_t) |
| 1776 | sg_dma_address(sg); |
| 1777 | update->len = sg->length; |
| 1778 | } |
| 1779 | len += sg->length; |
| 1780 | update++; |
| 1781 | sg = sg_next(sg); |
| 1782 | } |
| 1783 | } |
| 1784 | if (cleanup) |
| 1785 | msm_ion_do_cache_op(qseecom.ion_clnt, |
| 1786 | ihandle, NULL, len, |
| 1787 | ION_IOC_INV_CACHES); |
| 1788 | else |
| 1789 | msm_ion_do_cache_op(qseecom.ion_clnt, |
| 1790 | ihandle, NULL, len, |
| 1791 | ION_IOC_CLEAN_INV_CACHES); |
| 1792 | /* Deallocate the handle */ |
| 1793 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1794 | ion_free(qseecom.ion_clnt, ihandle); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1795 | } |
| 1796 | return ret; |
Mona Hossain | f1f2ed6 | 2012-11-15 19:51:33 -0800 | [diff] [blame] | 1797 | err: |
| 1798 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1799 | ion_free(qseecom.ion_clnt, ihandle); |
| 1800 | return -ENOMEM; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | static int qseecom_send_modfd_cmd(struct qseecom_dev_handle *data, |
| 1804 | void __user *argp) |
| 1805 | { |
| 1806 | int ret = 0; |
Mona Hossain | dddf444 | 2013-10-01 14:08:20 -0700 | [diff] [blame] | 1807 | int i; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1808 | struct qseecom_send_modfd_cmd_req req; |
| 1809 | struct qseecom_send_cmd_req send_cmd_req; |
| 1810 | |
| 1811 | ret = copy_from_user(&req, argp, sizeof(req)); |
| 1812 | if (ret) { |
| 1813 | pr_err("copy_from_user failed\n"); |
| 1814 | return ret; |
| 1815 | } |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 1816 | |
jeron | f5cf704 | 2016-07-26 16:13:50 +0800 | [diff] [blame] | 1817 | if (req.cmd_req_buf == NULL || req.resp_buf == NULL) { |
| 1818 | pr_err("cmd buffer or response buffer is null\n"); |
| 1819 | return -EINVAL; |
| 1820 | } |
| 1821 | if (((uint32_t)req.cmd_req_buf < data->client.user_virt_sb_base) || |
| 1822 | ((uint32_t)req.cmd_req_buf >= (data->client.user_virt_sb_base + |
| 1823 | data->client.sb_length))) { |
| 1824 | pr_err("cmd buffer address not within shared bufffer\n"); |
| 1825 | return -EINVAL; |
| 1826 | } |
| 1827 | |
| 1828 | if (((uint32_t)req.resp_buf < data->client.user_virt_sb_base) || |
| 1829 | ((uint32_t)req.resp_buf >= (data->client.user_virt_sb_base + |
| 1830 | data->client.sb_length))){ |
| 1831 | pr_err("response buffer address not within shared bufffer\n"); |
| 1832 | return -EINVAL; |
| 1833 | } |
Teow Wan Yee | 1a428fd | 2016-07-27 14:01:01 +0800 | [diff] [blame] | 1834 | |
| 1835 | if (req.cmd_req_len == 0 || req.cmd_req_len > data->client.sb_length || |
| 1836 | req.resp_len > data->client.sb_length) { |
| 1837 | pr_err("cmd or response buffer length not valid\n"); |
| 1838 | return -EINVAL; |
| 1839 | } |
| 1840 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1841 | send_cmd_req.cmd_req_buf = req.cmd_req_buf; |
| 1842 | send_cmd_req.cmd_req_len = req.cmd_req_len; |
| 1843 | send_cmd_req.resp_buf = req.resp_buf; |
| 1844 | send_cmd_req.resp_len = req.resp_len; |
| 1845 | |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 1846 | if (__validate_send_cmd_inputs(data, &send_cmd_req)) |
| 1847 | return -EINVAL; |
| 1848 | |
Mona Hossain | dddf444 | 2013-10-01 14:08:20 -0700 | [diff] [blame] | 1849 | /* validate offsets */ |
| 1850 | for (i = 0; i < MAX_ION_FD; i++) { |
| 1851 | if (req.ifd_data[i].cmd_buf_offset >= req.cmd_req_len) { |
| 1852 | pr_err("Invalid offset %d = 0x%x\n", |
| 1853 | i, req.ifd_data[i].cmd_buf_offset); |
| 1854 | return -EINVAL; |
| 1855 | } |
| 1856 | } |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 1857 | req.cmd_req_buf = (void *)__qseecom_uvirt_to_kvirt(data, |
| 1858 | (uint32_t)req.cmd_req_buf); |
| 1859 | req.resp_buf = (void *)__qseecom_uvirt_to_kvirt(data, |
| 1860 | (uint32_t)req.resp_buf); |
| 1861 | |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1862 | ret = __qseecom_update_cmd_buf(&req, false, data, false); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1863 | if (ret) |
| 1864 | return ret; |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 1865 | ret = __qseecom_send_cmd(data, &send_cmd_req); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1866 | if (ret) |
| 1867 | return ret; |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 1868 | ret = __qseecom_update_cmd_buf(&req, true, data, false); |
Mona Hossain | c56584d | 2013-05-28 09:06:26 -0700 | [diff] [blame] | 1869 | if (ret) |
| 1870 | return ret; |
Zhen Kong | 04f65b8 | 2013-10-03 13:58:45 -0700 | [diff] [blame] | 1871 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1872 | return ret; |
| 1873 | } |
| 1874 | |
| 1875 | static int __qseecom_listener_has_rcvd_req(struct qseecom_dev_handle *data, |
| 1876 | struct qseecom_registered_listener_list *svc) |
| 1877 | { |
| 1878 | int ret; |
| 1879 | ret = (svc->rcv_req_flag != 0); |
| 1880 | return ret || data->abort; |
| 1881 | } |
| 1882 | |
| 1883 | static int qseecom_receive_req(struct qseecom_dev_handle *data) |
| 1884 | { |
| 1885 | int ret = 0; |
| 1886 | struct qseecom_registered_listener_list *this_lstnr; |
| 1887 | |
| 1888 | this_lstnr = __qseecom_find_svc(data->listener.id); |
AnilKumar Chimata | 69b857a | 2013-07-23 23:03:26 +0530 | [diff] [blame] | 1889 | if (!this_lstnr) { |
| 1890 | pr_err("Invalid listener ID\n"); |
| 1891 | return -ENODATA; |
| 1892 | } |
| 1893 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1894 | while (1) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 1895 | if (wait_event_freezable(this_lstnr->rcv_req_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1896 | __qseecom_listener_has_rcvd_req(data, |
| 1897 | this_lstnr))) { |
Hariprasad Dhalinarasimha | f44cbd2 | 2013-07-20 04:49:34 +0530 | [diff] [blame] | 1898 | pr_debug("Interrupted: exiting Listener Service = %d\n", |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 1899 | (uint32_t)data->listener.id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1900 | /* woken up for different reason */ |
| 1901 | return -ERESTARTSYS; |
| 1902 | } |
| 1903 | |
| 1904 | if (data->abort) { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 1905 | pr_err("Aborting Listener Service = %d\n", |
| 1906 | (uint32_t)data->listener.id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1907 | return -ENODEV; |
| 1908 | } |
| 1909 | this_lstnr->rcv_req_flag = 0; |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 1910 | break; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1911 | } |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1915 | static bool __qseecom_is_fw_image_valid(const struct firmware *fw_entry) |
| 1916 | { |
| 1917 | struct elf32_hdr *ehdr; |
| 1918 | |
| 1919 | if (fw_entry->size < sizeof(*ehdr)) { |
| 1920 | pr_err("%s: Not big enough to be an elf header\n", |
| 1921 | qseecom.pdev->init_name); |
| 1922 | return false; |
| 1923 | } |
| 1924 | ehdr = (struct elf32_hdr *)fw_entry->data; |
| 1925 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
| 1926 | pr_err("%s: Not an elf header\n", |
| 1927 | qseecom.pdev->init_name); |
| 1928 | return false; |
| 1929 | } |
| 1930 | |
| 1931 | if (ehdr->e_phnum == 0) { |
| 1932 | pr_err("%s: No loadable segments\n", |
| 1933 | qseecom.pdev->init_name); |
| 1934 | return false; |
| 1935 | } |
| 1936 | if (sizeof(struct elf32_phdr) * ehdr->e_phnum + |
| 1937 | sizeof(struct elf32_hdr) > fw_entry->size) { |
| 1938 | pr_err("%s: Program headers not within mdt\n", |
| 1939 | qseecom.pdev->init_name); |
| 1940 | return false; |
| 1941 | } |
| 1942 | return true; |
| 1943 | } |
| 1944 | |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 1945 | static int __qseecom_get_fw_size(const char *appname, uint32_t *fw_size) |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1946 | { |
| 1947 | int ret = -1; |
| 1948 | int i = 0, rc = 0; |
| 1949 | const struct firmware *fw_entry = NULL; |
| 1950 | struct elf32_phdr *phdr; |
| 1951 | char fw_name[MAX_APP_NAME_SIZE]; |
| 1952 | struct elf32_hdr *ehdr; |
| 1953 | int num_images = 0; |
| 1954 | |
| 1955 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname); |
| 1956 | rc = request_firmware(&fw_entry, fw_name, qseecom.pdev); |
| 1957 | if (rc) { |
| 1958 | pr_err("error with request_firmware\n"); |
| 1959 | ret = -EIO; |
| 1960 | goto err; |
| 1961 | } |
| 1962 | if (!__qseecom_is_fw_image_valid(fw_entry)) { |
| 1963 | ret = -EIO; |
| 1964 | goto err; |
| 1965 | } |
| 1966 | *fw_size = fw_entry->size; |
| 1967 | phdr = (struct elf32_phdr *)(fw_entry->data + sizeof(struct elf32_hdr)); |
| 1968 | ehdr = (struct elf32_hdr *)fw_entry->data; |
| 1969 | num_images = ehdr->e_phnum; |
| 1970 | release_firmware(fw_entry); |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 1971 | fw_entry = NULL; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1972 | for (i = 0; i < num_images; i++, phdr++) { |
| 1973 | memset(fw_name, 0, sizeof(fw_name)); |
| 1974 | snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i); |
| 1975 | ret = request_firmware(&fw_entry, fw_name, qseecom.pdev); |
| 1976 | if (ret) |
| 1977 | goto err; |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 1978 | if (*fw_size > U32_MAX - fw_entry->size) { |
| 1979 | pr_err("QSEE %s app file size overflow\n", appname); |
| 1980 | ret = -EINVAL; |
| 1981 | goto err; |
| 1982 | } |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1983 | *fw_size += fw_entry->size; |
| 1984 | release_firmware(fw_entry); |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 1985 | fw_entry = NULL; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1986 | } |
| 1987 | return ret; |
| 1988 | err: |
| 1989 | if (fw_entry) |
| 1990 | release_firmware(fw_entry); |
| 1991 | *fw_size = 0; |
| 1992 | return ret; |
| 1993 | } |
| 1994 | |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 1995 | static int __qseecom_get_fw_data(const char *appname, u8 *img_data, |
| 1996 | uint32_t fw_size, |
| 1997 | struct qseecom_load_app_ireq *load_req) |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 1998 | { |
| 1999 | int ret = -1; |
| 2000 | int i = 0, rc = 0; |
| 2001 | const struct firmware *fw_entry = NULL; |
| 2002 | char fw_name[MAX_APP_NAME_SIZE]; |
| 2003 | u8 *img_data_ptr = img_data; |
| 2004 | struct elf32_hdr *ehdr; |
| 2005 | int num_images = 0; |
| 2006 | |
| 2007 | snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname); |
| 2008 | rc = request_firmware(&fw_entry, fw_name, qseecom.pdev); |
| 2009 | if (rc) { |
| 2010 | ret = -EIO; |
| 2011 | goto err; |
| 2012 | } |
| 2013 | load_req->img_len = fw_entry->size; |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 2014 | if (load_req->img_len > fw_size) { |
| 2015 | pr_err("app %s size %zu is larger than buf size %u\n", |
| 2016 | appname, fw_entry->size, fw_size); |
| 2017 | ret = -EINVAL; |
| 2018 | goto err; |
| 2019 | } |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2020 | memcpy(img_data_ptr, fw_entry->data, fw_entry->size); |
| 2021 | img_data_ptr = img_data_ptr + fw_entry->size; |
| 2022 | load_req->mdt_len = fw_entry->size; /*Get MDT LEN*/ |
| 2023 | ehdr = (struct elf32_hdr *)fw_entry->data; |
| 2024 | num_images = ehdr->e_phnum; |
| 2025 | release_firmware(fw_entry); |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 2026 | fw_entry = NULL; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2027 | for (i = 0; i < num_images; i++) { |
| 2028 | snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i); |
| 2029 | ret = request_firmware(&fw_entry, fw_name, qseecom.pdev); |
| 2030 | if (ret) { |
| 2031 | pr_err("Failed to locate blob %s\n", fw_name); |
| 2032 | goto err; |
| 2033 | } |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 2034 | if ((fw_entry->size > U32_MAX - load_req->img_len) || |
| 2035 | (fw_entry->size + load_req->img_len > fw_size)) { |
| 2036 | pr_err("Invalid file size for %s\n", fw_name); |
| 2037 | ret = -EINVAL; |
| 2038 | goto err; |
| 2039 | } |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2040 | memcpy(img_data_ptr, fw_entry->data, fw_entry->size); |
| 2041 | img_data_ptr = img_data_ptr + fw_entry->size; |
| 2042 | load_req->img_len += fw_entry->size; |
| 2043 | release_firmware(fw_entry); |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 2044 | fw_entry = NULL; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2045 | } |
| 2046 | load_req->phy_addr = virt_to_phys(img_data); |
| 2047 | return ret; |
| 2048 | err: |
| 2049 | release_firmware(fw_entry); |
| 2050 | return ret; |
| 2051 | } |
| 2052 | |
| 2053 | static int __qseecom_load_fw(struct qseecom_dev_handle *data, char *appname) |
| 2054 | { |
| 2055 | int ret = -1; |
| 2056 | uint32_t fw_size = 0; |
| 2057 | struct qseecom_load_app_ireq load_req = {0, 0, 0, 0}; |
| 2058 | struct qseecom_command_scm_resp resp; |
| 2059 | u8 *img_data = NULL; |
| 2060 | |
| 2061 | if (__qseecom_get_fw_size(appname, &fw_size)) |
| 2062 | return -EIO; |
| 2063 | |
| 2064 | img_data = kzalloc(fw_size, GFP_KERNEL); |
| 2065 | if (!img_data) { |
| 2066 | pr_err("Failied to allocate memory for copying image data\n"); |
| 2067 | return -ENOMEM; |
| 2068 | } |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 2069 | ret = __qseecom_get_fw_data(appname, img_data, fw_size, &load_req); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2070 | if (ret) { |
| 2071 | kzfree(img_data); |
| 2072 | return -EIO; |
| 2073 | } |
| 2074 | |
| 2075 | /* Populate the remaining parameters */ |
| 2076 | load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 2077 | strlcpy(load_req.app_name, appname, MAX_APP_NAME_SIZE); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2078 | |
| 2079 | if (qseecom.support_bus_scaling) { |
| 2080 | mutex_lock(&qsee_bw_mutex); |
| 2081 | ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM); |
| 2082 | mutex_unlock(&qsee_bw_mutex); |
| 2083 | if (ret) { |
| 2084 | kzfree(img_data); |
| 2085 | return ret; |
| 2086 | } |
| 2087 | } |
| 2088 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2089 | ret = __qseecom_enable_clk_scale_up(data); |
Mona Hossain | 60f9fb0 | 2012-11-05 13:51:50 -0800 | [diff] [blame] | 2090 | if (ret) { |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2091 | ret = -EIO; |
| 2092 | goto loadfw_err; |
Mona Hossain | 60f9fb0 | 2012-11-05 13:51:50 -0800 | [diff] [blame] | 2093 | } |
| 2094 | |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 2095 | __cpuc_flush_dcache_area((void *)img_data, fw_size); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2096 | /* SCM_CALL to load the image */ |
| 2097 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req, |
| 2098 | sizeof(struct qseecom_load_app_ireq), |
| 2099 | &resp, sizeof(resp)); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2100 | if (ret) { |
| 2101 | pr_err("scm_call to load failed : ret %d\n", ret); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2102 | ret = -EIO; |
| 2103 | goto loadfw_err; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | switch (resp.result) { |
| 2107 | case QSEOS_RESULT_SUCCESS: |
| 2108 | ret = resp.data; |
| 2109 | break; |
| 2110 | case QSEOS_RESULT_INCOMPLETE: |
| 2111 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 2112 | if (ret) |
| 2113 | pr_err("process_incomplete_cmd FAILED\n"); |
| 2114 | else |
| 2115 | ret = resp.data; |
| 2116 | break; |
| 2117 | case QSEOS_RESULT_FAILURE: |
| 2118 | pr_err("scm call failed with response QSEOS_RESULT FAILURE\n"); |
| 2119 | break; |
| 2120 | default: |
| 2121 | pr_err("scm call return unknown response %d\n", resp.result); |
| 2122 | ret = -EINVAL; |
| 2123 | break; |
| 2124 | } |
Mona Hossain | 60f9fb0 | 2012-11-05 13:51:50 -0800 | [diff] [blame] | 2125 | |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2126 | loadfw_err: |
William Clark | d83bdcb | 2014-07-17 14:40:41 -0700 | [diff] [blame] | 2127 | kzfree(img_data); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2128 | __qseecom_disable_clk_scale_down(data); |
| 2129 | if (qseecom.support_bus_scaling) { |
| 2130 | mutex_lock(&qsee_bw_mutex); |
| 2131 | qseecom_unregister_bus_bandwidth_needs(data); |
| 2132 | mutex_unlock(&qsee_bw_mutex); |
| 2133 | } |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2134 | return ret; |
| 2135 | } |
| 2136 | |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2137 | static int qseecom_load_commonlib_image(struct qseecom_dev_handle *data) |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2138 | { |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2139 | int ret = 0; |
Zhen Kong | fd64c98 | 2013-12-19 16:35:43 -0800 | [diff] [blame] | 2140 | int len = 0; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2141 | uint32_t fw_size = 0; |
| 2142 | struct qseecom_load_app_ireq load_req = {0, 0, 0, 0}; |
| 2143 | struct qseecom_command_scm_resp resp; |
| 2144 | u8 *img_data = NULL; |
Zhen Kong | fd64c98 | 2013-12-19 16:35:43 -0800 | [diff] [blame] | 2145 | ion_phys_addr_t pa; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2146 | |
Hariprasad Dhalinarasimha | a3b9644 | 2013-01-02 10:40:40 -0800 | [diff] [blame] | 2147 | if (__qseecom_get_fw_size("cmnlib", &fw_size)) |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2148 | return -EIO; |
| 2149 | |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2150 | qseecom.cmnlib_ion_handle = ion_alloc(qseecom.ion_clnt, fw_size, |
| 2151 | SZ_4K, ION_HEAP(ION_QSECOM_HEAP_ID), 0); |
| 2152 | if (IS_ERR_OR_NULL(qseecom.cmnlib_ion_handle)) { |
Zhen Kong | fd64c98 | 2013-12-19 16:35:43 -0800 | [diff] [blame] | 2153 | pr_err("ION alloc failed\n"); |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2154 | return -ENOMEM; |
| 2155 | } |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2156 | |
| 2157 | img_data = (u8 *)ion_map_kernel(qseecom.ion_clnt, |
| 2158 | qseecom.cmnlib_ion_handle); |
| 2159 | if (IS_ERR_OR_NULL(img_data)) { |
Zhen Kong | fd64c98 | 2013-12-19 16:35:43 -0800 | [diff] [blame] | 2160 | pr_err("ION memory mapping for cmnlib failed\n"); |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2161 | ret = -ENOMEM; |
| 2162 | goto exit_ion_free; |
| 2163 | } |
Zhen Kong | 82e296d | 2016-10-20 17:34:20 -0700 | [diff] [blame] | 2164 | ret = __qseecom_get_fw_data("cmnlib", img_data, fw_size, &load_req); |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2165 | if (ret) { |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2166 | ret = -EIO; |
| 2167 | goto exit_ion_unmap_kernel; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2168 | } |
Zhen Kong | fd64c98 | 2013-12-19 16:35:43 -0800 | [diff] [blame] | 2169 | /* Get the physical address of the ION BUF */ |
| 2170 | ret = ion_phys(qseecom.ion_clnt, qseecom.cmnlib_ion_handle, |
| 2171 | &pa, &len); |
| 2172 | load_req.phy_addr = (s32)pa; |
| 2173 | if (ret) { |
| 2174 | pr_err("physical memory retrieval failure\n"); |
| 2175 | ret = -EIO; |
| 2176 | goto exit_ion_unmap_kernel; |
| 2177 | } |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2178 | /* Populate the remaining parameters */ |
| 2179 | load_req.qsee_cmd_id = QSEOS_LOAD_SERV_IMAGE_COMMAND; |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2180 | |
| 2181 | if (qseecom.support_bus_scaling) { |
| 2182 | mutex_lock(&qsee_bw_mutex); |
| 2183 | ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM); |
| 2184 | mutex_unlock(&qsee_bw_mutex); |
| 2185 | if (ret) { |
| 2186 | ret = -EIO; |
| 2187 | goto exit_ion_unmap_kernel; |
| 2188 | } |
| 2189 | } |
| 2190 | |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2191 | /* Vote for the SFPB clock */ |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2192 | ret = __qseecom_enable_clk_scale_up(data); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2193 | if (ret) { |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2194 | ret = -EIO; |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2195 | goto exit_unregister_bus_bw_need; |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2196 | } |
| 2197 | |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 2198 | __cpuc_flush_dcache_area((void *)img_data, fw_size); |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2199 | /* SCM_CALL to load the image */ |
| 2200 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req, |
| 2201 | sizeof(struct qseecom_load_lib_image_ireq), |
| 2202 | &resp, sizeof(resp)); |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2203 | if (ret) { |
| 2204 | pr_err("scm_call to load failed : ret %d\n", ret); |
| 2205 | ret = -EIO; |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2206 | goto exit_disable_clk_vote; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2207 | } |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2208 | |
| 2209 | switch (resp.result) { |
| 2210 | case QSEOS_RESULT_SUCCESS: |
| 2211 | break; |
| 2212 | case QSEOS_RESULT_FAILURE: |
| 2213 | pr_err("scm call failed w/response result%d\n", resp.result); |
| 2214 | ret = -EINVAL; |
| 2215 | goto exit_disable_clk_vote; |
| 2216 | case QSEOS_RESULT_INCOMPLETE: |
| 2217 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 2218 | if (ret) { |
| 2219 | pr_err("process_incomplete_cmd failed err: %d\n", ret); |
| 2220 | goto exit_disable_clk_vote; |
| 2221 | } |
| 2222 | break; |
| 2223 | default: |
| 2224 | pr_err("scm call return unknown response %d\n", resp.result); |
| 2225 | ret = -EINVAL; |
| 2226 | goto exit_disable_clk_vote; |
| 2227 | } |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2228 | __qseecom_disable_clk_scale_down(data); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2229 | if (qseecom.support_bus_scaling) { |
| 2230 | mutex_lock(&qsee_bw_mutex); |
| 2231 | qseecom_unregister_bus_bandwidth_needs(data); |
| 2232 | mutex_unlock(&qsee_bw_mutex); |
| 2233 | } |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2234 | return ret; |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2235 | |
| 2236 | exit_disable_clk_vote: |
| 2237 | __qseecom_disable_clk_scale_down(data); |
| 2238 | |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 2239 | exit_unregister_bus_bw_need: |
| 2240 | if (qseecom.support_bus_scaling) { |
| 2241 | mutex_lock(&qsee_bw_mutex); |
| 2242 | qseecom_unregister_bus_bandwidth_needs(data); |
| 2243 | mutex_unlock(&qsee_bw_mutex); |
| 2244 | } |
| 2245 | |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2246 | exit_ion_unmap_kernel: |
| 2247 | ion_unmap_kernel(qseecom.ion_clnt, qseecom.cmnlib_ion_handle); |
| 2248 | |
| 2249 | exit_ion_free: |
| 2250 | ion_free(qseecom.ion_clnt, qseecom.cmnlib_ion_handle); |
| 2251 | qseecom.cmnlib_ion_handle = NULL; |
| 2252 | return ret; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | static int qseecom_unload_commonlib_image(void) |
| 2256 | { |
| 2257 | int ret = -EINVAL; |
| 2258 | struct qseecom_unload_lib_image_ireq unload_req = {0}; |
| 2259 | struct qseecom_command_scm_resp resp; |
| 2260 | |
| 2261 | /* Populate the remaining parameters */ |
| 2262 | unload_req.qsee_cmd_id = QSEOS_UNLOAD_SERV_IMAGE_COMMAND; |
| 2263 | /* SCM_CALL to load the image */ |
| 2264 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &unload_req, |
| 2265 | sizeof(struct qseecom_unload_lib_image_ireq), |
| 2266 | &resp, sizeof(resp)); |
| 2267 | if (ret) { |
| 2268 | pr_err("scm_call to unload lib failed : ret %d\n", ret); |
| 2269 | ret = -EIO; |
| 2270 | } else { |
| 2271 | switch (resp.result) { |
| 2272 | case QSEOS_RESULT_SUCCESS: |
| 2273 | break; |
| 2274 | case QSEOS_RESULT_FAILURE: |
| 2275 | pr_err("scm fail resp.result QSEOS_RESULT FAILURE\n"); |
| 2276 | break; |
| 2277 | default: |
| 2278 | pr_err("scm call return unknown response %d\n", |
| 2279 | resp.result); |
| 2280 | ret = -EINVAL; |
| 2281 | break; |
| 2282 | } |
| 2283 | } |
Zhen Kong | c46b584 | 2013-12-12 13:09:16 +0530 | [diff] [blame] | 2284 | |
| 2285 | ion_unmap_kernel(qseecom.ion_clnt, qseecom.cmnlib_ion_handle); |
| 2286 | ion_free(qseecom.ion_clnt, qseecom.cmnlib_ion_handle); |
| 2287 | qseecom.cmnlib_ion_handle = NULL; |
| 2288 | |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2289 | return ret; |
| 2290 | } |
| 2291 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2292 | int qseecom_start_app(struct qseecom_handle **handle, |
| 2293 | char *app_name, uint32_t size) |
| 2294 | { |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 2295 | int32_t ret = 0; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2296 | unsigned long flags = 0; |
| 2297 | struct qseecom_dev_handle *data = NULL; |
| 2298 | struct qseecom_check_app_ireq app_ireq; |
| 2299 | struct qseecom_registered_app_list *entry = NULL; |
| 2300 | struct qseecom_registered_kclient_list *kclient_entry = NULL; |
| 2301 | bool found_app = false; |
| 2302 | uint32_t len; |
| 2303 | ion_phys_addr_t pa; |
| 2304 | |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 2305 | if (!app_name || strlen(app_name) >= MAX_APP_NAME_SIZE) { |
| 2306 | pr_err("The app_name (%s) is not valid\n", app_name); |
| 2307 | return -EINVAL; |
| 2308 | } |
| 2309 | |
Mona Hossain | 823f988 | 2012-11-23 14:42:20 -0800 | [diff] [blame] | 2310 | *handle = kzalloc(sizeof(struct qseecom_handle), GFP_KERNEL); |
| 2311 | if (!(*handle)) { |
| 2312 | pr_err("failed to allocate memory for kernel client handle\n"); |
| 2313 | return -ENOMEM; |
| 2314 | } |
| 2315 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2316 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 2317 | if (!data) { |
| 2318 | pr_err("kmalloc failed\n"); |
| 2319 | if (ret == 0) { |
| 2320 | kfree(*handle); |
| 2321 | *handle = NULL; |
| 2322 | } |
| 2323 | return -ENOMEM; |
| 2324 | } |
| 2325 | data->abort = 0; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 2326 | data->type = QSEECOM_CLIENT_APP; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2327 | data->released = false; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2328 | data->client.sb_length = size; |
| 2329 | data->client.user_virt_sb_base = 0; |
| 2330 | data->client.ihandle = NULL; |
| 2331 | |
| 2332 | init_waitqueue_head(&data->abort_wq); |
| 2333 | atomic_set(&data->ioctl_count, 0); |
| 2334 | |
| 2335 | data->client.ihandle = ion_alloc(qseecom.ion_clnt, size, 4096, |
| 2336 | ION_HEAP(ION_QSECOM_HEAP_ID), 0); |
| 2337 | if (IS_ERR_OR_NULL(data->client.ihandle)) { |
| 2338 | pr_err("Ion client could not retrieve the handle\n"); |
| 2339 | kfree(data); |
| 2340 | kfree(*handle); |
| 2341 | *handle = NULL; |
| 2342 | return -EINVAL; |
| 2343 | } |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2344 | mutex_lock(&app_access_lock); |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2345 | if (qseecom.qsee_version > QSEEE_VERSION_00) { |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2346 | if (qseecom.commonlib_loaded == false) { |
| 2347 | ret = qseecom_load_commonlib_image(data); |
| 2348 | if (ret == 0) |
| 2349 | qseecom.commonlib_loaded = true; |
| 2350 | } |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2351 | } |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2352 | if (ret) { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2353 | pr_err("Failed to load commonlib image\n"); |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2354 | ret = -EIO; |
| 2355 | goto err; |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | app_ireq.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 2359 | strlcpy(app_ireq.app_name, app_name, MAX_APP_NAME_SIZE); |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 2360 | ret = __qseecom_check_app_exists(app_ireq); |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2361 | if (ret < 0) |
| 2362 | goto err; |
| 2363 | |
Hariprasad Dhalinarasimha | efecbfd | 2013-04-10 15:13:03 -0700 | [diff] [blame] | 2364 | data->client.app_id = ret; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2365 | if (ret > 0) { |
| 2366 | pr_warn("App id %d for [%s] app exists\n", ret, |
| 2367 | (char *)app_ireq.app_name); |
| 2368 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 2369 | list_for_each_entry(entry, |
| 2370 | &qseecom.registered_app_list_head, list){ |
| 2371 | if (entry->app_id == ret) { |
| 2372 | entry->ref_cnt++; |
| 2373 | found_app = true; |
| 2374 | break; |
| 2375 | } |
| 2376 | } |
| 2377 | spin_unlock_irqrestore( |
| 2378 | &qseecom.registered_app_list_lock, flags); |
| 2379 | if (!found_app) |
| 2380 | pr_warn("App_id %d [%s] was loaded but not registered\n", |
| 2381 | ret, (char *)app_ireq.app_name); |
| 2382 | } else { |
| 2383 | /* load the app and get the app_id */ |
| 2384 | pr_debug("%s: Loading app for the first time'\n", |
| 2385 | qseecom.pdev->init_name); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2386 | ret = __qseecom_load_fw(data, app_name); |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2387 | if (ret < 0) |
| 2388 | goto err; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2389 | data->client.app_id = ret; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 2390 | strlcpy(data->client.app_name, app_name, MAX_APP_NAME_SIZE); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2391 | } |
| 2392 | if (!found_app) { |
| 2393 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 2394 | if (!entry) { |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2395 | pr_err("kmalloc for app entry failed\n"); |
| 2396 | ret = -ENOMEM; |
| 2397 | goto err; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2398 | } |
| 2399 | entry->app_id = ret; |
| 2400 | entry->ref_cnt = 1; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 2401 | strlcpy(entry->app_name, app_name, MAX_APP_NAME_SIZE); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2402 | |
| 2403 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 2404 | list_add_tail(&entry->list, &qseecom.registered_app_list_head); |
| 2405 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, |
| 2406 | flags); |
| 2407 | } |
| 2408 | |
| 2409 | /* Get the physical address of the ION BUF */ |
| 2410 | ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len); |
Zhen Kong | 05ed446 | 2014-01-28 18:21:30 -0800 | [diff] [blame] | 2411 | if (ret) { |
| 2412 | pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n", |
| 2413 | ret); |
| 2414 | goto err; |
| 2415 | } |
| 2416 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2417 | /* Populate the structure for sending scm call to load image */ |
| 2418 | data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, |
| 2419 | data->client.ihandle); |
Hariprasad Dhalinarasimha | acfb09c | 2013-01-10 13:16:15 -0800 | [diff] [blame] | 2420 | data->client.user_virt_sb_base = (uint32_t)data->client.sb_virt; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2421 | data->client.sb_phys = pa; |
| 2422 | (*handle)->dev = (void *)data; |
| 2423 | (*handle)->sbuf = (unsigned char *)data->client.sb_virt; |
| 2424 | (*handle)->sbuf_len = data->client.sb_length; |
| 2425 | |
| 2426 | kclient_entry = kzalloc(sizeof(*kclient_entry), GFP_KERNEL); |
| 2427 | if (!kclient_entry) { |
| 2428 | pr_err("kmalloc failed\n"); |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2429 | ret = -ENOMEM; |
| 2430 | goto err; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2431 | } |
| 2432 | kclient_entry->handle = *handle; |
| 2433 | |
| 2434 | spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags); |
| 2435 | list_add_tail(&kclient_entry->list, |
| 2436 | &qseecom.registered_kclient_list_head); |
| 2437 | spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags); |
| 2438 | |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2439 | mutex_unlock(&app_access_lock); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2440 | return 0; |
Mona Hossain | b2dc0f8 | 2013-05-22 09:26:50 -0700 | [diff] [blame] | 2441 | |
| 2442 | err: |
| 2443 | kfree(data); |
| 2444 | kfree(*handle); |
| 2445 | *handle = NULL; |
| 2446 | mutex_unlock(&app_access_lock); |
| 2447 | return ret; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2448 | } |
| 2449 | EXPORT_SYMBOL(qseecom_start_app); |
| 2450 | |
| 2451 | int qseecom_shutdown_app(struct qseecom_handle **handle) |
| 2452 | { |
| 2453 | int ret = -EINVAL; |
Mona Hossain | 3382402 | 2013-02-25 09:32:33 -0800 | [diff] [blame] | 2454 | struct qseecom_dev_handle *data; |
| 2455 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2456 | struct qseecom_registered_kclient_list *kclient = NULL; |
| 2457 | unsigned long flags = 0; |
| 2458 | bool found_handle = false; |
| 2459 | |
Mona Hossain | 3382402 | 2013-02-25 09:32:33 -0800 | [diff] [blame] | 2460 | if ((handle == NULL) || (*handle == NULL)) { |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2461 | pr_err("Handle is not initialized\n"); |
| 2462 | return -EINVAL; |
| 2463 | } |
Mona Hossain | 3382402 | 2013-02-25 09:32:33 -0800 | [diff] [blame] | 2464 | data = (struct qseecom_dev_handle *) ((*handle)->dev); |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 2465 | mutex_lock(&app_access_lock); |
| 2466 | atomic_inc(&data->ioctl_count); |
| 2467 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2468 | spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags); |
| 2469 | list_for_each_entry(kclient, &qseecom.registered_kclient_list_head, |
| 2470 | list) { |
| 2471 | if (kclient->handle == (*handle)) { |
| 2472 | list_del(&kclient->list); |
| 2473 | found_handle = true; |
| 2474 | break; |
| 2475 | } |
| 2476 | } |
| 2477 | spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags); |
| 2478 | if (!found_handle) |
| 2479 | pr_err("Unable to find the handle, exiting\n"); |
| 2480 | else |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 2481 | ret = qseecom_unload_app(data, false); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2482 | |
| 2483 | if (qseecom.support_bus_scaling) { |
| 2484 | mutex_lock(&qsee_bw_mutex); |
| 2485 | if (data->mode != INACTIVE) { |
| 2486 | qseecom_unregister_bus_bandwidth_needs(data); |
| 2487 | if (qseecom.cumulative_mode == INACTIVE) { |
| 2488 | ret = __qseecom_set_msm_bus_request(INACTIVE); |
| 2489 | if (ret) |
| 2490 | pr_err("Fail to scale down bus\n"); |
| 2491 | } |
| 2492 | } |
| 2493 | mutex_unlock(&qsee_bw_mutex); |
| 2494 | } else { |
| 2495 | if (data->fast_load_enabled == true) |
| 2496 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 2497 | if (data->perf_enabled == true) |
| 2498 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 2499 | } |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 2500 | |
| 2501 | atomic_dec(&data->ioctl_count); |
| 2502 | mutex_unlock(&app_access_lock); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2503 | if (ret == 0) { |
| 2504 | kzfree(data); |
| 2505 | kzfree(*handle); |
| 2506 | kzfree(kclient); |
| 2507 | *handle = NULL; |
| 2508 | } |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 2509 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2510 | return ret; |
| 2511 | } |
| 2512 | EXPORT_SYMBOL(qseecom_shutdown_app); |
| 2513 | |
| 2514 | int qseecom_send_command(struct qseecom_handle *handle, void *send_buf, |
| 2515 | uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len) |
| 2516 | { |
| 2517 | int ret = 0; |
| 2518 | struct qseecom_send_cmd_req req = {0, 0, 0, 0}; |
| 2519 | struct qseecom_dev_handle *data; |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 2520 | bool perf_enabled = false; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2521 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2522 | if (handle == NULL) { |
| 2523 | pr_err("Handle is not initialized\n"); |
| 2524 | return -EINVAL; |
| 2525 | } |
| 2526 | data = handle->dev; |
| 2527 | |
| 2528 | req.cmd_req_len = sbuf_len; |
| 2529 | req.resp_len = rbuf_len; |
| 2530 | req.cmd_req_buf = send_buf; |
| 2531 | req.resp_buf = resp_buf; |
| 2532 | |
Mona Hossain | 268c312 | 2014-11-03 17:05:48 -0800 | [diff] [blame] | 2533 | if (__validate_send_cmd_inputs(data, &req)) |
| 2534 | return -EINVAL; |
| 2535 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2536 | mutex_lock(&app_access_lock); |
| 2537 | atomic_inc(&data->ioctl_count); |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 2538 | if (qseecom.support_bus_scaling) { |
| 2539 | ret = qseecom_scale_bus_bandwidth_timer(INACTIVE); |
| 2540 | if (ret) { |
| 2541 | pr_err("Failed to set bw.\n"); |
| 2542 | atomic_dec(&data->ioctl_count); |
| 2543 | mutex_unlock(&app_access_lock); |
| 2544 | return ret; |
| 2545 | } |
| 2546 | } |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 2547 | /* |
| 2548 | * On targets where crypto clock is handled by HLOS, |
| 2549 | * if clk_access_cnt is zero and perf_enabled is false, |
| 2550 | * then the crypto clock was not enabled before sending cmd |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 2551 | * to tz, qseecom will enable the clock to avoid service failure. |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 2552 | */ |
| 2553 | if (!qseecom.qsee.clk_access_cnt && !data->perf_enabled) { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 2554 | pr_debug("ce clock is not enabled!\n"); |
| 2555 | ret = qseecom_perf_enable(data); |
| 2556 | if (ret) { |
| 2557 | pr_err("Failed to vote for clock with err %d\n", |
| 2558 | ret); |
| 2559 | atomic_dec(&data->ioctl_count); |
| 2560 | mutex_unlock(&app_access_lock); |
| 2561 | return -EINVAL; |
| 2562 | } |
| 2563 | perf_enabled = true; |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2566 | ret = __qseecom_send_cmd(data, &req); |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 2567 | if (qseecom.support_bus_scaling) |
| 2568 | __qseecom_add_bw_scale_down_timer( |
| 2569 | QSEECOM_SEND_CMD_CRYPTO_TIMEOUT); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2570 | |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 2571 | if (perf_enabled) { |
| 2572 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 2573 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 2574 | } |
| 2575 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2576 | atomic_dec(&data->ioctl_count); |
| 2577 | mutex_unlock(&app_access_lock); |
| 2578 | |
| 2579 | if (ret) |
| 2580 | return ret; |
| 2581 | |
Jeron Susan | 84186d4 | 2016-11-03 10:02:25 +0800 | [diff] [blame] | 2582 | pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%pK\n", |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 2583 | req.resp_len, req.resp_buf); |
| 2584 | return ret; |
| 2585 | } |
| 2586 | EXPORT_SYMBOL(qseecom_send_command); |
| 2587 | |
Mona Hossain | 91a8fc9 | 2012-11-07 19:58:30 -0800 | [diff] [blame] | 2588 | int qseecom_set_bandwidth(struct qseecom_handle *handle, bool high) |
| 2589 | { |
Mona Hossain | fca6f42 | 2013-01-12 13:00:35 -0800 | [diff] [blame] | 2590 | int ret = 0; |
Mona Hossain | 91a8fc9 | 2012-11-07 19:58:30 -0800 | [diff] [blame] | 2591 | if ((handle == NULL) || (handle->dev == NULL)) { |
| 2592 | pr_err("No valid kernel client\n"); |
| 2593 | return -EINVAL; |
| 2594 | } |
Mona Hossain | fca6f42 | 2013-01-12 13:00:35 -0800 | [diff] [blame] | 2595 | if (high) { |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2596 | if (qseecom.support_bus_scaling) { |
| 2597 | mutex_lock(&qsee_bw_mutex); |
| 2598 | __qseecom_register_bus_bandwidth_needs(handle->dev, |
| 2599 | HIGH); |
| 2600 | mutex_unlock(&qsee_bw_mutex); |
| 2601 | if (ret) |
| 2602 | pr_err("Failed to scale bus (med) %d\n", ret); |
| 2603 | } else { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 2604 | ret = qseecom_perf_enable(handle->dev); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2605 | if (ret) |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 2606 | pr_err("Failed to vote for clock with err %d\n", |
| 2607 | ret); |
Mona Hossain | fca6f42 | 2013-01-12 13:00:35 -0800 | [diff] [blame] | 2608 | } |
| 2609 | } else { |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2610 | if (!qseecom.support_bus_scaling) { |
| 2611 | qsee_disable_clock_vote(handle->dev, CLK_DFAB); |
| 2612 | qsee_disable_clock_vote(handle->dev, CLK_SFPB); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 2613 | } else { |
| 2614 | mutex_lock(&qsee_bw_mutex); |
| 2615 | qseecom_unregister_bus_bandwidth_needs(handle->dev); |
| 2616 | mutex_unlock(&qsee_bw_mutex); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 2617 | } |
Mona Hossain | 91a8fc9 | 2012-11-07 19:58:30 -0800 | [diff] [blame] | 2618 | } |
Mona Hossain | fca6f42 | 2013-01-12 13:00:35 -0800 | [diff] [blame] | 2619 | return ret; |
Mona Hossain | 91a8fc9 | 2012-11-07 19:58:30 -0800 | [diff] [blame] | 2620 | } |
| 2621 | EXPORT_SYMBOL(qseecom_set_bandwidth); |
| 2622 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2623 | static int qseecom_send_resp(void) |
| 2624 | { |
| 2625 | qseecom.send_resp_flag = 1; |
| 2626 | wake_up_interruptible(&qseecom.send_resp_wq); |
| 2627 | return 0; |
| 2628 | } |
| 2629 | |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 2630 | |
Zhen Kong | 66e478b | 2016-07-18 13:20:18 -0700 | [diff] [blame] | 2631 | static int __validate_send_modfd_resp_inputs(struct qseecom_dev_handle *data, |
| 2632 | struct qseecom_send_modfd_listener_resp *resp, |
| 2633 | struct qseecom_registered_listener_list *this_lstnr) |
| 2634 | { |
| 2635 | int i; |
| 2636 | |
| 2637 | if (!data || !resp || !this_lstnr) { |
| 2638 | pr_err("listener handle or resp msg is null\n"); |
| 2639 | return -EINVAL; |
| 2640 | } |
| 2641 | |
| 2642 | if (resp->resp_buf_ptr == NULL) { |
| 2643 | pr_err("resp buffer is null\n"); |
| 2644 | return -EINVAL; |
| 2645 | } |
| 2646 | /* validate resp buf length */ |
| 2647 | if ((resp->resp_len == 0) || |
| 2648 | (resp->resp_len > this_lstnr->sb_length)) { |
| 2649 | pr_err("resp buf length %d not valid\n", resp->resp_len); |
| 2650 | return -EINVAL; |
| 2651 | } |
| 2652 | |
| 2653 | if ((uintptr_t)resp->resp_buf_ptr > (ULONG_MAX - resp->resp_len)) { |
| 2654 | pr_err("Integer overflow in resp_len & resp_buf\n"); |
| 2655 | return -EINVAL; |
| 2656 | } |
| 2657 | if ((uintptr_t)this_lstnr->user_virt_sb_base > |
| 2658 | (ULONG_MAX - this_lstnr->sb_length)) { |
| 2659 | pr_err("Integer overflow in user_virt_sb_base & sb_length\n"); |
| 2660 | return -EINVAL; |
| 2661 | } |
| 2662 | /* validate resp buf */ |
| 2663 | if (((uintptr_t)resp->resp_buf_ptr < |
| 2664 | (uintptr_t)this_lstnr->user_virt_sb_base) || |
| 2665 | ((uintptr_t)resp->resp_buf_ptr >= |
| 2666 | ((uintptr_t)this_lstnr->user_virt_sb_base + |
| 2667 | this_lstnr->sb_length)) || |
| 2668 | (((uintptr_t)resp->resp_buf_ptr + resp->resp_len) > |
| 2669 | ((uintptr_t)this_lstnr->user_virt_sb_base + |
| 2670 | this_lstnr->sb_length))) { |
| 2671 | pr_err("resp buf is out of shared buffer region\n"); |
| 2672 | return -EINVAL; |
| 2673 | } |
| 2674 | |
| 2675 | /* validate offsets */ |
| 2676 | for (i = 0; i < MAX_ION_FD; i++) { |
| 2677 | if (resp->ifd_data[i].cmd_buf_offset >= resp->resp_len) { |
| 2678 | pr_err("Invalid offset %d = 0x%x\n", |
| 2679 | i, resp->ifd_data[i].cmd_buf_offset); |
| 2680 | return -EINVAL; |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
| 2687 | static int __qseecom_send_modfd_resp(struct qseecom_dev_handle *data, |
| 2688 | void __user *argp, bool is_64bit_addr) |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 2689 | { |
| 2690 | struct qseecom_send_modfd_listener_resp resp; |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 2691 | struct qseecom_registered_listener_list *this_lstnr = NULL; |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 2692 | |
| 2693 | if (copy_from_user(&resp, argp, sizeof(resp))) { |
| 2694 | pr_err("copy_from_user failed"); |
| 2695 | return -EINVAL; |
| 2696 | } |
Zhen Kong | 66e478b | 2016-07-18 13:20:18 -0700 | [diff] [blame] | 2697 | |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 2698 | this_lstnr = __qseecom_find_svc(data->listener.id); |
| 2699 | if (this_lstnr == NULL) |
| 2700 | return -EINVAL; |
| 2701 | |
Zhen Kong | 66e478b | 2016-07-18 13:20:18 -0700 | [diff] [blame] | 2702 | if (__validate_send_modfd_resp_inputs(data, &resp, this_lstnr)) |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 2703 | return -EINVAL; |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 2704 | |
Zhen Kong | f494819 | 2013-11-25 13:05:35 -0800 | [diff] [blame] | 2705 | resp.resp_buf_ptr = (uint32_t)this_lstnr->sb_virt + |
| 2706 | (resp.resp_buf_ptr - this_lstnr->user_virt_sb_base); |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 2707 | __qseecom_update_cmd_buf(&resp, false, data, true); |
| 2708 | qseecom.send_resp_flag = 1; |
| 2709 | wake_up_interruptible(&qseecom.send_resp_wq); |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
Zhen Kong | 66e478b | 2016-07-18 13:20:18 -0700 | [diff] [blame] | 2713 | static int qseecom_send_modfd_resp(struct qseecom_dev_handle *data, |
| 2714 | void __user *argp) |
| 2715 | { |
| 2716 | return __qseecom_send_modfd_resp(data, argp, false); |
| 2717 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 2718 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2719 | static int qseecom_get_qseos_version(struct qseecom_dev_handle *data, |
| 2720 | void __user *argp) |
| 2721 | { |
| 2722 | struct qseecom_qseos_version_req req; |
| 2723 | |
| 2724 | if (copy_from_user(&req, argp, sizeof(req))) { |
| 2725 | pr_err("copy_from_user failed"); |
| 2726 | return -EINVAL; |
| 2727 | } |
| 2728 | req.qseos_version = qseecom.qseos_version; |
| 2729 | if (copy_to_user(argp, &req, sizeof(req))) { |
| 2730 | pr_err("copy_to_user failed"); |
| 2731 | return -EINVAL; |
| 2732 | } |
| 2733 | return 0; |
| 2734 | } |
| 2735 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2736 | static int __qseecom_enable_clk(enum qseecom_ce_hw_instance ce) |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2737 | { |
| 2738 | int rc = 0; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2739 | struct qseecom_clk *qclk; |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2740 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2741 | if (ce == CLK_QSEE) |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 2742 | qclk = &qseecom.qsee; |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2743 | else |
| 2744 | qclk = &qseecom.ce_drv; |
| 2745 | |
| 2746 | mutex_lock(&clk_access_lock); |
Zhen Kong | 1f09c769 | 2013-05-03 17:50:32 -0700 | [diff] [blame] | 2747 | |
| 2748 | if (qclk->clk_access_cnt == ULONG_MAX) |
| 2749 | goto err; |
| 2750 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2751 | if (qclk->clk_access_cnt > 0) { |
| 2752 | qclk->clk_access_cnt++; |
| 2753 | mutex_unlock(&clk_access_lock); |
| 2754 | return rc; |
| 2755 | } |
| 2756 | |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2757 | /* Enable CE core clk */ |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2758 | rc = clk_prepare_enable(qclk->ce_core_clk); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2759 | if (rc) { |
| 2760 | pr_err("Unable to enable/prepare CE core clk\n"); |
| 2761 | goto err; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2762 | } |
| 2763 | /* Enable CE clk */ |
| 2764 | rc = clk_prepare_enable(qclk->ce_clk); |
| 2765 | if (rc) { |
| 2766 | pr_err("Unable to enable/prepare CE iface clk\n"); |
| 2767 | goto ce_clk_err; |
| 2768 | } |
| 2769 | /* Enable AXI clk */ |
| 2770 | rc = clk_prepare_enable(qclk->ce_bus_clk); |
| 2771 | if (rc) { |
| 2772 | pr_err("Unable to enable/prepare CE bus clk\n"); |
| 2773 | goto ce_bus_clk_err; |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2774 | } |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2775 | qclk->clk_access_cnt++; |
| 2776 | mutex_unlock(&clk_access_lock); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2777 | return 0; |
| 2778 | |
| 2779 | ce_bus_clk_err: |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2780 | clk_disable_unprepare(qclk->ce_clk); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2781 | ce_clk_err: |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2782 | clk_disable_unprepare(qclk->ce_core_clk); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2783 | err: |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2784 | mutex_unlock(&clk_access_lock); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2785 | return -EIO; |
| 2786 | } |
| 2787 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2788 | static void __qseecom_disable_clk(enum qseecom_ce_hw_instance ce) |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2789 | { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2790 | struct qseecom_clk *qclk; |
| 2791 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2792 | if (ce == CLK_QSEE) |
| 2793 | qclk = &qseecom.qsee; |
| 2794 | else |
| 2795 | qclk = &qseecom.ce_drv; |
| 2796 | |
| 2797 | mutex_lock(&clk_access_lock); |
Zhen Kong | 1f09c769 | 2013-05-03 17:50:32 -0700 | [diff] [blame] | 2798 | |
| 2799 | if (qclk->clk_access_cnt == 0) { |
| 2800 | mutex_unlock(&clk_access_lock); |
| 2801 | return; |
| 2802 | } |
| 2803 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2804 | if (qclk->clk_access_cnt == 1) { |
| 2805 | if (qclk->ce_clk != NULL) |
| 2806 | clk_disable_unprepare(qclk->ce_clk); |
| 2807 | if (qclk->ce_core_clk != NULL) |
| 2808 | clk_disable_unprepare(qclk->ce_core_clk); |
| 2809 | if (qclk->ce_bus_clk != NULL) |
| 2810 | clk_disable_unprepare(qclk->ce_bus_clk); |
| 2811 | } |
| 2812 | qclk->clk_access_cnt--; |
| 2813 | mutex_unlock(&clk_access_lock); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2814 | } |
| 2815 | |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2816 | static int qsee_vote_for_clock(struct qseecom_dev_handle *data, |
| 2817 | int32_t clk_type) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2818 | { |
| 2819 | int ret = 0; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2820 | struct qseecom_clk *qclk; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2821 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2822 | qclk = &qseecom.qsee; |
| 2823 | if (!qseecom.qsee_perf_client) |
Ramesh Masavarapu | e640e84 | 2012-04-03 11:21:54 -0700 | [diff] [blame] | 2824 | return ret; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2825 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2826 | switch (clk_type) { |
| 2827 | case CLK_DFAB: |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2828 | mutex_lock(&qsee_bw_mutex); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2829 | if (!qseecom.qsee_bw_count) { |
| 2830 | if (qseecom.qsee_sfpb_bw_count > 0) |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2831 | ret = msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2832 | qseecom.qsee_perf_client, 3); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2833 | else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2834 | if (qclk->ce_core_src_clk != NULL) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2835 | ret = __qseecom_enable_clk(CLK_QSEE); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2836 | if (!ret) { |
| 2837 | ret = |
| 2838 | msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2839 | qseecom.qsee_perf_client, 1); |
| 2840 | if ((ret) && |
| 2841 | (qclk->ce_core_src_clk != NULL)) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2842 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2843 | } |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2844 | } |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2845 | if (ret) |
| 2846 | pr_err("DFAB Bandwidth req failed (%d)\n", |
| 2847 | ret); |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2848 | else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2849 | qseecom.qsee_bw_count++; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2850 | data->perf_enabled = true; |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2851 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2852 | } else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2853 | qseecom.qsee_bw_count++; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2854 | data->perf_enabled = true; |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2855 | } |
| 2856 | mutex_unlock(&qsee_bw_mutex); |
| 2857 | break; |
| 2858 | case CLK_SFPB: |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2859 | mutex_lock(&qsee_bw_mutex); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2860 | if (!qseecom.qsee_sfpb_bw_count) { |
| 2861 | if (qseecom.qsee_bw_count > 0) |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2862 | ret = msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2863 | qseecom.qsee_perf_client, 3); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2864 | else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2865 | if (qclk->ce_core_src_clk != NULL) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2866 | ret = __qseecom_enable_clk(CLK_QSEE); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2867 | if (!ret) { |
| 2868 | ret = |
| 2869 | msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2870 | qseecom.qsee_perf_client, 2); |
| 2871 | if ((ret) && |
| 2872 | (qclk->ce_core_src_clk != NULL)) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2873 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 2874 | } |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2875 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2876 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2877 | if (ret) |
| 2878 | pr_err("SFPB Bandwidth req failed (%d)\n", |
| 2879 | ret); |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2880 | else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2881 | qseecom.qsee_sfpb_bw_count++; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2882 | data->fast_load_enabled = true; |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2883 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2884 | } else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2885 | qseecom.qsee_sfpb_bw_count++; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2886 | data->fast_load_enabled = true; |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2887 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2888 | mutex_unlock(&qsee_bw_mutex); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2889 | break; |
| 2890 | default: |
| 2891 | pr_err("Clock type not defined\n"); |
| 2892 | break; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2893 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2894 | return ret; |
| 2895 | } |
| 2896 | |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2897 | static void qsee_disable_clock_vote(struct qseecom_dev_handle *data, |
| 2898 | int32_t clk_type) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2899 | { |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2900 | int32_t ret = 0; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2901 | struct qseecom_clk *qclk; |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 2902 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2903 | qclk = &qseecom.qsee; |
| 2904 | if (!qseecom.qsee_perf_client) |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 2905 | return; |
| 2906 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2907 | switch (clk_type) { |
| 2908 | case CLK_DFAB: |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2909 | mutex_lock(&qsee_bw_mutex); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2910 | if (qseecom.qsee_bw_count == 0) { |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2911 | pr_err("Client error.Extra call to disable DFAB clk\n"); |
| 2912 | mutex_unlock(&qsee_bw_mutex); |
| 2913 | return; |
| 2914 | } |
| 2915 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2916 | if (qseecom.qsee_bw_count == 1) { |
| 2917 | if (qseecom.qsee_sfpb_bw_count > 0) |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2918 | ret = msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2919 | qseecom.qsee_perf_client, 2); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2920 | else { |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2921 | ret = msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2922 | qseecom.qsee_perf_client, 0); |
| 2923 | if ((!ret) && (qclk->ce_core_src_clk != NULL)) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2924 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2925 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2926 | if (ret) |
| 2927 | pr_err("SFPB Bandwidth req fail (%d)\n", |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2928 | ret); |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2929 | else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2930 | qseecom.qsee_bw_count--; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2931 | data->perf_enabled = false; |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2932 | } |
| 2933 | } else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2934 | qseecom.qsee_bw_count--; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2935 | data->perf_enabled = false; |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2936 | } |
| 2937 | mutex_unlock(&qsee_bw_mutex); |
| 2938 | break; |
| 2939 | case CLK_SFPB: |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2940 | mutex_lock(&qsee_bw_mutex); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2941 | if (qseecom.qsee_sfpb_bw_count == 0) { |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2942 | pr_err("Client error.Extra call to disable SFPB clk\n"); |
| 2943 | mutex_unlock(&qsee_bw_mutex); |
| 2944 | return; |
| 2945 | } |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2946 | if (qseecom.qsee_sfpb_bw_count == 1) { |
| 2947 | if (qseecom.qsee_bw_count > 0) |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2948 | ret = msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2949 | qseecom.qsee_perf_client, 1); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2950 | else { |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2951 | ret = msm_bus_scale_client_update_request( |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2952 | qseecom.qsee_perf_client, 0); |
| 2953 | if ((!ret) && (qclk->ce_core_src_clk != NULL)) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 2954 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 2955 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2956 | if (ret) |
| 2957 | pr_err("SFPB Bandwidth req fail (%d)\n", |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2958 | ret); |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2959 | else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2960 | qseecom.qsee_sfpb_bw_count--; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2961 | data->fast_load_enabled = false; |
Mona Hossain | 04d3fac | 2012-12-03 10:10:37 -0800 | [diff] [blame] | 2962 | } |
| 2963 | } else { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 2964 | qseecom.qsee_sfpb_bw_count--; |
Mona Hossain | c9c83c7 | 2013-04-11 12:43:48 -0700 | [diff] [blame] | 2965 | data->fast_load_enabled = false; |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2966 | } |
Ramesh Masavarapu | 8d75658 | 2012-10-03 10:18:06 -0700 | [diff] [blame] | 2967 | mutex_unlock(&qsee_bw_mutex); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 2968 | break; |
| 2969 | default: |
| 2970 | pr_err("Clock type not defined\n"); |
| 2971 | break; |
Ramesh Masavarapu | e640e84 | 2012-04-03 11:21:54 -0700 | [diff] [blame] | 2972 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2973 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 2974 | } |
| 2975 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 2976 | static int qseecom_load_external_elf(struct qseecom_dev_handle *data, |
| 2977 | void __user *argp) |
| 2978 | { |
| 2979 | struct ion_handle *ihandle; /* Ion handle */ |
| 2980 | struct qseecom_load_img_req load_img_req; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 2981 | int ret; |
| 2982 | int set_cpu_ret = 0; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 2983 | ion_phys_addr_t pa = 0; |
| 2984 | uint32_t len; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 2985 | struct cpumask mask; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 2986 | struct qseecom_load_app_ireq load_req; |
| 2987 | struct qseecom_command_scm_resp resp; |
| 2988 | |
| 2989 | /* Copy the relevant information needed for loading the image */ |
Hariprasad Dhalinarasimha | e201495 | 2013-10-01 18:25:21 -0700 | [diff] [blame] | 2990 | if (copy_from_user(&load_img_req, |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 2991 | (void __user *)argp, |
| 2992 | sizeof(struct qseecom_load_img_req))) { |
| 2993 | pr_err("copy_from_user failed\n"); |
| 2994 | return -EFAULT; |
| 2995 | } |
| 2996 | |
| 2997 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 2998 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 2999 | load_img_req.ifd_data_fd); |
| 3000 | if (IS_ERR_OR_NULL(ihandle)) { |
| 3001 | pr_err("Ion client could not retrieve the handle\n"); |
| 3002 | return -ENOMEM; |
| 3003 | } |
| 3004 | |
| 3005 | /* Get the physical address of the ION BUF */ |
| 3006 | ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len); |
Zhen Kong | 05ed446 | 2014-01-28 18:21:30 -0800 | [diff] [blame] | 3007 | if (ret) { |
| 3008 | pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n", |
| 3009 | ret); |
| 3010 | return ret; |
| 3011 | } |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3012 | /* Populate the structure for sending scm call to load image */ |
| 3013 | load_req.qsee_cmd_id = QSEOS_LOAD_EXTERNAL_ELF_COMMAND; |
| 3014 | load_req.mdt_len = load_img_req.mdt_len; |
| 3015 | load_req.img_len = load_img_req.img_len; |
| 3016 | load_req.phy_addr = pa; |
| 3017 | |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3018 | /* SCM_CALL tied to Core0 */ |
| 3019 | mask = CPU_MASK_CPU0; |
| 3020 | set_cpu_ret = set_cpus_allowed_ptr(current, &mask); |
| 3021 | if (set_cpu_ret) { |
| 3022 | pr_err("set_cpus_allowed_ptr failed : ret %d\n", |
| 3023 | set_cpu_ret); |
| 3024 | ret = -EFAULT; |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3025 | goto exit_ion_free; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3026 | } |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3027 | |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 3028 | if (qseecom.support_bus_scaling) { |
| 3029 | mutex_lock(&qsee_bw_mutex); |
| 3030 | ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM); |
| 3031 | mutex_unlock(&qsee_bw_mutex); |
| 3032 | if (ret) { |
| 3033 | ret = -EIO; |
| 3034 | goto exit_cpu_restore; |
| 3035 | } |
| 3036 | } |
| 3037 | |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 3038 | /* Vote for the SFPB clock */ |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 3039 | ret = __qseecom_enable_clk_scale_up(data); |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 3040 | if (ret) { |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 3041 | ret = -EIO; |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 3042 | goto exit_register_bus_bandwidth_needs; |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 3043 | } |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 3044 | msm_ion_do_cache_op(qseecom.ion_clnt, ihandle, NULL, len, |
| 3045 | ION_IOC_CLEAN_INV_CACHES); |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3046 | /* SCM_CALL to load the external elf */ |
| 3047 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req, |
| 3048 | sizeof(struct qseecom_load_app_ireq), |
| 3049 | &resp, sizeof(resp)); |
| 3050 | if (ret) { |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3051 | pr_err("scm_call to load failed : ret %d\n", |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3052 | ret); |
| 3053 | ret = -EFAULT; |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3054 | goto exit_disable_clock; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3055 | } |
| 3056 | |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3057 | switch (resp.result) { |
| 3058 | case QSEOS_RESULT_SUCCESS: |
| 3059 | break; |
| 3060 | case QSEOS_RESULT_INCOMPLETE: |
| 3061 | pr_err("%s: qseos result incomplete\n", __func__); |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3062 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 3063 | if (ret) |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3064 | pr_err("process_incomplete_cmd failed: err: %d\n", ret); |
| 3065 | break; |
| 3066 | case QSEOS_RESULT_FAILURE: |
| 3067 | pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n"); |
| 3068 | ret = -EFAULT; |
| 3069 | break; |
| 3070 | default: |
| 3071 | pr_err("scm_call response result %d not supported\n", |
| 3072 | resp.result); |
| 3073 | ret = -EFAULT; |
| 3074 | break; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3075 | } |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3076 | |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3077 | exit_disable_clock: |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 3078 | __qseecom_disable_clk_scale_down(data); |
Zhen Kong | 4bead3c | 2014-04-14 15:07:13 -0700 | [diff] [blame] | 3079 | |
| 3080 | exit_register_bus_bandwidth_needs: |
| 3081 | if (qseecom.support_bus_scaling) { |
| 3082 | mutex_lock(&qsee_bw_mutex); |
| 3083 | ret = qseecom_unregister_bus_bandwidth_needs(data); |
| 3084 | mutex_unlock(&qsee_bw_mutex); |
| 3085 | } |
| 3086 | |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3087 | exit_cpu_restore: |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3088 | /* Restore the CPU mask */ |
| 3089 | mask = CPU_MASK_ALL; |
| 3090 | set_cpu_ret = set_cpus_allowed_ptr(current, &mask); |
| 3091 | if (set_cpu_ret) { |
| 3092 | pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n", |
| 3093 | set_cpu_ret); |
| 3094 | ret = -EFAULT; |
| 3095 | } |
AnilKumar Chimata | c470bc9 | 2013-06-12 13:08:37 +0530 | [diff] [blame] | 3096 | exit_ion_free: |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3097 | /* Deallocate the handle */ |
| 3098 | if (!IS_ERR_OR_NULL(ihandle)) |
| 3099 | ion_free(qseecom.ion_clnt, ihandle); |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3100 | return ret; |
| 3101 | } |
| 3102 | |
| 3103 | static int qseecom_unload_external_elf(struct qseecom_dev_handle *data) |
| 3104 | { |
| 3105 | int ret = 0; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3106 | int set_cpu_ret = 0; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3107 | struct qseecom_command_scm_resp resp; |
| 3108 | struct qseecom_unload_app_ireq req; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3109 | struct cpumask mask; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3110 | |
AnilKumar Chimata | f732bbe | 2013-07-24 00:44:09 +0530 | [diff] [blame] | 3111 | /* unavailable client app */ |
| 3112 | data->type = QSEECOM_UNAVAILABLE_CLIENT_APP; |
| 3113 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3114 | /* Populate the structure for sending scm call to unload image */ |
| 3115 | req.qsee_cmd_id = QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3116 | |
| 3117 | /* SCM_CALL tied to Core0 */ |
| 3118 | mask = CPU_MASK_CPU0; |
| 3119 | ret = set_cpus_allowed_ptr(current, &mask); |
| 3120 | if (ret) { |
| 3121 | pr_err("set_cpus_allowed_ptr failed : ret %d\n", |
| 3122 | ret); |
| 3123 | return -EFAULT; |
| 3124 | } |
| 3125 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3126 | /* SCM_CALL to unload the external elf */ |
| 3127 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 3128 | sizeof(struct qseecom_unload_app_ireq), |
| 3129 | &resp, sizeof(resp)); |
| 3130 | if (ret) { |
| 3131 | pr_err("scm_call to unload failed : ret %d\n", |
| 3132 | ret); |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3133 | ret = -EFAULT; |
| 3134 | goto qseecom_unload_external_elf_scm_err; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3135 | } |
| 3136 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 3137 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 3138 | if (ret) |
| 3139 | pr_err("process_incomplete_cmd fail err: %d\n", |
| 3140 | ret); |
| 3141 | } else { |
| 3142 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 3143 | pr_err("scm_call to unload image failed resp.result =%d\n", |
| 3144 | resp.result); |
| 3145 | ret = -EFAULT; |
| 3146 | } |
| 3147 | } |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 3148 | |
| 3149 | qseecom_unload_external_elf_scm_err: |
| 3150 | /* Restore the CPU mask */ |
| 3151 | mask = CPU_MASK_ALL; |
| 3152 | set_cpu_ret = set_cpus_allowed_ptr(current, &mask); |
| 3153 | if (set_cpu_ret) { |
| 3154 | pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n", |
| 3155 | set_cpu_ret); |
| 3156 | ret = -EFAULT; |
| 3157 | } |
| 3158 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 3159 | return ret; |
| 3160 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3161 | |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 3162 | static int qseecom_query_app_loaded(struct qseecom_dev_handle *data, |
| 3163 | void __user *argp) |
| 3164 | { |
| 3165 | |
| 3166 | int32_t ret; |
| 3167 | struct qseecom_qseos_app_load_query query_req; |
| 3168 | struct qseecom_check_app_ireq req; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 3169 | struct qseecom_registered_app_list *entry = NULL; |
| 3170 | unsigned long flags = 0; |
Zhen Kong | 59155a7 | 2015-05-20 13:50:04 -0700 | [diff] [blame] | 3171 | bool found_app = false; |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 3172 | |
| 3173 | /* Copy the relevant information needed for loading the image */ |
Hariprasad Dhalinarasimha | e201495 | 2013-10-01 18:25:21 -0700 | [diff] [blame] | 3174 | if (copy_from_user(&query_req, |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 3175 | (void __user *)argp, |
| 3176 | sizeof(struct qseecom_qseos_app_load_query))) { |
| 3177 | pr_err("copy_from_user failed\n"); |
| 3178 | return -EFAULT; |
| 3179 | } |
| 3180 | |
| 3181 | req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND; |
Hariprasad Dhalinarasimha | 7a01882 | 2013-10-03 16:52:16 -0700 | [diff] [blame] | 3182 | query_req.app_name[MAX_APP_NAME_SIZE-1] = '\0'; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 3183 | strlcpy(req.app_name, query_req.app_name, MAX_APP_NAME_SIZE); |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 3184 | |
| 3185 | ret = __qseecom_check_app_exists(req); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 3186 | |
| 3187 | if ((ret == -EINVAL) || (ret == -ENODEV)) { |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 3188 | pr_err(" scm call to check if app is loaded failed"); |
| 3189 | return ret; /* scm call failed */ |
| 3190 | } else if (ret > 0) { |
Mona Hossain | 7c44320 | 2013-04-18 12:08:58 -0700 | [diff] [blame] | 3191 | pr_debug("App id %d (%s) already exists\n", ret, |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 3192 | (char *)(req.app_name)); |
| 3193 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 3194 | list_for_each_entry(entry, |
| 3195 | &qseecom.registered_app_list_head, list){ |
| 3196 | if (entry->app_id == ret) { |
| 3197 | entry->ref_cnt++; |
Zhen Kong | 59155a7 | 2015-05-20 13:50:04 -0700 | [diff] [blame] | 3198 | found_app = true; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 3199 | break; |
| 3200 | } |
| 3201 | } |
| 3202 | spin_unlock_irqrestore( |
| 3203 | &qseecom.registered_app_list_lock, flags); |
| 3204 | data->client.app_id = ret; |
| 3205 | query_req.app_id = ret; |
William Clark | ba7f03c | 2015-03-09 12:07:49 -0700 | [diff] [blame] | 3206 | strlcpy(data->client.app_name, query_req.app_name, |
Zhen Kong | 44c2a6f | 2014-10-31 11:33:34 -0700 | [diff] [blame] | 3207 | MAX_APP_NAME_SIZE); |
Zhen Kong | 59155a7 | 2015-05-20 13:50:04 -0700 | [diff] [blame] | 3208 | /* |
| 3209 | * If app was loaded by appsbl or kernel client before |
| 3210 | * and was not registered, regiser this app now. |
| 3211 | */ |
| 3212 | if (!found_app) { |
| 3213 | pr_debug("Register app %d [%s] which was loaded before\n", |
| 3214 | ret, (char *)query_req.app_name); |
| 3215 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 3216 | if (!entry) { |
| 3217 | pr_err("kmalloc for app entry failed\n"); |
| 3218 | return -ENOMEM; |
| 3219 | } |
| 3220 | entry->app_id = ret; |
| 3221 | entry->ref_cnt = 1; |
| 3222 | strlcpy(entry->app_name, data->client.app_name, |
| 3223 | MAX_APP_NAME_SIZE); |
| 3224 | spin_lock_irqsave(&qseecom.registered_app_list_lock, |
| 3225 | flags); |
| 3226 | list_add_tail(&entry->list, |
| 3227 | &qseecom.registered_app_list_head); |
| 3228 | spin_unlock_irqrestore( |
| 3229 | &qseecom.registered_app_list_lock, flags); |
| 3230 | } |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 3231 | if (copy_to_user(argp, &query_req, sizeof(query_req))) { |
| 3232 | pr_err("copy_to_user failed\n"); |
| 3233 | return -EFAULT; |
| 3234 | } |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 3235 | return -EEXIST; /* app already loaded */ |
| 3236 | } else { |
| 3237 | return 0; /* app not loaded */ |
| 3238 | } |
| 3239 | } |
| 3240 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3241 | static int __qseecom_get_ce_pipe_info( |
| 3242 | enum qseecom_key_management_usage_type usage, |
| 3243 | uint32_t *pipe, uint32_t *ce_hw) |
| 3244 | { |
| 3245 | int ret; |
| 3246 | switch (usage) { |
| 3247 | case QSEOS_KM_USAGE_DISK_ENCRYPTION: |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 3248 | if (qseecom.support_fde) { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3249 | *pipe = qseecom.ce_info.disk_encrypt_pipe; |
| 3250 | *ce_hw = qseecom.ce_info.hlos_ce_hw_instance; |
| 3251 | ret = 0; |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 3252 | |
| 3253 | } else { |
| 3254 | pr_err("info unavailable: disk encr pipe %d ce_hw %d\n", |
| 3255 | qseecom.ce_info.disk_encrypt_pipe, |
| 3256 | qseecom.ce_info.hlos_ce_hw_instance); |
| 3257 | ret = -EINVAL; |
| 3258 | } |
| 3259 | break; |
| 3260 | case QSEOS_KM_USAGE_FILE_ENCRYPTION: |
| 3261 | if (qseecom.support_pfe) { |
| 3262 | *pipe = qseecom.ce_info.file_encrypt_pipe; |
| 3263 | *ce_hw = qseecom.ce_info.hlos_ce_hw_instance; |
| 3264 | ret = 0; |
| 3265 | } else { |
| 3266 | pr_err("info unavailable: file encr pipe %d ce_hw %d\n", |
| 3267 | qseecom.ce_info.file_encrypt_pipe, |
| 3268 | qseecom.ce_info.hlos_ce_hw_instance); |
| 3269 | ret = -EINVAL; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3270 | } |
| 3271 | break; |
| 3272 | default: |
| 3273 | ret = -EINVAL; |
| 3274 | break; |
| 3275 | } |
| 3276 | return ret; |
| 3277 | } |
| 3278 | |
| 3279 | static int __qseecom_generate_and_save_key(struct qseecom_dev_handle *data, |
| 3280 | enum qseecom_key_management_usage_type usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3281 | struct qseecom_key_generate_ireq *ireq) |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3282 | { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3283 | struct qseecom_command_scm_resp resp; |
| 3284 | int ret; |
| 3285 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3286 | if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3287 | usage >= QSEOS_KM_USAGE_MAX) { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3288 | pr_err("Error:: unsupported usage %d\n", usage); |
| 3289 | return -EFAULT; |
| 3290 | } |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3291 | __qseecom_enable_clk(CLK_QSEE); |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3292 | |
| 3293 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3294 | ireq, sizeof(struct qseecom_key_generate_ireq), |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3295 | &resp, sizeof(resp)); |
| 3296 | if (ret) { |
| 3297 | pr_err("scm call to generate key failed : %d\n", ret); |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3298 | __qseecom_disable_clk(CLK_QSEE); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3299 | return -EFAULT; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | switch (resp.result) { |
| 3303 | case QSEOS_RESULT_SUCCESS: |
| 3304 | break; |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3305 | case QSEOS_RESULT_FAIL_KEY_ID_EXISTS: |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3306 | pr_debug("Key ID exists.\n"); |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3307 | break; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3308 | case QSEOS_RESULT_INCOMPLETE: |
| 3309 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3310 | if (ret) { |
| 3311 | if (resp.result == QSEOS_RESULT_FAIL_KEY_ID_EXISTS) { |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3312 | pr_debug("Key ID exists.\n"); |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3313 | ret = 0; |
| 3314 | } else { |
| 3315 | pr_err("process_incomplete_cmd FAILED, resp.result %d\n", |
| 3316 | resp.result); |
| 3317 | } |
| 3318 | } |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3319 | break; |
| 3320 | case QSEOS_RESULT_FAILURE: |
| 3321 | default: |
| 3322 | pr_err("gen key scm call failed resp.result %d\n", resp.result); |
| 3323 | ret = -EINVAL; |
| 3324 | break; |
| 3325 | } |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3326 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3327 | return ret; |
| 3328 | } |
| 3329 | |
| 3330 | static int __qseecom_delete_saved_key(struct qseecom_dev_handle *data, |
| 3331 | enum qseecom_key_management_usage_type usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3332 | struct qseecom_key_delete_ireq *ireq) |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3333 | { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3334 | struct qseecom_command_scm_resp resp; |
| 3335 | int ret; |
| 3336 | |
Amir Samuelov | 48a1e7d | 2014-06-23 11:49:05 +0300 | [diff] [blame] | 3337 | if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3338 | usage >= QSEOS_KM_USAGE_MAX) { |
| 3339 | pr_err("Error:: unsupported usage %d\n", usage); |
| 3340 | return -EFAULT; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3341 | } |
| 3342 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3343 | __qseecom_enable_clk(CLK_QSEE); |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3344 | |
| 3345 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3346 | ireq, sizeof(struct qseecom_key_delete_ireq), |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3347 | &resp, sizeof(struct qseecom_command_scm_resp)); |
| 3348 | if (ret) { |
| 3349 | pr_err("scm call to delete key failed : %d\n", ret); |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3350 | __qseecom_disable_clk(CLK_QSEE); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3351 | return -EFAULT; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | switch (resp.result) { |
| 3355 | case QSEOS_RESULT_SUCCESS: |
| 3356 | break; |
| 3357 | case QSEOS_RESULT_INCOMPLETE: |
| 3358 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3359 | if (ret) { |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3360 | pr_err("process_incomplete_cmd FAILED, resp.result %d\n", |
| 3361 | resp.result); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3362 | if (resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) { |
| 3363 | pr_debug("Max attempts to input password reached.\n"); |
| 3364 | ret = -ERANGE; |
| 3365 | } |
| 3366 | } |
| 3367 | break; |
| 3368 | case QSEOS_RESULT_FAIL_MAX_ATTEMPT: |
| 3369 | pr_debug("Max attempts to input password reached.\n"); |
| 3370 | ret = -ERANGE; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3371 | break; |
| 3372 | case QSEOS_RESULT_FAILURE: |
| 3373 | default: |
| 3374 | pr_err("Delete key scm call failed resp.result %d\n", |
| 3375 | resp.result); |
| 3376 | ret = -EINVAL; |
| 3377 | break; |
| 3378 | } |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3379 | __qseecom_disable_clk(CLK_QSEE); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3380 | return ret; |
| 3381 | } |
| 3382 | |
| 3383 | static int __qseecom_set_clear_ce_key(struct qseecom_dev_handle *data, |
| 3384 | enum qseecom_key_management_usage_type usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3385 | struct qseecom_key_select_ireq *ireq) |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3386 | { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3387 | struct qseecom_command_scm_resp resp; |
| 3388 | int ret; |
| 3389 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3390 | if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3391 | usage >= QSEOS_KM_USAGE_MAX) { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3392 | pr_err("Error:: unsupported usage %d\n", usage); |
| 3393 | return -EFAULT; |
| 3394 | } |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3395 | |
Zhen Kong | db2bf74 | 2013-05-13 23:55:42 -0700 | [diff] [blame] | 3396 | __qseecom_enable_clk(CLK_QSEE); |
| 3397 | if (qseecom.qsee.instance != qseecom.ce_drv.instance) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3398 | __qseecom_enable_clk(CLK_CE_DRV); |
| 3399 | |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3400 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3401 | ireq, sizeof(struct qseecom_key_select_ireq), |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3402 | &resp, sizeof(struct qseecom_command_scm_resp)); |
| 3403 | if (ret) { |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3404 | pr_err("scm call to set QSEOS_PIPE_ENC key failed : %d\n", ret); |
Zhen Kong | db2bf74 | 2013-05-13 23:55:42 -0700 | [diff] [blame] | 3405 | __qseecom_disable_clk(CLK_QSEE); |
| 3406 | if (qseecom.qsee.instance != qseecom.ce_drv.instance) |
| 3407 | __qseecom_disable_clk(CLK_CE_DRV); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3408 | return -EFAULT; |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3409 | } |
| 3410 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3411 | switch (resp.result) { |
| 3412 | case QSEOS_RESULT_SUCCESS: |
| 3413 | break; |
| 3414 | case QSEOS_RESULT_INCOMPLETE: |
| 3415 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3416 | if (ret) { |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 3417 | pr_err("process_incomplete_cmd FAILED, resp.result %d\n", |
| 3418 | resp.result); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3419 | if (resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) { |
| 3420 | pr_debug("Max attempts to input password reached.\n"); |
| 3421 | ret = -ERANGE; |
| 3422 | } |
| 3423 | } |
| 3424 | break; |
| 3425 | case QSEOS_RESULT_FAIL_MAX_ATTEMPT: |
| 3426 | pr_debug("Max attempts to input password reached.\n"); |
| 3427 | ret = -ERANGE; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3428 | break; |
| 3429 | case QSEOS_RESULT_FAILURE: |
| 3430 | default: |
| 3431 | pr_err("Set key scm call failed resp.result %d\n", resp.result); |
| 3432 | ret = -EINVAL; |
| 3433 | break; |
| 3434 | } |
| 3435 | |
Zhen Kong | db2bf74 | 2013-05-13 23:55:42 -0700 | [diff] [blame] | 3436 | __qseecom_disable_clk(CLK_QSEE); |
| 3437 | if (qseecom.qsee.instance != qseecom.ce_drv.instance) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 3438 | __qseecom_disable_clk(CLK_CE_DRV); |
| 3439 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3440 | return ret; |
| 3441 | } |
| 3442 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3443 | static int __qseecom_update_current_key_user_info( |
| 3444 | struct qseecom_dev_handle *data, |
| 3445 | enum qseecom_key_management_usage_type usage, |
| 3446 | struct qseecom_key_userinfo_update_ireq *ireq) |
| 3447 | { |
| 3448 | struct qseecom_command_scm_resp resp; |
| 3449 | int ret; |
| 3450 | |
| 3451 | if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3452 | usage >= QSEOS_KM_USAGE_MAX) { |
| 3453 | pr_err("Error:: unsupported usage %d\n", usage); |
| 3454 | return -EFAULT; |
| 3455 | } |
| 3456 | |
| 3457 | __qseecom_enable_clk(CLK_QSEE); |
| 3458 | |
| 3459 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, |
| 3460 | ireq, sizeof(struct qseecom_key_userinfo_update_ireq), |
| 3461 | &resp, sizeof(struct qseecom_command_scm_resp)); |
| 3462 | if (ret) { |
| 3463 | pr_err("scm call to update key userinfo failed : %d\n", ret); |
| 3464 | __qseecom_disable_clk(CLK_QSEE); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3465 | return -EFAULT; |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | switch (resp.result) { |
| 3469 | case QSEOS_RESULT_SUCCESS: |
| 3470 | break; |
| 3471 | case QSEOS_RESULT_INCOMPLETE: |
| 3472 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 3473 | if (ret) |
| 3474 | pr_err("process_incomplete_cmd FAILED, resp.result %d\n", |
| 3475 | resp.result); |
| 3476 | break; |
| 3477 | case QSEOS_RESULT_FAILURE: |
| 3478 | default: |
| 3479 | pr_err("Set key scm call failed resp.result %d\n", resp.result); |
| 3480 | ret = -EINVAL; |
| 3481 | break; |
| 3482 | } |
| 3483 | |
| 3484 | __qseecom_disable_clk(CLK_QSEE); |
| 3485 | return ret; |
| 3486 | } |
| 3487 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3488 | static int qseecom_create_key(struct qseecom_dev_handle *data, |
| 3489 | void __user *argp) |
| 3490 | { |
| 3491 | uint32_t ce_hw = 0; |
| 3492 | uint32_t pipe = 0; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3493 | int ret = 0; |
| 3494 | uint32_t flags = 0; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3495 | struct qseecom_create_key_req create_key_req; |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3496 | struct qseecom_key_generate_ireq generate_key_ireq; |
| 3497 | struct qseecom_key_select_ireq set_key_ireq; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3498 | |
| 3499 | ret = copy_from_user(&create_key_req, argp, sizeof(create_key_req)); |
| 3500 | if (ret) { |
| 3501 | pr_err("copy_from_user failed\n"); |
| 3502 | return ret; |
| 3503 | } |
| 3504 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3505 | if (create_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3506 | create_key_req.usage >= QSEOS_KM_USAGE_MAX) { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3507 | pr_err("Error:: unsupported usage %d\n", create_key_req.usage); |
| 3508 | return -EFAULT; |
| 3509 | } |
| 3510 | |
| 3511 | ret = __qseecom_get_ce_pipe_info(create_key_req.usage, &pipe, &ce_hw); |
| 3512 | if (ret) { |
| 3513 | pr_err("Failed to retrieve pipe/ce_hw info: %d\n", ret); |
| 3514 | return -EINVAL; |
| 3515 | } |
| 3516 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3517 | generate_key_ireq.flags = flags; |
| 3518 | generate_key_ireq.qsee_command_id = QSEOS_GENERATE_KEY; |
| 3519 | memset((void *)generate_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE); |
| 3520 | memset((void *)generate_key_ireq.hash32, 0, QSEECOM_HASH_SIZE); |
| 3521 | memcpy((void *)generate_key_ireq.key_id, |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3522 | (void *)key_id_array[create_key_req.usage].desc, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3523 | QSEECOM_KEY_ID_SIZE); |
| 3524 | memcpy((void *)generate_key_ireq.hash32, |
| 3525 | (void *)create_key_req.hash32, QSEECOM_HASH_SIZE); |
| 3526 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3527 | ret = __qseecom_generate_and_save_key(data, create_key_req.usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3528 | &generate_key_ireq); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3529 | if (ret) { |
| 3530 | pr_err("Failed to generate key on storage: %d\n", ret); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3531 | return ret; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3532 | } |
| 3533 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3534 | set_key_ireq.qsee_command_id = QSEOS_SET_KEY; |
| 3535 | set_key_ireq.ce = ce_hw; |
| 3536 | set_key_ireq.pipe = pipe; |
| 3537 | set_key_ireq.flags = flags; |
| 3538 | |
| 3539 | /* set both PIPE_ENC and PIPE_ENC_XTS*/ |
| 3540 | set_key_ireq.pipe_type = QSEOS_PIPE_ENC|QSEOS_PIPE_ENC_XTS; |
| 3541 | memset((void *)set_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE); |
| 3542 | memset((void *)set_key_ireq.hash32, 0, QSEECOM_HASH_SIZE); |
| 3543 | memcpy((void *)set_key_ireq.key_id, |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3544 | (void *)key_id_array[create_key_req.usage].desc, |
| 3545 | QSEECOM_KEY_ID_SIZE); |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3546 | memcpy((void *)set_key_ireq.hash32, (void *)create_key_req.hash32, |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3547 | QSEECOM_HASH_SIZE); |
| 3548 | |
| 3549 | ret = __qseecom_set_clear_ce_key(data, create_key_req.usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3550 | &set_key_ireq); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3551 | if (ret) { |
| 3552 | pr_err("Failed to create key: pipe %d, ce %d: %d\n", |
| 3553 | pipe, ce_hw, ret); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3554 | return ret; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | static int qseecom_wipe_key(struct qseecom_dev_handle *data, |
| 3561 | void __user *argp) |
| 3562 | { |
| 3563 | uint32_t ce_hw = 0; |
| 3564 | uint32_t pipe = 0; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3565 | int ret = 0; |
| 3566 | uint32_t flags = 0; |
| 3567 | int i; |
| 3568 | struct qseecom_wipe_key_req wipe_key_req; |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3569 | struct qseecom_key_delete_ireq delete_key_ireq; |
| 3570 | struct qseecom_key_select_ireq clear_key_ireq; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3571 | |
| 3572 | ret = copy_from_user(&wipe_key_req, argp, sizeof(wipe_key_req)); |
| 3573 | if (ret) { |
| 3574 | pr_err("copy_from_user failed\n"); |
| 3575 | return ret; |
| 3576 | } |
| 3577 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3578 | if (wipe_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3579 | wipe_key_req.usage >= QSEOS_KM_USAGE_MAX) { |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3580 | pr_err("Error:: unsupported usage %d\n", wipe_key_req.usage); |
| 3581 | return -EFAULT; |
| 3582 | } |
| 3583 | |
| 3584 | ret = __qseecom_get_ce_pipe_info(wipe_key_req.usage, &pipe, &ce_hw); |
| 3585 | if (ret) { |
| 3586 | pr_err("Failed to retrieve pipe/ce_hw info: %d\n", ret); |
| 3587 | return -EINVAL; |
| 3588 | } |
| 3589 | |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3590 | if (wipe_key_req.wipe_key_flag) { |
| 3591 | delete_key_ireq.flags = flags; |
| 3592 | delete_key_ireq.qsee_command_id = QSEOS_DELETE_KEY; |
| 3593 | memset((void *)delete_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE); |
| 3594 | memcpy((void *)delete_key_ireq.key_id, |
| 3595 | (void *)key_id_array[wipe_key_req.usage].desc, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3596 | QSEECOM_KEY_ID_SIZE); |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3597 | memset((void *)delete_key_ireq.hash32, 0, QSEECOM_HASH_SIZE); |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3598 | |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3599 | ret = __qseecom_delete_saved_key(data, wipe_key_req.usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3600 | &delete_key_ireq); |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3601 | if (ret) { |
| 3602 | pr_err("Failed to delete key from ssd storage: %d\n", |
| 3603 | ret); |
| 3604 | return -EFAULT; |
| 3605 | } |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3608 | clear_key_ireq.qsee_command_id = QSEOS_SET_KEY; |
| 3609 | clear_key_ireq.ce = ce_hw; |
| 3610 | clear_key_ireq.pipe = pipe; |
| 3611 | clear_key_ireq.flags = flags; |
| 3612 | clear_key_ireq.pipe_type = QSEOS_PIPE_ENC|QSEOS_PIPE_ENC_XTS; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3613 | for (i = 0; i < QSEECOM_KEY_ID_SIZE; i++) |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3614 | clear_key_ireq.key_id[i] = QSEECOM_INVALID_KEY_ID; |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3615 | memset((void *)clear_key_ireq.hash32, 0, QSEECOM_HASH_SIZE); |
| 3616 | |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3617 | ret = __qseecom_set_clear_ce_key(data, wipe_key_req.usage, |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3618 | &clear_key_ireq); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 3619 | if (ret) { |
| 3620 | pr_err("Failed to wipe key: pipe %d, ce %d: %d\n", |
| 3621 | pipe, ce_hw, ret); |
| 3622 | return -EFAULT; |
| 3623 | } |
| 3624 | |
| 3625 | return ret; |
| 3626 | } |
| 3627 | |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3628 | static int qseecom_update_key_user_info(struct qseecom_dev_handle *data, |
| 3629 | void __user *argp) |
| 3630 | { |
| 3631 | int ret = 0; |
| 3632 | uint32_t flags = 0; |
| 3633 | struct qseecom_update_key_userinfo_req update_key_req; |
| 3634 | struct qseecom_key_userinfo_update_ireq ireq; |
| 3635 | |
| 3636 | ret = copy_from_user(&update_key_req, argp, sizeof(update_key_req)); |
| 3637 | if (ret) { |
| 3638 | pr_err("copy_from_user failed\n"); |
| 3639 | return ret; |
| 3640 | } |
| 3641 | |
| 3642 | if (update_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION || |
| 3643 | update_key_req.usage >= QSEOS_KM_USAGE_MAX) { |
| 3644 | pr_err("Error:: unsupported usage %d\n", update_key_req.usage); |
| 3645 | return -EFAULT; |
| 3646 | } |
| 3647 | |
| 3648 | ireq.qsee_command_id = QSEOS_UPDATE_KEY_USERINFO; |
| 3649 | ireq.flags = flags; |
| 3650 | memset(ireq.key_id, 0, QSEECOM_KEY_ID_SIZE); |
| 3651 | memset((void *)ireq.current_hash32, 0, QSEECOM_HASH_SIZE); |
| 3652 | memset((void *)ireq.new_hash32, 0, QSEECOM_HASH_SIZE); |
Zhen Kong | ba69dfe | 2014-02-28 15:19:53 -0800 | [diff] [blame] | 3653 | memcpy((void *)ireq.key_id, |
| 3654 | (void *)key_id_array[update_key_req.usage].desc, |
| 3655 | QSEECOM_KEY_ID_SIZE); |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3656 | memcpy((void *)ireq.current_hash32, |
| 3657 | (void *)update_key_req.current_hash32, QSEECOM_HASH_SIZE); |
| 3658 | memcpy((void *)ireq.new_hash32, |
| 3659 | (void *)update_key_req.new_hash32, QSEECOM_HASH_SIZE); |
| 3660 | |
| 3661 | ret = __qseecom_update_current_key_user_info(data, update_key_req.usage, |
| 3662 | &ireq); |
| 3663 | if (ret) { |
| 3664 | pr_err("Failed to update key info: %d\n", ret); |
Zhen Kong | ca39e44 | 2013-12-25 22:57:08 -0800 | [diff] [blame] | 3665 | return ret; |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 3666 | } |
| 3667 | return ret; |
| 3668 | |
| 3669 | } |
| 3670 | |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 3671 | static int qseecom_is_es_activated(void __user *argp) |
| 3672 | { |
| 3673 | struct qseecom_is_es_activated_req req; |
| 3674 | int ret; |
| 3675 | int resp_buf; |
| 3676 | |
| 3677 | if (qseecom.qsee_version < QSEE_VERSION_04) { |
| 3678 | pr_err("invalid qsee version"); |
| 3679 | return -ENODEV; |
| 3680 | } |
| 3681 | |
| 3682 | if (argp == NULL) { |
| 3683 | pr_err("arg is null"); |
| 3684 | return -EINVAL; |
| 3685 | } |
| 3686 | |
| 3687 | ret = scm_call(SCM_SVC_ES, SCM_IS_ACTIVATED_ID, NULL, 0, |
| 3688 | (void *) &resp_buf, sizeof(resp_buf)); |
| 3689 | if (ret) { |
| 3690 | pr_err("scm_call failed"); |
| 3691 | return ret; |
| 3692 | } |
| 3693 | |
| 3694 | req.is_activated = resp_buf; |
| 3695 | ret = copy_to_user(argp, &req, sizeof(req)); |
| 3696 | if (ret) { |
| 3697 | pr_err("copy_to_user failed"); |
| 3698 | return ret; |
| 3699 | } |
| 3700 | |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
| 3704 | static int qseecom_save_partition_hash(void __user *argp) |
| 3705 | { |
| 3706 | struct qseecom_save_partition_hash_req req; |
| 3707 | int ret; |
| 3708 | |
| 3709 | if (qseecom.qsee_version < QSEE_VERSION_04) { |
| 3710 | pr_err("invalid qsee version "); |
| 3711 | return -ENODEV; |
| 3712 | } |
| 3713 | |
| 3714 | if (argp == NULL) { |
| 3715 | pr_err("arg is null"); |
| 3716 | return -EINVAL; |
| 3717 | } |
| 3718 | |
| 3719 | ret = copy_from_user(&req, argp, sizeof(req)); |
| 3720 | if (ret) { |
| 3721 | pr_err("copy_from_user failed"); |
| 3722 | return ret; |
| 3723 | } |
| 3724 | |
| 3725 | ret = scm_call(SCM_SVC_ES, SCM_SAVE_PARTITION_HASH_ID, |
| 3726 | (void *) &req, sizeof(req), NULL, 0); |
| 3727 | if (ret) { |
Hariprasad Dhalinarasimha | 682d1c3 | 2013-05-19 21:22:10 -0700 | [diff] [blame] | 3728 | pr_err("qseecom_scm_call failed"); |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 3729 | return ret; |
| 3730 | } |
| 3731 | |
| 3732 | return 0; |
| 3733 | } |
| 3734 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3735 | static long qseecom_ioctl(struct file *file, unsigned cmd, |
| 3736 | unsigned long arg) |
| 3737 | { |
| 3738 | int ret = 0; |
| 3739 | struct qseecom_dev_handle *data = file->private_data; |
| 3740 | void __user *argp = (void __user *) arg; |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3741 | bool perf_enabled = false; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3742 | |
AnilKumar Chimata | 11e1f52 | 2013-07-23 06:02:23 +0530 | [diff] [blame] | 3743 | if (!data) { |
| 3744 | pr_err("Invalid/uninitialized device handle\n"); |
| 3745 | return -EINVAL; |
| 3746 | } |
| 3747 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3748 | if (data->abort) { |
| 3749 | pr_err("Aborting qseecom driver\n"); |
| 3750 | return -ENODEV; |
| 3751 | } |
| 3752 | |
| 3753 | switch (cmd) { |
| 3754 | case QSEECOM_IOCTL_REGISTER_LISTENER_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3755 | if (data->type != QSEECOM_GENERIC) { |
| 3756 | pr_err("reg lstnr req: invalid handle (%d)\n", |
| 3757 | data->type); |
| 3758 | ret = -EINVAL; |
| 3759 | break; |
| 3760 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3761 | pr_debug("ioctl register_listener_req()\n"); |
| 3762 | atomic_inc(&data->ioctl_count); |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 3763 | data->type = QSEECOM_LISTENER_SERVICE; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3764 | ret = qseecom_register_listener(data, argp); |
| 3765 | atomic_dec(&data->ioctl_count); |
| 3766 | wake_up_all(&data->abort_wq); |
| 3767 | if (ret) |
| 3768 | pr_err("failed qseecom_register_listener: %d\n", ret); |
| 3769 | break; |
| 3770 | } |
| 3771 | case QSEECOM_IOCTL_UNREGISTER_LISTENER_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3772 | if ((data->listener.id == 0) || |
| 3773 | (data->type != QSEECOM_LISTENER_SERVICE)) { |
| 3774 | pr_err("unreg lstnr req: invalid handle (%d) lid(%d)\n", |
| 3775 | data->type, data->listener.id); |
| 3776 | ret = -EINVAL; |
| 3777 | break; |
| 3778 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3779 | pr_debug("ioctl unregister_listener_req()\n"); |
| 3780 | atomic_inc(&data->ioctl_count); |
| 3781 | ret = qseecom_unregister_listener(data); |
| 3782 | atomic_dec(&data->ioctl_count); |
| 3783 | wake_up_all(&data->abort_wq); |
| 3784 | if (ret) |
| 3785 | pr_err("failed qseecom_unregister_listener: %d\n", ret); |
| 3786 | break; |
| 3787 | } |
| 3788 | case QSEECOM_IOCTL_SEND_CMD_REQ: { |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 3789 | pr_debug("qseecom.current_mode %d\n", qseecom.current_mode); |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3790 | if ((data->client.app_id == 0) || |
| 3791 | (data->type != QSEECOM_CLIENT_APP)) { |
| 3792 | pr_err("send cmd req: invalid handle (%d) app_id(%d)\n", |
| 3793 | data->type, data->client.app_id); |
| 3794 | ret = -EINVAL; |
| 3795 | break; |
| 3796 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3797 | /* Only one client allowed here at a time */ |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 3798 | mutex_lock(&app_access_lock); |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 3799 | if (qseecom.support_bus_scaling) { |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 3800 | /* register bus bw in case the client doesn't do it */ |
| 3801 | if (!data->mode) { |
| 3802 | mutex_lock(&qsee_bw_mutex); |
| 3803 | __qseecom_register_bus_bandwidth_needs( |
| 3804 | data, HIGH); |
| 3805 | mutex_unlock(&qsee_bw_mutex); |
| 3806 | } |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 3807 | ret = qseecom_scale_bus_bandwidth_timer(INACTIVE); |
| 3808 | if (ret) { |
| 3809 | pr_err("Failed to set bw.\n"); |
| 3810 | ret = -EINVAL; |
| 3811 | mutex_unlock(&app_access_lock); |
| 3812 | break; |
| 3813 | } |
| 3814 | } |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 3815 | /* |
| 3816 | * On targets where crypto clock is handled by HLOS, |
| 3817 | * if clk_access_cnt is zero and perf_enabled is false, |
| 3818 | * then the crypto clock was not enabled before sending cmd |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3819 | * to tz, qseecom will enable the clock to avoid service failure. |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 3820 | */ |
| 3821 | if (!qseecom.qsee.clk_access_cnt && !data->perf_enabled) { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3822 | pr_debug("ce clock is not enabled!\n"); |
| 3823 | ret = qseecom_perf_enable(data); |
| 3824 | if (ret) { |
| 3825 | pr_err("Failed to vote for clock with err %d\n", |
| 3826 | ret); |
| 3827 | mutex_unlock(&app_access_lock); |
| 3828 | ret = -EINVAL; |
| 3829 | break; |
| 3830 | } |
| 3831 | perf_enabled = true; |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 3832 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3833 | atomic_inc(&data->ioctl_count); |
| 3834 | ret = qseecom_send_cmd(data, argp); |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 3835 | if (qseecom.support_bus_scaling) |
| 3836 | __qseecom_add_bw_scale_down_timer( |
| 3837 | QSEECOM_SEND_CMD_CRYPTO_TIMEOUT); |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3838 | if (perf_enabled) { |
| 3839 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 3840 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 3841 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3842 | atomic_dec(&data->ioctl_count); |
| 3843 | wake_up_all(&data->abort_wq); |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 3844 | mutex_unlock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3845 | if (ret) |
| 3846 | pr_err("failed qseecom_send_cmd: %d\n", ret); |
| 3847 | break; |
| 3848 | } |
| 3849 | case QSEECOM_IOCTL_SEND_MODFD_CMD_REQ: { |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 3850 | pr_debug("qseecom.current_mode %d\n", qseecom.current_mode); |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3851 | if ((data->client.app_id == 0) || |
| 3852 | (data->type != QSEECOM_CLIENT_APP)) { |
| 3853 | pr_err("send mdfd cmd: invalid handle (%d) appid(%d)\n", |
| 3854 | data->type, data->client.app_id); |
| 3855 | ret = -EINVAL; |
| 3856 | break; |
| 3857 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3858 | /* Only one client allowed here at a time */ |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 3859 | mutex_lock(&app_access_lock); |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 3860 | if (qseecom.support_bus_scaling) { |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 3861 | if (!data->mode) { |
| 3862 | mutex_lock(&qsee_bw_mutex); |
| 3863 | __qseecom_register_bus_bandwidth_needs( |
| 3864 | data, HIGH); |
| 3865 | mutex_unlock(&qsee_bw_mutex); |
| 3866 | } |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 3867 | ret = qseecom_scale_bus_bandwidth_timer(INACTIVE); |
| 3868 | if (ret) { |
| 3869 | pr_err("Failed to set bw.\n"); |
| 3870 | mutex_unlock(&app_access_lock); |
| 3871 | ret = -EINVAL; |
| 3872 | break; |
| 3873 | } |
| 3874 | } |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 3875 | /* |
| 3876 | * On targets where crypto clock is handled by HLOS, |
| 3877 | * if clk_access_cnt is zero and perf_enabled is false, |
| 3878 | * then the crypto clock was not enabled before sending cmd |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3879 | * to tz, qseecom will enable the clock to avoid service failure. |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 3880 | */ |
| 3881 | if (!qseecom.qsee.clk_access_cnt && !data->perf_enabled) { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3882 | pr_debug("ce clock is not enabled!\n"); |
| 3883 | ret = qseecom_perf_enable(data); |
| 3884 | if (ret) { |
| 3885 | pr_err("Failed to vote for clock with err %d\n", |
| 3886 | ret); |
| 3887 | mutex_unlock(&app_access_lock); |
| 3888 | ret = -EINVAL; |
| 3889 | break; |
| 3890 | } |
| 3891 | perf_enabled = true; |
Zhen Kong | 1e15c27 | 2014-09-18 12:33:07 -0700 | [diff] [blame] | 3892 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3893 | atomic_inc(&data->ioctl_count); |
| 3894 | ret = qseecom_send_modfd_cmd(data, argp); |
Zhen Kong | af95019 | 2014-02-05 17:36:23 -0800 | [diff] [blame] | 3895 | if (qseecom.support_bus_scaling) |
| 3896 | __qseecom_add_bw_scale_down_timer( |
| 3897 | QSEECOM_SEND_CMD_CRYPTO_TIMEOUT); |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 3898 | if (perf_enabled) { |
| 3899 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 3900 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 3901 | } |
Zhen Kong | e8a0208 | 2014-03-11 17:36:50 -0700 | [diff] [blame] | 3902 | atomic_dec(&data->ioctl_count); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3903 | wake_up_all(&data->abort_wq); |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 3904 | mutex_unlock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3905 | if (ret) |
| 3906 | pr_err("failed qseecom_send_cmd: %d\n", ret); |
| 3907 | break; |
| 3908 | } |
| 3909 | case QSEECOM_IOCTL_RECEIVE_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3910 | if ((data->listener.id == 0) || |
| 3911 | (data->type != QSEECOM_LISTENER_SERVICE)) { |
| 3912 | pr_err("receive req: invalid handle (%d), lid(%d)\n", |
| 3913 | data->type, data->listener.id); |
| 3914 | ret = -EINVAL; |
| 3915 | break; |
| 3916 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3917 | atomic_inc(&data->ioctl_count); |
| 3918 | ret = qseecom_receive_req(data); |
| 3919 | atomic_dec(&data->ioctl_count); |
| 3920 | wake_up_all(&data->abort_wq); |
Hariprasad Dhalinarasimha | f44cbd2 | 2013-07-20 04:49:34 +0530 | [diff] [blame] | 3921 | if (ret && (ret != -ERESTARTSYS)) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3922 | pr_err("failed qseecom_receive_req: %d\n", ret); |
| 3923 | break; |
| 3924 | } |
| 3925 | case QSEECOM_IOCTL_SEND_RESP_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3926 | if ((data->listener.id == 0) || |
| 3927 | (data->type != QSEECOM_LISTENER_SERVICE)) { |
| 3928 | pr_err("send resp req: invalid handle (%d), lid(%d)\n", |
| 3929 | data->type, data->listener.id); |
| 3930 | ret = -EINVAL; |
| 3931 | break; |
| 3932 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3933 | atomic_inc(&data->ioctl_count); |
| 3934 | ret = qseecom_send_resp(); |
| 3935 | atomic_dec(&data->ioctl_count); |
| 3936 | wake_up_all(&data->abort_wq); |
| 3937 | if (ret) |
| 3938 | pr_err("failed qseecom_send_resp: %d\n", ret); |
| 3939 | break; |
| 3940 | } |
| 3941 | case QSEECOM_IOCTL_SET_MEM_PARAM_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3942 | if ((data->type != QSEECOM_CLIENT_APP) && |
| 3943 | (data->type != QSEECOM_GENERIC) && |
| 3944 | (data->type != QSEECOM_SECURE_SERVICE)) { |
| 3945 | pr_err("set mem param req: invalid handle (%d)\n", |
| 3946 | data->type); |
| 3947 | ret = -EINVAL; |
| 3948 | break; |
| 3949 | } |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 3950 | pr_debug("SET_MEM_PARAM: qseecom addr = 0x%x\n", (u32)data); |
Zhen Kong | f8f3e1b | 2017-02-27 23:41:00 +0200 | [diff] [blame] | 3951 | mutex_lock(&app_access_lock); |
Zhen Kong | 96f5c01 | 2017-05-23 10:21:20 +0800 | [diff] [blame] | 3952 | atomic_inc(&data->ioctl_count); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3953 | ret = qseecom_set_client_mem_param(data, argp); |
Zhen Kong | f8f3e1b | 2017-02-27 23:41:00 +0200 | [diff] [blame] | 3954 | atomic_dec(&data->ioctl_count); |
Zhen Kong | 96f5c01 | 2017-05-23 10:21:20 +0800 | [diff] [blame] | 3955 | mutex_unlock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3956 | if (ret) |
| 3957 | pr_err("failed Qqseecom_set_mem_param request: %d\n", |
| 3958 | ret); |
| 3959 | break; |
| 3960 | } |
| 3961 | case QSEECOM_IOCTL_LOAD_APP_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3962 | if ((data->type != QSEECOM_GENERIC) && |
| 3963 | (data->type != QSEECOM_CLIENT_APP)) { |
| 3964 | pr_err("load app req: invalid handle (%d)\n", |
| 3965 | data->type); |
| 3966 | ret = -EINVAL; |
| 3967 | break; |
| 3968 | } |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 3969 | data->type = QSEECOM_CLIENT_APP; |
| 3970 | pr_debug("LOAD_APP_REQ: qseecom_addr = 0x%x\n", (u32)data); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3971 | mutex_lock(&app_access_lock); |
| 3972 | atomic_inc(&data->ioctl_count); |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 3973 | if (qseecom.qsee_version > QSEEE_VERSION_00) { |
| 3974 | if (qseecom.commonlib_loaded == false) { |
Mona Hossain | 9498f5e | 2013-01-23 18:08:45 -0800 | [diff] [blame] | 3975 | ret = qseecom_load_commonlib_image(data); |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 3976 | if (ret == 0) |
| 3977 | qseecom.commonlib_loaded = true; |
| 3978 | } |
| 3979 | } |
| 3980 | if (ret == 0) |
| 3981 | ret = qseecom_load_app(data, argp); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3982 | atomic_dec(&data->ioctl_count); |
| 3983 | mutex_unlock(&app_access_lock); |
| 3984 | if (ret) |
| 3985 | pr_err("failed load_app request: %d\n", ret); |
| 3986 | break; |
| 3987 | } |
| 3988 | case QSEECOM_IOCTL_UNLOAD_APP_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 3989 | if ((data->client.app_id == 0) || |
| 3990 | (data->type != QSEECOM_CLIENT_APP)) { |
| 3991 | pr_err("unload app req:invalid handle(%d) app_id(%d)\n", |
| 3992 | data->type, data->client.app_id); |
| 3993 | ret = -EINVAL; |
| 3994 | break; |
| 3995 | } |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 3996 | pr_debug("UNLOAD_APP: qseecom_addr = 0x%x\n", (u32)data); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3997 | mutex_lock(&app_access_lock); |
| 3998 | atomic_inc(&data->ioctl_count); |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 3999 | ret = qseecom_unload_app(data, false); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4000 | atomic_dec(&data->ioctl_count); |
| 4001 | mutex_unlock(&app_access_lock); |
| 4002 | if (ret) |
| 4003 | pr_err("failed unload_app request: %d\n", ret); |
| 4004 | break; |
| 4005 | } |
| 4006 | case QSEECOM_IOCTL_GET_QSEOS_VERSION_REQ: { |
| 4007 | atomic_inc(&data->ioctl_count); |
| 4008 | ret = qseecom_get_qseos_version(data, argp); |
| 4009 | if (ret) |
| 4010 | pr_err("qseecom_get_qseos_version: %d\n", ret); |
| 4011 | atomic_dec(&data->ioctl_count); |
| 4012 | break; |
| 4013 | } |
| 4014 | case QSEECOM_IOCTL_PERF_ENABLE_REQ:{ |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4015 | if ((data->type != QSEECOM_GENERIC) && |
| 4016 | (data->type != QSEECOM_CLIENT_APP)) { |
| 4017 | pr_err("perf enable req: invalid handle (%d)\n", |
| 4018 | data->type); |
| 4019 | ret = -EINVAL; |
| 4020 | break; |
| 4021 | } |
| 4022 | if ((data->type == QSEECOM_CLIENT_APP) && |
| 4023 | (data->client.app_id == 0)) { |
| 4024 | pr_err("perf enable req:invalid handle(%d) appid(%d)\n", |
| 4025 | data->type, data->client.app_id); |
| 4026 | ret = -EINVAL; |
| 4027 | break; |
| 4028 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4029 | atomic_inc(&data->ioctl_count); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4030 | if (qseecom.support_bus_scaling) { |
| 4031 | mutex_lock(&qsee_bw_mutex); |
| 4032 | __qseecom_register_bus_bandwidth_needs(data, HIGH); |
| 4033 | mutex_unlock(&qsee_bw_mutex); |
| 4034 | } else { |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 4035 | ret = qseecom_perf_enable(data); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4036 | if (ret) |
Zhen Kong | d08301c | 2014-10-08 17:02:54 -0700 | [diff] [blame] | 4037 | pr_err("Fail to vote for clocks %d\n", ret); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4038 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4039 | atomic_dec(&data->ioctl_count); |
| 4040 | break; |
| 4041 | } |
| 4042 | case QSEECOM_IOCTL_PERF_DISABLE_REQ:{ |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4043 | if ((data->type != QSEECOM_SECURE_SERVICE) && |
| 4044 | (data->type != QSEECOM_CLIENT_APP)) { |
| 4045 | pr_err("perf disable req: invalid handle (%d)\n", |
| 4046 | data->type); |
| 4047 | ret = -EINVAL; |
| 4048 | break; |
| 4049 | } |
| 4050 | if ((data->type == QSEECOM_CLIENT_APP) && |
| 4051 | (data->client.app_id == 0)) { |
| 4052 | pr_err("perf disable: invalid handle (%d)app_id(%d)\n", |
| 4053 | data->type, data->client.app_id); |
| 4054 | ret = -EINVAL; |
| 4055 | break; |
| 4056 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4057 | atomic_inc(&data->ioctl_count); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4058 | if (!qseecom.support_bus_scaling) { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4059 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 4060 | qsee_disable_clock_vote(data, CLK_SFPB); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4061 | } else { |
| 4062 | mutex_lock(&qsee_bw_mutex); |
| 4063 | qseecom_unregister_bus_bandwidth_needs(data); |
| 4064 | mutex_unlock(&qsee_bw_mutex); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4065 | } |
| 4066 | atomic_dec(&data->ioctl_count); |
| 4067 | break; |
| 4068 | } |
| 4069 | |
| 4070 | case QSEECOM_IOCTL_SET_BUS_SCALING_REQ: { |
| 4071 | if ((data->client.app_id == 0) || |
| 4072 | (data->type != QSEECOM_CLIENT_APP)) { |
| 4073 | pr_err("set bus scale: invalid handle (%d) appid(%d)\n", |
| 4074 | data->type, data->client.app_id); |
| 4075 | ret = -EINVAL; |
| 4076 | break; |
| 4077 | } |
| 4078 | atomic_inc(&data->ioctl_count); |
| 4079 | ret = qseecom_scale_bus_bandwidth(data, argp); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4080 | atomic_dec(&data->ioctl_count); |
| 4081 | break; |
| 4082 | } |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 4083 | case QSEECOM_IOCTL_LOAD_EXTERNAL_ELF_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4084 | if (data->type != QSEECOM_GENERIC) { |
| 4085 | pr_err("load ext elf req: invalid client handle (%d)\n", |
| 4086 | data->type); |
| 4087 | ret = -EINVAL; |
| 4088 | break; |
| 4089 | } |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4090 | data->type = QSEECOM_UNAVAILABLE_CLIENT_APP; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 4091 | data->released = true; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 4092 | mutex_lock(&app_access_lock); |
| 4093 | atomic_inc(&data->ioctl_count); |
| 4094 | ret = qseecom_load_external_elf(data, argp); |
| 4095 | atomic_dec(&data->ioctl_count); |
| 4096 | mutex_unlock(&app_access_lock); |
| 4097 | if (ret) |
| 4098 | pr_err("failed load_external_elf request: %d\n", ret); |
| 4099 | break; |
| 4100 | } |
| 4101 | case QSEECOM_IOCTL_UNLOAD_EXTERNAL_ELF_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4102 | if (data->type != QSEECOM_UNAVAILABLE_CLIENT_APP) { |
| 4103 | pr_err("unload ext elf req: invalid handle (%d)\n", |
| 4104 | data->type); |
| 4105 | ret = -EINVAL; |
| 4106 | break; |
| 4107 | } |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 4108 | data->released = true; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 4109 | mutex_lock(&app_access_lock); |
| 4110 | atomic_inc(&data->ioctl_count); |
| 4111 | ret = qseecom_unload_external_elf(data); |
| 4112 | atomic_dec(&data->ioctl_count); |
| 4113 | mutex_unlock(&app_access_lock); |
| 4114 | if (ret) |
| 4115 | pr_err("failed unload_app request: %d\n", ret); |
| 4116 | break; |
| 4117 | } |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 4118 | case QSEECOM_IOCTL_APP_LOADED_QUERY_REQ: { |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4119 | data->type = QSEECOM_CLIENT_APP; |
Hariprasad Dhalinarasimha | ba30614 | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4120 | pr_debug("APP_LOAD_QUERY: qseecom_addr = 0x%x\n", (u32)data); |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 4121 | mutex_lock(&app_access_lock); |
| 4122 | atomic_inc(&data->ioctl_count); |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4123 | pr_debug("APP_LOAD_QUERY: qseecom_addr = 0x%x\n", (u32)data); |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 4124 | ret = qseecom_query_app_loaded(data, argp); |
| 4125 | atomic_dec(&data->ioctl_count); |
| 4126 | mutex_unlock(&app_access_lock); |
| 4127 | break; |
| 4128 | } |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4129 | case QSEECOM_IOCTL_SEND_CMD_SERVICE_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4130 | if (data->type != QSEECOM_GENERIC) { |
| 4131 | pr_err("send cmd svc req: invalid handle (%d)\n", |
| 4132 | data->type); |
| 4133 | ret = -EINVAL; |
| 4134 | break; |
| 4135 | } |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4136 | data->type = QSEECOM_SECURE_SERVICE; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4137 | if (qseecom.qsee_version < QSEE_VERSION_03) { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4138 | pr_err("SEND_CMD_SERVICE_REQ: Invalid qsee ver %u\n", |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4139 | qseecom.qsee_version); |
| 4140 | return -EINVAL; |
| 4141 | } |
| 4142 | mutex_lock(&app_access_lock); |
| 4143 | atomic_inc(&data->ioctl_count); |
| 4144 | ret = qseecom_send_service_cmd(data, argp); |
| 4145 | atomic_dec(&data->ioctl_count); |
| 4146 | mutex_unlock(&app_access_lock); |
| 4147 | break; |
| 4148 | } |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4149 | case QSEECOM_IOCTL_CREATE_KEY_REQ: { |
Zhen Kong | a10dcab | 2014-03-19 10:32:27 -0700 | [diff] [blame] | 4150 | if (!(qseecom.support_pfe || qseecom.support_fde)) |
| 4151 | pr_err("Features requiring key init not supported\n"); |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4152 | if (data->type != QSEECOM_GENERIC) { |
| 4153 | pr_err("create key req: invalid handle (%d)\n", |
| 4154 | data->type); |
| 4155 | ret = -EINVAL; |
| 4156 | break; |
| 4157 | } |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 4158 | if (qseecom.qsee_version < QSEE_VERSION_05) { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4159 | pr_err("Create Key feature unsupported: qsee ver %u\n", |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 4160 | qseecom.qsee_version); |
| 4161 | return -EINVAL; |
| 4162 | } |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4163 | data->released = true; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4164 | atomic_inc(&data->ioctl_count); |
| 4165 | ret = qseecom_create_key(data, argp); |
| 4166 | if (ret) |
| 4167 | pr_err("failed to create encryption key: %d\n", ret); |
| 4168 | |
| 4169 | atomic_dec(&data->ioctl_count); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4170 | break; |
| 4171 | } |
| 4172 | case QSEECOM_IOCTL_WIPE_KEY_REQ: { |
Zhen Kong | a10dcab | 2014-03-19 10:32:27 -0700 | [diff] [blame] | 4173 | if (!(qseecom.support_pfe || qseecom.support_fde)) |
| 4174 | pr_err("Features requiring key init not supported\n"); |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4175 | if (data->type != QSEECOM_GENERIC) { |
| 4176 | pr_err("wipe key req: invalid handle (%d)\n", |
| 4177 | data->type); |
| 4178 | ret = -EINVAL; |
| 4179 | break; |
| 4180 | } |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 4181 | if (qseecom.qsee_version < QSEE_VERSION_05) { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4182 | pr_err("Wipe Key feature unsupported in qsee ver %u\n", |
Zhen Kong | 336636e | 2013-04-15 11:04:54 -0700 | [diff] [blame] | 4183 | qseecom.qsee_version); |
| 4184 | return -EINVAL; |
| 4185 | } |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4186 | data->released = true; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4187 | atomic_inc(&data->ioctl_count); |
| 4188 | ret = qseecom_wipe_key(data, argp); |
| 4189 | if (ret) |
| 4190 | pr_err("failed to wipe encryption key: %d\n", ret); |
| 4191 | atomic_dec(&data->ioctl_count); |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 4192 | break; |
| 4193 | } |
| 4194 | case QSEECOM_IOCTL_UPDATE_KEY_USER_INFO_REQ: { |
Zhen Kong | a10dcab | 2014-03-19 10:32:27 -0700 | [diff] [blame] | 4195 | if (!(qseecom.support_pfe || qseecom.support_fde)) |
| 4196 | pr_err("Features requiring key init not supported\n"); |
Zhen Kong | 9730ddf | 2013-12-17 16:49:43 -0800 | [diff] [blame] | 4197 | if (data->type != QSEECOM_GENERIC) { |
| 4198 | pr_err("update key req: invalid handle (%d)\n", |
| 4199 | data->type); |
| 4200 | ret = -EINVAL; |
| 4201 | break; |
| 4202 | } |
| 4203 | if (qseecom.qsee_version < QSEE_VERSION_05) { |
| 4204 | pr_err("Update Key feature unsupported in qsee ver %u\n", |
| 4205 | qseecom.qsee_version); |
| 4206 | return -EINVAL; |
| 4207 | } |
| 4208 | data->released = true; |
| 4209 | atomic_inc(&data->ioctl_count); |
| 4210 | ret = qseecom_update_key_user_info(data, argp); |
| 4211 | if (ret) |
| 4212 | pr_err("failed to update key user info: %d\n", ret); |
| 4213 | atomic_dec(&data->ioctl_count); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4214 | break; |
| 4215 | } |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 4216 | case QSEECOM_IOCTL_SAVE_PARTITION_HASH_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4217 | if (data->type != QSEECOM_GENERIC) { |
| 4218 | pr_err("save part hash req: invalid handle (%d)\n", |
| 4219 | data->type); |
| 4220 | ret = -EINVAL; |
| 4221 | break; |
| 4222 | } |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 4223 | data->released = true; |
| 4224 | mutex_lock(&app_access_lock); |
| 4225 | atomic_inc(&data->ioctl_count); |
| 4226 | ret = qseecom_save_partition_hash(argp); |
| 4227 | atomic_dec(&data->ioctl_count); |
| 4228 | mutex_unlock(&app_access_lock); |
| 4229 | break; |
| 4230 | } |
| 4231 | case QSEECOM_IOCTL_IS_ES_ACTIVATED_REQ: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4232 | if (data->type != QSEECOM_GENERIC) { |
| 4233 | pr_err("ES activated req: invalid handle (%d)\n", |
| 4234 | data->type); |
| 4235 | ret = -EINVAL; |
| 4236 | break; |
| 4237 | } |
Amir Samuelov | d1fc741 | 2013-03-10 16:56:13 +0200 | [diff] [blame] | 4238 | data->released = true; |
| 4239 | mutex_lock(&app_access_lock); |
| 4240 | atomic_inc(&data->ioctl_count); |
| 4241 | ret = qseecom_is_es_activated(argp); |
| 4242 | atomic_dec(&data->ioctl_count); |
| 4243 | mutex_unlock(&app_access_lock); |
| 4244 | break; |
| 4245 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 4246 | case QSEECOM_IOCTL_SEND_MODFD_RESP: { |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4247 | if ((data->listener.id == 0) || |
| 4248 | (data->type != QSEECOM_LISTENER_SERVICE)) { |
| 4249 | pr_err("receive req: invalid handle (%d), lid(%d)\n", |
| 4250 | data->type, data->listener.id); |
| 4251 | ret = -EINVAL; |
| 4252 | break; |
| 4253 | } |
Hariprasad Dhalinarasimha | 23fdcd9 | 2013-05-01 14:51:09 -0700 | [diff] [blame] | 4254 | /* Only one client allowed here at a time */ |
| 4255 | atomic_inc(&data->ioctl_count); |
| 4256 | ret = qseecom_send_modfd_resp(data, argp); |
| 4257 | atomic_dec(&data->ioctl_count); |
| 4258 | wake_up_all(&data->abort_wq); |
| 4259 | if (ret) |
| 4260 | pr_err("failed qseecom_send_mod_resp: %d\n", ret); |
| 4261 | break; |
| 4262 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4263 | default: |
Mona Hossain | a1124de | 2013-10-01 13:41:09 -0700 | [diff] [blame] | 4264 | pr_err("Invalid IOCTL: %d\n", cmd); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4265 | return -EINVAL; |
| 4266 | } |
| 4267 | return ret; |
| 4268 | } |
| 4269 | |
| 4270 | static int qseecom_open(struct inode *inode, struct file *file) |
| 4271 | { |
| 4272 | int ret = 0; |
| 4273 | struct qseecom_dev_handle *data; |
| 4274 | |
| 4275 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 4276 | if (!data) { |
| 4277 | pr_err("kmalloc failed\n"); |
| 4278 | return -ENOMEM; |
| 4279 | } |
| 4280 | file->private_data = data; |
| 4281 | data->abort = 0; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4282 | data->type = QSEECOM_GENERIC; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4283 | data->released = false; |
William Clark | c7a043d | 2014-05-23 15:08:52 -0700 | [diff] [blame] | 4284 | memset((void *)data->client.app_name, 0, MAX_APP_NAME_SIZE); |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4285 | data->mode = INACTIVE; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4286 | init_waitqueue_head(&data->abort_wq); |
| 4287 | atomic_set(&data->ioctl_count, 0); |
Mona Hossain | d4613de | 2013-05-15 16:49:29 -0700 | [diff] [blame] | 4288 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4289 | return ret; |
| 4290 | } |
| 4291 | |
| 4292 | static int qseecom_release(struct inode *inode, struct file *file) |
| 4293 | { |
| 4294 | struct qseecom_dev_handle *data = file->private_data; |
| 4295 | int ret = 0; |
| 4296 | |
| 4297 | if (data->released == false) { |
AnilKumar Chimata | 58eddab | 2015-04-17 17:17:35 +0530 | [diff] [blame] | 4298 | pr_debug("data: released = false, type = %d, data = 0x%x\n", |
Hariprasad Dhalinarasimha | ba30614 | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4299 | data->type, (u32)data); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4300 | switch (data->type) { |
| 4301 | case QSEECOM_LISTENER_SERVICE: |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4302 | ret = qseecom_unregister_listener(data); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4303 | break; |
| 4304 | case QSEECOM_CLIENT_APP: |
Zhen Kong | 9bfacc5 | 2015-06-09 14:49:36 -0700 | [diff] [blame] | 4305 | mutex_lock(&app_access_lock); |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 4306 | ret = qseecom_unload_app(data, true); |
Zhen Kong | 9bfacc5 | 2015-06-09 14:49:36 -0700 | [diff] [blame] | 4307 | mutex_unlock(&app_access_lock); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4308 | break; |
| 4309 | case QSEECOM_SECURE_SERVICE: |
Mona Hossain | d4b70573 | 2013-04-05 21:56:28 -0700 | [diff] [blame] | 4310 | case QSEECOM_GENERIC: |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4311 | ret = qseecom_unmap_ion_allocated_memory(data); |
Hariprasad Dhalinarasimha | 492530f | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4312 | if (ret) |
Hariprasad Dhalinarasimha | ba30614 | 2013-08-19 12:45:18 -0700 | [diff] [blame] | 4313 | pr_err("Ion Unmap failed\n"); |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4314 | break; |
AnilKumar Chimata | f732bbe | 2013-07-24 00:44:09 +0530 | [diff] [blame] | 4315 | case QSEECOM_UNAVAILABLE_CLIENT_APP: |
| 4316 | break; |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4317 | default: |
| 4318 | pr_err("Unsupported clnt_handle_type %d", |
| 4319 | data->type); |
| 4320 | break; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4321 | } |
| 4322 | } |
Hariprasad Dhalinarasimha | adf27a8 | 2013-02-11 15:07:59 -0800 | [diff] [blame] | 4323 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4324 | if (qseecom.support_bus_scaling) { |
| 4325 | mutex_lock(&qsee_bw_mutex); |
| 4326 | if (data->mode != INACTIVE) { |
| 4327 | qseecom_unregister_bus_bandwidth_needs(data); |
| 4328 | if (qseecom.cumulative_mode == INACTIVE) { |
| 4329 | ret = __qseecom_set_msm_bus_request(INACTIVE); |
| 4330 | if (ret) |
| 4331 | pr_err("Fail to scale down bus\n"); |
| 4332 | } |
| 4333 | } |
| 4334 | mutex_unlock(&qsee_bw_mutex); |
| 4335 | } else { |
| 4336 | if (data->fast_load_enabled == true) |
| 4337 | qsee_disable_clock_vote(data, CLK_SFPB); |
| 4338 | if (data->perf_enabled == true) |
| 4339 | qsee_disable_clock_vote(data, CLK_DFAB); |
| 4340 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4341 | kfree(data); |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 4342 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4343 | return ret; |
| 4344 | } |
| 4345 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4346 | static const struct file_operations qseecom_fops = { |
| 4347 | .owner = THIS_MODULE, |
| 4348 | .unlocked_ioctl = qseecom_ioctl, |
| 4349 | .open = qseecom_open, |
| 4350 | .release = qseecom_release |
| 4351 | }; |
| 4352 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4353 | static int __qseecom_init_clk(enum qseecom_ce_hw_instance ce) |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4354 | { |
| 4355 | int rc = 0; |
| 4356 | struct device *pdev; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4357 | struct qseecom_clk *qclk; |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4358 | char *core_clk_src = NULL; |
| 4359 | char *core_clk = NULL; |
| 4360 | char *iface_clk = NULL; |
| 4361 | char *bus_clk = NULL; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4362 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4363 | switch (ce) { |
| 4364 | case CLK_QSEE: { |
| 4365 | core_clk_src = "core_clk_src"; |
| 4366 | core_clk = "core_clk"; |
| 4367 | iface_clk = "iface_clk"; |
| 4368 | bus_clk = "bus_clk"; |
| 4369 | qclk = &qseecom.qsee; |
| 4370 | qclk->instance = CLK_QSEE; |
| 4371 | break; |
| 4372 | }; |
| 4373 | case CLK_CE_DRV: { |
| 4374 | core_clk_src = "ce_drv_core_clk_src"; |
| 4375 | core_clk = "ce_drv_core_clk"; |
| 4376 | iface_clk = "ce_drv_iface_clk"; |
| 4377 | bus_clk = "ce_drv_bus_clk"; |
| 4378 | qclk = &qseecom.ce_drv; |
| 4379 | qclk->instance = CLK_CE_DRV; |
| 4380 | break; |
| 4381 | }; |
| 4382 | default: |
| 4383 | pr_err("Invalid ce hw instance: %d!\n", ce); |
| 4384 | return -EIO; |
| 4385 | } |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4386 | pdev = qseecom.pdev; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4387 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4388 | /* Get CE3 src core clk. */ |
| 4389 | qclk->ce_core_src_clk = clk_get(pdev, core_clk_src); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4390 | if (!IS_ERR(qclk->ce_core_src_clk)) { |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 4391 | /* Set the core src clk @100Mhz */ |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4392 | rc = clk_set_rate(qclk->ce_core_src_clk, QSEE_CE_CLK_100MHZ); |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4393 | if (rc) { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4394 | clk_put(qclk->ce_core_src_clk); |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4395 | pr_err("Unable to set the core src clk @100Mhz.\n"); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4396 | return -EIO; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4397 | } |
| 4398 | } else { |
| 4399 | pr_warn("Unable to get CE core src clk, set to NULL\n"); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4400 | qclk->ce_core_src_clk = NULL; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | /* Get CE core clk */ |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4404 | qclk->ce_core_clk = clk_get(pdev, core_clk); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4405 | if (IS_ERR(qclk->ce_core_clk)) { |
| 4406 | rc = PTR_ERR(qclk->ce_core_clk); |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4407 | pr_err("Unable to get CE core clk\n"); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4408 | if (qclk->ce_core_src_clk != NULL) |
| 4409 | clk_put(qclk->ce_core_src_clk); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4410 | return -EIO; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | /* Get CE Interface clk */ |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4414 | qclk->ce_clk = clk_get(pdev, iface_clk); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4415 | if (IS_ERR(qclk->ce_clk)) { |
| 4416 | rc = PTR_ERR(qclk->ce_clk); |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4417 | pr_err("Unable to get CE interface clk\n"); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4418 | if (qclk->ce_core_src_clk != NULL) |
| 4419 | clk_put(qclk->ce_core_src_clk); |
| 4420 | clk_put(qclk->ce_core_clk); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4421 | return -EIO; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | /* Get CE AXI clk */ |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4425 | qclk->ce_bus_clk = clk_get(pdev, bus_clk); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4426 | if (IS_ERR(qclk->ce_bus_clk)) { |
| 4427 | rc = PTR_ERR(qclk->ce_bus_clk); |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4428 | pr_err("Unable to get CE BUS interface clk\n"); |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4429 | if (qclk->ce_core_src_clk != NULL) |
| 4430 | clk_put(qclk->ce_core_src_clk); |
| 4431 | clk_put(qclk->ce_core_clk); |
| 4432 | clk_put(qclk->ce_clk); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4433 | return -EIO; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4434 | } |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4435 | return rc; |
| 4436 | } |
| 4437 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4438 | static void __qseecom_deinit_clk(enum qseecom_ce_hw_instance ce) |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4439 | { |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4440 | struct qseecom_clk *qclk; |
| 4441 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4442 | if (ce == CLK_QSEE) |
| 4443 | qclk = &qseecom.qsee; |
| 4444 | else |
| 4445 | qclk = &qseecom.ce_drv; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4446 | |
| 4447 | if (qclk->ce_clk != NULL) { |
| 4448 | clk_put(qclk->ce_clk); |
| 4449 | qclk->ce_clk = NULL; |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4450 | } |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4451 | if (qclk->ce_core_clk != NULL) { |
| 4452 | clk_put(qclk->ce_core_clk); |
| 4453 | qclk->ce_clk = NULL; |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4454 | } |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4455 | if (qclk->ce_bus_clk != NULL) { |
| 4456 | clk_put(qclk->ce_bus_clk); |
| 4457 | qclk->ce_clk = NULL; |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4458 | } |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4459 | if (qclk->ce_core_src_clk != NULL) { |
| 4460 | clk_put(qclk->ce_core_src_clk); |
| 4461 | qclk->ce_core_src_clk = NULL; |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4462 | } |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4463 | } |
| 4464 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 4465 | static int __devinit qseecom_probe(struct platform_device *pdev) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4466 | { |
| 4467 | int rc; |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4468 | int ret = 0; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4469 | struct device *class_dev; |
| 4470 | char qsee_not_legacy = 0; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4471 | struct msm_bus_scale_pdata *qseecom_platform_support = NULL; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4472 | uint32_t system_call_id = QSEOS_CHECK_VERSION_CMD; |
| 4473 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4474 | qseecom.qsee_bw_count = 0; |
| 4475 | qseecom.qsee_perf_client = 0; |
| 4476 | qseecom.qsee_sfpb_bw_count = 0; |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4477 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4478 | qseecom.qsee.ce_core_clk = NULL; |
| 4479 | qseecom.qsee.ce_clk = NULL; |
| 4480 | qseecom.qsee.ce_core_src_clk = NULL; |
| 4481 | qseecom.qsee.ce_bus_clk = NULL; |
Ramesh Masavarapu | e640e84 | 2012-04-03 11:21:54 -0700 | [diff] [blame] | 4482 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4483 | qseecom.cumulative_mode = 0; |
| 4484 | qseecom.current_mode = INACTIVE; |
| 4485 | qseecom.support_bus_scaling = false; |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 4486 | qseecom.support_fde = false; |
| 4487 | qseecom.support_pfe = false; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4488 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4489 | qseecom.ce_drv.ce_core_clk = NULL; |
| 4490 | qseecom.ce_drv.ce_clk = NULL; |
| 4491 | qseecom.ce_drv.ce_core_src_clk = NULL; |
| 4492 | qseecom.ce_drv.ce_bus_clk = NULL; |
| 4493 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4494 | rc = alloc_chrdev_region(&qseecom_device_no, 0, 1, QSEECOM_DEV); |
| 4495 | if (rc < 0) { |
| 4496 | pr_err("alloc_chrdev_region failed %d\n", rc); |
| 4497 | return rc; |
| 4498 | } |
| 4499 | |
| 4500 | driver_class = class_create(THIS_MODULE, QSEECOM_DEV); |
| 4501 | if (IS_ERR(driver_class)) { |
| 4502 | rc = -ENOMEM; |
| 4503 | pr_err("class_create failed %d\n", rc); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4504 | goto exit_unreg_chrdev_region; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | class_dev = device_create(driver_class, NULL, qseecom_device_no, NULL, |
| 4508 | QSEECOM_DEV); |
| 4509 | if (!class_dev) { |
| 4510 | pr_err("class_device_create failed %d\n", rc); |
| 4511 | rc = -ENOMEM; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4512 | goto exit_destroy_class; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4513 | } |
| 4514 | |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4515 | cdev_init(&qseecom.cdev, &qseecom_fops); |
| 4516 | qseecom.cdev.owner = THIS_MODULE; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4517 | |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4518 | rc = cdev_add(&qseecom.cdev, MKDEV(MAJOR(qseecom_device_no), 0), 1); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4519 | if (rc < 0) { |
| 4520 | pr_err("cdev_add failed %d\n", rc); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4521 | goto exit_destroy_device; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | INIT_LIST_HEAD(&qseecom.registered_listener_list_head); |
| 4525 | spin_lock_init(&qseecom.registered_listener_list_lock); |
| 4526 | INIT_LIST_HEAD(&qseecom.registered_app_list_head); |
| 4527 | spin_lock_init(&qseecom.registered_app_list_lock); |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4528 | INIT_LIST_HEAD(&qseecom.registered_kclient_list_head); |
| 4529 | spin_lock_init(&qseecom.registered_kclient_list_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4530 | init_waitqueue_head(&qseecom.send_resp_wq); |
| 4531 | qseecom.send_resp_flag = 0; |
| 4532 | |
| 4533 | rc = scm_call(6, 1, &system_call_id, sizeof(system_call_id), |
| 4534 | &qsee_not_legacy, sizeof(qsee_not_legacy)); |
| 4535 | if (rc) { |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 4536 | pr_err("Failed to retrieve QSEOS version information %d\n", rc); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4537 | goto exit_del_cdev; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4538 | } |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 4539 | if (qsee_not_legacy) { |
| 4540 | uint32_t feature = 10; |
| 4541 | |
| 4542 | qseecom.qsee_version = QSEEE_VERSION_00; |
| 4543 | rc = scm_call(6, 3, &feature, sizeof(feature), |
| 4544 | &qseecom.qsee_version, sizeof(qseecom.qsee_version)); |
| 4545 | if (rc) { |
| 4546 | pr_err("Failed to get QSEE version info %d\n", rc); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4547 | goto exit_del_cdev; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 4548 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4549 | qseecom.qseos_version = QSEOS_VERSION_14; |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 4550 | } else { |
Mona Hossain | 9c1f6c5 | 2013-05-19 21:27:26 -0700 | [diff] [blame] | 4551 | pr_err("QSEE legacy version is not supported:"); |
| 4552 | pr_err("Support for TZ1.3 and earlier is deprecated\n"); |
| 4553 | rc = -EINVAL; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4554 | goto exit_del_cdev; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4555 | } |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 4556 | qseecom.commonlib_loaded = false; |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4557 | qseecom.pdev = class_dev; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4558 | /* Create ION msm client */ |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4559 | qseecom.ion_clnt = msm_ion_client_create(-1, "qseecom-kernel"); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4560 | if (qseecom.ion_clnt == NULL) { |
| 4561 | pr_err("Ion client cannot be created\n"); |
| 4562 | rc = -ENOMEM; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4563 | goto exit_del_cdev; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | /* register client for bus scaling */ |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4567 | if (pdev->dev.of_node) { |
AnilKumar Chimata | bb51272 | 2014-01-29 00:12:35 +0530 | [diff] [blame] | 4568 | qseecom.pdev->of_node = pdev->dev.of_node; |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4569 | qseecom.support_bus_scaling = |
| 4570 | of_property_read_bool((&pdev->dev)->of_node, |
| 4571 | "qcom,support-bus-scaling"); |
| 4572 | pr_warn("support_bus_scaling=0x%x", |
| 4573 | qseecom.support_bus_scaling); |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 4574 | qseecom.support_fde = |
| 4575 | of_property_read_bool((&pdev->dev)->of_node, |
| 4576 | "qcom,support-fde"); |
| 4577 | if (qseecom.support_fde) { |
| 4578 | if (of_property_read_u32((&pdev->dev)->of_node, |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4579 | "qcom,disk-encrypt-pipe-pair", |
| 4580 | &qseecom.ce_info.disk_encrypt_pipe)) { |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 4581 | pr_err("Fail to get FDE pipe information.\n"); |
| 4582 | rc = -EINVAL; |
| 4583 | goto exit_destroy_ion_client; |
| 4584 | } else { |
| 4585 | pr_warn("disk-encrypt-pipe-pair=0x%x", |
| 4586 | qseecom.ce_info.disk_encrypt_pipe); |
| 4587 | } |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4588 | } else { |
Zhen Kong | 4ffeacf | 2014-02-27 17:21:08 -0800 | [diff] [blame] | 4589 | pr_warn("Device does not support FDE"); |
| 4590 | qseecom.ce_info.disk_encrypt_pipe = 0xff; |
| 4591 | } |
| 4592 | qseecom.support_pfe = |
| 4593 | of_property_read_bool((&pdev->dev)->of_node, |
| 4594 | "qcom,support-pfe"); |
| 4595 | if (qseecom.support_pfe) { |
| 4596 | if (of_property_read_u32((&pdev->dev)->of_node, |
| 4597 | "qcom,file-encrypt-pipe-pair", |
| 4598 | &qseecom.ce_info.disk_encrypt_pipe)) { |
| 4599 | pr_err("Fail to get PFE pipe information.\n"); |
| 4600 | rc = -EINVAL; |
| 4601 | goto exit_destroy_ion_client; |
| 4602 | } else { |
| 4603 | pr_warn("file-encrypt-pipe-pair=0x%x", |
| 4604 | qseecom.ce_info.file_encrypt_pipe); |
| 4605 | } |
| 4606 | } else { |
| 4607 | pr_warn("Device does not support PFE"); |
| 4608 | qseecom.ce_info.file_encrypt_pipe = 0xff; |
| 4609 | } |
| 4610 | if (qseecom.support_pfe || qseecom.support_fde) { |
| 4611 | if (of_property_read_u32((&pdev->dev)->of_node, |
| 4612 | "qcom,hlos-ce-hw-instance", |
| 4613 | &qseecom.ce_info.hlos_ce_hw_instance)) { |
| 4614 | pr_err("Fail: get hlos ce hw instanc info\n"); |
| 4615 | rc = -EINVAL; |
| 4616 | goto exit_destroy_ion_client; |
| 4617 | } else { |
| 4618 | pr_warn("hlos-ce-hw-instance=0x%x", |
| 4619 | qseecom.ce_info.hlos_ce_hw_instance); |
| 4620 | } |
| 4621 | } else { |
| 4622 | pr_warn("Device does not support PFE/FDE"); |
| 4623 | qseecom.ce_info.hlos_ce_hw_instance = 0xff; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | if (of_property_read_u32((&pdev->dev)->of_node, |
| 4627 | "qcom,qsee-ce-hw-instance", |
| 4628 | &qseecom.ce_info.qsee_ce_hw_instance)) { |
| 4629 | pr_err("Fail to get qsee ce hw instance information.\n"); |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4630 | rc = -EINVAL; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4631 | goto exit_destroy_ion_client; |
Mona Hossain | 4cf78a9 | 2013-02-14 12:06:41 -0800 | [diff] [blame] | 4632 | } else { |
| 4633 | pr_warn("qsee-ce-hw-instance=0x%x", |
| 4634 | qseecom.ce_info.qsee_ce_hw_instance); |
| 4635 | } |
| 4636 | |
Zhen Kong | 56d6264 | 2015-03-10 16:29:53 -0700 | [diff] [blame] | 4637 | qseecom.appsbl_qseecom_support = |
| 4638 | of_property_read_bool((&pdev->dev)->of_node, |
| 4639 | "qcom,appsbl-qseecom-support"); |
| 4640 | pr_info("qseecom.appsbl_qseecom_support = 0x%x", |
| 4641 | qseecom.appsbl_qseecom_support); |
| 4642 | |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4643 | qseecom.qsee.instance = qseecom.ce_info.qsee_ce_hw_instance; |
| 4644 | qseecom.ce_drv.instance = qseecom.ce_info.hlos_ce_hw_instance; |
| 4645 | |
| 4646 | ret = __qseecom_init_clk(CLK_QSEE); |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4647 | if (ret) |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4648 | goto exit_destroy_ion_client; |
Mona Hossain | 6311d57 | 2013-03-01 15:54:02 -0800 | [diff] [blame] | 4649 | |
Zhen Kong | a10dcab | 2014-03-19 10:32:27 -0700 | [diff] [blame] | 4650 | if ((qseecom.qsee.instance != qseecom.ce_drv.instance) && |
| 4651 | (qseecom.support_pfe || qseecom.support_fde)) { |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4652 | ret = __qseecom_init_clk(CLK_CE_DRV); |
| 4653 | if (ret) { |
| 4654 | __qseecom_deinit_clk(CLK_QSEE); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4655 | goto exit_destroy_ion_client; |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4656 | } |
| 4657 | } else { |
| 4658 | struct qseecom_clk *qclk; |
| 4659 | |
| 4660 | qclk = &qseecom.qsee; |
| 4661 | qseecom.ce_drv.ce_core_clk = qclk->ce_core_clk; |
| 4662 | qseecom.ce_drv.ce_clk = qclk->ce_clk; |
| 4663 | qseecom.ce_drv.ce_core_src_clk = qclk->ce_core_src_clk; |
| 4664 | qseecom.ce_drv.ce_bus_clk = qclk->ce_bus_clk; |
| 4665 | } |
| 4666 | |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4667 | qseecom_platform_support = (struct msm_bus_scale_pdata *) |
| 4668 | msm_bus_cl_get_pdata(pdev); |
Zhen Kong | 56d6264 | 2015-03-10 16:29:53 -0700 | [diff] [blame] | 4669 | if (qseecom.qsee_version >= (QSEE_VERSION_02) && |
| 4670 | !qseecom.appsbl_qseecom_support) { |
Mona Hossain | 5b76a62 | 2012-11-15 20:09:08 -0800 | [diff] [blame] | 4671 | struct resource *resource = NULL; |
| 4672 | struct qsee_apps_region_info_ireq req; |
| 4673 | struct qseecom_command_scm_resp resp; |
| 4674 | |
| 4675 | resource = platform_get_resource_byname(pdev, |
| 4676 | IORESOURCE_MEM, "secapp-region"); |
| 4677 | if (resource) { |
| 4678 | req.qsee_cmd_id = QSEOS_APP_REGION_NOTIFICATION; |
| 4679 | req.addr = resource->start; |
| 4680 | req.size = resource_size(resource); |
| 4681 | pr_warn("secure app region addr=0x%x size=0x%x", |
| 4682 | req.addr, req.size); |
| 4683 | } else { |
| 4684 | pr_err("Fail to get secure app region info\n"); |
| 4685 | rc = -EINVAL; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4686 | goto exit_destroy_ion_client; |
Mona Hossain | 5b76a62 | 2012-11-15 20:09:08 -0800 | [diff] [blame] | 4687 | } |
| 4688 | rc = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, sizeof(req), |
| 4689 | &resp, sizeof(resp)); |
Mona Hossain | 32deb98 | 2013-08-06 16:25:44 -0700 | [diff] [blame] | 4690 | if (rc || (resp.result != QSEOS_RESULT_SUCCESS)) { |
| 4691 | pr_err("send secapp reg fail %d resp.res %d\n", |
| 4692 | rc, resp.result); |
| 4693 | rc = -EINVAL; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4694 | goto exit_destroy_ion_client; |
Mona Hossain | 5b76a62 | 2012-11-15 20:09:08 -0800 | [diff] [blame] | 4695 | } |
| 4696 | } |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4697 | } else { |
Ramesh Masavarapu | fb1f01e | 2012-06-14 09:40:40 -0700 | [diff] [blame] | 4698 | qseecom_platform_support = (struct msm_bus_scale_pdata *) |
| 4699 | pdev->dev.platform_data; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4700 | } |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4701 | if (qseecom.support_bus_scaling) { |
| 4702 | init_timer(&(qseecom.bw_scale_down_timer)); |
| 4703 | INIT_WORK(&qseecom.bw_inactive_req_ws, |
| 4704 | qseecom_bw_inactive_req_work); |
| 4705 | qseecom.bw_scale_down_timer.function = |
| 4706 | qseecom_scale_bus_bandwidth_timer_callback; |
| 4707 | } |
Zhen Kong | ea5d4bb | 2014-02-18 14:59:53 -0800 | [diff] [blame] | 4708 | qseecom.timer_running = false; |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4709 | qseecom.qsee_perf_client = msm_bus_scale_register_client( |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4710 | qseecom_platform_support); |
| 4711 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4712 | if (!qseecom.qsee_perf_client) |
Ramesh Masavarapu | ff37703 | 2012-09-14 12:11:32 -0700 | [diff] [blame] | 4713 | pr_err("Unable to register bus client\n"); |
| 4714 | return 0; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4715 | |
| 4716 | exit_destroy_ion_client: |
| 4717 | ion_client_destroy(qseecom.ion_clnt); |
| 4718 | exit_del_cdev: |
| 4719 | cdev_del(&qseecom.cdev); |
| 4720 | exit_destroy_device: |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4721 | device_destroy(driver_class, qseecom_device_no); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4722 | exit_destroy_class: |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4723 | class_destroy(driver_class); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4724 | exit_unreg_chrdev_region: |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4725 | unregister_chrdev_region(qseecom_device_no, 1); |
| 4726 | return rc; |
| 4727 | } |
| 4728 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 4729 | static int __devinit qseecom_remove(struct platform_device *pdev) |
| 4730 | { |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4731 | struct qseecom_registered_kclient_list *kclient = NULL; |
| 4732 | unsigned long flags = 0; |
| 4733 | int ret = 0; |
| 4734 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4735 | spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4736 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4737 | list_for_each_entry(kclient, &qseecom.registered_kclient_list_head, |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4738 | list) { |
| 4739 | if (!kclient) |
| 4740 | goto exit_irqrestore; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4741 | |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4742 | /* Break the loop if client handle is NULL */ |
| 4743 | if (!kclient->handle) |
| 4744 | goto exit_free_kclient; |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4745 | |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4746 | if (list_empty(&kclient->list)) |
| 4747 | goto exit_free_kc_handle; |
| 4748 | |
| 4749 | list_del(&kclient->list); |
Hariprasad Dhalinarasimha | bd0c545 | 2013-06-21 18:40:50 -0700 | [diff] [blame] | 4750 | ret = qseecom_unload_app(kclient->handle->dev, false); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4751 | if (!ret) { |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4752 | kzfree(kclient->handle->dev); |
| 4753 | kzfree(kclient->handle); |
| 4754 | kzfree(kclient); |
| 4755 | } |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4756 | } |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4757 | |
| 4758 | exit_free_kc_handle: |
| 4759 | kzfree(kclient->handle); |
| 4760 | exit_free_kclient: |
| 4761 | kzfree(kclient); |
| 4762 | exit_irqrestore: |
| 4763 | spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags); |
| 4764 | |
| 4765 | if (qseecom.qseos_version > QSEEE_VERSION_00) |
Mona Hossain | 05c7356 | 2012-10-29 17:49:01 -0700 | [diff] [blame] | 4766 | qseecom_unload_commonlib_image(); |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4767 | |
Mona Hossain | 17a4faf | 2013-03-22 16:40:56 -0700 | [diff] [blame] | 4768 | if (qseecom.qsee_perf_client) |
| 4769 | msm_bus_scale_client_update_request(qseecom.qsee_perf_client, |
| 4770 | 0); |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4771 | if (pdev->dev.platform_data != NULL) |
| 4772 | msm_bus_scale_unregister_client(qseecom.qsee_perf_client); |
| 4773 | |
Zhen Kong | 2edf90d | 2013-08-27 12:05:06 -0700 | [diff] [blame] | 4774 | if (qseecom.support_bus_scaling) { |
| 4775 | cancel_work_sync(&qseecom.bw_inactive_req_ws); |
| 4776 | del_timer_sync(&qseecom.bw_scale_down_timer); |
| 4777 | } |
| 4778 | |
Mona Hossain | d39e33b | 2012-11-05 13:36:40 -0800 | [diff] [blame] | 4779 | /* register client for bus scaling */ |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4780 | if (pdev->dev.of_node) { |
| 4781 | __qseecom_deinit_clk(CLK_QSEE); |
Zhen Kong | a10dcab | 2014-03-19 10:32:27 -0700 | [diff] [blame] | 4782 | if ((qseecom.qsee.instance != qseecom.ce_drv.instance) && |
| 4783 | (qseecom.support_pfe || qseecom.support_fde)) |
Mona Hossain | c92629e | 2013-04-01 13:37:46 -0700 | [diff] [blame] | 4784 | __qseecom_deinit_clk(CLK_CE_DRV); |
| 4785 | } |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4786 | |
| 4787 | ion_client_destroy(qseecom.ion_clnt); |
| 4788 | |
| 4789 | cdev_del(&qseecom.cdev); |
| 4790 | |
| 4791 | device_destroy(driver_class, qseecom_device_no); |
| 4792 | |
| 4793 | class_destroy(driver_class); |
| 4794 | |
| 4795 | unregister_chrdev_region(qseecom_device_no, 1); |
| 4796 | |
Mona Hossain | d44a384 | 2012-10-15 09:41:35 -0700 | [diff] [blame] | 4797 | return ret; |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4798 | } |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 4799 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4800 | static int qseecom_suspend(struct platform_device *pdev, pm_message_t state) |
| 4801 | { |
| 4802 | int ret = 0; |
| 4803 | struct qseecom_clk *qclk; |
| 4804 | qclk = &qseecom.qsee; |
| 4805 | |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4806 | mutex_lock(&qsee_bw_mutex); |
| 4807 | mutex_lock(&clk_access_lock); |
| 4808 | |
Zhen Kong | 60f4f70 | 2014-10-15 17:57:17 -0700 | [diff] [blame] | 4809 | if (qseecom.current_mode != INACTIVE) { |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 4810 | ret = msm_bus_scale_client_update_request( |
| 4811 | qseecom.qsee_perf_client, INACTIVE); |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4812 | if (ret) |
| 4813 | pr_err("Fail to scale down bus\n"); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4814 | else |
| 4815 | qseecom.current_mode = INACTIVE; |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4816 | } |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4817 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4818 | if (qclk->clk_access_cnt) { |
| 4819 | if (qclk->ce_clk != NULL) |
| 4820 | clk_disable_unprepare(qclk->ce_clk); |
| 4821 | if (qclk->ce_core_clk != NULL) |
| 4822 | clk_disable_unprepare(qclk->ce_core_clk); |
| 4823 | if (qclk->ce_bus_clk != NULL) |
| 4824 | clk_disable_unprepare(qclk->ce_bus_clk); |
| 4825 | } |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4826 | |
| 4827 | del_timer_sync(&(qseecom.bw_scale_down_timer)); |
| 4828 | qseecom.timer_running = false; |
| 4829 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4830 | mutex_unlock(&clk_access_lock); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4831 | mutex_unlock(&qsee_bw_mutex); |
| 4832 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4833 | return 0; |
| 4834 | } |
| 4835 | |
| 4836 | static int qseecom_resume(struct platform_device *pdev) |
| 4837 | { |
| 4838 | int mode = 0; |
| 4839 | int ret = 0; |
| 4840 | struct qseecom_clk *qclk; |
| 4841 | qclk = &qseecom.qsee; |
| 4842 | |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4843 | mutex_lock(&qsee_bw_mutex); |
| 4844 | mutex_lock(&clk_access_lock); |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4845 | if (qseecom.cumulative_mode >= HIGH) |
| 4846 | mode = HIGH; |
| 4847 | else |
| 4848 | mode = qseecom.cumulative_mode; |
| 4849 | |
| 4850 | if (qseecom.cumulative_mode != INACTIVE) { |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 4851 | ret = msm_bus_scale_client_update_request( |
Zhen Kong | 98b0e51 | 2014-04-04 10:12:24 -0700 | [diff] [blame] | 4852 | qseecom.qsee_perf_client, mode); |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4853 | if (ret) |
Zhen Kong | 98b0e51 | 2014-04-04 10:12:24 -0700 | [diff] [blame] | 4854 | pr_err("Fail to scale up bus to %d\n", mode); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4855 | else |
| 4856 | qseecom.current_mode = mode; |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4857 | } |
| 4858 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4859 | if (qclk->clk_access_cnt) { |
| 4860 | |
| 4861 | ret = clk_prepare_enable(qclk->ce_core_clk); |
| 4862 | if (ret) { |
| 4863 | pr_err("Unable to enable/prepare CE core clk\n"); |
| 4864 | qclk->clk_access_cnt = 0; |
| 4865 | goto err; |
| 4866 | } |
| 4867 | |
| 4868 | ret = clk_prepare_enable(qclk->ce_clk); |
| 4869 | if (ret) { |
| 4870 | pr_err("Unable to enable/prepare CE iface clk\n"); |
| 4871 | qclk->clk_access_cnt = 0; |
| 4872 | goto ce_clk_err; |
| 4873 | } |
| 4874 | |
| 4875 | ret = clk_prepare_enable(qclk->ce_bus_clk); |
| 4876 | if (ret) { |
| 4877 | pr_err("Unable to enable/prepare CE bus clk\n"); |
| 4878 | qclk->clk_access_cnt = 0; |
| 4879 | goto ce_bus_clk_err; |
| 4880 | } |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | if (qclk->clk_access_cnt || qseecom.cumulative_mode) { |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 4884 | qseecom.bw_scale_down_timer.expires = jiffies + |
| 4885 | msecs_to_jiffies(QSEECOM_SEND_CMD_CRYPTO_TIMEOUT); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4886 | mod_timer(&(qseecom.bw_scale_down_timer), |
| 4887 | qseecom.bw_scale_down_timer.expires); |
Zhen Kong | ca4c2d5 | 2014-03-12 13:22:46 -0700 | [diff] [blame] | 4888 | qseecom.timer_running = true; |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4889 | } |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4890 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4891 | mutex_unlock(&clk_access_lock); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4892 | mutex_unlock(&qsee_bw_mutex); |
| 4893 | |
| 4894 | |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4895 | return 0; |
| 4896 | |
| 4897 | ce_bus_clk_err: |
| 4898 | clk_disable_unprepare(qclk->ce_clk); |
| 4899 | ce_clk_err: |
| 4900 | clk_disable_unprepare(qclk->ce_core_clk); |
| 4901 | err: |
| 4902 | mutex_unlock(&clk_access_lock); |
Zhen Kong | e5b434e | 2014-04-17 16:47:06 -0700 | [diff] [blame] | 4903 | mutex_unlock(&qsee_bw_mutex); |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4904 | return -EIO; |
| 4905 | } |
Ramesh Masavarapu | fb1f01e | 2012-06-14 09:40:40 -0700 | [diff] [blame] | 4906 | static struct of_device_id qseecom_match[] = { |
| 4907 | { |
| 4908 | .compatible = "qcom,qseecom", |
| 4909 | }, |
| 4910 | {} |
| 4911 | }; |
| 4912 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 4913 | static struct platform_driver qseecom_plat_driver = { |
| 4914 | .probe = qseecom_probe, |
| 4915 | .remove = qseecom_remove, |
Zhen Kong | a0944b8 | 2013-11-06 17:02:00 -0800 | [diff] [blame] | 4916 | .suspend = qseecom_suspend, |
| 4917 | .resume = qseecom_resume, |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 4918 | .driver = { |
| 4919 | .name = "qseecom", |
| 4920 | .owner = THIS_MODULE, |
Ramesh Masavarapu | fb1f01e | 2012-06-14 09:40:40 -0700 | [diff] [blame] | 4921 | .of_match_table = qseecom_match, |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 4922 | }, |
| 4923 | }; |
| 4924 | |
| 4925 | static int __devinit qseecom_init(void) |
| 4926 | { |
| 4927 | return platform_driver_register(&qseecom_plat_driver); |
| 4928 | } |
| 4929 | |
| 4930 | static void __devexit qseecom_exit(void) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4931 | { |
AnilKumar Chimata | a253a03 | 2013-10-04 18:53:42 +0530 | [diff] [blame] | 4932 | platform_driver_unregister(&qseecom_plat_driver); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | MODULE_LICENSE("GPL v2"); |
| 4936 | MODULE_DESCRIPTION("Qualcomm Secure Execution Environment Communicator"); |
| 4937 | |
| 4938 | module_init(qseecom_init); |
| 4939 | module_exit(qseecom_exit); |