ofw_bus_subr.c revision 291648
1/*- 2 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>. 3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_bus_subr.c 291648 2015-12-02 14:21:16Z mmel $"); 32 33#include "opt_platform.h" 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/bus.h> 37#include <sys/errno.h> 38#include <sys/libkern.h> 39 40#include <machine/resource.h> 41 42#include <dev/ofw/ofw_bus.h> 43#include <dev/ofw/ofw_bus_subr.h> 44#include <dev/ofw/openfirm.h> 45 46#include "ofw_bus_if.h" 47 48int 49ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node) 50{ 51 52 if (obd == NULL) 53 return (ENOMEM); 54 /* The 'name' property is considered mandatory. */ 55 if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1) 56 return (EINVAL); 57 OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat); 58 OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type); 59 OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model); 60 OF_getprop_alloc(node, "status", 1, (void **)&obd->obd_status); 61 obd->obd_node = node; 62 return (0); 63} 64 65void 66ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd) 67{ 68 69 if (obd == NULL) 70 return; 71 if (obd->obd_compat != NULL) 72 free(obd->obd_compat, M_OFWPROP); 73 if (obd->obd_model != NULL) 74 free(obd->obd_model, M_OFWPROP); 75 if (obd->obd_name != NULL) 76 free(obd->obd_name, M_OFWPROP); 77 if (obd->obd_type != NULL) 78 free(obd->obd_type, M_OFWPROP); 79 if (obd->obd_status != NULL) 80 free(obd->obd_status, M_OFWPROP); 81} 82 83int 84ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf, 85 size_t buflen) 86{ 87 88 if (ofw_bus_get_name(child) != NULL) { 89 strlcat(buf, "name=", buflen); 90 strlcat(buf, ofw_bus_get_name(child), buflen); 91 } 92 93 if (ofw_bus_get_compat(child) != NULL) { 94 strlcat(buf, " compat=", buflen); 95 strlcat(buf, ofw_bus_get_compat(child), buflen); 96 } 97 return (0); 98}; 99 100const char * 101ofw_bus_gen_get_compat(device_t bus, device_t dev) 102{ 103 const struct ofw_bus_devinfo *obd; 104 105 obd = OFW_BUS_GET_DEVINFO(bus, dev); 106 if (obd == NULL) 107 return (NULL); 108 return (obd->obd_compat); 109} 110 111const char * 112ofw_bus_gen_get_model(device_t bus, device_t dev) 113{ 114 const struct ofw_bus_devinfo *obd; 115 116 obd = OFW_BUS_GET_DEVINFO(bus, dev); 117 if (obd == NULL) 118 return (NULL); 119 return (obd->obd_model); 120} 121 122const char * 123ofw_bus_gen_get_name(device_t bus, device_t dev) 124{ 125 const struct ofw_bus_devinfo *obd; 126 127 obd = OFW_BUS_GET_DEVINFO(bus, dev); 128 if (obd == NULL) 129 return (NULL); 130 return (obd->obd_name); 131} 132 133phandle_t 134ofw_bus_gen_get_node(device_t bus, device_t dev) 135{ 136 const struct ofw_bus_devinfo *obd; 137 138 obd = OFW_BUS_GET_DEVINFO(bus, dev); 139 if (obd == NULL) 140 return (0); 141 return (obd->obd_node); 142} 143 144const char * 145ofw_bus_gen_get_type(device_t bus, device_t dev) 146{ 147 const struct ofw_bus_devinfo *obd; 148 149 obd = OFW_BUS_GET_DEVINFO(bus, dev); 150 if (obd == NULL) 151 return (NULL); 152 return (obd->obd_type); 153} 154 155const char * 156ofw_bus_get_status(device_t dev) 157{ 158 const struct ofw_bus_devinfo *obd; 159 160 obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev); 161 if (obd == NULL) 162 return (NULL); 163 164 return (obd->obd_status); 165} 166 167int 168ofw_bus_status_okay(device_t dev) 169{ 170 const char *status; 171 172 status = ofw_bus_get_status(dev); 173 if (status == NULL || strcmp(status, "okay") == 0 || 174 strcmp(status, "ok") == 0) 175 return (1); 176 177 return (0); 178} 179 180static int 181ofw_bus_node_is_compatible(const char *compat, int len, const char *onecompat) 182{ 183 int onelen, l, ret; 184 185 onelen = strlen(onecompat); 186 187 ret = 0; 188 while (len > 0) { 189 if (strlen(compat) == onelen && 190 strncasecmp(compat, onecompat, onelen) == 0) { 191 /* Found it. */ 192 ret = 1; 193 break; 194 } 195 196 /* Slide to the next sub-string. */ 197 l = strlen(compat) + 1; 198 compat += l; 199 len -= l; 200 } 201 202 return (ret); 203} 204 205int 206ofw_bus_is_compatible(device_t dev, const char *onecompat) 207{ 208 phandle_t node; 209 const char *compat; 210 int len; 211 212 if ((compat = ofw_bus_get_compat(dev)) == NULL) 213 return (0); 214 215 if ((node = ofw_bus_get_node(dev)) == -1) 216 return (0); 217 218 /* Get total 'compatible' prop len */ 219 if ((len = OF_getproplen(node, "compatible")) <= 0) 220 return (0); 221 222 return (ofw_bus_node_is_compatible(compat, len, onecompat)); 223} 224 225int 226ofw_bus_is_compatible_strict(device_t dev, const char *compatible) 227{ 228 const char *compat; 229 size_t len; 230 231 if ((compat = ofw_bus_get_compat(dev)) == NULL) 232 return (0); 233 234 len = strlen(compatible); 235 if (strlen(compat) == len && 236 strncasecmp(compat, compatible, len) == 0) 237 return (1); 238 239 return (0); 240} 241 242const struct ofw_compat_data * 243ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat) 244{ 245 246 if (compat == NULL) 247 return NULL; 248 249 for (; compat->ocd_str != NULL; ++compat) { 250 if (ofw_bus_is_compatible(dev, compat->ocd_str)) 251 break; 252 } 253 254 return (compat); 255} 256 257int 258ofw_bus_has_prop(device_t dev, const char *propname) 259{ 260 phandle_t node; 261 262 if ((node = ofw_bus_get_node(dev)) == -1) 263 return (0); 264 265 return (OF_hasprop(node, propname)); 266} 267 268void 269ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz) 270{ 271 pcell_t addrc; 272 int msksz; 273 274 if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1) 275 addrc = 2; 276 ii->opi_addrc = addrc * sizeof(pcell_t); 277 278 ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1, 279 (void **)&ii->opi_imap); 280 if (ii->opi_imapsz > 0) { 281 msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1, 282 (void **)&ii->opi_imapmsk); 283 /* 284 * Failure to get the mask is ignored; a full mask is used 285 * then. We barf on bad mask sizes, however. 286 */ 287 if (msksz != -1 && msksz != ii->opi_addrc + intrsz) 288 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask " 289 "property!"); 290 } 291} 292 293int 294ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg, 295 int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz, 296 phandle_t *iparent) 297{ 298 uint8_t maskbuf[regsz + pintrsz]; 299 int rv; 300 301 if (ii->opi_imapsz <= 0) 302 return (0); 303 KASSERT(regsz >= ii->opi_addrc, 304 ("ofw_bus_lookup_imap: register size too small: %d < %d", 305 regsz, ii->opi_addrc)); 306 if (node != -1) { 307 rv = OF_getencprop(node, "reg", reg, regsz); 308 if (rv < regsz) 309 panic("ofw_bus_lookup_imap: cannot get reg property"); 310 } 311 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc, 312 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr, 313 mintrsz, iparent)); 314} 315 316/* 317 * Map an interrupt using the firmware reg, interrupt-map and 318 * interrupt-map-mask properties. 319 * The interrupt property to be mapped must be of size intrsz, and pointed to 320 * by intr. The regs property of the node for which the mapping is done must 321 * be passed as regs. This property is an array of register specifications; 322 * the size of the address part of such a specification must be passed as 323 * physsz. Only the first element of the property is used. 324 * imap and imapsz hold the interrupt mask and it's size. 325 * imapmsk is a pointer to the interrupt-map-mask property, which must have 326 * a size of physsz + intrsz; it may be NULL, in which case a full mask is 327 * assumed. 328 * maskbuf must point to a buffer of length physsz + intrsz. 329 * The interrupt is returned in result, which must point to a buffer of length 330 * rintrsz (which gives the expected size of the mapped interrupt). 331 * Returns number of cells in the interrupt if a mapping was found, 0 otherwise. 332 */ 333int 334ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz, 335 void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result, 336 int rintrsz, phandle_t *iparent) 337{ 338 phandle_t parent; 339 uint8_t *ref = maskbuf; 340 uint8_t *uiintr = intr; 341 uint8_t *uiregs = regs; 342 uint8_t *uiimapmsk = imapmsk; 343 uint8_t *mptr; 344 pcell_t pintrsz; 345 int i, rsz, tsz; 346 347 rsz = -1; 348 if (imapmsk != NULL) { 349 for (i = 0; i < physsz; i++) 350 ref[i] = uiregs[i] & uiimapmsk[i]; 351 for (i = 0; i < intrsz; i++) 352 ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i]; 353 } else { 354 bcopy(regs, ref, physsz); 355 bcopy(intr, ref + physsz, intrsz); 356 } 357 358 mptr = imap; 359 i = imapsz; 360 while (i > 0) { 361 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent)); 362 if (OF_searchencprop(OF_node_from_xref(parent), 363 "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1) 364 pintrsz = 1; /* default */ 365 pintrsz *= sizeof(pcell_t); 366 367 /* Compute the map stride size. */ 368 tsz = physsz + intrsz + sizeof(phandle_t) + pintrsz; 369 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map")); 370 371 if (bcmp(ref, mptr, physsz + intrsz) == 0) { 372 bcopy(mptr + physsz + intrsz + sizeof(parent), 373 result, MIN(rintrsz, pintrsz)); 374 375 if (iparent != NULL) 376 *iparent = parent; 377 return (pintrsz/sizeof(pcell_t)); 378 } 379 mptr += tsz; 380 i -= tsz; 381 } 382 return (0); 383} 384 385int 386ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells, 387 struct resource_list *rl) 388{ 389 uint64_t phys, size; 390 ssize_t i, j, rid, nreg, ret; 391 uint32_t *reg; 392 char *name; 393 394 /* 395 * This may be just redundant when having ofw_bus_devinfo 396 * but makes this routine independent of it. 397 */ 398 ret = OF_getprop_alloc(node, "name", sizeof(*name), (void **)&name); 399 if (ret == -1) 400 name = NULL; 401 402 ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®); 403 nreg = (ret == -1) ? 0 : ret; 404 405 if (nreg % (acells + scells) != 0) { 406 if (bootverbose) 407 device_printf(dev, "Malformed reg property on <%s>\n", 408 (name == NULL) ? "unknown" : name); 409 nreg = 0; 410 } 411 412 for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) { 413 phys = size = 0; 414 for (j = 0; j < acells; j++) { 415 phys <<= 32; 416 phys |= reg[i + j]; 417 } 418 for (j = 0; j < scells; j++) { 419 size <<= 32; 420 size |= reg[i + acells + j]; 421 } 422 /* Skip the dummy reg property of glue devices like ssm(4). */ 423 if (size != 0) 424 resource_list_add(rl, SYS_RES_MEMORY, rid, 425 phys, phys + size - 1, size); 426 } 427 free(name, M_OFWPROP); 428 free(reg, M_OFWPROP); 429 430 return (0); 431} 432 433/* 434 * Get interrupt parent for given node. 435 * Returns 0 if interrupt parent doesn't exist. 436 */ 437phandle_t 438ofw_bus_find_iparent(phandle_t node) 439{ 440 phandle_t iparent; 441 442 if (OF_searchencprop(node, "interrupt-parent", &iparent, 443 sizeof(iparent)) == -1) { 444 for (iparent = node; iparent != 0; 445 iparent = OF_parent(iparent)) { 446 if (OF_hasprop(iparent, "interrupt-controller")) 447 break; 448 } 449 iparent = OF_xref_from_node(iparent); 450 } 451 return (iparent); 452} 453 454int 455ofw_bus_intr_to_rl(device_t dev, phandle_t node, 456 struct resource_list *rl, int *rlen) 457{ 458 phandle_t iparent; 459 uint32_t icells, *intr; 460 int err, i, irqnum, nintr, rid; 461 boolean_t extended; 462 463 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr), 464 (void **)&intr); 465 if (nintr > 0) { 466 iparent = ofw_bus_find_iparent(node); 467 if (iparent == 0) { 468 device_printf(dev, "No interrupt-parent found, " 469 "assuming direct parent\n"); 470 iparent = OF_parent(node); 471 iparent = OF_xref_from_node(iparent); 472 } 473 if (OF_searchencprop(OF_node_from_xref(iparent), 474 "#interrupt-cells", &icells, sizeof(icells)) == -1) { 475 device_printf(dev, "Missing #interrupt-cells " 476 "property, assuming <1>\n"); 477 icells = 1; 478 } 479 if (icells < 1 || icells > nintr) { 480 device_printf(dev, "Invalid #interrupt-cells property " 481 "value <%d>, assuming <1>\n", icells); 482 icells = 1; 483 } 484 extended = false; 485 } else { 486 nintr = OF_getencprop_alloc(node, "interrupts-extended", 487 sizeof(*intr), (void **)&intr); 488 if (nintr <= 0) 489 return (0); 490 extended = true; 491 } 492 err = 0; 493 rid = 0; 494 for (i = 0; i < nintr; i += icells) { 495 if (extended) { 496 iparent = intr[i++]; 497 if (OF_searchencprop(OF_node_from_xref(iparent), 498 "#interrupt-cells", &icells, sizeof(icells)) == -1) { 499 device_printf(dev, "Missing #interrupt-cells " 500 "property\n"); 501 err = ENOENT; 502 break; 503 } 504 if (icells < 1 || (i + icells) > nintr) { 505 device_printf(dev, "Invalid #interrupt-cells " 506 "property value <%d>\n", icells); 507 err = ERANGE; 508 break; 509 } 510 } 511 irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]); 512 resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1); 513 } 514 if (rlen != NULL) 515 *rlen = rid; 516 free(intr, M_OFWPROP); 517 return (err); 518} 519 520phandle_t 521ofw_bus_find_child(phandle_t start, const char *child_name) 522{ 523 char *name; 524 int ret; 525 phandle_t child; 526 527 for (child = OF_child(start); child != 0; child = OF_peer(child)) { 528 ret = OF_getprop_alloc(child, "name", sizeof(*name), (void **)&name); 529 if (ret == -1) 530 continue; 531 if (strcmp(name, child_name) == 0) { 532 free(name, M_OFWPROP); 533 return (child); 534 } 535 536 free(name, M_OFWPROP); 537 } 538 539 return (0); 540} 541 542phandle_t 543ofw_bus_find_compatible(phandle_t node, const char *onecompat) 544{ 545 phandle_t child, ret; 546 void *compat; 547 int len; 548 549 /* 550 * Traverse all children of 'start' node, and find first with 551 * matching 'compatible' property. 552 */ 553 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 554 len = OF_getprop_alloc(child, "compatible", 1, &compat); 555 if (len >= 0) { 556 ret = ofw_bus_node_is_compatible(compat, len, 557 onecompat); 558 free(compat, M_OFWPROP); 559 if (ret != 0) 560 return (child); 561 } 562 563 ret = ofw_bus_find_compatible(child, onecompat); 564 if (ret != 0) 565 return (ret); 566 } 567 return (0); 568} 569 570/** 571 * @brief Return child of bus whose phandle is node 572 * 573 * A direct child of @p will be returned if it its phandle in the 574 * OFW tree is @p node. Otherwise, NULL is returned. 575 * 576 * @param bus The bus to examine 577 * @param node The phandle_t to look for. 578 */ 579device_t 580ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node) 581{ 582 device_t *children, retval, child; 583 int nkid, i; 584 585 /* 586 * Nothing can match the flag value for no node. 587 */ 588 if (node == -1) 589 return (NULL); 590 591 /* 592 * Search the children for a match. We microoptimize 593 * a bit by not using ofw_bus_get since we already know 594 * the parent. We do not recurse. 595 */ 596 if (device_get_children(bus, &children, &nkid) != 0) 597 return (NULL); 598 retval = NULL; 599 for (i = 0; i < nkid; i++) { 600 child = children[i]; 601 if (OFW_BUS_GET_NODE(bus, child) == node) { 602 retval = child; 603 break; 604 } 605 } 606 free(children, M_TEMP); 607 608 return (retval); 609} 610