blob: d4ae652bcfb1d5b625d845974b0234bb015063da [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060032#include <linux/printk.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070033
34DECLARE_GLOBAL_DATA_PTR;
35
Walter Lozano95282d62020-06-25 01:10:10 -030036static struct driver_info root_info = {
Simon Glassdd6ab882014-02-26 15:59:18 -070037 .name = "root_driver",
38};
39
Heiko Schocherb74fcb42014-05-22 12:43:05 +020040struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070041{
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
Simon Glass1b0deee2016-11-13 14:21:58 -070050void dm_fixup_for_gd_move(struct global_data *new_gd)
51{
52 /* The sentinel node has moved, so update things that point to it */
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053053 if (gd->dm_root) {
Simon Glass784cd9e2020-12-19 10:40:17 -070054 new_gd->uclass_root->next->prev = new_gd->uclass_root;
55 new_gd->uclass_root->prev->next = new_gd->uclass_root;
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053056 }
Simon Glass1b0deee2016-11-13 14:21:58 -070057}
58
Simon Glass43f7ea52021-03-15 17:25:17 +130059static int dm_setup_inst(void)
60{
61 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
62
Simon Glass8beeb282021-03-15 17:25:36 +130063 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
64 struct udevice_rt *urt;
Simon Glass308d91b2022-03-27 14:26:20 -060065 void *start, *end;
66 int each_size;
Simon Glass8dee67a2021-03-15 17:25:38 +130067 void *base;
Simon Glass8beeb282021-03-15 17:25:36 +130068 int n_ents;
Simon Glass8dee67a2021-03-15 17:25:38 +130069 uint size;
Simon Glass8beeb282021-03-15 17:25:36 +130070
71 /* Allocate the udevice_rt table */
Simon Glass308d91b2022-03-27 14:26:20 -060072 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;
Simon Glass8beeb282021-03-15 17:25:36 +130077 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);
Simon Glass8dee67a2021-03-15 17:25:38 +130081
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);
Simon Glass8beeb282021-03-15 17:25:36 +130090 }
91
Simon Glass43f7ea52021-03-15 17:25:17 +130092 return 0;
93}
94
Simon Glassf19d9f22017-05-18 20:09:08 -060095int dm_init(bool of_live)
Simon Glassdd6ab882014-02-26 15:59:18 -070096{
97 int ret;
98
99 if (gd->dm_root) {
100 dm_warn("Virtual root driver already exists!\n");
101 return -EINVAL;
102 }
Simon Glass6f156532021-03-15 17:25:16 +1300103 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 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700109
Simon Glass43f7ea52021-03-15 17:25:17 +1300110 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 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700127
AKASHI Takahiroe04f4fb2022-03-08 20:36:46 +0900128 INIT_LIST_HEAD((struct list_head *)&gd->dmtag_list);
129
Simon Glassdd6ab882014-02-26 15:59:18 -0700130 return 0;
131}
132
Simon Glass00197582014-07-23 06:55:01 -0600133int dm_uninit(void)
134{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700135 /* Remove non-vital devices first */
136 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100137 device_remove(dm_root(), DM_REMOVE_NORMAL);
Simon Glass00197582014-07-23 06:55:01 -0600138 device_unbind(dm_root());
Jean-Jacques Hiblot272b67d2018-12-07 14:50:54 +0100139 gd->dm_root = NULL;
Simon Glass00197582014-07-23 06:55:01 -0600140
141 return 0;
142}
143
Stefan Roese505045d2017-03-27 10:58:53 +0200144#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
Simon Glassb75b15b2020-12-03 16:55:23 -0700153int dm_scan_plat(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700154{
155 int ret;
156
Simon Glass8beeb282021-03-15 17:25:36 +1300157 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
Simon Glasscfd6a002020-10-03 11:31:33 -0600158 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
Simon Glassfef72b72014-07-23 06:55:03 -0600168 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700169 if (ret == -ENOENT) {
170 dm_warn("Some drivers were not found\n");
171 ret = 0;
172 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700173
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900174 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700175}
176
Simon Glass3580f6d2021-08-07 07:24:03 -0600177#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassa4dbe252017-05-17 17:18:08 -0600178/**
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
Simon Glass21a30882020-11-28 17:50:08 -0700185 * @node: Node to scan
Simon Glassa4dbe252017-05-17 17:18:08 -0600186 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
187 * flag. If false bind all drivers.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100188 * Return: 0 if OK, -ve on error
Simon Glassa4dbe252017-05-17 17:18:08 -0600189 */
Simon Glass21a30882020-11-28 17:50:08 -0700190static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
191 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700192{
Sean Anderson0449f062021-04-08 17:15:00 -0400193 int ret = 0, err = 0;
Simon Glass21a30882020-11-28 17:50:08 -0700194 ofnode node;
Simon Glassdd6ab882014-02-26 15:59:18 -0700195
Simon Glass21a30882020-11-28 17:50:08 -0700196 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);
Jens Wiklander651a8362018-09-25 16:40:05 +0200203
Simon Glass21a30882020-11-28 17:50:08 -0700204 if (!ofnode_is_enabled(node)) {
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900205 pr_debug(" - ignoring disabled device\n");
Simon Glass528b1ef2015-01-25 08:27:16 -0700206 continue;
207 }
Patrice Chotardcc523cf2021-09-10 16:16:20 +0200208 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only);
Simon Glassc90b9172015-08-30 16:55:17 -0600209 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600210 ret = err;
Jens Wiklander651a8362018-09-25 16:40:05 +0200211 debug("%s: ret=%d\n", node_name, ret);
Simon Glassc90b9172015-08-30 16:55:17 -0600212 }
Simon Glass40717422014-07-23 06:55:18 -0600213 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700214
215 if (ret)
216 dm_warn("Some drivers failed to bind\n");
217
218 return ret;
219}
Simon Glass40717422014-07-23 06:55:18 -0600220
Simon Glass5d5388d2016-07-05 17:10:08 -0600221int dm_scan_fdt_dev(struct udevice *dev)
222{
Simon Glass21a30882020-11-28 17:50:08 -0700223 return dm_scan_fdt_node(dev, dev_ofnode(dev),
Simon Glass5d5388d2016-07-05 17:10:08 -0600224 gd->flags & GD_FLG_RELOC ? false : true);
225}
226
Simon Glass5039cab2020-11-28 17:50:09 -0700227int dm_scan_fdt(bool pre_reloc_only)
Simon Glass40717422014-07-23 06:55:18 -0600228{
Simon Glass21a30882020-11-28 17:50:08 -0700229 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
Simon Glass40717422014-07-23 06:55:18 -0600230}
Simon Glassdd6ab882014-02-26 15:59:18 -0700231
Simon Glass5039cab2020-11-28 17:50:09 -0700232static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
Rajan Vajafac64632018-08-10 01:45:33 -0700233{
234 ofnode node;
235
236 node = ofnode_path(path);
Simon Glass40916e62020-10-03 09:25:22 -0600237
Simon Glass21a30882020-11-28 17:50:08 -0700238 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
Rajan Vajafac64632018-08-10 01:45:33 -0700239}
240
Simon Glass4d71d602020-11-28 17:50:10 -0700241int dm_extended_scan(bool pre_reloc_only)
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200242{
Patrick Delaunayc779e202020-02-18 15:43:46 +0100243 int ret, i;
244 const char * const nodes[] = {
245 "/chosen",
246 "/clocks",
247 "/firmware"
248 };
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200249
Simon Glass5039cab2020-11-28 17:50:09 -0700250 ret = dm_scan_fdt(pre_reloc_only);
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200251 if (ret) {
252 debug("dm_scan_fdt() failed: %d\n", ret);
253 return ret;
254 }
255
Patrick Delaunayc779e202020-02-18 15:43:46 +0100256 /* Some nodes aren't devices themselves but may contain some */
257 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
Simon Glass5039cab2020-11-28 17:50:09 -0700258 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
Patrick Delaunayc779e202020-02-18 15:43:46 +0100259 if (ret) {
260 debug("dm_scan_fdt() scan for %s failed: %d\n",
261 nodes[i], ret);
262 return ret;
263 }
Rajan Vajae9fc81d2018-08-10 01:45:34 -0700264 }
265
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200266 return ret;
267}
Marek Vasut7f18c342019-08-31 18:03:28 +0200268#endif
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200269
Simon Glass97e22e32014-07-23 06:55:23 -0600270__weak int dm_scan_other(bool pre_reloc_only)
271{
272 return 0;
273}
274
Simon Glass781aa3e2021-03-15 17:25:40 +1300275#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
Marek Vasut11a6f582022-04-22 15:15:53 +0200284static int dm_probe_devices(struct udevice *dev, bool pre_reloc_only)
285{
Quentin Schulz446d6002022-09-22 17:53:25 +0200286 ofnode node = dev_ofnode(dev);
Marek Vasut11a6f582022-04-22 15:15:53 +0200287 struct udevice *child;
288 int ret;
289
Quentin Schulz446d6002022-09-22 17:53:25 +0200290 if (pre_reloc_only &&
291 (!ofnode_valid(node) || !ofnode_pre_reloc(node)) &&
292 !(dev->driver->flags & DM_FLAG_PRE_RELOC))
293 goto probe_children;
Marek Vasut11a6f582022-04-22 15:15:53 +0200294
Quentin Schulz446d6002022-09-22 17:53:25 +0200295 if (dev_get_flags(dev) & DM_FLAG_PROBE_AFTER_BIND) {
Marek Vasut11a6f582022-04-22 15:15:53 +0200296 ret = device_probe(dev);
297 if (ret)
298 return ret;
299 }
300
Quentin Schulz446d6002022-09-22 17:53:25 +0200301probe_children:
Marek Vasut11a6f582022-04-22 15:15:53 +0200302 list_for_each_entry(child, &dev->child_head, sibling_node)
303 dm_probe_devices(child, pre_reloc_only);
304
305 return 0;
306}
307
Simon Glassc77695b2020-12-19 10:40:16 -0700308/**
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)
Simon Glassa730c5d2014-07-23 06:55:04 -0600319{
320 int ret;
321
Simon Glassb75b15b2020-12-03 16:55:23 -0700322 ret = dm_scan_plat(pre_reloc_only);
Simon Glassa730c5d2014-07-23 06:55:04 -0600323 if (ret) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700324 debug("dm_scan_plat() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700325 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600326 }
Simon Glass0704b852015-02-27 22:06:41 -0700327
Simon Glass3580f6d2021-08-07 07:24:03 -0600328 if (CONFIG_IS_ENABLED(OF_REAL)) {
Simon Glass4d71d602020-11-28 17:50:10 -0700329 ret = dm_extended_scan(pre_reloc_only);
Simon Glass0704b852015-02-27 22:06:41 -0700330 if (ret) {
Simon Glass4d71d602020-11-28 17:50:10 -0700331 debug("dm_extended_scan() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700332 return ret;
Simon Glass0704b852015-02-27 22:06:41 -0700333 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600334 }
Simon Glass0704b852015-02-27 22:06:41 -0700335
Simon Glass97e22e32014-07-23 06:55:23 -0600336 ret = dm_scan_other(pre_reloc_only);
337 if (ret)
Simon Glassc77695b2020-12-19 10:40:16 -0700338 return ret;
339
Marek Vasut11a6f582022-04-22 15:15:53 +0200340 return dm_probe_devices(gd->dm_root, pre_reloc_only);
Simon Glassc77695b2020-12-19 10:40:16 -0700341}
342
343int dm_init_and_scan(bool pre_reloc_only)
344{
345 int ret;
346
Simon Glassc77695b2020-12-19 10:40:16 -0700347 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
348 if (ret) {
349 debug("dm_init() failed: %d\n", ret);
350 return ret;
351 }
Simon Glass43f7ea52021-03-15 17:25:17 +1300352 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 }
Simon Glassc77695b2020-12-19 10:40:16 -0700358 }
Chanho Park5865a002023-08-18 14:11:02 +0900359 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);
Simon Glassfc557362022-03-04 08:43:05 -0700363 if (ret)
364 return log_msg_ret("ev", ret);
365 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600366
367 return 0;
368}
369
Simon Glass51608c92021-12-16 20:59:32 -0700370void 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
Simon Glassbe1621b2022-05-08 04:39:25 -0600376void 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
Sean Anderson0da49802023-10-14 16:47:47 -0400429#if CONFIG_IS_ENABLED(ACPIGEN)
Simon Glass719ea082020-07-07 13:11:38 -0600430static 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
Simon Glassdd6ab882014-02-26 15:59:18 -0700440/* This is the root driver - all drivers are children of this */
441U_BOOT_DRIVER(root_driver) = {
442 .name = "root_driver",
443 .id = UCLASS_ROOT,
Simon Glass719ea082020-07-07 13:11:38 -0600444 ACPI_OPS_PTR(&root_acpi_ops)
Simon Glassdd6ab882014-02-26 15:59:18 -0700445};
446
447/* This is the root uclass */
448UCLASS_DRIVER(root) = {
449 .name = "root",
450 .id = UCLASS_ROOT,
451};