x86: fsp: Only compile fsp_save_s3_stack if (SPL_)DM_RTC is enabled
[platform/kernel/u-boot.git] / drivers / core / root.c
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
33 DECLARE_GLOBAL_DATA_PTR;
34
35 static struct driver_info root_info = {
36         .name           = "root_driver",
37 };
38
39 struct udevice *dm_root(void)
40 {
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
49 void dm_fixup_for_gd_move(struct global_data *new_gd)
50 {
51         /* The sentinel node has moved, so update things that point to it */
52         if (gd->dm_root) {
53                 new_gd->uclass_root->next->prev = new_gd->uclass_root;
54                 new_gd->uclass_root->prev->next = new_gd->uclass_root;
55         }
56 }
57
58 void 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 *)
68                                 ((ulong)entry->of_match + gd->reloc_off);
69                 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;
77                 if (entry->of_to_plat)
78                         entry->of_to_plat += gd->reloc_off;
79                 if (entry->child_post_bind)
80                         entry->child_post_bind += gd->reloc_off;
81                 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
91 void fix_uclass(void)
92 {
93         struct uclass_driver *uclass =
94                 ll_entry_start(struct uclass_driver, uclass_driver);
95         const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
96         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;
103                 if (entry->pre_probe)
104                         entry->pre_probe += gd->reloc_off;
105                 if (entry->post_probe)
106                         entry->post_probe += gd->reloc_off;
107                 if (entry->pre_remove)
108                         entry->pre_remove += gd->reloc_off;
109                 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;
113                 if (entry->init)
114                         entry->init += gd->reloc_off;
115                 if (entry->destroy)
116                         entry->destroy += gd->reloc_off;
117         }
118 }
119
120 void 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++) {
128                 if (entry->plat)
129                         entry->plat += gd->reloc_off;
130         }
131 }
132
133 static int dm_setup_inst(void)
134 {
135         DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
136
137         if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
138                 struct udevice_rt *urt;
139                 void *start, *end;
140                 int each_size;
141                 void *base;
142                 int n_ents;
143                 uint size;
144
145                 /* Allocate the udevice_rt table */
146                 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;
151                 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);
155
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);
164         }
165
166         return 0;
167 }
168
169 int dm_init(bool of_live)
170 {
171         int ret;
172
173         if (gd->dm_root) {
174                 dm_warn("Virtual root driver already exists!\n");
175                 return -EINVAL;
176         }
177         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         }
183
184         if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
185                 fix_drivers();
186                 fix_uclass();
187                 fix_devices();
188         }
189
190         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         }
207
208         INIT_LIST_HEAD((struct list_head *)&gd->dmtag_list);
209
210         return 0;
211 }
212
213 int dm_uninit(void)
214 {
215         /* Remove non-vital devices first */
216         device_remove(dm_root(), DM_REMOVE_NON_VITAL);
217         device_remove(dm_root(), DM_REMOVE_NORMAL);
218         device_unbind(dm_root());
219         gd->dm_root = NULL;
220
221         return 0;
222 }
223
224 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
225 int dm_remove_devices_flags(uint flags)
226 {
227         device_remove(dm_root(), flags);
228
229         return 0;
230 }
231 #endif
232
233 int dm_scan_plat(bool pre_reloc_only)
234 {
235         int ret;
236
237         if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
238                 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
248         ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
249         if (ret == -ENOENT) {
250                 dm_warn("Some drivers were not found\n");
251                 ret = 0;
252         }
253
254         return ret;
255 }
256
257 #if CONFIG_IS_ENABLED(OF_REAL)
258 /**
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
265  * @node: Node to scan
266  * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
267  * flag. If false bind all drivers.
268  * Return: 0 if OK, -ve on error
269  */
270 static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
271                             bool pre_reloc_only)
272 {
273         int ret = 0, err = 0;
274         ofnode node;
275
276         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);
283
284                 if (!ofnode_is_enabled(node)) {
285                         pr_debug("   - ignoring disabled device\n");
286                         continue;
287                 }
288                 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only);
289                 if (err && !ret) {
290                         ret = err;
291                         debug("%s: ret=%d\n", node_name, ret);
292                 }
293         }
294
295         if (ret)
296                 dm_warn("Some drivers failed to bind\n");
297
298         return ret;
299 }
300
301 int dm_scan_fdt_dev(struct udevice *dev)
302 {
303         return dm_scan_fdt_node(dev, dev_ofnode(dev),
304                                 gd->flags & GD_FLG_RELOC ? false : true);
305 }
306
307 int dm_scan_fdt(bool pre_reloc_only)
308 {
309         return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
310 }
311
312 static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
313 {
314         ofnode node;
315
316         node = ofnode_path(path);
317
318         return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
319 }
320
321 int dm_extended_scan(bool pre_reloc_only)
322 {
323         int ret, i;
324         const char * const nodes[] = {
325                 "/chosen",
326                 "/clocks",
327                 "/firmware"
328         };
329
330         ret = dm_scan_fdt(pre_reloc_only);
331         if (ret) {
332                 debug("dm_scan_fdt() failed: %d\n", ret);
333                 return ret;
334         }
335
336         /* Some nodes aren't devices themselves but may contain some */
337         for (i = 0; i < ARRAY_SIZE(nodes); i++) {
338                 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
339                 if (ret) {
340                         debug("dm_scan_fdt() scan for %s failed: %d\n",
341                               nodes[i], ret);
342                         return ret;
343                 }
344         }
345
346         return ret;
347 }
348 #endif
349
350 __weak int dm_scan_other(bool pre_reloc_only)
351 {
352         return 0;
353 }
354
355 #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
356 void *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
364 static int dm_probe_devices(struct udevice *dev, bool pre_reloc_only)
365 {
366         u32 mask = DM_FLAG_PROBE_AFTER_BIND;
367         u32 flags = dev_get_flags(dev);
368         struct udevice *child;
369         int ret;
370
371         if (pre_reloc_only)
372                 mask |= DM_FLAG_PRE_RELOC;
373
374         if ((flags & mask) == mask) {
375                 ret = device_probe(dev);
376                 if (ret)
377                         return ret;
378         }
379
380         list_for_each_entry(child, &dev->child_head, sibling_node)
381                 dm_probe_devices(child, pre_reloc_only);
382
383         return 0;
384 }
385
386 /**
387  * dm_scan() - Scan tables to bind devices
388  *
389  * Runs through the driver_info tables and binds the devices it finds. Then runs
390  * through the devicetree nodes. Finally calls dm_scan_other() to add any
391  * special devices
392  *
393  * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
394  * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
395  */
396 static int dm_scan(bool pre_reloc_only)
397 {
398         int ret;
399
400         ret = dm_scan_plat(pre_reloc_only);
401         if (ret) {
402                 debug("dm_scan_plat() failed: %d\n", ret);
403                 return ret;
404         }
405
406         if (CONFIG_IS_ENABLED(OF_REAL)) {
407                 ret = dm_extended_scan(pre_reloc_only);
408                 if (ret) {
409                         debug("dm_extended_scan() failed: %d\n", ret);
410                         return ret;
411                 }
412         }
413
414         ret = dm_scan_other(pre_reloc_only);
415         if (ret)
416                 return ret;
417
418         return dm_probe_devices(gd->dm_root, pre_reloc_only);
419 }
420
421 int dm_init_and_scan(bool pre_reloc_only)
422 {
423         int ret;
424
425         ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
426         if (ret) {
427                 debug("dm_init() failed: %d\n", ret);
428                 return ret;
429         }
430         if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
431                 ret = dm_scan(pre_reloc_only);
432                 if (ret) {
433                         log_debug("dm_scan() failed: %d\n", ret);
434                         return ret;
435                 }
436         }
437         if (CONFIG_IS_ENABLED(DM_EVENT)) {
438                 ret = event_notify_null(EVT_DM_POST_INIT);
439                 if (ret)
440                         return log_msg_ret("ev", ret);
441         }
442
443         return 0;
444 }
445
446 void dm_get_stats(int *device_countp, int *uclass_countp)
447 {
448         *device_countp = device_get_decendent_count(gd->dm_root);
449         *uclass_countp = uclass_get_count();
450 }
451
452 void dev_collect_stats(struct dm_stats *stats, const struct udevice *parent)
453 {
454         const struct udevice *dev;
455         int i;
456
457         stats->dev_count++;
458         stats->dev_size += sizeof(struct udevice);
459         stats->dev_name_size += strlen(parent->name) + 1;
460         for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
461                 int size = dev_get_attach_size(parent, i);
462
463                 if (size ||
464                     (i == DM_TAG_DRIVER_DATA && parent->driver_data)) {
465                         stats->attach_count[i]++;
466                         stats->attach_size[i] += size;
467                         stats->attach_count_total++;
468                         stats->attach_size_total += size;
469                 }
470         }
471
472         list_for_each_entry(dev, &parent->child_head, sibling_node)
473                 dev_collect_stats(stats, dev);
474 }
475
476 void uclass_collect_stats(struct dm_stats *stats)
477 {
478         struct uclass *uc;
479
480         list_for_each_entry(uc, gd->uclass_root, sibling_node) {
481                 int size;
482
483                 stats->uc_count++;
484                 stats->uc_size += sizeof(struct uclass);
485                 size = uc->uc_drv->priv_auto;
486                 if (size) {
487                         stats->uc_attach_count++;
488                         stats->uc_attach_size += size;
489                 }
490         }
491 }
492
493 void dm_get_mem(struct dm_stats *stats)
494 {
495         memset(stats, '\0', sizeof(*stats));
496         dev_collect_stats(stats, gd->dm_root);
497         uclass_collect_stats(stats);
498         dev_tag_collect_stats(stats);
499
500         stats->total_size = stats->dev_size + stats->uc_size +
501                 stats->attach_size_total + stats->uc_attach_size +
502                 stats->tag_size;
503 }
504
505 #ifdef CONFIG_ACPIGEN
506 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
507 {
508         return acpi_copy_name(out_name, "\\_SB");
509 }
510
511 struct acpi_ops root_acpi_ops = {
512         .get_name       = root_acpi_get_name,
513 };
514 #endif
515
516 /* This is the root driver - all drivers are children of this */
517 U_BOOT_DRIVER(root_driver) = {
518         .name   = "root_driver",
519         .id     = UCLASS_ROOT,
520         ACPI_OPS_PTR(&root_acpi_ops)
521 };
522
523 /* This is the root uclass */
524 UCLASS_DRIVER(root) = {
525         .name   = "root",
526         .id     = UCLASS_ROOT,
527 };