1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
12 #include <dm/uclass-internal.h>
15 * struct sort_info - information used for sorting
17 * @dev: List of devices
18 * @alloced: Maximum number of devices in @dev
25 static int h_cmp_uclass_id(const void *d1, const void *d2)
27 const struct udevice *const *dev1 = d1;
28 const struct udevice *const *dev2 = d2;
30 return device_get_uclass_id(*dev1) - device_get_uclass_id(*dev2);
33 static void show_devices(struct udevice *dev, int depth, int last_flag,
34 struct udevice **devs)
37 struct udevice *child;
38 u32 flags = dev_get_flags(dev);
40 /* print the first 20 characters to not break the tree-format. */
41 printf(CONFIG_IS_ENABLED(USE_TINY_PRINTF) ? " %s %d [ %c ] %s " :
42 " %-10.10s %3d [ %c ] %-20.20s ", dev->uclass->uc_drv->name,
44 flags & DM_FLAG_ACTIVATED ? '+' : ' ', dev->driver->name);
46 for (i = depth; i >= 0; i--) {
47 is_last = (last_flag >> i) & 1;
61 printf("%s\n", dev->name);
68 device_foreach_child(child, dev)
69 devs[count++] = child;
70 qsort(devs, count, sizeof(struct udevice *), h_cmp_uclass_id);
72 for (i = 0; i < count; i++) {
73 show_devices(devs[i], depth + 1,
74 (last_flag << 1) | (i == count - 1),
78 device_foreach_child(child, dev) {
79 is_last = list_is_last(&child->sibling_node,
81 show_devices(child, depth + 1,
82 (last_flag << 1) | is_last, NULL);
87 static void dm_dump_tree_single(struct udevice *dev, bool sort)
89 int dev_count, uclasses;
90 struct udevice **devs = NULL;
93 dm_get_stats(&dev_count, &uclasses);
94 devs = calloc(dev_count, sizeof(struct udevice *));
96 printf("(out of memory)\n");
100 show_devices(dev, -1, 0, devs);
104 static void dm_dump_tree_recursive(struct udevice *dev, char *dev_name,
105 bool extended, bool sort)
107 struct udevice *child;
110 len = strlen(dev_name);
112 device_foreach_child(child, dev) {
114 if (!strncmp(child->name, dev_name, len)) {
115 dm_dump_tree_single(child, sort);
119 if (!strcmp(child->name, dev_name)) {
120 dm_dump_tree_single(child, sort);
124 dm_dump_tree_recursive(child, dev_name, extended, sort);
128 void dm_dump_tree(char *dev_name, bool extended, bool sort)
130 struct udevice *root;
132 printf(" Class Seq Probed Driver Name\n");
133 printf("-----------------------------------------------------------\n");
139 if (!dev_name || !strcmp(dev_name, "root")) {
140 dm_dump_tree_single(root, sort);
144 dm_dump_tree_recursive(root, dev_name, extended, sort);
148 * dm_display_line() - Display information about a single device
150 * Displays a single line of information with an option prefix
152 * @dev: Device to display
154 static void dm_display_line(struct udevice *dev, int index)
156 printf("%-3i %c %s @ %08lx", index,
157 dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ',
158 dev->name, (ulong)map_to_sysmem(dev));
160 printf(", seq %d", dev_seq(dev));
164 static void dm_dump_uclass_single(enum uclass_id id)
170 ret = uclass_get(id, &uc);
174 printf("uclass %d: %s\n", id, uc->uc_drv->name);
175 uclass_foreach_dev(dev, uc) {
176 dm_display_line(dev, i);
182 void dm_dump_uclass(char *uclass, bool extended)
189 matching = !!(uclass && strcmp(uclass, "root"));
191 for (id = 0; id < UCLASS_COUNT; id++) {
192 ret = uclass_get(id, &uc);
198 if (!strncmp(uc->uc_drv->name, uclass,
200 dm_dump_uclass_single(id);
202 if (!strcmp(uc->uc_drv->name, uclass))
203 dm_dump_uclass_single(id);
206 dm_dump_uclass_single(id);
211 void dm_dump_driver_compat(void)
213 struct driver *d = ll_entry_start(struct driver, driver);
214 const int n_ents = ll_entry_count(struct driver, driver);
215 struct driver *entry;
216 const struct udevice_id *match;
218 puts("Driver Compatible\n");
219 puts("--------------------------------\n");
220 for (entry = d; entry < d + n_ents; entry++) {
221 match = entry->of_match;
223 printf("%-20.20s", entry->name);
225 printf(" %s", match->compatible);
230 for (; match && match->compatible; match++)
231 printf("%-20.20s %s\n", "", match->compatible);
235 void dm_dump_drivers(void)
237 struct driver *d = ll_entry_start(struct driver, driver);
238 const int n_ents = ll_entry_count(struct driver, driver);
239 struct driver *entry;
240 struct udevice *udev;
245 puts("Driver uid uclass Devices\n");
246 puts("----------------------------------------------------------\n");
248 for (entry = d; entry < d + n_ents; entry++) {
249 ret = uclass_get(entry->id, &uc);
251 printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id,
252 !ret ? uc->uc_drv->name : "<no uclass>");
260 uclass_foreach_dev(udev, uc) {
261 if (udev->driver != entry)
264 printf("%-51.51s", "");
266 printf("%-25.25s\n", udev->name);
274 void dm_dump_static_driver_info(void)
276 struct driver_info *drv = ll_entry_start(struct driver_info,
278 const int n_ents = ll_entry_count(struct driver_info, driver_info);
279 struct driver_info *entry;
281 puts("Driver Address\n");
282 puts("---------------------------------\n");
283 for (entry = drv; entry != drv + n_ents; entry++)
284 printf("%-25.25s %p\n", entry->name, entry->plat);
287 void dm_dump_mem(struct dm_stats *stats)
289 int total, total_delta;
292 /* Support SPL printf() */
293 printf("Struct sizes: udevice %x, driver %x, uclass %x, uc_driver %x\n",
294 (int)sizeof(struct udevice), (int)sizeof(struct driver),
295 (int)sizeof(struct uclass), (int)sizeof(struct uclass_driver));
296 printf("Memory: device %x:%x, device names %x, uclass %x:%x\n",
297 stats->dev_count, stats->dev_size, stats->dev_name_size,
298 stats->uc_count, stats->uc_size);
300 printf("%-15s %5s %5s %5s %5s %5s\n", "Attached type", "Count",
301 "Size", "Cur", "Tags", "Save");
302 printf("%-15s %5s %5s %5s %5s %5s\n", "---------------", "-----",
303 "-----", "-----", "-----", "-----");
305 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
306 int cur_size, new_size, delta;
308 cur_size = stats->dev_count * sizeof(struct udevice);
309 new_size = stats->dev_count * (sizeof(struct udevice) -
312 * Let's assume we can fit each dmtag_node into 32 bits. We can
313 * limit the 'tiny tags' feature to SPL with
314 * CONFIG_SPL_SYS_MALLOC_F_LEN <= 64KB, so needing 14 bits to
315 * point to anything in that region (with 4-byte alignment).
318 * 14 bits for offset of dev
319 * 14 bits for offset of data
321 new_size += stats->attach_count[i] * sizeof(u32);
322 delta = cur_size - new_size;
323 total_delta += delta;
324 printf("%-16s %5x %6x %6x %6x %6x (%d)\n", tag_get_name(i),
325 stats->attach_count[i], stats->attach_size[i],
326 cur_size, new_size, delta > 0 ? delta : 0, delta);
328 printf("%-16s %5x %6x\n", "uclass", stats->uc_attach_count,
329 stats->uc_attach_size);
330 printf("%-16s %5x %6x %5s %5s %6x (%d)\n", "Attached total",
331 stats->attach_count_total + stats->uc_attach_count,
332 stats->attach_size_total + stats->uc_attach_size, "", "",
333 total_delta > 0 ? total_delta : 0, total_delta);
334 printf("%-16s %5x %6x\n", "tags", stats->tag_count, stats->tag_size);
336 printf("Total size: %x (%d)\n", stats->total_size, stats->total_size);
339 total = stats->total_size;
340 total -= total_delta;
341 printf("With tags: %x (%d)\n", total, total);
343 /* Use singly linked lists in struct udevice (3 nodes in each) */
344 total -= sizeof(void *) * 3 * stats->dev_count;
345 printf("- singly-linked: %x (%d)\n", total, total);
347 /* Use an index into the struct_driver list instead of a pointer */
348 total = total + stats->dev_count * (1 - sizeof(void *));
349 printf("- driver index: %x (%d)\n", total, total);
351 /* Same with the uclass */
352 total = total + stats->dev_count * (1 - sizeof(void *));
353 printf("- uclass index: %x (%d)\n", total, total);
355 /* Drop the device name */
356 printf("Drop device name (not SRAM): %x (%d)\n", stats->dev_name_size,
357 stats->dev_name_size);