Skip to contentSkip to conversation

add IPU3 CIO2 CSI2 driver

PATCH Superseded Yong Zhi 6 participants 17 messages Jun 7, 2017 – Oct 6, 2017

Subsystems (3)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Yong Zhi

author maintainer
#0 lore raw

v2 cover add IPU3 CIO2 CSI2 driver

This patch adds the driver for the CIO2 device found in some the Skylake and Kaby Kake SoCs. The CIO2 consists of four D-PHY receivers.

The CIO2 driver exposes V4L2, V4L2 sub-device and Media controller interfaces to the user space.

===========
= history =
===========
version 2:
- remove all explicit DMA flush operations
- change dma_free_noncoherent() to dma_free_coherent()
- remove cio2_hw_mipi_lanes()
- replace v4l2_g_ext_ctrls() with v4l2_ctrl_g_ctrl()
in cio2_csi2_calc_timing().
- use ffs() to iterate the port_status in cio2_irq()
- add static inline file_to_cio2_queue() function
- comment dma_wmb(), cio2_rx_timing() and few other places
- use ktime_get_ns() for vb2_buf.timestamp in cio2_buffer_done()
- use of SET_RUNTIME_PM_OPS() macro for cio2_pm_ops
- use BIT() macro for bit difinitions
- remove un-used macros such as CIO2_QUEUE_WIDTH() in ipu3-cio2.h
- move the MODULE_AUTHOR() to the end of the file
- change file path to drivers/media/pci/intel/ipu3

version 1:
- Initial submission
Yong Zhi (3):
[media] videodev2.h, v4l2-ioctl: add IPU3 raw10 color format [media] doc-rst: add IPU3 raw10 bayer pixel format definitions [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

 Documentation/media/uapi/v4l/pixfmt-rgb.rst        |    1 +
 .../media/uapi/v4l/pixfmt-srggb10-ipu3.rst         |   62 +
 drivers/media/pci/Kconfig                          |    2 +
 drivers/media/pci/Makefile                         |    3 +-
 drivers/media/pci/intel/Makefile                   |    5 +
 drivers/media/pci/intel/ipu3/Kconfig               |   17 +
 drivers/media/pci/intel/ipu3/Makefile              |    1 +
 drivers/media/pci/intel/ipu3/ipu3-cio2.c           | 1788 ++++++++++++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h           |  424 +++++
 drivers/media/v4l2-core/v4l2-ioctl.c               |    4 +
 include/uapi/linux/videodev2.h                     |    5 +
 11 files changed, 2311 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-srggb10-ipu3.rst
 create mode 100644 drivers/media/pci/intel/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/Kconfig
 create mode 100644 drivers/media/pci/intel/ipu3/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.c
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.h
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v2 1/3 videodev2.h, v4l2-ioctl: add IPU3 raw10 color format

Add IPU3 specific formats:

	V4L2_PIX_FMT_IPU3_SBGGR10
	V4L2_PIX_FMT_IPU3_SGBRG10
	V4L2_PIX_FMT_IPU3_SGRBG10
	V4L2_PIX_FMT_IPU3_SRGGB10
---
 drivers/media/v4l2-core/v4l2-ioctl.c | 4 ++++
 include/uapi/linux/videodev2.h       | 5 +++++
 2 files changed, 9 insertions(+)
drivers/media/v4l2-core/v4l2-ioctl.c +4 -0
index e5a2187..fb1387f 100644
@@ -1202,6 +1202,10 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
 	case V4L2_PIX_FMT_SGBRG10P:	descr = "10-bit Bayer GBGB/RGRG Packed"; break;
 	case V4L2_PIX_FMT_SGRBG10P:	descr = "10-bit Bayer GRGR/BGBG Packed"; break;
 	case V4L2_PIX_FMT_SRGGB10P:	descr = "10-bit Bayer RGRG/GBGB Packed"; break;
+	case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
+	case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
+	case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
+	case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
 	case V4L2_PIX_FMT_SBGGR10ALAW8:	descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
 	case V4L2_PIX_FMT_SGBRG10ALAW8:	descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
 	case V4L2_PIX_FMT_SGRBG10ALAW8:	descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
include/uapi/linux/videodev2.h +5 -0
index 2b8feb8..7bfa6ad 100644
@@ -663,6 +663,11 @@ struct v4l2_pix_format {
 #define V4L2_PIX_FMT_MT21C    v4l2_fourcc('M', 'T', '2', '1') /* Mediatek compressed block mode  */
 #define V4L2_PIX_FMT_INZI     v4l2_fourcc('I', 'N', 'Z', 'I') /* Intel Planar Greyscale 10-bit and Depth 16-bit */
 
+#define V4L2_PIX_FMT_IPU3_SBGGR10	v4l2_fourcc('i', 'p', '3', 'b') /* IPU3 packed 10-bit BGGR bayer */
+#define V4L2_PIX_FMT_IPU3_SGBRG10	v4l2_fourcc('i', 'p', '3', 'g') /* IPU3 packed 10-bit GBRG bayer */
+#define V4L2_PIX_FMT_IPU3_SGRBG10	v4l2_fourcc('i', 'p', '3', 'G') /* IPU3 packed 10-bit GRBG bayer */
+#define V4L2_PIX_FMT_IPU3_SRGGB10	v4l2_fourcc('i', 'p', '3', 'r') /* IPU3 packed 10-bit RGGB bayer */
+
 /* SDR formats - used only for Software Defined Radio devices */
 #define V4L2_SDR_FMT_CU8          v4l2_fourcc('C', 'U', '0', '8') /* IQ u8 */
 #define V4L2_SDR_FMT_CU16LE       v4l2_fourcc('C', 'U', '1', '6') /* IQ u16le */
Signature · 1 line
2.7.4
author maintainer
#2 lore raw

PATCH v2 2/3 doc-rst: add IPU3 raw10 bayer pixel format definitions

In reply to Yong Zhi

The formats added by this patch are:

    V4L2_PIX_FMT_IPU3_SBGGR10
    V4L2_PIX_FMT_IPU3_SGBRG10
    V4L2_PIX_FMT_IPU3_SGRBG10
    V4L2_PIX_FMT_IPU3_SRGGB10
---
 Documentation/media/uapi/v4l/pixfmt-rgb.rst        |  1 +
 .../media/uapi/v4l/pixfmt-srggb10-ipu3.rst         | 62 ++++++++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 Documentation/media/uapi/v4l/pixfmt-srggb10-ipu3.rst
Documentation/media/uapi/v4l/pixfmt-rgb.rst +1 -0
index b0f3513..6900d5c 100644
@@ -16,5 +16,6 @@ RGB Formats
     pixfmt-srggb10p
     pixfmt-srggb10alaw8
     pixfmt-srggb10dpcm8
+    pixfmt-srggb10-ipu3
     pixfmt-srggb12
     pixfmt-srggb16
Documentation/media/uapi/v4l/pixfmt-srggb10-ipu3.rst new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH v2 2/3] [media] doc-rst: add IPU3 raw10 bayer pixel format definitions

Documentation/media/uapi/v4l/pixfmt-srggb10-ipu3.rst

Quoted 7 lines
Documentation/media/uapi/v4l/pixfmt-srggb10-ipu3.rst new file +7 -0
+
+10-bit Bayer formats
+
+Description
+===========
+
+These four pixel formats are used by Intel IPU3 driver,

Are the same formats present in IPUv2, will they ever be present in other hardware.

If so (and I think it is so...) then it's not a good idea to encode ipu3 in the name. Something like V4l2_PIX_FMT_SBGGR10_PACKED might be better ?

Alan

author maintainer
#4 lore raw

PATCH v2 3/3 intel-ipu3: cio2: Add new MIPI-CSI2 driver

In reply to Yong Zhi

This patch adds CIO2 CSI-2 device driver for
Intel's IPU3 camera sub-system support.

---
 drivers/media/pci/Kconfig                |    2 +
 drivers/media/pci/Makefile               |    3 +-
 drivers/media/pci/intel/Makefile         |    5 +
 drivers/media/pci/intel/ipu3/Kconfig     |   17 +
 drivers/media/pci/intel/ipu3/Makefile    |    1 +
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 1788 ++++++++++++++++++++++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  424 +++++++
 7 files changed, 2239 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/pci/intel/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/Kconfig
 create mode 100644 drivers/media/pci/intel/ipu3/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.c
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.h
drivers/media/pci/Kconfig +2 -0
index da28e68..5932e22 100644
@@ -54,5 +54,7 @@ source "drivers/media/pci/smipcie/Kconfig"
 source "drivers/media/pci/netup_unidvb/Kconfig"
 endif
 
+source "drivers/media/pci/intel/ipu3/Kconfig"
+
 endif #MEDIA_PCI_SUPPORT
 endif #PCI
drivers/media/pci/Makefile +2 -1
index a7e8af0..d8f9843 100644
@@ -13,7 +13,8 @@ obj-y        +=	ttpci/		\
 		ddbridge/	\
 		saa7146/	\
 		smipcie/	\
-		netup_unidvb/
+		netup_unidvb/	\
+		intel/
 
 obj-$(CONFIG_VIDEO_IVTV) += ivtv/
 obj-$(CONFIG_VIDEO_ZORAN) += zoran/
drivers/media/pci/intel/Makefile new file +5 -0
new file mode 100644
index 0000000..745c8b2
@@ -0,0 +1,5 @@
+#
+# Makefile for the IPU3 cio2 and ImGU drivers
+#
+
+obj-y	+= ipu3/
drivers/media/pci/intel/ipu3/Kconfig new file +17 -0
new file mode 100644
index 0000000..2a895d6
@@ -0,0 +1,17 @@
+config VIDEO_IPU3_CIO2
+	tristate "Intel ipu3-cio2 driver"
+	depends on VIDEO_V4L2 && PCI
+	depends on MEDIA_CONTROLLER
+	depends on HAS_DMA
+	depends on ACPI
+	select V4L2_FWNODE
+	select VIDEOBUF2_DMA_SG
+
+	---help---
+	This is the Intel IPU3 CIO2 CSI-2 receiver unit, found in Intel
+	Skylake and Kaby Lake SoCs and used for capturing images and
+	video from a camera sensor.
+
+	Say Y or M here if you have a Skylake/Kaby Lake SoC with MIPI CSI-2
+	connected camera.
+	The module will be called ipu3-cio2.
drivers/media/pci/intel/ipu3/Makefile new file +1 -0
new file mode 100644
index 0000000..20186e3
@@ -0,0 +1 @@
+obj-$(CONFIG_VIDEO_IPU3_CIO2) += ipu3-cio2.o
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +1788 -0

1,788 lines not on this page · raw message

drivers/media/pci/intel/ipu3/ipu3-cio2.h new file +424 -0

424 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Hi Yong,

Please see my comments inline.

Quoted 18 lines · Yong Zhi

This patch adds CIO2 CSI-2 device driver for
Intel's IPU3 camera sub-system support.

---
 drivers/media/pci/Kconfig                |    2 +
 drivers/media/pci/Makefile               |    3 +-
 drivers/media/pci/intel/Makefile         |    5 +
 drivers/media/pci/intel/ipu3/Kconfig     |   17 +
 drivers/media/pci/intel/ipu3/Makefile    |    1 +
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 1788 ++++++++++++++++++++++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  424 +++++++
 7 files changed, 2239 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/pci/intel/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/Kconfig
 create mode 100644 drivers/media/pci/intel/ipu3/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.c
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.h

[snip]

drivers/media/pci/intel/ipu3/Kconfig line 6

Quoted 12 lines
drivers/media/pci/intel/ipu3/Kconfig new file +6 -0
new file mode 100644
index 0000000..2a895d6
@@ -0,0 +1,17 @@
+config VIDEO_IPU3_CIO2
+       tristate "Intel ipu3-cio2 driver"
+       depends on VIDEO_V4L2 && PCI
+       depends on MEDIA_CONTROLLER
+       depends on HAS_DMA
+       depends on ACPI

I wonder if it wouldn't make sense to make this depend on X86 (|| COMPILE_TEST) as well. Are we expecting a standalone PCI(e) card with this device in the future?

Quoted 23 lines
drivers/media/pci/intel/ipu3/Kconfig new file +11 -0
+       select V4L2_FWNODE
+       select VIDEOBUF2_DMA_SG
+
+       ---help---
+       This is the Intel IPU3 CIO2 CSI-2 receiver unit, found in Intel
+       Skylake and Kaby Lake SoCs and used for capturing images and
+       video from a camera sensor.
+
+       Say Y or M here if you have a Skylake/Kaby Lake SoC with MIPI CSI-2
+       connected camera.
+       The module will be called ipu3-cio2.
drivers/media/pci/intel/ipu3/Makefile new file +1 -0
new file mode 100644
index 0000000..20186e3
@@ -0,0 +1 @@
+obj-$(CONFIG_VIDEO_IPU3_CIO2) += ipu3-cio2.o
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +0 -0
new file mode 100644
index 0000000..69c47fc

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 84

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+static int cio2_fbpt_init_dummy(struct cio2_device *cio2)
+{
+       unsigned int i;
+
+       cio2->dummy_page = dma_alloc_noncoherent(&cio2->pci_dev->dev, PAGE_SIZE,
+                                       &cio2->dummy_page_bus_addr, GFP_KERNEL);
+       cio2->dummy_lop = dma_alloc_noncoherent(&cio2->pci_dev->dev, PAGE_SIZE,
+                                       &cio2->dummy_lop_bus_addr, GFP_KERNEL);

Something is not right here. Why noncoherent memory is allocated, but coherent memory is freed in the free function above? Wasn't the intention to just always use coherent memory throughout the driver?

Quoted 13 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +13 -0
+       if (!cio2->dummy_page || !cio2->dummy_lop) {
+               cio2_fbpt_exit_dummy(cio2);
+               return -ENOMEM;
+       }
+       /*
+        * List of Pointers(LOP) contains 1024x32b pointers to 4KB page each
+        * Initialize each entry to dummy_page bus base address.
+        */
+       for (i = 0; i < PAGE_SIZE / sizeof(*cio2->dummy_lop); i++)
+               cio2->dummy_lop[i] = cio2->dummy_page_bus_addr >> PAGE_SHIFT;
+
+       return 0;
+}

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 148

Quoted 13 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +13 -0
+/* Initialize fpbt entries to point to a given buffer */
+static void cio2_fbpt_entry_init_buf(struct cio2_device *cio2,
+                                    struct cio2_buffer *b,
+                                    struct cio2_fbpt_entry
+                                    entry[CIO2_MAX_LOPS])
+{
+       struct vb2_buffer *vb = &b->vbb.vb2_buf;
+       unsigned int length = vb->planes[0].length;
+       dma_addr_t lop_bus_addr = b->lop_bus_addr;
+       int remaining;
+
+       entry[0].first_entry.first_page_offset =
+               offset_in_page(vb2_plane_vaddr(vb, 0));

nit: Even though it's technically the same value, it's kind of logically confusing that a function for virtual addresses is used for DMA calculations. Similarly for offset_in_page, since it refers to CPU pages.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 161

Quoted 13 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +13 -0
+       remaining = length + entry[0].first_entry.first_page_offset;
+       entry[1].second_entry.num_of_pages = DIV_ROUND_UP(remaining, PAGE_SIZE);
+       /*
+        * last_page_available_bytes has the offset of the last byte in the
+        * last page which is still accessible by DMA. DMA cannot access
+        * beyond this point. Valid range for this is from 0 to 4095.
+        * 0 indicates 1st byte in the page is DMA accessible.
+        * 4095 (PAGE_SIZE - 1) means every single byte in the last page
+        * is available for DMA transfer.
+        */
+       entry[1].second_entry.last_page_available_bytes =
+                       (remaining & ~PAGE_MASK) ?
+                               (remaining & ~PAGE_MASK) - 1 : PAGE_SIZE - 1;

nit: Probably PAGE_SIZE is not going to change easily, but it's referring to CPU default page size. At least for clarity, you should have your own defines for CIO2 page sizes.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 169

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+       /* Fill FBPT */
+       remaining = length;
+       while (remaining > 0) {
+               entry->lop_page_addr = lop_bus_addr >> PAGE_SHIFT;
+               lop_bus_addr += PAGE_SIZE;
+               remaining -= PAGE_SIZE / sizeof(u32) * PAGE_SIZE;
+               entry++;
+       }

By any chance, doesn't the hardware provide some simple mode for contiguous buffers? Since we have an MMU anyway, we could use vb2_dma_contig and simplify the code significantly.

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+
+       /*
+        * The first not meaningful FBPT entry should point to a valid LOP
+        */
+       entry->lop_page_addr = cio2->dummy_lop_bus_addr >> PAGE_SHIFT;

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 184

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+
+       cio2_fbpt_entry_enable(cio2, entry);
+}
+
+static int cio2_fbpt_init(struct cio2_device *cio2, struct cio2_queue *q)
+{
+       struct device *dev = &cio2->pci_dev->dev;
+
+       q->fbpt = dma_alloc_noncoherent(dev, CIO2_FBPT_SIZE,
+                       &q->fbpt_bus_addr, GFP_KERNEL);

_coherent?

Quoted 39 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +39 -0
+       if (!q->fbpt)
+               return -ENOMEM;
+
+       memset(q->fbpt, 0, CIO2_FBPT_SIZE);
+
+       return 0;
+}
+
+static void cio2_fbpt_exit(struct cio2_queue *q, struct device *dev)
+{
+       dma_free_coherent(dev, CIO2_FBPT_SIZE, q->fbpt, q->fbpt_bus_addr);
+}
+
+/**************** CSI2 hardware setup ****************/
+
+/*
+ * This should come from sensor driver. No
+ * driver interface nor requirement yet.
+ */
+static u8 sensor_vc;   /* Virtual channel */
+
+/*
+ * The CSI2 receiver has several parameters affecting
+ * the receiver timings. These depend on the MIPI bus frequency
+ * F in Hz (sensor transmitter rate) as follows:
+ *     register value = (A/1e9 + B * UI) / COUNT_ACC
+ * where
+ *      UI = 1 / (2 * F) in seconds
+ *      COUNT_ACC = counter accuracy in seconds
+ *      For IPU3 COUNT_ACC = 0.0625
+ *
+ * A and B are coefficients from the table below,
+ * depending whether the register minimum or maximum value is
+ * calculated.
+ *                                     Minimum     Maximum
+ * Clock lane                          A     B     A     B
+ * reg_rx_csi_dly_cnt_termen_clane     0     0    38     0
+ * reg_rx_csi_dly_cnt_settle_clane    95    -8   300   -16
+ * Data lanes

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 233

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+ * reg_rx_csi_dly_cnt_termen_dlane0    0     0    35
+ * reg_rx_csi_dly_cnt_settle_dlane0   85    -2   145    -6
+ * reg_rx_csi_dly_cnt_termen_dlane1    0     0    35     4
+ * reg_rx_csi_dly_cnt_settle_dlane1   85    -2   145    -6
+ * reg_rx_csi_dly_cnt_termen_dlane2    0     0    35     4
+ * reg_rx_csi_dly_cnt_settle_dlane2   85    -2   145    -6
+ * reg_rx_csi_dly_cnt_termen_dlane3    0     0    35     4
+ * reg_rx_csi_dly_cnt_settle_dlane3   85    -2   145    -6
+ *
+ * We use the minimum values of both A and B.

Why?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 239

Quoted 6 lines

+ */

drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+static int cio2_rx_timing(s32 a, s32 b, s64 freq)
+{
+       int r;
+       const u32 accinv = 16;
+       const u32 ds = 8; /* divde shift */

typo: divide

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 2 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +2 -0
+
+       freq = (s32)freq >> ds;

Why do we demote freq from 64 to 32 bits here?

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 2 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +2 -0
+       if (WARN_ON(freq <= 0))
+               return -EINVAL;

It generally doesn't make sense for the frequency to be negative, so maybe the argument should have been unsigned to start with? (And 32-bit if we don't expect frequencies higher than 4 GHz anyway.)

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 2 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +2 -0
+
+       /* b could be 0, -2 or -8, so r < 500000000 */

Definitely. Anything <= 0 is also less than 500000000. Let's take a look at the computation below again:

1) accinv is multiplied by b,
2) 500000000 is divided by 256 (=== shift right by 8 bits) = 1953125,
3) accinv*b is multiplied by 1953125 to form the value of r.

