Skip to contentSkip to conversation

Intro to Hardware timestamping engine

RFC Superseded Dipen Patel 12 participants 78 messages Jun 25, 2021 – Sep 26, 2021 Page 2 of 2

Subsystems (7)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
author maintainer
#34 lore raw

Re: [RFC 03/11] hte: Add tegra194 HTE kernel provider

In reply to Jonathan Cameron

Thanks Jonathan, I accept all your comments, will be reflected in next RFC patch series.

Thanks for taking time to review and comment.

Best Regards,

Dipen Patel

Quoted 757 lines · Jonathan Cameron
Quoted 6 lines · On Fri, 25 Jun 2021 16:55:24 -0700 Dipen Patel <[email protected]> wrote:

Tegra194 device has multiple HTE instances also known as GTE (Generic hardware Timestamping Engine) which can timestamp subset of SoC lines/signals. This provider driver focuses on IRQ and GPIO lines and exposes timestamping ability on those lines to the consumers through HTE subsystem.

Quoted 6 lines

Also, with this patch, added:
- documentation about this provider and its capabilities at Documentation/hte.
- Compilation support in Makefile and Kconfig

A few comments inline,

J

Quoted 149 lines
---
 Documentation/hte/index.rst        |  21 ++
 Documentation/hte/tegra194-hte.rst |  65 ++++
 Documentation/index.rst            |   1 +
 drivers/hte/Kconfig                |  12 +
 drivers/hte/Makefile               |   1 +
 drivers/hte/hte-tegra194.c         | 554 +++++++++++++++++++++++++++++
 6 files changed, 654 insertions(+)
 create mode 100644 Documentation/hte/index.rst
 create mode 100644 Documentation/hte/tegra194-hte.rst
 create mode 100644 drivers/hte/hte-tegra194.c
Documentation/hte/index.rst new file +21 -0
new file mode 100644
index 000000000000..f311ebec6b47
@@ -0,0 +1,21 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+============================================
+The Linux Hardware Timestamping Engine (HTE)
+============================================
+
+The HTE Subsystem
+=================
+
+.. toctree::
+   :maxdepth: 1
+
+   hte
+
+HTE Tegra Provider
+==================
+
+.. toctree::
+   :maxdepth: 1
+
+   tegra194-hte
\ No newline at end of file
Documentation/hte/tegra194-hte.rst new file +65 -0
new file mode 100644
index 000000000000..c23eaafcf080
@@ -0,0 +1,65 @@
+HTE Kernel provider driver
+==========================
+
+Description
+-----------
+The Nvidia tegra194 chip has many hardware timestamping engine (HTE) instances
+known as generic timestamping engine (GTE). This provider driver implements
+two GTE instances 1) GPIO GTE and 2) IRQ GTE. The both GTEs instances get the
+timestamp from the system counter TSC which has 31.25MHz clock rate, and the
+driver converts clock tick rate to nano seconds before storing it as timestamp
+value.
+
+GPIO GTE
+--------
+
+This GTE instance help timestamps GPIO in real time, for that to happen GPIO
+needs to be configured as input and IRQ needs to ba enabled as well. The only
+always on (AON) gpio controller instance supports timestamping GPIOs in
+realtime and it has 39 GPIO lines. There is also a dependency on AON GPIO
+controller as it requires very specific bits to be set in GPIO config register.
+It in a way creates cyclic dependency between GTE and GPIO controller. The GTE
+GPIO functionality is accessed from the GPIOLIB. It can support both the in
+kernel and userspace consumers. In the later case, requests go through GPIOLIB
+CDEV framework. The below APIs are added in GPIOLIB framework to access HTE
+subsystem and GPIO GTE for in kernel consumers.
+
+.. c:function:: int gpiod_hw_timestamp_control( struct gpio_desc *desc, bool enable )
+
+	To enable HTE on given GPIO line.
+
+.. c:function:: u64 gpiod_get_hw_timestamp( struct gpio_desc *desc, bool block )
+
+	To retrieve hardwre timestamp in nano seconds.
+
+.. c:function:: bool gpiod_is_hw_timestamp_enabled( const struct gpio_desc *desc )
+
+	To query if HTE is enabled on the given GPIO.
+
+There is hte-tegra194-gpio-test.c, located in ``drivers/hte/`` directory, test
+driver which demonstrates above APIs for the Jetson AGX platform. For userspace
+consumers, GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE flag must be specifed during
+IOCTL calls, refer ``tools/gpio/gpio-event-mon.c``, which returns the timestamp
+in nano second.
+
+IRQ GTE
+--------
+
+This GTE instance helps timestamp LIC IRQ lines in real time. There are 352 IRQ
+lines which this instance can help timestamp realtime. The hte devicetree
+binding described at ``Documentation/devicetree/bindings/hte/`` gives out
+example how consumer can request IRQ line, since it is one to one mapping,
+consumers can simply specify IRQ number that they are interested in. There is
+no userspace consumer support for this GTE instance. The sample test code
+hte-tegra194-irq-test.c, located in ``drivers/hte/`` directory,
+demonstrates how to use IRQ GTE instance. The below is sample device tree
+snippet code for the test driver::
+
+ tegra_hte_irq_test {
+        compatible = "nvidia,tegra194-hte-irq-test";
+        htes = <&tegra_hte_lic 0x19>;
+        hte-names = "hte-lic";
+ };
+
+The source code of the driver both IRQ and GPIO GTE is locate at
+``drivers/hte/hte-tegra194.c``.
\ No newline at end of file
Documentation/index.rst +1 -0
index 1b13c2445e87..b41118577fe6 100644
@@ -138,6 +138,7 @@ needed).
    misc-devices/index
    scheduler/index
    mhi/index
+   hte/index
 
 Architecture-agnostic documentation
 -----------------------------------
drivers/hte/Kconfig +12 -0
index 394e112f7dfb..f7b01fcc7190 100644
@@ -20,3 +20,15 @@ menuconfig HTE
 
           If unsure, say no.
 
+if HTE
+
+config HTE_TEGRA194
+	tristate "NVIDIA Tegra194 HTE Support"
+	depends on ARCH_TEGRA_194_SOC
+	help
+	  Enable this option for integrated hardware timestamping engine also
+	  known as generic timestamping engine (GTE) support on NVIDIA Tegra194
+	  systems-on-chip. The driver supports 352 LIC IRQs and 39 AON GPIOs
+	  lines for timestamping in realtime.
+
+endif
drivers/hte/Makefile +0 -0
index 9899dbe516f7..52f978cfc913 100644

drivers/hte/Makefile line 2

Quoted 3 lines
drivers/hte/Makefile +1 -0
@@ -1 +1,2 @@
 obj-$(CONFIG_HTE)		+= hte.o
+obj-$(CONFIG_HTE_TEGRA194)	+= hte-tegra194.o
\ No newline at end of file

fix that

Quoted 435 lines
drivers/hte/hte-tegra194.c new file +429 -0
new file mode 100644
index 000000000000..8ad10efd3641
@@ -0,0 +1,554 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021 NVIDIA Corporation
+ *
+ * Author: Dipen Patel <[email protected]>
+ */
+
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/stat.h>
+#include <linux/interrupt.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/hte.h>
+#include <linux/uaccess.h>
+
+#define HTE_SUSPEND	0
+
+/* HTE source clock TSC is 31.25MHz */
+#define HTE_TS_CLK_RATE_HZ	31250000ULL
+#define HTE_CLK_RATE_NS		32
+#define HTE_TS_NS_SHIFT	__builtin_ctz(HTE_CLK_RATE_NS)
+
+#define NV_AON_SLICE_INVALID	-1
+
+/* AON HTE line map For slice 1 */
+#define NV_AON_HTE_SLICE1_IRQ_GPIO_28	12
+#define NV_AON_HTE_SLICE1_IRQ_GPIO_29	13
+
+/* AON HTE line map For slice 2 */
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_0	0
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_1	1
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_2	2
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_3	3
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_4	4
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_5	5
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_6	6
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_7	7
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_8	8
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_9	9
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_10	10
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_11	11
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_12	12
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_13	13
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_14	14
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_15	15
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_16	16
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_17	17
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_18	18
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_19	19
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_20	20
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_21	21
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_22	22
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_23	23
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_24	24
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_25	25
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_26	26
+#define NV_AON_HTE_SLICE2_IRQ_GPIO_27	27
+
+/* AON GPIO port AA pins */
+#define NV_AON_GPIO_PORT_AA_0		0
+#define NV_AON_GPIO_PORT_AA_1		1
+#define NV_AON_GPIO_PORT_AA_2		2
+#define NV_AON_GPIO_PORT_AA_3		3
+#define NV_AON_GPIO_PORT_AA_4		4
+#define NV_AON_GPIO_PORT_AA_5		5
+#define NV_AON_GPIO_PORT_AA_6		6
+#define NV_AON_GPIO_PORT_AA_7		7
+
+/* AON GPIO port BB pins */
+#define NV_AON_GPIO_PORT_BB_0		8
+#define NV_AON_GPIO_PORT_BB_1		9
+#define NV_AON_GPIO_PORT_BB_2		10
+#define NV_AON_GPIO_PORT_BB_3		11
+
+/* AON GPIO port CC pins */
+#define NV_AON_GPIO_PORT_CC_0		16
+#define NV_AON_GPIO_PORT_CC_1		17
+#define NV_AON_GPIO_PORT_CC_2		18
+#define NV_AON_GPIO_PORT_CC_3		19
+#define NV_AON_GPIO_PORT_CC_4		20
+#define NV_AON_GPIO_PORT_CC_5		21
+#define NV_AON_GPIO_PORT_CC_6		22
+#define NV_AON_GPIO_PORT_CC_7		23
+
+/* AON GPIO port DD pins */
+#define NV_AON_GPIO_PORT_DD_0		24
+#define NV_AON_GPIO_PORT_DD_1		25
+#define NV_AON_GPIO_PORT_DD_2		26
+
+/* AON GPIO port EE pins */
+#define NV_AON_GPIO_PORT_EE_0		32
+#define NV_AON_GPIO_PORT_EE_1		33
+#define NV_AON_GPIO_PORT_EE_2		34
+#define NV_AON_GPIO_PORT_EE_3		35
+#define NV_AON_GPIO_PORT_EE_4		36
+#define NV_AON_GPIO_PORT_EE_5		37
+#define NV_AON_GPIO_PORT_EE_6		38
+
+
+#define HTE_TECTRL		0x0
+#define HTE_TETSCH		0x4
+#define HTE_TETSCL		0x8
+#define HTE_TESRC		0xC
+#define HTE_TECCV		0x10
+#define HTE_TEPCV		0x14
+#define HTE_TECMD		0x1C
+#define HTE_TESTATUS		0x20
+#define HTE_SLICE0_TETEN	0x40
+#define HTE_SLICE1_TETEN	0x60
+
+#define HTE_SLICE_SIZE		(HTE_SLICE1_TETEN - HTE_SLICE0_TETEN)
+
+#define HTE_TECTRL_ENABLE_ENABLE	0x1
+
+#define HTE_TECTRL_OCCU_SHIFT		0x8
+#define HTE_TECTRL_INTR_SHIFT		0x1
+#define HTE_TECTRL_INTR_ENABLE		0x1
+
+#define HTE_TESRC_SLICE_SHIFT		16
+#define HTE_TESRC_SLICE_DEFAULT_MASK	0xFF
+
+#define HTE_TECMD_CMD_POP		0x1
+
+#define HTE_TESTATUS_OCCUPANCY_SHIFT	8
+#define HTE_TESTATUS_OCCUPANCY_MASK	0xFF
+
+struct hte_slices {
+	u32 r_val;
+	unsigned long flags;
+	/* to prevent lines mapped to same slice updating its register */
+	spinlock_t s_lock;
+};
+
+struct tegra_hte_line_mapped {
+	int slice;
+	u32 bit_index;
+};
+
+struct tegra_hte_line_table {
+	int map_sz;
+	const struct tegra_hte_line_mapped *map;
+};
+
+struct tegra_hte_soc {
+	int hte_irq;
+	u32 itr_thrshld;
+	u32 conf_rval;
+	struct hte_slices *sl;
+	const struct tegra_hte_line_table *line_map;
+	struct hte_chip *chip;
+	void __iomem *regs;
+};
+
+static const struct tegra_hte_line_mapped tegra194_aon_gpio_map[] = {
+	/* gpio, slice, bit_index */
+	[NV_AON_GPIO_PORT_AA_0]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_11},
+	[NV_AON_GPIO_PORT_AA_1]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_10},
+	[NV_AON_GPIO_PORT_AA_2]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_9},
+	[NV_AON_GPIO_PORT_AA_3]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_8},
+	[NV_AON_GPIO_PORT_AA_4]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_7},
+	[NV_AON_GPIO_PORT_AA_5]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_6},
+	[NV_AON_GPIO_PORT_AA_6]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_5},
+	[NV_AON_GPIO_PORT_AA_7]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_4},
+	[NV_AON_GPIO_PORT_BB_0]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_3},
+	[NV_AON_GPIO_PORT_BB_1]  = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_2},
+	[NV_AON_GPIO_PORT_BB_2] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_1},
+	[NV_AON_GPIO_PORT_BB_3] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_0},
+	[12] = {NV_AON_SLICE_INVALID, 0},
+	[13] = {NV_AON_SLICE_INVALID, 0},
+	[14] = {NV_AON_SLICE_INVALID, 0},
+	[15] = {NV_AON_SLICE_INVALID, 0},
+	[NV_AON_GPIO_PORT_CC_0] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_22},
+	[NV_AON_GPIO_PORT_CC_1] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_21},
+	[NV_AON_GPIO_PORT_CC_2] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_20},
+	[NV_AON_GPIO_PORT_CC_3] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_19},
+	[NV_AON_GPIO_PORT_CC_4] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_18},
+	[NV_AON_GPIO_PORT_CC_5] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_17},
+	[NV_AON_GPIO_PORT_CC_6] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_16},
+	[NV_AON_GPIO_PORT_CC_7] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_15},
+	[NV_AON_GPIO_PORT_DD_0] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_14},
+	[NV_AON_GPIO_PORT_DD_1] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_13},
+	[NV_AON_GPIO_PORT_DD_2] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_12},
+	[27] = {NV_AON_SLICE_INVALID, 0},
+	[28] = {NV_AON_SLICE_INVALID, 0},
+	[29] = {NV_AON_SLICE_INVALID, 0},
+	[30] = {NV_AON_SLICE_INVALID, 0},
+	[31] = {NV_AON_SLICE_INVALID, 0},
+	[NV_AON_GPIO_PORT_EE_0] = {1, NV_AON_HTE_SLICE1_IRQ_GPIO_29},
+	[NV_AON_GPIO_PORT_EE_1] = {1, NV_AON_HTE_SLICE1_IRQ_GPIO_28},
+	[NV_AON_GPIO_PORT_EE_2] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_27},
+	[NV_AON_GPIO_PORT_EE_3] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_26},
+	[NV_AON_GPIO_PORT_EE_4] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_25},
+	[NV_AON_GPIO_PORT_EE_5] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_24},
+	[NV_AON_GPIO_PORT_EE_6] = {2, NV_AON_HTE_SLICE2_IRQ_GPIO_23},
+};
+
+static const struct tegra_hte_line_table aon_hte_map = {
+	.map_sz = ARRAY_SIZE(tegra194_aon_gpio_map),
+	.map = tegra194_aon_gpio_map,
+};
+
+static inline u32 tegra_hte_readl(struct tegra_hte_soc *hte, u32 reg)
+{
+	return readl(hte->regs + reg);
+}
+
+static inline void tegra_hte_writel(struct tegra_hte_soc *hte, u32 reg,
+				    u32 val)
+{
+	writel(val, hte->regs + reg);
+}
+
+static inline int tegra_hte_map_to_line_id(u32 eid, struct tegra_hte_soc *gs,
+					  u32 *mapped)
+{
+	const struct tegra_hte_line_mapped *m;
+
+	if (gs->line_map) {
+		m = gs->line_map->map;
+		if (eid > gs->line_map->map_sz)
+			return -EINVAL;
+		if (m[eid].slice == NV_AON_SLICE_INVALID)
+			return -EINVAL;
+
+		*mapped = (m[eid].slice << 5) + m[eid].bit_index;
+	} else {
+		*mapped = eid;
+	}
+
+	return 0;
+}
+
+static int tegra_hte_line_xlate(struct hte_chip *gc,
+				 const struct of_phandle_args *args,
+				 struct hte_ts_desc *desc, u32 *xlated_id)
+{
+	int ret = 0;
+
+	if (!gc || !desc || !xlated_id)
+		return -EINVAL;
+
+	if (args) {
+		if (gc->of_hte_n_cells < 1)
+			return -EINVAL;
+
+		if (args->args_count != gc->of_hte_n_cells)
+			return -EINVAL;
+
+		desc->con_id = args->args[0];
+	}
+
+	ret = tegra_hte_map_to_line_id(desc->con_id, gc->data,
+				       xlated_id);
+	if (ret < 0) {
+		dev_dbg(gc->dev, "con_id:%u mapping failed\n",
+			desc->con_id);
+		return ret;
+	}
+
+	if (*xlated_id > gc->nlines)
+		return -EINVAL;
+
+	dev_dbg(gc->dev, "requested id:%u, xlated id:%u\n",
+		desc->con_id, *xlated_id);
+
+	return 0;
+}
+
+static int tegra_hte_en_dis_common(struct hte_chip *chip, u32 line_id, bool en)
+{
+	u32 slice, sl_bit_shift, line_bit, val, reg;
+	struct tegra_hte_soc *gs;
+
+	sl_bit_shift = __builtin_ctz(HTE_SLICE_SIZE);
+
+	if (!chip)
+		return -EINVAL;
+
+	gs = (struct tegra_hte_soc *)chip->data;
+
+	if (line_id > chip->nlines) {
+		dev_err(chip->dev,
+			"line id: %u is not supported by this controller\n",
+			line_id);
+		return -EINVAL;
+	}
+
+	slice = line_id >> sl_bit_shift;
+	line_bit = line_id & (HTE_SLICE_SIZE - 1);
+	reg = (slice << sl_bit_shift) + HTE_SLICE0_TETEN;
+
+	spin_lock(&gs->sl[slice].s_lock);
+
+	if (test_bit(HTE_SUSPEND, &gs->sl[slice].flags)) {
+		spin_unlock(&gs->sl[slice].s_lock);
+		dev_dbg(chip->dev, "device suspended");
+		return -EBUSY;
+	}
+
+	val = tegra_hte_readl(gs, reg);
+	if (en)
+		val = val | (1 << line_bit);
+	else
+		val = val & (~(1 << line_bit));
+	tegra_hte_writel(gs, reg, val);
+
+	spin_unlock(&gs->sl[slice].s_lock);
+
+	dev_dbg(chip->dev, "line: %u, slice %u, line_bit %u, reg:0x%x\n",
+		line_id, slice, line_bit, reg);
+
+	return 0;
+}
+
+static int tegra_hte_request(struct hte_chip *chip, u32 line_id)
+{
+	return tegra_hte_en_dis_common(chip, line_id, true);
+}
+
+static int tegra_hte_release(struct hte_chip *chip, u32 line_id)
+{
+	return tegra_hte_en_dis_common(chip, line_id, false);
+}
+
+static int tegra_hte_clk_src_info(struct hte_chip *chip,
+				  struct hte_clk_info *ci)
+{
+	(void)chip;
+
+	ci->hz = HTE_TS_CLK_RATE_HZ;
+	ci->type = CLOCK_MONOTONIC;
+
+	return 0;
+}
+
+static void tegra_hte_read_fifo(struct tegra_hte_soc *gs)
+{
+	u32 tsh, tsl, src, pv, cv, acv, slice, bit_index, line_id;
+	u64 tsc;
+	int dir;
+	struct hte_ts_data el;
+
+	while ((tegra_hte_readl(gs, HTE_TESTATUS) >>
+		HTE_TESTATUS_OCCUPANCY_SHIFT) &
+		HTE_TESTATUS_OCCUPANCY_MASK) {
+		tsh = tegra_hte_readl(gs, HTE_TETSCH);
+		tsl = tegra_hte_readl(gs, HTE_TETSCL);
+		tsc = (((u64)tsh << 32) | tsl);
+
+		src = tegra_hte_readl(gs, HTE_TESRC);
+		slice = (src >> HTE_TESRC_SLICE_SHIFT) &
+			    HTE_TESRC_SLICE_DEFAULT_MASK;
+
+		pv = tegra_hte_readl(gs, HTE_TEPCV);
+		cv = tegra_hte_readl(gs, HTE_TECCV);
+		acv = pv ^ cv;
+		while (acv) {
+			bit_index = __builtin_ctz(acv);
+			if ((pv >> bit_index) & BIT(0))
+				dir = HTE_EVENT_RISING_EDGE;
+			else
+				dir = HTE_EVENT_FALLING_EDGE;
+
+			line_id = bit_index + (slice << 5);
+			el.dir = dir;
+			el.tsc = tsc << HTE_TS_NS_SHIFT;
+			hte_push_ts_ns_atomic(gs->chip, line_id, &el,
+					      sizeof(el));
+			acv &= ~BIT(bit_index);
+		}
+		tegra_hte_writel(gs, HTE_TECMD, HTE_TECMD_CMD_POP);
+	}
+}
+
+static irqreturn_t tegra_hte_isr(int irq, void *dev_id)
+{
+	struct tegra_hte_soc *gs = dev_id;
+
+	tegra_hte_read_fifo(gs);
+
+	return IRQ_HANDLED;
+}
+
+static const struct of_device_id tegra_hte_of_match[] = {
+	{ .compatible = "nvidia,tegra194-gte-lic"},
+	{ .compatible = "nvidia,tegra194-gte-aon", .data = &aon_hte_map},
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tegra_hte_of_match);
+
+static const struct hte_ops g_ops = {
+	.request = tegra_hte_request,
+	.release = tegra_hte_release,
+	.enable = tegra_hte_request,
+	.disable = tegra_hte_release,
+	.get_clk_src_info = tegra_hte_clk_src_info,
+};
+
+static int tegra_hte_probe(struct platform_device *pdev)
+{
+	int ret;
+	u32 i, slices, val = 0;
+	struct device *dev;
+	struct tegra_hte_soc *hte_dev;
+	struct hte_chip *gc;
+
+	dev = &pdev->dev;
+
+	hte_dev = devm_kzalloc(dev, sizeof(*hte_dev), GFP_KERNEL);
+	if (!hte_dev)
+		return -ENOMEM;
+
+	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
+	if (!gc)
+		return -ENOMEM;
+
+	dev_set_drvdata(&pdev->dev, hte_dev);
+	hte_dev->line_map = of_device_get_match_data(&pdev->dev);
+
+	hte_dev->regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(hte_dev->regs))
+		return PTR_ERR(hte_dev->regs);
+
+	ret = of_property_read_u32(dev->of_node, "int-threshold",
+				   &hte_dev->itr_thrshld);

