Skip to contentSkip to conversation

gpio: Add Intel Granite Rapids-D vGPIO driver

PATCH Superseded Aapo Vienamo 6 participants 11 messages Apr 19, 2024 – Apr 23, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: Add Intel Granite Rapids-D vGPIO driver

This driver provides a basic GPIO driver for the Intel Granite Rapids-D virtual GPIOs. On SoCs with limited physical pins on the package, the physical pins controlled by this driver would be exposed on an external device such as a BMC or CPLD.

---
 MAINTAINERS                       |   1 +
 drivers/gpio/Kconfig              |  16 ++
 drivers/gpio/Makefile             |   1 +
 drivers/gpio/gpio-graniterapids.c | 382 ++++++++++++++++++++++++++++++
 4 files changed, 400 insertions(+)
 create mode 100644 drivers/gpio/gpio-graniterapids.c
MAINTAINERS +1 -0
index 7c5760f4484f..cd97f6fcc3c0 100644
@@ -10995,6 +10995,7 @@ L:	[email protected]
 S:	Supported
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/andy/linux-gpio-intel.git
 F:	drivers/gpio/gpio-elkhartlake.c
+F:	drivers/gpio/gpio-graniterapids.c
 F:	drivers/gpio/gpio-ich.c
 F:	drivers/gpio/gpio-merrifield.c
 F:	drivers/gpio/gpio-ml-ioh.c
drivers/gpio/Kconfig +16 -0
index 00b5c007a2bb..1796eaed34ee 100644
@@ -321,6 +321,22 @@ config GPIO_GENERIC_PLATFORM
 	help
 	  Say yes here to support basic platform_device memory-mapped GPIO controllers.
 
+config GPIO_GRANITERAPIDS
+	tristate "Intel Granite Rapids-D vGPIO support"
+	depends on X86 || COMPILE_TEST
+	select GPIOLIB_IRQCHIP
+	help
+	  Select this to enable GPIO support on platforms with the following
+	  SoCs:
+
+	  - Intel Granite Rapids-D
+
+	  The driver enables basic GPIO functionality and implements interrupt
+	  support.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called gpio-graniterapids.
+
 config GPIO_GRGPIO
 	tristate "Aeroflex Gaisler GRGPIO support"
 	depends on OF_GPIO
drivers/gpio/Makefile +1 -0
index fdd28c58d890..e2a53013780e 100644
@@ -66,6 +66,7 @@ obj-$(CONFIG_GPIO_FTGPIO010)		+= gpio-ftgpio010.o
 obj-$(CONFIG_GPIO_FXL6408)		+= gpio-fxl6408.o
 obj-$(CONFIG_GPIO_GE_FPGA)		+= gpio-ge.o
 obj-$(CONFIG_GPIO_GPIO_MM)		+= gpio-gpio-mm.o
+obj-$(CONFIG_GPIO_GRANITERAPIDS)	+= gpio-graniterapids.o
 obj-$(CONFIG_GPIO_GRGPIO)		+= gpio-grgpio.o
 obj-$(CONFIG_GPIO_GW_PLD)		+= gpio-gw-pld.o
 obj-$(CONFIG_GPIO_HISI)                 += gpio-hisi.o
drivers/gpio/gpio-graniterapids.c new file +382 -0

382 lines not on this page · raw message

Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Aapo,

thanks for your patch!

The code is impeccable, not much to say about that. From that PoV the driver is finished.

I have some technical review comments:

Quoted 8 lines · Aapo Vienamo

This driver provides a basic GPIO driver for the Intel Granite Rapids-D virtual GPIOs. On SoCs with limited physical pins on the package, the physical pins controlled by this driver would be exposed on an external device such as a BMC or CPLD.

OK I get how it works, but not all the way right? We write these registers, and somehow that results on pins on a completely different piece of silicon in a different package driving some lines low/high?

So ... can we write something about how the signal gets over there from where the driver is running? It needs to happen somehow, right?