Now let's see at possible maximum absolute values for particular steps:
1) 16 * -8 = -128 (signed 8 bits),
2) 1953125 (unsigned 21 bits),
3) -128 * 1953125 = -249999872 (signed 29 bits).

So I think the important thing to note in the comment is:

/* b could be 0, -2 or -8, so |accinv * b| is always less than (1 << ds) and thus |r| < 500000000. */

Quoted 1 line

+ r = accinv * b * (500000000 >> ds);

On the other hand, you lose some precision here. If you used s64 instead and did the divide shift at the end ((accinv * b * 500000000)

Quoted 1 line
Quoted 1 line

ds), for the example above you would get -250007629. (Depending on

how big freq is, it might not matter, though.)

Also nit: What is 500000000? We have local constants defined above, I think it could also make sense to do the same for this one. The compiler should do constant propagation and simplify respective calculations anyway.

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       r /= freq;
+       /* max value of a is 95 */
+       r += accinv * a;
+
+       return r;
+};
+
+/* Computation for the Delay value for Termination enable of Clock lane HS Rx */
+static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
+                           struct cio2_csi2_timing *timing)

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 266

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+{
+       struct device *dev = &cio2->pci_dev->dev;
+       struct v4l2_querymenu qm = {.id = V4L2_CID_LINK_FREQ, };
+       struct v4l2_ctrl *link_freq;
+       s64 freq;
+       int r;
+
+       if (q->sensor)
+               link_freq = v4l2_ctrl_find(q->sensor->ctrl_handler,
+                                               V4L2_CID_LINK_FREQ);

Is it even possible to have this function called with !q->sensor? If yes, maybe the function should check it and fail earlier?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 272

Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+       if (!link_freq) {
+               dev_err(dev, "failed to find LINK_FREQ\n");
+               return -EPIPE;
+       };
+
+       qm.index = v4l2_ctrl_g_ctrl(link_freq);

We will crash here or even dereference some invalid memory if (!q->sensor).

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 280

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+       r = v4l2_querymenu(q->sensor->ctrl_handler, &qm);
+       if (r) {
+               dev_err(dev, "failed to get menu item\n");
+               return r;
+       }
+
+       if (!qm.value)
+               return -EINVAL;

I think an error message would make sense here.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 283

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+       freq = qm.value;
+
+       dev_info(dev, "link freq is %lld\n", qm.value);

Do we need to print this? Perhaps dev_dbg() could be more appropriate.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 288

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+
+       timing->clk_termen = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_TERMEN_CLANE_A,
+                               CIO2_CSIRX_DLY_CNT_TERMEN_CLANE_B, freq);
+       /* test freq/div_shift > 0 */
+       if (timing->clk_termen < 0)

