xref: /linux/drivers/media/platform/qcom/iris/iris_firmware.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #include <linux/firmware.h>
7 #include <linux/firmware/qcom/qcom_scm.h>
8 #include <linux/of_address.h>
9 #include <linux/of_reserved_mem.h>
10 #include <linux/soc/qcom/mdt_loader.h>
11 
12 #include "iris_core.h"
13 #include "iris_firmware.h"
14 
15 #define IRIS_PAS_ID				9
16 
17 #define MAX_FIRMWARE_NAME_SIZE	128
18 
19 static int iris_load_fw_to_memory(struct iris_core *core, const char *fw_name)
20 {
21 	const struct firmware *firmware = NULL;
22 	struct device *dev = core->dev;
23 	struct resource res;
24 	phys_addr_t mem_phys;
25 	size_t res_size;
26 	ssize_t fw_size;
27 	void *mem_virt;
28 	int ret;
29 
30 	if (strlen(fw_name) >= MAX_FIRMWARE_NAME_SIZE - 4)
31 		return -EINVAL;
32 
33 	ret = of_reserved_mem_region_to_resource(dev->of_node, 0, &res);
34 	if (ret)
35 		return ret;
36 
37 	mem_phys = res.start;
38 	res_size = resource_size(&res);
39 
40 	ret = request_firmware(&firmware, fw_name, dev);
41 	if (ret)
42 		return ret;
43 
44 	fw_size = qcom_mdt_get_size(firmware);
45 	if (fw_size < 0 || res_size < (size_t)fw_size) {
46 		ret = -EINVAL;
47 		goto err_release_fw;
48 	}
49 
50 	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
51 	if (!mem_virt) {
52 		ret = -ENOMEM;
53 		goto err_release_fw;
54 	}
55 
56 	ret = qcom_mdt_load(dev, firmware, fw_name,
57 			    IRIS_PAS_ID, mem_virt, mem_phys, res_size, NULL);
58 
59 	memunmap(mem_virt);
60 err_release_fw:
61 	release_firmware(firmware);
62 
63 	return ret;
64 }
65 
66 int iris_fw_load(struct iris_core *core)
67 {
68 	const struct tz_cp_config *cp_config;
69 	const char *fwpath = NULL;
70 	int i, ret;
71 
72 	ret = of_property_read_string_index(core->dev->of_node, "firmware-name", 0,
73 					    &fwpath);
74 	if (ret)
75 		fwpath = core->iris_firmware_desc->fwname;
76 
77 	ret = iris_load_fw_to_memory(core, fwpath);
78 	if (ret) {
79 		dev_err(core->dev, "firmware download failed\n");
80 		return -ENOMEM;
81 	}
82 
83 	ret = qcom_scm_pas_auth_and_reset(IRIS_PAS_ID);
84 	if (ret)  {
85 		dev_err(core->dev, "auth and reset failed: %d\n", ret);
86 		return ret;
87 	}
88 
89 	for (i = 0; i < core->iris_platform_data->tz_cp_config_data_size; i++) {
90 		cp_config = &core->iris_platform_data->tz_cp_config_data[i];
91 		ret = qcom_scm_mem_protect_video_var(cp_config->cp_start,
92 						     cp_config->cp_size,
93 						     cp_config->cp_nonpixel_start,
94 						     cp_config->cp_nonpixel_size);
95 		if (ret) {
96 			dev_err(core->dev, "qcom_scm_mem_protect_video_var failed: %d\n", ret);
97 			qcom_scm_pas_shutdown(IRIS_PAS_ID);
98 			return ret;
99 		}
100 	}
101 
102 	return ret;
103 }
104 
105 int iris_fw_unload(struct iris_core *core)
106 {
107 	return qcom_scm_pas_shutdown(IRIS_PAS_ID);
108 }
109 
110 int iris_set_hw_state(struct iris_core *core, bool resume)
111 {
112 	return qcom_scm_set_remote_state(resume, 0);
113 }
114