media: staging: rkisp1: change workqueue to threaded irq in stats
Subsystems (3)
cover media: staging: rkisp1: change workqueue to threaded irq in stats
Reading the statistics registers might take too long
to run inside the irq handler. Currently it is deferred to bottom half using workqueues. This patch replaces the workqueue with threaded interrupts.
This fixes an item in the TODO file to move the driver from staging.
Patches Summary:
1. Since the irq is shared, the isr should return either IRQ_NONE or IRQ_HANDELD. This patch fixes it. In later patch, IRQ_WAKE_THREAD will be added.
2. Replace a long bitwise-or of the statistics flags with a macro to improve readability
3. Fixes a bug of using spin_lock instead of spin_lock_irqsave inside an irq handler
3. Replace two locks in the rkisp1_stats object with one lock that protects the 'is_streaming' variable and the 'stat' list of buffers.
4. Replaces the workqueue with threaded irq in the statistics.
The code is tested using the 'cam' command from libcamera:
cam -c 1 -C -s width=1280,height=960 --file="/tmp/libcamframe#.data"
Dafna Hirschfeld (4):
media: staging: rkisp1: use a macro for the statistics flags mask
media: staging: rkisp1: stats: use spin_lock_irqsave for irq_lock
media: staging: rkisp1: stats: replace locks wq_lock, irq_lock with
one lock
media: staging: rkisp1: replace workqueue with threaded irq for
reading statistics registers
Helen Koike (1):
media: staging: rkisp1: return IRQ_NONE in isr when irq isn't for ISP
drivers/staging/media/rkisp1/TODO | 1 - drivers/staging/media/rkisp1/rkisp1-capture.c | 7 +- drivers/staging/media/rkisp1/rkisp1-common.h | 20 +- drivers/staging/media/rkisp1/rkisp1-dev.c | 22 +- drivers/staging/media/rkisp1/rkisp1-isp.c | 20 +- drivers/staging/media/rkisp1/rkisp1-stats.c | 197 +++++++----------- 6 files changed, 121 insertions(+), 146 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/5 media: staging: rkisp1: return IRQ_NONE in isr when irq isn't for ISP
From: Helen Koike <[email protected]>
rkisp1 shares the interrupt line, then it shouldn't always return IRQ_HANDLED, otherwise it can flag as handled an interrupt that wans't meant for ISP.
return IRQ_NONE when the interrupt wans't meant for ISP
--- drivers/staging/media/rkisp1/rkisp1-capture.c | 7 ++++++- drivers/staging/media/rkisp1/rkisp1-common.h | 6 +++--- drivers/staging/media/rkisp1/rkisp1-dev.c | 14 ++++++++++---- drivers/staging/media/rkisp1/rkisp1-isp.c | 12 ++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-)
drivers/staging/media/rkisp1/rkisp1-capture.c +6 -1
@@ -649,12 +649,15 @@ static void rkisp1_handle_buffer(struct rkisp1_capture *cap)rkisp1_set_next_buf (cap); } -void rkisp1_capture_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1) {unsigned int i;u32 status; status =rkisp1_read (rkisp1,RKISP1_CIF_MI_MIS ); +if (!status) +return IRQ_NONE ; +rkisp1_write (rkisp1, status,RKISP1_CIF_MI_ICR );for (i =0 ; i <ARRAY_SIZE (rkisp1->capture_devs); ++i) {
@@ -682,6 +685,8 @@ void rkisp1_capture_isr(struct rkisp1_device *rkisp1) cap->is_streaming = false;wake_up (&cap->done); } + +return IRQ_HANDLED ; }/* ----------------------------------------------------------------------------
drivers/staging/media/rkisp1/rkisp1-common.h +3 -3
@@ -305,9 +305,9 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); -void rkisp1_isp_isr (struct rkisp1_device *rkisp1); -void rkisp1_mipi_isr (struct rkisp1_device *rkisp1); -void rkisp1_capture_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1);void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);
drivers/staging/media/rkisp1/rkisp1-dev.c +10 -4
@@ -387,10 +387,13 @@ static int rkisp1_entities_register(struct rkisp1_device *rkisp1)return ret; } -static irqreturn_t rkisp1_isr (int irq,void *ctx) +irqreturn_t rkisp1_isr (int irq,void *ctx) {struct device *dev = ctx;struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +irqreturn_t isp_ret; +irqreturn_t cap_ret; +irqreturn_t mipi_ret;/* * Call rkisp1_capture_isr() first to handle the frame that
@@ -398,9 +401,12 @@ static irqreturn_t rkisp1_isr(int irq, void *ctx)* it is potentially incremented by rkisp1_isp_isr() in the vertical * sync. */ -rkisp1_capture_isr (rkisp1); -rkisp1_isp_isr (rkisp1); -rkisp1_mipi_isr (rkisp1); + cap_ret =rkisp1_capture_isr (rkisp1); + isp_ret =rkisp1_isp_isr (rkisp1); + mipi_ret =rkisp1_mipi_isr (rkisp1); + +if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE ) +return IRQ_NONE ;return IRQ_HANDLED ; }
drivers/staging/media/rkisp1/rkisp1-isp.c +8 -4
@@ -1046,13 +1046,13 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)* Interrupt handlers */ -void rkisp1_mipi_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1) {u32 val, status; status =rkisp1_read (rkisp1,RKISP1_CIF_MIPI_MIS );if (!status) -return ; +return IRQ_NONE ;rkisp1_write (rkisp1, status,RKISP1_CIF_MIPI_ICR );
@@ -1087,6 +1087,8 @@ void rkisp1_mipi_isr(struct rkisp1_device *rkisp1) }else { rkisp1->debug.mipi_error++; } + +return IRQ_HANDLED ; }static void rkisp1_isp_queue_event_sof (struct rkisp1_isp *isp)
@@ -1106,13 +1108,13 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)v4l2_event_queue (isp->sd.devnode, &event); } -void rkisp1_isp_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status) -return ; +return IRQ_NONE ;rkisp1_write (rkisp1, status,RKISP1_CIF_ISP_ICR );
@@ -1148,4 +1150,6 @@ void rkisp1_isp_isr(struct rkisp1_device *rkisp1)* Do the updates in the order of the processing flow. */ rkisp1_params_isr (rkisp1, status); + +return IRQ_HANDLED ; }
Signature · 1 line
2.17.1
kbuild test robot
build botRe: [PATCH 1/5] media: staging: rkisp1: return IRQ_NONE in isr when irq isn't for ISP
I love your patch! Yet something to improve:
Hi Dafna,
I love your patch! Yet something to improve:
[auto build test ERROR on linuxtv-media/master]
[also build test ERROR on v5.7-rc5 next-20200512]
[cannot apply to staging/staging-testing]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Dafna-Hirschfeld/media-staging-rkisp1-change-workqueue-to-threaded-irq-in-stats/20200512-200942 base: git://linuxtv.org/media_tree.git master config: nios2-allyesconfig (attached as .config) compiler: nios2-linux-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=nios2
If you fix the issue, kindly add following tag as appropriate
All error/warnings (new ones prefixed by >>):
In file included from drivers/staging/media/rkisp1/rkisp1-capture.c:21:
>> drivers/staging/media/rkisp1/rkisp1-common.h:308:1: error: unknown type name 'irqreturn_t'
308 | irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:309:1: error: unknown type name 'irqreturn_t' 309 | irqreturn_t rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:310:1: error: unknown type name 'irqreturn_t' 310 | irqreturn_t rkisp1_capture_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~
>> drivers/staging/media/rkisp1/rkisp1-capture.c:652:1: error: unknown type name 'irqreturn_t' 652 | irqreturn_t rkisp1_capture_isr(struct rkisp1_device *rkisp1) | ^~~~~~~~~~~ drivers/staging/media/rkisp1/rkisp1-capture.c: In function 'rkisp1_capture_isr': >> drivers/staging/media/rkisp1/rkisp1-capture.c:659:10: error: 'IRQ_NONE' undeclared (first use in this function) 659 | return IRQ_NONE; | ^~~~~~~~ drivers/staging/media/rkisp1/rkisp1-capture.c:659:10: note: each undeclared identifier is reported only once for each function it appears in >> drivers/staging/media/rkisp1/rkisp1-capture.c:689:9: error: 'IRQ_HANDLED' undeclared (first use in this function) 689 | return IRQ_HANDLED; | ^~~~~~~~~~~ >> drivers/staging/media/rkisp1/rkisp1-capture.c:690:1: warning: control reaches end of non-void function [-Wreturn-type]
690 | }
| ^
--
In file included from drivers/staging/media/rkisp1/rkisp1-common.c:10:
>> drivers/staging/media/rkisp1/rkisp1-common.h:308:1: error: unknown type name 'irqreturn_t'
308 | irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:309:1: error: unknown type name 'irqreturn_t' 309 | irqreturn_t rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:310:1: error: unknown type name 'irqreturn_t' 310 | irqreturn_t rkisp1_capture_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~
--
In file included from drivers/staging/media/rkisp1/rkisp1-isp.c:19:
>> drivers/staging/media/rkisp1/rkisp1-common.h:308:1: error: unknown type name 'irqreturn_t'
308 | irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:309:1: error: unknown type name 'irqreturn_t' 309 | irqreturn_t rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:310:1: error: unknown type name 'irqreturn_t' 310 | irqreturn_t rkisp1_capture_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~
>> drivers/staging/media/rkisp1/rkisp1-isp.c:1049:1: error: unknown type name 'irqreturn_t' 1049 | irqreturn_t rkisp1_mipi_isr(struct rkisp1_device *rkisp1) | ^~~~~~~~~~~ drivers/staging/media/rkisp1/rkisp1-isp.c: In function 'rkisp1_mipi_isr': >> drivers/staging/media/rkisp1/rkisp1-isp.c:1055:10: error: 'IRQ_NONE' undeclared (first use in this function) 1055 | return IRQ_NONE; | ^~~~~~~~ drivers/staging/media/rkisp1/rkisp1-isp.c:1055:10: note: each undeclared identifier is reported only once for each function it appears in >> drivers/staging/media/rkisp1/rkisp1-isp.c:1091:9: error: 'IRQ_HANDLED' undeclared (first use in this function)
1091 | return IRQ_HANDLED;
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-isp.c: At top level:drivers/staging/media/rkisp1/rkisp1-isp.c:1111:1: error: unknown type name 'irqreturn_t' 1111 | irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-isp.c: In function 'rkisp1_isp_isr':drivers/staging/media/rkisp1/rkisp1-isp.c:1117:10: error: 'IRQ_NONE' undeclared (first use in this function)
1117 | return IRQ_NONE;
| ^~~~~~~~drivers/staging/media/rkisp1/rkisp1-isp.c:1154:9: error: 'IRQ_HANDLED' undeclared (first use in this function) 1154 | return IRQ_HANDLED;
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-isp.c: In function 'rkisp1_mipi_isr':
>> drivers/staging/media/rkisp1/rkisp1-isp.c:1092:1: warning: control reaches end of non-void function [-Wreturn-type]
1092 | }
| ^drivers/staging/media/rkisp1/rkisp1-isp.c: In function 'rkisp1_isp_isr':drivers/staging/media/rkisp1/rkisp1-isp.c:1155:1: warning: control reaches end of non-void function [-Wreturn-type] 1155 | }
| ^
vim +/irqreturn_t +308 drivers/staging/media/rkisp1/rkisp1-common.h
307
> 308 irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1);
309 irqreturn_t rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
310 irqreturn_t rkisp1_capture_isr(struct rkisp1_device *rkisp1);
311 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
312 void rkisp1_params_isr(struct rkisp1_device *rkisp1, u32 isp_mis);
313
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
_______________________________________________
Linux-rockchip mailing list [email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Re: [PATCH 1/5] media: staging: rkisp1: return IRQ_NONE in isr when irq isn't for ISP
In reply to Dafna HirschfeldHi Dafna,
Thanks for the patch.
Quoted 72 lines · Dafna Hirschfeld
From: Helen Koike <[email protected]>
rkisp1 shares the interrupt line, then it shouldn't always return
IRQ_HANDLED, otherwise it can flag as handled an interrupt that wans't meant for ISP.return
IRQ_NONEwhen the interrupt wans't meant for ISPFixes d65dd85281fb ("media: staging: rkisp1: add Rockchip ISP1 base driver")Signed-off-by Helen Koike <[email protected]>Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/rkisp1-capture.c | 7 ++++++- drivers/staging/media/rkisp1/rkisp1-common.h | 6 +++--- drivers/staging/media/rkisp1/rkisp1-dev.c | 14 ++++++++++---- drivers/staging/media/rkisp1/rkisp1-isp.c | 12 ++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-)drivers/staging/media/rkisp1/rkisp1-capture.c +6
-1@@ -649,12 +649,15 @@ static void rkisp1_handle_buffer(struct rkisp1_capture *cap)rkisp1_set_next_buf (cap); } -void rkisp1_capture_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1) {unsigned int i;u32 status; status =rkisp1_read (rkisp1,RKISP1_CIF_MI_MIS ); +if (!status) +return IRQ_NONE ; +rkisp1_write (rkisp1, status,RKISP1_CIF_MI_ICR );for (i =0 ; i <ARRAY_SIZE (rkisp1->capture_devs); ++i) {@@ -682,6 +685,8 @@ void rkisp1_capture_isr(struct rkisp1_device *rkisp1) cap->is_streaming = false;wake_up (&cap->done); } + +return IRQ_HANDLED ; }/* ---------------------------------------------------------------------------- drivers/staging/media/rkisp1/rkisp1-common.h +3
-3@@ -305,9 +305,9 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); -void rkisp1_isp_isr (struct rkisp1_device *rkisp1); -void rkisp1_mipi_isr (struct rkisp1_device *rkisp1); -void rkisp1_capture_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1);void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0@@ -387,10 +387,13 @@ static int rkisp1_entities_register(struct rkisp1_device *rkisp1)return ret;
drivers/staging/media/rkisp1/rkisp1-dev.c static int rkisp1_entities_register(struct rkisp1_device *rkisp1) · line 396
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +4
-1} -static irqreturn_t rkisp1_isr (int irq,void *ctx) +irqreturn_t rkisp1_isr (int irq,void *ctx) {struct device *dev = ctx;struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +irqreturn_t isp_ret; +irqreturn_t cap_ret; +irqreturn_t mipi_ret;
Just cosmetics, you could declare them in a single line
irqreturn_t cap_ret, isp_ret, mipi_ret;
With or without this change:
Thanks
Helen
Quoted 72 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +6
-3/* * Call rkisp1_capture_isr() first to handle the frame that @@ -398,9 +401,12 @@ static irqreturn_t rkisp1_isr(int irq, void *ctx)* it is potentially incremented by rkisp1_isp_isr() in the vertical * sync. */ -rkisp1_capture_isr (rkisp1); -rkisp1_isp_isr (rkisp1); -rkisp1_mipi_isr (rkisp1); + cap_ret =rkisp1_capture_isr (rkisp1); + isp_ret =rkisp1_isp_isr (rkisp1); + mipi_ret =rkisp1_mipi_isr (rkisp1); + +if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE ) +return IRQ_NONE ;return IRQ_HANDLED ; }drivers/staging/media/rkisp1/rkisp1-isp.c +8
-4@@ -1046,13 +1046,13 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)* Interrupt handlers */ -void rkisp1_mipi_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1) {u32 val, status; status =rkisp1_read (rkisp1,RKISP1_CIF_MIPI_MIS );if (!status) -return ; +return IRQ_NONE ;rkisp1_write (rkisp1, status,RKISP1_CIF_MIPI_ICR );@@ -1087,6 +1087,8 @@ void rkisp1_mipi_isr(struct rkisp1_device *rkisp1) }else { rkisp1->debug.mipi_error++; } + +return IRQ_HANDLED ; }static void rkisp1_isp_queue_event_sof (struct rkisp1_isp *isp)@@ -1106,13 +1108,13 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)v4l2_event_queue (isp->sd.devnode, &event); } -void rkisp1_isp_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status) -return ; +return IRQ_NONE ;rkisp1_write (rkisp1, status,RKISP1_CIF_ISP_ICR );@@ -1148,4 +1150,6 @@ void rkisp1_isp_isr(struct rkisp1_device *rkisp1)* Do the updates in the order of the processing flow. */ rkisp1_params_isr (rkisp1, status); + +return IRQ_HANDLED ; }
Laurent Pinchart
Reviewed-byRe: [PATCH 1/5] media: staging: rkisp1: return IRQ_NONE in isr when irq isn't for ISP
Hi Dafna,
Thank you for the patch.
Quoted 93 lines · Helen Koike
Quoted 72 lines · Dafna Hirschfeld
From: Helen Koike <[email protected]>
rkisp1 shares the interrupt line, then it shouldn't always return
IRQ_HANDLED, otherwise it can flag as handled an interrupt that wans't meant for ISP.return
IRQ_NONEwhen the interrupt wans't meant for ISPFixes d65dd85281fb ("media: staging: rkisp1: add Rockchip ISP1 base driver")Signed-off-by Helen Koike <[email protected]>Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/rkisp1-capture.c | 7 ++++++- drivers/staging/media/rkisp1/rkisp1-common.h | 6 +++--- drivers/staging/media/rkisp1/rkisp1-dev.c | 14 ++++++++++---- drivers/staging/media/rkisp1/rkisp1-isp.c | 12 ++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-)drivers/staging/media/rkisp1/rkisp1-capture.c +6
-1@@ -649,12 +649,15 @@ static void rkisp1_handle_buffer(struct rkisp1_capture *cap)rkisp1_set_next_buf (cap); } -void rkisp1_capture_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1) {unsigned int i;u32 status; status =rkisp1_read (rkisp1,RKISP1_CIF_MI_MIS ); +if (!status) +return IRQ_NONE ; +rkisp1_write (rkisp1, status,RKISP1_CIF_MI_ICR );for (i =0 ; i <ARRAY_SIZE (rkisp1->capture_devs); ++i) {@@ -682,6 +685,8 @@ void rkisp1_capture_isr(struct rkisp1_device *rkisp1) cap->is_streaming = false;wake_up (&cap->done); } + +return IRQ_HANDLED ; }/* ---------------------------------------------------------------------------- drivers/staging/media/rkisp1/rkisp1-common.h +3
-3@@ -305,9 +305,9 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); -void rkisp1_isp_isr (struct rkisp1_device *rkisp1); -void rkisp1_mipi_isr (struct rkisp1_device *rkisp1); -void rkisp1_capture_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1); +irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1);void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0@@ -387,10 +387,13 @@ static int rkisp1_entities_register(struct rkisp1_device *rkisp1)return ret;drivers/staging/media/rkisp1/rkisp1-dev.c static int rkisp1_entities_register(struct rkisp1_device *rkisp1) · line 396
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +4
-1} -static irqreturn_t rkisp1_isr (int irq,void *ctx) +irqreturn_t rkisp1_isr (int irq,void *ctx) {struct device *dev = ctx;struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +irqreturn_t isp_ret; +irqreturn_t cap_ret; +irqreturn_t mipi_ret;Just cosmetics, you could declare them in a single line
irqreturn_t cap_ret, isp_ret, mipi_ret;With or without this change:
Acked-by Helen Koike <[email protected]>Quoted 1 line
Quoted 15 lines
Quoted 5 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0/* * Call rkisp1_capture_isr() first to handle the frame that @@ -398,9 +401,12 @@ static irqreturn_t rkisp1_isr(int irq, void *ctx)* it is potentially incremented by rkisp1_isp_isr() in the vertical * sync. drivers/staging/media/rkisp1/rkisp1-dev.c static irqreturn_t rkisp1_isr(int irq, void *ctx) · line 409
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +6
-3*/ -rkisp1_capture_isr (rkisp1); -rkisp1_isp_isr (rkisp1); -rkisp1_mipi_isr (rkisp1); + cap_ret =rkisp1_capture_isr (rkisp1); + isp_ret =rkisp1_isp_isr (rkisp1); + mipi_ret =rkisp1_mipi_isr (rkisp1); + +if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE ) +return IRQ_NONE ;
Another cosmetic change proposal:
irqreturn_t ret = IRQ_NONE; ... if (rkisp1_capture_isr(rkisp1) == IRQ_HANDLED) ret = IRQ_HANDLED; if (rkisp1_isp_isr(rkisp1) == IRQ_HANDLED) ret = IRQ_HANDLED; if (rkisp1_mipi_isr(rkisp1) == IRQ_HANDLED) ret = IRQ_HANDLED; return ret;
With or without it,
Quoted 56 lines
Quoted 56 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0return IRQ_HANDLED ; }drivers/staging/media/rkisp1/rkisp1-isp.c +8
-4@@ -1046,13 +1046,13 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)* Interrupt handlers */ -void rkisp1_mipi_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1) {u32 val, status; status =rkisp1_read (rkisp1,RKISP1_CIF_MIPI_MIS );if (!status) -return ; +return IRQ_NONE ;rkisp1_write (rkisp1, status,RKISP1_CIF_MIPI_ICR );@@ -1087,6 +1087,8 @@ void rkisp1_mipi_isr(struct rkisp1_device *rkisp1) }else { rkisp1->debug.mipi_error++; } + +return IRQ_HANDLED ; }static void rkisp1_isp_queue_event_sof (struct rkisp1_isp *isp)@@ -1106,13 +1108,13 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)v4l2_event_queue (isp->sd.devnode, &event); } -void rkisp1_isp_isr (struct rkisp1_device *rkisp1) +irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status) -return ; +return IRQ_NONE ;rkisp1_write (rkisp1, status,RKISP1_CIF_ISP_ICR );@@ -1148,4 +1150,6 @@ void rkisp1_isp_isr(struct rkisp1_device *rkisp1)* Do the updates in the order of the processing flow. */ rkisp1_params_isr (rkisp1, status); + +return IRQ_HANDLED ; }
Signature · 3 lines
Regards, Laurent Pinchart
PATCH 2/5 media: staging: rkisp1: use a macro for the statistics flags mask
In reply to Dafna HirschfeldThe mask of the ready statistics flags is used in several places in the code using bitwise-or. Use a macro for that to make the code more readable.
--- drivers/staging/media/rkisp1/rkisp1-common.h | 4 ++++ drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +---- drivers/staging/media/rkisp1/rkisp1-stats.c | 12 +++--------- 3 files changed, 8 insertions(+), 13 deletions(-)
drivers/staging/media/rkisp1/rkisp1-common.h +4 -0
@@ -42,6 +42,10 @@#define RKISP1_MAX_BUS_CLK 8 +#define RKISP1_STATS_MEAS_MASK (RKISP1_CIF_ISP_AWB_DONE | \ + RKISP1_CIF_ISP_AFM_FIN | \ + RKISP1_CIF_ISP_EXP_END | \ + RKISP1_CIF_ISP_HIST_MEASURE_RDY)enum rkisp1_rsz_pad {RKISP1_RSZ_PAD_SINK ,RKISP1_RSZ_PAD_SRC ,
drivers/staging/media/rkisp1/rkisp1-isp.c +1 -4
@@ -1137,10 +1137,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS ); -if (isp_ris & (RKISP1_CIF_ISP_AWB_DONE | -RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END | -RKISP1_CIF_ISP_HIST_MEASURE_RDY )) +if (isp_ris &RKISP1_STATS_MEAS_MASK )rkisp1_stats_isr (&rkisp1->stats, isp_ris); }
drivers/staging/media/rkisp1/rkisp1-stats.c +3 -9
@@ -407,22 +407,16 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)spin_lock (&stats->irq_lock); - val =RKISP1_CIF_ISP_AWB_DONE |RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END |RKISP1_CIF_ISP_HIST_MEASURE_RDY ; + val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR ); isp_mis_tmp =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS ); -if (isp_mis_tmp & - (RKISP1_CIF_ISP_AWB_DONE |RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END |RKISP1_CIF_ISP_HIST_MEASURE_RDY )) +if (isp_mis_tmp &RKISP1_STATS_MEAS_MASK ) rkisp1->debug.stats_error++;if (!stats->is_streaming) goto unlock; -if (isp_ris & (RKISP1_CIF_ISP_AWB_DONE | -RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END | -RKISP1_CIF_ISP_HIST_MEASURE_RDY )) { +if (isp_ris &RKISP1_STATS_MEAS_MASK ) { work =kzalloc (sizeof (*work),GFP_ATOMIC );if (work) { INIT_WORK(&work->work,
Signature · 1 line
2.17.1
Re: [PATCH 2/5] media: staging: rkisp1: use a macro for the statistics flags mask
Hi Dafna,
Quoted 5 lines · Dafna Hirschfeld
The mask of the ready statistics flags is used in several places in the code using bitwise-or. Use a macro for that to make the code more readable.
Signed-off-by Dafna Hirschfeld <[email protected]>
Thanks
Helen
Quoted 68 lines
--- drivers/staging/media/rkisp1/rkisp1-common.h | 4 ++++ drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +---- drivers/staging/media/rkisp1/rkisp1-stats.c | 12 +++--------- 3 files changed, 8 insertions(+), 13 deletions(-)drivers/staging/media/rkisp1/rkisp1-common.h +4
-0@@ -42,6 +42,10 @@#define RKISP1_MAX_BUS_CLK 8 +#define RKISP1_STATS_MEAS_MASK (RKISP1_CIF_ISP_AWB_DONE | \ + RKISP1_CIF_ISP_AFM_FIN | \ + RKISP1_CIF_ISP_EXP_END | \ + RKISP1_CIF_ISP_HIST_MEASURE_RDY)enum rkisp1_rsz_pad {RKISP1_RSZ_PAD_SINK ,RKISP1_RSZ_PAD_SRC ,drivers/staging/media/rkisp1/rkisp1-isp.c +1
-4@@ -1137,10 +1137,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS ); -if (isp_ris & (RKISP1_CIF_ISP_AWB_DONE | -RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END | -RKISP1_CIF_ISP_HIST_MEASURE_RDY )) +if (isp_ris &RKISP1_STATS_MEAS_MASK )rkisp1_stats_isr (&rkisp1->stats, isp_ris); }drivers/staging/media/rkisp1/rkisp1-stats.c +3
-9@@ -407,22 +407,16 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)spin_lock (&stats->irq_lock); - val =RKISP1_CIF_ISP_AWB_DONE |RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END |RKISP1_CIF_ISP_HIST_MEASURE_RDY ; + val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR ); isp_mis_tmp =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS ); -if (isp_mis_tmp & - (RKISP1_CIF_ISP_AWB_DONE |RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END |RKISP1_CIF_ISP_HIST_MEASURE_RDY )) +if (isp_mis_tmp &RKISP1_STATS_MEAS_MASK ) rkisp1->debug.stats_error++;if (!stats->is_streaming) goto unlock; -if (isp_ris & (RKISP1_CIF_ISP_AWB_DONE | -RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END | -RKISP1_CIF_ISP_HIST_MEASURE_RDY )) { +if (isp_ris &RKISP1_STATS_MEAS_MASK ) { work =kzalloc (sizeof (*work),GFP_ATOMIC );if (work) { INIT_WORK(&work->work,
Re: [PATCH 2/5] media: staging: rkisp1: use a macro for the statistics flags mask
In reply to Dafna HirschfeldHi Dafna,
Thank you for the patch.
Quoted 47 lines · Dafna Hirschfeld
The mask of the ready statistics flags is used in several places in the code using bitwise-or. Use a macro for that to make the code more readable.
Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/rkisp1-common.h | 4 ++++ drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +---- drivers/staging/media/rkisp1/rkisp1-stats.c | 12 +++--------- 3 files changed, 8 insertions(+), 13 deletions(-)drivers/staging/media/rkisp1/rkisp1-common.h +4
-0@@ -42,6 +42,10 @@#define RKISP1_MAX_BUS_CLK 8 +#define RKISP1_STATS_MEAS_MASK (RKISP1_CIF_ISP_AWB_DONE | \ + RKISP1_CIF_ISP_AFM_FIN | \ + RKISP1_CIF_ISP_EXP_END | \ + RKISP1_CIF_ISP_HIST_MEASURE_RDY)enum rkisp1_rsz_pad {RKISP1_RSZ_PAD_SINK ,RKISP1_RSZ_PAD_SRC ,drivers/staging/media/rkisp1/rkisp1-isp.c +1
-4@@ -1137,10 +1137,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS ); -if (isp_ris & (RKISP1_CIF_ISP_AWB_DONE | -RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END | -RKISP1_CIF_ISP_HIST_MEASURE_RDY )) +if (isp_ris &RKISP1_STATS_MEAS_MASK )rkisp1_stats_isr (&rkisp1->stats, isp_ris); }drivers/staging/media/rkisp1/rkisp1-stats.c +0
-0
drivers/staging/media/rkisp1/rkisp1-stats.c void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) · line 411
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +1
-2@@ -407,22 +407,16 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)spin_lock (&stats->irq_lock); - val =RKISP1_CIF_ISP_AWB_DONE |RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END |RKISP1_CIF_ISP_HIST_MEASURE_RDY ; + val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );
This could become
rkisp1_write(rkisp1, RKISP1_STATS_MEAS_MASK, RKISP1_CIF_ISP_ICR);
Quoted 18 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +2
-7isp_mis_tmp =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS ); -if (isp_mis_tmp & - (RKISP1_CIF_ISP_AWB_DONE |RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END |RKISP1_CIF_ISP_HIST_MEASURE_RDY )) +if (isp_mis_tmp &RKISP1_STATS_MEAS_MASK ) rkisp1->debug.stats_error++;if (!stats->is_streaming) goto unlock; -if (isp_ris & (RKISP1_CIF_ISP_AWB_DONE | -RKISP1_CIF_ISP_AFM_FIN | -RKISP1_CIF_ISP_EXP_END | -RKISP1_CIF_ISP_HIST_MEASURE_RDY )) { +if (isp_ris &RKISP1_STATS_MEAS_MASK ) { work =kzalloc (sizeof (*work),GFP_ATOMIC );if (work) { INIT_WORK(&work->work,
Signature · 3 lines
Regards, Laurent Pinchart
PATCH 3/5 media: staging: rkisp1: stats: use spin_lock_irqsave for irq_lock
In reply to Dafna HirschfeldCurrently 'spin_lock' is used in order to lock the 'irq_lock'. This should be replaced with 'spin_lock_irqsave' since it is used in the irq handler.
--- drivers/staging/media/rkisp1/rkisp1-stats.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/media/rkisp1/rkisp1-stats.c +3 -2
@@ -403,9 +403,10 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)struct rkisp1_device *rkisp1 = stats->rkisp1;struct rkisp1_isp_readout_work *work;unsigned int isp_mis_tmp =0 ; +unsigned long flags;u32 val; -spin_lock (&stats->irq_lock); +spin_lock_irqsave (&stats->irq_lock, flags); val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );
@@ -435,7 +436,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) } unlock: -spin_unlock (&stats->irq_lock); +spin_unlock_irqrestore (&stats->irq_lock, flags); }static void rkisp1_init_stats (struct rkisp1_stats *stats)
Signature · 1 line
2.17.1
Re: [PATCH 3/5] media: staging: rkisp1: stats: use spin_lock_irqsave for irq_lock
Hi Dafna,
Quoted 14 lines · Dafna Hirschfeld
Currently '
spin_lock' is used in order to lock the 'irq_lock'. This should be replaced with 'spin_lock_irqsave' since it is used in the irq handler.Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/rkisp1-stats.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/staging/media/rkisp1/rkisp1-stats.c +0
-0
drivers/staging/media/rkisp1/rkisp1-stats.c void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) · line 409
Quoted 8 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +2
-1@@ -403,9 +403,10 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)struct rkisp1_device *rkisp1 = stats->rkisp1;struct rkisp1_isp_readout_work *work;unsigned int isp_mis_tmp =0 ; +unsigned long flags;u32 val; -spin_lock (&stats->irq_lock); +spin_lock_irqsave (&stats->irq_lock, flags);
Since you are moving this function to a threaded irq handler, you won't be in interrupt context.
The spin_lock_irqsave() function disable interrupts for the critical section, are you sure this is required?
Regards,
Helen
Quoted 13 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +1
-1val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );@@ -435,7 +436,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) } unlock: -spin_unlock (&stats->irq_lock); +spin_unlock_irqrestore (&stats->irq_lock, flags); }static void rkisp1_init_stats (struct rkisp1_stats *stats)
Re: [PATCH 3/5] media: staging: rkisp1: stats: use spin_lock_irqsave for irq_lock
Quoted 23 lines · Helen Koike
Hi Dafna,
Quoted 20 lines · Dafna Hirschfeld
Currently '
spin_lock' is used in order to lock the 'irq_lock'. This should be replaced with 'spin_lock_irqsave' since it is used in the irq handler.Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/rkisp1-stats.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/staging/media/rkisp1/rkisp1-stats.c +1
-0@@ -403,9 +403,10 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)struct rkisp1_device *rkisp1 = stats->rkisp1;struct rkisp1_isp_readout_work *work;unsigned int isp_mis_tmp =0 ; +unsigned long flags;u32 val;
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-stats.c void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) · line 409
Quoted 2 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +1
-1-spin_lock (&stats->irq_lock); +spin_lock_irqsave (&stats->irq_lock, flags);Since you are moving this function to a threaded irq handler, you won't be in interrupt context.
The
spin_lock_irqsave()function disable interrupts for the critical section, are you sure this is required?
Hi,
The lock is also used in the hard irq handler in the patch that moves the statistics to threaded interrupt. The code in the hard irq iterates the buffers queue to find the next buffer available and set the flags of the ready statistics on it.
Thanks,
Dafna
Quoted 17 lines
Regards,
HelenQuoted 13 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +1
-1val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );@@ -435,7 +436,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) } unlock: -spin_unlock (&stats->irq_lock); +spin_unlock_irqrestore (&stats->irq_lock, flags); }static void rkisp1_init_stats (struct rkisp1_stats *stats)
Re: [PATCH 3/5] media: staging: rkisp1: stats: use spin_lock_irqsave for irq_lock
Hi Dafna,
Quoted 27 lines · Dafna Hirschfeld
Quoted 26 lines · Helen Koike
Quoted 14 lines · Dafna Hirschfeld
Currently '
spin_lock' is used in order to lock the 'irq_lock'. This should be replaced with 'spin_lock_irqsave' since it is used in the irq handler.Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/rkisp1-stats.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/staging/media/rkisp1/rkisp1-stats.c +0
-0drivers/staging/media/rkisp1/rkisp1-stats.c void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) · line 409
Quoted 8 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +2
-1@@ -403,9 +403,10 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)struct rkisp1_device *rkisp1 = stats->rkisp1;struct rkisp1_isp_readout_work *work;unsigned int isp_mis_tmp =0 ; +unsigned long flags;u32 val; -spin_lock (&stats->irq_lock); +spin_lock_irqsave (&stats->irq_lock, flags);Since you are moving this function to a threaded irq handler, you won't be in interrupt context.
Quoted 7 lines
Quoted 2 lines
The
spin_lock_irqsave()function disable interrupts for the critical section, are you sure this is required?Hi,
The lock is also used in the hard irq handler in the patch that moves the statistics to threaded interrupt. The code in the hard irq iterates the buffers queue to find the next buffer available and set the flags of the ready statistics on it.
The rules about spinlocks are as follows.
If the lock is never used in an interrupt handler context (or similar context, such as a timer handler for instance), then you can use spin_lock(). The reason is that, in such cases, a section covered by spin_lock()/spin_unlock() will not be preempted on the same CPU by a section that could try to take the same lock. This also applies to locks that are only used in interrupt contexts where interrupts are guaranteed to be disabled. To put it differently, if there's no risk that a spinlock-covered section will be preempted by code that will try to take the same lock, spin_lock() is enough.
Otherwise, you should use spin_lock() in code that is guaranteed to run with interrupts disabled (such as hard IRQ handlers, or code that called after another spin_lock_irq() or spin_lock_irqsave() on another lock), spin_lock_irq() in code that is guaranteed to run with interrupts enabled (such as code paths from userspace where you can guarantee interrupts haven't been disabled by, for instance, a spin_lock_irq() in the call stack), and spin_lock_irqsave() when you're not sure.
There's a tendency to always use spin_lock_irqsave() just to make sure, and it can indeed avoid potential issues when code is later refactored and assumptions that lead to the selection of the propery spin_lock*() variant change. That's a bit idea in paths where latency is critical, but should otherwise not be too much of a problem.
In this patch, as rkisp1_stats_isr() is run in a hard IRQ context with interrupts disabled, there's no need to use spin_lock_irqsave(), spin_lock() is totally fine.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +1
-1val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );@@ -435,7 +436,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) } unlock: -spin_unlock (&stats->irq_lock); +spin_unlock_irqrestore (&stats->irq_lock, flags); }static void rkisp1_init_stats (struct rkisp1_stats *stats)
Signature · 3 lines
Regards, Laurent Pinchart
PATCH 4/5 media: staging: rkisp1: stats: replace locks wq_lock, irq_lock with one lock
In reply to Dafna HirschfeldThis patch removes two locks in the rkisp1_stats struct:
- The mutex 'wq_lock' that is used to protect the buffers list 'stat'
- The spin_lock 'irq_lock' that is used to protect the variable 'is_streaming'
It replaces them with one spin_lock 'stats_lock' that protects both the buffers list and the 'is_streaming' variable.
--- drivers/staging/media/rkisp1/rkisp1-common.h | 5 ++-- drivers/staging/media/rkisp1/rkisp1-stats.c | 27 ++++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-)
drivers/staging/media/rkisp1/rkisp1-common.h +2 -3
@@ -178,7 +178,7 @@ struct rkisp1_capture {/* * struct rkisp1_stats - ISP Statistics device * -* @irq_lock: buffer queue lock +* @stats_lock: locks the buffer list 'stat' and 'is_streaming' * @stat: stats buffer list * @readout_wq: workqueue for statistics information read */
@@ -186,13 +186,12 @@ struct rkisp1_stats {struct rkisp1_vdev_node vnode;struct rkisp1_device *rkisp1; -spinlock_t irq_lock; +spinlock_t stats_lock;/* locks 'is_streaming', and 'stats' */ struct list_head stat;struct v4l2_format vdev_fmt;bool is_streaming;struct workqueue_struct *readout_wq; -struct mutex wq_lock; };/*
drivers/staging/media/rkisp1/rkisp1-stats.c +13 -14
@@ -130,12 +130,13 @@ static void rkisp1_stats_vb2_buf_queue(struct vb2_buffer *vb) container_of(vbuf, struct rkisp1_buffer, vb);struct vb2_queue *vq = vb->vb2_queue;struct rkisp1_stats *stats_dev = vq->drv_priv; +unsigned long flags; stats_buf->vaddr[0 ] =vb2_plane_vaddr (vb,0 ); -mutex_lock (&stats_dev->wq_lock); +spin_lock_irqsave (&stats_dev->stats_lock, flags);list_add_tail (&stats_buf->queue, &stats_dev->stat); -mutex_unlock (&stats_dev->wq_lock); +spin_unlock_irqrestore (&stats_dev->stats_lock, flags); }static int rkisp1_stats_vb2_buf_prepare (struct vb2_buffer *vb)
@@ -156,13 +157,13 @@ static void rkisp1_stats_vb2_stop_streaming(struct vb2_queue *vq)unsigned int i;/* Make sure no new work queued in isr before draining wq */ -spin_lock_irqsave (&stats->irq_lock, flags); +spin_lock_irqsave (&stats->stats_lock, flags); stats->is_streaming = false; -spin_unlock_irqrestore (&stats->irq_lock, flags); +spin_unlock_irqrestore (&stats->stats_lock, flags);drain_workqueue (stats->readout_wq); -mutex_lock (&stats->wq_lock); +spin_lock_irqsave (&stats->stats_lock, flags);for (i =0 ; i <RKISP1_ISP_STATS_REQ_BUFS_MAX ; i++) {if (list_empty (&stats->stat))break ;
@@ -171,7 +172,7 @@ static void rkisp1_stats_vb2_stop_streaming(struct vb2_queue *vq)list_del (&buf->queue);vb2_buffer_done (&buf->vb.vb2_buf,VB2_BUF_STATE_ERROR ); } -mutex_unlock (&stats->wq_lock); +spin_unlock_irqrestore (&stats->stats_lock, flags); } static int
@@ -333,6 +334,7 @@ rkisp1_stats_send_measurement(struct rkisp1_stats *stats,unsigned int frame_sequence =atomic_read (&stats->rkisp1->isp.frame_sequence);u64 timestamp =ktime_get_ns (); +unsigned long flags;if (frame_sequence != meas_work->frame_id) { dev_warn(stats->rkisp1->dev,
@@ -341,14 +343,14 @@ rkisp1_stats_send_measurement(struct rkisp1_stats *stats, frame_sequence = meas_work->frame_id; } -mutex_lock (&stats->wq_lock); +spin_lock_irqsave (&stats->stats_lock, flags);/* get one empty buffer */ if (!list_empty (&stats->stat)) { cur_buf =list_first_entry (&stats->stat, struct rkisp1_buffer, queue);list_del (&cur_buf->queue); } -mutex_unlock (&stats->wq_lock); +spin_unlock_irqrestore (&stats->stats_lock, flags);if (!cur_buf)return ;
@@ -406,7 +408,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)unsigned long flags;u32 val; -spin_lock_irqsave (&stats->irq_lock, flags); +spin_lock_irqsave (&stats->stats_lock, flags); val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );
@@ -436,7 +438,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) } unlock: -spin_unlock_irqrestore (&stats->irq_lock, flags); +spin_unlock_irqrestore (&stats->stats_lock, flags); }static void rkisp1_init_stats (struct rkisp1_stats *stats)
@@ -456,10 +458,9 @@ int rkisp1_stats_register(struct rkisp1_stats *stats,int ret; stats->rkisp1 = rkisp1; -mutex_init (&stats->wq_lock);mutex_init (&node->vlock);INIT_LIST_HEAD (&stats->stat); -spin_lock_init (&stats->irq_lock); +spin_lock_init (&stats->stats_lock);strscpy (vdev->name,RKISP1_STATS_DEV_NAME ,sizeof (vdev->name));
@@ -506,7 +507,6 @@ int rkisp1_stats_register(struct rkisp1_stats *stats, err_release_queue:vb2_queue_release (vdev->queue);mutex_destroy (&node->vlock); -mutex_destroy (&stats->wq_lock);return ret; }
@@ -520,5 +520,4 @@ void rkisp1_stats_unregister(struct rkisp1_stats *stats)media_entity_cleanup (&vdev->entity);vb2_queue_release (vdev->queue);mutex_destroy (&node->vlock); -mutex_destroy (&stats->wq_lock); }
Signature · 1 line
2.17.1
Re: [PATCH 4/5] media: staging: rkisp1: stats: replace locks wq_lock, irq_lock with one lock
Hi Dafna,
Thank you for the patch.
Quoted 7 lines · Dafna Hirschfeld
This patch removes two locks in the
rkisp1_statsstruct:
- The mutex 'wq_lock' that is used to protect the buffers list 'stat'
- Thespin_lock'irq_lock' that is used to protect the variable 'is_streaming'It replaces them with one
spin_lock'stats_lock' that protects both the buffers list and the 'is_streaming' variable.
You should explain in the commit message why this is needed.
Quoted 20 lines
--- drivers/staging/media/rkisp1/rkisp1-common.h | 5 ++-- drivers/staging/media/rkisp1/rkisp1-stats.c | 27 ++++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-)drivers/staging/media/rkisp1/rkisp1-common.h +1
-1@@ -178,7 +178,7 @@ struct rkisp1_capture {/* * struct rkisp1_stats - ISP Statistics device * -* @irq_lock: buffer queue lock +* @stats_lock: locks the buffer list 'stat' and 'is_streaming' * @stat: stats buffer list * @readout_wq: workqueue for statistics information read */
drivers/staging/media/rkisp1/rkisp1-common.h struct rkisp1_stats { · line 189
Quoted 5 lines
drivers/staging/media/rkisp1/rkisp1-common.h +1
-1@@ -186,13 +186,12 @@ struct rkisp1_stats {struct rkisp1_vdev_node vnode;struct rkisp1_device *rkisp1; -spinlock_t irq_lock; +spinlock_t stats_lock;/* locks 'is_streaming', and 'stats' */
If there's a single lock, how about simply calling it 'lock' ?
Quoted 14 lines
drivers/staging/media/rkisp1/rkisp1-common.h +0
-1struct list_head stat;struct v4l2_format vdev_fmt;bool is_streaming;struct workqueue_struct *readout_wq; -struct mutex wq_lock; };/* drivers/staging/media/rkisp1/rkisp1-stats.c +0
-0
drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_vb2_buf_queue(struct vb2_buffer *vb) · line 137
Quoted 9 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +2
-1@@ -130,12 +130,13 @@ static void rkisp1_stats_vb2_buf_queue(struct vb2_buffer *vb) container_of(vbuf, struct rkisp1_buffer, vb);struct vb2_queue *vq = vb->vb2_queue;struct rkisp1_stats *stats_dev = vq->drv_priv; +unsigned long flags; stats_buf->vaddr[0 ] =vb2_plane_vaddr (vb,0 ); -mutex_lock (&stats_dev->wq_lock); +spin_lock_irqsave (&stats_dev->stats_lock, flags);
As I explained in a previous e-mail in this series, you could use spin_lock_irq() here as there's a guarantee interrupts are not disabled. Up to you.
Quoted 101 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +11
-13list_add_tail (&stats_buf->queue, &stats_dev->stat); -mutex_unlock (&stats_dev->wq_lock); +spin_unlock_irqrestore (&stats_dev->stats_lock, flags); }static int rkisp1_stats_vb2_buf_prepare (struct vb2_buffer *vb)@@ -156,13 +157,13 @@ static void rkisp1_stats_vb2_stop_streaming(struct vb2_queue *vq)unsigned int i;/* Make sure no new work queued in isr before draining wq */ -spin_lock_irqsave (&stats->irq_lock, flags); +spin_lock_irqsave (&stats->stats_lock, flags); stats->is_streaming = false; -spin_unlock_irqrestore (&stats->irq_lock, flags); +spin_unlock_irqrestore (&stats->stats_lock, flags);drain_workqueue (stats->readout_wq); -mutex_lock (&stats->wq_lock); +spin_lock_irqsave (&stats->stats_lock, flags);for (i =0 ; i <RKISP1_ISP_STATS_REQ_BUFS_MAX ; i++) {if (list_empty (&stats->stat))break ;@@ -171,7 +172,7 @@ static void rkisp1_stats_vb2_stop_streaming(struct vb2_queue *vq)list_del (&buf->queue);vb2_buffer_done (&buf->vb.vb2_buf,VB2_BUF_STATE_ERROR ); } -mutex_unlock (&stats->wq_lock); +spin_unlock_irqrestore (&stats->stats_lock, flags); } static int@@ -333,6 +334,7 @@ rkisp1_stats_send_measurement(struct rkisp1_stats *stats,unsigned int frame_sequence =atomic_read (&stats->rkisp1->isp.frame_sequence);u64 timestamp =ktime_get_ns (); +unsigned long flags;if (frame_sequence != meas_work->frame_id) { dev_warn(stats->rkisp1->dev,@@ -341,14 +343,14 @@ rkisp1_stats_send_measurement(struct rkisp1_stats *stats, frame_sequence = meas_work->frame_id; } -mutex_lock (&stats->wq_lock); +spin_lock_irqsave (&stats->stats_lock, flags);/* get one empty buffer */ if (!list_empty (&stats->stat)) { cur_buf =list_first_entry (&stats->stat, struct rkisp1_buffer, queue);list_del (&cur_buf->queue); } -mutex_unlock (&stats->wq_lock); +spin_unlock_irqrestore (&stats->stats_lock, flags);if (!cur_buf)return ;@@ -406,7 +408,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)unsigned long flags;u32 val; -spin_lock_irqsave (&stats->irq_lock, flags); +spin_lock_irqsave (&stats->stats_lock, flags); val =RKISP1_STATS_MEAS_MASK ;rkisp1_write (rkisp1, val,RKISP1_CIF_ISP_ICR );@@ -436,7 +438,7 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris) } unlock: -spin_unlock_irqrestore (&stats->irq_lock, flags); +spin_unlock_irqrestore (&stats->stats_lock, flags); }static void rkisp1_init_stats (struct rkisp1_stats *stats)@@ -456,10 +458,9 @@ int rkisp1_stats_register(struct rkisp1_stats *stats,int ret; stats->rkisp1 = rkisp1; -mutex_init (&stats->wq_lock);mutex_init (&node->vlock);INIT_LIST_HEAD (&stats->stat); -spin_lock_init (&stats->irq_lock); +spin_lock_init (&stats->stats_lock);strscpy (vdev->name,RKISP1_STATS_DEV_NAME ,sizeof (vdev->name));@@ -506,7 +507,6 @@ int rkisp1_stats_register(struct rkisp1_stats *stats, err_release_queue:vb2_queue_release (vdev->queue);mutex_destroy (&node->vlock); -mutex_destroy (&stats->wq_lock);return ret; }@@ -520,5 +520,4 @@ void rkisp1_stats_unregister(struct rkisp1_stats *stats)media_entity_cleanup (&vdev->entity);vb2_queue_release (vdev->queue);mutex_destroy (&node->vlock); -mutex_destroy (&stats->wq_lock); }
Signature · 3 lines
Regards, Laurent Pinchart
PATCH 5/5 media: staging: rkisp1: replace workqueue with threaded irq for reading statistics registers
In reply to Dafna HirschfeldReading the statistics registers might take too long to run inside the irq handler. Currently it is deferred to bottom half using workqueues. This patch replaces the workqueue with threaded interrupts for reading the statistics registers.
A new struct type 'rkisp1_kstats_buffer' is used as the statistics buffers. The struct has a field 'ris' which is the flags of ready statistics. If new statistics are ready, the irq handler sets this variable and the frame sequence on the next available buffer and returns IRQ_WAKE_THREAD.
Then the threaded interrupt reads the registers and calls vb2_buffer_done.
--- drivers/staging/media/rkisp1/TODO | 1 - drivers/staging/media/rkisp1/rkisp1-common.h | 5 +- drivers/staging/media/rkisp1/rkisp1-dev.c | 8 +- drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +- drivers/staging/media/rkisp1/rkisp1-stats.c | 167 ++++++++----------- 5 files changed, 76 insertions(+), 110 deletions(-)
drivers/staging/media/rkisp1/TODO +0 -1
@@ -1,5 +1,4 @@ * Fix pad format size for statistics and parameters entities. -* Use threaded interrupt for rkisp1_stats_isr(), remove work queue. * Fix checkpatch errors. * Review and comment every lock * Handle quantization
drivers/staging/media/rkisp1/rkisp1-common.h +2 -3
@@ -190,8 +190,6 @@ struct rkisp1_stats {struct list_head stat;struct v4l2_format vdev_fmt;bool is_streaming; - -struct workqueue_struct *readout_wq; };/*
@@ -308,10 +306,11 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx);irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1); -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris); +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);int rkisp1_capture_devs_register (struct rkisp1_device *rkisp1);
drivers/staging/media/rkisp1/rkisp1-dev.c +6 -2
@@ -405,6 +405,8 @@ irqreturn_t rkisp1_isr(int irq, void *ctx) isp_ret =rkisp1_isp_isr (rkisp1); mipi_ret =rkisp1_mipi_isr (rkisp1); +if (isp_ret ==IRQ_WAKE_THREAD ) +return IRQ_WAKE_THREAD ;if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE )return IRQ_NONE ;
@@ -490,8 +492,10 @@ static int rkisp1_probe(struct platform_device *pdev)if (irq <0 )return irq; - ret =devm_request_irq (dev, irq, rkisp1_isr,IRQF_SHARED , -dev_driver_string (dev), dev); + ret =devm_request_threaded_irq (dev, irq, rkisp1_isr, + rkisp1_read_stats_threaded_irq, +IRQF_SHARED , +dev_driver_string (dev), dev);if (ret) {dev_err (dev,"request irq failed: %d\n" , ret);return ret;
drivers/staging/media/rkisp1/rkisp1-isp.c +3 -2
@@ -1111,6 +1111,7 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; +irqreturn_t ret =IRQ_HANDLED ; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status)
@@ -1138,7 +1139,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS );if (isp_ris &RKISP1_STATS_MEAS_MASK ) -rkisp1_stats_isr (&rkisp1->stats, isp_ris); + ret =rkisp1_stats_isr (&rkisp1->stats, isp_ris); }/*
@@ -1148,5 +1149,5 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)*/ rkisp1_params_isr (rkisp1, status); -return IRQ_HANDLED ; +return ret; }
drivers/staging/media/rkisp1/rkisp1-stats.c +65 -102
271 lines not on this page · raw message
Signature · 1 line
2.17.1
kbuild test robot
build botRe: [PATCH 5/5] media: staging: rkisp1: replace workqueue with threaded irq for reading statistics registers
Thank you for the patch! Yet something to improve:
Hi Dafna,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linuxtv-media/master]
[also build test ERROR on v5.7-rc5 next-20200512]
[cannot apply to staging/staging-testing]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Dafna-Hirschfeld/media-staging-rkisp1-change-workqueue-to-threaded-irq-in-stats/20200512-200942 base: git://linuxtv.org/media_tree.git master config: nios2-allyesconfig (attached as .config) compiler: nios2-linux-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=nios2
If you fix the issue, kindly add following tag as appropriate
All error/warnings (new ones prefixed by >>):
In file included from drivers/staging/media/rkisp1/rkisp1-stats.c:14:drivers/staging/media/rkisp1/rkisp1-common.h:309:1: error: unknown type name 'irqreturn_t' 309 | irqreturn_t rkisp1_read_stats_threaded_irq(int irq, void *ctx);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:310:1: error: unknown type name 'irqreturn_t' 310 | irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:311:1: error: unknown type name 'irqreturn_t' 311 | irqreturn_t rkisp1_mipi_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:312:1: error: unknown type name 'irqreturn_t' 312 | irqreturn_t rkisp1_capture_isr(struct rkisp1_device *rkisp1);
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-common.h:313:1: error: unknown type name 'irqreturn_t' 313 | irqreturn_t rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris); | ^~~~~~~~~~~
>> drivers/staging/media/rkisp1/rkisp1-stats.c:313:1: error: unknown type name 'irqreturn_t' 313 | irqreturn_t rkisp1_read_stats_threaded_irq(int irq, void *ctx) | ^~~~~~~~~~~ drivers/staging/media/rkisp1/rkisp1-stats.c: In function 'rkisp1_read_stats_threaded_irq': >> drivers/staging/media/rkisp1/rkisp1-stats.c:326:10: error: 'IRQ_HANDLED' undeclared (first use in this function)
326 | return IRQ_HANDLED;
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-stats.c:326:10: note: each undeclared identifier is reported only once for each function it appears in drivers/staging/media/rkisp1/rkisp1-stats.c: At top level:drivers/staging/media/rkisp1/rkisp1-stats.c:382:1: error: unknown type name 'irqreturn_t'
382 | irqreturn_t rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-stats.c: In function 'rkisp1_stats_isr':drivers/staging/media/rkisp1/rkisp1-stats.c:387:2: error: unknown type name 'irqreturn_t'
387 | irqreturn_t ret = IRQ_HANDLED;
| ^~~~~~~~~~~drivers/staging/media/rkisp1/rkisp1-stats.c:387:20: error: 'IRQ_HANDLED' undeclared (first use in this function) 387 | irqreturn_t ret = IRQ_HANDLED;
| ^~~~~~~~~~~
>> drivers/staging/media/rkisp1/rkisp1-stats.c:409:10: error: 'IRQ_WAKE_THREAD' undeclared (first use in this function); did you mean 'RUSAGE_THREAD'? 409 | ret = IRQ_WAKE_THREAD; | ^~~~~~~~~~~~~~~ | RUSAGE_THREAD drivers/staging/media/rkisp1/rkisp1-stats.c: In function 'rkisp1_read_stats_threaded_irq': >> drivers/staging/media/rkisp1/rkisp1-stats.c:379:1: warning: control reaches end of non-void function [-Wreturn-type]
379 | }
| ^
vim +/irqreturn_t +313 drivers/staging/media/rkisp1/rkisp1-stats.c
312
> 313 irqreturn_t rkisp1_read_stats_threaded_irq(int irq, void *ctx)
314 {
315 struct device *dev = ctx;
316 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
317 struct rkisp1_stats *stats = &rkisp1->stats;
318 struct rkisp1_kstats_buffer *kstats_buf = NULL;
319 struct rkisp1_stat_buffer *cur_stat_buf;
320 unsigned long flags;
321 u64 timestamp = ktime_get_ns();
322
323 spin_lock_irqsave(&stats->stats_lock, flags);
324 if (!stats->is_streaming) {
325 spin_unlock_irqrestore(&stats->stats_lock, flags);
> 326 return IRQ_HANDLED;
327 }
328 if (list_empty(&stats->stat)) {
329 spin_unlock_irqrestore(&stats->stats_lock, flags);
330 WARN("%s: threaded irq waked but there are no buffers",
331 __func__);
332 return IRQ_HANDLED;
333 }
334 kstats_buf = list_first_entry(&stats->stat,
335 struct rkisp1_kstats_buffer, buff.queue);
336
337 /*
338 * each waked irq thread reads exactly one ready statistics
339 * so it is a bug if no statistics are ready
340 */
341 if (!kstats_buf->ris) {
342 spin_unlock_irqrestore(&stats->stats_lock, flags);
343 WARN("%s: threaded irq waked but buffer holds no measures",
344 __func__);
345 return IRQ_HANDLED;
346 }
347 list_del(&kstats_buf->buff.queue);
348 spin_unlock_irqrestore(&stats->stats_lock, flags);
349
350 cur_stat_buf =
351 (struct rkisp1_stat_buffer *)(kstats_buf->buff.vaddr[0]);
352
353 if (kstats_buf->ris & RKISP1_CIF_ISP_AWB_DONE) {
354 rkisp1_stats_get_awb_meas(stats, cur_stat_buf);
355 cur_stat_buf->meas_type |= RKISP1_CIF_ISP_STAT_AWB;
356 }
357
358 if (kstats_buf->ris & RKISP1_CIF_ISP_AFM_FIN) {
359 rkisp1_stats_get_afc_meas(stats, cur_stat_buf);
360 cur_stat_buf->meas_type |= RKISP1_CIF_ISP_STAT_AFM_FIN;
361 }
362
363 if (kstats_buf->ris & RKISP1_CIF_ISP_EXP_END) {
364 rkisp1_stats_get_aec_meas(stats, cur_stat_buf);
365 rkisp1_stats_get_bls_meas(stats, cur_stat_buf);
366 cur_stat_buf->meas_type |= RKISP1_CIF_ISP_STAT_AUTOEXP;
367 }
368
369 if (kstats_buf->ris & RKISP1_CIF_ISP_HIST_MEASURE_RDY) {
370 rkisp1_stats_get_hst_meas(stats, cur_stat_buf);
371 cur_stat_buf->meas_type |= RKISP1_CIF_ISP_STAT_HIST;
372 }
373
374 vb2_set_plane_payload(&kstats_buf->buff.vb.vb2_buf, 0,
375 sizeof(struct rkisp1_stat_buffer));
376 kstats_buf->buff.vb.vb2_buf.timestamp = timestamp;
377 vb2_buffer_done(&kstats_buf->buff.vb.vb2_buf, VB2_BUF_STATE_DONE);
378 return IRQ_HANDLED;
> 379 }
380
381
382 irqreturn_t rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)
383 {
384 struct rkisp1_device *rkisp1 = stats->rkisp1;
385 struct rkisp1_isp *isp = &rkisp1->isp;
386 struct rkisp1_kstats_buffer *buf = NULL;
> 387 irqreturn_t ret = IRQ_HANDLED;
388 unsigned int isp_mis_tmp = 0;
389 unsigned long flags;
390 u32 val;
391
392 spin_lock_irqsave(&stats->stats_lock, flags);
393
394 val = RKISP1_STATS_MEAS_MASK;
395 rkisp1_write(rkisp1, val, RKISP1_CIF_ISP_ICR);
396
397 isp_mis_tmp = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
398 if (isp_mis_tmp & RKISP1_STATS_MEAS_MASK)
399 rkisp1->debug.stats_error++;
400
401 if (!stats->is_streaming || !(isp_ris & RKISP1_STATS_MEAS_MASK))
402 goto unlock;
403
404 list_for_each_entry(buf, &stats->stat, buff.queue) {
405 if (!buf->ris) {
406 buf->buff.vb.sequence =
407 atomic_read(&isp->frame_sequence);
408 buf->ris = isp_ris;
> 409 ret = IRQ_WAKE_THREAD; 410 break;
411 }
412 }
413
414 unlock:
415 spin_unlock_irqrestore(&stats->stats_lock, flags);
416 return ret;
417 }
418
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
Re: [PATCH 5/5] media: staging: rkisp1: replace workqueue with threaded irq for reading statistics registers
In reply to Dafna HirschfeldHi Dafna,
Thank you for the patch.
Quoted 5 lines · Dafna Hirschfeld
Reading the statistics registers might take too long to run inside the irq handler. Currently it is deferred to bottom half using workqueues. This patch replaces the workqueue with threaded interrupts for reading the statistics registers.
Could you please explain why this is needed/desired ? Removal of the kzalloc(GFP_ATOMIC) is very nice, but I'm sure there would have been ways to avoid it while keeping a workqueue. I'm not claiming the patch is bad, but I'd like to understand the reason.
Quoted 58 lines
A new struct type '
rkisp1_kstats_buffer' is used as the statistics buffers. The struct has a field 'ris' which is the flags of ready statistics. If new statistics are ready, the irq handler sets this variable and the frame sequence on the next available buffer and returnsIRQ_WAKE_THREAD.
Then the threaded interrupt reads the registers and callsvb2_buffer_done.Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/TODO | 1 - drivers/staging/media/rkisp1/rkisp1-common.h | 5 +- drivers/staging/media/rkisp1/rkisp1-dev.c | 8 +- drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +- drivers/staging/media/rkisp1/rkisp1-stats.c | 167 ++++++++----------- 5 files changed, 76 insertions(+), 110 deletions(-)drivers/staging/media/rkisp1/TODO +0
-1@@ -1,5 +1,4 @@ * Fix pad format size for statistics and parameters entities. -* Use threaded interrupt for rkisp1_stats_isr(), remove work queue. * Fix checkpatch errors. * Review and comment every lock * Handle quantizationdrivers/staging/media/rkisp1/rkisp1-common.h +2
-3@@ -190,8 +190,6 @@ struct rkisp1_stats {struct list_head stat;struct v4l2_format vdev_fmt;bool is_streaming; - -struct workqueue_struct *readout_wq; };/* @@ -308,10 +306,11 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx);irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1); -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris); +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);int rkisp1_capture_devs_register (struct rkisp1_device *rkisp1);drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0
drivers/staging/media/rkisp1/rkisp1-dev.c irqreturn_t rkisp1_isr(int irq, void *ctx) · line 411
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +2
-0@@ -405,6 +405,8 @@ irqreturn_t rkisp1_isr(int irq, void *ctx) isp_ret =rkisp1_isp_isr (rkisp1); mipi_ret =rkisp1_mipi_isr (rkisp1); +if (isp_ret ==IRQ_WAKE_THREAD ) +return IRQ_WAKE_THREAD ;if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE )return IRQ_NONE ;
Let me slightly modify my proposal from another patch in this series:
irqreturn_t ret = IRQ_NONE; ... ret |= rkisp1_capture_isr(rkisp1); ret |= rkisp1_isp_isr(rkisp1); ret |= rkisp1_mipi_isr(rkisp1); return ret & IRQ_WAKE_THREAD ? IRQ_WAKE_THREAD : ret;
Up to you.
Quoted 27 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +4
-2@@ -490,8 +492,10 @@ static int rkisp1_probe(struct platform_device *pdev)if (irq <0 )return irq; - ret =devm_request_irq (dev, irq, rkisp1_isr,IRQF_SHARED , -dev_driver_string (dev), dev); + ret =devm_request_threaded_irq (dev, irq, rkisp1_isr, + rkisp1_read_stats_threaded_irq, +IRQF_SHARED , +dev_driver_string (dev), dev);if (ret) {dev_err (dev,"request irq failed: %d\n" , ret);return ret;drivers/staging/media/rkisp1/rkisp1-isp.c +1
-0@@ -1111,6 +1111,7 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; +irqreturn_t ret =IRQ_HANDLED ; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status)
drivers/staging/media/rkisp1/rkisp1-isp.c irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1) · line 1142
Quoted 5 lines
drivers/staging/media/rkisp1/rkisp1-isp.c +1
-1@@ -1138,7 +1139,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS );if (isp_ris &RKISP1_STATS_MEAS_MASK ) -rkisp1_stats_isr (&rkisp1->stats, isp_ris); + ret =rkisp1_stats_isr (&rkisp1->stats, isp_ris);
Overriding the ret variable isn't very nice, even if it should work in practice here as ret is already equal to IRQ_HANDLED and rkisp1_stats_isr() returns IRQ_HANDLED or IRQ_WAKE_THREAD, never IRQ_NONE. You could however write
ret |= rkisp1_stats_isr(&rkisp1->stats, isp_ris);
as IRQ_HANDLED and IRQ_WAKE_THREAD are independent bit flags to accumulate them, provided you return IRQ_WAKE_THREAD from rkisp1_isr() when both IRQ_WAKE_THREAD and IRQ_HANDLED are set (it seems the the core IRQ code can't deal with both being set).
Quoted 139 lines
drivers/staging/media/rkisp1/rkisp1-isp.c +1
-1}/* @@ -1148,5 +1149,5 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)*/ rkisp1_params_isr (rkisp1, status); -return IRQ_HANDLED ; +return ret; }drivers/staging/media/rkisp1/rkisp1-stats.c +26
-47@@ -18,19 +18,9 @@#define RKISP1_ISP_STATS_REQ_BUFS_MIN 2#define RKISP1_ISP_STATS_REQ_BUFS_MAX 8 -enum rkisp1_isp_readout_cmd { -RKISP1_ISP_READOUT_MEAS , -RKISP1_ISP_READOUT_META , -}; - -struct rkisp1_isp_readout_work { -struct work_struct work; -struct rkisp1_stats *stats; - -unsigned int frame_id; -unsigned int isp_ris; -enum rkisp1_isp_readout_cmd readout; -struct vb2_buffer *vb; +struct rkisp1_kstats_buffer { +struct rkisp1_buffer buff; +u32 ris; }; static int rkisp1_stats_enum_fmt_meta_cap(struct file *file, void *priv,@@ -126,16 +116,17 @@ static int rkisp1_stats_vb2_queue_setup(struct vb2_queue *vq,static void rkisp1_stats_vb2_buf_queue (struct vb2_buffer *vb) {struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); -struct rkisp1_buffer *stats_buf = - container_of(vbuf, struct rkisp1_buffer, vb); +struct rkisp1_kstats_buffer *kstats_buf = + container_of(vbuf, struct rkisp1_kstats_buffer, buff.vb);struct vb2_queue *vq = vb->vb2_queue;struct rkisp1_stats *stats_dev = vq->drv_priv;unsigned long flags; - stats_buf->vaddr[0 ] =vb2_plane_vaddr (vb,0 ); + kstats_buf->buff.vaddr[0 ] =vb2_plane_vaddr (vb,0 ); + kstats_buf->ris =0 ;spin_lock_irqsave (&stats_dev->stats_lock, flags); -list_add_tail (&stats_buf->queue, &stats_dev->stat); +list_add_tail (&kstats_buf->buff.queue, &stats_dev->stat);spin_unlock_irqrestore (&stats_dev->stats_lock, flags); }@@ -152,25 +143,19 @@ static int rkisp1_stats_vb2_buf_prepare(struct vb2_buffer *vb)static void rkisp1_stats_vb2_stop_streaming (struct vb2_queue *vq) {struct rkisp1_stats *stats = vq->drv_priv; -struct rkisp1_buffer *buf; +struct rkisp1_kstats_buffer *buf;unsigned long flags;unsigned int i; -/* Make sure no new work queued in isr before draining wq */ spin_lock_irqsave (&stats->stats_lock, flags); stats->is_streaming = false; -spin_unlock_irqrestore (&stats->stats_lock, flags); - -drain_workqueue (stats->readout_wq); - -spin_lock_irqsave (&stats->stats_lock, flags);for (i =0 ; i <RKISP1_ISP_STATS_REQ_BUFS_MAX ; i++) {if (list_empty (&stats->stat))break ; buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&buf->queue); -vb2_buffer_done (&buf->vb.vb2_buf,VB2_BUF_STATE_ERROR ); + struct rkisp1_kstats_buffer, buff.queue); +list_del (&buf->buff.queue); +vb2_buffer_done (&buf->buff.vb.vb2_buf,VB2_BUF_STATE_ERROR ); }spin_unlock_irqrestore (&stats->stats_lock, flags); }@@ -207,7 +192,7 @@ rkisp1_stats_init_vb2_queue(struct vb2_queue *q, struct rkisp1_stats *stats) q->drv_priv = stats; q->ops = &rkisp1_stats_vb2_ops; q->mem_ops = &vb2_vmalloc_memops; - q->buf_struct_size =sizeof (struct rkisp1_buffer ); + q->buf_struct_size =sizeof (struct rkisp1_kstats_buffer ); q->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC ; q->lock = &node->vlock;@@ -325,85 +310,81 @@ static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, } } -static void -rkisp1_stats_send_measurement (struct rkisp1_stats *stats, -struct rkisp1_isp_readout_work *meas_work) +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx) { +struct device *dev = ctx; +struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +struct rkisp1_stats *stats = &rkisp1->stats; +struct rkisp1_kstats_buffer *kstats_buf =NULL ;struct rkisp1_stat_buffer *cur_stat_buf; -struct rkisp1_buffer *cur_buf =NULL ; -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence); -u64 timestamp =ktime_get_ns ();unsigned long flags; - -if (frame_sequence != meas_work->frame_id) { -dev_warn (stats->rkisp1->dev, -"Measurement late(%d, %d)\n" , - frame_sequence, meas_work->frame_id); - frame_sequence = meas_work->frame_id; - } +u64 timestamp =ktime_get_ns ();spin_lock_irqsave (&stats->stats_lock, flags); -/* get one empty buffer */ -if (!list_empty (&stats->stat)) { - cur_buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&cur_buf->queue); +if (!stats->is_streaming) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +return IRQ_HANDLED ; + } +if (list_empty (&stats->stat)) { +spin_unlock_irqrestore (&stats->stats_lock, flags); + WARN("%s: threaded irq waked but there are no buffers",
drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 340
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +10
-0+ __func__); +return IRQ_HANDLED ; + } + kstats_buf =list_first_entry (&stats->stat, + struct rkisp1_kstats_buffer, buff.queue); + +/* +* each waked irq thread reads exactly one ready statistics +* so it is a bug if no statistics are ready +*/
What happens if this function takes too long to run ? With the workqueue, if the work gets delayed once in a while, the work items will accumulate, and all of them will be handled eventually. Here it seems that an IRQ could get lost.
Quoted 51 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +15
-12+if (!kstats_buf->ris) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +WARN ("%s: threaded irq waked but buffer holds no measures" , + __func__); +return IRQ_HANDLED ; } +list_del (&kstats_buf->buff.queue);spin_unlock_irqrestore (&stats->stats_lock, flags); -if (!cur_buf) -return ; - cur_stat_buf = - (struct rkisp1_stat_buffer *)(cur_buf->vaddr[0 ]); + (struct rkisp1_stat_buffer *)(kstats_buf->buff.vaddr[0 ]); -if (meas_work->isp_ris &RKISP1_CIF_ISP_AWB_DONE ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_AWB_DONE ) {rkisp1_stats_get_awb_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AWB ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_AFM_FIN ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_AFM_FIN ) {rkisp1_stats_get_afc_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AFM_FIN ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_EXP_END ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_EXP_END ) {rkisp1_stats_get_aec_meas (stats, cur_stat_buf);rkisp1_stats_get_bls_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AUTOEXP ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_HIST_MEASURE_RDY ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_HIST_MEASURE_RDY ) {rkisp1_stats_get_hst_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_HIST ; } -vb2_set_plane_payload (&cur_buf->vb.vb2_buf,0 , +vb2_set_plane_payload (&kstats_buf->buff.vb.vb2_buf,0 ,sizeof (struct rkisp1_stat_buffer )); - cur_buf->vb.sequence = frame_sequence; - cur_buf->vb.vb2_buf.timestamp = timestamp; -vb2_buffer_done (&cur_buf->vb.vb2_buf,VB2_BUF_STATE_DONE ); + kstats_buf->buff.vb.vb2_buf.timestamp = timestamp; +vb2_buffer_done (&kstats_buf->buff.vb.vb2_buf,VB2_BUF_STATE_DONE ); +return IRQ_HANDLED ; }
drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 381
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +0
-8-static void rkisp1_stats_readout_work (struct work_struct *work) -{ -struct rkisp1_isp_readout_work *readout_work = - container_of(work, struct rkisp1_isp_readout_work, work); -struct rkisp1_stats *stats = readout_work->stats; - -if (readout_work->readout ==RKISP1_ISP_READOUT_MEAS ) -rkisp1_stats_send_measurement (stats, readout_work);
Is the second blank line intentional ?
Quoted 83 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +14
-35-kfree (readout_work); -} - -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris) +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris) { -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence);struct rkisp1_device *rkisp1 = stats->rkisp1; -struct rkisp1_isp_readout_work *work; +struct rkisp1_isp *isp = &rkisp1->isp; +struct rkisp1_kstats_buffer *buf =NULL ; +irqreturn_t ret =IRQ_HANDLED ;unsigned int isp_mis_tmp =0 ;unsigned long flags;u32 val;@@ -417,28 +398,22 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)if (isp_mis_tmp &RKISP1_STATS_MEAS_MASK ) rkisp1->debug.stats_error++; -if (!stats->is_streaming) +if (!stats->is_streaming || !(isp_ris &RKISP1_STATS_MEAS_MASK )) goto unlock; -if (isp_ris &RKISP1_STATS_MEAS_MASK ) { - work =kzalloc (sizeof (*work),GFP_ATOMIC ); -if (work) { -INIT_WORK (&work->work, - rkisp1_stats_readout_work); - work->readout =RKISP1_ISP_READOUT_MEAS ; - work->stats = stats; - work->frame_id = frame_sequence; - work->isp_ris = isp_ris; -if (!queue_work (stats->readout_wq, - &work->work)) -kfree (work); - }else { -dev_err (stats->rkisp1->dev, -"Could not allocate work\n" ); + +list_for_each_entry (buf, &stats->stat, buff.queue) { +if (!buf->ris) { + buf->buff.vb.sequence = +atomic_read (&isp->frame_sequence); + buf->ris = isp_ris; + ret =IRQ_WAKE_THREAD ; +break ; } } unlock:spin_unlock_irqrestore (&stats->stats_lock, flags); +return ret; }static void rkisp1_init_stats (struct rkisp1_stats *stats)@@ -489,19 +464,8 @@ int rkisp1_stats_register(struct rkisp1_stats *stats, goto err_cleanup_media_entity; } - stats->readout_wq =alloc_workqueue ("measurement_queue" , -WQ_UNBOUND |WQ_MEM_RECLAIM , -1 ); - -if (!stats->readout_wq) { - ret = -ENOMEM ; - goto err_unreg_vdev; - } -return 0 ; -err_unreg_vdev: -video_unregister_device (vdev); err_cleanup_media_entity:media_entity_cleanup (&vdev->entity); err_release_queue:@@ -515,7 +479,6 @@ void rkisp1_stats_unregister(struct rkisp1_stats *stats)struct rkisp1_vdev_node *node = &stats->vnode;struct video_device *vdev = &node->vdev; -destroy_workqueue (stats->readout_wq);video_unregister_device (vdev);media_entity_cleanup (&vdev->entity);vb2_queue_release (vdev->queue);
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH 5/5] media: staging: rkisp1: replace workqueue with threaded irq for reading statistics registers
Hi Dafna,
Quoted 12 lines · Laurent Pinchart
Hi Dafna,
Thank you for the patch.
Quoted 5 lines · Dafna Hirschfeld
Reading the statistics registers might take too long to run inside the irq handler. Currently it is deferred to bottom half using workqueues. This patch replaces the workqueue with threaded interrupts for reading the statistics registers.
Quoted 5 lines
Could you please explain why this is needed/desired ? Removal of the
kzalloc(GFP_ATOMIC)is very nice, but I'm sure there would have been ways to avoid it while keeping a workqueue. I'm not claiming the patch is bad, but I'd like to understand the reason.
Thanks a lot for working on this driver!
I'll second what Laurent said. In general, a description of the patch should explain why a change is needed, e.g. what issues it fixes or what improvements it brings.
Also, would you mind adding me on CC for any patches for this driver? Since we use this driver in Chrome OS, I'd like to stay on top of any updates. Thanks in advance!
Best regards,
Tomasz
Quoted 431 lines
Quoted 58 lines
A new struct type '
rkisp1_kstats_buffer' is used as the statistics buffers. The struct has a field 'ris' which is the flags of ready statistics. If new statistics are ready, the irq handler sets this variable and the frame sequence on the next available buffer and returnsIRQ_WAKE_THREAD.
Then the threaded interrupt reads the registers and callsvb2_buffer_done.Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/TODO | 1 - drivers/staging/media/rkisp1/rkisp1-common.h | 5 +- drivers/staging/media/rkisp1/rkisp1-dev.c | 8 +- drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +- drivers/staging/media/rkisp1/rkisp1-stats.c | 167 ++++++++----------- 5 files changed, 76 insertions(+), 110 deletions(-)drivers/staging/media/rkisp1/TODO +0
-1@@ -1,5 +1,4 @@ * Fix pad format size for statistics and parameters entities. -* Use threaded interrupt for rkisp1_stats_isr(), remove work queue. * Fix checkpatch errors. * Review and comment every lock * Handle quantizationdrivers/staging/media/rkisp1/rkisp1-common.h +2
-3@@ -190,8 +190,6 @@ struct rkisp1_stats {struct list_head stat;struct v4l2_format vdev_fmt;bool is_streaming; - -struct workqueue_struct *readout_wq; };/* @@ -308,10 +306,11 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx);irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1); -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris); +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);int rkisp1_capture_devs_register (struct rkisp1_device *rkisp1);drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0drivers/staging/media/rkisp1/rkisp1-dev.c irqreturn_t rkisp1_isr(int irq, void *ctx) · line 411
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +2
-0@@ -405,6 +405,8 @@ irqreturn_t rkisp1_isr(int irq, void *ctx) isp_ret =rkisp1_isp_isr (rkisp1); mipi_ret =rkisp1_mipi_isr (rkisp1); +if (isp_ret ==IRQ_WAKE_THREAD ) +return IRQ_WAKE_THREAD ;if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE )return IRQ_NONE ;Let me slightly modify my proposal from another patch in this series:
irqreturn_t ret = IRQ_NONE; ... ret |= rkisp1_capture_isr(rkisp1); ret |= rkisp1_isp_isr(rkisp1); ret |= rkisp1_mipi_isr(rkisp1); return ret & IRQ_WAKE_THREAD ? IRQ_WAKE_THREAD : ret;Up to you.
Quoted 27 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +4
-2@@ -490,8 +492,10 @@ static int rkisp1_probe(struct platform_device *pdev)if (irq <0 )return irq; - ret =devm_request_irq (dev, irq, rkisp1_isr,IRQF_SHARED , -dev_driver_string (dev), dev); + ret =devm_request_threaded_irq (dev, irq, rkisp1_isr, + rkisp1_read_stats_threaded_irq, +IRQF_SHARED , +dev_driver_string (dev), dev);if (ret) {dev_err (dev,"request irq failed: %d\n" , ret);return ret;drivers/staging/media/rkisp1/rkisp1-isp.c +1
-0@@ -1111,6 +1111,7 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; +irqreturn_t ret =IRQ_HANDLED ; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status)drivers/staging/media/rkisp1/rkisp1-isp.c irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1) · line 1142
Quoted 5 lines
drivers/staging/media/rkisp1/rkisp1-isp.c +1
-1@@ -1138,7 +1139,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS );if (isp_ris &RKISP1_STATS_MEAS_MASK ) -rkisp1_stats_isr (&rkisp1->stats, isp_ris); + ret =rkisp1_stats_isr (&rkisp1->stats, isp_ris);Overriding the ret variable isn't very nice, even if it should work in practice here as ret is already equal to
IRQ_HANDLEDandrkisp1_stats_isr()returnsIRQ_HANDLEDorIRQ_WAKE_THREAD, neverIRQ_NONE. You could however writeret |= rkisp1_stats_isr(&rkisp1->stats, isp_ris);as
IRQ_HANDLEDandIRQ_WAKE_THREADare independent bit flags to accumulate them, provided you returnIRQ_WAKE_THREADfromrkisp1_isr()when bothIRQ_WAKE_THREADandIRQ_HANDLEDare set (it seems the the core IRQ code can't deal with both being set).Quoted 139 lines
drivers/staging/media/rkisp1/rkisp1-isp.c +1
-1}/* @@ -1148,5 +1149,5 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)*/ rkisp1_params_isr (rkisp1, status); -return IRQ_HANDLED ; +return ret; }drivers/staging/media/rkisp1/rkisp1-stats.c +26
-47@@ -18,19 +18,9 @@#define RKISP1_ISP_STATS_REQ_BUFS_MIN 2#define RKISP1_ISP_STATS_REQ_BUFS_MAX 8 -enum rkisp1_isp_readout_cmd { -RKISP1_ISP_READOUT_MEAS , -RKISP1_ISP_READOUT_META , -}; - -struct rkisp1_isp_readout_work { -struct work_struct work; -struct rkisp1_stats *stats; - -unsigned int frame_id; -unsigned int isp_ris; -enum rkisp1_isp_readout_cmd readout; -struct vb2_buffer *vb; +struct rkisp1_kstats_buffer { +struct rkisp1_buffer buff; +u32 ris; }; static int rkisp1_stats_enum_fmt_meta_cap(struct file *file, void *priv,@@ -126,16 +116,17 @@ static int rkisp1_stats_vb2_queue_setup(struct vb2_queue *vq,static void rkisp1_stats_vb2_buf_queue (struct vb2_buffer *vb) {struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); -struct rkisp1_buffer *stats_buf = - container_of(vbuf, struct rkisp1_buffer, vb); +struct rkisp1_kstats_buffer *kstats_buf = + container_of(vbuf, struct rkisp1_kstats_buffer, buff.vb);struct vb2_queue *vq = vb->vb2_queue;struct rkisp1_stats *stats_dev = vq->drv_priv;unsigned long flags; - stats_buf->vaddr[0 ] =vb2_plane_vaddr (vb,0 ); + kstats_buf->buff.vaddr[0 ] =vb2_plane_vaddr (vb,0 ); + kstats_buf->ris =0 ;spin_lock_irqsave (&stats_dev->stats_lock, flags); -list_add_tail (&stats_buf->queue, &stats_dev->stat); +list_add_tail (&kstats_buf->buff.queue, &stats_dev->stat);spin_unlock_irqrestore (&stats_dev->stats_lock, flags); }@@ -152,25 +143,19 @@ static int rkisp1_stats_vb2_buf_prepare(struct vb2_buffer *vb)static void rkisp1_stats_vb2_stop_streaming (struct vb2_queue *vq) {struct rkisp1_stats *stats = vq->drv_priv; -struct rkisp1_buffer *buf; +struct rkisp1_kstats_buffer *buf;unsigned long flags;unsigned int i; -/* Make sure no new work queued in isr before draining wq */ spin_lock_irqsave (&stats->stats_lock, flags); stats->is_streaming = false; -spin_unlock_irqrestore (&stats->stats_lock, flags); - -drain_workqueue (stats->readout_wq); - -spin_lock_irqsave (&stats->stats_lock, flags);for (i =0 ; i <RKISP1_ISP_STATS_REQ_BUFS_MAX ; i++) {if (list_empty (&stats->stat))break ; buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&buf->queue); -vb2_buffer_done (&buf->vb.vb2_buf,VB2_BUF_STATE_ERROR ); + struct rkisp1_kstats_buffer, buff.queue); +list_del (&buf->buff.queue); +vb2_buffer_done (&buf->buff.vb.vb2_buf,VB2_BUF_STATE_ERROR ); }spin_unlock_irqrestore (&stats->stats_lock, flags); }@@ -207,7 +192,7 @@ rkisp1_stats_init_vb2_queue(struct vb2_queue *q, struct rkisp1_stats *stats) q->drv_priv = stats; q->ops = &rkisp1_stats_vb2_ops; q->mem_ops = &vb2_vmalloc_memops; - q->buf_struct_size =sizeof (struct rkisp1_buffer ); + q->buf_struct_size =sizeof (struct rkisp1_kstats_buffer ); q->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC ; q->lock = &node->vlock;@@ -325,85 +310,81 @@ static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, } } -static void -rkisp1_stats_send_measurement (struct rkisp1_stats *stats, -struct rkisp1_isp_readout_work *meas_work) +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx) { +struct device *dev = ctx; +struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +struct rkisp1_stats *stats = &rkisp1->stats; +struct rkisp1_kstats_buffer *kstats_buf =NULL ;struct rkisp1_stat_buffer *cur_stat_buf; -struct rkisp1_buffer *cur_buf =NULL ; -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence); -u64 timestamp =ktime_get_ns ();unsigned long flags; - -if (frame_sequence != meas_work->frame_id) { -dev_warn (stats->rkisp1->dev, -"Measurement late(%d, %d)\n" , - frame_sequence, meas_work->frame_id); - frame_sequence = meas_work->frame_id; - } +u64 timestamp =ktime_get_ns ();spin_lock_irqsave (&stats->stats_lock, flags); -/* get one empty buffer */ -if (!list_empty (&stats->stat)) { - cur_buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&cur_buf->queue); +if (!stats->is_streaming) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +return IRQ_HANDLED ; + } +if (list_empty (&stats->stat)) { +spin_unlock_irqrestore (&stats->stats_lock, flags); + WARN("%s: threaded irq waked but there are no buffers",drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 340
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +10
-0+ __func__); +return IRQ_HANDLED ; + } + kstats_buf =list_first_entry (&stats->stat, + struct rkisp1_kstats_buffer, buff.queue); + +/* +* each waked irq thread reads exactly one ready statistics +* so it is a bug if no statistics are ready +*/ What happens if this function takes too long to run ? With the workqueue, if the work gets delayed once in a while, the work items will accumulate, and all of them will be handled eventually. Here it seems that an IRQ could get lost.
Quoted 51 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +15
-12+if (!kstats_buf->ris) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +WARN ("%s: threaded irq waked but buffer holds no measures" , + __func__); +return IRQ_HANDLED ; } +list_del (&kstats_buf->buff.queue);spin_unlock_irqrestore (&stats->stats_lock, flags); -if (!cur_buf) -return ; - cur_stat_buf = - (struct rkisp1_stat_buffer *)(cur_buf->vaddr[0 ]); + (struct rkisp1_stat_buffer *)(kstats_buf->buff.vaddr[0 ]); -if (meas_work->isp_ris &RKISP1_CIF_ISP_AWB_DONE ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_AWB_DONE ) {rkisp1_stats_get_awb_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AWB ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_AFM_FIN ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_AFM_FIN ) {rkisp1_stats_get_afc_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AFM_FIN ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_EXP_END ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_EXP_END ) {rkisp1_stats_get_aec_meas (stats, cur_stat_buf);rkisp1_stats_get_bls_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AUTOEXP ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_HIST_MEASURE_RDY ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_HIST_MEASURE_RDY ) {rkisp1_stats_get_hst_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_HIST ; } -vb2_set_plane_payload (&cur_buf->vb.vb2_buf,0 , +vb2_set_plane_payload (&kstats_buf->buff.vb.vb2_buf,0 ,sizeof (struct rkisp1_stat_buffer )); - cur_buf->vb.sequence = frame_sequence; - cur_buf->vb.vb2_buf.timestamp = timestamp; -vb2_buffer_done (&cur_buf->vb.vb2_buf,VB2_BUF_STATE_DONE ); + kstats_buf->buff.vb.vb2_buf.timestamp = timestamp; +vb2_buffer_done (&kstats_buf->buff.vb.vb2_buf,VB2_BUF_STATE_DONE ); +return IRQ_HANDLED ; }drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 381
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +0
-8-static void rkisp1_stats_readout_work (struct work_struct *work) -{ -struct rkisp1_isp_readout_work *readout_work = - container_of(work, struct rkisp1_isp_readout_work, work); -struct rkisp1_stats *stats = readout_work->stats; - -if (readout_work->readout ==RKISP1_ISP_READOUT_MEAS ) -rkisp1_stats_send_measurement (stats, readout_work);Is the second blank line intentional ?
Quoted 76 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +14
-34-kfree (readout_work); -} - -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris) +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris) { -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence);struct rkisp1_device *rkisp1 = stats->rkisp1; -struct rkisp1_isp_readout_work *work; +struct rkisp1_isp *isp = &rkisp1->isp; +struct rkisp1_kstats_buffer *buf =NULL ; +irqreturn_t ret =IRQ_HANDLED ;unsigned int isp_mis_tmp =0 ;unsigned long flags;u32 val;@@ -417,28 +398,22 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)if (isp_mis_tmp &RKISP1_STATS_MEAS_MASK ) rkisp1->debug.stats_error++; -if (!stats->is_streaming) +if (!stats->is_streaming || !(isp_ris &RKISP1_STATS_MEAS_MASK )) goto unlock; -if (isp_ris &RKISP1_STATS_MEAS_MASK ) { - work =kzalloc (sizeof (*work),GFP_ATOMIC ); -if (work) { -INIT_WORK (&work->work, - rkisp1_stats_readout_work); - work->readout =RKISP1_ISP_READOUT_MEAS ; - work->stats = stats; - work->frame_id = frame_sequence; - work->isp_ris = isp_ris; -if (!queue_work (stats->readout_wq, - &work->work)) -kfree (work); - }else { -dev_err (stats->rkisp1->dev, -"Could not allocate work\n" ); + +list_for_each_entry (buf, &stats->stat, buff.queue) { +if (!buf->ris) { + buf->buff.vb.sequence = +atomic_read (&isp->frame_sequence); + buf->ris = isp_ris; + ret =IRQ_WAKE_THREAD ; +break ; } } unlock:spin_unlock_irqrestore (&stats->stats_lock, flags); +return ret; }static void rkisp1_init_stats (struct rkisp1_stats *stats)@@ -489,19 +464,8 @@ int rkisp1_stats_register(struct rkisp1_stats *stats, goto err_cleanup_media_entity; } - stats->readout_wq =alloc_workqueue ("measurement_queue" , -WQ_UNBOUND |WQ_MEM_RECLAIM , -1 ); - -if (!stats->readout_wq) { - ret = -ENOMEM ; - goto err_unreg_vdev; - } -return 0 ; -err_unreg_vdev: -video_unregister_device (vdev); err_cleanup_media_entity:media_entity_cleanup (&vdev->entity); err_release_queue:drivers/staging/media/rkisp1/rkisp1-stats.c void rkisp1_stats_unregister(struct rkisp1_stats *stats) · line 484
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +0
-1@@ -515,7 +479,6 @@ void rkisp1_stats_unregister(struct rkisp1_stats *stats)struct rkisp1_vdev_node *node = &stats->vnode;struct video_device *vdev = &node->vdev; -destroy_workqueue (stats->readout_wq);video_unregister_device (vdev);media_entity_cleanup (&vdev->entity);vb2_queue_release (vdev->queue);--
Regards,Laurent Pinchart
Re: [PATCH 5/5] media: staging: rkisp1: replace workqueue with threaded irq for reading statistics registers
Hi, Tomasz, Laurent, and everyone
Quoted 22 lines · Tomasz Figa
Hi Dafna,
Quoted 12 lines · Laurent Pinchart
Hi Dafna,
Thank you for the patch.
Quoted 5 lines · Dafna Hirschfeld
Reading the statistics registers might take too long to run inside the irq handler. Currently it is deferred to bottom half using workqueues. This patch replaces the workqueue with threaded interrupts for reading the statistics registers.
Quoted 5 lines
Could you please explain why this is needed/desired ? Removal of the
kzalloc(GFP_ATOMIC)is very nice, but I'm sure there would have been ways to avoid it while keeping a workqueue. I'm not claiming the patch is bad, but I'd like to understand the reason.
Quoted 5 lines
Thanks a lot for working on this driver!
I'll second what Laurent said. In general, a description of the patch should explain why a change is needed, e.g. what issues it fixes or what improvements it brings.
So from what I understand, the correct way to handle bottom half is using threaded interrupts since they run in higher priority.
In case of this statistics reading for example, we want to read the stats as fast as possible once we recieve the interrupt. If we read the stats too long after the interrupt was recieved, then the values in the stats registerd might have changed by then. Does that make sense?
I use the rockpi4 board to test the code and actually I did an experiment of moving the code that reads the stats into the hard irq and it ran fine. Maybe you know why the code is currently in a work queue and not inside the hard irq? Also, the params struct is of size 5337 bytes and the params subdevice can potentionaly run a lot of registers writing but for some reasone the code of writing to the params registers still runs inside the hard irq.
Quoted 4 lines
Also, would you mind adding me on CC for any patches for this driver? Since we use this driver in Chrome OS, I'd like to stay on top of any updates. Thanks in advance!
sure!
Quoted 282 lines
Best regards,
TomaszQuoted 278 lines
Quoted 58 lines
A new struct type '
rkisp1_kstats_buffer' is used as the statistics buffers. The struct has a field 'ris' which is the flags of ready statistics. If new statistics are ready, the irq handler sets this variable and the frame sequence on the next available buffer and returnsIRQ_WAKE_THREAD.
Then the threaded interrupt reads the registers and callsvb2_buffer_done.Signed-off-by Dafna Hirschfeld <[email protected]>--- drivers/staging/media/rkisp1/TODO | 1 - drivers/staging/media/rkisp1/rkisp1-common.h | 5 +- drivers/staging/media/rkisp1/rkisp1-dev.c | 8 +- drivers/staging/media/rkisp1/rkisp1-isp.c | 5 +- drivers/staging/media/rkisp1/rkisp1-stats.c | 167 ++++++++----------- 5 files changed, 76 insertions(+), 110 deletions(-)drivers/staging/media/rkisp1/TODO +0
-1@@ -1,5 +1,4 @@ * Fix pad format size for statistics and parameters entities. -* Use threaded interrupt for rkisp1_stats_isr(), remove work queue. * Fix checkpatch errors. * Review and comment every lock * Handle quantizationdrivers/staging/media/rkisp1/rkisp1-common.h +2
-3@@ -190,8 +190,6 @@ struct rkisp1_stats {struct list_head stat;struct v4l2_format vdev_fmt;bool is_streaming; - -struct workqueue_struct *readout_wq; };/* @@ -308,10 +306,11 @@ void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get (u32 mbus_code); +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx);irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_mipi_isr (struct rkisp1_device *rkisp1);irqreturn_t rkisp1_capture_isr (struct rkisp1_device *rkisp1); -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris); +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris);void rkisp1_params_isr (struct rkisp1_device *rkisp1,u32 isp_mis);int rkisp1_capture_devs_register (struct rkisp1_device *rkisp1);drivers/staging/media/rkisp1/rkisp1-dev.c +0
-0drivers/staging/media/rkisp1/rkisp1-dev.c irqreturn_t rkisp1_isr(int irq, void *ctx) · line 411
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +2
-0@@ -405,6 +405,8 @@ irqreturn_t rkisp1_isr(int irq, void *ctx) isp_ret =rkisp1_isp_isr (rkisp1); mipi_ret =rkisp1_mipi_isr (rkisp1); +if (isp_ret ==IRQ_WAKE_THREAD ) +return IRQ_WAKE_THREAD ;if (isp_ret ==IRQ_NONE && cap_ret ==IRQ_NONE && mipi_ret ==IRQ_NONE )return IRQ_NONE ;Let me slightly modify my proposal from another patch in this series:
irqreturn_t ret = IRQ_NONE; ... ret |= rkisp1_capture_isr(rkisp1); ret |= rkisp1_isp_isr(rkisp1); ret |= rkisp1_mipi_isr(rkisp1); return ret & IRQ_WAKE_THREAD ? IRQ_WAKE_THREAD : ret;Up to you.
Quoted 27 lines
drivers/staging/media/rkisp1/rkisp1-dev.c +4
-2@@ -490,8 +492,10 @@ static int rkisp1_probe(struct platform_device *pdev)if (irq <0 )return irq; - ret =devm_request_irq (dev, irq, rkisp1_isr,IRQF_SHARED , -dev_driver_string (dev), dev); + ret =devm_request_threaded_irq (dev, irq, rkisp1_isr, + rkisp1_read_stats_threaded_irq, +IRQF_SHARED , +dev_driver_string (dev), dev);if (ret) {dev_err (dev,"request irq failed: %d\n" , ret);return ret;drivers/staging/media/rkisp1/rkisp1-isp.c +1
-0@@ -1111,6 +1111,7 @@ static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)irqreturn_t rkisp1_isp_isr (struct rkisp1_device *rkisp1) {u32 status, isp_err; +irqreturn_t ret =IRQ_HANDLED ; status =rkisp1_read (rkisp1,RKISP1_CIF_ISP_MIS );if (!status)drivers/staging/media/rkisp1/rkisp1-isp.c irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1) · line 1142
Quoted 5 lines
drivers/staging/media/rkisp1/rkisp1-isp.c +1
-1@@ -1138,7 +1139,7 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)/* New frame from the sensor received */ isp_ris =rkisp1_read (rkisp1,RKISP1_CIF_ISP_RIS );if (isp_ris &RKISP1_STATS_MEAS_MASK ) -rkisp1_stats_isr (&rkisp1->stats, isp_ris); + ret =rkisp1_stats_isr (&rkisp1->stats, isp_ris);Overriding the ret variable isn't very nice, even if it should work in practice here as ret is already equal to
IRQ_HANDLEDandrkisp1_stats_isr()returnsIRQ_HANDLEDorIRQ_WAKE_THREAD, neverIRQ_NONE. You could however writeret |= rkisp1_stats_isr(&rkisp1->stats, isp_ris);as
IRQ_HANDLEDandIRQ_WAKE_THREADare independent bit flags to accumulate them, provided you returnIRQ_WAKE_THREADfromrkisp1_isr()when bothIRQ_WAKE_THREADandIRQ_HANDLEDare set (it seems the the core IRQ code can't deal with both being set).Quoted 139 lines
drivers/staging/media/rkisp1/rkisp1-isp.c +1
-1}/* @@ -1148,5 +1149,5 @@ irqreturn_t rkisp1_isp_isr(struct rkisp1_device *rkisp1)*/ rkisp1_params_isr (rkisp1, status); -return IRQ_HANDLED ; +return ret; }drivers/staging/media/rkisp1/rkisp1-stats.c +26
-47@@ -18,19 +18,9 @@#define RKISP1_ISP_STATS_REQ_BUFS_MIN 2#define RKISP1_ISP_STATS_REQ_BUFS_MAX 8 -enum rkisp1_isp_readout_cmd { -RKISP1_ISP_READOUT_MEAS , -RKISP1_ISP_READOUT_META , -}; - -struct rkisp1_isp_readout_work { -struct work_struct work; -struct rkisp1_stats *stats; - -unsigned int frame_id; -unsigned int isp_ris; -enum rkisp1_isp_readout_cmd readout; -struct vb2_buffer *vb; +struct rkisp1_kstats_buffer { +struct rkisp1_buffer buff; +u32 ris; }; static int rkisp1_stats_enum_fmt_meta_cap(struct file *file, void *priv,@@ -126,16 +116,17 @@ static int rkisp1_stats_vb2_queue_setup(struct vb2_queue *vq,static void rkisp1_stats_vb2_buf_queue (struct vb2_buffer *vb) {struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); -struct rkisp1_buffer *stats_buf = - container_of(vbuf, struct rkisp1_buffer, vb); +struct rkisp1_kstats_buffer *kstats_buf = + container_of(vbuf, struct rkisp1_kstats_buffer, buff.vb);struct vb2_queue *vq = vb->vb2_queue;struct rkisp1_stats *stats_dev = vq->drv_priv;unsigned long flags; - stats_buf->vaddr[0 ] =vb2_plane_vaddr (vb,0 ); + kstats_buf->buff.vaddr[0 ] =vb2_plane_vaddr (vb,0 ); + kstats_buf->ris =0 ;spin_lock_irqsave (&stats_dev->stats_lock, flags); -list_add_tail (&stats_buf->queue, &stats_dev->stat); +list_add_tail (&kstats_buf->buff.queue, &stats_dev->stat);spin_unlock_irqrestore (&stats_dev->stats_lock, flags); }@@ -152,25 +143,19 @@ static int rkisp1_stats_vb2_buf_prepare(struct vb2_buffer *vb)static void rkisp1_stats_vb2_stop_streaming (struct vb2_queue *vq) {struct rkisp1_stats *stats = vq->drv_priv; -struct rkisp1_buffer *buf; +struct rkisp1_kstats_buffer *buf;unsigned long flags;unsigned int i; -/* Make sure no new work queued in isr before draining wq */ spin_lock_irqsave (&stats->stats_lock, flags); stats->is_streaming = false; -spin_unlock_irqrestore (&stats->stats_lock, flags); - -drain_workqueue (stats->readout_wq); - -spin_lock_irqsave (&stats->stats_lock, flags);for (i =0 ; i <RKISP1_ISP_STATS_REQ_BUFS_MAX ; i++) {if (list_empty (&stats->stat))break ; buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&buf->queue); -vb2_buffer_done (&buf->vb.vb2_buf,VB2_BUF_STATE_ERROR ); + struct rkisp1_kstats_buffer, buff.queue); +list_del (&buf->buff.queue); +vb2_buffer_done (&buf->buff.vb.vb2_buf,VB2_BUF_STATE_ERROR ); }spin_unlock_irqrestore (&stats->stats_lock, flags); }@@ -207,7 +192,7 @@ rkisp1_stats_init_vb2_queue(struct vb2_queue *q, struct rkisp1_stats *stats) q->drv_priv = stats; q->ops = &rkisp1_stats_vb2_ops; q->mem_ops = &vb2_vmalloc_memops; - q->buf_struct_size =sizeof (struct rkisp1_buffer ); + q->buf_struct_size =sizeof (struct rkisp1_kstats_buffer ); q->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC ; q->lock = &node->vlock;@@ -325,85 +310,81 @@ static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, } } -static void -rkisp1_stats_send_measurement (struct rkisp1_stats *stats, -struct rkisp1_isp_readout_work *meas_work) +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx) { +struct device *dev = ctx; +struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +struct rkisp1_stats *stats = &rkisp1->stats; +struct rkisp1_kstats_buffer *kstats_buf =NULL ;struct rkisp1_stat_buffer *cur_stat_buf; -struct rkisp1_buffer *cur_buf =NULL ; -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence); -u64 timestamp =ktime_get_ns ();unsigned long flags; - -if (frame_sequence != meas_work->frame_id) { -dev_warn (stats->rkisp1->dev, -"Measurement late(%d, %d)\n" , - frame_sequence, meas_work->frame_id); - frame_sequence = meas_work->frame_id; - } +u64 timestamp =ktime_get_ns ();spin_lock_irqsave (&stats->stats_lock, flags); -/* get one empty buffer */ -if (!list_empty (&stats->stat)) { - cur_buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&cur_buf->queue); +if (!stats->is_streaming) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +return IRQ_HANDLED ; + } +if (list_empty (&stats->stat)) { +spin_unlock_irqrestore (&stats->stats_lock, flags); + WARN("%s: threaded irq waked but there are no buffers",drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 340
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +10
-0+ __func__); +return IRQ_HANDLED ; + } + kstats_buf =list_first_entry (&stats->stat, + struct rkisp1_kstats_buffer, buff.queue); + +/* +* each waked irq thread reads exactly one ready statistics +* so it is a bug if no statistics are ready +*/ What happens if this function takes too long to run ? With the workqueue, if the work gets delayed once in a while, the work items will accumulate, and all of them will be handled eventually. Here it seems that an IRQ could get lost.
If the irs runs and there are no available buffers then indeed we lost the interrupt. You think it might be a problem if userspace expect an associated stats buffer for every recieved frame?
Also with workqueue we will lose the stats if we don't have an available buffer when the workqueue runs.
Thanks,
Dafna
Quoted 153 lines
Quoted 153 lines
Quoted 52 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +15
-12+if (!kstats_buf->ris) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +WARN ("%s: threaded irq waked but buffer holds no measures" , + __func__); +return IRQ_HANDLED ; } +list_del (&kstats_buf->buff.queue);spin_unlock_irqrestore (&stats->stats_lock, flags); -if (!cur_buf) -return ; - cur_stat_buf = - (struct rkisp1_stat_buffer *)(cur_buf->vaddr[0 ]); + (struct rkisp1_stat_buffer *)(kstats_buf->buff.vaddr[0 ]); -if (meas_work->isp_ris &RKISP1_CIF_ISP_AWB_DONE ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_AWB_DONE ) {rkisp1_stats_get_awb_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AWB ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_AFM_FIN ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_AFM_FIN ) {rkisp1_stats_get_afc_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AFM_FIN ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_EXP_END ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_EXP_END ) {rkisp1_stats_get_aec_meas (stats, cur_stat_buf);rkisp1_stats_get_bls_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_AUTOEXP ; } -if (meas_work->isp_ris &RKISP1_CIF_ISP_HIST_MEASURE_RDY ) { +if (kstats_buf->ris &RKISP1_CIF_ISP_HIST_MEASURE_RDY ) {rkisp1_stats_get_hst_meas (stats, cur_stat_buf); cur_stat_buf->meas_type |=RKISP1_CIF_ISP_STAT_HIST ; } -vb2_set_plane_payload (&cur_buf->vb.vb2_buf,0 , +vb2_set_plane_payload (&kstats_buf->buff.vb.vb2_buf,0 ,sizeof (struct rkisp1_stat_buffer )); - cur_buf->vb.sequence = frame_sequence; - cur_buf->vb.vb2_buf.timestamp = timestamp; -vb2_buffer_done (&cur_buf->vb.vb2_buf,VB2_BUF_STATE_DONE ); + kstats_buf->buff.vb.vb2_buf.timestamp = timestamp; +vb2_buffer_done (&kstats_buf->buff.vb.vb2_buf,VB2_BUF_STATE_DONE ); +return IRQ_HANDLED ; }drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 381
Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +0
-8-static void rkisp1_stats_readout_work (struct work_struct *work) -{ -struct rkisp1_isp_readout_work *readout_work = - container_of(work, struct rkisp1_isp_readout_work, work); -struct rkisp1_stats *stats = readout_work->stats; - -if (readout_work->readout ==RKISP1_ISP_READOUT_MEAS ) -rkisp1_stats_send_measurement (stats, readout_work);Is the second blank line intentional ?
Quoted 76 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +14
-34-kfree (readout_work); -} - -void rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris) +irqreturn_t rkisp1_stats_isr (struct rkisp1_stats *stats,u32 isp_ris) { -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence);struct rkisp1_device *rkisp1 = stats->rkisp1; -struct rkisp1_isp_readout_work *work; +struct rkisp1_isp *isp = &rkisp1->isp; +struct rkisp1_kstats_buffer *buf =NULL ; +irqreturn_t ret =IRQ_HANDLED ;unsigned int isp_mis_tmp =0 ;unsigned long flags;u32 val;@@ -417,28 +398,22 @@ void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)if (isp_mis_tmp &RKISP1_STATS_MEAS_MASK ) rkisp1->debug.stats_error++; -if (!stats->is_streaming) +if (!stats->is_streaming || !(isp_ris &RKISP1_STATS_MEAS_MASK )) goto unlock; -if (isp_ris &RKISP1_STATS_MEAS_MASK ) { - work =kzalloc (sizeof (*work),GFP_ATOMIC ); -if (work) { -INIT_WORK (&work->work, - rkisp1_stats_readout_work); - work->readout =RKISP1_ISP_READOUT_MEAS ; - work->stats = stats; - work->frame_id = frame_sequence; - work->isp_ris = isp_ris; -if (!queue_work (stats->readout_wq, - &work->work)) -kfree (work); - }else { -dev_err (stats->rkisp1->dev, -"Could not allocate work\n" ); + +list_for_each_entry (buf, &stats->stat, buff.queue) { +if (!buf->ris) { + buf->buff.vb.sequence = +atomic_read (&isp->frame_sequence); + buf->ris = isp_ris; + ret =IRQ_WAKE_THREAD ; +break ; } } unlock:spin_unlock_irqrestore (&stats->stats_lock, flags); +return ret; }static void rkisp1_init_stats (struct rkisp1_stats *stats)@@ -489,19 +464,8 @@ int rkisp1_stats_register(struct rkisp1_stats *stats, goto err_cleanup_media_entity; } - stats->readout_wq =alloc_workqueue ("measurement_queue" , -WQ_UNBOUND |WQ_MEM_RECLAIM , -1 ); - -if (!stats->readout_wq) { - ret = -ENOMEM ; - goto err_unreg_vdev; - } -return 0 ; -err_unreg_vdev: -video_unregister_device (vdev); err_cleanup_media_entity:media_entity_cleanup (&vdev->entity); err_release_queue:drivers/staging/media/rkisp1/rkisp1-stats.c void rkisp1_stats_unregister(struct rkisp1_stats *stats) · line 484
Quoted 7 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +0
-1@@ -515,7 +479,6 @@ void rkisp1_stats_unregister(struct rkisp1_stats *stats)struct rkisp1_vdev_node *node = &stats->vnode;struct video_device *vdev = &node->vdev; -destroy_workqueue (stats->readout_wq);video_unregister_device (vdev);media_entity_cleanup (&vdev->entity);vb2_queue_release (vdev->queue);--
Regards,Laurent Pinchart
Re: [PATCH 5/5] media: staging: rkisp1: replace workqueue with threaded irq for reading statistics registers
Quoted 39 lines · Dafna Hirschfeld
Hi, Tomasz, Laurent, and everyone
Quoted 22 lines · Tomasz Figa
Hi Dafna,
Quoted 12 lines · Laurent Pinchart
Hi Dafna,
Thank you for the patch.
Quoted 5 lines · Dafna Hirschfeld
Reading the statistics registers might take too long to run inside the irq handler. Currently it is deferred to bottom half using workqueues. This patch replaces the workqueue with threaded interrupts for reading the statistics registers.
Quoted 5 lines
Could you please explain why this is needed/desired ? Removal of the
kzalloc(GFP_ATOMIC)is very nice, but I'm sure there would have been ways to avoid it while keeping a workqueue. I'm not claiming the patch is bad, but I'd like to understand the reason.Quoted 5 lines
Thanks a lot for working on this driver!
I'll second what Laurent said. In general, a description of the patch should explain why a change is needed, e.g. what issues it fixes or what improvements it brings.
So from what I understand, the correct way to handle bottom half is using threaded interrupts since they run in higher priority.
In case of this statistics reading for example, we want to read the stats as fast as possible once we recieve the interrupt. If we read the stats too long after the interrupt was recieved, then the values in the stats registerd might have changed by then. Does that make sense?
Quoted 8 lines
I use the rockpi4 board to test the code and actually I did an experiment of moving the code that reads the stats into the hard irq and it ran fine. Maybe you know why the code is currently in a work queue and not inside the hard irq? Also, the params struct is of size 5337 bytes and the params subdevice can potentionaly run a lot of registers writing but for some reasone the code of writing to the params registers still runs inside the hard irq.
Actually I now recall that we already discussed this in the past with Ezequiel and Helen and I even measured the time needed to readout the statistics registers and it was consistently taking between 23 to 40 microseconds. We concluded that it could be just done in the hardirq handler.
I've added you to that email thread.
Quoted 6 lines
Quoted 4 lines
Also, would you mind adding me on CC for any patches for this driver? Since we use this driver in Chrome OS, I'd like to stay on top of any updates. Thanks in advance!
sure!
Thanks!
[snip]
Quoted 53 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +20
-19-static void -rkisp1_stats_send_measurement (struct rkisp1_stats *stats, -struct rkisp1_isp_readout_work *meas_work) +irqreturn_t rkisp1_read_stats_threaded_irq (int irq,void *ctx) { +struct device *dev = ctx; +struct rkisp1_device *rkisp1 =dev_get_drvdata (dev); +struct rkisp1_stats *stats = &rkisp1->stats; +struct rkisp1_kstats_buffer *kstats_buf =NULL ;struct rkisp1_stat_buffer *cur_stat_buf; -struct rkisp1_buffer *cur_buf =NULL ; -unsigned int frame_sequence = -atomic_read (&stats->rkisp1->isp.frame_sequence); -u64 timestamp =ktime_get_ns ();unsigned long flags; - -if (frame_sequence != meas_work->frame_id) { -dev_warn (stats->rkisp1->dev, -"Measurement late(%d, %d)\n" , - frame_sequence, meas_work->frame_id); - frame_sequence = meas_work->frame_id; - } +u64 timestamp =ktime_get_ns ();spin_lock_irqsave (&stats->stats_lock, flags); -/* get one empty buffer */ -if (!list_empty (&stats->stat)) { - cur_buf =list_first_entry (&stats->stat, - struct rkisp1_buffer, queue); -list_del (&cur_buf->queue); +if (!stats->is_streaming) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +return IRQ_HANDLED ; + } +if (list_empty (&stats->stat)) { +spin_unlock_irqrestore (&stats->stats_lock, flags); +WARN ("%s: threaded irq waked but there are no buffers" , + __func__); +return IRQ_HANDLED ; + } + kstats_buf =list_first_entry (&stats->stat, + struct rkisp1_kstats_buffer, buff.queue); + +/* Quoted 8 lines
Quoted 8 lines
drivers/staging/media/rkisp1/rkisp1-stats.c static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats, · line 340
Quoted 3 lines
drivers/staging/media/rkisp1/rkisp1-stats.c +3
-0+* each waked irq thread reads exactly one ready statistics +* so it is a bug if no statistics are ready +*/ What happens if this function takes too long to run ? With the workqueue, if the work gets delayed once in a while, the work items will accumulate, and all of them will be handled eventually. Here it seems that an IRQ could get lost.
Quoted 5 lines
If the irs runs and there are no available buffers then indeed we lost the interrupt. You think it might be a problem if userspace expect an associated stats buffer for every recieved frame?
Also with workqueue we will lose the stats if we don't have an available buffer when the workqueue runs.
I believe this hardware doesn't do any buffering internally, so if one doesn't read the statistics in time, the values are lost, because they are overridden with the ones coming from the next frame. So actually hardirq might be a better context to do the read-out indeed.
Best regards,
Tomasz