address.c 24.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3
#define pr_fmt(fmt)	"OF: " fmt

4
#include <linux/device.h>
5
#include <linux/fwnode.h>
G
Grant Likely 已提交
6 7
#include <linux/io.h>
#include <linux/ioport.h>
8
#include <linux/logic_pio.h>
9
#include <linux/module.h>
G
Grant Likely 已提交
10
#include <linux/of_address.h>
11
#include <linux/pci.h>
12
#include <linux/pci_regs.h>
13 14
#include <linux/sizes.h>
#include <linux/slab.h>
15
#include <linux/string.h>
G
Grant Likely 已提交
16

17 18
/* Max address size we deal with */
#define OF_MAX_ADDR_CELLS	4
19 20
#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)
21 22

static struct of_bus *of_match_bus(struct device_node *np);
23 24
static int __of_address_to_resource(struct device_node *dev,
		const __be32 *addrp, u64 size, unsigned int flags,
25
		const char *name, struct resource *r);
26 27 28

/* Debug utility */
#ifdef DEBUG
29
static void of_dump_addr(const char *s, const __be32 *addr, int na)
30
{
31
	pr_debug("%s", s);
32
	while (na--)
33 34
		pr_cont(" %08x", be32_to_cpu(*(addr++)));
	pr_cont("\n");
35 36
}
#else
37
static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
38 39 40 41 42 43 44 45 46
#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);
K
Kim Phillips 已提交
47
	u64		(*map)(__be32 *addr, const __be32 *range,
48
				int na, int ns, int pna);
K
Kim Phillips 已提交
49
	int		(*translate)(__be32 *addr, u64 offset, int na);
50
	unsigned int	(*get_flags)(const __be32 *addr);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
};

/*
 * 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);
}

K
Kim Phillips 已提交
66
static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
67 68 69 70 71 72 73 74
		int na, int ns, int pna)
{
	u64 cp, s, da;

	cp = of_read_number(range, na);
	s  = of_read_number(range + na + pna, ns);
	da = of_read_number(addr, na);

75
	pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
76 77 78 79 80 81 82 83
		 (unsigned long long)cp, (unsigned long long)s,
		 (unsigned long long)da);

	if (da < cp || da >= (cp + s))
		return OF_BAD_ADDR;
	return da - cp;
}

K
Kim Phillips 已提交
84
static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
85 86 87 88 89
{
	u64 a = of_read_number(addr, na);
	memset(addr, 0, na * 4);
	a += offset;
	if (na > 1)
G
Grant Likely 已提交
90 91
		addr[na - 2] = cpu_to_be32(a >> 32);
	addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
92 93 94 95

	return 0;
}

96
static unsigned int of_bus_default_get_flags(const __be32 *addr)
97 98 99 100
{
	return IORESOURCE_MEM;
}

101
#ifdef CONFIG_PCI
102 103 104 105 106 107
/*
 * PCI bus specific translator
 */

static int of_bus_pci_match(struct device_node *np)
{
108
	/*
109
 	 * "pciex" is PCI Express
110 111 112
	 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
	 * "ht" is hypertransport
	 */
113 114
	return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") ||
		!strcmp(np->type, "vci") || !strcmp(np->type, "ht");
115 116 117 118 119 120 121 122 123 124 125
}

static void of_bus_pci_count_cells(struct device_node *np,
				   int *addrc, int *sizec)
{
	if (addrc)
		*addrc = 3;
	if (sizec)
		*sizec = 2;
}

126
static unsigned int of_bus_pci_get_flags(const __be32 *addr)
127 128
{
	unsigned int flags = 0;
129
	u32 w = be32_to_cpup(addr);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	switch((w >> 24) & 0x03) {
	case 0x01:
		flags |= IORESOURCE_IO;
		break;
	case 0x02: /* 32 bits */
	case 0x03: /* 64 bits */
		flags |= IORESOURCE_MEM;
		break;
	}
	if (w & 0x40000000)
		flags |= IORESOURCE_PREFETCH;
	return flags;
}

K
Kim Phillips 已提交
145
static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
146
		int pna)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
	u64 cp, s, da;
	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;

	/* Read address values, skipping high cell */
	cp = of_read_number(range + 1, na - 1);
	s  = of_read_number(range + na + pna, ns);
	da = of_read_number(addr + 1, na - 1);

163
	pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
