blob: 057e4e1b2ff56c59b97be01f7efbb799547273a0 [file] [log] [blame]
Mona Hossaina5f1aab2012-03-29 10:18:07 -07001
Mona Hossainacea1022012-04-09 13:37:27 -07002
Mona Hossaind44a3842012-10-15 09:41:35 -07003/*Qualcomm Secure Execution Environment Communicator (QSEECOM) driver
Mona Hossain2892b6b2012-02-17 13:53:11 -08004 *
Hariprasad Dhalinarasimhaa3b96442013-01-02 10:40:40 -08005 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Mona Hossain2892b6b2012-02-17 13:53:11 -08006 *
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 Humpherys4f8be2e2012-09-06 10:41:41 -070031#include <linux/msm_ion.h>
Mona Hossain2892b6b2012-02-17 13:53:11 -080032#include <linux/types.h>
33#include <linux/clk.h>
34#include <linux/qseecom.h>
Mona Hossaind44a3842012-10-15 09:41:35 -070035#include <linux/elf.h>
36#include <linux/firmware.h>
Mona Hossainacea1022012-04-09 13:37:27 -070037#include <linux/freezer.h>
Mona Hossainf1f2ed62012-11-15 19:51:33 -080038#include <linux/scatterlist.h>
Ramesh Masavarapua26cce72012-04-09 12:32:25 -070039#include <mach/board.h>
Mona Hossain2892b6b2012-02-17 13:53:11 -080040#include <mach/msm_bus.h>
41#include <mach/msm_bus_board.h>
42#include <mach/scm.h>
Stephen Boyd77db8bb2012-06-27 15:15:16 -070043#include <mach/subsystem_restart.h>
Ramesh Masavarapuff377032012-09-14 12:11:32 -070044#include <mach/socinfo.h>
Mona Hossain2892b6b2012-02-17 13:53:11 -080045#include "qseecom_legacy.h"
Mona Hossaind44a3842012-10-15 09:41:35 -070046#include "qseecom_kernel.h"
Mona Hossain2892b6b2012-02-17 13:53:11 -080047
48#define QSEECOM_DEV "qseecom"
49#define QSEOS_VERSION_13 0x13
50#define QSEOS_VERSION_14 0x14
Mona Hossain05c73562012-10-29 17:49:01 -070051#define QSEEE_VERSION_00 0x400000
Mona Hossain5b76a622012-11-15 20:09:08 -080052#define QSEE_VERSION_01 0x401000
53#define QSEE_VERSION_02 0x402000
54
Mona Hossain05c73562012-10-29 17:49:01 -070055
56#define QSEOS_CHECK_VERSION_CMD 0x00001803
Mona Hossain2892b6b2012-02-17 13:53:11 -080057
Mona Hossaind39e33b2012-11-05 13:36:40 -080058#define QSEE_CE_CLK_100MHZ 100000000
59#define QSEE_CE_CLK_50MHZ 50000000
60
Mona Hossainf1f2ed62012-11-15 19:51:33 -080061#define QSEECOM_MAX_SG_ENTRY 10
62
Mona Hossain2892b6b2012-02-17 13:53:11 -080063enum qseecom_command_scm_resp_type {
64 QSEOS_APP_ID = 0xEE01,
65 QSEOS_LISTENER_ID
66};
67
68enum qseecom_qceos_cmd_id {
69 QSEOS_APP_START_COMMAND = 0x01,
70 QSEOS_APP_SHUTDOWN_COMMAND,
71 QSEOS_APP_LOOKUP_COMMAND,
72 QSEOS_REGISTER_LISTENER,
73 QSEOS_DEREGISTER_LISTENER,
74 QSEOS_CLIENT_SEND_DATA_COMMAND,
75 QSEOS_LISTENER_DATA_RSP_COMMAND,
Mona Hossain5ab9d772012-04-11 21:00:40 -070076 QSEOS_LOAD_EXTERNAL_ELF_COMMAND,
77 QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND,
Mona Hossain05c73562012-10-29 17:49:01 -070078 QSEOS_GET_APP_STATE_COMMAND,
79 QSEOS_LOAD_SERV_IMAGE_COMMAND,
80 QSEOS_UNLOAD_SERV_IMAGE_COMMAND,
Mona Hossain5b76a622012-11-15 20:09:08 -080081 QSEOS_APP_REGION_NOTIFICATION,
Mona Hossain2892b6b2012-02-17 13:53:11 -080082 QSEOS_CMD_MAX = 0xEFFFFFFF
83};
84
85enum qseecom_qceos_cmd_status {
86 QSEOS_RESULT_SUCCESS = 0,
87 QSEOS_RESULT_INCOMPLETE,
88 QSEOS_RESULT_FAILURE = 0xFFFFFFFF
89};
90
Ramesh Masavarapua26cce72012-04-09 12:32:25 -070091enum qseecom_clk_definitions {
92 CLK_DFAB = 0,
93 CLK_SFPB,
94};
95
Mona Hossain5b76a622012-11-15 20:09:08 -080096__packed struct qsee_apps_region_info_ireq {
97 uint32_t qsee_cmd_id;
98 uint32_t addr;
99 uint32_t size;
100};
101
Mona Hossain2892b6b2012-02-17 13:53:11 -0800102__packed struct qseecom_check_app_ireq {
103 uint32_t qsee_cmd_id;
104 char app_name[MAX_APP_NAME_SIZE];
105};
106
107__packed struct qseecom_load_app_ireq {
108 uint32_t qsee_cmd_id;
109 uint32_t mdt_len; /* Length of the mdt file */
110 uint32_t img_len; /* Length of .bxx and .mdt files */
111 uint32_t phy_addr; /* phy addr of the start of image */
112 char app_name[MAX_APP_NAME_SIZE]; /* application name*/
113};
114
115__packed struct qseecom_unload_app_ireq {
116 uint32_t qsee_cmd_id;
117 uint32_t app_id;
118};
119
Mona Hossain05c73562012-10-29 17:49:01 -0700120__packed struct qseecom_load_lib_image_ireq {
121 uint32_t qsee_cmd_id;
122 uint32_t mdt_len;
123 uint32_t img_len;
124 uint32_t phy_addr;
125};
126
127__packed struct qseecom_unload_lib_image_ireq {
128 uint32_t qsee_cmd_id;
129};
130
Mona Hossain2892b6b2012-02-17 13:53:11 -0800131__packed struct qseecom_register_listener_ireq {
132 uint32_t qsee_cmd_id;
133 uint32_t listener_id;
134 void *sb_ptr;
135 uint32_t sb_len;
136};
137
138__packed struct qseecom_unregister_listener_ireq {
139 uint32_t qsee_cmd_id;
140 uint32_t listener_id;
141};
142
143__packed struct qseecom_client_send_data_ireq {
144 uint32_t qsee_cmd_id;
145 uint32_t app_id;
146 void *req_ptr;
147 uint32_t req_len;
148 void *rsp_ptr; /* First 4 bytes should always be the return status */
149 uint32_t rsp_len;
150};
151
152/* send_data resp */
153__packed struct qseecom_client_listener_data_irsp {
154 uint32_t qsee_cmd_id;
155 uint32_t listener_id;
156};
157
158/*
159 * struct qseecom_command_scm_resp - qseecom response buffer
160 * @cmd_status: value from enum tz_sched_cmd_status
161 * @sb_in_rsp_addr: points to physical location of response
162 * buffer
163 * @sb_in_rsp_len: length of command response
164 */
165__packed struct qseecom_command_scm_resp {
166 uint32_t result;
167 enum qseecom_command_scm_resp_type resp_type;
168 unsigned int data;
169};
170
171static struct class *driver_class;
172static dev_t qseecom_device_no;
173static struct cdev qseecom_cdev;
174
175/* Data structures used in legacy support */
176static void *pil;
177static uint32_t pil_ref_cnt;
178static DEFINE_MUTEX(pil_access_lock);
179
Mona Hossain2892b6b2012-02-17 13:53:11 -0800180static DEFINE_MUTEX(qsee_bw_mutex);
181static DEFINE_MUTEX(app_access_lock);
182
183static int qsee_bw_count;
Ramesh Masavarapua26cce72012-04-09 12:32:25 -0700184static int qsee_sfpb_bw_count;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800185static uint32_t qsee_perf_client;
186
187struct qseecom_registered_listener_list {
188 struct list_head list;
189 struct qseecom_register_listener_req svc;
190 u8 *sb_reg_req;
191 u8 *sb_virt;
192 s32 sb_phys;
193 size_t sb_length;
194 struct ion_handle *ihandle; /* Retrieve phy addr */
195
196 wait_queue_head_t rcv_req_wq;
197 int rcv_req_flag;
198};
199
200struct qseecom_registered_app_list {
201 struct list_head list;
202 u32 app_id;
203 u32 ref_cnt;
204};
205
Mona Hossaind44a3842012-10-15 09:41:35 -0700206struct qseecom_registered_kclient_list {
207 struct list_head list;
208 struct qseecom_handle *handle;
209};
210
Mona Hossain2892b6b2012-02-17 13:53:11 -0800211struct qseecom_control {
212 struct ion_client *ion_clnt; /* Ion client */
213 struct list_head registered_listener_list_head;
214 spinlock_t registered_listener_list_lock;
215
216 struct list_head registered_app_list_head;
217 spinlock_t registered_app_list_lock;
218
Mona Hossaind44a3842012-10-15 09:41:35 -0700219 struct list_head registered_kclient_list_head;
220 spinlock_t registered_kclient_list_lock;
221
Mona Hossain2892b6b2012-02-17 13:53:11 -0800222 wait_queue_head_t send_resp_wq;
223 int send_resp_flag;
224
225 uint32_t qseos_version;
Mona Hossain05c73562012-10-29 17:49:01 -0700226 uint32_t qsee_version;
Ramesh Masavarapuff377032012-09-14 12:11:32 -0700227 struct device *pdev;
Mona Hossain05c73562012-10-29 17:49:01 -0700228 bool commonlib_loaded;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800229};
230
231struct qseecom_client_handle {
232 u32 app_id;
233 u8 *sb_virt;
234 s32 sb_phys;
235 uint32_t user_virt_sb_base;
236 size_t sb_length;
237 struct ion_handle *ihandle; /* Retrieve phy addr */
Mona Hossain04d3fac2012-12-03 10:10:37 -0800238 bool perf_enabled;
239 bool fast_load_enabled;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800240};
241
242struct qseecom_listener_handle {
243 u32 id;
244};
245
246static struct qseecom_control qseecom;
247
248struct qseecom_dev_handle {
249 bool service;
250 union {
251 struct qseecom_client_handle client;
252 struct qseecom_listener_handle listener;
253 };
254 bool released;
255 int abort;
256 wait_queue_head_t abort_wq;
257 atomic_t ioctl_count;
258};
259
Ramesh Masavarapuff377032012-09-14 12:11:32 -0700260struct clk *ce_core_clk;
261struct clk *ce_clk;
262struct clk *ce_core_src_clk;
263struct clk *ce_bus_clk;
264
Mona Hossainf1f2ed62012-11-15 19:51:33 -0800265struct qseecom_sg_entry {
266 uint32_t phys_addr;
267 uint32_t len;
268};
269
Ramesh Masavarapua26cce72012-04-09 12:32:25 -0700270/* Function proto types */
Mona Hossain04d3fac2012-12-03 10:10:37 -0800271static int qsee_vote_for_clock(struct qseecom_dev_handle *, int32_t);
272static void qsee_disable_clock_vote(struct qseecom_dev_handle *, int32_t);
Ramesh Masavarapua26cce72012-04-09 12:32:25 -0700273
Mona Hossain2892b6b2012-02-17 13:53:11 -0800274static int __qseecom_is_svc_unique(struct qseecom_dev_handle *data,
Mona Hossain0af10ab2012-02-28 18:26:41 -0800275 struct qseecom_register_listener_req *svc)
Mona Hossain2892b6b2012-02-17 13:53:11 -0800276{
277 struct qseecom_registered_listener_list *ptr;
278 int unique = 1;
279 unsigned long flags;
280
281 spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
282 list_for_each_entry(ptr, &qseecom.registered_listener_list_head, list) {
Mona Hossain0af10ab2012-02-28 18:26:41 -0800283 if (ptr->svc.listener_id == svc->listener_id) {
Mona Hossain2892b6b2012-02-17 13:53:11 -0800284 pr_err("Service id: %u is already registered\n",
285 ptr->svc.listener_id);
286 unique = 0;
287 break;
288 }
289 }
290 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
291 return unique;
292}
293
294static struct qseecom_registered_listener_list *__qseecom_find_svc(
295 int32_t listener_id)
296{
297 struct qseecom_registered_listener_list *entry = NULL;
298 unsigned long flags;
299
300 spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
301 list_for_each_entry(entry, &qseecom.registered_listener_list_head, list)
302 {
303 if (entry->svc.listener_id == listener_id)
304 break;
305 }
306 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
307 return entry;
308}
309
310static int __qseecom_set_sb_memory(struct qseecom_registered_listener_list *svc,
311 struct qseecom_dev_handle *handle,
312 struct qseecom_register_listener_req *listener)
313{
314 int ret = 0;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800315 struct qseecom_register_listener_ireq req;
316 struct qseecom_command_scm_resp resp;
317 ion_phys_addr_t pa;
318
319 /* Get the handle of the shared fd */
Laura Abbottb14ed962012-01-30 14:18:08 -0800320 svc->ihandle = ion_import_dma_buf(qseecom.ion_clnt,
321 listener->ifd_data_fd);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800322 if (svc->ihandle == NULL) {
323 pr_err("Ion client could not retrieve the handle\n");
324 return -ENOMEM;
325 }
326
327 /* Get the physical address of the ION BUF */
328 ret = ion_phys(qseecom.ion_clnt, svc->ihandle, &pa, &svc->sb_length);
329
330 /* Populate the structure for sending scm call to load image */
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700331 svc->sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, svc->ihandle);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800332 svc->sb_phys = pa;
333
334 if (qseecom.qseos_version == QSEOS_VERSION_14) {
335 req.qsee_cmd_id = QSEOS_REGISTER_LISTENER;
336 req.listener_id = svc->svc.listener_id;
337 req.sb_len = svc->sb_length;
338 req.sb_ptr = (void *)svc->sb_phys;
339
340 resp.result = QSEOS_RESULT_INCOMPLETE;
341
342 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
343 sizeof(req), &resp, sizeof(resp));
344 if (ret) {
345 pr_err("qseecom_scm_call failed with err: %d\n", ret);
346 return -EINVAL;
347 }
348
349 if (resp.result != QSEOS_RESULT_SUCCESS) {
350 pr_err("Error SB registration req: resp.result = %d\n",
351 resp.result);
352 return -EPERM;
353 }
354 } else {
355 struct qseecom_command cmd;
356 struct qseecom_response resp;
357 struct qse_pr_init_sb_req_s sb_init_req;
358 struct qse_pr_init_sb_rsp_s sb_init_rsp;
359
360 svc->sb_reg_req = kzalloc((sizeof(sb_init_req) +
361 sizeof(sb_init_rsp)), GFP_KERNEL);
362
363 sb_init_req.pr_cmd = TZ_SCHED_CMD_ID_REGISTER_LISTENER;
364 sb_init_req.listener_id = svc->svc.listener_id;
365 sb_init_req.sb_len = svc->sb_length;
366 sb_init_req.sb_ptr = svc->sb_phys;
367
368 memcpy(svc->sb_reg_req, &sb_init_req, sizeof(sb_init_req));
369
370 /* It will always be a new cmd from this method */
371 cmd.cmd_type = TZ_SCHED_CMD_NEW;
372 cmd.sb_in_cmd_addr = (u8 *)(virt_to_phys(svc->sb_reg_req));
373 cmd.sb_in_cmd_len = sizeof(sb_init_req);
374
375 resp.cmd_status = TZ_SCHED_STATUS_INCOMPLETE;
376
377 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &cmd, sizeof(cmd)
378 , &resp, sizeof(resp));
379
380 if (ret) {
381 pr_err("qseecom_scm_call failed with err: %d\n", ret);
382 return -EINVAL;
383 }
384
385 if (resp.cmd_status != TZ_SCHED_STATUS_COMPLETE) {
386 pr_err("SB registration fail resp.cmd_status %d\n",
387 resp.cmd_status);
388 return -EINVAL;
389 }
390 memset(svc->sb_virt, 0, svc->sb_length);
391 }
392 return 0;
393}
394
395static int qseecom_register_listener(struct qseecom_dev_handle *data,
396 void __user *argp)
397{
398 int ret = 0;
399 unsigned long flags;
400 struct qseecom_register_listener_req rcvd_lstnr;
401 struct qseecom_registered_listener_list *new_entry;
402
403 ret = copy_from_user(&rcvd_lstnr, argp, sizeof(rcvd_lstnr));
404 if (ret) {
405 pr_err("copy_from_user failed\n");
406 return ret;
407 }
Mona Hossain0af10ab2012-02-28 18:26:41 -0800408 data->listener.id = 0;
409 data->service = true;
410 if (!__qseecom_is_svc_unique(data, &rcvd_lstnr)) {
Mona Hossain2892b6b2012-02-17 13:53:11 -0800411 pr_err("Service is not unique and is already registered\n");
Mona Hossain0af10ab2012-02-28 18:26:41 -0800412 data->released = true;
413 return -EBUSY;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800414 }
415
416 new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL);
417 if (!new_entry) {
418 pr_err("kmalloc failed\n");
419 return -ENOMEM;
420 }
421 memcpy(&new_entry->svc, &rcvd_lstnr, sizeof(rcvd_lstnr));
422 new_entry->rcv_req_flag = 0;
423
424 new_entry->svc.listener_id = rcvd_lstnr.listener_id;
425 new_entry->sb_length = rcvd_lstnr.sb_size;
426 if (__qseecom_set_sb_memory(new_entry, data, &rcvd_lstnr)) {
427 pr_err("qseecom_set_sb_memoryfailed\n");
428 kzfree(new_entry);
429 return -ENOMEM;
430 }
Mona Hossain0af10ab2012-02-28 18:26:41 -0800431
Mona Hossain2892b6b2012-02-17 13:53:11 -0800432 data->listener.id = rcvd_lstnr.listener_id;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800433 init_waitqueue_head(&new_entry->rcv_req_wq);
434
435 spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
436 list_add_tail(&new_entry->list, &qseecom.registered_listener_list_head);
437 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
Mona Hossain0af10ab2012-02-28 18:26:41 -0800438
Mona Hossain2892b6b2012-02-17 13:53:11 -0800439 return ret;
440}
441
442static int qseecom_unregister_listener(struct qseecom_dev_handle *data)
443{
444 int ret = 0;
445 unsigned long flags;
446 uint32_t unmap_mem = 0;
447 struct qseecom_register_listener_ireq req;
448 struct qseecom_registered_listener_list *ptr_svc = NULL;
449 struct qseecom_command_scm_resp resp;
450 struct ion_handle *ihandle = NULL; /* Retrieve phy addr */
451
452 if (qseecom.qseos_version == QSEOS_VERSION_14) {
453 req.qsee_cmd_id = QSEOS_DEREGISTER_LISTENER;
454 req.listener_id = data->listener.id;
455 resp.result = QSEOS_RESULT_INCOMPLETE;
456
457 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
458 sizeof(req), &resp, sizeof(resp));
459 if (ret) {
460 pr_err("qseecom_scm_call failed with err: %d\n", ret);
461 return ret;
462 }
463
464 if (resp.result != QSEOS_RESULT_SUCCESS) {
465 pr_err("SB deregistartion: result=%d\n", resp.result);
466 return -EPERM;
467 }
468 } else {
469 struct qse_pr_init_sb_req_s sb_init_req;
470 struct qseecom_command cmd;
471 struct qseecom_response resp;
472 struct qseecom_registered_listener_list *svc;
473
474 svc = __qseecom_find_svc(data->listener.id);
475 sb_init_req.pr_cmd = TZ_SCHED_CMD_ID_REGISTER_LISTENER;
476 sb_init_req.listener_id = data->listener.id;
477 sb_init_req.sb_len = 0;
478 sb_init_req.sb_ptr = 0;
479
480 memcpy(svc->sb_reg_req, &sb_init_req, sizeof(sb_init_req));
481
482 /* It will always be a new cmd from this method */
483 cmd.cmd_type = TZ_SCHED_CMD_NEW;
484 cmd.sb_in_cmd_addr = (u8 *)(virt_to_phys(svc->sb_reg_req));
485 cmd.sb_in_cmd_len = sizeof(sb_init_req);
486 resp.cmd_status = TZ_SCHED_STATUS_INCOMPLETE;
487
488 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &cmd, sizeof(cmd),
489 &resp, sizeof(resp));
490 if (ret) {
491 pr_err("qseecom_scm_call failed with err: %d\n", ret);
492 return ret;
493 }
494 kzfree(svc->sb_reg_req);
495 if (resp.cmd_status != TZ_SCHED_STATUS_COMPLETE) {
496 pr_err("Error with SB initialization\n");
497 return -EPERM;
498 }
499 }
500 data->abort = 1;
501 spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
502 list_for_each_entry(ptr_svc, &qseecom.registered_listener_list_head,
503 list) {
504 if (ptr_svc->svc.listener_id == data->listener.id) {
505 wake_up_all(&ptr_svc->rcv_req_wq);
506 break;
507 }
508 }
509 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
510
511 while (atomic_read(&data->ioctl_count) > 1) {
Mona Hossainacea1022012-04-09 13:37:27 -0700512 if (wait_event_freezable(data->abort_wq,
Mona Hossain2892b6b2012-02-17 13:53:11 -0800513 atomic_read(&data->ioctl_count) <= 1)) {
514 pr_err("Interrupted from abort\n");
515 ret = -ERESTARTSYS;
516 break;
517 }
518 }
519
520 spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
521 list_for_each_entry(ptr_svc,
522 &qseecom.registered_listener_list_head,
523 list)
524 {
525 if (ptr_svc->svc.listener_id == data->listener.id) {
526 if (ptr_svc->sb_virt) {
527 unmap_mem = 1;
528 ihandle = ptr_svc->ihandle;
529 }
530 list_del(&ptr_svc->list);
531 kzfree(ptr_svc);
532 break;
533 }
534 }
535 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
536
537 /* Unmap the memory */
538 if (unmap_mem) {
539 if (!IS_ERR_OR_NULL(ihandle)) {
540 ion_unmap_kernel(qseecom.ion_clnt, ihandle);
541 ion_free(qseecom.ion_clnt, ihandle);
542 }
543 }
544 data->released = true;
545 return ret;
546}
547
548static int qseecom_set_client_mem_param(struct qseecom_dev_handle *data,
549 void __user *argp)
550{
551 ion_phys_addr_t pa;
552 int32_t ret;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800553 struct qseecom_set_sb_mem_param_req req;
554 uint32_t len;
555
556 /* Copy the relevant information needed for loading the image */
557 if (__copy_from_user(&req, (void __user *)argp, sizeof(req)))
558 return -EFAULT;
559
Mona Hossain2892b6b2012-02-17 13:53:11 -0800560 /* Get the handle of the shared fd */
Laura Abbottb14ed962012-01-30 14:18:08 -0800561 data->client.ihandle = ion_import_dma_buf(qseecom.ion_clnt,
562 req.ifd_data_fd);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800563 if (IS_ERR_OR_NULL(data->client.ihandle)) {
564 pr_err("Ion client could not retrieve the handle\n");
565 return -ENOMEM;
566 }
567 /* Get the physical address of the ION BUF */
568 ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len);
569 /* Populate the structure for sending scm call to load image */
570 data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt,
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700571 data->client.ihandle);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800572 data->client.sb_phys = pa;
573 data->client.sb_length = req.sb_len;
574 data->client.user_virt_sb_base = req.virt_sb_base;
575 return 0;
576}
577
Mona Hossain2892b6b2012-02-17 13:53:11 -0800578static int __qseecom_listener_has_sent_rsp(struct qseecom_dev_handle *data)
579{
580 int ret;
581 ret = (qseecom.send_resp_flag != 0);
582 return ret || data->abort;
583}
584
585static int __qseecom_process_incomplete_cmd(struct qseecom_dev_handle *data,
586 struct qseecom_command_scm_resp *resp)
587{
588 int ret = 0;
589 uint32_t lstnr;
590 unsigned long flags;
591 struct qseecom_client_listener_data_irsp send_data_rsp;
592 struct qseecom_registered_listener_list *ptr_svc = NULL;
593
594
595 while (resp->result == QSEOS_RESULT_INCOMPLETE) {
596 lstnr = resp->data;
597 /*
598 * Wake up blocking lsitener service with the lstnr id
599 */
600 spin_lock_irqsave(&qseecom.registered_listener_list_lock,
601 flags);
602 list_for_each_entry(ptr_svc,
603 &qseecom.registered_listener_list_head, list) {
604 if (ptr_svc->svc.listener_id == lstnr) {
605 ptr_svc->rcv_req_flag = 1;
606 wake_up_interruptible(&ptr_svc->rcv_req_wq);
607 break;
608 }
609 }
610 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock,
611 flags);
612 if (ptr_svc->svc.listener_id != lstnr) {
613 pr_warning("Service requested for does on exist\n");
614 return -ERESTARTSYS;
615 }
616 pr_debug("waking up rcv_req_wq and "
617 "waiting for send_resp_wq\n");
Mona Hossainacea1022012-04-09 13:37:27 -0700618 if (wait_event_freezable(qseecom.send_resp_wq,
Mona Hossain2892b6b2012-02-17 13:53:11 -0800619 __qseecom_listener_has_sent_rsp(data))) {
620 pr_warning("Interrupted: exiting send_cmd loop\n");
621 return -ERESTARTSYS;
622 }
623
624 if (data->abort) {
Mona Hossaina5f1aab2012-03-29 10:18:07 -0700625 pr_err("Aborting listener service %d\n",
626 data->listener.id);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800627 return -ENODEV;
628 }
629 qseecom.send_resp_flag = 0;
630 send_data_rsp.qsee_cmd_id = QSEOS_LISTENER_DATA_RSP_COMMAND;
631 send_data_rsp.listener_id = lstnr ;
632
633 ret = scm_call(SCM_SVC_TZSCHEDULER, 1,
634 (const void *)&send_data_rsp,
635 sizeof(send_data_rsp), resp,
636 sizeof(*resp));
637 if (ret) {
638 pr_err("qseecom_scm_call failed with err: %d\n", ret);
639 return ret;
640 }
Mona Hossainbb0bca12012-04-12 11:47:45 -0700641 if (resp->result == QSEOS_RESULT_FAILURE) {
642 pr_err("Response result %d not supported\n",
643 resp->result);
644 return -EINVAL;
645 }
Mona Hossain2892b6b2012-02-17 13:53:11 -0800646 }
647 return ret;
648}
649
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -0700650static int __qseecom_check_app_exists(struct qseecom_check_app_ireq req)
651{
652 int32_t ret;
653 struct qseecom_command_scm_resp resp;
654
655 /* SCM_CALL to check if app_id for the mentioned app exists */
656 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
657 sizeof(struct qseecom_check_app_ireq),
658 &resp, sizeof(resp));
659 if (ret) {
660 pr_err("scm_call to check if app is already loaded failed\n");
661 return -EINVAL;
662 }
663
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700664 if (resp.result == QSEOS_RESULT_FAILURE) {
665 return 0;
666 } else {
667 switch (resp.resp_type) {
668 /*qsee returned listener type response */
669 case QSEOS_LISTENER_ID:
670 pr_err("resp type is of listener type instead of app");
671 return -EINVAL;
672 break;
673 case QSEOS_APP_ID:
674 return resp.data;
675 default:
676 pr_err("invalid resp type (%d) from qsee",
677 resp.resp_type);
678 return -ENODEV;
679 break;
680 }
681 }
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -0700682}
683
Mona Hossain2892b6b2012-02-17 13:53:11 -0800684static int qseecom_load_app(struct qseecom_dev_handle *data, void __user *argp)
685{
686 struct qseecom_registered_app_list *entry = NULL;
687 unsigned long flags = 0;
688 u32 app_id = 0;
689 struct ion_handle *ihandle; /* Ion handle */
690 struct qseecom_load_img_req load_img_req;
691 int32_t ret;
692 ion_phys_addr_t pa = 0;
693 uint32_t len;
694 struct qseecom_command_scm_resp resp;
Mona Hossain436b75f2012-11-20 17:10:40 -0800695 struct qseecom_check_app_ireq req;
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700696 struct qseecom_load_app_ireq load_req;
697
Mona Hossain2892b6b2012-02-17 13:53:11 -0800698 /* Copy the relevant information needed for loading the image */
699 if (__copy_from_user(&load_img_req,
700 (void __user *)argp,
701 sizeof(struct qseecom_load_img_req))) {
702 pr_err("copy_from_user failed\n");
703 return -EFAULT;
704 }
Ramesh Masavarapua26cce72012-04-09 12:32:25 -0700705 /* Vote for the SFPB clock */
Mona Hossain04d3fac2012-12-03 10:10:37 -0800706 ret = qsee_vote_for_clock(data, CLK_SFPB);
Ramesh Masavarapua26cce72012-04-09 12:32:25 -0700707 if (ret)
708 pr_warning("Unable to vote for SFPB clock");
Mona Hossain436b75f2012-11-20 17:10:40 -0800709 req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
710 memcpy(req.app_name, load_img_req.img_name, MAX_APP_NAME_SIZE);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800711
Mona Hossain436b75f2012-11-20 17:10:40 -0800712 ret = __qseecom_check_app_exists(req);
713 if (ret < 0)
714 return ret;
715 else
716 app_id = ret;
717
718 if (app_id) {
719 pr_warn("App id %d (%s) already exists\n", app_id,
720 (char *)(req.app_name));
721 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
722 list_for_each_entry(entry,
723 &qseecom.registered_app_list_head, list){
724 if (entry->app_id == app_id) {
725 entry->ref_cnt++;
726 break;
727 }
728 }
729 spin_unlock_irqrestore(
730 &qseecom.registered_app_list_lock, flags);
731 } else {
732 pr_warn("App (%s) does'nt exist, loading apps for first time\n",
Mona Hossaind44a3842012-10-15 09:41:35 -0700733 (char *)(load_img_req.img_name));
Mona Hossain436b75f2012-11-20 17:10:40 -0800734 /* Get the handle of the shared fd */
735 ihandle = ion_import_dma_buf(qseecom.ion_clnt,
Mona Hossain2892b6b2012-02-17 13:53:11 -0800736 load_img_req.ifd_data_fd);
Mona Hossain436b75f2012-11-20 17:10:40 -0800737 if (IS_ERR_OR_NULL(ihandle)) {
738 pr_err("Ion client could not retrieve the handle\n");
Mona Hossain04d3fac2012-12-03 10:10:37 -0800739 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain436b75f2012-11-20 17:10:40 -0800740 return -ENOMEM;
741 }
Mona Hossain2892b6b2012-02-17 13:53:11 -0800742
Mona Hossain436b75f2012-11-20 17:10:40 -0800743 /* Get the physical address of the ION BUF */
744 ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800745
Mona Hossain436b75f2012-11-20 17:10:40 -0800746 /* Populate the structure for sending scm call to load image */
747 memcpy(load_req.app_name, load_img_req.img_name,
748 MAX_APP_NAME_SIZE);
749 load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND;
750 load_req.mdt_len = load_img_req.mdt_len;
751 load_req.img_len = load_img_req.img_len;
752 load_req.phy_addr = pa;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800753
Mona Hossain436b75f2012-11-20 17:10:40 -0800754 /* SCM_CALL to load the app and get the app_id back */
755 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700756 sizeof(struct qseecom_load_app_ireq),
757 &resp, sizeof(resp));
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700758 if (ret) {
Mona Hossain436b75f2012-11-20 17:10:40 -0800759 pr_err("scm_call to load app failed\n");
760 return -EINVAL;
761 }
762
763 if (resp.result == QSEOS_RESULT_FAILURE) {
764 pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n");
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700765 if (!IS_ERR_OR_NULL(ihandle))
766 ion_free(qseecom.ion_clnt, ihandle);
Mona Hossain04d3fac2012-12-03 10:10:37 -0800767 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain436b75f2012-11-20 17:10:40 -0800768 return -EFAULT;
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700769 }
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700770
Mona Hossain436b75f2012-11-20 17:10:40 -0800771 if (resp.result == QSEOS_RESULT_INCOMPLETE) {
772 ret = __qseecom_process_incomplete_cmd(data, &resp);
773 if (ret) {
774 pr_err("process_incomplete_cmd failed err: %d\n",
775 ret);
776 if (!IS_ERR_OR_NULL(ihandle))
777 ion_free(qseecom.ion_clnt, ihandle);
Mona Hossain04d3fac2012-12-03 10:10:37 -0800778 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain436b75f2012-11-20 17:10:40 -0800779 return ret;
780 }
781 }
782
783 if (resp.result != QSEOS_RESULT_SUCCESS) {
784 pr_err("scm_call failed resp.result unknown, %d\n",
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700785 resp.result);
Mona Hossain436b75f2012-11-20 17:10:40 -0800786 if (!IS_ERR_OR_NULL(ihandle))
787 ion_free(qseecom.ion_clnt, ihandle);
Mona Hossain04d3fac2012-12-03 10:10:37 -0800788 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain436b75f2012-11-20 17:10:40 -0800789 return -EFAULT;
790 }
791
792 app_id = resp.data;
793
794 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
795 if (!entry) {
796 pr_err("kmalloc failed\n");
Mona Hossain04d3fac2012-12-03 10:10:37 -0800797 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain436b75f2012-11-20 17:10:40 -0800798 return -ENOMEM;
799 }
800 entry->app_id = app_id;
801 entry->ref_cnt = 1;
802
803 /* Deallocate the handle */
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700804 if (!IS_ERR_OR_NULL(ihandle))
805 ion_free(qseecom.ion_clnt, ihandle);
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700806
Mona Hossain436b75f2012-11-20 17:10:40 -0800807 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
808 list_add_tail(&entry->list, &qseecom.registered_app_list_head);
809 spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
810 flags);
Ramesh Masavarapu13d99672012-07-17 11:05:15 -0700811
Mona Hossain436b75f2012-11-20 17:10:40 -0800812 pr_warn("App with id %d (%s) now loaded\n", app_id,
Mona Hossaind44a3842012-10-15 09:41:35 -0700813 (char *)(load_img_req.img_name));
Mona Hossain436b75f2012-11-20 17:10:40 -0800814 }
Mona Hossain2892b6b2012-02-17 13:53:11 -0800815 data->client.app_id = app_id;
816 load_img_req.app_id = app_id;
817 if (copy_to_user(argp, &load_img_req, sizeof(load_img_req))) {
818 pr_err("copy_to_user failed\n");
819 kzfree(entry);
Mona Hossain04d3fac2012-12-03 10:10:37 -0800820 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800821 return -EFAULT;
822 }
Mona Hossain04d3fac2012-12-03 10:10:37 -0800823 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800824 return 0;
825}
826
827static int __qseecom_cleanup_app(struct qseecom_dev_handle *data)
828{
829 wake_up_all(&qseecom.send_resp_wq);
830 while (atomic_read(&data->ioctl_count) > 1) {
Mona Hossainacea1022012-04-09 13:37:27 -0700831 if (wait_event_freezable(data->abort_wq,
Mona Hossain2892b6b2012-02-17 13:53:11 -0800832 atomic_read(&data->ioctl_count) <= 1)) {
833 pr_err("Interrupted from abort\n");
834 return -ERESTARTSYS;
835 break;
836 }
837 }
838 /* Set unload app */
839 return 1;
840}
841
842static int qseecom_unload_app(struct qseecom_dev_handle *data)
843{
844 unsigned long flags;
845 int ret = 0;
846 struct qseecom_command_scm_resp resp;
847 struct qseecom_registered_app_list *ptr_app;
Mona Hossain340dba82012-08-07 19:54:46 -0700848 bool unload = false;
849 bool found_app = false;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800850
Mona Hossain1fb538f2012-08-30 16:19:38 -0700851 if ((qseecom.qseos_version == QSEOS_VERSION_14) &&
852 (data->client.app_id > 0)) {
Mona Hossain2892b6b2012-02-17 13:53:11 -0800853 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
854 list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
855 list) {
856 if (ptr_app->app_id == data->client.app_id) {
Mona Hossain340dba82012-08-07 19:54:46 -0700857 found_app = true;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800858 if (ptr_app->ref_cnt == 1) {
Mona Hossain340dba82012-08-07 19:54:46 -0700859 unload = true;
Mona Hossain2892b6b2012-02-17 13:53:11 -0800860 break;
861 } else {
862 ptr_app->ref_cnt--;
Mona Hossain1fb538f2012-08-30 16:19:38 -0700863 pr_warn("Can't unload app(%d) inuse\n",
Mona Hossaina5f1aab2012-03-29 10:18:07 -0700864 ptr_app->app_id);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800865 break;
866 }
867 }
868 }
869 spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
870 flags);
Mona Hossain1fb538f2012-08-30 16:19:38 -0700871 if (found_app == false) {
872 pr_err("Cannot find app with id = %d\n",
873 data->client.app_id);
874 return -EINVAL;
875 }
Mona Hossain2892b6b2012-02-17 13:53:11 -0800876 }
877
878 if ((unload) && (qseecom.qseos_version == QSEOS_VERSION_14)) {
879 struct qseecom_unload_app_ireq req;
880
Mona Hossain340dba82012-08-07 19:54:46 -0700881 __qseecom_cleanup_app(data);
882 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
883 list_del(&ptr_app->list);
884 kzfree(ptr_app);
885 spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
886 flags);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800887 /* Populate the structure for sending scm call to load image */
888 req.qsee_cmd_id = QSEOS_APP_SHUTDOWN_COMMAND;
889 req.app_id = data->client.app_id;
890
891 /* SCM_CALL to unload the app */
892 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
893 sizeof(struct qseecom_unload_app_ireq),
894 &resp, sizeof(resp));
895 if (ret) {
Mona Hossainbb0bca12012-04-12 11:47:45 -0700896 pr_err("scm_call to unload app (id = %d) failed\n",
897 req.app_id);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800898 return -EFAULT;
Mona Hossainbb0bca12012-04-12 11:47:45 -0700899 } else {
900 pr_warn("App id %d now unloaded\n", req.app_id);
Mona Hossain2892b6b2012-02-17 13:53:11 -0800901 }
902 if (resp.result == QSEOS_RESULT_INCOMPLETE) {
903 ret = __qseecom_process_incomplete_cmd(data, &resp);
904 if (ret) {
905 pr_err("process_incomplete_cmd fail err: %d\n",
906 ret);
907 return ret;
908 }
909 }
910 }
911
912 if (qseecom.qseos_version == QSEOS_VERSION_13) {
913 data->abort = 1;
914 wake_up_all(&qseecom.send_resp_wq);
915 while (atomic_read(&data->ioctl_count) > 0) {
Mona Hossainacea1022012-04-09 13:37:27 -0700916 if (wait_event_freezable(data->abort_wq,
Mona Hossain2892b6b2012-02-17 13:53:11 -0800917 atomic_read(&data->ioctl_count) <= 0)) {
918 pr_err("Interrupted from abort\n");
919 ret = -ERESTARTSYS;
920 break;
921 }
922 }
Mona Hossain2892b6b2012-02-17 13:53:11 -0800923 }
Mona Hossain340dba82012-08-07 19:54:46 -0700924 if (!IS_ERR_OR_NULL(data->client.ihandle)) {
925 ion_unmap_kernel(qseecom.ion_clnt, data->client.ihandle);
926 ion_free(qseecom.ion_clnt, data->client.ihandle);
927 data->client.ihandle = NULL;
928 }
Mona Hossain2892b6b2012-02-17 13:53:11 -0800929 data->released = true;
930 return ret;
931}
932
933static uint32_t __qseecom_uvirt_to_kphys(struct qseecom_dev_handle *data,
934 uint32_t virt)
935{
936 return data->client.sb_phys + (virt - data->client.user_virt_sb_base);
937}
938
939static int __qseecom_send_cmd_legacy(struct qseecom_dev_handle *data,
940 struct qseecom_send_cmd_req *req)
941{
942 int ret = 0;
943 unsigned long flags;
944 u32 reqd_len_sb_in = 0;
945 struct qseecom_command cmd;
946 struct qseecom_response resp;
947
948
949 if (req->cmd_req_buf == NULL || req->resp_buf == NULL) {
950 pr_err("cmd buffer or response buffer is null\n");
951 return -EINVAL;
952 }
953
954 if (req->cmd_req_len <= 0 ||
955 req->resp_len <= 0 ||
956 req->cmd_req_len > data->client.sb_length ||
957 req->resp_len > data->client.sb_length) {
958 pr_err("cmd buffer length or "
959 "response buffer length not valid\n");
960 return -EINVAL;
961 }
962
963 reqd_len_sb_in = req->cmd_req_len + req->resp_len;
964 if (reqd_len_sb_in > data->client.sb_length) {
965 pr_debug("Not enough memory to fit cmd_buf and "
966 "resp_buf. Required: %u, Available: %u\n",
967 reqd_len_sb_in, data->client.sb_length);
968 return -ENOMEM;
969 }
970 cmd.cmd_type = TZ_SCHED_CMD_NEW;
971 cmd.sb_in_cmd_addr = (u8 *) data->client.sb_phys;
972 cmd.sb_in_cmd_len = req->cmd_req_len;
973
974 resp.cmd_status = TZ_SCHED_STATUS_INCOMPLETE;
975 resp.sb_in_rsp_addr = (u8 *)data->client.sb_phys + req->cmd_req_len;
976 resp.sb_in_rsp_len = req->resp_len;
977
978 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *)&cmd,
979 sizeof(cmd), &resp, sizeof(resp));
980
981 if (ret) {
982 pr_err("qseecom_scm_call_legacy failed with err: %d\n", ret);
983 return ret;
984 }
985
986 while (resp.cmd_status != TZ_SCHED_STATUS_COMPLETE) {
987 /*
988 * If cmd is incomplete, get the callback cmd out from SB out
989 * and put it on the list
990 */
991 struct qseecom_registered_listener_list *ptr_svc = NULL;
992 /*
993 * We don't know which service can handle the command. so we
994 * wake up all blocking services and let them figure out if
995 * they can handle the given command.
996 */
997 spin_lock_irqsave(&qseecom.registered_listener_list_lock,
998 flags);
999 list_for_each_entry(ptr_svc,
1000 &qseecom.registered_listener_list_head, list) {
1001 ptr_svc->rcv_req_flag = 1;
1002 wake_up_interruptible(&ptr_svc->rcv_req_wq);
1003 }
1004 spin_unlock_irqrestore(&qseecom.registered_listener_list_lock,
1005 flags);
1006
1007 pr_debug("waking up rcv_req_wq and "
1008 "waiting for send_resp_wq\n");
Mona Hossainacea1022012-04-09 13:37:27 -07001009 if (wait_event_freezable(qseecom.send_resp_wq,
Mona Hossain2892b6b2012-02-17 13:53:11 -08001010 __qseecom_listener_has_sent_rsp(data))) {
1011 pr_warning("qseecom Interrupted: exiting send_cmd loop\n");
1012 return -ERESTARTSYS;
1013 }
1014
1015 if (data->abort) {
1016 pr_err("Aborting driver\n");
1017 return -ENODEV;
1018 }
1019 qseecom.send_resp_flag = 0;
1020 cmd.cmd_type = TZ_SCHED_CMD_PENDING;
1021 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *)&cmd,
1022 sizeof(cmd), &resp, sizeof(resp));
1023 if (ret) {
1024 pr_err("qseecom_scm_call failed with err: %d\n", ret);
1025 return ret;
1026 }
1027 }
1028 return ret;
1029}
1030
1031static int __qseecom_send_cmd(struct qseecom_dev_handle *data,
1032 struct qseecom_send_cmd_req *req)
1033{
1034 int ret = 0;
1035 u32 reqd_len_sb_in = 0;
1036 struct qseecom_client_send_data_ireq send_data_req;
1037 struct qseecom_command_scm_resp resp;
1038
1039 if (req->cmd_req_buf == NULL || req->resp_buf == NULL) {
1040 pr_err("cmd buffer or response buffer is null\n");
1041 return -EINVAL;
1042 }
1043
1044 if (req->cmd_req_len <= 0 ||
1045 req->resp_len <= 0 ||
1046 req->cmd_req_len > data->client.sb_length ||
1047 req->resp_len > data->client.sb_length) {
1048 pr_err("cmd buffer length or "
1049 "response buffer length not valid\n");
1050 return -EINVAL;
1051 }
1052
1053 reqd_len_sb_in = req->cmd_req_len + req->resp_len;
1054 if (reqd_len_sb_in > data->client.sb_length) {
1055 pr_debug("Not enough memory to fit cmd_buf and "
1056 "resp_buf. Required: %u, Available: %u\n",
1057 reqd_len_sb_in, data->client.sb_length);
1058 return -ENOMEM;
1059 }
1060
1061 send_data_req.qsee_cmd_id = QSEOS_CLIENT_SEND_DATA_COMMAND;
1062 send_data_req.app_id = data->client.app_id;
1063 send_data_req.req_ptr = (void *)(__qseecom_uvirt_to_kphys(data,
1064 (uint32_t)req->cmd_req_buf));
1065 send_data_req.req_len = req->cmd_req_len;
1066 send_data_req.rsp_ptr = (void *)(__qseecom_uvirt_to_kphys(data,
1067 (uint32_t)req->resp_buf));
1068 send_data_req.rsp_len = req->resp_len;
1069
1070 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *) &send_data_req,
1071 sizeof(send_data_req),
1072 &resp, sizeof(resp));
1073 if (ret) {
1074 pr_err("qseecom_scm_call failed with err: %d\n", ret);
1075 return ret;
1076 }
1077
1078 if (resp.result == QSEOS_RESULT_INCOMPLETE) {
1079 ret = __qseecom_process_incomplete_cmd(data, &resp);
1080 if (ret) {
1081 pr_err("process_incomplete_cmd failed err: %d\n", ret);
1082 return ret;
1083 }
Mona Hossainbb0bca12012-04-12 11:47:45 -07001084 } else {
1085 if (resp.result != QSEOS_RESULT_SUCCESS) {
1086 pr_err("Response result %d not supported\n",
1087 resp.result);
1088 ret = -EINVAL;
1089 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08001090 }
1091 return ret;
1092}
1093
1094
1095static int qseecom_send_cmd(struct qseecom_dev_handle *data, void __user *argp)
1096{
1097 int ret = 0;
1098 struct qseecom_send_cmd_req req;
1099
1100 ret = copy_from_user(&req, argp, sizeof(req));
1101 if (ret) {
1102 pr_err("copy_from_user failed\n");
1103 return ret;
1104 }
1105 if (qseecom.qseos_version == QSEOS_VERSION_14)
1106 ret = __qseecom_send_cmd(data, &req);
1107 else
1108 ret = __qseecom_send_cmd_legacy(data, &req);
1109 if (ret)
1110 return ret;
1111
1112 pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%p\n",
1113 req.resp_len, req.resp_buf);
1114 return ret;
1115}
1116
1117static int __qseecom_send_cmd_req_clean_up(
1118 struct qseecom_send_modfd_cmd_req *req)
1119{
1120 char *field;
1121 uint32_t *update;
1122 int ret = 0;
1123 int i = 0;
1124
1125 for (i = 0; i < MAX_ION_FD; i++) {
Mona Hossaina5f1aab2012-03-29 10:18:07 -07001126 if (req->ifd_data[i].fd > 0) {
Mona Hossain2892b6b2012-02-17 13:53:11 -08001127 field = (char *)req->cmd_req_buf +
1128 req->ifd_data[i].cmd_buf_offset;
1129 update = (uint32_t *) field;
1130 *update = 0;
1131 }
1132 }
1133 return ret;
1134}
1135
1136static int __qseecom_update_with_phy_addr(
1137 struct qseecom_send_modfd_cmd_req *req)
1138{
1139 struct ion_handle *ihandle;
1140 char *field;
Mona Hossain2892b6b2012-02-17 13:53:11 -08001141 int ret = 0;
1142 int i = 0;
Mona Hossain2892b6b2012-02-17 13:53:11 -08001143
1144 for (i = 0; i < MAX_ION_FD; i++) {
Mona Hossainf1f2ed62012-11-15 19:51:33 -08001145 struct sg_table *sg_ptr = NULL;
Mona Hossaina5f1aab2012-03-29 10:18:07 -07001146 if (req->ifd_data[i].fd > 0) {
Mona Hossain2892b6b2012-02-17 13:53:11 -08001147 /* Get the handle of the shared fd */
Laura Abbottb14ed962012-01-30 14:18:08 -08001148 ihandle = ion_import_dma_buf(qseecom.ion_clnt,
Mona Hossain2892b6b2012-02-17 13:53:11 -08001149 req->ifd_data[i].fd);
1150 if (IS_ERR_OR_NULL(ihandle)) {
1151 pr_err("Ion client can't retrieve the handle\n");
1152 return -ENOMEM;
1153 }
1154 field = (char *) req->cmd_req_buf +
1155 req->ifd_data[i].cmd_buf_offset;
Mona Hossain2892b6b2012-02-17 13:53:11 -08001156
1157 /* Populate the cmd data structure with the phys_addr */
Mona Hossainf1f2ed62012-11-15 19:51:33 -08001158 sg_ptr = ion_sg_table(qseecom.ion_clnt, ihandle);
1159 if (sg_ptr == NULL) {
1160 pr_err("IOn client could not retrieve sg table\n");
1161 goto err;
1162 }
1163 if (sg_ptr->nents == 0) {
1164 pr_err("Num of scattered entries is 0\n");
1165 goto err;
1166 }
1167 if (sg_ptr->nents > QSEECOM_MAX_SG_ENTRY) {
1168 pr_err("Num of scattered entries");
1169 pr_err(" (%d) is greater than max supported %d\n",
1170 sg_ptr->nents, QSEECOM_MAX_SG_ENTRY);
1171 goto err;
1172 }
1173 if (sg_ptr->nents == 1) {
1174 uint32_t *update;
1175 update = (uint32_t *) field;
1176 *update = (uint32_t)sg_dma_address(sg_ptr->sgl);
1177 } else {
1178 struct qseecom_sg_entry *update;
1179 struct scatterlist *sg;
1180 int j = 0;
1181 update = (struct qseecom_sg_entry *) field;
1182 sg = sg_ptr->sgl;
1183 for (j = 0; j < sg_ptr->nents; j++) {
1184 update->phys_addr = (uint32_t)
1185 sg_dma_address(sg);
1186 update->len = (uint32_t)sg->length;
1187 update++;
1188 sg = sg_next(sg);
1189 }
1190 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08001191 /* Deallocate the handle */
1192 if (!IS_ERR_OR_NULL(ihandle))
1193 ion_free(qseecom.ion_clnt, ihandle);
1194 }
1195 }
1196 return ret;
Mona Hossainf1f2ed62012-11-15 19:51:33 -08001197err:
1198 if (!IS_ERR_OR_NULL(ihandle))
1199 ion_free(qseecom.ion_clnt, ihandle);
1200 return -ENOMEM;
Mona Hossain2892b6b2012-02-17 13:53:11 -08001201}
1202
1203static int qseecom_send_modfd_cmd(struct qseecom_dev_handle *data,
1204 void __user *argp)
1205{
1206 int ret = 0;
1207 struct qseecom_send_modfd_cmd_req req;
1208 struct qseecom_send_cmd_req send_cmd_req;
1209
1210 ret = copy_from_user(&req, argp, sizeof(req));
1211 if (ret) {
1212 pr_err("copy_from_user failed\n");
1213 return ret;
1214 }
1215 send_cmd_req.cmd_req_buf = req.cmd_req_buf;
1216 send_cmd_req.cmd_req_len = req.cmd_req_len;
1217 send_cmd_req.resp_buf = req.resp_buf;
1218 send_cmd_req.resp_len = req.resp_len;
1219
1220 ret = __qseecom_update_with_phy_addr(&req);
1221 if (ret)
1222 return ret;
1223 if (qseecom.qseos_version == QSEOS_VERSION_14)
1224 ret = __qseecom_send_cmd(data, &send_cmd_req);
1225 else
1226 ret = __qseecom_send_cmd_legacy(data, &send_cmd_req);
1227 __qseecom_send_cmd_req_clean_up(&req);
1228
1229 if (ret)
1230 return ret;
1231
1232 pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%p\n",
1233 req.resp_len, req.resp_buf);
1234 return ret;
1235}
1236
1237static int __qseecom_listener_has_rcvd_req(struct qseecom_dev_handle *data,
1238 struct qseecom_registered_listener_list *svc)
1239{
1240 int ret;
1241 ret = (svc->rcv_req_flag != 0);
1242 return ret || data->abort;
1243}
1244
1245static int qseecom_receive_req(struct qseecom_dev_handle *data)
1246{
1247 int ret = 0;
1248 struct qseecom_registered_listener_list *this_lstnr;
1249
1250 this_lstnr = __qseecom_find_svc(data->listener.id);
1251 while (1) {
Mona Hossainacea1022012-04-09 13:37:27 -07001252 if (wait_event_freezable(this_lstnr->rcv_req_wq,
Mona Hossain2892b6b2012-02-17 13:53:11 -08001253 __qseecom_listener_has_rcvd_req(data,
1254 this_lstnr))) {
1255 pr_warning("Interrupted: exiting wait_rcv_req loop\n");
1256 /* woken up for different reason */
1257 return -ERESTARTSYS;
1258 }
1259
1260 if (data->abort) {
1261 pr_err("Aborting driver!\n");
1262 return -ENODEV;
1263 }
1264 this_lstnr->rcv_req_flag = 0;
1265 if (qseecom.qseos_version == QSEOS_VERSION_13) {
1266 if (*((uint32_t *)this_lstnr->sb_virt) != 0)
1267 break;
1268 } else {
1269 break;
1270 }
1271 }
1272 return ret;
1273}
1274
Mona Hossaind44a3842012-10-15 09:41:35 -07001275static bool __qseecom_is_fw_image_valid(const struct firmware *fw_entry)
1276{
1277 struct elf32_hdr *ehdr;
1278
1279 if (fw_entry->size < sizeof(*ehdr)) {
1280 pr_err("%s: Not big enough to be an elf header\n",
1281 qseecom.pdev->init_name);
1282 return false;
1283 }
1284 ehdr = (struct elf32_hdr *)fw_entry->data;
1285 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
1286 pr_err("%s: Not an elf header\n",
1287 qseecom.pdev->init_name);
1288 return false;
1289 }
1290
1291 if (ehdr->e_phnum == 0) {
1292 pr_err("%s: No loadable segments\n",
1293 qseecom.pdev->init_name);
1294 return false;
1295 }
1296 if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
1297 sizeof(struct elf32_hdr) > fw_entry->size) {
1298 pr_err("%s: Program headers not within mdt\n",
1299 qseecom.pdev->init_name);
1300 return false;
1301 }
1302 return true;
1303}
1304
1305static int __qseecom_get_fw_size(char *appname, uint32_t *fw_size)
1306{
1307 int ret = -1;
1308 int i = 0, rc = 0;
1309 const struct firmware *fw_entry = NULL;
1310 struct elf32_phdr *phdr;
1311 char fw_name[MAX_APP_NAME_SIZE];
1312 struct elf32_hdr *ehdr;
1313 int num_images = 0;
1314
1315 snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname);
1316 rc = request_firmware(&fw_entry, fw_name, qseecom.pdev);
1317 if (rc) {
1318 pr_err("error with request_firmware\n");
1319 ret = -EIO;
1320 goto err;
1321 }
1322 if (!__qseecom_is_fw_image_valid(fw_entry)) {
1323 ret = -EIO;
1324 goto err;
1325 }
1326 *fw_size = fw_entry->size;
1327 phdr = (struct elf32_phdr *)(fw_entry->data + sizeof(struct elf32_hdr));
1328 ehdr = (struct elf32_hdr *)fw_entry->data;
1329 num_images = ehdr->e_phnum;
1330 release_firmware(fw_entry);
1331 for (i = 0; i < num_images; i++, phdr++) {
1332 memset(fw_name, 0, sizeof(fw_name));
1333 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i);
1334 ret = request_firmware(&fw_entry, fw_name, qseecom.pdev);
1335 if (ret)
1336 goto err;
1337 *fw_size += fw_entry->size;
1338 release_firmware(fw_entry);
1339 }
1340 return ret;
1341err:
1342 if (fw_entry)
1343 release_firmware(fw_entry);
1344 *fw_size = 0;
1345 return ret;
1346}
1347
1348static int __qseecom_get_fw_data(char *appname, u8 *img_data,
1349 struct qseecom_load_app_ireq *load_req)
1350{
1351 int ret = -1;
1352 int i = 0, rc = 0;
1353 const struct firmware *fw_entry = NULL;
1354 char fw_name[MAX_APP_NAME_SIZE];
1355 u8 *img_data_ptr = img_data;
1356 struct elf32_hdr *ehdr;
1357 int num_images = 0;
1358
1359 snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname);
1360 rc = request_firmware(&fw_entry, fw_name, qseecom.pdev);
1361 if (rc) {
1362 ret = -EIO;
1363 goto err;
1364 }
1365 load_req->img_len = fw_entry->size;
1366 memcpy(img_data_ptr, fw_entry->data, fw_entry->size);
1367 img_data_ptr = img_data_ptr + fw_entry->size;
1368 load_req->mdt_len = fw_entry->size; /*Get MDT LEN*/
1369 ehdr = (struct elf32_hdr *)fw_entry->data;
1370 num_images = ehdr->e_phnum;
1371 release_firmware(fw_entry);
1372 for (i = 0; i < num_images; i++) {
1373 snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i);
1374 ret = request_firmware(&fw_entry, fw_name, qseecom.pdev);
1375 if (ret) {
1376 pr_err("Failed to locate blob %s\n", fw_name);
1377 goto err;
1378 }
1379 memcpy(img_data_ptr, fw_entry->data, fw_entry->size);
1380 img_data_ptr = img_data_ptr + fw_entry->size;
1381 load_req->img_len += fw_entry->size;
1382 release_firmware(fw_entry);
1383 }
1384 load_req->phy_addr = virt_to_phys(img_data);
1385 return ret;
1386err:
1387 release_firmware(fw_entry);
1388 return ret;
1389}
1390
1391static int __qseecom_load_fw(struct qseecom_dev_handle *data, char *appname)
1392{
1393 int ret = -1;
1394 uint32_t fw_size = 0;
1395 struct qseecom_load_app_ireq load_req = {0, 0, 0, 0};
1396 struct qseecom_command_scm_resp resp;
1397 u8 *img_data = NULL;
1398
1399 if (__qseecom_get_fw_size(appname, &fw_size))
1400 return -EIO;
1401
1402 img_data = kzalloc(fw_size, GFP_KERNEL);
1403 if (!img_data) {
1404 pr_err("Failied to allocate memory for copying image data\n");
1405 return -ENOMEM;
1406 }
1407 ret = __qseecom_get_fw_data(appname, img_data, &load_req);
1408 if (ret) {
1409 kzfree(img_data);
1410 return -EIO;
1411 }
1412
1413 /* Populate the remaining parameters */
1414 load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND;
1415 memcpy(load_req.app_name, appname, MAX_APP_NAME_SIZE);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001416 ret = qsee_vote_for_clock(data, CLK_SFPB);
Mona Hossain60f9fb02012-11-05 13:51:50 -08001417 if (ret) {
1418 kzfree(img_data);
1419 pr_warning("Unable to vote for SFPB clock");
Mona Hossain60f9fb02012-11-05 13:51:50 -08001420 return -EIO;
1421 }
1422
Mona Hossaind44a3842012-10-15 09:41:35 -07001423 /* SCM_CALL to load the image */
1424 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
1425 sizeof(struct qseecom_load_app_ireq),
1426 &resp, sizeof(resp));
1427 kzfree(img_data);
1428 if (ret) {
1429 pr_err("scm_call to load failed : ret %d\n", ret);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001430 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossaind44a3842012-10-15 09:41:35 -07001431 return -EIO;
1432 }
1433
1434 switch (resp.result) {
1435 case QSEOS_RESULT_SUCCESS:
1436 ret = resp.data;
1437 break;
1438 case QSEOS_RESULT_INCOMPLETE:
1439 ret = __qseecom_process_incomplete_cmd(data, &resp);
1440 if (ret)
1441 pr_err("process_incomplete_cmd FAILED\n");
1442 else
1443 ret = resp.data;
1444 break;
1445 case QSEOS_RESULT_FAILURE:
1446 pr_err("scm call failed with response QSEOS_RESULT FAILURE\n");
1447 break;
1448 default:
1449 pr_err("scm call return unknown response %d\n", resp.result);
1450 ret = -EINVAL;
1451 break;
1452 }
Mona Hossain04d3fac2012-12-03 10:10:37 -08001453 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain60f9fb02012-11-05 13:51:50 -08001454
Mona Hossaind44a3842012-10-15 09:41:35 -07001455 return ret;
1456}
1457
Mona Hossain05c73562012-10-29 17:49:01 -07001458static int qseecom_load_commonlib_image(void)
1459{
1460 int32_t ret = 0;
1461 uint32_t fw_size = 0;
1462 struct qseecom_load_app_ireq load_req = {0, 0, 0, 0};
1463 struct qseecom_command_scm_resp resp;
1464 u8 *img_data = NULL;
1465
Hariprasad Dhalinarasimhaa3b96442013-01-02 10:40:40 -08001466 if (__qseecom_get_fw_size("cmnlib", &fw_size))
Mona Hossain05c73562012-10-29 17:49:01 -07001467 return -EIO;
1468
1469 img_data = kzalloc(fw_size, GFP_KERNEL);
1470 if (!img_data) {
1471 pr_err("Mem allocation for lib image data failed\n");
1472 return -ENOMEM;
1473 }
Hariprasad Dhalinarasimhaa3b96442013-01-02 10:40:40 -08001474 ret = __qseecom_get_fw_data("cmnlib", img_data, &load_req);
Mona Hossain05c73562012-10-29 17:49:01 -07001475 if (ret) {
1476 kzfree(img_data);
1477 return -EIO;
1478 }
1479 /* Populate the remaining parameters */
1480 load_req.qsee_cmd_id = QSEOS_LOAD_SERV_IMAGE_COMMAND;
1481 /* SCM_CALL to load the image */
1482 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
1483 sizeof(struct qseecom_load_lib_image_ireq),
1484 &resp, sizeof(resp));
1485 kzfree(img_data);
1486 if (ret) {
1487 pr_err("scm_call to load failed : ret %d\n", ret);
1488 ret = -EIO;
1489 } else {
1490 switch (resp.result) {
1491 case QSEOS_RESULT_SUCCESS:
1492 break;
1493 case QSEOS_RESULT_FAILURE:
1494 pr_err("scm call failed w/response result%d\n",
1495 resp.result);
1496 ret = -EINVAL;
1497 break;
1498 default:
1499 pr_err("scm call return unknown response %d\n",
1500 resp.result);
1501 ret = -EINVAL;
1502 break;
1503 }
1504 }
1505 return ret;
1506}
1507
1508static int qseecom_unload_commonlib_image(void)
1509{
1510 int ret = -EINVAL;
1511 struct qseecom_unload_lib_image_ireq unload_req = {0};
1512 struct qseecom_command_scm_resp resp;
1513
1514 /* Populate the remaining parameters */
1515 unload_req.qsee_cmd_id = QSEOS_UNLOAD_SERV_IMAGE_COMMAND;
1516 /* SCM_CALL to load the image */
1517 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &unload_req,
1518 sizeof(struct qseecom_unload_lib_image_ireq),
1519 &resp, sizeof(resp));
1520 if (ret) {
1521 pr_err("scm_call to unload lib failed : ret %d\n", ret);
1522 ret = -EIO;
1523 } else {
1524 switch (resp.result) {
1525 case QSEOS_RESULT_SUCCESS:
1526 break;
1527 case QSEOS_RESULT_FAILURE:
1528 pr_err("scm fail resp.result QSEOS_RESULT FAILURE\n");
1529 break;
1530 default:
1531 pr_err("scm call return unknown response %d\n",
1532 resp.result);
1533 ret = -EINVAL;
1534 break;
1535 }
1536 }
1537 return ret;
1538}
1539
Mona Hossaind44a3842012-10-15 09:41:35 -07001540int qseecom_start_app(struct qseecom_handle **handle,
1541 char *app_name, uint32_t size)
1542{
Mona Hossain05c73562012-10-29 17:49:01 -07001543 int32_t ret = 0;
Mona Hossaind44a3842012-10-15 09:41:35 -07001544 unsigned long flags = 0;
1545 struct qseecom_dev_handle *data = NULL;
1546 struct qseecom_check_app_ireq app_ireq;
1547 struct qseecom_registered_app_list *entry = NULL;
1548 struct qseecom_registered_kclient_list *kclient_entry = NULL;
1549 bool found_app = false;
1550 uint32_t len;
1551 ion_phys_addr_t pa;
1552
1553 if (qseecom.qseos_version == QSEOS_VERSION_13) {
1554 pr_err("This functionality is UNSUPPORTED in version 1.3\n");
1555 return -EINVAL;
1556 }
1557
Mona Hossain05c73562012-10-29 17:49:01 -07001558 if (qseecom.qsee_version > QSEEE_VERSION_00) {
1559 mutex_lock(&app_access_lock);
1560 if (qseecom.commonlib_loaded == false) {
1561 ret = qseecom_load_commonlib_image();
1562 if (ret == 0)
1563 qseecom.commonlib_loaded = true;
1564 }
1565 mutex_unlock(&app_access_lock);
1566 }
1567
1568 if (ret)
1569 return -EIO;
1570
Mona Hossaind44a3842012-10-15 09:41:35 -07001571 app_ireq.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
1572 memcpy(app_ireq.app_name, app_name, MAX_APP_NAME_SIZE);
1573 ret = __qseecom_check_app_exists(app_ireq);
1574 if (ret < 0)
1575 return -EINVAL;
1576
Mona Hossain823f9882012-11-23 14:42:20 -08001577 *handle = kzalloc(sizeof(struct qseecom_handle), GFP_KERNEL);
1578 if (!(*handle)) {
1579 pr_err("failed to allocate memory for kernel client handle\n");
1580 return -ENOMEM;
1581 }
1582
Mona Hossaind44a3842012-10-15 09:41:35 -07001583 data = kzalloc(sizeof(*data), GFP_KERNEL);
1584 if (!data) {
1585 pr_err("kmalloc failed\n");
1586 if (ret == 0) {
1587 kfree(*handle);
1588 *handle = NULL;
1589 }
1590 return -ENOMEM;
1591 }
1592 data->abort = 0;
1593 data->service = false;
1594 data->released = false;
1595 data->client.app_id = ret;
1596 data->client.sb_length = size;
1597 data->client.user_virt_sb_base = 0;
1598 data->client.ihandle = NULL;
1599
1600 init_waitqueue_head(&data->abort_wq);
1601 atomic_set(&data->ioctl_count, 0);
1602
1603 data->client.ihandle = ion_alloc(qseecom.ion_clnt, size, 4096,
1604 ION_HEAP(ION_QSECOM_HEAP_ID), 0);
1605 if (IS_ERR_OR_NULL(data->client.ihandle)) {
1606 pr_err("Ion client could not retrieve the handle\n");
1607 kfree(data);
1608 kfree(*handle);
1609 *handle = NULL;
1610 return -EINVAL;
1611 }
1612
1613 if (ret > 0) {
1614 pr_warn("App id %d for [%s] app exists\n", ret,
1615 (char *)app_ireq.app_name);
1616 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
1617 list_for_each_entry(entry,
1618 &qseecom.registered_app_list_head, list){
1619 if (entry->app_id == ret) {
1620 entry->ref_cnt++;
1621 found_app = true;
1622 break;
1623 }
1624 }
1625 spin_unlock_irqrestore(
1626 &qseecom.registered_app_list_lock, flags);
1627 if (!found_app)
1628 pr_warn("App_id %d [%s] was loaded but not registered\n",
1629 ret, (char *)app_ireq.app_name);
1630 } else {
1631 /* load the app and get the app_id */
1632 pr_debug("%s: Loading app for the first time'\n",
1633 qseecom.pdev->init_name);
1634 mutex_lock(&app_access_lock);
1635 ret = __qseecom_load_fw(data, app_name);
1636 mutex_unlock(&app_access_lock);
1637
1638 if (ret < 0) {
1639 kfree(*handle);
1640 *handle = NULL;
1641 return ret;
1642 }
1643 data->client.app_id = ret;
1644 }
1645 if (!found_app) {
1646 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1647 if (!entry) {
1648 pr_err("kmalloc failed\n");
1649 return -ENOMEM;
1650 }
1651 entry->app_id = ret;
1652 entry->ref_cnt = 1;
1653
1654 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
1655 list_add_tail(&entry->list, &qseecom.registered_app_list_head);
1656 spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
1657 flags);
1658 }
1659
1660 /* Get the physical address of the ION BUF */
1661 ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len);
1662 /* Populate the structure for sending scm call to load image */
1663 data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt,
1664 data->client.ihandle);
1665 data->client.sb_phys = pa;
1666 (*handle)->dev = (void *)data;
1667 (*handle)->sbuf = (unsigned char *)data->client.sb_virt;
1668 (*handle)->sbuf_len = data->client.sb_length;
1669
1670 kclient_entry = kzalloc(sizeof(*kclient_entry), GFP_KERNEL);
1671 if (!kclient_entry) {
1672 pr_err("kmalloc failed\n");
1673 return -ENOMEM;
1674 }
1675 kclient_entry->handle = *handle;
1676
1677 spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
1678 list_add_tail(&kclient_entry->list,
1679 &qseecom.registered_kclient_list_head);
1680 spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
1681
1682 return 0;
1683}
1684EXPORT_SYMBOL(qseecom_start_app);
1685
1686int qseecom_shutdown_app(struct qseecom_handle **handle)
1687{
1688 int ret = -EINVAL;
1689 struct qseecom_dev_handle *data =
1690 (struct qseecom_dev_handle *) ((*handle)->dev);
1691 struct qseecom_registered_kclient_list *kclient = NULL;
1692 unsigned long flags = 0;
1693 bool found_handle = false;
1694
1695 if (qseecom.qseos_version == QSEOS_VERSION_13) {
1696 pr_err("This functionality is UNSUPPORTED in version 1.3\n");
1697 return -EINVAL;
1698 }
1699 if (*handle == NULL) {
1700 pr_err("Handle is not initialized\n");
1701 return -EINVAL;
1702 }
1703
1704 spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
1705 list_for_each_entry(kclient, &qseecom.registered_kclient_list_head,
1706 list) {
1707 if (kclient->handle == (*handle)) {
1708 list_del(&kclient->list);
1709 found_handle = true;
1710 break;
1711 }
1712 }
1713 spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
1714 if (!found_handle)
1715 pr_err("Unable to find the handle, exiting\n");
1716 else
1717 ret = qseecom_unload_app(data);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001718 if (data->client.fast_load_enabled == true)
1719 qsee_disable_clock_vote(data, CLK_SFPB);
1720 if (data->client.perf_enabled == true)
1721 qsee_disable_clock_vote(data, CLK_DFAB);
Mona Hossaind44a3842012-10-15 09:41:35 -07001722 if (ret == 0) {
1723 kzfree(data);
1724 kzfree(*handle);
1725 kzfree(kclient);
1726 *handle = NULL;
1727 }
1728 return ret;
1729}
1730EXPORT_SYMBOL(qseecom_shutdown_app);
1731
1732int qseecom_send_command(struct qseecom_handle *handle, void *send_buf,
1733 uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len)
1734{
1735 int ret = 0;
1736 struct qseecom_send_cmd_req req = {0, 0, 0, 0};
1737 struct qseecom_dev_handle *data;
1738
1739 if (qseecom.qseos_version == QSEOS_VERSION_13) {
1740 pr_err("This functionality is UNSUPPORTED in version 1.3\n");
1741 return -EINVAL;
1742 }
1743
1744 if (handle == NULL) {
1745 pr_err("Handle is not initialized\n");
1746 return -EINVAL;
1747 }
1748 data = handle->dev;
1749
1750 req.cmd_req_len = sbuf_len;
1751 req.resp_len = rbuf_len;
1752 req.cmd_req_buf = send_buf;
1753 req.resp_buf = resp_buf;
1754
1755 mutex_lock(&app_access_lock);
1756 atomic_inc(&data->ioctl_count);
1757
1758 ret = __qseecom_send_cmd(data, &req);
1759
1760 atomic_dec(&data->ioctl_count);
1761 mutex_unlock(&app_access_lock);
1762
1763 if (ret)
1764 return ret;
1765
1766 pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%p\n",
1767 req.resp_len, req.resp_buf);
1768 return ret;
1769}
1770EXPORT_SYMBOL(qseecom_send_command);
1771
Mona Hossain91a8fc92012-11-07 19:58:30 -08001772int qseecom_set_bandwidth(struct qseecom_handle *handle, bool high)
1773{
Mona Hossainfca6f422013-01-12 13:00:35 -08001774 int ret = 0;
Mona Hossain91a8fc92012-11-07 19:58:30 -08001775 if ((handle == NULL) || (handle->dev == NULL)) {
1776 pr_err("No valid kernel client\n");
1777 return -EINVAL;
1778 }
Mona Hossainfca6f422013-01-12 13:00:35 -08001779 if (high) {
1780 ret = qsee_vote_for_clock(handle->dev, CLK_DFAB);
1781 if (ret)
1782 pr_err("Failed to vote for DFAB clock%d\n", ret);
1783 ret = qsee_vote_for_clock(handle->dev, CLK_SFPB);
1784 if (ret) {
1785 pr_err("Failed to vote for SFPB clock%d\n", ret);
1786 qsee_disable_clock_vote(handle->dev, CLK_DFAB);
1787 }
1788 } else {
Mona Hossain04d3fac2012-12-03 10:10:37 -08001789 qsee_disable_clock_vote(handle->dev, CLK_DFAB);
Mona Hossainfca6f422013-01-12 13:00:35 -08001790 qsee_disable_clock_vote(handle->dev, CLK_SFPB);
Mona Hossain91a8fc92012-11-07 19:58:30 -08001791 }
Mona Hossainfca6f422013-01-12 13:00:35 -08001792 return ret;
Mona Hossain91a8fc92012-11-07 19:58:30 -08001793}
1794EXPORT_SYMBOL(qseecom_set_bandwidth);
1795
Mona Hossain2892b6b2012-02-17 13:53:11 -08001796static int qseecom_send_resp(void)
1797{
1798 qseecom.send_resp_flag = 1;
1799 wake_up_interruptible(&qseecom.send_resp_wq);
1800 return 0;
1801}
1802
1803static int qseecom_get_qseos_version(struct qseecom_dev_handle *data,
1804 void __user *argp)
1805{
1806 struct qseecom_qseos_version_req req;
1807
1808 if (copy_from_user(&req, argp, sizeof(req))) {
1809 pr_err("copy_from_user failed");
1810 return -EINVAL;
1811 }
1812 req.qseos_version = qseecom.qseos_version;
1813 if (copy_to_user(argp, &req, sizeof(req))) {
1814 pr_err("copy_to_user failed");
1815 return -EINVAL;
1816 }
1817 return 0;
1818}
1819
Mona Hossain04d3fac2012-12-03 10:10:37 -08001820static int qsee_vote_for_clock(struct qseecom_dev_handle *data,
1821 int32_t clk_type)
Mona Hossain2892b6b2012-02-17 13:53:11 -08001822{
1823 int ret = 0;
1824
1825 if (!qsee_perf_client)
Ramesh Masavarapue640e842012-04-03 11:21:54 -07001826 return ret;
Mona Hossain2892b6b2012-02-17 13:53:11 -08001827
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001828 switch (clk_type) {
1829 case CLK_DFAB:
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001830 mutex_lock(&qsee_bw_mutex);
1831 if (!qsee_bw_count) {
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001832 if (qsee_sfpb_bw_count > 0)
1833 ret = msm_bus_scale_client_update_request(
1834 qsee_perf_client, 3);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001835 else {
1836 if (ce_core_src_clk != NULL)
1837 clk_set_rate(ce_core_src_clk,
1838 QSEE_CE_CLK_100MHZ);
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001839 ret = msm_bus_scale_client_update_request(
1840 qsee_perf_client, 1);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001841 }
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001842 if (ret)
1843 pr_err("DFAB Bandwidth req failed (%d)\n",
1844 ret);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001845 else {
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001846 qsee_bw_count++;
Mona Hossain04d3fac2012-12-03 10:10:37 -08001847 data->client.perf_enabled = true;
1848 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001849 } else {
1850 qsee_bw_count++;
Mona Hossain04d3fac2012-12-03 10:10:37 -08001851 data->client.perf_enabled = true;
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001852 }
1853 mutex_unlock(&qsee_bw_mutex);
1854 break;
1855 case CLK_SFPB:
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001856 mutex_lock(&qsee_bw_mutex);
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001857 if (!qsee_sfpb_bw_count) {
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001858 if (qsee_bw_count > 0)
1859 ret = msm_bus_scale_client_update_request(
1860 qsee_perf_client, 3);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001861 else {
1862 if (ce_core_src_clk != NULL)
1863 clk_set_rate(ce_core_src_clk,
1864 QSEE_CE_CLK_100MHZ);
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001865 ret = msm_bus_scale_client_update_request(
1866 qsee_perf_client, 2);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001867 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001868
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001869 if (ret)
1870 pr_err("SFPB Bandwidth req failed (%d)\n",
1871 ret);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001872 else {
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001873 qsee_sfpb_bw_count++;
Mona Hossain04d3fac2012-12-03 10:10:37 -08001874 data->client.fast_load_enabled = true;
1875 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001876 } else {
1877 qsee_sfpb_bw_count++;
Mona Hossain04d3fac2012-12-03 10:10:37 -08001878 data->client.fast_load_enabled = true;
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001879 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001880 mutex_unlock(&qsee_bw_mutex);
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001881 break;
1882 default:
1883 pr_err("Clock type not defined\n");
1884 break;
Mona Hossain2892b6b2012-02-17 13:53:11 -08001885 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08001886 return ret;
1887}
1888
Mona Hossain04d3fac2012-12-03 10:10:37 -08001889static void qsee_disable_clock_vote(struct qseecom_dev_handle *data,
1890 int32_t clk_type)
Mona Hossain2892b6b2012-02-17 13:53:11 -08001891{
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001892 int32_t ret = 0;
Ramesh Masavarapua1bc0e42012-03-05 07:42:48 -08001893
Mona Hossain2892b6b2012-02-17 13:53:11 -08001894 if (!qsee_perf_client)
Ramesh Masavarapua1bc0e42012-03-05 07:42:48 -08001895 return;
1896
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001897 switch (clk_type) {
1898 case CLK_DFAB:
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001899 mutex_lock(&qsee_bw_mutex);
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001900 if (qsee_bw_count == 0) {
1901 pr_err("Client error.Extra call to disable DFAB clk\n");
1902 mutex_unlock(&qsee_bw_mutex);
1903 return;
1904 }
1905
Mona Hossain04d3fac2012-12-03 10:10:37 -08001906 if (qsee_bw_count == 1) {
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001907 if (qsee_sfpb_bw_count > 0)
1908 ret = msm_bus_scale_client_update_request(
1909 qsee_perf_client, 2);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001910 else {
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001911 ret = msm_bus_scale_client_update_request(
1912 qsee_perf_client, 0);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001913 if (ce_core_src_clk != NULL)
1914 clk_set_rate(ce_core_src_clk,
1915 QSEE_CE_CLK_50MHZ);
1916 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001917 if (ret)
1918 pr_err("SFPB Bandwidth req fail (%d)\n",
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001919 ret);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001920 else {
1921 qsee_bw_count--;
1922 data->client.perf_enabled = false;
1923 }
1924 } else {
1925 qsee_bw_count--;
1926 data->client.perf_enabled = false;
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001927 }
1928 mutex_unlock(&qsee_bw_mutex);
1929 break;
1930 case CLK_SFPB:
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001931 mutex_lock(&qsee_bw_mutex);
1932 if (qsee_sfpb_bw_count == 0) {
1933 pr_err("Client error.Extra call to disable SFPB clk\n");
1934 mutex_unlock(&qsee_bw_mutex);
1935 return;
1936 }
Mona Hossain04d3fac2012-12-03 10:10:37 -08001937 if (qsee_sfpb_bw_count == 1) {
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001938 if (qsee_bw_count > 0)
1939 ret = msm_bus_scale_client_update_request(
1940 qsee_perf_client, 1);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001941 else {
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001942 ret = msm_bus_scale_client_update_request(
1943 qsee_perf_client, 0);
Mona Hossaind39e33b2012-11-05 13:36:40 -08001944 if (ce_core_src_clk != NULL)
1945 clk_set_rate(ce_core_src_clk,
1946 QSEE_CE_CLK_50MHZ);
1947 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001948 if (ret)
1949 pr_err("SFPB Bandwidth req fail (%d)\n",
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001950 ret);
Mona Hossain04d3fac2012-12-03 10:10:37 -08001951 else {
1952 qsee_sfpb_bw_count--;
1953 data->client.fast_load_enabled = false;
1954 }
1955 } else {
1956 qsee_sfpb_bw_count--;
1957 data->client.fast_load_enabled = false;
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001958 }
Ramesh Masavarapu8d756582012-10-03 10:18:06 -07001959 mutex_unlock(&qsee_bw_mutex);
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07001960 break;
1961 default:
1962 pr_err("Clock type not defined\n");
1963 break;
Ramesh Masavarapue640e842012-04-03 11:21:54 -07001964 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08001965
Mona Hossain2892b6b2012-02-17 13:53:11 -08001966}
1967
Mona Hossain5ab9d772012-04-11 21:00:40 -07001968static int qseecom_load_external_elf(struct qseecom_dev_handle *data,
1969 void __user *argp)
1970{
1971 struct ion_handle *ihandle; /* Ion handle */
1972 struct qseecom_load_img_req load_img_req;
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07001973 int ret;
1974 int set_cpu_ret = 0;
Mona Hossain5ab9d772012-04-11 21:00:40 -07001975 ion_phys_addr_t pa = 0;
1976 uint32_t len;
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07001977 struct cpumask mask;
Mona Hossain5ab9d772012-04-11 21:00:40 -07001978 struct qseecom_load_app_ireq load_req;
1979 struct qseecom_command_scm_resp resp;
1980
1981 /* Copy the relevant information needed for loading the image */
1982 if (__copy_from_user(&load_img_req,
1983 (void __user *)argp,
1984 sizeof(struct qseecom_load_img_req))) {
1985 pr_err("copy_from_user failed\n");
1986 return -EFAULT;
1987 }
1988
1989 /* Get the handle of the shared fd */
Laura Abbottb14ed962012-01-30 14:18:08 -08001990 ihandle = ion_import_dma_buf(qseecom.ion_clnt,
Mona Hossain5ab9d772012-04-11 21:00:40 -07001991 load_img_req.ifd_data_fd);
1992 if (IS_ERR_OR_NULL(ihandle)) {
1993 pr_err("Ion client could not retrieve the handle\n");
1994 return -ENOMEM;
1995 }
1996
1997 /* Get the physical address of the ION BUF */
1998 ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len);
1999
2000 /* Populate the structure for sending scm call to load image */
2001 load_req.qsee_cmd_id = QSEOS_LOAD_EXTERNAL_ELF_COMMAND;
2002 load_req.mdt_len = load_img_req.mdt_len;
2003 load_req.img_len = load_img_req.img_len;
2004 load_req.phy_addr = pa;
2005
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002006 /* SCM_CALL tied to Core0 */
2007 mask = CPU_MASK_CPU0;
2008 set_cpu_ret = set_cpus_allowed_ptr(current, &mask);
2009 if (set_cpu_ret) {
2010 pr_err("set_cpus_allowed_ptr failed : ret %d\n",
2011 set_cpu_ret);
2012 ret = -EFAULT;
2013 goto qseecom_load_external_elf_set_cpu_err;
2014 }
2015
Mona Hossain5ab9d772012-04-11 21:00:40 -07002016 /* SCM_CALL to load the external elf */
2017 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
2018 sizeof(struct qseecom_load_app_ireq),
2019 &resp, sizeof(resp));
2020 if (ret) {
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002021 pr_err("scm_call to load failed : ret %d\n",
Mona Hossain5ab9d772012-04-11 21:00:40 -07002022 ret);
2023 ret = -EFAULT;
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002024 goto qseecom_load_external_elf_scm_err;
Mona Hossain5ab9d772012-04-11 21:00:40 -07002025 }
2026
2027 if (resp.result == QSEOS_RESULT_INCOMPLETE) {
2028 ret = __qseecom_process_incomplete_cmd(data, &resp);
2029 if (ret)
2030 pr_err("process_incomplete_cmd failed err: %d\n",
2031 ret);
2032 } else {
2033 if (resp.result != QSEOS_RESULT_SUCCESS) {
2034 pr_err("scm_call to load image failed resp.result =%d\n",
2035 resp.result);
2036 ret = -EFAULT;
2037 }
2038 }
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002039
2040qseecom_load_external_elf_scm_err:
2041 /* Restore the CPU mask */
2042 mask = CPU_MASK_ALL;
2043 set_cpu_ret = set_cpus_allowed_ptr(current, &mask);
2044 if (set_cpu_ret) {
2045 pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n",
2046 set_cpu_ret);
2047 ret = -EFAULT;
2048 }
2049
2050qseecom_load_external_elf_set_cpu_err:
Mona Hossain5ab9d772012-04-11 21:00:40 -07002051 /* Deallocate the handle */
2052 if (!IS_ERR_OR_NULL(ihandle))
2053 ion_free(qseecom.ion_clnt, ihandle);
2054
2055 return ret;
2056}
2057
2058static int qseecom_unload_external_elf(struct qseecom_dev_handle *data)
2059{
2060 int ret = 0;
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002061 int set_cpu_ret = 0;
Mona Hossain5ab9d772012-04-11 21:00:40 -07002062 struct qseecom_command_scm_resp resp;
2063 struct qseecom_unload_app_ireq req;
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002064 struct cpumask mask;
Mona Hossain5ab9d772012-04-11 21:00:40 -07002065
2066 /* Populate the structure for sending scm call to unload image */
2067 req.qsee_cmd_id = QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND;
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002068
2069 /* SCM_CALL tied to Core0 */
2070 mask = CPU_MASK_CPU0;
2071 ret = set_cpus_allowed_ptr(current, &mask);
2072 if (ret) {
2073 pr_err("set_cpus_allowed_ptr failed : ret %d\n",
2074 ret);
2075 return -EFAULT;
2076 }
2077
Mona Hossain5ab9d772012-04-11 21:00:40 -07002078 /* SCM_CALL to unload the external elf */
2079 ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
2080 sizeof(struct qseecom_unload_app_ireq),
2081 &resp, sizeof(resp));
2082 if (ret) {
2083 pr_err("scm_call to unload failed : ret %d\n",
2084 ret);
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002085 ret = -EFAULT;
2086 goto qseecom_unload_external_elf_scm_err;
Mona Hossain5ab9d772012-04-11 21:00:40 -07002087 }
2088 if (resp.result == QSEOS_RESULT_INCOMPLETE) {
2089 ret = __qseecom_process_incomplete_cmd(data, &resp);
2090 if (ret)
2091 pr_err("process_incomplete_cmd fail err: %d\n",
2092 ret);
2093 } else {
2094 if (resp.result != QSEOS_RESULT_SUCCESS) {
2095 pr_err("scm_call to unload image failed resp.result =%d\n",
2096 resp.result);
2097 ret = -EFAULT;
2098 }
2099 }
Ramesh Masavarapu41af8ab2012-05-21 11:08:00 -07002100
2101qseecom_unload_external_elf_scm_err:
2102 /* Restore the CPU mask */
2103 mask = CPU_MASK_ALL;
2104 set_cpu_ret = set_cpus_allowed_ptr(current, &mask);
2105 if (set_cpu_ret) {
2106 pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n",
2107 set_cpu_ret);
2108 ret = -EFAULT;
2109 }
2110
Mona Hossain5ab9d772012-04-11 21:00:40 -07002111 return ret;
2112}
Mona Hossain2892b6b2012-02-17 13:53:11 -08002113
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -07002114static int qseecom_query_app_loaded(struct qseecom_dev_handle *data,
2115 void __user *argp)
2116{
2117
2118 int32_t ret;
2119 struct qseecom_qseos_app_load_query query_req;
2120 struct qseecom_check_app_ireq req;
Ramesh Masavarapu13d99672012-07-17 11:05:15 -07002121 struct qseecom_registered_app_list *entry = NULL;
2122 unsigned long flags = 0;
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -07002123
2124 /* Copy the relevant information needed for loading the image */
2125 if (__copy_from_user(&query_req,
2126 (void __user *)argp,
2127 sizeof(struct qseecom_qseos_app_load_query))) {
2128 pr_err("copy_from_user failed\n");
2129 return -EFAULT;
2130 }
2131
2132 req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
2133 memcpy(req.app_name, query_req.app_name, MAX_APP_NAME_SIZE);
2134
2135 ret = __qseecom_check_app_exists(req);
Ramesh Masavarapu13d99672012-07-17 11:05:15 -07002136
2137 if ((ret == -EINVAL) || (ret == -ENODEV)) {
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -07002138 pr_err(" scm call to check if app is loaded failed");
2139 return ret; /* scm call failed */
2140 } else if (ret > 0) {
Ramesh Masavarapu13d99672012-07-17 11:05:15 -07002141 pr_warn("App id %d (%s) already exists\n", ret,
2142 (char *)(req.app_name));
2143 spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
2144 list_for_each_entry(entry,
2145 &qseecom.registered_app_list_head, list){
2146 if (entry->app_id == ret) {
2147 entry->ref_cnt++;
2148 break;
2149 }
2150 }
2151 spin_unlock_irqrestore(
2152 &qseecom.registered_app_list_lock, flags);
2153 data->client.app_id = ret;
2154 query_req.app_id = ret;
2155
2156 if (copy_to_user(argp, &query_req, sizeof(query_req))) {
2157 pr_err("copy_to_user failed\n");
2158 return -EFAULT;
2159 }
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -07002160 return -EEXIST; /* app already loaded */
2161 } else {
2162 return 0; /* app not loaded */
2163 }
2164}
2165
Mona Hossain2892b6b2012-02-17 13:53:11 -08002166static long qseecom_ioctl(struct file *file, unsigned cmd,
2167 unsigned long arg)
2168{
2169 int ret = 0;
2170 struct qseecom_dev_handle *data = file->private_data;
2171 void __user *argp = (void __user *) arg;
2172
2173 if (data->abort) {
2174 pr_err("Aborting qseecom driver\n");
2175 return -ENODEV;
2176 }
2177
2178 switch (cmd) {
2179 case QSEECOM_IOCTL_REGISTER_LISTENER_REQ: {
2180 pr_debug("ioctl register_listener_req()\n");
2181 atomic_inc(&data->ioctl_count);
2182 ret = qseecom_register_listener(data, argp);
2183 atomic_dec(&data->ioctl_count);
2184 wake_up_all(&data->abort_wq);
2185 if (ret)
2186 pr_err("failed qseecom_register_listener: %d\n", ret);
2187 break;
2188 }
2189 case QSEECOM_IOCTL_UNREGISTER_LISTENER_REQ: {
2190 pr_debug("ioctl unregister_listener_req()\n");
2191 atomic_inc(&data->ioctl_count);
2192 ret = qseecom_unregister_listener(data);
2193 atomic_dec(&data->ioctl_count);
2194 wake_up_all(&data->abort_wq);
2195 if (ret)
2196 pr_err("failed qseecom_unregister_listener: %d\n", ret);
2197 break;
2198 }
2199 case QSEECOM_IOCTL_SEND_CMD_REQ: {
2200 /* Only one client allowed here at a time */
Ramesh Masavarapud8610112012-06-26 15:32:52 -07002201 mutex_lock(&app_access_lock);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002202 atomic_inc(&data->ioctl_count);
2203 ret = qseecom_send_cmd(data, argp);
2204 atomic_dec(&data->ioctl_count);
2205 wake_up_all(&data->abort_wq);
Ramesh Masavarapud8610112012-06-26 15:32:52 -07002206 mutex_unlock(&app_access_lock);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002207 if (ret)
2208 pr_err("failed qseecom_send_cmd: %d\n", ret);
2209 break;
2210 }
2211 case QSEECOM_IOCTL_SEND_MODFD_CMD_REQ: {
2212 /* Only one client allowed here at a time */
Ramesh Masavarapud8610112012-06-26 15:32:52 -07002213 mutex_lock(&app_access_lock);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002214 atomic_inc(&data->ioctl_count);
2215 ret = qseecom_send_modfd_cmd(data, argp);
2216 atomic_dec(&data->ioctl_count);
2217 wake_up_all(&data->abort_wq);
Ramesh Masavarapud8610112012-06-26 15:32:52 -07002218 mutex_unlock(&app_access_lock);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002219 if (ret)
2220 pr_err("failed qseecom_send_cmd: %d\n", ret);
2221 break;
2222 }
2223 case QSEECOM_IOCTL_RECEIVE_REQ: {
2224 atomic_inc(&data->ioctl_count);
2225 ret = qseecom_receive_req(data);
2226 atomic_dec(&data->ioctl_count);
2227 wake_up_all(&data->abort_wq);
2228 if (ret)
2229 pr_err("failed qseecom_receive_req: %d\n", ret);
2230 break;
2231 }
2232 case QSEECOM_IOCTL_SEND_RESP_REQ: {
2233 atomic_inc(&data->ioctl_count);
2234 ret = qseecom_send_resp();
2235 atomic_dec(&data->ioctl_count);
2236 wake_up_all(&data->abort_wq);
2237 if (ret)
2238 pr_err("failed qseecom_send_resp: %d\n", ret);
2239 break;
2240 }
2241 case QSEECOM_IOCTL_SET_MEM_PARAM_REQ: {
2242 ret = qseecom_set_client_mem_param(data, argp);
2243 if (ret)
2244 pr_err("failed Qqseecom_set_mem_param request: %d\n",
2245 ret);
2246 break;
2247 }
2248 case QSEECOM_IOCTL_LOAD_APP_REQ: {
2249 mutex_lock(&app_access_lock);
2250 atomic_inc(&data->ioctl_count);
Mona Hossain05c73562012-10-29 17:49:01 -07002251 if (qseecom.qsee_version > QSEEE_VERSION_00) {
2252 if (qseecom.commonlib_loaded == false) {
2253 ret = qseecom_load_commonlib_image();
2254 if (ret == 0)
2255 qseecom.commonlib_loaded = true;
2256 }
2257 }
2258 if (ret == 0)
2259 ret = qseecom_load_app(data, argp);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002260 atomic_dec(&data->ioctl_count);
2261 mutex_unlock(&app_access_lock);
2262 if (ret)
2263 pr_err("failed load_app request: %d\n", ret);
2264 break;
2265 }
2266 case QSEECOM_IOCTL_UNLOAD_APP_REQ: {
2267 mutex_lock(&app_access_lock);
2268 atomic_inc(&data->ioctl_count);
2269 ret = qseecom_unload_app(data);
2270 atomic_dec(&data->ioctl_count);
2271 mutex_unlock(&app_access_lock);
2272 if (ret)
2273 pr_err("failed unload_app request: %d\n", ret);
2274 break;
2275 }
2276 case QSEECOM_IOCTL_GET_QSEOS_VERSION_REQ: {
2277 atomic_inc(&data->ioctl_count);
2278 ret = qseecom_get_qseos_version(data, argp);
2279 if (ret)
2280 pr_err("qseecom_get_qseos_version: %d\n", ret);
2281 atomic_dec(&data->ioctl_count);
2282 break;
2283 }
2284 case QSEECOM_IOCTL_PERF_ENABLE_REQ:{
2285 atomic_inc(&data->ioctl_count);
Mona Hossain04d3fac2012-12-03 10:10:37 -08002286 ret = qsee_vote_for_clock(data, CLK_DFAB);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002287 if (ret)
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002288 pr_err("Failed to vote for DFAB clock%d\n", ret);
Mona Hossain8e2d73a2013-01-10 04:30:04 -08002289 ret = qsee_vote_for_clock(data, CLK_SFPB);
2290 if (ret)
2291 pr_err("Failed to vote for SFPB clock%d\n", ret);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002292 atomic_dec(&data->ioctl_count);
2293 break;
2294 }
2295 case QSEECOM_IOCTL_PERF_DISABLE_REQ:{
2296 atomic_inc(&data->ioctl_count);
Mona Hossain04d3fac2012-12-03 10:10:37 -08002297 qsee_disable_clock_vote(data, CLK_DFAB);
Mona Hossain8e2d73a2013-01-10 04:30:04 -08002298 qsee_disable_clock_vote(data, CLK_SFPB);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002299 atomic_dec(&data->ioctl_count);
2300 break;
2301 }
Mona Hossain5ab9d772012-04-11 21:00:40 -07002302 case QSEECOM_IOCTL_LOAD_EXTERNAL_ELF_REQ: {
2303 data->released = true;
2304 if (qseecom.qseos_version == QSEOS_VERSION_13) {
2305 pr_err("Loading External elf image unsupported in rev 0x13\n");
2306 ret = -EINVAL;
2307 break;
2308 }
2309 mutex_lock(&app_access_lock);
2310 atomic_inc(&data->ioctl_count);
2311 ret = qseecom_load_external_elf(data, argp);
2312 atomic_dec(&data->ioctl_count);
2313 mutex_unlock(&app_access_lock);
2314 if (ret)
2315 pr_err("failed load_external_elf request: %d\n", ret);
2316 break;
2317 }
2318 case QSEECOM_IOCTL_UNLOAD_EXTERNAL_ELF_REQ: {
2319 data->released = true;
2320 if (qseecom.qseos_version == QSEOS_VERSION_13) {
2321 pr_err("Unloading External elf image unsupported in rev 0x13\n");
2322 ret = -EINVAL;
2323 break;
2324 }
2325 mutex_lock(&app_access_lock);
2326 atomic_inc(&data->ioctl_count);
2327 ret = qseecom_unload_external_elf(data);
2328 atomic_dec(&data->ioctl_count);
2329 mutex_unlock(&app_access_lock);
2330 if (ret)
2331 pr_err("failed unload_app request: %d\n", ret);
2332 break;
2333 }
Ramesh Masavarapu1ee4ac82012-06-22 15:38:34 -07002334 case QSEECOM_IOCTL_APP_LOADED_QUERY_REQ: {
2335 mutex_lock(&app_access_lock);
2336 atomic_inc(&data->ioctl_count);
2337 ret = qseecom_query_app_loaded(data, argp);
2338 atomic_dec(&data->ioctl_count);
2339 mutex_unlock(&app_access_lock);
2340 break;
2341 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08002342 default:
2343 return -EINVAL;
2344 }
2345 return ret;
2346}
2347
2348static int qseecom_open(struct inode *inode, struct file *file)
2349{
2350 int ret = 0;
2351 struct qseecom_dev_handle *data;
2352
2353 data = kzalloc(sizeof(*data), GFP_KERNEL);
2354 if (!data) {
2355 pr_err("kmalloc failed\n");
2356 return -ENOMEM;
2357 }
2358 file->private_data = data;
2359 data->abort = 0;
2360 data->service = false;
2361 data->released = false;
2362 init_waitqueue_head(&data->abort_wq);
2363 atomic_set(&data->ioctl_count, 0);
Mona Hossain7cdbfaf2012-03-15 18:56:10 -07002364 if (qseecom.qseos_version == QSEOS_VERSION_13) {
2365 int pil_error;
2366 mutex_lock(&pil_access_lock);
2367 if (pil_ref_cnt == 0) {
Stephen Boyd77db8bb2012-06-27 15:15:16 -07002368 pil = subsystem_get("tzapps");
Mona Hossain7cdbfaf2012-03-15 18:56:10 -07002369 if (IS_ERR(pil)) {
2370 pr_err("Playready PIL image load failed\n");
2371 pil_error = PTR_ERR(pil);
2372 pil = NULL;
2373 pr_debug("tzapps image load FAILED\n");
2374 mutex_unlock(&pil_access_lock);
2375 return pil_error;
2376 }
2377 }
2378 pil_ref_cnt++;
2379 mutex_unlock(&pil_access_lock);
2380 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08002381 return ret;
2382}
2383
2384static int qseecom_release(struct inode *inode, struct file *file)
2385{
2386 struct qseecom_dev_handle *data = file->private_data;
2387 int ret = 0;
2388
2389 if (data->released == false) {
2390 pr_warn("data->released == false\n");
2391 if (data->service)
2392 ret = qseecom_unregister_listener(data);
2393 else
2394 ret = qseecom_unload_app(data);
2395 if (ret) {
2396 pr_err("Close failed\n");
2397 return ret;
2398 }
2399 }
Mona Hossain04d3fac2012-12-03 10:10:37 -08002400 if (data->client.fast_load_enabled == true)
2401 qsee_disable_clock_vote(data, CLK_SFPB);
2402 if (data->client.perf_enabled == true)
2403 qsee_disable_clock_vote(data, CLK_DFAB);
2404
Mona Hossain7cdbfaf2012-03-15 18:56:10 -07002405 if (qseecom.qseos_version == QSEOS_VERSION_13) {
2406 mutex_lock(&pil_access_lock);
2407 if (pil_ref_cnt == 1)
Stephen Boyd77db8bb2012-06-27 15:15:16 -07002408 subsystem_put(pil);
Mona Hossain7cdbfaf2012-03-15 18:56:10 -07002409 pil_ref_cnt--;
2410 mutex_unlock(&pil_access_lock);
2411 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08002412 kfree(data);
Ramesh Masavarapua1bc0e42012-03-05 07:42:48 -08002413
Mona Hossain2892b6b2012-02-17 13:53:11 -08002414 return ret;
2415}
2416
Mona Hossain2892b6b2012-02-17 13:53:11 -08002417static const struct file_operations qseecom_fops = {
2418 .owner = THIS_MODULE,
2419 .unlocked_ioctl = qseecom_ioctl,
2420 .open = qseecom_open,
2421 .release = qseecom_release
2422};
2423
Mona Hossaind39e33b2012-11-05 13:36:40 -08002424static int __qseecom_enable_clk(void)
2425{
2426 int rc = 0;
2427
2428 /* Enable CE core clk */
2429 rc = clk_prepare_enable(ce_core_clk);
2430 if (rc) {
2431 pr_err("Unable to enable/prepare CE core clk\n");
2432 return -EIO;
2433 } else {
2434 /* Enable CE clk */
2435 rc = clk_prepare_enable(ce_clk);
2436 if (rc) {
2437 pr_err("Unable to enable/prepare CE iface clk\n");
2438 clk_disable_unprepare(ce_core_clk);
2439 return -EIO;
2440 } else {
2441 /* Enable AXI clk */
2442 rc = clk_prepare_enable(ce_bus_clk);
2443 if (rc) {
2444 pr_err("Unable to enable/prepare CE iface clk\n");
2445 clk_disable_unprepare(ce_core_clk);
2446 clk_disable_unprepare(ce_clk);
2447 return -EIO;
2448 }
2449 }
2450 }
2451 return rc;
2452}
2453
2454static void __qseecom_disable_clk(void)
2455{
2456 if (ce_clk != NULL)
2457 clk_disable_unprepare(ce_clk);
2458 if (ce_core_clk != NULL)
2459 clk_disable_unprepare(ce_core_clk);
2460 if (ce_bus_clk != NULL)
2461 clk_disable_unprepare(ce_bus_clk);
2462}
2463
2464static int __qseecom_init_clk(void)
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002465{
2466 int rc = 0;
2467 struct device *pdev;
2468
2469 pdev = qseecom.pdev;
2470 /* Get CE3 src core clk. */
2471 ce_core_src_clk = clk_get(pdev, "core_clk_src");
2472 if (!IS_ERR(ce_core_src_clk)) {
Mona Hossaind39e33b2012-11-05 13:36:40 -08002473 /* Set the core src clk @50Mhz */
2474 rc = clk_set_rate(ce_core_src_clk, QSEE_CE_CLK_50MHZ);
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002475 if (rc) {
2476 clk_put(ce_core_src_clk);
2477 pr_err("Unable to set the core src clk @100Mhz.\n");
Mona Hossaind39e33b2012-11-05 13:36:40 -08002478 return -EIO;
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002479 }
2480 } else {
2481 pr_warn("Unable to get CE core src clk, set to NULL\n");
2482 ce_core_src_clk = NULL;
2483 }
2484
2485 /* Get CE core clk */
2486 ce_core_clk = clk_get(pdev, "core_clk");
2487 if (IS_ERR(ce_core_clk)) {
2488 rc = PTR_ERR(ce_core_clk);
2489 pr_err("Unable to get CE core clk\n");
2490 if (ce_core_src_clk != NULL)
2491 clk_put(ce_core_src_clk);
Mona Hossaind39e33b2012-11-05 13:36:40 -08002492 return -EIO;
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002493 }
2494
2495 /* Get CE Interface clk */
2496 ce_clk = clk_get(pdev, "iface_clk");
2497 if (IS_ERR(ce_clk)) {
2498 rc = PTR_ERR(ce_clk);
2499 pr_err("Unable to get CE interface clk\n");
2500 if (ce_core_src_clk != NULL)
2501 clk_put(ce_core_src_clk);
2502 clk_put(ce_core_clk);
Mona Hossaind39e33b2012-11-05 13:36:40 -08002503 return -EIO;
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002504 }
2505
2506 /* Get CE AXI clk */
2507 ce_bus_clk = clk_get(pdev, "bus_clk");
2508 if (IS_ERR(ce_bus_clk)) {
2509 rc = PTR_ERR(ce_bus_clk);
2510 pr_err("Unable to get CE BUS interface clk\n");
2511 if (ce_core_src_clk != NULL)
2512 clk_put(ce_core_src_clk);
2513 clk_put(ce_core_clk);
2514 clk_put(ce_clk);
Mona Hossaind39e33b2012-11-05 13:36:40 -08002515 return -EIO;
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002516 }
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002517 return rc;
2518}
2519
Mona Hossaind39e33b2012-11-05 13:36:40 -08002520static void __qseecom_deinit_clk(void)
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002521{
Mona Hossaind39e33b2012-11-05 13:36:40 -08002522 if (ce_clk != NULL) {
2523 clk_put(ce_clk);
2524 ce_clk = NULL;
2525 }
2526 if (ce_core_clk != NULL) {
2527 clk_put(ce_core_clk);
2528 ce_clk = NULL;
2529 }
2530 if (ce_bus_clk != NULL) {
2531 clk_put(ce_bus_clk);
2532 ce_clk = NULL;
2533 }
2534 if (ce_core_src_clk != NULL) {
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002535 clk_put(ce_core_src_clk);
Mona Hossaind39e33b2012-11-05 13:36:40 -08002536 ce_core_src_clk = NULL;
2537 }
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002538}
2539
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002540static int __devinit qseecom_probe(struct platform_device *pdev)
Mona Hossain2892b6b2012-02-17 13:53:11 -08002541{
2542 int rc;
Mona Hossaind39e33b2012-11-05 13:36:40 -08002543 int ret = 0;
Mona Hossain2892b6b2012-02-17 13:53:11 -08002544 struct device *class_dev;
2545 char qsee_not_legacy = 0;
Mona Hossaind44a3842012-10-15 09:41:35 -07002546 struct msm_bus_scale_pdata *qseecom_platform_support = NULL;
Mona Hossain2892b6b2012-02-17 13:53:11 -08002547 uint32_t system_call_id = QSEOS_CHECK_VERSION_CMD;
2548
Ramesh Masavarapue640e842012-04-03 11:21:54 -07002549 qsee_bw_count = 0;
Ramesh Masavarapue640e842012-04-03 11:21:54 -07002550 qsee_perf_client = 0;
Mona Hossaind39e33b2012-11-05 13:36:40 -08002551 qsee_sfpb_bw_count = 0;
2552
2553 ce_core_clk = NULL;
2554 ce_clk = NULL;
2555 ce_core_src_clk = NULL;
2556 ce_bus_clk = NULL;
Ramesh Masavarapue640e842012-04-03 11:21:54 -07002557
Mona Hossain2892b6b2012-02-17 13:53:11 -08002558 rc = alloc_chrdev_region(&qseecom_device_no, 0, 1, QSEECOM_DEV);
2559 if (rc < 0) {
2560 pr_err("alloc_chrdev_region failed %d\n", rc);
2561 return rc;
2562 }
2563
2564 driver_class = class_create(THIS_MODULE, QSEECOM_DEV);
2565 if (IS_ERR(driver_class)) {
2566 rc = -ENOMEM;
2567 pr_err("class_create failed %d\n", rc);
2568 goto unregister_chrdev_region;
2569 }
2570
2571 class_dev = device_create(driver_class, NULL, qseecom_device_no, NULL,
2572 QSEECOM_DEV);
2573 if (!class_dev) {
2574 pr_err("class_device_create failed %d\n", rc);
2575 rc = -ENOMEM;
2576 goto class_destroy;
2577 }
2578
2579 cdev_init(&qseecom_cdev, &qseecom_fops);
2580 qseecom_cdev.owner = THIS_MODULE;
2581
2582 rc = cdev_add(&qseecom_cdev, MKDEV(MAJOR(qseecom_device_no), 0), 1);
2583 if (rc < 0) {
2584 pr_err("cdev_add failed %d\n", rc);
2585 goto err;
2586 }
2587
2588 INIT_LIST_HEAD(&qseecom.registered_listener_list_head);
2589 spin_lock_init(&qseecom.registered_listener_list_lock);
2590 INIT_LIST_HEAD(&qseecom.registered_app_list_head);
2591 spin_lock_init(&qseecom.registered_app_list_lock);
Mona Hossaind44a3842012-10-15 09:41:35 -07002592 INIT_LIST_HEAD(&qseecom.registered_kclient_list_head);
2593 spin_lock_init(&qseecom.registered_kclient_list_lock);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002594 init_waitqueue_head(&qseecom.send_resp_wq);
2595 qseecom.send_resp_flag = 0;
2596
2597 rc = scm_call(6, 1, &system_call_id, sizeof(system_call_id),
2598 &qsee_not_legacy, sizeof(qsee_not_legacy));
2599 if (rc) {
Mona Hossain05c73562012-10-29 17:49:01 -07002600 pr_err("Failed to retrieve QSEOS version information %d\n", rc);
Mona Hossain2892b6b2012-02-17 13:53:11 -08002601 goto err;
2602 }
Mona Hossain05c73562012-10-29 17:49:01 -07002603 if (qsee_not_legacy) {
2604 uint32_t feature = 10;
2605
2606 qseecom.qsee_version = QSEEE_VERSION_00;
2607 rc = scm_call(6, 3, &feature, sizeof(feature),
2608 &qseecom.qsee_version, sizeof(qseecom.qsee_version));
2609 if (rc) {
2610 pr_err("Failed to get QSEE version info %d\n", rc);
2611 goto err;
2612 }
Mona Hossain2892b6b2012-02-17 13:53:11 -08002613 qseecom.qseos_version = QSEOS_VERSION_14;
Mona Hossain05c73562012-10-29 17:49:01 -07002614 } else {
Mona Hossain2892b6b2012-02-17 13:53:11 -08002615 qseecom.qseos_version = QSEOS_VERSION_13;
Mona Hossain05c73562012-10-29 17:49:01 -07002616 qseecom.qsee_version = 0;
Mona Hossain2892b6b2012-02-17 13:53:11 -08002617 pil = NULL;
2618 pil_ref_cnt = 0;
2619 }
Mona Hossain05c73562012-10-29 17:49:01 -07002620 qseecom.commonlib_loaded = false;
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002621 qseecom.pdev = class_dev;
Mona Hossain2892b6b2012-02-17 13:53:11 -08002622 /* Create ION msm client */
Mona Hossaind44a3842012-10-15 09:41:35 -07002623 qseecom.ion_clnt = msm_ion_client_create(-1, "qseecom-kernel");
Mona Hossain2892b6b2012-02-17 13:53:11 -08002624 if (qseecom.ion_clnt == NULL) {
2625 pr_err("Ion client cannot be created\n");
2626 rc = -ENOMEM;
2627 goto err;
2628 }
2629
2630 /* register client for bus scaling */
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002631 if (pdev->dev.of_node) {
2632 ret = __qseecom_init_clk();
2633 if (ret)
2634 goto err;
Mona Hossaind39e33b2012-11-05 13:36:40 -08002635 ret = __qseecom_enable_clk();
2636 if (ret) {
2637 __qseecom_deinit_clk();
2638 goto err;
2639 }
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002640 qseecom_platform_support = (struct msm_bus_scale_pdata *)
2641 msm_bus_cl_get_pdata(pdev);
Mona Hossain5b76a622012-11-15 20:09:08 -08002642 if (qseecom.qsee_version >= (QSEE_VERSION_02)) {
2643 struct resource *resource = NULL;
2644 struct qsee_apps_region_info_ireq req;
2645 struct qseecom_command_scm_resp resp;
2646
2647 resource = platform_get_resource_byname(pdev,
2648 IORESOURCE_MEM, "secapp-region");
2649 if (resource) {
2650 req.qsee_cmd_id = QSEOS_APP_REGION_NOTIFICATION;
2651 req.addr = resource->start;
2652 req.size = resource_size(resource);
2653 pr_warn("secure app region addr=0x%x size=0x%x",
2654 req.addr, req.size);
2655 } else {
2656 pr_err("Fail to get secure app region info\n");
2657 rc = -EINVAL;
2658 goto err;
2659 }
2660 rc = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, sizeof(req),
2661 &resp, sizeof(resp));
2662 if (rc) {
2663 pr_err("Failed to send secapp region info %d\n",
2664 rc);
2665 goto err;
2666 }
2667 }
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002668 } else {
Ramesh Masavarapufb1f01e2012-06-14 09:40:40 -07002669 qseecom_platform_support = (struct msm_bus_scale_pdata *)
2670 pdev->dev.platform_data;
Mona Hossain2892b6b2012-02-17 13:53:11 -08002671 }
Ramesh Masavarapua1bc0e42012-03-05 07:42:48 -08002672
Ramesh Masavarapuff377032012-09-14 12:11:32 -07002673 qsee_perf_client = msm_bus_scale_register_client(
2674 qseecom_platform_support);
2675
2676 if (!qsee_perf_client)
2677 pr_err("Unable to register bus client\n");
2678 return 0;
Mona Hossain2892b6b2012-02-17 13:53:11 -08002679err:
2680 device_destroy(driver_class, qseecom_device_no);
2681class_destroy:
2682 class_destroy(driver_class);
2683unregister_chrdev_region:
2684 unregister_chrdev_region(qseecom_device_no, 1);
2685 return rc;
2686}
2687
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002688static int __devinit qseecom_remove(struct platform_device *pdev)
2689{
Mona Hossaind44a3842012-10-15 09:41:35 -07002690 struct qseecom_registered_kclient_list *kclient = NULL;
2691 unsigned long flags = 0;
2692 int ret = 0;
2693
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002694 if (pdev->dev.platform_data != NULL)
2695 msm_bus_scale_unregister_client(qsee_perf_client);
Mona Hossaind44a3842012-10-15 09:41:35 -07002696
2697 spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
2698 kclient = list_entry((&qseecom.registered_kclient_list_head)->next,
2699 struct qseecom_registered_kclient_list, list);
2700 if (list_empty(&kclient->list)) {
2701 spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock,
2702 flags);
2703 return 0;
2704 }
2705 list_for_each_entry(kclient, &qseecom.registered_kclient_list_head,
2706 list) {
2707 if (kclient)
2708 list_del(&kclient->list);
2709 break;
2710 }
2711 spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
2712
2713
2714 while (kclient->handle != NULL) {
2715 ret = qseecom_unload_app(kclient->handle->dev);
2716 if (ret == 0) {
2717 kzfree(kclient->handle->dev);
2718 kzfree(kclient->handle);
2719 kzfree(kclient);
2720 }
2721 spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
2722 kclient = list_entry(
2723 (&qseecom.registered_kclient_list_head)->next,
2724 struct qseecom_registered_kclient_list, list);
2725 if (list_empty(&kclient->list)) {
2726 spin_unlock_irqrestore(
2727 &qseecom.registered_kclient_list_lock, flags);
2728 return 0;
2729 }
2730 list_for_each_entry(kclient,
2731 &qseecom.registered_kclient_list_head, list) {
2732 if (kclient)
2733 list_del(&kclient->list);
2734 break;
2735 }
2736 spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock,
2737 flags);
2738 if (!kclient) {
2739 ret = 0;
2740 break;
2741 }
2742 }
Mona Hossain05c73562012-10-29 17:49:01 -07002743 if (qseecom.qseos_version > QSEEE_VERSION_00)
2744 qseecom_unload_commonlib_image();
Mona Hossaind39e33b2012-11-05 13:36:40 -08002745
2746 if (qsee_perf_client)
2747 msm_bus_scale_client_update_request(qsee_perf_client, 0);
2748 /* register client for bus scaling */
2749 if (pdev->dev.of_node) {
2750 __qseecom_disable_clk();
2751 __qseecom_deinit_clk();
2752 }
Mona Hossaind44a3842012-10-15 09:41:35 -07002753 return ret;
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002754};
2755
Ramesh Masavarapufb1f01e2012-06-14 09:40:40 -07002756static struct of_device_id qseecom_match[] = {
2757 {
2758 .compatible = "qcom,qseecom",
2759 },
2760 {}
2761};
2762
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002763static struct platform_driver qseecom_plat_driver = {
2764 .probe = qseecom_probe,
2765 .remove = qseecom_remove,
2766 .driver = {
2767 .name = "qseecom",
2768 .owner = THIS_MODULE,
Ramesh Masavarapufb1f01e2012-06-14 09:40:40 -07002769 .of_match_table = qseecom_match,
Ramesh Masavarapua26cce72012-04-09 12:32:25 -07002770 },
2771};
2772
2773static int __devinit qseecom_init(void)
2774{
2775 return platform_driver_register(&qseecom_plat_driver);
2776}
2777
2778static void __devexit qseecom_exit(void)
Mona Hossain2892b6b2012-02-17 13:53:11 -08002779{
Mona Hossain2892b6b2012-02-17 13:53:11 -08002780 device_destroy(driver_class, qseecom_device_no);
2781 class_destroy(driver_class);
2782 unregister_chrdev_region(qseecom_device_no, 1);
2783 ion_client_destroy(qseecom.ion_clnt);
2784}
2785
2786MODULE_LICENSE("GPL v2");
2787MODULE_DESCRIPTION("Qualcomm Secure Execution Environment Communicator");
2788
2789module_init(qseecom_init);
2790module_exit(qseecom_exit);