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