164 165 166 167 168 169 170 171
		 (unsigned long long)cp, (unsigned long long)s,
		 (unsigned long long)da);

	if (da < cp || da >= (cp + s))
		return OF_BAD_ADDR;
	return da - cp;
}

K
Kim Phillips 已提交
172
static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
173 174 175 176
{
	return of_bus_default_translate(addr + 1, offset, na - 1);
}

177
const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
178 179
			unsigned int *flags)
{
180
	const __be32 *prop;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	unsigned int psize;
	struct device_node *parent;
	struct of_bus *bus;
	int onesize, i, na, ns;

	/* Get parent & match bus type */
	parent = of_get_parent(dev);
	if (parent == NULL)
		return NULL;
	bus = of_match_bus(parent);
	if (strcmp(bus->name, "pci")) {
		of_node_put(parent);
		return NULL;
	}
	bus->count_cells(dev, &na, &ns);
	of_node_put(parent);
197
	if (!OF_CHECK_ADDR_COUNT(na))
198 199 200 201 202 203 204 205 206
		return NULL;

	/* Get "reg" or "assigned-addresses" property */
	prop = of_get_property(dev, bus->addresses, &psize);
	if (prop == NULL)
		return NULL;
	psize /= 4;

	onesize = na + ns;
G
Grant Likely 已提交
207 208 209
	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
		u32 val = be32_to_cpu(prop[0]);
		if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
210 211 212 213 214 215
			if (size)
				*size = of_read_number(prop + na, ns);
			if (flags)
				*flags = bus->get_flags(prop);
			return prop;
		}
G
Grant Likely 已提交
216
	}
217 218 219 220 221 222 223
	return NULL;
}
EXPORT_SYMBOL(of_get_pci_address);

int of_pci_address_to_resource(struct device_node *dev, int bar,
			       struct resource *r)
{
224
	const __be32	*addrp;
225 226 227 228 229 230
	u64		size;
	unsigned int	flags;

	addrp = of_get_pci_address(dev, bar, &size, &flags);
	if (addrp == NULL)
		return -EINVAL;
231
	return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
232 233
}
EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
234

235 236
static int parser_init(struct of_pci_range_parser *parser,
			struct device_node *node, const char *name)
237 238 239 240 241 242 243 244
{
	const int na = 3, ns = 2;
	int rlen;

	parser->node = node;
	parser->pna = of_n_addr_cells(node);
	parser->np = parser->pna + na + ns;

245
	parser->range = of_get_property(node, name, &rlen);
246 247 248 249 250 251 252
	if (parser->range == NULL)
		return -ENOENT;

	parser->end = parser->range + rlen / sizeof(__be32);

	return 0;
}
253 254 255 256 257 258

int of_pci_range_parser_init(struct of_pci_range_parser *parser,
				struct device_node *node)
{
	return parser_init(parser, node, "ranges");
}
259 260
EXPORT_SYMBOL_GPL(of_pci_range_parser_init);

261 262 263 264 265 266 267
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);

268 269 270 271 272 273 274 275 276 277 278
struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
						struct of_pci_range *range)
{
	const int na = 3, ns = 2;

	if (!range)
		return NULL;

	if (!parser->range || parser->range + parser->np > parser->end)
		return NULL;

279
	range->pci_space = be32_to_cpup(parser->range);
280 281 282 283 284 285 286 287 288 289
	range->flags = of_bus_pci_get_flags(parser->range);
	range->pci_addr = of_read_number(parser->range + 1, ns);
	range->cpu_addr = of_translate_address(parser->node,
				parser->range + na);
	range->size = of_read_number(parser->range + parser->pna + na, ns);

	parser->range += parser->np;

	/* Now consume following elements while they are contiguous */
	while (parser->range + parser->np <= parser->end) {
290
		u32 flags;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		u64 pci_addr, cpu_addr, size;

		flags = of_bus_pci_get_flags(parser->range);
		pci_addr = of_read_number(parser->range + 1, ns);
		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;
		if (pci_addr != range->pci_addr + range->size ||
		    cpu_addr != range->cpu_addr + range->size)
			break;

		range->size += size;
		parser->range += parser->np;
	}

	return range;
}
EXPORT_SYMBOL_GPL(of_pci_range_parser_one);

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
/*
 * 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.
 *
 * Returns EINVAL if the range cannot be converted to resource.
 *
 * 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.
 */
