fc29fd821d
Current layout support was initially written without modules support in mind. When the requirement for module support rose, the existing base was improved to adopt modularization support, but kind of a design flaw was introduced. With the existing implementation, when a storage device registers into NVMEM, the core tries to hook a layout (if any) and populates its cells immediately. This means, if the hardware description expects a layout to be hooked up, but no driver was provided for that, the storage medium will fail to probe and try later from scratch. Even if we consider that the hardware description shall be correct, we could still probe the storage device (especially if it contains the rootfs). One way to overcome this situation is to consider the layouts as devices, and leverage the native notifier mechanism. When a new NVMEM device is registered, we can populate its nvmem-layout child, if any, and wait for the matching to be done in order to get the cells (the waiting can be easily done with the NVMEM notifiers). If the layout driver is compiled as a module, it should automatically be loaded. This way, there is no strong order to enforce, any NVMEM device creation or NVMEM layout driver insertion will be observed as a new event which may lead to the creation of additional cells, without disturbing the probes with costly (and sometimes endless) deferrals. In order to achieve that goal we create a new bus for the nvmem-layouts with minimal logic to match nvmem-layout devices with nvmem-layout drivers. All this infrastructure code is created in the layouts.c file. Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Tested-by: Rafał Miłecki <rafal@milecki.pl> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> Link: https://lore.kernel.org/r/20231215111536.316972-7-srinivas.kandagatla@linaro.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
202 lines
4.8 KiB
C
202 lines
4.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* NVMEM layout bus handling
|
|
*
|
|
* Copyright (C) 2023 Bootlin
|
|
* Author: Miquel Raynal <miquel.raynal@bootlin.com
|
|
*/
|
|
|
|
#include <linux/device.h>
|
|
#include <linux/dma-mapping.h>
|
|
#include <linux/nvmem-consumer.h>
|
|
#include <linux/nvmem-provider.h>
|
|
#include <linux/of.h>
|
|
#include <linux/of_device.h>
|
|
#include <linux/of_irq.h>
|
|
|
|
#include "internals.h"
|
|
|
|
#define to_nvmem_layout_driver(drv) \
|
|
(container_of((drv), struct nvmem_layout_driver, driver))
|
|
#define to_nvmem_layout_device(_dev) \
|
|
container_of((_dev), struct nvmem_layout, dev)
|
|
|
|
static int nvmem_layout_bus_match(struct device *dev, struct device_driver *drv)
|
|
{
|
|
return of_driver_match_device(dev, drv);
|
|
}
|
|
|
|
static int nvmem_layout_bus_probe(struct device *dev)
|
|
{
|
|
struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver);
|
|
struct nvmem_layout *layout = to_nvmem_layout_device(dev);
|
|
|
|
if (!drv->probe || !drv->remove)
|
|
return -EINVAL;
|
|
|
|
return drv->probe(layout);
|
|
}
|
|
|
|
static void nvmem_layout_bus_remove(struct device *dev)
|
|
{
|
|
struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver);
|
|
struct nvmem_layout *layout = to_nvmem_layout_device(dev);
|
|
|
|
return drv->remove(layout);
|
|
}
|
|
|
|
static struct bus_type nvmem_layout_bus_type = {
|
|
.name = "nvmem-layout",
|
|
.match = nvmem_layout_bus_match,
|
|
.probe = nvmem_layout_bus_probe,
|
|
.remove = nvmem_layout_bus_remove,
|
|
};
|
|
|
|
int nvmem_layout_driver_register(struct nvmem_layout_driver *drv)
|
|
{
|
|
drv->driver.bus = &nvmem_layout_bus_type;
|
|
|
|
return driver_register(&drv->driver);
|
|
}
|
|
EXPORT_SYMBOL_GPL(nvmem_layout_driver_register);
|
|
|
|
void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv)
|
|
{
|
|
driver_unregister(&drv->driver);
|
|
}
|
|
EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister);
|
|
|
|
static void nvmem_layout_release_device(struct device *dev)
|
|
{
|
|
struct nvmem_layout *layout = to_nvmem_layout_device(dev);
|
|
|
|
of_node_put(layout->dev.of_node);
|
|
kfree(layout);
|
|
}
|
|
|
|
static int nvmem_layout_create_device(struct nvmem_device *nvmem,
|
|
struct device_node *np)
|
|
{
|
|
struct nvmem_layout *layout;
|
|
struct device *dev;
|
|
int ret;
|
|
|
|
layout = kzalloc(sizeof(*layout), GFP_KERNEL);
|
|
if (!layout)
|
|
return -ENOMEM;
|
|
|
|
/* Create a bidirectional link */
|
|
layout->nvmem = nvmem;
|
|
nvmem->layout = layout;
|
|
|
|
/* Device model registration */
|
|
dev = &layout->dev;
|
|
device_initialize(dev);
|
|
dev->parent = &nvmem->dev;
|
|
dev->bus = &nvmem_layout_bus_type;
|
|
dev->release = nvmem_layout_release_device;
|
|
dev->coherent_dma_mask = DMA_BIT_MASK(32);
|
|
dev->dma_mask = &dev->coherent_dma_mask;
|
|
device_set_node(dev, of_fwnode_handle(of_node_get(np)));
|
|
of_device_make_bus_id(dev);
|
|
of_msi_configure(dev, dev->of_node);
|
|
|
|
ret = device_add(dev);
|
|
if (ret) {
|
|
put_device(dev);
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct of_device_id of_nvmem_layout_skip_table[] = {
|
|
{ .compatible = "fixed-layout", },
|
|
{}
|
|
};
|
|
|
|
static int nvmem_layout_bus_populate(struct nvmem_device *nvmem,
|
|
struct device_node *layout_dn)
|
|
{
|
|
int ret;
|
|
|
|
/* Make sure it has a compatible property */
|
|
if (!of_get_property(layout_dn, "compatible", NULL)) {
|
|
pr_debug("%s() - skipping %pOF, no compatible prop\n",
|
|
__func__, layout_dn);
|
|
return 0;
|
|
}
|
|
|
|
/* Fixed layouts are parsed manually somewhere else for now */
|
|
if (of_match_node(of_nvmem_layout_skip_table, layout_dn)) {
|
|
pr_debug("%s() - skipping %pOF node\n", __func__, layout_dn);
|
|
return 0;
|
|
}
|
|
|
|
if (of_node_check_flag(layout_dn, OF_POPULATED_BUS)) {
|
|
pr_debug("%s() - skipping %pOF, already populated\n",
|
|
__func__, layout_dn);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* NVMEM layout buses expect only a single device representing the layout */
|
|
ret = nvmem_layout_create_device(nvmem, layout_dn);
|
|
if (ret)
|
|
return ret;
|
|
|
|
of_node_set_flag(layout_dn, OF_POPULATED_BUS);
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
|
|
{
|
|
return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
|
|
}
|
|
EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
|
|
|
|
/*
|
|
* Returns the number of devices populated, 0 if the operation was not relevant
|
|
* for this nvmem device, an error code otherwise.
|
|
*/
|
|
int nvmem_populate_layout(struct nvmem_device *nvmem)
|
|
{
|
|
struct device_node *layout_dn;
|
|
int ret;
|
|
|
|
layout_dn = of_nvmem_layout_get_container(nvmem);
|
|
if (!layout_dn)
|
|
return 0;
|
|
|
|
/* Populate the layout device */
|
|
device_links_supplier_sync_state_pause();
|
|
ret = nvmem_layout_bus_populate(nvmem, layout_dn);
|
|
device_links_supplier_sync_state_resume();
|
|
|
|
of_node_put(layout_dn);
|
|
return ret;
|
|
}
|
|
|
|
void nvmem_destroy_layout(struct nvmem_device *nvmem)
|
|
{
|
|
struct device *dev;
|
|
|
|
if (!nvmem->layout)
|
|
return;
|
|
|
|
dev = &nvmem->layout->dev;
|
|
of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
|
|
device_unregister(dev);
|
|
}
|
|
|
|
int nvmem_layout_bus_register(void)
|
|
{
|
|
return bus_register(&nvmem_layout_bus_type);
|
|
}
|
|
|
|
void nvmem_layout_bus_unregister(void)
|
|
{
|
|
bus_unregister(&nvmem_layout_bus_type);
|
|
}
|