Either the comment should say >= 0 or the check be <= 0.

Quoted 1 line

+ return -EINVAL;

Given that freq comes from the sensor, the calculation might involve a subtraction (B might be negative) and returned value is not allowed to be negative, is it really okay to always use the minimum values?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 296

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+
+       timing->clk_settle = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_SETTLE_CLANE_A,
+                               CIO2_CSIRX_DLY_CNT_SETTLE_CLANE_B, freq);
+       timing->dat_termen = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_TERMEN_DLANE_A,
+                               CIO2_CSIRX_DLY_CNT_TERMEN_DLANE_B, freq);
+       timing->dat_settle = cio2_rx_timing(CIO2_CSIRX_DLY_CNT_SETTLE_DLANE_A,
+                               CIO2_CSIRX_DLY_CNT_SETTLE_DLANE_B, freq);

No need to check the above for > 0?

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+
+       dev_dbg(dev, "freq ct value is %d\n", timing->clk_termen);
+       dev_dbg(dev, "freq cs value is %d\n", timing->clk_settle);
+       dev_dbg(dev, "freq dt value is %d\n", timing->dat_termen);
+       dev_dbg(dev, "freq ds value is %d\n", timing->dat_settle);
+
+       return 0;
+};

[snip]

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+static int cio2_hw_init(struct cio2_device *cio2, struct cio2_queue *q)
+{
+       static const int NUM_VCS = 4;
+       static const int SID;   /* Stream id */
+       static const int ENTRY;

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 340

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       static const int FBPT_WIDTH = DIV_ROUND_UP(CIO2_MAX_LOPS,
+                                       CIO2_FBPT_SUBENTRY_UNIT);
+       const u32 num_buffers1 = CIO2_MAX_BUFFERS - 1;
+       void __iomem *const base = cio2->base;
+       u8 mipicode, lanes, csi2bus = q->csi2.port;
+       struct cio2_csi2_timing timing;
+       int i, r;
+
+       /* TODO: add support for virtual channels */
+       sensor_vc = 0;

Modifying a global variable here. I think it would make more sense to make it a local variable for this function then and also define a macro with the default VC number.

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+       mipicode = r = cio2_hw_mbus_to_mipicode(
+                       q->subdev_fmt.code);
+       if (r < 0)
+               return r;
+
+       lanes = r = q->csi2.num_of_lanes;
+       if (r < 0)
+               return r;

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 407

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+       r = cio2_csi2_calc_timing(cio2, q, &timing);
+       if (r) {
+               /* Use default values */

Is it really the good thing to do here? Perhaps calling cio2_csi2_calc_timing() before starting to program the hardware and bailing out if it fails would be a better choice?

Quoted 1 line

+ for (i = -1; i < lanes; i++) {

-1?

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+                       writel(0x4, q->csi_rx_base +
+                               CIO2_REG_CSIRX_DLY_CNT_TERMEN(i));
+                       writel(0x570, q->csi_rx_base +
+                               CIO2_REG_CSIRX_DLY_CNT_SETTLE(i));
+               }

[snip]

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static void cio2_hw_exit(struct cio2_device *cio2, struct cio2_queue *q)
+{
+       void __iomem *base = cio2->base;
+       unsigned int i, maxloops = 1000;
+
+       /* Disable CSI receiver and MIPI backend devices */
+       writel(0, q->csi_rx_base + CIO2_REG_CSIRX_ENABLE);
+       writel(0, q->csi_rx_base + CIO2_REG_MIPIBE_ENABLE);
+
+       /* Halt DMA */

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 515

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       writel(0, base + CIO2_REG_CDMAC0(CIO2_DMA_CHAN));
+       do {
+               if (readl(base + CIO2_REG_CDMAC0(CIO2_DMA_CHAN)) &
+                   CIO2_CDMAC0_DMA_HALTED)
+                       break;
+               usleep_range(1000, 2000);
+       } while (--maxloops);
+       if (!maxloops)
+               dev_err(&cio2->pci_dev->dev,
+                       "DMA %i can not be halted\n", CIO2_DMA_CHAN);

Does the code below ensure that the hardware is gracefully cut from the bus to avoid memory corruption?

Quoted 41 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +41 -0
+
+       for (i = 0; i < CIO2_NUM_PORTS; i++) {
+               writel(readl(base + CIO2_REG_PXM_FRF_CFG(i)) |
+                      CIO2_PXM_FRF_CFG_ABORT, base + CIO2_REG_PXM_FRF_CFG(i));
+               writel(readl(base + CIO2_REG_PBM_FOPN_ABORT) |
+                      CIO2_PBM_FOPN_ABORT(i), base + CIO2_REG_PBM_FOPN_ABORT);
+       }
+}
+
+static void cio2_buffer_done(struct cio2_device *cio2, unsigned int dma_chan)
+{
+       struct device *dev = &cio2->pci_dev->dev;
+       struct cio2_queue *q = cio2->cur_queue;
+       int buffers_found = 0;
+
+       if (dma_chan >= CIO2_QUEUES) {
+               dev_err(dev, "bad DMA channel %i\n", dma_chan);
+               return;
+       }
+
+       /* Find out which buffer(s) are ready */
+       do {
+               struct cio2_fbpt_entry *const entry =
+                       &q->fbpt[q->bufs_first * CIO2_MAX_LOPS];
+               struct cio2_buffer *b;
+
+               if (entry->first_entry.ctrl & CIO2_FBPT_CTRL_VALID)
+                       break;
+
+               b = q->bufs[q->bufs_first];
+               if (b) {
+                       u64 ns = ktime_get_ns();
+                       int bytes = entry[1].second_entry.num_of_bytes;
+
+                       q->bufs[q->bufs_first] = NULL;
+                       atomic_dec(&q->bufs_queued);
+                       dev_dbg(&cio2->pci_dev->dev,
+                               "buffer %i done\n", b->vbb.vb2_buf.index);
+
+                       /* Fill vb2 buffer entries and tell it's ready */
+                       vb2_set_plane_payload(&b->vbb.vb2_buf, 0, bytes);

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 566

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+                       b->vbb.vb2_buf.timestamp = ns;
+                       b->vbb.flags = V4L2_BUF_FLAG_DONE;
+                       b->vbb.field = V4L2_FIELD_NONE;
+                       memset(&b->vbb.timecode, 0, sizeof(b->vbb.timecode));
+                       b->vbb.sequence = entry[0].first_entry.frame_num;
+                       vb2_buffer_done(&b->vbb.vb2_buf, VB2_BUF_STATE_DONE);
+               }
+               cio2_fbpt_entry_init_dummy(cio2, entry);
+               q->bufs_first = (q->bufs_first + 1) % CIO2_MAX_BUFFERS;
+               buffers_found++;

Personally, I'm a bit afraid of such potentially infinite loops in interrupt handlers (if the CPU doesn't process the buffers faster than the hardware produces, it would never finish spinning). Let me defer this to other reviewers, though...

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       } while (1);
+
+       if (buffers_found == 0)
+               dev_warn(&cio2->pci_dev->dev,
+                        "no ready buffers found on DMA channel %i\n",
+                        dma_chan);
+}

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 626

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static irqreturn_t cio2_irq(int irq, void *cio2_ptr)
+{
+       struct cio2_device *cio2 = cio2_ptr;
+       void __iomem *const base = cio2->base;
+       struct device *dev = &cio2->pci_dev->dev;
+       u32 int_status, int_clear;
+
+       int_clear = int_status = readl(base + CIO2_REG_INT_STS);
+       if (!int_status)
+               return IRQ_NONE;

CodingStyle states clearly: "Don't put multiple assignments on a single line either. Kernel coding style
is super simple. Avoid tricky expressions."

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 629

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+
+       if (int_status & CIO2_INT_IOOE) {
+               /* Interrupt on Output Error:

CodingStyle: Multi-line comment should start with an empty line.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 635

Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+                * 1) SRAM is full and FS received, or
+                * 2) An invalid bit detected by DMA.
+                */
+               u32 oe_status, oe_clear;
+
+               oe_clear = oe_status = readl(base + CIO2_REG_INT_STS_EXT_OE);

Multiple assignments on a single ine.

Quoted 22 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +22 -0
+
+               if (oe_status & CIO2_INT_EXT_OE_DMAOE_MASK) {
+                       dev_err(dev, "DMA output error: 0x%x\n",
+                               (oe_status & CIO2_INT_EXT_OE_DMAOE_MASK)
+                               >> CIO2_INT_EXT_OE_DMAOE_SHIFT);
+                       oe_status &= ~CIO2_INT_EXT_OE_DMAOE_MASK;
+               }
+               if (oe_status & CIO2_INT_EXT_OE_OES_MASK) {
+                       dev_err(dev, "DMA output error on CSI2 buses: 0x%x\n",
+                               (oe_status & CIO2_INT_EXT_OE_OES_MASK)
+                               >> CIO2_INT_EXT_OE_OES_SHIFT);
+                       oe_status &= ~CIO2_INT_EXT_OE_OES_MASK;
+               }
+               writel(oe_clear, base + CIO2_REG_INT_STS_EXT_OE);
+               if (oe_status)
+                       dev_warn(dev, "unknown interrupt 0x%x on OE\n",
+                                oe_status);
+               int_status &= ~CIO2_INT_IOOE;
+       }
+
+       if (int_status & CIO2_INT_IOC_MASK) {
+               /* DMA IO done -- frame ready */

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 667

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+               u32 clr = 0;
+               unsigned int d;
+
+               for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
+                       if (int_status & CIO2_INT_IOC(d)) {
+                               clr |= CIO2_INT_IOC(d);
+                               dev_dbg(dev, "DMA %i done\n", d);
+                               cio2_buffer_done(cio2, d);
+                       }
+               int_status &= ~clr;

Perhaps a construct using lfs/ffs would be a bit smarter here. For example:

        while (int_status & CIO2_INT_IOC_MASK) {
                d = __ffs(int_status & CIO2_INT_IOC_MASK) - 1;
                int_status &= ~CIO2_INT_IOC(d);
                dev_dbg(dev, "DMA %i done\n", d);
                cio2_buffer_done(cio2, d);
        }
Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       }
+
+       if (int_status & CIO2_INT_IOS_IOLN_MASK) {
+               /* DMA IO starts or reached specified line */
+               u32 clr = 0;
+               unsigned int d;
+

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 684

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+               for (d = 0; d < CIO2_NUM_DMA_CHAN; d++)
+                       if (int_status & CIO2_INT_IOS_IOLN(d)) {
+                               clr |= CIO2_INT_IOS_IOLN(d);
+                               if (d == CIO2_DMA_CHAN)
+                                       cio2_queue_event_sof(cio2,
+                                                            cio2->cur_queue);
+                               dev_dbg(dev,
+                                       "DMA %i started or reached line\n", d);
+                       }
+               int_status &= ~clr;

Ditto.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 692

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+       }
+
+       if (int_status & (CIO2_INT_IOIE | CIO2_INT_IOIRQ)) {
+               /* CSI2 receiver (error) interrupt */
+               u32 ie_status, ie_clear;
+               unsigned int port;
+
+               ie_clear = ie_status = readl(base + CIO2_REG_INT_STS_EXT_IE);

Multiple assignments.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 694

Quoted 2 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +2 -0
+
+               for (port = 0; port < CIO2_NUM_PORTS; port++) {

CIO2_NUM_PORTS is small and the bits are scattered through ie_status, so I guess no __ffs() trick here.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 706

Quoted 12 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +12 -0
+                       u32 port_status = (ie_status >> (port * 8)) & 0xff;
+                       void __iomem *const csi_rx_base =
+                                               base + CIO2_REG_PIPE_BASE(port);
+                       unsigned int i;
+
+                       while (port_status) {
+                               i = ffs(port_status) - 1;
+                               dev_err(dev, "port %i error %s\n",
+                                       port, cio2_port_errs[i]);
+                               ie_status &= ~BIT(port * 8 + i);
+                               port_status &= ~BIT(i);
+                       }

Yeah, exactly like this. ;)

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+
+                       if (ie_status & CIO2_INT_EXT_IE_IRQ(port)) {
+                               u32 csi2_status, csi2_clear;
+
+                               csi2_clear = csi2_status = readl(csi_rx_base +

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 721

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+                                               CIO2_REG_IRQCTRL_STATUS);
+                               for (i = 0; i < ARRAY_SIZE(cio2_irq_errs);
+                                    i++) {
+                                       if (csi2_status & (1 << i)) {
+                                               dev_err(dev,
+                                                       "CSI-2 receiver port %i: %s\n",
+                                                       port, cio2_irq_errs[i]);
+                                               csi2_status &= ~(1 << i);
+                                       }
+                               }

__ffs() trick applies here too.

Quoted 21 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +21 -0
+
+                               writel(csi2_clear,
+                                      csi_rx_base + CIO2_REG_IRQCTRL_CLEAR);
+                               if (csi2_status)
+                                       dev_warn(dev,
+                                                "unknown CSI2 error 0x%x on port %i\n",
+                                                csi2_status, port);
+
+                               ie_status &= ~CIO2_INT_EXT_IE_IRQ(port);
+                       }
+               }
+
+               writel(ie_clear, base + CIO2_REG_INT_STS_EXT_IE);
+               if (ie_status)
+                       dev_warn(dev, "unknown interrupt 0x%x on IE\n",
+                                ie_status);
+
+               int_status &= ~(CIO2_INT_IOIE | CIO2_INT_IOIRQ);
+       }
+
+       writel(int_clear, base + CIO2_REG_INT_STS);

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 752

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       if (int_status)
+               dev_warn(dev, "unknown interrupt 0x%x on INT\n", int_status);
+
+       return IRQ_HANDLED;
+}
+
+/**************** Videobuf2 interface ****************/
+
+static void cio2_vb2_return_all_buffers(struct cio2_queue *q,
+                                       enum vb2_buffer_state state)

