blob: c4fb48548bb34e1ff7d82cba80352a5b651e5e3d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07007 */
8
Patrick Delaunay81313352021-04-27 11:02:19 +02009#define LOG_CATEGORY UCLASS_ROOT
10
Simon Glassdd6ab882014-02-26 15:59:18 -070011#include <common.h>
12#include <errno.h>
Simon Glass528b1ef2015-01-25 08:27:16 -070013#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070015#include <malloc.h>
Simon Glass8dee67a2021-03-15 17:25:38 +130016#include <asm-generic/sections.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Simon Glass719ea082020-07-07 13:11:38 -060019#include <dm/acpi.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Simon Glassf19d9f22017-05-18 20:09:08 -060023#include <dm/of.h>
24#include <dm/of_access.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glassf19d9f22017-05-18 20:09:08 -060026#include <dm/read.h>
Jeroen Hofsteecf0cf822014-06-25 21:57:45 +020027#include <dm/root.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070028#include <dm/uclass.h>
Simon Glass51608c92021-12-16 20:59:32 -070029#include <dm/uclass-internal.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070030#include <dm/util.h>
31#include <linux/list.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
Walter Lozano95282d62020-06-25 01:10:10 -030035static struct driver_info root_info = {
Simon Glassdd6ab882014-02-26 15:59:18 -070036 .name = "root_driver",
37};
38
Heiko Schocherb74fcb42014-05-22 12:43:05 +020039struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070040{
41 if (!gd->dm_root) {
42 dm_warn("Virtual root driver does not exist!\n");
43 return NULL;
44 }
45
46 return gd->dm_root;
47}
48
Simon Glass1b0deee2016-11-13 14:21:58 -070049void dm_fixup_for_gd_move(struct global_data *new_gd)
50{
51 /* The sentinel node has moved, so update things that point to it */
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053052 if (gd->dm_root) {
Simon Glass784cd9e2020-12-19 10:40:17 -070053 new_gd->uclass_root->next->prev = new_gd->uclass_root;
54 new_gd->uclass_root->prev->next = new_gd->uclass_root;
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053055 }
Simon Glass1b0deee2016-11-13 14:21:58 -070056}
57
Michal Simek0ec473b2015-02-02 16:31:59 +010058void fix_drivers(void)
59{
60 struct driver *drv =
61 ll_entry_start(struct driver, driver);
62 const int n_ents = ll_entry_count(struct driver, driver);
63 struct driver *entry;
64
65 for (entry = drv; entry != drv + n_ents; entry++) {
66 if (entry->of_match)
67 entry->of_match = (const struct udevice_id *)
Simon Glass40916e62020-10-03 09:25:22 -060068 ((ulong)entry->of_match + gd->reloc_off);
Michal Simek0ec473b2015-02-02 16:31:59 +010069 if (entry->bind)
70 entry->bind += gd->reloc_off;
71 if (entry->probe)
72 entry->probe += gd->reloc_off;
73 if (entry->remove)
74 entry->remove += gd->reloc_off;
75 if (entry->unbind)
76 entry->unbind += gd->reloc_off;
Simon Glassaad29ae2020-12-03 16:55:21 -070077 if (entry->of_to_plat)
78 entry->of_to_plat += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010079 if (entry->child_post_bind)
80 entry->child_post_bind += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010081 if (entry->child_pre_probe)
82 entry->child_pre_probe += gd->reloc_off;
83 if (entry->child_post_remove)
84 entry->child_post_remove += gd->reloc_off;
85 /* OPS are fixed in every uclass post_probe function */
86 if (entry->ops)
87 entry->ops += gd->reloc_off;
88 }
89}
90
91void fix_uclass(void)
92{
93 struct uclass_driver *uclass =
Simon Glass69ea20e2020-12-22 19:30:23 -070094 ll_entry_start(struct uclass_driver, uclass_driver);
95 const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
Michal Simek0ec473b2015-02-02 16:31:59 +010096 struct uclass_driver *entry;
97
98 for (entry = uclass; entry != uclass + n_ents; entry++) {
99 if (entry->post_bind)
100 entry->post_bind += gd->reloc_off;
101 if (entry->pre_unbind)
102 entry->pre_unbind += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100103 if (entry->pre_probe)
104 entry->pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100105 if (entry->post_probe)
106 entry->post_probe += gd->reloc_off;
107 if (entry->pre_remove)
108 entry->pre_remove += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100109 if (entry->child_post_bind)
110 entry->child_post_bind += gd->reloc_off;
111 if (entry->child_pre_probe)
112 entry->child_pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100113 if (entry->init)
114 entry->init += gd->reloc_off;
115 if (entry->destroy)
116 entry->destroy += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100117 }
118}
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200119
120void fix_devices(void)
121{
122 struct driver_info *dev =
123 ll_entry_start(struct driver_info, driver_info);
124 const int n_ents = ll_entry_count(struct driver_info, driver_info);
125 struct driver_info *entry;
126
127 for (entry = dev; entry != dev + n_ents; entry++) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700128 if (entry->plat)
129 entry->plat += gd->reloc_off;
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200130 }
131}
132
Simon Glass43f7ea52021-03-15 17:25:17 +1300133static int dm_setup_inst(void)
134{
135 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
136
Simon Glass8beeb282021-03-15 17:25:36 +1300137 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
138 struct udevice_rt *urt;
Simon Glass308d91b2022-03-27 14:26:20 -0600139 void *start, *end;
140 int each_size;
Simon Glass8dee67a2021-03-15 17:25:38 +1300141 void *base;
Simon Glass8beeb282021-03-15 17:25:36 +1300142 int n_ents;
Simon Glass8dee67a2021-03-15 17:25:38 +1300143 uint size;
Simon Glass8beeb282021-03-15 17:25:36 +1300144
145 /* Allocate the udevice_rt table */
Simon Glass308d91b2022-03-27 14:26:20 -0600146 each_size = dm_udevice_size();
147 start = ll_entry_start(struct udevice, udevice);
148 end = ll_entry_end(struct udevice, udevice);
149 size = end - start;
150 n_ents = size / each_size;
Simon Glass8beeb282021-03-15 17:25:36 +1300151 urt = calloc(n_ents, sizeof(struct udevice_rt));
152 if (!urt)
153 return log_msg_ret("urt", -ENOMEM);
154 gd_set_dm_udevice_rt(urt);
Simon Glass8dee67a2021-03-15 17:25:38 +1300155
156 /* Now allocate space for the priv/plat data, and copy it in */
157 size = __priv_data_end - __priv_data_start;
158
159 base = calloc(1, size);
160 if (!base)
161 return log_msg_ret("priv", -ENOMEM);
162 memcpy(base, __priv_data_start, size);
163 gd_set_dm_priv_base(base);
Simon Glass8beeb282021-03-15 17:25:36 +1300164 }
165
Simon Glass43f7ea52021-03-15 17:25:17 +1300166 return 0;
167}
168
Simon Glassf19d9f22017-05-18 20:09:08 -0600169int dm_init(bool of_live)
Simon Glassdd6ab882014-02-26 15:59:18 -0700170{
171 int ret;
172
173 if (gd->dm_root) {
174 dm_warn("Virtual root driver already exists!\n");
175 return -EINVAL;
176 }
Simon Glass6f156532021-03-15 17:25:16 +1300177 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
178 gd->uclass_root = &uclass_head;
179 } else {
180 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
181 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
182 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700183
Simon Glasse2e89072020-10-03 11:31:37 -0600184 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
185 fix_drivers();
186 fix_uclass();
187 fix_devices();
188 }
Michal Simek0ec473b2015-02-02 16:31:59 +0100189
Simon Glass43f7ea52021-03-15 17:25:17 +1300190 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
191 ret = dm_setup_inst();
192 if (ret) {
193 log_debug("dm_setup_inst() failed: %d\n", ret);
194 return ret;
195 }
196 } else {
197 ret = device_bind_by_name(NULL, false, &root_info,
198 &DM_ROOT_NON_CONST);
199 if (ret)
200 return ret;
201 if (CONFIG_IS_ENABLED(OF_CONTROL))
202 dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
203 ret = device_probe(DM_ROOT_NON_CONST);
204 if (ret)
205 return ret;
206 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700207
AKASHI Takahiroe04f4fb2022-03-08 20:36:46 +0900208 INIT_LIST_HEAD((struct list_head *)&gd->dmtag_list);
209
Simon Glassdd6ab882014-02-26 15:59:18 -0700210 return 0;
211}
212
Simon Glass00197582014-07-23 06:55:01 -0600213int dm_uninit(void)
214{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700215 /* Remove non-vital devices first */
216 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100217 device_remove(dm_root(), DM_REMOVE_NORMAL);
Simon Glass00197582014-07-23 06:55:01 -0600218 device_unbind(dm_root());
Jean-Jacques Hiblot272b67d2018-12-07 14:50:54 +0100219 gd->dm_root = NULL;
Simon Glass00197582014-07-23 06:55:01 -0600220
221 return 0;
222}
223
Stefan Roese505045d2017-03-27 10:58:53 +0200224#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
225int dm_remove_devices_flags(uint flags)
226{
227 device_remove(dm_root(), flags);
228
229 return 0;
230}
231#endif
232
Simon Glassb75b15b2020-12-03 16:55:23 -0700233int dm_scan_plat(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700234{
235 int ret;
236
Simon Glass8beeb282021-03-15 17:25:36 +1300237 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
Simon Glasscfd6a002020-10-03 11:31:33 -0600238 struct driver_rt *dyn;
239 int n_ents;
240
241 n_ents = ll_entry_count(struct driver_info, driver_info);
242 dyn = calloc(n_ents, sizeof(struct driver_rt));
243 if (!dyn)
244 return -ENOMEM;
245 gd_set_dm_driver_rt(dyn);
246 }
247
Simon Glassfef72b72014-07-23 06:55:03 -0600248 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700249 if (ret == -ENOENT) {
250 dm_warn("Some drivers were not found\n");
251 ret = 0;
252 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700253
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900254 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700255}
256
Simon Glass3580f6d2021-08-07 07:24:03 -0600257#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassa4dbe252017-05-17 17:18:08 -0600258/**
259 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
260 *
261 * This scans the subnodes of a device tree node and and creates a driver
262 * for each one.
263 *
264 * @parent: Parent device for the devices that will be created
Simon Glass21a30882020-11-28 17:50:08 -0700265 * @node: Node to scan
Simon Glassa4dbe252017-05-17 17:18:08 -0600266 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
267 * flag. If false bind all drivers.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100268 * Return: 0 if OK, -ve on error
Simon Glassa4dbe252017-05-17 17:18:08 -0600269 */
Simon Glass21a30882020-11-28 17:50:08 -0700270static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
271 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700272{
Sean Anderson0449f062021-04-08 17:15:00 -0400273 int ret = 0, err = 0;
Simon Glass21a30882020-11-28 17:50:08 -0700274 ofnode node;
Simon Glassdd6ab882014-02-26 15:59:18 -0700275
Simon Glass21a30882020-11-28 17:50:08 -0700276 if (!ofnode_valid(parent_node))
277 return 0;
278
279 for (node = ofnode_first_subnode(parent_node);
280 ofnode_valid(node);
281 node = ofnode_next_subnode(node)) {
282 const char *node_name = ofnode_get_name(node);
Jens Wiklander651a8362018-09-25 16:40:05 +0200283
Simon Glass21a30882020-11-28 17:50:08 -0700284 if (!ofnode_is_enabled(node)) {
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900285 pr_debug(" - ignoring disabled device\n");
Simon Glass528b1ef2015-01-25 08:27:16 -0700286 continue;
287 }
Patrice Chotardcc523cf2021-09-10 16:16:20 +0200288 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only);
Simon Glassc90b9172015-08-30 16:55:17 -0600289 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600290 ret = err;
Jens Wiklander651a8362018-09-25 16:40:05 +0200291 debug("%s: ret=%d\n", node_name, ret);
Simon Glassc90b9172015-08-30 16:55:17 -0600292 }
Simon Glass40717422014-07-23 06:55:18 -0600293 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700294
295 if (ret)
296 dm_warn("Some drivers failed to bind\n");
297
298 return ret;
299}
Simon Glass40717422014-07-23 06:55:18 -0600300
Simon Glass5d5388d2016-07-05 17:10:08 -0600301int dm_scan_fdt_dev(struct udevice *dev)
302{
Simon Glass21a30882020-11-28 17:50:08 -0700303 return dm_scan_fdt_node(dev, dev_ofnode(dev),
Simon Glass5d5388d2016-07-05 17:10:08 -0600304 gd->flags & GD_FLG_RELOC ? false : true);
305}
306
Simon Glass5039cab2020-11-28 17:50:09 -0700307int dm_scan_fdt(bool pre_reloc_only)
Simon Glass40717422014-07-23 06:55:18 -0600308{
Simon Glass21a30882020-11-28 17:50:08 -0700309 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
Simon Glass40717422014-07-23 06:55:18 -0600310}
Simon Glassdd6ab882014-02-26 15:59:18 -0700311
Simon Glass5039cab2020-11-28 17:50:09 -0700312static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
Rajan Vajafac64632018-08-10 01:45:33 -0700313{
314 ofnode node;
315
316 node = ofnode_path(path);
Simon Glass40916e62020-10-03 09:25:22 -0600317
Simon Glass21a30882020-11-28 17:50:08 -0700318 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
Rajan Vajafac64632018-08-10 01:45:33 -0700319}
320
Simon Glass4d71d602020-11-28 17:50:10 -0700321int dm_extended_scan(bool pre_reloc_only)
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200322{
Patrick Delaunayc779e202020-02-18 15:43:46 +0100323 int ret, i;
324 const char * const nodes[] = {
325 "/chosen",
326 "/clocks",
327 "/firmware"
328 };
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200329
Simon Glass5039cab2020-11-28 17:50:09 -0700330 ret = dm_scan_fdt(pre_reloc_only);
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200331 if (ret) {
332 debug("dm_scan_fdt() failed: %d\n", ret);
333 return ret;
334 }
335
Patrick Delaunayc779e202020-02-18 15:43:46 +0100336 /* Some nodes aren't devices themselves but may contain some */
337 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
Simon Glass5039cab2020-11-28 17:50:09 -0700338 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
Patrick Delaunayc779e202020-02-18 15:43:46 +0100339 if (ret) {
340 debug("dm_scan_fdt() scan for %s failed: %d\n",
341 nodes[i], ret);
342 return ret;
343 }
Rajan Vajae9fc81d2018-08-10 01:45:34 -0700344 }
345
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200346 return ret;
347}
Marek Vasut7f18c342019-08-31 18:03:28 +0200348#endif
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200349
Simon Glass97e22e32014-07-23 06:55:23 -0600350__weak int dm_scan_other(bool pre_reloc_only)
351{
352 return 0;
353}
354
Simon Glass781aa3e2021-03-15 17:25:40 +1300355#if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
356void *dm_priv_to_rw(void *priv)
357{
358 long offset = priv - (void *)__priv_data_start;
359
360 return gd_dm_priv_base() + offset;
361}
362#endif
363
Marek Vasut11a6f582022-04-22 15:15:53 +0200364static int dm_probe_devices(struct udevice *dev, bool pre_reloc_only)
365{
Quentin Schulz446d6002022-09-22 17:53:25 +0200366 ofnode node = dev_ofnode(dev);
Marek Vasut11a6f582022-04-22 15:15:53 +0200367 struct udevice *child;
368 int ret;
369
Quentin Schulz446d6002022-09-22 17:53:25 +0200370 if (pre_reloc_only &&
371 (!ofnode_valid(node) || !ofnode_pre_reloc(node)) &&
372 !(dev->driver->flags & DM_FLAG_PRE_RELOC))
373 goto probe_children;
Marek Vasut11a6f582022-04-22 15:15:53 +0200374
Quentin Schulz446d6002022-09-22 17:53:25 +0200375 if (dev_get_flags(dev) & DM_FLAG_PROBE_AFTER_BIND) {
Marek Vasut11a6f582022-04-22 15:15:53 +0200376 ret = device_probe(dev);
377 if (ret)
378 return ret;
379 }
380
Quentin Schulz446d6002022-09-22 17:53:25 +0200381probe_children:
Marek Vasut11a6f582022-04-22 15:15:53 +0200382 list_for_each_entry(child, &dev->child_head, sibling_node)
383 dm_probe_devices(child, pre_reloc_only);
384
385 return 0;
386}
387
Simon Glassc77695b2020-12-19 10:40:16 -0700388/**
389 * dm_scan() - Scan tables to bind devices
390 *
391 * Runs through the driver_info tables and binds the devices it finds. Then runs
392 * through the devicetree nodes. Finally calls dm_scan_other() to add any
393 * special devices
394 *
395 * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
396 * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
397 */
398static int dm_scan(bool pre_reloc_only)
Simon Glassa730c5d2014-07-23 06:55:04 -0600399{
400 int ret;
401
Simon Glassb75b15b2020-12-03 16:55:23 -0700402 ret = dm_scan_plat(pre_reloc_only);
Simon Glassa730c5d2014-07-23 06:55:04 -0600403 if (ret) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700404 debug("dm_scan_plat() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700405 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600406 }
Simon Glass0704b852015-02-27 22:06:41 -0700407
Simon Glass3580f6d2021-08-07 07:24:03 -0600408 if (CONFIG_IS_ENABLED(OF_REAL)) {
Simon Glass4d71d602020-11-28 17:50:10 -0700409 ret = dm_extended_scan(pre_reloc_only);
Simon Glass0704b852015-02-27 22:06:41 -0700410 if (ret) {
Simon Glass4d71d602020-11-28 17:50:10 -0700411 debug("dm_extended_scan() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700412 return ret;
Simon Glass0704b852015-02-27 22:06:41 -0700413 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600414 }
Simon Glass0704b852015-02-27 22:06:41 -0700415
Simon Glass97e22e32014-07-23 06:55:23 -0600416 ret = dm_scan_other(pre_reloc_only);
417 if (ret)
Simon Glassc77695b2020-12-19 10:40:16 -0700418 return ret;
419
Marek Vasut11a6f582022-04-22 15:15:53 +0200420 return dm_probe_devices(gd->dm_root, pre_reloc_only);
Simon Glassc77695b2020-12-19 10:40:16 -0700421}
422
423int dm_init_and_scan(bool pre_reloc_only)
424{
425 int ret;
426
Simon Glassc77695b2020-12-19 10:40:16 -0700427 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
428 if (ret) {
429 debug("dm_init() failed: %d\n", ret);
430 return ret;
431 }
Simon Glass43f7ea52021-03-15 17:25:17 +1300432 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
433 ret = dm_scan(pre_reloc_only);
434 if (ret) {
435 log_debug("dm_scan() failed: %d\n", ret);
436 return ret;
437 }
Simon Glassc77695b2020-12-19 10:40:16 -0700438 }
Simon Glassfc557362022-03-04 08:43:05 -0700439 if (CONFIG_IS_ENABLED(DM_EVENT)) {
440 ret = event_notify_null(EVT_DM_POST_INIT);
441 if (ret)
442 return log_msg_ret("ev", ret);
443 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600444
445 return 0;
446}
447
Simon Glass51608c92021-12-16 20:59:32 -0700448void dm_get_stats(int *device_countp, int *uclass_countp)
449{
450 *device_countp = device_get_decendent_count(gd->dm_root);
451 *uclass_countp = uclass_get_count();
452}
453
Simon Glassbe1621b2022-05-08 04:39:25 -0600454void dev_collect_stats(struct dm_stats *stats, const struct udevice *parent)
455{
456 const struct udevice *dev;
457 int i;
458
459 stats->dev_count++;
460 stats->dev_size += sizeof(struct udevice);
461 stats->dev_name_size += strlen(parent->name) + 1;
462 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
463 int size = dev_get_attach_size(parent, i);
464
465 if (size ||
466 (i == DM_TAG_DRIVER_DATA && parent->driver_data)) {
467 stats->attach_count[i]++;
468 stats->attach_size[i] += size;
469 stats->attach_count_total++;
470 stats->attach_size_total += size;
471 }
472 }
473
474 list_for_each_entry(dev, &parent->child_head, sibling_node)
475 dev_collect_stats(stats, dev);
476}
477
478void uclass_collect_stats(struct dm_stats *stats)
479{
480 struct uclass *uc;
481
482 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
483 int size;
484
485 stats->uc_count++;
486 stats->uc_size += sizeof(struct uclass);
487 size = uc->uc_drv->priv_auto;
488 if (size) {
489 stats->uc_attach_count++;
490 stats->uc_attach_size += size;
491 }
492 }
493}
494
495void dm_get_mem(struct dm_stats *stats)
496{
497 memset(stats, '\0', sizeof(*stats));
498 dev_collect_stats(stats, gd->dm_root);
499 uclass_collect_stats(stats);
500 dev_tag_collect_stats(stats);
501
502 stats->total_size = stats->dev_size + stats->uc_size +
503 stats->attach_size_total + stats->uc_attach_size +
504 stats->tag_size;
505}
506
Simon Glass719ea082020-07-07 13:11:38 -0600507#ifdef CONFIG_ACPIGEN
508static int root_acpi_get_name(const struct udevice *dev, char *out_name)
509{
510 return acpi_copy_name(out_name, "\\_SB");
511}
512
513struct acpi_ops root_acpi_ops = {
514 .get_name = root_acpi_get_name,
515};
516#endif
517
Simon Glassdd6ab882014-02-26 15:59:18 -0700518/* This is the root driver - all drivers are children of this */
519U_BOOT_DRIVER(root_driver) = {
520 .name = "root_driver",
521 .id = UCLASS_ROOT,
Simon Glass719ea082020-07-07 13:11:38 -0600522 ACPI_OPS_PTR(&root_acpi_ops)
Simon Glassdd6ab882014-02-26 15:59:18 -0700523};
524
525/* This is the root uclass */
526UCLASS_DRIVER(root) = {
527 .name = "root",
528 .id = UCLASS_ROOT,
529};