Raspberry Pi 400/Compute Module 4 support
Subsystems (8)
v4 cover Raspberry Pi 400/Compute Module 4 support
@Simon: I had to roll back all "if (CONFIG_IS_ENABLED(DM_DMA)" uses. As per buildman it breaks compilation when !DM or !DM_DMA due to access to undefined variables, namely dev and dev->dma_offset. I figure in u-boot we root for small Image sizes, so defining the variables regarless of them being needed is not an option.
---
This series could be split into at least two or even three parts, but I kept it as is for now as it contains all the changes needed in order to have u-boot working on the new Raspberry Pi 400 and Raspberry Pi Compute Module 4.
There are core changes, specifically with regard to cpu to bus address space translations. So far we had relied on hard-coded values but RPi needs per device translations as it has at least three distinct bus address spaces with different offsets. So it's a good opportunity to implement bus translations the right way by parsing DT's dma-ranges.
Here's a more concrete example of what we're dealing with:
- On a RPi4, SoC version BCM2711C0 with 8GB of memory
[0x0 0x200000000] [0x200000000 0x400000000] [0xc0000000 0x100000000] [0x00000000 0x100000000]
phys/cpu address PCIe bus address Legacy peripheral emmc2 address
space space address space space
- On a RPi4, SoC version BCM2711C0 with 4GB of memory
[0x0 0x100000000] [0x100000000 0x200000000] [0xc0000000 0x100000000] [0x00000000 0x100000000]
phys/cpu address PCIe bus address Legacy peripheral emmc2 address
space space address space space
- On a RPi4, SoC version BCM2711B0 with 8GB of memory (bus can only access the lower 3GB of memory because of a SoC routing bug)
[0x0 0x200000000] [0x00000000 0xC0000000] [0xc0000000 0x100000000]
phys/cpu address PCIe bus address Legacy peripheral
space space address space
---
Changes since v3:
- Add commit "video: arm: rpi: Add brcm,bcm2711-hdmi0 compatible", it's pretty harmless
- Get rid of non-device based phys2bus call in xhci & mmc
- Simon's review changes in tests
- Comment some of the APIs
Changes since v2:
- Test builds not broken with buildman
- Add tests to all DM changes
- Make code conditional with config option
- Correct OF refcount
- Add config changes
- Address small changes as per reviews
Changes since v1:
- Fix some issues in 'dm: Introduce xxx_get_dma_range()'
- Fix some typos in commit messages
- Change DTB file name for RPi400
- Address Matthias' comments
Nicolas Saenz Julienne (13):
rpi: Add identifier for the new RPi400
rpi: Add identifier for the new CM4
pci: pcie-brcmstb: Fix inbound window configurations
dm: Introduce xxx_get_dma_range()
dm: test: Add test case for dev_get_dma_ranges()
dm: Introduce DMA constraints into the core device model
dm: test: Add test case for dev->dma_offset
dm: Introduce dev_phys_to_bus()/dev_bus_to_phys()
dm: test: Add test case for dev_phys_to_bus()/dev_bus_to_phys()
xhci: translate virtual addresses into the bus's address space
mmc: Introduce mmc_phys_to_bus()/mmc_bus_to_phys()
configs: rpi4: Enable DM_DMA across all RPi4 configurations
video: arm: rpi: Add brcm,bcm2711-hdmi0 compatible
arch/sandbox/dts/test.dts | 21 ++++++++ board/raspberrypi/rpi/rpi.c | 10 ++++ common/fdt_support.c | 73 ++++++++++++++++++++++++++++ configs/rpi_4_32b_defconfig | 1 + configs/rpi_4_defconfig | 1 + configs/rpi_arm64_defconfig | 1 + configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + configs/sandbox_flattree_defconfig | 1 + configs/sandbox_spl_defconfig | 1 + drivers/core/Kconfig | 10 ++++ drivers/core/device.c | 45 +++++++++++++++++ drivers/core/of_addr.c | 78 ++++++++++++++++++++++++++++++ drivers/core/ofnode.c | 9 ++++ drivers/core/read.c | 6 +++ drivers/mmc/sdhci.c | 7 +-- drivers/pci/pcie_brcmstb.c | 12 ++--- drivers/usb/host/xhci-mem.c | 45 +++++++++-------- drivers/usb/host/xhci-ring.c | 11 +++-- drivers/usb/host/xhci.c | 4 +- drivers/video/bcm2835.c | 1 + include/dm/device.h | 5 ++ include/dm/of_addr.h | 17 +++++++ include/dm/ofnode.h | 16 ++++++ include/dm/read.h | 21 ++++++++ include/fdt_support.h | 14 ++++++ include/mmc.h | 10 ++++ include/phys2bus.h | 16 ++++++ include/usb/xhci.h | 22 ++++++++- test/dm/Makefile | 2 + test/dm/core.c | 30 ++++++++++++ test/dm/phys2bus.c | 37 ++++++++++++++ test/dm/read.c | 49 +++++++++++++++++++ 33 files changed, 540 insertions(+), 38 deletions(-) create mode 100644 test/dm/phys2bus.c create mode 100644 test/dm/read.c
Signature · 1 line
2.29.2
PATCH v4 1/13 rpi: Add identifier for the new RPi400
The Raspberry Pi Foundation released the new RPi400 which we want to detect, so we can enable Ethernet on it and know the correct device tree file name.
---
Changes since v1:
- The RPi Foundation introduced a RPi400 specific device tree, so use that file name instead of the fallback (RPi4b).
board/raspberrypi/rpi/rpi.c | 5 +++++ 1 file changed, 5 insertions(+)
board/raspberrypi/rpi/rpi.c +5 -0
@@ -157,6 +157,11 @@ static const struct rpi_model rpi_models_new_scheme[] = {DTB_DIR "bcm2711-rpi-4-b.dtb" , true, }, + [0x13] = { +"400" , +DTB_DIR "bcm2711-rpi-400.dtb" , + true, + }, }; static const struct rpi_model rpi_models_old_scheme[] = {
Signature · 1 line
2.29.2
PATCH v4 2/13 rpi: Add identifier for the new CM4
In reply to Nicolas Saenz JulienneThe Raspberry Pi Foundation released the new Compute Module 4 which we want to detect, so we can enable Ethernet on it and know the correct device tree file name.
Note that this sets the Ethernet option to true since the official CM4 IO board has an Ethernet port. But that might not be the case when using custom ones.
--- board/raspberrypi/rpi/rpi.c | 5 +++++ 1 file changed, 5 insertions(+)
board/raspberrypi/rpi/rpi.c +5 -0
@@ -162,6 +162,11 @@ static const struct rpi_model rpi_models_new_scheme[] = {DTB_DIR "bcm2711-rpi-400.dtb" , true, }, + [0x14] = { +"Compute Module 4" , +DTB_DIR "bcm2711-rpi-cm4.dtb" , + true, + }, }; static const struct rpi_model rpi_models_old_scheme[] = {
Signature · 1 line
2.29.2
PATCH v4 3/13 pci: pcie-brcmstb: Fix inbound window configurations
In reply to Nicolas Saenz JulienneSo far we've assumed a fixed configuration for inbound windows as we had a single user for this controller. But the controller's DMA constraints were improved starting with BCM2711's B1 revision of the SoC, notably available in CM4 and Pi400. They allow for wider inbound windows. We can now cover the whole address space, whereas before we where limited to the lower 3GB.
This information is passed to us through DT's 'dma-ranges' property and it's specially important for us to honor it since some interactions with the board's co-processor assume we're doing so (specifically the XHCI firmware load operation, which is handled by the co-processor after u-boot has correctly configured the PCIe controller).
--- drivers/pci/pcie_brcmstb.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/pci/pcie_brcmstb.c +5 -7
@@ -432,6 +432,7 @@ static int brcm_pcie_probe(struct udevice *dev)struct pci_controller *hose =dev_get_uclass_priv (ctlr);struct brcm_pcie *pcie =dev_get_priv (dev);void __iomem *base = pcie->base; +struct pci_region region;bool ssc_good = false;int num_out_wins =0 ;u64 rc_bar2_offset, rc_bar2_size;
@@ -468,13 +469,10 @@ static int brcm_pcie_probe(struct udevice *dev)MISC_CTRL_SCB_ACCESS_EN_MASK |MISC_CTRL_CFG_READ_UR_MODE_MASK | MISC_CTRL_MAX_BURST_SIZE_128); -/* -* TODO: When support for other SoCs than BCM2711 is added we may -* need to use the base address and size(s) provided in the dma-ranges -* property. -*/ - rc_bar2_offset =0 ; - rc_bar2_size =0xc0000000 ; + +pci_get_dma_regions (dev, ®ion,0 ); + rc_bar2_offset = region.bus_start - region.phys_start; + rc_bar2_size =1ULL <<fls64 (region.size -1 ); tmp =lower_32_bits (rc_bar2_offset); u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(rc_bar2_size),
Signature · 1 line
2.29.2
PATCH v4 4/13 dm: Introduce xxx_get_dma_range()
In reply to Nicolas Saenz JulienneAdd the following functions to get a specific device's DMA ranges:
- dev_get_dma_range()
- ofnode_get_dma_range()
- of_get_dma_range()
- fdt_get_dma_range()
They are specially useful in oder to be able validate a physical address space range into a bus's and to convert addresses from and to address spaces.
---
Changes since v2:
- Return ENOENT instead of ENODEV
- Refcount of nodes
Changes since v1:
- Fix wrong arguments in of_get_dma_range()'s call to of_translate_dma_address()
- Fix build in SPL/TPL and no LIBFDT supprt
- Add missing declaration in 'core/read.c'
- Address Matthias' comments
common/fdt_support.c | 73 +++++++++++++++++++++++++++++++++++++++ drivers/core/of_addr.c | 78 ++++++++++++++++++++++++++++++++++++++++++ drivers/core/ofnode.c | 9 +++++ drivers/core/read.c | 6 ++++ include/dm/of_addr.h | 17 +++++++++ include/dm/ofnode.h | 16 +++++++++ include/dm/read.h | 21 ++++++++++++ include/fdt_support.h | 14 ++++++++ 8 files changed, 234 insertions(+)
common/fdt_support.c +73 -0
@@ -1342,6 +1342,79 @@ u64 fdt_translate_dma_address(const void *blob, int node_offset,return __of_translate_address (blob, node_offset, in_addr,"dma-ranges" ); } +int fdt_get_dma_range (const void *blob,int node,phys_addr_t *cpu, +dma_addr_t *bus,u64 *size) +{ +bool found_dma_ranges = false; +struct of_bus *bus_node; +const fdt32_t *ranges; +int na, ns, pna, pns; +int parent = node; +int ret =0 ; +int len; + +/* Find the closest dma-ranges property */ +while (parent >=0 ) { + ranges =fdt_getprop (blob, parent,"dma-ranges" , &len); + +/* Ignore empty ranges, they imply no translation required */ +if (ranges && len >0 ) +break ; + +/* Once we find 'dma-ranges', then a missing one is an error */ +if (found_dma_ranges && !ranges) { + ret = -EINVAL ; + goto out; + } + +if (ranges) + found_dma_ranges = true; + + parent =fdt_parent_offset (blob, parent); + } + +if (!ranges || parent <0 ) { +debug ("no dma-ranges found for node %s\n" , +fdt_get_name (blob, node,NULL )); + ret = -ENOENT ; + goto out; + } + +/* switch to that node */ + node = parent; + parent =fdt_parent_offset (blob, node); +if (parent <0 ) { +printf ("Found dma-ranges in root node, shoudln't happen\n" ); + ret = -EINVAL ; + goto out; + } + +/* Get the address sizes both for the bus and its parent */ + bus_node =of_match_bus (blob, node); + bus_node->count_cells (blob, node, &na, &ns); +if (!OF_CHECK_COUNTS (na, ns)) { +printf ("%s: Bad cell count for %s\n" , __FUNCTION__, +fdt_get_name (blob, node,NULL )); +return -EINVAL ; + goto out; + } + + bus_node =of_match_bus (blob, parent); + bus_node->count_cells (blob, parent, &pna, &pns); +if (!OF_CHECK_COUNTS (pna, pns)) { +printf ("%s: Bad cell count for %s\n" , __FUNCTION__, +fdt_get_name (blob, parent,NULL )); +return -EINVAL ; + goto out; + } + + *bus =fdt_read_number (ranges, na); + *cpu =fdt_translate_dma_address (blob, node, ranges + na); + *size =fdt_read_number (ranges + na + pna, ns); +out: +return ret; +} +/** * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and * who's reg property matches a physical cpu address
drivers/core/of_addr.c +78 -0
84 lines not on this page · raw message
drivers/core/ofnode.c +9 -0
@@ -911,6 +911,15 @@ u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)return fdt_translate_dma_address (gd->fdt_blob,ofnode_to_offset (node), in_addr); } +int ofnode_get_dma_range (ofnode node,phys_addr_t *cpu,dma_addr_t *bus,u64 *size) +{ +if (ofnode_is_np (node)) +return of_get_dma_range (ofnode_to_np (node), cpu, bus, size); +else +return fdt_get_dma_range (gd->fdt_blob,ofnode_to_offset (node), + cpu, bus, size); +} +int ofnode_device_is_compatible (ofnode node,const char *compat) {if (ofnode_is_np (node))
drivers/core/read.c +6 -0
@@ -338,6 +338,12 @@ u64 dev_translate_dma_address(const struct udevice *dev, const fdt32_t *in_addr)return ofnode_translate_dma_address (dev_ofnode (dev), in_addr); } +int dev_get_dma_range (const struct udevice *dev,phys_addr_t *cpu, +dma_addr_t *bus,u64 *size) +{ +return ofnode_get_dma_range (dev_ofnode (dev), cpu, bus, size); +} +int dev_read_alias_highest_id (const char *stem) {if (of_live_active ())
include/dm/of_addr.h +17 -0
@@ -44,6 +44,23 @@ u64 of_translate_address(const struct device_node *no, const __be32 *in_addr);*/ u64 of_translate_dma_address (const struct device_node *no,const __be32 *in_addr); + +/** +* of_get_dma_range() - get dma-ranges for a specific DT node +* +* Get DMA ranges for a specifc node, this is useful to perform bus->cpu and +* cpu->bus address translations +* +* @param blob Pointer to device tree blob +* @param node_offset Node DT offset +* @param cpu Pointer to variable storing the range's cpu address +* @param bus Pointer to variable storing the range's bus address +* @param size Pointer to variable storing the range's size +* @return translated DMA address or OF_BAD_ADDR on error +*/ +int of_get_dma_range (const struct device_node *dev,phys_addr_t *cpu, +dma_addr_t *bus,u64 *size); +/** * of_get_address() - obtain an address from a node *
include/dm/ofnode.h +16 -0
@@ -939,6 +939,22 @@ u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);*/ u64 ofnode_translate_dma_address (ofnode node,const fdt32_t *in_addr); +/** +* ofnode_get_dma_range() - get dma-ranges for a specific DT node +* +* Get DMA ranges for a specifc node, this is useful to perform bus->cpu and +* cpu->bus address translations +* +* @param blob Pointer to device tree blob +* @param node_offset Node DT offset +* @param cpu Pointer to variable storing the range's cpu address +* @param bus Pointer to variable storing the range's bus address +* @param size Pointer to variable storing the range's size +* @return translated DMA address or OF_BAD_ADDR on error +*/ +int ofnode_get_dma_range (ofnode node,phys_addr_t *cpu,dma_addr_t *bus, +u64 *size); +/** * ofnode_device_is_compatible() - check if the node is compatible with compat *
include/dm/read.h +21 -0
@@ -664,6 +664,21 @@ u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);u64 dev_translate_dma_address (const struct udevice *dev,const fdt32_t *in_addr); +/** +* dev_get_dma_range() - Get a device's DMA constraints +* +* Provide the address bases and size of the linear mapping between the CPU and +* a device's BUS address space. +* +* @dev: device giving the context in which to translate the DMA address +* @cpu: base address for CPU's view of memory +* @bus: base address for BUS's view of memory +* @size: size of the address space +* @return 0 if ok, negative on error +*/ +int dev_get_dma_range (const struct udevice *dev,phys_addr_t *cpu, +dma_addr_t *bus,u64 *size); +/** * dev_read_alias_highest_id - Get highest alias id for the given stem * @stem: Alias stem to be examined
@@ -1004,6 +1019,12 @@ static inline u64 dev_translate_dma_address(const struct udevice *dev,return ofnode_translate_dma_address (dev_ofnode (dev), in_addr); } +static inline int dev_get_dma_range (const struct udevice *dev,phys_addr_t *cpu, +dma_addr_t *bus,u64 *size) +{ +return ofnode_get_dma_range (dev_ofnode (dev), cpu, bus, size); +} +static inline int dev_read_alias_highest_id (const char *stem) {if (!CONFIG_IS_ENABLED (OF_LIBFDT ))
include/fdt_support.h +14 -0
@@ -260,6 +260,20 @@ u64 fdt_translate_address(const void *blob, int node_offset,u64 fdt_translate_dma_address (const void *blob,int node_offset,const __be32 *in_addr); +/** +* Get DMA ranges for a specifc node, this is useful to perform bus->cpu and +* cpu->bus address translations +* +* @param blob Pointer to device tree blob +* @param node_offset Node DT offset +* @param cpu Pointer to variable storing the range's cpu address +* @param bus Pointer to variable storing the range's bus address +* @param size Pointer to variable storing the range's size +* @return translated DMA address or OF_BAD_ADDR on error +*/ +int fdt_get_dma_range (const void *blob,int node_offset,phys_addr_t *cpu, +dma_addr_t *bus,u64 *size); +int fdt_node_offset_by_compat_reg (void *blob,const char *compat,phys_addr_t compat_off);int fdt_alloc_phandle (void *blob);
Signature · 1 line
2.29.2
PATCH v4 5/13 dm: test: Add test case for dev_get_dma_ranges()
In reply to Nicolas Saenz JulienneIntroduce some new nodes in sandbox's test device-tree and dm tests in order to validate dev_get_dma_range().
---
Changes since v3:
- Use ut_assertok() instead of ut_assert(!func())
arch/sandbox/dts/test.dts | 17 ++++++++++++++ test/dm/Makefile | 1 + test/dm/read.c | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 test/dm/read.c
arch/sandbox/dts/test.dts +17 -0
@@ -292,6 +292,23 @@ test5-gpios = <&gpio_a 19>; }; + mmio-bus at 0 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "denx,u-boot-test-bus"; + dma-ranges = <0x10000000 0x00000000 0x00040000>; + + subnode at 0 { + compatible = "denx,u-boot-fdt-test"; + }; + }; + + mmio-bus at 1 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "denx,u-boot-test-bus"; + }; + acpi_test1: acpi-test { compatible = "denx,u-boot-acpi-test"; acpi-ssdt-test-data = "ab";
test/dm/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_UT_DM) += test-fdt.o obj-$(CONFIG_UT_DM) += test-uclass.o obj-$(CONFIG_UT_DM) += core.o +obj-$(CONFIG_UT_DM) += read.o ifneq ($(CONFIG_SANDBOX),) obj-$(CONFIG_ACPIGEN) += acpi.o obj-$(CONFIG_ACPIGEN) += acpigen.o
test/dm/read.c new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.29.2
PATCH v4 6/13 dm: Introduce DMA constraints into the core device model
In reply to Nicolas Saenz JulienneCalculating the DMA offset between a bus address space and CPU's every time we call phys_to_bus() and bus_to_phys() isn't ideal performance wise, as it implies traversing the device tree from the device's node up to the root. Since this information is static and available before the device's initialization, parse it before the probe call an provide the DMA offset in 'struct udevice' for the address translation code to use it.
---
Changes since v2:
- Comment functions and struct variables
- Correct typos
Changes since v2:
- Return/Fail on error
- Add config option
- use ulong instead for u64 for dev->dma_offset
Changes since v1:
- Update commit message so as to explain better the reasoning behind this
drivers/core/Kconfig | 10 ++++++++++ drivers/core/device.c | 45 +++++++++++++++++++++++++++++++++++++++++++ include/dm/device.h | 5 +++++ 3 files changed, 60 insertions(+)
drivers/core/Kconfig +10 -0
@@ -113,6 +113,16 @@ config SPL_DM_SEQ_ALIAS numbered devices (e.g. serial0 = &serial0). This feature can be disabled if it is not required, to save code space in SPL. +config DM_DMA + bool "Support per-device DMA constraints" + depends on DM + default n + help + Enable this to extract per-device DMA constraints, only supported on + device-tree systems for now. This is needed in order translate + addresses on systems where different buses have different views of + the physical address space. + config REGMAP bool "Support register maps" depends on DM
drivers/core/device.c +45 -0
57 lines not on this page · raw message
include/dm/device.h +5 -0
@@ -138,6 +138,8 @@ enum {* When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will * add to this list. Memory so-allocated will be freed * automatically when the device is removed / unbound +* @dma_offset: Offset between the physical address space (CPU's) and the +* device's bus address space */ struct udevice {const struct driver *driver;
@@ -161,6 +163,9 @@ struct udevice {#ifdef CONFIG_DEVRES struct list_head devres_head;#endif +#ifdef CONFIG_DM_DMA +ulong dma_offset; +#endif };/* Maximum sequence number supported */
Signature · 1 line
2.29.2
PATCH v4 7/13 dm: test: Add test case for dev->dma_offset
In reply to Nicolas Saenz JulienneAdd test to validate dev->dma_offset is properly set on devices.
---
Changes since v3:
- Use ut_assertok() instead of ut_assert(!func())
arch/sandbox/dts/test.dts | 4 ++++ configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + configs/sandbox_flattree_defconfig | 1 + configs/sandbox_spl_defconfig | 1 + test/dm/core.c | 30 ++++++++++++++++++++++++++++++ 6 files changed, 38 insertions(+)
arch/sandbox/dts/test.dts +4 -0
@@ -307,6 +307,10 @@ #address-cells = <1>; #size-cells = <1>; compatible = "denx,u-boot-test-bus"; + + subnode at 0 { + compatible = "denx,u-boot-fdt-test"; + }; }; acpi_test1: acpi-test {
configs/sandbox64_defconfig +1 -0
@@ -94,6 +94,7 @@ CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0" CONFIG_BOOTP_SEND_HOSTNAME=y CONFIG_NETCONSOLE=y CONFIG_IP_DEFRAG=y +CONFIG_DM_DMA=y CONFIG_REGMAP=y CONFIG_SYSCON=y CONFIG_DEVRES=y
configs/sandbox_defconfig +1 -0
@@ -108,6 +108,7 @@ CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0" CONFIG_BOOTP_SEND_HOSTNAME=y CONFIG_NETCONSOLE=y CONFIG_IP_DEFRAG=y +CONFIG_DM_DMA=y CONFIG_REGMAP=y CONFIG_SYSCON=y CONFIG_DEVRES=y
configs/sandbox_flattree_defconfig +1 -0
@@ -75,6 +75,7 @@ CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0" CONFIG_BOOTP_SEND_HOSTNAME=y CONFIG_NETCONSOLE=y CONFIG_IP_DEFRAG=y +CONFIG_DM_DMA=y CONFIG_REGMAP=y CONFIG_SYSCON=y CONFIG_DEVRES=y
configs/sandbox_spl_defconfig +1 -0
@@ -94,6 +94,7 @@ CONFIG_BOOTP_SEND_HOSTNAME=y CONFIG_NETCONSOLE=y CONFIG_IP_DEFRAG=y CONFIG_SPL_DM=y +CONFIG_DM_DMA=y CONFIG_REGMAP=y CONFIG_SPL_REGMAP=y CONFIG_SYSCON=y
test/dm/core.c +30 -0
@@ -1066,3 +1066,33 @@ static int dm_test_inactive_child(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_inactive_child,UT_TESTF_SCAN_PDATA ); + +static int dm_test_dma_offset (struct unit_test_state *uts) +{ +struct udevice *dev; +ofnode node; + +/* Make sure the bus's dma-ranges aren't taken into account here */ + node =ofnode_path ("/mmio-bus at 0" ); +ut_assert (ofnode_valid (node)); +ut_assertok (uclass_get_device_by_ofnode (UCLASS_TEST_BUS , node, &dev)); +ut_asserteq_64 (0 , dev->dma_offset); + +/* Device behind a bus with dma-ranges */ + node =ofnode_path ("/mmio-bus at 0/subnode at 0" ); +ut_assert (ofnode_valid (node)); +ut_assertok (uclass_get_device_by_ofnode (UCLASS_TEST_FDT , node, &dev)); +ut_asserteq_64 (-0x10000000ULL , dev->dma_offset); + +/* This one has no dma-ranges */ + node =ofnode_path ("/mmio-bus at 1" ); +ut_assert (ofnode_valid (node)); +ut_assertok (uclass_get_device_by_ofnode (UCLASS_TEST_BUS , node, &dev)); + node =ofnode_path ("/mmio-bus at 1/subnode at 0" ); +ut_assert (ofnode_valid (node)); +ut_assertok (uclass_get_device_by_ofnode (UCLASS_TEST_FDT , node, &dev)); +ut_asserteq_64 (0 , dev->dma_offset); + +return 0 ; +} +DM_TEST (dm_test_dma_offset,UT_TESTF_SCAN_PDATA |UT_TESTF_SCAN_FDT );
Signature · 1 line
2.29.2
PATCH v4 8/13 dm: Introduce dev_phys_to_bus()/dev_bus_to_phys()
In reply to Nicolas Saenz JulienneThese functions, instead of relying on hard-coded platform-specific address translations, make use of the DMA constraints provided by the DM core. This allows for per-device translations.
We can't yet get rid of the legacy phys_to_bus()/bus_to_phys() implementations as some of its users are not integrated into the device model.
---
Changes since v2:
- Use CONFIG_DM_DMA
include/phys2bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/phys2bus.h +16 -0
@@ -21,4 +21,20 @@ static inline unsigned long bus_to_phys(unsigned long bus) }#endif +#if CONFIG_IS_ENABLED (DM_DMA ) +#include <dm/device.h> + +static inline dma_addr_t dev_phys_to_bus (struct udevice *dev, +phys_addr_t phys) +{ +return phys - dev->dma_offset; +} + +static inline phys_addr_t dev_bus_to_phys (struct udevice *dev, +dma_addr_t bus) +{ +return bus + dev->dma_offset; +} +#endif +#endif
Signature · 1 line
2.29.2
PATCH v4 8/13 dm: Introduce dev_phys_to_bus()/dev_bus_to_phys()
Quoted 10 lines · Nicolas Saenz Julienne
These functions, instead of relying on hard-coded platform-specific address translations, make use of the DMA constraints provided by the DM core. This allows for per-device translations.
We can't yet get rid of the legacy phys_to_bus()/bus_to_phys() implementations as some of its users are not integrated into the device model.
Signed-off-by Nicolas Saenz Julienne <[email protected]>Reviewed-by Simon Glass <[email protected]>
Thanks,
Stefan
Quoted 23 lines
---
Changes since v2:
- UseCONFIG_DM_DMAinclude/phys2bus.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/phys2bus.h +7
-0@@ -21,4 +21,20 @@ static inline unsigned long bus_to_phys(unsigned long bus) }#endif +#if CONFIG_IS_ENABLED (DM_DMA ) +#include <dm/device.h> + +static inline dma_addr_t dev_phys_to_bus (struct udevice *dev, +phys_addr_t phys) +{ +return phys - dev->dma_offset;
include/phys2bus.h static inline unsigned long bus_to_phys(unsigned long bus) · line 40
Quoted 10 lines
include/phys2bus.h +9
-0+} + +static inline phys_addr_t dev_bus_to_phys (struct udevice *dev, +dma_addr_t bus) +{ +return bus + dev->dma_offset; +} +#endif +#endif
Viele Gr??e,
Stefan
Signature · 3 lines
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
PATCH v4 9/13 dm: test: Add test case for dev_phys_to_bus()/dev_bus_to_phys()
In reply to Nicolas Saenz JulienneBy reusing DT nodes already available in sandbox's test DT introduce a test to validate dev_phys_to_bus()/dev_bus_to_phys().
---
Changes since v3:
- Use ut_assertok() instead of ut_assert(!func())
- Use ut_assert_addr()
test/dm/Makefile | 1 + test/dm/phys2bus.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 test/dm/phys2bus.c
test/dm/Makefile +1 -0
@@ -16,6 +16,7 @@ obj-$(CONFIG_UT_DM) += test-uclass.o obj-$(CONFIG_UT_DM) += core.o obj-$(CONFIG_UT_DM) += read.o +obj-$(CONFIG_UT_DM) += phys2bus.o ifneq ($(CONFIG_SANDBOX),) obj-$(CONFIG_ACPIGEN) += acpi.o obj-$(CONFIG_ACPIGEN) += acpigen.o
test/dm/phys2bus.c new file +37 -0
@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (c) 2020 Nicolas Saenz Julienne <[email protected]> +*/ + +#include <common.h> +#include <dm.h> +#include <mapmem.h> +#include <phys2bus.h> +#include <dm/device.h> +#include <dm/ofnode.h> +#include <dm/root.h> +#include <dm/test.h> +#include <dm/uclass-internal.h> +#include <test/ut.h> + +static int dm_test_phys_to_bus (struct unit_test_state *uts) +{ +struct udevice *dev; +ofnode node; + + node =ofnode_path ("/mmio-bus at 0" ); +ut_assert (ofnode_valid (node)); +ut_assertok (uclass_get_device_by_ofnode (UCLASS_TEST_BUS , node, &dev)); +/* In this case it should be transparent, no dma-ranges in parent bus */ +ut_asserteq_addr ((void *)0xfffffULL , (void *)dev_phys_to_bus (dev,0xfffff )); +ut_asserteq_addr ((void *)0xfffffULL , (void *)(ulong )dev_bus_to_phys (dev,0xfffff )); + + node =ofnode_path ("/mmio-bus at 0/subnode at 0" ); +ut_assert (ofnode_valid (node)); +ut_assertok (uclass_get_device_by_ofnode (UCLASS_TEST_FDT , node, &dev)); +ut_asserteq_addr ((void *)0x100fffffULL , (void *)dev_phys_to_bus (dev,0xfffff )); +ut_asserteq_addr ((void *)0xfffffULL , (void *)(ulong )dev_bus_to_phys (dev,0x100fffff )); + +return 0 ; +} +DM_TEST (dm_test_phys_to_bus,UT_TESTF_SCAN_PDATA |UT_TESTF_SCAN_FDT );
Signature · 1 line
2.29.2
PATCH v4 10/13 xhci: translate virtual addresses into the bus's address space
In reply to Nicolas Saenz JulienneSo far we've been content with passing physical addresses when configuring memory addresses into XHCI controllers, but not all platforms have buses with transparent mappings. Specifically the Raspberry Pi 4 might introduce an offset to memory accesses incoming from its PCIe port.
Introduce xhci_virt_to_bus() and xhci_bus_to_virt() to cater with these limitations, and make sure we don't break non DM users.
---
Changes since v3:
- Don't call phys_to_bus()/bus_to_phys(), we only support DM
drivers/usb/host/xhci-mem.c | 45 +++++++++++++++++++----------------- drivers/usb/host/xhci-ring.c | 11 +++++---- drivers/usb/host/xhci.c | 4 ++-- include/usb/xhci.h | 22 +++++++++++++++++- 4 files changed, 54 insertions(+), 28 deletions(-)
drivers/usb/host/xhci-mem.c +24 -21
@@ -110,7 +110,7 @@ static void xhci_scratchpad_free(struct xhci_ctrl *ctrl) ctrl->dcbaa->dev_context_ptrs[0 ] =0 ; -free ((void *)(uintptr_t )le64_to_cpu (ctrl->scratchpad->sp_array[0 ])); +free (xhci_bus_to_virt (ctrl,le64_to_cpu (ctrl->scratchpad->sp_array[0 ])));free (ctrl->scratchpad->sp_array);free (ctrl->scratchpad); ctrl->scratchpad =NULL ;
@@ -216,8 +216,8 @@ static void *xhci_malloc(unsigned int size)* @param link_trbs flag to indicate whether to link the trbs or NOT * @return none */ -static void xhci_link_segments (struct xhci_segment *prev, -struct xhci_segment *next,bool link_trbs) +static void xhci_link_segments (struct xhci_ctrl *ctrl,struct xhci_segment *prev, +struct xhci_segment *next,bool link_trbs) {u32 val;u64 val_64 =0 ;
@@ -226,7 +226,7 @@ static void xhci_link_segments(struct xhci_segment *prev,return ; prev->next = next;if (link_trbs) { - val_64 =virt_to_phys (next->trbs); + val_64 =xhci_virt_to_bus (ctrl, next->trbs); prev->trbs[TRBS_PER_SEGMENT -1 ].link.segment_ptr =cpu_to_le64 (val_64);
@@ -304,7 +304,8 @@ static struct xhci_segment *xhci_segment_alloc(void)* @param link_trbs flag to indicate whether to link the trbs or NOT * @return pointer to the newly created RING */ -struct xhci_ring *xhci_ring_alloc (unsigned int num_segs,bool link_trbs) +struct xhci_ring *xhci_ring_alloc (struct xhci_ctrl *ctrl,unsigned int num_segs, +bool link_trbs) {struct xhci_ring *ring;struct xhci_segment *prev;
@@ -327,12 +328,12 @@ struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs) next =xhci_segment_alloc ();BUG_ON (!next); -xhci_link_segments (prev, next, link_trbs); +xhci_link_segments (ctrl, prev, next, link_trbs); prev = next; num_segs--; } -xhci_link_segments (prev, ring->first_seg, link_trbs); +xhci_link_segments (ctrl, prev, ring->first_seg, link_trbs);if (link_trbs) {/* See section 4.9.2.1 and 6.4.4.1 */ prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
@@ -354,6 +355,7 @@ static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)struct xhci_hccr *hccr = ctrl->hccr;struct xhci_hcor *hcor = ctrl->hcor;struct xhci_scratchpad *scratchpad; +uint64_t val_64;int num_sp;uint32_t page_size;void *buf;
@@ -371,8 +373,9 @@ static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl) scratchpad->sp_array =xhci_malloc (num_sp *sizeof (u64));if (!scratchpad->sp_array) goto fail_sp2; - ctrl->dcbaa->dev_context_ptrs[0 ] = -cpu_to_le64 ((uintptr_t )scratchpad->sp_array); + + val_64 =xhci_virt_to_bus (ctrl, scratchpad->sp_array); + ctrl->dcbaa->dev_context_ptrs[0 ] =cpu_to_le64 (val_64);xhci_flush_cache ((uintptr_t )&ctrl->dcbaa->dev_context_ptrs[0 ],sizeof (ctrl->dcbaa->dev_context_ptrs[0 ]));
@@ -393,8 +396,8 @@ static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)xhci_flush_cache ((uintptr_t )buf, num_sp * page_size);for (i =0 ; i < num_sp; i++) { -uintptr_t ptr = (uintptr_t )buf + i * page_size; - scratchpad->sp_array[i] =cpu_to_le64 (ptr); + val_64 =xhci_virt_to_bus (ctrl, buf + i * page_size); + scratchpad->sp_array[i] =cpu_to_le64 (val_64); } xhci_flush_cache((uintptr_t)scratchpad->sp_array,
@@ -484,9 +487,9 @@ int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id) }/* Allocate endpoint 0 ring */ - virt_dev->eps[0 ].ring =xhci_ring_alloc (1 , true); + virt_dev->eps[0 ].ring =xhci_ring_alloc (ctrl,1 , true); - byte_64 =virt_to_phys (virt_dev->out_ctx->bytes); + byte_64 =xhci_virt_to_bus (ctrl, virt_dev->out_ctx->bytes);/* Point to output device context in dcbaa. */ ctrl->dcbaa->dev_context_ptrs[slot_id] =cpu_to_le64 (byte_64);
@@ -522,15 +525,15 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,return -ENOMEM ; } - val_64 =virt_to_phys (ctrl->dcbaa); + val_64 =xhci_virt_to_bus (ctrl, ctrl->dcbaa);/* Set the pointer in DCBAA register */ xhci_writeq (&hcor->or_dcbaap, val_64);/* Command ring control pointer register initialization */ - ctrl->cmd_ring =xhci_ring_alloc (1 , true); + ctrl->cmd_ring =xhci_ring_alloc (ctrl,1 , true);/* Set the address in the Command Ring Control register */ - trb_64 =virt_to_phys (ctrl->cmd_ring->first_seg->trbs); + trb_64 =xhci_virt_to_bus (ctrl, ctrl->cmd_ring->first_seg->trbs); val_64 =xhci_readq (&hcor->or_crcr); val_64 = (val_64 & (u64 )CMD_RING_RSVD_BITS ) | (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
@@ -551,7 +554,7 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr, ctrl->ir_set = &ctrl->run_regs->ir_set[0 ];/* Event ring does not maintain link TRB */ - ctrl->event_ring =xhci_ring_alloc (ERST_NUM_SEGS , false); + ctrl->event_ring =xhci_ring_alloc (ctrl,ERST_NUM_SEGS , false); ctrl->erst.entries =xhci_malloc (sizeof (struct xhci_erst_entry ) *ERST_NUM_SEGS );
@@ -560,8 +563,8 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,for (val =0 , seg = ctrl->event_ring->first_seg; val <ERST_NUM_SEGS ; val++) { - trb_64 =virt_to_phys (seg->trbs);struct xhci_erst_entry *entry = &ctrl->erst.entries[val]; + trb_64 =xhci_virt_to_bus (ctrl, seg->trbs); entry->seg_addr =cpu_to_le64 (trb_64); entry->seg_size =cpu_to_le32 (TRBS_PER_SEGMENT ); entry->rsvd =0 ;
@@ -570,7 +573,7 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,xhci_flush_cache ((uintptr_t )ctrl->erst.entries,ERST_NUM_SEGS *sizeof (struct xhci_erst_entry )); - deq =virt_to_phys (ctrl->event_ring->dequeue); + deq =xhci_virt_to_bus (ctrl, ctrl->event_ring->dequeue);/* Update HC event ring dequeue pointer */ xhci_writeq(&ctrl->ir_set->erst_dequeue,
@@ -585,7 +588,7 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,/* this is the event ring segment table pointer */ val_64 =xhci_readq (&ctrl->ir_set->erst_base); val_64 &=ERST_PTR_MASK ; - val_64 |=virt_to_phys (ctrl->erst.entries) & ~ERST_PTR_MASK ; + val_64 |=xhci_virt_to_bus (ctrl, ctrl->erst.entries) & ~ERST_PTR_MASK ;xhci_writeq (&ctrl->ir_set->erst_base, val_64);
@@ -848,7 +851,7 @@ void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,/* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */ ep0_ctx->ep_info2 |=cpu_to_le32 (MAX_BURST (0 ) |ERROR_COUNT (3 )); - trb_64 =virt_to_phys (virt_dev->eps[0 ].ring->first_seg->trbs); + trb_64 =xhci_virt_to_bus (ctrl, virt_dev->eps[0 ].ring->first_seg->trbs); ep0_ctx->deq =cpu_to_le64 (trb_64 | virt_dev->eps[0 ].ring->cycle_state);/*
drivers/usb/host/xhci-ring.c +7 -4
@@ -275,10 +275,13 @@ void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id, u32 ep_index, trb_type cmd) {u32 fields[4 ]; -u64 val_64 =virt_to_phys (ptr); +u64 val_64 =0 ;BUG_ON (prepare_ring (ctrl, ctrl->cmd_ring,EP_STATE_RUNNING )); +if (ptr) + val_64 =xhci_virt_to_bus (ctrl, ptr); + fields[0 ] =lower_32_bits (val_64); fields[1 ] =upper_32_bits (val_64); fields[2 ] =0 ;
@@ -401,7 +404,7 @@ void xhci_acknowledge_event(struct xhci_ctrl *ctrl)/* Inform the hardware */ xhci_writeq (&ctrl->ir_set->erst_dequeue, -virt_to_phys (ctrl->event_ring->dequeue) |ERST_EHB ); +xhci_virt_to_bus (ctrl, ctrl->event_ring->dequeue) |ERST_EHB ); }/**
@@ -579,7 +582,7 @@ int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,u64 addr;int ret;u32 trb_fields[4 ]; -u64 val_64 =virt_to_phys (buffer); +u64 val_64 =xhci_virt_to_bus (ctrl, buffer);debug ("dev=%p, pipe=%lx, buffer=%p, length=%d\n" , udev, pipe, buffer, length);
@@ -872,7 +875,7 @@ int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,if (length >0 ) {if (req->requesttype &USB_DIR_IN ) field |=TRB_DIR_IN ; - buf_64 =virt_to_phys (buffer); + buf_64 =xhci_virt_to_bus (ctrl, buffer); trb_fields[0 ] =lower_32_bits (buf_64); trb_fields[1 ] =upper_32_bits (buf_64);
drivers/usb/host/xhci.c +2 -2
@@ -604,7 +604,7 @@ static int xhci_set_configuration(struct usb_device *udev) ep_ctx[ep_index] =xhci_get_ep_ctx (ctrl, in_ctx, ep_index);/* Allocate the ep rings */ - virt_dev->eps[ep_index].ring =xhci_ring_alloc (1 , true); + virt_dev->eps[ep_index].ring =xhci_ring_alloc (ctrl,1 , true);if (!virt_dev->eps[ep_index].ring)return -ENOMEM ;
@@ -628,7 +628,7 @@ static int xhci_set_configuration(struct usb_device *udev)cpu_to_le32 (MAX_BURST (max_burst) |ERROR_COUNT (err_count)); - trb_64 =virt_to_phys (virt_dev->eps[ep_index].ring->enqueue); + trb_64 =xhci_virt_to_bus (ctrl, virt_dev->eps[ep_index].ring->enqueue); ep_ctx[ep_index]->deq =cpu_to_le64 (trb_64 | virt_dev->eps[ep_index].ring->cycle_state);
include/usb/xhci.h +21 -1
@@ -16,6 +16,7 @@#ifndef HOST_XHCI_H_ #define HOST_XHCI_H_ +#include <phys2bus.h> #include <reset.h> #include <asm/types.h> #include <asm/cache.h>
@@ -1250,7 +1251,8 @@ int xhci_check_maxpacket(struct usb_device *udev);void xhci_flush_cache (uintptr_t addr,u32 type_len);void xhci_inval_cache (uintptr_t addr,u32 type_len);void xhci_cleanup (struct xhci_ctrl *ctrl); -struct xhci_ring *xhci_ring_alloc (unsigned int num_segs,bool link_trbs); +struct xhci_ring *xhci_ring_alloc (struct xhci_ctrl *ctrl,unsigned int num_segs, +bool link_trbs);int xhci_alloc_virt_device (struct xhci_ctrl *ctrl,unsigned int slot_id);int xhci_mem_init (struct xhci_ctrl *ctrl,struct xhci_hccr *hccr,struct xhci_hcor *hcor);
@@ -1278,4 +1280,22 @@ extern struct dm_usb_ops xhci_usb_ops;struct xhci_ctrl *xhci_get_ctrl (struct usb_device *udev); +static inline dma_addr_t xhci_virt_to_bus (struct xhci_ctrl *ctrl,void *addr) +{ +#if CONFIG_IS_ENABLED (DM_DMA ) +return dev_phys_to_bus (ctrl->dev,virt_to_phys (addr)); +#else +return (dma_addr_t )virt_to_phys (addr); +#endif +} + +static inline void *xhci_bus_to_virt (struct xhci_ctrl *ctrl,dma_addr_t addr) +{ +#if CONFIG_IS_ENABLED (DM_DMA ) +return phys_to_virt (dev_bus_to_phys (ctrl->dev, addr)); +#else +return phys_to_virt ((phys_addr_t )addr); +#endif +} +#endif /* HOST_XHCI_H_ */
Signature · 1 line
2.29.2
PATCH v4 10/13 xhci: translate virtual addresses into the bus's address space
Quoted 11 lines · Nicolas Saenz Julienne
So far we've been content with passing physical addresses when configuring memory addresses into XHCI controllers, but not all platforms have buses with transparent mappings. Specifically the Raspberry Pi 4 might introduce an offset to memory accesses incoming from its PCIe port.
Introduce
xhci_virt_to_bus()andxhci_bus_to_virt()to cater with these limitations, and make sure we don't break non DM users.Signed-off-by Nicolas Saenz Julienne <[email protected]>Reviewed-by Simon Glass <[email protected]>
Thanks,
Stefan
Quoted 280 lines
---
Changes since v3:
- Don't call phys_to_bus()/bus_to_phys(), we only support DMdrivers/usb/host/xhci-mem.c | 45 +++++++++++++++++++----------------- drivers/usb/host/xhci-ring.c | 11 +++++---- drivers/usb/host/xhci.c | 4 ++-- include/usb/xhci.h | 22 +++++++++++++++++- 4 files changed, 54 insertions(+), 28 deletions(-)drivers/usb/host/xhci-mem.c +24
-21@@ -110,7 +110,7 @@ static void xhci_scratchpad_free(struct xhci_ctrl *ctrl) ctrl->dcbaa->dev_context_ptrs[0 ] =0 ; -free ((void *)(uintptr_t )le64_to_cpu (ctrl->scratchpad->sp_array[0 ])); +free (xhci_bus_to_virt (ctrl,le64_to_cpu (ctrl->scratchpad->sp_array[0 ])));free (ctrl->scratchpad->sp_array);free (ctrl->scratchpad); ctrl->scratchpad =NULL ;@@ -216,8 +216,8 @@ static void *xhci_malloc(unsigned int size)* @param link_trbs flag to indicate whether to link the trbs or NOT * @return none */ -static void xhci_link_segments (struct xhci_segment *prev, -struct xhci_segment *next,bool link_trbs) +static void xhci_link_segments (struct xhci_ctrl *ctrl,struct xhci_segment *prev, +struct xhci_segment *next,bool link_trbs) {u32 val;u64 val_64 =0 ;@@ -226,7 +226,7 @@ static void xhci_link_segments(struct xhci_segment *prev,return ; prev->next = next;if (link_trbs) { - val_64 =virt_to_phys (next->trbs); + val_64 =xhci_virt_to_bus (ctrl, next->trbs); prev->trbs[TRBS_PER_SEGMENT -1 ].link.segment_ptr =cpu_to_le64 (val_64);@@ -304,7 +304,8 @@ static struct xhci_segment *xhci_segment_alloc(void)* @param link_trbs flag to indicate whether to link the trbs or NOT * @return pointer to the newly created RING */ -struct xhci_ring *xhci_ring_alloc (unsigned int num_segs,bool link_trbs) +struct xhci_ring *xhci_ring_alloc (struct xhci_ctrl *ctrl,unsigned int num_segs, +bool link_trbs) {struct xhci_ring *ring;struct xhci_segment *prev;@@ -327,12 +328,12 @@ struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs) next =xhci_segment_alloc ();BUG_ON (!next); -xhci_link_segments (prev, next, link_trbs); +xhci_link_segments (ctrl, prev, next, link_trbs); prev = next; num_segs--; } -xhci_link_segments (prev, ring->first_seg, link_trbs); +xhci_link_segments (ctrl, prev, ring->first_seg, link_trbs);if (link_trbs) {/* See section 4.9.2.1 and 6.4.4.1 */ prev->trbs[TRBS_PER_SEGMENT-1].link.control |=@@ -354,6 +355,7 @@ static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)struct xhci_hccr *hccr = ctrl->hccr;struct xhci_hcor *hcor = ctrl->hcor;struct xhci_scratchpad *scratchpad; +uint64_t val_64;int num_sp;uint32_t page_size;void *buf;@@ -371,8 +373,9 @@ static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl) scratchpad->sp_array =xhci_malloc (num_sp *sizeof (u64));if (!scratchpad->sp_array) goto fail_sp2; - ctrl->dcbaa->dev_context_ptrs[0 ] = -cpu_to_le64 ((uintptr_t )scratchpad->sp_array); + + val_64 =xhci_virt_to_bus (ctrl, scratchpad->sp_array); + ctrl->dcbaa->dev_context_ptrs[0 ] =cpu_to_le64 (val_64);xhci_flush_cache ((uintptr_t )&ctrl->dcbaa->dev_context_ptrs[0 ],sizeof (ctrl->dcbaa->dev_context_ptrs[0 ]));@@ -393,8 +396,8 @@ static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)xhci_flush_cache ((uintptr_t )buf, num_sp * page_size);for (i =0 ; i < num_sp; i++) { -uintptr_t ptr = (uintptr_t )buf + i * page_size; - scratchpad->sp_array[i] =cpu_to_le64 (ptr); + val_64 =xhci_virt_to_bus (ctrl, buf + i * page_size); + scratchpad->sp_array[i] =cpu_to_le64 (val_64); } xhci_flush_cache((uintptr_t)scratchpad->sp_array,@@ -484,9 +487,9 @@ int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id) }/* Allocate endpoint 0 ring */ - virt_dev->eps[0 ].ring =xhci_ring_alloc (1 , true); + virt_dev->eps[0 ].ring =xhci_ring_alloc (ctrl,1 , true); - byte_64 =virt_to_phys (virt_dev->out_ctx->bytes); + byte_64 =xhci_virt_to_bus (ctrl, virt_dev->out_ctx->bytes);/* Point to output device context in dcbaa. */ ctrl->dcbaa->dev_context_ptrs[slot_id] =cpu_to_le64 (byte_64);@@ -522,15 +525,15 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,return -ENOMEM ; } - val_64 =virt_to_phys (ctrl->dcbaa); + val_64 =xhci_virt_to_bus (ctrl, ctrl->dcbaa);/* Set the pointer in DCBAA register */ xhci_writeq (&hcor->or_dcbaap, val_64);/* Command ring control pointer register initialization */ - ctrl->cmd_ring =xhci_ring_alloc (1 , true); + ctrl->cmd_ring =xhci_ring_alloc (ctrl,1 , true);/* Set the address in the Command Ring Control register */ - trb_64 =virt_to_phys (ctrl->cmd_ring->first_seg->trbs); + trb_64 =xhci_virt_to_bus (ctrl, ctrl->cmd_ring->first_seg->trbs); val_64 =xhci_readq (&hcor->or_crcr); val_64 = (val_64 & (u64 )CMD_RING_RSVD_BITS ) | (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |@@ -551,7 +554,7 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr, ctrl->ir_set = &ctrl->run_regs->ir_set[0 ];/* Event ring does not maintain link TRB */ - ctrl->event_ring =xhci_ring_alloc (ERST_NUM_SEGS , false); + ctrl->event_ring =xhci_ring_alloc (ctrl,ERST_NUM_SEGS , false); ctrl->erst.entries =xhci_malloc (sizeof (struct xhci_erst_entry ) *ERST_NUM_SEGS );@@ -560,8 +563,8 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,for (val =0 , seg = ctrl->event_ring->first_seg; val <ERST_NUM_SEGS ; val++) { - trb_64 =virt_to_phys (seg->trbs);struct xhci_erst_entry *entry = &ctrl->erst.entries[val]; + trb_64 =xhci_virt_to_bus (ctrl, seg->trbs); entry->seg_addr =cpu_to_le64 (trb_64); entry->seg_size =cpu_to_le32 (TRBS_PER_SEGMENT ); entry->rsvd =0 ;@@ -570,7 +573,7 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,xhci_flush_cache ((uintptr_t )ctrl->erst.entries,ERST_NUM_SEGS *sizeof (struct xhci_erst_entry )); - deq =virt_to_phys (ctrl->event_ring->dequeue); + deq =xhci_virt_to_bus (ctrl, ctrl->event_ring->dequeue);/* Update HC event ring dequeue pointer */ xhci_writeq(&ctrl->ir_set->erst_dequeue,@@ -585,7 +588,7 @@ int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,/* this is the event ring segment table pointer */ val_64 =xhci_readq (&ctrl->ir_set->erst_base); val_64 &=ERST_PTR_MASK ; - val_64 |=virt_to_phys (ctrl->erst.entries) & ~ERST_PTR_MASK ; + val_64 |=xhci_virt_to_bus (ctrl, ctrl->erst.entries) & ~ERST_PTR_MASK ;xhci_writeq (&ctrl->ir_set->erst_base, val_64);@@ -848,7 +851,7 @@ void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,/* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */ ep0_ctx->ep_info2 |=cpu_to_le32 (MAX_BURST (0 ) |ERROR_COUNT (3 )); - trb_64 =virt_to_phys (virt_dev->eps[0 ].ring->first_seg->trbs); + trb_64 =xhci_virt_to_bus (ctrl, virt_dev->eps[0 ].ring->first_seg->trbs); ep0_ctx->deq =cpu_to_le64 (trb_64 | virt_dev->eps[0 ].ring->cycle_state);/* drivers/usb/host/xhci-ring.c +7
-4@@ -275,10 +275,13 @@ void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id, u32 ep_index, trb_type cmd) {u32 fields[4 ]; -u64 val_64 =virt_to_phys (ptr); +u64 val_64 =0 ;BUG_ON (prepare_ring (ctrl, ctrl->cmd_ring,EP_STATE_RUNNING )); +if (ptr) + val_64 =xhci_virt_to_bus (ctrl, ptr); + fields[0 ] =lower_32_bits (val_64); fields[1 ] =upper_32_bits (val_64); fields[2 ] =0 ;@@ -401,7 +404,7 @@ void xhci_acknowledge_event(struct xhci_ctrl *ctrl)/* Inform the hardware */ xhci_writeq (&ctrl->ir_set->erst_dequeue, -virt_to_phys (ctrl->event_ring->dequeue) |ERST_EHB ); +xhci_virt_to_bus (ctrl, ctrl->event_ring->dequeue) |ERST_EHB ); }/** @@ -579,7 +582,7 @@ int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,u64 addr;int ret;u32 trb_fields[4 ]; -u64 val_64 =virt_to_phys (buffer); +u64 val_64 =xhci_virt_to_bus (ctrl, buffer);debug ("dev=%p, pipe=%lx, buffer=%p, length=%d\n" , udev, pipe, buffer, length);@@ -872,7 +875,7 @@ int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,if (length >0 ) {if (req->requesttype &USB_DIR_IN ) field |=TRB_DIR_IN ; - buf_64 =virt_to_phys (buffer); + buf_64 =xhci_virt_to_bus (ctrl, buffer); trb_fields[0 ] =lower_32_bits (buf_64); trb_fields[1 ] =upper_32_bits (buf_64);drivers/usb/host/xhci.c +2
-2@@ -604,7 +604,7 @@ static int xhci_set_configuration(struct usb_device *udev) ep_ctx[ep_index] =xhci_get_ep_ctx (ctrl, in_ctx, ep_index);/* Allocate the ep rings */ - virt_dev->eps[ep_index].ring =xhci_ring_alloc (1 , true); + virt_dev->eps[ep_index].ring =xhci_ring_alloc (ctrl,1 , true);if (!virt_dev->eps[ep_index].ring)return -ENOMEM ;@@ -628,7 +628,7 @@ static int xhci_set_configuration(struct usb_device *udev)cpu_to_le32 (MAX_BURST (max_burst) |ERROR_COUNT (err_count)); - trb_64 =virt_to_phys (virt_dev->eps[ep_index].ring->enqueue); + trb_64 =xhci_virt_to_bus (ctrl, virt_dev->eps[ep_index].ring->enqueue); ep_ctx[ep_index]->deq =cpu_to_le64 (trb_64 | virt_dev->eps[ep_index].ring->cycle_state);include/usb/xhci.h +12
-1@@ -16,6 +16,7 @@#ifndef HOST_XHCI_H_ #define HOST_XHCI_H_ +#include <phys2bus.h> #include <reset.h> #include <asm/types.h> #include <asm/cache.h> @@ -1250,7 +1251,8 @@ int xhci_check_maxpacket(struct usb_device *udev);void xhci_flush_cache (uintptr_t addr,u32 type_len);void xhci_inval_cache (uintptr_t addr,u32 type_len);void xhci_cleanup (struct xhci_ctrl *ctrl); -struct xhci_ring *xhci_ring_alloc (unsigned int num_segs,bool link_trbs); +struct xhci_ring *xhci_ring_alloc (struct xhci_ctrl *ctrl,unsigned int num_segs, +bool link_trbs);int xhci_alloc_virt_device (struct xhci_ctrl *ctrl,unsigned int slot_id);int xhci_mem_init (struct xhci_ctrl *ctrl,struct xhci_hccr *hccr,struct xhci_hcor *hcor);@@ -1278,4 +1280,22 @@ extern struct dm_usb_ops xhci_usb_ops;struct xhci_ctrl *xhci_get_ctrl (struct usb_device *udev); +static inline dma_addr_t xhci_virt_to_bus (struct xhci_ctrl *ctrl,void *addr) +{ +#if CONFIG_IS_ENABLED (DM_DMA ) +return dev_phys_to_bus (ctrl->dev,virt_to_phys (addr)); +#else +return (dma_addr_t )virt_to_phys (addr); +#endif +} +
include/usb/xhci.h extern struct dm_usb_ops xhci_usb_ops; · line 1301
Quoted 10 lines
include/usb/xhci.h +9
-0+static inline void *xhci_bus_to_virt (struct xhci_ctrl *ctrl,dma_addr_t addr) +{ +#if CONFIG_IS_ENABLED (DM_DMA ) +return phys_to_virt (dev_bus_to_phys (ctrl->dev, addr)); +#else +return phys_to_virt ((phys_addr_t )addr); +#endif +} +#endif /* HOST_XHCI_H_ */
Viele Gr??e,
Stefan
Signature · 3 lines
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
PATCH v4 11/13 mmc: Introduce mmc_phys_to_bus()/mmc_bus_to_phys()
In reply to Nicolas Saenz JulienneThis will allow us to use DM variants of phys_to_bus()/bus_to_phys() when relevant.
---
Changes since v3
- Don't call phys_to_bus()/bus_to_phys(), we only support DM
drivers/mmc/sdhci.c | 7 ++++--- include/mmc.h | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-)
drivers/mmc/sdhci.c +4 -3
@@ -19,7 +19,6 @@#include <linux/bitops.h> #include <linux/delay.h> #include <linux/dma-mapping.h> -#include <phys2bus.h> static void sdhci_reset (struct sdhci_host *host,u8 mask) {
@@ -103,7 +102,8 @@ static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data, mmc_get_dma_dir(data));if (host->flags &USE_SDMA ) { -sdhci_writel (host,phys_to_bus ((ulong )host->start_addr), +sdhci_writel (host, +mmc_phys_to_bus (host->mmc, (ulong )host->start_addr),SDHCI_DMA_ADDRESS ); }#if CONFIG_IS_ENABLED (MMC_SDHCI_ADMA )
@@ -162,7 +162,8 @@ static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data) start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE -1 ); start_addr +=SDHCI_DEFAULT_BOUNDARY_SIZE ; -sdhci_writel (host,phys_to_bus ((ulong )start_addr), +sdhci_writel (host, +mmc_phys_to_bus (host->mmc, (ulong )start_addr),SDHCI_DMA_ADDRESS ); } }
include/mmc.h +10 -0
@@ -15,6 +15,7 @@#include <linux/compiler.h> #include <linux/dma-direction.h> #include <part.h> +#include <phys2bus.h> struct bd_info ;
@@ -977,4 +978,13 @@ static inline enum dma_data_direction mmc_get_dma_dir(struct mmc_data *data) return data->flags & MMC_DATA_WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE; } +static inline dma_addr_t mmc_phys_to_bus (struct mmc *mmc,phys_addr_t addr) +{ +#if CONFIG_IS_ENABLED (DM_DMA ) +return dev_phys_to_bus (mmc->dev, addr); +#else +return (dma_addr_t )addr; +#endif +} +#endif /* _MMC_H_ */
Signature · 1 line
2.29.2
PATCH v4 12/13 configs: rpi4: Enable DM_DMA across all RPi4 configurations
In reply to Nicolas Saenz JulienneThe DM_DMA option is needed in order to translate physical address into bus addresses on a per-device basis.
--- configs/rpi_4_32b_defconfig | 1 + configs/rpi_4_defconfig | 1 + configs/rpi_arm64_defconfig | 1 + 3 files changed, 3 insertions(+)
configs/rpi_4_32b_defconfig +1 -0
@@ -22,6 +22,7 @@ CONFIG_OF_BOARD=y CONFIG_ENV_FAT_DEVICE_AND_PART="0:1" CONFIG_SYS_RELOC_GD_ENV_ADDR=y CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG=y +CONFIG_DM_DMA=y CONFIG_DFU_MMC=y CONFIG_DM_KEYBOARD=y CONFIG_DM_MMC=y
configs/rpi_4_defconfig +1 -0
@@ -22,6 +22,7 @@ CONFIG_OF_BOARD=y CONFIG_ENV_FAT_DEVICE_AND_PART="0:1" CONFIG_SYS_RELOC_GD_ENV_ADDR=y CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG=y +CONFIG_DM_DMA=y CONFIG_DFU_MMC=y CONFIG_DM_KEYBOARD=y CONFIG_DM_MMC=y
configs/rpi_arm64_defconfig +1 -0
@@ -20,6 +20,7 @@ CONFIG_CMD_FS_UUID=y CONFIG_OF_BOARD=y CONFIG_ENV_FAT_DEVICE_AND_PART="0:1" CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG=y +CONFIG_DM_DMA=y CONFIG_DM_KEYBOARD=y CONFIG_DM_MMC=y CONFIG_MMC_SDHCI=y
Signature · 1 line
2.29.2
PATCH v4 13/13 video: arm: rpi: Add brcm,bcm2711-hdmi0 compatible
In reply to Nicolas Saenz JulienneThe 'brcm,bcm2711-hdmi0' compatible string is used on RPi4 instead of 'brcm,bcm2835-hdmi' since the IP core was upgraded (now called VC6 instead of VC4). This has no functional change as far as u-boot driver is concerned. So simply add the compatible string.
---
Changes since v3:
- Add this patch
drivers/video/bcm2835.c | 1 + 1 file changed, 1 insertion(+)
drivers/video/bcm2835.c +1 -0
@@ -52,6 +52,7 @@ static int bcm2835_video_probe(struct udevice *dev)static const struct udevice_id bcm2835_video_ids[] = { { .compatible ="brcm,bcm2835-hdmi" }, + { .compatible ="brcm,bcm2711-hdmi0" }, { .compatible ="brcm,bcm2708-fb" }, { } };
Signature · 1 line
2.29.2
v4 cover Raspberry Pi 400/Compute Module 4 support
In reply to Nicolas Saenz JulienneHi Nicolas,
Quoted 6 lines · Nicolas Saenz Julienne
@Simon: I had to roll back all "if (
CONFIG_IS_ENABLED(DM_DMA)" uses. As per buildman it breaks compilation when !DM or !DM_DMA due to access to undefined variables, namely dev anddev->dma_offset. I figure in u-boot we root for small Image sizes, so defining the variables regarless of them being needed is not an option.
Our approach so far is to define inline functions to get/set the variables, which do nothing if those vars don't exist. See gd_of_root() for example.
Quoted 110 lines
---
This series could be split into at least two or even three parts, but I kept it as is for now as it contains all the changes needed in order to have u-boot working on the new Raspberry Pi 400 and Raspberry Pi Compute Module 4.
There are core changes, specifically with regard to cpu to bus address space translations. So far we had relied on hard-coded values but RPi needs per device translations as it has at least three distinct bus address spaces with different offsets. So it's a good opportunity to implement bus translations the right way by parsing DT's dma-ranges.
Here's a more concrete example of what we're dealing with:
- On a RPi4, SoC version BCM2711C0 with 8GB of memory
[
0x00x200000000] [0x2000000000x400000000] [0xc00000000x100000000] [0x000000000x100000000]
phys/cpu address PCIe bus address Legacy peripheral emmc2 address
space space address space space- On a RPi4, SoC version BCM2711C0 with 4GB of memory
[
0x00x100000000] [0x1000000000x200000000] [0xc00000000x100000000] [0x000000000x100000000]
phys/cpu address PCIe bus address Legacy peripheral emmc2 address
space space address space space- On a RPi4, SoC version BCM2711B0 with 8GB of memory (bus can only access the lower 3GB of memory because of a SoC routing bug)
[
0x00x200000000] [0x000000000xC0000000] [0xc00000000x100000000]
phys/cpu address PCIe bus address Legacy peripheral
space space address space---
Changes since v3:
- Add commit "video: arm: rpi: Add brcm,bcm2711-hdmi0 compatible", it's pretty harmless
- Get rid of non-device based phys2bus call in xhci & mmc
- Simon's review changes in tests
- Comment some of the APIsChanges since v2:
- Test builds not broken with buildman
- Add tests to all DM changes
- Make code conditional with config option
- Correct OF refcount
- Add config changes
- Address small changes as per reviewsChanges since v1:
- Fix some issues in 'dm: Introducexxx_get_dma_range()'
- Fix some typos in commit messages
- Change DTB file name for RPi400
- Address Matthias' commentsNicolas Saenz Julienne (13):
rpi: Add identifier for the new RPi400
rpi: Add identifier for the new CM4
pci: pcie-brcmstb: Fix inbound window configurations
dm: Introducexxx_get_dma_range()
dm: test: Add test case fordev_get_dma_ranges()
dm: Introduce DMA constraints into the core device model
dm: test: Add test case fordev->dma_offset
dm: Introduce dev_phys_to_bus()/dev_bus_to_phys()
dm: test: Add test case for dev_phys_to_bus()/dev_bus_to_phys()
xhci: translate virtual addresses into the bus's address space
mmc: Introduce mmc_phys_to_bus()/mmc_bus_to_phys()
configs: rpi4: EnableDM_DMAacross all RPi4 configurations
video: arm: rpi: Add brcm,bcm2711-hdmi0 compatiblearch/sandbox/dts/test.dts | 21 ++++++++ board/raspberrypi/rpi/rpi.c | 10 ++++ common/fdt_support.c | 73 ++++++++++++++++++++++++++++ configs/rpi_4_32b_defconfig | 1 + configs/rpi_4_defconfig | 1 + configs/rpi_arm64_defconfig | 1 + configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + configs/sandbox_flattree_defconfig | 1 + configs/sandbox_spl_defconfig | 1 + drivers/core/Kconfig | 10 ++++ drivers/core/device.c | 45 +++++++++++++++++ drivers/core/of_addr.c | 78 ++++++++++++++++++++++++++++++ drivers/core/ofnode.c | 9 ++++ drivers/core/read.c | 6 +++ drivers/mmc/sdhci.c | 7 +-- drivers/pci/pcie_brcmstb.c | 12 ++--- drivers/usb/host/xhci-mem.c | 45 +++++++++-------- drivers/usb/host/xhci-ring.c | 11 +++-- drivers/usb/host/xhci.c | 4 +- drivers/video/bcm2835.c | 1 + include/dm/device.h | 5 ++ include/dm/of_addr.h | 17 +++++++ include/dm/ofnode.h | 16 ++++++ include/dm/read.h | 21 ++++++++ include/fdt_support.h | 14 ++++++ include/mmc.h | 10 ++++ include/phys2bus.h | 16 ++++++ include/usb/xhci.h | 22 ++++++++- test/dm/Makefile | 2 + test/dm/core.c | 30 ++++++++++++ test/dm/phys2bus.c | 37 ++++++++++++++ test/dm/read.c | 49 +++++++++++++++++++ 33 files changed, 540 insertions(+), 38 deletions(-) create mode 100644 test/dm/phys2bus.c create mode 100644 test/dm/read.c
Quoted 3 lines
--
2.29.2
Regards,
Simon