Hmm, is there ever a reason to return all buffers with a state other than _ERROR?

Quoted 22 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +22 -0
+{
+       unsigned int i;
+
+       for (i = 0; i < CIO2_MAX_BUFFERS; i++) {
+               if (q->bufs[i]) {
+                       atomic_dec(&q->bufs_queued);
+                       vb2_buffer_done(&q->bufs[i]->vbb.vb2_buf, state);
+               }
+       }
+}
+
+static int cio2_vb2_queue_setup(struct vb2_queue *vq,
+                               unsigned int *num_buffers,
+                               unsigned int *num_planes,
+                               unsigned int sizes[],
+                               struct device *alloc_devs[])
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vq);
+       struct cio2_queue *q = container_of(vq, struct cio2_queue, vbq);
+       u32 width = q->subdev_fmt.width;
+       u32 height = q->subdev_fmt.height;
+       u32 pixelformat = q->pixelformat;

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 784

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       unsigned int i, szimage;
+       int r = 0;
+
+       for (i = 0; i < ARRAY_SIZE(cio2_csi2_fmts); i++) {
+               if (pixelformat == cio2_csi2_fmts[i])
+                       break;
+       }
+
+       /* Use SRGGB10 instead of return err */
+       if (i >= ARRAY_SIZE(cio2_csi2_fmts))

I think this should be impossible, since S_FMT should have already validated (and corrected) the setting.

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 787

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+               pixelformat = V4L2_PIX_FMT_IPU3_SRGGB10;
+
+       alloc_devs[0] = &cio2->pci_dev->dev;

Hmm, so it doesn't go through the IPU MMU in the end?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 795

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+       szimage = cio2_bytesperline(width) * height;
+
+       if (*num_planes) {
+               /*
+                * Only single plane is supported
+                */
+               if (*num_planes != 1 || sizes[0] < szimage)
+                       return -EINVAL;

S_FMT should validate this and then queue_setup should never get an invalid value.

Quoted 31 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +31 -0
+       }
+
+       *num_planes = 1;
+       sizes[0] = szimage;
+
+       *num_buffers = clamp_val(*num_buffers, 1, CIO2_MAX_BUFFERS);
+
+       /* Initialize buffer queue */
+       for (i = 0; i < CIO2_MAX_BUFFERS; i++) {
+               q->bufs[i] = NULL;
+               cio2_fbpt_entry_init_dummy(cio2, &q->fbpt[i * CIO2_MAX_LOPS]);
+       }
+       atomic_set(&q->bufs_queued, 0);
+       q->bufs_first = 0;
+       q->bufs_next = 0;
+
+       return r;
+}
+
+/* Called after each buffer is allocated */
+static int cio2_vb2_buf_init(struct vb2_buffer *vb)
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
+       struct device *dev = &cio2->pci_dev->dev;
+       struct cio2_buffer *b =
+               container_of(vb, struct cio2_buffer, vbb.vb2_buf);
+       unsigned int length = vb->planes[0].length;
+       int lops  = DIV_ROUND_UP(DIV_ROUND_UP(length, PAGE_SIZE) + 1,
+                                PAGE_SIZE / sizeof(u32));
+       u32 *lop;
+       struct sg_table *sg;

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 836

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       struct sg_page_iter sg_iter;
+
+       if (lops <= 0 || lops > CIO2_MAX_LOPS) {
+               dev_err(dev, "%s: bad buffer size (%i)\n", __func__, length);
+               return -ENOSPC;         /* Should never happen */
+       }
+
+       /* Allocate LOP table */
+       b->lop = lop = dma_alloc_noncoherent(dev, lops * PAGE_SIZE,
+                                       &b->lop_bus_addr, GFP_KERNEL);

_coherent?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 843

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       if (!lop)
+               return -ENOMEM;
+
+       /* Fill LOP */
+       sg = vb2_dma_sg_plane_desc(vb, 0);
+       if (!sg)
+               return -EFAULT;

I'd say -ENOMEM is better here. (But actually it should be impossible, if allocation succeeded previously.)

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +11 -0
+
+       for_each_sg_page(sg->sgl, &sg_iter, sg->nents, 0)
+               *lop++ = sg_page_iter_dma_address(&sg_iter) >> PAGE_SHIFT;
+       *lop++ = cio2->dummy_page_bus_addr >> PAGE_SHIFT;
+
+       return 0;
+}
+
+/* Transfer buffer ownership to cio2 */
+static void cio2_vb2_buf_queue(struct vb2_buffer *vb)
+{

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 864

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
+       struct cio2_queue *q =
+               container_of(vb->vb2_queue, struct cio2_queue, vbq);
+       struct cio2_buffer *b =
+               container_of(vb, struct cio2_buffer, vbb.vb2_buf);
+       struct cio2_fbpt_entry *entry;
+       unsigned int next = q->bufs_next;
+       int bufs_queued = atomic_inc_return(&q->bufs_queued);
+
+       if (vb2_start_streaming_called(&q->vbq)) {

Shouldn't it be vb2_is_streaming()? (There is not much difference, though, except that vb2_start_streaming_called() returns true, even before .start_streaming finished, while vb2_is_streaming() does so only after it returns successfully.)

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 875

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +11 -0
+               u32 fbpt_rp =
+                       (readl(cio2->base + CIO2_REG_CDMARI(CIO2_DMA_CHAN))
+                        >> CIO2_CDMARI_FBPT_RP_SHIFT)
+                       & CIO2_CDMARI_FBPT_RP_MASK;
+
+               /*
+                * fbpt_rp is the fbpt entry that the dma is currently working
+                * on, but since it could jump to next entry at any time,
+                * assume that we might already be there.
+                */
+               fbpt_rp = (fbpt_rp + 1) % CIO2_MAX_BUFFERS;

Hmm, this is really racy. This code can be pre-empted and not execute for quite long time, depending on system load, resuming after the hardware goes even further. Technically you could prevent this using *_irq_save()/_irq_restore(), but I'd try to find a way that doesn't rely on the timing, if possible.

Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+
+               if (bufs_queued <= 1)
+                       next = fbpt_rp + 1;     /* Buffers were drained */
+               else if (fbpt_rp == next)
+                       next++;
+               next %= CIO2_MAX_BUFFERS;

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 891

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       }
+
+       while (q->bufs[next]) {
+               /* If the entry is used, get the next one,
+                * We can not break here if all are filled,
+                * Will wait for one free, otherwise it will crash
+                */
+               dev_dbg(&cio2->pci_dev->dev,
+                       "entry %i was already full!\n", next);
+               next = (next + 1) % CIO2_MAX_BUFFERS;

A busy waiting, possibly infinite, loop. Hmm.

I think we could do something smarter here, such as sleeping on a wait_queue, which is woken up from the interrupt handler.

Also, why do you think it will crash? I think you can just do return the buffer to vb2 with _ERROR status and bail out, if you can't queue due to some failure.

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       }
+
+       q->bufs[next] = b;
+       entry = &q->fbpt[next * CIO2_MAX_LOPS];
+       cio2_fbpt_entry_init_buf(cio2, b, entry);
+       q->bufs_next = (next + 1) % CIO2_MAX_BUFFERS;
+}

[snip]

Quoted 9 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +9 -0
+static int cio2_set_power(struct vb2_queue *vq, int enable)
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vq);
+       struct device *dev = &cio2->pci_dev->dev;
+       int ret = 0;
+
+       if (enable) {
+               ret = pm_runtime_get_sync(dev);
+               if (ret < 0) {

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 933

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+                       dev_info(&cio2->pci_dev->dev,
+                               "failed to get power %d\n", ret);
+                       pm_runtime_put(dev);
+               }
+       } else {
+               ret = pm_runtime_put(dev);
+       }
+
+       /* return 0 if power is active */
+       return (ret >= 0) ? 0 : ret;

nit: I think this function is unnecessary, as it only adds one more level of indirection and also combines two completely different code paths together, especially since it is called exactly once with enable==1 and once with enable==0. I'd suggest just pasting respective code in place of the call instead.

Quoted 1 line

+}

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1026

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap)
+{
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+       cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
+
+       return 0;
+}

[snip]

Quoted 9 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +9 -0
+static int cio2_v4l2_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
+{
+       u32 pixelformat = f->fmt.pix.pixelformat;
+       unsigned int i;
+
+       cio2_v4l2_g_fmt(file, fh, f);
+
+       for (i = 0; i < ARRAY_SIZE(cio2_csi2_fmts); i++) {
+               if (pixelformat == cio2_csi2_fmts[i])

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1079

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+                       break;
+       }
+
+       /* Use SRGGB10 as default if not found */
+       if (i >= ARRAY_SIZE(cio2_csi2_fmts))
+               pixelformat = V4L2_PIX_FMT_IPU3_SRGGB10;
+
+       f->fmt.pix.pixelformat = pixelformat;
+       f->fmt.pix.bytesperline = cio2_bytesperline(f->fmt.pix.width);
+       f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;

Shouldn't you use f->fmt.pix_mp instead?

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+
+       return 0;
+}

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1497

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+
+       /* Initialize vbq */
+       vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       vbq->io_modes = VB2_USERPTR | VB2_MMAP;

