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