xen/arm: vpci: remove PCI I/O ranges property value
PATCH v2 1/1 xen/arm: vpci: remove PCI I/O ranges property value
PCI I/O space are not mapped to dom0 when PCI passthrough is enabled, also there is no vpci trap handler register for IO bar.
Remove PCI I/O ranges property value from dom0 device tree node so that dom0 linux will not allocate I/O space for PCI devices if pci-passthrough is enabled.
--- xen/arch/arm/domain_build.c | 29 +++++++++++++++ xen/common/device_tree.c | 69 +++++++++++++++++++++++++++++++++++ xen/include/xen/device_tree.h | 10 +++++ 3 files changed, 108 insertions(+)
xen/arch/arm/domain_build.c +29 -0
@@ -648,6 +648,31 @@ static void __init allocate_static_memory(struct domain *d, }#endif +/* +* PCI IO bar are not mapped to dom0 when PCI passthrough is enabled, also +* there is no trap handler registered for IO bar, therefore remove the IO +* range property from the device tree node for dom0. +*/ +static int handle_linux_pci_io_ranges (struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +if ( !is_pci_passthrough_enabled () ) +return 0 ; + +if ( !dt_device_type_is_equal (node,"pci" ) ) +return 0 ; + +/* +* The current heuristic assumes that a device is a host bridge +* if the type is "pci" and then parent type is not "pci". +*/ +if ( node->parent &&dt_device_type_is_equal (node->parent,"pci" ) ) +return 0 ; + +return dt_pci_remove_io_ranges (kinfo->fdt, node); +} + +/* * When PCI passthrough is available we want to keep the * "linux,pci-domain" in sync for every host bridge.
@@ -723,6 +748,10 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,if ( iommu_node &&device_get_class (iommu_node) !=DEVICE_IOMMU ) iommu_node =NULL ; + res =handle_linux_pci_io_ranges (kinfo, node); +if ( res ) +return res; + dt_for_each_property_node (node, prop) {const void *prop_data = prop->value;
xen/common/device_tree.c +69 -0
75 lines not on this page · raw message
xen/include/xen/device_tree.h +10 -0
@@ -849,6 +849,16 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,*/ int dt_get_pci_domain_nr (struct dt_device_node *node); +/** +* dt_pci_remove_io_range - Remove the PCI I/O range property value. +* @fdt: Pointer to the file descriptor tree. +* @node: Device tree node. +* +* This function will remove the PCI IO range property from the PCI device tree +* node. +*/ +int dt_pci_remove_io_ranges (void *fdt,const struct dt_device_node *node); +struct dt_device_node *dt_find_node_by_phandle (dt_phandle handle);#ifdef CONFIG_DEVICE_TREE_DEBUG
Signature · 1 line
2.25.1
Hi Rahul,
Quoted 7 lines · Rahul Singh
PCI I/O space are not mapped to dom0 when PCI passthrough is enabled, also there is no vpci trap handler register for IO bar.
Remove PCI I/O ranges property value from dom0 device tree node so that dom0 linux will not allocate I/O space for PCI devices if pci-passthrough is enabled.
Quoted 6 lines
Signed-off-by Rahul Singh <[email protected]>--- xen/arch/arm/domain_build.c | 29 +++++++++++++++ xen/common/device_tree.c | 69 +++++++++++++++++++++++++++++++++++ xen/include/xen/device_tree.h | 10 +++++ 3 files changed, 108 insertions(+)
For future version, please add a changelog. This helps to figure out what changed more easily.
Quoted 19 lines
xen/arch/arm/domain_build.c +10
-0@@ -648,6 +648,31 @@ static void __init allocate_static_memory(struct domain *d, }#endif +/* +* PCI IO bar are not mapped to dom0 when PCI passthrough is enabled, also +* there is no trap handler registered for IO bar, therefore remove the IO +* range property from the device tree node for dom0. +*/ +static int handle_linux_pci_io_ranges (struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +if ( !is_pci_passthrough_enabled () ) +return 0 ;
xen/arch/arm/domain_build.c static void __init allocate_static_memory(struct domain *d, · line 670
Quoted 10 lines
xen/arch/arm/domain_build.c +10
-0+ +if ( !dt_device_type_is_equal (node,"pci" ) ) +return 0 ; + +/* +* The current heuristic assumes that a device is a host bridge +* if the type is "pci" and then parent type is not "pci". +*/ +if ( node->parent &&dt_device_type_is_equal (node->parent,"pci" ) ) +return 0 ;
The logic above is exactly the same as in handle_linux_pci_domain(). Can we create an helper that could be used by both functions? This would help to keep the logic synchronized.
xen/common/device_tree.c
Quoted 23 lines
xen/arch/arm/domain_build.c +9
-0+ +return dt_pci_remove_io_ranges (kinfo->fdt, node); +} + +/* * When PCI passthrough is available we want to keep the * "linux,pci-domain" in sync for every host bridge. @@ -723,6 +748,10 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,if ( iommu_node &&device_get_class (iommu_node) !=DEVICE_IOMMU ) iommu_node =NULL ; + res =handle_linux_pci_io_ranges (kinfo, node); +if ( res ) +return res; + dt_for_each_property_node (node, prop) {const void *prop_data = prop->value;xen/common/device_tree.c +0
-0
If I am not mistaken, the file common/device_tree.c is so far only containing code to parse the host device-tree. But now...
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2198
Quoted 5 lines
xen/common/device_tree.c +1
-0@@ -2195,6 +2195,75 @@ int dt_get_pci_domain_nr(struct dt_device_node *node)return (u16 )domain; } +int dt_pci_remove_io_ranges (void *fdt,const struct dt_device_node *dev)
you are introducing code to write the domain device-tree. I understand this is because dt_match_bus() is internal. However, I would rather prefer if we export dt_match_bus() & co and move this code to under arch/arm/pci/. Maybe we should introduce a file domain_build.c.
Furthermore, the name of the function doesn't really match what the function does. It will generate "ranges" for the hostbridge and remove the I/O. We may want to perform other modifications on the range. So I would name the function something like:
domain_build_generate_hostbridge_range()
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2205
Quoted 6 lines
xen/common/device_tree.c +6
-0+const struct dt_device_node *parent =NULL ; +const struct dt_bus *bus, *pbus; +unsigned int rlen; +int na, ns, pna, pns, rone; +const __be32 *ranges; + __be32 regs[((GUEST_ROOT_ADDRESS_CELLS * 2) + GUEST_ROOT_SIZE_CELLS + 1)
GUEST_ROOT_*_CELLS are only valid for domU. In theory, there are no guarantee this will be bigger that what the host device-tree supports.
So you want to use DT_MAX_ADDR_CELLS here.
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2206
Quoted 1 line
xen/common/device_tree.c +1
-0+* 2];
Looking at the code below. I couldn't find any check guaranteing the static array will be big enough to store the ranges provided by the host DT.
Quoted 38 lines
xen/common/device_tree.c +38
-0+__be32 *addr = ®s[0 ]; + + bus =dt_match_bus (dev); +if ( !bus ) +return 0 ;/* device is not a bus */ + + parent =dt_get_parent (dev); +if ( !parent ) +return -EINVAL ; + + ranges =dt_get_property (dev,"ranges" , &rlen); +if ( !ranges ) + { +printk (XENLOG_ERR "DT: no ranges; cannot enumerate %s\n" , + dev->full_name); +return -EINVAL ; + } +if ( !rlen )/* Nothing to do */ +return 0 ; + + bus->count_cells (dev, &na, &ns); +if ( !DT_CHECK_COUNTS (na, ns) ) + { +printk (XENLOG_ERR "dt_parse: Bad cell count for device %s\n" , + dev->full_name); +return -EINVAL ; + } + + pbus =dt_match_bus (parent); +if ( !pbus ) + { +printk (XENLOG_ERR "DT: %s is not a valid bus\n" , parent->full_name); +return -EINVAL ; + } + + pbus->count_cells (dev, &pna, &pns); +if ( !DT_CHECK_COUNTS (pna, pns) ) + {
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2254
Quoted 10 lines
xen/common/device_tree.c +10
-0+printk (XENLOG_ERR "dt_parse: Bad cell count for parent %s\n" , + dev->full_name); +return -EINVAL ; + } + +/* Now walk through the ranges */ + rlen /=4 ; + rone = na + pna + ns; +for ( ; rlen >= rone; rlen -= rone, ranges += rone ) + {
Most of the code in this function is the same as dt_for_each_range(). Can we refactor it to avoid code duplication?
Quoted 26 lines
xen/common/device_tree.c +12
-0+unsigned int flags = bus->get_flags (ranges); +if ( flags &IORESOURCE_IO ) +continue ; + +memcpy (addr, ranges,4 * rone); + + addr += rone; + } + +return fdt_property (fdt,"ranges" , regs,sizeof (regs)); +} +/* * Local variables: * mode: C xen/include/xen/device_tree.h +3
-0@@ -849,6 +849,16 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,*/ int dt_get_pci_domain_nr (struct dt_device_node *node); +/** +* dt_pci_remove_io_range - Remove the PCI I/O range property value. +* @fdt: Pointer to the file descriptor tree.
xen/include/xen/device_tree.h int dt_count_phandle_with_args(const struct dt_device_node *np, · line 864
Quoted 10 lines
xen/include/xen/device_tree.h +7
-0+* @node: Device tree node. +* +* This function will remove the PCI IO range property from the PCI device tree +* node. +*/ +int dt_pci_remove_io_ranges (void *fdt,const struct dt_device_node *node); +struct dt_device_node *dt_find_node_by_phandle (dt_phandle handle);#ifdef CONFIG_DEVICE_TREE_DEBUG
Cheers,
Signature · 1 line
Julien Grall
Hi Julien,
Thanks for reviewing the code.
Quoted 17 lines · Julien Grall
Hi Rahul,
Quoted 11 lines · Rahul Singh
PCI I/O space are not mapped to dom0 when PCI passthrough is enabled, also there is no vpci trap handler register for IO bar. Remove PCI I/O ranges property value from dom0 device tree node so that dom0 linux will not allocate I/O space for PCI devices if pci-passthrough is enabled.
Signed-off-by Rahul Singh <[email protected]>--- xen/arch/arm/domain_build.c | 29 +++++++++++++++ xen/common/device_tree.c | 69 +++++++++++++++++++++++++++++++++++ xen/include/xen/device_tree.h | 10 +++++ 3 files changed, 108 insertions(+)
Quoted 1 line
For future version, please add a changelog. This helps to figure out what changed more easily.
Ok. I will add the changelog in next version.
Quoted 23 lines
Quoted 23 lines
xen/arch/arm/domain_build.c +14
-0@@ -648,6 +648,31 @@ static void __init allocate_static_memory(struct domain *d, }#endif +/* +* PCI IO bar are not mapped to dom0 when PCI passthrough is enabled, also +* there is no trap handler registered for IO bar, therefore remove the IO +* range property from the device tree node for dom0. +*/ +static int handle_linux_pci_io_ranges(struct kernel_info *kinfo, +const struct dt_device_node *node) +{ +if ( !is_pci_passthrough_enabled () ) +return 0 ; + +if ( !dt_device_type_is_equal (node,"pci" ) ) +return 0 ; + +/*
Quoted 8 lines
xen/arch/arm/domain_build.c static void __init allocate_static_memory(struct domain *d, · line 670
Quoted 5 lines
xen/arch/arm/domain_build.c +5
-0+* The current heuristic assumes that a device is a host bridge +* if the type is "pci" and then parent type is not "pci". +*/ +if ( node->parent &&dt_device_type_is_equal (node->parent,"pci" ) ) +return 0 ;The logic above is exactly the same as in
handle_linux_pci_domain(). Can we create an helper that could be used by both functions? This would help to keep the logic synchronized.
Ok. I will create the helper name “dt_node_check_pci_hostbridge(..)”.
Quoted 33 lines
xen/common/device_tree.c
Quoted 23 lines
xen/arch/arm/domain_build.c +8
-0+ +return dt_pci_remove_io_ranges (kinfo->fdt, node); +} + +/* * When PCI passthrough is available we want to keep the * "linux,pci-domain" in sync for every host bridge. @@ -723,6 +748,10 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,if ( iommu_node &&device_get_class (iommu_node) !=DEVICE_IOMMU ) iommu_node =NULL ; + res =handle_linux_pci_io_ranges (kinfo, node); +if ( res ) +return res; + dt_for_each_property_node (node, prop) {const void *prop_data = prop->value;xen/common/device_tree.c +0
-0If I am not mistaken, the file
common/device_tree.cis so far only containing code to parse the host device-tree. But now...xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2197
Quoted 4 lines
xen/common/device_tree.c +0
-0@@ -2195,6 +2195,75 @@ int dt_get_pci_domain_nr(struct dt_device_node *node)return (u16 )domain; } +int dt_pci_remove_io_ranges(void *fdt, const struct dt_device_node *dev)you are introducing code to write the domain device-tree. I understand this is because
dt_match_bus()is internal. However, I would rather prefer if we exportdt_match_bus()& co and move this code to underarch/arm/pci/. Maybe we should introduce a filedomain_build.c.
Quoted 3 lines
Furthermore, the name of the function doesn't really match what the function does. It will generate "ranges" for the hostbridge and remove the I/O. We may want to perform other modifications on the range. So I would name the function something like:
domain_build_generate_hostbridge_range()
I will modify the code based on your comment in next version.
Quoted 8 lines
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2205
Quoted 7 lines
xen/common/device_tree.c +6
-0+const struct dt_device_node *parent =NULL ; +const struct dt_bus *bus, *pbus; +unsigned int rlen; +int na, ns, pna, pns, rone; +const __be32 *ranges; + __be32 regs[((GUEST_ROOT_ADDRESS_CELLS * 2) + GUEST_ROOT_SIZE_CELLS + 1)
Quoted 6 lines
GUEST_ROOT_*_CELLS are only valid for domU. In theory, there are no guarantee this will be bigger that what the host device-tree supports.
So you want to use
DT_MAX_ADDR_CELLShere.xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2206
Quoted 1 line
xen/common/device_tree.c +1
-0+* 2]; Looking at the code below. I couldn't find any check guaranteing the static array will be big enough to store the ranges provided by the host DT.
Let me fix this in next version.
Quoted 43 lines
Quoted 43 lines
xen/common/device_tree.c +42
-0+__be32 *addr = ®s[0 ]; + + bus =dt_match_bus (dev); +if ( !bus ) +return 0 ;/* device is not a bus */ + + parent =dt_get_parent (dev); +if ( !parent ) +return -EINVAL ; + + ranges =dt_get_property (dev,"ranges" , &rlen); +if ( !ranges ) + { +printk (XENLOG_ERR "DT: no ranges; cannot enumerate %s\n" , + dev->full_name); +return -EINVAL ; + } +if ( !rlen )/* Nothing to do */ +return 0 ; + + bus->count_cells (dev, &na, &ns); +if ( !DT_CHECK_COUNTS (na, ns) ) + { +printk (XENLOG_ERR "dt_parse: Bad cell count for device %s\n" , + dev->full_name); +return -EINVAL ; + } + + pbus =dt_match_bus (parent); +if ( !pbus ) + { +printk (XENLOG_ERR "DT: %s is not a valid bus\n" , parent->full_name); +return -EINVAL ; + } + + pbus->count_cells (dev, &pna, &pns); +if ( !DT_CHECK_COUNTS (pna, pns) ) + { +printk (XENLOG_ERR "dt_parse: Bad cell count for parent %s\n" , + dev->full_name); +return -EINVAL ; + }
Quoted 8 lines
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2254
Quoted 6 lines
xen/common/device_tree.c +6
-0+ +/* Now walk through the ranges */ + rlen /=4 ; + rone = na + pna + ns; +for ( ; rlen >= rone; rlen -= rone, ranges += rone ) + {Most of the code in this function is the same as
dt_for_each_range(). Can we refactor it to avoid code duplication?
Ok Let me try to refactor the code.
Regards,
Rahul
Quoted 40 lines
Quoted 25 lines
xen/common/device_tree.c +12
-0+unsigned int flags = bus->get_flags (ranges); +if ( flags &IORESOURCE_IO ) +continue ; + +memcpy (addr, ranges,4 * rone); + + addr += rone; + } + +return fdt_property (fdt,"ranges" , regs,sizeof (regs)); +} +/* * Local variables: * mode: C xen/include/xen/device_tree.h +1
-0@@ -849,6 +849,16 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,*/ int dt_get_pci_domain_nr (struct dt_device_node *node); +/** +* dt_pci_remove_io_range - Remove the PCI I/O range property value. xen/include/xen/device_tree.h int dt_count_phandle_with_args(const struct dt_device_node *np, · line 862
Quoted 10 lines
xen/include/xen/device_tree.h +8
-0+* @fdt: Pointer to the file descriptor tree. +* @node: Device tree node. +* +* This function will remove the PCI IO range property from the PCI device tree +* node. +*/ +int dt_pci_remove_io_ranges (void *fdt,const struct dt_device_node *node); +struct dt_device_node *dt_find_node_by_phandle (dt_phandle handle);#ifdef CONFIG_DEVICE_TREE_DEBUG Cheers,
Signature · 1 line
Julien Grall