VB2_DMABUF?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1504

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       vbq->ops = &cio2_vb2_ops;
+       vbq->mem_ops = &vb2_dma_sg_memops;
+       vbq->buf_struct_size = sizeof(struct cio2_buffer);
+       vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+       vbq->min_buffers_needed = 1;
+       vbq->drv_priv = cio2;
+       vbq->lock = &q->lock;

Does the code take into account queue operations and video device operations being asynchronous regarding each other? Given that in this case there is always one queue per video device, maybe it would just make sense to use the same lock for both? (This happens if you leave vbq->lock with NULL.)

Best regards,
Tomasz

#6 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Hi Tomasz,

Quoted 3 lines · On Monday, June 12, 2017 18:59:18 Tomasz Figa wrote:

By any chance, doesn't the hardware provide some simple mode for contiguous buffers? Since we have an MMU anyway, we could use vb2_dma_contig and simplify the code significantly.

In IPU3 the CIO2 (CSI-2 receiver) and the IMGU (image processing system) are entirely separate PCI devices. The MMU is only in the IMGU device; the CIO2 doesn't have MMU but has the FBPT (frame buffer pointer tables) to handle discontinuous buffers.

[...]

Quoted 6 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 787

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+               pixelformat = V4L2_PIX_FMT_IPU3_SRGGB10;
+
+       alloc_devs[0] = &cio2->pci_dev->dev;

Hmm, so it doesn't go through the IPU MMU in the end?

No, it doesn't.

Quoted 13 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 795

Quoted 9 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+       szimage = cio2_bytesperline(width) * height;
+
+       if (*num_planes) {
+               /*
+                * Only single plane is supported
+                */
+               if (*num_planes != 1 || sizes[0] < szimage)
+                       return -EINVAL;

S_FMT should validate this and then queue_setup should never get an invalid value.

Quoted 11 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +11 -0
+       }
+
+       *num_planes = 1;
+       sizes[0] = szimage;
+
+       *num_buffers = clamp_val(*num_buffers, 1, CIO2_MAX_BUFFERS);
+
+       /* Initialize buffer queue */
+       for (i = 0; i < CIO2_MAX_BUFFERS; i++) {
+               q->bufs[i] = NULL;
+               cio2_fbpt_entry_init_dummy(cio2, &q->fbpt[i * 

CIO2_MAX_LOPS]);

Quoted 9 lines
Quoted 9 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +9 -0
+       }
+       atomic_set(&q->bufs_queued, 0);
+       q->bufs_first = 0;
+       q->bufs_next = 0;
+
+       return r;
+}
+
+/* Called after each buffer is allocated */
Quoted 8 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +8 -0
+static int cio2_vb2_buf_init(struct vb2_buffer *vb)
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
+       struct device *dev = &cio2->pci_dev->dev;
+       struct cio2_buffer *b =
+               container_of(vb, struct cio2_buffer, vbb.vb2_buf);
+       unsigned int length = vb->planes[0].length;
+       int lops  = DIV_ROUND_UP(DIV_ROUND_UP(length, PAGE_SIZE) + 

1,

Quoted 7 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+                                PAGE_SIZE / sizeof(u32));
+       u32 *lop;
+       struct sg_table *sg;
+       struct sg_page_iter sg_iter;
+
+       if (lops <= 0 || lops > CIO2_MAX_LOPS) {
+               dev_err(dev, "%s: bad buffer size (%i)\n", __func__, 

length);

Quoted 6 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+               return -ENOSPC;         /* Should never happen */
+       }
+
+       /* Allocate LOP table */
+       b->lop = lop = dma_alloc_noncoherent(dev, lops * PAGE_SIZE,
+                                       &b->lop_bus_addr, 

GFP_KERNEL);

Quoted 11 lines

_coherent?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 843

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       if (!lop)
+               return -ENOMEM;
+
+       /* Fill LOP */
+       sg = vb2_dma_sg_plane_desc(vb, 0);
+       if (!sg)
+               return -EFAULT;
Quoted 6 lines

I'd say -ENOMEM is better here. (But actually it should be impossible, if allocation succeeded previously.)

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+
+       for_each_sg_page(sg->sgl, &sg_iter, sg->nents, 0)
+               *lop++ = sg_page_iter_dma_address(&sg_iter) >> 

PAGE_SHIFT;

Quoted 15 lines
Quoted 15 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +15 -0
+       *lop++ = cio2->dummy_page_bus_addr >> PAGE_SHIFT;
+
+       return 0;
+}
+
+/* Transfer buffer ownership to cio2 */
+static void cio2_vb2_buf_queue(struct vb2_buffer *vb)
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
+       struct cio2_queue *q =
+               container_of(vb->vb2_queue, struct cio2_queue, vbq);
+       struct cio2_buffer *b =
+               container_of(vb, struct cio2_buffer, vbb.vb2_buf);
+       struct cio2_fbpt_entry *entry;
+       unsigned int next = q->bufs_next;
Quoted 11 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 864

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+       int bufs_queued = atomic_inc_return(&q->bufs_queued);
+
+       if (vb2_start_streaming_called(&q->vbq)) {

Shouldn't it be vb2_is_streaming()? (There is not much difference, though, except that vb2_start_streaming_called() returns true, even before .start_streaming finished, while vb2_is_streaming() does so only after it returns successfully.)

Quoted 2 lines
(quoted diff) +2 -0
+               u32 fbpt_rp =
+                       (readl(cio2->base + 

CIO2_REG_CDMARI(CIO2_DMA_CHAN))

Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+                        >> CIO2_CDMARI_FBPT_RP_SHIFT)
+                       & CIO2_CDMARI_FBPT_RP_MASK;
+
+               /*
+                * fbpt_rp is the fbpt entry that the dma is 

currently working

Quoted 1 line
Quoted 1 line

+ * on, but since it could jump to next entry at any

time,

Quoted 9 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 875

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+                * assume that we might already be there.
+                */
+               fbpt_rp = (fbpt_rp + 1) % CIO2_MAX_BUFFERS;

Hmm, this is really racy. This code can be pre-empted and not execute for quite long time, depending on system load, resuming after the hardware goes even further. Technically you could prevent this using *_irq_save()/_irq_restore(), but I'd try to find a way that doesn't rely on the timing, if possible.

That is true, if the driver doesn't get executed in more than one frame time. I don't think that's very common, but should be handled.

Hmm. Actually the buffer has VALID bit which is set by driver to indicate that the HW can fill the buffer and cleared by HW to indicate that the buffer is filled. Probably the HW can not actually jump to the next buffer as suggested by the comment, because I think the VALID bit would be clear in that case. That should be checked.

Quoted 4 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+
+               if (bufs_queued <= 1)
+                       next = fbpt_rp + 1;     /* Buffers were 

drained */

Quoted 10 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 888

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+               else if (fbpt_rp == next)
+                       next++;
+               next %= CIO2_MAX_BUFFERS;
+       }
+
+       while (q->bufs[next]) {
+               /* If the entry is used, get the next one,
+                * We can not break here if all are filled,
+                * Will wait for one free, otherwise it will crash
+                */

That comment should be fixed. "otherwise it will crash" doesn't tell much useful. Why would it crash?

Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 891

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+               dev_dbg(&cio2->pci_dev->dev,
+                       "entry %i was already full!\n", next);
+               next = (next + 1) % CIO2_MAX_BUFFERS;

A busy waiting, possibly infinite, loop. Hmm.

It's not really busy waiting. We have allocated CIO2_MAX_BUFFERS buffers (or actually just buffer entries in HW table) circularly for the hardware, and then the user has requested N buffer queue. The driver ensures N <= CIO2_MAX_BUFFERS and this guarantees that whenever user queues a buffer, there necessarily is a free buffer in the hardware circular buffer list. The loop above finds the first free buffer from the circular list, which necessarily exists. In practice it should be always the very first since that is the oldest one given to hardware.

Quoted 3 lines

I think we could do something smarter here, such as sleeping on a wait_queue, which is woken up from the interrupt handler.

I think that's a bit complicated for situation which should be never possible to happen.

Quoted 3 lines

Also, why do you think it will crash? I think you can just do return the buffer to vb2 with _ERROR status and bail out, if you can't queue due to some failure.

Agree.

- Tuukka

#7 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Hi Tuukka,

Thanks for your replies. Please see mine inline.

Quoted 15 lines · Tuukka Toivonen

Hi Tomasz,

Quoted 3 lines · On Monday, June 12, 2017 18:59:18 Tomasz Figa wrote:

By any chance, doesn't the hardware provide some simple mode for contiguous buffers? Since we have an MMU anyway, we could use vb2_dma_contig and simplify the code significantly.

In IPU3 the CIO2 (CSI-2 receiver) and the IMGU (image processing system) are entirely separate PCI devices. The MMU is only in the IMGU device; the CIO2 doesn't have MMU but has the FBPT (frame buffer pointer tables) to handle discontinuous buffers.

[...]

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 787

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+               pixelformat = V4L2_PIX_FMT_IPU3_SRGGB10;
+
+       alloc_devs[0] = &cio2->pci_dev->dev;

Hmm, so it doesn't go through the IPU MMU in the end?

No, it doesn't.

Aha. I was confused by the fact that the driver calls dma_alloc_(non)coherent() with sizes likely greater than PAGE_SIZE.

So, given the above, I believe we need to fix the LOP allocation to allocate one page at a time and stop relying on bus address contiguity.

Quoted 17 lines
Quoted 9 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 9 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +9 -0
+/* Called after each buffer is allocated */
+static int cio2_vb2_buf_init(struct vb2_buffer *vb)
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vb->vb2_queue);
+       struct device *dev = &cio2->pci_dev->dev;
+       struct cio2_buffer *b =
+               container_of(vb, struct cio2_buffer, vbb.vb2_buf);
+       unsigned int length = vb->planes[0].length;
+       int lops  = DIV_ROUND_UP(DIV_ROUND_UP(length, PAGE_SIZE) +

1,

Quoted 7 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+                                PAGE_SIZE / sizeof(u32));
+       u32 *lop;
+       struct sg_table *sg;
+       struct sg_page_iter sg_iter;
+
+       if (lops <= 0 || lops > CIO2_MAX_LOPS) {
+               dev_err(dev, "%s: bad buffer size (%i)\n", __func__,
Quoted 8 lines

length);

Quoted 6 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+               return -ENOSPC;         /* Should never happen */
+       }
+
+       /* Allocate LOP table */
+       b->lop = lop = dma_alloc_noncoherent(dev, lops * PAGE_SIZE,
+                                       &b->lop_bus_addr,

GFP_KERNEL);

^^ Here is the offending allocation.

Quoted 22 lines
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+               u32 fbpt_rp =
+                       (readl(cio2->base +

CIO2_REG_CDMARI(CIO2_DMA_CHAN))

Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+                        >> CIO2_CDMARI_FBPT_RP_SHIFT)
+                       & CIO2_CDMARI_FBPT_RP_MASK;
+
+               /*
+                * fbpt_rp is the fbpt entry that the dma is

currently working

Quoted 1 line
Quoted 1 line

+ * on, but since it could jump to next entry at any

time,

Quoted 9 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 875

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+                * assume that we might already be there.
+                */
+               fbpt_rp = (fbpt_rp + 1) % CIO2_MAX_BUFFERS;

Hmm, this is really racy. This code can be pre-empted and not execute for quite long time, depending on system load, resuming after the hardware goes even further. Technically you could prevent this using *_irq_save()/_irq_restore(), but I'd try to find a way that doesn't rely on the timing, if possible.

Quoted 8 lines

That is true, if the driver doesn't get executed in more than one frame time. I don't think that's very common, but should be handled.

Hmm. Actually the buffer has VALID bit which is set by driver to indicate that the HW can fill the buffer and cleared by HW to indicate that the buffer is filled. Probably the HW can not actually jump to the next buffer as suggested by the comment, because I think the VALID bit would be clear in that case. That should be checked.

I think the problem here is that we keep all the entries valid and only point to dummy buffers if there are no buffers queued by userspace.

Quoted 39 lines
Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+
+               if (bufs_queued <= 1)
+                       next = fbpt_rp + 1;     /* Buffers were

drained */

Quoted 10 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 888

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+               else if (fbpt_rp == next)
+                       next++;
+               next %= CIO2_MAX_BUFFERS;
+       }
+
+       while (q->bufs[next]) {
+               /* If the entry is used, get the next one,
+                * We can not break here if all are filled,
+                * Will wait for one free, otherwise it will crash
+                */

That comment should be fixed. "otherwise it will crash" doesn't tell much useful. Why would it crash?

Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 891

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+               dev_dbg(&cio2->pci_dev->dev,
+                       "entry %i was already full!\n", next);
+               next = (next + 1) % CIO2_MAX_BUFFERS;

A busy waiting, possibly infinite, loop. Hmm.

It's not really busy waiting. We have allocated CIO2_MAX_BUFFERS buffers (or actually just buffer entries in HW table) circularly for the hardware, and then the user has requested N buffer queue. The driver ensures N <= CIO2_MAX_BUFFERS and this guarantees that whenever user queues a buffer, there necessarily is a free buffer in the hardware circular buffer list. The loop above finds the first free buffer from the circular list, which necessarily exists. In practice it should be always the very first since that is the oldest one given to hardware.

Quoted 3 lines

I think we could do something smarter here, such as sleeping on a wait_queue, which is woken up from the interrupt handler.

Quoted 8 lines

I think that's a bit complicated for situation which should be never possible to happen.

Quoted 3 lines

Also, why do you think it will crash? I think you can just do return the buffer to vb2 with _ERROR status and bail out, if you can't queue due to some failure.

Agree.

Given your explanation, wouldn't it make sense to actually make the loop finite, limited by the number of buffers and if (due to some unforeseen condition, like a driver bug) there is no buffer available until then, error out?

Best regards,
Tomasz

#8 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

In reply to Tomasz Figa

Hi Tomasz,

Quoted 24 lines · Tomasz Figa

Hi Yong,

Please see my comments inline.

Quoted 18 lines · Yong Zhi

This patch adds CIO2 CSI-2 device driver for
Intel's IPU3 camera sub-system support.

---
 drivers/media/pci/Kconfig                |    2 +
 drivers/media/pci/Makefile               |    3 +-
 drivers/media/pci/intel/Makefile         |    5 +
 drivers/media/pci/intel/ipu3/Kconfig     |   17 +
 drivers/media/pci/intel/ipu3/Makefile    |    1 +
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 1788 ++++++++++++++++++++++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  424 +++++++
 7 files changed, 2239 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/pci/intel/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/Kconfig
 create mode 100644 drivers/media/pci/intel/ipu3/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.c
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.h

[snip]

Quoted 16 lines

drivers/media/pci/intel/ipu3/Kconfig line 6

Quoted 12 lines
drivers/media/pci/intel/ipu3/Kconfig new file +6 -0
new file mode 100644
index 0000000..2a895d6
@@ -0,0 +1,17 @@
+config VIDEO_IPU3_CIO2
+       tristate "Intel ipu3-cio2 driver"
+       depends on VIDEO_V4L2 && PCI
+       depends on MEDIA_CONTROLLER
+       depends on HAS_DMA
+       depends on ACPI

I wonder if it wouldn't make sense to make this depend on X86 (|| COMPILE_TEST) as well. Are we expecting a standalone PCI(e) card with this device in the future?

All I'm aware of are integrated with the CPU (or the chipset).

Signature · 4 lines
Regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#9 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

In reply to Tomasz Figa

Hi Tomasz,

A few more comments, better late than never I guess.

On Mon, Jun 12, 2017 at 06:59:18PM +0900, Tomasz Figa wrote:
...

Quoted 40 lines
Quoted 16 lines

+/*
+ * The CSI2 receiver has several parameters affecting
+ * the receiver timings. These depend on the MIPI bus frequency
+ * F in Hz (sensor transmitter rate) as follows:
+ * register value = (A/1e9 + B * UI) / COUNT_ACC
+ * where

+ *      UI = 1 / (2 * F) in seconds
+ *      COUNT_ACC = counter accuracy in seconds
+ *      For IPU3 COUNT_ACC = 0.0625

+ *
+ * A and B are coefficients from the table below,
+ * depending whether the register minimum or maximum value is
+ * calculated.

+ *                                     Minimum     Maximum
+ * Clock lane                          A     B     A     B
+ * reg_rx_csi_dly_cnt_termen_clane     0     0    38     0
Quoted 12 lines

+ * reg_rx_csi_dly_cnt_settle_clane 95 -8 300 -16
+ * Data lanes
+ * reg_rx_csi_dly_cnt_termen_dlane0 0 0 35
+ * reg_rx_csi_dly_cnt_settle_dlane0 85 -2 145 -6
+ * reg_rx_csi_dly_cnt_termen_dlane1 0 0 35 4
+ * reg_rx_csi_dly_cnt_settle_dlane1 85 -2 145 -6
+ * reg_rx_csi_dly_cnt_termen_dlane2 0 0 35 4
+ * reg_rx_csi_dly_cnt_settle_dlane2 85 -2 145 -6
+ * reg_rx_csi_dly_cnt_termen_dlane3 0 0 35 4
+ * reg_rx_csi_dly_cnt_settle_dlane3 85 -2 145 -6
+ *
+ * We use the minimum values of both A and B.

Why?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 239

Quoted 6 lines

+ */

drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +5 -0
+static int cio2_rx_timing(s32 a, s32 b, s64 freq)
+{
+       int r;
+       const u32 accinv = 16;
+       const u32 ds = 8; /* divde shift */

typo: divide

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+       freq = (s32)freq >> ds;

Why do we demote freq from 64 to 32 bits here?

I don't think there's any reason to. The original purpose of the check has likely been to avoid dividing by a 64-bit number but that has been lost here. The cast should be elsewhere...

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+       if (WARN_ON(freq <= 0))
+               return -EINVAL;

It generally doesn't make sense for the frequency to be negative, so maybe the argument should have been unsigned to start with? (And 32-bit if we don't expect frequencies higher than 4 GHz anyway.)

The value comes from a 64-bit integer V4L2 control so that implies the value range of s64 as well.

Quoted 21 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+       /* b could be 0, -2 or -8, so r < 500000000 */

Definitely. Anything <= 0 is also less than 500000000. Let's take a look at the computation below again:

1) accinv is multiplied by b,
2) 500000000 is divided by 256 (=== shift right by 8 bits) = 1953125,
3) accinv*b is multiplied by 1953125 to form the value of r.

Now let's see at possible maximum absolute values for particular steps:
1) 16 * -8 = -128 (signed 8 bits),
2) 1953125 (unsigned 21 bits),
3) -128 * 1953125 = -249999872 (signed 29 bits).

So I think the important thing to note in the comment is:

/* b could be 0, -2 or -8, so |accinv * b| is always less than (1 << ds) and thus |r| < 500000000. */

Quoted 7 lines
Quoted 1 line

+ r = accinv * b * (500000000 >> ds);

On the other hand, you lose some precision here. If you used s64 instead and did the divide shift at the end ((accinv * b * 500000000)

Quoted 1 line
Quoted 1 line

ds), for the example above you would get -250007629. (Depending on

how big freq is, it might not matter, though.)

The frequency is typically hundreds of mega-Hertz.

Quoted 4 lines

Also nit: What is 500000000? We have local constants defined above, I think it could also make sense to do the same for this one. The compiler should do constant propagation and simplify respective calculations anyway.

COUNT_ACC in the formula in the comment a few decalines above is in nanoseconds. Performing the calculations in integer arithmetics results in having 500000000 in the resulting formula.

So this is actually a constant related to the hardware but it does not have a pre-determined name because it is derived from COUNT_ACC.

...

Quoted 25 lines
Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +11 -0
+static int cio2_vb2_queue_setup(struct vb2_queue *vq,
+                               unsigned int *num_buffers,
+                               unsigned int *num_planes,
+                               unsigned int sizes[],
+                               struct device *alloc_devs[])
+{
+       struct cio2_device *cio2 = vb2_get_drv_priv(vq);
+       struct cio2_queue *q = container_of(vq, struct cio2_queue, vbq);
+       u32 width = q->subdev_fmt.width;
+       u32 height = q->subdev_fmt.height;
+       u32 pixelformat = q->pixelformat;

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 784

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+       unsigned int i, szimage;
+       int r = 0;
+
+       for (i = 0; i < ARRAY_SIZE(cio2_csi2_fmts); i++) {
+               if (pixelformat == cio2_csi2_fmts[i])
+                       break;
+       }
+
+       /* Use SRGGB10 instead of return err */
+       if (i >= ARRAY_SIZE(cio2_csi2_fmts))

I think this should be impossible, since S_FMT should have already validated (and corrected) the setting.

Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 787

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +3 -0
+               pixelformat = V4L2_PIX_FMT_IPU3_SRGGB10;
+
+       alloc_devs[0] = &cio2->pci_dev->dev;

Hmm, so it doesn't go through the IPU MMU in the end?

No. The CSI-2 receiver isn't behind the MMU --- it's entirely separate from the ISP.

...

Quoted 6 lines
Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap)
+{
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
Quoted 8 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1026

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

If the device supports single plane formats only, I don't see a reason to use MPLANE buffer types.

Quoted 16 lines

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1497

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+
+       /* Initialize vbq */
+       vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       vbq->io_modes = VB2_USERPTR | VB2_MMAP;

VB2_DMABUF?

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1504

Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +7 -0
+       vbq->ops = &cio2_vb2_ops;
+       vbq->mem_ops = &vb2_dma_sg_memops;
+       vbq->buf_struct_size = sizeof(struct cio2_buffer);
+       vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+       vbq->min_buffers_needed = 1;
+       vbq->drv_priv = cio2;
+       vbq->lock = &q->lock;
Quoted 5 lines

Does the code take into account queue operations and video device operations being asynchronous regarding each other? Given that in this case there is always one queue per video device, maybe it would just make sense to use the same lock for both? (This happens if you leave vbq->lock with NULL.)

Using the same lock should be fine IMO.

Signature · 2 lines
Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#10 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Quoted 6 lines · Sakari Ailus
Quoted 4 lines · Tomasz Figa
Quoted 3 lines
(quoted diff) +2 -0
+       if (WARN_ON(freq <= 0))
+               return -EINVAL;
Quoted 6 lines
Quoted 3 lines

It generally doesn't make sense for the frequency to be negative, so maybe the argument should have been unsigned to start with? (And 32-bit if we don't expect frequencies higher than 4 GHz anyway.)