Quoted 5 lines
drivers/gpio/Kconfig +5 -0
+config GPIO_GRANITERAPIDS
+       tristate "Intel Granite Rapids-D vGPIO support"
+       depends on X86 || COMPILE_TEST
+       select GPIOLIB_IRQCHIP
+       help

drivers/gpio/Kconfig config GPIO_GENERIC_PLATFORM · line 338

Quoted 10 lines
drivers/gpio/Kconfig +10 -0
+         Select this to enable GPIO support on platforms with the following
+         SoCs:
+
+         - Intel Granite Rapids-D
+
+         The driver enables basic GPIO functionality and implements interrupt
+         support.
+
+         To compile this driver as a module, choose M here: the module will
+         be called gpio-graniterapids.

This help text is not as informative as the commit log. Write something about how the GPIO works here, too.

Quoted 9 lines
drivers/gpio/gpio-graniterapids.c new file +9 -0
+static int gnr_gpio_configure_pad(struct gpio_chip *gc, unsigned int gpio,
+                                 u32 clear_mask, u32 set_mask)
+{
+       struct gnr_gpio *priv = gpiochip_get_data(gc);
+       void __iomem *addr = gnr_gpio_get_padcfg_addr(priv, gpio);
+       u32 dw;
+
+       if (test_bit(gpio, priv->ro_bitmap))
+               return -EACCES;

drivers/gpio/gpio-graniterapids.c line 88

Quoted 10 lines
drivers/gpio/gpio-graniterapids.c new file +10 -0
+
+       guard(raw_spinlock_irqsave)(&priv->lock);
+
+       dw = readl(addr);
+       dw &= ~clear_mask;
+       dw |= set_mask;
+       writel(dw, addr);
+
+       return 0;
+}

Configure pad sounds like pin control so it's a bit of icky name. What it really does is configure the direction (in or out) for this GPIO pad. And it's not really the *pad* that is configured, right? It is the hardware *driver* for the pad, i.e. what is reflected in the GPIO line control register.

Can you rename this:
gnr_gpio_configure_direction()?

With the above stuff addressed:

Yours,
Linus Walleij

#2 lore raw

Hi Linus,

Thanks for the review!

Quoted 27 lines · Linus Walleij

On Fri, Apr 19, 2024 at 10:07 AM Aapo Vienamo

Quoted 9 lines
drivers/gpio/gpio-graniterapids.c new file +9 -0
+static int gnr_gpio_configure_pad(struct gpio_chip *gc, unsigned int gpio,
+                                 u32 clear_mask, u32 set_mask)
+{
+       struct gnr_gpio *priv = gpiochip_get_data(gc);
+       void __iomem *addr = gnr_gpio_get_padcfg_addr(priv, gpio);
+       u32 dw;
+
+       if (test_bit(gpio, priv->ro_bitmap))
+               return -EACCES;

drivers/gpio/gpio-graniterapids.c line 88

Quoted 10 lines
drivers/gpio/gpio-graniterapids.c new file +10 -0
+
+       guard(raw_spinlock_irqsave)(&priv->lock);
+
+       dw = readl(addr);
+       dw &= ~clear_mask;
+       dw |= set_mask;
+       writel(dw, addr);
+
+       return 0;
+}

Configure pad sounds like pin control so it's a bit of icky name. What it really does is configure the direction (in or out) for this GPIO pad. And it's not really the *pad* that is configured, right? It is the hardware *driver* for the pad, i.e. what is reflected in the GPIO line control register.

Quoted 2 lines

Can you rename this:
gnr_gpio_configure_direction()?

I do agree that the pad part of the name maybe isn't the best, though this function isn't just for direction control, since it's used for setting the pin output state as well in gnr_gpio_set(). The idea is that locking and masking of the register accesses is factored out of the gpio callbacks and implemented in this function.

Maybe gnr_gpio_configure_pin()?

Best regards,
Aapo

