drivers: add new variants of devm_platform_ioremap_resource()
Subsystems (6)
cover drivers: add new variants of devm_platform_ioremap_resource()
From: Bartosz Golaszewski <[email protected]>
The new devm_platform_ioremap_resource() helper has now been widely adopted and used in many drivers. Users of nocache and write-combined ioremap() variants could profit from the same code shrinkage. This series provides two new versions of devm_platform_ioremap_resource() and uses it in a few example drivers with the assumption that - just like was the case previously - a coccinelle script will be developed to ease the transition for others.
Bartosz Golaszewski (9):
Documentation: devres: add missing entry for
devm_platform_ioremap_resource()
lib: devres: prepare devm_ioremap_resource() for more variants
lib: devres: provide new variants for devm_ioremap_resource()
drivers: provide new variants of devm_platform_ioremap_resource()
gpio: em: use devm_platform_ioremap_resource_nocache()
gpio: ath79: use devm_platform_ioremap_resource_nocache()
gpio: htc-egpio: use devm_platform_ioremap_resource_nocache()
gpio: xgene: use devm_platform_ioremap_resource_nocache()
misc: sram: use devm_platform_ioremap_resource_wc()
.../driver-api/driver-model/devres.rst | 5 ++ drivers/base/platform.c | 70 +++++++++++++++-- drivers/gpio/gpio-ath79.c | 10 +-- drivers/gpio/gpio-em.c | 22 +++--- drivers/gpio/gpio-htc-egpio.c | 13 ++-- drivers/gpio/gpio-xgene.c | 14 +--- drivers/misc/sram.c | 28 ++----- include/linux/device.h | 4 + include/linux/platform_device.h | 6 ++ lib/devres.c | 76 ++++++++++++++----- 10 files changed, 165 insertions(+), 83 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/9 Documentation: devres: add missing entry for devm_platform_ioremap_resource()
From: Bartosz Golaszewski <[email protected]>
devm_platform_ioremap_resource() should be documented in devres.rst. Add the missing entry.
--- Documentation/driver-api/driver-model/devres.rst | 1 + 1 file changed, 1 insertion(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
@@ -316,6 +316,7 @@ IOMAP devm_ioremap_nocache() devm_ioremap_wc() devm_ioremap_resource() : checks resource, requests memory region, ioremaps + devm_platform_ioremap_resource() : calls devm_ioremap_resource() for platform device devm_iounmap() pcim_iomap() pcim_iomap_regions() : do request_region() and iomap() on multiple BARs
Signature · 1 line
2.21.0
PATCH 2/9 lib: devres: prepare devm_ioremap_resource() for more variants
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We want to add the nocache and write-combined variants of devm_ioremap_resource(). Let's first implement __devm_ioremap_resource() which takes an additional argument type. The types are the same as for __devm_ioremap(). The existing devm_ioremap_resource() now simply calls __devm_ioremap_resource() with regular DEVM_IOREMAP type.
--- lib/devres.c | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-)
lib/devres.c +27 -20
@@ -114,25 +114,9 @@ void devm_iounmap(struct device *dev, void __iomem *addr) }EXPORT_SYMBOL (devm_iounmap); -/** -* devm_ioremap_resource() - check, request region, and ioremap resource -* @dev: generic device to handle the resource for -* @res: resource to be handled -* -* Checks that a resource is a valid memory region, requests the memory -* region and ioremaps it. All operations are managed and will be undone -* on driver detach. -* -* Returns a pointer to the remapped memory or an ERR_PTR() encoded error code -* on failure. Usage example: -* -* res = platform_get_resource(pdev, IORESOURCE_MEM, 0); -* base = devm_ioremap_resource(&pdev->dev, res); -* if (IS_ERR(base)) -* return PTR_ERR(base); -*/ -void __iomem *devm_ioremap_resource (struct device *dev, -const struct resource *res) +static void __iomem * +__devm_ioremap_resource (struct device *dev,const struct resource *res, +enum devm_ioremap_type type) {resource_size_t size;void __iomem *dest_ptr;
@@ -151,7 +135,7 @@ void __iomem *devm_ioremap_resource(struct device *dev,return IOMEM_ERR_PTR (-EBUSY ); } - dest_ptr =devm_ioremap (dev, res->start, size); + dest_ptr =__devm_ioremap (dev, res->start, size, type);if (!dest_ptr) {dev_err (dev,"ioremap failed for resource %pR\n" , res);devm_release_mem_region (dev, res->start, size);
@@ -160,6 +144,29 @@ void __iomem *devm_ioremap_resource(struct device *dev,return dest_ptr; } + +/** +* devm_ioremap_resource() - check, request region, and ioremap resource +* @dev: generic device to handle the resource for +* @res: resource to be handled +* +* Checks that a resource is a valid memory region, requests the memory +* region and ioremaps it. All operations are managed and will be undone +* on driver detach. +* +* Returns a pointer to the remapped memory or an ERR_PTR() encoded error code +* on failure. Usage example: +* +* res = platform_get_resource(pdev, IORESOURCE_MEM, 0); +* base = devm_ioremap_resource(&pdev->dev, res); +* if (IS_ERR(base)) +* return PTR_ERR(base); +*/ +void __iomem *devm_ioremap_resource (struct device *dev, +const struct resource *res) +{ +return __devm_ioremap_resource (dev, res,DEVM_IOREMAP ); +}EXPORT_SYMBOL (devm_ioremap_resource);/*
Signature · 1 line
2.21.0
PATCH 3/9 lib: devres: provide new variants for devm_ioremap_resource()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Provide two new variants of devm_ioremap_resource() - one for nocache and one for write-combined ioremap.
--- .../driver-api/driver-model/devres.rst | 2 ++ include/linux/device.h | 4 +++ lib/devres.c | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+)
Documentation/driver-api/driver-model/devres.rst +2 -0
@@ -316,6 +316,8 @@ IOMAP devm_ioremap_nocache() devm_ioremap_wc() devm_ioremap_resource() : checks resource, requests memory region, ioremaps + devm_ioremap_resource_nocache() + devm_ioremap_resource_wc() devm_platform_ioremap_resource() : calls devm_ioremap_resource() for platform device devm_iounmap() pcim_iomap()
include/linux/device.h +4 -0
@@ -710,6 +710,10 @@ extern void devm_free_pages(struct device *dev, unsigned long addr);void __iomem *devm_ioremap_resource (struct device *dev,const struct resource *res); +void __iomem *devm_ioremap_resource_nocache (struct device *dev, +const struct resource *res); +void __iomem *devm_ioremap_resource_wc (struct device *dev, +const struct resource *res); void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
lib/devres.c +29 -0
@@ -169,6 +169,35 @@ void __iomem *devm_ioremap_resource(struct device *dev, }EXPORT_SYMBOL (devm_ioremap_resource); +/** +* devm_ioremap_resource_nocache() - nocache variant of devm_ioremap_resource() +* @dev: generic device to handle the resource for +* @res: resource to be handled +* +* Returns a pointer to the remapped memory or an ERR_PTR() encoded error code +* on failure. Usage example: +*/ +void __iomem *devm_ioremap_resource_nocache (struct device *dev, +const struct resource *res) +{ +return __devm_ioremap_resource (dev, res,DEVM_IOREMAP_NC ); +} + +/** +* devm_ioremap_resource_wc() - write-combined variant of +* devm_ioremap_resource() +* @dev: generic device to handle the resource for +* @res: resource to be handled +* +* Returns a pointer to the remapped memory or an ERR_PTR() encoded error code +* on failure. Usage example: +*/ +void __iomem *devm_ioremap_resource_wc (struct device *dev, +const struct resource *res) +{ +return __devm_ioremap_resource (dev, res,DEVM_IOREMAP_WC ); +} +/* * devm_of_iomap - Requests a resource and maps the memory mapped IO * for a given device_node managed by a given device
Signature · 1 line
2.21.0
Arnd Bergmann
maintainerRe: [PATCH 3/9] lib: devres: provide new variants for devm_ioremap_resource()
lib/devres.c
Quoted 12 lines · Bartosz Golaszewski
include/linux/device.h +4
-0@@ -710,6 +710,10 @@ extern void devm_free_pages(struct device *dev, unsigned long addr);void __iomem *devm_ioremap_resource (struct device *dev,const struct resource *res); +void __iomem *devm_ioremap_resource_nocache (struct device *dev, +const struct resource *res); +void __iomem *devm_ioremap_resource_wc (struct device *dev, +const struct resource *res); void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,lib/devres.c +0
-0
I think adding devm_ioremap_resource_wc() and devm_platform_ioremap_resource_wc() makes sense, but I think we're better off without devm_ioremap_resource_nocache() and devm_ioremap_resource_cache().
The only architecture that actually has a difference between ioremap() and ioremap_nocache() seems to be ia64. I would generally assume that any driver using ioremap_nocache() that is not ia64 specific should just use ioremap().
The ia64 version of ioremap() tries to guess whether it needs a cached or uncached mapping, everyone else always gets uncached these days.
Arnd
PATCH 4/9 drivers: provide new variants of devm_platform_ioremap_resource()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Provide two new variants of devm_platform_ioremap_resource() - one for nocache and one for write-combined ioremap.
Move the core functionality into a separate static function - __devm_platform_ioremap_resource() - that takes an additional type argument.
--- .../driver-api/driver-model/devres.rst | 2 + drivers/base/platform.c | 70 +++++++++++++++++-- include/linux/platform_device.h | 6 ++ 3 files changed, 73 insertions(+), 5 deletions(-)
Documentation/driver-api/driver-model/devres.rst +2 -0
@@ -319,6 +319,8 @@ IOMAP devm_ioremap_resource_nocache() devm_ioremap_resource_wc() devm_platform_ioremap_resource() : calls devm_ioremap_resource() for platform device + devm_platform_ioremap_resource_nocache() + devm_platform_ioremap_resource_wc() devm_iounmap() pcim_iomap() pcim_iomap_regions() : do request_region() and iomap() on multiple BARs
drivers/base/platform.c +65 -5
87 lines not on this page · raw message
include/linux/platform_device.h +6 -0
@@ -54,6 +54,12 @@ extern struct resource *platform_get_resource(struct platform_device *,extern void __iomem *devm_platform_ioremap_resource (struct platform_device *pdev,unsigned int index); +extern void __iomem * +devm_platform_ioremap_resource_nocache (struct platform_device *pdev, +unsigned int index); +extern void __iomem * +devm_platform_ioremap_resource_wc (struct platform_device *pdev, +unsigned int index);extern int platform_get_irq (struct platform_device *,unsigned int );extern int platform_irq_count (struct platform_device *); extern struct resource *platform_get_resource_byname(struct platform_device *,
Signature · 1 line
2.21.0
PATCH 5/9 gpio: em: use devm_platform_ioremap_resource_nocache()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devm_platform_ioremap_resource_nocache() helper for memory range mapping instead of devm_ioremap_nocache() combined with a call to platform_get_resource().
--- drivers/gpio/gpio-em.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/gpio/gpio-em.c +9 -13
@@ -269,7 +269,7 @@ static void em_gio_irq_domain_remove(void *data)static int em_gio_probe (struct platform_device *pdev) {struct em_gio_priv *p; -struct resource *io[2 ], *irq[2 ]; +struct resource *irq[2 ];struct gpio_chip *gpio_chip;struct irq_chip *irq_chip;const char *name =dev_name (&pdev->dev);
@@ -284,25 +284,21 @@ static int em_gio_probe(struct platform_device *pdev)platform_set_drvdata (pdev, p);spin_lock_init (&p->sense_lock); - io[0 ] =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); - io[1 ] =platform_get_resource (pdev,IORESOURCE_MEM ,1 ); irq[0 ] =platform_get_resource (pdev,IORESOURCE_IRQ ,0 ); irq[1 ] =platform_get_resource (pdev,IORESOURCE_IRQ ,1 ); -if (!io[0 ] || !io[1 ] || !irq[0 ] || !irq[1 ]) { -dev_err (&pdev->dev,"missing IRQ or IOMEM\n" ); +if (!irq[0 ] || !irq[1 ]) { +dev_err (&pdev->dev,"missing IRQ resources\n" );return -EINVAL ; } - p->base0 =devm_ioremap_nocache (&pdev->dev, io[0 ]->start, -resource_size (io[0 ])); -if (!p->base0) -return -ENOMEM ; + p->base0 =devm_platform_ioremap_resource_nocache (pdev,0 ); +if (IS_ERR (p->base0)) +return PTR_ERR (p->base0); - p->base1 =devm_ioremap_nocache (&pdev->dev, io[1 ]->start, -resource_size (io[1 ])); -if (!p->base1) -return -ENOMEM ; + p->base1 =devm_platform_ioremap_resource_nocache (pdev,1 ); +if (IS_ERR (p->base1)) +return PTR_ERR (p->base1);if (of_property_read_u32 (pdev->dev.of_node,"ngpios" , &ngpios)) {dev_err (&pdev->dev,"Missing ngpios OF property\n" );
Signature · 1 line
2.21.0
PATCH 6/9 gpio: ath79: use devm_platform_ioremap_resource_nocache()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devm_platform_ioremap_resource_nocache() helper for memory range mapping instead of devm_ioremap_nocache() combined with a call to platform_get_resource().
--- drivers/gpio/gpio-ath79.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/gpio/gpio-ath79.c +3 -7
@@ -226,7 +226,6 @@ static int ath79_gpio_probe(struct platform_device *pdev)struct device_node *np = dev->of_node;struct ath79_gpio_ctrl *ctrl;struct gpio_irq_chip *girq; -struct resource *res;u32 ath79_gpio_count;bool oe_inverted;int err;
@@ -256,12 +255,9 @@ static int ath79_gpio_probe(struct platform_device *pdev)return -EINVAL ; } - res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (!res) -return -EINVAL ; - ctrl->base =devm_ioremap_nocache (dev, res->start,resource_size (res)); -if (!ctrl->base) -return -ENOMEM ; + ctrl->base =devm_platform_ioremap_resource_nocache (pdev,0 ); +if (IS_ERR (ctrl->base)) +return PTR_ERR (ctrl->base);raw_spin_lock_init (&ctrl->lock); err = bgpio_init(&ctrl->gc, dev, 4,
Signature · 1 line
2.21.0
PATCH 7/9 gpio: htc-egpio: use devm_platform_ioremap_resource_nocache()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devm_platform_ioremap_resource_nocache() helper for memory range mapping instead of devm_ioremap_nocache() combined with a call to platform_get_resource().
--- drivers/gpio/gpio-htc-egpio.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/gpio/gpio-htc-egpio.c +6 -7
@@ -295,14 +295,13 @@ static int __init egpio_probe(struct platform_device *pdev) ei->chained_irq = res->start;/* Map egpio chip into virtual address space. */ - res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (!res) + ei->base_addr =devm_platform_ioremap_resource_nocache (pdev,0 ); +if (IS_ERR (ei->base_addr)) { + ret =PTR_ERR (ei->base_addr); goto fail; - ei->base_addr =devm_ioremap_nocache (&pdev->dev, res->start, -resource_size (res)); -if (!ei->base_addr) - goto fail; -pr_debug ("EGPIO phys=%08x virt=%p\n" , (u32 )res->start, ei->base_addr); + } +pr_debug ("EGPIO phys=%08x virt=%p\n" , +virt_to_phys (ei->base_addr), ei->base_addr);if ((pdata->bus_width !=16 ) && (pdata->bus_width !=32 )) goto fail;
Signature · 1 line
2.21.0
PATCH 8/9 gpio: xgene: use devm_platform_ioremap_resource_nocache()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devm_platform_ioremap_resource_nocache() helper for memory range mapping instead of devm_ioremap_nocache() combined with a call to platform_get_resource().
--- drivers/gpio/gpio-xgene.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/gpio/gpio-xgene.c +3 -11
@@ -155,7 +155,6 @@ static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);static int xgene_gpio_probe (struct platform_device *pdev) { -struct resource *res;struct xgene_gpio *gpio;int err =0 ;
@@ -165,16 +164,9 @@ static int xgene_gpio_probe(struct platform_device *pdev) goto err; } - res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (!res) { - err = -EINVAL ; - goto err; - } - - gpio->base =devm_ioremap_nocache (&pdev->dev, res->start, -resource_size (res)); -if (!gpio->base) { - err = -ENOMEM ; + gpio->base =devm_platform_ioremap_resource_nocache (pdev,0 ); +if (IS_ERR (gpio->base)) { + err =PTR_ERR (gpio->base); goto err; }
Signature · 1 line
2.21.0
PATCH 9/9 misc: sram: use devm_platform_ioremap_resource_wc()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Use the new devm_platform_ioremap_resource_wc() helper instead of devm_ioremap_wc() combinded with a call to platform_get_resource(). Also use devm_platform_ioremap_resource() where applicable.
--- drivers/misc/sram.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-)
drivers/misc/sram.c +8 -20
@@ -340,8 +340,6 @@ static const struct of_device_id sram_dt_ids[] = {static int sram_probe (struct platform_device *pdev) {struct sram_dev *sram; -struct resource *res; -size_t size;int ret;int (*init_func)(void );
@@ -351,25 +349,14 @@ static int sram_probe(struct platform_device *pdev) sram->dev = &pdev->dev; - res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (!res) { -dev_err (sram->dev,"found no memory resource\n" ); -return -EINVAL ; - } - - size =resource_size (res); - -if (!devm_request_mem_region (sram->dev, res->start, size, pdev->name)) { -dev_err (sram->dev,"could not request region for resource\n" ); -return -EBUSY ; - } -if (of_property_read_bool (pdev->dev.of_node,"no-memory-wc" )) - sram->virt_base =devm_ioremap (sram->dev, res->start, size); + sram->virt_base =devm_platform_ioremap_resource (pdev,0 );else - sram->virt_base =devm_ioremap_wc (sram->dev, res->start, size); -if (!sram->virt_base) -return -ENOMEM ; + sram->virt_base =devm_platform_ioremap_resource_wc (pdev,0 ); +if (IS_ERR (sram->virt_base)) { +dev_err (&pdev->dev,"could not map SRAM registers\n" ); +return PTR_ERR (sram->virt_base); + } sram->pool =devm_gen_pool_create (sram->dev,ilog2 (SRAM_GRANULARITY ),NUMA_NO_NODE ,NULL );
@@ -382,7 +369,8 @@ static int sram_probe(struct platform_device *pdev)else clk_prepare_enable (sram->clk); - ret =sram_reserve_regions (sram, res); + ret =sram_reserve_regions (sram, +platform_get_resource (pdev,IORESOURCE_MEM ,0 ));if (ret) goto err_disable_clk;
Signature · 1 line
2.21.0
Hi Bartosz,
Quoted 9 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
The new
devm_platform_ioremap_resource()helper has now been widely adopted and used in many drivers. Users of nocache and write-combinedioremap()variants could profit from the same code shrinkage. This series provides two new versions ofdevm_platform_ioremap_resource()and uses it in a few example drivers with the assumption that - just like was the case previously - a coccinelle script will be developed to ease the transition for others.
Please be aware that the number of ioremap() variants is being reduced, as some of them are redundant (e.g. ioremap() already creates an uncached mapping, so ioremap_nocache() is not needed). So less is better than more ;-)
https://lore.kernel.org/lkml/[email protected]/
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 13 lines · Geert Uytterhoeven
Hi Bartosz,
Quoted 9 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
The new
devm_platform_ioremap_resource()helper has now been widely adopted and used in many drivers. Users of nocache and write-combinedioremap()variants could profit from the same code shrinkage. This series provides two new versions ofdevm_platform_ioremap_resource()and uses it in a few example drivers with the assumption that - just like was the case previously - a coccinelle script will be developed to ease the transition for others.
Quoted 4 lines
Please be aware that the number of
ioremap()variants is being reduced, as some of them are redundant (e.g.ioremap()already creates an uncached mapping, soioremap_nocache()is not needed). So less is better than more ;-)
Yes. If I can get the ia64 and openrisc patch in I plan to send Linus a scripted removal of ioremap_nocache after -rc1. I already have a local patch for current mainline as of about two weeks ago.