The value comes from a 64-bit integer V4L2 control so that implies the value range of s64 as well.

Okay, if there is no way to enforce this at control level, then I guess we have to keep this here.

Quoted 24 lines
Quoted 24 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+
+       /* b could be 0, -2 or -8, so r < 500000000 */

Definitely. Anything <= 0 is also less than 500000000. Let's take a look at the computation below again:

1) accinv is multiplied by b,
2) 500000000 is divided by 256 (=== shift right by 8 bits) = 1953125,
3) accinv*b is multiplied by 1953125 to form the value of r.

Now let's see at possible maximum absolute values for particular steps:
1) 16 * -8 = -128 (signed 8 bits),
2) 1953125 (unsigned 21 bits),
3) -128 * 1953125 = -249999872 (signed 29 bits).

So I think the important thing to note in the comment is:

/* b could be 0, -2 or -8, so |accinv * b| is always less than (1 << ds) and thus |r| < 500000000. */

Quoted 1 line

+ r = accinv * b * (500000000 >> ds);

Quoted 7 lines
Quoted 5 lines

On the other hand, you lose some precision here. If you used s64 instead and did the divide shift at the end ((accinv * b * 500000000)

Quoted 1 line
Quoted 1 line

ds), for the example above you would get -250007629. (Depending on

how big freq is, it might not matter, though.)

The frequency is typically hundreds of mega-Hertz.

I think it still would make sense to have the calculation a bit more precise.

Quoted 6 lines
Quoted 5 lines

Also nit: What is 500000000? We have local constants defined above, I think it could also make sense to do the same for this one. The compiler should do constant propagation and simplify respective calculations anyway.

Quoted 6 lines

COUNT_ACC in the formula in the comment a few decalines above is in nanoseconds. Performing the calculations in integer arithmetics results in having 500000000 in the resulting formula.

So this is actually a constant related to the hardware but it does not have a pre-determined name because it is derived from COUNT_ACC.

Which, I believe, doesn't stop us from naming it.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap)
+{
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
Quoted 8 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1026

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

Quoted 2 lines

If the device supports single plane formats only, I don't see a reason to use MPLANE buffer types.

On the other hand, if a further new revision of the hardware (or amendment of supported feature set of current hardware) actually adds support for multiple planes, changing it to MPLANE will require keeping a non-MPLANE variant of the code, due to userspace compatibility concerns...

Best regards,
Tomasz

#11 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Quoted 43 lines · Tomasz Figa
Quoted 6 lines · Sakari Ailus
Quoted 4 lines · Tomasz Figa
Quoted 3 lines
(quoted diff) +2 -0
+       if (WARN_ON(freq <= 0))
+               return -EINVAL;
Quoted 6 lines
Quoted 3 lines

It generally doesn't make sense for the frequency to be negative, so maybe the argument should have been unsigned to start with? (And 32-bit if we don't expect frequencies higher than 4 GHz anyway.)

The value comes from a 64-bit integer V4L2 control so that implies the value range of s64 as well.

Okay, if there is no way to enforce this at control level, then I guess we have to keep this here.

Quoted 26 lines
Quoted 26 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+
+       /* b could be 0, -2 or -8, so r < 500000000 */

Definitely. Anything <= 0 is also less than 500000000. Let's take a look at the computation below again:

1) accinv is multiplied by b,
2) 500000000 is divided by 256 (=== shift right by 8 bits) = 1953125,
3) accinv*b is multiplied by 1953125 to form the value of r.

Now let's see at possible maximum absolute values for particular steps:
1) 16 * -8 = -128 (signed 8 bits),
2) 1953125 (unsigned 21 bits),
3) -128 * 1953125 = -249999872 (signed 29 bits).

So I think the important thing to note in the comment is:

/* b could be 0, -2 or -8, so |accinv * b| is always less than (1 << ds) and thus |r| < 500000000. */

Quoted 1 line

+ r = accinv * b * (500000000 >> ds);

On the other hand, you lose some precision here. If you used s64 instead and did the divide shift at the end ((accinv * b * 500000000)

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

ds), for the example above you would get -250007629. (Depending on

how big freq is, it might not matter, though.)

The frequency is typically hundreds of mega-Hertz.

I think it still would make sense to have the calculation a bit more precise.

Then the solution is to divide by the 64-bit number, i.e. do_div(). IMO this shouldn't be a big deal either way: the result needs to be in a value range and this is only done once when streaming is started.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Also nit: What is 500000000? We have local constants defined above, I think it could also make sense to do the same for this one. The compiler should do constant propagation and simplify respective calculations anyway.

Quoted 8 lines
Quoted 6 lines

COUNT_ACC in the formula in the comment a few decalines above is in nanoseconds. Performing the calculations in integer arithmetics results in having 500000000 in the resulting formula.

So this is actually a constant related to the hardware but it does not have a pre-determined name because it is derived from COUNT_ACC.

Which, I believe, doesn't stop us from naming it.

No, but the value is derived from another value and used once. There's not much value in adding a macro for IMO.

The formula can be perhaps easier written as:

	accinv * a + (accinv * b * (500000000 >> ds)
		      / (int32_t)(link_freq >> ds));

If you insist, how about COUNT_ACC_FACTOR, for it's derived from COUNT_ACC?

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1026

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap)
+{
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

Quoted 8 lines
Quoted 2 lines

If the device supports single plane formats only, I don't see a reason to use MPLANE buffer types.

On the other hand, if a further new revision of the hardware (or amendment of supported feature set of current hardware) actually adds support for multiple planes, changing it to MPLANE will require keeping a non-MPLANE variant of the code, due to userspace compatibility concerns...

I think I have to correct my earlier statement --- the device supports multi-planar formats as well. They're only useful with SoC cameras though, not with raw Bayer cameras.

IMO VB2/V4L2 could better support conversion between single and multi-planar buffer types so that the applications could just use any and drivers could manage with one.

I don't have a strong opinion either way, but IMO this could be well addressed later on by improving the framework when (or if) the support for formats such as NV12 is added.

Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#12 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Quoted 111 lines · Sakari Ailus
Quoted 43 lines · Tomasz Figa
Quoted 6 lines · Sakari Ailus
Quoted 4 lines · Tomasz Figa
Quoted 3 lines
(quoted diff) +2 -0
+       if (WARN_ON(freq <= 0))
+               return -EINVAL;
Quoted 6 lines
Quoted 3 lines

It generally doesn't make sense for the frequency to be negative, so maybe the argument should have been unsigned to start with? (And 32-bit if we don't expect frequencies higher than 4 GHz anyway.)

The value comes from a 64-bit integer V4L2 control so that implies the value range of s64 as well.

Okay, if there is no way to enforce this at control level, then I guess we have to keep this here.

Quoted 26 lines
Quoted 26 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+
+       /* b could be 0, -2 or -8, so r < 500000000 */

Definitely. Anything <= 0 is also less than 500000000. Let's take a look at the computation below again:

1) accinv is multiplied by b,
2) 500000000 is divided by 256 (=== shift right by 8 bits) = 1953125,
3) accinv*b is multiplied by 1953125 to form the value of r.

Now let's see at possible maximum absolute values for particular steps:
1) 16 * -8 = -128 (signed 8 bits),
2) 1953125 (unsigned 21 bits),
3) -128 * 1953125 = -249999872 (signed 29 bits).

So I think the important thing to note in the comment is:

/* b could be 0, -2 or -8, so |accinv * b| is always less than (1 << ds) and thus |r| < 500000000. */

Quoted 1 line

+ r = accinv * b * (500000000 >> ds);

On the other hand, you lose some precision here. If you used s64 instead and did the divide shift at the end ((accinv * b * 500000000)

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

ds), for the example above you would get -250007629. (Depending on

how big freq is, it might not matter, though.)

The frequency is typically hundreds of mega-Hertz.

I think it still would make sense to have the calculation a bit more precise.

Then the solution is to divide by the 64-bit number, i.e. do_div(). IMO this shouldn't be a big deal either way: the result needs to be in a value range and this is only done once when streaming is started.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Also nit: What is 500000000? We have local constants defined above, I think it could also make sense to do the same for this one. The compiler should do constant propagation and simplify respective calculations anyway.

Quoted 8 lines
Quoted 6 lines

COUNT_ACC in the formula in the comment a few decalines above is in nanoseconds. Performing the calculations in integer arithmetics results in having 500000000 in the resulting formula.

So this is actually a constant related to the hardware but it does not have a pre-determined name because it is derived from COUNT_ACC.

Which, I believe, doesn't stop us from naming it.

No, but the value is derived from another value and used once. There's not much value in adding a macro for IMO.

The formula can be perhaps easier written as:

        accinv * a + (accinv * b * (500000000 >> ds)
                      / (int32_t)(link_freq >> ds));

If you insist, how about COUNT_ACC_FACTOR, for it's derived from COUNT_ACC?

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1026

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap)
+{
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

Quoted 8 lines
Quoted 2 lines

If the device supports single plane formats only, I don't see a reason to use MPLANE buffer types.

On the other hand, if a further new revision of the hardware (or amendment of supported feature set of current hardware) actually adds support for multiple planes, changing it to MPLANE will require keeping a non-MPLANE variant of the code, due to userspace compatibility concerns...

I think I have to correct my earlier statement --- the device supports multi-planar formats as well. They're only useful with SoC cameras though, not with raw Bayer cameras.

Quoted 7 lines

IMO VB2/V4L2 could better support conversion between single and multi-planar buffer types so that the applications could just use any and drivers could manage with one.

I don't have a strong opinion either way, but IMO this could be well addressed later on by improving the framework when (or if) the support for formats such as NV12 is added.

The problem is that it couldn't, because it would change the userspace ABI.

...and somehow I still recall (voice echoing in my head ;)) someone saying (writing) that single plane ABI is deprecated and all new drivers should be using MPLANE. Hans, was that you?

Best regards,
Tomasz

#13 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Quoted 116 lines · Tomasz Figa
Quoted 115 lines · Sakari Ailus
Quoted 43 lines · Tomasz Figa
Quoted 6 lines · Sakari Ailus
Quoted 4 lines · Tomasz Figa
Quoted 3 lines
(quoted diff) +2 -0
+       if (WARN_ON(freq <= 0))
+               return -EINVAL;
Quoted 6 lines
Quoted 3 lines

It generally doesn't make sense for the frequency to be negative, so maybe the argument should have been unsigned to start with? (And 32-bit if we don't expect frequencies higher than 4 GHz anyway.)

The value comes from a 64-bit integer V4L2 control so that implies the value range of s64 as well.

Okay, if there is no way to enforce this at control level, then I guess we have to keep this here.

Quoted 26 lines
Quoted 26 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+
+       /* b could be 0, -2 or -8, so r < 500000000 */

Definitely. Anything <= 0 is also less than 500000000. Let's take a look at the computation below again:

1) accinv is multiplied by b,
2) 500000000 is divided by 256 (=== shift right by 8 bits) = 1953125,
3) accinv*b is multiplied by 1953125 to form the value of r.

Now let's see at possible maximum absolute values for particular steps:
1) 16 * -8 = -128 (signed 8 bits),
2) 1953125 (unsigned 21 bits),
3) -128 * 1953125 = -249999872 (signed 29 bits).

