rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * RAM Oops/Panic logger |
| 3 | * |
| 4 | * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com> |
| 5 | * Copyright (C) 2011 Kees Cook <keescook@chromium.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/version.h> |
| 29 | #include <linux/pstore.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/ioport.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/compiler.h> |
| 35 | #include <linux/pstore_ram.h> |
| 36 | #include <linux/of.h> |
| 37 | #include <linux/of_address.h> |
| 38 | |
| 39 | #define RAMOOPS_KERNMSG_HDR "====" |
| 40 | #define MIN_MEM_SIZE 4096UL |
| 41 | #ifdef __aarch64__ |
| 42 | #ifdef memcpy |
| 43 | #undef memcpy |
| 44 | #endif |
| 45 | #define memcpy memcpy_toio |
| 46 | #endif |
| 47 | |
| 48 | static ulong record_size = MIN_MEM_SIZE; |
| 49 | module_param(record_size, ulong, 0400); |
| 50 | MODULE_PARM_DESC(record_size, |
| 51 | "size of each dump done on oops/panic"); |
| 52 | |
| 53 | static ulong ramoops_console_size = MIN_MEM_SIZE; |
| 54 | module_param_named(console_size, ramoops_console_size, ulong, 0400); |
| 55 | MODULE_PARM_DESC(console_size, "size of kernel console log"); |
| 56 | |
| 57 | static ulong ramoops_ftrace_size = MIN_MEM_SIZE; |
| 58 | module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400); |
| 59 | MODULE_PARM_DESC(ftrace_size, "size of ftrace log"); |
| 60 | |
| 61 | static ulong ramoops_pmsg_size = MIN_MEM_SIZE; |
| 62 | module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400); |
| 63 | MODULE_PARM_DESC(pmsg_size, "size of user space message log"); |
| 64 | |
| 65 | static unsigned long long mem_address; |
| 66 | module_param_hw(mem_address, ullong, other, 0400); |
| 67 | MODULE_PARM_DESC(mem_address, |
| 68 | "start of reserved RAM used to store oops/panic logs"); |
| 69 | |
| 70 | static ulong mem_size; |
| 71 | module_param(mem_size, ulong, 0400); |
| 72 | MODULE_PARM_DESC(mem_size, |
| 73 | "size of reserved RAM used to store oops/panic logs"); |
| 74 | |
| 75 | static unsigned int mem_type; |
| 76 | module_param(mem_type, uint, 0600); |
| 77 | MODULE_PARM_DESC(mem_type, |
| 78 | "set to 1 to try to use unbuffered memory (default 0)"); |
| 79 | |
| 80 | static int dump_oops = 1; |
| 81 | module_param(dump_oops, int, 0600); |
| 82 | MODULE_PARM_DESC(dump_oops, |
| 83 | "set to 1 to dump oopses, 0 to only dump panics (default 1)"); |
| 84 | |
| 85 | static int ramoops_ecc; |
| 86 | module_param_named(ecc, ramoops_ecc, int, 0600); |
| 87 | MODULE_PARM_DESC(ramoops_ecc, |
| 88 | "if non-zero, the option enables ECC support and specifies " |
| 89 | "ECC buffer size in bytes (1 is a special value, means 16 " |
| 90 | "bytes ECC)"); |
| 91 | |
| 92 | struct ramoops_context { |
| 93 | struct persistent_ram_zone **dprzs; /* Oops dump zones */ |
| 94 | struct persistent_ram_zone *cprz; /* Console zone */ |
| 95 | struct persistent_ram_zone **fprzs; /* Ftrace zones */ |
| 96 | struct persistent_ram_zone *mprz; /* PMSG zone */ |
| 97 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 98 | struct persistent_ram_zone *bprz; /* AEE lockless console zone */ |
| 99 | #endif |
| 100 | phys_addr_t phys_addr; |
| 101 | unsigned long size; |
| 102 | unsigned int memtype; |
| 103 | size_t record_size; |
| 104 | size_t console_size; |
| 105 | size_t ftrace_size; |
| 106 | size_t pmsg_size; |
| 107 | int dump_oops; |
| 108 | u32 flags; |
| 109 | struct persistent_ram_ecc_info ecc_info; |
| 110 | unsigned int max_dump_cnt; |
| 111 | unsigned int dump_write_cnt; |
| 112 | /* _read_cnt need clear on ramoops_pstore_open */ |
| 113 | unsigned int dump_read_cnt; |
| 114 | unsigned int console_read_cnt; |
| 115 | unsigned int max_ftrace_cnt; |
| 116 | unsigned int ftrace_read_cnt; |
| 117 | unsigned int pmsg_read_cnt; |
| 118 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 119 | unsigned int bconsole_read_cnt; |
| 120 | #endif |
| 121 | struct pstore_info pstore; |
| 122 | }; |
| 123 | |
| 124 | static struct platform_device *dummy; |
| 125 | static struct ramoops_platform_data *dummy_data; |
| 126 | |
| 127 | static int ramoops_pstore_open(struct pstore_info *psi) |
| 128 | { |
| 129 | struct ramoops_context *cxt = psi->data; |
| 130 | |
| 131 | cxt->dump_read_cnt = 0; |
| 132 | cxt->console_read_cnt = 0; |
| 133 | cxt->ftrace_read_cnt = 0; |
| 134 | cxt->pmsg_read_cnt = 0; |
| 135 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 136 | cxt->bconsole_read_cnt = 0; |
| 137 | #endif |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static struct persistent_ram_zone * |
| 142 | ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max, |
| 143 | u64 *id, |
| 144 | enum pstore_type_id *typep, enum pstore_type_id type, |
| 145 | bool update) |
| 146 | { |
| 147 | struct persistent_ram_zone *prz; |
| 148 | int i = (*c)++; |
| 149 | |
| 150 | /* Give up if we never existed or have hit the end. */ |
| 151 | if (!przs || i >= max) |
| 152 | return NULL; |
| 153 | |
| 154 | prz = przs[i]; |
| 155 | if (!prz) |
| 156 | return NULL; |
| 157 | |
| 158 | /* Update old/shadowed buffer. */ |
| 159 | if (update) |
| 160 | persistent_ram_save_old(prz); |
| 161 | |
| 162 | if (!persistent_ram_old_size(prz)) |
| 163 | return NULL; |
| 164 | |
| 165 | *typep = type; |
| 166 | *id = i; |
| 167 | |
| 168 | return prz; |
| 169 | } |
| 170 | |
| 171 | static int ramoops_read_kmsg_hdr(char *buffer, struct timespec *time, |
| 172 | bool *compressed) |
| 173 | { |
| 174 | char data_type; |
| 175 | int header_length = 0; |
| 176 | |
| 177 | if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n%n", &time->tv_sec, |
| 178 | &time->tv_nsec, &data_type, &header_length) == 3) { |
| 179 | if (data_type == 'C') |
| 180 | *compressed = true; |
| 181 | else |
| 182 | *compressed = false; |
| 183 | } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu\n%n", |
| 184 | &time->tv_sec, &time->tv_nsec, &header_length) == 2) { |
| 185 | *compressed = false; |
| 186 | } else { |
| 187 | time->tv_sec = 0; |
| 188 | time->tv_nsec = 0; |
| 189 | *compressed = false; |
| 190 | } |
| 191 | return header_length; |
| 192 | } |
| 193 | |
| 194 | static bool prz_ok(struct persistent_ram_zone *prz) |
| 195 | { |
| 196 | return !!prz && !!(persistent_ram_old_size(prz) + |
| 197 | persistent_ram_ecc_string(prz, NULL, 0)); |
| 198 | } |
| 199 | |
| 200 | static ssize_t ftrace_log_combine(struct persistent_ram_zone *dest, |
| 201 | struct persistent_ram_zone *src) |
| 202 | { |
| 203 | size_t dest_size, src_size, total, dest_off, src_off; |
| 204 | size_t dest_idx = 0, src_idx = 0, merged_idx = 0; |
| 205 | void *merged_buf; |
| 206 | struct pstore_ftrace_record *drec, *srec, *mrec; |
| 207 | size_t record_size = sizeof(struct pstore_ftrace_record); |
| 208 | |
| 209 | dest_off = dest->old_log_size % record_size; |
| 210 | dest_size = dest->old_log_size - dest_off; |
| 211 | |
| 212 | src_off = src->old_log_size % record_size; |
| 213 | src_size = src->old_log_size - src_off; |
| 214 | |
| 215 | total = dest_size + src_size; |
| 216 | merged_buf = kmalloc(total, GFP_KERNEL); |
| 217 | if (!merged_buf) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | drec = (struct pstore_ftrace_record *)(dest->old_log + dest_off); |
| 221 | srec = (struct pstore_ftrace_record *)(src->old_log + src_off); |
| 222 | mrec = (struct pstore_ftrace_record *)(merged_buf); |
| 223 | |
| 224 | while (dest_size > 0 && src_size > 0) { |
| 225 | if (pstore_ftrace_read_timestamp(&drec[dest_idx]) < |
| 226 | pstore_ftrace_read_timestamp(&srec[src_idx])) { |
| 227 | mrec[merged_idx++] = drec[dest_idx++]; |
| 228 | dest_size -= record_size; |
| 229 | } else { |
| 230 | mrec[merged_idx++] = srec[src_idx++]; |
| 231 | src_size -= record_size; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | while (dest_size > 0) { |
| 236 | mrec[merged_idx++] = drec[dest_idx++]; |
| 237 | dest_size -= record_size; |
| 238 | } |
| 239 | |
| 240 | while (src_size > 0) { |
| 241 | mrec[merged_idx++] = srec[src_idx++]; |
| 242 | src_size -= record_size; |
| 243 | } |
| 244 | |
| 245 | kfree(dest->old_log); |
| 246 | dest->old_log = merged_buf; |
| 247 | dest->old_log_size = total; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static ssize_t ramoops_pstore_read(struct pstore_record *record) |
| 253 | { |
| 254 | ssize_t size = 0; |
| 255 | struct ramoops_context *cxt = record->psi->data; |
| 256 | struct persistent_ram_zone *prz = NULL; |
| 257 | int header_length = 0; |
| 258 | bool free_prz = false; |
| 259 | |
| 260 | /* |
| 261 | * Ramoops headers provide time stamps for PSTORE_TYPE_DMESG, but |
| 262 | * PSTORE_TYPE_CONSOLE and PSTORE_TYPE_FTRACE don't currently have |
| 263 | * valid time stamps, so it is initialized to zero. |
| 264 | */ |
| 265 | record->time.tv_sec = 0; |
| 266 | record->time.tv_nsec = 0; |
| 267 | record->compressed = false; |
| 268 | |
| 269 | /* Find the next valid persistent_ram_zone for DMESG */ |
| 270 | while (cxt->dump_read_cnt < cxt->max_dump_cnt && !prz) { |
| 271 | prz = ramoops_get_next_prz(cxt->dprzs, &cxt->dump_read_cnt, |
| 272 | cxt->max_dump_cnt, &record->id, |
| 273 | &record->type, |
| 274 | PSTORE_TYPE_DMESG, 1); |
| 275 | if (!prz_ok(prz)) |
| 276 | continue; |
| 277 | header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz), |
| 278 | &record->time, |
| 279 | &record->compressed); |
| 280 | /* Clear and skip this DMESG record if it has no valid header */ |
| 281 | if (!header_length) { |
| 282 | persistent_ram_free_old(prz); |
| 283 | persistent_ram_zap(prz); |
| 284 | prz = NULL; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (!prz_ok(prz)) |
| 289 | prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt, |
| 290 | 1, &record->id, &record->type, |
| 291 | PSTORE_TYPE_CONSOLE, 0); |
| 292 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 293 | if (!prz_ok(prz)) { |
| 294 | prz = ramoops_get_next_prz(&cxt->bprz, &cxt->bconsole_read_cnt, |
| 295 | 1, &record->id, &record->type, |
| 296 | PSTORE_TYPE_CONSOLE, 0); |
| 297 | record->id = 2; |
| 298 | } |
| 299 | #endif |
| 300 | if (!prz_ok(prz)) |
| 301 | prz = ramoops_get_next_prz(&cxt->mprz, &cxt->pmsg_read_cnt, |
| 302 | 1, &record->id, &record->type, |
| 303 | PSTORE_TYPE_PMSG, 0); |
| 304 | |
| 305 | /* ftrace is last since it may want to dynamically allocate memory. */ |
| 306 | if (!prz_ok(prz)) { |
| 307 | if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU)) { |
| 308 | prz = ramoops_get_next_prz(cxt->fprzs, |
| 309 | &cxt->ftrace_read_cnt, 1, &record->id, |
| 310 | &record->type, PSTORE_TYPE_FTRACE, 0); |
| 311 | } else { |
| 312 | /* |
| 313 | * Build a new dummy record which combines all the |
| 314 | * per-cpu records including metadata and ecc info. |
| 315 | */ |
| 316 | struct persistent_ram_zone *tmp_prz, *prz_next; |
| 317 | |
| 318 | tmp_prz = kzalloc(sizeof(struct persistent_ram_zone), |
| 319 | GFP_KERNEL); |
| 320 | if (!tmp_prz) |
| 321 | return -ENOMEM; |
| 322 | prz = tmp_prz; |
| 323 | free_prz = true; |
| 324 | |
| 325 | while (cxt->ftrace_read_cnt < cxt->max_ftrace_cnt) { |
| 326 | prz_next = ramoops_get_next_prz(cxt->fprzs, |
| 327 | &cxt->ftrace_read_cnt, |
| 328 | cxt->max_ftrace_cnt, |
| 329 | &record->id, |
| 330 | &record->type, |
| 331 | PSTORE_TYPE_FTRACE, 0); |
| 332 | |
| 333 | if (!prz_ok(prz_next)) |
| 334 | continue; |
| 335 | |
| 336 | tmp_prz->ecc_info = prz_next->ecc_info; |
| 337 | tmp_prz->corrected_bytes += |
| 338 | prz_next->corrected_bytes; |
| 339 | tmp_prz->bad_blocks += prz_next->bad_blocks; |
| 340 | size = ftrace_log_combine(tmp_prz, prz_next); |
| 341 | if (size) |
| 342 | goto out; |
| 343 | } |
| 344 | record->id = 0; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (!prz_ok(prz)) { |
| 349 | size = 0; |
| 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | size = persistent_ram_old_size(prz) - header_length; |
| 354 | |
| 355 | /* ECC correction notice */ |
| 356 | record->ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0); |
| 357 | |
| 358 | record->buf = kmalloc(size + record->ecc_notice_size + 1, GFP_KERNEL); |
| 359 | if (record->buf == NULL) { |
| 360 | size = -ENOMEM; |
| 361 | goto out; |
| 362 | } |
| 363 | |
| 364 | memcpy(record->buf, (char *)persistent_ram_old(prz) + header_length, |
| 365 | size); |
| 366 | |
| 367 | persistent_ram_ecc_string(prz, record->buf + size, |
| 368 | record->ecc_notice_size + 1); |
| 369 | |
| 370 | out: |
| 371 | if (free_prz) { |
| 372 | kfree(prz->old_log); |
| 373 | kfree(prz); |
| 374 | } |
| 375 | |
| 376 | return size; |
| 377 | } |
| 378 | |
| 379 | static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz, |
| 380 | struct pstore_record *record) |
| 381 | { |
| 382 | char *hdr; |
| 383 | size_t len; |
| 384 | |
| 385 | hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n", |
| 386 | record->time.tv_sec, |
| 387 | record->time.tv_nsec / 1000, |
| 388 | record->compressed ? 'C' : 'D'); |
| 389 | WARN_ON_ONCE(!hdr); |
| 390 | len = hdr ? strlen(hdr) : 0; |
| 391 | persistent_ram_write(prz, hdr, len); |
| 392 | kfree(hdr); |
| 393 | |
| 394 | return len; |
| 395 | } |
| 396 | |
| 397 | static int notrace ramoops_pstore_write(struct pstore_record *record) |
| 398 | { |
| 399 | struct ramoops_context *cxt = record->psi->data; |
| 400 | struct persistent_ram_zone *prz; |
| 401 | size_t size, hlen; |
| 402 | |
| 403 | if (record->type == PSTORE_TYPE_CONSOLE) { |
| 404 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 405 | if (record->reason == 0) { |
| 406 | if (!cxt->cprz) |
| 407 | return -ENOMEM; |
| 408 | persistent_ram_write(cxt->cprz, record->buf, |
| 409 | record->size); |
| 410 | } else { |
| 411 | if (!cxt->bprz) |
| 412 | return -ENOMEM; |
| 413 | persistent_ram_write(cxt->bprz, record->buf, |
| 414 | record->size); |
| 415 | } |
| 416 | #else |
| 417 | if (!cxt->cprz) |
| 418 | return -ENOMEM; |
| 419 | persistent_ram_write(cxt->cprz, record->buf, record->size); |
| 420 | #endif |
| 421 | return 0; |
| 422 | } else if (record->type == PSTORE_TYPE_FTRACE) { |
| 423 | int zonenum; |
| 424 | |
| 425 | if (!cxt->fprzs) |
| 426 | return -ENOMEM; |
| 427 | /* |
| 428 | * Choose zone by if we're using per-cpu buffers. |
| 429 | */ |
| 430 | if (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) |
| 431 | zonenum = smp_processor_id(); |
| 432 | else |
| 433 | zonenum = 0; |
| 434 | |
| 435 | persistent_ram_write(cxt->fprzs[zonenum], record->buf, |
| 436 | record->size); |
| 437 | return 0; |
| 438 | } else if (record->type == PSTORE_TYPE_PMSG) { |
| 439 | pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__); |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | if (record->type != PSTORE_TYPE_DMESG) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | /* |
| 447 | * Out of the various dmesg dump types, ramoops is currently designed |
| 448 | * to only store crash logs, rather than storing general kernel logs. |
| 449 | */ |
| 450 | if (record->reason != KMSG_DUMP_OOPS && |
| 451 | record->reason != KMSG_DUMP_PANIC) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | /* Skip Oopes when configured to do so. */ |
| 455 | if (record->reason == KMSG_DUMP_OOPS && !cxt->dump_oops) |
| 456 | return -EINVAL; |
| 457 | |
| 458 | /* |
| 459 | * Explicitly only take the first part of any new crash. |
| 460 | * If our buffer is larger than kmsg_bytes, this can never happen, |
| 461 | * and if our buffer is smaller than kmsg_bytes, we don't want the |
| 462 | * report split across multiple records. |
| 463 | */ |
| 464 | if (record->part != 1) |
| 465 | return -ENOSPC; |
| 466 | |
| 467 | if (!cxt->dprzs) |
| 468 | return -ENOSPC; |
| 469 | |
| 470 | prz = cxt->dprzs[cxt->dump_write_cnt]; |
| 471 | |
| 472 | /* |
| 473 | * Since this is a new crash dump, we need to reset the buffer in |
| 474 | * case it still has an old dump present. Without this, the new dump |
| 475 | * will get appended, which would seriously confuse anything trying |
| 476 | * to check dump file contents. Specifically, ramoops_read_kmsg_hdr() |
| 477 | * expects to find a dump header in the beginning of buffer data, so |
| 478 | * we must to reset the buffer values, in order to ensure that the |
| 479 | * header will be written to the beginning of the buffer. |
| 480 | */ |
| 481 | persistent_ram_zap(prz); |
| 482 | |
| 483 | /* Build header and append record contents. */ |
| 484 | hlen = ramoops_write_kmsg_hdr(prz, record); |
| 485 | size = record->size; |
| 486 | if (size + hlen > prz->buffer_size) |
| 487 | size = prz->buffer_size - hlen; |
| 488 | persistent_ram_write(prz, record->buf, size); |
| 489 | |
| 490 | cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int notrace ramoops_pstore_write_user(struct pstore_record *record, |
| 496 | const char __user *buf) |
| 497 | { |
| 498 | if (record->type == PSTORE_TYPE_PMSG) { |
| 499 | struct ramoops_context *cxt = record->psi->data; |
| 500 | |
| 501 | if (!cxt->mprz) |
| 502 | return -ENOMEM; |
| 503 | return persistent_ram_write_user(cxt->mprz, buf, record->size); |
| 504 | } |
| 505 | |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | static int ramoops_pstore_erase(struct pstore_record *record) |
| 510 | { |
| 511 | struct ramoops_context *cxt = record->psi->data; |
| 512 | struct persistent_ram_zone *prz; |
| 513 | |
| 514 | switch (record->type) { |
| 515 | case PSTORE_TYPE_DMESG: |
| 516 | if (record->id >= cxt->max_dump_cnt) |
| 517 | return -EINVAL; |
| 518 | prz = cxt->dprzs[record->id]; |
| 519 | break; |
| 520 | case PSTORE_TYPE_CONSOLE: |
| 521 | prz = cxt->cprz; |
| 522 | break; |
| 523 | case PSTORE_TYPE_FTRACE: |
| 524 | if (record->id >= cxt->max_ftrace_cnt) |
| 525 | return -EINVAL; |
| 526 | prz = cxt->fprzs[record->id]; |
| 527 | break; |
| 528 | case PSTORE_TYPE_PMSG: |
| 529 | prz = cxt->mprz; |
| 530 | break; |
| 531 | default: |
| 532 | return -EINVAL; |
| 533 | } |
| 534 | |
| 535 | persistent_ram_free_old(prz); |
| 536 | persistent_ram_zap(prz); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static struct ramoops_context oops_cxt = { |
| 542 | .pstore = { |
| 543 | .owner = THIS_MODULE, |
| 544 | .name = "ramoops", |
| 545 | .open = ramoops_pstore_open, |
| 546 | .read = ramoops_pstore_read, |
| 547 | .write = ramoops_pstore_write, |
| 548 | .write_user = ramoops_pstore_write_user, |
| 549 | .erase = ramoops_pstore_erase, |
| 550 | }, |
| 551 | }; |
| 552 | |
| 553 | static void ramoops_free_przs(struct ramoops_context *cxt) |
| 554 | { |
| 555 | int i; |
| 556 | |
| 557 | /* Free dump PRZs */ |
| 558 | if (cxt->dprzs) { |
| 559 | for (i = 0; i < cxt->max_dump_cnt; i++) |
| 560 | persistent_ram_free(cxt->dprzs[i]); |
| 561 | |
| 562 | kfree(cxt->dprzs); |
| 563 | cxt->max_dump_cnt = 0; |
| 564 | } |
| 565 | |
| 566 | /* Free ftrace PRZs */ |
| 567 | if (cxt->fprzs) { |
| 568 | for (i = 0; i < cxt->max_ftrace_cnt; i++) |
| 569 | persistent_ram_free(cxt->fprzs[i]); |
| 570 | kfree(cxt->fprzs); |
| 571 | cxt->max_ftrace_cnt = 0; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | static int ramoops_init_przs(const char *name, |
| 576 | struct device *dev, struct ramoops_context *cxt, |
| 577 | struct persistent_ram_zone ***przs, |
| 578 | phys_addr_t *paddr, size_t mem_sz, |
| 579 | ssize_t record_size, |
| 580 | unsigned int *cnt, u32 sig, u32 flags) |
| 581 | { |
| 582 | int err = -ENOMEM; |
| 583 | int i; |
| 584 | size_t zone_sz; |
| 585 | struct persistent_ram_zone **prz_ar; |
| 586 | |
| 587 | /* Allocate nothing for 0 mem_sz or 0 record_size. */ |
| 588 | if (mem_sz == 0 || record_size == 0) { |
| 589 | *cnt = 0; |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * If we have a negative record size, calculate it based on |
| 595 | * mem_sz / *cnt. If we have a positive record size, calculate |
| 596 | * cnt from mem_sz / record_size. |
| 597 | */ |
| 598 | if (record_size < 0) { |
| 599 | if (*cnt == 0) |
| 600 | return 0; |
| 601 | record_size = mem_sz / *cnt; |
| 602 | if (record_size == 0) { |
| 603 | dev_err(dev, "%s record size == 0 (%zu / %u)\n", |
| 604 | name, mem_sz, *cnt); |
| 605 | goto fail; |
| 606 | } |
| 607 | } else { |
| 608 | *cnt = mem_sz / record_size; |
| 609 | if (*cnt == 0) { |
| 610 | dev_err(dev, "%s record count == 0 (%zu / %zu)\n", |
| 611 | name, mem_sz, record_size); |
| 612 | goto fail; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (*paddr + mem_sz - cxt->phys_addr > cxt->size) { |
| 617 | dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n", |
| 618 | name, |
| 619 | mem_sz, (unsigned long long)*paddr, |
| 620 | cxt->size, (unsigned long long)cxt->phys_addr); |
| 621 | goto fail; |
| 622 | } |
| 623 | |
| 624 | zone_sz = mem_sz / *cnt; |
| 625 | if (!zone_sz) { |
| 626 | dev_err(dev, "%s zone size == 0\n", name); |
| 627 | goto fail; |
| 628 | } |
| 629 | |
| 630 | prz_ar = kcalloc(*cnt, sizeof(**przs), GFP_KERNEL); |
| 631 | if (!prz_ar) |
| 632 | goto fail; |
| 633 | |
| 634 | for (i = 0; i < *cnt; i++) { |
| 635 | prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig, |
| 636 | &cxt->ecc_info, |
| 637 | cxt->memtype, flags); |
| 638 | if (IS_ERR(prz_ar[i])) { |
| 639 | err = PTR_ERR(prz_ar[i]); |
| 640 | dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n", |
| 641 | name, record_size, |
| 642 | (unsigned long long)*paddr, err); |
| 643 | |
| 644 | while (i > 0) { |
| 645 | i--; |
| 646 | persistent_ram_free(prz_ar[i]); |
| 647 | } |
| 648 | kfree(prz_ar); |
| 649 | goto fail; |
| 650 | } |
| 651 | *paddr += zone_sz; |
| 652 | } |
| 653 | |
| 654 | *przs = prz_ar; |
| 655 | return 0; |
| 656 | |
| 657 | fail: |
| 658 | *cnt = 0; |
| 659 | return err; |
| 660 | } |
| 661 | |
| 662 | static int ramoops_init_prz(const char *name, |
| 663 | struct device *dev, struct ramoops_context *cxt, |
| 664 | struct persistent_ram_zone **prz, |
| 665 | phys_addr_t *paddr, size_t sz, u32 sig) |
| 666 | { |
| 667 | if (!sz) |
| 668 | return 0; |
| 669 | |
| 670 | if (*paddr + sz - cxt->phys_addr > cxt->size) { |
| 671 | dev_err(dev, "no room for %s mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n", |
| 672 | name, sz, (unsigned long long)*paddr, |
| 673 | cxt->size, (unsigned long long)cxt->phys_addr); |
| 674 | return -ENOMEM; |
| 675 | } |
| 676 | |
| 677 | *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, |
| 678 | cxt->memtype, 0); |
| 679 | if (IS_ERR(*prz)) { |
| 680 | int err = PTR_ERR(*prz); |
| 681 | |
| 682 | dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n", |
| 683 | name, sz, (unsigned long long)*paddr, err); |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | persistent_ram_zap(*prz); |
| 688 | |
| 689 | *paddr += sz; |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static int ramoops_parse_dt_size(struct platform_device *pdev, |
| 695 | const char *propname, u32 *value) |
| 696 | { |
| 697 | u32 val32 = 0; |
| 698 | int ret; |
| 699 | |
| 700 | ret = of_property_read_u32(pdev->dev.of_node, propname, &val32); |
| 701 | if (ret < 0 && ret != -EINVAL) { |
| 702 | dev_err(&pdev->dev, "failed to parse property %s: %d\n", |
| 703 | propname, ret); |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | if (val32 > INT_MAX) { |
| 708 | dev_err(&pdev->dev, "%s %u > INT_MAX\n", propname, val32); |
| 709 | return -EOVERFLOW; |
| 710 | } |
| 711 | |
| 712 | *value = val32; |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int ramoops_parse_dt(struct platform_device *pdev, |
| 717 | struct ramoops_platform_data *pdata) |
| 718 | { |
| 719 | struct device_node *of_node = pdev->dev.of_node; |
| 720 | struct resource *res; |
| 721 | u32 value; |
| 722 | int ret; |
| 723 | |
| 724 | dev_dbg(&pdev->dev, "using Device Tree\n"); |
| 725 | |
| 726 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 727 | if (!res) { |
| 728 | dev_err(&pdev->dev, |
| 729 | "failed to locate DT /reserved-memory resource\n"); |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
| 733 | pdata->mem_size = resource_size(res); |
| 734 | pdata->mem_address = res->start; |
| 735 | pdata->mem_type = of_property_read_bool(of_node, "unbuffered"); |
| 736 | pdata->dump_oops = !of_property_read_bool(of_node, "no-dump-oops"); |
| 737 | |
| 738 | #define parse_size(name, field) { \ |
| 739 | ret = ramoops_parse_dt_size(pdev, name, &value); \ |
| 740 | if (ret < 0) \ |
| 741 | return ret; \ |
| 742 | field = value; \ |
| 743 | } |
| 744 | |
| 745 | parse_size("record-size", pdata->record_size); |
| 746 | parse_size("console-size", pdata->console_size); |
| 747 | parse_size("ftrace-size", pdata->ftrace_size); |
| 748 | parse_size("pmsg-size", pdata->pmsg_size); |
| 749 | parse_size("ecc-size", pdata->ecc_info.ecc_size); |
| 750 | parse_size("flags", pdata->flags); |
| 751 | |
| 752 | #undef parse_size |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | void notrace ramoops_console_write_buf(const char *buf, size_t size) |
| 758 | { |
| 759 | struct ramoops_context *cxt = &oops_cxt; |
| 760 | persistent_ram_write(cxt->cprz, buf, size); |
| 761 | } |
| 762 | |
| 763 | static int ramoops_probe(struct platform_device *pdev) |
| 764 | { |
| 765 | struct device *dev = &pdev->dev; |
| 766 | struct ramoops_platform_data *pdata = dev->platform_data; |
| 767 | struct ramoops_platform_data pdata_local; |
| 768 | struct ramoops_context *cxt = &oops_cxt; |
| 769 | size_t dump_mem_sz; |
| 770 | phys_addr_t paddr; |
| 771 | int err = -EINVAL; |
| 772 | |
| 773 | if (dev_of_node(dev) && !pdata) { |
| 774 | pdata = &pdata_local; |
| 775 | memset(pdata, 0, sizeof(*pdata)); |
| 776 | |
| 777 | err = ramoops_parse_dt(pdev, pdata); |
| 778 | if (err < 0) |
| 779 | goto fail_out; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Only a single ramoops area allowed at a time, so fail extra |
| 784 | * probes. |
| 785 | */ |
| 786 | if (cxt->max_dump_cnt) { |
| 787 | pr_err("already initialized\n"); |
| 788 | goto fail_out; |
| 789 | } |
| 790 | |
| 791 | /* Make sure we didn't get bogus platform data pointer. */ |
| 792 | if (!pdata) { |
| 793 | pr_err("NULL platform data\n"); |
| 794 | goto fail_out; |
| 795 | } |
| 796 | |
| 797 | if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size && |
| 798 | !pdata->ftrace_size && !pdata->pmsg_size)) { |
| 799 | pr_err("The memory size and the record/console size must be " |
| 800 | "non-zero\n"); |
| 801 | goto fail_out; |
| 802 | } |
| 803 | |
| 804 | if (pdata->record_size && !is_power_of_2(pdata->record_size)) |
| 805 | pdata->record_size = rounddown_pow_of_two(pdata->record_size); |
| 806 | if (pdata->console_size && !is_power_of_2(pdata->console_size)) |
| 807 | pdata->console_size = rounddown_pow_of_two(pdata->console_size); |
| 808 | if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size)) |
| 809 | pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size); |
| 810 | if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size)) |
| 811 | pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size); |
| 812 | |
| 813 | cxt->size = pdata->mem_size; |
| 814 | cxt->phys_addr = pdata->mem_address; |
| 815 | cxt->memtype = pdata->mem_type; |
| 816 | cxt->record_size = pdata->record_size; |
| 817 | cxt->console_size = pdata->console_size; |
| 818 | cxt->ftrace_size = pdata->ftrace_size; |
| 819 | cxt->pmsg_size = pdata->pmsg_size; |
| 820 | cxt->dump_oops = pdata->dump_oops; |
| 821 | cxt->flags = pdata->flags; |
| 822 | cxt->ecc_info = pdata->ecc_info; |
| 823 | |
| 824 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 825 | pr_notice("pstore:address is 0x%lx, size is 0x%lx, console_size is 0x%zx, pmsg_size is 0x%zx\n", |
| 826 | (unsigned long)cxt->phys_addr, cxt->size, |
| 827 | cxt->console_size, cxt->pmsg_size); |
| 828 | #endif |
| 829 | paddr = cxt->phys_addr; |
| 830 | |
| 831 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 832 | dump_mem_sz = cxt->size - cxt->console_size * 2 - cxt->ftrace_size |
| 833 | - cxt->pmsg_size; |
| 834 | #else |
| 835 | dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size |
| 836 | - cxt->pmsg_size; |
| 837 | #endif |
| 838 | err = ramoops_init_przs("dump", dev, cxt, &cxt->dprzs, &paddr, |
| 839 | dump_mem_sz, cxt->record_size, |
| 840 | &cxt->max_dump_cnt, 0, 0); |
| 841 | if (err) |
| 842 | goto fail_out; |
| 843 | |
| 844 | err = ramoops_init_prz("console", dev, cxt, &cxt->cprz, &paddr, |
| 845 | cxt->console_size, 0); |
| 846 | if (err) |
| 847 | goto fail_init_cprz; |
| 848 | |
| 849 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 850 | err = ramoops_init_prz("bconsole", dev, cxt, &cxt->bprz, &paddr, |
| 851 | cxt->console_size, 0); |
| 852 | if (err) |
| 853 | goto fail_init_bprz; |
| 854 | #endif |
| 855 | cxt->max_ftrace_cnt = (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) |
| 856 | ? nr_cpu_ids |
| 857 | : 1; |
| 858 | err = ramoops_init_przs("ftrace", dev, cxt, &cxt->fprzs, &paddr, |
| 859 | cxt->ftrace_size, -1, |
| 860 | &cxt->max_ftrace_cnt, LINUX_VERSION_CODE, |
| 861 | (cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) |
| 862 | ? PRZ_FLAG_NO_LOCK : 0); |
| 863 | if (err) |
| 864 | goto fail_init_fprz; |
| 865 | |
| 866 | err = ramoops_init_prz("pmsg", dev, cxt, &cxt->mprz, &paddr, |
| 867 | cxt->pmsg_size, 0); |
| 868 | if (err) |
| 869 | goto fail_init_mprz; |
| 870 | |
| 871 | cxt->pstore.data = cxt; |
| 872 | /* |
| 873 | * Prepare frontend flags based on which areas are initialized. |
| 874 | * For ramoops_init_przs() cases, the "max count" variable tells |
| 875 | * if there are regions present. For ramoops_init_prz() cases, |
| 876 | * the single region size is how to check. |
| 877 | */ |
| 878 | cxt->pstore.flags = 0; |
| 879 | if (cxt->max_dump_cnt) |
| 880 | cxt->pstore.flags |= PSTORE_FLAGS_DMESG; |
| 881 | if (cxt->console_size) |
| 882 | cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE; |
| 883 | if (cxt->max_ftrace_cnt) |
| 884 | cxt->pstore.flags |= PSTORE_FLAGS_FTRACE; |
| 885 | if (cxt->pmsg_size) |
| 886 | cxt->pstore.flags |= PSTORE_FLAGS_PMSG; |
| 887 | |
| 888 | /* |
| 889 | * Since bufsize is only used for dmesg crash dumps, it |
| 890 | * must match the size of the dprz record (after PRZ header |
| 891 | * and ECC bytes have been accounted for). |
| 892 | */ |
| 893 | if (cxt->pstore.flags & PSTORE_FLAGS_DMESG) { |
| 894 | cxt->pstore.bufsize = cxt->dprzs[0]->buffer_size; |
| 895 | cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL); |
| 896 | if (!cxt->pstore.buf) { |
| 897 | pr_err("cannot allocate pstore crash dump buffer\n"); |
| 898 | err = -ENOMEM; |
| 899 | goto fail_clear; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | err = pstore_register(&cxt->pstore); |
| 904 | if (err) { |
| 905 | pr_err("registering with pstore failed\n"); |
| 906 | goto fail_buf; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Update the module parameter variables as well so they are visible |
| 911 | * through /sys/module/ramoops/parameters/ |
| 912 | */ |
| 913 | mem_size = pdata->mem_size; |
| 914 | mem_address = pdata->mem_address; |
| 915 | record_size = pdata->record_size; |
| 916 | dump_oops = pdata->dump_oops; |
| 917 | ramoops_console_size = pdata->console_size; |
| 918 | ramoops_pmsg_size = pdata->pmsg_size; |
| 919 | ramoops_ftrace_size = pdata->ftrace_size; |
| 920 | |
| 921 | pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n", |
| 922 | cxt->size, (unsigned long long)cxt->phys_addr, |
| 923 | cxt->ecc_info.ecc_size, cxt->ecc_info.block_size); |
| 924 | |
| 925 | return 0; |
| 926 | |
| 927 | fail_buf: |
| 928 | kfree(cxt->pstore.buf); |
| 929 | fail_clear: |
| 930 | cxt->pstore.bufsize = 0; |
| 931 | persistent_ram_free(cxt->mprz); |
| 932 | fail_init_mprz: |
| 933 | fail_init_fprz: |
| 934 | #ifdef CONFIG_MTK_RAM_CONSOLE |
| 935 | persistent_ram_free(cxt->bprz); |
| 936 | fail_init_bprz: |
| 937 | #endif |
| 938 | persistent_ram_free(cxt->cprz); |
| 939 | fail_init_cprz: |
| 940 | ramoops_free_przs(cxt); |
| 941 | fail_out: |
| 942 | return err; |
| 943 | } |
| 944 | |
| 945 | static int ramoops_remove(struct platform_device *pdev) |
| 946 | { |
| 947 | struct ramoops_context *cxt = &oops_cxt; |
| 948 | |
| 949 | pstore_unregister(&cxt->pstore); |
| 950 | |
| 951 | kfree(cxt->pstore.buf); |
| 952 | cxt->pstore.bufsize = 0; |
| 953 | |
| 954 | persistent_ram_free(cxt->mprz); |
| 955 | persistent_ram_free(cxt->cprz); |
| 956 | ramoops_free_przs(cxt); |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static const struct of_device_id dt_match[] = { |
| 962 | { .compatible = "ramoops" }, |
| 963 | {} |
| 964 | }; |
| 965 | |
| 966 | static struct platform_driver ramoops_driver = { |
| 967 | .probe = ramoops_probe, |
| 968 | .remove = ramoops_remove, |
| 969 | .driver = { |
| 970 | .name = "ramoops", |
| 971 | .of_match_table = dt_match, |
| 972 | }, |
| 973 | }; |
| 974 | |
| 975 | static void ramoops_register_dummy(void) |
| 976 | { |
| 977 | if (!mem_size) |
| 978 | return; |
| 979 | |
| 980 | pr_info("using module parameters\n"); |
| 981 | |
| 982 | dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); |
| 983 | if (!dummy_data) { |
| 984 | pr_info("could not allocate pdata\n"); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | dummy_data->mem_size = mem_size; |
| 989 | dummy_data->mem_address = mem_address; |
| 990 | dummy_data->mem_type = mem_type; |
| 991 | dummy_data->record_size = record_size; |
| 992 | dummy_data->console_size = ramoops_console_size; |
| 993 | dummy_data->ftrace_size = ramoops_ftrace_size; |
| 994 | dummy_data->pmsg_size = ramoops_pmsg_size; |
| 995 | dummy_data->dump_oops = dump_oops; |
| 996 | dummy_data->flags = RAMOOPS_FLAG_FTRACE_PER_CPU; |
| 997 | |
| 998 | /* |
| 999 | * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC |
| 1000 | * (using 1 byte for ECC isn't much of use anyway). |
| 1001 | */ |
| 1002 | dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc; |
| 1003 | |
| 1004 | dummy = platform_device_register_data(NULL, "ramoops", -1, |
| 1005 | dummy_data, sizeof(struct ramoops_platform_data)); |
| 1006 | if (IS_ERR(dummy)) { |
| 1007 | pr_info("could not create platform device: %ld\n", |
| 1008 | PTR_ERR(dummy)); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | static int __init ramoops_init(void) |
| 1013 | { |
| 1014 | ramoops_register_dummy(); |
| 1015 | return platform_driver_register(&ramoops_driver); |
| 1016 | } |
| 1017 | postcore_initcall(ramoops_init); |
| 1018 | |
| 1019 | static void __exit ramoops_exit(void) |
| 1020 | { |
| 1021 | platform_driver_unregister(&ramoops_driver); |
| 1022 | platform_device_unregister(dummy); |
| 1023 | kfree(dummy_data); |
| 1024 | } |
| 1025 | module_exit(ramoops_exit); |
| 1026 | |
| 1027 | MODULE_LICENSE("GPL"); |
| 1028 | MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>"); |
| 1029 | MODULE_DESCRIPTION("RAM Oops/Panic logger/driver"); |