drivers/hte/hte-tegra194.c line 439

Quoted 10 lines
drivers/hte/hte-tegra194.c new file +10 -0
+	if (ret != 0)
+		hte_dev->itr_thrshld = 1;
+
+	ret = of_property_read_u32(dev->of_node, "slices", &slices);
+	if (ret != 0) {
+		dev_err(dev, "Could not read slices\n");
+		return -EINVAL;
+	}
+
+	hte_dev->sl = devm_kzalloc(dev, sizeof(struct hte_slices) * slices,

Preference for sizeof(*hte_dev->sl) as it saves me checking the size is correct for the type.

drivers/hte/hte-tegra194.c line 446

Quoted 7 lines
drivers/hte/hte-tegra194.c new file +7 -0
+				   GFP_KERNEL);
+	if (!hte_dev->sl)
+		return -ENOMEM;
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0) {
+		dev_err(dev, "get irq failed.\n");

dev_err_probe() probably so you don't print the message if deferred probing is going on.

Quoted 5 lines
drivers/hte/hte-tegra194.c new file +5 -0
+		return ret;
+	}
+	hte_dev->hte_irq = ret;
+	ret = devm_request_irq(dev, hte_dev->hte_irq, tegra_hte_isr, 0,
+			       dev_name(dev), hte_dev);

drivers/hte/hte-tegra194.c line 461

Quoted 10 lines
drivers/hte/hte-tegra194.c new file +10 -0
+	if (ret < 0) {
+		dev_err(dev, "request irq failed.\n");
+		return ret;
+	}
+
+	gc->nlines = slices << 5;
+	gc->ops = &g_ops;
+	gc->dev = dev;
+	hte_dev->chip = gc;
+	gc->data = (void *)hte_dev;

Don't case to void * - cast to the actual type if necessary.
If it is a void * then most likely it shouldn't be if we always put something in particular it in it.

drivers/hte/hte-tegra194.c line 466

Quoted 5 lines
drivers/hte/hte-tegra194.c new file +5 -0
+	gc->xlate = tegra_hte_line_xlate;
+	gc->of_hte_n_cells = 1;
+
+	ret = hte_register_chip(hte_dev->chip);
+

No blank line before error handler. Under the circumstances is that not fatal?

drivers/hte/hte-tegra194.c line 478

Quoted 12 lines
drivers/hte/hte-tegra194.c new file +12 -0
+	if (ret)
+		dev_err(gc->dev, "hte chip register failed");
+
+	for (i = 0; i < slices; i++) {
+		hte_dev->sl[i].flags = 0;
+		spin_lock_init(&hte_dev->sl[i].s_lock);
+	}
+
+	val = HTE_TECTRL_ENABLE_ENABLE |
+	      (HTE_TECTRL_INTR_ENABLE << HTE_TECTRL_INTR_SHIFT) |
+	      (hte_dev->itr_thrshld << HTE_TECTRL_OCCU_SHIFT);
+	tegra_hte_writel(hte_dev, HTE_TECTRL, val);

You could use a devm_add_action_or_reset() to deal with unwinding this plus add a devm_hte_register_chip() and then you can get rid of remove entirely which is always nice.

Quoted 5 lines
drivers/hte/hte-tegra194.c new file +5 -0
+
+	dev_dbg(gc->dev, "lines: %d, slices:%d", gc->nlines, slices);
+	return 0;
+}
+

drivers/hte/hte-tegra194.c line 493

Quoted 10 lines
drivers/hte/hte-tegra194.c new file +10 -0
+static int tegra_hte_remove(struct platform_device *pdev)
+{
+	struct tegra_hte_soc *gs = dev_get_drvdata(&pdev->dev);
+
+	tegra_hte_writel(gs, HTE_TECTRL, 0);
+
+	return hte_unregister_chip(gs->chip);
+}
+
+#ifdef CONFIG_PM_SLEEP

Personally I prefer the approach of marking PM functions __maybe_unused and dropping the ifdef protections. There have been a lot of subtle issues in the build system in the past around those and it's much easier to just let the compiler drop them if they are unused.

drivers/hte/hte-tegra194.c line 498

Quoted 5 lines
drivers/hte/hte-tegra194.c new file +5 -0
+static int tegra_hte_resume_early(struct device *dev)
+{
+	u32 i;
+	struct tegra_hte_soc *gs = dev_get_drvdata(dev);
+	u32 slices = gs->chip->nlines >> 5;

Whilst it's the same thing, I'd prefer a divide there by however lines there are in a slice.

Quoted 56 lines
drivers/hte/hte-tegra194.c new file +56 -0
+	u32 sl_bit_shift = __builtin_ctz(HTE_SLICE_SIZE);
+
+	tegra_hte_writel(gs, HTE_TECTRL, gs->conf_rval);
+
+	for (i = 0; i < slices; i++) {
+		spin_lock(&gs->sl[i].s_lock);
+		tegra_hte_writel(gs,
+				 ((i << sl_bit_shift) + HTE_SLICE0_TETEN),
+				 gs->sl[i].r_val);
+		clear_bit(HTE_SUSPEND, &gs->sl[i].flags);
+		spin_unlock(&gs->sl[i].s_lock);
+	}
+
+	return 0;
+}
+
+static int tegra_hte_suspend_late(struct device *dev)
+{
+	u32 i;
+	struct tegra_hte_soc *gs = dev_get_drvdata(dev);
+	u32 slices = gs->chip->nlines >> 5;
+	u32 sl_bit_shift = __builtin_ctz(HTE_SLICE_SIZE);
+
+	gs->conf_rval = tegra_hte_readl(gs, HTE_TECTRL);
+	for (i = 0; i < slices; i++) {
+		spin_lock(&gs->sl[i].s_lock);
+		gs->sl[i].r_val = tegra_hte_readl(gs,
+				((i << sl_bit_shift) + HTE_SLICE0_TETEN));
+		set_bit(HTE_SUSPEND, &gs->sl[i].flags);
+		spin_unlock(&gs->sl[i].s_lock);
+	}
+
+	return 0;
+}
+#endif
+
+static const struct dev_pm_ops tegra_hte_pm = {
+	SET_LATE_SYSTEM_SLEEP_PM_OPS(tegra_hte_suspend_late,
+				     tegra_hte_resume_early)
+};
+
+static struct platform_driver tegra_hte_driver = {
+	.probe = tegra_hte_probe,
+	.remove = tegra_hte_remove,
+	.driver = {
+		.name = "tegra_hte",
+		.pm = &tegra_hte_pm,
+		.of_match_table = tegra_hte_of_match,
+	},
+};
+
+module_platform_driver(tegra_hte_driver);
+
+MODULE_AUTHOR("Dipen Patel <[email protected]>");
+MODULE_DESCRIPTION("NVIDIA Tegra HTE (Hardware Timestamping Engine) driver");
+MODULE_LICENSE("GPL v2");
#35 lore raw

Re: [RFC 03/11] hte: Add tegra194 HTE kernel provider

In reply to Dipen Patel
Quoted 5 lines · Dipen Patel

Tegra194 device has multiple HTE instances also known as GTE (Generic hardware Timestamping Engine) which can timestamp subset of SoC lines/signals. This provider driver focuses on IRQ and GPIO lines and exposes timestamping ability on those lines to the consumers through HTE subsystem.

[...]

drivers/hte/hte-tegra194.c line 440

Quoted 8 lines
drivers/hte/hte-tegra194.c new file +8 -0
+	ret = of_property_read_u32(dev->of_node, "slices", &slices);
+	if (ret != 0) {
+		dev_err(dev, "Could not read slices\n");
+		return -EINVAL;
+	}
+
+	hte_dev->sl = devm_kzalloc(dev, sizeof(struct hte_slices) * slices,
+				   GFP_KERNEL);

Nit: There is devm_kcalloc() that will check for overflow in the multiply in case @slices from DT is broken.

Best Regards
Michał Mirosław

author maintainer
#36 lore raw

Re: [RFC 03/11] hte: Add tegra194 HTE kernel provider

Thanks for the suggestion, will change it in next RFC series.

Quoted 20 lines · Michał Mirosław
Quoted 5 lines · Dipen Patel

Tegra194 device has multiple HTE instances also known as GTE (Generic hardware Timestamping Engine) which can timestamp subset of SoC lines/signals. This provider driver focuses on IRQ and GPIO lines and exposes timestamping ability on those lines to the consumers through HTE subsystem.

[...]

drivers/hte/hte-tegra194.c line 440

Quoted 8 lines
drivers/hte/hte-tegra194.c new file +8 -0
+	ret = of_property_read_u32(dev->of_node, "slices", &slices);
+	if (ret != 0) {
+		dev_err(dev, "Could not read slices\n");
+		return -EINVAL;
+	}
+
+	hte_dev->sl = devm_kzalloc(dev, sizeof(struct hte_slices) * slices,
+				   GFP_KERNEL);

Nit: There is devm_kcalloc() that will check for overflow in the multiply in case @slices from DT is broken.

Best Regards
Michał Mirosław

author maintainer
#37 lore raw

RFC 4/11 dt-bindings: Add HTE bindings

In reply to Dipen Patel

Introduces HTE devicetree binding details for the HTE subsystem. It includes examples for the consumers, binding details for the providers and specific binding details for the Tegra194 based HTE providers.

---
 .../devicetree/bindings/hte/hte-consumer.yaml | 47 +++++++++++
 .../devicetree/bindings/hte/hte.yaml          | 34 ++++++++
 .../bindings/hte/nvidia,tegra194-hte.yaml     | 83 +++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml
Documentation/devicetree/bindings/hte/hte-consumer.yaml new file +47 -0
new file mode 100644
index 000000000000..79ae1f7d5185
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/hte-consumer.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HTE Consumer Device Tree Bindings
+
+maintainers:
+  - Dipen Patel <[email protected]>
+
+description: |
+  HTE properties should be named "htes". The exact meaning of each htes
+  property must be documented in the device tree binding for each device.
+  An optional property "hte-names" may contain a list of strings to label
+  each of the HTE devices listed in the "htes" property.
+
+  The "hte-names" property if specified is used to map the name of the HTE
+  device requested by the devm_of_hte_request_ts() or of_hte_request_ts
+  call to an index into the list given by the "htes" property.
+
+properties:
+  htes:
+    $ref: /schemas/types.yaml#/definitions/phandle-array
+    description:
+      The list of HTE provider phandle. The provider must document the number
+      of cell that must be passed in this property along with phandle.
+
+  hte-names:
+    $ref: /schemas/types.yaml#/definitions/string-array
+    description:
+      An optional string property.
+
+required:
+  - "htes"
+
+dependencies:
+  hte-names: [ htes ]
+
+additionalProperties: true
+
+examples:
+  - |
+    hte_irq_consumer {
+              htes = <&tegra_hte_lic 0x19>;
+              hte-names = "hte-irq";
+    };
Documentation/devicetree/bindings/hte/hte.yaml new file +34 -0
new file mode 100644
index 000000000000..e285c38f1a05
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/hte.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HTE providers
+
+maintainers:
+  - Dipen Patel <[email protected]>
+
+properties:
+  $nodename:
+    pattern: "^hte(@.*|-[0-9a-f])*$"
+
+  "#hte-cells":
+    description:
+      Number of cells in a HTE specifier.
+
+required:
+  - "#hte-cells"
+
+additionalProperties: true
+
+examples:
+  - |
+    tegra_hte_aon: hte@c1e0000 {
+              compatible = "nvidia,tegra194-gte-aon";
+              reg = <0xc1e0000 0x10000>;
+              interrupts = <0 13 0x4>;
+              int-threshold = <1>;
+              slices = <3>;
+              #hte-cells = <1>;
+    };
\ No newline at end of file
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.17.1
#38 lore raw

Re: [RFC 04/11] dt-bindings: Add HTE bindings

Hi Dipen,

thanks a lot for this very interesting patch set!

I'm gonna try to review properly, just pointing out some conceptual things to begin with. Bindings is a good place to start.

Documentation/devicetree/bindings/hte/hte-consumer.yaml line 16

Quoted 5 lines · Dipen Patel
Documentation/devicetree/bindings/hte/hte-consumer.yaml new file +5 -0
+description: |
+  HTE properties should be named "htes". The exact meaning of each htes
+  property must be documented in the device tree binding for each device.
+  An optional property "hte-names" may contain a list of strings to label
+  each of the HTE devices listed in the "htes" property.

I think this is a bit over-abbreviated. IIO has:
io-channels =...
io-channel-names =...

Given DT:s infatuation with using english plural I would opt for:
hardware-timestamps = ..
hardware-timestamp-names = ...

