linux/drivers/staging/media/ipu3/ipu3-css-fw.c
<<
>>
Prefs
   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
  13static 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
  57unsigned 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
  72void *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
  94void 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
 112int 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, css->dev);
 121        if (r)
 122                return r;
 123
 124        /* Check and display fw header info */
 125
 126        css->fwp = (struct imgu_fw_header *)css->fw->data;
 127        if (css->fw->size < sizeof(struct imgu_fw_header *) ||
 128            css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
 129                goto bad_fw;
 130        if (sizeof(struct imgu_fw_bi_file_h) +
 131            css->fwp->file_header.binary_nr * sizeof(struct imgu_fw_info) >
 132            css->fw->size)
 133                goto bad_fw;
 134
 135        dev_info(dev, "loaded firmware version %.64s, %u binaries, %zu bytes\n",
 136                 css->fwp->file_header.version, css->fwp->file_header.binary_nr,
 137                 css->fw->size);
 138
 139        /* Validate and display info on fw binaries */
 140
 141        binary_nr = css->fwp->file_header.binary_nr;
 142
 143        css->fw_bl = -1;
 144        css->fw_sp[0] = -1;
 145        css->fw_sp[1] = -1;
 146
 147        for (i = 0; i < binary_nr; i++) {
 148                struct imgu_fw_info *bi = &css->fwp->binary_header[i];
 149                const char *name = (void *)css->fwp + bi->blob.prog_name_offset;
 150                size_t len;
 151
 152                if (bi->blob.prog_name_offset >= css->fw->size)
 153                        goto bad_fw;
 154                len = strnlen(name, css->fw->size - bi->blob.prog_name_offset);
 155                if (len + 1 > css->fw->size - bi->blob.prog_name_offset ||
 156                    len + 1 >= IMGU_ABI_MAX_BINARY_NAME)
 157                        goto bad_fw;
 158
 159                if (bi->blob.size != bi->blob.text_size + bi->blob.icache_size
 160                    + bi->blob.data_size + bi->blob.padding_size)
 161                        goto bad_fw;
 162                if (bi->blob.offset + bi->blob.size > css->fw->size)
 163                        goto bad_fw;
 164
 165                if (bi->type == IMGU_FW_BOOTLOADER_FIRMWARE) {
 166                        css->fw_bl = i;
 167                        if (bi->info.bl.sw_state >= css->iomem_length ||
 168                            bi->info.bl.num_dma_cmds >= css->iomem_length ||
 169                            bi->info.bl.dma_cmd_list >= css->iomem_length)
 170                                goto bad_fw;
 171                }
 172                if (bi->type == IMGU_FW_SP_FIRMWARE ||
 173                    bi->type == IMGU_FW_SP1_FIRMWARE) {
 174                        css->fw_sp[bi->type == IMGU_FW_SP_FIRMWARE ? 0 : 1] = i;
 175                        if (bi->info.sp.per_frame_data >= css->iomem_length ||
 176                            bi->info.sp.init_dmem_data >= css->iomem_length ||
 177                            bi->info.sp.host_sp_queue >= css->iomem_length ||
 178                            bi->info.sp.isp_started >= css->iomem_length ||
 179                            bi->info.sp.sw_state >= css->iomem_length ||
 180                            bi->info.sp.sleep_mode >= css->iomem_length ||
 181                            bi->info.sp.invalidate_tlb >= css->iomem_length ||
 182                            bi->info.sp.host_sp_com >= css->iomem_length ||
 183                            bi->info.sp.output + 12 >= css->iomem_length ||
 184                            bi->info.sp.host_sp_queues_initialized >=
 185                            css->iomem_length)
 186                                goto bad_fw;
 187                }
 188                if (bi->type != IMGU_FW_ISP_FIRMWARE)
 189                        continue;
 190
 191                if (bi->info.isp.sp.pipeline.mode >= IPU3_CSS_PIPE_ID_NUM)
 192                        goto bad_fw;
 193
 194                if (bi->info.isp.sp.iterator.num_stripes >
 195                    IPU3_UAPI_MAX_STRIPES)
 196                        goto bad_fw;
 197
 198                if (bi->info.isp.num_vf_formats > IMGU_ABI_FRAME_FORMAT_NUM ||
 199                    bi->info.isp.num_output_formats > IMGU_ABI_FRAME_FORMAT_NUM)
 200                        goto bad_fw;
 201
 202                for (j = 0; j < bi->info.isp.num_output_formats; j++)
 203                        if (bi->info.isp.output_formats[j] >=
 204                            IMGU_ABI_FRAME_FORMAT_NUM)
 205                                goto bad_fw;
 206                for (j = 0; j < bi->info.isp.num_vf_formats; j++)
 207                        if (bi->info.isp.vf_formats[j] >=
 208                            IMGU_ABI_FRAME_FORMAT_NUM)
 209                                goto bad_fw;
 210
 211                if (bi->info.isp.sp.block.block_width <= 0 ||
 212                    bi->info.isp.sp.block.block_width > BLOCK_MAX ||
 213                    bi->info.isp.sp.block.output_block_height <= 0 ||
 214                    bi->info.isp.sp.block.output_block_height > BLOCK_MAX)
 215                        goto bad_fw;
 216
 217                if (bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_PARAM]
 218                    + sizeof(struct imgu_fw_param_memory_offsets) >
 219                    css->fw->size ||
 220                    bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_CONFIG]
 221                    + sizeof(struct imgu_fw_config_memory_offsets) >
 222                    css->fw->size ||
 223                    bi->blob.memory_offsets.offsets[IMGU_ABI_PARAM_CLASS_STATE]
 224                    + sizeof(struct imgu_fw_state_memory_offsets) >
 225                    css->fw->size)
 226                        goto bad_fw;
 227
 228                imgu_css_fw_show_binary(dev, bi, name);
 229        }
 230
 231        if (css->fw_bl == -1 || css->fw_sp[0] == -1 || css->fw_sp[1] == -1)
 232                goto bad_fw;
 233
 234        /* Allocate and map fw binaries into IMGU */
 235
 236        css->binary = kcalloc(binary_nr, sizeof(*css->binary), GFP_KERNEL);
 237        if (!css->binary) {
 238                r = -ENOMEM;
 239                goto error_out;
 240        }
 241
 242        for (i = 0; i < css->fwp->file_header.binary_nr; i++) {
 243                struct imgu_fw_info *bi = &css->fwp->binary_header[i];
 244                void *blob = (void *)css->fwp + bi->blob.offset;
 245                size_t size = bi->blob.size;
 246
 247                if (!imgu_dmamap_alloc(imgu, &css->binary[i], size)) {
 248                        r = -ENOMEM;
 249                        goto error_out;
 250                }
 251                memcpy(css->binary[i].vaddr, blob, size);
 252        }
 253
 254        return 0;
 255
 256bad_fw:
 257        dev_err(dev, "invalid firmware binary, size %u\n", (int)css->fw->size);
 258        r = -ENODEV;
 259
 260error_out:
 261        imgu_css_fw_cleanup(css);
 262        return r;
 263}
 264