| 1 | |
| 2 | |
| 3 | * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
|
| 4 | * Copyright (c) 2022-2025, Qualcomm Innovation Center, Inc. All rights reserved.
|
| 5 |
|
| 6 |
|
| 7 | #include <linux/bitfield.h>
|
| 8 | #include <linux/debugfs.h>
|
| 9 | #include <linux/device.h>
|
| 10 | #include <linux/io.h>
|
| 11 | #include <linux/module.h>
|
| 12 | #include <linux/of.h>
|
| 13 | #include <linux/platform_device.h>
|
| 14 | #include <linux/seq_file.h>
|
| 15 |
|
| 16 | #include <linux/soc/qcom/qcom_aoss.h>
|
| 17 | #include <linux/soc/qcom/smem.h>
|
| 18 | #include <clocksource/arm_arch_timer.h>
|
| 19 |
|
| 20 | #define RPM_DYNAMIC_ADDR 0x14
|
| 21 | #define RPM_DYNAMIC_ADDR_MASK 0xFFFF
|
| 22 |
|
| 23 | #define DDR_STATS_MAGIC_KEY 0xA1157A75
|
| 24 | #define DDR_STATS_MAX_NUM_MODES 20
|
| 25 | #define DDR_STATS_MAGIC_KEY_ADDR 0x0
|
| 26 | #define DDR_STATS_NUM_MODES_ADDR 0x4
|
| 27 | #define DDR_STATS_ENTRY_START_ADDR 0x8
|
| 28 |
|
| 29 | #define DDR_STATS_CP_IDX(data) FIELD_GET(GENMASK(4, 0), data)
|
| 30 | #define DDR_STATS_LPM_NAME(data) FIELD_GET(GENMASK(7, 0), data)
|
| 31 | #define DDR_STATS_TYPE(data) FIELD_GET(GENMASK(15, 8), data)
|
| 32 | #define DDR_STATS_FREQ(data) FIELD_GET(GENMASK(31, 16), data)
|
| 33 |
|
| 34 | static struct qmp *qcom_stats_qmp;
|
| 35 |
|
| 36 | struct subsystem_data {
|
| 37 | const char *name;
|
| 38 | u32 smem_item;
|
| 39 | u32 pid;
|
| 40 | };
|
| 41 |
|
| 42 | static const struct subsystem_data subsystems[] = {
|
| 43 | { "modem", 605, 1 },
|
| 44 | { "wpss", 605, 13 },
|
| 45 | { "adsp", 606, 2 },
|
| 46 | { "cdsp", 607, 5 },
|
| 47 | { "cdsp1", 607, 12 },
|
| 48 | { "gpdsp0", 607, 17 },
|
| 49 | { "gpdsp1", 607, 18 },
|
| 50 | { "soccp", 607, 19 },
|
| 51 | { "dcp", 607, 22 },
|
| 52 | { "slpi", 608, 3 },
|
| 53 | { "gpu", 609, 0 },
|
| 54 | { "display", 610, 0 },
|
| 55 | { "adsp_island", 613, 2 },
|
| 56 | { "slpi_island", 613, 3 },
|
| 57 | { "apss", 631, QCOM_SMEM_HOST_ANY },
|
| 58 | };
|
| 59 |
|
| 60 | struct stats_config {
|
| 61 | size_t stats_offset;
|
| 62 | size_t ddr_stats_offset;
|
| 63 | size_t num_records;
|
| 64 | bool appended_stats_avail;
|
| 65 | bool dynamic_offset;
|
| 66 | bool subsystem_stats_in_smem;
|
| 67 | };
|
| 68 |
|
| 69 | struct ddr_stats_entry {
|
| 70 | u32 name;
|
| 71 | u32 count;
|
| 72 | u64 duration;
|
| 73 | };
|
| 74 |
|
| 75 | struct stats_data {
|
| 76 | bool appended_stats_avail;
|
| 77 | void __iomem *base;
|
| 78 | };
|
| 79 |
|
| 80 | struct sleep_stats {
|
| 81 | u32 stat_type;
|
| 82 | u32 count;
|
| 83 | u64 last_entered_at;
|
| 84 | u64 last_exited_at;
|
| 85 | u64 accumulated;
|
| 86 | };
|
| 87 |
|
| 88 | struct appended_stats {
|
| 89 | u32 client_votes;
|
| 90 | u32 reserved[3];
|
| 91 | };
|
| 92 |
|
| 93 | static void qcom_print_stats(struct seq_file *s, const struct sleep_stats *stat)
|
| 94 | {
|
| 95 | u64 accumulated = stat->accumulated;
|
| 96 | |
| 97 | * If a subsystem is in sleep when reading the sleep stats adjust
|
| 98 | * the accumulated sleep duration to show actual sleep time.
|
| 99 |
|
| 100 | if (stat->last_entered_at > stat->last_exited_at)
|
| 101 | accumulated += arch_timer_read_counter() - stat->last_entered_at;
|
| 102 |
|
| 103 | seq_printf(s, "Count: %u\n", stat->count);
|
| 104 | seq_printf(s, "Last Entered At: %llu\n", stat->last_entered_at);
|
| 105 | seq_printf(s, "Last Exited At: %llu\n", stat->last_exited_at);
|
| 106 | seq_printf(s, "Accumulated Duration: %llu\n", accumulated);
|
| 107 | }
|
| 108 |
|
| 109 | static int qcom_subsystem_sleep_stats_show(struct seq_file *s, void *unused)
|
| 110 | {
|
| 111 | struct subsystem_data *subsystem = s->private;
|
| 112 | struct sleep_stats *stat;
|
| 113 |
|
| 114 |
|
| 115 | stat = qcom_smem_get(subsystem->pid, subsystem->smem_item, NULL);
|
| 116 | if (IS_ERR(stat))
|
| 117 | return 0;
|
| 118 |
|
| 119 | qcom_print_stats(s, stat);
|
| 120 |
|
| 121 | return 0;
|
| 122 | }
|
| 123 |
|
| 124 | static int qcom_soc_sleep_stats_show(struct seq_file *s, void *unused)
|
| 125 | {
|
| 126 | struct stats_data *d = s->private;
|
| 127 | void __iomem *reg = d->base;
|
| 128 | struct sleep_stats stat;
|
| 129 |
|
| 130 | memcpy_fromio(&stat, reg, sizeof(stat));
|
| 131 | qcom_print_stats(s, &stat);
|
| 132 |
|
| 133 | if (d->appended_stats_avail) {
|
| 134 | struct appended_stats votes;
|
| 135 |
|
| 136 | memcpy_fromio(&votes, reg + sizeof(struct sleep_stats), sizeof(votes));
|
| 137 | seq_printf(s, "Client Votes: %#x\n", votes.client_votes);
|
| 138 | }
|
| 139 |
|
| 140 | return 0;
|
| 141 | }
|
| 142 |
|
| 143 | static void qcom_ddr_stats_print(struct seq_file *s, struct ddr_stats_entry *data)
|
| 144 | {
|
| 145 | u32 cp_idx;
|
| 146 |
|
| 147 | |
| 148 | * DDR statistic have two different types of details encoded.
|
| 149 | * (1) DDR LPM Stats
|
| 150 | * (2) DDR Frequency Stats
|
| 151 | *
|
| 152 | * The name field have details like which type of DDR stat (bits 8:15)
|
| 153 | * along with other details as explained below
|
| 154 | *
|
| 155 | * In case of DDR LPM stat, name field will be encoded as,
|
| 156 | * Bits - Meaning
|
| 157 | * 0:7 - DDR LPM name, can be of 0xd4, 0xd3, 0x11 and 0xd0.
|
| 158 | * 8:15 - 0x0 (indicates its a LPM stat)
|
| 159 | * 16:31 - Unused
|
| 160 | *
|
| 161 | * In case of DDR FREQ stats, name field will be encoded as,
|
| 162 | * Bits - Meaning
|
| 163 | * 0:4 - DDR Clock plan index (CP IDX)
|
| 164 | * 5:7 - Unused
|
| 165 | * 8:15 - 0x1 (indicates its Freq stat)
|
| 166 | * 16:31 - Frequency value in Mhz
|
| 167 |
|
| 168 | switch (DDR_STATS_TYPE(data->name)) {
|
| 169 | case 0:
|
| 170 | seq_printf(s, "DDR LPM Stat Name:0x%lx\tcount:%u\tDuration (ticks):%llu\n",
|
| 171 | DDR_STATS_LPM_NAME(data->name), data->count, data->duration);
|
| 172 | break;
|
| 173 | case 1:
|
| 174 | if (!data->count || !DDR_STATS_FREQ(data->name))
|
| 175 | return;
|
| 176 |
|
| 177 | cp_idx = DDR_STATS_CP_IDX(data->name);
|
| 178 | seq_printf(s, "DDR Freq %luMhz:\tCP IDX:%u\tcount:%u\tDuration (ticks):%llu\n",
|
| 179 | DDR_STATS_FREQ(data->name), cp_idx, data->count, data->duration);
|
| 180 | break;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | static int qcom_ddr_stats_show(struct seq_file *s, void *d)
|
| 185 | {
|
| 186 | struct ddr_stats_entry data[DDR_STATS_MAX_NUM_MODES];
|
| 187 | void __iomem *reg = (void __iomem *)s->private;
|
| 188 | u32 entry_count;
|
| 189 | int i, ret;
|
| 190 |
|
| 191 | entry_count = readl_relaxed(reg + DDR_STATS_NUM_MODES_ADDR);
|
| 192 | if (entry_count > DDR_STATS_MAX_NUM_MODES)
|
| 193 | return -EINVAL;
|
| 194 |
|
| 195 | if (qcom_stats_qmp) {
|
| 196 | |
| 197 | * Recent SoCs (SM8450 onwards) do not have duration field
|
| 198 | * populated from boot up onwards for both DDR LPM Stats
|
| 199 | * and DDR Frequency Stats.
|
| 200 | *
|
| 201 | * Send QMP message to Always on processor which will
|
| 202 | * populate duration field into MSG RAM area.
|
| 203 | *
|
| 204 | * Sent every time to read latest data.
|
| 205 |
|
| 206 | ret = qmp_send(qcom_stats_qmp, "{class: ddr, action: freqsync}");
|
| 207 | if (ret)
|
| 208 | return ret;
|
| 209 | }
|
| 210 |
|
| 211 | reg += DDR_STATS_ENTRY_START_ADDR;
|
| 212 | memcpy_fromio(data, reg, sizeof(struct ddr_stats_entry) * entry_count);
|
| 213 |
|
| 214 | for (i = 0; i < entry_count; i++)
|
| 215 | qcom_ddr_stats_print(s, &data[i]);
|
| 216 |
|
| 217 | return 0;
|
| 218 | }
|
| 219 |
|
| 220 | DEFINE_SHOW_ATTRIBUTE(qcom_soc_sleep_stats);
|
| 221 | DEFINE_SHOW_ATTRIBUTE(qcom_subsystem_sleep_stats);
|
| 222 | DEFINE_SHOW_ATTRIBUTE(qcom_ddr_stats);
|
| 223 |
|
| 224 | static void qcom_create_ddr_stat_files(struct dentry *root, void __iomem *reg,
|
| 225 | const struct stats_config *config)
|
| 226 | {
|
| 227 | u32 key;
|
| 228 |
|
| 229 | if (!config->ddr_stats_offset)
|
| 230 | return;
|
| 231 |
|
| 232 | key = readl_relaxed(reg + config->ddr_stats_offset + DDR_STATS_MAGIC_KEY_ADDR);
|
| 233 | if (key == DDR_STATS_MAGIC_KEY)
|
| 234 | debugfs_create_file("ddr_stats", 0400, root,
|
| 235 | (__force void *)reg + config->ddr_stats_offset,
|
| 236 | &qcom_ddr_stats_fops);
|
| 237 | }
|
| 238 |
|
| 239 | static void qcom_create_soc_sleep_stat_files(struct dentry *root, void __iomem *reg,
|
| 240 | struct stats_data *d,
|
| 241 | const struct stats_config *config)
|
| 242 | {
|
| 243 | char stat_type[sizeof(u32) + 1] = {0};
|
| 244 | size_t stats_offset = config->stats_offset;
|
| 245 | u32 offset = 0, type;
|
| 246 | int i, j;
|
| 247 |
|
| 248 | |
| 249 | * On RPM targets, stats offset location is dynamic and changes from target
|
| 250 | * to target and sometimes from build to build for same target.
|
| 251 | *
|
| 252 | * In such cases the dynamic address is present at 0x14 offset from base
|
| 253 | * address in devicetree. The last 16bits indicates the stats_offset.
|
| 254 |
|
| 255 | if (config->dynamic_offset) {
|
| 256 | stats_offset = readl(reg + RPM_DYNAMIC_ADDR);
|
| 257 | stats_offset &= RPM_DYNAMIC_ADDR_MASK;
|
| 258 | }
|
| 259 |
|
| 260 | for (i = 0; i < config->num_records; i++) {
|
| 261 | d[i].base = reg + offset + stats_offset;
|
| 262 |
|
| 263 | |
| 264 | * Read the low power mode name and create debugfs file for it.
|
| 265 | * The names read could be of below,
|
| 266 | * (may change depending on low power mode supported).
|
| 267 | * For rpmh-sleep-stats: "aosd", "cxsd" and "ddr".
|
| 268 | * For rpm-sleep-stats: "vmin" and "vlow".
|
| 269 |
|
| 270 | type = readl(d[i].base);
|
| 271 | for (j = 0; j < sizeof(u32); j++) {
|
| 272 | stat_type[j] = type & 0xff;
|
| 273 | type = type >> 8;
|
| 274 | }
|
| 275 | strim(stat_type);
|
| 276 | debugfs_create_file(stat_type, 0400, root, &d[i],
|
| 277 | &qcom_soc_sleep_stats_fops);
|
| 278 |
|
| 279 | offset += sizeof(struct sleep_stats);
|
| 280 | if (d[i].appended_stats_avail)
|
| 281 | offset += sizeof(struct appended_stats);
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | static void qcom_create_subsystem_stat_files(struct dentry *root,
|
| 286 | const struct stats_config *config)
|
| 287 | {
|
| 288 | int i;
|
| 289 |
|
| 290 | if (!config->subsystem_stats_in_smem)
|
| 291 | return;
|
| 292 |
|
| 293 | for (i = 0; i < ARRAY_SIZE(subsystems); i++)
|
| 294 | debugfs_create_file(subsystems[i].name, 0400, root, (void *)&subsystems[i],
|
| 295 | &qcom_subsystem_sleep_stats_fops);
|
| 296 | }
|
| 297 |
|
| 298 | static int qcom_stats_probe(struct platform_device *pdev)
|
| 299 | {
|
| 300 | void __iomem *reg;
|
| 301 | struct dentry *root;
|
| 302 | const struct stats_config *config;
|
| 303 | struct stats_data *d;
|
| 304 | int i;
|
| 305 |
|
| 306 | config = device_get_match_data(&pdev->dev);
|
| 307 | if (!config)
|
| 308 | return -ENODEV;
|
| 309 |
|
| 310 | reg = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
|
| 311 | if (IS_ERR(reg))
|
| 312 | return -ENOMEM;
|
| 313 |
|
| 314 | d = devm_kcalloc(&pdev->dev, config->num_records,
|
| 315 | sizeof(*d), GFP_KERNEL);
|
| 316 | if (!d)
|
| 317 | return -ENOMEM;
|
| 318 |
|
| 319 | for (i = 0; i < config->num_records; i++)
|
| 320 | d[i].appended_stats_avail = config->appended_stats_avail;
|
| 321 | |
| 322 | * QMP is used for DDR stats syncing to MSG RAM for recent SoCs (SM8450 onwards).
|
| 323 | * The prior SoCs do not need QMP handle as the required stats are already present
|
| 324 | * in MSG RAM, provided the DDR_STATS_MAGIC_KEY matches.
|
| 325 |
|
| 326 | qcom_stats_qmp = qmp_get(&pdev->dev);
|
| 327 | if (IS_ERR(qcom_stats_qmp)) {
|
| 328 |
|
| 329 | if (!of_property_present(pdev->dev.of_node, "qcom,qmp"))
|
| 330 | qcom_stats_qmp = NULL;
|
| 331 | else if (PTR_ERR(qcom_stats_qmp) == -EPROBE_DEFER)
|
| 332 | return -EPROBE_DEFER;
|
| 333 | else
|
| 334 | return PTR_ERR(qcom_stats_qmp);
|
| 335 | }
|
| 336 |
|
| 337 | root = debugfs_create_dir("qcom_stats", NULL);
|
| 338 |
|
| 339 | qcom_create_subsystem_stat_files(root, config);
|
| 340 | qcom_create_soc_sleep_stat_files(root, reg, d, config);
|
| 341 | qcom_create_ddr_stat_files(root, reg, config);
|
| 342 |
|
| 343 | platform_set_drvdata(pdev, root);
|
| 344 |
|
| 345 | device_set_pm_not_required(&pdev->dev);
|
| 346 |
|
| 347 | return 0;
|
| 348 | }
|
| 349 |
|
| 350 | static void qcom_stats_remove(struct platform_device *pdev)
|
| 351 | {
|
| 352 | struct dentry *root = platform_get_drvdata(pdev);
|
| 353 |
|
| 354 | debugfs_remove_recursive(root);
|
| 355 | }
|
| 356 |
|
| 357 | static const struct stats_config rpm_data = {
|
| 358 | .stats_offset = 0,
|
| 359 | .num_records = 2,
|
| 360 | .appended_stats_avail = true,
|
| 361 | .dynamic_offset = true,
|
| 362 | .subsystem_stats_in_smem = false,
|
| 363 | };
|
| 364 |
|
| 365 |
|
| 366 | static const struct stats_config rpm_data_dba0 = {
|
| 367 | .stats_offset = 0xdba0,
|
| 368 | .num_records = 2,
|
| 369 | .appended_stats_avail = true,
|
| 370 | .dynamic_offset = false,
|
| 371 | .subsystem_stats_in_smem = false,
|
| 372 | };
|
| 373 |
|
| 374 | static const struct stats_config rpmh_data_sdm845 = {
|
| 375 | .stats_offset = 0x48,
|
| 376 | .num_records = 2,
|
| 377 | .appended_stats_avail = false,
|
| 378 | .dynamic_offset = false,
|
| 379 | .subsystem_stats_in_smem = true,
|
| 380 | };
|
| 381 |
|
| 382 | static const struct stats_config rpmh_data = {
|
| 383 | .stats_offset = 0x48,
|
| 384 | .ddr_stats_offset = 0xb8,
|
| 385 | .num_records = 3,
|
| 386 | .appended_stats_avail = false,
|
| 387 | .dynamic_offset = false,
|
| 388 | .subsystem_stats_in_smem = true,
|
| 389 | };
|
| 390 |
|
| 391 | static const struct of_device_id qcom_stats_table[] = {
|
| 392 | { .compatible = "qcom,apq8084-rpm-stats", .data = &rpm_data_dba0 },
|
| 393 | { .compatible = "qcom,msm8226-rpm-stats", .data = &rpm_data_dba0 },
|
| 394 | { .compatible = "qcom,msm8916-rpm-stats", .data = &rpm_data_dba0 },
|
| 395 | { .compatible = "qcom,msm8974-rpm-stats", .data = &rpm_data_dba0 },
|
| 396 | { .compatible = "qcom,rpm-stats", .data = &rpm_data },
|
| 397 | { .compatible = "qcom,rpmh-stats", .data = &rpmh_data },
|
| 398 | { .compatible = "qcom,sdm845-rpmh-stats", .data = &rpmh_data_sdm845 },
|
| 399 | { }
|
| 400 | };
|
| 401 | MODULE_DEVICE_TABLE(of, qcom_stats_table);
|
| 402 |
|
| 403 | static struct platform_driver qcom_stats = {
|
| 404 | .probe = qcom_stats_probe,
|
| 405 | .remove = qcom_stats_remove,
|
| 406 | .driver = {
|
| 407 | .name = "qcom_stats",
|
| 408 | .of_match_table = qcom_stats_table,
|
| 409 | },
|
| 410 | };
|
| 411 |
|
| 412 | static int __init qcom_stats_init(void)
|
| 413 | {
|
| 414 | return platform_driver_register(&qcom_stats);
|
| 415 | }
|
| 416 | late_initcall(qcom_stats_init);
|
| 417 |
|
| 418 | static void __exit qcom_stats_exit(void)
|
| 419 | {
|
| 420 | platform_driver_unregister(&qcom_stats);
|
| 421 | }
|
| 422 | module_exit(qcom_stats_exit)
|
| 423 |
|
| 424 | MODULE_DESCRIPTION("Qualcomm Technologies, Inc. (QTI) Stats driver");
|
| 425 | MODULE_LICENSE("GPL v2");
|