#3 lore raw
Quoted 10 lines · Aapo Vienamo
Quoted 2 lines

Can you rename this:
gnr_gpio_configure_direction()?

I do agree that the pad part of the name maybe isn't the best, though this function isn't just for direction control, since it's used for setting the pin output state as well in gnr_gpio_set(). The idea is that locking and masking of the register accesses is factored out of the gpio callbacks and implemented in this function.

Maybe gnr_gpio_configure_pin()?

gnr_gpio_configure_line() in that case, it clearly isn't a pin since it is virtual and for that reason called *v*GPIO, right? Pins are a very physical thing. It's that kind of confusion I want to avoid in naming.

Yours,
Linus Walleij

#4 lore raw
In reply to Aapo Vienamo
Quoted 10 lines · Aapo Vienamo

This driver provides a basic GPIO driver for the Intel Granite Rapids-D virtual GPIOs. On SoCs with limited physical pins on the package, the physical pins controlled by this driver would be exposed on an external device such as a BMC or CPLD.

Hmm... How did this happen?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Aapo Vienamo

This driver provides a basic GPIO driver for the Intel Granite Rapids-D virtual GPIOs. On SoCs with limited physical pins on the package, the physical pins controlled by this driver would be exposed on an external device such as a BMC or CPLD.

Quoted 6 lines
Quoted 4 lines

Hmm... How did this happen?

This happened because the commits got mixed up between the internal tree and my own branch. Apologies for that.

Sincerely,
Aapo

#6 lore raw
In reply to Aapo Vienamo
Quoted 7 lines

-----Original Message-----
From: Aapo Vienamo <[email protected]>
Sent: Friday, April 19, 2024 3:06 AM
Subject: [PATCH] gpio: Add Intel Granite Rapids-D vGPIO driver

...