The "engine" part is a bit of an nVidia:ism I think and a too generic term. Could as well be "processor" or "automata" but nVidia just happened to name it an engine. (DMA engine would be a precedent though, so no hard preference from my side.)

When reading this it is pretty intuitively evident what is going on.

Other than that it looks really good!

Quoted 1 line

+++ b/Documentation/devicetree/bindings/hte/hte.yaml

I would name this hardware-timestamp-common.yamp or so.

Quoted 1 line

+title: HTE providers

Spell this out: Hardware timestamp providers

Documentation/devicetree/bindings/hte/hte.yaml line 14

Quoted 3 lines
Documentation/devicetree/bindings/hte/hte.yaml new file +3 -0
+properties:
+  $nodename:
+    pattern: "^hte(@.*|-[0-9a-f])*$"

Likewise:
hardware-timestamp@ ...

I think this is good because it is very unambiguous.

Documentation/devicetree/bindings/hte/hte.yaml line 34

Quoted 10 lines
Documentation/devicetree/bindings/hte/hte.yaml new file +10 -0
+examples:
+  - |
+    tegra_hte_aon: hte@c1e0000 {
+              compatible = "nvidia,tegra194-gte-aon";
+              reg = <0xc1e0000 0x10000>;
+              interrupts = <0 13 0x4>;
+              int-threshold = <1>;
+              slices = <3>;
+              #hte-cells = <1>;
+    };

The examples can be kept to the tegra194 bindings I think, this generic binding doesn't need an example as such.

Quoted 1 line

+$id: http://devicetree.org/schemas/hte/nvidia,tegra194-hte.yaml#

This one should be named like this, that is great.

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 7

Quoted 3 lines

+$schema: http://devicetree.org/meta-schemas/core.yaml#

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +2 -0
+
+title: Tegra194 on chip generic hardware timestamping engine (HTE)

This is clear and nice.

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 37

Quoted 6 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +6 -0
+  int-threshold:
+    description:
+      HTE device generates its interrupt based on this u32 FIFO threshold
+      value. The recommended value is 1.
+    minimum: 1
+    maximum: 256

Does this mean a single timestamp in the FIFO will generate an IRQ? Then spell that out so it is clear.

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 48

Quoted 10 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +10 -0
+  slices:
+   description:
+    HTE lines are arranged in 32 bit slice where each bit represents different
+    line/signal that it can enable/configure for the timestamp. It is u32
+    property and depends on the HTE instance in the chip.
+   oneOf:
+    - items:
+        - const: 3
+    - items:
+        - const: 11

Can't you just use
enum: [3, 11]
?

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 51

Quoted 2 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +2 -0
+  '#hte-cells':
+    const: 1

So IMO this would be something like
#hardware-timestamp-cells

Other than this it overall looks very nice to me!

Yours,
Linus Walleij

author maintainer
#39 lore raw

Re: [RFC 04/11] dt-bindings: Add HTE bindings

Quoted 14 lines · Linus Walleij

Hi Dipen,

thanks a lot for this very interesting patch set!

I'm gonna try to review properly, just pointing out some conceptual things to begin with. Bindings is a good place to start.

Documentation/devicetree/bindings/hte/hte-consumer.yaml line 16

Quoted 5 lines · Dipen Patel
Documentation/devicetree/bindings/hte/hte-consumer.yaml new file +5 -0
+description: |
+  HTE properties should be named "htes". The exact meaning of each htes
+  property must be documented in the device tree binding for each device.
+  An optional property "hte-names" may contain a list of strings to label
+  each of the HTE devices listed in the "htes" property.
Quoted 7 lines

I think this is a bit over-abbreviated. IIO has:
io-channels =...
io-channel-names =...

Given DT:s infatuation with using english plural I would opt for:
hardware-timestamps = ..
hardware-timestamp-names = ...

I can change it to suggested names in next RFC series.

Quoted 6 lines

The "engine" part is a bit of an nVidia:ism I think and a too generic term. Could as well be "processor" or "automata" but nVidia just happened to name it an engine. (DMA engine would be a precedent though, so no hard preference from my side.)

Quoted 6 lines

When reading this it is pretty intuitively evident what is going on.

Other than that it looks really good!

Quoted 1 line

+++ b/Documentation/devicetree/bindings/hte/hte.yaml

I would name this hardware-timestamp-common.yamp or so.

Sure, but do I have to change hte-consumer and other hte named

yaml as well in this directory? If yes, I am referring HTE everywhere  in the

code (framework is named as hte itself), I hope that is fine and does not

create any confusion.

Quoted 3 lines
Quoted 2 lines

+title: HTE providers

Spell this out: Hardware timestamp providers

Can I do hardware timestamp engine provider instead?

Quoted 19 lines

Documentation/devicetree/bindings/hte/hte.yaml line 14

Quoted 4 lines
Documentation/devicetree/bindings/hte/hte.yaml new file +3 -0
+properties:
+  $nodename:
+    pattern: "^hte(@.*|-[0-9a-f])*$"

Likewise:
hardware-timestamp@ ...

I think this is good because it is very unambiguous.

Documentation/devicetree/bindings/hte/hte.yaml line 34

Quoted 10 lines
Documentation/devicetree/bindings/hte/hte.yaml new file +10 -0
+examples:
+  - |
+    tegra_hte_aon: hte@c1e0000 {
+              compatible = "nvidia,tegra194-gte-aon";
+              reg = <0xc1e0000 0x10000>;
+              interrupts = <0 13 0x4>;
+              int-threshold = <1>;
+              slices = <3>;
+              #hte-cells = <1>;
+    };
Quoted 2 lines

The examples can be kept to the tegra194 bindings I think, this generic binding doesn't need an example as such.

Ok, will remove it.

Quoted 9 lines
Quoted 2 lines

+$id: http://devicetree.org/schemas/hte/nvidia,tegra194-hte.yaml#

This one should be named like this, that is great.

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 7

Quoted 3 lines

+$schema: http://devicetree.org/meta-schemas/core.yaml#

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +2 -0
+
+title: Tegra194 on chip generic hardware timestamping engine (HTE)

This is clear and nice.

Quoted 8 lines

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 37

Quoted 6 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +6 -0
+  int-threshold:
+    description:
+      HTE device generates its interrupt based on this u32 FIFO threshold
+      value. The recommended value is 1.
+    minimum: 1
+    maximum: 256

Does this mean a single timestamp in the FIFO will generate an IRQ? Then spell that out so it is clear.

In the description I said that.

Quoted 11 lines

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 48

Quoted 11 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +10 -0
+  slices:
+   description:
+    HTE lines are arranged in 32 bit slice where each bit represents different
+    line/signal that it can enable/configure for the timestamp. It is u32
+    property and depends on the HTE instance in the chip.
+   oneOf:
+    - items:
+        - const: 3
+    - items:
+        - const: 11
Quoted 3 lines

Can't you just use
enum: [3, 11]
?

Sure, will change it.

Quoted 5 lines

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 51

Quoted 3 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +2 -0
+  '#hte-cells':
+    const: 1

So IMO this would be something like
#hardware-timestamp-cells

Sure.

Quoted 5 lines

Other than this it overall looks very nice to me!

Yours,
Linus Walleij

maintainer
#40 lore raw

Re: [RFC 04/11] dt-bindings: Add HTE bindings

In reply to Dipen Patel
Quoted 5 lines · Dipen Patel

Introduces HTE devicetree binding details for the HTE subsystem. It includes examples for the consumers, binding details for the providers and specific binding details for the Tegra194 based HTE providers.

Quoted 9 lines
---
 .../devicetree/bindings/hte/hte-consumer.yaml | 47 +++++++++++
 .../devicetree/bindings/hte/hte.yaml          | 34 ++++++++
 .../bindings/hte/nvidia,tegra194-hte.yaml     | 83 +++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:
./Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml:40:4: [warning] wrong indentation: expected 4 but found 3 (indentation) ./Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml:41:5: [warning] wrong indentation: expected 5 but found 4 (indentation) ./Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml:45:5: [warning] wrong indentation: expected 5 but found 4 (indentation) ./Documentation/devicetree/bindings/hte/hte.yaml:34:7: [error] no new line character at the end of file (new-line-at-end-of-file)

dtschema/dtc warnings/errors:
\ndoc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/1497480

This check can fail if there are any dependencies. The base for a patch series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit.

author maintainer
#41 lore raw

Re: [RFC 04/11] dt-bindings: Add HTE bindings

Thanks Rob, I will correct those warning.

Quoted 40 lines · Rob Herring
Quoted 5 lines · Dipen Patel

Introduces HTE devicetree binding details for the HTE subsystem. It includes examples for the consumers, binding details for the providers and specific binding details for the Tegra194 based HTE providers.

Quoted 9 lines
---
 .../devicetree/bindings/hte/hte-consumer.yaml | 47 +++++++++++
 .../devicetree/bindings/hte/hte.yaml          | 34 ++++++++
 .../bindings/hte/nvidia,tegra194-hte.yaml     | 83 +++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:
./Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml:40:4: [warning] wrong indentation: expected 4 but found 3 (indentation) ./Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml:41:5: [warning] wrong indentation: expected 5 but found 4 (indentation) ./Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml:45:5: [warning] wrong indentation: expected 5 but found 4 (indentation) ./Documentation/devicetree/bindings/hte/hte.yaml:34:7: [error] no new line character at the end of file (new-line-at-end-of-file)

dtschema/dtc warnings/errors:
\ndoc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/1497480

This check can fail if there are any dependencies. The base for a patch series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit.

#42 lore raw

Re: [RFC 04/11] dt-bindings: Add HTE bindings

In reply to Dipen Patel
Quoted 115 lines · Dipen Patel

Introduces HTE devicetree binding details for the HTE subsystem. It includes examples for the consumers, binding details for the providers and specific binding details for the Tegra194 based HTE providers.

---
 .../devicetree/bindings/hte/hte-consumer.yaml | 47 +++++++++++
 .../devicetree/bindings/hte/hte.yaml          | 34 ++++++++
 .../bindings/hte/nvidia,tegra194-hte.yaml     | 83 +++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml
Documentation/devicetree/bindings/hte/hte-consumer.yaml new file +47 -0
new file mode 100644
index 000000000000..79ae1f7d5185
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/hte-consumer.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HTE Consumer Device Tree Bindings
+
+maintainers:
+  - Dipen Patel <[email protected]>
+
+description: |
+  HTE properties should be named "htes". The exact meaning of each htes
+  property must be documented in the device tree binding for each device.
+  An optional property "hte-names" may contain a list of strings to label
+  each of the HTE devices listed in the "htes" property.
+
+  The "hte-names" property if specified is used to map the name of the HTE
+  device requested by the devm_of_hte_request_ts() or of_hte_request_ts
+  call to an index into the list given by the "htes" property.
+
+properties:
+  htes:
+    $ref: /schemas/types.yaml#/definitions/phandle-array
+    description:
+      The list of HTE provider phandle. The provider must document the number
+      of cell that must be passed in this property along with phandle.
+
+  hte-names:
+    $ref: /schemas/types.yaml#/definitions/string-array
+    description:
+      An optional string property.
+
+required:
+  - "htes"
+
+dependencies:
+  hte-names: [ htes ]
+
+additionalProperties: true
+
+examples:
+  - |
+    hte_irq_consumer {
+              htes = <&tegra_hte_lic 0x19>;
+              hte-names = "hte-irq";
+    };
Documentation/devicetree/bindings/hte/hte.yaml new file +34 -0
new file mode 100644
index 000000000000..e285c38f1a05
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/hte.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HTE providers
+
+maintainers:
+  - Dipen Patel <[email protected]>
+
+properties:
+  $nodename:
+    pattern: "^hte(@.*|-[0-9a-f])*$"
+
+  "#hte-cells":
+    description:
+      Number of cells in a HTE specifier.
+
+required:
+  - "#hte-cells"
+
+additionalProperties: true
+
+examples:
+  - |
+    tegra_hte_aon: hte@c1e0000 {
+              compatible = "nvidia,tegra194-gte-aon";
+              reg = <0xc1e0000 0x10000>;
+              interrupts = <0 13 0x4>;
+              int-threshold = <1>;
+              slices = <3>;
+              #hte-cells = <1>;
+    };
\ No newline at end of file
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +0 -0
new file mode 100644
index 000000000000..bb76cc1971f0

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 7

Quoted 7 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +7 -0
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/nvidia,tegra194-hte.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Tegra194 on chip generic hardware timestamping engine (HTE)

I had to read until here to know what HTE is.

Is there another example of this type of h/w that this should be a generic binding?

Rob

author maintainer
#43 lore raw

Re: [RFC 04/11] dt-bindings: Add HTE bindings

Quoted 115 lines · Rob Herring
Quoted 114 lines · Dipen Patel

Introduces HTE devicetree binding details for the HTE subsystem. It includes examples for the consumers, binding details for the providers and specific binding details for the Tegra194 based HTE providers.

---
 .../devicetree/bindings/hte/hte-consumer.yaml | 47 +++++++++++
 .../devicetree/bindings/hte/hte.yaml          | 34 ++++++++
 .../bindings/hte/nvidia,tegra194-hte.yaml     | 83 +++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml
Documentation/devicetree/bindings/hte/hte-consumer.yaml new file +47 -0
new file mode 100644
index 000000000000..79ae1f7d5185
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/hte-consumer.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HTE Consumer Device Tree Bindings
+
+maintainers:
+  - Dipen Patel <[email protected]>
+
+description: |
+  HTE properties should be named "htes". The exact meaning of each htes
+  property must be documented in the device tree binding for each device.
+  An optional property "hte-names" may contain a list of strings to label
+  each of the HTE devices listed in the "htes" property.
+
+  The "hte-names" property if specified is used to map the name of the HTE
+  device requested by the devm_of_hte_request_ts() or of_hte_request_ts
+  call to an index into the list given by the "htes" property.
+
+properties:
+  htes:
+    $ref: /schemas/types.yaml#/definitions/phandle-array
+    description:
+      The list of HTE provider phandle. The provider must document the number
+      of cell that must be passed in this property along with phandle.
+
+  hte-names:
+    $ref: /schemas/types.yaml#/definitions/string-array
+    description:
+      An optional string property.
+
+required:
+  - "htes"
+
+dependencies:
+  hte-names: [ htes ]
+
+additionalProperties: true
+
+examples:
+  - |
+    hte_irq_consumer {
+              htes = <&tegra_hte_lic 0x19>;
+              hte-names = "hte-irq";
+    };
Documentation/devicetree/bindings/hte/hte.yaml new file +34 -0
new file mode 100644
index 000000000000..e285c38f1a05
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/hte.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: HTE providers
+
+maintainers:
+  - Dipen Patel <[email protected]>
+
+properties:
+  $nodename:
+    pattern: "^hte(@.*|-[0-9a-f])*$"
+
+  "#hte-cells":
+    description:
+      Number of cells in a HTE specifier.
+
+required:
+  - "#hte-cells"
+
+additionalProperties: true
+
+examples:
+  - |
+    tegra_hte_aon: hte@c1e0000 {
+              compatible = "nvidia,tegra194-gte-aon";
+              reg = <0xc1e0000 0x10000>;
+              interrupts = <0 13 0x4>;
+              int-threshold = <1>;
+              slices = <3>;
+              #hte-cells = <1>;
+    };
\ No newline at end of file
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +0 -0
new file mode 100644
index 000000000000..bb76cc1971f0
@@ -0,0 +1,83 @@
Quoted 8 lines

Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml line 7

Quoted 7 lines
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml new file +7 -0
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hte/nvidia,tegra194-hte.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Tegra194 on chip generic hardware timestamping engine (HTE)

I had to read until here to know what HTE is.

Do you think I should have added more description in commit message of hte.yaml for that matter?

Quoted 3 lines

Is there another example of this type of h/w that this should be a generic binding?

For now, I have only this hardware.

Quoted 2 lines

Rob

author maintainer
#44 lore raw

RFC 5/11 hte: Add Tegra194 IRQ HTE test driver

In reply to Dipen Patel

Tegra194 has IRQ HTE provider which can timestamp IRQ lines in realtime , this test driver implements consumer side which tests such provider through HTE subsystem. During its probe, it registers sysfs interface to easily navigate from userspace as below.

All the files are at /sys/kernel/tegra_hte_irq_test/.

- en_dis - Write only, Value 1 enables HTE line, 0 disables it
- buf_len - Write/Read, sets/gets per line buffer length
- watermark - Write/Read, sets/gets software threshold/watermark

Its devicetree detail can be accessed from
Documentation/hte/tegra194-hte.rst.

This driver can be compiled as loadable module and is tested on Jetson AGX platform using 0x19 IRQ line which belongs to one of the i2c controller 3160000.i2c.

i2cdetect -y 1 from the userspace on this platform should be enough to generate this IRQ at such point HTE should be able to generate timestamps for this test consumer driver.

---
 drivers/hte/Kconfig                 |   7 +
 drivers/hte/Makefile                |   3 +-
 drivers/hte/hte-tegra194-irq-test.c | 400 ++++++++++++++++++++++++++++
 3 files changed, 409 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hte/hte-tegra194-irq-test.c
drivers/hte/Kconfig +7 -0
index f7b01fcc7190..c4d335c41254 100644
@@ -31,4 +31,11 @@ config HTE_TEGRA194
 	  systems-on-chip. The driver supports 352 LIC IRQs and 39 AON GPIOs
 	  lines for timestamping in realtime.
 
+config HTE_TEGRA194_IRQ_TEST
+        tristate "NVIDIA Tegra194 HTE LIC IRQ Test"
+        depends on HTE_TEGRA194
+        help
+          The NVIDIA Tegra194 GTE IRQ test driver demonstrates HTE subsystem
+	  usage for the LIC IRQ hardware timestamp.
+
 endif
