1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009-2014 The FreeBSD Foundation 5 * 6 * This software was developed by Andrew Turner under sponsorship from 7 * the FreeBSD Foundation. 8 * This software was developed by Semihalf under sponsorship from 9 * the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/limits.h> 42 #include <sys/sysctl.h> 43 44 #include <machine/resource.h> 45 46 #include <dev/fdt/fdt_common.h> 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 #include <dev/ofw/openfirm.h> 50 51 #include "ofw_bus_if.h" 52 53 #ifdef DEBUG 54 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 55 printf(fmt,##args); } while (0) 56 #else 57 #define debugf(fmt, args...) 58 #endif 59 60 #define FDT_COMPAT_LEN 255 61 62 #define FDT_REG_CELLS 4 63 #define FDT_RANGES_SIZE 48 64 65 SYSCTL_NODE(_hw, OID_AUTO, fdt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 66 "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 if (phy_sc == NULL) 403 return (0); 404 405 /* 406 * Search for softc used to communicate with phy. 407 */ 408 409 /* 410 * Step 1: Search for ancestor of the phy-node with a "phy-handle" 411 * property set. 412 */ 413 phy_node = OF_parent(phy_node); 414 while (phy_node != 0) { 415 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle, 416 sizeof(phy_handle)) > 0) 417 break; 418 phy_node = OF_parent(phy_node); 419 } 420 if (phy_node == 0) 421 return (ENXIO); 422 423 /* 424 * Step 2: For each device with the same parent and name as ours 425 * compare its node with the one found in step 1, ancestor of phy 426 * node (stored in phy_node). 427 */ 428 parent = device_get_parent(dev); 429 i = 0; 430 child = device_find_child(parent, device_get_name(dev), i); 431 while (child != NULL) { 432 if (ofw_bus_get_node(child) == phy_node) 433 break; 434 i++; 435 child = device_find_child(parent, device_get_name(dev), i); 436 } 437 if (child == NULL) 438 return (ENXIO); 439 440 /* 441 * Use softc of the device found. 442 */ 443 *phy_sc = (void *)device_get_softc(child); 444 445 return (0); 446 } 447 448 int 449 fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt) 450 { 451 pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS]; 452 pcell_t *reservep; 453 phandle_t memory, root; 454 int addr_cells, size_cells; 455 int i, res_len, rv, tuple_size, tuples; 456 457 root = OF_finddevice("/"); 458 memory = OF_finddevice("/memory"); 459 if (memory == -1) { 460 rv = ENXIO; 461 goto out; 462 } 463 464 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 465 &size_cells)) != 0) 466 goto out; 467 468 if (addr_cells > 2) { 469 rv = ERANGE; 470 goto out; 471 } 472 473 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 474 475 res_len = OF_getproplen(root, "memreserve"); 476 if (res_len <= 0 || res_len > sizeof(reserve)) { 477 rv = ERANGE; 478 goto out; 479 } 480 481 if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) { 482 rv = ENXIO; 483 goto out; 484 } 485 486 tuples = res_len / tuple_size; 487 reservep = (pcell_t *)&reserve; 488 for (i = 0; i < tuples; i++) { 489 490 rv = fdt_data_to_res(reservep, addr_cells, size_cells, 491 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size); 492 493 if (rv != 0) 494 goto out; 495 496 reservep += addr_cells + size_cells; 497 } 498 499 *mrcnt = i; 500 rv = 0; 501 out: 502 return (rv); 503 } 504 505 int 506 fdt_get_reserved_mem(struct mem_region *reserved, int *mreserved) 507 { 508 pcell_t reg[FDT_REG_CELLS]; 509 phandle_t child, root; 510 int addr_cells, size_cells; 511 int i, rv; 512 513 root = OF_finddevice("/reserved-memory"); 514 if (root == -1) { 515 return (ENXIO); 516 } 517 518 if ((rv = fdt_addrsize_cells(root, &addr_cells, &size_cells)) != 0) 519 return (rv); 520 521 if (addr_cells + size_cells > FDT_REG_CELLS) 522 panic("Too many address and size cells %d %d", addr_cells, 523 size_cells); 524 525 i = 0; 526 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 527 if (!OF_hasprop(child, "no-map")) 528 continue; 529 530 rv = OF_getprop(child, "reg", reg, sizeof(reg)); 531 if (rv <= 0) 532 /* XXX: Does a no-map of a dynamic range make sense? */ 533 continue; 534 535 fdt_data_to_res(reg, addr_cells, size_cells, 536 (u_long *)&reserved[i].mr_start, 537 (u_long *)&reserved[i].mr_size); 538 i++; 539 } 540 541 *mreserved = i; 542 543 return (0); 544 } 545 546 int 547 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint64_t *memsize) 548 { 549 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS]; 550 pcell_t *regp; 551 phandle_t memory; 552 uint64_t memory_size; 553 int addr_cells, size_cells; 554 int i, reg_len, rv, tuple_size, tuples; 555 556 memory = OF_finddevice("/memory"); 557 if (memory == -1) { 558 rv = ENXIO; 559 goto out; 560 } 561 562 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 563 &size_cells)) != 0) 564 goto out; 565 566 if (addr_cells > 2) { 567 rv = ERANGE; 568 goto out; 569 } 570 571 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 572 reg_len = OF_getproplen(memory, "reg"); 573 if (reg_len <= 0 || reg_len > sizeof(reg)) { 574 rv = ERANGE; 575 goto out; 576 } 577 578 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) { 579 rv = ENXIO; 580 goto out; 581 } 582 583 memory_size = 0; 584 tuples = reg_len / tuple_size; 585 regp = (pcell_t *)® 586 for (i = 0; i < tuples; i++) { 587 588 rv = fdt_data_to_res(regp, addr_cells, size_cells, 589 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size); 590 591 if (rv != 0) 592 goto out; 593 594 regp += addr_cells + size_cells; 595 memory_size += mr[i].mr_size; 596 } 597 598 if (memory_size == 0) { 599 rv = ERANGE; 600 goto out; 601 } 602 603 *mrcnt = i; 604 if (memsize != NULL) 605 *memsize = memory_size; 606 rv = 0; 607 out: 608 return (rv); 609 } 610 611 int 612 fdt_get_chosen_bootargs(char *bootargs, size_t max_size) 613 { 614 phandle_t chosen; 615 616 chosen = OF_finddevice("/chosen"); 617 if (chosen == -1) 618 return (ENXIO); 619 if (OF_getprop(chosen, "bootargs", bootargs, max_size) == -1) 620 return (ENXIO); 621 return (0); 622 } 623