int of_pci_range_to_resource(struct of_pci_range *range,
			     struct device_node *np, struct resource *res)
330
{
331
	int err;
332 333 334
	res->flags = range->flags;
	res->parent = res->child = res->sibling = NULL;
	res->name = np->full_name;
335 336 337

	if (res->flags & IORESOURCE_IO) {
		unsigned long port;
338 339
		err = pci_register_io_range(&np->fwnode, range->cpu_addr,
				range->size);
340 341 342 343 344 345 346 347 348
		if (err)
			goto invalid_range;
		port = pci_address_to_pio(range->cpu_addr);
		if (port == (unsigned long)-1) {
			err = -EINVAL;
			goto invalid_range;
		}
		res->start = port;
	} else {
349 350 351 352 353 354
		if ((sizeof(resource_size_t) < 8) &&
		    upper_32_bits(range->cpu_addr)) {
			err = -EINVAL;
			goto invalid_range;
		}

355 356 357 358 359 360 361 362 363
		res->start = range->cpu_addr;
	}
	res->end = res->start + range->size - 1;
	return 0;

invalid_range:
	res->start = (resource_size_t)OF_BAD_ADDR;
	res->end = (resource_size_t)OF_BAD_ADDR;
	return err;
364
}
365
EXPORT_SYMBOL(of_pci_range_to_resource);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
#endif /* CONFIG_PCI */

/*
 * ISA bus specific translator
 */

static int of_bus_isa_match(struct device_node *np)
{
	return !strcmp(np->name, "isa");
}

static void of_bus_isa_count_cells(struct device_node *child,
				   int *addrc, int *sizec)
{
	if (addrc)
		*addrc = 2;
	if (sizec)
		*sizec = 1;
}

K
Kim Phillips 已提交
386
static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
387
		int pna)
388 389 390 391
{
	u64 cp, s, da;

	/* Check address type match */
392
	if ((addr[0] ^ range[0]) & cpu_to_be32(1))
393 394 395 396 397 398 399
		return OF_BAD_ADDR;

	/* Read address values, skipping high cell */
	cp = of_read_number(range + 1, na - 1);
	s  = of_read_number(range + na + pna, ns);
	da = of_read_number(addr + 1, na - 1);

400
	pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
401 402 403 404 405 406 407 408
		 (unsigned long long)cp, (unsigned long long)s,
		 (unsigned long long)da);

	if (da < cp || da >= (cp + s))
		return OF_BAD_ADDR;
	return da - cp;
}

K
Kim Phillips 已提交
409
static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
410 411 412 413
{
	return of_bus_default_translate(addr + 1, offset, na - 1);
}

414
static unsigned int of_bus_isa_get_flags(const __be32 *addr)
415 416
{
	unsigned int flags = 0;
417
	u32 w = be32_to_cpup(addr);
418 419 420 421 422 423 424 425 426 427 428 429 430

	if (w & 1)
		flags |= IORESOURCE_IO;
	else
		flags |= IORESOURCE_MEM;
	return flags;
}

/*
 * Array of bus specific translators
 */

static struct of_bus of_busses[] = {
431
#ifdef CONFIG_PCI
432 433 434 435 436 437 438 439 440 441
	/* PCI */
	{
		.name = "pci",
		.addresses = "assigned-addresses",
		.match = of_bus_pci_match,
		.count_cells = of_bus_pci_count_cells,
		.map = of_bus_pci_map,
		.translate = of_bus_pci_translate,
		.get_flags = of_bus_pci_get_flags,
	},
442
#endif /* CONFIG_PCI */
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	/* ISA */
	{
		.name = "isa",
		.addresses = "reg",
		.match = of_bus_isa_match,
		.count_cells = of_bus_isa_count_cells,
		.map = of_bus_isa_map,
		.translate = of_bus_isa_translate,
		.get_flags = of_bus_isa_get_flags,
	},
	/* 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,
	},
};

static struct of_bus *of_match_bus(struct device_node *np)
{
	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;
}

476
static int of_empty_ranges_quirk(struct device_node *np)
477 478
{
	if (IS_ENABLED(CONFIG_PPC)) {
479
		/* To save cycles, we cache the result for global "Mac" setting */
480 481
		static int quirk_state = -1;

482 483 484 485 486
		/* PA-SEMI sdc DT bug */
		if (of_device_is_compatible(np, "1682m-sdc"))
			return true;

