1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009-2014 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Andrew Turner under sponsorship from 8 * the FreeBSD Foundation. 9 * This software was developed by Semihalf under sponsorship from 10 * the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/module.h> 41 #include <sys/bus.h> 42 #include <sys/limits.h> 43 #include <sys/sysctl.h> 44 45 #include <machine/resource.h> 46 47 #include <dev/fdt/fdt_common.h> 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_bus_subr.h> 50 #include <dev/ofw/openfirm.h> 51 52 #include "ofw_bus_if.h" 53 54 #ifdef DEBUG 55 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 56 printf(fmt,##args); } while (0) 57 #else 58 #define debugf(fmt, args...) 59 #endif 60 61 #define FDT_COMPAT_LEN 255 62 63 #define FDT_REG_CELLS 4 64 #define FDT_RANGES_SIZE 48 65 66 SYSCTL_NODE(_hw, OID_AUTO, fdt, CTLFLAG_RD, 0, "Flattened Device Tree"); 67 68 vm_paddr_t fdt_immr_pa; 69 vm_offset_t fdt_immr_va; 70 vm_offset_t fdt_immr_size; 71 72 struct fdt_ic_list fdt_ic_list_head = SLIST_HEAD_INITIALIZER(fdt_ic_list_head); 73 74 static int 75 fdt_get_range_by_busaddr(phandle_t node, u_long addr, u_long *base, 76 u_long *size) 77 { 78 pcell_t ranges[32], *rangesptr; 79 pcell_t addr_cells, size_cells, par_addr_cells; 80 u_long bus_addr, par_bus_addr, pbase, psize; 81 int err, i, len, tuple_size, tuples; 82 83 if (node == 0) { 84 *base = 0; 85 *size = ULONG_MAX; 86 return (0); 87 } 88 89 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0) 90 return (ENXIO); 91 /* 92 * Process 'ranges' property. 93 */ 94 par_addr_cells = fdt_parent_addr_cells(node); 95 if (par_addr_cells > 2) { 96 return (ERANGE); 97 } 98 99 len = OF_getproplen(node, "ranges"); 100 if (len < 0) 101 return (-1); 102 if (len > sizeof(ranges)) 103 return (ENOMEM); 104 if (len == 0) { 105 return (fdt_get_range_by_busaddr(OF_parent(node), addr, 106 base, size)); 107 } 108 109 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0) 110 return (EINVAL); 111 112 tuple_size = addr_cells + par_addr_cells + size_cells; 113 tuples = len / (tuple_size * sizeof(cell_t)); 114 115 if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2) 116 return (ERANGE); 117 118 *base = 0; 119 *size = 0; 120 121 for (i = 0; i < tuples; i++) { 122 rangesptr = &ranges[i * tuple_size]; 123 124 bus_addr = fdt_data_get((void *)rangesptr, addr_cells); 125 if (bus_addr != addr) 126 continue; 127 rangesptr += addr_cells; 128 129 par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells); 130 rangesptr += par_addr_cells; 131 132 err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr, 133 &pbase, &psize); 134 if (err > 0) 135 return (err); 136 if (err == 0) 137 *base = pbase; 138 else 139 *base = par_bus_addr; 140 141 *size = fdt_data_get((void *)rangesptr, size_cells); 142 143 return (0); 144 } 145 146 return (EINVAL); 147 } 148 149 int 150 fdt_get_range(phandle_t node, int range_id, u_long *base, u_long *size) 151 { 152 pcell_t ranges[FDT_RANGES_SIZE], *rangesptr; 153 pcell_t addr_cells, size_cells, par_addr_cells; 154 u_long par_bus_addr, pbase, psize; 155 int err, len; 156 157 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0) 158 return (ENXIO); 159 /* 160 * Process 'ranges' property. 161 */ 162 par_addr_cells = fdt_parent_addr_cells(node); 163 if (par_addr_cells > 2) 164 return (ERANGE); 165 166 len = OF_getproplen(node, "ranges"); 167 if (len > sizeof(ranges)) 168 return (ENOMEM); 169 if (len == 0) { 170 *base = 0; 171 *size = ULONG_MAX; 172 return (0); 173 } 174 175 if (!(range_id < len)) 176 return (ERANGE); 177 178 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0) 179 return (EINVAL); 180 181 if (par_addr_cells > 2 || addr_cells > 2 || size_cells > 2) 182 return (ERANGE); 183 184 *base = 0; 185 *size = 0; 186 rangesptr = &ranges[range_id]; 187 188 *base = fdt_data_get((void *)rangesptr, addr_cells); 189 rangesptr += addr_cells; 190 191 par_bus_addr = fdt_data_get((void *)rangesptr, par_addr_cells); 192 rangesptr += par_addr_cells; 193 194 err = fdt_get_range_by_busaddr(OF_parent(node), par_bus_addr, 195 &pbase, &psize); 196 if (err == 0) 197 *base += pbase; 198 else 199 *base += par_bus_addr; 200 201 *size = fdt_data_get((void *)rangesptr, size_cells); 202 return (0); 203 } 204 205 int 206 fdt_immr_addr(vm_offset_t immr_va) 207 { 208 phandle_t node; 209 u_long base, size; 210 int r; 211 212 /* 213 * Try to access the SOC node directly i.e. through /aliases/. 214 */ 215 if ((node = OF_finddevice("soc")) != -1) 216 if (ofw_bus_node_is_compatible(node, "simple-bus")) 217 goto moveon; 218 /* 219 * Find the node the long way. 220 */ 221 if ((node = OF_finddevice("/")) == -1) 222 return (ENXIO); 223 224 if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0) 225 return (ENXIO); 226 227 moveon: 228 if ((r = fdt_get_range(node, 0, &base, &size)) == 0) { 229 fdt_immr_pa = base; 230 fdt_immr_va = immr_va; 231 fdt_immr_size = size; 232 } 233 234 return (r); 235 } 236 237 int 238 fdt_is_compatible_strict(phandle_t node, const char *compatible) 239 { 240 char compat[FDT_COMPAT_LEN]; 241 242 if (OF_getproplen(node, "compatible") <= 0) 243 return (0); 244 245 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0) 246 return (0); 247 248 if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0) 249 /* This fits. */ 250 return (1); 251 252 return (0); 253 } 254 255 phandle_t 256 fdt_find_compatible(phandle_t start, const char *compat, int strict) 257 { 258 phandle_t child; 259 260 /* 261 * Traverse all children of 'start' node, and find first with 262 * matching 'compatible' property. 263 */ 264 for (child = OF_child(start); child != 0; child = OF_peer(child)) 265 if (ofw_bus_node_is_compatible(child, compat)) { 266 if (strict) 267 if (!fdt_is_compatible_strict(child, compat)) 268 continue; 269 return (child); 270 } 271 return (0); 272 } 273 274 phandle_t 275 fdt_depth_search_compatible(phandle_t start, const char *compat, int strict) 276 { 277 phandle_t child, node; 278 279 /* 280 * Depth-search all descendants of 'start' node, and find first with 281 * matching 'compatible' property. 282 */ 283 for (node = OF_child(start); node != 0; node = OF_peer(node)) { 284 if (ofw_bus_node_is_compatible(node, compat) && 285 (strict == 0 || fdt_is_compatible_strict(node, compat))) { 286 return (node); 287 } 288 child = fdt_depth_search_compatible(node, compat, strict); 289 if (child != 0) 290 return (child); 291 } 292 return (0); 293 } 294 295 int 296 fdt_parent_addr_cells(phandle_t node) 297 { 298 pcell_t addr_cells; 299 300 /* Find out #address-cells of the superior bus. */ 301 if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells, 302 sizeof(addr_cells)) <= 0) 303 return (2); 304 305 return ((int)fdt32_to_cpu(addr_cells)); 306 } 307 308 u_long 309 fdt_data_get(void *data, int cells) 310 { 311 312 if (cells == 1) 313 return (fdt32_to_cpu(*((uint32_t *)data))); 314 315 return (fdt64_to_cpu(*((uint64_t *)data))); 316 } 317 318 int 319 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells) 320 { 321 pcell_t cell; 322 int cell_size; 323 324 /* 325 * Retrieve #{address,size}-cells. 326 */ 327 cell_size = sizeof(cell); 328 if (OF_getencprop(node, "#address-cells", &cell, cell_size) < cell_size) 329 cell = 2; 330 *addr_cells = (int)cell; 331 332 if (OF_getencprop(node, "#size-cells", &cell, cell_size) < cell_size) 333 cell = 1; 334 *size_cells = (int)cell; 335 336 if (*addr_cells > 3 || *size_cells > 2) 337 return (ERANGE); 338 return (0); 339 } 340 341 int 342 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start, 343 u_long *count) 344 { 345 346 /* Address portion. */ 347 if (addr_cells > 2) 348 return (ERANGE); 349 350 *start = fdt_data_get((void *)data, addr_cells); 351 data += addr_cells; 352 353 /* Size portion. */ 354 if (size_cells > 2) 355 return (ERANGE); 356 357 *count = fdt_data_get((void *)data, size_cells); 358 return (0); 359 } 360 361 int 362 fdt_regsize(phandle_t node, u_long *base, u_long *size) 363 { 364 pcell_t reg[4]; 365 int addr_cells, len, size_cells; 366 367 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells)) 368 return (ENXIO); 369 370 if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg)) 371 return (ENOMEM); 372 373 len = OF_getprop(node, "reg", ®, sizeof(reg)); 374 if (len <= 0) 375 return (EINVAL); 376 377 *base = fdt_data_get(®[0], addr_cells); 378 *size = fdt_data_get(®[addr_cells], size_cells); 379 return (0); 380 } 381 382 int 383 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc) 384 { 385 phandle_t phy_node; 386 pcell_t phy_handle, phy_reg; 387 uint32_t i; 388 device_t parent, child; 389 390 if (OF_getencprop(node, "phy-handle", (void *)&phy_handle, 391 sizeof(phy_handle)) <= 0) 392 return (ENXIO); 393 394 phy_node = OF_node_from_xref(phy_handle); 395 396 if (OF_getencprop(phy_node, "reg", (void *)&phy_reg, 397 sizeof(phy_reg)) <= 0) 398 return (ENXIO); 399 400 *phy_addr = phy_reg; 401 402 /* 403 * Search for softc used to communicate with phy. 404 */ 405 406 /* 407 * Step 1: Search for ancestor of the phy-node with a "phy-handle" 408 * property set. 409 */ 410 phy_node = OF_parent(phy_node); 411 while (phy_node != 0) { 412 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle, 413 sizeof(phy_handle)) > 0) 414 break; 415 phy_node = OF_parent(phy_node); 416 } 417 if (phy_node == 0) 418 return (ENXIO); 419 420 /* 421 * Step 2: For each device with the same parent and name as ours 422 * compare its node with the one found in step 1, ancestor of phy 423 * node (stored in phy_node). 424 */ 425 parent = device_get_parent(dev); 426 i = 0; 427 child = device_find_child(parent, device_get_name(dev), i); 428 while (child != NULL) { 429 if (ofw_bus_get_node(child) == phy_node) 430 break; 431 i++; 432 child = device_find_child(parent, device_get_name(dev), i); 433 } 434 if (child == NULL) 435 return (ENXIO); 436 437 /* 438 * Use softc of the device found. 439 */ 440 *phy_sc = (void *)device_get_softc(child); 441 442 return (0); 443 } 444 445 int 446 fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt) 447 { 448 pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS]; 449 pcell_t *reservep; 450 phandle_t memory, root; 451 int addr_cells, size_cells; 452 int i, res_len, rv, tuple_size, tuples; 453 454 root = OF_finddevice("/"); 455 memory = OF_finddevice("/memory"); 456 if (memory == -1) { 457 rv = ENXIO; 458 goto out; 459 } 460 461 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 462 &size_cells)) != 0) 463 goto out; 464 465 if (addr_cells > 2) { 466 rv = ERANGE; 467 goto out; 468 } 469 470 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 471 472 res_len = OF_getproplen(root, "memreserve"); 473 if (res_len <= 0 || res_len > sizeof(reserve)) { 474 rv = ERANGE; 475 goto out; 476 } 477 478 if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) { 479 rv = ENXIO; 480 goto out; 481 } 482 483 tuples = res_len / tuple_size; 484 reservep = (pcell_t *)&reserve; 485 for (i = 0; i < tuples; i++) { 486 487 rv = fdt_data_to_res(reservep, addr_cells, size_cells, 488 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size); 489 490 if (rv != 0) 491 goto out; 492 493 reservep += addr_cells + size_cells; 494 } 495 496 *mrcnt = i; 497 rv = 0; 498 out: 499 return (rv); 500 } 501 502 int 503 fdt_get_reserved_mem(struct mem_region *reserved, int *mreserved) 504 { 505 pcell_t reg[FDT_REG_CELLS]; 506 phandle_t child, root; 507 int addr_cells, size_cells; 508 int i, rv; 509 510 root = OF_finddevice("/reserved-memory"); 511 if (root == -1) { 512 return (ENXIO); 513 } 514 515 if ((rv = fdt_addrsize_cells(root, &addr_cells, &size_cells)) != 0) 516 return (rv); 517 518 if (addr_cells + size_cells > FDT_REG_CELLS) 519 panic("Too many address and size cells %d %d", addr_cells, 520 size_cells); 521 522 i = 0; 523 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 524 if (!OF_hasprop(child, "no-map")) 525 continue; 526 527 rv = OF_getprop(child, "reg", reg, sizeof(reg)); 528 if (rv <= 0) 529 /* XXX: Does a no-map of a dynamic range make sense? */ 530 continue; 531 532 fdt_data_to_res(reg, addr_cells, size_cells, 533 (u_long *)&reserved[i].mr_start, 534 (u_long *)&reserved[i].mr_size); 535 i++; 536 } 537 538 *mreserved = i; 539 540 return (0); 541 } 542 543 int 544 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint64_t *memsize) 545 { 546 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS]; 547 pcell_t *regp; 548 phandle_t memory; 549 uint64_t memory_size; 550 int addr_cells, size_cells; 551 int i, reg_len, rv, tuple_size, tuples; 552 553 memory = OF_finddevice("/memory"); 554 if (memory == -1) { 555 rv = ENXIO; 556 goto out; 557 } 558 559 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 560 &size_cells)) != 0) 561 goto out; 562 563 if (addr_cells > 2) { 564 rv = ERANGE; 565 goto out; 566 } 567 568 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 569 reg_len = OF_getproplen(memory, "reg"); 570 if (reg_len <= 0 || reg_len > sizeof(reg)) { 571 rv = ERANGE; 572 goto out; 573 } 574 575 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) { 576 rv = ENXIO; 577 goto out; 578 } 579 580 memory_size = 0; 581 tuples = reg_len / tuple_size; 582 regp = (pcell_t *)® 583 for (i = 0; i < tuples; i++) { 584 585 rv = fdt_data_to_res(regp, addr_cells, size_cells, 586 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size); 587 588 if (rv != 0) 589 goto out; 590 591 regp += addr_cells + size_cells; 592 memory_size += mr[i].mr_size; 593 } 594 595 if (memory_size == 0) { 596 rv = ERANGE; 597 goto out; 598 } 599 600 *mrcnt = i; 601 if (memsize != NULL) 602 *memsize = memory_size; 603 rv = 0; 604 out: 605 return (rv); 606 } 607 608 int 609 fdt_get_chosen_bootargs(char *bootargs, size_t max_size) 610 { 611 phandle_t chosen; 612 613 chosen = OF_finddevice("/chosen"); 614 if (chosen == -1) 615 return (ENXIO); 616 if (OF_getprop(chosen, "bootargs", bootargs, max_size) == -1) 617 return (ENXIO); 618 return (0); 619 } 620