media: mali-c55: Add support for CCM and Gamma
Subsystems (2)
Jacopo Mondi
maintainerv3 cover media: mali-c55: Add support for CCM and Gamma
Add support for Ccm and Gamma to the Mali-C55 ISP by defining the corresponding blocks in the uAPI and implementing their handling in the driver.
v3 implements validation of CCM and Gamma parameters using the new block_validate() callback from v4l2-isp.
I kept the two patches unsquashed, to discuss if it is worth validating the fields or we should rely on masking before writing to registers.
---
Changes in v3:
- Address review comments from Linus and Vincenzo (changelog per patch)
- Rebase on top of
"[PATCH v3 0/6] media: v4l2-isp: Add support for extensible statistics"
https://patchwork.linuxtv.org/project/linux-media/list/?series=26929
to perform per-block validation
- Add 2 patches for validating CCM and Gamma parameters
- Collect tags
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- remove unused 'rgb_enable' in gamma uapi
- address checkpatch issues
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jacopo Mondi (4):
media: mali-c55: Add support for CCM
media: mali-c55: Implement CCM block validation
media: mali-c55: Add support for RGB Gamma
media: mali-c55: Implement Gamma block validation
.../media/platform/arm/mali-c55/mali-c55-params.c | 197 +++++++++++++++++++++ .../platform/arm/mali-c55/mali-c55-registers.h | 13 +- include/uapi/linux/media/arm/mali-c55-config.h | 84 ++++++++- 3 files changed, 289 insertions(+), 5 deletions(-)
---
base-commit: 06cb687a5132fcffe624c0070576ab852ac6b568
change-id: 20260616-mali-c55-ccm-gamma-c02a0df59f98
prerequisite-message-id: [email protected]
prerequisite-patch-id: 2939dff7f477209138725aa8d86318d6580f4ea3
prerequisite-patch-id: f77370d8fc480b2e972a773f7d56f33ff1995eef
prerequisite-patch-id: bd3b231bc86129d25fca4ae34408a61db43bf883
prerequisite-patch-id: e44f05055f4bc9dc93ae6bd25af80c64f5ce627a
prerequisite-patch-id: a5f27213eba078f7ed5420d90df3ecd45151219e
prerequisite-patch-id: 9b2fbe610fb17689030c6da6c529f7db91b86d9c
Best regards,
Signature · 1 line
Jacopo Mondi <[email protected]>
Jacopo Mondi
maintainerPATCH v3 1/4 media: mali-c55: Add support for CCM
From: Jacopo Mondi <[email protected]>
Add support for the CCM (Color Correction Matrix) for the Mali C55 ISP.
Define a new block in the uAPI using the extensible v4l2-isp format and implement support for configuring the CCM parameters in the mali-c55 ISP driver.
--- .../media/platform/arm/mali-c55/mali-c55-params.c | 52 ++++++++++++++++++++++ include/uapi/linux/media/arm/mali-c55-config.h | 41 ++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-)
drivers/media/platform/arm/mali-c55/mali-c55-params.c +52 -0
@@ -46,6 +46,7 @@* @awb_config: For header->type == MALI_C55_PARAM_BLOCK_AWB_CONFIG * @shading_config: For header->type == MALI_C55_PARAM_MESH_SHADING_CONFIG * @shading_selection: For header->type == MALI_C55_PARAM_MESH_SHADING_SELECTION +* @ccm: For header->type == MALI_C55_PARAM_BLOCK_CCM * @data: Allows easy initialisation of a union variable with a * pointer into a __u8 array. */
@@ -59,6 +60,7 @@ union mali_c55_params_block {const struct mali_c55_params_awb_config *awb_config;const struct mali_c55_params_mesh_shading_config *shading_config;const struct mali_c55_params_mesh_shading_selection *shading_selection; +const struct mali_c55_params_ccm *ccm;const __u8 *data; };
@@ -414,6 +416,52 @@ static void mali_c55_params_lsc_selection(struct mali_c55 *mali_c55, params->mesh_strength); } +static void mali_c55_params_ccm (struct mali_c55 *mali_c55, +union mali_c55_params_block block) +{ +const struct mali_c55_params_ccm *params = block.ccm; + +if (block.header->flags &V4L2_ISP_PARAMS_FL_BLOCK_DISABLE ) { +mali_c55_ctx_write (mali_c55,MALI_C55_REG_CCM_ENABLE ,0 ); +return ; + } + +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_R_R , +MALI_C55_CCM_COEF_MASK , params->coeffs[0 ][0 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_R_G , +MALI_C55_CCM_COEF_MASK , params->coeffs[0 ][1 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_R_B , +MALI_C55_CCM_COEF_MASK , params->coeffs[0 ][2 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_G_R , +MALI_C55_CCM_COEF_MASK , params->coeffs[1 ][0 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_G_G , +MALI_C55_CCM_COEF_MASK , params->coeffs[1 ][1 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_G_B , +MALI_C55_CCM_COEF_MASK , params->coeffs[1 ][2 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_B_R , +MALI_C55_CCM_COEF_MASK , params->coeffs[2 ][0 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_B_G , +MALI_C55_CCM_COEF_MASK , params->coeffs[2 ][1 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_COEF_B_B , +MALI_C55_CCM_COEF_MASK , params->coeffs[2 ][2 ]); + +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_ANTIFOG_GAIN_R , +MALI_C55_CCM_ANTIFOG_GAIN_MASK , params->gains[0 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_ANTIFOG_GAIN_G , +MALI_C55_CCM_ANTIFOG_GAIN_MASK , params->gains[1 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_ANTIFOG_GAIN_B , +MALI_C55_CCM_ANTIFOG_GAIN_MASK , params->gains[2 ]); + +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_ANTIFOG_OFFSET_R , +MALI_C55_CCM_ANTIFOG_OFFSET_MASK , params->offs[0 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_ANTIFOG_OFFSET_G , +MALI_C55_CCM_ANTIFOG_OFFSET_MASK , params->offs[1 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_CCM_ANTIFOG_OFFSET_B , +MALI_C55_CCM_ANTIFOG_OFFSET_MASK , params->offs[2 ]); + +mali_c55_ctx_write (mali_c55,MALI_C55_REG_CCM_ENABLE ,1 ); +} +static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = &mali_c55_params_sensor_offs, [MALI_C55_PARAM_BLOCK_AEXP_HIST ] = &mali_c55_params_aexp_hist,
@@ -426,6 +474,7 @@ static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_AWB_GAINS_AEXP] = &mali_c55_params_awb_gains, [MALI_C55_PARAM_MESH_SHADING_CONFIG] = &mali_c55_params_lsc_config, [MALI_C55_PARAM_MESH_SHADING_SELECTION] = &mali_c55_params_lsc_selection, + [MALI_C55_PARAM_BLOCK_CCM] = &mali_c55_params_ccm, }; static const struct v4l2_isp_params_block_type_info
@@ -463,6 +512,9 @@ mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_MESH_SHADING_SELECTION ] = { .size =sizeof (struct mali_c55_params_mesh_shading_selection ), }, + [MALI_C55_PARAM_BLOCK_CCM ] = { + .size =sizeof (struct mali_c55_params_ccm ), + }, }; static_assert(ARRAY_SIZE(mali_c55_params_handlers) ==
include/uapi/linux/media/arm/mali-c55-config.h +40 -1
64 lines not on this page · raw message
Signature · 1 line
2.54.0
Jacopo Mondi
maintainerPATCH v3 2/4 media: mali-c55: Implement CCM block validation
In reply to Jacopo MondiFrom: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct v4l2_isp_params_block_type_info.
---
v3:
- new patch
--- .../media/platform/arm/mali-c55/mali-c55-params.c | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+)
drivers/media/platform/arm/mali-c55/mali-c55-params.c +36 -0
@@ -477,6 +477,41 @@ static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_CCM] = &mali_c55_params_ccm, }; +static int mali_c55_ccm_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_ccm *ccm = + (const struct mali_c55_params_ccm *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { + +for (unsigned int j =0 ; j <3 ; j++) { +/* Coefficients are 13 bits signed Q4.8. */ +if (ccm->coeffs[i][j] >8191 ) { +dev_dbg (dev,"Invalid ccm coefficient %u\n" , + ccm->coeffs[i][j]); +return -EINVAL ; + } + } + +/* Gains are 12 bits unsigned Q4.8. */ +if (ccm->gains[i] >4095 ) { +dev_dbg (dev,"Invalid ccm gain %u\n" , + ccm->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (ccm->offs[i] >4095 ) { +dev_dbg (dev,"Invalid ccm offset %u\n" , + ccm->offs[i]); +return -EINVAL ; + } + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {
@@ -514,6 +549,7 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_CCM ] = { .size =sizeof (struct mali_c55_params_ccm ), + .block_validate = mali_c55_ccm_validate, }, };
Signature · 1 line
2.54.0
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hi Jacopo,
thank you for your patch.
Quoted 10 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
Quoted 8 lines
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.Signed-off-by Jacopo Mondi <[email protected]>
I have a couple of comments. With this:
Quoted 13 lines
---
v3:
- new patch--- .../media/platform/arm/mali-c55/mali-c55-params.c | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+)drivers/media/platform/arm/mali-c55/mali-c55-params.c +0
-0@@ -477,6 +477,41 @@ static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_CCM] = &mali_c55_params_ccm,
drivers/media/platform/arm/mali-c55/mali-c55-params.c static const mali_c55_params_handler mali_c55_params_handlers[] = { · line 487
Quoted 10 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +8
-0}; +static int mali_c55_ccm_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_ccm *ccm = + (const struct mali_c55_params_ccm *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { +
Nit: extra blank line after the for opening brace, kernel style usually avoids this.
drivers/media/platform/arm/mali-c55/mali-c55-params.c static const mali_c55_params_handler mali_c55_params_handlers[] = { · line 490
Quoted 3 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +3
-0+for (unsigned int j =0 ; j <3 ; j++) { +/* Coefficients are 13 bits signed Q4.8. */ +if (ccm->coeffs[i][j] >8191 ) {
No magic numbers please :) Maybe use GENMASK(12, 0) or BIT(13) - 1 for readability and consistency with the commit message, and similarly GENMASK(11,
0) or BIT(12) - 1 for gains and offsets. This also avoids magic numbers in validation. The same could be applied to the other cases in this patch.
Quoted 36 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +25
-0+dev_dbg (dev,"Invalid ccm coefficient %u\n" , + ccm->coeffs[i][j]); +return -EINVAL ; + } + } + +/* Gains are 12 bits unsigned Q4.8. */ +if (ccm->gains[i] >4095 ) { +dev_dbg (dev,"Invalid ccm gain %u\n" , + ccm->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (ccm->offs[i] >4095 ) { +dev_dbg (dev,"Invalid ccm offset %u\n" , + ccm->offs[i]); +return -EINVAL ; + } + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {@@ -514,6 +549,7 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_CCM ] = { .size =sizeof (struct mali_c55_params_ccm ), + .block_validate = mali_c55_ccm_validate, }, };
Signature · 2 lines
Regards, Vincenzo
Jacopo Mondi
maintainerRe: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hi Vincenzo
thanks for the review
Quoted 43 lines · Vincenzo Frascino
Hi Jacopo,
thank you for your patch.
Quoted 10 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
Quoted 8 lines
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.Signed-off-by Jacopo Mondi <[email protected]>I have a couple of comments. With this:
Reviewed-by Vincenzo Frascino <[email protected]>Quoted 15 lines
---
v3:
- new patch--- .../media/platform/arm/mali-c55/mali-c55-params.c | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+)drivers/media/platform/arm/mali-c55/mali-c55-params.c +0
-0@@ -477,6 +477,41 @@ static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_CCM] = &mali_c55_params_ccm, };
Quoted 11 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c static const mali_c55_params_handler mali_c55_params_handlers[] = { · line 487
Quoted 8 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +8
-0+static int mali_c55_ccm_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_ccm *ccm = + (const struct mali_c55_params_ccm *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { +Nit: extra blank line after the for opening brace, kernel style usually avoids this.
I know, checkpatch reports this as well, but as I have three validation 'blocks' it felt nicer this way. I can drop the blank line though
Quoted 9 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c static const mali_c55_params_handler mali_c55_params_handlers[] = { · line 490
Quoted 3 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +3
-0+for (unsigned int j =0 ; j <3 ; j++) { +/* Coefficients are 13 bits signed Q4.8. */ +if (ccm->coeffs[i][j] >8191 ) {No magic numbers please :) Maybe use
GENMASK(12, 0)or BIT(13) - 1 for readability and consistency with the commit message, and similarly GENMASK(11,
0) or BIT(12) - 1 for gains and offsets. This also avoids magic numbers in validation. The same could be applied to the other cases in this patch.
Nice suggestion. I used comments to explain the meaning of 8191 and 4095 but indeed using GENMASK() is certainly more explicit.
I'll do so.
Quoted 41 lines
Quoted 29 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +24
-0+dev_dbg (dev,"Invalid ccm coefficient %u\n" , + ccm->coeffs[i][j]); +return -EINVAL ; + } + } + +/* Gains are 12 bits unsigned Q4.8. */ +if (ccm->gains[i] >4095 ) { +dev_dbg (dev,"Invalid ccm gain %u\n" , + ccm->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (ccm->offs[i] >4095 ) { +dev_dbg (dev,"Invalid ccm offset %u\n" , + ccm->offs[i]); +return -EINVAL ; + } + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {drivers/media/platform/arm/mali-c55/mali-c55-params.c mali_c55_params_block_types_info[] = { · line 555
Quoted 7 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +1
-0@@ -514,6 +549,7 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_CCM ] = { .size =sizeof (struct mali_c55_params_ccm ), + .block_validate = mali_c55_ccm_validate, }, };--
Regards,
Vincenzo
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
In reply to Jacopo MondiHi Jacopo,
Thank you for the patch.
Quoted 7 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
Quoted 8 lines
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.
I don't think this is needed.
We need to validate parameters that can cause the ISP to malfunction in ways that requires a system reset, or in ways that cause malfunction of other system components (e.g. buffer overflows, memory bus lock ups, ...). The rest doesn't need to be validated.
If you want to be cautious, you can just mask the value when writing to registers, which I think you're doing in patch 1/4.
Quoted 63 lines
Signed-off-by Jacopo Mondi <[email protected]>---
v3:
- new patch--- .../media/platform/arm/mali-c55/mali-c55-params.c | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+)drivers/media/platform/arm/mali-c55/mali-c55-params.c +36
-0@@ -477,6 +477,41 @@ static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_CCM] = &mali_c55_params_ccm, }; +static int mali_c55_ccm_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_ccm *ccm = + (const struct mali_c55_params_ccm *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { + +for (unsigned int j =0 ; j <3 ; j++) { +/* Coefficients are 13 bits signed Q4.8. */ +if (ccm->coeffs[i][j] >8191 ) { +dev_dbg (dev,"Invalid ccm coefficient %u\n" , + ccm->coeffs[i][j]); +return -EINVAL ; + } + } + +/* Gains are 12 bits unsigned Q4.8. */ +if (ccm->gains[i] >4095 ) { +dev_dbg (dev,"Invalid ccm gain %u\n" , + ccm->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (ccm->offs[i] >4095 ) { +dev_dbg (dev,"Invalid ccm offset %u\n" , + ccm->offs[i]); +return -EINVAL ; + } + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {@@ -514,6 +549,7 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_CCM ] = { .size =sizeof (struct mali_c55_params_ccm ), + .block_validate = mali_c55_ccm_validate, }, };
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hello Laurent,
Quoted 22 lines · Laurent Pinchart
Hi Jacopo,
Thank you for the patch.
Quoted 7 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
Quoted 8 lines
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.I don't think this is needed.
Quoted 7 lines
We need to validate parameters that can cause the ISP to malfunction in ways that requires a system reset, or in ways that cause malfunction of other system components (e.g. buffer overflows, memory bus lock ups, ...). The rest doesn't need to be validated.
If you want to be cautious, you can just mask the value when writing to registers, which I think you're doing in patch 1/4.
According to me here is not a matter of being cautious, but of honouring the contract with the userspace.
If the userspace is doing something wrong it should be notified. The only reasonable argument against this would be if this code is on a critical path and the validations have a performance impact.
@Jacopo, can you please confirm if this is the case?
Signature · 2 lines
Regards, Vincenzo
Jacopo Mondi
maintainerRe: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hi Vincenzo
Quoted 33 lines · Vincenzo Frascino
Hello Laurent,
Quoted 22 lines · Laurent Pinchart
Hi Jacopo,
Thank you for the patch.
Quoted 7 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
Quoted 8 lines
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.I don't think this is needed.
Quoted 7 lines
We need to validate parameters that can cause the ISP to malfunction in ways that requires a system reset, or in ways that cause malfunction of other system components (e.g. buffer overflows, memory bus lock ups, ...). The rest doesn't need to be validated.
If you want to be cautious, you can just mask the value when writing to registers, which I think you're doing in patch 1/4.
Quoted 8 lines
According to me here is not a matter of being cautious, but of honouring the contract with the userspace.
If the userspace is doing something wrong it should be notified. The only reasonable argument against this would be if this code is on a critical path and the validations have a performance impact.
@Jacopo, can you please confirm if this is the case?
validation happens in the qbuf ioctl handler call path (and in the prepare_buf handler too).
I don't think it's strictly an hot path but we're in ioctl context and I think it makes sense to minimize the time it takes to complete the ioctl call.
Thing is, if we go down the path of validating everything, then why would you validate Gamma LUT tables of 129 entries but not LSC tables of 3k values ?
I feel it's hard and quite subjective to draw a line on when it's too costly to perform validation or not, and I think the severity of the potential issue caused by a wrong parameter is a more suitable metric to decide what to validate ?
Quoted 6 lines
--
Regards,
Vincenzo
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hi Jacopo,
Thanks for the clarification.
Quoted 3 lines · Jacopo Mondi
I don't think it's strictly an hot path but we're in ioctl context and I think it makes sense to minimize the time it takes to complete the ioctl call.
I agree with what you are saying on the validation of the paramenters, I am fine with dropping the checks.
Signature · 2 lines
Regards, Vincenzo
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
In reply to Vincenzo FrascinoQuoted 27 lines · Vincenzo Frascino
Quoted 18 lines · Laurent Pinchart
Quoted 7 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
Quoted 8 lines
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.I don't think this is needed.
Quoted 7 lines
We need to validate parameters that can cause the ISP to malfunction in ways that requires a system reset, or in ways that cause malfunction of other system components (e.g. buffer overflows, memory bus lock ups, ...). The rest doesn't need to be validated.
If you want to be cautious, you can just mask the value when writing to registers, which I think you're doing in patch 1/4.
Quoted 6 lines
According to me here is not a matter of being cautious, but of honouring the contract with the userspace.
If the userspace is doing something wrong it should be notified. The only reasonable argument against this would be if this code is on a critical path and the validations have a performance impact.
I don't agree with this. As long as it doesn't have an impact on other parts of the system, there's no need to notify userspace. It's purely a userspace issue, it's pointless to waste CPU cycles every frame.
Quoted 1 line
@Jacopo, can you please confirm if this is the case?
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hi Laurent,
Thank you for your answer.
Quoted 31 lines · Laurent Pinchart
Quoted 30 lines · Vincenzo Frascino
Quoted 18 lines · Laurent Pinchart
Quoted 7 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
Quoted 8 lines
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.I don't think this is needed.
Quoted 7 lines
We need to validate parameters that can cause the ISP to malfunction in ways that requires a system reset, or in ways that cause malfunction of other system components (e.g. buffer overflows, memory bus lock ups, ...). The rest doesn't need to be validated.
If you want to be cautious, you can just mask the value when writing to registers, which I think you're doing in patch 1/4.
According to me here is not a matter of being cautious, but of honouring the contract with the userspace.
Quoted 7 lines
Quoted 3 lines
If the userspace is doing something wrong it should be notified. The only reasonable argument against this would be if this code is on a critical path and the validations have a performance impact.
I don't agree with this. As long as it doesn't have an impact on other parts of the system, there's no need to notify userspace. It's purely a userspace issue, it's pointless to waste CPU cycles every frame.
I don't think it's only about protecting the hardware from invalid values.
The userspace API defines the valid range for these parameters. If userspace provides values outside that range, returning an error makes the issue visible immediately instead of silently changing the requested configuration by masking or clamping the values. From userspace's perspective, silently accepting invalid input can make debugging harder, as the configuration that gets applied is no longer the one that was requested.
This also matches the documented behaviour of the V4L2 extended controls ioctl, which specifies that EINVAL should be returned when the value of a control is invalid:
https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/vidioc-g-ext-ctrls.html
The validation also isn't on the critical per-frame path. It's only performed when userspace updates the parameters, so the CPU cost is negligible.
That said, if the expectation for the V4L2 ISP API is that drivers should only validate values that could affect system stability or security, and silently mask everything else, I'm happy to follow that approach for consistency.
Quoted 3 lines
Quoted 2 lines
@Jacopo, can you please confirm if this is the case?
Signature · 2 lines
Regards, Vincenzo
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Quoted 40 lines · Vincenzo Frascino
Quoted 31 lines · Laurent Pinchart
Quoted 30 lines · Vincenzo Frascino
Quoted 18 lines · Laurent Pinchart
Quoted 7 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of CCM block parameters.
CCM coefficients are expressed as 13 bits signed Q4.8 format and their raw value cannot be higher than 8191 (BIT(13) - 1).
Quoted 8 lines
CCM gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
CCM offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.I don't think this is needed.
Quoted 7 lines
We need to validate parameters that can cause the ISP to malfunction in ways that requires a system reset, or in ways that cause malfunction of other system components (e.g. buffer overflows, memory bus lock ups, ...). The rest doesn't need to be validated.
If you want to be cautious, you can just mask the value when writing to registers, which I think you're doing in patch 1/4.
According to me here is not a matter of being cautious, but of honouring the contract with the userspace.
Quoted 7 lines
Quoted 3 lines
If the userspace is doing something wrong it should be notified. The only reasonable argument against this would be if this code is on a critical path and the validations have a performance impact.
I don't agree with this. As long as it doesn't have an impact on other parts of the system, there's no need to notify userspace. It's purely a userspace issue, it's pointless to waste CPU cycles every frame.
Quoted 8 lines
I don't think it's only about protecting the hardware from invalid values.
The userspace API defines the valid range for these parameters. If userspace provides values outside that range, returning an error makes the issue visible immediately instead of silently changing the requested configuration by masking or clamping the values. From userspace's perspective, silently accepting invalid input can make debugging harder, as the configuration that gets applied is no longer the one that was requested.
That's a valid concern, but I would handle it with a validation layer in userspace if needed for debugging. That will allow userspace implementations to select the amount of validation they deem fit for their use cases.
Quoted 11 lines
This also matches the documented behaviour of the V4L2 extended controls ioctl, which specifies that EINVAL should be returned when the value of a control is invalid:
https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/vidioc-g-ext-ctrls.htmlThe validation also isn't on the critical per-frame path. It's only performed when userspace updates the parameters, so the CPU cost is negligible.
That said, if the expectation for the V4L2 ISP API is that drivers should only validate values that could affect system stability or security, and silently mask everything else, I'm happy to follow that approach for consistency.
That's my expectation at least :-) That's what we aimed for when designing the API. The expectation may not have always been voiced clearly though.
Quoted 1 line
Quoted 1 line
Quoted 1 line
@Jacopo, can you please confirm if this is the case?
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v3 2/4] media: mali-c55: Implement CCM block validation
Hi Laurant,
Quoted 3 lines · Laurent Pinchart
That's my expectation at least 🙂 That's what we aimed for when designing the API. The expectation may not have always been voiced clearly though.
Makes sense. Do not worry, now I know :)
Signature · 2 lines
Regards, Vincenzo
Jacopo Mondi
maintainerPATCH v3 3/4 media: mali-c55: Add support for RGB Gamma
In reply to Jacopo MondiFrom: Jacopo Mondi <[email protected]>
Add support for Gamma curve correction for the Mali C55 ISP.
Define a new block in the uAPI using the extensible v4l2-isp format and implement support for configuring the RGB Gamma parameters in the mali-c55 parameters handler.
While at it, rename the MALI_C55_REG_GAMMA_GAINS_[1|2] register name to MALI_C55_REG_GAMMA_GAINS_[RG|B] and the MALI_C55_REG_GAMMA_OFFSETS_[1|2] register name to MALI_C55_REG_GAMMA_OFFSETS_[RG|B] to better clarify their intent.
---
v3:
- Rename register as suggested by Linus
- Remove an additional tab and fix gamma maximum value in uapi as suggested by Vincenzo
v2:
- Remove unused 'rgb_enable' member
- Address checkpatch issues
--- .../media/platform/arm/mali-c55/mali-c55-params.c | 75 ++++++++++++++++++++++ .../platform/arm/mali-c55/mali-c55-registers.h | 13 ++-- include/uapi/linux/media/arm/mali-c55-config.h | 45 ++++++++++++- 3 files changed, 128 insertions(+), 5 deletions(-)
drivers/media/platform/arm/mali-c55/mali-c55-params.c +75 -0
@@ -47,6 +47,8 @@* @shading_config: For header->type == MALI_C55_PARAM_MESH_SHADING_CONFIG * @shading_selection: For header->type == MALI_C55_PARAM_MESH_SHADING_SELECTION * @ccm: For header->type == MALI_C55_PARAM_BLOCK_CCM +* @gamma: For header->type == MALI_C55_PARAM_BLOCK_GAMMA_FR and +* header->type = MALI_C55_PARAM_BLOCK_GAMMA_DS * @data: Allows easy initialisation of a union variable with a * pointer into a __u8 array. */
@@ -61,6 +63,7 @@ union mali_c55_params_block {const struct mali_c55_params_mesh_shading_config *shading_config;const struct mali_c55_params_mesh_shading_selection *shading_selection;const struct mali_c55_params_ccm *ccm; +const struct mali_c55_params_gamma *gamma;const __u8 *data; };
@@ -462,6 +465,70 @@ static void mali_c55_params_ccm(struct mali_c55 *mali_c55,mali_c55_ctx_write (mali_c55,MALI_C55_REG_CCM_ENABLE ,1 ); } +static void mali_c55_params_gamma (struct mali_c55 *mali_c55, +union mali_c55_params_block block, +__u32 offset,__u32 lut_base) +{ +const struct mali_c55_params_gamma *params = block.gamma; + +if (block.header->flags &V4L2_ISP_PARAMS_FL_BLOCK_DISABLE ) { +mali_c55_ctx_update_bits (mali_c55, +MALI_C55_REG_GAMMA_RGB_ENABLE + offset, +MALI_C55_GAMMA_ENABLE_MASK ,0x00 ); +return ; + } + +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_GAMMA_GAINS_RG + offset, +MALI_C55_GAMMA_GAIN_R_MASK , params->gains[0 ]); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_GAMMA_GAINS_RG + offset, +MALI_C55_GAMMA_GAIN_G_MASK , +MALI_C55_GAMMA_GAIN_G (params->gains[1 ])); +mali_c55_ctx_update_bits (mali_c55,MALI_C55_REG_GAMMA_GAINS_B + offset, +MALI_C55_GAMMA_GAIN_B_MASK , params->gains[2 ]); +mali_c55_ctx_update_bits (mali_c55, +MALI_C55_REG_GAMMA_OFFSETS_RG + offset, +MALI_C55_GAMMA_OFFSET_R_MASK , + params->offs[0 ]); +mali_c55_ctx_update_bits (mali_c55, +MALI_C55_REG_GAMMA_OFFSETS_RG + offset, +MALI_C55_GAMMA_OFFSET_G_MASK , +MALI_C55_GAMMA_OFFSET_G (params->offs[1 ])); +mali_c55_ctx_update_bits (mali_c55, +MALI_C55_REG_GAMMA_OFFSETS_B + offset, +MALI_C55_GAMMA_OFFSET_B_MASK , + params->offs[2 ]); + +for (unsigned int i =0 ; i <MALI_C55_NUM_GAMMA_LUT_ELEMENTS ; i++) { +__u32 addr = lut_base + (i *4 ); + +mali_c55_ctx_write (mali_c55, addr, params->lut[i]); + } + +mali_c55_ctx_update_bits (mali_c55, +MALI_C55_REG_GAMMA_RGB_ENABLE + offset, +MALI_C55_GAMMA_ENABLE_MASK ,0x1 ); +} + +static void mali_c55_params_gamma_fr (struct mali_c55 *mali_c55, +union mali_c55_params_block block) +{ +return mali_c55_params_gamma (mali_c55, block, +MALI_C55_CAP_DEV_FR_REG_OFFSET , +MALI_C55_REG_FR_GAMMA_RGB_MEM ); +} + +static void mali_c55_params_gamma_ds (struct mali_c55 *mali_c55, +union mali_c55_params_block block) +{ +/* We cannot apply parameters to DS if it is not fitted. */ +if (!(mali_c55->capabilities &MALI_C55_GPS_DS_PIPE_FITTED )) +return ; + +return mali_c55_params_gamma (mali_c55, block, +MALI_C55_CAP_DEV_DS_REG_OFFSET , +MALI_C55_REG_DS_GAMMA_RGB_MEM ); +} +static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = &mali_c55_params_sensor_offs, [MALI_C55_PARAM_BLOCK_AEXP_HIST ] = &mali_c55_params_aexp_hist,
@@ -475,6 +542,8 @@ static const mali_c55_params_handler mali_c55_params_handlers[] = { [MALI_C55_PARAM_MESH_SHADING_CONFIG] = &mali_c55_params_lsc_config, [MALI_C55_PARAM_MESH_SHADING_SELECTION] = &mali_c55_params_lsc_selection, [MALI_C55_PARAM_BLOCK_CCM] = &mali_c55_params_ccm, + [MALI_C55_PARAM_BLOCK_GAMMA_FR] = &mali_c55_params_gamma_fr, + [MALI_C55_PARAM_BLOCK_GAMMA_DS] = &mali_c55_params_gamma_ds, }; static int mali_c55_ccm_validate(struct device *dev,
@@ -551,6 +620,12 @@ mali_c55_params_block_types_info[] = { .size = sizeof(struct mali_c55_params_ccm), .block_validate = mali_c55_ccm_validate, }, + [MALI_C55_PARAM_BLOCK_GAMMA_FR] = { + .size = sizeof(struct mali_c55_params_gamma), + }, + [MALI_C55_PARAM_BLOCK_GAMMA_DS] = { + .size = sizeof(struct mali_c55_params_gamma), + }, }; static_assert(ARRAY_SIZE(mali_c55_params_handlers) ==
drivers/media/platform/arm/mali-c55/mali-c55-registers.h +9 -4
@@ -422,15 +422,17 @@ enum mali_c55_interrupts {#define MALI_C55_REG_GAMMA_RGB_ENABLE 0x1c064#define MALI_C55_GAMMA_ENABLE_MASK BIT(0) -#define MALI_C55_REG_GAMMA_GAINS_1 0x1c068 +#define MALI_C55_REG_GAMMA_GAINS_RG 0x1c068#define MALI_C55_GAMMA_GAIN_R_MASK GENMASK(11, 0)#define MALI_C55_GAMMA_GAIN_G_MASK GENMASK(27, 16) -#define MALI_C55_REG_GAMMA_GAINS_2 0x1c06c +#define MALI_C55_GAMMA_GAIN_G (x) ((x) << 16) +#define MALI_C55_REG_GAMMA_GAINS_B 0x1c06c#define MALI_C55_GAMMA_GAIN_B_MASK GENMASK(11, 0) -#define MALI_C55_REG_GAMMA_OFFSETS_1 0x1c070 +#define MALI_C55_REG_GAMMA_OFFSETS_RG 0x1c070#define MALI_C55_GAMMA_OFFSET_R_MASK GENMASK(11, 0)#define MALI_C55_GAMMA_OFFSET_G_MASK GENMASK(27, 16) -#define MALI_C55_REG_GAMMA_OFFSETS_2 0x1c074 +#define MALI_C55_GAMMA_OFFSET_G (x) ((x) << 16) +#define MALI_C55_REG_GAMMA_OFFSETS_B 0x1c074#define MALI_C55_GAMMA_OFFSET_B_MASK GENMASK(11, 0)/*
@@ -441,6 +443,9 @@ enum mali_c55_interrupts {#define MALI_C55_REG_FR_GAMMA_RGB_ENABLE 0x1c064#define MALI_C55_REG_DS_GAMMA_RGB_ENABLE 0x1c1d8 +#define MALI_C55_REG_FR_GAMMA_RGB_MEM 0x18280 +#define MALI_C55_REG_DS_GAMMA_RGB_MEM 0x18484 +#define MALI_C55_REG_FR_SCALER_HFILT 0x34a8#define MALI_C55_REG_FR_SCALER_VFILT 0x44a8#define MALI_C55_REG_DS_SCALER_HFILT 0x14a8
include/uapi/linux/media/arm/mali-c55-config.h +44 -1
74 lines not on this page · raw message
Signature · 1 line
2.54.0
Jacopo Mondi
maintainerPATCH v3 4/4 media: mali-c55: Implement Gamma block validation
In reply to Jacopo MondiFrom: Jacopo Mondi <[email protected]>
Implement validation of Gamma block parameters.
Gamma gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
Gamma offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
The Gamma LUT table is expected to have 0 as first member and 0xfff as last member.
Validate the parameters provided by userspace using the .block_validate callback of struct v4l2_isp_params_block_type_info.
---
v3:
- new patch
--- .../media/platform/arm/mali-c55/mali-c55-params.c | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+)
drivers/media/platform/arm/mali-c55/mali-c55-params.c +34 -0
@@ -581,6 +581,38 @@ static int mali_c55_ccm_validate(struct device *dev,return 0 ; } +static int mali_c55_gamma_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_gamma *gamma = + (const struct mali_c55_params_gamma *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { +/* Gains are 12 bits unsigned Q4.8. */ +if (gamma->gains[i] >4095 ) { +dev_dbg (dev,"Invalid gain value %u\n" , + gamma->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (gamma->offs[i] >4095 ) { +dev_dbg (dev,"Invalid offset value %u\n" , + gamma->offs[i]); +return -EINVAL ; + } + } + +/* Check the first and last gamma lut entries match the expectations. */ +if (gamma->lut[0 ] !=0 || + gamma->lut[MALI_C55_NUM_GAMMA_LUT_ELEMENTS -1 ] !=0xfff ) { +dev_dbg (dev,"Invalid Gamma LUT table\n" ); +return -EINVAL ; + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {
@@ -622,9 +654,11 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_GAMMA_FR ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, [MALI_C55_PARAM_BLOCK_GAMMA_DS ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, };
Signature · 1 line
2.54.0
Re: [PATCH v3 4/4] media: mali-c55: Implement Gamma block validation
Hi Jacopo,
thank you for your patch.
Quoted 10 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of Gamma block parameters.
Gamma gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
Gamma offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Quoted 8 lines
The Gamma LUT table is expected to have 0 as first member and
0xfffas last member.Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.Signed-off-by Jacopo Mondi <[email protected]>
Looks good overall. I have just one comment. With this:
Quoted 30 lines
---
v3:
- new patch--- .../media/platform/arm/mali-c55/mali-c55-params.c | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+)drivers/media/platform/arm/mali-c55/mali-c55-params.c +15
-0@@ -581,6 +581,38 @@ static int mali_c55_ccm_validate(struct device *dev,return 0 ; } +static int mali_c55_gamma_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_gamma *gamma = + (const struct mali_c55_params_gamma *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { +/* Gains are 12 bits unsigned Q4.8. */ +if (gamma->gains[i] >4095 ) { +dev_dbg (dev,"Invalid gain value %u\n" , + gamma->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */
drivers/media/platform/arm/mali-c55/mali-c55-params.c static int mali_c55_ccm_validate(struct device *dev, · line 608
Quoted 10 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +10
-0+if (gamma->offs[i] >4095 ) { +dev_dbg (dev,"Invalid offset value %u\n" , + gamma->offs[i]); +return -EINVAL ; + } + } + +/* Check the first and last gamma lut entries match the expectations. */ +if (gamma->lut[0 ] !=0 || + gamma->lut[MALI_C55_NUM_GAMMA_LUT_ELEMENTS -1 ] !=0xfff ) {
I am still learning about these things and might be wrong. Should not we also validate that intermediate LUT entries are monotonic and non-decreasing?
Quoted 23 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +9
-0+dev_dbg (dev,"Invalid Gamma LUT table\n" ); +return -EINVAL ; + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {@@ -622,9 +654,11 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_GAMMA_FR ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, [MALI_C55_PARAM_BLOCK_GAMMA_DS ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, };
Signature · 2 lines
Regards, Vincenzo
Jacopo Mondi
maintainerRe: [PATCH v3 4/4] media: mali-c55: Implement Gamma block validation
Hi Vincenzo
Quoted 63 lines · Vincenzo Frascino
Hi Jacopo,
thank you for your patch.
Quoted 10 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of Gamma block parameters.
Gamma gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
Gamma offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Quoted 8 lines
The Gamma LUT table is expected to have 0 as first member and
0xfffas last member.Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.Signed-off-by Jacopo Mondi <[email protected]>Looks good overall. I have just one comment. With this:
Reviewed-by Vincenzo Frascino <[email protected]>Quoted 35 lines
---
v3:
- new patch--- .../media/platform/arm/mali-c55/mali-c55-params.c | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+)drivers/media/platform/arm/mali-c55/mali-c55-params.c +20
-0@@ -581,6 +581,38 @@ static int mali_c55_ccm_validate(struct device *dev,return 0 ; } +static int mali_c55_gamma_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_gamma *gamma = + (const struct mali_c55_params_gamma *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { +/* Gains are 12 bits unsigned Q4.8. */ +if (gamma->gains[i] >4095 ) { +dev_dbg (dev,"Invalid gain value %u\n" , + gamma->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (gamma->offs[i] >4095 ) { +dev_dbg (dev,"Invalid offset value %u\n" , + gamma->offs[i]); +return -EINVAL ; + }
Quoted 9 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c static int mali_c55_ccm_validate(struct device *dev, · line 608
Quoted 5 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +5
-0+ } + +/* Check the first and last gamma lut entries match the expectations. */ +if (gamma->lut[0 ] !=0 || + gamma->lut[MALI_C55_NUM_GAMMA_LUT_ELEMENTS -1 ] !=0xfff ) {I am still learning about these things and might be wrong. Should not we also validate that intermediate LUT entries are monotonic and non-decreasing?
Eh, good question.
This all new for everyone, as this would be the first user of 'block_validate()' in tree.
Here I checked only the first and last member because it seems like a good compromise between performances and correctness. This function might potentially run for every frame (where a CCM table is specified by userspace), so I'm a bit unsure how far we should go with validation.
I kept these two patches broken out from the ones that introduce the uapi specifically to get feedback on this. Maybe I'm overconcerned about the additional cost of validation ?
Quoted 28 lines
Quoted 13 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +7
-0+dev_dbg (dev,"Invalid Gamma LUT table\n" ); +return -EINVAL ; + } + +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {@@ -622,9 +654,11 @@ mali_c55_params_block_types_info[] = { },drivers/media/platform/arm/mali-c55/mali-c55-params.c mali_c55_params_block_types_info[] = { · line 664
Quoted 10 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +2
-0[MALI_C55_PARAM_BLOCK_GAMMA_FR ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, [MALI_C55_PARAM_BLOCK_GAMMA_DS ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, };--
Regards,
Vincenzo
Re: [PATCH v3 4/4] media: mali-c55: Implement Gamma block validation
Hi Jacopo,
Quoted 17 lines · Jacopo Mondi
Quoted 6 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c static int mali_c55_ccm_validate(struct device *dev, · line 608
Quoted 3 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +3
-0+/* Check the first and last gamma lut entries match the expectations. */ +if (gamma->lut[0 ] !=0 || + gamma->lut[MALI_C55_NUM_GAMMA_LUT_ELEMENTS -1 ] !=0xfff ) {I am still learning about these things and might be wrong. Should not we also validate that intermediate LUT entries are monotonic and non-decreasing?
Eh, good question.
This all new for everyone, as this would be the first user of '
block_validate()' in tree.Here I checked only the first and last member because it seems like a good compromise between performances and correctness. This function might potentially run for every frame (where a CCM table is specified by userspace), so I'm a bit unsure how far we should go with validation.
Quoted 3 lines
I kept these two patches broken out from the ones that introduce the uapi specifically to get feedback on this. Maybe I'm overconcerned about the additional cost of validation ?
I am not sure either. As a strategy I always prefer to be on the safe side hence my comment. Is there any way we could come up with a comparative analysis and check what's the impact of these additional costs? This should answer my question I guess.
Signature · 2 lines
Regards, Vincenzo
Re: [PATCH v3 4/4] media: mali-c55: Implement Gamma block validation
In reply to Jacopo MondiQuoted 10 lines · Jacopo Mondi
From: Jacopo Mondi <[email protected]>
Implement validation of Gamma block parameters.
Gamma gains are expressed as unsigned 12 bits Q4.8 format and their raw value cannot be higher than 4095 (BIT(12) - 1).
Gamma offsets are 12 bits unsigned integers and their value cannot be higher than 4095 (BIT(12) - 1).
Quoted 7 lines
The Gamma LUT table is expected to have 0 as first member and
0xfffas last member.Validate the parameters provided by userspace using the .block_validate callback of struct
v4l2_isp_params_block_type_info.Signed-off-by Jacopo Mondi <[email protected]>
For the same reasons as 3/4, I'd drop this patch.
Quoted 33 lines
---
v3:
- new patch--- .../media/platform/arm/mali-c55/mali-c55-params.c | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+)drivers/media/platform/arm/mali-c55/mali-c55-params.c +18
-0@@ -581,6 +581,38 @@ static int mali_c55_ccm_validate(struct device *dev,return 0 ; } +static int mali_c55_gamma_validate (struct device *dev, +const struct v4l2_isp_block_header *block) +{ +const struct mali_c55_params_gamma *gamma = + (const struct mali_c55_params_gamma *)(block); + +for (unsigned int i =0 ; i <3 ; i++) { +/* Gains are 12 bits unsigned Q4.8. */ +if (gamma->gains[i] >4095 ) { +dev_dbg (dev,"Invalid gain value %u\n" , + gamma->gains[i]); +return -EINVAL ; + } + +/* Offsets are 12 bits unsigned integers. */ +if (gamma->offs[i] >4095 ) { +dev_dbg (dev,"Invalid offset value %u\n" , + gamma->offs[i]);
drivers/media/platform/arm/mali-c55/mali-c55-params.c static int mali_c55_ccm_validate(struct device *dev, · line 611
Quoted 10 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +10
-0+return -EINVAL ; + } + } + +/* Check the first and last gamma lut entries match the expectations. */ +if (gamma->lut[0 ] !=0 || + gamma->lut[MALI_C55_NUM_GAMMA_LUT_ELEMENTS -1 ] !=0xfff ) { +dev_dbg (dev,"Invalid Gamma LUT table\n" ); +return -EINVAL ; + }
You can possibly hardcode the first and last values to 0 and 0xfff respectively in mali_c55_params_gamma() if you want to guard against invalid values being programmed in registers.
Quoted 19 lines
drivers/media/platform/arm/mali-c55/mali-c55-params.c +6
-0+ +return 0 ; +} +static const struct v4l2_isp_params_block_type_info mali_c55_params_block_types_info[] = { [MALI_C55_PARAM_BLOCK_SENSOR_OFFS ] = {@@ -622,9 +654,11 @@ mali_c55_params_block_types_info[] = { }, [MALI_C55_PARAM_BLOCK_GAMMA_FR ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, [MALI_C55_PARAM_BLOCK_GAMMA_DS ] = { .size =sizeof (struct mali_c55_params_gamma ), + .block_validate = mali_c55_gamma_validate, }, };
Signature · 3 lines
Regards, Laurent Pinchart