[v3,07/29] media: iris: implement video firmware load/unload

Message ID 20240827-iris_v3-v3-7-c5fdbbe65e70@quicinc.com (mailing list archive)
State New
Headers
Series Qualcomm iris video decoder driver |

Commit Message

Dikshita Agarwal via B4 Relay Aug. 27, 2024, 10:05 a.m. UTC
  From: Dikshita Agarwal <quic_dikshita@quicinc.com>

Load/unload firmware in memory via mdt loader.
Firmware is loaded as part of core initialization
and unloaded as part of core de-initialization.

Signed-off-by: Dikshita Agarwal <quic_dikshita@quicinc.com>
---
 drivers/media/platform/qcom/iris/Kconfig           |   2 +
 drivers/media/platform/qcom/iris/Makefile          |   1 +
 drivers/media/platform/qcom/iris/iris_core.c       |   8 ++
 drivers/media/platform/qcom/iris/iris_firmware.c   | 146 +++++++++++++++++++++
 drivers/media/platform/qcom/iris/iris_firmware.h   |  14 ++
 .../platform/qcom/iris/iris_platform_common.h      |  12 ++
 .../platform/qcom/iris/iris_platform_sm8250.c      |  10 ++
 .../platform/qcom/iris/iris_platform_sm8550.c      |  10 ++
 8 files changed, 203 insertions(+)
  

Comments

kernel test robot Aug. 27, 2024, 11:13 p.m. UTC | #1
Hi Dikshita,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 31aaa7d95e09892c81df0d7c49ae85640fa4e202]

url:    https://github.com/intel-lab-lkp/linux/commits/Dikshita-Agarwal-via-B4-Relay/dt-bindings-media-Add-sm8550-dt-schema/20240827-181059
base:   31aaa7d95e09892c81df0d7c49ae85640fa4e202
patch link:    https://lore.kernel.org/r/20240827-iris_v3-v3-7-c5fdbbe65e70%40quicinc.com
patch subject: [PATCH v3 07/29] media: iris: implement video firmware load/unload
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20240828/202408280657.WziT1uXM-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240828/202408280657.WziT1uXM-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202408280657.WziT1uXM-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from include/linux/device.h:15,
                    from include/linux/of_reserved_mem.h:5,
                    from drivers/media/platform/qcom/iris/iris_firmware.c:9:
   drivers/media/platform/qcom/iris/iris_firmware.c: In function 'iris_load_fw_to_memory':
>> drivers/media/platform/qcom/iris/iris_firmware.c:80:36: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'ssize_t' {aka 'int'} [-Wformat=]
      80 |                 dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
         |                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/dev_printk.h:110:30: note: in definition of macro 'dev_printk_index_wrap'
     110 |                 _p_func(dev, fmt, ##__VA_ARGS__);                       \
         |                              ^~~
   include/linux/dev_printk.h:154:56: note: in expansion of macro 'dev_fmt'
     154 |         dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
         |                                                        ^~~~~~~
   drivers/media/platform/qcom/iris/iris_firmware.c:80:17: note: in expansion of macro 'dev_err'
      80 |                 dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
         |                 ^~~~~~~
   drivers/media/platform/qcom/iris/iris_firmware.c:80:70: note: format string is defined here
      80 |                 dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
         |                                                                    ~~^
         |                                                                      |
         |                                                                      long int
         |                                                                    %d
>> drivers/media/platform/qcom/iris/iris_firmware.c:80:36: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=]
      80 |                 dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
         |                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/dev_printk.h:110:30: note: in definition of macro 'dev_printk_index_wrap'
     110 |                 _p_func(dev, fmt, ##__VA_ARGS__);                       \
         |                              ^~~
   include/linux/dev_printk.h:154:56: note: in expansion of macro 'dev_fmt'
     154 |         dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
         |                                                        ^~~~~~~
   drivers/media/platform/qcom/iris/iris_firmware.c:80:17: note: in expansion of macro 'dev_err'
      80 |                 dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
         |                 ^~~~~~~
   drivers/media/platform/qcom/iris/iris_firmware.c:80:85: note: format string is defined here
      80 |                 dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
         |                                                                                   ~~^
         |                                                                                     |
         |                                                                                     long unsigned int
         |                                                                                   %u


vim +80 drivers/media/platform/qcom/iris/iris_firmware.c

   > 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 MAX_FIRMWARE_NAME_SIZE	128
    16	
    17	static int iris_protect_cp_mem(struct iris_core *core)
    18	{
    19		struct tz_cp_config *cp_config;
    20		int ret;
    21	
    22		cp_config = core->iris_platform_data->tz_cp_config_data;
    23	
    24		ret = qcom_scm_mem_protect_video_var(cp_config->cp_start,
    25						     cp_config->cp_size,
    26						     cp_config->cp_nonpixel_start,
    27						     cp_config->cp_nonpixel_size);
    28		if (ret)
    29			dev_err(core->dev, "failed to protect memory(%d)\n", ret);
    30	
    31		return ret;
    32	}
    33	
    34	static int iris_load_fw_to_memory(struct iris_core *core, const char *fw_name)
    35	{
    36		const struct firmware *firmware = NULL;
    37		struct device_node *node = NULL;
    38		struct reserved_mem *rmem;
    39		phys_addr_t mem_phys = 0;
    40		void *mem_virt = NULL;
    41		size_t res_size = 0;
    42		ssize_t fw_size = 0;
    43		struct device *dev;
    44		int pas_id = 0;
    45		int ret;
    46	
    47		if (!fw_name || !(*fw_name) || !core)
    48			return -EINVAL;
    49	
    50		dev = core->dev;
    51	
    52		if (strlen(fw_name) >= MAX_FIRMWARE_NAME_SIZE - 4)
    53			return -EINVAL;
    54	
    55		pas_id = core->iris_platform_data->pas_id;
    56	
    57		node = of_parse_phandle(dev->of_node, "memory-region", 0);
    58		if (!node)
    59			return -EINVAL;
    60	
    61		rmem = of_reserved_mem_lookup(node);
    62		if (!rmem) {
    63			ret = -EINVAL;
    64			goto err_put_node;
    65		}
    66	
    67		mem_phys = rmem->base;
    68		res_size = rmem->size;
    69	
    70		ret = request_firmware(&firmware, fw_name, dev);
    71		if (ret) {
    72			dev_err(core->dev, "failed to request fw \"%s\", error %d\n",
    73				fw_name, ret);
    74			goto err_put_node;
    75		}
    76	
    77		fw_size = qcom_mdt_get_size(firmware);
    78		if (fw_size < 0 || res_size < (size_t)fw_size) {
    79			ret = -EINVAL;
  > 80			dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
    81				fw_size, res_size);
    82			goto err_release_fw;
    83		}
    84	
    85		mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
    86		if (!mem_virt) {
    87			dev_err(core->dev, "failed to remap fw memory phys %pa[p]\n",
    88				&mem_phys);
    89			goto err_release_fw;
    90		}
    91	
    92		ret = qcom_mdt_load(dev, firmware, fw_name,
    93				    pas_id, mem_virt, mem_phys, res_size, NULL);
    94		if (ret) {
    95			dev_err(core->dev, "error %d loading fw \"%s\"\n",
    96				ret, fw_name);
    97			goto err_mem_unmap;
    98		}
    99		ret = qcom_scm_pas_auth_and_reset(pas_id);
   100		if (ret) {
   101			dev_err(core->dev, "error %d authenticating fw \"%s\"\n",
   102				ret, fw_name);
   103			goto err_mem_unmap;
   104		}
   105	
   106		return ret;
   107	
   108	err_mem_unmap:
   109		memunmap(mem_virt);
   110	err_release_fw:
   111		release_firmware(firmware);
   112	err_put_node:
   113		of_node_put(node);
   114		return ret;
   115	}
   116
  
