blob: 0c7d2ec4d0c953bc0e42c543e55f50b8e1f428e5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd9f9a892015-06-23 15:38:35 -06002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glassd9f9a892015-06-23 15:38:35 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glasse5314c12023-01-17 10:47:12 -07008#include <malloc.h>
Simon Glassd9f9a892015-06-23 15:38:35 -06009#include <mapmem.h>
Simon Glasse5314c12023-01-17 10:47:12 -070010#include <sort.h>
Simon Glassd9f9a892015-06-23 15:38:35 -060011#include <dm/root.h>
Masahiro Yamada77018de2017-06-22 17:10:11 +090012#include <dm/util.h>
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +020013#include <dm/uclass-internal.h>
Simon Glassd9f9a892015-06-23 15:38:35 -060014
Simon Glasse5314c12023-01-17 10:47:12 -070015/**
16 * struct sort_info - information used for sorting
17 *
18 * @dev: List of devices
19 * @alloced: Maximum number of devices in @dev
20 */
21struct sort_info {
22 struct udevice **dev;
23 int size;
24};
25
26static int h_cmp_uclass_id(const void *d1, const void *d2)
27{
28 const struct udevice *const *dev1 = d1;
29 const struct udevice *const *dev2 = d2;
30
31 return device_get_uclass_id(*dev1) - device_get_uclass_id(*dev2);
32}
33
34static void show_devices(struct udevice *dev, int depth, int last_flag,
35 struct udevice **devs)
Simon Glassd9f9a892015-06-23 15:38:35 -060036{
37 int i, is_last;
38 struct udevice *child;
Simon Glass6211d762020-12-19 10:40:10 -070039 u32 flags = dev_get_flags(dev);
Simon Glassd9f9a892015-06-23 15:38:35 -060040
Liviu Dudau764cae52018-10-15 10:03:06 +010041 /* print the first 20 characters to not break the tree-format. */
Simon Glassf03341f2020-12-22 19:30:22 -070042 printf(IS_ENABLED(CONFIG_SPL_BUILD) ? " %s %d [ %c ] %s " :
43 " %-10.10s %3d [ %c ] %-20.20s ", dev->uclass->uc_drv->name,
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +020044 dev_get_uclass_index(dev, NULL),
Simon Glassf03341f2020-12-22 19:30:22 -070045 flags & DM_FLAG_ACTIVATED ? '+' : ' ', dev->driver->name);
Simon Glassd9f9a892015-06-23 15:38:35 -060046
47 for (i = depth; i >= 0; i--) {
48 is_last = (last_flag >> i) & 1;
49 if (i) {
50 if (is_last)
51 printf(" ");
52 else
53 printf("| ");
54 } else {
55 if (is_last)
56 printf("`-- ");
57 else
58 printf("|-- ");
59 }
60 }
61
62 printf("%s\n", dev->name);
63
Simon Glasse5314c12023-01-17 10:47:12 -070064 if (devs) {
65 int count;
66 int i;
67
68 count = 0;
69 device_foreach_child(child, dev)
70 devs[count++] = child;
71 qsort(devs, count, sizeof(struct udevice *), h_cmp_uclass_id);
72
73 for (i = 0; i < count; i++) {
74 show_devices(devs[i], depth + 1,
75 (last_flag << 1) | (i == count - 1),
76 devs + count);
77 }
78 } else {
79 device_foreach_child(child, dev) {
80 is_last = list_is_last(&child->sibling_node,
81 &dev->child_head);
82 show_devices(child, depth + 1,
83 (last_flag << 1) | is_last, NULL);
84 }
Simon Glassd9f9a892015-06-23 15:38:35 -060085 }
86}
87
Simon Glasse5314c12023-01-17 10:47:12 -070088void dm_dump_tree(bool sort)
Simon Glassd9f9a892015-06-23 15:38:35 -060089{
90 struct udevice *root;
91
92 root = dm_root();
93 if (root) {
Simon Glasse5314c12023-01-17 10:47:12 -070094 int dev_count, uclasses;
95 struct udevice **devs = NULL;
96
97 dm_get_stats(&dev_count, &uclasses);
98
Simon Glassdf604242018-12-05 18:42:52 -070099 printf(" Class Index Probed Driver Name\n");
Liviu Dudau764cae52018-10-15 10:03:06 +0100100 printf("-----------------------------------------------------------\n");
Simon Glasse5314c12023-01-17 10:47:12 -0700101 if (sort) {
102 devs = calloc(dev_count, sizeof(struct udevice *));
103 if (!devs) {
104 printf("(out of memory)\n");
105 return;
106 }
107 }
108 show_devices(root, -1, 0, devs);
109 free(devs);
Simon Glassd9f9a892015-06-23 15:38:35 -0600110 }
111}
112
113/**
114 * dm_display_line() - Display information about a single device
115 *
116 * Displays a single line of information with an option prefix
117 *
118 * @dev: Device to display
119 */
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +0200120static void dm_display_line(struct udevice *dev, int index)
Simon Glassd9f9a892015-06-23 15:38:35 -0600121{
Patrick Delaunay68148972019-09-30 10:19:13 +0200122 printf("%-3i %c %s @ %08lx", index,
Simon Glass6211d762020-12-19 10:40:10 -0700123 dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ',
Simon Glassd9f9a892015-06-23 15:38:35 -0600124 dev->name, (ulong)map_to_sysmem(dev));
Simon Glass5e349922020-12-19 10:40:09 -0700125 if (dev->seq_ != -1)
Simon Glassf7eca612020-12-16 21:20:31 -0700126 printf(", seq %d", dev_seq(dev));
Simon Glassd9f9a892015-06-23 15:38:35 -0600127 puts("\n");
128}
129
130void dm_dump_uclass(void)
131{
132 struct uclass *uc;
133 int ret;
134 int id;
135
136 for (id = 0; id < UCLASS_COUNT; id++) {
137 struct udevice *dev;
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +0200138 int i = 0;
Simon Glassd9f9a892015-06-23 15:38:35 -0600139
140 ret = uclass_get(id, &uc);
141 if (ret)
142 continue;
143
144 printf("uclass %d: %s\n", id, uc->uc_drv->name);
Liviu Dudau6f386222018-09-28 14:12:55 +0100145 uclass_foreach_dev(dev, uc) {
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +0200146 dm_display_line(dev, i);
147 i++;
Simon Glassd9f9a892015-06-23 15:38:35 -0600148 }
149 puts("\n");
150 }
151}
Sean Anderson02827572020-01-17 14:48:09 -0500152
Niel Fourie39832fb2020-03-24 16:17:05 +0100153void dm_dump_driver_compat(void)
Sean Anderson02827572020-01-17 14:48:09 -0500154{
155 struct driver *d = ll_entry_start(struct driver, driver);
156 const int n_ents = ll_entry_count(struct driver, driver);
157 struct driver *entry;
158 const struct udevice_id *match;
159
160 puts("Driver Compatible\n");
161 puts("--------------------------------\n");
162 for (entry = d; entry < d + n_ents; entry++) {
Ovidiu Panait8dada7f2020-04-05 19:47:41 +0300163 match = entry->of_match;
164
165 printf("%-20.20s", entry->name);
166 if (match) {
167 printf(" %s", match->compatible);
168 match++;
169 }
170 printf("\n");
171
172 for (; match && match->compatible; match++)
173 printf("%-20.20s %s\n", "", match->compatible);
Sean Anderson02827572020-01-17 14:48:09 -0500174 }
175}
Niel Fourie39832fb2020-03-24 16:17:05 +0100176
177void dm_dump_drivers(void)
178{
179 struct driver *d = ll_entry_start(struct driver, driver);
180 const int n_ents = ll_entry_count(struct driver, driver);
181 struct driver *entry;
182 struct udevice *udev;
183 struct uclass *uc;
Simon Glass4e456c42021-05-13 19:39:24 -0600184 int ret;
Niel Fourie39832fb2020-03-24 16:17:05 +0100185 int i;
186
187 puts("Driver uid uclass Devices\n");
188 puts("----------------------------------------------------------\n");
189
190 for (entry = d; entry < d + n_ents; entry++) {
Simon Glass4e456c42021-05-13 19:39:24 -0600191 ret = uclass_get(entry->id, &uc);
Niel Fourie39832fb2020-03-24 16:17:05 +0100192
193 printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id,
Simon Glass4e456c42021-05-13 19:39:24 -0600194 !ret ? uc->uc_drv->name : "<no uclass>");
Niel Fourie39832fb2020-03-24 16:17:05 +0100195
Simon Glass4e456c42021-05-13 19:39:24 -0600196 if (ret) {
Niel Fourie39832fb2020-03-24 16:17:05 +0100197 puts("\n");
198 continue;
199 }
200
201 i = 0;
202 uclass_foreach_dev(udev, uc) {
203 if (udev->driver != entry)
204 continue;
205 if (i)
206 printf("%-51.51s", "");
207
208 printf("%-25.25s\n", udev->name);
209 i++;
210 }
211 if (!i)
212 puts("<none>\n");
213 }
214}
215
216void dm_dump_static_driver_info(void)
217{
218 struct driver_info *drv = ll_entry_start(struct driver_info,
219 driver_info);
220 const int n_ents = ll_entry_count(struct driver_info, driver_info);
221 struct driver_info *entry;
222
223 puts("Driver Address\n");
224 puts("---------------------------------\n");
Simon Glass3b64ecd2022-05-08 04:39:21 -0600225 for (entry = drv; entry != drv + n_ents; entry++)
226 printf("%-25.25s %p\n", entry->name, entry->plat);
Niel Fourie39832fb2020-03-24 16:17:05 +0100227}
Simon Glassc8b5e222022-05-08 04:39:26 -0600228
229void dm_dump_mem(struct dm_stats *stats)
230{
231 int total, total_delta;
232 int i;
233
234 /* Support SPL printf() */
235 printf("Struct sizes: udevice %x, driver %x, uclass %x, uc_driver %x\n",
236 (int)sizeof(struct udevice), (int)sizeof(struct driver),
237 (int)sizeof(struct uclass), (int)sizeof(struct uclass_driver));
238 printf("Memory: device %x:%x, device names %x, uclass %x:%x\n",
239 stats->dev_count, stats->dev_size, stats->dev_name_size,
240 stats->uc_count, stats->uc_size);
241 printf("\n");
242 printf("%-15s %5s %5s %5s %5s %5s\n", "Attached type", "Count",
243 "Size", "Cur", "Tags", "Save");
244 printf("%-15s %5s %5s %5s %5s %5s\n", "---------------", "-----",
245 "-----", "-----", "-----", "-----");
246 total_delta = 0;
247 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
248 int cur_size, new_size, delta;
249
250 cur_size = stats->dev_count * sizeof(struct udevice);
251 new_size = stats->dev_count * (sizeof(struct udevice) -
252 sizeof(void *));
253 /*
254 * Let's assume we can fit each dmtag_node into 32 bits. We can
255 * limit the 'tiny tags' feature to SPL with
256 * CONFIG_SPL_SYS_MALLOC_F_LEN <= 64KB, so needing 14 bits to
257 * point to anything in that region (with 4-byte alignment).
258 * So:
259 * 4 bits for tag
260 * 14 bits for offset of dev
261 * 14 bits for offset of data
262 */
263 new_size += stats->attach_count[i] * sizeof(u32);
264 delta = cur_size - new_size;
265 total_delta += delta;
266 printf("%-16s %5x %6x %6x %6x %6x (%d)\n", tag_get_name(i),
267 stats->attach_count[i], stats->attach_size[i],
268 cur_size, new_size, delta > 0 ? delta : 0, delta);
269 }
270 printf("%-16s %5x %6x\n", "uclass", stats->uc_attach_count,
271 stats->uc_attach_size);
272 printf("%-16s %5x %6x %5s %5s %6x (%d)\n", "Attached total",
273 stats->attach_count_total + stats->uc_attach_count,
274 stats->attach_size_total + stats->uc_attach_size, "", "",
275 total_delta > 0 ? total_delta : 0, total_delta);
276 printf("%-16s %5x %6x\n", "tags", stats->tag_count, stats->tag_size);
277 printf("\n");
278 printf("Total size: %x (%d)\n", stats->total_size, stats->total_size);
279 printf("\n");
280
281 total = stats->total_size;
282 total -= total_delta;
283 printf("With tags: %x (%d)\n", total, total);
284
285 /* Use singly linked lists in struct udevice (3 nodes in each) */
286 total -= sizeof(void *) * 3 * stats->dev_count;
287 printf("- singly-linked: %x (%d)\n", total, total);
288
289 /* Use an index into the struct_driver list instead of a pointer */
290 total = total + stats->dev_count * (1 - sizeof(void *));
291 printf("- driver index: %x (%d)\n", total, total);
292
293 /* Same with the uclass */
294 total = total + stats->dev_count * (1 - sizeof(void *));
295 printf("- uclass index: %x (%d)\n", total, total);
296
297 /* Drop the device name */
298 printf("Drop device name (not SRAM): %x (%d)\n", stats->dev_name_size,
299 stats->dev_name_size);
300}