Mona Hossain | a5f1aab | 2012-03-29 10:18:07 -0700 | [diff] [blame] | 1 | |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 2 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 3 | /* Qualcomm Secure Execution Environment Communicator (QSEECOM) driver |
| 4 | * |
| 5 | * Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 and |
| 9 | * only version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #define pr_fmt(fmt) "QSEECOM: %s: " fmt, __func__ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/cdev.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/io.h> |
Mitchel Humpherys | 4f8be2e | 2012-09-06 10:41:41 -0700 | [diff] [blame^] | 31 | #include <linux/msm_ion.h> |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 32 | #include <linux/types.h> |
| 33 | #include <linux/clk.h> |
| 34 | #include <linux/qseecom.h> |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 35 | #include <linux/freezer.h> |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 36 | #include <mach/board.h> |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 37 | #include <mach/msm_bus.h> |
| 38 | #include <mach/msm_bus_board.h> |
| 39 | #include <mach/scm.h> |
| 40 | #include <mach/peripheral-loader.h> |
| 41 | #include "qseecom_legacy.h" |
| 42 | |
| 43 | #define QSEECOM_DEV "qseecom" |
| 44 | #define QSEOS_VERSION_13 0x13 |
| 45 | #define QSEOS_VERSION_14 0x14 |
| 46 | #define QSEOS_CHECK_VERSION_CMD 0x00001803; |
| 47 | |
| 48 | enum qseecom_command_scm_resp_type { |
| 49 | QSEOS_APP_ID = 0xEE01, |
| 50 | QSEOS_LISTENER_ID |
| 51 | }; |
| 52 | |
| 53 | enum qseecom_qceos_cmd_id { |
| 54 | QSEOS_APP_START_COMMAND = 0x01, |
| 55 | QSEOS_APP_SHUTDOWN_COMMAND, |
| 56 | QSEOS_APP_LOOKUP_COMMAND, |
| 57 | QSEOS_REGISTER_LISTENER, |
| 58 | QSEOS_DEREGISTER_LISTENER, |
| 59 | QSEOS_CLIENT_SEND_DATA_COMMAND, |
| 60 | QSEOS_LISTENER_DATA_RSP_COMMAND, |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 61 | QSEOS_LOAD_EXTERNAL_ELF_COMMAND, |
| 62 | QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 63 | QSEOS_CMD_MAX = 0xEFFFFFFF |
| 64 | }; |
| 65 | |
| 66 | enum qseecom_qceos_cmd_status { |
| 67 | QSEOS_RESULT_SUCCESS = 0, |
| 68 | QSEOS_RESULT_INCOMPLETE, |
| 69 | QSEOS_RESULT_FAILURE = 0xFFFFFFFF |
| 70 | }; |
| 71 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 72 | enum qseecom_clk_definitions { |
| 73 | CLK_DFAB = 0, |
| 74 | CLK_SFPB, |
| 75 | }; |
| 76 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 77 | __packed struct qseecom_check_app_ireq { |
| 78 | uint32_t qsee_cmd_id; |
| 79 | char app_name[MAX_APP_NAME_SIZE]; |
| 80 | }; |
| 81 | |
| 82 | __packed struct qseecom_load_app_ireq { |
| 83 | uint32_t qsee_cmd_id; |
| 84 | uint32_t mdt_len; /* Length of the mdt file */ |
| 85 | uint32_t img_len; /* Length of .bxx and .mdt files */ |
| 86 | uint32_t phy_addr; /* phy addr of the start of image */ |
| 87 | char app_name[MAX_APP_NAME_SIZE]; /* application name*/ |
| 88 | }; |
| 89 | |
| 90 | __packed struct qseecom_unload_app_ireq { |
| 91 | uint32_t qsee_cmd_id; |
| 92 | uint32_t app_id; |
| 93 | }; |
| 94 | |
| 95 | __packed struct qseecom_register_listener_ireq { |
| 96 | uint32_t qsee_cmd_id; |
| 97 | uint32_t listener_id; |
| 98 | void *sb_ptr; |
| 99 | uint32_t sb_len; |
| 100 | }; |
| 101 | |
| 102 | __packed struct qseecom_unregister_listener_ireq { |
| 103 | uint32_t qsee_cmd_id; |
| 104 | uint32_t listener_id; |
| 105 | }; |
| 106 | |
| 107 | __packed struct qseecom_client_send_data_ireq { |
| 108 | uint32_t qsee_cmd_id; |
| 109 | uint32_t app_id; |
| 110 | void *req_ptr; |
| 111 | uint32_t req_len; |
| 112 | void *rsp_ptr; /* First 4 bytes should always be the return status */ |
| 113 | uint32_t rsp_len; |
| 114 | }; |
| 115 | |
| 116 | /* send_data resp */ |
| 117 | __packed struct qseecom_client_listener_data_irsp { |
| 118 | uint32_t qsee_cmd_id; |
| 119 | uint32_t listener_id; |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * struct qseecom_command_scm_resp - qseecom response buffer |
| 124 | * @cmd_status: value from enum tz_sched_cmd_status |
| 125 | * @sb_in_rsp_addr: points to physical location of response |
| 126 | * buffer |
| 127 | * @sb_in_rsp_len: length of command response |
| 128 | */ |
| 129 | __packed struct qseecom_command_scm_resp { |
| 130 | uint32_t result; |
| 131 | enum qseecom_command_scm_resp_type resp_type; |
| 132 | unsigned int data; |
| 133 | }; |
| 134 | |
| 135 | static struct class *driver_class; |
| 136 | static dev_t qseecom_device_no; |
| 137 | static struct cdev qseecom_cdev; |
| 138 | |
| 139 | /* Data structures used in legacy support */ |
| 140 | static void *pil; |
| 141 | static uint32_t pil_ref_cnt; |
| 142 | static DEFINE_MUTEX(pil_access_lock); |
| 143 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 144 | static DEFINE_MUTEX(qsee_bw_mutex); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 145 | static DEFINE_MUTEX(qsee_sfpb_bw_mutex); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 146 | static DEFINE_MUTEX(app_access_lock); |
| 147 | |
| 148 | static int qsee_bw_count; |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 149 | static int qsee_sfpb_bw_count; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 150 | static struct clk *qseecom_bus_clk; |
| 151 | static uint32_t qsee_perf_client; |
| 152 | |
| 153 | struct qseecom_registered_listener_list { |
| 154 | struct list_head list; |
| 155 | struct qseecom_register_listener_req svc; |
| 156 | u8 *sb_reg_req; |
| 157 | u8 *sb_virt; |
| 158 | s32 sb_phys; |
| 159 | size_t sb_length; |
| 160 | struct ion_handle *ihandle; /* Retrieve phy addr */ |
| 161 | |
| 162 | wait_queue_head_t rcv_req_wq; |
| 163 | int rcv_req_flag; |
| 164 | }; |
| 165 | |
| 166 | struct qseecom_registered_app_list { |
| 167 | struct list_head list; |
| 168 | u32 app_id; |
| 169 | u32 ref_cnt; |
| 170 | }; |
| 171 | |
| 172 | struct qseecom_control { |
| 173 | struct ion_client *ion_clnt; /* Ion client */ |
| 174 | struct list_head registered_listener_list_head; |
| 175 | spinlock_t registered_listener_list_lock; |
| 176 | |
| 177 | struct list_head registered_app_list_head; |
| 178 | spinlock_t registered_app_list_lock; |
| 179 | |
| 180 | wait_queue_head_t send_resp_wq; |
| 181 | int send_resp_flag; |
| 182 | |
| 183 | uint32_t qseos_version; |
| 184 | }; |
| 185 | |
| 186 | struct qseecom_client_handle { |
| 187 | u32 app_id; |
| 188 | u8 *sb_virt; |
| 189 | s32 sb_phys; |
| 190 | uint32_t user_virt_sb_base; |
| 191 | size_t sb_length; |
| 192 | struct ion_handle *ihandle; /* Retrieve phy addr */ |
| 193 | }; |
| 194 | |
| 195 | struct qseecom_listener_handle { |
| 196 | u32 id; |
| 197 | }; |
| 198 | |
| 199 | static struct qseecom_control qseecom; |
| 200 | |
| 201 | struct qseecom_dev_handle { |
| 202 | bool service; |
| 203 | union { |
| 204 | struct qseecom_client_handle client; |
| 205 | struct qseecom_listener_handle listener; |
| 206 | }; |
| 207 | bool released; |
| 208 | int abort; |
| 209 | wait_queue_head_t abort_wq; |
| 210 | atomic_t ioctl_count; |
| 211 | }; |
| 212 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 213 | /* Function proto types */ |
| 214 | static int qsee_vote_for_clock(int32_t); |
| 215 | static void qsee_disable_clock_vote(int32_t); |
| 216 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 217 | static int __qseecom_is_svc_unique(struct qseecom_dev_handle *data, |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 218 | struct qseecom_register_listener_req *svc) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 219 | { |
| 220 | struct qseecom_registered_listener_list *ptr; |
| 221 | int unique = 1; |
| 222 | unsigned long flags; |
| 223 | |
| 224 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 225 | list_for_each_entry(ptr, &qseecom.registered_listener_list_head, list) { |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 226 | if (ptr->svc.listener_id == svc->listener_id) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 227 | pr_err("Service id: %u is already registered\n", |
| 228 | ptr->svc.listener_id); |
| 229 | unique = 0; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 234 | return unique; |
| 235 | } |
| 236 | |
| 237 | static struct qseecom_registered_listener_list *__qseecom_find_svc( |
| 238 | int32_t listener_id) |
| 239 | { |
| 240 | struct qseecom_registered_listener_list *entry = NULL; |
| 241 | unsigned long flags; |
| 242 | |
| 243 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 244 | list_for_each_entry(entry, &qseecom.registered_listener_list_head, list) |
| 245 | { |
| 246 | if (entry->svc.listener_id == listener_id) |
| 247 | break; |
| 248 | } |
| 249 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 250 | return entry; |
| 251 | } |
| 252 | |
| 253 | static int __qseecom_set_sb_memory(struct qseecom_registered_listener_list *svc, |
| 254 | struct qseecom_dev_handle *handle, |
| 255 | struct qseecom_register_listener_req *listener) |
| 256 | { |
| 257 | int ret = 0; |
| 258 | unsigned int flags = 0; |
| 259 | struct qseecom_register_listener_ireq req; |
| 260 | struct qseecom_command_scm_resp resp; |
| 261 | ion_phys_addr_t pa; |
| 262 | |
| 263 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 264 | svc->ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
| 265 | listener->ifd_data_fd); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 266 | if (svc->ihandle == NULL) { |
| 267 | pr_err("Ion client could not retrieve the handle\n"); |
| 268 | return -ENOMEM; |
| 269 | } |
| 270 | |
| 271 | /* Get the physical address of the ION BUF */ |
| 272 | ret = ion_phys(qseecom.ion_clnt, svc->ihandle, &pa, &svc->sb_length); |
| 273 | |
| 274 | /* Populate the structure for sending scm call to load image */ |
| 275 | svc->sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, |
| 276 | svc->ihandle, flags); |
| 277 | svc->sb_phys = pa; |
| 278 | |
| 279 | if (qseecom.qseos_version == QSEOS_VERSION_14) { |
| 280 | req.qsee_cmd_id = QSEOS_REGISTER_LISTENER; |
| 281 | req.listener_id = svc->svc.listener_id; |
| 282 | req.sb_len = svc->sb_length; |
| 283 | req.sb_ptr = (void *)svc->sb_phys; |
| 284 | |
| 285 | resp.result = QSEOS_RESULT_INCOMPLETE; |
| 286 | |
| 287 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 288 | sizeof(req), &resp, sizeof(resp)); |
| 289 | if (ret) { |
| 290 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 295 | pr_err("Error SB registration req: resp.result = %d\n", |
| 296 | resp.result); |
| 297 | return -EPERM; |
| 298 | } |
| 299 | } else { |
| 300 | struct qseecom_command cmd; |
| 301 | struct qseecom_response resp; |
| 302 | struct qse_pr_init_sb_req_s sb_init_req; |
| 303 | struct qse_pr_init_sb_rsp_s sb_init_rsp; |
| 304 | |
| 305 | svc->sb_reg_req = kzalloc((sizeof(sb_init_req) + |
| 306 | sizeof(sb_init_rsp)), GFP_KERNEL); |
| 307 | |
| 308 | sb_init_req.pr_cmd = TZ_SCHED_CMD_ID_REGISTER_LISTENER; |
| 309 | sb_init_req.listener_id = svc->svc.listener_id; |
| 310 | sb_init_req.sb_len = svc->sb_length; |
| 311 | sb_init_req.sb_ptr = svc->sb_phys; |
| 312 | |
| 313 | memcpy(svc->sb_reg_req, &sb_init_req, sizeof(sb_init_req)); |
| 314 | |
| 315 | /* It will always be a new cmd from this method */ |
| 316 | cmd.cmd_type = TZ_SCHED_CMD_NEW; |
| 317 | cmd.sb_in_cmd_addr = (u8 *)(virt_to_phys(svc->sb_reg_req)); |
| 318 | cmd.sb_in_cmd_len = sizeof(sb_init_req); |
| 319 | |
| 320 | resp.cmd_status = TZ_SCHED_STATUS_INCOMPLETE; |
| 321 | |
| 322 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &cmd, sizeof(cmd) |
| 323 | , &resp, sizeof(resp)); |
| 324 | |
| 325 | if (ret) { |
| 326 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | if (resp.cmd_status != TZ_SCHED_STATUS_COMPLETE) { |
| 331 | pr_err("SB registration fail resp.cmd_status %d\n", |
| 332 | resp.cmd_status); |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | memset(svc->sb_virt, 0, svc->sb_length); |
| 336 | } |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int qseecom_register_listener(struct qseecom_dev_handle *data, |
| 341 | void __user *argp) |
| 342 | { |
| 343 | int ret = 0; |
| 344 | unsigned long flags; |
| 345 | struct qseecom_register_listener_req rcvd_lstnr; |
| 346 | struct qseecom_registered_listener_list *new_entry; |
| 347 | |
| 348 | ret = copy_from_user(&rcvd_lstnr, argp, sizeof(rcvd_lstnr)); |
| 349 | if (ret) { |
| 350 | pr_err("copy_from_user failed\n"); |
| 351 | return ret; |
| 352 | } |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 353 | data->listener.id = 0; |
| 354 | data->service = true; |
| 355 | if (!__qseecom_is_svc_unique(data, &rcvd_lstnr)) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 356 | pr_err("Service is not unique and is already registered\n"); |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 357 | data->released = true; |
| 358 | return -EBUSY; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL); |
| 362 | if (!new_entry) { |
| 363 | pr_err("kmalloc failed\n"); |
| 364 | return -ENOMEM; |
| 365 | } |
| 366 | memcpy(&new_entry->svc, &rcvd_lstnr, sizeof(rcvd_lstnr)); |
| 367 | new_entry->rcv_req_flag = 0; |
| 368 | |
| 369 | new_entry->svc.listener_id = rcvd_lstnr.listener_id; |
| 370 | new_entry->sb_length = rcvd_lstnr.sb_size; |
| 371 | if (__qseecom_set_sb_memory(new_entry, data, &rcvd_lstnr)) { |
| 372 | pr_err("qseecom_set_sb_memoryfailed\n"); |
| 373 | kzfree(new_entry); |
| 374 | return -ENOMEM; |
| 375 | } |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 376 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 377 | data->listener.id = rcvd_lstnr.listener_id; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 378 | init_waitqueue_head(&new_entry->rcv_req_wq); |
| 379 | |
| 380 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 381 | list_add_tail(&new_entry->list, &qseecom.registered_listener_list_head); |
| 382 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
Mona Hossain | 0af10ab | 2012-02-28 18:26:41 -0800 | [diff] [blame] | 383 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static int qseecom_unregister_listener(struct qseecom_dev_handle *data) |
| 388 | { |
| 389 | int ret = 0; |
| 390 | unsigned long flags; |
| 391 | uint32_t unmap_mem = 0; |
| 392 | struct qseecom_register_listener_ireq req; |
| 393 | struct qseecom_registered_listener_list *ptr_svc = NULL; |
| 394 | struct qseecom_command_scm_resp resp; |
| 395 | struct ion_handle *ihandle = NULL; /* Retrieve phy addr */ |
| 396 | |
| 397 | if (qseecom.qseos_version == QSEOS_VERSION_14) { |
| 398 | req.qsee_cmd_id = QSEOS_DEREGISTER_LISTENER; |
| 399 | req.listener_id = data->listener.id; |
| 400 | resp.result = QSEOS_RESULT_INCOMPLETE; |
| 401 | |
| 402 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 403 | sizeof(req), &resp, sizeof(resp)); |
| 404 | if (ret) { |
| 405 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 410 | pr_err("SB deregistartion: result=%d\n", resp.result); |
| 411 | return -EPERM; |
| 412 | } |
| 413 | } else { |
| 414 | struct qse_pr_init_sb_req_s sb_init_req; |
| 415 | struct qseecom_command cmd; |
| 416 | struct qseecom_response resp; |
| 417 | struct qseecom_registered_listener_list *svc; |
| 418 | |
| 419 | svc = __qseecom_find_svc(data->listener.id); |
| 420 | sb_init_req.pr_cmd = TZ_SCHED_CMD_ID_REGISTER_LISTENER; |
| 421 | sb_init_req.listener_id = data->listener.id; |
| 422 | sb_init_req.sb_len = 0; |
| 423 | sb_init_req.sb_ptr = 0; |
| 424 | |
| 425 | memcpy(svc->sb_reg_req, &sb_init_req, sizeof(sb_init_req)); |
| 426 | |
| 427 | /* It will always be a new cmd from this method */ |
| 428 | cmd.cmd_type = TZ_SCHED_CMD_NEW; |
| 429 | cmd.sb_in_cmd_addr = (u8 *)(virt_to_phys(svc->sb_reg_req)); |
| 430 | cmd.sb_in_cmd_len = sizeof(sb_init_req); |
| 431 | resp.cmd_status = TZ_SCHED_STATUS_INCOMPLETE; |
| 432 | |
| 433 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &cmd, sizeof(cmd), |
| 434 | &resp, sizeof(resp)); |
| 435 | if (ret) { |
| 436 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 437 | return ret; |
| 438 | } |
| 439 | kzfree(svc->sb_reg_req); |
| 440 | if (resp.cmd_status != TZ_SCHED_STATUS_COMPLETE) { |
| 441 | pr_err("Error with SB initialization\n"); |
| 442 | return -EPERM; |
| 443 | } |
| 444 | } |
| 445 | data->abort = 1; |
| 446 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 447 | list_for_each_entry(ptr_svc, &qseecom.registered_listener_list_head, |
| 448 | list) { |
| 449 | if (ptr_svc->svc.listener_id == data->listener.id) { |
| 450 | wake_up_all(&ptr_svc->rcv_req_wq); |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 455 | |
| 456 | while (atomic_read(&data->ioctl_count) > 1) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 457 | if (wait_event_freezable(data->abort_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 458 | atomic_read(&data->ioctl_count) <= 1)) { |
| 459 | pr_err("Interrupted from abort\n"); |
| 460 | ret = -ERESTARTSYS; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags); |
| 466 | list_for_each_entry(ptr_svc, |
| 467 | &qseecom.registered_listener_list_head, |
| 468 | list) |
| 469 | { |
| 470 | if (ptr_svc->svc.listener_id == data->listener.id) { |
| 471 | if (ptr_svc->sb_virt) { |
| 472 | unmap_mem = 1; |
| 473 | ihandle = ptr_svc->ihandle; |
| 474 | } |
| 475 | list_del(&ptr_svc->list); |
| 476 | kzfree(ptr_svc); |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags); |
| 481 | |
| 482 | /* Unmap the memory */ |
| 483 | if (unmap_mem) { |
| 484 | if (!IS_ERR_OR_NULL(ihandle)) { |
| 485 | ion_unmap_kernel(qseecom.ion_clnt, ihandle); |
| 486 | ion_free(qseecom.ion_clnt, ihandle); |
| 487 | } |
| 488 | } |
| 489 | data->released = true; |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | static int qseecom_set_client_mem_param(struct qseecom_dev_handle *data, |
| 494 | void __user *argp) |
| 495 | { |
| 496 | ion_phys_addr_t pa; |
| 497 | int32_t ret; |
| 498 | unsigned int flags = 0; |
| 499 | struct qseecom_set_sb_mem_param_req req; |
| 500 | uint32_t len; |
| 501 | |
| 502 | /* Copy the relevant information needed for loading the image */ |
| 503 | if (__copy_from_user(&req, (void __user *)argp, sizeof(req))) |
| 504 | return -EFAULT; |
| 505 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 506 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 507 | data->client.ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
| 508 | req.ifd_data_fd); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 509 | if (IS_ERR_OR_NULL(data->client.ihandle)) { |
| 510 | pr_err("Ion client could not retrieve the handle\n"); |
| 511 | return -ENOMEM; |
| 512 | } |
| 513 | /* Get the physical address of the ION BUF */ |
| 514 | ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len); |
| 515 | /* Populate the structure for sending scm call to load image */ |
| 516 | data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, |
| 517 | data->client.ihandle, |
| 518 | flags); |
| 519 | data->client.sb_phys = pa; |
| 520 | data->client.sb_length = req.sb_len; |
| 521 | data->client.user_virt_sb_base = req.virt_sb_base; |
| 522 | return 0; |
| 523 | } |
| 524 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 525 | static int __qseecom_listener_has_sent_rsp(struct qseecom_dev_handle *data) |
| 526 | { |
| 527 | int ret; |
| 528 | ret = (qseecom.send_resp_flag != 0); |
| 529 | return ret || data->abort; |
| 530 | } |
| 531 | |
| 532 | static int __qseecom_process_incomplete_cmd(struct qseecom_dev_handle *data, |
| 533 | struct qseecom_command_scm_resp *resp) |
| 534 | { |
| 535 | int ret = 0; |
| 536 | uint32_t lstnr; |
| 537 | unsigned long flags; |
| 538 | struct qseecom_client_listener_data_irsp send_data_rsp; |
| 539 | struct qseecom_registered_listener_list *ptr_svc = NULL; |
| 540 | |
| 541 | |
| 542 | while (resp->result == QSEOS_RESULT_INCOMPLETE) { |
| 543 | lstnr = resp->data; |
| 544 | /* |
| 545 | * Wake up blocking lsitener service with the lstnr id |
| 546 | */ |
| 547 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, |
| 548 | flags); |
| 549 | list_for_each_entry(ptr_svc, |
| 550 | &qseecom.registered_listener_list_head, list) { |
| 551 | if (ptr_svc->svc.listener_id == lstnr) { |
| 552 | ptr_svc->rcv_req_flag = 1; |
| 553 | wake_up_interruptible(&ptr_svc->rcv_req_wq); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, |
| 558 | flags); |
| 559 | if (ptr_svc->svc.listener_id != lstnr) { |
| 560 | pr_warning("Service requested for does on exist\n"); |
| 561 | return -ERESTARTSYS; |
| 562 | } |
| 563 | pr_debug("waking up rcv_req_wq and " |
| 564 | "waiting for send_resp_wq\n"); |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 565 | if (wait_event_freezable(qseecom.send_resp_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 566 | __qseecom_listener_has_sent_rsp(data))) { |
| 567 | pr_warning("Interrupted: exiting send_cmd loop\n"); |
| 568 | return -ERESTARTSYS; |
| 569 | } |
| 570 | |
| 571 | if (data->abort) { |
Mona Hossain | a5f1aab | 2012-03-29 10:18:07 -0700 | [diff] [blame] | 572 | pr_err("Aborting listener service %d\n", |
| 573 | data->listener.id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 574 | return -ENODEV; |
| 575 | } |
| 576 | qseecom.send_resp_flag = 0; |
| 577 | send_data_rsp.qsee_cmd_id = QSEOS_LISTENER_DATA_RSP_COMMAND; |
| 578 | send_data_rsp.listener_id = lstnr ; |
| 579 | |
| 580 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, |
| 581 | (const void *)&send_data_rsp, |
| 582 | sizeof(send_data_rsp), resp, |
| 583 | sizeof(*resp)); |
| 584 | if (ret) { |
| 585 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 586 | return ret; |
| 587 | } |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 588 | if (resp->result == QSEOS_RESULT_FAILURE) { |
| 589 | pr_err("Response result %d not supported\n", |
| 590 | resp->result); |
| 591 | return -EINVAL; |
| 592 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 593 | } |
| 594 | return ret; |
| 595 | } |
| 596 | |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 597 | static int __qseecom_check_app_exists(struct qseecom_check_app_ireq req) |
| 598 | { |
| 599 | int32_t ret; |
| 600 | struct qseecom_command_scm_resp resp; |
| 601 | |
| 602 | /* SCM_CALL to check if app_id for the mentioned app exists */ |
| 603 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 604 | sizeof(struct qseecom_check_app_ireq), |
| 605 | &resp, sizeof(resp)); |
| 606 | if (ret) { |
| 607 | pr_err("scm_call to check if app is already loaded failed\n"); |
| 608 | return -EINVAL; |
| 609 | } |
| 610 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 611 | if (resp.result == QSEOS_RESULT_FAILURE) { |
| 612 | return 0; |
| 613 | } else { |
| 614 | switch (resp.resp_type) { |
| 615 | /*qsee returned listener type response */ |
| 616 | case QSEOS_LISTENER_ID: |
| 617 | pr_err("resp type is of listener type instead of app"); |
| 618 | return -EINVAL; |
| 619 | break; |
| 620 | case QSEOS_APP_ID: |
| 621 | return resp.data; |
| 622 | default: |
| 623 | pr_err("invalid resp type (%d) from qsee", |
| 624 | resp.resp_type); |
| 625 | return -ENODEV; |
| 626 | break; |
| 627 | } |
| 628 | } |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 631 | static int qseecom_load_app(struct qseecom_dev_handle *data, void __user *argp) |
| 632 | { |
| 633 | struct qseecom_registered_app_list *entry = NULL; |
| 634 | unsigned long flags = 0; |
| 635 | u32 app_id = 0; |
| 636 | struct ion_handle *ihandle; /* Ion handle */ |
| 637 | struct qseecom_load_img_req load_img_req; |
| 638 | int32_t ret; |
| 639 | ion_phys_addr_t pa = 0; |
| 640 | uint32_t len; |
| 641 | struct qseecom_command_scm_resp resp; |
| 642 | struct qseecom_check_app_ireq req; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 643 | struct qseecom_load_app_ireq load_req; |
| 644 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 645 | /* Copy the relevant information needed for loading the image */ |
| 646 | if (__copy_from_user(&load_img_req, |
| 647 | (void __user *)argp, |
| 648 | sizeof(struct qseecom_load_img_req))) { |
| 649 | pr_err("copy_from_user failed\n"); |
| 650 | return -EFAULT; |
| 651 | } |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 652 | /* Vote for the SFPB clock */ |
| 653 | ret = qsee_vote_for_clock(CLK_SFPB); |
| 654 | if (ret) |
| 655 | pr_warning("Unable to vote for SFPB clock"); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 656 | |
| 657 | req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND; |
| 658 | memcpy(req.app_name, load_img_req.img_name, MAX_APP_NAME_SIZE); |
| 659 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 660 | pr_warn("App (%s) does not exist, loading apps for first time\n", |
Mona Hossain | a5f1aab | 2012-03-29 10:18:07 -0700 | [diff] [blame] | 661 | (char *)(req.app_name)); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 662 | /* Get the handle of the shared fd */ |
| 663 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 664 | load_img_req.ifd_data_fd); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 665 | if (IS_ERR_OR_NULL(ihandle)) { |
| 666 | pr_err("Ion client could not retrieve the handle\n"); |
| 667 | qsee_disable_clock_vote(CLK_SFPB); |
| 668 | return -ENOMEM; |
| 669 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 670 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 671 | /* Get the physical address of the ION BUF */ |
| 672 | ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 673 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 674 | /* Populate the structure for sending scm call to load image */ |
| 675 | load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND; |
| 676 | load_req.mdt_len = load_img_req.mdt_len; |
| 677 | load_req.img_len = load_img_req.img_len; |
| 678 | load_req.phy_addr = pa; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 679 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 680 | /* SCM_CALL to load the app and get the app_id back */ |
| 681 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req, |
| 682 | sizeof(struct qseecom_load_app_ireq), |
| 683 | &resp, sizeof(resp)); |
| 684 | if (ret) { |
| 685 | pr_err("scm_call to load app failed\n"); |
| 686 | return -EINVAL; |
| 687 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 688 | |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 689 | if (resp.result == QSEOS_RESULT_FAILURE) { |
| 690 | pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n"); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 691 | if (!IS_ERR_OR_NULL(ihandle)) |
| 692 | ion_free(qseecom.ion_clnt, ihandle); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 693 | qsee_disable_clock_vote(CLK_SFPB); |
| 694 | return -EFAULT; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 695 | } |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 696 | |
| 697 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 698 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 699 | if (ret) { |
| 700 | pr_err("process_incomplete_cmd failed err: %d\n", |
| 701 | ret); |
| 702 | if (!IS_ERR_OR_NULL(ihandle)) |
| 703 | ion_free(qseecom.ion_clnt, ihandle); |
| 704 | qsee_disable_clock_vote(CLK_SFPB); |
| 705 | return ret; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 710 | pr_err("scm_call failed resp.result unknown, %d\n", |
| 711 | resp.result); |
| 712 | if (!IS_ERR_OR_NULL(ihandle)) |
| 713 | ion_free(qseecom.ion_clnt, ihandle); |
| 714 | qsee_disable_clock_vote(CLK_SFPB); |
| 715 | return -EFAULT; |
| 716 | } |
| 717 | |
| 718 | app_id = resp.data; |
| 719 | |
| 720 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 721 | if (!entry) { |
| 722 | pr_err("kmalloc failed\n"); |
| 723 | qsee_disable_clock_vote(CLK_SFPB); |
| 724 | return -ENOMEM; |
| 725 | } |
| 726 | entry->app_id = app_id; |
| 727 | entry->ref_cnt = 1; |
| 728 | |
| 729 | /* Deallocate the handle */ |
| 730 | if (!IS_ERR_OR_NULL(ihandle)) |
| 731 | ion_free(qseecom.ion_clnt, ihandle); |
| 732 | |
| 733 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 734 | list_add_tail(&entry->list, &qseecom.registered_app_list_head); |
| 735 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags); |
| 736 | |
| 737 | pr_warn("App with id %d (%s) now loaded\n", app_id, |
| 738 | (char *)(req.app_name)); |
| 739 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 740 | data->client.app_id = app_id; |
| 741 | load_img_req.app_id = app_id; |
| 742 | if (copy_to_user(argp, &load_img_req, sizeof(load_img_req))) { |
| 743 | pr_err("copy_to_user failed\n"); |
| 744 | kzfree(entry); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 745 | qsee_disable_clock_vote(CLK_SFPB); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 746 | return -EFAULT; |
| 747 | } |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 748 | qsee_disable_clock_vote(CLK_SFPB); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int __qseecom_cleanup_app(struct qseecom_dev_handle *data) |
| 753 | { |
| 754 | wake_up_all(&qseecom.send_resp_wq); |
| 755 | while (atomic_read(&data->ioctl_count) > 1) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 756 | if (wait_event_freezable(data->abort_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 757 | atomic_read(&data->ioctl_count) <= 1)) { |
| 758 | pr_err("Interrupted from abort\n"); |
| 759 | return -ERESTARTSYS; |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | /* Set unload app */ |
| 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | static int qseecom_unload_app(struct qseecom_dev_handle *data) |
| 768 | { |
| 769 | unsigned long flags; |
| 770 | int ret = 0; |
| 771 | struct qseecom_command_scm_resp resp; |
| 772 | struct qseecom_registered_app_list *ptr_app; |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 773 | bool unload = false; |
| 774 | bool found_app = false; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 775 | |
| 776 | if (qseecom.qseos_version == QSEOS_VERSION_14) { |
| 777 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 778 | list_for_each_entry(ptr_app, &qseecom.registered_app_list_head, |
| 779 | list) { |
| 780 | if (ptr_app->app_id == data->client.app_id) { |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 781 | found_app = true; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 782 | if (ptr_app->ref_cnt == 1) { |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 783 | unload = true; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 784 | break; |
| 785 | } else { |
| 786 | ptr_app->ref_cnt--; |
Mona Hossain | a5f1aab | 2012-03-29 10:18:07 -0700 | [diff] [blame] | 787 | pr_warn("Can't unload app with id %d (it is inuse)\n", |
| 788 | ptr_app->app_id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, |
| 794 | flags); |
| 795 | } |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 796 | if (found_app == false) { |
| 797 | pr_err("Cannot find app with id = %d\n", data->client.app_id); |
| 798 | return -EINVAL; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | if ((unload) && (qseecom.qseos_version == QSEOS_VERSION_14)) { |
| 802 | struct qseecom_unload_app_ireq req; |
| 803 | |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 804 | __qseecom_cleanup_app(data); |
| 805 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 806 | list_del(&ptr_app->list); |
| 807 | kzfree(ptr_app); |
| 808 | spin_unlock_irqrestore(&qseecom.registered_app_list_lock, |
| 809 | flags); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 810 | /* Populate the structure for sending scm call to load image */ |
| 811 | req.qsee_cmd_id = QSEOS_APP_SHUTDOWN_COMMAND; |
| 812 | req.app_id = data->client.app_id; |
| 813 | |
| 814 | /* SCM_CALL to unload the app */ |
| 815 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 816 | sizeof(struct qseecom_unload_app_ireq), |
| 817 | &resp, sizeof(resp)); |
| 818 | if (ret) { |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 819 | pr_err("scm_call to unload app (id = %d) failed\n", |
| 820 | req.app_id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 821 | return -EFAULT; |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 822 | } else { |
| 823 | pr_warn("App id %d now unloaded\n", req.app_id); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 824 | } |
| 825 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 826 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 827 | if (ret) { |
| 828 | pr_err("process_incomplete_cmd fail err: %d\n", |
| 829 | ret); |
| 830 | return ret; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | if (qseecom.qseos_version == QSEOS_VERSION_13) { |
| 836 | data->abort = 1; |
| 837 | wake_up_all(&qseecom.send_resp_wq); |
| 838 | while (atomic_read(&data->ioctl_count) > 0) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 839 | if (wait_event_freezable(data->abort_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 840 | atomic_read(&data->ioctl_count) <= 0)) { |
| 841 | pr_err("Interrupted from abort\n"); |
| 842 | ret = -ERESTARTSYS; |
| 843 | break; |
| 844 | } |
| 845 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 846 | } |
Mona Hossain | 340dba8 | 2012-08-07 19:54:46 -0700 | [diff] [blame] | 847 | if (!IS_ERR_OR_NULL(data->client.ihandle)) { |
| 848 | ion_unmap_kernel(qseecom.ion_clnt, data->client.ihandle); |
| 849 | ion_free(qseecom.ion_clnt, data->client.ihandle); |
| 850 | data->client.ihandle = NULL; |
| 851 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 852 | data->released = true; |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | static uint32_t __qseecom_uvirt_to_kphys(struct qseecom_dev_handle *data, |
| 857 | uint32_t virt) |
| 858 | { |
| 859 | return data->client.sb_phys + (virt - data->client.user_virt_sb_base); |
| 860 | } |
| 861 | |
| 862 | static int __qseecom_send_cmd_legacy(struct qseecom_dev_handle *data, |
| 863 | struct qseecom_send_cmd_req *req) |
| 864 | { |
| 865 | int ret = 0; |
| 866 | unsigned long flags; |
| 867 | u32 reqd_len_sb_in = 0; |
| 868 | struct qseecom_command cmd; |
| 869 | struct qseecom_response resp; |
| 870 | |
| 871 | |
| 872 | if (req->cmd_req_buf == NULL || req->resp_buf == NULL) { |
| 873 | pr_err("cmd buffer or response buffer is null\n"); |
| 874 | return -EINVAL; |
| 875 | } |
| 876 | |
| 877 | if (req->cmd_req_len <= 0 || |
| 878 | req->resp_len <= 0 || |
| 879 | req->cmd_req_len > data->client.sb_length || |
| 880 | req->resp_len > data->client.sb_length) { |
| 881 | pr_err("cmd buffer length or " |
| 882 | "response buffer length not valid\n"); |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | |
| 886 | reqd_len_sb_in = req->cmd_req_len + req->resp_len; |
| 887 | if (reqd_len_sb_in > data->client.sb_length) { |
| 888 | pr_debug("Not enough memory to fit cmd_buf and " |
| 889 | "resp_buf. Required: %u, Available: %u\n", |
| 890 | reqd_len_sb_in, data->client.sb_length); |
| 891 | return -ENOMEM; |
| 892 | } |
| 893 | cmd.cmd_type = TZ_SCHED_CMD_NEW; |
| 894 | cmd.sb_in_cmd_addr = (u8 *) data->client.sb_phys; |
| 895 | cmd.sb_in_cmd_len = req->cmd_req_len; |
| 896 | |
| 897 | resp.cmd_status = TZ_SCHED_STATUS_INCOMPLETE; |
| 898 | resp.sb_in_rsp_addr = (u8 *)data->client.sb_phys + req->cmd_req_len; |
| 899 | resp.sb_in_rsp_len = req->resp_len; |
| 900 | |
| 901 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *)&cmd, |
| 902 | sizeof(cmd), &resp, sizeof(resp)); |
| 903 | |
| 904 | if (ret) { |
| 905 | pr_err("qseecom_scm_call_legacy failed with err: %d\n", ret); |
| 906 | return ret; |
| 907 | } |
| 908 | |
| 909 | while (resp.cmd_status != TZ_SCHED_STATUS_COMPLETE) { |
| 910 | /* |
| 911 | * If cmd is incomplete, get the callback cmd out from SB out |
| 912 | * and put it on the list |
| 913 | */ |
| 914 | struct qseecom_registered_listener_list *ptr_svc = NULL; |
| 915 | /* |
| 916 | * We don't know which service can handle the command. so we |
| 917 | * wake up all blocking services and let them figure out if |
| 918 | * they can handle the given command. |
| 919 | */ |
| 920 | spin_lock_irqsave(&qseecom.registered_listener_list_lock, |
| 921 | flags); |
| 922 | list_for_each_entry(ptr_svc, |
| 923 | &qseecom.registered_listener_list_head, list) { |
| 924 | ptr_svc->rcv_req_flag = 1; |
| 925 | wake_up_interruptible(&ptr_svc->rcv_req_wq); |
| 926 | } |
| 927 | spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, |
| 928 | flags); |
| 929 | |
| 930 | pr_debug("waking up rcv_req_wq and " |
| 931 | "waiting for send_resp_wq\n"); |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 932 | if (wait_event_freezable(qseecom.send_resp_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 933 | __qseecom_listener_has_sent_rsp(data))) { |
| 934 | pr_warning("qseecom Interrupted: exiting send_cmd loop\n"); |
| 935 | return -ERESTARTSYS; |
| 936 | } |
| 937 | |
| 938 | if (data->abort) { |
| 939 | pr_err("Aborting driver\n"); |
| 940 | return -ENODEV; |
| 941 | } |
| 942 | qseecom.send_resp_flag = 0; |
| 943 | cmd.cmd_type = TZ_SCHED_CMD_PENDING; |
| 944 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *)&cmd, |
| 945 | sizeof(cmd), &resp, sizeof(resp)); |
| 946 | if (ret) { |
| 947 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 948 | return ret; |
| 949 | } |
| 950 | } |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | static int __qseecom_send_cmd(struct qseecom_dev_handle *data, |
| 955 | struct qseecom_send_cmd_req *req) |
| 956 | { |
| 957 | int ret = 0; |
| 958 | u32 reqd_len_sb_in = 0; |
| 959 | struct qseecom_client_send_data_ireq send_data_req; |
| 960 | struct qseecom_command_scm_resp resp; |
| 961 | |
| 962 | if (req->cmd_req_buf == NULL || req->resp_buf == NULL) { |
| 963 | pr_err("cmd buffer or response buffer is null\n"); |
| 964 | return -EINVAL; |
| 965 | } |
| 966 | |
| 967 | if (req->cmd_req_len <= 0 || |
| 968 | req->resp_len <= 0 || |
| 969 | req->cmd_req_len > data->client.sb_length || |
| 970 | req->resp_len > data->client.sb_length) { |
| 971 | pr_err("cmd buffer length or " |
| 972 | "response buffer length not valid\n"); |
| 973 | return -EINVAL; |
| 974 | } |
| 975 | |
| 976 | reqd_len_sb_in = req->cmd_req_len + req->resp_len; |
| 977 | if (reqd_len_sb_in > data->client.sb_length) { |
| 978 | pr_debug("Not enough memory to fit cmd_buf and " |
| 979 | "resp_buf. Required: %u, Available: %u\n", |
| 980 | reqd_len_sb_in, data->client.sb_length); |
| 981 | return -ENOMEM; |
| 982 | } |
| 983 | |
| 984 | send_data_req.qsee_cmd_id = QSEOS_CLIENT_SEND_DATA_COMMAND; |
| 985 | send_data_req.app_id = data->client.app_id; |
| 986 | send_data_req.req_ptr = (void *)(__qseecom_uvirt_to_kphys(data, |
| 987 | (uint32_t)req->cmd_req_buf)); |
| 988 | send_data_req.req_len = req->cmd_req_len; |
| 989 | send_data_req.rsp_ptr = (void *)(__qseecom_uvirt_to_kphys(data, |
| 990 | (uint32_t)req->resp_buf)); |
| 991 | send_data_req.rsp_len = req->resp_len; |
| 992 | |
| 993 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *) &send_data_req, |
| 994 | sizeof(send_data_req), |
| 995 | &resp, sizeof(resp)); |
| 996 | if (ret) { |
| 997 | pr_err("qseecom_scm_call failed with err: %d\n", ret); |
| 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 1002 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1003 | if (ret) { |
| 1004 | pr_err("process_incomplete_cmd failed err: %d\n", ret); |
| 1005 | return ret; |
| 1006 | } |
Mona Hossain | bb0bca1 | 2012-04-12 11:47:45 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 1009 | pr_err("Response result %d not supported\n", |
| 1010 | resp.result); |
| 1011 | ret = -EINVAL; |
| 1012 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1013 | } |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | static int qseecom_send_cmd(struct qseecom_dev_handle *data, void __user *argp) |
| 1019 | { |
| 1020 | int ret = 0; |
| 1021 | struct qseecom_send_cmd_req req; |
| 1022 | |
| 1023 | ret = copy_from_user(&req, argp, sizeof(req)); |
| 1024 | if (ret) { |
| 1025 | pr_err("copy_from_user failed\n"); |
| 1026 | return ret; |
| 1027 | } |
| 1028 | if (qseecom.qseos_version == QSEOS_VERSION_14) |
| 1029 | ret = __qseecom_send_cmd(data, &req); |
| 1030 | else |
| 1031 | ret = __qseecom_send_cmd_legacy(data, &req); |
| 1032 | if (ret) |
| 1033 | return ret; |
| 1034 | |
| 1035 | pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%p\n", |
| 1036 | req.resp_len, req.resp_buf); |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | static int __qseecom_send_cmd_req_clean_up( |
| 1041 | struct qseecom_send_modfd_cmd_req *req) |
| 1042 | { |
| 1043 | char *field; |
| 1044 | uint32_t *update; |
| 1045 | int ret = 0; |
| 1046 | int i = 0; |
| 1047 | |
| 1048 | for (i = 0; i < MAX_ION_FD; i++) { |
Mona Hossain | a5f1aab | 2012-03-29 10:18:07 -0700 | [diff] [blame] | 1049 | if (req->ifd_data[i].fd > 0) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1050 | field = (char *)req->cmd_req_buf + |
| 1051 | req->ifd_data[i].cmd_buf_offset; |
| 1052 | update = (uint32_t *) field; |
| 1053 | *update = 0; |
| 1054 | } |
| 1055 | } |
| 1056 | return ret; |
| 1057 | } |
| 1058 | |
| 1059 | static int __qseecom_update_with_phy_addr( |
| 1060 | struct qseecom_send_modfd_cmd_req *req) |
| 1061 | { |
| 1062 | struct ion_handle *ihandle; |
| 1063 | char *field; |
| 1064 | uint32_t *update; |
| 1065 | ion_phys_addr_t pa; |
| 1066 | int ret = 0; |
| 1067 | int i = 0; |
| 1068 | uint32_t length; |
| 1069 | |
| 1070 | for (i = 0; i < MAX_ION_FD; i++) { |
Mona Hossain | a5f1aab | 2012-03-29 10:18:07 -0700 | [diff] [blame] | 1071 | if (req->ifd_data[i].fd > 0) { |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1072 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1073 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1074 | req->ifd_data[i].fd); |
| 1075 | if (IS_ERR_OR_NULL(ihandle)) { |
| 1076 | pr_err("Ion client can't retrieve the handle\n"); |
| 1077 | return -ENOMEM; |
| 1078 | } |
| 1079 | field = (char *) req->cmd_req_buf + |
| 1080 | req->ifd_data[i].cmd_buf_offset; |
| 1081 | update = (uint32_t *) field; |
| 1082 | |
| 1083 | /* Populate the cmd data structure with the phys_addr */ |
| 1084 | ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &length); |
| 1085 | if (ret) |
| 1086 | return -ENOMEM; |
| 1087 | |
| 1088 | *update = (uint32_t)pa; |
| 1089 | /* Deallocate the handle */ |
| 1090 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1091 | ion_free(qseecom.ion_clnt, ihandle); |
| 1092 | } |
| 1093 | } |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | static int qseecom_send_modfd_cmd(struct qseecom_dev_handle *data, |
| 1098 | void __user *argp) |
| 1099 | { |
| 1100 | int ret = 0; |
| 1101 | struct qseecom_send_modfd_cmd_req req; |
| 1102 | struct qseecom_send_cmd_req send_cmd_req; |
| 1103 | |
| 1104 | ret = copy_from_user(&req, argp, sizeof(req)); |
| 1105 | if (ret) { |
| 1106 | pr_err("copy_from_user failed\n"); |
| 1107 | return ret; |
| 1108 | } |
| 1109 | send_cmd_req.cmd_req_buf = req.cmd_req_buf; |
| 1110 | send_cmd_req.cmd_req_len = req.cmd_req_len; |
| 1111 | send_cmd_req.resp_buf = req.resp_buf; |
| 1112 | send_cmd_req.resp_len = req.resp_len; |
| 1113 | |
| 1114 | ret = __qseecom_update_with_phy_addr(&req); |
| 1115 | if (ret) |
| 1116 | return ret; |
| 1117 | if (qseecom.qseos_version == QSEOS_VERSION_14) |
| 1118 | ret = __qseecom_send_cmd(data, &send_cmd_req); |
| 1119 | else |
| 1120 | ret = __qseecom_send_cmd_legacy(data, &send_cmd_req); |
| 1121 | __qseecom_send_cmd_req_clean_up(&req); |
| 1122 | |
| 1123 | if (ret) |
| 1124 | return ret; |
| 1125 | |
| 1126 | pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%p\n", |
| 1127 | req.resp_len, req.resp_buf); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | static int __qseecom_listener_has_rcvd_req(struct qseecom_dev_handle *data, |
| 1132 | struct qseecom_registered_listener_list *svc) |
| 1133 | { |
| 1134 | int ret; |
| 1135 | ret = (svc->rcv_req_flag != 0); |
| 1136 | return ret || data->abort; |
| 1137 | } |
| 1138 | |
| 1139 | static int qseecom_receive_req(struct qseecom_dev_handle *data) |
| 1140 | { |
| 1141 | int ret = 0; |
| 1142 | struct qseecom_registered_listener_list *this_lstnr; |
| 1143 | |
| 1144 | this_lstnr = __qseecom_find_svc(data->listener.id); |
| 1145 | while (1) { |
Mona Hossain | acea102 | 2012-04-09 13:37:27 -0700 | [diff] [blame] | 1146 | if (wait_event_freezable(this_lstnr->rcv_req_wq, |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1147 | __qseecom_listener_has_rcvd_req(data, |
| 1148 | this_lstnr))) { |
| 1149 | pr_warning("Interrupted: exiting wait_rcv_req loop\n"); |
| 1150 | /* woken up for different reason */ |
| 1151 | return -ERESTARTSYS; |
| 1152 | } |
| 1153 | |
| 1154 | if (data->abort) { |
| 1155 | pr_err("Aborting driver!\n"); |
| 1156 | return -ENODEV; |
| 1157 | } |
| 1158 | this_lstnr->rcv_req_flag = 0; |
| 1159 | if (qseecom.qseos_version == QSEOS_VERSION_13) { |
| 1160 | if (*((uint32_t *)this_lstnr->sb_virt) != 0) |
| 1161 | break; |
| 1162 | } else { |
| 1163 | break; |
| 1164 | } |
| 1165 | } |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
| 1169 | static int qseecom_send_resp(void) |
| 1170 | { |
| 1171 | qseecom.send_resp_flag = 1; |
| 1172 | wake_up_interruptible(&qseecom.send_resp_wq); |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | static int qseecom_get_qseos_version(struct qseecom_dev_handle *data, |
| 1177 | void __user *argp) |
| 1178 | { |
| 1179 | struct qseecom_qseos_version_req req; |
| 1180 | |
| 1181 | if (copy_from_user(&req, argp, sizeof(req))) { |
| 1182 | pr_err("copy_from_user failed"); |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | req.qseos_version = qseecom.qseos_version; |
| 1186 | if (copy_to_user(argp, &req, sizeof(req))) { |
| 1187 | pr_err("copy_to_user failed"); |
| 1188 | return -EINVAL; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1193 | static int qsee_vote_for_clock(int32_t clk_type) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1194 | { |
| 1195 | int ret = 0; |
| 1196 | |
| 1197 | if (!qsee_perf_client) |
Ramesh Masavarapu | e640e84 | 2012-04-03 11:21:54 -0700 | [diff] [blame] | 1198 | return ret; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1199 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1200 | switch (clk_type) { |
| 1201 | case CLK_DFAB: |
| 1202 | /* Check if the clk is valid */ |
| 1203 | if (IS_ERR_OR_NULL(qseecom_bus_clk)) { |
| 1204 | pr_warn("qseecom bus clock is null or error"); |
| 1205 | return -EINVAL; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1206 | } |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1207 | mutex_lock(&qsee_bw_mutex); |
| 1208 | if (!qsee_bw_count) { |
| 1209 | ret = msm_bus_scale_client_update_request( |
| 1210 | qsee_perf_client, 1); |
| 1211 | if (ret) |
| 1212 | pr_err("DFAB Bandwidth req failed (%d)\n", |
| 1213 | ret); |
| 1214 | else |
| 1215 | qsee_bw_count++; |
| 1216 | } |
| 1217 | mutex_unlock(&qsee_bw_mutex); |
| 1218 | break; |
| 1219 | case CLK_SFPB: |
| 1220 | mutex_lock(&qsee_sfpb_bw_mutex); |
| 1221 | if (!qsee_sfpb_bw_count) { |
| 1222 | ret = msm_bus_scale_client_update_request( |
| 1223 | qsee_perf_client, 2); |
| 1224 | if (ret) |
| 1225 | pr_err("SFPB Bandwidth req failed (%d)\n", |
| 1226 | ret); |
| 1227 | else |
| 1228 | qsee_sfpb_bw_count++; |
| 1229 | } |
| 1230 | mutex_unlock(&qsee_sfpb_bw_mutex); |
| 1231 | break; |
| 1232 | default: |
| 1233 | pr_err("Clock type not defined\n"); |
| 1234 | break; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1235 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1236 | return ret; |
| 1237 | } |
| 1238 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1239 | static void qsee_disable_clock_vote(int32_t clk_type) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1240 | { |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1241 | int32_t ret = 0; |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 1242 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1243 | if (!qsee_perf_client) |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 1244 | return; |
| 1245 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1246 | switch (clk_type) { |
| 1247 | case CLK_DFAB: |
| 1248 | /* Check if the DFAB clk is valid */ |
| 1249 | if (IS_ERR_OR_NULL(qseecom_bus_clk)) { |
| 1250 | pr_warn("qseecom bus clock is null or error"); |
| 1251 | return; |
| 1252 | } |
| 1253 | mutex_lock(&qsee_bw_mutex); |
| 1254 | if (qsee_bw_count > 0) { |
| 1255 | if (qsee_bw_count-- == 1) { |
| 1256 | ret = msm_bus_scale_client_update_request( |
| 1257 | qsee_perf_client, 0); |
| 1258 | if (ret) |
| 1259 | pr_err("SFPB Bandwidth req fail (%d)\n", |
| 1260 | ret); |
| 1261 | } |
| 1262 | } |
| 1263 | mutex_unlock(&qsee_bw_mutex); |
| 1264 | break; |
| 1265 | case CLK_SFPB: |
| 1266 | mutex_lock(&qsee_sfpb_bw_mutex); |
| 1267 | if (qsee_sfpb_bw_count > 0) { |
| 1268 | if (qsee_sfpb_bw_count-- == 1) { |
| 1269 | ret = msm_bus_scale_client_update_request( |
| 1270 | qsee_perf_client, 0); |
| 1271 | if (ret) |
| 1272 | pr_err("SFPB Bandwidth req fail (%d)\n", |
| 1273 | ret); |
| 1274 | } |
| 1275 | } |
| 1276 | mutex_unlock(&qsee_sfpb_bw_mutex); |
| 1277 | break; |
| 1278 | default: |
| 1279 | pr_err("Clock type not defined\n"); |
| 1280 | break; |
Ramesh Masavarapu | e640e84 | 2012-04-03 11:21:54 -0700 | [diff] [blame] | 1281 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1282 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1285 | static int qseecom_load_external_elf(struct qseecom_dev_handle *data, |
| 1286 | void __user *argp) |
| 1287 | { |
| 1288 | struct ion_handle *ihandle; /* Ion handle */ |
| 1289 | struct qseecom_load_img_req load_img_req; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1290 | int ret; |
| 1291 | int set_cpu_ret = 0; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1292 | ion_phys_addr_t pa = 0; |
| 1293 | uint32_t len; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1294 | struct cpumask mask; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1295 | struct qseecom_load_app_ireq load_req; |
| 1296 | struct qseecom_command_scm_resp resp; |
| 1297 | |
| 1298 | /* Copy the relevant information needed for loading the image */ |
| 1299 | if (__copy_from_user(&load_img_req, |
| 1300 | (void __user *)argp, |
| 1301 | sizeof(struct qseecom_load_img_req))) { |
| 1302 | pr_err("copy_from_user failed\n"); |
| 1303 | return -EFAULT; |
| 1304 | } |
| 1305 | |
| 1306 | /* Get the handle of the shared fd */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1307 | ihandle = ion_import_dma_buf(qseecom.ion_clnt, |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1308 | load_img_req.ifd_data_fd); |
| 1309 | if (IS_ERR_OR_NULL(ihandle)) { |
| 1310 | pr_err("Ion client could not retrieve the handle\n"); |
| 1311 | return -ENOMEM; |
| 1312 | } |
| 1313 | |
| 1314 | /* Get the physical address of the ION BUF */ |
| 1315 | ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len); |
| 1316 | |
| 1317 | /* Populate the structure for sending scm call to load image */ |
| 1318 | load_req.qsee_cmd_id = QSEOS_LOAD_EXTERNAL_ELF_COMMAND; |
| 1319 | load_req.mdt_len = load_img_req.mdt_len; |
| 1320 | load_req.img_len = load_img_req.img_len; |
| 1321 | load_req.phy_addr = pa; |
| 1322 | |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1323 | /* SCM_CALL tied to Core0 */ |
| 1324 | mask = CPU_MASK_CPU0; |
| 1325 | set_cpu_ret = set_cpus_allowed_ptr(current, &mask); |
| 1326 | if (set_cpu_ret) { |
| 1327 | pr_err("set_cpus_allowed_ptr failed : ret %d\n", |
| 1328 | set_cpu_ret); |
| 1329 | ret = -EFAULT; |
| 1330 | goto qseecom_load_external_elf_set_cpu_err; |
| 1331 | } |
| 1332 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1333 | /* SCM_CALL to load the external elf */ |
| 1334 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req, |
| 1335 | sizeof(struct qseecom_load_app_ireq), |
| 1336 | &resp, sizeof(resp)); |
| 1337 | if (ret) { |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1338 | pr_err("scm_call to load failed : ret %d\n", |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1339 | ret); |
| 1340 | ret = -EFAULT; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1341 | goto qseecom_load_external_elf_scm_err; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 1345 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1346 | if (ret) |
| 1347 | pr_err("process_incomplete_cmd failed err: %d\n", |
| 1348 | ret); |
| 1349 | } else { |
| 1350 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 1351 | pr_err("scm_call to load image failed resp.result =%d\n", |
| 1352 | resp.result); |
| 1353 | ret = -EFAULT; |
| 1354 | } |
| 1355 | } |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1356 | |
| 1357 | qseecom_load_external_elf_scm_err: |
| 1358 | /* Restore the CPU mask */ |
| 1359 | mask = CPU_MASK_ALL; |
| 1360 | set_cpu_ret = set_cpus_allowed_ptr(current, &mask); |
| 1361 | if (set_cpu_ret) { |
| 1362 | pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n", |
| 1363 | set_cpu_ret); |
| 1364 | ret = -EFAULT; |
| 1365 | } |
| 1366 | |
| 1367 | qseecom_load_external_elf_set_cpu_err: |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1368 | /* Deallocate the handle */ |
| 1369 | if (!IS_ERR_OR_NULL(ihandle)) |
| 1370 | ion_free(qseecom.ion_clnt, ihandle); |
| 1371 | |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
| 1375 | static int qseecom_unload_external_elf(struct qseecom_dev_handle *data) |
| 1376 | { |
| 1377 | int ret = 0; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1378 | int set_cpu_ret = 0; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1379 | struct qseecom_command_scm_resp resp; |
| 1380 | struct qseecom_unload_app_ireq req; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1381 | struct cpumask mask; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1382 | |
| 1383 | /* Populate the structure for sending scm call to unload image */ |
| 1384 | req.qsee_cmd_id = QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND; |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1385 | |
| 1386 | /* SCM_CALL tied to Core0 */ |
| 1387 | mask = CPU_MASK_CPU0; |
| 1388 | ret = set_cpus_allowed_ptr(current, &mask); |
| 1389 | if (ret) { |
| 1390 | pr_err("set_cpus_allowed_ptr failed : ret %d\n", |
| 1391 | ret); |
| 1392 | return -EFAULT; |
| 1393 | } |
| 1394 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1395 | /* SCM_CALL to unload the external elf */ |
| 1396 | ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, |
| 1397 | sizeof(struct qseecom_unload_app_ireq), |
| 1398 | &resp, sizeof(resp)); |
| 1399 | if (ret) { |
| 1400 | pr_err("scm_call to unload failed : ret %d\n", |
| 1401 | ret); |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1402 | ret = -EFAULT; |
| 1403 | goto qseecom_unload_external_elf_scm_err; |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1404 | } |
| 1405 | if (resp.result == QSEOS_RESULT_INCOMPLETE) { |
| 1406 | ret = __qseecom_process_incomplete_cmd(data, &resp); |
| 1407 | if (ret) |
| 1408 | pr_err("process_incomplete_cmd fail err: %d\n", |
| 1409 | ret); |
| 1410 | } else { |
| 1411 | if (resp.result != QSEOS_RESULT_SUCCESS) { |
| 1412 | pr_err("scm_call to unload image failed resp.result =%d\n", |
| 1413 | resp.result); |
| 1414 | ret = -EFAULT; |
| 1415 | } |
| 1416 | } |
Ramesh Masavarapu | 41af8ab | 2012-05-21 11:08:00 -0700 | [diff] [blame] | 1417 | |
| 1418 | qseecom_unload_external_elf_scm_err: |
| 1419 | /* Restore the CPU mask */ |
| 1420 | mask = CPU_MASK_ALL; |
| 1421 | set_cpu_ret = set_cpus_allowed_ptr(current, &mask); |
| 1422 | if (set_cpu_ret) { |
| 1423 | pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n", |
| 1424 | set_cpu_ret); |
| 1425 | ret = -EFAULT; |
| 1426 | } |
| 1427 | |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1428 | return ret; |
| 1429 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1430 | |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 1431 | static int qseecom_query_app_loaded(struct qseecom_dev_handle *data, |
| 1432 | void __user *argp) |
| 1433 | { |
| 1434 | |
| 1435 | int32_t ret; |
| 1436 | struct qseecom_qseos_app_load_query query_req; |
| 1437 | struct qseecom_check_app_ireq req; |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1438 | struct qseecom_registered_app_list *entry = NULL; |
| 1439 | unsigned long flags = 0; |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* Copy the relevant information needed for loading the image */ |
| 1442 | if (__copy_from_user(&query_req, |
| 1443 | (void __user *)argp, |
| 1444 | sizeof(struct qseecom_qseos_app_load_query))) { |
| 1445 | pr_err("copy_from_user failed\n"); |
| 1446 | return -EFAULT; |
| 1447 | } |
| 1448 | |
| 1449 | req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND; |
| 1450 | memcpy(req.app_name, query_req.app_name, MAX_APP_NAME_SIZE); |
| 1451 | |
| 1452 | ret = __qseecom_check_app_exists(req); |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1453 | |
| 1454 | if ((ret == -EINVAL) || (ret == -ENODEV)) { |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 1455 | pr_err(" scm call to check if app is loaded failed"); |
| 1456 | return ret; /* scm call failed */ |
| 1457 | } else if (ret > 0) { |
Ramesh Masavarapu | 13d9967 | 2012-07-17 11:05:15 -0700 | [diff] [blame] | 1458 | pr_warn("App id %d (%s) already exists\n", ret, |
| 1459 | (char *)(req.app_name)); |
| 1460 | spin_lock_irqsave(&qseecom.registered_app_list_lock, flags); |
| 1461 | list_for_each_entry(entry, |
| 1462 | &qseecom.registered_app_list_head, list){ |
| 1463 | if (entry->app_id == ret) { |
| 1464 | entry->ref_cnt++; |
| 1465 | break; |
| 1466 | } |
| 1467 | } |
| 1468 | spin_unlock_irqrestore( |
| 1469 | &qseecom.registered_app_list_lock, flags); |
| 1470 | data->client.app_id = ret; |
| 1471 | query_req.app_id = ret; |
| 1472 | |
| 1473 | if (copy_to_user(argp, &query_req, sizeof(query_req))) { |
| 1474 | pr_err("copy_to_user failed\n"); |
| 1475 | return -EFAULT; |
| 1476 | } |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 1477 | return -EEXIST; /* app already loaded */ |
| 1478 | } else { |
| 1479 | return 0; /* app not loaded */ |
| 1480 | } |
| 1481 | } |
| 1482 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1483 | static long qseecom_ioctl(struct file *file, unsigned cmd, |
| 1484 | unsigned long arg) |
| 1485 | { |
| 1486 | int ret = 0; |
| 1487 | struct qseecom_dev_handle *data = file->private_data; |
| 1488 | void __user *argp = (void __user *) arg; |
| 1489 | |
| 1490 | if (data->abort) { |
| 1491 | pr_err("Aborting qseecom driver\n"); |
| 1492 | return -ENODEV; |
| 1493 | } |
| 1494 | |
| 1495 | switch (cmd) { |
| 1496 | case QSEECOM_IOCTL_REGISTER_LISTENER_REQ: { |
| 1497 | pr_debug("ioctl register_listener_req()\n"); |
| 1498 | atomic_inc(&data->ioctl_count); |
| 1499 | ret = qseecom_register_listener(data, argp); |
| 1500 | atomic_dec(&data->ioctl_count); |
| 1501 | wake_up_all(&data->abort_wq); |
| 1502 | if (ret) |
| 1503 | pr_err("failed qseecom_register_listener: %d\n", ret); |
| 1504 | break; |
| 1505 | } |
| 1506 | case QSEECOM_IOCTL_UNREGISTER_LISTENER_REQ: { |
| 1507 | pr_debug("ioctl unregister_listener_req()\n"); |
| 1508 | atomic_inc(&data->ioctl_count); |
| 1509 | ret = qseecom_unregister_listener(data); |
| 1510 | atomic_dec(&data->ioctl_count); |
| 1511 | wake_up_all(&data->abort_wq); |
| 1512 | if (ret) |
| 1513 | pr_err("failed qseecom_unregister_listener: %d\n", ret); |
| 1514 | break; |
| 1515 | } |
| 1516 | case QSEECOM_IOCTL_SEND_CMD_REQ: { |
| 1517 | /* Only one client allowed here at a time */ |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 1518 | mutex_lock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1519 | atomic_inc(&data->ioctl_count); |
| 1520 | ret = qseecom_send_cmd(data, argp); |
| 1521 | atomic_dec(&data->ioctl_count); |
| 1522 | wake_up_all(&data->abort_wq); |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 1523 | mutex_unlock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1524 | if (ret) |
| 1525 | pr_err("failed qseecom_send_cmd: %d\n", ret); |
| 1526 | break; |
| 1527 | } |
| 1528 | case QSEECOM_IOCTL_SEND_MODFD_CMD_REQ: { |
| 1529 | /* Only one client allowed here at a time */ |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 1530 | mutex_lock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1531 | atomic_inc(&data->ioctl_count); |
| 1532 | ret = qseecom_send_modfd_cmd(data, argp); |
| 1533 | atomic_dec(&data->ioctl_count); |
| 1534 | wake_up_all(&data->abort_wq); |
Ramesh Masavarapu | d861011 | 2012-06-26 15:32:52 -0700 | [diff] [blame] | 1535 | mutex_unlock(&app_access_lock); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1536 | if (ret) |
| 1537 | pr_err("failed qseecom_send_cmd: %d\n", ret); |
| 1538 | break; |
| 1539 | } |
| 1540 | case QSEECOM_IOCTL_RECEIVE_REQ: { |
| 1541 | atomic_inc(&data->ioctl_count); |
| 1542 | ret = qseecom_receive_req(data); |
| 1543 | atomic_dec(&data->ioctl_count); |
| 1544 | wake_up_all(&data->abort_wq); |
| 1545 | if (ret) |
| 1546 | pr_err("failed qseecom_receive_req: %d\n", ret); |
| 1547 | break; |
| 1548 | } |
| 1549 | case QSEECOM_IOCTL_SEND_RESP_REQ: { |
| 1550 | atomic_inc(&data->ioctl_count); |
| 1551 | ret = qseecom_send_resp(); |
| 1552 | atomic_dec(&data->ioctl_count); |
| 1553 | wake_up_all(&data->abort_wq); |
| 1554 | if (ret) |
| 1555 | pr_err("failed qseecom_send_resp: %d\n", ret); |
| 1556 | break; |
| 1557 | } |
| 1558 | case QSEECOM_IOCTL_SET_MEM_PARAM_REQ: { |
| 1559 | ret = qseecom_set_client_mem_param(data, argp); |
| 1560 | if (ret) |
| 1561 | pr_err("failed Qqseecom_set_mem_param request: %d\n", |
| 1562 | ret); |
| 1563 | break; |
| 1564 | } |
| 1565 | case QSEECOM_IOCTL_LOAD_APP_REQ: { |
| 1566 | mutex_lock(&app_access_lock); |
| 1567 | atomic_inc(&data->ioctl_count); |
| 1568 | ret = qseecom_load_app(data, argp); |
| 1569 | atomic_dec(&data->ioctl_count); |
| 1570 | mutex_unlock(&app_access_lock); |
| 1571 | if (ret) |
| 1572 | pr_err("failed load_app request: %d\n", ret); |
| 1573 | break; |
| 1574 | } |
| 1575 | case QSEECOM_IOCTL_UNLOAD_APP_REQ: { |
| 1576 | mutex_lock(&app_access_lock); |
| 1577 | atomic_inc(&data->ioctl_count); |
| 1578 | ret = qseecom_unload_app(data); |
| 1579 | atomic_dec(&data->ioctl_count); |
| 1580 | mutex_unlock(&app_access_lock); |
| 1581 | if (ret) |
| 1582 | pr_err("failed unload_app request: %d\n", ret); |
| 1583 | break; |
| 1584 | } |
| 1585 | case QSEECOM_IOCTL_GET_QSEOS_VERSION_REQ: { |
| 1586 | atomic_inc(&data->ioctl_count); |
| 1587 | ret = qseecom_get_qseos_version(data, argp); |
| 1588 | if (ret) |
| 1589 | pr_err("qseecom_get_qseos_version: %d\n", ret); |
| 1590 | atomic_dec(&data->ioctl_count); |
| 1591 | break; |
| 1592 | } |
| 1593 | case QSEECOM_IOCTL_PERF_ENABLE_REQ:{ |
| 1594 | atomic_inc(&data->ioctl_count); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1595 | ret = qsee_vote_for_clock(CLK_DFAB); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1596 | if (ret) |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1597 | pr_err("Failed to vote for DFAB clock%d\n", ret); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1598 | atomic_dec(&data->ioctl_count); |
| 1599 | break; |
| 1600 | } |
| 1601 | case QSEECOM_IOCTL_PERF_DISABLE_REQ:{ |
| 1602 | atomic_inc(&data->ioctl_count); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1603 | qsee_disable_clock_vote(CLK_DFAB); |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1604 | atomic_dec(&data->ioctl_count); |
| 1605 | break; |
| 1606 | } |
Mona Hossain | 5ab9d77 | 2012-04-11 21:00:40 -0700 | [diff] [blame] | 1607 | case QSEECOM_IOCTL_LOAD_EXTERNAL_ELF_REQ: { |
| 1608 | data->released = true; |
| 1609 | if (qseecom.qseos_version == QSEOS_VERSION_13) { |
| 1610 | pr_err("Loading External elf image unsupported in rev 0x13\n"); |
| 1611 | ret = -EINVAL; |
| 1612 | break; |
| 1613 | } |
| 1614 | mutex_lock(&app_access_lock); |
| 1615 | atomic_inc(&data->ioctl_count); |
| 1616 | ret = qseecom_load_external_elf(data, argp); |
| 1617 | atomic_dec(&data->ioctl_count); |
| 1618 | mutex_unlock(&app_access_lock); |
| 1619 | if (ret) |
| 1620 | pr_err("failed load_external_elf request: %d\n", ret); |
| 1621 | break; |
| 1622 | } |
| 1623 | case QSEECOM_IOCTL_UNLOAD_EXTERNAL_ELF_REQ: { |
| 1624 | data->released = true; |
| 1625 | if (qseecom.qseos_version == QSEOS_VERSION_13) { |
| 1626 | pr_err("Unloading External elf image unsupported in rev 0x13\n"); |
| 1627 | ret = -EINVAL; |
| 1628 | break; |
| 1629 | } |
| 1630 | mutex_lock(&app_access_lock); |
| 1631 | atomic_inc(&data->ioctl_count); |
| 1632 | ret = qseecom_unload_external_elf(data); |
| 1633 | atomic_dec(&data->ioctl_count); |
| 1634 | mutex_unlock(&app_access_lock); |
| 1635 | if (ret) |
| 1636 | pr_err("failed unload_app request: %d\n", ret); |
| 1637 | break; |
| 1638 | } |
Ramesh Masavarapu | 1ee4ac8 | 2012-06-22 15:38:34 -0700 | [diff] [blame] | 1639 | case QSEECOM_IOCTL_APP_LOADED_QUERY_REQ: { |
| 1640 | mutex_lock(&app_access_lock); |
| 1641 | atomic_inc(&data->ioctl_count); |
| 1642 | ret = qseecom_query_app_loaded(data, argp); |
| 1643 | atomic_dec(&data->ioctl_count); |
| 1644 | mutex_unlock(&app_access_lock); |
| 1645 | break; |
| 1646 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1647 | default: |
| 1648 | return -EINVAL; |
| 1649 | } |
| 1650 | return ret; |
| 1651 | } |
| 1652 | |
| 1653 | static int qseecom_open(struct inode *inode, struct file *file) |
| 1654 | { |
| 1655 | int ret = 0; |
| 1656 | struct qseecom_dev_handle *data; |
| 1657 | |
| 1658 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1659 | if (!data) { |
| 1660 | pr_err("kmalloc failed\n"); |
| 1661 | return -ENOMEM; |
| 1662 | } |
| 1663 | file->private_data = data; |
| 1664 | data->abort = 0; |
| 1665 | data->service = false; |
| 1666 | data->released = false; |
| 1667 | init_waitqueue_head(&data->abort_wq); |
| 1668 | atomic_set(&data->ioctl_count, 0); |
Mona Hossain | 7cdbfaf | 2012-03-15 18:56:10 -0700 | [diff] [blame] | 1669 | if (qseecom.qseos_version == QSEOS_VERSION_13) { |
| 1670 | int pil_error; |
| 1671 | mutex_lock(&pil_access_lock); |
| 1672 | if (pil_ref_cnt == 0) { |
| 1673 | pil = pil_get("tzapps"); |
| 1674 | if (IS_ERR(pil)) { |
| 1675 | pr_err("Playready PIL image load failed\n"); |
| 1676 | pil_error = PTR_ERR(pil); |
| 1677 | pil = NULL; |
| 1678 | pr_debug("tzapps image load FAILED\n"); |
| 1679 | mutex_unlock(&pil_access_lock); |
| 1680 | return pil_error; |
| 1681 | } |
| 1682 | } |
| 1683 | pil_ref_cnt++; |
| 1684 | mutex_unlock(&pil_access_lock); |
| 1685 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1686 | return ret; |
| 1687 | } |
| 1688 | |
| 1689 | static int qseecom_release(struct inode *inode, struct file *file) |
| 1690 | { |
| 1691 | struct qseecom_dev_handle *data = file->private_data; |
| 1692 | int ret = 0; |
| 1693 | |
| 1694 | if (data->released == false) { |
| 1695 | pr_warn("data->released == false\n"); |
| 1696 | if (data->service) |
| 1697 | ret = qseecom_unregister_listener(data); |
| 1698 | else |
| 1699 | ret = qseecom_unload_app(data); |
| 1700 | if (ret) { |
| 1701 | pr_err("Close failed\n"); |
| 1702 | return ret; |
| 1703 | } |
| 1704 | } |
Mona Hossain | 7cdbfaf | 2012-03-15 18:56:10 -0700 | [diff] [blame] | 1705 | if (qseecom.qseos_version == QSEOS_VERSION_13) { |
| 1706 | mutex_lock(&pil_access_lock); |
| 1707 | if (pil_ref_cnt == 1) |
| 1708 | pil_put(pil); |
| 1709 | pil_ref_cnt--; |
| 1710 | mutex_unlock(&pil_access_lock); |
| 1711 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1712 | kfree(data); |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1713 | qsee_disable_clock_vote(CLK_DFAB); |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 1714 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1715 | return ret; |
| 1716 | } |
| 1717 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1718 | static const struct file_operations qseecom_fops = { |
| 1719 | .owner = THIS_MODULE, |
| 1720 | .unlocked_ioctl = qseecom_ioctl, |
| 1721 | .open = qseecom_open, |
| 1722 | .release = qseecom_release |
| 1723 | }; |
| 1724 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1725 | static int __devinit qseecom_probe(struct platform_device *pdev) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1726 | { |
| 1727 | int rc; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1728 | struct device *class_dev; |
| 1729 | char qsee_not_legacy = 0; |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1730 | struct msm_bus_scale_pdata *qseecom_platform_support; |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1731 | uint32_t system_call_id = QSEOS_CHECK_VERSION_CMD; |
| 1732 | |
Ramesh Masavarapu | e640e84 | 2012-04-03 11:21:54 -0700 | [diff] [blame] | 1733 | qsee_bw_count = 0; |
| 1734 | qseecom_bus_clk = NULL; |
| 1735 | qsee_perf_client = 0; |
| 1736 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1737 | rc = alloc_chrdev_region(&qseecom_device_no, 0, 1, QSEECOM_DEV); |
| 1738 | if (rc < 0) { |
| 1739 | pr_err("alloc_chrdev_region failed %d\n", rc); |
| 1740 | return rc; |
| 1741 | } |
| 1742 | |
| 1743 | driver_class = class_create(THIS_MODULE, QSEECOM_DEV); |
| 1744 | if (IS_ERR(driver_class)) { |
| 1745 | rc = -ENOMEM; |
| 1746 | pr_err("class_create failed %d\n", rc); |
| 1747 | goto unregister_chrdev_region; |
| 1748 | } |
| 1749 | |
| 1750 | class_dev = device_create(driver_class, NULL, qseecom_device_no, NULL, |
| 1751 | QSEECOM_DEV); |
| 1752 | if (!class_dev) { |
| 1753 | pr_err("class_device_create failed %d\n", rc); |
| 1754 | rc = -ENOMEM; |
| 1755 | goto class_destroy; |
| 1756 | } |
| 1757 | |
| 1758 | cdev_init(&qseecom_cdev, &qseecom_fops); |
| 1759 | qseecom_cdev.owner = THIS_MODULE; |
| 1760 | |
| 1761 | rc = cdev_add(&qseecom_cdev, MKDEV(MAJOR(qseecom_device_no), 0), 1); |
| 1762 | if (rc < 0) { |
| 1763 | pr_err("cdev_add failed %d\n", rc); |
| 1764 | goto err; |
| 1765 | } |
| 1766 | |
| 1767 | INIT_LIST_HEAD(&qseecom.registered_listener_list_head); |
| 1768 | spin_lock_init(&qseecom.registered_listener_list_lock); |
| 1769 | INIT_LIST_HEAD(&qseecom.registered_app_list_head); |
| 1770 | spin_lock_init(&qseecom.registered_app_list_lock); |
| 1771 | init_waitqueue_head(&qseecom.send_resp_wq); |
| 1772 | qseecom.send_resp_flag = 0; |
| 1773 | |
| 1774 | rc = scm_call(6, 1, &system_call_id, sizeof(system_call_id), |
| 1775 | &qsee_not_legacy, sizeof(qsee_not_legacy)); |
| 1776 | if (rc) { |
| 1777 | pr_err("Failed to retrieve QSEE version information %d\n", rc); |
| 1778 | goto err; |
| 1779 | } |
| 1780 | if (qsee_not_legacy) |
| 1781 | qseecom.qseos_version = QSEOS_VERSION_14; |
| 1782 | else { |
| 1783 | qseecom.qseos_version = QSEOS_VERSION_13; |
| 1784 | pil = NULL; |
| 1785 | pil_ref_cnt = 0; |
| 1786 | } |
| 1787 | /* Create ION msm client */ |
| 1788 | qseecom.ion_clnt = msm_ion_client_create(0x03, "qseecom-kernel"); |
| 1789 | if (qseecom.ion_clnt == NULL) { |
| 1790 | pr_err("Ion client cannot be created\n"); |
| 1791 | rc = -ENOMEM; |
| 1792 | goto err; |
| 1793 | } |
| 1794 | |
| 1795 | /* register client for bus scaling */ |
Ramesh Masavarapu | fb1f01e | 2012-06-14 09:40:40 -0700 | [diff] [blame] | 1796 | if (!pdev->dev.of_node) { |
| 1797 | qseecom_platform_support = (struct msm_bus_scale_pdata *) |
| 1798 | pdev->dev.platform_data; |
| 1799 | qsee_perf_client = msm_bus_scale_register_client( |
| 1800 | qseecom_platform_support); |
| 1801 | |
| 1802 | if (!qsee_perf_client) { |
| 1803 | pr_err("Unable to register bus client\n"); |
| 1804 | } else { |
| 1805 | qseecom_bus_clk = clk_get(class_dev, "bus_clk"); |
| 1806 | if (IS_ERR(qseecom_bus_clk)) { |
| 1807 | qseecom_bus_clk = NULL; |
| 1808 | } else if (qseecom_bus_clk != NULL) { |
| 1809 | pr_debug("Enabled DFAB clock"); |
| 1810 | clk_set_rate(qseecom_bus_clk, 64000000); |
| 1811 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1812 | } |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1813 | } |
| 1814 | return 0; |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 1815 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1816 | err: |
| 1817 | device_destroy(driver_class, qseecom_device_no); |
| 1818 | class_destroy: |
| 1819 | class_destroy(driver_class); |
| 1820 | unregister_chrdev_region: |
| 1821 | unregister_chrdev_region(qseecom_device_no, 1); |
| 1822 | return rc; |
| 1823 | } |
| 1824 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1825 | static int __devinit qseecom_remove(struct platform_device *pdev) |
| 1826 | { |
| 1827 | if (pdev->dev.platform_data != NULL) |
| 1828 | msm_bus_scale_unregister_client(qsee_perf_client); |
| 1829 | return 0; |
| 1830 | }; |
| 1831 | |
Ramesh Masavarapu | fb1f01e | 2012-06-14 09:40:40 -0700 | [diff] [blame] | 1832 | static struct of_device_id qseecom_match[] = { |
| 1833 | { |
| 1834 | .compatible = "qcom,qseecom", |
| 1835 | }, |
| 1836 | {} |
| 1837 | }; |
| 1838 | |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1839 | static struct platform_driver qseecom_plat_driver = { |
| 1840 | .probe = qseecom_probe, |
| 1841 | .remove = qseecom_remove, |
| 1842 | .driver = { |
| 1843 | .name = "qseecom", |
| 1844 | .owner = THIS_MODULE, |
Ramesh Masavarapu | fb1f01e | 2012-06-14 09:40:40 -0700 | [diff] [blame] | 1845 | .of_match_table = qseecom_match, |
Ramesh Masavarapu | a26cce7 | 2012-04-09 12:32:25 -0700 | [diff] [blame] | 1846 | }, |
| 1847 | }; |
| 1848 | |
| 1849 | static int __devinit qseecom_init(void) |
| 1850 | { |
| 1851 | return platform_driver_register(&qseecom_plat_driver); |
| 1852 | } |
| 1853 | |
| 1854 | static void __devexit qseecom_exit(void) |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1855 | { |
Ramesh Masavarapu | a1bc0e4 | 2012-03-05 07:42:48 -0800 | [diff] [blame] | 1856 | clk_put(qseecom_bus_clk); |
| 1857 | |
Mona Hossain | 2892b6b | 2012-02-17 13:53:11 -0800 | [diff] [blame] | 1858 | device_destroy(driver_class, qseecom_device_no); |
| 1859 | class_destroy(driver_class); |
| 1860 | unregister_chrdev_region(qseecom_device_no, 1); |
| 1861 | ion_client_destroy(qseecom.ion_clnt); |
| 1862 | } |
| 1863 | |
| 1864 | MODULE_LICENSE("GPL v2"); |
| 1865 | MODULE_DESCRIPTION("Qualcomm Secure Execution Environment Communicator"); |
| 1866 | |
| 1867 | module_init(qseecom_init); |
| 1868 | module_exit(qseecom_exit); |