Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 7 | #include <malloc.h> |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 8 | #include <mapmem.h> |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 9 | #include <sort.h> |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 10 | #include <dm/root.h> |
Masahiro Yamada | 77018de | 2017-06-22 17:10:11 +0900 | [diff] [blame] | 11 | #include <dm/util.h> |
Jean-Jacques Hiblot | 9bb6c7f | 2018-08-09 16:17:43 +0200 | [diff] [blame] | 12 | #include <dm/uclass-internal.h> |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 13 | |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 14 | /** |
| 15 | * struct sort_info - information used for sorting |
| 16 | * |
| 17 | * @dev: List of devices |
| 18 | * @alloced: Maximum number of devices in @dev |
| 19 | */ |
| 20 | struct sort_info { |
| 21 | struct udevice **dev; |
| 22 | int size; |
| 23 | }; |
| 24 | |
| 25 | static 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 | |
| 33 | static void show_devices(struct udevice *dev, int depth, int last_flag, |
| 34 | struct udevice **devs) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 35 | { |
| 36 | int i, is_last; |
| 37 | struct udevice *child; |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 38 | u32 flags = dev_get_flags(dev); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 39 | |
Liviu Dudau | 764cae5 | 2018-10-15 10:03:06 +0100 | [diff] [blame] | 40 | /* print the first 20 characters to not break the tree-format. */ |
Samuel Holland | a70a35d | 2023-01-21 17:30:12 -0600 | [diff] [blame] | 41 | printf(CONFIG_IS_ENABLED(USE_TINY_PRINTF) ? " %s %d [ %c ] %s " : |
Simon Glass | f03341f | 2020-12-22 19:30:22 -0700 | [diff] [blame] | 42 | " %-10.10s %3d [ %c ] %-20.20s ", dev->uclass->uc_drv->name, |
Jean-Jacques Hiblot | 9bb6c7f | 2018-08-09 16:17:43 +0200 | [diff] [blame] | 43 | dev_get_uclass_index(dev, NULL), |
Simon Glass | f03341f | 2020-12-22 19:30:22 -0700 | [diff] [blame] | 44 | flags & DM_FLAG_ACTIVATED ? '+' : ' ', dev->driver->name); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 45 | |
| 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 Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 63 | 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 Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 87 | static void dm_dump_tree_single(struct udevice *dev, bool sort) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 88 | { |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 89 | int dev_count, uclasses; |
| 90 | struct udevice **devs = NULL; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 91 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 92 | if (sort) { |
Simon Glass | 01a8d22 | 2023-09-19 20:05:48 -0600 | [diff] [blame] | 93 | dm_get_stats(&dev_count, &uclasses); |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 94 | 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 | |
| 104 | static 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 Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 109 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 110 | len = strlen(dev_name); |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 111 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 112 | 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 Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 124 | dm_dump_tree_recursive(child, dev_name, extended, sort); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void 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 Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 142 | } |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 143 | |
| 144 | dm_dump_tree_recursive(root, dev_name, extended, sort); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 145 | } |
| 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 Hiblot | 9bb6c7f | 2018-08-09 16:17:43 +0200 | [diff] [blame] | 154 | static void dm_display_line(struct udevice *dev, int index) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 155 | { |
Patrick Delaunay | 6814897 | 2019-09-30 10:19:13 +0200 | [diff] [blame] | 156 | printf("%-3i %c %s @ %08lx", index, |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 157 | dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ', |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 158 | dev->name, (ulong)map_to_sysmem(dev)); |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 159 | if (dev->seq_ != -1) |
Simon Glass | f7eca61 | 2020-12-16 21:20:31 -0700 | [diff] [blame] | 160 | printf(", seq %d", dev_seq(dev)); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 161 | puts("\n"); |
| 162 | } |
| 163 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 164 | static void dm_dump_uclass_single(enum uclass_id id) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 165 | { |
| 166 | struct uclass *uc; |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 167 | 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 | |
| 182 | void dm_dump_uclass(char *uclass, bool extended) |
| 183 | { |
| 184 | struct uclass *uc; |
| 185 | enum uclass_id id; |
| 186 | bool matching; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 187 | int ret; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 188 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 189 | matching = !!(uclass && strcmp(uclass, "root")); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 190 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 191 | for (id = 0; id < UCLASS_COUNT; id++) { |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 192 | ret = uclass_get(id, &uc); |
| 193 | if (ret) |
| 194 | continue; |
| 195 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 196 | 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 Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 207 | } |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 208 | } |
| 209 | } |
Sean Anderson | 0282757 | 2020-01-17 14:48:09 -0500 | [diff] [blame] | 210 | |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 211 | void dm_dump_driver_compat(void) |
Sean Anderson | 0282757 | 2020-01-17 14:48:09 -0500 | [diff] [blame] | 212 | { |
| 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 Panait | 8dada7f | 2020-04-05 19:47:41 +0300 | [diff] [blame] | 221 | 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 Anderson | 0282757 | 2020-01-17 14:48:09 -0500 | [diff] [blame] | 232 | } |
| 233 | } |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 234 | |
| 235 | void 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 Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 242 | int ret; |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 243 | int i; |
| 244 | |
| 245 | puts("Driver uid uclass Devices\n"); |
| 246 | puts("----------------------------------------------------------\n"); |
| 247 | |
| 248 | for (entry = d; entry < d + n_ents; entry++) { |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 249 | ret = uclass_get(entry->id, &uc); |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 250 | |
| 251 | printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id, |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 252 | !ret ? uc->uc_drv->name : "<no uclass>"); |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 253 | |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 254 | if (ret) { |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 255 | 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 | |
| 274 | void 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 Glass | 3b64ecd | 2022-05-08 04:39:21 -0600 | [diff] [blame] | 283 | for (entry = drv; entry != drv + n_ents; entry++) |
| 284 | printf("%-25.25s %p\n", entry->name, entry->plat); |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 285 | } |
Simon Glass | c8b5e22 | 2022-05-08 04:39:26 -0600 | [diff] [blame] | 286 | |
| 287 | void 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 | } |