All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anup Patel <apatel@ventanamicro.com>
To: opensbi@lists.infradead.org
Subject: [PATCH v2 08/18] lib: utils/gpio: Use heap in SiFive and StartFive GPIO drivers
Date: Mon,  5 Jun 2023 17:07:39 +0530	[thread overview]
Message-ID: <20230605113749.230696-9-apatel@ventanamicro.com> (raw)
In-Reply-To: <20230605113749.230696-1-apatel@ventanamicro.com>

Let's use heap allocation in SiFive and Starfive GPIO drivers
instead of using a fixed size global array.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
---
 lib/utils/gpio/fdt_gpio_sifive.c   | 21 ++++++++++-----------
 lib/utils/gpio/fdt_gpio_starfive.c | 20 ++++++++++----------
 2 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/lib/utils/gpio/fdt_gpio_sifive.c b/lib/utils/gpio/fdt_gpio_sifive.c
index 677f0fa..5e3f39d 100644
--- a/lib/utils/gpio/fdt_gpio_sifive.c
+++ b/lib/utils/gpio/fdt_gpio_sifive.c
@@ -9,11 +9,10 @@
 
 #include <sbi/riscv_io.h>
 #include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/gpio/fdt_gpio.h>
 
-#define SIFIVE_GPIO_CHIP_MAX	2
-
 #define SIFIVE_GPIO_PINS_MIN	1
 #define SIFIVE_GPIO_PINS_MAX	32
 #define SIFIVE_GPIO_PINS_DEF	16
@@ -27,9 +26,6 @@ struct sifive_gpio_chip {
 	struct gpio_chip chip;
 };
 
-static unsigned int sifive_gpio_chip_count;
-static struct sifive_gpio_chip sifive_gpio_chip_array[SIFIVE_GPIO_CHIP_MAX];
-
 static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 {
 	unsigned int v;
@@ -73,13 +69,15 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
 	struct sifive_gpio_chip *chip;
 	uint64_t addr;
 
-	if (SIFIVE_GPIO_CHIP_MAX <= sifive_gpio_chip_count)
-		return SBI_ENOSPC;
-	chip = &sifive_gpio_chip_array[sifive_gpio_chip_count];
+	chip = sbi_zalloc(sizeof(*chip));
+	if (!chip)
+		return SBI_ENOMEM;
 
 	rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
-	if (rc)
+	if (rc) {
+		sbi_free(chip);
 		return rc;
+	}
 
 	chip->addr = addr;
 	chip->chip.driver = &fdt_gpio_sifive;
@@ -88,10 +86,11 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
 	chip->chip.direction_output = sifive_gpio_direction_output;
 	chip->chip.set = sifive_gpio_set;
 	rc = gpio_chip_add(&chip->chip);
-	if (rc)
+	if (rc) {
+		sbi_free(chip);
 		return rc;
+	}
 
-	sifive_gpio_chip_count++;
 	return 0;
 }
 
diff --git a/lib/utils/gpio/fdt_gpio_starfive.c b/lib/utils/gpio/fdt_gpio_starfive.c
index 18cca72..f430b13 100644
--- a/lib/utils/gpio/fdt_gpio_starfive.c
+++ b/lib/utils/gpio/fdt_gpio_starfive.c
@@ -9,11 +9,11 @@
 
 #include <sbi/riscv_io.h>
 #include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
 #include <sbi/sbi_console.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/gpio/fdt_gpio.h>
 
-#define STARFIVE_GPIO_CHIP_MAX		2
 #define STARFIVE_GPIO_PINS_DEF		64
 #define STARFIVE_GPIO_OUTVAL		0x40
 #define STARFIVE_GPIO_MASK		0xff
@@ -25,9 +25,6 @@ struct starfive_gpio_chip {
 	struct gpio_chip chip;
 };
 
-static unsigned int starfive_gpio_chip_count;
-static struct starfive_gpio_chip starfive_gpio_chip_array[STARFIVE_GPIO_CHIP_MAX];
-
 static int starfive_gpio_direction_output(struct gpio_pin *gp, int value)
 {
 	u32 val;
@@ -82,13 +79,15 @@ static int starfive_gpio_init(void *fdt, int nodeoff, u32 phandle,
 	struct starfive_gpio_chip *chip;
 	u64 addr;
 
-	if (starfive_gpio_chip_count >= STARFIVE_GPIO_CHIP_MAX)
-		return SBI_ENOSPC;
-	chip = &starfive_gpio_chip_array[starfive_gpio_chip_count];
+	chip = sbi_zalloc(sizeof(*chip));
+	if (!chip)
+		return SBI_ENOMEM;
 
 	rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
-	if (rc)
+	if (rc) {
+		sbi_free(chip);
 		return rc;
+	}
 
 	chip->addr = addr;
 	chip->chip.driver = &fdt_gpio_starfive;
@@ -97,10 +96,11 @@ static int starfive_gpio_init(void *fdt, int nodeoff, u32 phandle,
 	chip->chip.direction_output = starfive_gpio_direction_output;
 	chip->chip.set = starfive_gpio_set;
 	rc = gpio_chip_add(&chip->chip);
-	if (rc)
+	if (rc) {
+		sbi_free(chip);
 		return rc;
+	}
 
-	starfive_gpio_chip_count++;
 	return 0;
 }
 
-- 
2.34.1



  parent reply	other threads:[~2023-06-05 11:37 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-05 11:37 [PATCH v2 00/18] Introduce and use simple heap allocator Anup Patel
2023-06-05 11:37 ` [PATCH v2 01/18] include: sbi_scratch: Add helper macros to access data type Anup Patel
2023-06-05 11:37 ` [PATCH v2 02/18] platform: Allow platforms to specify heap size Anup Patel
2023-06-05 11:37 ` [PATCH v2 03/18] lib: sbi: Introduce simple heap allocator Anup Patel
2023-06-05 11:37 ` [PATCH v2 04/18] lib: sbi: Print scratch size and usage at boot time Anup Patel
2023-06-05 11:37 ` [PATCH v2 05/18] lib: sbi_pmu: Use heap for per-HART PMU state Anup Patel
2023-06-05 11:37 ` [PATCH v2 06/18] lib: sbi: Use heap for root domain creation Anup Patel
2023-06-05 11:37 ` [PATCH v2 07/18] lib: sbi: Use scratch space to save per-HART domain pointer Anup Patel
2023-06-05 11:37 ` Anup Patel [this message]
2023-06-05 11:37 ` [PATCH v2 09/18] lib: utils/i2c: Use heap in DesignWare and SiFive I2C drivers Anup Patel
2023-06-05 11:37 ` [PATCH v2 10/18] lib: utils/ipi: Use heap in ACLINT MSWI driver Anup Patel
2023-06-05 11:37 ` [PATCH v2 11/18] lib: utils/irqchip: Use heap in PLIC, APLIC and IMSIC drivers Anup Patel
2023-06-05 11:37 ` [PATCH v2 12/18] lib: utils/timer: Use heap in ACLINT MTIMER driver Anup Patel
2023-06-05 11:37 ` [PATCH v2 13/18] lib: utils/fdt: Use heap in FDT domain parsing Anup Patel
2023-06-05 11:37 ` [PATCH v2 14/18] lib: utils/ipi: Use scratch space to save per-HART MSWI pointer Anup Patel
2023-06-05 11:37 ` [PATCH v2 15/18] lib: utils/timer: Use scratch space to save per-HART MTIMER pointer Anup Patel
2023-06-05 11:37 ` [PATCH v2 16/18] lib: utils/irqchip: Use scratch space to save per-HART PLIC pointer Anup Patel
2023-06-05 11:37 ` [PATCH v2 17/18] lib: utils/irqchip: Don't check hartid in imsic_update_hartid_table() Anup Patel
2023-06-05 11:37 ` [PATCH v2 18/18] lib: utils/irqchip: Use scratch space to save per-HART IMSIC pointer Anup Patel
2023-06-06 10:33 ` [PATCH v2 00/18] Introduce and use simple heap allocator Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230605113749.230696-9-apatel@ventanamicro.com \
    --to=apatel@ventanamicro.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.