Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (C) 2018 Intel Corporation
0003 
0004 #include <linux/device.h>
0005 #include <linux/firmware.h>
0006 #include <linux/mm.h>
0007 #include <linux/slab.h>
0008 
0009 #include "ipu3-css.h"
0010 #include "ipu3-css-fw.h"
0011 #include "ipu3-dmamap.h"
0012 
0013 static void imgu_css_fw_show_binary(struct device *dev, struct imgu_fw_info *bi,
0014                     const char *name)
0015 {
0016     unsigned int i;
0017 
0018     dev_dbg(dev, "found firmware binary type %i size %i name %s\n",
0019         bi->type, bi->blob.size, name);
0020     if (bi->type != IMGU_FW_ISP_FIRMWARE)
0021         return;
0022 
0023     dev_dbg(dev, "    id %i mode %i bds 0x%x veceven %i/%i out_pins %i\n",
0024         bi->info.isp.sp.id, bi->info.isp.sp.pipeline.mode,
0025         bi->info.isp.sp.bds.supported_bds_factors,
0026         bi->info.isp.sp.enable.vf_veceven,
0027         bi->info.isp.sp.vf_dec.is_variable,
0028         bi->info.isp.num_output_pins);
0029 
0030     dev_dbg(dev, "    input (%i,%i)-(%i,%i) formats %s%s%s\n",
0031         bi->info.isp.sp.input.min_width,
0032         bi->info.isp.sp.input.min_height,
0033         bi->info.isp.sp.input.max_width,
0034         bi->info.isp.sp.input.max_height,
0035         bi->info.isp.sp.enable.input_yuv ? "yuv420 " : "",
0036         bi->info.isp.sp.enable.input_feeder ||
0037         bi->info.isp.sp.enable.input_raw ? "raw8 raw10 " : "",
0038         bi->info.isp.sp.enable.input_raw ? "raw12" : "");
0039 
0040     dev_dbg(dev, "    internal (%i,%i)\n",
0041         bi->info.isp.sp.internal.max_width,
0042         bi->info.isp.sp.internal.max_height);
0043 
0044     dev_dbg(dev, "    output (%i,%i)-(%i,%i) formats",
0045         bi->info.isp.sp.output.min_width,
0046         bi->info.isp.sp.output.min_height,
0047         bi->info.isp.sp.output.max_width,
0048         bi->info.isp.sp.output.max_height);
0049     for (i = 0; i < bi->info.isp.num_output_formats; i++)
0050         dev_dbg(dev, " %i", bi->info.isp.output_formats[i]);
0051     dev_dbg(dev, " vf");
0052     for (i = 0; i < bi->info.isp.num_vf_formats; i++)
0053         dev_dbg(dev, " %i", bi->info.isp.vf_formats[i]);
0054     dev_dbg(dev, "\n");
0055 }
0056 
0057 unsigned int imgu_css_fw_obgrid_size(const struct imgu_fw_info *bi)
0058 {
0059     unsigned int width = DIV_ROUND_UP(bi->info.isp.sp.internal.max_width,
0060                       IMGU_OBGRID_TILE_SIZE * 2) + 1;
0061     unsigned int height = DIV_ROUND_UP(bi->info.isp.sp.internal.max_height,
0062                        IMGU_OBGRID_TILE_SIZE * 2) + 1;
0063     unsigned int obgrid_size;
0064 
0065     width = ALIGN(width, IPU3_UAPI_ISP_VEC_ELEMS / 4);
0066     obgrid_size = PAGE_ALIGN(width * height *
0067                  sizeof(struct ipu3_uapi_obgrid_param)) *
0068                  bi->info.isp.sp.iterator.num_stripes;
0069     return obgrid_size;
0070 }
0071 
0072 void *imgu_css_fw_pipeline_params(struct imgu_css *css, unsigned int pipe,
0073                   enum imgu_abi_param_class cls,
0074                   enum imgu_abi_memories mem,
0075                   struct imgu_fw_isp_parameter *par,
0076                   size_t par_size, void *binary_params)
0077 {
0078     struct imgu_fw_info *bi =
0079         &css->fwp->binary_header[css->pipes[pipe].bindex];
0080 
0081     if (par->offset + par->size >
0082         bi->info.isp.sp.mem_initializers.params[cls][mem].size)
0083         return NULL;
0084 
0085     if (par->size != par_size)
0086         pr_warn("parameter size doesn't match defined size\n");
0087 
0088     if (par->size < par_size)
0089         return NULL;
0090 
0091     return binary_params + par->offset;
0092 }
0093 
0094 void imgu_css_fw_cleanup(struct imgu_css *css)
0095 {
0096     struct imgu_device *imgu = dev_get_drvdata(css->dev);
0097 
0098     if (css->binary) {
0099         unsigned int i;
0100 
0101         for (i = 0; i < css->fwp->file_header.binary_nr; i++)
0102             imgu_dmamap_free(imgu, &css->binary[i]);
0103         kfree(css->binary);
0104     }
0105     if (css->fw)
0106         release_firmware(css->fw);
0107 
0108     css->binary = NULL;
0109     css->fw = NULL;
0110 }
0111 
0112 int imgu_css_fw_init(struct imgu_css *css)
0113 {
0114     static const u32 BLOCK_MAX = 65536;
0115     struct imgu_device *imgu = dev_get_drvdata(css->dev);
0116     struct device *dev = css->dev;
0117     unsigned int i, j, binary_nr;
0118     int r;
0119 
0120     r = request_firmware(&css->fw, IMGU_FW_NAME_20161208, css->dev);
0121     if (r == -ENOENT)
0122         r = request_firmware(&css->fw, IMGU_FW_NAME, css->dev);
0123     if (r)
0124         return r;
0125 
0126     /* Check and display fw header info */
0127 
0128     css->fwp = (struct imgu_fw_header *)css->fw->data;
0129     if (css->fw->size < struct_size(css->fwp, binary_header, 1) ||
0130         css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
0131         goto bad_fw;
0132     if (struct_size(css->fwp, binary_header,
0133             css->fwp->file_header.binary_nr) > css->fw->size)
0134         goto bad_fw;
0135 
0136     dev_info(dev, "loaded firmware version %.64s, %u binaries, %zu bytes\n",
0137          css->fwp->file_header.version, css->fwp->file_header.binary_nr,
0138          css->fw->size);
0139 
0140     /* Validate and display info on fw binaries */
0141 
0142     binary_nr = css->fwp->file_header.binary_nr;
0143 
0144     css->fw_bl = -1;
0145     css->fw_sp[0] = -1;
0146     css->fw_sp[1] = -1;
0147 
0148     for (i = 0; i < binary_nr; i++) {
0149         struct imgu_fw_info *bi = &css->fwp->binary_header[i];
0150         const char *name = (void *)css->fwp + bi->blob.prog_name_offset;
0151         size_t len;
0152 
0153         if (bi->blob.prog_name_offset >= css->fw->size)
0154             goto bad_fw;
0155         len = strnlen(name, css->fw->size - bi->blob.prog_name_offset);
0156         if (len + 1 > css->fw->size - bi->blob.prog_name_offset ||
0157             len + 1 >= IMGU_ABI_MAX_BINARY_NAME)
0158             goto bad_fw;
0159 
0160         if (bi->blob.size != bi->blob.text_size + bi->blob.icache_size
0161             + bi->blob.data_size + bi->blob.padding_size)
0162             goto bad_fw;
0163         if (bi->blob.offset + bi->blob.size > css->fw->size)
0164             goto bad_fw;
0165 
0166         if (bi->type == IMGU_FW_BOOTLOADER_FIRMWARE) {
0167             css->fw_bl = i;
0168             if (bi->info.bl.sw_state >= css->iomem_length ||
0169                 bi->info.bl.num_dma_cmds >= css->iomem_length ||
0170                 bi->info.bl.dma_cmd_list >= css->iomem_length)
0171                 goto bad_fw;
0172         }
0173         if (bi->type == IMGU_FW_SP_FIRMWARE ||
0174             bi->type == IMGU_FW_SP1_FIRMWARE) {
0175             css->fw_sp[bi->type == IMGU_FW_SP_FIRMWARE ? 0 : 1] = i;
0176             if (bi->info.sp.per_frame_data >= css->iomem_length ||
0177                 bi->info.sp.init_dmem_data >= css->iomem_length ||
0178                 bi->info.sp.host_sp_queue >= css->iomem_length ||
0179                 bi->info.sp.isp_started >= css->iomem_length ||
0180                 bi->info.sp.sw_state >= css->iomem_length ||
0181                 bi->info.sp.sleep_mode >= css->iomem_length ||
0182                 bi->info.sp.invalidate_tlb >= css->iomem_length ||
0183                 bi->info.sp.host_sp_com >= css->iomem_length ||
0184                 bi->info.sp.output + 12 >= css->iomem_length ||
0185                 bi->info.sp.host_sp_queues_initialized >=
0186                 css->iomem_length)
0187                 goto bad_fw;
0188         }
0189         if (bi->type != IMGU_FW_ISP_FIRMWARE)
0190             continue;
0191 
0192         if (bi->info.isp.sp.pipeline.mode >= IPU3_CSS_PIPE_ID_NUM)
0193             goto bad_fw;
0194 
0195         if (bi->info.isp.sp.iterator.num_stripes >
0196             IPU3_UAPI_MAX_STRIPES)
0197             goto bad_fw;
0198 
0199         if (bi->info.isp.num_vf_formats > IMGU_ABI_FRAME_FORMAT_NUM ||
0200             bi->info.isp.num_output_formats > IMGU_ABI_FRAME_FORMAT_NUM)
0201             goto bad_fw;
0202 
0203         for (j = 0; j < bi->info.isp.num_output_formats; j++)
0204             if (bi->info.isp.output_formats[j] >=
0205                 IMGU_ABI_FRAME_FORMAT_NUM)
0206                 goto bad_fw;
0207         for (j = 0; j < bi->info.isp.num_vf_formats; j++)
0208             if (bi->info.isp.vf_formats[j] >=
0209                 IMGU_ABI_FRAME_FORMAT_NUM)
0210                 goto bad_fw;
0211 
0212         if (bi->info.isp.sp.block.block_width <= 0 ||
0213             bi->info.isp.sp.block.block_width > BLOCK_MAX ||
0214             bi->info.isp.sp.block.output_block_height <= 0 ||
0215             bi->info.isp.sp.block.output_block_height > BLOCK_MAX)
0216             goto bad_fw;
0217 
0218         if (bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_PARAM]
0219             + sizeof(struct imgu_fw_param_memory_offsets) >
0220             css->fw->size ||
0221             bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_CONFIG]
0222             + sizeof(struct imgu_fw_config_memory_offsets) >
0223             css->fw->size ||
0224             bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_STATE]
0225             + sizeof(struct imgu_fw_state_memory_offsets) >
0226             css->fw->size)
0227             goto bad_fw;
0228 
0229         imgu_css_fw_show_binary(dev, bi, name);
0230     }
0231 
0232     if (css->fw_bl == -1 || css->fw_sp[0] == -1 || css->fw_sp[1] == -1)
0233         goto bad_fw;
0234 
0235     /* Allocate and map fw binaries into IMGU */
0236 
0237     css->binary = kcalloc(binary_nr, sizeof(*css->binary), GFP_KERNEL);
0238     if (!css->binary) {
0239         r = -ENOMEM;
0240         goto error_out;
0241     }
0242 
0243     for (i = 0; i < css->fwp->file_header.binary_nr; i++) {
0244         struct imgu_fw_info *bi = &css->fwp->binary_header[i];
0245         void *blob = (void *)css->fwp + bi->blob.offset;
0246         size_t size = bi->blob.size;
0247 
0248         if (!imgu_dmamap_alloc(imgu, &css->binary[i], size)) {
0249             r = -ENOMEM;
0250             goto error_out;
0251         }
0252         memcpy(css->binary[i].vaddr, blob, size);
0253     }
0254 
0255     return 0;
0256 
0257 bad_fw:
0258     dev_err(dev, "invalid firmware binary, size %u\n", (int)css->fw->size);
0259     r = -ENODEV;
0260 
0261 error_out:
0262     imgu_css_fw_cleanup(css);
0263     return r;
0264 }