drivers/hte/Makefile +2 -1
index 52f978cfc913..b1cde6bc939b 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_HTE)		+= hte.o
-obj-$(CONFIG_HTE_TEGRA194)	+= hte-tegra194.o
\ No newline at end of file
+obj-$(CONFIG_HTE_TEGRA194)	+= hte-tegra194.o
+obj-$(CONFIG_HTE_TEGRA194_IRQ_TEST) += hte-tegra194-irq-test.o
\ No newline at end of file
drivers/hte/hte-tegra194-irq-test.c new file +400 -0

400 lines not on this page · raw message

Signature · 1 line
2.17.1
reviewer
#45 lore raw

Re: [RFC 05/11] hte: Add Tegra194 IRQ HTE test driver

Quoted 6 lines · Dipen Patel
drivers/hte/Kconfig +0 -0
index f7b01fcc7190..c4d335c41254 100644
@@ -31,4 +31,11 @@ config HTE_TEGRA194
 	  systems-on-chip. The driver supports 352 LIC IRQs and 39 AON GPIOs

drivers/hte/Kconfig config HTE_TEGRA194 · line 41

Quoted 10 lines
drivers/hte/Kconfig +7 -0
 	  lines for timestamping in realtime.
 
+config HTE_TEGRA194_IRQ_TEST
+        tristate "NVIDIA Tegra194 HTE LIC IRQ Test"
+        depends on HTE_TEGRA194
+        help
+          The NVIDIA Tegra194 GTE IRQ test driver demonstrates HTE subsystem
+	  usage for the LIC IRQ hardware timestamp.
+
 endif

Fix indentation, please.

Signature · 1 line
~Randy
author maintainer
#46 lore raw

RFC 6/11 gpiolib: Add HTE support

In reply to Dipen Patel

Some GPIO chip can provide hardware timestamp support on its GPIO lines , in order to support that additional functions needs to be added which can talk to both GPIO chip and HTE (hardware timestamping engine) subsystem. This patch introduces functions which gpio consumer can use to request hardware assisted timestamping. Below is the list of the APIs that are added in gpiolib subsystem.

- gpiod_hw_timestamp_control - to enable/disable HTE on specified GPIO line. This API will return HTE specific descriptor for the specified GPIO line during the enable call, it will be stored as pointer in the gpio_desc structure as hw_ts_data.
- gpiod_is_hw_timestamp_enabled - to query if HTE is enabled on specified GPIO line.
- gpiod_get_hw_timestamp - to retrieve hardware timestamps.

---
 drivers/gpio/gpiolib.c        | 92 +++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.h        | 11 +++++
 include/linux/gpio/consumer.h | 21 +++++++-
 include/linux/gpio/driver.h   | 13 +++++
 4 files changed, 135 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +92 -0
index 220a9d8dd4e3..335eaddfde98 100644
@@ -2361,6 +2361,98 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
 }
 EXPORT_SYMBOL_GPL(gpiod_direction_output);
 
+/**
+ * gpiod_hw_timestamp_control - set the hardware assisted timestamp control.
+ * @desc:	GPIO to set
+ * @enable:	Set true to enable the hardware timestamp, false otherwise.
+ *
+ * Certain GPIO chip can rely on hardware assisted timestamp engines which can
+ * record timestamp at the occurance of the configured events on selected GPIO
+ * lines. This is helper API to control such engine.
+ *
+ * Return 0 in case of success, else an error code.
+ */
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->timestamp_control) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+
+	ret = gc->timestamp_control(gc, gpio_chip_hwgpio(desc),
+				    &desc->hdesc, enable);
+
+	if (ret) {
+		gpiod_warn(desc,
+			   "%s: ts control operation failed\n", __func__);
+		return ret;
+	}
+
+	if (!enable)
+		desc->hdesc = NULL;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(gpiod_hw_timestamp_control);
+
+/**
+ * gpiod_is_hw_timestamp_enabled - check if hardware assisted timestamp is
+ * enabled.
+ * @desc:	GPIO to check
+ *
+ * Return true in case of success, false otherwise.
+ */
+bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc)
+{
+	if (!desc)
+		return false;
+
+	return (desc->hdesc) ? true : false;
+}
+EXPORT_SYMBOL_GPL(gpiod_is_hw_timestamp_enabled);
+
+/**
+ * gpiod_get_hw_timestamp - Get hardware timestamp in nano seconds.
+ * @desc:	GPIO to get the timestamp.
+ * @block:	Set true to block until data is available.
+ *
+ * Return non-zero on success, else 0.
+ */
+u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+	u64 ts;
+
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->get_hw_timestamp) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+
+	ret = gc->get_hw_timestamp(gc, block, desc->hdesc, &ts);
+	if (ret) {
+		gpiod_warn(desc,
+			   "%s: get timestamp operation failed\n", __func__);
+		return 0;
+	}
+
+	return ts;
+}
+EXPORT_SYMBOL_GPL(gpiod_get_hw_timestamp);
+
 /**
  * gpiod_set_config - sets @config for a GPIO
  * @desc: descriptor of the GPIO for which to set the configuration
drivers/gpio/gpiolib.h +11 -0
index 30bc3f80f83e..5393e1d90f61 100644
@@ -15,6 +15,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/cdev.h>
+#include <linux/hte.h>
 
 #define GPIOCHIP_NAME	"gpiochip"
 
@@ -117,6 +118,7 @@ struct gpio_desc {
 #define FLAG_EDGE_RISING     16	/* GPIO CDEV detects rising edge events */
 #define FLAG_EDGE_FALLING    17	/* GPIO CDEV detects falling edge events */
 #define FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
+#define FLAG_EVENT_CLOCK_HARDWARE	19 /* GPIO CDEV reports hardware timestamps in events */
 
 	/* Connection label */
 	const char		*label;
@@ -129,6 +131,15 @@ struct gpio_desc {
 	/* debounce period in microseconds */
 	unsigned int		debounce_period_us;
 #endif
+	/*
+	 * Hardware timestamp engine related internal data structure.
+	 * This gets set when the consumer calls gpiod_hw_timestamp_control to enable
+	 * hardware timestamping on the specified GPIO line. The API calls into HTE
+	 * subsystem, in turns HTE subsystem return the HTE descriptor for the GPIO
+	 * line. The hdesc will be later used with gpiod_is_hw_timestamp_enabled
+	 * and gpiod_get_hw_timestamp API calls.
+	 */
+	struct hte_ts_desc *hdesc;
 };
 
 #define gpiod_not_found(desc)		(IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
include/linux/gpio/consumer.h +19 -2
index c73b25bc9213..476ee04de7d0 100644
@@ -112,6 +112,9 @@ int gpiod_get_direction(struct gpio_desc *desc);
 int gpiod_direction_input(struct gpio_desc *desc);
 int gpiod_direction_output(struct gpio_desc *desc, int value);
 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable);
+bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc);
+u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block);
 
 /* Value get/set from non-sleeping context */
 int gpiod_get_value(const struct gpio_desc *desc);
@@ -353,8 +356,22 @@ static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
 	WARN_ON(desc);
 	return -ENOSYS;
 }
-
-
+static inline int gpiod_hw_timestamp_control(struct gpio_desc *desc,
+					     bool enable)
+{
+	WARN_ON(desc);
+	return -ENOSYS;
+}
+static inline bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc)
+{
+	WARN_ON(desc);
+	return false;
+}
+static inline u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block)
+{
+	WARN_ON(desc);
+	return 0;
+}
 static inline int gpiod_get_value(const struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
include/linux/gpio/driver.h +13 -0
index 3a268781fcec..f343e8f54b08 100644
@@ -10,6 +10,7 @@
 #include <linux/lockdep.h>
 #include <linux/pinctrl/pinctrl.h>
 #include <linux/pinctrl/pinconf-generic.h>
+#include <linux/hte.h> /* For hardware timestamping */
 
 struct gpio_desc;
 struct of_phandle_args;
@@ -304,6 +305,10 @@ struct gpio_irq_chip {
  * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
  *	requires special mapping of the pins that provides GPIO functionality.
  *	It is called after adding GPIO chip and before adding IRQ chip.
+ * @timestamp_control: Dependent on GPIO chip, an optional routine to
+ * 	enable/disable hardware assisted timestamp.
+ * @get_hw_timestamp: Retrieves hardware timestamp. The consumer can specify
+ * 	block parameter if it wishes to block till timestamp is available. 
  * @base: identifies the first GPIO number handled by this chip;
  *	or, if negative during registration, requests dynamic ID allocation.
  *	DEPRECATION: providing anything non-negative and nailing the base
@@ -396,6 +401,14 @@ struct gpio_chip {
 
 	int			(*add_pin_ranges)(struct gpio_chip *gc);
 
+	int			(*timestamp_control)(struct gpio_chip *gc,
+						     unsigned int offset,
+						     struct hte_ts_desc **hdesc,
+						     bool enable);
+	int			(*get_hw_timestamp)(struct gpio_chip *gc,
+						    bool block,
+						    struct hte_ts_desc *hdesc,
+						    u64 *ts);
 	int			base;
 	u16			ngpio;
 	const char		*const *names;
Signature · 1 line
2.17.1
#47 lore raw

Re: [RFC 06/11] gpiolib: Add HTE support

Quoted 15 lines · Dipen Patel

Some GPIO chip can provide hardware timestamp support on its GPIO lines , in order to support that additional functions needs to be added which can talk to both GPIO chip and HTE (hardware timestamping engine) subsystem. This patch introduces functions which gpio consumer can use to request hardware assisted timestamping. Below is the list of the APIs that are added in gpiolib subsystem.

- gpiod_hw_timestamp_control - to enable/disable HTE on specified GPIO line. This API will return HTE specific descriptor for the specified GPIO line during the enable call, it will be stored as pointer in the gpio_desc structure as hw_ts_data.
- gpiod_is_hw_timestamp_enabled - to query if HTE is enabled on specified GPIO line.
- gpiod_get_hw_timestamp - to retrieve hardware timestamps.

Quoted 1 line

This looks good to me.

The chip driver can look up and provide a timestamp provider for a certain line, which is proper since the GPIO hardware will be tightly coupled with the timestamp hardware so we need to ask the hardware about this directly and delegate it to the GPIO driver.

Yours,
Linus Walleij

reviewer
#48 lore raw

Re: [RFC 06/11] gpiolib: Add HTE support

In reply to Dipen Patel
Quoted 61 lines · Dipen Patel

Some GPIO chip can provide hardware timestamp support on its GPIO lines , in order to support that additional functions needs to be added which can talk to both GPIO chip and HTE (hardware timestamping engine) subsystem. This patch introduces functions which gpio consumer can use to request hardware assisted timestamping. Below is the list of the APIs that are added in gpiolib subsystem.

- gpiod_hw_timestamp_control - to enable/disable HTE on specified GPIO line. This API will return HTE specific descriptor for the specified GPIO line during the enable call, it will be stored as pointer in the gpio_desc structure as hw_ts_data.
- gpiod_is_hw_timestamp_enabled - to query if HTE is enabled on specified GPIO line.
- gpiod_get_hw_timestamp - to retrieve hardware timestamps.

---
 drivers/gpio/gpiolib.c        | 92 +++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.h        | 11 +++++
 include/linux/gpio/consumer.h | 21 +++++++-
 include/linux/gpio/driver.h   | 13 +++++
 4 files changed, 135 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +30 -0
index 220a9d8dd4e3..335eaddfde98 100644
@@ -2361,6 +2361,98 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
 }
 EXPORT_SYMBOL_GPL(gpiod_direction_output);
 
+/**
+ * gpiod_hw_timestamp_control - set the hardware assisted timestamp control.
+ * @desc:	GPIO to set
+ * @enable:	Set true to enable the hardware timestamp, false otherwise.
+ *
+ * Certain GPIO chip can rely on hardware assisted timestamp engines which can
+ * record timestamp at the occurance of the configured events on selected GPIO
+ * lines. This is helper API to control such engine.
+ *
+ * Return 0 in case of success, else an error code.
+ */
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->timestamp_control) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+
+	ret = gc->timestamp_control(gc, gpio_chip_hwgpio(desc),
+				    &desc->hdesc, enable);
+
+	if (ret) {

drivers/gpio/gpiolib.c int gpiod_direction_output(struct gpio_desc *desc, int value) · line 2403

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+		gpiod_warn(desc,
+			   "%s: ts control operation failed\n", __func__);
+		return ret;
+	}
+
+	if (!enable)
+		desc->hdesc = NULL;
+
+	return ret;
+}

Last I checked, pointer accesses are not guaranteed atomic, so how is hdesc protected from concurrent access?
Here is it modified unprotected.
Below it is read unprotected.

Quoted 31 lines
drivers/gpio/gpiolib.c +31 -0
+EXPORT_SYMBOL_GPL(gpiod_hw_timestamp_control);
+
+/**
+ * gpiod_is_hw_timestamp_enabled - check if hardware assisted timestamp is
+ * enabled.
+ * @desc:	GPIO to check
+ *
+ * Return true in case of success, false otherwise.
+ */
+bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc)
+{
+	if (!desc)
+		return false;
+
+	return (desc->hdesc) ? true : false;
+}
+EXPORT_SYMBOL_GPL(gpiod_is_hw_timestamp_enabled);
+
+/**
+ * gpiod_get_hw_timestamp - Get hardware timestamp in nano seconds.
+ * @desc:	GPIO to get the timestamp.
+ * @block:	Set true to block until data is available.
+ *
+ * Return non-zero on success, else 0.
+ */
+u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+	u64 ts;
+

drivers/gpio/gpiolib.c int gpiod_direction_output(struct gpio_desc *desc, int value) · line 2444

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->get_hw_timestamp) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+

Can't return an error code here. Return value is u64, so this will look like a valid ts.

Just return 0 on error, as you do immediately below...

Quoted 130 lines
drivers/gpio/gpiolib.c +11 -0
+	ret = gc->get_hw_timestamp(gc, block, desc->hdesc, &ts);
+	if (ret) {
+		gpiod_warn(desc,
+			   "%s: get timestamp operation failed\n", __func__);
+		return 0;
+	}
+
+	return ts;
+}
+EXPORT_SYMBOL_GPL(gpiod_get_hw_timestamp);
+
 /**
  * gpiod_set_config - sets @config for a GPIO
  * @desc: descriptor of the GPIO for which to set the configuration
drivers/gpio/gpiolib.h +11 -0
index 30bc3f80f83e..5393e1d90f61 100644
@@ -15,6 +15,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/cdev.h>
+#include <linux/hte.h>
 
 #define GPIOCHIP_NAME	"gpiochip"
 
@@ -117,6 +118,7 @@ struct gpio_desc {
 #define FLAG_EDGE_RISING     16	/* GPIO CDEV detects rising edge events */
 #define FLAG_EDGE_FALLING    17	/* GPIO CDEV detects falling edge events */
 #define FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
+#define FLAG_EVENT_CLOCK_HARDWARE	19 /* GPIO CDEV reports hardware timestamps in events */
 
 	/* Connection label */
 	const char		*label;
@@ -129,6 +131,15 @@ struct gpio_desc {
 	/* debounce period in microseconds */
 	unsigned int		debounce_period_us;
 #endif
+	/*
+	 * Hardware timestamp engine related internal data structure.
+	 * This gets set when the consumer calls gpiod_hw_timestamp_control to enable
+	 * hardware timestamping on the specified GPIO line. The API calls into HTE
+	 * subsystem, in turns HTE subsystem return the HTE descriptor for the GPIO
+	 * line. The hdesc will be later used with gpiod_is_hw_timestamp_enabled
+	 * and gpiod_get_hw_timestamp API calls.
+	 */
+	struct hte_ts_desc *hdesc;
 };
 
 #define gpiod_not_found(desc)		(IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
include/linux/gpio/consumer.h +19 -2
index c73b25bc9213..476ee04de7d0 100644
@@ -112,6 +112,9 @@ int gpiod_get_direction(struct gpio_desc *desc);
 int gpiod_direction_input(struct gpio_desc *desc);
 int gpiod_direction_output(struct gpio_desc *desc, int value);
 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable);
+bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc);
+u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block);
 
 /* Value get/set from non-sleeping context */
 int gpiod_get_value(const struct gpio_desc *desc);
@@ -353,8 +356,22 @@ static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
 	WARN_ON(desc);
 	return -ENOSYS;
 }