Quoted 25 lines
drivers/gpio/gpio-graniterapids.c new file +3 -0
+static int gnr_gpio_probe(struct platform_device *pdev)
+{
+	size_t num_backup_pins = IS_ENABLED(CONFIG_PM_SLEEP) ? GNR_NUM_PINS

: 0;

drivers/gpio/gpio-graniterapids.c new file +7 -0
+	struct device *dev = &pdev->dev;
+	struct gpio_irq_chip *girq;
+	struct gnr_gpio *priv;
+	void __iomem *regs;
+	int irq, ret;
+
+	priv = devm_kzalloc(dev, struct_size(priv, pad_backup,

num_backup_pins), GFP_KERNEL);

drivers/gpio/gpio-graniterapids.c new file +12 -0
+	if (!priv)
+		return -ENOMEM;
+
+	regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	ret = devm_request_irq(dev, irq, gnr_gpio_irq, IRQF_SHARED |

IRQF_NO_THREAD,

drivers/gpio/gpio-graniterapids.c line 309

Quoted 3 lines
drivers/gpio/gpio-graniterapids.c new file +3 -0
+			       dev_name(dev), priv);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to request interrupt\n");

All of these early returns will leave the memory allocated to priv forever, since priv isn't saved anywhere until:

Quoted 1 line
drivers/gpio/gpio-graniterapids.c new file +1 -0
+	platform_set_drvdata(pdev, priv);
#7 lore raw
Quoted 3 lines · Robert (Servers)
Quoted 3 lines

-----Original Message-----
From: Aapo Vienamo <[email protected]>
Sent: Friday, April 19, 2024 3:06 AM

...

Quoted 23 lines
Quoted 23 lines
drivers/gpio/gpio-graniterapids.c new file +3 -0
+static int gnr_gpio_probe(struct platform_device *pdev)
+{
+     size_t num_backup_pins = IS_ENABLED(CONFIG_PM_SLEEP) ? GNR_NUM_PINS

: 0;

drivers/gpio/gpio-graniterapids.c new file +7 -0
+     struct device *dev = &pdev->dev;
+     struct gpio_irq_chip *girq;
+     struct gnr_gpio *priv;
+     void __iomem *regs;
+     int irq, ret;
+
+     priv = devm_kzalloc(dev, struct_size(priv, pad_backup,

num_backup_pins), GFP_KERNEL);

drivers/gpio/gpio-graniterapids.c new file +11 -0
+     if (!priv)
+             return -ENOMEM;
+
+     regs = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(regs))
+             return PTR_ERR(regs);
+
+     irq = platform_get_irq(pdev, 0);
+     if (irq < 0)
+             return irq;
+
Quoted 10 lines

drivers/gpio/gpio-graniterapids.c line 309

Quoted 5 lines

+ ret = devm_request_irq(dev, irq, gnr_gpio_irq, IRQF_SHARED |
IRQF_NO_THREAD,

drivers/gpio/gpio-graniterapids.c new file +3 -0
+                            dev_name(dev), priv);
+     if (ret)
+             return dev_err_probe(dev, ret, "failed to request interrupt\n");

All of these early returns will leave the memory allocated to priv forever, since priv isn't saved anywhere until:

Quoted 1 line

+ platform_set_drvdata(pdev, priv);

I'm not sure I got your comment right. Do you think there is a memory leak on the error paths above? If so, can you elaborate, because I don't see it?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 41 lines · Andy Shevchenko
Quoted 3 lines · Robert (Servers)
Quoted 3 lines

-----Original Message-----
From: Aapo Vienamo <[email protected]>
Sent: Friday, April 19, 2024 3:06 AM

...

Quoted 31 lines
Quoted 25 lines
drivers/gpio/gpio-graniterapids.c new file +3 -0
+static int gnr_gpio_probe(struct platform_device *pdev)
+{
+     size_t num_backup_pins = IS_ENABLED(CONFIG_PM_SLEEP) ? GNR_NUM_PINS

: 0;

drivers/gpio/gpio-graniterapids.c new file +7 -0
+     struct device *dev = &pdev->dev;
+     struct gpio_irq_chip *girq;
+     struct gnr_gpio *priv;
+     void __iomem *regs;
+     int irq, ret;
+
+     priv = devm_kzalloc(dev, struct_size(priv, pad_backup,

num_backup_pins), GFP_KERNEL);

drivers/gpio/gpio-graniterapids.c new file +12 -0
+     if (!priv)
+             return -ENOMEM;
+
+     regs = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(regs))
+             return PTR_ERR(regs);
+
+     irq = platform_get_irq(pdev, 0);
+     if (irq < 0)
+             return irq;
+
+     ret = devm_request_irq(dev, irq, gnr_gpio_irq, IRQF_SHARED |

IRQF_NO_THREAD,

drivers/gpio/gpio-graniterapids.c line 309

Quoted 3 lines
drivers/gpio/gpio-graniterapids.c new file +3 -0
+                            dev_name(dev), priv);
+     if (ret)
+             return dev_err_probe(dev, ret, "failed to request interrupt\n");

All of these early returns will leave the memory allocated to priv forever, since priv isn't saved anywhere until:

Quoted 6 lines
Quoted 1 line
Quoted 1 line

+ platform_set_drvdata(pdev, priv);

I'm not sure I got your comment right. Do you think there is a memory leak on the error paths above? If so, can you elaborate, because I don't see it?

Elliott: if probe fails and we return, all devm_ resources will be released. I don't see any error in this code either.

Bart

#9 lore raw
In reply to Andy Shevchenko
Quoted 9 lines

-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Friday, April 19, 2024 5:02 PM
Subject: Re: [PATCH] gpio: Add Intel Granite Rapids-D vGPIO driver

On Fri, Apr 19, 2024 at 11:00 PM Elliott, Robert (Servers) <[email protected]> wrote:

...

Quoted 20 lines
Quoted 10 lines
Quoted 10 lines

+ priv = devm_kzalloc(dev, struct_size(priv, pad_backup,
num_backup_pins), GFP_KERNEL);

drivers/gpio/gpio-graniterapids.c new file +8 -0
+     if (!priv)
+             return -ENOMEM;
+
+     regs = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(regs))
+             return PTR_ERR(regs);
+
+     irq = platform_get_irq(pdev, 0);
Quoted 8 lines

