Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# For a description of the syntax of this configuration file,
# see Documentation/kbuild/kconfig-language.rst.
#
menu "Qualcomm firmware drivers"
config QCOM_SCM
select QCOM_TZMEM
tristate
config QCOM_TZMEM
tristate
select GENERIC_ALLOCATOR
choice
prompt "TrustZone interface memory allocator mode"
depends on QCOM_TZMEM
default QCOM_TZMEM_MODE_GENERIC
help
Selects the mode of the memory allocator providing memory buffers of
suitable format for sharing with the TrustZone. If in doubt, select
'Generic'.
config QCOM_TZMEM_MODE_GENERIC
bool "Generic"
help
Use the generic allocator mode. The memory is page-aligned, non-cachable
and physically contiguous.
config QCOM_TZMEM_MODE_SHMBRIDGE
bool "SHM Bridge"
help
Use Qualcomm Shared Memory Bridge. The memory has the same alignment as
in the 'Generic' allocator but is also explicitly marked as an SHM Bridge
buffer.
With this selected, all buffers passed to the TrustZone must be allocated
using the TZMem allocator or else the TrustZone will refuse to use them.
endchoice
config QCOM_QSEECOM
bool "Qualcomm QSEECOM interface driver"
depends on QCOM_SCM=y
select AUXILIARY_BUS
help
Various Qualcomm SoCs have a Secure Execution Environment (SEE) running
in the Trust Zone. This module provides an interface to that via the
QSEECOM mechanism, using SCM calls.
The QSEECOM interface allows, among other things, access to applications
running in the SEE. An example of such an application is 'uefisecapp',
which is required to access UEFI variables on certain systems. If
selected, the interface will also attempt to detect and register client
devices for supported applications.
Select Y here to enable the QSEECOM interface driver.
config QCOM_QSEECOM_UEFISECAPP
bool "Qualcomm SEE UEFI Secure App client driver"
depends on QCOM_QSEECOM
depends on EFI
help
Various Qualcomm SoCs do not allow direct access to EFI variables.
Instead, these need to be accessed via the UEFI Secure Application
(uefisecapp), residing in the Secure Execution Environment (SEE).
This module provides a client driver for uefisecapp, installing efivar
operations to allow the kernel accessing EFI variables, and via that also
provide user-space with access to EFI variables via efivarfs.
Select Y here to provide access to EFI variables on the aforementioned
platforms.
endmenu
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the linux kernel.
#
obj-$(CONFIG_QCOM_SCM) += qcom-scm.o
qcom-scm-objs += qcom_scm.o qcom_scm-smc.o qcom_scm-legacy.o
obj-$(CONFIG_QCOM_TZMEM) += qcom_tzmem.o
obj-$(CONFIG_QCOM_QSEECOM) += qcom_qseecom.o
obj-$(CONFIG_QCOM_QSEECOM_UEFISECAPP) += qcom_qseecom_uefisecapp.o
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for Qualcomm Secure Execution Environment (SEE) interface (QSEECOM).
* Responsible for setting up and managing QSEECOM client devices.
*
* Copyright (C) 2023 Maximilian Luz <luzmaximilian@gmail.com>
*/
#include <linux/auxiliary_bus.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/firmware/qcom/qcom_qseecom.h>
#include <linux/firmware/qcom/qcom_scm.h>
struct qseecom_app_desc {
const char *app_name;
const char *dev_name;
};
static void qseecom_client_release(struct device *dev)
{
struct qseecom_client *client;
client = container_of(dev, struct qseecom_client, aux_dev.dev);
kfree(client);
}
static void qseecom_client_remove(void *data)
{
struct qseecom_client *client = data;
auxiliary_device_delete(&client->aux_dev);
auxiliary_device_uninit(&client->aux_dev);
}
static int qseecom_client_register(struct platform_device *qseecom_dev,
const struct qseecom_app_desc *desc)
{
struct qseecom_client *client;
u32 app_id;
int ret;
/* Try to find the app ID, skip device if not found */
ret = qcom_scm_qseecom_app_get_id(desc->app_name, &app_id);
if (ret)
return ret == -ENOENT ? 0 : ret;
dev_info(&qseecom_dev->dev, "setting up client for %s\n", desc->app_name);
/* Allocate and set-up the client device */
client = kzalloc_obj(*client);
if (!client)
return -ENOMEM;
client->aux_dev.name = desc->dev_name;
client->aux_dev.dev.parent = &qseecom_dev->dev;
client->aux_dev.dev.release = qseecom_client_release;
client->app_id = app_id;
ret = auxiliary_device_init(&client->aux_dev);
if (ret) {
kfree(client);
return ret;
}
ret = auxiliary_device_add(&client->aux_dev);
if (ret) {
auxiliary_device_uninit(&client->aux_dev);
return ret;
}
ret = devm_add_action_or_reset(&qseecom_dev->dev, qseecom_client_remove, client);
if (ret)
return ret;
return 0;
}
/*
* List of supported applications. One client device will be created per entry,
* assuming the app has already been loaded (usually by firmware bootloaders)
* and its ID can be queried successfully.
*/
static const struct qseecom_app_desc qcom_qseecom_apps[] = {
{ "qcom.tz.uefisecapp", "uefisecapp" },
};
static int qcom_qseecom_probe(struct platform_device *qseecom_dev)
{
int ret;
int i;
/* Set up client devices for each base application */
for (i = 0; i < ARRAY_SIZE(qcom_qseecom_apps); i++) {
ret = qseecom_client_register(qseecom_dev, &qcom_qseecom_apps[i]);
if (ret)
return ret;
}
return 0;
}
static struct platform_driver qcom_qseecom_driver = {
.driver = {
.name = "qcom_qseecom",
},
.probe = qcom_qseecom_probe,
};
static int __init qcom_qseecom_init(void)
{
return platform_driver_register(&qcom_qseecom_driver);
}
subsys_initcall(qcom_qseecom_init);
MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
MODULE_DESCRIPTION("Driver for the Qualcomm SEE (QSEECOM) interface");
MODULE_LICENSE("GPL");
@@ -0,0 +1,865 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Client driver for Qualcomm UEFI Secure Application (qcom.tz.uefisecapp).
* Provides access to UEFI variables on platforms where they are secured by the
* aforementioned Secure Execution Environment (SEE) application.
*
* Copyright (C) 2023 Maximilian Luz <luzmaximilian@gmail.com>
*/
#include <linux/efi.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/ucs2_string.h>
#include <linux/firmware/qcom/qcom_qseecom.h>
#include <linux/firmware/qcom/qcom_scm.h>
#include <linux/firmware/qcom/qcom_tzmem.h>
/* -- Qualcomm "uefisecapp" interface definitions. -------------------------- */
/* Maximum length of name string with null-terminator */
#define QSEE_MAX_NAME_LEN 1024
#define QSEE_CMD_UEFI(x) (0x8000 | (x))
#define QSEE_CMD_UEFI_GET_VARIABLE QSEE_CMD_UEFI(0)
#define QSEE_CMD_UEFI_SET_VARIABLE QSEE_CMD_UEFI(1)
#define QSEE_CMD_UEFI_GET_NEXT_VARIABLE QSEE_CMD_UEFI(2)
#define QSEE_CMD_UEFI_QUERY_VARIABLE_INFO QSEE_CMD_UEFI(3)
/**
* struct qsee_req_uefi_get_variable - Request for GetVariable command.
* @command_id: The ID of the command. Must be %QSEE_CMD_UEFI_GET_VARIABLE.
* @length: Length of the request in bytes, including this struct and any
* parameters (name, GUID) stored after it as well as any padding
* thereof for alignment.
* @name_offset: Offset from the start of this struct to where the variable
* name is stored (as utf-16 string), in bytes.
* @name_size: Size of the name parameter in bytes, including null-terminator.
* @guid_offset: Offset from the start of this struct to where the GUID
* parameter is stored, in bytes.
* @guid_size: Size of the GUID parameter in bytes, i.e. sizeof(efi_guid_t).
* @data_size: Size of the output buffer, in bytes.
*/
struct qsee_req_uefi_get_variable {
u32 command_id;
u32 length;
u32 name_offset;
u32 name_size;
u32 guid_offset;
u32 guid_size;
u32 data_size;
} __packed;
/**
* struct qsee_rsp_uefi_get_variable - Response for GetVariable command.
* @command_id: The ID of the command. Should be %QSEE_CMD_UEFI_GET_VARIABLE.
* @length: Length of the response in bytes, including this struct and the
* returned data.
* @status: Status of this command.
* @attributes: EFI variable attributes.
* @data_offset: Offset from the start of this struct to where the data is
* stored, in bytes.
* @data_size: Size of the returned data, in bytes. In case status indicates
* that the buffer is too small, this will be the size required
* to store the EFI variable data.
*/
struct qsee_rsp_uefi_get_variable {
u32 command_id;
u32 length;
u32 status;
u32 attributes;
u32 data_offset;
u32 data_size;
} __packed;
/**
* struct qsee_req_uefi_set_variable - Request for the SetVariable command.
* @command_id: The ID of the command. Must be %QSEE_CMD_UEFI_SET_VARIABLE.
* @length: Length of the request in bytes, including this struct and any
* parameters (name, GUID, data) stored after it as well as any
* padding thereof required for alignment.
* @name_offset: Offset from the start of this struct to where the variable
* name is stored (as utf-16 string), in bytes.
* @name_size: Size of the name parameter in bytes, including null-terminator.
* @guid_offset: Offset from the start of this struct to where the GUID
* parameter is stored, in bytes.
* @guid_size: Size of the GUID parameter in bytes, i.e. sizeof(efi_guid_t).
* @attributes: The EFI variable attributes to set for this variable.
* @data_offset: Offset from the start of this struct to where the EFI variable
* data is stored, in bytes.
* @data_size: Size of EFI variable data, in bytes.
*
*/
struct qsee_req_uefi_set_variable {
u32 command_id;
u32 length;
u32 name_offset;
u32 name_size;
u32 guid_offset;
u32 guid_size;
u32 attributes;
u32 data_offset;
u32 data_size;
} __packed;
/**
* struct qsee_rsp_uefi_set_variable - Response for the SetVariable command.
* @command_id: The ID of the command. Should be %QSEE_CMD_UEFI_SET_VARIABLE.
* @length: The length of this response, i.e. the size of this struct in
* bytes.
* @status: Status of this command.
* @_unknown1: Unknown response field.
* @_unknown2: Unknown response field.
*/
struct qsee_rsp_uefi_set_variable {
u32 command_id;
u32 length;
u32 status;
u32 _unknown1;
u32 _unknown2;
} __packed;
/**
* struct qsee_req_uefi_get_next_variable - Request for the
* GetNextVariableName command.
* @command_id: The ID of the command. Must be
* %QSEE_CMD_UEFI_GET_NEXT_VARIABLE.
* @length: Length of the request in bytes, including this struct and any
* parameters (name, GUID) stored after it as well as any padding
* thereof for alignment.
* @guid_offset: Offset from the start of this struct to where the GUID
* parameter is stored, in bytes.
* @guid_size: Size of the GUID parameter in bytes, i.e. sizeof(efi_guid_t).
* @name_offset: Offset from the start of this struct to where the variable
* name is stored (as utf-16 string), in bytes.
* @name_size: Size of the name parameter in bytes, including null-terminator.
*/
struct qsee_req_uefi_get_next_variable {
u32 command_id;
u32 length;
u32 guid_offset;
u32 guid_size;
u32 name_offset;
u32 name_size;
} __packed;
/**
* struct qsee_rsp_uefi_get_next_variable - Response for the
* GetNextVariableName command.
* @command_id: The ID of the command. Should be
* %QSEE_CMD_UEFI_GET_NEXT_VARIABLE.
* @length: Length of the response in bytes, including this struct and any
* parameters (name, GUID) stored after it as well as any padding
* thereof for alignment.
* @status: Status of this command.
* @guid_offset: Offset from the start of this struct to where the GUID
* parameter is stored, in bytes.
* @guid_size: Size of the GUID parameter in bytes, i.e. sizeof(efi_guid_t).
* @name_offset: Offset from the start of this struct to where the variable
* name is stored (as utf-16 string), in bytes.
* @name_size: Size of the name parameter in bytes, including null-terminator.
*/
struct qsee_rsp_uefi_get_next_variable {
u32 command_id;
u32 length;
u32 status;
u32 guid_offset;
u32 guid_size;
u32 name_offset;
u32 name_size;
} __packed;
/**
* struct qsee_req_uefi_query_variable_info - Response for the
* GetNextVariableName command.
* @command_id: The ID of the command. Must be
* %QSEE_CMD_UEFI_QUERY_VARIABLE_INFO.
* @length: The length of this request, i.e. the size of this struct in
* bytes.
* @attributes: The storage attributes to query the info for.
*/
struct qsee_req_uefi_query_variable_info {
u32 command_id;
u32 length;
u32 attributes;
} __packed;
/**
* struct qsee_rsp_uefi_query_variable_info - Response for the
* GetNextVariableName command.
* @command_id: The ID of the command. Must be
* %QSEE_CMD_UEFI_QUERY_VARIABLE_INFO.
* @length: The length of this response, i.e. the size of this
* struct in bytes.
* @status: Status of this command.
* @_pad: Padding.
* @storage_space: Full storage space size, in bytes.
* @remaining_space: Free storage space available, in bytes.
* @max_variable_size: Maximum variable data size, in bytes.
*/
struct qsee_rsp_uefi_query_variable_info {
u32 command_id;
u32 length;
u32 status;
u32 _pad;
u64 storage_space;
u64 remaining_space;
u64 max_variable_size;
} __packed;
/* -- Alignment helpers ----------------------------------------------------- */
/*
* Helper macro to ensure proper alignment of types (fields and arrays) when
* stored in some (contiguous) buffer.
*
* Note: The driver from which this one has been reverse-engineered expects an
* alignment of 8 bytes (64 bits) for GUIDs. Our definition of efi_guid_t,
* however, has an alignment of 4 byte (32 bits). So far, this seems to work
* fine here. See also the comment on the typedef of efi_guid_t.
*
* Note: It looks like uefisecapp is quite picky about how the memory passed to
* it is structured and aligned. In particular the request/response setup used
* for QSEE_CMD_UEFI_GET_VARIABLE. While qcom_qseecom_app_send(), in theory,
* accepts separate buffers/addresses for the request and response parts, in
* practice, however, it seems to expect them to be both part of a larger
* contiguous block. We initially allocated separate buffers for the request
* and response but this caused the QSEE_CMD_UEFI_GET_VARIABLE command to
* either not write any response to the response buffer or outright crash the
* device. Therefore, we now allocate a single contiguous block of DMA memory
* for both and properly align the data using the macros below. In particular,
* request and response structs are aligned at 8 byte (via __reqdata_offs()),
* following the driver that this has been reverse-engineered from.
*/
#define qcuefi_buf_align_fields(fields...) \
({ \
size_t __len = 0; \
fields \
__len; \
})
#define __field_impl(size, align, offset) \
({ \
size_t *__offset = (offset); \
size_t __aligned; \
\
__aligned = ALIGN(__len, align); \
__len = __aligned + (size); \
\
if (__offset) \
*__offset = __aligned; \
});
#define __array_offs(type, count, offset) \
__field_impl(sizeof(type) * (count), __alignof__(type), offset)
#define __array_offs_aligned(type, count, align, offset) \
__field_impl(sizeof(type) * (count), align, offset)
#define __reqdata_offs(size, offset) \
__array_offs_aligned(u8, size, 8, offset)
#define __array(type, count) __array_offs(type, count, NULL)
#define __field_offs(type, offset) __array_offs(type, 1, offset)
#define __field(type) __array_offs(type, 1, NULL)
/* -- UEFI app interface. --------------------------------------------------- */
struct qcuefi_client {
struct qseecom_client *client;
struct efivars efivars;
struct qcom_tzmem_pool *mempool;
};
static struct device *qcuefi_dev(struct qcuefi_client *qcuefi)
{
return &qcuefi->client->aux_dev.dev;
}
static efi_status_t qsee_uefi_status_to_efi(u32 status)
{
u64 category = status & 0xf0000000;
u64 code = status & 0x0fffffff;
return category << (BITS_PER_LONG - 32) | code;
}
static efi_status_t qsee_uefi_get_variable(struct qcuefi_client *qcuefi, const efi_char16_t *name,
const efi_guid_t *guid, u32 *attributes,
unsigned long *data_size, void *data)
{
struct qsee_req_uefi_get_variable *req_data;
struct qsee_rsp_uefi_get_variable *rsp_data;
void *cmd_buf __free(qcom_tzmem) = NULL;
unsigned long buffer_size = *data_size;
unsigned long name_length;
efi_status_t efi_status;
size_t cmd_buf_size;
size_t guid_offs;
size_t name_offs;
size_t req_size;
size_t rsp_size;
size_t req_offs;
size_t rsp_offs;
ssize_t status;
if (!name || !guid)
return EFI_INVALID_PARAMETER;
name_length = ucs2_strnlen(name, QSEE_MAX_NAME_LEN) + 1;
if (name_length > QSEE_MAX_NAME_LEN)
return EFI_INVALID_PARAMETER;
if (buffer_size && !data)
return EFI_INVALID_PARAMETER;
req_size = qcuefi_buf_align_fields(
__field(*req_data)
__array_offs(*name, name_length, &name_offs)
__field_offs(*guid, &guid_offs)
);
rsp_size = qcuefi_buf_align_fields(
__field(*rsp_data)
__array(u8, buffer_size)
);
cmd_buf_size = qcuefi_buf_align_fields(
__reqdata_offs(req_size, &req_offs)
__reqdata_offs(rsp_size, &rsp_offs)
);
cmd_buf = qcom_tzmem_alloc(qcuefi->mempool, cmd_buf_size, GFP_KERNEL);
if (!cmd_buf)
return EFI_OUT_OF_RESOURCES;
req_data = cmd_buf + req_offs;
rsp_data = cmd_buf + rsp_offs;
req_data->command_id = QSEE_CMD_UEFI_GET_VARIABLE;
req_data->data_size = buffer_size;
req_data->name_offset = name_offs;
req_data->name_size = name_length * sizeof(*name);
req_data->guid_offset = guid_offs;
req_data->guid_size = sizeof(*guid);
req_data->length = req_size;
status = ucs2_strscpy(((void *)req_data) + req_data->name_offset, name, name_length);
if (status < 0)
return EFI_INVALID_PARAMETER;
memcpy(((void *)req_data) + req_data->guid_offset, guid, req_data->guid_size);
status = qcom_qseecom_app_send(qcuefi->client,
cmd_buf + req_offs, req_size,
cmd_buf + rsp_offs, rsp_size);
if (status)
return EFI_DEVICE_ERROR;
if (rsp_data->command_id != QSEE_CMD_UEFI_GET_VARIABLE)
return EFI_DEVICE_ERROR;
if (rsp_data->length < sizeof(*rsp_data))
return EFI_DEVICE_ERROR;
if (rsp_data->status) {
dev_dbg(qcuefi_dev(qcuefi), "%s: uefisecapp error: 0x%x\n",
__func__, rsp_data->status);
efi_status = qsee_uefi_status_to_efi(rsp_data->status);
/* Update size and attributes in case buffer is too small. */
if (efi_status == EFI_BUFFER_TOO_SMALL) {
*data_size = rsp_data->data_size;
if (attributes)
*attributes = rsp_data->attributes;
}
return qsee_uefi_status_to_efi(rsp_data->status);
}
if (rsp_data->length > rsp_size)
return EFI_DEVICE_ERROR;
if (rsp_data->data_offset + rsp_data->data_size > rsp_data->length)
return EFI_DEVICE_ERROR;
/*
* Note: We need to set attributes and data size even if the buffer is
* too small and we won't copy any data. This is described in spec, so
* that callers can either allocate a buffer properly (with two calls
* to this function) or just read back attributes withouth having to
* deal with that.
*
* Specifically:
* - If we have a buffer size of zero and no buffer, just return the
* attributes, required size, and indicate success.
* - If the buffer size is nonzero but too small, indicate that as an
* error.
* - Otherwise, we are good to copy the data.
*
* Note that we have already ensured above that the buffer pointer is
* non-NULL if its size is nonzero.
*/
*data_size = rsp_data->data_size;
if (attributes)
*attributes = rsp_data->attributes;
if (buffer_size == 0 && !data)
return EFI_SUCCESS;
if (buffer_size < rsp_data->data_size)
return EFI_BUFFER_TOO_SMALL;
memcpy(data, ((void *)rsp_data) + rsp_data->data_offset, rsp_data->data_size);
return EFI_SUCCESS;
}
static efi_status_t qsee_uefi_set_variable(struct qcuefi_client *qcuefi, const efi_char16_t *name,
const efi_guid_t *guid, u32 attributes,
unsigned long data_size, const void *data)
{
struct qsee_req_uefi_set_variable *req_data;
struct qsee_rsp_uefi_set_variable *rsp_data;
void *cmd_buf __free(qcom_tzmem) = NULL;
unsigned long name_length;
size_t cmd_buf_size;
size_t name_offs;
size_t guid_offs;
size_t data_offs;
size_t req_size;
size_t req_offs;
size_t rsp_offs;
ssize_t status;
if (!name || !guid)
return EFI_INVALID_PARAMETER;
name_length = ucs2_strnlen(name, QSEE_MAX_NAME_LEN) + 1;
if (name_length > QSEE_MAX_NAME_LEN)
return EFI_INVALID_PARAMETER;
/*
* Make sure we have some data if data_size is nonzero. Note that using
* a size of zero is a valid use-case described in spec and deletes the
* variable.
*/
if (data_size && !data)
return EFI_INVALID_PARAMETER;
req_size = qcuefi_buf_align_fields(
__field(*req_data)
__array_offs(*name, name_length, &name_offs)
__field_offs(*guid, &guid_offs)
__array_offs(u8, data_size, &data_offs)
);
cmd_buf_size = qcuefi_buf_align_fields(
__reqdata_offs(req_size, &req_offs)
__reqdata_offs(sizeof(*rsp_data), &rsp_offs)
);
cmd_buf = qcom_tzmem_alloc(qcuefi->mempool, cmd_buf_size, GFP_KERNEL);
if (!cmd_buf)
return EFI_OUT_OF_RESOURCES;
req_data = cmd_buf + req_offs;
rsp_data = cmd_buf + rsp_offs;
req_data->command_id = QSEE_CMD_UEFI_SET_VARIABLE;
req_data->attributes = attributes;
req_data->name_offset = name_offs;
req_data->name_size = name_length * sizeof(*name);
req_data->guid_offset = guid_offs;
req_data->guid_size = sizeof(*guid);
req_data->data_offset = data_offs;
req_data->data_size = data_size;
req_data->length = req_size;
status = ucs2_strscpy(((void *)req_data) + req_data->name_offset, name, name_length);
if (status < 0)
return EFI_INVALID_PARAMETER;
memcpy(((void *)req_data) + req_data->guid_offset, guid, req_data->guid_size);
if (data_size)
memcpy(((void *)req_data) + req_data->data_offset, data, req_data->data_size);
status = qcom_qseecom_app_send(qcuefi->client,
cmd_buf + req_offs, req_size,
cmd_buf + rsp_offs, sizeof(*rsp_data));
if (status)
return EFI_DEVICE_ERROR;
if (rsp_data->command_id != QSEE_CMD_UEFI_SET_VARIABLE)
return EFI_DEVICE_ERROR;
if (rsp_data->length != sizeof(*rsp_data))
return EFI_DEVICE_ERROR;
if (rsp_data->status) {
dev_dbg(qcuefi_dev(qcuefi), "%s: uefisecapp error: 0x%x\n",
__func__, rsp_data->status);
return qsee_uefi_status_to_efi(rsp_data->status);
}
return EFI_SUCCESS;
}
static efi_status_t qsee_uefi_get_next_variable(struct qcuefi_client *qcuefi,
unsigned long *name_size, efi_char16_t *name,
efi_guid_t *guid)
{
struct qsee_req_uefi_get_next_variable *req_data;
struct qsee_rsp_uefi_get_next_variable *rsp_data;
void *cmd_buf __free(qcom_tzmem) = NULL;
efi_status_t efi_status;
size_t cmd_buf_size;
size_t guid_offs;
size_t name_offs;
size_t req_size;
size_t rsp_size;
size_t req_offs;
size_t rsp_offs;
ssize_t status;
if (!name_size || !name || !guid)
return EFI_INVALID_PARAMETER;
if (*name_size == 0)
return EFI_INVALID_PARAMETER;
req_size = qcuefi_buf_align_fields(
__field(*req_data)
__field_offs(*guid, &guid_offs)
__array_offs(*name, *name_size / sizeof(*name), &name_offs)
);
rsp_size = qcuefi_buf_align_fields(
__field(*rsp_data)
__field(*guid)
__array(*name, *name_size / sizeof(*name))
);
cmd_buf_size = qcuefi_buf_align_fields(
__reqdata_offs(req_size, &req_offs)
__reqdata_offs(rsp_size, &rsp_offs)
);
cmd_buf = qcom_tzmem_alloc(qcuefi->mempool, cmd_buf_size, GFP_KERNEL);
if (!cmd_buf)
return EFI_OUT_OF_RESOURCES;
req_data = cmd_buf + req_offs;
rsp_data = cmd_buf + rsp_offs;
req_data->command_id = QSEE_CMD_UEFI_GET_NEXT_VARIABLE;
req_data->guid_offset = guid_offs;
req_data->guid_size = sizeof(*guid);
req_data->name_offset = name_offs;
req_data->name_size = *name_size;
req_data->length = req_size;
memcpy(((void *)req_data) + req_data->guid_offset, guid, req_data->guid_size);
status = ucs2_strscpy(((void *)req_data) + req_data->name_offset, name,
*name_size / sizeof(*name));
if (status < 0)
return EFI_INVALID_PARAMETER;
status = qcom_qseecom_app_send(qcuefi->client,
cmd_buf + req_offs, req_size,
cmd_buf + rsp_offs, rsp_size);
if (status)
return EFI_DEVICE_ERROR;
if (rsp_data->command_id != QSEE_CMD_UEFI_GET_NEXT_VARIABLE)
return EFI_DEVICE_ERROR;
if (rsp_data->length < sizeof(*rsp_data))
return EFI_DEVICE_ERROR;
if (rsp_data->status) {
dev_dbg(qcuefi_dev(qcuefi), "%s: uefisecapp error: 0x%x\n",
__func__, rsp_data->status);
efi_status = qsee_uefi_status_to_efi(rsp_data->status);
/*
* If the buffer to hold the name is too small, update the
* name_size with the required size, so that callers can
* reallocate it accordingly.
*/
if (efi_status == EFI_BUFFER_TOO_SMALL)
*name_size = rsp_data->name_size;
return efi_status;
}
if (rsp_data->length > rsp_size)
return EFI_DEVICE_ERROR;
if (rsp_data->name_offset + rsp_data->name_size > rsp_data->length)
return EFI_DEVICE_ERROR;
if (rsp_data->guid_offset + rsp_data->guid_size > rsp_data->length)
return EFI_DEVICE_ERROR;
if (rsp_data->name_size > *name_size) {
*name_size = rsp_data->name_size;
return EFI_BUFFER_TOO_SMALL;
}
if (rsp_data->guid_size != sizeof(*guid))
return EFI_DEVICE_ERROR;
memcpy(guid, ((void *)rsp_data) + rsp_data->guid_offset, rsp_data->guid_size);
status = ucs2_strscpy(name, ((void *)rsp_data) + rsp_data->name_offset,
rsp_data->name_size / sizeof(*name));
*name_size = rsp_data->name_size;
if (status < 0)
/*
* Return EFI_DEVICE_ERROR here because the buffer size should
* have already been validated above, causing this function to
* bail with EFI_BUFFER_TOO_SMALL.
*/
return EFI_DEVICE_ERROR;
return EFI_SUCCESS;
}
static efi_status_t qsee_uefi_query_variable_info(struct qcuefi_client *qcuefi, u32 attr,
u64 *storage_space, u64 *remaining_space,
u64 *max_variable_size)
{
struct qsee_req_uefi_query_variable_info *req_data;
struct qsee_rsp_uefi_query_variable_info *rsp_data;
void *cmd_buf __free(qcom_tzmem) = NULL;
size_t cmd_buf_size;
size_t req_offs;
size_t rsp_offs;
int status;
cmd_buf_size = qcuefi_buf_align_fields(
__reqdata_offs(sizeof(*req_data), &req_offs)
__reqdata_offs(sizeof(*rsp_data), &rsp_offs)
);
cmd_buf = qcom_tzmem_alloc(qcuefi->mempool, cmd_buf_size, GFP_KERNEL);
if (!cmd_buf)
return EFI_OUT_OF_RESOURCES;
req_data = cmd_buf + req_offs;
rsp_data = cmd_buf + rsp_offs;
req_data->command_id = QSEE_CMD_UEFI_QUERY_VARIABLE_INFO;
req_data->attributes = attr;
req_data->length = sizeof(*req_data);
status = qcom_qseecom_app_send(qcuefi->client,
cmd_buf + req_offs, sizeof(*req_data),
cmd_buf + rsp_offs, sizeof(*rsp_data));
if (status)
return EFI_DEVICE_ERROR;
if (rsp_data->command_id != QSEE_CMD_UEFI_QUERY_VARIABLE_INFO)
return EFI_DEVICE_ERROR;
if (rsp_data->length != sizeof(*rsp_data))
return EFI_DEVICE_ERROR;
if (rsp_data->status) {
dev_dbg(qcuefi_dev(qcuefi), "%s: uefisecapp error: 0x%x\n",
__func__, rsp_data->status);
return qsee_uefi_status_to_efi(rsp_data->status);
}
if (storage_space)
*storage_space = rsp_data->storage_space;
if (remaining_space)
*remaining_space = rsp_data->remaining_space;
if (max_variable_size)
*max_variable_size = rsp_data->max_variable_size;
return EFI_SUCCESS;
}
/* -- Global efivar interface. ---------------------------------------------- */
static struct qcuefi_client *__qcuefi;
static DEFINE_MUTEX(__qcuefi_lock);
static int qcuefi_set_reference(struct qcuefi_client *qcuefi)
{
guard(mutex)(&__qcuefi_lock);
if (qcuefi && __qcuefi)
return -EEXIST;
__qcuefi = qcuefi;
return 0;
}
static struct qcuefi_client *qcuefi_acquire(void)
__acquires(__qcuefi_lock)
{
mutex_lock(&__qcuefi_lock);
if (!__qcuefi) {
mutex_unlock(&__qcuefi_lock);
return NULL;
}
return __qcuefi;
}
static void qcuefi_release(void)
__releases(__qcuefi_lock)
{
mutex_unlock(&__qcuefi_lock);
}
static efi_status_t qcuefi_get_variable(efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
unsigned long *data_size, void *data)
{
struct qcuefi_client *qcuefi;
efi_status_t status;
qcuefi = qcuefi_acquire();
if (!qcuefi)
return EFI_NOT_READY;
status = qsee_uefi_get_variable(qcuefi, name, vendor, attr, data_size, data);
qcuefi_release();
return status;
}
static efi_status_t qcuefi_set_variable(efi_char16_t *name, efi_guid_t *vendor,
u32 attr, unsigned long data_size, void *data)
{
struct qcuefi_client *qcuefi;
efi_status_t status;
qcuefi = qcuefi_acquire();
if (!qcuefi)
return EFI_NOT_READY;
status = qsee_uefi_set_variable(qcuefi, name, vendor, attr, data_size, data);
qcuefi_release();
return status;
}
static efi_status_t qcuefi_get_next_variable(unsigned long *name_size, efi_char16_t *name,
efi_guid_t *vendor)
{
struct qcuefi_client *qcuefi;
efi_status_t status;
qcuefi = qcuefi_acquire();
if (!qcuefi)
return EFI_NOT_READY;
status = qsee_uefi_get_next_variable(qcuefi, name_size, name, vendor);
qcuefi_release();
return status;
}
static efi_status_t qcuefi_query_variable_info(u32 attr, u64 *storage_space, u64 *remaining_space,
u64 *max_variable_size)
{
struct qcuefi_client *qcuefi;
efi_status_t status;
qcuefi = qcuefi_acquire();
if (!qcuefi)
return EFI_NOT_READY;
status = qsee_uefi_query_variable_info(qcuefi, attr, storage_space, remaining_space,
max_variable_size);
qcuefi_release();
return status;
}
static const struct efivar_operations qcom_efivar_ops = {
.get_variable = qcuefi_get_variable,
.set_variable = qcuefi_set_variable,
.get_next_variable = qcuefi_get_next_variable,
.query_variable_info = qcuefi_query_variable_info,
};
/* -- Driver setup. --------------------------------------------------------- */
static int qcom_uefisecapp_probe(struct auxiliary_device *aux_dev,
const struct auxiliary_device_id *aux_dev_id)
{
struct qcom_tzmem_pool_config pool_config;
struct qcuefi_client *qcuefi;
int status;
qcuefi = devm_kzalloc(&aux_dev->dev, sizeof(*qcuefi), GFP_KERNEL);
if (!qcuefi)
return -ENOMEM;
qcuefi->client = container_of(aux_dev, struct qseecom_client, aux_dev);
memset(&pool_config, 0, sizeof(pool_config));
pool_config.initial_size = SZ_4K;
pool_config.policy = QCOM_TZMEM_POLICY_MULTIPLIER;
pool_config.increment = 2;
pool_config.max_size = SZ_256K;
qcuefi->mempool = devm_qcom_tzmem_pool_new(&aux_dev->dev, &pool_config);
if (IS_ERR(qcuefi->mempool))
return PTR_ERR(qcuefi->mempool);
auxiliary_set_drvdata(aux_dev, qcuefi);
status = qcuefi_set_reference(qcuefi);
if (status)
return status;
status = efivars_register(&qcuefi->efivars, &qcom_efivar_ops);
if (status)
qcuefi_set_reference(NULL);
return status;
}
static void qcom_uefisecapp_remove(struct auxiliary_device *aux_dev)
{
struct qcuefi_client *qcuefi = auxiliary_get_drvdata(aux_dev);
efivars_unregister(&qcuefi->efivars);
qcuefi_set_reference(NULL);
}
static const struct auxiliary_device_id qcom_uefisecapp_id_table[] = {
{ .name = "qcom_qseecom.uefisecapp" },
{}
};
MODULE_DEVICE_TABLE(auxiliary, qcom_uefisecapp_id_table);
static struct auxiliary_driver qcom_uefisecapp_driver = {
.probe = qcom_uefisecapp_probe,
.remove = qcom_uefisecapp_remove,
.id_table = qcom_uefisecapp_id_table,
.driver = {
.name = "qcom_qseecom_uefisecapp",
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
},
};
module_auxiliary_driver(qcom_uefisecapp_driver);
MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
MODULE_DESCRIPTION("Client driver for Qualcomm SEE UEFI Secure App");
MODULE_LICENSE("GPL");
@@ -0,0 +1,246 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved.
* Copyright (C) 2015 Linaro Ltd.
*/
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/firmware/qcom/qcom_scm.h>
#include <linux/arm-smccc.h>
#include <linux/dma-mapping.h>
#include "qcom_scm.h"
static DEFINE_MUTEX(qcom_scm_lock);
/**
* struct arm_smccc_args
* @args: The array of values used in registers in smc instruction
*/
struct arm_smccc_args {
unsigned long args[8];
};
/**
* struct scm_legacy_command - one SCM command buffer
* @len: total available memory for command and response
* @buf_offset: start of command buffer
* @resp_hdr_offset: start of response buffer
* @id: command to be executed
* @buf: buffer returned from scm_legacy_get_command_buffer()
*
* An SCM command is laid out in memory as follows:
*
* ------------------- <--- struct scm_legacy_command
* | command header |
* ------------------- <--- scm_legacy_get_command_buffer()
* | command buffer |
* ------------------- <--- struct scm_legacy_response and
* | response header | scm_legacy_command_to_response()
* ------------------- <--- scm_legacy_get_response_buffer()
* | response buffer |
* -------------------
*
* There can be arbitrary padding between the headers and buffers so
* you should always use the appropriate scm_legacy_get_*_buffer() routines
* to access the buffers in a safe manner.
*/
struct scm_legacy_command {
__le32 len;
__le32 buf_offset;
__le32 resp_hdr_offset;
__le32 id;
__le32 buf[];
};
/**
* struct scm_legacy_response - one SCM response buffer
* @len: total available memory for response
* @buf_offset: start of response data relative to start of scm_legacy_response
* @is_complete: indicates if the command has finished processing
*/
struct scm_legacy_response {
__le32 len;
__le32 buf_offset;
__le32 is_complete;
};
/**
* scm_legacy_command_to_response() - Get a pointer to a scm_legacy_response
* @cmd: command
*
* Returns a pointer to a response for a command.
*/
static inline struct scm_legacy_response *scm_legacy_command_to_response(
const struct scm_legacy_command *cmd)
{
return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
}
/**
* scm_legacy_get_command_buffer() - Get a pointer to a command buffer
* @cmd: command
*
* Returns a pointer to the command buffer of a command.
*/
static inline void *scm_legacy_get_command_buffer(
const struct scm_legacy_command *cmd)
{
return (void *)cmd->buf;
}
/**
* scm_legacy_get_response_buffer() - Get a pointer to a response buffer
* @rsp: response
*
* Returns a pointer to a response buffer of a response.
*/
static inline void *scm_legacy_get_response_buffer(
const struct scm_legacy_response *rsp)
{
return (void *)rsp + le32_to_cpu(rsp->buf_offset);
}
static void __scm_legacy_do(const struct arm_smccc_args *smc,
struct arm_smccc_res *res)
{
do {
arm_smccc_smc(smc->args[0], smc->args[1], smc->args[2],
smc->args[3], smc->args[4], smc->args[5],
smc->args[6], smc->args[7], res);
} while (res->a0 == QCOM_SCM_INTERRUPTED);
}
/**
* scm_legacy_call() - Sends a command to the SCM and waits for the command to
* finish processing.
* @dev: device
* @desc: descriptor structure containing arguments and return values
* @res: results from SMC call
*
* A note on cache maintenance:
* Note that any buffers that are expected to be accessed by the secure world
* must be flushed before invoking qcom_scm_call and invalidated in the cache
* immediately after qcom_scm_call returns. Cache maintenance on the command
* and response buffers is taken care of by qcom_scm_call; however, callers are
* responsible for any other cached buffers passed over to the secure world.
*/
int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc,
struct qcom_scm_res *res)
{
u8 arglen = desc->arginfo & 0xf;
int ret = 0, context_id;
unsigned int i;
struct scm_legacy_command *cmd;
struct scm_legacy_response *rsp;
struct arm_smccc_args smc = {0};
struct arm_smccc_res smc_res;
const size_t cmd_len = arglen * sizeof(__le32);
const size_t resp_len = MAX_QCOM_SCM_RETS * sizeof(__le32);
size_t alloc_len = sizeof(*cmd) + cmd_len + sizeof(*rsp) + resp_len;
dma_addr_t cmd_phys;
__le32 *arg_buf;
const __le32 *res_buf;
cmd = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
if (!cmd)
return -ENOMEM;
cmd->len = cpu_to_le32(alloc_len);
cmd->buf_offset = cpu_to_le32(sizeof(*cmd));
cmd->resp_hdr_offset = cpu_to_le32(sizeof(*cmd) + cmd_len);
cmd->id = cpu_to_le32(SCM_LEGACY_FNID(desc->svc, desc->cmd));
arg_buf = scm_legacy_get_command_buffer(cmd);
for (i = 0; i < arglen; i++)
arg_buf[i] = cpu_to_le32(desc->args[i]);
rsp = scm_legacy_command_to_response(cmd);
cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE);
if (dma_mapping_error(dev, cmd_phys)) {
kfree(cmd);
return -ENOMEM;
}
smc.args[0] = 1;
smc.args[1] = (unsigned long)&context_id;
smc.args[2] = cmd_phys;
mutex_lock(&qcom_scm_lock);
__scm_legacy_do(&smc, &smc_res);
if (smc_res.a0)
ret = qcom_scm_remap_error(smc_res.a0);
mutex_unlock(&qcom_scm_lock);
if (ret)
goto out;
do {
dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len,
sizeof(*rsp), DMA_FROM_DEVICE);
} while (!rsp->is_complete);
dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len +
le32_to_cpu(rsp->buf_offset),
resp_len, DMA_FROM_DEVICE);
if (res) {
res_buf = scm_legacy_get_response_buffer(rsp);
for (i = 0; i < MAX_QCOM_SCM_RETS; i++)
res->result[i] = le32_to_cpu(res_buf[i]);
}
out:
dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE);
kfree(cmd);
return ret;
}
#define SCM_LEGACY_ATOMIC_N_REG_ARGS 5
#define SCM_LEGACY_ATOMIC_FIRST_REG_IDX 2
#define SCM_LEGACY_CLASS_REGISTER (0x2 << 8)
#define SCM_LEGACY_MASK_IRQS BIT(5)
#define SCM_LEGACY_ATOMIC_ID(svc, cmd, n) \
((SCM_LEGACY_FNID(svc, cmd) << 12) | \
SCM_LEGACY_CLASS_REGISTER | \
SCM_LEGACY_MASK_IRQS | \
(n & 0xf))
/**
* scm_legacy_call_atomic() - Send an atomic SCM command with up to 5 arguments
* and 3 return values
* @unused: device, legacy argument, not used, can be NULL
* @desc: SCM call descriptor containing arguments
* @res: SCM call return values
*
* This shall only be used with commands that are guaranteed to be
* uninterruptable, atomic and SMP safe.
*/
int scm_legacy_call_atomic(struct device *unused,
const struct qcom_scm_desc *desc,
struct qcom_scm_res *res)
{
int context_id;
struct arm_smccc_res smc_res;
size_t arglen = desc->arginfo & 0xf;
BUG_ON(arglen > SCM_LEGACY_ATOMIC_N_REG_ARGS);
arm_smccc_smc(SCM_LEGACY_ATOMIC_ID(desc->svc, desc->cmd, arglen),
(unsigned long)&context_id,
desc->args[0], desc->args[1], desc->args[2],
desc->args[3], desc->args[4], 0, &smc_res);
if (res) {
res->result[0] = smc_res.a1;
res->result[1] = smc_res.a2;
res->result[2] = smc_res.a3;
}
return smc_res.a0;
}
@@ -0,0 +1,215 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2015,2019 The Linux Foundation. All rights reserved.
*/
#include <linux/cleanup.h>
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/firmware/qcom/qcom_scm.h>
#include <linux/firmware/qcom/qcom_tzmem.h>
#include <linux/arm-smccc.h>
#include <linux/dma-mapping.h>
#include "qcom_scm.h"
/**
* struct arm_smccc_args
* @args: The array of values used in registers in smc instruction
*/
struct arm_smccc_args {
unsigned long args[8];
};
static DEFINE_MUTEX(qcom_scm_lock);
#define QCOM_SCM_EBUSY_WAIT_MS 30
#define QCOM_SCM_EBUSY_MAX_RETRY 20
#define SCM_SMC_N_REG_ARGS 4
#define SCM_SMC_FIRST_EXT_IDX (SCM_SMC_N_REG_ARGS - 1)
#define SCM_SMC_N_EXT_ARGS (MAX_QCOM_SCM_ARGS - SCM_SMC_N_REG_ARGS + 1)
#define SCM_SMC_FIRST_REG_IDX 2
#define SCM_SMC_LAST_REG_IDX (SCM_SMC_FIRST_REG_IDX + SCM_SMC_N_REG_ARGS - 1)
static void __scm_smc_do_quirk(const struct arm_smccc_args *smc,
struct arm_smccc_res *res)
{
unsigned long a0 = smc->args[0];
struct arm_smccc_quirk quirk = { .id = ARM_SMCCC_QUIRK_QCOM_A6 };
quirk.state.a6 = 0;
do {
arm_smccc_smc_quirk(a0, smc->args[1], smc->args[2],
smc->args[3], smc->args[4], smc->args[5],
quirk.state.a6, smc->args[7], res, &quirk);
if (res->a0 == QCOM_SCM_INTERRUPTED)
a0 = res->a0;
} while (res->a0 == QCOM_SCM_INTERRUPTED);
}
static void fill_wq_resume_args(struct arm_smccc_args *resume, u32 smc_call_ctx)
{
memset(resume->args, 0, sizeof(resume->args[0]) * ARRAY_SIZE(resume->args));
resume->args[0] = ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL,
ARM_SMCCC_SMC_64, ARM_SMCCC_OWNER_SIP,
SCM_SMC_FNID(QCOM_SCM_SVC_WAITQ, QCOM_SCM_WAITQ_RESUME));
resume->args[1] = QCOM_SCM_ARGS(1);
resume->args[2] = smc_call_ctx;
}
int scm_get_wq_ctx(u32 *wq_ctx, u32 *flags, u32 *more_pending)
{
int ret;
struct arm_smccc_res get_wq_res;
struct arm_smccc_args get_wq_ctx = {0};
get_wq_ctx.args[0] = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,
ARM_SMCCC_SMC_64, ARM_SMCCC_OWNER_SIP,
SCM_SMC_FNID(QCOM_SCM_SVC_WAITQ, QCOM_SCM_WAITQ_GET_WQ_CTX));
/* Guaranteed to return only success or error, no WAITQ_* */
__scm_smc_do_quirk(&get_wq_ctx, &get_wq_res);
ret = get_wq_res.a0;
if (ret)
return ret;
*wq_ctx = get_wq_res.a1;
*flags = get_wq_res.a2;
*more_pending = get_wq_res.a3;
return 0;
}
static int __scm_smc_do_quirk_handle_waitq(struct device *dev, struct arm_smccc_args *waitq,
struct arm_smccc_res *res)
{
int ret;
u32 wq_ctx, smc_call_ctx;
struct arm_smccc_args resume;
struct arm_smccc_args *smc = waitq;
do {
__scm_smc_do_quirk(smc, res);
if (res->a0 == QCOM_SCM_WAITQ_SLEEP) {
wq_ctx = res->a1;
smc_call_ctx = res->a2;
ret = qcom_scm_wait_for_wq_completion(wq_ctx);
if (ret)
return ret;
fill_wq_resume_args(&resume, smc_call_ctx);
smc = &resume;
}
} while (res->a0 == QCOM_SCM_WAITQ_SLEEP);
return 0;
}
static int __scm_smc_do(struct device *dev, struct arm_smccc_args *smc,
struct arm_smccc_res *res, bool atomic)
{
int ret, retry_count = 0;
if (atomic) {
__scm_smc_do_quirk(smc, res);
return 0;
}
do {
mutex_lock(&qcom_scm_lock);
ret = __scm_smc_do_quirk_handle_waitq(dev, smc, res);
mutex_unlock(&qcom_scm_lock);
if (ret)
return ret;
if (res->a0 == QCOM_SCM_V2_EBUSY) {
if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
break;
msleep(QCOM_SCM_EBUSY_WAIT_MS);
}
} while (res->a0 == QCOM_SCM_V2_EBUSY);
return 0;
}
int __scm_smc_call(struct device *dev, const struct qcom_scm_desc *desc,
enum qcom_scm_convention qcom_convention,
struct qcom_scm_res *res, bool atomic)
{
int arglen = desc->arginfo & 0xf;
int i, ret;
void *args_virt __free(qcom_tzmem) = NULL;
gfp_t flag = atomic ? GFP_ATOMIC : GFP_KERNEL;
u32 smccc_call_type = atomic ? ARM_SMCCC_FAST_CALL : ARM_SMCCC_STD_CALL;
u32 qcom_smccc_convention = (qcom_convention == SMC_CONVENTION_ARM_32) ?
ARM_SMCCC_SMC_32 : ARM_SMCCC_SMC_64;
struct arm_smccc_res smc_res;
struct arm_smccc_args smc = {0};
smc.args[0] = ARM_SMCCC_CALL_VAL(
smccc_call_type,
qcom_smccc_convention,
desc->owner,
SCM_SMC_FNID(desc->svc, desc->cmd));
smc.args[1] = desc->arginfo;
for (i = 0; i < SCM_SMC_N_REG_ARGS; i++)
smc.args[i + SCM_SMC_FIRST_REG_IDX] = desc->args[i];
if (unlikely(arglen > SCM_SMC_N_REG_ARGS)) {
struct qcom_tzmem_pool *mempool = qcom_scm_get_tzmem_pool();
if (!mempool)
return -EINVAL;
args_virt = qcom_tzmem_alloc(mempool,
SCM_SMC_N_EXT_ARGS * sizeof(u64),
flag);
if (!args_virt)
return -ENOMEM;
if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
__le32 *args = args_virt;
for (i = 0; i < SCM_SMC_N_EXT_ARGS; i++)
args[i] = cpu_to_le32(desc->args[i +
SCM_SMC_FIRST_EXT_IDX]);
} else {
__le64 *args = args_virt;
for (i = 0; i < SCM_SMC_N_EXT_ARGS; i++)
args[i] = cpu_to_le64(desc->args[i +
SCM_SMC_FIRST_EXT_IDX]);
}
smc.args[SCM_SMC_LAST_REG_IDX] = qcom_tzmem_to_phys(args_virt);
}
ret = __scm_smc_do(dev, &smc, &smc_res, atomic);
if (ret)
return ret;
if (res) {
res->result[0] = smc_res.a1;
res->result[1] = smc_res.a2;
res->result[2] = smc_res.a3;
}
return (long)smc_res.a0 ? qcom_scm_remap_error(smc_res.a0) : 0;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,196 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright (c) 2010-2015,2019 The Linux Foundation. All rights reserved.
*/
#ifndef __QCOM_SCM_INT_H
#define __QCOM_SCM_INT_H
struct device;
struct qcom_tzmem_pool;
enum qcom_scm_convention {
SMC_CONVENTION_UNKNOWN,
SMC_CONVENTION_LEGACY,
SMC_CONVENTION_ARM_32,
SMC_CONVENTION_ARM_64,
};
extern enum qcom_scm_convention qcom_scm_convention;
#define MAX_QCOM_SCM_ARGS 10
#define MAX_QCOM_SCM_RETS 3
enum qcom_scm_arg_types {
QCOM_SCM_VAL,
QCOM_SCM_RO,
QCOM_SCM_RW,
QCOM_SCM_BUFVAL,
};
#define QCOM_SCM_ARGS_IMPL(num, a, b, c, d, e, f, g, h, i, j, ...) (\
(((a) & 0x3) << 4) | \
(((b) & 0x3) << 6) | \
(((c) & 0x3) << 8) | \
(((d) & 0x3) << 10) | \
(((e) & 0x3) << 12) | \
(((f) & 0x3) << 14) | \
(((g) & 0x3) << 16) | \
(((h) & 0x3) << 18) | \
(((i) & 0x3) << 20) | \
(((j) & 0x3) << 22) | \
((num) & 0xf))
#define QCOM_SCM_ARGS(...) QCOM_SCM_ARGS_IMPL(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
/**
* struct qcom_scm_desc
* @svc: Service identifier
* @cmd: Command identifier
* @arginfo: Metadata describing the arguments in args[]
* @args: The array of arguments for the secure syscall
* @owner: Owner identifier
*/
struct qcom_scm_desc {
u32 svc;
u32 cmd;
u32 arginfo;
u64 args[MAX_QCOM_SCM_ARGS];
u32 owner;
};
/**
* struct qcom_scm_res
* @result: The values returned by the secure syscall
*/
struct qcom_scm_res {
u64 result[MAX_QCOM_SCM_RETS];
};
int qcom_scm_wait_for_wq_completion(u32 wq_ctx);
int scm_get_wq_ctx(u32 *wq_ctx, u32 *flags, u32 *more_pending);
#define SCM_SMC_FNID(s, c) ((((s) & 0xFF) << 8) | ((c) & 0xFF))
int __scm_smc_call(struct device *dev, const struct qcom_scm_desc *desc,
enum qcom_scm_convention qcom_convention,
struct qcom_scm_res *res, bool atomic);
#define scm_smc_call(dev, desc, res, atomic) \
__scm_smc_call((dev), (desc), qcom_scm_convention, (res), (atomic))
#define SCM_LEGACY_FNID(s, c) (((s) << 10) | ((c) & 0x3ff))
int scm_legacy_call_atomic(struct device *dev, const struct qcom_scm_desc *desc,
struct qcom_scm_res *res);
int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc,
struct qcom_scm_res *res);
struct qcom_tzmem_pool *qcom_scm_get_tzmem_pool(void);
int qcom_scm_shm_bridge_enable(struct device *scm_dev);
#define QCOM_SCM_SVC_BOOT 0x01
#define QCOM_SCM_BOOT_SET_ADDR 0x01
#define QCOM_SCM_BOOT_TERMINATE_PC 0x02
#define QCOM_SCM_BOOT_SDI_CONFIG 0x09
#define QCOM_SCM_BOOT_SET_DLOAD_MODE 0x10
#define QCOM_SCM_BOOT_SET_ADDR_MC 0x11
#define QCOM_SCM_BOOT_SET_REMOTE_STATE 0x0a
#define QCOM_SCM_FLUSH_FLAG_MASK 0x3
#define QCOM_SCM_BOOT_MAX_CPUS 4
#define QCOM_SCM_BOOT_MC_FLAG_AARCH64 BIT(0)
#define QCOM_SCM_BOOT_MC_FLAG_COLDBOOT BIT(1)
#define QCOM_SCM_BOOT_MC_FLAG_WARMBOOT BIT(2)
#define QCOM_SCM_SVC_PIL 0x02
#define QCOM_SCM_PIL_PAS_INIT_IMAGE 0x01
#define QCOM_SCM_PIL_PAS_MEM_SETUP 0x02
#define QCOM_SCM_PIL_PAS_AUTH_AND_RESET 0x05
#define QCOM_SCM_PIL_PAS_SHUTDOWN 0x06
#define QCOM_SCM_PIL_PAS_IS_SUPPORTED 0x07
#define QCOM_SCM_PIL_PAS_MSS_RESET 0x0a
#define QCOM_SCM_PIL_PAS_GET_RSCTABLE 0x21
#define QCOM_SCM_SVC_IO 0x05
#define QCOM_SCM_IO_READ 0x01
#define QCOM_SCM_IO_WRITE 0x02
#define QCOM_SCM_SVC_INFO 0x06
#define QCOM_SCM_INFO_IS_CALL_AVAIL 0x01
#define QCOM_SCM_SVC_MP 0x0c
#define QCOM_SCM_MP_RESTORE_SEC_CFG 0x02
#define QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE 0x03
#define QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT 0x04
#define QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE 0x05
#define QCOM_SCM_MP_VIDEO_VAR 0x08
#define QCOM_SCM_MP_ASSIGN 0x16
#define QCOM_SCM_MP_CP_SMMU_APERTURE_ID 0x1b
#define QCOM_SCM_MP_SHM_BRIDGE_ENABLE 0x1c
#define QCOM_SCM_MP_SHM_BRIDGE_DELETE 0x1d
#define QCOM_SCM_MP_SHM_BRIDGE_CREATE 0x1e
#define QCOM_SCM_SVC_OCMEM 0x0f
#define QCOM_SCM_OCMEM_LOCK_CMD 0x01
#define QCOM_SCM_OCMEM_UNLOCK_CMD 0x02
#define QCOM_SCM_SVC_ES 0x10 /* Enterprise Security */
#define QCOM_SCM_ES_INVALIDATE_ICE_KEY 0x03
#define QCOM_SCM_ES_CONFIG_SET_ICE_KEY 0x04
#define QCOM_SCM_ES_DERIVE_SW_SECRET 0x07
#define QCOM_SCM_ES_GENERATE_ICE_KEY 0x08
#define QCOM_SCM_ES_PREPARE_ICE_KEY 0x09
#define QCOM_SCM_ES_IMPORT_ICE_KEY 0x0a
#define QCOM_SCM_SVC_HDCP 0x11
#define QCOM_SCM_HDCP_INVOKE 0x01
#define QCOM_SCM_SVC_LMH 0x13
#define QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE 0x01
#define QCOM_SCM_LMH_LIMIT_DCVSH 0x10
#define QCOM_SCM_SVC_SMMU_PROGRAM 0x15
#define QCOM_SCM_SMMU_PT_FORMAT 0x01
#define QCOM_SCM_SMMU_CONFIG_ERRATA1 0x03
#define QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL 0x02
#define QCOM_SCM_SVC_WAITQ 0x24
#define QCOM_SCM_WAITQ_RESUME 0x02
#define QCOM_SCM_WAITQ_GET_WQ_CTX 0x03
#define QCOM_SCM_WAITQ_GET_INFO 0x04
#define QCOM_SCM_SVC_GPU 0x28
#define QCOM_SCM_SVC_GPU_INIT_REGS 0x01
/* ARM_SMCCC_OWNER_TRUSTED_OS calls */
#define QCOM_SCM_SVC_SMCINVOKE 0x06
#define QCOM_SCM_SMCINVOKE_INVOKE_LEGACY 0x00
#define QCOM_SCM_SMCINVOKE_CB_RSP 0x01
#define QCOM_SCM_SMCINVOKE_INVOKE 0x02
/* common error codes */
#define QCOM_SCM_V2_EBUSY -12
#define QCOM_SCM_ENOMEM -5
#define QCOM_SCM_EOPNOTSUPP -4
#define QCOM_SCM_EINVAL_ADDR -3
#define QCOM_SCM_EINVAL_ARG -2
#define QCOM_SCM_ERROR -1
#define QCOM_SCM_INTERRUPTED 1
#define QCOM_SCM_WAITQ_SLEEP 2
static inline int qcom_scm_remap_error(int err)
{
switch (err) {
case QCOM_SCM_ERROR:
return -EIO;
case QCOM_SCM_EINVAL_ADDR:
case QCOM_SCM_EINVAL_ARG:
return -EINVAL;
case QCOM_SCM_EOPNOTSUPP:
return -EOPNOTSUPP;
case QCOM_SCM_ENOMEM:
return -ENOMEM;
case QCOM_SCM_V2_EBUSY:
return -EBUSY;
}
return -EINVAL;
}
#endif
@@ -0,0 +1,523 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Memory allocator for buffers shared with the TrustZone.
*
* Copyright (C) 2023-2024 Linaro Ltd.
*/
#include <linux/bug.h>
#include <linux/cleanup.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/firmware/qcom/qcom_tzmem.h>
#include <linux/genalloc.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/radix-tree.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/types.h>
#include "qcom_scm.h"
#include "qcom_tzmem.h"
struct qcom_tzmem_area {
struct list_head list;
void *vaddr;
dma_addr_t paddr;
size_t size;
void *priv;
};
struct qcom_tzmem_pool {
struct gen_pool *genpool;
struct list_head areas;
enum qcom_tzmem_policy policy;
size_t increment;
size_t max_size;
spinlock_t lock;
};
struct qcom_tzmem_chunk {
size_t size;
struct qcom_tzmem_pool *owner;
};
static struct device *qcom_tzmem_dev;
static RADIX_TREE(qcom_tzmem_chunks, GFP_ATOMIC);
static DEFINE_SPINLOCK(qcom_tzmem_chunks_lock);
#if IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_GENERIC)
static int qcom_tzmem_init(void)
{
return 0;
}
static int qcom_tzmem_init_area(struct qcom_tzmem_area *area)
{
return 0;
}
static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area)
{
}
#elif IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE)
#include <linux/firmware/qcom/qcom_scm.h>
#include <linux/of.h>
#define QCOM_SHM_BRIDGE_NUM_VM_SHIFT 9
static bool qcom_tzmem_using_shm_bridge;
/* List of machines that are known to not support SHM bridge correctly. */
static const char *const qcom_tzmem_blacklist[] = {
"qcom,sc7180", /* hang in rmtfs memory assignment */
"qcom,sc8180x",
"qcom,sdm670", /* failure in GPU firmware loading */
"qcom,sdm845", /* reset in rmtfs memory assignment */
"qcom,sm7150", /* reset in rmtfs memory assignment */
"qcom,sm8150", /* reset in rmtfs memory assignment */
NULL
};
static int qcom_tzmem_init(void)
{
const char *const *platform;
int ret;
for (platform = qcom_tzmem_blacklist; *platform; platform++) {
if (of_machine_is_compatible(*platform))
goto notsupp;
}
ret = qcom_scm_shm_bridge_enable(qcom_tzmem_dev);
if (ret == -EOPNOTSUPP)
goto notsupp;
if (!ret)
qcom_tzmem_using_shm_bridge = true;
return ret;
notsupp:
dev_info(qcom_tzmem_dev, "SHM Bridge not supported\n");
return 0;
}
/**
* qcom_tzmem_shm_bridge_create() - Create a SHM bridge.
* @paddr: Physical address of the memory to share.
* @size: Size of the memory to share.
* @handle: Handle to the SHM bridge.
*
* On platforms that support SHM bridge, this function creates a SHM bridge
* for the given memory region with QTEE. The handle returned by this function
* must be passed to qcom_tzmem_shm_bridge_delete() to free the SHM bridge.
*
* Return: On success, returns 0; on failure, returns < 0.
*/
int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *handle)
{
u64 pfn_and_ns_perm, ipfn_and_s_perm, size_and_flags;
int ret;
if (!qcom_tzmem_using_shm_bridge)
return 0;
pfn_and_ns_perm = paddr | QCOM_SCM_PERM_RW;
ipfn_and_s_perm = paddr | QCOM_SCM_PERM_RW;
size_and_flags = size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT);
ret = qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm,
size_and_flags, QCOM_SCM_VMID_HLOS,
handle);
if (ret) {
dev_err(qcom_tzmem_dev,
"SHM Bridge failed: ret %d paddr 0x%pa, size %zu\n",
ret, &paddr, size);
return ret;
}
return 0;
}
EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_create);
/**
* qcom_tzmem_shm_bridge_delete() - Delete a SHM bridge.
* @handle: Handle to the SHM bridge.
*
* On platforms that support SHM bridge, this function deletes the SHM bridge
* for the given memory region. The handle must be the same as the one
* returned by qcom_tzmem_shm_bridge_create().
*/
void qcom_tzmem_shm_bridge_delete(u64 handle)
{
if (qcom_tzmem_using_shm_bridge)
qcom_scm_shm_bridge_delete(handle);
}
EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_delete);
static int qcom_tzmem_init_area(struct qcom_tzmem_area *area)
{
int ret;
u64 *handle __free(kfree) = kzalloc(sizeof(*handle), GFP_KERNEL);
if (!handle)
return -ENOMEM;
ret = qcom_tzmem_shm_bridge_create(area->paddr, area->size, handle);
if (ret)
return ret;
area->priv = no_free_ptr(handle);
return 0;
}
static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area)
{
u64 *handle = area->priv;
qcom_tzmem_shm_bridge_delete(*handle);
kfree(handle);
}
#endif /* CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE */
static int qcom_tzmem_pool_add_memory(struct qcom_tzmem_pool *pool,
size_t size, gfp_t gfp)
{
int ret;
struct qcom_tzmem_area *area __free(kfree) = kzalloc(sizeof(*area),
gfp);
if (!area)
return -ENOMEM;
area->size = PAGE_ALIGN(size);
area->vaddr = dma_alloc_coherent(qcom_tzmem_dev, area->size,
&area->paddr, gfp);
if (!area->vaddr)
return -ENOMEM;
ret = qcom_tzmem_init_area(area);
if (ret) {
dma_free_coherent(qcom_tzmem_dev, area->size,
area->vaddr, area->paddr);
return ret;
}
ret = gen_pool_add_virt(pool->genpool, (unsigned long)area->vaddr,
(phys_addr_t)area->paddr, size, -1);
if (ret) {
dma_free_coherent(qcom_tzmem_dev, area->size,
area->vaddr, area->paddr);
return ret;
}
scoped_guard(spinlock_irqsave, &pool->lock)
list_add_tail(&area->list, &pool->areas);
area = NULL;
return 0;
}
/**
* qcom_tzmem_pool_new() - Create a new TZ memory pool.
* @config: Pool configuration.
*
* Create a new pool of memory suitable for sharing with the TrustZone.
*
* Must not be used in atomic context.
*
* Return: New memory pool address or ERR_PTR() on error.
*/
struct qcom_tzmem_pool *
qcom_tzmem_pool_new(const struct qcom_tzmem_pool_config *config)
{
int ret = -ENOMEM;
might_sleep();
switch (config->policy) {
case QCOM_TZMEM_POLICY_STATIC:
if (!config->initial_size)
return ERR_PTR(-EINVAL);
break;
case QCOM_TZMEM_POLICY_MULTIPLIER:
if (!config->increment)
return ERR_PTR(-EINVAL);
break;
case QCOM_TZMEM_POLICY_ON_DEMAND:
break;
default:
return ERR_PTR(-EINVAL);
}
struct qcom_tzmem_pool *pool __free(kfree) = kzalloc_obj(*pool);
if (!pool)
return ERR_PTR(-ENOMEM);
pool->genpool = gen_pool_create(PAGE_SHIFT, -1);
if (!pool->genpool)
return ERR_PTR(-ENOMEM);
gen_pool_set_algo(pool->genpool, gen_pool_best_fit, NULL);
pool->policy = config->policy;
pool->increment = config->increment;
pool->max_size = config->max_size;
INIT_LIST_HEAD(&pool->areas);
spin_lock_init(&pool->lock);
if (config->initial_size) {
ret = qcom_tzmem_pool_add_memory(pool, config->initial_size,
GFP_KERNEL);
if (ret) {
gen_pool_destroy(pool->genpool);
return ERR_PTR(ret);
}
}
return_ptr(pool);
}
EXPORT_SYMBOL_GPL(qcom_tzmem_pool_new);
/**
* qcom_tzmem_pool_free() - Destroy a TZ memory pool and free all resources.
* @pool: Memory pool to free.
*
* Must not be called if any of the allocated chunks has not been freed.
* Must not be used in atomic context.
*/
void qcom_tzmem_pool_free(struct qcom_tzmem_pool *pool)
{
struct qcom_tzmem_area *area, *next;
struct qcom_tzmem_chunk *chunk;
struct radix_tree_iter iter;
bool non_empty = false;
void __rcu **slot;
might_sleep();
if (!pool)
return;
scoped_guard(spinlock_irqsave, &qcom_tzmem_chunks_lock) {
radix_tree_for_each_slot(slot, &qcom_tzmem_chunks, &iter, 0) {
chunk = radix_tree_deref_slot_protected(slot,
&qcom_tzmem_chunks_lock);
if (chunk->owner == pool)
non_empty = true;
}
}
WARN(non_empty, "Freeing TZ memory pool with memory still allocated");
list_for_each_entry_safe(area, next, &pool->areas, list) {
list_del(&area->list);
qcom_tzmem_cleanup_area(area);
dma_free_coherent(qcom_tzmem_dev, area->size,
area->vaddr, area->paddr);
kfree(area);
}
gen_pool_destroy(pool->genpool);
kfree(pool);
}
EXPORT_SYMBOL_GPL(qcom_tzmem_pool_free);
static void devm_qcom_tzmem_pool_free(void *data)
{
struct qcom_tzmem_pool *pool = data;
qcom_tzmem_pool_free(pool);
}
/**
* devm_qcom_tzmem_pool_new() - Managed variant of qcom_tzmem_pool_new().
* @dev: Device managing this resource.
* @config: Pool configuration.
*
* Must not be used in atomic context.
*
* Return: Address of the managed pool or ERR_PTR() on failure.
*/
struct qcom_tzmem_pool *
devm_qcom_tzmem_pool_new(struct device *dev,
const struct qcom_tzmem_pool_config *config)
{
struct qcom_tzmem_pool *pool;
int ret;
pool = qcom_tzmem_pool_new(config);
if (IS_ERR(pool))
return pool;
ret = devm_add_action_or_reset(dev, devm_qcom_tzmem_pool_free, pool);
if (ret)
return ERR_PTR(ret);
return pool;
}
EXPORT_SYMBOL_GPL(devm_qcom_tzmem_pool_new);
static bool qcom_tzmem_try_grow_pool(struct qcom_tzmem_pool *pool,
size_t requested, gfp_t gfp)
{
size_t current_size = gen_pool_size(pool->genpool);
if (pool->max_size && (current_size + requested) > pool->max_size)
return false;
switch (pool->policy) {
case QCOM_TZMEM_POLICY_STATIC:
return false;
case QCOM_TZMEM_POLICY_MULTIPLIER:
requested = current_size * pool->increment;
break;
case QCOM_TZMEM_POLICY_ON_DEMAND:
break;
}
return !qcom_tzmem_pool_add_memory(pool, requested, gfp);
}
/**
* qcom_tzmem_alloc() - Allocate a memory chunk suitable for sharing with TZ.
* @pool: TZ memory pool from which to allocate memory.
* @size: Number of bytes to allocate.
* @gfp: GFP flags.
*
* Can be used in any context.
*
* Return:
* Address of the allocated buffer or NULL if no more memory can be allocated.
* The buffer must be released using qcom_tzmem_free().
*/
void *qcom_tzmem_alloc(struct qcom_tzmem_pool *pool, size_t size, gfp_t gfp)
{
unsigned long vaddr;
int ret;
if (!size)
return NULL;
size = PAGE_ALIGN(size);
struct qcom_tzmem_chunk *chunk __free(kfree) = kzalloc(sizeof(*chunk),
gfp);
if (!chunk)
return NULL;
again:
vaddr = gen_pool_alloc(pool->genpool, size);
if (!vaddr) {
if (qcom_tzmem_try_grow_pool(pool, size, gfp))
goto again;
return NULL;
}
chunk->size = size;
chunk->owner = pool;
scoped_guard(spinlock_irqsave, &qcom_tzmem_chunks_lock) {
ret = radix_tree_insert(&qcom_tzmem_chunks, vaddr, chunk);
if (ret) {
gen_pool_free(pool->genpool, vaddr, size);
return NULL;
}
chunk = NULL;
}
return (void *)vaddr;
}
EXPORT_SYMBOL_GPL(qcom_tzmem_alloc);
/**
* qcom_tzmem_free() - Release a buffer allocated from a TZ memory pool.
* @vaddr: Virtual address of the buffer.
*
* Can be used in any context.
*/
void qcom_tzmem_free(void *vaddr)
{
struct qcom_tzmem_chunk *chunk;
scoped_guard(spinlock_irqsave, &qcom_tzmem_chunks_lock)
chunk = radix_tree_delete_item(&qcom_tzmem_chunks,
(unsigned long)vaddr, NULL);
if (!chunk) {
WARN(1, "Virtual address %p not owned by TZ memory allocator",
vaddr);
return;
}
scoped_guard(spinlock_irqsave, &chunk->owner->lock)
gen_pool_free(chunk->owner->genpool, (unsigned long)vaddr,
chunk->size);
kfree(chunk);
}
EXPORT_SYMBOL_GPL(qcom_tzmem_free);
/**
* qcom_tzmem_to_phys() - Map the virtual address of TZ memory to physical.
* @vaddr: Virtual address of memory allocated from a TZ memory pool.
*
* Can be used in any context. The address must point to memory allocated
* using qcom_tzmem_alloc().
*
* Returns:
* Physical address mapped from the virtual or 0 if the mapping failed.
*/
phys_addr_t qcom_tzmem_to_phys(void *vaddr)
{
struct qcom_tzmem_chunk *chunk;
struct radix_tree_iter iter;
void __rcu **slot;
phys_addr_t ret;
guard(spinlock_irqsave)(&qcom_tzmem_chunks_lock);
radix_tree_for_each_slot(slot, &qcom_tzmem_chunks, &iter, 0) {
chunk = radix_tree_deref_slot_protected(slot,
&qcom_tzmem_chunks_lock);
ret = gen_pool_virt_to_phys(chunk->owner->genpool,
(unsigned long)vaddr);
if (ret == -1)
continue;
return ret;
}
return 0;
}
EXPORT_SYMBOL_GPL(qcom_tzmem_to_phys);
int qcom_tzmem_enable(struct device *dev)
{
if (qcom_tzmem_dev)
return -EBUSY;
qcom_tzmem_dev = dev;
return qcom_tzmem_init();
}
EXPORT_SYMBOL_GPL(qcom_tzmem_enable);
MODULE_DESCRIPTION("TrustZone memory allocator for Qualcomm firmware drivers");
MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,13 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright (C) 2023-2024 Linaro Ltd.
*/
#ifndef __QCOM_TZMEM_PRIV_H
#define __QCOM_TZMEM_PRIV_H
struct device;
int qcom_tzmem_enable(struct device *dev);
#endif /* __QCOM_TZMEM_PRIV_H */