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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 9 | #include <mapmem.h> |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 10 | #include <sort.h> |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 11 | #include <dm/root.h> |
Masahiro Yamada | 77018de | 2017-06-22 17:10:11 +0900 | [diff] [blame] | 12 | #include <dm/util.h> |
Jean-Jacques Hiblot | 9bb6c7f | 2018-08-09 16:17:43 +0200 | [diff] [blame] | 13 | #include <dm/uclass-internal.h> |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 14 | |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 15 | /** |
| 16 | * struct sort_info - information used for sorting |
| 17 | * |
| 18 | * @dev: List of devices |
| 19 | * @alloced: Maximum number of devices in @dev |
| 20 | */ |
| 21 | struct sort_info { |
| 22 | struct udevice **dev; |
| 23 | int size; |
| 24 | }; |
| 25 | |
| 26 | static 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 | |
| 34 | static void show_devices(struct udevice *dev, int depth, int last_flag, |
| 35 | struct udevice **devs) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 36 | { |
| 37 | int i, is_last; |
| 38 | struct udevice *child; |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 39 | u32 flags = dev_get_flags(dev); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 40 | |
Liviu Dudau | 764cae5 | 2018-10-15 10:03:06 +0100 | [diff] [blame] | 41 | /* print the first 20 characters to not break the tree-format. */ |
Samuel Holland | a70a35d | 2023-01-21 17:30:12 -0600 | [diff] [blame] | 42 | printf(CONFIG_IS_ENABLED(USE_TINY_PRINTF) ? " %s %d [ %c ] %s " : |
Simon Glass | f03341f | 2020-12-22 19:30:22 -0700 | [diff] [blame] | 43 | " %-10.10s %3d [ %c ] %-20.20s ", dev->uclass->uc_drv->name, |
Jean-Jacques Hiblot | 9bb6c7f | 2018-08-09 16:17:43 +0200 | [diff] [blame] | 44 | dev_get_uclass_index(dev, NULL), |
Simon Glass | f03341f | 2020-12-22 19:30:22 -0700 | [diff] [blame] | 45 | flags & DM_FLAG_ACTIVATED ? '+' : ' ', dev->driver->name); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 46 | |
| 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 Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 64 | 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 Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 88 | static void dm_dump_tree_single(struct udevice *dev, bool sort) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 89 | { |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 90 | int dev_count, uclasses; |
| 91 | struct udevice **devs = NULL; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 92 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 93 | if (sort) { |
Simon Glass | 01a8d22 | 2023-09-19 20:05:48 -0600 | [diff] [blame] | 94 | dm_get_stats(&dev_count, &uclasses); |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 95 | devs = calloc(dev_count, sizeof(struct udevice *)); |
| 96 | if (!devs) { |
| 97 | printf("(out of memory)\n"); |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | show_devices(dev, -1, 0, devs); |
| 102 | free(devs); |
| 103 | } |
| 104 | |
| 105 | static void dm_dump_tree_recursive(struct udevice *dev, char *dev_name, |
| 106 | bool extended, bool sort) |
| 107 | { |
| 108 | struct udevice *child; |
| 109 | size_t len; |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 110 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 111 | len = strlen(dev_name); |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 112 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 113 | device_foreach_child(child, dev) { |
| 114 | if (extended) { |
| 115 | if (!strncmp(child->name, dev_name, len)) { |
| 116 | dm_dump_tree_single(child, sort); |
| 117 | continue; |
| 118 | } |
| 119 | } else { |
| 120 | if (!strcmp(child->name, dev_name)) { |
| 121 | dm_dump_tree_single(child, sort); |
| 122 | continue; |
Simon Glass | e5314c1 | 2023-01-17 10:47:12 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 125 | dm_dump_tree_recursive(child, dev_name, extended, sort); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void dm_dump_tree(char *dev_name, bool extended, bool sort) |
| 130 | { |
| 131 | struct udevice *root; |
| 132 | |
| 133 | printf(" Class Index Probed Driver Name\n"); |
| 134 | printf("-----------------------------------------------------------\n"); |
| 135 | |
| 136 | root = dm_root(); |
| 137 | if (!root) |
| 138 | return; |
| 139 | |
| 140 | if (!dev_name || !strcmp(dev_name, "root")) { |
| 141 | dm_dump_tree_single(root, sort); |
| 142 | return; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 143 | } |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 144 | |
| 145 | dm_dump_tree_recursive(root, dev_name, extended, sort); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * dm_display_line() - Display information about a single device |
| 150 | * |
| 151 | * Displays a single line of information with an option prefix |
| 152 | * |
| 153 | * @dev: Device to display |
| 154 | */ |
Jean-Jacques Hiblot | 9bb6c7f | 2018-08-09 16:17:43 +0200 | [diff] [blame] | 155 | static void dm_display_line(struct udevice *dev, int index) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 156 | { |
Patrick Delaunay | 6814897 | 2019-09-30 10:19:13 +0200 | [diff] [blame] | 157 | printf("%-3i %c %s @ %08lx", index, |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 158 | dev_get_flags(dev) & DM_FLAG_ACTIVATED ? '*' : ' ', |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 159 | dev->name, (ulong)map_to_sysmem(dev)); |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 160 | if (dev->seq_ != -1) |
Simon Glass | f7eca61 | 2020-12-16 21:20:31 -0700 | [diff] [blame] | 161 | printf(", seq %d", dev_seq(dev)); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 162 | puts("\n"); |
| 163 | } |
| 164 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 165 | static void dm_dump_uclass_single(enum uclass_id id) |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 166 | { |
| 167 | struct uclass *uc; |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 168 | struct udevice *dev; |
| 169 | int i = 0, ret; |
| 170 | |
| 171 | ret = uclass_get(id, &uc); |
| 172 | if (ret) |
| 173 | return; |
| 174 | |
| 175 | printf("uclass %d: %s\n", id, uc->uc_drv->name); |
| 176 | uclass_foreach_dev(dev, uc) { |
| 177 | dm_display_line(dev, i); |
| 178 | i++; |
| 179 | } |
| 180 | puts("\n"); |
| 181 | } |
| 182 | |
| 183 | void dm_dump_uclass(char *uclass, bool extended) |
| 184 | { |
| 185 | struct uclass *uc; |
| 186 | enum uclass_id id; |
| 187 | bool matching; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 188 | int ret; |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 189 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 190 | matching = !!(uclass && strcmp(uclass, "root")); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 191 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 192 | for (id = 0; id < UCLASS_COUNT; id++) { |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 193 | ret = uclass_get(id, &uc); |
| 194 | if (ret) |
| 195 | continue; |
| 196 | |
AKASHI Takahiro | f06f082 | 2023-08-23 10:49:47 +0900 | [diff] [blame] | 197 | if (matching) { |
| 198 | if (extended) { |
| 199 | if (!strncmp(uc->uc_drv->name, uclass, |
| 200 | strlen(uclass))) |
| 201 | dm_dump_uclass_single(id); |
| 202 | } else { |
| 203 | if (!strcmp(uc->uc_drv->name, uclass)) |
| 204 | dm_dump_uclass_single(id); |
| 205 | } |
| 206 | } else { |
| 207 | dm_dump_uclass_single(id); |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 208 | } |
Simon Glass | d9f9a89 | 2015-06-23 15:38:35 -0600 | [diff] [blame] | 209 | } |
| 210 | } |
Sean Anderson | 0282757 | 2020-01-17 14:48:09 -0500 | [diff] [blame] | 211 | |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 212 | void dm_dump_driver_compat(void) |
Sean Anderson | 0282757 | 2020-01-17 14:48:09 -0500 | [diff] [blame] | 213 | { |
| 214 | struct driver *d = ll_entry_start(struct driver, driver); |
| 215 | const int n_ents = ll_entry_count(struct driver, driver); |
| 216 | struct driver *entry; |
| 217 | const struct udevice_id *match; |
| 218 | |
| 219 | puts("Driver Compatible\n"); |
| 220 | puts("--------------------------------\n"); |
| 221 | for (entry = d; entry < d + n_ents; entry++) { |
Ovidiu Panait | 8dada7f | 2020-04-05 19:47:41 +0300 | [diff] [blame] | 222 | match = entry->of_match; |
| 223 | |
| 224 | printf("%-20.20s", entry->name); |
| 225 | if (match) { |
| 226 | printf(" %s", match->compatible); |
| 227 | match++; |
| 228 | } |
| 229 | printf("\n"); |
| 230 | |
| 231 | for (; match && match->compatible; match++) |
| 232 | printf("%-20.20s %s\n", "", match->compatible); |
Sean Anderson | 0282757 | 2020-01-17 14:48:09 -0500 | [diff] [blame] | 233 | } |
| 234 | } |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 235 | |
| 236 | void dm_dump_drivers(void) |
| 237 | { |
| 238 | struct driver *d = ll_entry_start(struct driver, driver); |
| 239 | const int n_ents = ll_entry_count(struct driver, driver); |
| 240 | struct driver *entry; |
| 241 | struct udevice *udev; |
| 242 | struct uclass *uc; |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 243 | int ret; |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 244 | int i; |
| 245 | |
| 246 | puts("Driver uid uclass Devices\n"); |
| 247 | puts("----------------------------------------------------------\n"); |
| 248 | |
| 249 | for (entry = d; entry < d + n_ents; entry++) { |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 250 | ret = uclass_get(entry->id, &uc); |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 251 | |
| 252 | printf("%-25.25s %-3.3d %-20.20s ", entry->name, entry->id, |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 253 | !ret ? uc->uc_drv->name : "<no uclass>"); |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 254 | |
Simon Glass | 4e456c4 | 2021-05-13 19:39:24 -0600 | [diff] [blame] | 255 | if (ret) { |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 256 | puts("\n"); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | i = 0; |
| 261 | uclass_foreach_dev(udev, uc) { |
| 262 | if (udev->driver != entry) |
| 263 | continue; |
| 264 | if (i) |
| 265 | printf("%-51.51s", ""); |
| 266 | |
| 267 | printf("%-25.25s\n", udev->name); |
| 268 | i++; |
| 269 | } |
| 270 | if (!i) |
| 271 | puts("<none>\n"); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void dm_dump_static_driver_info(void) |
| 276 | { |
| 277 | struct driver_info *drv = ll_entry_start(struct driver_info, |
| 278 | driver_info); |
| 279 | const int n_ents = ll_entry_count(struct driver_info, driver_info); |
| 280 | struct driver_info *entry; |
| 281 | |
| 282 | puts("Driver Address\n"); |
| 283 | puts("---------------------------------\n"); |
Simon Glass | 3b64ecd | 2022-05-08 04:39:21 -0600 | [diff] [blame] | 284 | for (entry = drv; entry != drv + n_ents; entry++) |
| 285 | printf("%-25.25s %p\n", entry->name, entry->plat); |
Niel Fourie | 39832fb | 2020-03-24 16:17:05 +0100 | [diff] [blame] | 286 | } |
Simon Glass | c8b5e22 | 2022-05-08 04:39:26 -0600 | [diff] [blame] | 287 | |
| 288 | void dm_dump_mem(struct dm_stats *stats) |
| 289 | { |
| 290 | int total, total_delta; |
| 291 | int i; |
| 292 | |
| 293 | /* Support SPL printf() */ |
| 294 | printf("Struct sizes: udevice %x, driver %x, uclass %x, uc_driver %x\n", |
| 295 | (int)sizeof(struct udevice), (int)sizeof(struct driver), |
| 296 | (int)sizeof(struct uclass), (int)sizeof(struct uclass_driver)); |
| 297 | printf("Memory: device %x:%x, device names %x, uclass %x:%x\n", |
| 298 | stats->dev_count, stats->dev_size, stats->dev_name_size, |
| 299 | stats->uc_count, stats->uc_size); |
| 300 | printf("\n"); |
| 301 | printf("%-15s %5s %5s %5s %5s %5s\n", "Attached type", "Count", |
| 302 | "Size", "Cur", "Tags", "Save"); |
| 303 | printf("%-15s %5s %5s %5s %5s %5s\n", "---------------", "-----", |
| 304 | "-----", "-----", "-----", "-----"); |
| 305 | total_delta = 0; |
| 306 | for (i = 0; i < DM_TAG_ATTACH_COUNT; i++) { |
| 307 | int cur_size, new_size, delta; |
| 308 | |
| 309 | cur_size = stats->dev_count * sizeof(struct udevice); |
| 310 | new_size = stats->dev_count * (sizeof(struct udevice) - |
| 311 | sizeof(void *)); |
| 312 | /* |
| 313 | * Let's assume we can fit each dmtag_node into 32 bits. We can |
| 314 | * limit the 'tiny tags' feature to SPL with |
| 315 | * CONFIG_SPL_SYS_MALLOC_F_LEN <= 64KB, so needing 14 bits to |
| 316 | * point to anything in that region (with 4-byte alignment). |
| 317 | * So: |
| 318 | * 4 bits for tag |
| 319 | * 14 bits for offset of dev |
| 320 | * 14 bits for offset of data |
| 321 | */ |
| 322 | new_size += stats->attach_count[i] * sizeof(u32); |
| 323 | delta = cur_size - new_size; |
| 324 | total_delta += delta; |
| 325 | printf("%-16s %5x %6x %6x %6x %6x (%d)\n", tag_get_name(i), |
| 326 | stats->attach_count[i], stats->attach_size[i], |
| 327 | cur_size, new_size, delta > 0 ? delta : 0, delta); |
| 328 | } |
| 329 | printf("%-16s %5x %6x\n", "uclass", stats->uc_attach_count, |
| 330 | stats->uc_attach_size); |
| 331 | printf("%-16s %5x %6x %5s %5s %6x (%d)\n", "Attached total", |
| 332 | stats->attach_count_total + stats->uc_attach_count, |
| 333 | stats->attach_size_total + stats->uc_attach_size, "", "", |
| 334 | total_delta > 0 ? total_delta : 0, total_delta); |
| 335 | printf("%-16s %5x %6x\n", "tags", stats->tag_count, stats->tag_size); |
| 336 | printf("\n"); |
| 337 | printf("Total size: %x (%d)\n", stats->total_size, stats->total_size); |
| 338 | printf("\n"); |
| 339 | |
| 340 | total = stats->total_size; |
| 341 | total -= total_delta; |
| 342 | printf("With tags: %x (%d)\n", total, total); |
| 343 | |
| 344 | /* Use singly linked lists in struct udevice (3 nodes in each) */ |
| 345 | total -= sizeof(void *) * 3 * stats->dev_count; |
| 346 | printf("- singly-linked: %x (%d)\n", total, total); |
| 347 | |
| 348 | /* Use an index into the struct_driver list instead of a pointer */ |
| 349 | total = total + stats->dev_count * (1 - sizeof(void *)); |
| 350 | printf("- driver index: %x (%d)\n", total, total); |
| 351 | |
| 352 | /* Same with the uclass */ |
| 353 | total = total + stats->dev_count * (1 - sizeof(void *)); |
| 354 | printf("- uclass index: %x (%d)\n", total, total); |
| 355 | |
| 356 | /* Drop the device name */ |
| 357 | printf("Drop device name (not SRAM): %x (%d)\n", stats->dev_name_size, |
| 358 | stats->dev_name_size); |
| 359 | } |