drivers/gpio/gpio-graniterapids.c

Quoted 8 lines
drivers/gpio/gpio-graniterapids.c new file +4 -0
+     if (irq < 0)
+             return irq;
+
+     ret = devm_request_irq(dev, irq, gnr_gpio_irq, IRQF_SHARED |

IRQF_NO_THREAD,

drivers/gpio/gpio-graniterapids.c new file +3 -0
+                            dev_name(dev), priv);
+     if (ret)
+             return dev_err_probe(dev, ret, "failed to request

interrupt\n");

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

All of these early returns will leave the memory allocated to priv forever, since priv isn't saved anywhere until:

Quoted 1 line

+ platform_set_drvdata(pdev, priv);

I'm not sure I got your comment right. Do you think there is a memory leak on the error paths above? If so, can you elaborate, because I don't see it?

I didn't recall that devm_kzalloc saves the pointer on its own (into dev), so it looks safe.

#10 lore raw
In reply to Aapo Vienamo
Quoted 45 lines · Aapo Vienamo

This driver provides a basic GPIO driver for the Intel Granite Rapids-D virtual GPIOs. On SoCs with limited physical pins on the package, the physical pins controlled by this driver would be exposed on an external device such as a BMC or CPLD.

drivers/gpio/gpio-graniterapids.c new file +29 -0
new file mode 100644
index 000000000000..61bcafe1985e
@@ -0,0 +1,382 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Intel Granite Rapids-D vGPIO driver
+ *
+ * Copyright (c) 2024, Intel Corporation.
+ *
+ * Author: Aapo Vienamo <[email protected]>
+ */
+
+#include <linux/array_size.h>
+#include <linux/bitmap.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gfp_types.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/math.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/overflow.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/spinlock.h>
+#include <linux/types.h>
+
+#include <linux/gpio/driver.h>
+

drivers/gpio/gpio-graniterapids.c line 39

Quoted 10 lines
drivers/gpio/gpio-graniterapids.c new file +10 -0
+#define GNR_NUM_PINS 128
+#define GNR_PINS_PER_REG 32
+#define GNR_NUM_REGS DIV_ROUND_UP(GNR_NUM_PINS, GNR_PINS_PER_REG)
+
+#define GNR_CFG_BAR		0x00
+#define GNR_CFG_LOCK_OFFSET	0x04
+#define GNR_GPI_STATUS_OFFSET	0x20
+#define GNR_GPI_ENABLE_OFFSET	0x24
+
+#define GNR_CFG_DW_RX_MASK	(3 << 22)

GENMASK()

+ #include <linux/bits.h>

drivers/gpio/gpio-graniterapids.c line 42

Quoted 3 lines
drivers/gpio/gpio-graniterapids.c new file +3 -0
+#define GNR_CFG_DW_RX_DISABLE	(2 << 22)
+#define GNR_CFG_DW_RX_EDGE	(1 << 22)
+#define GNR_CFG_DW_RX_LEVEL	(0 << 22)

FIELD_PREP(GNR_CFG_DW_RX_MASK, xx) x 3

drivers/gpio/gpio-graniterapids.c line 46

Quoted 4 lines
drivers/gpio/gpio-graniterapids.c new file +4 -0
+#define GNR_CFG_DW_RXDIS	BIT(4)
+#define GNR_CFG_DW_TXDIS	BIT(3)
+#define GNR_CFG_DW_RXSTATE	BIT(1)
+#define GNR_CFG_DW_TXSTATE	BIT(0)

These require #include <linux/bits.h> (just pointing this out so you know in future, you'll need to add it anyway for GENMASK() as mentioned above).

Signature · 1 line
 i.