-
-
+static inline int gpiod_hw_timestamp_control(struct gpio_desc *desc,
+					     bool enable)
+{
+	WARN_ON(desc);
+	return -ENOSYS;
+}
+static inline bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc)
+{
+	WARN_ON(desc);
+	return false;
+}
+static inline u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block)
+{
+	WARN_ON(desc);
+	return 0;
+}
 static inline int gpiod_get_value(const struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
include/linux/gpio/driver.h +13 -0
index 3a268781fcec..f343e8f54b08 100644
@@ -10,6 +10,7 @@
 #include <linux/lockdep.h>
 #include <linux/pinctrl/pinctrl.h>
 #include <linux/pinctrl/pinconf-generic.h>
+#include <linux/hte.h> /* For hardware timestamping */
 
 struct gpio_desc;
 struct of_phandle_args;
@@ -304,6 +305,10 @@ struct gpio_irq_chip {
  * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
  *	requires special mapping of the pins that provides GPIO functionality.
  *	It is called after adding GPIO chip and before adding IRQ chip.
+ * @timestamp_control: Dependent on GPIO chip, an optional routine to
+ * 	enable/disable hardware assisted timestamp.
+ * @get_hw_timestamp: Retrieves hardware timestamp. The consumer can specify
+ * 	block parameter if it wishes to block till timestamp is available. 
  * @base: identifies the first GPIO number handled by this chip;
  *	or, if negative during registration, requests dynamic ID allocation.
  *	DEPRECATION: providing anything non-negative and nailing the base
@@ -396,6 +401,14 @@ struct gpio_chip {
 
 	int			(*add_pin_ranges)(struct gpio_chip *gc);
 
+	int			(*timestamp_control)(struct gpio_chip *gc,
+						     unsigned int offset,
+						     struct hte_ts_desc **hdesc,
+						     bool enable);
+	int			(*get_hw_timestamp)(struct gpio_chip *gc,
+						    bool block,
+						    struct hte_ts_desc *hdesc,
+						    u64 *ts);
 	int			base;
 	u16			ngpio;
 	const char		*const *names;
Signature · 1 line
2.17.1

Cheers,
Kent.

author maintainer
#49 lore raw

Re: [RFC 06/11] gpiolib: Add HTE support

Quoted 68 lines · Kent Gibson
Quoted 67 lines · Dipen Patel

Some GPIO chip can provide hardware timestamp support on its GPIO lines , in order to support that additional functions needs to be added which can talk to both GPIO chip and HTE (hardware timestamping engine) subsystem. This patch introduces functions which gpio consumer can use to request hardware assisted timestamping. Below is the list of the APIs that are added in gpiolib subsystem.

- gpiod_hw_timestamp_control - to enable/disable HTE on specified GPIO line. This API will return HTE specific descriptor for the specified GPIO line during the enable call, it will be stored as pointer in the gpio_desc structure as hw_ts_data.
- gpiod_is_hw_timestamp_enabled - to query if HTE is enabled on specified GPIO line.
- gpiod_get_hw_timestamp - to retrieve hardware timestamps.

---
 drivers/gpio/gpiolib.c        | 92 +++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.h        | 11 +++++
 include/linux/gpio/consumer.h | 21 +++++++-
 include/linux/gpio/driver.h   | 13 +++++
 4 files changed, 135 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +36 -0
index 220a9d8dd4e3..335eaddfde98 100644
@@ -2361,6 +2361,98 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
 }
 EXPORT_SYMBOL_GPL(gpiod_direction_output);
 
+/**
+ * gpiod_hw_timestamp_control - set the hardware assisted timestamp control.
+ * @desc:	GPIO to set
+ * @enable:	Set true to enable the hardware timestamp, false otherwise.
+ *
+ * Certain GPIO chip can rely on hardware assisted timestamp engines which can
+ * record timestamp at the occurance of the configured events on selected GPIO
+ * lines. This is helper API to control such engine.
+ *
+ * Return 0 in case of success, else an error code.
+ */
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->timestamp_control) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+
+	ret = gc->timestamp_control(gc, gpio_chip_hwgpio(desc),
+				    &desc->hdesc, enable);
+
+	if (ret) {
+		gpiod_warn(desc,
+			   "%s: ts control operation failed\n", __func__);
+		return ret;
+	}
+
+	if (!enable)
Quoted 8 lines

drivers/gpio/gpiolib.c int gpiod_direction_output(struct gpio_desc *desc, int value) · line 2403

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+		desc->hdesc = NULL;
+
+	return ret;
+}

Last I checked, pointer accesses are not guaranteed atomic, so how is hdesc protected from concurrent access?
Here is it modified unprotected.
Below it is read unprotected.

The assumption I made here was, gpiod_hw_timestamp_control will be

called after client has made at least gpdio_request call. With that assumption,

how two or more client/consumers call gpiod_hw_timestamp_control API

with the same gpio_desc? I believe its not allowed as gpiod_request call will

fail for the looser if there is a race and hence there will not be any race here

in this API. Let me know your thoughts.

Quoted 45 lines
Quoted 32 lines
drivers/gpio/gpiolib.c +31 -0
+EXPORT_SYMBOL_GPL(gpiod_hw_timestamp_control);
+
+/**
+ * gpiod_is_hw_timestamp_enabled - check if hardware assisted timestamp is
+ * enabled.
+ * @desc:	GPIO to check
+ *
+ * Return true in case of success, false otherwise.
+ */
+bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc)
+{
+	if (!desc)
+		return false;
+
+	return (desc->hdesc) ? true : false;
+}
+EXPORT_SYMBOL_GPL(gpiod_is_hw_timestamp_enabled);
+
+/**
+ * gpiod_get_hw_timestamp - Get hardware timestamp in nano seconds.
+ * @desc:	GPIO to get the timestamp.
+ * @block:	Set true to block until data is available.
+ *
+ * Return non-zero on success, else 0.
+ */
+u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+	u64 ts;
+

drivers/gpio/gpiolib.c int gpiod_direction_output(struct gpio_desc *desc, int value) · line 2444

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->get_hw_timestamp) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+

Can't return an error code here. Return value is u64, so this will look like a valid ts.

Quoted 1 line

Just return 0 on error, as you do immediately below...

yes, good catch. I forgot to clean that up.

Quoted 133 lines
Quoted 131 lines
drivers/gpio/gpiolib.c +11 -0
+	ret = gc->get_hw_timestamp(gc, block, desc->hdesc, &ts);
+	if (ret) {
+		gpiod_warn(desc,
+			   "%s: get timestamp operation failed\n", __func__);
+		return 0;
+	}
+
+	return ts;
+}
+EXPORT_SYMBOL_GPL(gpiod_get_hw_timestamp);
+
 /**
  * gpiod_set_config - sets @config for a GPIO
  * @desc: descriptor of the GPIO for which to set the configuration
drivers/gpio/gpiolib.h +11 -0
index 30bc3f80f83e..5393e1d90f61 100644
@@ -15,6 +15,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/cdev.h>
+#include <linux/hte.h>
 
 #define GPIOCHIP_NAME	"gpiochip"
 
@@ -117,6 +118,7 @@ struct gpio_desc {
 #define FLAG_EDGE_RISING     16	/* GPIO CDEV detects rising edge events */
 #define FLAG_EDGE_FALLING    17	/* GPIO CDEV detects falling edge events */
 #define FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
+#define FLAG_EVENT_CLOCK_HARDWARE	19 /* GPIO CDEV reports hardware timestamps in events */
 
 	/* Connection label */
 	const char		*label;
@@ -129,6 +131,15 @@ struct gpio_desc {
 	/* debounce period in microseconds */
 	unsigned int		debounce_period_us;
 #endif
+	/*
+	 * Hardware timestamp engine related internal data structure.
+	 * This gets set when the consumer calls gpiod_hw_timestamp_control to enable
+	 * hardware timestamping on the specified GPIO line. The API calls into HTE
+	 * subsystem, in turns HTE subsystem return the HTE descriptor for the GPIO
+	 * line. The hdesc will be later used with gpiod_is_hw_timestamp_enabled
+	 * and gpiod_get_hw_timestamp API calls.
+	 */
+	struct hte_ts_desc *hdesc;
 };
 
 #define gpiod_not_found(desc)		(IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
include/linux/gpio/consumer.h +19 -2
index c73b25bc9213..476ee04de7d0 100644
@@ -112,6 +112,9 @@ int gpiod_get_direction(struct gpio_desc *desc);
 int gpiod_direction_input(struct gpio_desc *desc);
 int gpiod_direction_output(struct gpio_desc *desc, int value);
 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable);
+bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc);
+u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block);
 
 /* Value get/set from non-sleeping context */
 int gpiod_get_value(const struct gpio_desc *desc);
@@ -353,8 +356,22 @@ static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
 	WARN_ON(desc);
 	return -ENOSYS;
 }
-
-
+static inline int gpiod_hw_timestamp_control(struct gpio_desc *desc,
+					     bool enable)
+{
+	WARN_ON(desc);
+	return -ENOSYS;
+}
+static inline bool gpiod_is_hw_timestamp_enabled(const struct gpio_desc *desc)
+{
+	WARN_ON(desc);
+	return false;
+}
+static inline u64 gpiod_get_hw_timestamp(struct gpio_desc *desc, bool block)
+{
+	WARN_ON(desc);
+	return 0;
+}
 static inline int gpiod_get_value(const struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
include/linux/gpio/driver.h +13 -0
index 3a268781fcec..f343e8f54b08 100644
@@ -10,6 +10,7 @@
 #include <linux/lockdep.h>
 #include <linux/pinctrl/pinctrl.h>
 #include <linux/pinctrl/pinconf-generic.h>
+#include <linux/hte.h> /* For hardware timestamping */
 
 struct gpio_desc;
 struct of_phandle_args;
@@ -304,6 +305,10 @@ struct gpio_irq_chip {
  * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
  *	requires special mapping of the pins that provides GPIO functionality.
  *	It is called after adding GPIO chip and before adding IRQ chip.
+ * @timestamp_control: Dependent on GPIO chip, an optional routine to
+ * 	enable/disable hardware assisted timestamp.
+ * @get_hw_timestamp: Retrieves hardware timestamp. The consumer can specify
+ * 	block parameter if it wishes to block till timestamp is available. 
  * @base: identifies the first GPIO number handled by this chip;
  *	or, if negative during registration, requests dynamic ID allocation.
  *	DEPRECATION: providing anything non-negative and nailing the base
@@ -396,6 +401,14 @@ struct gpio_chip {
 
 	int			(*add_pin_ranges)(struct gpio_chip *gc);
 
+	int			(*timestamp_control)(struct gpio_chip *gc,
+						     unsigned int offset,
+						     struct hte_ts_desc **hdesc,
+						     bool enable);
+	int			(*get_hw_timestamp)(struct gpio_chip *gc,
+						    bool block,
+						    struct hte_ts_desc *hdesc,
+						    u64 *ts);
 	int			base;
 	u16			ngpio;
 	const char		*const *names;
Signature · 1 line
2.17.1

Cheers,
Kent.

reviewer
#50 lore raw

Re: [RFC 06/11] gpiolib: Add HTE support

Quoted 83 lines · Dipen Patel
Quoted 68 lines · Kent Gibson
Quoted 67 lines · Dipen Patel

Some GPIO chip can provide hardware timestamp support on its GPIO lines , in order to support that additional functions needs to be added which can talk to both GPIO chip and HTE (hardware timestamping engine) subsystem. This patch introduces functions which gpio consumer can use to request hardware assisted timestamping. Below is the list of the APIs that are added in gpiolib subsystem.

- gpiod_hw_timestamp_control - to enable/disable HTE on specified GPIO line. This API will return HTE specific descriptor for the specified GPIO line during the enable call, it will be stored as pointer in the gpio_desc structure as hw_ts_data.
- gpiod_is_hw_timestamp_enabled - to query if HTE is enabled on specified GPIO line.
- gpiod_get_hw_timestamp - to retrieve hardware timestamps.

---
 drivers/gpio/gpiolib.c        | 92 +++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.h        | 11 +++++
 include/linux/gpio/consumer.h | 21 +++++++-
 include/linux/gpio/driver.h   | 13 +++++
 4 files changed, 135 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +36 -0
index 220a9d8dd4e3..335eaddfde98 100644
@@ -2361,6 +2361,98 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
 }
 EXPORT_SYMBOL_GPL(gpiod_direction_output);
 
+/**
+ * gpiod_hw_timestamp_control - set the hardware assisted timestamp control.
+ * @desc:	GPIO to set
+ * @enable:	Set true to enable the hardware timestamp, false otherwise.
+ *
+ * Certain GPIO chip can rely on hardware assisted timestamp engines which can
+ * record timestamp at the occurance of the configured events on selected GPIO
+ * lines. This is helper API to control such engine.
+ *
+ * Return 0 in case of success, else an error code.
+ */
+int gpiod_hw_timestamp_control(struct gpio_desc *desc, bool enable)
+{
+	struct gpio_chip	*gc;
+	int			ret = 0;
+
+	VALIDATE_DESC(desc);
+	gc = desc->gdev->chip;
+
+	if (!gc->timestamp_control) {
+		gpiod_warn(desc,
+			   "%s: Hardware assisted ts not supported\n",
+			   __func__);
+		return -ENOTSUPP;
+	}
+
+	ret = gc->timestamp_control(gc, gpio_chip_hwgpio(desc),
+				    &desc->hdesc, enable);
+
+	if (ret) {
+		gpiod_warn(desc,
+			   "%s: ts control operation failed\n", __func__);
+		return ret;
+	}
+
+	if (!enable)
Quoted 8 lines

drivers/gpio/gpiolib.c int gpiod_direction_output(struct gpio_desc *desc, int value) · line 2403

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+		desc->hdesc = NULL;
+
+	return ret;
+}

Last I checked, pointer accesses are not guaranteed atomic, so how is hdesc protected from concurrent access?
Here is it modified unprotected.
Below it is read unprotected.

The assumption I made here was, gpiod_hw_timestamp_control will be

called after client has made at least gpdio_request call. With that assumption,

Quoted 8 lines

how two or more client/consumers call gpiod_hw_timestamp_control API

with the same gpio_desc? I believe its not allowed as gpiod_request call will

fail for the looser if there is a race and hence there will not be any race here

in this API. Let me know your thoughts.

My assumptions are that the userspace client is multi-threaded and that there is nothing preventing concurrent uAPI calls, including closing the line request fd.

The specific case I had in mind is one thread closing the req fd while another is using set_config to switch to the hardware event clock. In that race, the close be calling linereq_free() at the same time the linereq_set_config_unlocked() is being called. Both of those functions make calls to the functions here that read and write the hdesc.

There may be others, e.g. line_event_timestamp() running in the irq_thread at the same time a set_config call switches the event clock away from the hardware clock.

So assume concurrent access unless you can prove otherwise.

Cheers,
Kent.

author maintainer
#51 lore raw

RFC 7/11 gpio: tegra186: Add HTE in gpio-tegra186 driver

In reply to Dipen Patel

Tegra194 AON GPIO controller with the use of its internal hardware timestamping engine (HTE) also known as GTE can timestamp its GPIO lines through system counter. This patch implements two callbacks which are essential for the gpio consumers that want such HTE functionality. The callbacks details can be found at include/gpio/driver.h.

Since AON GPIO controller depends on HTE engine, it creates hardware dependency between controller and AON HTE provider. To express that, the optional devicetree property is introduced for AON GPIO controller.

---
 .../bindings/gpio/nvidia,tegra186-gpio.txt    |  7 ++
 drivers/gpio/gpio-tegra186.c                  | 78 +++++++++++++++++++
 2 files changed, 85 insertions(+)
Documentation/devicetree/bindings/gpio/nvidia,tegra186-gpio.txt +7 -0
index adff16c71d21..00a3e47ab560 100644
@@ -127,6 +127,12 @@ Required properties:
             - 8: Active low level-sensitive.
             Valid combinations are 1, 2, 3, 4, 8.
 
+Optional properties:
+- timestamp-engine
+    AON GPIO controller has timestamp engine which can hardware timestamp
+    GPIO configured as input and IRQ. This property specifies hardware
+    timestamp engine (HTE) device-tree node.
+
 Example:
 
 #include <dt-bindings/interrupt-controller/irq.h>
@@ -162,4 +168,5 @@ gpio@c2f0000 {
 	#gpio-cells = <2>;
 	interrupt-controller;
 	#interrupt-cells = <2>;
+	timestamp-engine = <&tegra_hte_aon>;
 };
drivers/gpio/gpio-tegra186.c +78 -0

114 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#52 lore raw

RFC 8/11 gpiolib: cdev: Add hardware timestamp clock type

In reply to Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
 drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++--
 include/uapi/linux/gpio.h   |  1 +
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +63 -2
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
 	 GPIO_V2_LINE_DRIVE_FLAGS | \
 	 GPIO_V2_LINE_EDGE_FLAGS | \
 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
 	 GPIO_V2_LINE_BIAS_FLAGS)
 
 static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
 
 static u64 line_event_timestamp(struct line *line)
 {
+	bool block;
+
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
 		return ktime_get_real_ns();
 
+	if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {
+		if (irq_count())
+			block = false;
+		else
+			block = true;
+
+		return gpiod_get_hw_timestamp(line->desc, block);
+	}
+
 	return ktime_get_ns();
 }
 
@@ -828,6 +840,7 @@ static int edge_detector_setup(struct line *line,
 		return ret;
 
 	line->irq = irq;
+
 	return 0;
 }
 
@@ -891,7 +904,6 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	/* Return an error if an unknown flag is set */
 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
 		return -EINVAL;
-
 	/*
 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
 	 * contradictory.
@@ -900,6 +912,14 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
 		return -EINVAL;
 
+	/*
+	 * Do not mix with any other clocks if hardware assisted timestamp is
+	 * asked.
+	 */
+	if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
+	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE))
+		return -EINVAL;
+
 	/* Edge detection requires explicit input. */
 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
@@ -992,6 +1012,8 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
 
 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
+	assign_bit(FLAG_EVENT_CLOCK_HARDWARE, flagsp,
+		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE);
 }
 
 static long linereq_get_values(struct linereq *lr, void __user *ip)
@@ -1139,6 +1161,18 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 			int val = gpio_v2_line_config_output_value(lc, i);
 
 			edge_detector_stop(&lr->lines[i]);
+
+			/*
+			 * Assuming line was input before and hardware
+			 * assisted timestamp only timestamps the input
+			 * lines.
+			 */
+			if (gpiod_is_hw_timestamp_enabled(desc)) {
+				ret = gpiod_hw_timestamp_control(desc, false);
+				if (ret)
+					return ret;
+			}
+
 			ret = gpiod_direction_output(desc, val);
 			if (ret)
 				return ret;
@@ -1152,6 +1186,13 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 					polarity_change);
 			if (ret)
 				return ret;
+
+			/* Check if new config sets hardware assisted clock */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					return ret;
+			}
 		}
 
 		blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1281,8 +1322,12 @@ static void linereq_free(struct linereq *lr)
 
 	for (i = 0; i < lr->num_lines; i++) {
 		edge_detector_stop(&lr->lines[i]);
-		if (lr->lines[i].desc)
+		if (lr->lines[i].desc) {
+			if (gpiod_is_hw_timestamp_enabled(lr->lines[i].desc))
+				gpiod_hw_timestamp_control(lr->lines[i].desc,
+							   false);
 			gpiod_free(lr->lines[i].desc);
+		}
 	}
 	kfifo_free(&lr->events);
 	kfree(lr->label);