		/* Make quirk cached */
487 488 489 490 491 492 493 494 495
		if (quirk_state < 0)
			quirk_state =
				of_machine_is_compatible("Power Macintosh") ||
				of_machine_is_compatible("MacRISC");
		return quirk_state;
	}
	return false;
}

496
static int of_translate_one(struct device_node *parent, struct of_bus *bus,
K
Kim Phillips 已提交
497
			    struct of_bus *pbus, __be32 *addr,
498 499
			    int na, int ns, int pna, const char *rprop)
{
500
	const __be32 *ranges;
501 502 503 504
	unsigned int rlen;
	int rone;
	u64 offset = OF_BAD_ADDR;

505 506
	/*
	 * Normally, an absence of a "ranges" property means we are
507
	 * crossing a non-translatable boundary, and thus the addresses
508
	 * below the current cannot be converted to CPU physical ones.
509 510 511 512 513 514 515 516
	 * 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.
517 518 519
	 *
	 * As far as we know, this damage only exists on Apple machines, so
	 * This code is only enabled on powerpc. --gcl
520 521
	 */
	ranges = of_get_property(parent, rprop, &rlen);
522
	if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
523
		pr_debug("no ranges; cannot translate\n");
524 525
		return 1;
	}
526 527 528
	if (ranges == NULL || rlen == 0) {
		offset = of_read_number(addr, na);
		memset(addr, 0, pna * 4);
529
		pr_debug("empty ranges; 1:1 translation\n");
530 531 532
		goto finish;
	}

533
	pr_debug("walking ranges...\n");
534 535 536 537 538 539 540 541 542 543

	/* Now walk through the ranges */
	rlen /= 4;
	rone = na + pna + ns;
	for (; rlen >= rone; rlen -= rone, ranges += rone) {
		offset = bus->map(addr, ranges, na, ns, pna);
		if (offset != OF_BAD_ADDR)
			break;
	}
	if (offset == OF_BAD_ADDR) {
544
		pr_debug("not found !\n");
545 546 547 548 549
		return 1;
	}
	memcpy(addr, ranges + na, 4 * pna);

 finish:
550 551
	of_dump_addr("parent translation for:", addr, pna);
	pr_debug("with offset: %llx\n", (unsigned long long)offset);
552 553 554 555 556 557 558 559 560 561 562 563 564 565

	/* 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
566 567 568 569
 *
 * 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.
570
 */
K
Kim Phillips 已提交
571
static u64 __of_translate_address(struct device_node *dev,
572 573
				  const __be32 *in_addr, const char *rprop,
				  struct device_node **host)
574 575 576
{
	struct device_node *parent = NULL;
	struct of_bus *bus, *pbus;
K
Kim Phillips 已提交
577
	__be32 addr[OF_MAX_ADDR_CELLS];
578 579 580
	int na, ns, pna, pns;
	u64 result = OF_BAD_ADDR;

581
	pr_debug("** translation for device %pOF **\n", dev);
582 583 584 585

	/* Increase refcount at current level */
	of_node_get(dev);

586
	*host = NULL;
587 588 589 590 591 592
	/* Get parent & match bus type */
	parent = of_get_parent(dev);
	if (parent == NULL)
		goto bail;
	bus = of_match_bus(parent);

593
	/* Count address cells & copy address locally */
594 595
	bus->count_cells(dev, &na, &ns);
	if (!OF_CHECK_COUNTS(na, ns)) {
596
		pr_debug("Bad cell count for %pOF\n", dev);
597 598 599 600
		goto bail;
	}
	memcpy(addr, in_addr, na * 4);

601 602
	pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
	    bus->name, na, ns, parent);
603
	of_dump_addr("translating address:", addr, na);
604 605 606

	/* Translate */
	for (;;) {
607 608
		struct logic_pio_hwaddr *iorange;

609 610 611 612 613 614 615
		/* Switch to parent bus */
		of_node_put(dev);
		dev = parent;
		parent = of_get_parent(dev);

		/* If root, we have finished */
		if (parent == NULL) {
616
			pr_debug("reached root node\n");
617 618 619 620
			result = of_read_number(addr, na);
			break;
		}

621 622 623 624 625 626 627 628 629 630 631 632 633
		/*
		 * 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)) {
			result = of_read_number(addr + 1, na - 1);
			pr_debug("indirectIO matched(%pOF) 0x%llx\n",
				 dev, result);
			*host = of_node_get(dev);
			break;
		}

634 635 636 637
		/* Get new parent bus and counts */
		pbus = of_match_bus(parent);
		pbus->count_cells(dev, &pna, &pns);
		if (!OF_CHECK_COUNTS(pna, pns)) {
638
			pr_err("Bad cell count for %pOF\n", dev);
639 640 641
			break;
		}

642 643
		pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
		    pbus->name, pna, pns, parent);
