mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-28 23:23:55 +08:00
a86854d0c5
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc(). This patch replaces cases of: devm_kzalloc(handle, a * b, gfp) with: devm_kcalloc(handle, a * b, gfp) as well as handling cases of: devm_kzalloc(handle, a * b * c, gfp) with: devm_kzalloc(handle, array3_size(a, b, c), gfp) as it's slightly less ugly than: devm_kcalloc(handle, array_size(a, b), c, gfp) This does, however, attempt to ignore constant size factors like: devm_kzalloc(handle, 4 * 1024, gfp) though any constants defined via macros get caught up in the conversion. Any factors with a sizeof() of "unsigned char", "char", and "u8" were dropped, since they're redundant. Some manual whitespace fixes were needed in this patch, as Coccinelle really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...". The Coccinelle script used for this was: // Fix redundant parens around sizeof(). @@ expression HANDLE; type TYPE; expression THING, E; @@ ( devm_kzalloc(HANDLE, - (sizeof(TYPE)) * E + sizeof(TYPE) * E , ...) | devm_kzalloc(HANDLE, - (sizeof(THING)) * E + sizeof(THING) * E , ...) ) // Drop single-byte sizes and redundant parens. @@ expression HANDLE; expression COUNT; typedef u8; typedef __u8; @@ ( devm_kzalloc(HANDLE, - sizeof(u8) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(__u8) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(char) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(unsigned char) * (COUNT) + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(u8) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(__u8) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(char) * COUNT + COUNT , ...) | devm_kzalloc(HANDLE, - sizeof(unsigned char) * COUNT + COUNT , ...) ) // 2-factor product with sizeof(type/expression) and identifier or constant. @@ expression HANDLE; type TYPE; expression THING; identifier COUNT_ID; constant COUNT_CONST; @@ ( - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (COUNT_ID) + COUNT_ID, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * COUNT_ID + COUNT_ID, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (COUNT_CONST) + COUNT_CONST, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * COUNT_CONST + COUNT_CONST, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (COUNT_ID) + COUNT_ID, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * COUNT_ID + COUNT_ID, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (COUNT_CONST) + COUNT_CONST, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * COUNT_CONST + COUNT_CONST, sizeof(THING) , ...) ) // 2-factor product, only identifiers. @@ expression HANDLE; identifier SIZE, COUNT; @@ - devm_kzalloc + devm_kcalloc (HANDLE, - SIZE * COUNT + COUNT, SIZE , ...) // 3-factor product with 1 sizeof(type) or sizeof(expression), with // redundant parens removed. @@ expression HANDLE; expression THING; identifier STRIDE, COUNT; type TYPE; @@ ( devm_kzalloc(HANDLE, - sizeof(TYPE) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(TYPE)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * (COUNT) * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * (COUNT) * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * COUNT * (STRIDE) + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING) * COUNT * STRIDE + array3_size(COUNT, STRIDE, sizeof(THING)) , ...) ) // 3-factor product with 2 sizeof(variable), with redundant parens removed. @@ expression HANDLE; expression THING1, THING2; identifier COUNT; type TYPE1, TYPE2; @@ ( devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(TYPE2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(THING1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(THING1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * COUNT + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) | devm_kzalloc(HANDLE, - sizeof(TYPE1) * sizeof(THING2) * (COUNT) + array3_size(COUNT, sizeof(TYPE1), sizeof(THING2)) , ...) ) // 3-factor product, only identifiers, with redundant parens removed. @@ expression HANDLE; identifier STRIDE, SIZE, COUNT; @@ ( devm_kzalloc(HANDLE, - (COUNT) * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * (STRIDE) * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * STRIDE * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - (COUNT) * (STRIDE) * (SIZE) + array3_size(COUNT, STRIDE, SIZE) , ...) | devm_kzalloc(HANDLE, - COUNT * STRIDE * SIZE + array3_size(COUNT, STRIDE, SIZE) , ...) ) // Any remaining multi-factor products, first at least 3-factor products, // when they're not all constants... @@ expression HANDLE; expression E1, E2, E3; constant C1, C2, C3; @@ ( devm_kzalloc(HANDLE, C1 * C2 * C3, ...) | devm_kzalloc(HANDLE, - (E1) * E2 * E3 + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - (E1) * (E2) * E3 + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - (E1) * (E2) * (E3) + array3_size(E1, E2, E3) , ...) | devm_kzalloc(HANDLE, - E1 * E2 * E3 + array3_size(E1, E2, E3) , ...) ) // And then all remaining 2 factors products when they're not all constants, // keeping sizeof() as the second factor argument. @@ expression HANDLE; expression THING, E1, E2; type TYPE; constant C1, C2, C3; @@ ( devm_kzalloc(HANDLE, sizeof(THING) * C2, ...) | devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...) | devm_kzalloc(HANDLE, C1 * C2 * C3, ...) | devm_kzalloc(HANDLE, C1 * C2, ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * (E2) + E2, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(TYPE) * E2 + E2, sizeof(TYPE) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * (E2) + E2, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - sizeof(THING) * E2 + E2, sizeof(THING) , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - (E1) * E2 + E1, E2 , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - (E1) * (E2) + E1, E2 , ...) | - devm_kzalloc + devm_kcalloc (HANDLE, - E1 * E2 + E1, E2 , ...) ) Signed-off-by: Kees Cook <keescook@chromium.org>
196 lines
4.4 KiB
C
196 lines
4.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright (c) 2012, The Linux Foundation. All rights reserved.
|
|
*/
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/err.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/clk.h>
|
|
#include <linux/of.h>
|
|
#include <linux/of_address.h>
|
|
#include <linux/of_graph.h>
|
|
#include <linux/of_platform.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/amba/bus.h>
|
|
#include <linux/coresight.h>
|
|
#include <linux/cpumask.h>
|
|
#include <asm/smp_plat.h>
|
|
|
|
|
|
static int of_dev_node_match(struct device *dev, void *data)
|
|
{
|
|
return dev->of_node == data;
|
|
}
|
|
|
|
static struct device *
|
|
of_coresight_get_endpoint_device(struct device_node *endpoint)
|
|
{
|
|
struct device *dev = NULL;
|
|
|
|
/*
|
|
* If we have a non-configurable replicator, it will be found on the
|
|
* platform bus.
|
|
*/
|
|
dev = bus_find_device(&platform_bus_type, NULL,
|
|
endpoint, of_dev_node_match);
|
|
if (dev)
|
|
return dev;
|
|
|
|
/*
|
|
* We have a configurable component - circle through the AMBA bus
|
|
* looking for the device that matches the endpoint node.
|
|
*/
|
|
return bus_find_device(&amba_bustype, NULL,
|
|
endpoint, of_dev_node_match);
|
|
}
|
|
|
|
static void of_coresight_get_ports(const struct device_node *node,
|
|
int *nr_inport, int *nr_outport)
|
|
{
|
|
struct device_node *ep = NULL;
|
|
int in = 0, out = 0;
|
|
|
|
do {
|
|
ep = of_graph_get_next_endpoint(node, ep);
|
|
if (!ep)
|
|
break;
|
|
|
|
if (of_property_read_bool(ep, "slave-mode"))
|
|
in++;
|
|
else
|
|
out++;
|
|
|
|
} while (ep);
|
|
|
|
*nr_inport = in;
|
|
*nr_outport = out;
|
|
}
|
|
|
|
static int of_coresight_alloc_memory(struct device *dev,
|
|
struct coresight_platform_data *pdata)
|
|
{
|
|
/* List of output port on this component */
|
|
pdata->outports = devm_kcalloc(dev,
|
|
pdata->nr_outport,
|
|
sizeof(*pdata->outports),
|
|
GFP_KERNEL);
|
|
if (!pdata->outports)
|
|
return -ENOMEM;
|
|
|
|
/* Children connected to this component via @outports */
|
|
pdata->child_names = devm_kcalloc(dev,
|
|
pdata->nr_outport,
|
|
sizeof(*pdata->child_names),
|
|
GFP_KERNEL);
|
|
if (!pdata->child_names)
|
|
return -ENOMEM;
|
|
|
|
/* Port number on the child this component is connected to */
|
|
pdata->child_ports = devm_kcalloc(dev,
|
|
pdata->nr_outport,
|
|
sizeof(*pdata->child_ports),
|
|
GFP_KERNEL);
|
|
if (!pdata->child_ports)
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int of_coresight_get_cpu(const struct device_node *node)
|
|
{
|
|
int cpu;
|
|
struct device_node *dn;
|
|
|
|
dn = of_parse_phandle(node, "cpu", 0);
|
|
/* Affinity defaults to CPU0 */
|
|
if (!dn)
|
|
return 0;
|
|
cpu = of_cpu_node_to_id(dn);
|
|
of_node_put(dn);
|
|
|
|
/* Affinity to CPU0 if no cpu nodes are found */
|
|
return (cpu < 0) ? 0 : cpu;
|
|
}
|
|
EXPORT_SYMBOL_GPL(of_coresight_get_cpu);
|
|
|
|
struct coresight_platform_data *
|
|
of_get_coresight_platform_data(struct device *dev,
|
|
const struct device_node *node)
|
|
{
|
|
int i = 0, ret = 0;
|
|
struct coresight_platform_data *pdata;
|
|
struct of_endpoint endpoint, rendpoint;
|
|
struct device *rdev;
|
|
struct device_node *ep = NULL;
|
|
struct device_node *rparent = NULL;
|
|
struct device_node *rport = NULL;
|
|
|
|
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
|
|
if (!pdata)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
/* Use device name as sysfs handle */
|
|
pdata->name = dev_name(dev);
|
|
|
|
/* Get the number of input and output port for this component */
|
|
of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
|
|
|
|
if (pdata->nr_outport) {
|
|
ret = of_coresight_alloc_memory(dev, pdata);
|
|
if (ret)
|
|
return ERR_PTR(ret);
|
|
|
|
/* Iterate through each port to discover topology */
|
|
do {
|
|
/* Get a handle on a port */
|
|
ep = of_graph_get_next_endpoint(node, ep);
|
|
if (!ep)
|
|
break;
|
|
|
|
/*
|
|
* No need to deal with input ports, processing for as
|
|
* processing for output ports will deal with them.
|
|
*/
|
|
if (of_find_property(ep, "slave-mode", NULL))
|
|
continue;
|
|
|
|
/* Get a handle on the local endpoint */
|
|
ret = of_graph_parse_endpoint(ep, &endpoint);
|
|
|
|
if (ret)
|
|
continue;
|
|
|
|
/* The local out port number */
|
|
pdata->outports[i] = endpoint.port;
|
|
|
|
/*
|
|
* Get a handle on the remote port and parent
|
|
* attached to it.
|
|
*/
|
|
rparent = of_graph_get_remote_port_parent(ep);
|
|
rport = of_graph_get_remote_port(ep);
|
|
|
|
if (!rparent || !rport)
|
|
continue;
|
|
|
|
if (of_graph_parse_endpoint(rport, &rendpoint))
|
|
continue;
|
|
|
|
rdev = of_coresight_get_endpoint_device(rparent);
|
|
if (!rdev)
|
|
return ERR_PTR(-EPROBE_DEFER);
|
|
|
|
pdata->child_names[i] = dev_name(rdev);
|
|
pdata->child_ports[i] = rendpoint.id;
|
|
|
|
i++;
|
|
} while (ep);
|
|
}
|
|
|
|
pdata->cpu = of_coresight_get_cpu(node);
|
|
|
|
return pdata;
|
|
}
|
|
EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);
|