2014-10-21 12:28:05 +08:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file "COPYING" in the main directory of this archive
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Kevin Cernekee <cernekee@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2014-12-26 01:49:12 +08:00
|
|
|
#define pr_fmt(fmt) "bmips-dma: " fmt
|
|
|
|
|
2014-10-21 12:28:05 +08:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/dma-direction.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/init.h>
|
2014-12-26 01:49:12 +08:00
|
|
|
#include <linux/io.h>
|
2014-10-21 12:28:05 +08:00
|
|
|
#include <linux/of.h>
|
2014-12-26 01:49:12 +08:00
|
|
|
#include <linux/printk.h>
|
|
|
|
#include <linux/slab.h>
|
2014-10-21 12:28:05 +08:00
|
|
|
#include <linux/types.h>
|
2018-06-15 19:08:52 +08:00
|
|
|
#include <asm/bmips.h>
|
2014-10-21 12:28:05 +08:00
|
|
|
|
|
|
|
/*
|
2014-12-26 01:49:12 +08:00
|
|
|
* BCM338x has configurable address translation windows which allow the
|
2014-10-21 12:28:05 +08:00
|
|
|
* peripherals' DMA addresses to be different from the Zephyr-visible
|
|
|
|
* physical addresses. e.g. usb_dma_addr = zephyr_pa ^ 0x08000000
|
|
|
|
*
|
2014-12-26 01:49:12 +08:00
|
|
|
* If the "brcm,ubus" node has a "dma-ranges" property we will enable this
|
|
|
|
* translation globally using the provided information. This implements a
|
|
|
|
* very limited subset of "dma-ranges" support and it will probably be
|
|
|
|
* replaced by a more generic version later.
|
2014-10-21 12:28:05 +08:00
|
|
|
*/
|
|
|
|
|
2014-12-26 01:49:12 +08:00
|
|
|
struct bmips_dma_range {
|
|
|
|
u32 child_addr;
|
|
|
|
u32 parent_addr;
|
|
|
|
u32 size;
|
|
|
|
};
|
2014-10-21 12:28:05 +08:00
|
|
|
|
2014-12-26 01:49:12 +08:00
|
|
|
static struct bmips_dma_range *bmips_dma_ranges;
|
|
|
|
|
|
|
|
#define FLUSH_RAC 0x100
|
|
|
|
|
2018-06-15 19:08:52 +08:00
|
|
|
dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t pa)
|
2014-10-21 12:28:05 +08:00
|
|
|
{
|
2014-12-26 01:49:12 +08:00
|
|
|
struct bmips_dma_range *r;
|
|
|
|
|
|
|
|
for (r = bmips_dma_ranges; r && r->size; r++) {
|
|
|
|
if (pa >= r->child_addr &&
|
|
|
|
pa < (r->child_addr + r->size))
|
|
|
|
return pa - r->child_addr + r->parent_addr;
|
|
|
|
}
|
2014-10-21 12:28:05 +08:00
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:08:52 +08:00
|
|
|
phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dma_addr)
|
2014-10-21 12:28:05 +08:00
|
|
|
{
|
2014-12-26 01:49:12 +08:00
|
|
|
struct bmips_dma_range *r;
|
|
|
|
|
|
|
|
for (r = bmips_dma_ranges; r && r->size; r++) {
|
|
|
|
if (dma_addr >= r->parent_addr &&
|
|
|
|
dma_addr < (r->parent_addr + r->size))
|
|
|
|
return dma_addr - r->parent_addr + r->child_addr;
|
|
|
|
}
|
2014-10-21 12:28:05 +08:00
|
|
|
return dma_addr;
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:08:52 +08:00
|
|
|
void arch_sync_dma_for_cpu_all(struct device *dev)
|
|
|
|
{
|
|
|
|
void __iomem *cbr = BMIPS_GET_CBR();
|
|
|
|
u32 cfg;
|
|
|
|
|
|
|
|
if (boot_cpu_type() != CPU_BMIPS3300 &&
|
|
|
|
boot_cpu_type() != CPU_BMIPS4350 &&
|
|
|
|
boot_cpu_type() != CPU_BMIPS4380)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Flush stale data out of the readahead cache */
|
|
|
|
cfg = __raw_readl(cbr + BMIPS_RAC_CONFIG);
|
|
|
|
__raw_writel(cfg | 0x100, cbr + BMIPS_RAC_CONFIG);
|
|
|
|
__raw_readl(cbr + BMIPS_RAC_CONFIG);
|
|
|
|
}
|
|
|
|
|
2014-12-26 01:49:12 +08:00
|
|
|
static int __init bmips_init_dma_ranges(void)
|
2014-10-21 12:28:05 +08:00
|
|
|
{
|
2014-12-26 01:49:12 +08:00
|
|
|
struct device_node *np =
|
|
|
|
of_find_compatible_node(NULL, NULL, "brcm,ubus");
|
|
|
|
const __be32 *data;
|
|
|
|
struct bmips_dma_range *r;
|
|
|
|
int len;
|
2014-10-21 12:28:05 +08:00
|
|
|
|
|
|
|
if (!np)
|
|
|
|
return 0;
|
|
|
|
|
2014-12-26 01:49:12 +08:00
|
|
|
data = of_get_property(np, "dma-ranges", &len);
|
|
|
|
if (!data)
|
|
|
|
goto out_good;
|
|
|
|
|
|
|
|
len /= sizeof(*data) * 3;
|
|
|
|
if (!len)
|
|
|
|
goto out_bad;
|
|
|
|
|
|
|
|
/* add a dummy (zero) entry at the end as a sentinel */
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(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.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- 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 E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- 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 THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
bmips_dma_ranges = kcalloc(len + 1, sizeof(struct bmips_dma_range),
|
2014-12-26 01:49:12 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!bmips_dma_ranges)
|
|
|
|
goto out_bad;
|
2014-10-21 12:28:05 +08:00
|
|
|
|
2014-12-26 01:49:12 +08:00
|
|
|
for (r = bmips_dma_ranges; len; len--, r++) {
|
|
|
|
r->child_addr = be32_to_cpup(data++);
|
|
|
|
r->parent_addr = be32_to_cpup(data++);
|
|
|
|
r->size = be32_to_cpup(data++);
|
|
|
|
}
|
|
|
|
|
|
|
|
out_good:
|
2014-10-21 12:28:05 +08:00
|
|
|
of_node_put(np);
|
|
|
|
return 0;
|
2014-12-26 01:49:12 +08:00
|
|
|
|
|
|
|
out_bad:
|
|
|
|
pr_err("error parsing dma-ranges property\n");
|
|
|
|
of_node_put(np);
|
|
|
|
return -EINVAL;
|
2014-10-21 12:28:05 +08:00
|
|
|
}
|
2014-12-26 01:49:12 +08:00
|
|
|
arch_initcall(bmips_init_dma_ranges);
|