644 645 646 647 648 649 650 651 652 653

		/* Apply bus translation */
		if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
			break;

		/* Complete the move up one level */
		na = pna;
		ns = pns;
		bus = pbus;

654
		of_dump_addr("one level translation:", addr, na);
655 656 657 658 659 660 661 662
	}
 bail:
	of_node_put(parent);
	of_node_put(dev);

	return result;
}

663
u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
664
{
665 666 667 668 669 670 671 672 673 674
	struct device_node *host;
	u64 ret;

	ret = __of_translate_address(dev, in_addr, "ranges", &host);
	if (host) {
		of_node_put(host);
		return OF_BAD_ADDR;
	}

	return ret;
675 676 677
}
EXPORT_SYMBOL(of_translate_address);

678
u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
679
{
680 681 682 683 684 685 686 687 688 689 690
	struct device_node *host;
	u64 ret;

	ret = __of_translate_address(dev, in_addr, "dma-ranges", &host);

	if (host) {
		of_node_put(host);
		return OF_BAD_ADDR;
	}

	return ret;
691 692 693
}
EXPORT_SYMBOL(of_translate_dma_address);

694
const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
695 696
		    unsigned int *flags)
{
697
	const __be32 *prop;
698 699 700 701 702 703 704 705 706 707 708 709
	unsigned int psize;
	struct device_node *parent;
	struct of_bus *bus;
	int onesize, i, na, ns;

	/* Get parent & match bus type */
	parent = of_get_parent(dev);
	if (parent == NULL)
		return NULL;
	bus = of_match_bus(parent);
	bus->count_cells(dev, &na, &ns);
	of_node_put(parent);
710
	if (!OF_CHECK_ADDR_COUNT(na))
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
		return NULL;

	/* Get "reg" or "assigned-addresses" property */
	prop = of_get_property(dev, bus->addresses, &psize);
	if (prop == NULL)
		return NULL;
	psize /= 4;

	onesize = na + ns;
	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
		if (i == index) {
			if (size)
				*size = of_read_number(prop + na, ns);
			if (flags)
				*flags = bus->get_flags(prop);
			return prop;
		}
	return NULL;
}
EXPORT_SYMBOL(of_get_address);

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
			u64 size)
{
	u64 taddr;
	unsigned long port;
	struct device_node *host;

	taddr = __of_translate_address(dev, in_addr, "ranges", &host);
	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;
}

755 756
static int __of_address_to_resource(struct device_node *dev,
		const __be32 *addrp, u64 size, unsigned int flags,
757
		const char *name, struct resource *r)
758 759 760
{
	u64 taddr;

761 762 763 764 765
	if (flags & IORESOURCE_MEM)
		taddr = of_translate_address(dev, addrp);
	else if (flags & IORESOURCE_IO)
		taddr = of_translate_ioport(dev, addrp, size);
	else
766
		return -EINVAL;
767

768 769 770
	if (taddr == OF_BAD_ADDR)
		return -EINVAL;
	memset(r, 0, sizeof(struct resource));
771 772 773

	r->start = taddr;
	r->end = taddr + size - 1;
774
	r->flags = flags;
775 776
	r->name = name ? name : dev->full_name;

777 778 779 780 781 782 783 784
	return 0;
}

/**
 * of_address_to_resource - Translate device tree address and return as 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
F
Frank Rowand 已提交
785
 * pci_address_to_pio(), that is because it's either called too early or it
786 787 788 789 790
 * can't be matched to any host bridge IO space
 */
int of_address_to_resource(struct device_node *dev, int index,
			   struct resource *r)
{
791
	const __be32	*addrp;
792 793
	u64		size;
	unsigned int	flags;
794
	const char	*name = NULL;
795 796 797 798

	addrp = of_get_address(dev, index, &size, &flags);
	if (addrp == NULL)
		return -EINVAL;
799 800 801 802 803

