qseecom: Unload the app upon receiving qseecom_release.

Upon receiving qseecom_release() app corresponding to the app id
needs to be shutdown.This is applicable to user space clients as well.

Change-Id: Ie659a9409f8470b7ed4a5088e6779b50c5dc1cca
Signed-off-by: Hariprasad Dhalinarasimha <hnamgund@codeaurora.org>
(cherry picked from commit 1e34cdd714219a4cd0acb3d5b3dfe4280c380699)
qseecom: Fix issues with failure to release client handle

There exists scenarios when qseecom_release is called,
the client handle type is incorrect; hence proper cleanup
is not done with regards to unloading app or cleaning up
memory allocated for the device handle.
This change addresses all cleanup issues related to qseecom
client in qseecom_release call by ensuring the handle type is
set correctly and cleanup is done properly based on client type.

Change-Id: I126aa431ce635ae98100b774e3d92b704635d199
Signed-off-by: Hariprasad Dhalinarasimha <hnamgund@codeaurora.org>
(cherry picked from commit f12641d6733009c4cd502d3ba25cb3c5d61ab5e2)
qseecom: Replace kzalloc with ion_alloc

Allocate memory to common lib through kzalloc issues no page errors due to
higher order memory request. So allocate the memory from the heap specially
dedicated to QSEECOM.

Change-Id: Ice4a609d6791ce5adc84b9882231def31674f461
Signed-off-by: AnilKumar Chimata <anilc@codeaurora.org>
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Fix failure to load cmnlib image

Set the physical address field of load_req data struct
correctly using the right ion_phys API. Without this,
secure domain is not able to address/dereference memory
where cmnlib image resides.

Change-Id: I9f75e78227e4eee89fff9ad8b19701ff08e5d90a
Signed-off-by: Mona Hossain <mhossain@codeaurora.org>
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Check the return value of "ion_phys"

Check for return value of "ion_phys" and handle
the error cases appropriately.

Change-Id: Ib4f24af24de2b2553ed91479f46b2f79e0be0446
Signed-off-by: Hariprasad Dhalinarasimha <hnamgund@codeaurora.org>
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: register bus bandwidth mode when loading app

When there are multiple running qseecom clients, ce clock may be disabled
by one client while TZ is still loading another client's app, which will
lead to load app failure. So, we make a change to register bus bandwidth
mode before loading app and avoid to disable clock when current bandwidth
mode is not INACTIVE.

Change-Id: I1727f6fc038baf95da39089e1f0faefe42f3d40c
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: change bus scale up mode in resume function

The maximum bus bandwidth mode supported by bus bandwidth setting
function is 3 (i.e., HIGH), then to scale up bus bandwidth based on
cumulative mode may prevent us from updating the bus bandwidth
correctly when cumulative mode is greater than 3 (which is the case
if there are concurrent applications running). Thus, we made a
change to scale up bus bandwidth mode to at most 3 in qseecom
resume function.

Change-Id: I3ec6eedace2fa2ea7de519df307c3ff9b193c910
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: update cumulative_mode and timer properly

qseecom.cumulative_mode is used to track the cummulative bus request
mode from all concurrent running clients. It is registered before
send_cmd operation, but is not un-registered after it is complete.
This may cause the bus bandwidth is restored to a wrong mode after
resuming from sleep. Thus, we make change to update it accordingly.
qseecom.current_mode is also updated in suspend/resume function to
ensure timer can scale down bus bw after resume when cumulative_mode
is non-zero. Besides, timer operation is optimized in case there are
multiple unserialized concurrent users of the same timer.

Change-Id: I06043d2b88a47e24da03460fe1ae26a94cb6a608
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
misc: qseecom: fix wipe-key api for PFE key

The qseecom_wipe_key() api allows both FDE and PFE usage,
It is calling  __qseecom_delete_saved_key()  api
that should also allow both.

Change-Id: I2ae583977e7e4958e6dd8a939af74ac62a147895
Signed-off-by: Amir Samuelov <amirs@codeaurora.org>
qseecom: add support to clear key for per file encryption

