]> Git Repo - linux.git/blob - drivers/staging/media/ipu3/ipu3-css-fw.c
x86/kaslr: Expose and use the end of the physical memory address space
[linux.git] / drivers / staging / media / ipu3 / ipu3-css-fw.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
3
4 #include <linux/device.h>
5 #include <linux/firmware.h>
6 #include <linux/mm.h>
7 #include <linux/slab.h>
8
9 #include "ipu3-css.h"
10 #include "ipu3-css-fw.h"
11 #include "ipu3-dmamap.h"
12
13 static void imgu_css_fw_show_binary(struct device *dev, struct imgu_fw_info *bi,
14                                     const char *name)
15 {
16         unsigned int i;
17
18         dev_dbg(dev, "found firmware binary type %i size %i name %s\n",
19                 bi->type, bi->blob.size, name);
20         if (bi->type != IMGU_FW_ISP_FIRMWARE)
21                 return;
22
23         dev_dbg(dev, "    id %i mode %i bds 0x%x veceven %i/%i out_pins %i\n",
24                 bi->info.isp.sp.id, bi->info.isp.sp.pipeline.mode,
25                 bi->info.isp.sp.bds.supported_bds_factors,
26                 bi->info.isp.sp.enable.vf_veceven,
27                 bi->info.isp.sp.vf_dec.is_variable,
28                 bi->info.isp.num_output_pins);
29
30         dev_dbg(dev, "    input (%i,%i)-(%i,%i) formats %s%s%s\n",
31                 bi->info.isp.sp.input.min_width,
32                 bi->info.isp.sp.input.min_height,
33                 bi->info.isp.sp.input.max_width,
34                 bi->info.isp.sp.input.max_height,
35                 bi->info.isp.sp.enable.input_yuv ? "yuv420 " : "",
36                 bi->info.isp.sp.enable.input_feeder ||
37                 bi->info.isp.sp.enable.input_raw ? "raw8 raw10 " : "",
38                 bi->info.isp.sp.enable.input_raw ? "raw12" : "");
39
40         dev_dbg(dev, "    internal (%i,%i)\n",
41                 bi->info.isp.sp.internal.max_width,
42                 bi->info.isp.sp.internal.max_height);
43
44         dev_dbg(dev, "    output (%i,%i)-(%i,%i) formats",
45                 bi->info.isp.sp.output.min_width,
46                 bi->info.isp.sp.output.min_height,
47                 bi->info.isp.sp.output.max_width,
48                 bi->info.isp.sp.output.max_height);
49         for (i = 0; i < bi->info.isp.num_output_formats; i++)
50                 dev_dbg(dev, " %i", bi->info.isp.output_formats[i]);
51         dev_dbg(dev, " vf");
52         for (i = 0; i < bi->info.isp.num_vf_formats; i++)
53                 dev_dbg(dev, " %i", bi->info.isp.vf_formats[i]);
54         dev_dbg(dev, "\n");
55 }
56
57 unsigned int imgu_css_fw_obgrid_size(const struct imgu_fw_info *bi)
58 {
59         unsigned int width = DIV_ROUND_UP(bi->info.isp.sp.internal.max_width,
60                                           IMGU_OBGRID_TILE_SIZE * 2) + 1;
61         unsigned int height = DIV_ROUND_UP(bi->info.isp.sp.internal.max_height,
62                                            IMGU_OBGRID_TILE_SIZE * 2) + 1;
63         unsigned int obgrid_size;
64
65         width = ALIGN(width, IPU3_UAPI_ISP_VEC_ELEMS / 4);
66         obgrid_size = PAGE_ALIGN(width * height *
67                                  sizeof(struct ipu3_uapi_obgrid_param)) *
68                                  bi->info.isp.sp.iterator.num_stripes;
69         return obgrid_size;
70 }
71
72 void *imgu_css_fw_pipeline_params(struct imgu_css *css, unsigned int pipe,
73                                   enum imgu_abi_param_class cls,
74                                   enum imgu_abi_memories mem,
75                                   struct imgu_fw_isp_parameter *par,
76                                   size_t par_size, void *binary_params)
77 {
78         struct imgu_fw_info *bi =
79                 &css->fwp->binary_header[css->pipes[pipe].bindex];
80
81         if (par->offset + par->size >
82             bi->info.isp.sp.mem_initializers.params[cls][mem].size)
83                 return NULL;
84
85         if (par->size != par_size)
86                 pr_warn("parameter size doesn't match defined size\n");
87
88         if (par->size < par_size)
89                 return NULL;
90
91         return binary_params + par->offset;
92 }
93
94 void imgu_css_fw_cleanup(struct imgu_css *css)
95 {
96         struct imgu_device *imgu = dev_get_drvdata(css->dev);
97
98         if (css->binary) {
99                 unsigned int i;
100
101                 for (i = 0; i < css->fwp->file_header.binary_nr; i++)
102                         imgu_dmamap_free(imgu, &css->binary[i]);
103                 kfree(css->binary);
104         }
105         if (css->fw)
106                 release_firmware(css->fw);
107
108         css->binary = NULL;
109         css->fw = NULL;
110 }
111
112 int imgu_css_fw_init(struct imgu_css *css)
113 {
114         static const u32 BLOCK_MAX = 65536;
115         struct imgu_device *imgu = dev_get_drvdata(css->dev);
116         struct device *dev = css->dev;
117         unsigned int i, j, binary_nr;
118         int r;
119
120         r = request_firmware(&css->fw, IMGU_FW_NAME_IPU_20161208, css->dev);
121         if (r == -ENOENT)
122                 r = request_firmware(&css->fw, IMGU_FW_NAME_20161208, css->dev);
123         if (r == -ENOENT)
124                 r = request_firmware(&css->fw, IMGU_FW_NAME, css->dev);
125         if (r)
126                 return r;
127
128         /* Check and display fw header info */
129
130         css->fwp = (struct imgu_fw_header *)css->fw->data;
131         if (css->fw->size < struct_size(css->fwp, binary_header, 1) ||
132             css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
133                 goto bad_fw;
134         if (struct_size(css->fwp, binary_header,
135                         css->fwp->file_header.binary_nr) > css->fw->size)
136                 goto bad_fw;
137
138         dev_info(dev, "loaded firmware version %.64s, %u binaries, %zu bytes\n",
139                  css->fwp->file_header.version, css->fwp->file_header.binary_nr,
140                  css->fw->size);
141
142         /* Validate and display info on fw binaries */
143
144         binary_nr = css->fwp->file_header.binary_nr;
145
146         css->fw_bl = -1;
147         css->fw_sp[0] = -1;
148         css->fw_sp[1] = -1;
149
150         for (i = 0; i < binary_nr; i++) {
151                 struct imgu_fw_info *bi = &css->fwp->binary_header[i];
152                 const char *name = (void *)css->fwp + bi->blob.prog_name_offset;
153                 size_t len;
154
155                 if (bi->blob.prog_name_offset >= css->fw->size)
156                         goto bad_fw;
157                 len = strnlen(name, css->fw->size - bi->blob.prog_name_offset);
158                 if (len + 1 > css->fw->size - bi->blob.prog_name_offset ||
159                     len + 1 >= IMGU_ABI_MAX_BINARY_NAME)
160                         goto bad_fw;
161
162                 if (bi->blob.size != bi->blob.text_size + bi->blob.icache_size
163                     + bi->blob.data_size + bi->blob.padding_size)
164                         goto bad_fw;
165                 if (bi->blob.offset + bi->blob.size > css->fw->size)
166                         goto bad_fw;
167
168                 if (bi->type == IMGU_FW_BOOTLOADER_FIRMWARE) {
169                         css->fw_bl = i;
170                         if (bi->info.bl.sw_state >= css->iomem_length ||
171                             bi->info.bl.num_dma_cmds >= css->iomem_length ||
172                             bi->info.bl.dma_cmd_list >= css->iomem_length)
173                                 goto bad_fw;
174                 }
175                 if (bi->type == IMGU_FW_SP_FIRMWARE ||
176                     bi->type == IMGU_FW_SP1_FIRMWARE) {
177                         css->fw_sp[bi->type == IMGU_FW_SP_FIRMWARE ? 0 : 1] = i;
178                         if (bi->info.sp.per_frame_data >= css->iomem_length ||
179                             bi->info.sp.init_dmem_data >= css->iomem_length ||
180                             bi->info.sp.host_sp_queue >= css->iomem_length ||
181                             bi->info.sp.isp_started >= css->iomem_length ||
182                             bi->info.sp.sw_state >= css->iomem_length ||
183                             bi->info.sp.sleep_mode >= css->iomem_length ||
184                             bi->info.sp.invalidate_tlb >= css->iomem_length ||
185                             bi->info.sp.host_sp_com >= css->iomem_length ||
186                             bi->info.sp.output + 12 >= css->iomem_length ||
187                             bi->info.sp.host_sp_queues_initialized >=
188                             css->iomem_length)
189                                 goto bad_fw;
190                 }
191                 if (bi->type != IMGU_FW_ISP_FIRMWARE)
192                         continue;
193
194                 if (bi->info.isp.sp.pipeline.mode >= IPU3_CSS_PIPE_ID_NUM)
195                         goto bad_fw;
196
197                 if (bi->info.isp.sp.iterator.num_stripes >
198                     IPU3_UAPI_MAX_STRIPES)
199                         goto bad_fw;
200
201                 if (bi->info.isp.num_vf_formats > IMGU_ABI_FRAME_FORMAT_NUM ||
202                     bi->info.isp.num_output_formats > IMGU_ABI_FRAME_FORMAT_NUM)
203                         goto bad_fw;
204
205                 for (j = 0; j < bi->info.isp.num_output_formats; j++)
206                         if (bi->info.isp.output_formats[j] >=
207                             IMGU_ABI_FRAME_FORMAT_NUM)
208                                 goto bad_fw;
209                 for (j = 0; j < bi->info.isp.num_vf_formats; j++)
210                         if (bi->info.isp.vf_formats[j] >=
211                             IMGU_ABI_FRAME_FORMAT_NUM)
212                                 goto bad_fw;
213
214                 if (bi->info.isp.sp.block.block_width <= 0 ||
215                     bi->info.isp.sp.block.block_width > BLOCK_MAX ||
216                     bi->info.isp.sp.block.output_block_height <= 0 ||
217                     bi->info.isp.sp.block.output_block_height > BLOCK_MAX)
218                         goto bad_fw;
219
220                 if (bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_PARAM]
221                     + sizeof(struct imgu_fw_param_memory_offsets) >
222                     css->fw->size ||
223                     bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_CONFIG]
224                     + sizeof(struct imgu_fw_config_memory_offsets) >
225                     css->fw->size ||
226                     bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_STATE]
227                     + sizeof(struct imgu_fw_state_memory_offsets) >
228                     css->fw->size)
229                         goto bad_fw;
230
231                 imgu_css_fw_show_binary(dev, bi, name);
232         }
233
234         if (css->fw_bl == -1 || css->fw_sp[0] == -1 || css->fw_sp[1] == -1)
235                 goto bad_fw;
236
237         /* Allocate and map fw binaries into IMGU */
238
239         css->binary = kcalloc(binary_nr, sizeof(*css->binary), GFP_KERNEL);
240         if (!css->binary) {
241                 r = -ENOMEM;
242                 goto error_out;
243         }
244
245         for (i = 0; i < css->fwp->file_header.binary_nr; i++) {
246                 struct imgu_fw_info *bi = &css->fwp->binary_header[i];
247                 void *blob = (void *)css->fwp + bi->blob.offset;
248                 size_t size = bi->blob.size;
249
250                 if (!imgu_dmamap_alloc(imgu, &css->binary[i], size)) {
251                         r = -ENOMEM;
252                         goto error_out;
253                 }
254                 memcpy(css->binary[i].vaddr, blob, size);
255         }
256
257         return 0;
258
259 bad_fw:
260         dev_err(dev, "invalid firmware binary, size %u\n", (int)css->fw->size);
261         r = -ENODEV;
262
263 error_out:
264         imgu_css_fw_cleanup(css);
265         return r;
266 }
This page took 0.050112 seconds and 4 git commands to generate.