	/* Get optional "reg-names" property to add a name to a resource */
	of_property_read_string_index(dev, "reg-names",	index, &name);

	return __of_address_to_resource(dev, addrp, size, flags, name, r);
804 805 806
}
EXPORT_SYMBOL_GPL(of_address_to_resource);

807 808 809 810 811 812 813 814
struct device_node *of_find_matching_node_by_address(struct device_node *from,
					const struct of_device_id *matches,
					u64 base_address)
{
	struct device_node *dn = of_find_matching_node(from, matches);
	struct resource res;

	while (dn) {
815 816
		if (!of_address_to_resource(dn, 0, &res) &&
		    res.start == base_address)
817
			return dn;
818

819 820 821 822 823 824
		dn = of_find_matching_node(dn, matches);
	}

	return NULL;
}

825

G
Grant Likely 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839
/**
 * of_iomap - 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
 *
 * 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;

840
	return ioremap(res.start, resource_size(&res));
G
Grant Likely 已提交
841 842
}
EXPORT_SYMBOL(of_iomap);
843

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
/*
 * 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 of the resource
 *
 * 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,
859
					const char *name)
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
{
	struct resource res;
	void __iomem *mem;

	if (of_address_to_resource(np, index, &res))
		return IOMEM_ERR_PTR(-EINVAL);

	if (!request_mem_region(res.start, resource_size(&res), name))
		return IOMEM_ERR_PTR(-EBUSY);

	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);

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
/**
 * of_dma_get_range - Get DMA range info
 * @np:		device node to get DMA range info
 * @dma_addr:	pointer to store initial DMA address of DMA range
 * @paddr:	pointer to store initial CPU address of DMA range
 * @size:	pointer to store size of DMA range
 *
 * Look in bottom up direction for the first "dma-ranges" property
 * and parse it.
 *  dma-ranges format:
 *	DMA addr (dma_addr)	: naddr cells
 *	CPU addr (phys_addr_t)	: pna cells
 *	size			: nsize cells
 *
 * It returns -ENODEV if "dma-ranges" property was not found
 * for this device in DT.
 */
int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
{
	struct device_node *node = of_node_get(np);
	const __be32 *ranges = NULL;
	int len, naddr, nsize, pna;
	int ret = 0;
	u64 dmaaddr;

	if (!node)
		return -EINVAL;

	while (1) {
		naddr = of_n_addr_cells(node);
		nsize = of_n_size_cells(node);
		node = of_get_next_parent(node);
		if (!node)
			break;

		ranges = of_get_property(node, "dma-ranges", &len);

		/* Ignore empty ranges, they imply no translation required */
		if (ranges && len > 0)
			break;

		/*
		 * At least empty ranges has to be defined for parent node if
		 * DMA is supported
		 */
		if (!ranges)
			break;
	}

	if (!ranges) {
930
		pr_debug("no dma-ranges found for node(%pOF)\n", np);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
		ret = -ENODEV;
		goto out;
	}

	len /= sizeof(u32);

	pna = of_n_addr_cells(node);

	/* dma-ranges format:
	 * DMA addr	: naddr cells
	 * CPU addr	: pna cells
	 * size		: nsize cells
	 */
	dmaaddr = of_read_number(ranges, naddr);
	*paddr = of_translate_dma_address(np, ranges);
	if (*paddr == OF_BAD_ADDR) {
947 948
		pr_err("translation of DMA address(%pad) to CPU address failed node(%pOF)\n",
		       dma_addr, np);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
		ret = -EINVAL;
		goto out;
	}
	*dma_addr = dmaaddr;

	*size = of_read_number(ranges + naddr + pna, nsize);

	pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
		 *dma_addr, *paddr, *size);

out:
	of_node_put(node);

	return ret;
}
EXPORT_SYMBOL_GPL(of_dma_get_range);
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

/**
 * of_dma_is_coherent - Check if device is coherent
 * @np:	device node
 *
 * It returns true if "dma-coherent" property was found
 * for this device in DT.
 */
bool of_dma_is_coherent(struct device_node *np)
{
	struct device_node *node = of_node_get(np);

	while (node) {
		if (of_property_read_bool(node, "dma-coherent")) {
			of_node_put(node);
			return true;
		}
		node = of_get_next_parent(node);
	}
	of_node_put(node);
	return false;
}
987
EXPORT_SYMBOL_GPL(of_dma_is_coherent);