1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * Copyright (c) 2013 Google, Inc 4 * 5 * (C) Copyright 2012 6 * Pavel Herrmann <morpheus.ibis@gmail.com> 7 */ 8 9#define LOG_CATEGORY UCLASS_ROOT 10 11#include <common.h> 12#include <errno.h> 13#include <fdtdec.h> 14#include <log.h> 15#include <malloc.h> 16#include <asm-generic/sections.h> 17#include <asm/global_data.h> 18#include <linux/libfdt.h> 19#include <dm/acpi.h> 20#include <dm/device.h> 21#include <dm/device-internal.h> 22#include <dm/lists.h> 23#include <dm/of.h> 24#include <dm/of_access.h> 25#include <dm/platdata.h> 26#include <dm/read.h> 27#include <dm/root.h> 28#include <dm/uclass.h> 29#include <dm/uclass-internal.h> 30#include <dm/util.h> 31#include <linux/list.h> 32#include <linux/printk.h> 33 34DECLARE_GLOBAL_DATA_PTR; 35 36static struct driver_info root_info = { 37 .name = "root_driver", 38}; 39 40struct udevice *dm_root(void) 41{ 42 if (!gd->dm_root) { 43 dm_warn("Virtual root driver does not exist!\n"); 44 return NULL; 45 } 46 47 return gd->dm_root; 48} 49 50void dm_fixup_for_gd_move(struct global_data *new_gd) 51{ 52 /* The sentinel node has moved, so update things that point to it */ 53 if (gd->dm_root) { 54 new_gd->uclass_root->next->prev = new_gd->uclass_root; 55 new_gd->uclass_root->prev->next = new_gd->uclass_root; 56 } 57} 58 59static int dm_setup_inst(void) 60{ 61 DM_ROOT_NON_CONST = DM_DEVICE_GET(root); 62 63 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) { 64 struct udevice_rt *urt; 65 void *start, *end; 66 int each_size; 67 void *base; 68 int n_ents; 69 uint size; 70 71 /* Allocate the udevice_rt table */ 72 each_size = dm_udevice_size(); 73 start = ll_entry_start(struct udevice, udevice); 74 end = ll_entry_end(struct udevice, udevice); 75 size = end - start; 76 n_ents = size / each_size; 77 urt = calloc(n_ents, sizeof(struct udevice_rt)); 78 if (!urt) 79 return log_msg_ret("urt", -ENOMEM); 80 gd_set_dm_udevice_rt(urt); 81 82 /* Now allocate space for the priv/plat data, and copy it in */ 83 size = __priv_data_end - __priv_data_start; 84 85 base = calloc(1, size); 86 if (!base) 87 return log_msg_ret("priv", -ENOMEM); 88 memcpy(base, __priv_data_start, size); 89 gd_set_dm_priv_base(base); 90 } 91 92 return 0; 93} 94 95int dm_init(bool of_live) 96{ 97 int ret; 98 99 if (gd->dm_root) { 100 dm_warn("Virtual root driver already exists!\n"); 101 return -EINVAL; 102 } 103 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { 104 gd->uclass_root = &uclass_head; 105 } else { 106 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST; 107 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST); 108 } 109 110 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { 111 ret = dm_setup_inst(); 112 if (ret) { 113 log_debug("dm_setup_inst() failed: %d\n", ret); 114 return ret; 115 } 116 } else { 117 ret = device_bind_by_name(NULL, false, &root_info, 118 &DM_ROOT_NON_CONST); 119 if (ret) 120 return ret; 121 if (CONFIG_IS_ENABLED(OF_CONTROL)) 122 dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root()); 123 ret = device_probe(DM_ROOT_NON_CONST); 124 if (ret) 125 return ret; 126 } 127 128 INIT_LIST_HEAD((struct list_head *)&gd->dmtag_list); 129 130 return 0; 131} 132 133int dm_uninit(void) 134{ 135 /* Remove non-vital devices first */ 136 device_remove(dm_root(), DM_REMOVE_NON_VITAL); 137 device_remove(dm_root(), DM_REMOVE_NORMAL); 138 device_unbind(dm_root()); 139 gd->dm_root = NULL; 140 141 return 0; 142} 143 144#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) 145int dm_remove_devices_flags(uint flags) 146{ 147 device_remove(dm_root(), flags); 148 149 return 0; 150} 151#endif 152 153int dm_scan_plat(bool pre_reloc_only) 154{ 155 int ret; 156 157 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) { 158 struct driver_rt *dyn; 159 int n_ents; 160 161 n_ents = ll_entry_count(struct driver_info, driver_info); 162 dyn = calloc(n_ents, sizeof(struct driver_rt)); 163 if (!dyn) 164 return -ENOMEM; 165 gd_set_dm_driver_rt(dyn); 166 } 167 168 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only); 169 if (ret == -ENOENT) { 170 dm_warn("Some drivers were not found\n"); 171 ret = 0; 172 } 173 174 return ret; 175} 176 177#if CONFIG_IS_ENABLED(OF_REAL) 178/** 179 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node 180 * 181 * This scans the subnodes of a device tree node and and creates a driver 182 * for each one. 183 * 184 * @parent: Parent device for the devices that will be created 185 * @node: Node to scan 186 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC 187 * flag. If false bind all drivers. 188 * Return: 0 if OK, -ve on error 189 */ 190static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node, 191 bool pre_reloc_only) 192{ 193 int ret = 0, err = 0; 194 ofnode node; 195 196 if (!ofnode_valid(parent_node)) 197 return 0; 198 199 for (node = ofnode_first_subnode(parent_node); 200 ofnode_valid(node); 201 node = ofnode_next_subnode(node)) { 202 const char *node_name = ofnode_get_name(node); 203 204 if (!ofnode_is_enabled(node)) { 205 pr_debug(" - ignoring disabled device\n"); 206 continue; 207 } 208 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only); 209 if (err && !ret) { 210 ret = err; 211 debug("%s: ret=%d\n", node_name, ret); 212 } 213 } 214 215 if (ret) 216 dm_warn("Some drivers failed to bind\n"); 217 218 return ret; 219} 220 221int dm_scan_fdt_dev(struct udevice *dev) 222{ 223 return dm_scan_fdt_node(dev, dev_ofnode(dev), 224 gd->flags & GD_FLG_RELOC ? false : true); 225} 226 227int dm_scan_fdt(bool pre_reloc_only) 228{ 229 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only); 230} 231 232static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only) 233{ 234 ofnode node; 235 236 node = ofnode_path(path); 237 238 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only); 239} 240 241int dm_extended_scan(bool pre_reloc_only) 242{ 243 int ret, i; 244 const char * const nodes[] = { 245 "/chosen", 246 "/clocks", 247 "/firmware" 248 }; 249 250 ret = dm_scan_fdt(pre_reloc_only); 251 if (ret) { 252 debug("dm_scan_fdt() failed: %d\n", ret); 253 return ret; 254 } 255 256 /* Some nodes aren't devices themselves but may contain some */ 257 for (i = 0; i < ARRAY_SIZE(nodes); i++) { 258 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only); 259 if (ret) { 260 debug("dm_scan_fdt() scan for %s failed: %d\n", 261 nodes[i], ret); 262 return ret; 263 } 264 } 265 266 return ret; 267} 268#endif 269 270__weak int dm_scan_other(bool pre_reloc_only) 271{ 272 return 0; 273} 274 275#if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY) 276void *dm_priv_to_rw(void *priv) 277{ 278 long offset = priv - (void *)__priv_data_start; 279 280 return gd_dm_priv_base() + offset; 281} 282#endif 283 284static int dm_probe_devices(struct udevice *dev, bool pre_reloc_only) 285{ 286 ofnode node = dev_ofnode(dev); 287 struct udevice *child; 288 int ret; 289 290 if (pre_reloc_only && 291 (!ofnode_valid(node) || !ofnode_pre_reloc(node)) && 292 !(dev->driver->flags & DM_FLAG_PRE_RELOC)) 293 goto probe_children; 294 295 if (dev_get_flags(dev) & DM_FLAG_PROBE_AFTER_BIND) { 296 ret = device_probe(dev); 297 if (ret) 298 return ret; 299 } 300 301probe_children: 302 list_for_each_entry(child, &dev->child_head, sibling_node) 303 dm_probe_devices(child, pre_reloc_only); 304 305 return 0; 306} 307 308/** 309 * dm_scan() - Scan tables to bind devices 310 * 311 * Runs through the driver_info tables and binds the devices it finds. Then runs 312 * through the devicetree nodes. Finally calls dm_scan_other() to add any 313 * special devices 314 * 315 * @pre_reloc_only: If true, bind only nodes with special devicetree properties, 316 * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. 317 */ 318static int dm_scan(bool pre_reloc_only) 319{ 320 int ret; 321 322 ret = dm_scan_plat(pre_reloc_only); 323 if (ret) { 324 debug("dm_scan_plat() failed: %d\n", ret); 325 return ret; 326 } 327 328 if (CONFIG_IS_ENABLED(OF_REAL)) { 329 ret = dm_extended_scan(pre_reloc_only); 330 if (ret) { 331 debug("dm_extended_scan() failed: %d\n", ret); 332 return ret; 333 } 334 } 335 336 ret = dm_scan_other(pre_reloc_only); 337 if (ret) 338 return ret; 339 340 return dm_probe_devices(gd->dm_root, pre_reloc_only); 341} 342 343int dm_init_and_scan(bool pre_reloc_only) 344{ 345 int ret; 346 347 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE)); 348 if (ret) { 349 debug("dm_init() failed: %d\n", ret); 350 return ret; 351 } 352 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { 353 ret = dm_scan(pre_reloc_only); 354 if (ret) { 355 log_debug("dm_scan() failed: %d\n", ret); 356 return ret; 357 } 358 } 359 if (CONFIG_IS_ENABLED(DM_EVENT)) { 360 ret = event_notify_null(gd->flags & GD_FLG_RELOC ? 361 EVT_DM_POST_INIT_R : 362 EVT_DM_POST_INIT_F); 363 if (ret) 364 return log_msg_ret("ev", ret); 365 } 366 367 return 0; 368} 369 370void dm_get_stats(int *device_countp, int *uclass_countp) 371{ 372 *device_countp = device_get_decendent_count(gd->dm_root); 373 *uclass_countp = uclass_get_count(); 374} 375 376void dev_collect_stats(struct dm_stats *stats, const struct udevice *parent) 377{ 378 const struct udevice *dev; 379 int i; 380 381 stats->dev_count++; 382 stats->dev_size += sizeof(struct udevice); 383 stats->dev_name_size += strlen(parent->name) + 1; 384 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) { 385 int size = dev_get_attach_size(parent, i); 386 387 if (size || 388 (i == DM_TAG_DRIVER_DATA && parent->driver_data)) { 389 stats->attach_count[i]++; 390 stats->attach_size[i] += size; 391 stats->attach_count_total++; 392 stats->attach_size_total += size; 393 } 394 } 395 396 list_for_each_entry(dev, &parent->child_head, sibling_node) 397 dev_collect_stats(stats, dev); 398} 399 400void uclass_collect_stats(struct dm_stats *stats) 401{ 402 struct uclass *uc; 403 404 list_for_each_entry(uc, gd->uclass_root, sibling_node) { 405 int size; 406 407 stats->uc_count++; 408 stats->uc_size += sizeof(struct uclass); 409 size = uc->uc_drv->priv_auto; 410 if (size) { 411 stats->uc_attach_count++; 412 stats->uc_attach_size += size; 413 } 414 } 415} 416 417void dm_get_mem(struct dm_stats *stats) 418{ 419 memset(stats, '\0', sizeof(*stats)); 420 dev_collect_stats(stats, gd->dm_root); 421 uclass_collect_stats(stats); 422 dev_tag_collect_stats(stats); 423 424 stats->total_size = stats->dev_size + stats->uc_size + 425 stats->attach_size_total + stats->uc_attach_size + 426 stats->tag_size; 427} 428 429#if CONFIG_IS_ENABLED(ACPIGEN) 430static int root_acpi_get_name(const struct udevice *dev, char *out_name) 431{ 432 return acpi_copy_name(out_name, "\\_SB"); 433} 434 435struct acpi_ops root_acpi_ops = { 436 .get_name = root_acpi_get_name, 437}; 438#endif 439 440/* This is the root driver - all drivers are children of this */ 441U_BOOT_DRIVER(root_driver) = { 442 .name = "root_driver", 443 .id = UCLASS_ROOT, 444 ACPI_OPS_PTR(&root_acpi_ops) 445}; 446 447/* This is the root uclass */ 448UCLASS_DRIVER(root) = { 449 .name = "root", 450 .id = UCLASS_ROOT, 451}; 452