Bryan O'Donoghue Aug. 31, 2024, 1:18 p.m. UTC | #2
On 27/08/2024 11:05, Dikshita Agarwal via B4 Relay wrote:
> From: Dikshita Agarwal <quic_dikshita@quicinc.com>
> 
> Load/unload firmware in memory via mdt loader.
> Firmware is loaded as part of core initialization
> and unloaded as part of core de-initialization.
> 
> Signed-off-by: Dikshita Agarwal <quic_dikshita@quicinc.com>
> ---
>   drivers/media/platform/qcom/iris/Kconfig           |   2 +
>   drivers/media/platform/qcom/iris/Makefile          |   1 +
>   drivers/media/platform/qcom/iris/iris_core.c       |   8 ++
>   drivers/media/platform/qcom/iris/iris_firmware.c   | 146 +++++++++++++++++++++
>   drivers/media/platform/qcom/iris/iris_firmware.h   |  14 ++
>   .../platform/qcom/iris/iris_platform_common.h      |  12 ++
>   .../platform/qcom/iris/iris_platform_sm8250.c      |  10 ++
>   .../platform/qcom/iris/iris_platform_sm8550.c      |  10 ++
>   8 files changed, 203 insertions(+)
> 
> diff --git a/drivers/media/platform/qcom/iris/Kconfig b/drivers/media/platform/qcom/iris/Kconfig
> index 4b757bb8adf3..ea518760c65c 100644
> --- a/drivers/media/platform/qcom/iris/Kconfig
> +++ b/drivers/media/platform/qcom/iris/Kconfig
> @@ -3,6 +3,8 @@ config VIDEO_QCOM_IRIS
>           depends on VIDEO_DEV
>           depends on ARCH_QCOM || COMPILE_TEST
>           select V4L2_MEM2MEM_DEV
> +        select QCOM_MDT_LOADER if ARCH_QCOM
> +        select QCOM_SCM
>           help
>             This is a V4L2 driver for Qualcomm Iris video accelerator
>             hardware. It accelerates decoding operations on various
> diff --git a/drivers/media/platform/qcom/iris/Makefile b/drivers/media/platform/qcom/iris/Makefile
> index 48d54e53a576..ddd4c994a0b9 100644
> --- a/drivers/media/platform/qcom/iris/Makefile
> +++ b/drivers/media/platform/qcom/iris/Makefile
> @@ -1,4 +1,5 @@
>   iris-objs += iris_core.o \
> +             iris_firmware.o \
>                iris_hfi_gen1_command.o \
>                iris_hfi_gen2_command.o \
>                iris_hfi_queue.o \
> diff --git a/drivers/media/platform/qcom/iris/iris_core.c b/drivers/media/platform/qcom/iris/iris_core.c
> index 360a54909ef6..8c7d53c57086 100644
> --- a/drivers/media/platform/qcom/iris/iris_core.c
> +++ b/drivers/media/platform/qcom/iris/iris_core.c
> @@ -4,11 +4,13 @@
>    */
>   
>   #include "iris_core.h"
> +#include "iris_firmware.h"
>   #include "iris_state.h"
>   
>   void iris_core_deinit(struct iris_core *core)
>   {
>   	mutex_lock(&core->lock);
> +	iris_fw_unload(core);
>   	iris_hfi_queues_deinit(core);
>   	core->state = IRIS_CORE_DEINIT;
>   	mutex_unlock(&core->lock);
> @@ -33,10 +35,16 @@ int iris_core_init(struct iris_core *core)
>   	if (ret)
>   		goto error;
>   
> +	ret = iris_fw_load(core);
> +	if (ret)
> +		goto error_queue_deinit;
> +
>   	mutex_unlock(&core->lock);
>   
>   	return 0;
>   
> +error_queue_deinit:
> +	iris_hfi_queues_deinit(core);
>   error:
>   	core->state = IRIS_CORE_DEINIT;
>   exit:
> diff --git a/drivers/media/platform/qcom/iris/iris_firmware.c b/drivers/media/platform/qcom/iris/iris_firmware.c
> new file mode 100644
> index 000000000000..55bbcc798f4c
> --- /dev/null
> +++ b/drivers/media/platform/qcom/iris/iris_firmware.c
> @@ -0,0 +1,146 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
> + */
> +
> +#include <linux/firmware.h>
> +#include <linux/firmware/qcom/qcom_scm.h>
> +#include <linux/of_address.h>
> +#include <linux/of_reserved_mem.h>
> +#include <linux/soc/qcom/mdt_loader.h>
> +
> +#include "iris_core.h"
> +#include "iris_firmware.h"
> +
> +#define MAX_FIRMWARE_NAME_SIZE	128
> +
> +static int iris_protect_cp_mem(struct iris_core *core)
> +{
> +	struct tz_cp_config *cp_config;
> +	int ret;
> +
> +	cp_config = core->iris_platform_data->tz_cp_config_data;
> +
> +	ret = qcom_scm_mem_protect_video_var(cp_config->cp_start,
> +					     cp_config->cp_size,
> +					     cp_config->cp_nonpixel_start,
> +					     cp_config->cp_nonpixel_size);
> +	if (ret)
> +		dev_err(core->dev, "failed to protect memory(%d)\n", ret);
> +
> +	return ret;
> +}
> +
> +static int iris_load_fw_to_memory(struct iris_core *core, const char *fw_name)
> +{
> +	const struct firmware *firmware = NULL;
> +	struct device_node *node = NULL;
> +	struct reserved_mem *rmem;
> +	phys_addr_t mem_phys = 0;
> +	void *mem_virt = NULL;
> +	size_t res_size = 0;
> +	ssize_t fw_size = 0;
> +	struct device *dev;
> +	int pas_id = 0;
> +	int ret;
> +
> +	if (!fw_name || !(*fw_name) || !core)
> +		return -EINVAL;
> +
> +	dev = core->dev;
> +
> +	if (strlen(fw_name) >= MAX_FIRMWARE_NAME_SIZE - 4)
> +		return -EINVAL;
> +
> +	pas_id = core->iris_platform_data->pas_id;
> +
> +	node = of_parse_phandle(dev->of_node, "memory-region", 0);
> +	if (!node)
> +		return -EINVAL;
> +
> +	rmem = of_reserved_mem_lookup(node);
> +	if (!rmem) {
> +		ret = -EINVAL;
> +		goto err_put_node;
> +	}
> +
> +	mem_phys = rmem->base;
> +	res_size = rmem->size;
> +
> +	ret = request_firmware(&firmware, fw_name, dev);
> +	if (ret) {
> +		dev_err(core->dev, "failed to request fw \"%s\", error %d\n",
> +			fw_name, ret);
> +		goto err_put_node;
> +	}
> +
> +	fw_size = qcom_mdt_get_size(firmware);
> +	if (fw_size < 0 || res_size < (size_t)fw_size) {
> +		ret = -EINVAL;
> +		dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
> +			fw_size, res_size);
> +		goto err_release_fw;
> +	}
> +
> +	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
> +	if (!mem_virt) {
> +		dev_err(core->dev, "failed to remap fw memory phys %pa[p]\n",
> +			&mem_phys);
> +		goto err_release_fw;
> +	}
> +
> +	ret = qcom_mdt_load(dev, firmware, fw_name,
> +			    pas_id, mem_virt, mem_phys, res_size, NULL);
> +	if (ret) {
> +		dev_err(core->dev, "error %d loading fw \"%s\"\n",
> +			ret, fw_name);
> +		goto err_mem_unmap;
> +	}
> +	ret = qcom_scm_pas_auth_and_reset(pas_id);
> +	if (ret) {
> +		dev_err(core->dev, "error %d authenticating fw \"%s\"\n",
> +			ret, fw_name);
> +		goto err_mem_unmap;
> +	}
> +
> +	return ret;
> +
> +err_mem_unmap:
> +	memunmap(mem_virt);
> +err_release_fw:
> +	release_firmware(firmware);
> +err_put_node:
> +	of_node_put(node);
> +	return ret;
> +}
> +
> +int iris_fw_load(struct iris_core *core)
> +{
> +	int ret;
> +
> +	ret = iris_load_fw_to_memory(core, core->iris_platform_data->fwname);
> +	if (ret) {
> +		dev_err(core->dev, "firmware download failed\n");
> +		return -ENOMEM;
> +	}
> +
> +	ret = iris_protect_cp_mem(core);
> +	if (ret) {
> +		dev_err(core->dev, "protect memory failed\n");
> +		qcom_scm_pas_shutdown(core->iris_platform_data->pas_id);
> +		return ret;
> +	}
> +
> +	return ret;
> +}
> +
> +int iris_fw_unload(struct iris_core *core)
> +{
> +	int ret;
> +
> +	ret = qcom_scm_pas_shutdown(core->iris_platform_data->pas_id);
> +	if (ret)
> +		dev_err(core->dev, "firmware unload failed with ret %d\n", ret);
> +
> +	return ret;
> +}
> diff --git a/drivers/media/platform/qcom/iris/iris_firmware.h b/drivers/media/platform/qcom/iris/iris_firmware.h
> new file mode 100644
> index 000000000000..8d4f6b7f75c5
> --- /dev/null
> +++ b/drivers/media/platform/qcom/iris/iris_firmware.h
> @@ -0,0 +1,14 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
> + */
> +
> +#ifndef _IRIS_FIRMWARE_H_
> +#define _IRIS_FIRMWARE_H_
> +
> +struct iris_core;
> +
> +int iris_fw_load(struct iris_core *core);
> +int iris_fw_unload(struct iris_core *core);
> +
> +#endif
> diff --git a/drivers/media/platform/qcom/iris/iris_platform_common.h b/drivers/media/platform/qcom/iris/iris_platform_common.h
> index 5287fdf913bc..9c919367f9d7 100644
> --- a/drivers/media/platform/qcom/iris/iris_platform_common.h
> +++ b/drivers/media/platform/qcom/iris/iris_platform_common.h
> @@ -6,6 +6,8 @@
>   #ifndef _IRIS_PLATFORM_COMMON_H_
>   #define _IRIS_PLATFORM_COMMON_H_
>   
> +#define IRIS_PAS_ID				9
> +
>   extern struct iris_platform_data sm8550_data;
>   extern struct iris_platform_data sm8250_data;
>   
> @@ -20,6 +22,13 @@ struct platform_clk_data {
>   	const char *clk_name;
>   };
>   
> +struct tz_cp_config {
> +	u32 cp_start;
> +	u32 cp_size;
> +	u32 cp_nonpixel_start;
> +	u32 cp_nonpixel_size;
> +};
> +
>   struct iris_platform_data {
>   	struct iris_inst *(*get_instance)(void);
>   	const struct icc_info *icc_tbl;
> @@ -33,6 +42,9 @@ struct iris_platform_data {
>   	const char * const *clk_rst_tbl;
>   	unsigned int clk_rst_tbl_size;
>   	u64 dma_mask;
> +	const char *fwname;
> +	u32 pas_id;
> +	struct tz_cp_config *tz_cp_config_data;
>   };
>   
>   #endif
> diff --git a/drivers/media/platform/qcom/iris/iris_platform_sm8250.c b/drivers/media/platform/qcom/iris/iris_platform_sm8250.c
> index 664cb2b359a3..36bcb1b851ed 100644
> --- a/drivers/media/platform/qcom/iris/iris_platform_sm8250.c
> +++ b/drivers/media/platform/qcom/iris/iris_platform_sm8250.c
> @@ -25,6 +25,13 @@ static const struct platform_clk_data sm8250_clk_table[] = {
>   	{IRIS_HW_CLK,   "vcodec0_core" },
>   };
>   
> +static struct tz_cp_config tz_cp_config_sm8250 = {
> +	.cp_start = 0,
> +	.cp_size = 0x25800000,
> +	.cp_nonpixel_start = 0x01000000,
> +	.cp_nonpixel_size = 0x24800000,
> +};
> +
>   struct iris_platform_data sm8250_data = {
>   	.get_instance = iris_hfi_gen1_get_instance,
>   	.icc_tbl = sm8250_icc_table,
> @@ -38,4 +45,7 @@ struct iris_platform_data sm8250_data = {
>   	.clk_tbl = sm8250_clk_table,
>   	.clk_tbl_size = ARRAY_SIZE(sm8250_clk_table),
>   	.dma_mask = GENMASK(31, 29) - 1,
> +	.fwname = "qcom/vpu/vpu20_p4.mbn",


RB5/sm8250 on this kernel

https://git.codelinaro.org/bryan.odonoghue/kernel/-/tree/linux-stable-24-08-29-iris-v3-rb5?ref_type=heads

CONFIG_QCOM_VENUS = m

Loads fine and I can run this simple smoke test

ffplay -loglevel debug -codec:video h264_v4l2m2m -i sample-5s.mp4
gst-launch-1.0 -vvv -e filesrc location=sample-5s.mp4 ! qtdemux ! 
parsebin ! v4l2h264dec ! autovideosink


CONFIG_QCOM_IRIS = m

[    2.130077] hw perfevents: enabled with armv8_pmuv3 PMU driver, 7 
counters available
[   12.282381] qcom-iris aa00000.video-codec: Adding to iommu group 13
[   12.463983] qcom-iris aa00000.video-codec: Direct firmware load for 
qcom/vpu/vpu20_p4.mbn failed with error -2
[   12.474396] qcom-iris aa00000.video-codec: failed to request fw 
"qcom/vpu/vpu20_p4.mbn", error -2
[   12.490919] qcom-iris aa00000.video-codec: firmware download failed
[   12.516391] qcom-iris aa00000.video-codec: core init failed

In venus the firmware name is

.fwname = "qcom/vpu-1.0/venus.mbn"

What firmware reference are you testing on with your rb5/sm8250 stuff ?

Mine is linux-firmware / Debian sid.

root@linaro-gnome:~# dpkg -S /lib/firmware/qcom/vpu-1.0/venus.mbn
firmware-qcom-soc: /lib/firmware/qcom/vpu-1.0/venus.mbn

https://packages.debian.org/sid/firmware-qcom-soc

Anyway I don't see much logic/justification for the firmware string change.

---
bod
  
Dmitry Baryshkov Sept. 2, 2024, 12:04 a.m. UTC | #3
On Sat, Aug 31, 2024 at 02:18:16PM GMT, Bryan O'Donoghue wrote:
> On 27/08/2024 11:05, Dikshita Agarwal via B4 Relay wrote:
> > From: Dikshita Agarwal <quic_dikshita@quicinc.com>
> > 
> > Load/unload firmware in memory via mdt loader.
> > Firmware is loaded as part of core initialization
> > and unloaded as part of core de-initialization.
> > 
> > Signed-off-by: Dikshita Agarwal <quic_dikshita@quicinc.com>
> > ---
> >   drivers/media/platform/qcom/iris/Kconfig           |   2 +
> >   drivers/media/platform/qcom/iris/Makefile          |   1 +
> >   drivers/media/platform/qcom/iris/iris_core.c       |   8 ++
> >   drivers/media/platform/qcom/iris/iris_firmware.c   | 146 +++++++++++++++++++++
> >   drivers/media/platform/qcom/iris/iris_firmware.h   |  14 ++
> >   .../platform/qcom/iris/iris_platform_common.h      |  12 ++
> >   .../platform/qcom/iris/iris_platform_sm8250.c      |  10 ++
> >   .../platform/qcom/iris/iris_platform_sm8550.c      |  10 ++
> >   8 files changed, 203 insertions(+)
> > 

[skipped]

> > @@ -38,4 +45,7 @@ struct iris_platform_data sm8250_data = {
> >   	.clk_tbl = sm8250_clk_table,
> >   	.clk_tbl_size = ARRAY_SIZE(sm8250_clk_table),
> >   	.dma_mask = GENMASK(31, 29) - 1,
> > +	.fwname = "qcom/vpu/vpu20_p4.mbn",
> 
> 
> RB5/sm8250 on this kernel
> 
> https://git.codelinaro.org/bryan.odonoghue/kernel/-/tree/linux-stable-24-08-29-iris-v3-rb5?ref_type=heads
> 
> CONFIG_QCOM_VENUS = m
> 
> Loads fine and I can run this simple smoke test
> 
> ffplay -loglevel debug -codec:video h264_v4l2m2m -i sample-5s.mp4
> gst-launch-1.0 -vvv -e filesrc location=sample-5s.mp4 ! qtdemux ! parsebin !
> v4l2h264dec ! autovideosink
> 
> 
> CONFIG_QCOM_IRIS = m
> 
> [    2.130077] hw perfevents: enabled with armv8_pmuv3 PMU driver, 7
> counters available
> [   12.282381] qcom-iris aa00000.video-codec: Adding to iommu group 13
> [   12.463983] qcom-iris aa00000.video-codec: Direct firmware load for
> qcom/vpu/vpu20_p4.mbn failed with error -2
> [   12.474396] qcom-iris aa00000.video-codec: failed to request fw
> "qcom/vpu/vpu20_p4.mbn", error -2
> [   12.490919] qcom-iris aa00000.video-codec: firmware download failed
> [   12.516391] qcom-iris aa00000.video-codec: core init failed
> 
> In venus the firmware name is
> 
> .fwname = "qcom/vpu-1.0/venus.mbn"
> 
> What firmware reference are you testing on with your rb5/sm8250 stuff ?
> 
> Mine is linux-firmware / Debian sid.

linux-firmware Git supports both firmware names, but I agree, the driver
should be using the old firmware name. At least until linux-firmware
gets released and that name actually propagates to several distros.

> 
> root@linaro-gnome:~# dpkg -S /lib/firmware/qcom/vpu-1.0/venus.mbn
> firmware-qcom-soc: /lib/firmware/qcom/vpu-1.0/venus.mbn
> 
> https://packages.debian.org/sid/firmware-qcom-soc
> 
> Anyway I don't see much logic/justification for the firmware string change.
> 
> ---
> bod
  
Dikshita Agarwal Sept. 5, 2024, 6:17 a.m. UTC | #4
On 9/2/2024 5:34 AM, Dmitry Baryshkov wrote:
> On Sat, Aug 31, 2024 at 02:18:16PM GMT, Bryan O'Donoghue wrote:
>> On 27/08/2024 11:05, Dikshita Agarwal via B4 Relay wrote:
>>> From: Dikshita Agarwal <quic_dikshita@quicinc.com>
>>>
>>> Load/unload firmware in memory via mdt loader.
>>> Firmware is loaded as part of core initialization
>>> and unloaded as part of core de-initialization.
>>>
>>> Signed-off-by: Dikshita Agarwal <quic_dikshita@quicinc.com>
>>> ---
>>>   drivers/media/platform/qcom/iris/Kconfig           |   2 +
>>>   drivers/media/platform/qcom/iris/Makefile          |   1 +
>>>   drivers/media/platform/qcom/iris/iris_core.c       |   8 ++
>>>   drivers/media/platform/qcom/iris/iris_firmware.c   | 146 +++++++++++++++++++++
>>>   drivers/media/platform/qcom/iris/iris_firmware.h   |  14 ++
>>>   .../platform/qcom/iris/iris_platform_common.h      |  12 ++
>>>   .../platform/qcom/iris/iris_platform_sm8250.c      |  10 ++
>>>   .../platform/qcom/iris/iris_platform_sm8550.c      |  10 ++
>>>   8 files changed, 203 insertions(+)
>>>
> 
> [skipped]
> 
>>> @@ -38,4 +45,7 @@ struct iris_platform_data sm8250_data = {
>>>   	.clk_tbl = sm8250_clk_table,
>>>   	.clk_tbl_size = ARRAY_SIZE(sm8250_clk_table),
>>>   	.dma_mask = GENMASK(31, 29) - 1,
>>> +	.fwname = "qcom/vpu/vpu20_p4.mbn",
>>
>>
>> RB5/sm8250 on this kernel
>>
>> https://git.codelinaro.org/bryan.odonoghue/kernel/-/tree/linux-stable-24-08-29-iris-v3-rb5?ref_type=heads
>>
>> CONFIG_QCOM_VENUS = m
>>
>> Loads fine and I can run this simple smoke test
>>
>> ffplay -loglevel debug -codec:video h264_v4l2m2m -i sample-5s.mp4
>> gst-launch-1.0 -vvv -e filesrc location=sample-5s.mp4 ! qtdemux ! parsebin !
>> v4l2h264dec ! autovideosink
>>
>>
>> CONFIG_QCOM_IRIS = m
>>
>> [    2.130077] hw perfevents: enabled with armv8_pmuv3 PMU driver, 7
>> counters available
>> [   12.282381] qcom-iris aa00000.video-codec: Adding to iommu group 13
>> [   12.463983] qcom-iris aa00000.video-codec: Direct firmware load for
>> qcom/vpu/vpu20_p4.mbn failed with error -2
>> [   12.474396] qcom-iris aa00000.video-codec: failed to request fw
>> "qcom/vpu/vpu20_p4.mbn", error -2
>> [   12.490919] qcom-iris aa00000.video-codec: firmware download failed
>> [   12.516391] qcom-iris aa00000.video-codec: core init failed
>>
>> In venus the firmware name is
>>
>> .fwname = "qcom/vpu-1.0/venus.mbn"
>>
>> What firmware reference are you testing on with your rb5/sm8250 stuff ?
>>
>> Mine is linux-firmware / Debian sid.
> 
> linux-firmware Git supports both firmware names, but I agree, the driver
> should be using the old firmware name. At least until linux-firmware
> gets released and that name actually propagates to several distros.
> 
Sure, will update the firmware name based on the status of linux-firmware
in the next patch.
>>
>> root@linaro-gnome:~# dpkg -S /lib/firmware/qcom/vpu-1.0/venus.mbn
>> firmware-qcom-soc: /lib/firmware/qcom/vpu-1.0/venus.mbn
>>
>> https://packages.debian.org/sid/firmware-qcom-soc
>>
>> Anyway I don't see much logic/justification for the firmware string change.
>>
>> ---
>> bod
>
  

Patch

diff --git a/drivers/media/platform/qcom/iris/Kconfig b/drivers/media/platform/qcom/iris/Kconfig
index 4b757bb8adf3..ea518760c65c 100644
--- a/drivers/media/platform/qcom/iris/Kconfig
+++ b/drivers/media/platform/qcom/iris/Kconfig
@@ -3,6 +3,8 @@  config VIDEO_QCOM_IRIS
         depends on VIDEO_DEV
         depends on ARCH_QCOM || COMPILE_TEST
         select V4L2_MEM2MEM_DEV
+        select QCOM_MDT_LOADER if ARCH_QCOM
+        select QCOM_SCM
         help
           This is a V4L2 driver for Qualcomm Iris video accelerator
           hardware. It accelerates decoding operations on various
diff --git a/drivers/media/platform/qcom/iris/Makefile b/drivers/media/platform/qcom/iris/Makefile
index 48d54e53a576..ddd4c994a0b9 100644
--- a/drivers/media/platform/qcom/iris/Makefile
+++ b/drivers/media/platform/qcom/iris/Makefile
@@ -1,4 +1,5 @@ 
 iris-objs += iris_core.o \
+             iris_firmware.o \
              iris_hfi_gen1_command.o \
              iris_hfi_gen2_command.o \
              iris_hfi_queue.o \
diff --git a/drivers/media/platform/qcom/iris/iris_core.c b/drivers/media/platform/qcom/iris/iris_core.c
index 360a54909ef6..8c7d53c57086 100644
--- a/drivers/media/platform/qcom/iris/iris_core.c
+++ b/drivers/media/platform/qcom/iris/iris_core.c
@@ -4,11 +4,13 @@ 
  */
 
 #include "iris_core.h"
+#include "iris_firmware.h"
 #include "iris_state.h"
 
 void iris_core_deinit(struct iris_core *core)
 {
 	mutex_lock(&core->lock);
+	iris_fw_unload(core);
 	iris_hfi_queues_deinit(core);
 	core->state = IRIS_CORE_DEINIT;
 	mutex_unlock(&core->lock);
@@ -33,10 +35,16 @@  int iris_core_init(struct iris_core *core)
 	if (ret)
 		goto error;
 
+	ret = iris_fw_load(core);
+	if (ret)
+		goto error_queue_deinit;
+
 	mutex_unlock(&core->lock);
 
 	return 0;
 
+error_queue_deinit:
+	iris_hfi_queues_deinit(core);
 error:
 	core->state = IRIS_CORE_DEINIT;
 exit:
diff --git a/drivers/media/platform/qcom/iris/iris_firmware.c b/drivers/media/platform/qcom/iris/iris_firmware.c
new file mode 100644
index 000000000000..55bbcc798f4c
--- /dev/null
+++ b/drivers/media/platform/qcom/iris/iris_firmware.c
@@ -0,0 +1,146 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#include <linux/firmware.h>
+#include <linux/firmware/qcom/qcom_scm.h>
+#include <linux/of_address.h>
+#include <linux/of_reserved_mem.h>
+#include <linux/soc/qcom/mdt_loader.h>
+
+#include "iris_core.h"
+#include "iris_firmware.h"
+
+#define MAX_FIRMWARE_NAME_SIZE	128
+
+static int iris_protect_cp_mem(struct iris_core *core)
+{
+	struct tz_cp_config *cp_config;
+	int ret;
+
+	cp_config = core->iris_platform_data->tz_cp_config_data;
+
+	ret = qcom_scm_mem_protect_video_var(cp_config->cp_start,
+					     cp_config->cp_size,
+					     cp_config->cp_nonpixel_start,
+					     cp_config->cp_nonpixel_size);
+	if (ret)
+		dev_err(core->dev, "failed to protect memory(%d)\n", ret);
+
+	return ret;
+}
+
+static int iris_load_fw_to_memory(struct iris_core *core, const char *fw_name)
+{
+	const struct firmware *firmware = NULL;
+	struct device_node *node = NULL;
+	struct reserved_mem *rmem;
+	phys_addr_t mem_phys = 0;
+	void *mem_virt = NULL;
+	size_t res_size = 0;
+	ssize_t fw_size = 0;
+	struct device *dev;
+	int pas_id = 0;
+	int ret;
+
+	if (!fw_name || !(*fw_name) || !core)
+		return -EINVAL;
+
+	dev = core->dev;
+
+	if (strlen(fw_name) >= MAX_FIRMWARE_NAME_SIZE - 4)
+		return -EINVAL;
+
+	pas_id = core->iris_platform_data->pas_id;
+
+	node = of_parse_phandle(dev->of_node, "memory-region", 0);
+	if (!node)
+		return -EINVAL;
+
+	rmem = of_reserved_mem_lookup(node);
+	if (!rmem) {
+		ret = -EINVAL;
+		goto err_put_node;
+	}
+
+	mem_phys = rmem->base;
+	res_size = rmem->size;
+
+	ret = request_firmware(&firmware, fw_name, dev);
+	if (ret) {
+		dev_err(core->dev, "failed to request fw \"%s\", error %d\n",
+			fw_name, ret);
+		goto err_put_node;
+	}
+
+	fw_size = qcom_mdt_get_size(firmware);
+	if (fw_size < 0 || res_size < (size_t)fw_size) {
+		ret = -EINVAL;
+		dev_err(core->dev, "out of bound fw image fw size: %ld, res_size: %lu\n",
+			fw_size, res_size);
+		goto err_release_fw;
+	}
+
+	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
+	if (!mem_virt) {
+		dev_err(core->dev, "failed to remap fw memory phys %pa[p]\n",
+			&mem_phys);
+		goto err_release_fw;
+	}
+
+	ret = qcom_mdt_load(dev, firmware, fw_name,
+			    pas_id, mem_virt, mem_phys, res_size, NULL);
+	if (ret) {
+		dev_err(core->dev, "error %d loading fw \"%s\"\n",
+			ret, fw_name);
+		goto err_mem_unmap;
+	}
+	ret = qcom_scm_pas_auth_and_reset(pas_id);
+	if (ret) {
+		dev_err(core->dev, "error %d authenticating fw \"%s\"\n",
+			ret, fw_name);
+		goto err_mem_unmap;
+	}
+
+	return ret;
+
+err_mem_unmap:
+	memunmap(mem_virt);
+err_release_fw:
+	release_firmware(firmware);
+err_put_node:
+	of_node_put(node);
+	return ret;
+}
+
+int iris_fw_load(struct iris_core *core)
+{
+	int ret;
+
+	ret = iris_load_fw_to_memory(core, core->iris_platform_data->fwname);
+	if (ret) {
+		dev_err(core->dev, "firmware download failed\n");
+		return -ENOMEM;
+	}
+
+	ret = iris_protect_cp_mem(core);
+	if (ret) {
+		dev_err(core->dev, "protect memory failed\n");
+		qcom_scm_pas_shutdown(core->iris_platform_data->pas_id);
+		return ret;
+	}
+
+	return ret;
+}
+
+int iris_fw_unload(struct iris_core *core)
+{
+	int ret;
+
+	ret = qcom_scm_pas_shutdown(core->iris_platform_data->pas_id);
+	if (ret)
+		dev_err(core->dev, "firmware unload failed with ret %d\n", ret);
+
+	return ret;
+}
diff --git a/drivers/media/platform/qcom/iris/iris_firmware.h b/drivers/media/platform/qcom/iris/iris_firmware.h
new file mode 100644
index 000000000000..8d4f6b7f75c5
--- /dev/null
+++ b/drivers/media/platform/qcom/iris/iris_firmware.h
@@ -0,0 +1,14 @@ 
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#ifndef _IRIS_FIRMWARE_H_
+#define _IRIS_FIRMWARE_H_
+
+struct iris_core;
+
+int iris_fw_load(struct iris_core *core);
+int iris_fw_unload(struct iris_core *core);
+
+#endif
diff --git a/drivers/media/platform/qcom/iris/iris_platform_common.h b/drivers/media/platform/qcom/iris/iris_platform_common.h
index 5287fdf913bc..9c919367f9d7 100644
--- a/drivers/media/platform/qcom/iris/iris_platform_common.h
+++ b/drivers/media/platform/qcom/iris/iris_platform_common.h
@@ -6,6 +6,8 @@ 
 #ifndef _IRIS_PLATFORM_COMMON_H_
 #define _IRIS_PLATFORM_COMMON_H_
 
+#define IRIS_PAS_ID				9
+
 extern struct iris_platform_data sm8550_data;
 extern struct iris_platform_data sm8250_data;
 
@@ -20,6 +22,13 @@  struct platform_clk_data {
 	const char *clk_name;
 };
 
+struct tz_cp_config {
+	u32 cp_start;
+	u32 cp_size;
+	u32 cp_nonpixel_start;
+	u32 cp_nonpixel_size;
+};
+
 struct iris_platform_data {
 	struct iris_inst *(*get_instance)(void);
 	const struct icc_info *icc_tbl;
@@ -33,6 +42,9 @@  struct iris_platform_data {
 	const char * const *clk_rst_tbl;
 	unsigned int clk_rst_tbl_size;
 	u64 dma_mask;
+	const char *fwname;
+	u32 pas_id;
+	struct tz_cp_config *tz_cp_config_data;
 };
 
 #endif
diff --git a/drivers/media/platform/qcom/iris/iris_platform_sm8250.c b/drivers/media/platform/qcom/iris/iris_platform_sm8250.c
index 664cb2b359a3..36bcb1b851ed 100644
--- a/drivers/media/platform/qcom/iris/iris_platform_sm8250.c
+++ b/drivers/media/platform/qcom/iris/iris_platform_sm8250.c
@@ -25,6 +25,13 @@  static const struct platform_clk_data sm8250_clk_table[] = {
 	{IRIS_HW_CLK,   "vcodec0_core" },
 };
 
+static struct tz_cp_config tz_cp_config_sm8250 = {
+	.cp_start = 0,
+	.cp_size = 0x25800000,
+	.cp_nonpixel_start = 0x01000000,
+	.cp_nonpixel_size = 0x24800000,
+};
+
 struct iris_platform_data sm8250_data = {
 	.get_instance = iris_hfi_gen1_get_instance,
 	.icc_tbl = sm8250_icc_table,
@@ -38,4 +45,7 @@  struct iris_platform_data sm8250_data = {
 	.clk_tbl = sm8250_clk_table,
 	.clk_tbl_size = ARRAY_SIZE(sm8250_clk_table),
 	.dma_mask = GENMASK(31, 29) - 1,
+	.fwname = "qcom/vpu/vpu20_p4.mbn",
+	.pas_id = IRIS_PAS_ID,
+	.tz_cp_config_data = &tz_cp_config_sm8250,
 };
diff --git a/drivers/media/platform/qcom/iris/iris_platform_sm8550.c b/drivers/media/platform/qcom/iris/iris_platform_sm8550.c
index 0db832b099ac..1bb34c3e6e18 100644
--- a/drivers/media/platform/qcom/iris/iris_platform_sm8550.c
+++ b/drivers/media/platform/qcom/iris/iris_platform_sm8550.c
@@ -25,6 +25,13 @@  static const struct platform_clk_data sm8550_clk_table[] = {
 	{IRIS_HW_CLK,   "vcodec0_core" },
 };
 
+static struct tz_cp_config tz_cp_config_sm8550 = {
+	.cp_start = 0,
+	.cp_size = 0x25800000,
+	.cp_nonpixel_start = 0x01000000,
+	.cp_nonpixel_size = 0x24800000,
+};
+
 struct iris_platform_data sm8550_data = {
 	.get_instance = iris_hfi_gen2_get_instance,
 	.icc_tbl = sm8550_icc_table,
@@ -38,4 +45,7 @@  struct iris_platform_data sm8550_data = {
 	.clk_tbl = sm8550_clk_table,
 	.clk_tbl_size = ARRAY_SIZE(sm8550_clk_table),
 	.dma_mask = GENMASK(31, 29) - 1,
+	.fwname = "qcom/vpu/vpu30_p4.mbn",
+	.pas_id = IRIS_PAS_ID,
+	.tz_cp_config_data = &tz_cp_config_sm8550,
 };