1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
13 #include <dm/uclass-internal.h>
16 * struct sort_info - information used for sorting
18 * @dev: List of devices
19 * @alloced: Maximum number of devices in @dev
26 static int h_cmp_uclass_id(const void *d1, const void *d2)
28 const struct udevice *const *dev1 = d1;
29 const struct udevice *const *dev2 = d2;
31 return device_get_uclass_id(*dev1) - device_get_uclass_id(*dev2);
34 static void show_devices(struct udevice *dev, int depth, int last_flag,
35 struct udevice **devs)
38 struct udevice *child;
39 u32 flags = dev_get_flags(dev);
41 /* print the first 20 characters to not break the tree-format. */
42 printf(CONFIG_IS_ENABLED(USE_TINY_PRINTF) ? " %s %d [ %c ] %s " :
43 " %-10.10s %3d [ %c ] %-20.20s ", dev->uclass->uc_drv->name,
44 dev_get_uclass_index(dev, NULL),
45 flags & DM_FLAG_ACTIVATED ? '+' : ' ', dev->driver->name);
47 for (i = depth; i >= 0; i--) {
48 is_last = (last_flag >> i) & 1;
62 printf("%s\n", dev->name);
69 device_foreach_child(child, dev)
70 devs[count++] = child;
71 qsort(devs, count, sizeof(struct udevice *), h_cmp_uclass_id);
73 for (i = 0; i < count; i++) {
74 show_devices(devs[i], depth + 1,
75 (last_flag << 1) | (i == count - 1),
79 device_foreach_child(child, dev) {
80 is_last = list_is_last(&child->sibling_node,
82 show_devices(child, depth + 1,
83 (last_flag << 1) | is_last, NULL);
88 void dm_dump_tree(bool sort)
94 int dev_count, uclasses;
95 struct udevice **devs = NULL;
97 dm_get_stats(&dev_count, &uclasses);
99 printf(" Class Index Probed Driver Name\n");
100 printf("-----------------------------------------------------------\n");
102 devs = calloc(dev_count, sizeof(struct udevice *));
104 printf("(out of memory)\n");
108 show_devices(root, -1, 0, devs);
114 * dm_display_line() - Display information about a single device
116 * Displays a single line of information with an option prefix
118 * @dev: Device to display
120 static void dm_display_line(struct udevice *dev, int index)
122 printf("%-3i %c %s @ %08lx", index,
123 dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ',
124 dev->name, (ulong)map_to_sysmem(dev));
126 printf(", seq %d", dev_seq(dev));
130 void dm_dump_uclass(void)
136 for (id = 0; id < UCLASS_COUNT; id++) {
140 ret = uclass_get(id, &uc);
144 printf("uclass %d: %s\n", id, uc->uc_drv->name);
145 uclass_foreach_dev(dev, uc) {
146 dm_display_line(dev, i);
153 void dm_dump_driver_compat(void)
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;
160 puts("Driver Compatible\n");
161 puts("--------------------------------\n");
162 for (entry = d; entry < d + n_ents; entry++) {
163 match = entry->of_match;
165 printf("%-20.20s", entry->name);
167 printf(" %s", match->compatible);
172 for (; match && match->compatible; match++)
173 printf("%-20.20s %s\n", "", match->compatible);
177 void dm_dump_drivers(void)
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;
187 puts("Driver uid uclass Devices\n");
188 puts("----------------------------------------------------------\n");
190 for (entry = d; entry < d + n_ents; entry++) {
191 ret = uclass_get(entry->id, &uc);
193 printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id,
194 !ret ? uc->uc_drv->name : "<no uclass>");
202 uclass_foreach_dev(udev, uc) {
203 if (udev->driver != entry)
206 printf("%-51.51s", "");
208 printf("%-25.25s\n", udev->name);
216 void dm_dump_static_driver_info(void)
218 struct driver_info *drv = ll_entry_start(struct driver_info,
220 const int n_ents = ll_entry_count(struct driver_info, driver_info);
221 struct driver_info *entry;
223 puts("Driver Address\n");
224 puts("---------------------------------\n");
225 for (entry = drv; entry != drv + n_ents; entry++)
226 printf("%-25.25s %p\n", entry->name, entry->plat);
229 void dm_dump_mem(struct dm_stats *stats)
231 int total, total_delta;
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);
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 "-----", "-----", "-----", "-----");
247 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
248 int cur_size, new_size, delta;
250 cur_size = stats->dev_count * sizeof(struct udevice);
251 new_size = stats->dev_count * (sizeof(struct udevice) -
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).
260 * 14 bits for offset of dev
261 * 14 bits for offset of data
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);
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);
278 printf("Total size: %x (%d)\n", stats->total_size, stats->total_size);
281 total = stats->total_size;
282 total -= total_delta;
283 printf("With tags: %x (%d)\n", total, total);
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);
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);
293 /* Same with the uclass */
294 total = total + stats->dev_count * (1 - sizeof(void *));
295 printf("- uclass index: %x (%d)\n", total, total);
297 /* Drop the device name */
298 printf("Drop device name (not SRAM): %x (%d)\n", stats->dev_name_size,
299 stats->dev_name_size);