1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
9 #define LOG_CATEGORY UCLASS_ROOT
15 #include <asm-generic/sections.h>
16 #include <asm/global_data.h>
17 #include <linux/libfdt.h>
19 #include <dm/device.h>
20 #include <dm/device-internal.h>
23 #include <dm/of_access.h>
24 #include <dm/platdata.h>
27 #include <dm/uclass.h>
28 #include <dm/uclass-internal.h>
30 #include <linux/list.h>
31 #include <linux/printk.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 static struct driver_info root_info = {
36 .name = "root_driver",
39 struct udevice *dm_root(void)
42 dm_warn("Virtual root driver does not exist!\n");
49 void dm_fixup_for_gd_move(struct global_data *new_gd)
51 /* The sentinel node has moved, so update things that point to it */
53 new_gd->uclass_root->next->prev = new_gd->uclass_root;
54 new_gd->uclass_root->prev->next = new_gd->uclass_root;
58 static int dm_setup_inst(void)
60 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
62 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
63 struct udevice_rt *urt;
70 /* Allocate the udevice_rt table */
71 each_size = dm_udevice_size();
72 start = ll_entry_start(struct udevice, udevice);
73 end = ll_entry_end(struct udevice, udevice);
75 n_ents = size / each_size;
76 urt = calloc(n_ents, sizeof(struct udevice_rt));
78 return log_msg_ret("urt", -ENOMEM);
79 gd_set_dm_udevice_rt(urt);
81 /* Now allocate space for the priv/plat data, and copy it in */
82 size = __priv_data_end - __priv_data_start;
84 base = calloc(1, size);
86 return log_msg_ret("priv", -ENOMEM);
87 memcpy(base, __priv_data_start, size);
88 gd_set_dm_priv_base(base);
94 int dm_init(bool of_live)
99 dm_warn("Virtual root driver already exists!\n");
102 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
103 gd->uclass_root = &uclass_head;
105 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
106 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
109 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
110 ret = dm_setup_inst();
112 log_debug("dm_setup_inst() failed: %d\n", ret);
116 ret = device_bind_by_name(NULL, false, &root_info,
120 if (CONFIG_IS_ENABLED(OF_CONTROL))
121 dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
122 ret = device_probe(DM_ROOT_NON_CONST);
127 INIT_LIST_HEAD((struct list_head *)&gd->dmtag_list);
134 /* Remove non-vital devices first */
135 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
136 device_remove(dm_root(), DM_REMOVE_NORMAL);
137 device_unbind(dm_root());
143 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
144 int dm_remove_devices_flags(uint flags)
146 device_remove(dm_root(), flags);
151 void dm_remove_devices_active(void)
153 /* Remove non-vital devices first */
154 device_remove(dm_root(), DM_REMOVE_ACTIVE_ALL | DM_REMOVE_NON_VITAL);
155 device_remove(dm_root(), DM_REMOVE_ACTIVE_ALL);
159 int dm_scan_plat(bool pre_reloc_only)
163 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
164 struct driver_rt *dyn;
167 n_ents = ll_entry_count(struct driver_info, driver_info);
168 dyn = calloc(n_ents, sizeof(struct driver_rt));
171 gd_set_dm_driver_rt(dyn);
174 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
175 if (ret == -ENOENT) {
176 dm_warn("Some drivers were not found\n");
183 #if CONFIG_IS_ENABLED(OF_REAL)
185 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
187 * This scans the subnodes of a device tree node and and creates a driver
190 * @parent: Parent device for the devices that will be created
191 * @node: Node to scan
192 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
193 * flag. If false bind all drivers.
194 * Return: 0 if OK, -ve on error
196 static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
199 int ret = 0, err = 0;
202 if (!ofnode_valid(parent_node))
205 for (node = ofnode_first_subnode(parent_node);
207 node = ofnode_next_subnode(node)) {
208 const char *node_name = ofnode_get_name(node);
210 if (!ofnode_is_enabled(node)) {
211 pr_debug(" - ignoring disabled device\n");
214 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only);
217 dm_warn("%s: ret=%d\n", node_name, ret);
222 dm_warn("Some drivers failed to bind\n");
227 int dm_scan_fdt_dev(struct udevice *dev)
229 return dm_scan_fdt_node(dev, dev_ofnode(dev),
230 gd->flags & GD_FLG_RELOC ? false : true);
233 int dm_scan_fdt(bool pre_reloc_only)
235 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
238 static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
242 node = ofnode_path(path);
244 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
247 int dm_extended_scan(bool pre_reloc_only)
250 const char * const nodes[] = {
257 ret = dm_scan_fdt(pre_reloc_only);
259 dm_warn("dm_scan_fdt() failed: %d\n", ret);
263 /* Some nodes aren't devices themselves but may contain some */
264 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
265 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
267 dm_warn("dm_scan_fdt() scan for %s failed: %d\n",
277 __weak int dm_scan_other(bool pre_reloc_only)
282 #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
283 void *dm_priv_to_rw(void *priv)
285 long offset = priv - (void *)__priv_data_start;
287 return gd_dm_priv_base() + offset;
291 static int dm_probe_devices(struct udevice *dev, bool pre_reloc_only)
293 ofnode node = dev_ofnode(dev);
294 struct udevice *child;
297 if (pre_reloc_only &&
298 (!ofnode_valid(node) || !ofnode_pre_reloc(node)) &&
299 !(dev->driver->flags & DM_FLAG_PRE_RELOC))
302 if (dev_get_flags(dev) & DM_FLAG_PROBE_AFTER_BIND) {
303 ret = device_probe(dev);
309 list_for_each_entry(child, &dev->child_head, sibling_node)
310 dm_probe_devices(child, pre_reloc_only);
316 * dm_scan() - Scan tables to bind devices
318 * Runs through the driver_info tables and binds the devices it finds. Then runs
319 * through the devicetree nodes. Finally calls dm_scan_other() to add any
322 * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
323 * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
325 static int dm_scan(bool pre_reloc_only)
329 ret = dm_scan_plat(pre_reloc_only);
331 dm_warn("dm_scan_plat() failed: %d\n", ret);
335 if (CONFIG_IS_ENABLED(OF_REAL)) {
336 ret = dm_extended_scan(pre_reloc_only);
338 dm_warn("dm_extended_scan() failed: %d\n", ret);
343 ret = dm_scan_other(pre_reloc_only);
347 return dm_probe_devices(gd->dm_root, pre_reloc_only);
350 int dm_init_and_scan(bool pre_reloc_only)
354 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
356 dm_warn("dm_init() failed: %d\n", ret);
359 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
360 ret = dm_scan(pre_reloc_only);
362 log_debug("dm_scan() failed: %d\n", ret);
366 if (CONFIG_IS_ENABLED(DM_EVENT)) {
367 ret = event_notify_null(gd->flags & GD_FLG_RELOC ?
371 return log_msg_ret("ev", ret);
377 void dm_get_stats(int *device_countp, int *uclass_countp)
379 *device_countp = device_get_decendent_count(gd->dm_root);
380 *uclass_countp = uclass_get_count();
383 void dev_collect_stats(struct dm_stats *stats, const struct udevice *parent)
385 const struct udevice *dev;
389 stats->dev_size += sizeof(struct udevice);
390 stats->dev_name_size += strlen(parent->name) + 1;
391 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
392 int size = dev_get_attach_size(parent, i);
395 (i == DM_TAG_DRIVER_DATA && parent->driver_data)) {
396 stats->attach_count[i]++;
397 stats->attach_size[i] += size;
398 stats->attach_count_total++;
399 stats->attach_size_total += size;
403 list_for_each_entry(dev, &parent->child_head, sibling_node)
404 dev_collect_stats(stats, dev);
407 void uclass_collect_stats(struct dm_stats *stats)
411 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
415 stats->uc_size += sizeof(struct uclass);
416 size = uc->uc_drv->priv_auto;
418 stats->uc_attach_count++;
419 stats->uc_attach_size += size;
424 void dm_get_mem(struct dm_stats *stats)
426 memset(stats, '\0', sizeof(*stats));
427 dev_collect_stats(stats, gd->dm_root);
428 uclass_collect_stats(stats);
429 dev_tag_collect_stats(stats);
431 stats->total_size = stats->dev_size + stats->uc_size +
432 stats->attach_size_total + stats->uc_attach_size +
436 #if CONFIG_IS_ENABLED(ACPIGEN)
437 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
439 return acpi_copy_name(out_name, "\\_SB");
442 struct acpi_ops root_acpi_ops = {
443 .get_name = root_acpi_get_name,
447 /* This is the root driver - all drivers are children of this */
448 U_BOOT_DRIVER(root_driver) = {
449 .name = "root_driver",
451 ACPI_OPS_PTR(&root_acpi_ops)
454 /* This is the root uclass */
455 UCLASS_DRIVER(root) = {