Skip to content

Commit 14e85c0

Browse files
Gnuroulinusw
authored andcommitted
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: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent c8aaa1b commit 14e85c0

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

drivers/gpio/gpiolib.c

+31-16
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
*/
4848
DEFINE_SPINLOCK(gpio_lock);
4949

50-
static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
51-
5250
#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
5351

5452
static DEFINE_MUTEX(gpio_lookup_lock);
@@ -65,10 +63,22 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
6563
*/
6664
struct gpio_desc *gpio_to_desc(unsigned gpio)
6765
{
68-
if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
69-
return NULL;
70-
else
71-
return &gpio_desc[gpio];
66+
struct gpio_chip *chip;
67+
unsigned long flags;
68+
69+
spin_lock_irqsave(&gpio_lock, flags);
70+
71+
list_for_each_entry(chip, &gpio_chips, list) {
72+
if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
73+
spin_unlock_irqrestore(&gpio_lock, flags);
74+
return &chip->desc[gpio - chip->base];
75+
}
76+
}
77+
78+
spin_unlock_irqrestore(&gpio_lock, flags);
79+
80+
WARN(1, "invalid GPIO %d\n", gpio);
81+
return NULL;
7282
}
7383
EXPORT_SYMBOL_GPL(gpio_to_desc);
7484

@@ -91,7 +101,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
91101
*/
92102
int desc_to_gpio(const struct gpio_desc *desc)
93103
{
94-
return desc - &gpio_desc[0];
104+
return desc->chip->base + (desc - &desc->chip->desc[0]);
95105
}
96106
EXPORT_SYMBOL_GPL(desc_to_gpio);
97107

@@ -206,7 +216,7 @@ static int gpiochip_add_to_list(struct gpio_chip *chip)
206216
/**
207217
* gpiochip_add() - register a gpio_chip
208218
* @chip: the chip to register, with chip->base initialized
209-
* Context: potentially before irqs or kmalloc will work
219+
* Context: potentially before irqs will work
210220
*
211221
* Returns a negative errno if the chip can't be registered, such as
212222
* because the chip->base is invalid or already associated with a
@@ -226,12 +236,11 @@ int gpiochip_add(struct gpio_chip *chip)
226236
int status = 0;
227237
unsigned id;
228238
int base = chip->base;
239+
struct gpio_desc *descs;
229240

230-
if (base >= 0 &&
231-
(!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1))) {
232-
status = -EINVAL;
233-
goto fail;
234-
}
241+
descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
242+
if (!descs)
243+
return -ENOMEM;
235244

236245
spin_lock_irqsave(&gpio_lock, flags);
237246

@@ -247,10 +256,8 @@ int gpiochip_add(struct gpio_chip *chip)
247256
status = gpiochip_add_to_list(chip);
248257

249258
if (status == 0) {
250-
chip->desc = &gpio_desc[chip->base];
251-
252259
for (id = 0; id < chip->ngpio; id++) {
253-
struct gpio_desc *desc = &chip->desc[id];
260+
struct gpio_desc *desc = &descs[id];
254261
desc->chip = chip;
255262

256263
/* REVISIT: most hardware initializes GPIOs as
@@ -266,6 +273,8 @@ int gpiochip_add(struct gpio_chip *chip)
266273
}
267274
}
268275

276+
chip->desc = descs;
277+
269278
spin_unlock_irqrestore(&gpio_lock, flags);
270279

271280
#ifdef CONFIG_PINCTRL
@@ -291,6 +300,9 @@ int gpiochip_add(struct gpio_chip *chip)
291300
unlock:
292301
spin_unlock_irqrestore(&gpio_lock, flags);
293302
fail:
303+
kfree(descs);
304+
chip->desc = NULL;
305+
294306
/* failures here can mean systems won't boot... */
295307
pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
296308
chip->base, chip->base + chip->ngpio - 1,
@@ -331,6 +343,9 @@ void gpiochip_remove(struct gpio_chip *chip)
331343
list_del(&chip->list);
332344
spin_unlock_irqrestore(&gpio_lock, flags);
333345
gpiochip_unexport(chip);
346+
347+
kfree(chip->desc);
348+
chip->desc = NULL;
334349
}
335350
EXPORT_SYMBOL_GPL(gpiochip_remove);
336351

0 commit comments

Comments
 (0)