Commit 14e85c0e authored by Alexandre Courbot's avatar Alexandre Courbot Committed by Linus Walleij

gpio: remove gpio_descs global array

Replace the ARCH_NR_GPIOS-sized static array of GPIO descriptors by
dynamically-allocated arrays for each GPIO chip.

This change makes gpio_to_desc() perform in O(n) (where n is the number
of GPIO chips registered) instead of O(1), however since n is rarely
bigger than 1 or 2 no noticeable performance issue is expected.
Besides this provides more incentive for GPIO consumers to move to the
gpiod interface. One could use a O(log(n)) structure to link the GPIO
chips together, but considering the low limit of n the hypothetical
performance benefit is probably not worth the added complexity.

This patch uses kcalloc() in gpiochip_add(), which removes the ability
to add a chip before kcalloc() can operate. I am not aware of such
cases, but if someone bisects up to this patch then I will be proven
wrong...
Signed-off-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent c8aaa1bf
...@@ -47,8 +47,6 @@ ...@@ -47,8 +47,6 @@
*/ */
DEFINE_SPINLOCK(gpio_lock); DEFINE_SPINLOCK(gpio_lock);
static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
static DEFINE_MUTEX(gpio_lookup_lock); static DEFINE_MUTEX(gpio_lookup_lock);
...@@ -65,10 +63,22 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label) ...@@ -65,10 +63,22 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
*/ */
struct gpio_desc *gpio_to_desc(unsigned gpio) struct gpio_desc *gpio_to_desc(unsigned gpio)
{ {
if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) struct gpio_chip *chip;
return NULL; unsigned long flags;
else
return &gpio_desc[gpio]; spin_lock_irqsave(&gpio_lock, flags);
list_for_each_entry(chip, &gpio_chips, list) {
if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
spin_unlock_irqrestore(&gpio_lock, flags);
return &chip->desc[gpio - chip->base];
}
}
spin_unlock_irqrestore(&gpio_lock, flags);
WARN(1, "invalid GPIO %d\n", gpio);
return NULL;
} }
EXPORT_SYMBOL_GPL(gpio_to_desc); EXPORT_SYMBOL_GPL(gpio_to_desc);
...@@ -91,7 +101,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, ...@@ -91,7 +101,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
*/ */
int desc_to_gpio(const struct gpio_desc *desc) int desc_to_gpio(const struct gpio_desc *desc)
{ {
return desc - &gpio_desc[0]; return desc->chip->base + (desc - &desc->chip->desc[0]);
} }
EXPORT_SYMBOL_GPL(desc_to_gpio); EXPORT_SYMBOL_GPL(desc_to_gpio);
...@@ -206,7 +216,7 @@ static int gpiochip_add_to_list(struct gpio_chip *chip) ...@@ -206,7 +216,7 @@ static int gpiochip_add_to_list(struct gpio_chip *chip)
/** /**
* gpiochip_add() - register a gpio_chip * gpiochip_add() - register a gpio_chip
* @chip: the chip to register, with chip->base initialized * @chip: the chip to register, with chip->base initialized
* Context: potentially before irqs or kmalloc will work * Context: potentially before irqs will work
* *
* Returns a negative errno if the chip can't be registered, such as * Returns a negative errno if the chip can't be registered, such as
* because the chip->base is invalid or already associated with a * because the chip->base is invalid or already associated with a
...@@ -226,12 +236,11 @@ int gpiochip_add(struct gpio_chip *chip) ...@@ -226,12 +236,11 @@ int gpiochip_add(struct gpio_chip *chip)
int status = 0; int status = 0;
unsigned id; unsigned id;
int base = chip->base; int base = chip->base;
struct gpio_desc *descs;
if (base >= 0 && descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
(!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))) { if (!descs)
status = -EINVAL; return -ENOMEM;
goto fail;
}
spin_lock_irqsave(&gpio_lock, flags); spin_lock_irqsave(&gpio_lock, flags);
...@@ -247,10 +256,8 @@ int gpiochip_add(struct gpio_chip *chip) ...@@ -247,10 +256,8 @@ int gpiochip_add(struct gpio_chip *chip)
status = gpiochip_add_to_list(chip); status = gpiochip_add_to_list(chip);
if (status == 0) { if (status == 0) {
chip->desc = &gpio_desc[chip->base];
for (id = 0; id < chip->ngpio; id++) { for (id = 0; id < chip->ngpio; id++) {
struct gpio_desc *desc = &chip->desc[id]; struct gpio_desc *desc = &descs[id];
desc->chip = chip; desc->chip = chip;
/* REVISIT: most hardware initializes GPIOs as /* REVISIT: most hardware initializes GPIOs as
...@@ -266,6 +273,8 @@ int gpiochip_add(struct gpio_chip *chip) ...@@ -266,6 +273,8 @@ int gpiochip_add(struct gpio_chip *chip)
} }
} }
chip->desc = descs;
spin_unlock_irqrestore(&gpio_lock, flags); spin_unlock_irqrestore(&gpio_lock, flags);
#ifdef CONFIG_PINCTRL #ifdef CONFIG_PINCTRL
...@@ -291,6 +300,9 @@ int gpiochip_add(struct gpio_chip *chip) ...@@ -291,6 +300,9 @@ int gpiochip_add(struct gpio_chip *chip)
unlock: unlock:
spin_unlock_irqrestore(&gpio_lock, flags); spin_unlock_irqrestore(&gpio_lock, flags);
fail: fail:
kfree(descs);
chip->desc = NULL;
/* failures here can mean systems won't boot... */ /* failures here can mean systems won't boot... */
pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
chip->base, chip->base + chip->ngpio - 1, chip->base, chip->base + chip->ngpio - 1,
...@@ -331,6 +343,9 @@ void gpiochip_remove(struct gpio_chip *chip) ...@@ -331,6 +343,9 @@ void gpiochip_remove(struct gpio_chip *chip)
list_del(&chip->list); list_del(&chip->list);
spin_unlock_irqrestore(&gpio_lock, flags); spin_unlock_irqrestore(&gpio_lock, flags);
gpiochip_unexport(chip); gpiochip_unexport(chip);
kfree(chip->desc);
chip->desc = NULL;
} }
EXPORT_SYMBOL_GPL(gpiochip_remove); EXPORT_SYMBOL_GPL(gpiochip_remove);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment