blob: 5ec30d5b3c1a49ef92754a907f60800d0040c4e6 [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
Simon Glassd9f9a892015-06-23 15:38:35 -06006#include <dm.h>
Simon Glasse5314c12023-01-17 10:47:12 -07007#include <malloc.h>
Simon Glassd9f9a892015-06-23 15:38:35 -06008#include <mapmem.h>
Simon Glasse5314c12023-01-17 10:47:12 -07009#include <sort.h>
Simon Glassd9f9a892015-06-23 15:38:35 -060010#include <dm/root.h>
Masahiro Yamada77018de2017-06-22 17:10:11 +090011#include <dm/util.h>
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +020012#include <dm/uclass-internal.h>
Simon Glassd9f9a892015-06-23 15:38:35 -060013
Simon Glasse5314c12023-01-17 10:47:12 -070014/**
15 * struct sort_info - information used for sorting
16 *
17 * @dev: List of devices
18 * @alloced: Maximum number of devices in @dev
19 */
20struct sort_info {
21 struct udevice **dev;
22 int size;
23};
24
25static int h_cmp_uclass_id(const void *d1, const void *d2)
26{
27 const struct udevice *const *dev1 = d1;
28 const struct udevice *const *dev2 = d2;
29
30 return device_get_uclass_id(*dev1) - device_get_uclass_id(*dev2);
31}
32
33static void show_devices(struct udevice *dev, int depth, int last_flag,
34 struct udevice **devs)
Simon Glassd9f9a892015-06-23 15:38:35 -060035{
36 int i, is_last;
37 struct udevice *child;
Simon Glass6211d762020-12-19 10:40:10 -070038 u32 flags = dev_get_flags(dev);
Simon Glassd9f9a892015-06-23 15:38:35 -060039
Liviu Dudau764cae52018-10-15 10:03:06 +010040 /* print the first 20 characters to not break the tree-format. */
Samuel Hollanda70a35d2023-01-21 17:30:12 -060041 printf(CONFIG_IS_ENABLED(USE_TINY_PRINTF) ? " %s %d [ %c ] %s " :
Simon Glassf03341f2020-12-22 19:30:22 -070042 " %-10.10s %3d [ %c ] %-20.20s ", dev->uclass->uc_drv->name,
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +020043 dev_get_uclass_index(dev, NULL),
Simon Glassf03341f2020-12-22 19:30:22 -070044 flags & DM_FLAG_ACTIVATED ? '+' : ' ', dev->driver->name);
Simon Glassd9f9a892015-06-23 15:38:35 -060045
46 for (i = depth; i >= 0; i--) {
47 is_last = (last_flag >> i) & 1;
48 if (i) {
49 if (is_last)
50 printf(" ");
51 else
52 printf("| ");
53 } else {
54 if (is_last)
55 printf("`-- ");
56 else
57 printf("|-- ");
58 }
59 }
60
61 printf("%s\n", dev->name);
62
Simon Glasse5314c12023-01-17 10:47:12 -070063 if (devs) {
64 int count;
65 int i;
66
67 count = 0;
68 device_foreach_child(child, dev)
69 devs[count++] = child;
70 qsort(devs, count, sizeof(struct udevice *), h_cmp_uclass_id);
71
72 for (i = 0; i < count; i++) {
73 show_devices(devs[i], depth + 1,
74 (last_flag << 1) | (i == count - 1),
75 devs + count);
76 }
77 } else {
78 device_foreach_child(child, dev) {
79 is_last = list_is_last(&child->sibling_node,
80 &dev->child_head);
81 show_devices(child, depth + 1,
82 (last_flag << 1) | is_last, NULL);
83 }
Simon Glassd9f9a892015-06-23 15:38:35 -060084 }
85}
86
AKASHI Takahirof06f0822023-08-23 10:49:47 +090087static void dm_dump_tree_single(struct udevice *dev, bool sort)
Simon Glassd9f9a892015-06-23 15:38:35 -060088{
AKASHI Takahirof06f0822023-08-23 10:49:47 +090089 int dev_count, uclasses;
90 struct udevice **devs = NULL;
Simon Glassd9f9a892015-06-23 15:38:35 -060091
AKASHI Takahirof06f0822023-08-23 10:49:47 +090092 if (sort) {
Simon Glass01a8d222023-09-19 20:05:48 -060093 dm_get_stats(&dev_count, &uclasses);
AKASHI Takahirof06f0822023-08-23 10:49:47 +090094 devs = calloc(dev_count, sizeof(struct udevice *));
95 if (!devs) {
96 printf("(out of memory)\n");
97 return;
98 }
99 }
100 show_devices(dev, -1, 0, devs);
101 free(devs);
102}
103
104static void dm_dump_tree_recursive(struct udevice *dev, char *dev_name,
105 bool extended, bool sort)
106{
107 struct udevice *child;
108 size_t len;
Simon Glasse5314c12023-01-17 10:47:12 -0700109
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900110 len = strlen(dev_name);
Simon Glasse5314c12023-01-17 10:47:12 -0700111
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900112 device_foreach_child(child, dev) {
113 if (extended) {
114 if (!strncmp(child->name, dev_name, len)) {
115 dm_dump_tree_single(child, sort);
116 continue;
117 }
118 } else {
119 if (!strcmp(child->name, dev_name)) {
120 dm_dump_tree_single(child, sort);
121 continue;
Simon Glasse5314c12023-01-17 10:47:12 -0700122 }
123 }
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900124 dm_dump_tree_recursive(child, dev_name, extended, sort);
125 }
126}
127
128void dm_dump_tree(char *dev_name, bool extended, bool sort)
129{
130 struct udevice *root;
131
132 printf(" Class Index Probed Driver Name\n");
133 printf("-----------------------------------------------------------\n");
134
135 root = dm_root();
136 if (!root)
137 return;
138
139 if (!dev_name || !strcmp(dev_name, "root")) {
140 dm_dump_tree_single(root, sort);
141 return;
Simon Glassd9f9a892015-06-23 15:38:35 -0600142 }
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900143
144 dm_dump_tree_recursive(root, dev_name, extended, sort);
Simon Glassd9f9a892015-06-23 15:38:35 -0600145}
146
147/**
148 * dm_display_line() - Display information about a single device
149 *
150 * Displays a single line of information with an option prefix
151 *
152 * @dev: Device to display
153 */
Jean-Jacques Hiblot9bb6c7f2018-08-09 16:17:43 +0200154static void dm_display_line(struct udevice *dev, int index)
Simon Glassd9f9a892015-06-23 15:38:35 -0600155{
Patrick Delaunay68148972019-09-30 10:19:13 +0200156 printf("%-3i %c %s @ %08lx", index,
Simon Glass6211d762020-12-19 10:40:10 -0700157 dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ',
Simon Glassd9f9a892015-06-23 15:38:35 -0600158 dev->name, (ulong)map_to_sysmem(dev));
Simon Glass5e349922020-12-19 10:40:09 -0700159 if (dev->seq_ != -1)
Simon Glassf7eca612020-12-16 21:20:31 -0700160 printf(", seq %d", dev_seq(dev));
Simon Glassd9f9a892015-06-23 15:38:35 -0600161 puts("\n");
162}
163
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900164static void dm_dump_uclass_single(enum uclass_id id)
Simon Glassd9f9a892015-06-23 15:38:35 -0600165{
166 struct uclass *uc;
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900167 struct udevice *dev;
168 int i = 0, ret;
169
170 ret = uclass_get(id, &uc);
171 if (ret)
172 return;
173
174 printf("uclass %d: %s\n", id, uc->uc_drv->name);
175 uclass_foreach_dev(dev, uc) {
176 dm_display_line(dev, i);
177 i++;
178 }
179 puts("\n");
180}
181
182void dm_dump_uclass(char *uclass, bool extended)
183{
184 struct uclass *uc;
185 enum uclass_id id;
186 bool matching;
Simon Glassd9f9a892015-06-23 15:38:35 -0600187 int ret;
Simon Glassd9f9a892015-06-23 15:38:35 -0600188
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900189 matching = !!(uclass && strcmp(uclass, "root"));
Simon Glassd9f9a892015-06-23 15:38:35 -0600190
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900191 for (id = 0; id < UCLASS_COUNT; id++) {
Simon Glassd9f9a892015-06-23 15:38:35 -0600192 ret = uclass_get(id, &uc);
193 if (ret)
194 continue;
195
AKASHI Takahirof06f0822023-08-23 10:49:47 +0900196 if (matching) {
197 if (extended) {
198 if (!strncmp(uc->uc_drv->name, uclass,
199 strlen(uclass)))
200 dm_dump_uclass_single(id);
201 } else {
202 if (!strcmp(uc->uc_drv->name, uclass))
203 dm_dump_uclass_single(id);
204 }
205 } else {
206 dm_dump_uclass_single(id);
Simon Glassd9f9a892015-06-23 15:38:35 -0600207 }
Simon Glassd9f9a892015-06-23 15:38:35 -0600208 }
209}
Sean Anderson02827572020-01-17 14:48:09 -0500210
Niel Fourie39832fb2020-03-24 16:17:05 +0100211void dm_dump_driver_compat(void)
Sean Anderson02827572020-01-17 14:48:09 -0500212{
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;
217
218 puts("Driver Compatible\n");
219 puts("--------------------------------\n");
220 for (entry = d; entry < d + n_ents; entry++) {
Ovidiu Panait8dada7f2020-04-05 19:47:41 +0300221 match = entry->of_match;
222
223 printf("%-20.20s", entry->name);
224 if (match) {
225 printf(" %s", match->compatible);
226 match++;
227 }
228 printf("\n");
229
230 for (; match && match->compatible; match++)
231 printf("%-20.20s %s\n", "", match->compatible);
Sean Anderson02827572020-01-17 14:48:09 -0500232 }
233}
Niel Fourie39832fb2020-03-24 16:17:05 +0100234
235void dm_dump_drivers(void)
236{
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;
241 struct uclass *uc;
Simon Glass4e456c42021-05-13 19:39:24 -0600242 int ret;
Niel Fourie39832fb2020-03-24 16:17:05 +0100243 int i;
244
245 puts("Driver uid uclass Devices\n");
246 puts("----------------------------------------------------------\n");
247
248 for (entry = d; entry < d + n_ents; entry++) {
Simon Glass4e456c42021-05-13 19:39:24 -0600249 ret = uclass_get(entry->id, &uc);
Niel Fourie39832fb2020-03-24 16:17:05 +0100250
251 printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id,
Simon Glass4e456c42021-05-13 19:39:24 -0600252 !ret ? uc->uc_drv->name : "<no uclass>");
Niel Fourie39832fb2020-03-24 16:17:05 +0100253
Simon Glass4e456c42021-05-13 19:39:24 -0600254 if (ret) {
Niel Fourie39832fb2020-03-24 16:17:05 +0100255 puts("\n");
256 continue;
257 }
258
259 i = 0;
260 uclass_foreach_dev(udev, uc) {
261 if (udev->driver != entry)
262 continue;
263 if (i)
264 printf("%-51.51s", "");
265
266 printf("%-25.25s\n", udev->name);
267 i++;
268 }
269 if (!i)
270 puts("<none>\n");
271 }
272}
273
274void dm_dump_static_driver_info(void)
275{
276 struct driver_info *drv = ll_entry_start(struct driver_info,
277 driver_info);
278 const int n_ents = ll_entry_count(struct driver_info, driver_info);
279 struct driver_info *entry;
280
281 puts("Driver Address\n");
282 puts("---------------------------------\n");
Simon Glass3b64ecd2022-05-08 04:39:21 -0600283 for (entry = drv; entry != drv + n_ents; entry++)
284 printf("%-25.25s %p\n", entry->name, entry->plat);
Niel Fourie39832fb2020-03-24 16:17:05 +0100285}
Simon Glassc8b5e222022-05-08 04:39:26 -0600286
287void dm_dump_mem(struct dm_stats *stats)
288{
289 int total, total_delta;
290 int i;
291
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);
299 printf("\n");
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 "-----", "-----", "-----", "-----");
304 total_delta = 0;
305 for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) {
306 int cur_size, new_size, delta;
307
308 cur_size = stats->dev_count * sizeof(struct udevice);
309 new_size = stats->dev_count * (sizeof(struct udevice) -
310 sizeof(void *));
311 /*
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).
316 * So:
317 * 4 bits for tag
318 * 14 bits for offset of dev
319 * 14 bits for offset of data
320 */
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);
327 }
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);
335 printf("\n");
336 printf("Total size: %x (%d)\n", stats->total_size, stats->total_size);
337 printf("\n");
338
339 total = stats->total_size;
340 total -= total_delta;
341 printf("With tags: %x (%d)\n", total, total);
342
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);
346
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);
350
351 /* Same with the uclass */
352 total = total + stats->dev_count * (1 - sizeof(void *));
353 printf("- uclass index: %x (%d)\n", total, total);
354
355 /* Drop the device name */
356 printf("Drop device name (not SRAM): %x (%d)\n", stats->dev_name_size,
357 stats->dev_name_size);
358}