Lines Matching +full:bus +full:- +full:range

1 // SPDX-License-Identifier: GPL-2.0
17 #include <linux/dma-direct.h> /* for bus_dma_region */
26 /* Callbacks for bus specific translators */
33 u64 (*map)(__be32 *addr, const __be32 *range,
41 * Default translator (generic bus)
53 static u64 of_bus_default_map(__be32 *addr, const __be32 *range, in of_bus_default_map() argument
58 cp = of_read_number(range + fna, na - fna); in of_bus_default_map()
59 s = of_read_number(range + na + pna, ns); in of_bus_default_map()
60 da = of_read_number(addr + fna, na - fna); in of_bus_default_map()
66 return da - cp; in of_bus_default_map()
75 addr[na - 2] = cpu_to_be32(a >> 32); in of_bus_default_translate()
76 addr[na - 1] = cpu_to_be32(a & 0xffffffffu); in of_bus_default_translate()
91 static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na, in of_bus_default_flags_map() argument
95 if (*addr != *range) in of_bus_default_flags_map()
98 return of_bus_default_map(addr, range, na, ns, pna, fna); in of_bus_default_flags_map()
104 return of_bus_default_translate(addr + 1, offset, na - 1); in of_bus_default_flags_translate()
134 * PCI bus specific translator
151 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs in of_bus_pci_match()
171 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, in of_bus_pci_map() argument
177 rf = of_bus_pci_get_flags(range); in of_bus_pci_map()
183 return of_bus_default_map(addr, range, na, ns, pna, fna); in of_bus_pci_map()
190 if (overflows_type(start, r->start)) in __of_address_resource_bounds()
191 return -EOVERFLOW; in __of_address_resource_bounds()
193 r->start = start; in __of_address_resource_bounds()
196 r->end = wrapping_sub(typeof(r->end), r->start, 1); in __of_address_resource_bounds()
197 else if (size && check_add_overflow(r->start, size - 1, &r->end)) in __of_address_resource_bounds()
198 return -EOVERFLOW; in __of_address_resource_bounds()
205 * of_pci_range_to_resource - Create a resource from an of_pci_range
206 * @range: the PCI range that describes the resource
207 * @np: device node where the range belongs to
209 * reflect the values contained in the range.
211 * Returns -EINVAL if the range cannot be converted to resource.
213 * Note that if the range is an IO range, the resource will be converted
215 * if the range cannot be matched to any host bridge IO space (our case here).
216 * To guard against that we try to register the IO range first.
219 int of_pci_range_to_resource(const struct of_pci_range *range, in of_pci_range_to_resource() argument
224 res->flags = range->flags; in of_pci_range_to_resource()
225 res->parent = res->child = res->sibling = NULL; in of_pci_range_to_resource()
226 res->name = np->full_name; in of_pci_range_to_resource()
228 if (res->flags & IORESOURCE_IO) { in of_pci_range_to_resource()
230 err = pci_register_io_range(&np->fwnode, range->cpu_addr, in of_pci_range_to_resource()
231 range->size); in of_pci_range_to_resource()
234 port = pci_address_to_pio(range->cpu_addr); in of_pci_range_to_resource()
235 if (port == (unsigned long)-1) { in of_pci_range_to_resource()
236 err = -EINVAL; in of_pci_range_to_resource()
241 start = range->cpu_addr; in of_pci_range_to_resource()
243 return __of_address_resource_bounds(res, start, range->size); in of_pci_range_to_resource()
246 res->start = (resource_size_t)OF_BAD_ADDR; in of_pci_range_to_resource()
247 res->end = (resource_size_t)OF_BAD_ADDR; in of_pci_range_to_resource()
253 * of_range_to_resource - Create a resource from a ranges entry
254 * @np: device node where the range belongs to
257 * reflect the values contained in the range.
259 * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
266 struct of_range range; in of_range_to_resource() local
272 for_each_of_range(&parser, &range) in of_range_to_resource()
274 return of_pci_range_to_resource(&range, np, res); in of_range_to_resource()
276 return -ENOENT; in of_range_to_resource()
281 * ISA bus specific translator
298 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns, in of_bus_isa_map() argument
302 if ((addr[0] ^ range[0]) & cpu_to_be32(1)) in of_bus_isa_map()
305 return of_bus_default_map(addr, range, na, ns, pna, fna); in of_bus_isa_map()
326 return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3); in of_bus_default_flags_match()
335 return of_property_present(np, "#address-cells"); in of_bus_default_match()
339 * Array of bus specific translators
347 .addresses = "assigned-addresses",
369 .name = "default-flags",
404 static int quirk_state = -1; in of_empty_ranges_quirk()
406 /* PA-SEMI sdc DT bug */ in of_empty_ranges_quirk()
407 if (of_device_is_compatible(np, "1682m-sdc")) in of_empty_ranges_quirk()
420 static int of_translate_one(const struct device_node *parent, const struct of_bus *bus, in of_translate_one() argument
431 * crossing a non-translatable boundary, and thus the addresses in of_translate_one()
434 * what Apple understood, and they do have things like /uni-n or in of_translate_one()
440 * the first place. --BenH. in of_translate_one()
443 * This code is only enabled on powerpc. --gcl in of_translate_one()
445 * This quirk also applies for 'dma-ranges' which frequently exist in in of_translate_one()
446 * child nodes without 'dma-ranges' in the parent nodes. --RobH in of_translate_one()
450 strcmp(rprop, "dma-ranges")) { in of_translate_one()
457 memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4); in of_translate_one()
467 for (; rlen >= rone; rlen -= rone, ranges += rone) { in of_translate_one()
468 offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells); in of_translate_one()
482 /* Translate it into parent bus space */ in of_translate_one()
483 return pbus->translate(addr, offset, pna); in of_translate_one()
487 * Translate an address from the device-tree into a CPU physical address,
488 * this walks up the tree and applies the various bus mappings on the
491 * Note: We consider that crossing any level with #size-cells == 0 to mean
507 const struct of_bus *bus, *pbus; in __of_translate_address() local
517 bus = of_match_bus(parent); in __of_translate_address()
518 if (!bus) in __of_translate_address()
522 bus->count_cells(dev, &na, &ns); in __of_translate_address()
529 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n", in __of_translate_address()
530 bus->name, na, ns, parent); in __of_translate_address()
537 /* Switch to parent bus */ in __of_translate_address()
552 iorange = find_io_range_by_fwnode(&dev->fwnode); in __of_translate_address()
553 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) { in __of_translate_address()
554 u64 result = of_read_number(addr + 1, na - 1); in __of_translate_address()
561 /* Get new parent bus and counts */ in __of_translate_address()
565 pbus->count_cells(dev, &pna, &pns); in __of_translate_address()
571 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n", in __of_translate_address()
572 pbus->name, pna, pns, parent); in __of_translate_address()
574 /* Apply bus translation */ in __of_translate_address()
575 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) in __of_translate_address()
581 bus = pbus; in __of_translate_address()
611 index = of_property_match_string(np, "interconnect-names", "dma-mem"); in __of_get_dma_parent()
616 "#interconnect-cells", in __of_get_dma_parent()
641 in_addr, "dma-ranges", &host); in of_translate_dma_address()
653 * of_translate_dma_region - Translate device tree address and size tuple
656 * @start: return value for the start of the DMA range
657 * @length: return value for the length of the DMA range
696 const struct of_bus *bus; in __of_get_address() local
702 /* match the parent's bus type */ in __of_get_address()
703 bus = of_match_bus(parent); in __of_get_address()
704 if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0))) in __of_get_address()
707 /* Get "reg" or "assigned-addresses" property */ in __of_get_address()
708 prop = of_get_property(dev, bus->addresses, &psize); in __of_get_address()
713 bus->count_cells(dev, &na, &ns); in __of_get_address()
718 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) { in __of_get_address()
720 /* PCI bus matches on BAR number instead of index */ in __of_get_address()
726 *flags = bus->get_flags(prop); in __of_get_address()
735 * of_property_read_reg - Retrieve the specified "reg" entry index without translating
741 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
749 return -EINVAL; in of_property_read_reg()
762 parser->node = node; in parser_init()
763 parser->pna = of_n_addr_cells(node); in parser_init()
764 parser->na = of_bus_n_addr_cells(node); in parser_init()
765 parser->ns = of_bus_n_size_cells(node); in parser_init()
766 parser->dma = !strcmp(name, "dma-ranges"); in parser_init()
767 parser->bus = of_match_bus(node); in parser_init()
769 parser->range = of_get_property(node, name, &rlen); in parser_init()
770 if (parser->range == NULL) in parser_init()
771 return -ENOENT; in parser_init()
773 parser->end = parser->range + rlen / sizeof(__be32); in parser_init()
788 return parser_init(parser, node, "dma-ranges"); in of_pci_dma_range_parser_init()
794 struct of_pci_range *range) in of_pci_range_parser_one() argument
796 int na = parser->na; in of_pci_range_parser_one()
797 int ns = parser->ns; in of_pci_range_parser_one()
798 int np = parser->pna + na + ns; in of_pci_range_parser_one()
799 int busflag_na = parser->bus->flag_cells; in of_pci_range_parser_one()
801 if (!range) in of_pci_range_parser_one()
804 if (!parser->range || parser->range + np > parser->end) in of_pci_range_parser_one()
807 range->flags = parser->bus->get_flags(parser->range); in of_pci_range_parser_one()
809 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); in of_pci_range_parser_one()
811 if (parser->dma) in of_pci_range_parser_one()
812 range->cpu_addr = of_translate_dma_address(parser->node, in of_pci_range_parser_one()
813 parser->range + na); in of_pci_range_parser_one()
815 range->cpu_addr = of_translate_address(parser->node, in of_pci_range_parser_one()
816 parser->range + na); in of_pci_range_parser_one()
818 range->parent_bus_addr = of_read_number(parser->range + na, parser->pna); in of_pci_range_parser_one()
819 range->size = of_read_number(parser->range + parser->pna + na, ns); in of_pci_range_parser_one()
821 parser->range += np; in of_pci_range_parser_one()
824 while (parser->range + np <= parser->end) { in of_pci_range_parser_one()
828 flags = parser->bus->get_flags(parser->range); in of_pci_range_parser_one()
829 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); in of_pci_range_parser_one()
830 if (parser->dma) in of_pci_range_parser_one()
831 cpu_addr = of_translate_dma_address(parser->node, in of_pci_range_parser_one()
832 parser->range + na); in of_pci_range_parser_one()
834 cpu_addr = of_translate_address(parser->node, in of_pci_range_parser_one()
835 parser->range + na); in of_pci_range_parser_one()
836 size = of_read_number(parser->range + parser->pna + na, ns); in of_pci_range_parser_one()
838 if (flags != range->flags) in of_pci_range_parser_one()
840 if (bus_addr != range->bus_addr + range->size || in of_pci_range_parser_one()
841 cpu_addr != range->cpu_addr + range->size) in of_pci_range_parser_one()
844 range->size += size; in of_pci_range_parser_one()
845 parser->range += np; in of_pci_range_parser_one()
848 return range; in of_pci_range_parser_one()
862 /* host-specific port access */ in of_translate_ioport()
863 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size); in of_translate_ioport()
866 /* memory-mapped I/O range */ in of_translate_ioport()
870 if (port == (unsigned long)-1) in of_translate_ioport()
878 * of_dma_get_range - Get DMA range info and put it into a map array
879 * @np: device node to get DMA range info
880 * @map: dma range structure to return
882 * Look in bottom up direction for the first "dma-ranges" property
885 * dma-ranges format:
890 * It returns -ENODEV if "dma-ranges" property was not found for this
899 struct of_range range; in of_dma_get_range() local
904 ranges = of_get_property(node, "dma-ranges", &len); in of_dma_get_range()
910 /* Once we find 'dma-ranges', then a missing one is an error */ in of_dma_get_range()
912 return -ENODEV; in of_dma_get_range()
920 pr_debug("no dma-ranges found for node(%pOF)\n", np); in of_dma_get_range()
921 return -ENODEV; in of_dma_get_range()
924 for_each_of_range(&parser, &range) { in of_dma_get_range()
925 if (range.cpu_addr == OF_BAD_ADDR) { in of_dma_get_range()
927 range.bus_addr, node); in of_dma_get_range()
934 return -EINVAL; in of_dma_get_range()
938 return -ENOMEM; in of_dma_get_range()
946 for_each_of_range(&parser, &range) { in of_dma_get_range()
948 range.bus_addr, range.cpu_addr, range.size); in of_dma_get_range()
949 if (range.cpu_addr == OF_BAD_ADDR) in of_dma_get_range()
951 r->cpu_start = range.cpu_addr; in of_dma_get_range()
952 r->dma_start = range.bus_addr; in of_dma_get_range()
953 r->size = range.size; in of_dma_get_range()
961 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
965 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
974 struct of_range range; in of_dma_get_max_cpu_address() local
982 ranges = of_get_property(np, "dma-ranges", &len); in of_dma_get_max_cpu_address()
985 for_each_of_range(&parser, &range) in of_dma_get_max_cpu_address()
986 if (range.cpu_addr + range.size > cpu_end) in of_dma_get_max_cpu_address()
987 cpu_end = range.cpu_addr + range.size - 1; in of_dma_get_max_cpu_address()
1003 * of_dma_is_coherent - Check if device is coherent
1006 * It returns true if "dma-coherent" property was found
1009 * "dma-noncoherent" property was found for this device.
1016 if (of_property_read_bool(node, "dma-coherent")) in of_dma_is_coherent()
1019 if (of_property_read_bool(node, "dma-noncoherent")) in of_dma_is_coherent()
1029 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1032 * Returns true if the "nonposted-mmio" property was found for
1033 * the device's bus.
1047 return of_property_read_bool(parent, "nonposted-mmio"); in of_mmio_is_nonposted()
1061 return -EINVAL; in __of_address_to_resource()
1063 /* Get optional "reg-names" property to add a name to a resource */ in __of_address_to_resource()
1065 of_property_read_string_index(dev, "reg-names", index, &name); in __of_address_to_resource()
1072 return -EINVAL; in __of_address_to_resource()
1075 return -EINVAL; in __of_address_to_resource()
1081 r->flags = flags; in __of_address_to_resource()
1082 r->name = name ? name : dev->full_name; in __of_address_to_resource()
1088 * of_address_to_resource - Translate device tree address and return as resource
1093 * Returns -EINVAL if the range cannot be converted to resource.
1103 return __of_address_to_resource(dev, index, -1, r); in of_address_to_resource()
1112 return -ENOSYS; in of_pci_address_to_resource()
1114 return __of_address_to_resource(dev, -1, bar, r); in of_pci_address_to_resource()
1119 * of_iomap - Maps the memory mapped IO for a given device_node
1120 * @np: the device whose io range will be mapped
1121 * @index: index of the io range
1140 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
1142 * @device: the device whose io range will be mapped
1143 * @index: index of the io range
1160 return IOMEM_ERR_PTR(-EINVAL); in of_io_request_and_map()
1165 return IOMEM_ERR_PTR(-EBUSY); in of_io_request_and_map()
1174 return IOMEM_ERR_PTR(-ENOMEM); in of_io_request_and_map()