2017-12-27 18:55:14 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-06-15 13:32:18 +00:00
|
|
|
#define pr_fmt(fmt) "OF: " fmt
|
|
|
|
|
2012-04-11 17:30:11 +00:00
|
|
|
#include <linux/device.h>
|
2018-03-14 18:15:52 +00:00
|
|
|
#include <linux/fwnode.h>
|
2010-06-08 13:48:09 +00:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/ioport.h>
|
2018-03-14 18:15:54 +00:00
|
|
|
#include <linux/logic_pio.h>
|
2010-06-08 13:48:10 +00:00
|
|
|
#include <linux/module.h>
|
2010-06-08 13:48:09 +00:00
|
|
|
#include <linux/of_address.h>
|
2024-09-05 07:46:01 +00:00
|
|
|
#include <linux/overflow.h>
|
2016-05-11 22:34:51 +00:00
|
|
|
#include <linux/pci.h>
|
2010-06-08 13:48:10 +00:00
|
|
|
#include <linux/pci_regs.h>
|
2014-09-29 14:29:21 +00:00
|
|
|
#include <linux/sizes.h>
|
|
|
|
#include <linux/slab.h>
|
2010-06-08 13:48:10 +00:00
|
|
|
#include <linux/string.h>
|
2020-09-17 16:43:40 +00:00
|
|
|
#include <linux/dma-direct.h> /* for bus_dma_region */
|
2010-06-08 13:48:09 +00:00
|
|
|
|
2019-07-02 17:42:39 +00:00
|
|
|
#include "of_private.h"
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/* Max address size we deal with */
|
|
|
|
#define OF_MAX_ADDR_CELLS 4
|
2012-07-25 23:34:37 +00:00
|
|
|
#define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
|
|
|
|
#define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* Debug utility */
|
|
|
|
#ifdef DEBUG
|
2010-12-01 09:54:46 +00:00
|
|
|
static void of_dump_addr(const char *s, const __be32 *addr, int na)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_debug("%s", s);
|
2010-06-08 13:48:10 +00:00
|
|
|
while (na--)
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_cont(" %08x", be32_to_cpu(*(addr++)));
|
|
|
|
pr_cont("\n");
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
#else
|
2010-12-01 09:54:46 +00:00
|
|
|
static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
|
2010-06-08 13:48:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Callbacks for bus specific translators */
|
|
|
|
struct of_bus {
|
|
|
|
const char *name;
|
|
|
|
const char *addresses;
|
|
|
|
int (*match)(struct device_node *parent);
|
|
|
|
void (*count_cells)(struct device_node *child,
|
|
|
|
int *addrc, int *sizec);
|
2012-10-09 00:41:58 +00:00
|
|
|
u64 (*map)(__be32 *addr, const __be32 *range,
|
2023-10-26 13:53:59 +00:00
|
|
|
int na, int ns, int pna, int fna);
|
2012-10-09 00:41:58 +00:00
|
|
|
int (*translate)(__be32 *addr, u64 offset, int na);
|
2023-10-26 13:53:58 +00:00
|
|
|
int flag_cells;
|
2010-12-01 09:54:46 +00:00
|
|
|
unsigned int (*get_flags)(const __be32 *addr);
|
2010-06-08 13:48:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default translator (generic bus)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void of_bus_default_count_cells(struct device_node *dev,
|
|
|
|
int *addrc, int *sizec)
|
|
|
|
{
|
|
|
|
if (addrc)
|
|
|
|
*addrc = of_n_addr_cells(dev);
|
|
|
|
if (sizec)
|
|
|
|
*sizec = of_n_size_cells(dev);
|
|
|
|
}
|
|
|
|
|
2012-10-09 00:41:58 +00:00
|
|
|
static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
|
2023-10-26 13:53:59 +00:00
|
|
|
int na, int ns, int pna, int fna)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
u64 cp, s, da;
|
|
|
|
|
2023-10-26 13:53:59 +00:00
|
|
|
cp = of_read_number(range + fna, na - fna);
|
2010-06-08 13:48:10 +00:00
|
|
|
s = of_read_number(range + na + pna, ns);
|
2023-10-26 13:53:59 +00:00
|
|
|
da = of_read_number(addr + fna, na - fna);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
2021-06-16 09:27:45 +00:00
|
|
|
pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
if (da < cp || da >= (cp + s))
|
|
|
|
return OF_BAD_ADDR;
|
|
|
|
return da - cp;
|
|
|
|
}
|
|
|
|
|
2012-10-09 00:41:58 +00:00
|
|
|
static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
u64 a = of_read_number(addr, na);
|
|
|
|
memset(addr, 0, na * 4);
|
|
|
|
a += offset;
|
|
|
|
if (na > 1)
|
2010-06-08 13:48:11 +00:00
|
|
|
addr[na - 2] = cpu_to_be32(a >> 32);
|
|
|
|
addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:15:58 +00:00
|
|
|
static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
|
|
|
|
{
|
|
|
|
return of_read_number(addr, 1);
|
|
|
|
}
|
|
|
|
|
2010-12-01 09:54:46 +00:00
|
|
|
static unsigned int of_bus_default_get_flags(const __be32 *addr)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
return IORESOURCE_MEM;
|
|
|
|
}
|
|
|
|
|
of: address: Fix address translation when address-size is greater than 2
With the recent addition of of_pci_prop_ranges() in commit 407d1a51921e
("PCI: Create device tree node for bridge"), the ranges property can
have a 3 cells child address, a 3 cells parent address and a 2 cells
child size.
A range item property for a PCI device is filled as follow:
<BAR_nbr> 0 0 <phys.hi> <phys.mid> <phys.low> <BAR_sizeh> <BAR_sizel>
<-- Child --> <-- Parent (PCI definition) --> <- BAR size (64bit) -->
This allow to translate BAR addresses from the DT. For instance:
pci@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
device_type = "pci";
compatible = "pci11ab,100", "pciclass,060400", "pciclass,0604";
ranges = <0x82000000 0x00 0xe8000000
0x82000000 0x00 0xe8000000
0x00 0x4400000>;
...
dev@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
compatible = "pci1055,9660", "pciclass,020000", "pciclass,0200";
/* Translations for BAR0 to BAR5 */
ranges = <0x00 0x00 0x00 0x82010000 0x00 0xe8000000 0x00 0x2000000
0x01 0x00 0x00 0x82010000 0x00 0xea000000 0x00 0x1000000
0x02 0x00 0x00 0x82010000 0x00 0xeb000000 0x00 0x800000
0x03 0x00 0x00 0x82010000 0x00 0xeb800000 0x00 0x800000
0x04 0x00 0x00 0x82010000 0x00 0xec000000 0x00 0x20000
0x05 0x00 0x00 0x82010000 0x00 0xec020000 0x00 0x2000>;
...
pci-ep-bus@0 {
#address-cells = <0x01>;
#size-cells = <0x01>;
compatible = "simple-bus";
/* Translate 0xe2000000 to BAR0 and 0xe0000000 to BAR1 */
ranges = <0xe2000000 0x00 0x00 0x00 0x2000000
0xe0000000 0x01 0x00 0x00 0x1000000>;
...
};
};
};
During the translation process, the "default-flags" map() function is
used to select the matching item in the ranges table and determine the
address offset from this matching item.
This map() function simply calls of_read_number() and when address-size
is greater than 2, the map() function skips the extra high address part
(ie part over 64bit). This lead to a wrong matching item and a wrong
offset computation.
Also during the translation itself, the extra high part related to the
parent address is not present in the translated address.
Fix the "default-flags" map() and translate() in order to take into
account the child extra high address part in map() and the parent extra
high address part in translate() and so having a correct address
translation for ranges patterns such as the one given in the example
above.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Link: https://lore.kernel.org/r/20231017110221.189299-2-herve.codina@bootlin.com
Signed-off-by: Rob Herring <robh@kernel.org>
2023-10-17 11:02:16 +00:00
|
|
|
static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
|
2023-10-26 13:53:59 +00:00
|
|
|
int ns, int pna, int fna)
|
of: address: Fix address translation when address-size is greater than 2
With the recent addition of of_pci_prop_ranges() in commit 407d1a51921e
("PCI: Create device tree node for bridge"), the ranges property can
have a 3 cells child address, a 3 cells parent address and a 2 cells
child size.
A range item property for a PCI device is filled as follow:
<BAR_nbr> 0 0 <phys.hi> <phys.mid> <phys.low> <BAR_sizeh> <BAR_sizel>
<-- Child --> <-- Parent (PCI definition) --> <- BAR size (64bit) -->
This allow to translate BAR addresses from the DT. For instance:
pci@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
device_type = "pci";
compatible = "pci11ab,100", "pciclass,060400", "pciclass,0604";
ranges = <0x82000000 0x00 0xe8000000
0x82000000 0x00 0xe8000000
0x00 0x4400000>;
...
dev@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
compatible = "pci1055,9660", "pciclass,020000", "pciclass,0200";
/* Translations for BAR0 to BAR5 */
ranges = <0x00 0x00 0x00 0x82010000 0x00 0xe8000000 0x00 0x2000000
0x01 0x00 0x00 0x82010000 0x00 0xea000000 0x00 0x1000000
0x02 0x00 0x00 0x82010000 0x00 0xeb000000 0x00 0x800000
0x03 0x00 0x00 0x82010000 0x00 0xeb800000 0x00 0x800000
0x04 0x00 0x00 0x82010000 0x00 0xec000000 0x00 0x20000
0x05 0x00 0x00 0x82010000 0x00 0xec020000 0x00 0x2000>;
...
pci-ep-bus@0 {
#address-cells = <0x01>;
#size-cells = <0x01>;
compatible = "simple-bus";
/* Translate 0xe2000000 to BAR0 and 0xe0000000 to BAR1 */
ranges = <0xe2000000 0x00 0x00 0x00 0x2000000
0xe0000000 0x01 0x00 0x00 0x1000000>;
...
};
};
};
During the translation process, the "default-flags" map() function is
used to select the matching item in the ranges table and determine the
address offset from this matching item.
This map() function simply calls of_read_number() and when address-size
is greater than 2, the map() function skips the extra high address part
(ie part over 64bit). This lead to a wrong matching item and a wrong
offset computation.
Also during the translation itself, the extra high part related to the
parent address is not present in the translated address.
Fix the "default-flags" map() and translate() in order to take into
account the child extra high address part in map() and the parent extra
high address part in translate() and so having a correct address
translation for ranges patterns such as the one given in the example
above.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Link: https://lore.kernel.org/r/20231017110221.189299-2-herve.codina@bootlin.com
Signed-off-by: Rob Herring <robh@kernel.org>
2023-10-17 11:02:16 +00:00
|
|
|
{
|
|
|
|
/* Check that flags match */
|
|
|
|
if (*addr != *range)
|
|
|
|
return OF_BAD_ADDR;
|
|
|
|
|
2023-10-26 13:53:59 +00:00
|
|
|
return of_bus_default_map(addr, range, na, ns, pna, fna);
|
of: address: Fix address translation when address-size is greater than 2
With the recent addition of of_pci_prop_ranges() in commit 407d1a51921e
("PCI: Create device tree node for bridge"), the ranges property can
have a 3 cells child address, a 3 cells parent address and a 2 cells
child size.
A range item property for a PCI device is filled as follow:
<BAR_nbr> 0 0 <phys.hi> <phys.mid> <phys.low> <BAR_sizeh> <BAR_sizel>
<-- Child --> <-- Parent (PCI definition) --> <- BAR size (64bit) -->
This allow to translate BAR addresses from the DT. For instance:
pci@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
device_type = "pci";
compatible = "pci11ab,100", "pciclass,060400", "pciclass,0604";
ranges = <0x82000000 0x00 0xe8000000
0x82000000 0x00 0xe8000000
0x00 0x4400000>;
...
dev@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
compatible = "pci1055,9660", "pciclass,020000", "pciclass,0200";
/* Translations for BAR0 to BAR5 */
ranges = <0x00 0x00 0x00 0x82010000 0x00 0xe8000000 0x00 0x2000000
0x01 0x00 0x00 0x82010000 0x00 0xea000000 0x00 0x1000000
0x02 0x00 0x00 0x82010000 0x00 0xeb000000 0x00 0x800000
0x03 0x00 0x00 0x82010000 0x00 0xeb800000 0x00 0x800000
0x04 0x00 0x00 0x82010000 0x00 0xec000000 0x00 0x20000
0x05 0x00 0x00 0x82010000 0x00 0xec020000 0x00 0x2000>;
...
pci-ep-bus@0 {
#address-cells = <0x01>;
#size-cells = <0x01>;
compatible = "simple-bus";
/* Translate 0xe2000000 to BAR0 and 0xe0000000 to BAR1 */
ranges = <0xe2000000 0x00 0x00 0x00 0x2000000
0xe0000000 0x01 0x00 0x00 0x1000000>;
...
};
};
};
During the translation process, the "default-flags" map() function is
used to select the matching item in the ranges table and determine the
address offset from this matching item.
This map() function simply calls of_read_number() and when address-size
is greater than 2, the map() function skips the extra high address part
(ie part over 64bit). This lead to a wrong matching item and a wrong
offset computation.
Also during the translation itself, the extra high part related to the
parent address is not present in the translated address.
Fix the "default-flags" map() and translate() in order to take into
account the child extra high address part in map() and the parent extra
high address part in translate() and so having a correct address
translation for ranges patterns such as the one given in the example
above.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Link: https://lore.kernel.org/r/20231017110221.189299-2-herve.codina@bootlin.com
Signed-off-by: Rob Herring <robh@kernel.org>
2023-10-17 11:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
|
|
|
|
{
|
|
|
|
/* Keep "flags" part (high cell) in translated address */
|
|
|
|
return of_bus_default_translate(addr + 1, offset, na - 1);
|
|
|
|
}
|
2023-03-28 20:15:58 +00:00
|
|
|
|
2020-07-30 00:56:14 +00:00
|
|
|
#ifdef CONFIG_PCI
|
2020-02-06 14:34:54 +00:00
|
|
|
static unsigned int of_bus_pci_get_flags(const __be32 *addr)
|
|
|
|
{
|
|
|
|
unsigned int flags = 0;
|
|
|
|
u32 w = be32_to_cpup(addr);
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_PCI))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch((w >> 24) & 0x03) {
|
|
|
|
case 0x01:
|
|
|
|
flags |= IORESOURCE_IO;
|
|
|
|
break;
|
|
|
|
case 0x02: /* 32 bits */
|
|
|
|
flags |= IORESOURCE_MEM;
|
|
|
|
break;
|
2021-04-15 18:00:51 +00:00
|
|
|
|
|
|
|
case 0x03: /* 64 bits */
|
|
|
|
flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
|
|
|
|
break;
|
2020-02-06 14:34:54 +00:00
|
|
|
}
|
|
|
|
if (w & 0x40000000)
|
|
|
|
flags |= IORESOURCE_PREFETCH;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/*
|
|
|
|
* PCI bus specific translator
|
|
|
|
*/
|
|
|
|
|
2024-10-10 16:27:16 +00:00
|
|
|
static bool of_node_is_pcie(const struct device_node *np)
|
2020-08-19 09:42:55 +00:00
|
|
|
{
|
|
|
|
bool is_pcie = of_node_name_eq(np, "pcie");
|
|
|
|
|
|
|
|
if (is_pcie)
|
|
|
|
pr_warn_once("%pOF: Missing device_type\n", np);
|
|
|
|
|
|
|
|
return is_pcie;
|
|
|
|
}
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
static int of_bus_pci_match(struct device_node *np)
|
|
|
|
{
|
2013-07-03 06:01:10 +00:00
|
|
|
/*
|
2014-02-03 15:31:03 +00:00
|
|
|
* "pciex" is PCI Express
|
2013-07-03 06:01:10 +00:00
|
|
|
* "vci" is for the /chaos bridge on 1st-gen PCI powermacs
|
|
|
|
* "ht" is hypertransport
|
2020-08-19 09:42:55 +00:00
|
|
|
*
|
|
|
|
* If none of the device_type match, and that the node name is
|
|
|
|
* "pcie", accept the device as PCI (with a warning).
|
2013-07-03 06:01:10 +00:00
|
|
|
*/
|
2018-08-29 13:36:12 +00:00
|
|
|
return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
|
2020-08-19 09:42:55 +00:00
|
|
|
of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
|
|
|
|
of_node_is_pcie(np);
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void of_bus_pci_count_cells(struct device_node *np,
|
|
|
|
int *addrc, int *sizec)
|
|
|
|
{
|
|
|
|
if (addrc)
|
|
|
|
*addrc = 3;
|
|
|
|
if (sizec)
|
|
|
|
*sizec = 2;
|
|
|
|
}
|
|
|
|
|
2012-10-09 00:41:58 +00:00
|
|
|
static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
|
2023-10-26 13:53:59 +00:00
|
|
|
int pna, int fna)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
unsigned int af, rf;
|
|
|
|
|
|
|
|
af = of_bus_pci_get_flags(addr);
|
|
|
|
rf = of_bus_pci_get_flags(range);
|
|
|
|
|
|
|
|
/* Check address type match */
|
|
|
|
if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
|
|
|
|
return OF_BAD_ADDR;
|
|
|
|
|
2023-10-26 13:53:59 +00:00
|
|
|
return of_bus_default_map(addr, range, na, ns, pna, fna);
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 19:45:46 +00:00
|
|
|
#endif /* CONFIG_PCI */
|
2010-06-08 13:48:10 +00:00
|
|
|
|
2024-09-06 12:25:19 +00:00
|
|
|
static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
|
|
|
|
{
|
|
|
|
u64 end = start;
|
|
|
|
|
|
|
|
if (overflows_type(start, r->start))
|
|
|
|
return -EOVERFLOW;
|
|
|
|
if (size && check_add_overflow(end, size - 1, &end))
|
|
|
|
return -EOVERFLOW;
|
|
|
|
if (overflows_type(end, r->end))
|
|
|
|
return -EOVERFLOW;
|
|
|
|
|
|
|
|
r->start = start;
|
|
|
|
r->end = end;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:29:25 +00:00
|
|
|
/*
|
|
|
|
* of_pci_range_to_resource - Create a resource from an of_pci_range
|
|
|
|
* @range: the PCI range that describes the resource
|
|
|
|
* @np: device node where the range belongs to
|
|
|
|
* @res: pointer to a valid resource that will be updated to
|
|
|
|
* reflect the values contained in the range.
|
|
|
|
*
|
2023-03-31 07:52:29 +00:00
|
|
|
* Returns -EINVAL if the range cannot be converted to resource.
|
2014-09-29 14:29:25 +00:00
|
|
|
*
|
|
|
|
* Note that if the range is an IO range, the resource will be converted
|
|
|
|
* using pci_address_to_pio() which can fail if it is called too early or
|
|
|
|
* if the range cannot be matched to any host bridge IO space (our case here).
|
|
|
|
* To guard against that we try to register the IO range first.
|
|
|
|
* If that fails we know that pci_address_to_pio() will do too.
|
|
|
|
*/
|
2024-10-10 16:27:16 +00:00
|
|
|
int of_pci_range_to_resource(const struct of_pci_range *range,
|
|
|
|
const struct device_node *np, struct resource *res)
|
2014-09-29 14:29:24 +00:00
|
|
|
{
|
2024-09-06 12:25:19 +00:00
|
|
|
u64 start;
|
2014-09-29 14:29:25 +00:00
|
|
|
int err;
|
2014-09-29 14:29:24 +00:00
|
|
|
res->flags = range->flags;
|
|
|
|
res->parent = res->child = res->sibling = NULL;
|
|
|
|
res->name = np->full_name;
|
2014-09-29 14:29:25 +00:00
|
|
|
|
|
|
|
if (res->flags & IORESOURCE_IO) {
|
|
|
|
unsigned long port;
|
2018-03-14 18:15:52 +00:00
|
|
|
err = pci_register_io_range(&np->fwnode, range->cpu_addr,
|
|
|
|
range->size);
|
2014-09-29 14:29:25 +00:00
|
|
|
if (err)
|
|
|
|
goto invalid_range;
|
|
|
|
port = pci_address_to_pio(range->cpu_addr);
|
|
|
|
if (port == (unsigned long)-1) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto invalid_range;
|
|
|
|
}
|
2024-09-06 12:25:19 +00:00
|
|
|
start = port;
|
2014-09-29 14:29:25 +00:00
|
|
|
} else {
|
2024-09-06 12:25:19 +00:00
|
|
|
start = range->cpu_addr;
|
2014-09-29 14:29:25 +00:00
|
|
|
}
|
2024-09-06 12:25:19 +00:00
|
|
|
return __of_address_resource_bounds(res, start, range->size);
|
2014-09-29 14:29:25 +00:00
|
|
|
|
|
|
|
invalid_range:
|
|
|
|
res->start = (resource_size_t)OF_BAD_ADDR;
|
|
|
|
res->end = (resource_size_t)OF_BAD_ADDR;
|
|
|
|
return err;
|
2014-09-29 14:29:24 +00:00
|
|
|
}
|
2018-01-11 06:08:02 +00:00
|
|
|
EXPORT_SYMBOL(of_pci_range_to_resource);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
2023-03-28 20:15:57 +00:00
|
|
|
/*
|
|
|
|
* of_range_to_resource - Create a resource from a ranges entry
|
|
|
|
* @np: device node where the range belongs to
|
|
|
|
* @index: the 'ranges' index to convert to a resource
|
|
|
|
* @res: pointer to a valid resource that will be updated to
|
|
|
|
* reflect the values contained in the range.
|
|
|
|
*
|
2024-09-06 12:25:19 +00:00
|
|
|
* Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
|
|
|
|
* cannot be converted to resource.
|
2023-03-28 20:15:57 +00:00
|
|
|
*/
|
|
|
|
int of_range_to_resource(struct device_node *np, int index, struct resource *res)
|
|
|
|
{
|
|
|
|
int ret, i = 0;
|
|
|
|
struct of_range_parser parser;
|
|
|
|
struct of_range range;
|
|
|
|
|
|
|
|
ret = of_range_parser_init(&parser, np);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for_each_of_range(&parser, &range)
|
|
|
|
if (i++ == index)
|
|
|
|
return of_pci_range_to_resource(&range, np, res);
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_range_to_resource);
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/*
|
|
|
|
* ISA bus specific translator
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int of_bus_isa_match(struct device_node *np)
|
|
|
|
{
|
2018-08-27 13:37:06 +00:00
|
|
|
return of_node_name_eq(np, "isa");
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void of_bus_isa_count_cells(struct device_node *child,
|
|
|
|
int *addrc, int *sizec)
|
|
|
|
{
|
|
|
|
if (addrc)
|
|
|
|
*addrc = 2;
|
|
|
|
if (sizec)
|
|
|
|
*sizec = 1;
|
|
|
|
}
|
|
|
|
|
2012-10-09 00:41:58 +00:00
|
|
|
static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
|
2023-10-26 13:53:59 +00:00
|
|
|
int pna, int fna)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
/* Check address type match */
|
2010-12-01 09:54:46 +00:00
|
|
|
if ((addr[0] ^ range[0]) & cpu_to_be32(1))
|
2010-06-08 13:48:10 +00:00
|
|
|
return OF_BAD_ADDR;
|
|
|
|
|
2023-10-26 13:53:59 +00:00
|
|
|
return of_bus_default_map(addr, range, na, ns, pna, fna);
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 09:54:46 +00:00
|
|
|
static unsigned int of_bus_isa_get_flags(const __be32 *addr)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
unsigned int flags = 0;
|
2010-12-01 09:54:46 +00:00
|
|
|
u32 w = be32_to_cpup(addr);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
if (w & 1)
|
|
|
|
flags |= IORESOURCE_IO;
|
|
|
|
else
|
|
|
|
flags |= IORESOURCE_MEM;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:15:58 +00:00
|
|
|
static int of_bus_default_flags_match(struct device_node *np)
|
|
|
|
{
|
2024-11-08 19:35:48 +00:00
|
|
|
/*
|
|
|
|
* Check for presence first since of_bus_n_addr_cells() will warn when
|
|
|
|
* walking parent nodes.
|
|
|
|
*/
|
|
|
|
return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
|
2023-03-28 20:15:58 +00:00
|
|
|
}
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/*
|
|
|
|
* Array of bus specific translators
|
|
|
|
*/
|
|
|
|
|
2024-10-10 16:27:20 +00:00
|
|
|
static const struct of_bus of_busses[] = {
|
2018-01-17 23:36:39 +00:00
|
|
|
#ifdef CONFIG_PCI
|
2010-06-08 13:48:10 +00:00
|
|
|
/* PCI */
|
|
|
|
{
|
|
|
|
.name = "pci",
|
|
|
|
.addresses = "assigned-addresses",
|
|
|
|
.match = of_bus_pci_match,
|
|
|
|
.count_cells = of_bus_pci_count_cells,
|
|
|
|
.map = of_bus_pci_map,
|
2023-10-17 11:02:17 +00:00
|
|
|
.translate = of_bus_default_flags_translate,
|
2023-10-26 13:53:58 +00:00
|
|
|
.flag_cells = 1,
|
2010-06-08 13:48:10 +00:00
|
|
|
.get_flags = of_bus_pci_get_flags,
|
|
|
|
},
|
2018-01-17 23:36:39 +00:00
|
|
|
#endif /* CONFIG_PCI */
|
2010-06-08 13:48:10 +00:00
|
|
|
/* ISA */
|
|
|
|
{
|
|
|
|
.name = "isa",
|
|
|
|
.addresses = "reg",
|
|
|
|
.match = of_bus_isa_match,
|
|
|
|
.count_cells = of_bus_isa_count_cells,
|
|
|
|
.map = of_bus_isa_map,
|
2023-10-17 11:02:17 +00:00
|
|
|
.translate = of_bus_default_flags_translate,
|
2023-10-26 13:53:58 +00:00
|
|
|
.flag_cells = 1,
|
2010-06-08 13:48:10 +00:00
|
|
|
.get_flags = of_bus_isa_get_flags,
|
|
|
|
},
|
2023-03-28 20:15:58 +00:00
|
|
|
/* Default with flags cell */
|
|
|
|
{
|
|
|
|
.name = "default-flags",
|
|
|
|
.addresses = "reg",
|
|
|
|
.match = of_bus_default_flags_match,
|
|
|
|
.count_cells = of_bus_default_count_cells,
|
of: address: Fix address translation when address-size is greater than 2
With the recent addition of of_pci_prop_ranges() in commit 407d1a51921e
("PCI: Create device tree node for bridge"), the ranges property can
have a 3 cells child address, a 3 cells parent address and a 2 cells
child size.
A range item property for a PCI device is filled as follow:
<BAR_nbr> 0 0 <phys.hi> <phys.mid> <phys.low> <BAR_sizeh> <BAR_sizel>
<-- Child --> <-- Parent (PCI definition) --> <- BAR size (64bit) -->
This allow to translate BAR addresses from the DT. For instance:
pci@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
device_type = "pci";
compatible = "pci11ab,100", "pciclass,060400", "pciclass,0604";
ranges = <0x82000000 0x00 0xe8000000
0x82000000 0x00 0xe8000000
0x00 0x4400000>;
...
dev@0,0 {
#address-cells = <0x03>;
#size-cells = <0x02>;
compatible = "pci1055,9660", "pciclass,020000", "pciclass,0200";
/* Translations for BAR0 to BAR5 */
ranges = <0x00 0x00 0x00 0x82010000 0x00 0xe8000000 0x00 0x2000000
0x01 0x00 0x00 0x82010000 0x00 0xea000000 0x00 0x1000000
0x02 0x00 0x00 0x82010000 0x00 0xeb000000 0x00 0x800000
0x03 0x00 0x00 0x82010000 0x00 0xeb800000 0x00 0x800000
0x04 0x00 0x00 0x82010000 0x00 0xec000000 0x00 0x20000
0x05 0x00 0x00 0x82010000 0x00 0xec020000 0x00 0x2000>;
...
pci-ep-bus@0 {
#address-cells = <0x01>;
#size-cells = <0x01>;
compatible = "simple-bus";
/* Translate 0xe2000000 to BAR0 and 0xe0000000 to BAR1 */
ranges = <0xe2000000 0x00 0x00 0x00 0x2000000
0xe0000000 0x01 0x00 0x00 0x1000000>;
...
};
};
};
During the translation process, the "default-flags" map() function is
used to select the matching item in the ranges table and determine the
address offset from this matching item.
This map() function simply calls of_read_number() and when address-size
is greater than 2, the map() function skips the extra high address part
(ie part over 64bit). This lead to a wrong matching item and a wrong
offset computation.
Also during the translation itself, the extra high part related to the
parent address is not present in the translated address.
Fix the "default-flags" map() and translate() in order to take into
account the child extra high address part in map() and the parent extra
high address part in translate() and so having a correct address
translation for ranges patterns such as the one given in the example
above.
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Link: https://lore.kernel.org/r/20231017110221.189299-2-herve.codina@bootlin.com
Signed-off-by: Rob Herring <robh@kernel.org>
2023-10-17 11:02:16 +00:00
|
|
|
.map = of_bus_default_flags_map,
|
|
|
|
.translate = of_bus_default_flags_translate,
|
2023-10-26 13:53:58 +00:00
|
|
|
.flag_cells = 1,
|
2023-03-28 20:15:58 +00:00
|
|
|
.get_flags = of_bus_default_flags_get_flags,
|
|
|
|
},
|
2010-06-08 13:48:10 +00:00
|
|
|
/* Default */
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.addresses = "reg",
|
|
|
|
.match = NULL,
|
|
|
|
.count_cells = of_bus_default_count_cells,
|
|
|
|
.map = of_bus_default_map,
|
|
|
|
.translate = of_bus_default_translate,
|
|
|
|
.get_flags = of_bus_default_get_flags,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-10-10 16:27:20 +00:00
|
|
|
static const struct of_bus *of_match_bus(struct device_node *np)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(of_busses); i++)
|
|
|
|
if (!of_busses[i].match || of_busses[i].match(np))
|
|
|
|
return &of_busses[i];
|
|
|
|
BUG();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-10-10 16:27:16 +00:00
|
|
|
static int of_empty_ranges_quirk(const struct device_node *np)
|
2014-11-14 06:55:03 +00:00
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_PPC)) {
|
2015-03-23 03:16:38 +00:00
|
|
|
/* To save cycles, we cache the result for global "Mac" setting */
|
2014-11-14 06:55:03 +00:00
|
|
|
static int quirk_state = -1;
|
|
|
|
|
2015-03-23 03:16:38 +00:00
|
|
|
/* PA-SEMI sdc DT bug */
|
|
|
|
if (of_device_is_compatible(np, "1682m-sdc"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Make quirk cached */
|
2014-11-14 06:55:03 +00:00
|
|
|
if (quirk_state < 0)
|
|
|
|
quirk_state =
|
|
|
|
of_machine_is_compatible("Power Macintosh") ||
|
|
|
|
of_machine_is_compatible("MacRISC");
|
|
|
|
return quirk_state;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-10 16:27:20 +00:00
|
|
|
static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,
|
|
|
|
const struct of_bus *pbus, __be32 *addr,
|
2010-06-08 13:48:10 +00:00
|
|
|
int na, int ns, int pna, const char *rprop)
|
|
|
|
{
|
2010-12-01 09:54:46 +00:00
|
|
|
const __be32 *ranges;
|
2010-06-08 13:48:10 +00:00
|
|
|
unsigned int rlen;
|
|
|
|
int rone;
|
|
|
|
u64 offset = OF_BAD_ADDR;
|
|
|
|
|
2015-11-30 06:22:37 +00:00
|
|
|
/*
|
|
|
|
* Normally, an absence of a "ranges" property means we are
|
2010-06-08 13:48:10 +00:00
|
|
|
* crossing a non-translatable boundary, and thus the addresses
|
2015-11-30 06:22:37 +00:00
|
|
|
* below the current cannot be converted to CPU physical ones.
|
2010-06-08 13:48:10 +00:00
|
|
|
* Unfortunately, while this is very clear in the spec, it's not
|
|
|
|
* what Apple understood, and they do have things like /uni-n or
|
|
|
|
* /ht nodes with no "ranges" property and a lot of perfectly
|
|
|
|
* useable mapped devices below them. Thus we treat the absence of
|
|
|
|
* "ranges" as equivalent to an empty "ranges" property which means
|
|
|
|
* a 1:1 translation at that level. It's up to the caller not to try
|
|
|
|
* to translate addresses that aren't supposed to be translated in
|
|
|
|
* the first place. --BenH.
|
2010-06-08 13:48:11 +00:00
|
|
|
*
|
|
|
|
* As far as we know, this damage only exists on Apple machines, so
|
|
|
|
* This code is only enabled on powerpc. --gcl
|
2019-09-04 10:43:30 +00:00
|
|
|
*
|
|
|
|
* This quirk also applies for 'dma-ranges' which frequently exist in
|
|
|
|
* child nodes without 'dma-ranges' in the parent nodes. --RobH
|
2010-06-08 13:48:10 +00:00
|
|
|
*/
|
|
|
|
ranges = of_get_property(parent, rprop, &rlen);
|
2019-09-04 10:43:30 +00:00
|
|
|
if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
|
|
|
|
strcmp(rprop, "dma-ranges")) {
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_debug("no ranges; cannot translate\n");
|
2010-06-08 13:48:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-06-08 13:48:10 +00:00
|
|
|
if (ranges == NULL || rlen == 0) {
|
|
|
|
offset = of_read_number(addr, na);
|
2024-11-24 10:05:37 +00:00
|
|
|
/* set address to zero, pass flags through */
|
|
|
|
memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_debug("empty ranges; 1:1 translation\n");
|
2010-06-08 13:48:10 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_debug("walking ranges...\n");
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* Now walk through the ranges */
|
|
|
|
rlen /= 4;
|
|
|
|
rone = na + pna + ns;
|
|
|
|
for (; rlen >= rone; rlen -= rone, ranges += rone) {
|
2023-10-26 13:53:59 +00:00
|
|
|
offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
|
2010-06-08 13:48:10 +00:00
|
|
|
if (offset != OF_BAD_ADDR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (offset == OF_BAD_ADDR) {
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_debug("not found !\n");
|
2010-06-08 13:48:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
memcpy(addr, ranges + na, 4 * pna);
|
|
|
|
|
|
|
|
finish:
|
2016-06-15 13:32:18 +00:00
|
|
|
of_dump_addr("parent translation for:", addr, pna);
|
2021-06-16 09:27:45 +00:00
|
|
|
pr_debug("with offset: %llx\n", offset);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* Translate it into parent bus space */
|
|
|
|
return pbus->translate(addr, offset, pna);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate an address from the device-tree into a CPU physical address,
|
|
|
|
* this walks up the tree and applies the various bus mappings on the
|
|
|
|
* way.
|
|
|
|
*
|
|
|
|
* Note: We consider that crossing any level with #size-cells == 0 to mean
|
|
|
|
* that translation is impossible (that is we are not dealing with a value
|
|
|
|
* that can be mapped to a cpu physical address). This is not really specified
|
|
|
|
* that way, but this is traditionally the way IBM at least do things
|
2018-03-14 18:15:54 +00:00
|
|
|
*
|
|
|
|
* Whenever the translation fails, the *host pointer will be set to the
|
|
|
|
* device that had registered logical PIO mapping, and the return code is
|
|
|
|
* relative to that node.
|
2010-06-08 13:48:10 +00:00
|
|
|
*/
|
2024-04-09 18:59:41 +00:00
|
|
|
static u64 __of_translate_address(struct device_node *node,
|
2019-04-01 08:56:43 +00:00
|
|
|
struct device_node *(*get_parent)(const struct device_node *),
|
2018-03-14 18:15:54 +00:00
|
|
|
const __be32 *in_addr, const char *rprop,
|
|
|
|
struct device_node **host)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
2024-04-09 18:59:41 +00:00
|
|
|
struct device_node *dev __free(device_node) = of_node_get(node);
|
|
|
|
struct device_node *parent __free(device_node) = get_parent(dev);
|
2024-10-10 16:27:20 +00:00
|
|
|
const struct of_bus *bus, *pbus;
|
2012-10-09 00:41:58 +00:00
|
|
|
__be32 addr[OF_MAX_ADDR_CELLS];
|
2010-06-08 13:48:10 +00:00
|
|
|
int na, ns, pna, pns;
|
|
|
|
|
2017-06-01 20:50:55 +00:00
|
|
|
pr_debug("** translation for device %pOF **\n", dev);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
2018-03-14 18:15:54 +00:00
|
|
|
*host = NULL;
|
2024-04-09 18:59:41 +00:00
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
if (parent == NULL)
|
2024-04-09 18:59:41 +00:00
|
|
|
return OF_BAD_ADDR;
|
2010-06-08 13:48:10 +00:00
|
|
|
bus = of_match_bus(parent);
|
|
|
|
|
2012-12-13 10:11:23 +00:00
|
|
|
/* Count address cells & copy address locally */
|
2010-06-08 13:48:10 +00:00
|
|
|
bus->count_cells(dev, &na, &ns);
|
|
|
|
if (!OF_CHECK_COUNTS(na, ns)) {
|
2017-06-01 20:50:55 +00:00
|
|
|
pr_debug("Bad cell count for %pOF\n", dev);
|
2024-04-09 18:59:41 +00:00
|
|
|
return OF_BAD_ADDR;
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
memcpy(addr, in_addr, na * 4);
|
|
|
|
|
2017-06-01 20:50:55 +00:00
|
|
|
pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
|
|
|
|
bus->name, na, ns, parent);
|
2016-06-15 13:32:18 +00:00
|
|
|
of_dump_addr("translating address:", addr, na);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* Translate */
|
|
|
|
for (;;) {
|
2018-03-14 18:15:54 +00:00
|
|
|
struct logic_pio_hwaddr *iorange;
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/* Switch to parent bus */
|
|
|
|
of_node_put(dev);
|
|
|
|
dev = parent;
|
2019-04-01 08:56:43 +00:00
|
|
|
parent = get_parent(dev);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* If root, we have finished */
|
|
|
|
if (parent == NULL) {
|
2016-06-15 13:32:18 +00:00
|
|
|
pr_debug("reached root node\n");
|
2024-04-09 18:59:41 +00:00
|
|
|
return of_read_number(addr, na);
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:15:54 +00:00
|
|
|
/*
|
|
|
|
* For indirectIO device which has no ranges property, get
|
|
|
|
* the address from reg directly.
|
|
|
|
*/
|
|
|
|
iorange = find_io_range_by_fwnode(&dev->fwnode);
|
|
|
|
if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
|
2024-04-09 18:59:41 +00:00
|
|
|
u64 result = of_read_number(addr + 1, na - 1);
|
2018-03-14 18:15:54 +00:00
|
|
|
pr_debug("indirectIO matched(%pOF) 0x%llx\n",
|
|
|
|
dev, result);
|
2024-04-09 18:59:41 +00:00
|
|
|
*host = no_free_ptr(dev);
|
|
|
|
return result;
|
2018-03-14 18:15:54 +00:00
|
|
|
}
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/* Get new parent bus and counts */
|
|
|
|
pbus = of_match_bus(parent);
|
|
|
|
pbus->count_cells(dev, &pna, &pns);
|
|
|
|
if (!OF_CHECK_COUNTS(pna, pns)) {
|
2017-06-01 20:50:55 +00:00
|
|
|
pr_err("Bad cell count for %pOF\n", dev);
|
2024-04-09 18:59:41 +00:00
|
|
|
return OF_BAD_ADDR;
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 20:50:55 +00:00
|
|
|
pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
|
|
|
|
pbus->name, pna, pns, parent);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* Apply bus translation */
|
|
|
|
if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
|
2024-04-09 18:59:41 +00:00
|
|
|
return OF_BAD_ADDR;
|
2010-06-08 13:48:10 +00:00
|
|
|
|
|
|
|
/* Complete the move up one level */
|
|
|
|
na = pna;
|
|
|
|
ns = pns;
|
|
|
|
bus = pbus;
|
|
|
|
|
2016-06-15 13:32:18 +00:00
|
|
|
of_dump_addr("one level translation:", addr, na);
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 18:59:41 +00:00
|
|
|
unreachable();
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 09:54:46 +00:00
|
|
|
u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
2018-03-14 18:15:54 +00:00
|
|
|
struct device_node *host;
|
|
|
|
u64 ret;
|
|
|
|
|
2019-04-01 08:56:43 +00:00
|
|
|
ret = __of_translate_address(dev, of_get_parent,
|
|
|
|
in_addr, "ranges", &host);
|
2018-03-14 18:15:54 +00:00
|
|
|
if (host) {
|
|
|
|
of_node_put(host);
|
|
|
|
return OF_BAD_ADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_translate_address);
|
|
|
|
|
2022-09-29 12:48:38 +00:00
|
|
|
#ifdef CONFIG_HAS_DMA
|
|
|
|
struct device_node *__of_get_dma_parent(const struct device_node *np)
|
2019-04-01 08:56:44 +00:00
|
|
|
{
|
|
|
|
struct of_phandle_args args;
|
|
|
|
int ret, index;
|
|
|
|
|
|
|
|
index = of_property_match_string(np, "interconnect-names", "dma-mem");
|
|
|
|
if (index < 0)
|
|
|
|
return of_get_parent(np);
|
|
|
|
|
|
|
|
ret = of_parse_phandle_with_args(np, "interconnects",
|
|
|
|
"#interconnect-cells",
|
|
|
|
index, &args);
|
|
|
|
if (ret < 0)
|
|
|
|
return of_get_parent(np);
|
|
|
|
|
2024-12-06 00:52:30 +00:00
|
|
|
return args.np;
|
2019-04-01 08:56:44 +00:00
|
|
|
}
|
2022-09-29 12:48:38 +00:00
|
|
|
#endif
|
2019-04-01 08:56:44 +00:00
|
|
|
|
2019-07-03 17:23:01 +00:00
|
|
|
static struct device_node *of_get_next_dma_parent(struct device_node *np)
|
|
|
|
{
|
|
|
|
struct device_node *parent;
|
|
|
|
|
|
|
|
parent = __of_get_dma_parent(np);
|
|
|
|
of_node_put(np);
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2010-12-01 09:54:46 +00:00
|
|
|
u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
2018-03-14 18:15:54 +00:00
|
|
|
struct device_node *host;
|
|
|
|
u64 ret;
|
|
|
|
|
2019-04-01 08:56:44 +00:00
|
|
|
ret = __of_translate_address(dev, __of_get_dma_parent,
|
2019-04-01 08:56:43 +00:00
|
|
|
in_addr, "dma-ranges", &host);
|
2018-03-14 18:15:54 +00:00
|
|
|
|
|
|
|
if (host) {
|
|
|
|
of_node_put(host);
|
|
|
|
return OF_BAD_ADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-06-08 13:48:10 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_translate_dma_address);
|
|
|
|
|
2023-01-20 17:42:48 +00:00
|
|
|
/**
|
|
|
|
* of_translate_dma_region - Translate device tree address and size tuple
|
|
|
|
* @dev: device tree node for which to translate
|
|
|
|
* @prop: pointer into array of cells
|
|
|
|
* @start: return value for the start of the DMA range
|
|
|
|
* @length: return value for the length of the DMA range
|
|
|
|
*
|
|
|
|
* Returns a pointer to the cell immediately following the translated DMA region.
|
|
|
|
*/
|
|
|
|
const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
|
|
|
|
phys_addr_t *start, size_t *length)
|
|
|
|
{
|
2024-04-09 18:59:41 +00:00
|
|
|
struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
|
2023-01-20 17:42:48 +00:00
|
|
|
u64 address, size;
|
|
|
|
int na, ns;
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
na = of_bus_n_addr_cells(parent);
|
|
|
|
ns = of_bus_n_size_cells(parent);
|
|
|
|
|
|
|
|
address = of_translate_dma_address(dev, prop);
|
|
|
|
if (address == OF_BAD_ADDR)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
size = of_read_number(prop + na, ns);
|
|
|
|
|
|
|
|
if (start)
|
|
|
|
*start = address;
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
*length = size;
|
|
|
|
|
|
|
|
return prop + na + ns;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_translate_dma_region);
|
|
|
|
|
2021-05-27 19:45:45 +00:00
|
|
|
const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
|
|
|
|
u64 *size, unsigned int *flags)
|
2010-06-08 13:48:10 +00:00
|
|
|
{
|
2010-12-01 09:54:46 +00:00
|
|
|
const __be32 *prop;
|
2010-06-08 13:48:10 +00:00
|
|
|
unsigned int psize;
|
2024-04-09 18:59:41 +00:00
|
|
|
struct device_node *parent __free(device_node) = of_get_parent(dev);
|
2024-10-10 16:27:20 +00:00
|
|
|
const struct of_bus *bus;
|
2010-06-08 13:48:10 +00:00
|
|
|
int onesize, i, na, ns;
|
|
|
|
|
|
|
|
if (parent == NULL)
|
|
|
|
return NULL;
|
2024-04-09 18:59:41 +00:00
|
|
|
|
|
|
|
/* match the parent's bus type */
|
2010-06-08 13:48:10 +00:00
|
|
|
bus = of_match_bus(parent);
|
2024-04-09 18:59:41 +00:00
|
|
|
if (strcmp(bus->name, "pci") && (bar_no >= 0))
|
2021-05-27 19:45:45 +00:00
|
|
|
return NULL;
|
2024-04-09 18:59:41 +00:00
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
/* Get "reg" or "assigned-addresses" property */
|
|
|
|
prop = of_get_property(dev, bus->addresses, &psize);
|
|
|
|
if (prop == NULL)
|
|
|
|
return NULL;
|
|
|
|
psize /= 4;
|
|
|
|
|
2024-11-08 19:35:48 +00:00
|
|
|
bus->count_cells(dev, &na, &ns);
|
|
|
|
if (!OF_CHECK_ADDR_COUNT(na))
|
|
|
|
return NULL;
|
|
|
|
|
2010-06-08 13:48:10 +00:00
|
|
|
onesize = na + ns;
|
2021-05-27 19:45:45 +00:00
|
|
|
for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
|
|
|
|
u32 val = be32_to_cpu(prop[0]);
|
|
|
|
/* PCI bus matches on BAR number instead of index */
|
|
|
|
if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
|
|
|
|
((index >= 0) && (i == index))) {
|
2010-06-08 13:48:10 +00:00
|
|
|
if (size)
|
|
|
|
*size = of_read_number(prop + na, ns);
|
|
|
|
if (flags)
|
|
|
|
*flags = bus->get_flags(prop);
|
|
|
|
return prop;
|
|
|
|
}
|
2021-05-27 19:45:45 +00:00
|
|
|
}
|
2010-06-08 13:48:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-05-27 19:45:45 +00:00
|
|
|
EXPORT_SYMBOL(__of_get_address);
|
2010-06-08 13:48:10 +00:00
|
|
|
|
2023-03-28 20:16:00 +00:00
|
|
|
/**
|
|
|
|
* of_property_read_reg - Retrieve the specified "reg" entry index without translating
|
|
|
|
* @np: device tree node for which to retrieve "reg" from
|
|
|
|
* @idx: "reg" entry index to read
|
|
|
|
* @addr: return value for the untranslated address
|
|
|
|
* @size: return value for the entry size
|
|
|
|
*
|
|
|
|
* Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
|
|
|
|
* size values filled in.
|
|
|
|
*/
|
|
|
|
int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
|
|
|
|
{
|
|
|
|
const __be32 *prop = of_get_address(np, idx, size, NULL);
|
|
|
|
|
|
|
|
if (!prop)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*addr = of_read_number(prop, of_n_addr_cells(np));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_property_read_reg);
|
|
|
|
|
2020-02-06 14:34:54 +00:00
|
|
|
static int parser_init(struct of_pci_range_parser *parser,
|
|
|
|
struct device_node *node, const char *name)
|
|
|
|
{
|
|
|
|
int rlen;
|
|
|
|
|
|
|
|
parser->node = node;
|
|
|
|
parser->pna = of_n_addr_cells(node);
|
2020-02-06 14:01:05 +00:00
|
|
|
parser->na = of_bus_n_addr_cells(node);
|
|
|
|
parser->ns = of_bus_n_size_cells(node);
|
2020-02-06 14:34:54 +00:00
|
|
|
parser->dma = !strcmp(name, "dma-ranges");
|
2020-07-28 15:36:55 +00:00
|
|
|
parser->bus = of_match_bus(node);
|
2020-02-06 14:34:54 +00:00
|
|
|
|
|
|
|
parser->range = of_get_property(node, name, &rlen);
|
|
|
|
if (parser->range == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
parser->end = parser->range + rlen / sizeof(__be32);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int of_pci_range_parser_init(struct of_pci_range_parser *parser,
|
|
|
|
struct device_node *node)
|
|
|
|
{
|
|
|
|
return parser_init(parser, node, "ranges");
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
|
|
|
|
|
|
|
|
int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
|
|
|
|
struct device_node *node)
|
|
|
|
{
|
|
|
|
return parser_init(parser, node, "dma-ranges");
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
|
2020-02-06 14:01:05 +00:00
|
|
|
#define of_dma_range_parser_init of_pci_dma_range_parser_init
|
2020-02-06 14:34:54 +00:00
|
|
|
|
|
|
|
struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
|
|
|
|
struct of_pci_range *range)
|
|
|
|
{
|
2020-02-06 14:01:05 +00:00
|
|
|
int na = parser->na;
|
|
|
|
int ns = parser->ns;
|
|
|
|
int np = parser->pna + na + ns;
|
2023-10-26 13:53:58 +00:00
|
|
|
int busflag_na = parser->bus->flag_cells;
|
2020-02-06 14:34:54 +00:00
|
|
|
|
|
|
|
if (!range)
|
|
|
|
return NULL;
|
|
|
|
|
2020-02-06 14:01:05 +00:00
|
|
|
if (!parser->range || parser->range + np > parser->end)
|
2020-02-06 14:34:54 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-07-28 15:36:55 +00:00
|
|
|
range->flags = parser->bus->get_flags(parser->range);
|
|
|
|
|
|
|
|
range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
|
2020-02-06 14:01:05 +00:00
|
|
|
|
2020-02-06 14:34:54 +00:00
|
|
|
if (parser->dma)
|
|
|
|
range->cpu_addr = of_translate_dma_address(parser->node,
|
|
|
|
parser->range + na);
|
|
|
|
else
|
|
|
|
range->cpu_addr = of_translate_address(parser->node,
|
|
|
|
parser->range + na);
|
|
|
|
range->size = of_read_number(parser->range + parser->pna + na, ns);
|
|
|
|
|
2020-02-06 14:01:05 +00:00
|
|
|
parser->range += np;
|
2020-02-06 14:34:54 +00:00
|
|
|
|
|
|
|
/* Now consume following elements while they are contiguous */
|
2020-02-06 14:01:05 +00:00
|
|
|
while (parser->range + np <= parser->end) {
|
|
|
|
u32 flags = 0;
|
2020-07-28 15:36:55 +00:00
|
|
|
u64 bus_addr, cpu_addr, size;
|
2020-02-06 14:34:54 +00:00
|
|
|
|
2020-07-28 15:36:55 +00:00
|
|
|
flags = parser->bus->get_flags(parser->range);
|
|
|
|
bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
|
2020-02-06 14:34:54 +00:00
|
|
|
if (parser->dma)
|
|
|
|
cpu_addr = of_translate_dma_address(parser->node,
|
|
|
|
parser->range + na);
|
|
|
|
else
|
|
|
|
cpu_addr = of_translate_address(parser->node,
|
|
|
|
parser->range + na);
|
|
|
|
size = of_read_number(parser->range + parser->pna + na, ns);
|
|
|
|
|
|
|
|
if (flags != range->flags)
|
|
|
|
break;
|
2020-07-28 15:36:55 +00:00
|
|
|
if (bus_addr != range->bus_addr + range->size ||
|
2020-02-06 14:34:54 +00:00
|
|
|
cpu_addr != range->cpu_addr + range->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
range->size += size;
|
2020-02-06 14:01:05 +00:00
|
|
|
parser->range += np;
|
2020-02-06 14:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
|
|
|
|
|
2018-03-14 18:15:54 +00:00
|
|
|
static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
|
|
|
|
u64 size)
|
|
|
|
{
|
|
|
|
u64 taddr;
|
|
|
|
unsigned long port;
|
|
|
|
struct device_node *host;
|
|
|
|
|
2019-04-01 08:56:43 +00:00
|
|
|
taddr = __of_translate_address(dev, of_get_parent,
|
|
|
|
in_addr, "ranges", &host);
|
2018-03-14 18:15:54 +00:00
|
|
|
if (host) {
|
|
|
|
/* host-specific port access */
|
|
|
|
port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
|
|
|
|
of_node_put(host);
|
|
|
|
} else {
|
|
|
|
/* memory-mapped I/O range */
|
|
|
|
port = pci_address_to_pio(taddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port == (unsigned long)-1)
|
|
|
|
return OF_BAD_ADDR;
|
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2020-09-17 16:43:40 +00:00
|
|
|
#ifdef CONFIG_HAS_DMA
|
2014-04-24 15:30:02 +00:00
|
|
|
/**
|
2020-09-17 16:43:40 +00:00
|
|
|
* of_dma_get_range - Get DMA range info and put it into a map array
|
2014-04-24 15:30:02 +00:00
|
|
|
* @np: device node to get DMA range info
|
2020-09-17 16:43:40 +00:00
|
|
|
* @map: dma range structure to return
|
2014-04-24 15:30:02 +00:00
|
|
|
*
|
|
|
|
* Look in bottom up direction for the first "dma-ranges" property
|
2020-09-17 16:43:40 +00:00
|
|
|
* and parse it. Put the information into a DMA offset map array.
|
|
|
|
*
|
|
|
|
* dma-ranges format:
|
2014-04-24 15:30:02 +00:00
|
|
|
* DMA addr (dma_addr) : naddr cells
|
|
|
|
* CPU addr (phys_addr_t) : pna cells
|
|
|
|
* size : nsize cells
|
|
|
|
*
|
2020-09-17 16:43:40 +00:00
|
|
|
* It returns -ENODEV if "dma-ranges" property was not found for this
|
|
|
|
* device in the DT.
|
2014-04-24 15:30:02 +00:00
|
|
|
*/
|
2020-09-17 16:43:40 +00:00
|
|
|
int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
|
2014-04-24 15:30:02 +00:00
|
|
|
{
|
2024-04-09 18:59:41 +00:00
|
|
|
struct device_node *node __free(device_node) = of_node_get(np);
|
2014-04-24 15:30:02 +00:00
|
|
|
const __be32 *ranges = NULL;
|
2019-07-03 17:42:20 +00:00
|
|
|
bool found_dma_ranges = false;
|
2020-02-06 14:02:30 +00:00
|
|
|
struct of_range_parser parser;
|
|
|
|
struct of_range range;
|
2020-09-17 16:43:40 +00:00
|
|
|
struct bus_dma_region *r;
|
|
|
|
int len, num_ranges = 0;
|
2014-04-24 15:30:02 +00:00
|
|
|
|
2019-07-03 17:42:20 +00:00
|
|
|
while (node) {
|
2014-04-24 15:30:02 +00:00
|
|
|
ranges = of_get_property(node, "dma-ranges", &len);
|
|
|
|
|
|
|
|
/* Ignore empty ranges, they imply no translation required */
|
|
|
|
if (ranges && len > 0)
|
|
|
|
break;
|
|
|
|
|
2019-07-03 17:42:20 +00:00
|
|
|
/* Once we find 'dma-ranges', then a missing one is an error */
|
2024-04-09 18:59:41 +00:00
|
|
|
if (found_dma_ranges && !ranges)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2019-07-03 17:42:20 +00:00
|
|
|
found_dma_ranges = true;
|
|
|
|
|
|
|
|
node = of_get_next_dma_parent(node);
|
2014-04-24 15:30:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 17:42:20 +00:00
|
|
|
if (!node || !ranges) {
|
2017-06-01 20:50:55 +00:00
|
|
|
pr_debug("no dma-ranges found for node(%pOF)\n", np);
|
2024-04-09 18:59:41 +00:00
|
|
|
return -ENODEV;
|
2014-04-24 15:30:02 +00:00
|
|
|
}
|
2020-02-06 14:02:30 +00:00
|
|
|
of_dma_range_parser_init(&parser, node);
|
2023-01-28 17:47:50 +00:00
|
|
|
for_each_of_range(&parser, &range) {
|
|
|
|
if (range.cpu_addr == OF_BAD_ADDR) {
|
|
|
|
pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
|
|
|
|
range.bus_addr, node);
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-17 16:43:40 +00:00
|
|
|
num_ranges++;
|
2023-01-28 17:47:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 18:59:41 +00:00
|
|
|
if (!num_ranges)
|
|
|
|
return -EINVAL;
|
2020-09-17 16:43:40 +00:00
|
|
|
|
|
|
|
r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
|
2024-04-09 18:59:41 +00:00
|
|
|
if (!r)
|
|
|
|
return -ENOMEM;
|
2020-02-06 14:02:30 +00:00
|
|
|
|
2020-09-17 16:43:40 +00:00
|
|
|
/*
|
2023-01-28 17:47:50 +00:00
|
|
|
* Record all info in the generic DMA ranges array for struct device,
|
|
|
|
* returning an error if we don't find any parsable ranges.
|
2020-09-17 16:43:40 +00:00
|
|
|
*/
|
|
|
|
*map = r;
|
|
|
|
of_dma_range_parser_init(&parser, node);
|
2020-02-06 14:02:30 +00:00
|
|
|
for_each_of_range(&parser, &range) {
|
|
|
|
pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
|
|
|
|
range.bus_addr, range.cpu_addr, range.size);
|
2023-01-28 17:47:50 +00:00
|
|
|
if (range.cpu_addr == OF_BAD_ADDR)
|
2020-08-17 11:32:08 +00:00
|
|
|
continue;
|
2020-09-17 16:43:40 +00:00
|
|
|
r->cpu_start = range.cpu_addr;
|
|
|
|
r->dma_start = range.bus_addr;
|
|
|
|
r->size = range.size;
|
|
|
|
r++;
|
2014-04-24 15:30:02 +00:00
|
|
|
}
|
2024-04-09 18:59:41 +00:00
|
|
|
return 0;
|
2014-04-24 15:30:02 +00:00
|
|
|
}
|
2020-09-17 16:43:40 +00:00
|
|
|
#endif /* CONFIG_HAS_DMA */
|
2014-04-24 15:30:03 +00:00
|
|
|
|
2020-11-19 17:53:55 +00:00
|
|
|
/**
|
|
|
|
* of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
|
|
|
|
* @np: The node to start searching from or NULL to start from the root
|
|
|
|
*
|
|
|
|
* Gets the highest CPU physical address that is addressable by all DMA masters
|
|
|
|
* in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
|
|
|
|
* DMA constrained device is found, it returns PHYS_ADDR_MAX.
|
|
|
|
*/
|
|
|
|
phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
|
|
|
|
{
|
|
|
|
phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
|
|
|
|
struct of_range_parser parser;
|
|
|
|
phys_addr_t subtree_max_addr;
|
|
|
|
struct device_node *child;
|
|
|
|
struct of_range range;
|
|
|
|
const __be32 *ranges;
|
|
|
|
u64 cpu_end = 0;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!np)
|
|
|
|
np = of_root;
|
|
|
|
|
|
|
|
ranges = of_get_property(np, "dma-ranges", &len);
|
|
|
|
if (ranges && len) {
|
|
|
|
of_dma_range_parser_init(&parser, np);
|
|
|
|
for_each_of_range(&parser, &range)
|
|
|
|
if (range.cpu_addr + range.size > cpu_end)
|
|
|
|
cpu_end = range.cpu_addr + range.size - 1;
|
|
|
|
|
|
|
|
if (max_cpu_addr > cpu_end)
|
|
|
|
max_cpu_addr = cpu_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for_each_available_child_of_node(np, child) {
|
|
|
|
subtree_max_addr = of_dma_get_max_cpu_address(child);
|
|
|
|
if (max_cpu_addr > subtree_max_addr)
|
|
|
|
max_cpu_addr = subtree_max_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_cpu_addr;
|
|
|
|
}
|
|
|
|
|
2014-04-24 15:30:03 +00:00
|
|
|
/**
|
|
|
|
* of_dma_is_coherent - Check if device is coherent
|
|
|
|
* @np: device node
|
|
|
|
*
|
|
|
|
* It returns true if "dma-coherent" property was found
|
2020-01-26 11:52:47 +00:00
|
|
|
* for this device in the DT, or if DMA is coherent by
|
2022-07-06 23:15:33 +00:00
|
|
|
* default for OF devices on the current platform and no
|
|
|
|
* "dma-noncoherent" property was found for this device.
|
2014-04-24 15:30:03 +00:00
|
|
|
*/
|
|
|
|
bool of_dma_is_coherent(struct device_node *np)
|
|
|
|
{
|
2024-04-09 18:59:41 +00:00
|
|
|
struct device_node *node __free(device_node) = of_node_get(np);
|
2020-11-10 02:28:25 +00:00
|
|
|
|
2014-04-24 15:30:03 +00:00
|
|
|
while (node) {
|
2024-04-09 18:59:41 +00:00
|
|
|
if (of_property_read_bool(node, "dma-coherent"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (of_property_read_bool(node, "dma-noncoherent"))
|
|
|
|
return false;
|
|
|
|
|
2019-07-03 13:47:31 +00:00
|
|
|
node = of_get_next_dma_parent(node);
|
2014-04-24 15:30:03 +00:00
|
|
|
}
|
2024-04-09 18:59:41 +00:00
|
|
|
return dma_default_coherent;
|
2014-04-24 15:30:03 +00:00
|
|
|
}
|
2014-06-05 22:57:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(of_dma_is_coherent);
|
2021-02-11 12:41:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* of_mmio_is_nonposted - Check if device uses non-posted MMIO
|
|
|
|
* @np: device node
|
|
|
|
*
|
|
|
|
* Returns true if the "nonposted-mmio" property was found for
|
|
|
|
* the device's bus.
|
|
|
|
*
|
|
|
|
* This is currently only enabled on builds that support Apple ARM devices, as
|
|
|
|
* an optimization.
|
|
|
|
*/
|
2024-10-10 16:27:16 +00:00
|
|
|
static bool of_mmio_is_nonposted(const struct device_node *np)
|
2021-02-11 12:41:56 +00:00
|
|
|
{
|
|
|
|
if (!IS_ENABLED(CONFIG_ARCH_APPLE))
|
|
|
|
return false;
|
|
|
|
|
2024-04-09 18:59:41 +00:00
|
|
|
struct device_node *parent __free(device_node) = of_get_parent(np);
|
2021-02-11 12:41:56 +00:00
|
|
|
if (!parent)
|
|
|
|
return false;
|
|
|
|
|
2024-04-09 18:59:41 +00:00
|
|
|
return of_property_read_bool(parent, "nonposted-mmio");
|
2021-02-11 12:41:56 +00:00
|
|
|
}
|
2023-03-31 07:52:30 +00:00
|
|
|
|
|
|
|
static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
|
|
|
|
struct resource *r)
|
|
|
|
{
|
|
|
|
u64 taddr;
|
|
|
|
const __be32 *addrp;
|
|
|
|
u64 size;
|
|
|
|
unsigned int flags;
|
|
|
|
const char *name = NULL;
|
|
|
|
|
|
|
|
addrp = __of_get_address(dev, index, bar_no, &size, &flags);
|
|
|
|
if (addrp == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Get optional "reg-names" property to add a name to a resource */
|
|
|
|
if (index >= 0)
|
|
|
|
of_property_read_string_index(dev, "reg-names", index, &name);
|
|
|
|
|
|
|
|
if (flags & IORESOURCE_MEM)
|
|
|
|
taddr = of_translate_address(dev, addrp);
|
|
|
|
else if (flags & IORESOURCE_IO)
|
|
|
|
taddr = of_translate_ioport(dev, addrp, size);
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (taddr == OF_BAD_ADDR)
|
|
|
|
return -EINVAL;
|
|
|
|
memset(r, 0, sizeof(struct resource));
|
|
|
|
|
|
|
|
if (of_mmio_is_nonposted(dev))
|
|
|
|
flags |= IORESOURCE_MEM_NONPOSTED;
|
|
|
|
|
|
|
|
r->flags = flags;
|
|
|
|
r->name = name ? name : dev->full_name;
|
|
|
|
|
2024-09-06 12:25:19 +00:00
|
|
|
return __of_address_resource_bounds(r, taddr, size);
|
2023-03-31 07:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_address_to_resource - Translate device tree address and return as resource
|
|
|
|
* @dev: Caller's Device Node
|
|
|
|
* @index: Index into the array
|
|
|
|
* @r: Pointer to resource array
|
|
|
|
*
|
|
|
|
* Returns -EINVAL if the range cannot be converted to resource.
|
|
|
|
*
|
|
|
|
* Note that if your address is a PIO address, the conversion will fail if
|
|
|
|
* the physical address can't be internally converted to an IO token with
|
|
|
|
* pci_address_to_pio(), that is because it's either called too early or it
|
|
|
|
* can't be matched to any host bridge IO space
|
|
|
|
*/
|
|
|
|
int of_address_to_resource(struct device_node *dev, int index,
|
|
|
|
struct resource *r)
|
|
|
|
{
|
|
|
|
return __of_address_to_resource(dev, index, -1, r);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_address_to_resource);
|
|
|
|
|
|
|
|
int of_pci_address_to_resource(struct device_node *dev, int bar,
|
|
|
|
struct resource *r)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_PCI))
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return __of_address_to_resource(dev, -1, bar, r);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_iomap - Maps the memory mapped IO for a given device_node
|
|
|
|
* @np: the device whose io range will be mapped
|
|
|
|
* @index: index of the io range
|
|
|
|
*
|
|
|
|
* Returns a pointer to the mapped memory
|
|
|
|
*/
|
|
|
|
void __iomem *of_iomap(struct device_node *np, int index)
|
|
|
|
{
|
|
|
|
struct resource res;
|
|
|
|
|
|
|
|
if (of_address_to_resource(np, index, &res))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (res.flags & IORESOURCE_MEM_NONPOSTED)
|
|
|
|
return ioremap_np(res.start, resource_size(&res));
|
|
|
|
else
|
|
|
|
return ioremap(res.start, resource_size(&res));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_iomap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* of_io_request_and_map - Requests a resource and maps the memory mapped IO
|
|
|
|
* for a given device_node
|
|
|
|
* @device: the device whose io range will be mapped
|
|
|
|
* @index: index of the io range
|
|
|
|
* @name: name "override" for the memory region request or NULL
|
|
|
|
*
|
|
|
|
* Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
|
|
|
|
* error code on failure. Usage example:
|
|
|
|
*
|
|
|
|
* base = of_io_request_and_map(node, 0, "foo");
|
|
|
|
* if (IS_ERR(base))
|
|
|
|
* return PTR_ERR(base);
|
|
|
|
*/
|
|
|
|
void __iomem *of_io_request_and_map(struct device_node *np, int index,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct resource res;
|
|
|
|
void __iomem *mem;
|
|
|
|
|
|
|
|
if (of_address_to_resource(np, index, &res))
|
|
|
|
return IOMEM_ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
name = res.name;
|
|
|
|
if (!request_mem_region(res.start, resource_size(&res), name))
|
|
|
|
return IOMEM_ERR_PTR(-EBUSY);
|
|
|
|
|
|
|
|
if (res.flags & IORESOURCE_MEM_NONPOSTED)
|
|
|
|
mem = ioremap_np(res.start, resource_size(&res));
|
|
|
|
else
|
|
|
|
mem = ioremap(res.start, resource_size(&res));
|
|
|
|
|
|
|
|
if (!mem) {
|
|
|
|
release_mem_region(res.start, resource_size(&res));
|
|
|
|
return IOMEM_ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(of_io_request_and_map);
|