@@ -1409,6 +1454,15 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
 					flags & GPIO_V2_LINE_EDGE_FLAGS);
 			if (ret)
 				goto out_free_linereq;
+
+			/*
+			 * Check if hardware assisted timestamp is requested
+			 */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					goto out_free_linereq;
+			}
 		}
 
 		blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1956,8 +2010,15 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
 
+	/*
+	 * Practically it is possible that user will want both the real time
+	 * and hardware timestamps on GPIO events, for now however lets just
+	 * work with either clocks
+	 */
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
+	else if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &desc->flags))
+		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
 
 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
 	if (debounce_period_us) {
include/uapi/linux/gpio.h +1 -0
index eaaea3d8e6b4..d360545b4c21 100644
@@ -80,6 +80,7 @@ enum gpio_v2_line_flag {
 	GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN	= _BITULL(9),
 	GPIO_V2_LINE_FLAG_BIAS_DISABLED		= _BITULL(10),
 	GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME	= _BITULL(11),
+	GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE	= _BITULL(12),
 };
 
 /**
Signature · 1 line
2.17.1
#53 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 10 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

This looks good to me, pretty much exactly as I imagine it should be done, and it is also nice that we only implement it
for the v2 UAPI.

Yours,
Linus Walleij

#54 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

In reply to Dipen Patel

On Sat, Jun 26, 2021 at 1:48 AM Dipen Patel <[email protected]> wrote:

Just a quick question about this:

Quoted 1 line

+ GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \

Is the usage intended to be such that since hardware timestamp can not be guaranteed we need to ask for it and fail and if that fails maybe the software wants to fall back to the realtime or common timestamp?

I'm thinking from the view of libgpiod or similar apps that abstract this and they will be "I want to use hardware timestamps if and only if it is available, otherwise I want to use this other timestamp" or is that use case uncommon, such that either you know exactly what you want or you should not be messing with hardware timestamps?

Yours,
Linus Walleij

author maintainer
#55 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 10 lines · Linus Walleij

On Sat, Jun 26, 2021 at 1:48 AM Dipen Patel <[email protected]> wrote:

Just a quick question about this:

Quoted 1 line

+ GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \

Is the usage intended to be such that since hardware timestamp can not be guaranteed we need to ask for it and fail and if that fails maybe the software wants to fall back to the realtime or common timestamp?

Quoted 6 lines

I'm thinking from the view of libgpiod or similar apps that abstract this and they will be "I want to use hardware timestamps if and only if it is available, otherwise I want to use this other timestamp" or is that use case uncommon, such that either you know exactly what you want or you should not be messing with hardware timestamps?

The way currently is implemented, if you have requested

FLAG_EVENT_CLOCK_HARDWARE and it fails, control will return

to userspace with an error. There is no fallback.

Quoted 3 lines

Yours,
Linus Walleij

reviewer
#56 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

In reply to Dipen Patel
Quoted 35 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
 drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++--
 include/uapi/linux/gpio.h   |  1 +
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
 	 GPIO_V2_LINE_DRIVE_FLAGS | \
 	 GPIO_V2_LINE_EDGE_FLAGS | \
 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
 	 GPIO_V2_LINE_BIAS_FLAGS)
 
 static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
 
 static u64 line_event_timestamp(struct line *line)
 {
+	bool block;
+
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
 		return ktime_get_real_ns();

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 557

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +9 -0
 
+	if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {
+		if (irq_count())
+			block = false;
+		else
+			block = true;
+
+		return gpiod_get_hw_timestamp(line->desc, block);
+	}
+

Use in_task() instead of block?

drivers/gpio/gpiolib-cdev.c static int edge_detector_setup(struct line *line, · line 846

Quoted 11 lines
drivers/gpio/gpiolib-cdev.c +1 -0
 	return ktime_get_ns();
 }
 
@@ -828,6 +840,7 @@ static int edge_detector_setup(struct line *line,
 		return ret;
 
 	line->irq = irq;
+
 	return 0;
 }
 

Remove gratuitous whitespace changes.
If you dislike the formatting then suggest it in a separate patch.

drivers/gpio/gpiolib-cdev.c static int gpio_v2_line_flags_validate(u64 flags) · line 914

Quoted 12 lines
drivers/gpio/gpiolib-cdev.c +0 -1
@@ -891,7 +904,6 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	/* Return an error if an unknown flag is set */
 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
 		return -EINVAL;
-
 	/*
 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
 	 * contradictory.
@@ -900,6 +912,14 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
 		return -EINVAL;
 

Same here.

drivers/gpio/gpiolib-cdev.c static int gpio_v2_line_flags_validate(u64 flags) · line 922

Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +8 -0
+	/*
+	 * Do not mix with any other clocks if hardware assisted timestamp is
+	 * asked.
+	 */
+	if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
+	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE))
+		return -EINVAL;
+

The comment is very hw timestamp centric. It should just be something along the lines of "only allow one event clock source".

Quoted 18 lines
drivers/gpio/gpiolib-cdev.c +4 -0
 	/* Edge detection requires explicit input. */
 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
@@ -992,6 +1012,8 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
 
 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
+	assign_bit(FLAG_EVENT_CLOCK_HARDWARE, flagsp,
+		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE);
 }
 
 static long linereq_get_values(struct linereq *lr, void __user *ip)
@@ -1139,6 +1161,18 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 			int val = gpio_v2_line_config_output_value(lc, i);
 
 			edge_detector_stop(&lr->lines[i]);
+
+			/*

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1175

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +10 -0
+			 * Assuming line was input before and hardware
+			 * assisted timestamp only timestamps the input
+			 * lines.
+			 */
+			if (gpiod_is_hw_timestamp_enabled(desc)) {
+				ret = gpiod_hw_timestamp_control(desc, false);
+				if (ret)
+					return ret;
+			}
+

So if you fail to disable the hw timestamp then you fail the set_config? Does that make sense?
It should be impossible to fail, as per the preceding edge_detector_stop(), or any failure in this context is irrelevant and so can be ignored.

Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +0 -0
 			ret = gpiod_direction_output(desc, val);
 			if (ret)
 				return ret;
@@ -1152,6 +1186,13 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 					polarity_change);
 			if (ret)

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1197

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +7 -0
 				return ret;
+
+			/* Check if new config sets hardware assisted clock */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					return ret;
+			}
 		}
 

The error code here can come from the pinctrl timestamp_control(), so it should be sanitised before being returned to userspace.

drivers/gpio/gpiolib-cdev.c static void linereq_free(struct linereq *lr) · line 1330

Quoted 12 lines
drivers/gpio/gpiolib-cdev.c +5 -1
 		blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1281,8 +1322,12 @@ static void linereq_free(struct linereq *lr)
 
 	for (i = 0; i < lr->num_lines; i++) {
 		edge_detector_stop(&lr->lines[i]);
-		if (lr->lines[i].desc)
+		if (lr->lines[i].desc) {
+			if (gpiod_is_hw_timestamp_enabled(lr->lines[i].desc))
+				gpiod_hw_timestamp_control(lr->lines[i].desc,
+							   false);
 			gpiod_free(lr->lines[i].desc);
+		}

Potential race on gpiod_is_hw_timestamp_enabled() and the call to gpiod_hw_timestamp_control()?
Why not put the gpiod_is_hw_timestamp_enabled() check inside gpiod_hw_timestamp_control()?

And the gpiod_hw_timestamp_control() call should be moved inside gpiod_free(), or more correctly gpiod_free_commit().
i.e. whenever you free the gpio you release any associated hw timestamp.

Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +1 -0
 	}
 	kfifo_free(&lr->events);
 	kfree(lr->label);
@@ -1409,6 +1454,15 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
 					flags & GPIO_V2_LINE_EDGE_FLAGS);
 			if (ret)
 				goto out_free_linereq;
+

drivers/gpio/gpiolib-cdev.c static int linereq_create(struct gpio_device *gdev, void __user *ip) · line 1467

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8 -0
+			/*
+			 * Check if hardware assisted timestamp is requested
+			 */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					goto out_free_linereq;
+			}
 		}
 

Comment can fit on one line, and probably isn't even necessary - the code is clear enough.

Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +0 -0
 		blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1956,8 +2010,15 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
 

drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 2022

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +7 -0
+	/*
+	 * Practically it is possible that user will want both the real time
+	 * and hardware timestamps on GPIO events, for now however lets just
+	 * work with either clocks
+	 */
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
+	else if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &desc->flags))
+		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
 

If there is any need or intent to support multiple clock sources then avoid creeping API changes and add it now.
Either way, drop the comment.

Quoted 17 lines
drivers/gpio/gpiolib-cdev.c +0 -0
 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
 	if (debounce_period_us) {
include/uapi/linux/gpio.h +1 -0
index eaaea3d8e6b4..d360545b4c21 100644
@@ -80,6 +80,7 @@ enum gpio_v2_line_flag {
 	GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN	= _BITULL(9),
 	GPIO_V2_LINE_FLAG_BIAS_DISABLED		= _BITULL(10),
 	GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME	= _BITULL(11),
+	GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE	= _BITULL(12),
 };
 
 /**
Signature · 1 line
2.17.1

Cheers,
Kent.

author maintainer
#57 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 37 lines · Kent Gibson
Quoted 36 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
 drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++--
 include/uapi/linux/gpio.h   |  1 +
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
 	 GPIO_V2_LINE_DRIVE_FLAGS | \
 	 GPIO_V2_LINE_EDGE_FLAGS | \
 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
 	 GPIO_V2_LINE_BIAS_FLAGS)
 
 static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
 
 static u64 line_event_timestamp(struct line *line)
 {
+	bool block;
+
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
 		return ktime_get_real_ns();
 
Quoted 10 lines

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 557

Quoted 9 lines
drivers/gpio/gpiolib-cdev.c +9 -0
+	if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {
+		if (irq_count())
+			block = false;
+		else
+			block = true;
+
+		return gpiod_get_hw_timestamp(line->desc, block);
+	}
+

Use in_task() instead of block?

yes, will change to in_task.

Quoted 7 lines
Quoted 7 lines
drivers/gpio/gpiolib-cdev.c +0 -0
 	return ktime_get_ns();
 }
 
@@ -828,6 +840,7 @@ static int edge_detector_setup(struct line *line,
 		return ret;
 
Quoted 7 lines

drivers/gpio/gpiolib-cdev.c static int edge_detector_setup(struct line *line, · line 846

Quoted 5 lines
drivers/gpio/gpiolib-cdev.c +1 -0
 	line->irq = irq;
+
 	return 0;
 }
 

Remove gratuitous whitespace changes.
If you dislike the formatting then suggest it in a separate patch.

I will remove this space.

Quoted 15 lines

drivers/gpio/gpiolib-cdev.c static int gpio_v2_line_flags_validate(u64 flags) · line 914

Quoted 13 lines
drivers/gpio/gpiolib-cdev.c +0 -1
@@ -891,7 +904,6 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	/* Return an error if an unknown flag is set */
 	if (flags & ~GPIO_V2_LINE_VALID_FLAGS)
 		return -EINVAL;
-
 	/*
 	 * Do not allow both INPUT and OUTPUT flags to be set as they are
 	 * contradictory.
@@ -900,6 +912,14 @@ static int gpio_v2_line_flags_validate(u64 flags)
 	    (flags & GPIO_V2_LINE_FLAG_OUTPUT))
 		return -EINVAL;
 

Same here.

Quoted 10 lines

drivers/gpio/gpiolib-cdev.c static int gpio_v2_line_flags_validate(u64 flags) · line 922

Quoted 8 lines
drivers/gpio/gpiolib-cdev.c +8 -0
+	/*
+	 * Do not mix with any other clocks if hardware assisted timestamp is
+	 * asked.
+	 */
+	if ((flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME) &&
+	    (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE))
+		return -EINVAL;
+

The comment is very hw timestamp centric. It should just be something along the lines of "only allow one event clock source".

Sure, will change it.

Quoted 25 lines
Quoted 25 lines
drivers/gpio/gpiolib-cdev.c +10 -0
 	/* Edge detection requires explicit input. */
 	if ((flags & GPIO_V2_LINE_EDGE_FLAGS) &&
 	    !(flags & GPIO_V2_LINE_FLAG_INPUT))
@@ -992,6 +1012,8 @@ static void gpio_v2_line_config_flags_to_desc_flags(u64 flags,
 
 	assign_bit(FLAG_EVENT_CLOCK_REALTIME, flagsp,
 		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME);
+	assign_bit(FLAG_EVENT_CLOCK_HARDWARE, flagsp,
+		   flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE);
 }
 
 static long linereq_get_values(struct linereq *lr, void __user *ip)
@@ -1139,6 +1161,18 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 			int val = gpio_v2_line_config_output_value(lc, i);
 
 			edge_detector_stop(&lr->lines[i]);
+
+			/*
+			 * Assuming line was input before and hardware
+			 * assisted timestamp only timestamps the input
+			 * lines.
+			 */
+			if (gpiod_is_hw_timestamp_enabled(desc)) {
+				ret = gpiod_hw_timestamp_control(desc, false);
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1175

Quoted 4 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+				if (ret)
+					return ret;
+			}
+

So if you fail to disable the hw timestamp then you fail the set_config? Does that make sense?
It should be impossible to fail, as per the preceding edge_detector_stop(), or any failure in this context is irrelevant and so can be ignored.

I am planning to remove is_hw_timestamp* API as it is not needed.

I will also remove ret check from timestamp_control API as it is not needed.

Quoted 11 lines
Quoted 11 lines
drivers/gpio/gpiolib-cdev.c +3 -0
 			ret = gpiod_direction_output(desc, val);
 			if (ret)
 				return ret;
@@ -1152,6 +1186,13 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 					polarity_change);
 			if (ret)
 				return ret;
+
+			/* Check if new config sets hardware assisted clock */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1197

Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					return ret;
+			}
 		}
 

The error code here can come from the pinctrl timestamp_control(), so it should be sanitised before being returned to userspace.

I do not understand what do you mean by sanitise. I just followed what

gpiod_direction_output did just above which also returns ret from gpio

driver code similar to timestamp_control API.

Quoted 18 lines

drivers/gpio/gpiolib-cdev.c static void linereq_free(struct linereq *lr) · line 1330

Quoted 13 lines
drivers/gpio/gpiolib-cdev.c +5 -1
 		blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1281,8 +1322,12 @@ static void linereq_free(struct linereq *lr)
 
 	for (i = 0; i < lr->num_lines; i++) {
 		edge_detector_stop(&lr->lines[i]);
-		if (lr->lines[i].desc)
+		if (lr->lines[i].desc) {
+			if (gpiod_is_hw_timestamp_enabled(lr->lines[i].desc))
+				gpiod_hw_timestamp_control(lr->lines[i].desc,
+							   false);
 			gpiod_free(lr->lines[i].desc);
+		}

Potential race on gpiod_is_hw_timestamp_enabled() and the call to gpiod_hw_timestamp_control()?
Why not put the gpiod_is_hw_timestamp_enabled() check inside gpiod_hw_timestamp_control()?

Quoted 3 lines

And the gpiod_hw_timestamp_control() call should be moved inside gpiod_free(), or more correctly gpiod_free_commit().
i.e. whenever you free the gpio you release any associated hw timestamp.

I am planning to remove is_hw_timestamp* API, that should take care

of race condition. For gpiod_free comment, I had thought about it before

but then ruled out as it would mean that for all the clients who did not

register with HTE, during their gpiod_free call, it has to make unncessary

call into HTE, however HTE release_ts has necessary checks which will return

without doing anything. Let me know if you still think to move it in gpiod_free.

Quoted 13 lines
Quoted 13 lines
drivers/gpio/gpiolib-cdev.c +5 -0
 	}
 	kfifo_free(&lr->events);
 	kfree(lr->label);
@@ -1409,6 +1454,15 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
 					flags & GPIO_V2_LINE_EDGE_FLAGS);
 			if (ret)
 				goto out_free_linereq;
+
+			/*
+			 * Check if hardware assisted timestamp is requested
+			 */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static int linereq_create(struct gpio_device *gdev, void __user *ip) · line 1467

Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					goto out_free_linereq;
+			}
 		}
 

Comment can fit on one line, and probably isn't even necessary - the code is clear enough.

I will remove comment.

Quoted 6 lines
Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +0 -0
 		blocking_notifier_call_chain(&desc->gdev->notifier,
@@ -1956,8 +2010,15 @@ static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
 	if (test_bit(FLAG_EDGE_FALLING, &desc->flags))
 		info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
 
Quoted 13 lines

drivers/gpio/gpiolib-cdev.c static void gpio_desc_to_lineinfo(struct gpio_desc *desc, · line 2022

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +7 -0
+	/*
+	 * Practically it is possible that user will want both the real time
+	 * and hardware timestamps on GPIO events, for now however lets just
+	 * work with either clocks
+	 */
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags))
 		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
+	else if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &desc->flags))
+		info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
 

If there is any need or intent to support multiple clock sources then avoid creeping API changes and add it now.
Either way, drop the comment.

I will remove comment in next RFC.

