blob: 4023b390f54255dd2ecc43c273ff1dbb238a4db7 [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. */
Samuel Hollanda70a35d2023-01-21 17:30:12 -060042 printf(CONFIG_IS_ENABLED(USE_TINY_PRINTF) ? " %s %d [ %c ] %s " :
Simon Glassf03341f2020-12-22 19:30:22 -070043 " %-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
AKASHI Takahirof06f0822023-08-23 10:49:47 +090088static void dm_dump_tree_single(struct udevice *dev, bool sort)
Simon Glassd9f9a892015-06-23 15:38:35 -060089{
AKASHI Takahirof06f0822023-08-23 10:49:47 +090090 int dev_count, uclasses;
91 struct udevice **devs = NULL;
Simon Glassd9f9a892015-06-23 15:38:35 -060092
AKASHI Takahirof06f0822023-08-23 10:49:47 +090093 dm_get_stats(&dev_count, &uclasses);
94
95 if (sort) {
96 devs = calloc(dev_count, sizeof(struct udevice *));
97 if (!devs) {
98 printf("(out of memory)\n");
99 return;
100 }
101 }
102 show_devices(dev, -1, 0, devs);
103 free(devs);
104}
105
106static void dm_dump_tree_recursive(struct udevice *dev, char *dev_name,
107 bool extended, bool sort)
108{
109 struct udevice *child;
110 size_t len;
Simon Glasse5314c12023-01-17 10:47:12 -0700111
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900112 len = strlen(dev_name);
Simon Glasse5314c12023-01-17 10:47:12 -0700113
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900114 device_foreach_child(child, dev) {
115 if (extended) {
116 if (!strncmp(child->name, dev_name, len)) {
117 dm_dump_tree_single(child, sort);
118 continue;
119 }
120 } else {
121 if (!strcmp(child->name, dev_name)) {
122 dm_dump_tree_single(child, sort);
123 continue;
Simon Glasse5314c12023-01-17 10:47:12 -0700124 }
125 }
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900126 dm_dump_tree_recursive(child, dev_name, extended, sort);
127 }
128}
129
130void dm_dump_tree(char *dev_name, bool extended, bool sort)
131{
132 struct udevice *root;
133
134 printf(" Class Index Probed Driver Name\n");
135 printf("-----------------------------------------------------------\n");
136
137 root = dm_root();
138 if (!root)
139 return;
140
141 if (!dev_name || !strcmp(dev_name, "root")) {
142 dm_dump_tree_single(root, sort);
143 return;
Simon Glassd9f9a892015-06-23 15:38:35 -0600144 }
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900145
146 dm_dump_tree_recursive(root, dev_name, extended, sort);
Simon Glassd9f9a892015-06-23 15:38:35 -0600147}
148
149/**
150 * dm_display_line() - Display information about a single device
151 *
152 * Displays a single line of information with an option prefix
153 *
154 * @dev: Device to display
155 */
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +0200156static void dm_display_line(struct udevice *dev, int index)
Simon Glassd9f9a892015-06-23 15:38:35 -0600157{
Patrick Delaunay68148972019-09-30 10:19:13 +0200158 printf("%-3i %c %s @ %08lx", index,
Simon Glass6211d762020-12-19 10:40:10 -0700159 dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ',
Simon Glassd9f9a892015-06-23 15:38:35 -0600160 dev->name, (ulong)map_to_sysmem(dev));
Simon Glass5e349922020-12-19 10:40:09 -0700161 if (dev->seq_ != -1)
Simon Glassf7eca612020-12-16 21:20:31 -0700162 printf(", seq %d", dev_seq(dev));
Simon Glassd9f9a892015-06-23 15:38:35 -0600163 puts("\n");
164}
165
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900166static void dm_dump_uclass_single(enum uclass_id id)
Simon Glassd9f9a892015-06-23 15:38:35 -0600167{
168 struct uclass *uc;
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900169 struct udevice *dev;
170 int i = 0, ret;
171
172 ret = uclass_get(id, &uc);
173 if (ret)
174 return;
175
176 printf("uclass %d: %s\n", id, uc->uc_drv->name);
177 uclass_foreach_dev(dev, uc) {
178 dm_display_line(dev, i);
179 i++;
180 }
181 puts("\n");
182}
183
184void dm_dump_uclass(char *uclass, bool extended)
185{
186 struct uclass *uc;
187 enum uclass_id id;
188 bool matching;
Simon Glassd9f9a892015-06-23 15:38:35 -0600189 int ret;
Simon Glassd9f9a892015-06-23 15:38:35 -0600190
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900191 matching = !!(uclass && strcmp(uclass, "root"));
Simon Glassd9f9a892015-06-23 15:38:35 -0600192
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900193 for (id = 0; id < UCLASS_COUNT; id++) {
Simon Glassd9f9a892015-06-23 15:38:35 -0600194 ret = uclass_get(id, &uc);
195 if (ret)
196 continue;
197
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900198 if (matching) {
199 if (extended) {
200 if (!strncmp(uc->uc_drv->name, uclass,
201 strlen(uclass)))
202 dm_dump_uclass_single(id);
203 } else {
204 if (!strcmp(uc->uc_drv->name, uclass))
205 dm_dump_uclass_single(id);
206 }
207 } else {
208 dm_dump_uclass_single(id);
Simon Glassd9f9a892015-06-23 15:38:35 -0600209 }
Simon Glassd9f9a892015-06-23 15:38:35 -0600210 }
211}
Sean Anderson02827572020-01-17 14:48:09 -0500212
Niel Fourie39832fb2020-03-24 16:17:05 +0100213void dm_dump_driver_compat(void)
Sean Anderson02827572020-01-17 14:48:09 -0500214{
215 struct driver *d = ll_entry_start(struct driver, driver);
216 const int n_ents = ll_entry_count(struct driver, driver);
217 struct driver *entry;
218 const struct udevice_id *match;
219
220 puts("Driver Compatible\n");
221 puts("--------------------------------\n");
222 for (entry = d; entry < d + n_ents; entry++) {
Ovidiu Panait8dada7f2020-04-05 19:47:41 +0300223 match = entry->of_match;
224
225 printf("%-20.20s", entry->name);
226 if (match) {
227 printf(" %s", match->compatible);
228 match++;
229 }
230 printf("\n");
231
232 for (; match && match->compatible; match++)
233 printf("%-20.20s %s\n", "", match->compatible);
Sean Anderson02827572020-01-17 14:48:09 -0500234 }
235}
Niel Fourie39832fb2020-03-24 16:17:05 +0100236
237void dm_dump_drivers(void)
238{
239 struct driver *d = ll_entry_start(struct driver, driver);
240 const int n_ents = ll_entry_count(struct driver, driver);
241 struct driver *entry;
242 struct udevice *udev;
243 struct uclass *uc;
Simon Glass4e456c42021-05-13 19:39:24 -0600244 int ret;
Niel Fourie39832fb2020-03-24 16:17:05 +0100245 int i;
246
247 puts("Driver uid uclass Devices\n");
248 puts("----------------------------------------------------------\n");
249
250 for (entry = d; entry < d + n_ents; entry++) {
Simon Glass4e456c42021-05-13 19:39:24 -0600251 ret = uclass_get(entry->id, &uc);
Niel Fourie39832fb2020-03-24 16:17:05 +0100252
253 printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id,
Simon Glass4e456c42021-05-13 19:39:24 -0600254 !ret ? uc->uc_drv->name : "<no uclass>");
Niel Fourie39832fb2020-03-24 16:17:05 +0100255
Simon Glass4e456c42021-05-13 19:39:24 -0600256 if (ret) {
Niel Fourie39832fb2020-03-24 16:17:05 +0100257 puts("\n");
258 continue;
259 }
260
261 i = 0;
262 uclass_foreach_dev(udev, uc) {
263 if (udev->driver != entry)
264 continue;
265 if (i)
266 printf("%-51.51s", "");
267
268 printf("%-25.25s\n", udev->name);
269 i++;
270 }
271 if (!i)
272 puts("<none>\n");
273 }
274}
275
276void dm_dump_static_driver_info(void)
277{
278 struct driver_info *drv = ll_entry_start(struct driver_info,
279 driver_info);
280 const int n_ents = ll_entry_count(struct driver_info, driver_info);
281 struct driver_info *entry;
282
283 puts("Driver Address\n");
284 puts("---------------------------------\n");
Simon Glass3b64ecd2022-05-08 04:39:21 -0600285 for (entry = drv; entry != drv + n_ents; entry++)
286 printf("%-25.25s %p\n", entry->name, entry->plat);
Niel Fourie39832fb2020-03-24 16:17:05 +0100287}
Simon Glassc8b5e222022-05-08 04:39:26 -0600288
289void dm_dump_mem(struct dm_stats *stats)
290{
291 int total, total_delta;
292 int i;
293
294 /* Support SPL printf() */
295 printf("Struct sizes: udevice %x, driver %x, uclass %x, uc_driver %x\n",
296 (int)sizeof(struct udevice), (int)sizeof(struct driver),
297 (int)sizeof(struct uclass), (int)sizeof(struct uclass_driver));
298 printf("Memory: device %x:%x, device names %x, uclass %x:%x\n",
299 stats->dev_count, stats->dev_size, stats->dev_name_size,
300 stats->uc_count, stats->uc_size);
301 printf("\n");
302 printf("%-15s %5s %5s %5s %5s %5s\n", "Attached type", "Count",
303 "Size", "Cur", "Tags", "Save");
304 printf("%-15s %5s %5s %5s %5s %5s\n", "---------------", "-----",
305 "-----", "-----", "-----", "-----");
306 total_delta = 0;
307 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
308 int cur_size, new_size, delta;
309
310 cur_size = stats->dev_count * sizeof(struct udevice);
311 new_size = stats->dev_count * (sizeof(struct udevice) -
312 sizeof(void *));
313 /*
314 * Let's assume we can fit each dmtag_node into 32 bits. We can
315 * limit the 'tiny tags' feature to SPL with
316 * CONFIG_SPL_SYS_MALLOC_F_LEN <= 64KB, so needing 14 bits to
317 * point to anything in that region (with 4-byte alignment).
318 * So:
319 * 4 bits for tag
320 * 14 bits for offset of dev
321 * 14 bits for offset of data
322 */
323 new_size += stats->attach_count[i] * sizeof(u32);
324 delta = cur_size - new_size;
325 total_delta += delta;
326 printf("%-16s %5x %6x %6x %6x %6x (%d)\n", tag_get_name(i),
327 stats->attach_count[i], stats->attach_size[i],
328 cur_size, new_size, delta > 0 ? delta : 0, delta);
329 }
330 printf("%-16s %5x %6x\n", "uclass", stats->uc_attach_count,
331 stats->uc_attach_size);
332 printf("%-16s %5x %6x %5s %5s %6x (%d)\n", "Attached total",
333 stats->attach_count_total + stats->uc_attach_count,
334 stats->attach_size_total + stats->uc_attach_size, "", "",
335 total_delta > 0 ? total_delta : 0, total_delta);
336 printf("%-16s %5x %6x\n", "tags", stats->tag_count, stats->tag_size);
337 printf("\n");
338 printf("Total size: %x (%d)\n", stats->total_size, stats->total_size);
339 printf("\n");
340
341 total = stats->total_size;
342 total -= total_delta;
343 printf("With tags: %x (%d)\n", total, total);
344
345 /* Use singly linked lists in struct udevice (3 nodes in each) */
346 total -= sizeof(void *) * 3 * stats->dev_count;
347 printf("- singly-linked: %x (%d)\n", total, total);
348
349 /* Use an index into the struct_driver list instead of a pointer */
350 total = total + stats->dev_count * (1 - sizeof(void *));
351 printf("- driver index: %x (%d)\n", total, total);
352
353 /* Same with the uclass */
354 total = total + stats->dev_count * (1 - sizeof(void *));
355 printf("- uclass index: %x (%d)\n", total, total);
356
357 /* Drop the device name */
358 printf("Drop device name (not SRAM): %x (%d)\n", stats->dev_name_size,
359 stats->dev_name_size);
360}