So I think the important thing to note in the comment is:

/* b could be 0, -2 or -8, so |accinv * b| is always less than (1 << ds) and thus |r| < 500000000. */

Quoted 1 line

+ r = accinv * b * (500000000 >> ds);

On the other hand, you lose some precision here. If you used s64 instead and did the divide shift at the end ((accinv * b * 500000000)

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

ds), for the example above you would get -250007629. (Depending on

how big freq is, it might not matter, though.)

The frequency is typically hundreds of mega-Hertz.

I think it still would make sense to have the calculation a bit more precise.

Then the solution is to divide by the 64-bit number, i.e. do_div(). IMO this shouldn't be a big deal either way: the result needs to be in a value range and this is only done once when streaming is started.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Also nit: What is 500000000? We have local constants defined above, I think it could also make sense to do the same for this one. The compiler should do constant propagation and simplify respective calculations anyway.

Quoted 8 lines
Quoted 6 lines

COUNT_ACC in the formula in the comment a few decalines above is in nanoseconds. Performing the calculations in integer arithmetics results in having 500000000 in the resulting formula.

So this is actually a constant related to the hardware but it does not have a pre-determined name because it is derived from COUNT_ACC.

Which, I believe, doesn't stop us from naming it.

No, but the value is derived from another value and used once. There's not much value in adding a macro for IMO.

The formula can be perhaps easier written as:

        accinv * a + (accinv * b * (500000000 >> ds)
                      / (int32_t)(link_freq >> ds));

If you insist, how about COUNT_ACC_FACTOR, for it's derived from COUNT_ACC?

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1026

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap)
+{
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

Quoted 8 lines
Quoted 2 lines

If the device supports single plane formats only, I don't see a reason to use MPLANE buffer types.

On the other hand, if a further new revision of the hardware (or amendment of supported feature set of current hardware) actually adds support for multiple planes, changing it to MPLANE will require keeping a non-MPLANE variant of the code, due to userspace compatibility concerns...

I think I have to correct my earlier statement --- the device supports multi-planar formats as well. They're only useful with SoC cameras though, not with raw Bayer cameras.

IMO VB2/V4L2 could better support conversion between single and multi-planar buffer types so that the applications could just use any and drivers could manage with one.

Quoted 5 lines
Quoted 3 lines

I don't have a strong opinion either way, but IMO this could be well addressed later on by improving the framework when (or if) the support for formats such as NV12 is added.

The problem is that it couldn't, because it would change the userspace ABI.

Not if the driver supports single plane formats using both single-planar and multi-planar API. I don't think there's much missing from that. It'd make sense to do that anyway, independently of this driver.

Signature · 2 lines
Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#14 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

In reply to Sakari Ailus

Hi,

Quoted 7 lines · Sakari Ailus

IMO VB2/V4L2 could better support conversion between single and multi-planar buffer types so that the applications could just use any and drivers could manage with one.

I don't have a strong opinion either way, but IMO this could be well addressed later on by improving the framework when (or if) the support for formats such as NV12 is added.

We had already conversion between single and multi-planar buffer types in the kernel. But for some reasons it got removed. [1] The conversion is supposed to be done in libv4l2, which is not mandatory so it cannot be used to ensure backward compatibility while moving driver from one API to the other.

[1]
commit 1d0c86cad38678fa42f6d048a7b9e4057c8c16fc [media] media: v4l: remove single to multiplane conversion

Regards,
Sylwester

#15 lore raw

Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Hi Sylwester,

Quoted 17 lines · Sylwester Nawrocki

Hi,

Quoted 7 lines · Sakari Ailus

IMO VB2/V4L2 could better support conversion between single and multi-planar buffer types so that the applications could just use any and drivers could manage with one.

I don't have a strong opinion either way, but IMO this could be well addressed later on by improving the framework when (or if) the support for formats such as NV12 is added.

We had already conversion between single and multi-planar buffer types in the kernel. But for some reasons it got removed. [1] The conversion is supposed to be done in libv4l2, which is not mandatory so it cannot be used to ensure backward compatibility while moving driver from one API to the other.

Quoted 3 lines

[1]
commit 1d0c86cad38678fa42f6d048a7b9e4057c8c16fc [media] media: v4l: remove single to multiplane conversion

Thanks for the pointer. I had missed this back then.

Not all applications will be using libv4l2. This is something that would make sense to do in the kernel IMO. The changes seem pretty minimal to me, based on the patch.

There is now at least one difference between single-planar and multi-planar cases; the data_offset field is only present in struct v4l2_plane. That should be easy to address by adding the field to the single-planar case, too. (We'll need new buffer structs in the near future anyway, there's no really a way around that.)

Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
author maintainer
#16 lore raw

RE: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

In reply to Tomasz Figa

Hi, Tomasz,

Sorry for the late reply. I will omit the points that have been fixed in v4 or discussed earlier by either Tuukka or Sakari (https://patchwork.linuxtv.org/patch/41665)

Quoted 47 lines

-----Original Message-----
From: [email protected] [mailto:linux-media- [email protected]] On Behalf Of Tomasz Figa
Sent: Monday, June 12, 2017 2:59 AM
To: Zhi, Yong <[email protected]>
Cc: [email protected]; Sakari Ailus <[email protected]>; Zheng, Jian Xu <[email protected]>; Mani, Rajmohan <[email protected]>; Toivonen, Tuukka <[email protected]>; Hans Verkuil <[email protected]>; Yang, Hyungwoo <[email protected]>
Subject: Re: [PATCH v2 3/3] [media] intel-ipu3: cio2: Add new MIPI-CSI2 driver

Hi Yong,

Please see my comments inline.

Quoted 19 lines · Yong Zhi

This patch adds CIO2 CSI-2 device driver for Intel's IPU3 camera sub-system support.

---
 drivers/media/pci/Kconfig                |    2 +
 drivers/media/pci/Makefile               |    3 +-
 drivers/media/pci/intel/Makefile         |    5 +
 drivers/media/pci/intel/ipu3/Kconfig     |   17 +
 drivers/media/pci/intel/ipu3/Makefile    |    1 +
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 1788
++++++++++++++++++++++++++++++
drivers/media/pci/intel/ipu3/ipu3-cio2.h |  424 +++++++
 7 files changed, 2239 insertions(+), 1 deletion(-)  create mode
100644 drivers/media/pci/intel/Makefile  create mode 100644
drivers/media/pci/intel/ipu3/Kconfig
 create mode 100644 drivers/media/pci/intel/ipu3/Makefile
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.c
 create mode 100644 drivers/media/pci/intel/ipu3/ipu3-cio2.h

[snip]

Quoted 9 lines
drivers/media/pci/intel/ipu3/Kconfig new file +2 -0
new file mode 100644
index 0000000..2a895d6
@@ -0,0 +1,17 @@
+config VIDEO_IPU3_CIO2
+       tristate "Intel ipu3-cio2 driver"
Quoted 8 lines

drivers/media/pci/intel/ipu3/Kconfig line 6

Quoted 4 lines
drivers/media/pci/intel/ipu3/Kconfig new file +4 -0
+       depends on VIDEO_V4L2 && PCI
+       depends on MEDIA_CONTROLLER
+       depends on HAS_DMA
+       depends on ACPI

I wonder if it wouldn't make sense to make this depend on X86 (|| COMPILE_TEST) as well. Are we expecting a standalone PCI(e) card with this device in the future?

Will add depends on (X86 || COMPILE_TEST) && 64BIT

Quoted 40 lines
Quoted 26 lines
drivers/media/pci/intel/ipu3/Kconfig new file +11 -0
+       select V4L2_FWNODE
+       select VIDEOBUF2_DMA_SG
+
+       ---help---
+       This is the Intel IPU3 CIO2 CSI-2 receiver unit, found in Intel
+       Skylake and Kaby Lake SoCs and used for capturing images and
+       video from a camera sensor.
+
+       Say Y or M here if you have a Skylake/Kaby Lake SoC with MIPI CSI-2
+       connected camera.
+       The module will be called ipu3-cio2.
drivers/media/pci/intel/ipu3/Makefile new file +1 -0
new file mode 100644
index 0000000..20186e3
@@ -0,0 +1 @@
+obj-$(CONFIG_VIDEO_IPU3_CIO2) += ipu3-cio2.o
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +0 -0
new file mode 100644
index 0000000..69c47fc

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 875

Quoted 11 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +11 -0
+               u32 fbpt_rp =
+                       (readl(cio2->base + CIO2_REG_CDMARI(CIO2_DMA_CHAN))
+                        >> CIO2_CDMARI_FBPT_RP_SHIFT)
+                       & CIO2_CDMARI_FBPT_RP_MASK;
+
+               /*
+                * fbpt_rp is the fbpt entry that the dma is currently working
+                * on, but since it could jump to next entry at any time,
+                * assume that we might already be there.
+                */
+               fbpt_rp = (fbpt_rp + 1) % CIO2_MAX_BUFFERS;
Quoted 5 lines

Hmm, this is really racy. This code can be pre-empted and not execute for quite long time, depending on system load, resuming after the hardware goes even further. Technically you could prevent this using *_irq_save()/_irq_restore(), but I'd try to find a way that doesn't rely on the timing, if possible.

Ack
Will disable interrupts for the duration of this buffer queueing.

Quoted 15 lines

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +10 -0
+static int cio2_v4l2_querycap(struct file *file, void *fh,
+                             struct v4l2_capability *cap) {
+       struct cio2_device *cio2 = video_drvdata(file);
+
+       strlcpy(cap->driver, CIO2_NAME, sizeof(cap->driver));
+       strlcpy(cap->card, CIO2_DEVICE_NAME, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info),
+                "PCI:%s", pci_name(cio2->pci_dev));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
+ V4L2_CAP_STREAMING;

Hmm, I thought single plane queue type was deprecated these days and _MPLANE recommended for all new drivers. I'll defer this to other reviewers, though.

Will switch to MPLANE support in v5.

Quoted 20 lines
Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+       cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
+
+       return 0;
+}

[snip]

Quoted 14 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +14 -0
+static int cio2_v4l2_try_fmt(struct file *file, void *fh, struct
+v4l2_format *f) {
+       u32 pixelformat = f->fmt.pix.pixelformat;
+       unsigned int i;
+
+       cio2_v4l2_g_fmt(file, fh, f);
+
+       for (i = 0; i < ARRAY_SIZE(cio2_csi2_fmts); i++) {
+               if (pixelformat == cio2_csi2_fmts[i])
+                       break;
+       }
+
+       /* Use SRGGB10 as default if not found */
+       if (i >= ARRAY_SIZE(cio2_csi2_fmts))
Quoted 9 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c

Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +6 -0
+               pixelformat = V4L2_PIX_FMT_IPU3_SRGGB10;
+
+       f->fmt.pix.pixelformat = pixelformat;
+       f->fmt.pix.bytesperline = cio2_bytesperline(f->fmt.pix.width);
+       f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
+ f->fmt.pix.height;

Shouldn't you use f->fmt.pix_mp instead?

Agreed, will update here together with MPLANE support.

Quoted 9 lines

[snip]

drivers/media/pci/intel/ipu3/ipu3-cio2.c line 1497

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c new file +4 -0
+
+       /* Initialize vbq */
+       vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       vbq->io_modes = VB2_USERPTR | VB2_MMAP;

Best regards,
Tomasz