Quoted 20 lines
Quoted 18 lines
drivers/gpio/gpiolib-cdev.c +0 -0
 	debounce_period_us = READ_ONCE(desc->debounce_period_us);
 	if (debounce_period_us) {
include/uapi/linux/gpio.h +1 -0
index eaaea3d8e6b4..d360545b4c21 100644
@@ -80,6 +80,7 @@ enum gpio_v2_line_flag {
 	GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN	= _BITULL(9),
 	GPIO_V2_LINE_FLAG_BIAS_DISABLED		= _BITULL(10),
 	GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME	= _BITULL(11),
+	GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE	= _BITULL(12),
 };
 
 /**
Signature · 1 line
2.17.1

Cheers,
Kent.

reviewer
#58 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 2 lines · Dipen Patel

On 7/1/21 7:24 AM, Kent Gibson wrote:

<snip>

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpio/gpiolib-cdev.c +3 -0
 			ret = gpiod_direction_output(desc, val);
 			if (ret)
 				return ret;
@@ -1152,6 +1186,13 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 					polarity_change);
 			if (ret)
 				return ret;
+
+			/* Check if new config sets hardware assisted clock */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1197

Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					return ret;
+			}
 		}
 

The error code here can come from the pinctrl timestamp_control(), so it should be sanitised before being returned to userspace.

Quoted 6 lines

I do not understand what do you mean by sanitise. I just followed what

gpiod_direction_output did just above which also returns ret from gpio

driver code similar to timestamp_control API.

In this context, sanitise means convert any kernel internal error codes to their userspace equivalent before returning them to userspace.

Fair enough with the gpiod_direction_output() comparison. I was thinking of a patch Andy recently submitted[1] to sanitise gpiod_request(), which can sometimes return EPROBE_DEFER. But I guess we can wait until we find a case of a driver returning an internal error code and add a sanitiser then.

Cheers,
Kent.

[1] https://www.spinics.net/lists/linux-gpio/msg60998.html

author maintainer
#59 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 27 lines · Kent Gibson
Quoted 1 line · Dipen Patel

On 7/1/21 7:24 AM, Kent Gibson wrote:

<snip>

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +3 -0
 			ret = gpiod_direction_output(desc, val);
 			if (ret)
 				return ret;
@@ -1152,6 +1186,13 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 					polarity_change);
 			if (ret)
 				return ret;
+
+			/* Check if new config sets hardware assisted clock */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1197

Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					return ret;
+			}
 		}
 

The error code here can come from the pinctrl timestamp_control(), so it should be sanitised before being returned to userspace.

Quoted 6 lines

I do not understand what do you mean by sanitise. I just followed what

gpiod_direction_output did just above which also returns ret from gpio

driver code similar to timestamp_control API.

Quoted 8 lines

In this context, sanitise means convert any kernel internal error codes to their userspace equivalent before returning them to userspace.

Fair enough with the gpiod_direction_output() comparison. I was thinking of a patch Andy recently submitted[1] to sanitise gpiod_request(), which can sometimes return EPROBE_DEFER. But I guess we can wait until we find a case of a driver returning an internal error code and add a sanitiser then.

Make sense, I will add sanity check

Quoted 6 lines

Cheers,
Kent.

[1] https://www.spinics.net/lists/linux-gpio/msg60998.html

reviewer
#60 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 32 lines · Dipen Patel
Quoted 30 lines · Kent Gibson
Quoted 1 line · Dipen Patel

On 7/1/21 7:24 AM, Kent Gibson wrote:

<snip>

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +3 -0
 			ret = gpiod_direction_output(desc, val);
 			if (ret)
 				return ret;
@@ -1152,6 +1186,13 @@ static long linereq_set_config_unlocked(struct linereq *lr,
 					polarity_change);
 			if (ret)
 				return ret;
+
+			/* Check if new config sets hardware assisted clock */
+			if (flags & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE) {
Quoted 8 lines

drivers/gpio/gpiolib-cdev.c static long linereq_set_config_unlocked(struct linereq *lr, · line 1197

Quoted 6 lines
drivers/gpio/gpiolib-cdev.c +4 -0
+				ret = gpiod_hw_timestamp_control(desc, true);
+				if (ret)
+					return ret;
+			}
 		}
 

The error code here can come from the pinctrl timestamp_control(), so it should be sanitised before being returned to userspace.

Quoted 6 lines

I do not understand what do you mean by sanitise. I just followed what

gpiod_direction_output did just above which also returns ret from gpio

driver code similar to timestamp_control API.

In this context, sanitise means convert any kernel internal error codes to their userspace equivalent before returning them to userspace.

Quoted 7 lines
Quoted 5 lines

Fair enough with the gpiod_direction_output() comparison. I was thinking of a patch Andy recently submitted[1] to sanitise gpiod_request(), which can sometimes return EPROBE_DEFER. But I guess we can wait until we find a case of a driver returning an internal error code and add a sanitiser then.

Make sense, I will add sanity check

Quoted 1 line

But I said don't bother yet. And you need to know what errors to sanitise before you sanitise them - unless you want to run through all the possibilities that can be returned to userspace.

Cheers,
Kent.

maintainer
#61 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

In reply to Dipen Patel
Quoted 37 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
 drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++--
 include/uapi/linux/gpio.h   |  1 +
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +4 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
 	 GPIO_V2_LINE_DRIVE_FLAGS | \
 	 GPIO_V2_LINE_EDGE_FLAGS | \
 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
 	 GPIO_V2_LINE_BIAS_FLAGS)
 
 static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
 
 static u64 line_event_timestamp(struct line *line)
 {
+	bool block;
+
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
 		return ktime_get_real_ns();
 
+	if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 559

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8 -0
+		if (irq_count())
+			block = false;
+		else
+			block = true;
+
+		return gpiod_get_hw_timestamp(line->desc, block);
+	}
+
 	return ktime_get_ns();
 }

Looking at line_event_timestamp() and the callers of this function, it appears that this should always return nanoseconds. Does gpiod_get_hw_timestamp() return nanoseconds?

Jon

Signature · 1 line
nvpublic
author maintainer
#62 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 37 lines · Jon Hunter
Quoted 36 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
 drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++--
 include/uapi/linux/gpio.h   |  1 +
 2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
 	 GPIO_V2_LINE_DRIVE_FLAGS | \
 	 GPIO_V2_LINE_EDGE_FLAGS | \
 	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
 	 GPIO_V2_LINE_BIAS_FLAGS)
 
 static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
 
 static u64 line_event_timestamp(struct line *line)
 {
+	bool block;
+
 	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
 		return ktime_get_real_ns();
 
Quoted 15 lines

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 559

Quoted 11 lines
drivers/gpio/gpiolib-cdev.c +9 -0
+	if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {
+		if (irq_count())
+			block = false;
+		else
+			block = true;
+
+		return gpiod_get_hw_timestamp(line->desc, block);
+	}
+
 	return ktime_get_ns();
 }

Looking at line_event_timestamp() and the callers of this function, it appears that this should always return nanoseconds. Does gpiod_get_hw_timestamp() return nanoseconds?

Yes, it returns in ns to align with line_event_timestamp.

Quoted 3 lines

Jon

maintainer
#63 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 54 lines · Dipen Patel
Quoted 37 lines · Jon Hunter
Quoted 36 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
  drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++--
  include/uapi/linux/gpio.h   |  1 +
  2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
  	 GPIO_V2_LINE_DRIVE_FLAGS | \
  	 GPIO_V2_LINE_EDGE_FLAGS | \
  	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+	 GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
  	 GPIO_V2_LINE_BIAS_FLAGS)
 
  static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
 
  static u64 line_event_timestamp(struct line *line)
  {
+	bool block;
+
  	if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
  		return ktime_get_real_ns();
 
Quoted 15 lines

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 559

Quoted 11 lines
drivers/gpio/gpiolib-cdev.c +9 -0
+	if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {
+		if (irq_count())
+			block = false;
+		else
+			block = true;
+
+		return gpiod_get_hw_timestamp(line->desc, block);
+	}
+
  	return ktime_get_ns();
  }

Looking at line_event_timestamp() and the callers of this function, it appears that this should always return nanoseconds. Does gpiod_get_hw_timestamp() return nanoseconds?

Quoted 1 line

Yes, it returns in ns to align with line_event_timestamp.

It might be worth updating the function name to gpiod_get_hw_timestamp_ns() so that this is clear.

Jon

--
nvpublic

author maintainer
#64 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 53 lines · Jon Hunter
Quoted 51 lines · Dipen Patel
Quoted 49 lines · Jon Hunter
Quoted 34 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
  drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++-- include/uapi/linux/gpio.h   |  1 +

  2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
        GPIO_V2_LINE_DRIVE_FLAGS | \
        GPIO_V2_LINE_EDGE_FLAGS | \
        GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+     GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
        GPIO_V2_LINE_BIAS_FLAGS)
     static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
     static u64 line_event_timestamp(struct line *line)
   {
+    bool block;
+
       if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
           return ktime_get_real_ns();
   +    if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 557

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8 -0
+        if (irq_count())
+            block = false;
+        else
+            block = true;
+
+        return gpiod_get_hw_timestamp(line->desc, block);
+    }
+
       return ktime_get_ns();
   }

Looking at line_event_timestamp() and the callers of this function, it appears that this should always return nanoseconds. Does gpiod_get_hw_timestamp() return nanoseconds?

Quoted 4 lines
Quoted 1 line

Yes, it returns in ns to align with line_event_timestamp.

It might be worth updating the function name to gpiod_get_hw_timestamp_ns() so that this is clear.

Wouldn't be sufficient to right into its description rather embed in API name?

Quoted 5 lines

Jon

Signature · 1 line
nvpublic
reviewer
#65 lore raw

Re: [RFC 08/11] gpiolib: cdev: Add hardware timestamp clock type

Quoted 55 lines · Dipen Patel
Quoted 53 lines · Jon Hunter
Quoted 51 lines · Dipen Patel
Quoted 49 lines · Jon Hunter
Quoted 34 lines · Dipen Patel

This patch adds new clock type for the GPIO controller which can timestamp gpio lines using hardware means. To expose such functionalities to the userspace, code has been added in this patch where during line create call, it checks for new clock type and if requested, calls hardware timestamp related API from gpiolib.c. During line change event, it retrieves timestamp in nano seconds by calling gpiod_get_hw_timestamp API from gpiolib.c. At the line release, it disables this functionality by calling gpiod_hw_timestamp_control.

---
  drivers/gpio/gpiolib-cdev.c | 65 +++++++++++++++++++++++++++++++++++-- include/uapi/linux/gpio.h   |  1 +

  2 files changed, 64 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -0