Make a KMS change to support just clear pipe key without deleting it
from ssd storage. Key id definitions for FDE and PDE are also updated.

Change-Id: I7c4804902a7a734087374c81ff0fd95655d84429
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Add support for PFE feature

Add key management support for crypto HW used for
PFE (per file encryption) features.

Change-Id: Ic0664973d9a4099f76d7b3040f0f81df8465cf71
Signed-off-by: Mona Hossain <mhossain@codeaurora.org>
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Check for FDE/PFE feature

Add checks for PFE/FDE feature before executing anything
related to the feature.

Change-Id: I575d4d4eea29fd9ae59e47805793f2416a97f5b9
Signed-off-by: Mona Hossain <mhossain@codeaurora.org>
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: change return value to 0 if app id exists when loading app

Currently qseecom will return the app_id (>0) instead of 0 if the
app id already exists when loading app. Then the userspace client
will release qseecom handle if an non-zero return value is obtained.
Make a change to let qseecom return 0 when app id exist so as to
avoid the incorrect qseecom release.

Change-Id: I03b0b0ae95b2e043fda972aa3b613e7df585154a
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Add support to handle multiple app load & unload

Media server is associated with multiple apps on TZ-side.
Unloading a single app for clean up is not enought. QSEECOM
needs to handle the app load & unload of multiple apps
associated with the client. During app_load & app_unload
along with app_id, app_name should also be checked. Besides,
keymaster will not be unloaded and app search execution
during app unload is optimized.

Change-Id: Ie92e97b9154851b713a8c3394b8ed10fdbdebd2f
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
Signed-off-by: William Clark <wclark@codeaurora.org>
qseecom: change the number of bytes to compare app names

keymaster app has two names with different length: "keymaster" and
"keymaste". To compare the app names correctly, the shorter name
length is choosen for memcmp in __qseecom_send_cmd.

Change-Id: I5f0dff7f9756362be40cc64985b65bc2268d734f
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
Signed-off-by: William Clark <wclark@codeaurora.org>
qseecom: Check if ce clock is enabled before sending cmd to TZ

Make change to check if ce clock is enabled before sending command
to TZ, then to prevent data aborting if clock is not enabled for
TZ crypto operation.

CRs-Fixed: 726840
Change-Id: I55b5b10ad80e741c39f8cf411f67e6b83887f3bb
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Enable/disable clock for client if bus scaling is not enabled

To avoid the service failure on a target where bus scaling flag is not
enabled in target device tree file by mistake, make a change to enable/
disable crypto clocks when the client calls to scale up bus bandwidth
and send command even if bus scaling feature is not enabled.

CRs-Fixed: 726840
Change-Id: Ib33a535051b68561bde5ab6a23ad0f02dc27ab13
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: fix secure app loading failure by kernel mode qseecom client

Make a change to fix secure app loading failure due to premature
kzfree() from __qseecom_load_fw(), when kernel mode qseecom client
tries to load app.

CRs-fixed: 694541

Change-Id: I1ac39ee885c12e08e863d976321dda2380ace852
Signed-off-by: William Clark <wclark@codeaurora.org>
qseecom: Just check current bus scaling mode in qseecom_suspend

In current qseecom_suspend function, qseecom will scale down bus
bandwidth if both of accumultive mode and current mode are not
INACTIVE. But if device goes into suspend before bus scaling timeout
happens, the accumulative mode may be zero but current mode is
still not INACTIVE. This may prevent the device going to suspend,
thus, make a change to just check current bus scaling mode in
qseecom_suspend.

CRs-Fixed: 740287
Change-Id: I60e90000aa9efe428b144fb766e19e81312af66f
Signed-off-by: Zhen Kong <zkong@codeaurora.org>
qseecom: Free ion memory even if keymaster app is not unloaded

Make a change to free the ion memory allocated for keymaster client
even if keymaster app doesn't need to be unloaded from TZ, so as to
avoid memory leak.

CRs-Fixed: 724638
Change-Id: I3ddb68c8a3f96b0a44afc3efc63e93f0a8be51bb
Signed-off-by: Zhen Kong <zkong@codeaurora.org>