index 1631727bf0da..9f98c727e937 100644
@@ -518,6 +518,7 @@ struct linereq {
        GPIO_V2_LINE_DRIVE_FLAGS | \
        GPIO_V2_LINE_EDGE_FLAGS | \
        GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME | \
+     GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE | \
        GPIO_V2_LINE_BIAS_FLAGS)
     static void linereq_put_event(struct linereq *lr,
@@ -540,9 +541,20 @@ static void linereq_put_event(struct linereq *lr,
     static u64 line_event_timestamp(struct line *line)
   {
+    bool block;
+
       if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &line->desc->flags))
           return ktime_get_real_ns();
   +    if (test_bit(FLAG_EVENT_CLOCK_HARDWARE, &line->desc->flags)) {

drivers/gpio/gpiolib-cdev.c static void linereq_put_event(struct linereq *lr, · line 557

Quoted 10 lines
drivers/gpio/gpiolib-cdev.c +8 -0
+        if (irq_count())
+            block = false;
+        else
+            block = true;
+
+        return gpiod_get_hw_timestamp(line->desc, block);
+    }
+
       return ktime_get_ns();
   }

Looking at line_event_timestamp() and the callers of this function, it appears that this should always return nanoseconds. Does gpiod_get_hw_timestamp() return nanoseconds?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Yes, it returns in ns to align with line_event_timestamp.

It might be worth updating the function name to gpiod_get_hw_timestamp_ns() so that this is clear.

Wouldn't be sufficient to right into its description rather embed in API name?

Quoted 1 line

Adding a suffix identifying the timestamp resolution to variable and function names is pretty standard in the kernel.
It makes the code easier to read as you don't have to keep checking the documentation.

Cheers,
Kent.

author maintainer
#66 lore raw

RFC 9/11 tools: gpio: Add new hardware clock type

In reply to Dipen Patel

gpiolib-cdev is extended to support hardware clock type, this patch reflects that fact.

---
 tools/gpio/gpio-event-mon.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/gpio/gpio-event-mon.c +5 -1
index a2b233fdb572..bed52333698d 100644
@@ -149,6 +149,7 @@ void print_usage(void)
 		"  -r         Listen for rising edges\n"
 		"  -f         Listen for falling edges\n"
 		"  -w         Report the wall-clock time for events\n"
+		"  -t         Report the hardware timestamp for events\n"
 		"  -b <n>     Debounce the line with period n microseconds\n"
 		" [-c <n>]    Do <n> loops (optional, infinite loop if not stated)\n"
 		"  -?         This helptext\n"
@@ -174,7 +175,7 @@ int main(int argc, char **argv)
 
 	memset(&config, 0, sizeof(config));
 	config.flags = GPIO_V2_LINE_FLAG_INPUT;
-	while ((c = getopt(argc, argv, "c:n:o:b:dsrfw?")) != -1) {
+	while ((c = getopt(argc, argv, "c:n:o:b:dsrfwt?")) != -1) {
 		switch (c) {
 		case 'c':
 			loops = strtoul(optarg, NULL, 10);
@@ -208,6 +209,9 @@ int main(int argc, char **argv)
 		case 'w':
 			config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
 			break;
+		case 't':
+			config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
+			break;
 		case '?':
 			print_usage();
 			return -1;
Signature · 1 line
2.17.1
#67 lore raw

Re: [RFC 09/11] tools: gpio: Add new hardware clock type

Quoted 4 lines · Dipen Patel

gpiolib-cdev is extended to support hardware clock type, this patch reflects that fact.

(...)

tools/gpio/gpio-event-mon.c int main(int argc, char **argv) · line 214

Quoted 6 lines
                case 'w':
                        config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
                        break;
tools/gpio/gpio-event-mon.c +3 -0
+               case 't':
+                       config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
+                       break;

After the checking of the command line options we need a small sanity check so we don't try to enable both realtime and hardware clock at the same time, we will only be able to request one of them.

Yours,
Linus Walleij

author maintainer
#68 lore raw

Re: [RFC 09/11] tools: gpio: Add new hardware clock type

Quoted 13 lines · Linus Walleij
Quoted 4 lines · Dipen Patel

gpiolib-cdev is extended to support hardware clock type, this patch reflects that fact.

(...)

tools/gpio/gpio-event-mon.c int main(int argc, char **argv) · line 214

Quoted 6 lines
                case 'w':
                        config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
                        break;
tools/gpio/gpio-event-mon.c +3 -0
+               case 't':
+                       config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
+                       break;
Quoted 3 lines

After the checking of the command line options we need a small sanity check so we don't try to enable both realtime and hardware clock at the same time, we will only be able to request one of them.

This will any way fail at gpiolib-cdev layer. Do we want to add it here

as well?

Quoted 3 lines

Yours,
Linus Walleij

reviewer
#69 lore raw

Re: [RFC 09/11] tools: gpio: Add new hardware clock type

Quoted 15 lines · Dipen Patel
Quoted 13 lines · Linus Walleij
Quoted 4 lines · Dipen Patel

gpiolib-cdev is extended to support hardware clock type, this patch reflects that fact.

(...)

Quoted 6 lines
                case 'w':
                        config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
                        break;
tools/gpio/gpio-event-mon.c +3 -0
+               case 't':
+                       config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
+                       break;
Quoted 8 lines
Quoted 3 lines

After the checking of the command line options we need a small sanity check so we don't try to enable both realtime and hardware clock at the same time, we will only be able to request one of them.

This will any way fail at gpiolib-cdev layer. Do we want to add it here

as well?

I can't speak for Linus, but I'm fine with it as is as it allows the tool to be used to exercise the sanity check in the kernel.

Cheers,
Kent.

#70 lore raw

Re: [RFC 09/11] tools: gpio: Add new hardware clock type

Quoted 20 lines · Kent Gibson
Quoted 19 lines · Dipen Patel
Quoted 16 lines · Linus Walleij
Quoted 4 lines · Dipen Patel

gpiolib-cdev is extended to support hardware clock type, this patch reflects that fact.

(...)

tools/gpio/gpio-event-mon.c int main(int argc, char **argv) · line 214

Quoted 6 lines
                case 'w':
                        config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
                        break;
tools/gpio/gpio-event-mon.c +3 -0
+               case 't':
+                       config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HARDWARE;
+                       break;

After the checking of the command line options we need a small sanity check so we don't try to enable both realtime and hardware clock at the same time, we will only be able to request one of them.

Quoted 7 lines
Quoted 4 lines

This will any way fail at gpiolib-cdev layer. Do we want to add it here

as well?

I can't speak for Linus, but I'm fine with it as is as it allows the tool to be used to exercise the sanity check in the kernel.

Fair enough, that sounds useful. Go ahead with this as-is.

Yours,
Linus Walleij

author maintainer
#71 lore raw

RFC 10/11 hte: Add tegra GPIO HTE test driver

In reply to Dipen Patel

Tegra194 GPIO controller and HTE supports AON GPIO lines for realtime timestamp using hardware means. This in kernel gpio consumer test driver demonstrates that functionality indirectly using HTE subsytem through GPIOLIB framework. During probe it also registers sysfs interface /sys/kernel/tegra_hte_gpio_test/gpio_en_dis. The value 1 enables gpio line for the HTE functionality while the value 0 disables that.

The test driver can be compiled as a module and takes optional parameters gpio_out and gpio_in of type uint that specifies GPIO numbers.

This patch also adds compilation support in Kconfig and Makefile.

---
 drivers/hte/Kconfig                  |   8 +
 drivers/hte/Makefile                 |   3 +-
 drivers/hte/hte-tegra194-gpio-test.c | 255 +++++++++++++++++++++++++++
 3 files changed, 265 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hte/hte-tegra194-gpio-test.c
drivers/hte/Kconfig +8 -0
index c4d335c41254..e62077c1024c 100644
@@ -38,4 +38,12 @@ config HTE_TEGRA194_IRQ_TEST
           The NVIDIA Tegra194 GTE IRQ test driver demonstrates HTE subsystem
 	  usage for the LIC IRQ hardware timestamp.
 
+config HTE_TEGRA194_GPIO_TEST
+        tristate "NVIDIA Tegra194 HTE GPIO Test"
+        depends on HTE_TEGRA194
+        help
+          The NVIDIA Tegra194 GTE GPIO test driver demonstrates how to use HTE
+	  subsystem indirectly through gpiolib API calls for GPIO line for the
+	  hardware assisted timestamping.
+
 endif
drivers/hte/Makefile +2 -1
index b1cde6bc939b..3f2f3a3ac4d4 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_HTE)		+= hte.o
 obj-$(CONFIG_HTE_TEGRA194)	+= hte-tegra194.o
-obj-$(CONFIG_HTE_TEGRA194_IRQ_TEST) += hte-tegra194-irq-test.o
\ No newline at end of file
+obj-$(CONFIG_HTE_TEGRA194_IRQ_TEST) += hte-tegra194-irq-test.o
+obj-$(CONFIG_HTE_TEGRA194_GPIO_TEST) += hte-tegra194-gpio-test.o
drivers/hte/hte-tegra194-gpio-test.c new file +255 -0

255 lines not on this page · raw message

Signature · 1 line
2.17.1
reviewer
#72 lore raw

Re: [RFC 10/11] hte: Add tegra GPIO HTE test driver

Quoted 7 lines · Dipen Patel
drivers/hte/Kconfig +0 -0
index c4d335c41254..e62077c1024c 100644
@@ -38,4 +38,12 @@ config HTE_TEGRA194_IRQ_TEST
           The NVIDIA Tegra194 GTE IRQ test driver demonstrates HTE subsystem
 	  usage for the LIC IRQ hardware timestamp.

drivers/hte/Kconfig config HTE_TEGRA194_IRQ_TEST · line 49

Quoted 10 lines
drivers/hte/Kconfig +8 -0
 
+config HTE_TEGRA194_GPIO_TEST
+        tristate "NVIDIA Tegra194 HTE GPIO Test"
+        depends on HTE_TEGRA194
+        help
+          The NVIDIA Tegra194 GTE GPIO test driver demonstrates how to use HTE
+	  subsystem indirectly through gpiolib API calls for GPIO line for the
+	  hardware assisted timestamping.
+
 endif

Fix indentation, please.

Signature · 1 line
~Randy
author maintainer
#73 lore raw

RFC 11/11 MAINTAINERS: Added HTE Subsystem

In reply to Dipen Patel

Added myself as a maintainer for this new Hardware Timestamping Engine (HTE) subsystem.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index ba0cc0a67b32..29e79e7f5a50 100644
@@ -8390,6 +8390,14 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/input/touchscreen/htcpen.c
 
+HTE SUBSYSTEM
+M:	[email protected]
+S:	Maintained
+F:	drivers/hte/*
+F:	include/linux/hte.h
+F:	Documentation/hte/*
+F:	Documentation/devicetree/bindings/hte/*
+
 HTS221 TEMPERATURE-HUMIDITY IIO DRIVER
 M:	Lorenzo Bianconi <[email protected]>
 L:	[email protected]
Signature · 1 line
2.17.1
#74 lore raw

cover Intro to Hardware timestamping engine

In reply to Dipen Patel

This is an unsolicited forward on the posting of the hardware timestamping engine that nVidia has, and which might be of interest to IIO as well, at least we need to make sure the DT bindings and in-kernel interfaces make sense for the IIO camp.

Yours,
Linus Walleij

---------- Forwarded message ---------
From: Dipen Patel <[email protected]>
Date: Sat, Jun 26, 2021 at 1:48 AM
Subject: [RFC 00/11] Intro to Hardware timestamping engine
To: <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>, <[email protected]>

This patch series introduces new subsystem called hardware timestamping engine (HTE). It offers functionality such as timestamping through hardware means in realtime. The HTE subsystem centralizes HTE provider and consumers where providers can register themselves with subsystem and the consumers can request interested entity which could be lines, GPIO, signals or buses. The HTE subsystem provides timestamp in nano seconds, having said that the provider need to convert the timestamp if its not in that unit. There was upstream discussion about the same at
https://lore.kernel.org/lkml/[email protected]/

To summarize upstream discussion:
- It was heavily favoured by Linus and Kent to extend GPIOLIB and supporting GPIO drivers to add HTE functionality and I agreed to experiment with it. This patch series implements and extends GPIOLIB and GPIO tegra driver.
- Discussed possibility to add HTE provider as irqchip instead which was argued against as HTE devices are not necessarily event emitting devices.
- Discussed other possibility if HTE device can be added as posix clock type like PTP clocks. That was also argues against since HTE devices are not necessarily tightly coupled with hardware clock.

Typical HTE provider does following:
- Register itself with HTE subsystem
- Provide *request, *release, *enable, *disable timestamp callbacks and optional get_clk_src_info callback to HTE subsystem.
- Provide optional xlate callback to the subsystem which can translate consumer provided logical ids into actual ids of the entity, where entity here is the provider dependent and could be GPIO, in chip lines or signals, buses etc...This converted id will be used between HTE subsystem and the provider for below bullet point.
- Push timestamps to the subsystem. This happens when HTE provider has timestamp data available and willing to push it to HTE subsystem. The HTE subsystem stores it into software buffer for the consumers.
- Unregister itself

Typical HTE consumer does following:
- Request interested entity it wishes to timestamp in realtime to the subsystem. During this call HTE subsystem allocates software buffer to store timestamps data.
- The subsystem does necessary communications with the provider to complete the request, which includes translating logical id of the entity to provider dependent physical/actual id and enabling hardware timestamping on requested id.
- It can optionally specify callback during registration, this cb will be called when provider pushes timestamps. Once notified through cb, the consumer can call retrieve API to read the data from the software buffer. If cb is not provided, the consumers can elect to call blocking version of retrieve API.
- Manage pre allocated software buffer if needed. It includes changing buffer length and watermark/threshold. The subsystem automatically sets watermark or threshold at 1, consumers can later change it to any other value it wishes. The main purpose for having threshold functionality is to notify consumer either through callback if provided or unblock waiting consumer when threshold is reached.
- Retrieve timestamp using various means provided by subsystem.
- Release entity and its resources.

HTE and GPIOLIB:
- For the HTE provider which can timestamp GPIO lines.
- For the GPIO consumers, either in kernel or userspace, The GPIOLIB and its CDEV framework are extended as frontend to the HTE by introducing new APIs.
- Tegra194 AON GPIO controller has HTE support also known as GTE (Generic Timestamping Engine). The tegra gpio driver is modified to accommodate HTE functionality.

Dipen Patel (11):
Documentation: Add HTE subsystem guide
drivers: Add HTE subsystem
hte: Add tegra194 HTE kernel provider
dt-bindings: Add HTE bindings
hte: Add Tegra194 IRQ HTE test driver
gpiolib: Add HTE support
gpio: tegra186: Add HTE in gpio-tegra186 driver
gpiolib: cdev: Add hardware timestamp clock type
tools: gpio: Add new hardware clock type
hte: Add tegra GPIO HTE test driver
MAINTAINERS: Added HTE Subsystem

 .../bindings/gpio/nvidia,tegra186-gpio.txt    |    7 +
 .../devicetree/bindings/hte/hte-consumer.yaml |   47 +
 .../devicetree/bindings/hte/hte.yaml          |   34 +
 .../bindings/hte/nvidia,tegra194-hte.yaml     |   83 +
 Documentation/hte/hte.rst                     |  198 +++
 Documentation/hte/index.rst                   |   21 +
 Documentation/hte/tegra194-hte.rst            |   65 +
 Documentation/index.rst                       |    1 +
 MAINTAINERS                                   |    8 +
 drivers/Kconfig                               |    2 +
 drivers/Makefile                              |    1 +
 drivers/gpio/gpio-tegra186.c                  |   78 +
 drivers/gpio/gpiolib-cdev.c                   |   65 +-
 drivers/gpio/gpiolib.c                        |   92 ++
 drivers/gpio/gpiolib.h                        |   11 +
 drivers/hte/Kconfig                           |   49 +
 drivers/hte/Makefile                          |    4 +
 drivers/hte/hte-tegra194-gpio-test.c          |  255 +++
 drivers/hte/hte-tegra194-irq-test.c           |  400 +++++
 drivers/hte/hte-tegra194.c                    |  554 +++++++
 drivers/hte/hte.c                             | 1368 +++++++++++++++++
 include/linux/gpio/consumer.h                 |   21 +-
 include/linux/gpio/driver.h                   |   13 +
 include/linux/hte.h                           |  278 ++++
 include/uapi/linux/gpio.h                     |    1 +
 tools/gpio/gpio-event-mon.c                   |    6 +-
 26 files changed, 3657 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644
Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml
 create mode 100644 Documentation/hte/hte.rst
 create mode 100644 Documentation/hte/index.rst
 create mode 100644 Documentation/hte/tegra194-hte.rst
 create mode 100644 drivers/hte/Kconfig
 create mode 100644 drivers/hte/Makefile
 create mode 100644 drivers/hte/hte-tegra194-gpio-test.c
 create mode 100644 drivers/hte/hte-tegra194-irq-test.c
 create mode 100644 drivers/hte/hte-tegra194.c
 create mode 100644 drivers/hte/hte.c
 create mode 100644 include/linux/hte.h

--
2.17.1

#75 lore raw
In reply to Dipen Patel
Quoted 11 lines · Dipen Patel

This patch series introduces new subsystem called hardware timestamping engine (HTE). It offers functionality such as timestamping through hardware means in realtime. The HTE subsystem centralizes HTE provider and consumers where providers can register themselves with subsystem and the consumers can request interested entity which could be lines, GPIO, signals or buses. The HTE subsystem provides timestamp in nano seconds, having said that the provider need to convert the timestamp if its not in that unit. There was upstream discussion about the same at
https://lore.kernel.org/lkml/[email protected]/

Quoted 3 lines

To summarize upstream discussion:
- It was heavily favoured by Linus and Kent to extend GPIOLIB and supporting GPIO drivers to add HTE functionality and I agreed to experiment with it.

I guess this series should include more people from different companies, especially documentation parts. This may be used by different hardware and quite different vendors. Developing a framework like this for only one vendor is no go in general.

Quoted 109 lines

This patch series implements and extends GPIOLIB and GPIO tegra driver.
- Discussed possibility to add HTE provider as irqchip instead which was argued against as HTE devices are not necessarily event emitting devices.
- Discussed other possibility if HTE device can be added as posix clock type like PTP clocks. That was also argues against since HTE devices are not necessarily tightly coupled with hardware clock.

Typical HTE provider does following:
- Register itself with HTE subsystem
- Provide *request, *release, *enable, *disable timestamp callbacks and optional get_clk_src_info callback to HTE subsystem.
- Provide optional xlate callback to the subsystem which can translate consumer provided logical ids into actual ids of the entity, where entity here is the provider dependent and could be GPIO, in chip lines or signals, buses etc...This converted id will be used between HTE subsystem and the provider for below bullet point.
- Push timestamps to the subsystem. This happens when HTE provider has timestamp data available and willing to push it to HTE subsystem. The HTE subsystem stores it into software buffer for the consumers.
- Unregister itself

Typical HTE consumer does following:
- Request interested entity it wishes to timestamp in realtime to the subsystem. During this call HTE subsystem allocates software buffer to store timestamps data.
- The subsystem does necessary communications with the provider to complete the request, which includes translating logical id of the entity to provider dependent physical/actual id and enabling hardware timestamping on requested id.
- It can optionally specify callback during registration, this cb will be called when provider pushes timestamps. Once notified through cb, the consumer can call retrieve API to read the data from the software buffer. If cb is not provided, the consumers can elect to call blocking version of retrieve API.
- Manage pre allocated software buffer if needed. It includes changing buffer length and watermark/threshold. The subsystem automatically sets watermark or threshold at 1, consumers can later change it to any other value it wishes. The main purpose for having threshold functionality is to notify consumer either through callback if provided or unblock waiting consumer when threshold is reached.
- Retrieve timestamp using various means provided by subsystem.
- Release entity and its resources.

HTE and GPIOLIB:
- For the HTE provider which can timestamp GPIO lines.
- For the GPIO consumers, either in kernel or userspace, The GPIOLIB and its CDEV framework are extended as frontend to the HTE by introducing new APIs.
- Tegra194 AON GPIO controller has HTE support also known as GTE (Generic Timestamping Engine). The tegra gpio driver is modified to accommodate HTE functionality.

Dipen Patel (11):
Documentation: Add HTE subsystem guide
drivers: Add HTE subsystem
hte: Add tegra194 HTE kernel provider
dt-bindings: Add HTE bindings
hte: Add Tegra194 IRQ HTE test driver
gpiolib: Add HTE support
gpio: tegra186: Add HTE in gpio-tegra186 driver
gpiolib: cdev: Add hardware timestamp clock type
tools: gpio: Add new hardware clock type
hte: Add tegra GPIO HTE test driver
MAINTAINERS: Added HTE Subsystem

 .../bindings/gpio/nvidia,tegra186-gpio.txt    |    7 +
 .../devicetree/bindings/hte/hte-consumer.yaml |   47 +
 .../devicetree/bindings/hte/hte.yaml          |   34 +
 .../bindings/hte/nvidia,tegra194-hte.yaml     |   83 +
 Documentation/hte/hte.rst                     |  198 +++
 Documentation/hte/index.rst                   |   21 +
 Documentation/hte/tegra194-hte.rst            |   65 +
 Documentation/index.rst                       |    1 +
 MAINTAINERS                                   |    8 +
 drivers/Kconfig                               |    2 +
 drivers/Makefile                              |    1 +
 drivers/gpio/gpio-tegra186.c                  |   78 +
 drivers/gpio/gpiolib-cdev.c                   |   65 +-
 drivers/gpio/gpiolib.c                        |   92 ++
 drivers/gpio/gpiolib.h                        |   11 +
 drivers/hte/Kconfig                           |   49 +
 drivers/hte/Makefile                          |    4 +
 drivers/hte/hte-tegra194-gpio-test.c          |  255 +++
 drivers/hte/hte-tegra194-irq-test.c           |  400 +++++
 drivers/hte/hte-tegra194.c                    |  554 +++++++
 drivers/hte/hte.c                             | 1368 +++++++++++++++++
 include/linux/gpio/consumer.h                 |   21 +-
 include/linux/gpio/driver.h                   |   13 +
 include/linux/hte.h                           |  278 ++++
 include/uapi/linux/gpio.h                     |    1 +
 tools/gpio/gpio-event-mon.c                   |    6 +-
 26 files changed, 3657 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/hte/hte-consumer.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/hte.yaml
 create mode 100644 Documentation/devicetree/bindings/hte/nvidia,tegra194-hte.yaml
 create mode 100644 Documentation/hte/hte.rst
 create mode 100644 Documentation/hte/index.rst
 create mode 100644 Documentation/hte/tegra194-hte.rst
 create mode 100644 drivers/hte/Kconfig
 create mode 100644 drivers/hte/Makefile
 create mode 100644 drivers/hte/hte-tegra194-gpio-test.c
 create mode 100644 drivers/hte/hte-tegra194-irq-test.c
 create mode 100644 drivers/hte/hte-tegra194.c
 create mode 100644 drivers/hte/hte.c
 create mode 100644 include/linux/hte.h

--
2.17.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#76 lore raw
Quoted 8 lines · Andy Shevchenko
Quoted 3 lines

To summarize upstream discussion:
- It was heavily favoured by Linus and Kent to extend GPIOLIB and supporting GPIO drivers to add HTE functionality and I agreed to experiment with it.

I guess this series should include more people from different companies, especially documentation parts. This may be used by different hardware and quite different vendors. Developing a framework like this for only one vendor is no go in general.

I forwarded patch 00 to the IIO list and Jonathan Cameron, and let's page Ye Xiang who made a bunch of contributions from Intel's side to IIO directly. (Hi Ye, please check this concept if you have time!)

The actually most important target group would be people doing things like sensor fusion where a common timebase is important, I don't know who does really, but Sandeep Singh from AMD has contributed the AMD Sensor Fusion hub in drivers/hid/amd-sfh-hid and might know a few things about this though I don't think SFH would need this directly.
https://en.wikipedia.org/wiki/Sensor_fusion

Also Paging Drew Fustini, who knows a lot of maker and tinker people, he might know a bit about this or know someone who knows.

Yours,
Linus Walleij

#77 lore raw
Quoted 25 lines · Linus Walleij
Quoted 8 lines · Andy Shevchenko
Quoted 3 lines

To summarize upstream discussion:
- It was heavily favoured by Linus and Kent to extend GPIOLIB and supporting GPIO drivers to add HTE functionality and I agreed to experiment with it.

I guess this series should include more people from different companies, especially documentation parts. This may be used by different hardware and quite different vendors. Developing a framework like this for only one vendor is no go in general.

I forwarded patch 00 to the IIO list and Jonathan Cameron, and let's page Ye Xiang who made a bunch of contributions from Intel's side to IIO directly. (Hi Ye, please check this concept if you have time!)

The actually most important target group would be people doing things like sensor fusion where a common timebase is important, I don't know who does really, but Sandeep Singh from AMD has contributed the AMD Sensor Fusion hub in drivers/hid/amd-sfh-hid and might know a few things about this though I don't think SFH would need this directly.
https://en.wikipedia.org/wiki/Sensor_fusion

Quoted 3 lines

Also Paging Drew Fustini, who knows a lot of maker and tinker people, he might know a bit about this or know someone who knows.

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko