| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2012 Google, Inc. |
|---|
| 3 | | - * |
|---|
| 4 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 5 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 6 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | | - * GNU General Public License for more details. |
|---|
| 12 | | - * |
|---|
| 13 | 4 | */ |
|---|
| 14 | 5 | |
|---|
| 15 | | -#define pr_fmt(fmt) "persistent_ram: " fmt |
|---|
| 6 | +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| 16 | 7 | |
|---|
| 17 | 8 | #include <linux/device.h> |
|---|
| 18 | 9 | #include <linux/err.h> |
|---|
| .. | .. |
|---|
| 28 | 19 | #include <linux/uaccess.h> |
|---|
| 29 | 20 | #include <linux/vmalloc.h> |
|---|
| 30 | 21 | #include <asm/page.h> |
|---|
| 22 | + |
|---|
| 23 | +/** |
|---|
| 24 | + * struct persistent_ram_buffer - persistent circular RAM buffer |
|---|
| 25 | + * |
|---|
| 26 | + * @sig: |
|---|
| 27 | + * signature to indicate header (PERSISTENT_RAM_SIG xor PRZ-type value) |
|---|
| 28 | + * @start: |
|---|
| 29 | + * offset into @data where the beginning of the stored bytes begin |
|---|
| 30 | + * @size: |
|---|
| 31 | + * number of valid bytes stored in @data |
|---|
| 32 | + */ |
|---|
| 33 | +struct persistent_ram_buffer { |
|---|
| 34 | + uint32_t sig; |
|---|
| 35 | + atomic_t start; |
|---|
| 36 | + atomic_t size; |
|---|
| 37 | + uint8_t data[]; |
|---|
| 38 | +}; |
|---|
| 31 | 39 | |
|---|
| 32 | 40 | #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */ |
|---|
| 33 | 41 | |
|---|
| .. | .. |
|---|
| 275 | 283 | const void __user *s, unsigned int start, unsigned int count) |
|---|
| 276 | 284 | { |
|---|
| 277 | 285 | struct persistent_ram_buffer *buffer = prz->buffer; |
|---|
| 278 | | - int ret = unlikely(__copy_from_user(buffer->data + start, s, count)) ? |
|---|
| 286 | + int ret = unlikely(copy_from_user(buffer->data + start, s, count)) ? |
|---|
| 279 | 287 | -EFAULT : 0; |
|---|
| 280 | 288 | persistent_ram_update_ecc(prz, start, count); |
|---|
| 281 | 289 | return ret; |
|---|
| .. | .. |
|---|
| 340 | 348 | int rem, ret = 0, c = count; |
|---|
| 341 | 349 | size_t start; |
|---|
| 342 | 350 | |
|---|
| 343 | | - if (unlikely(!access_ok(VERIFY_READ, s, count))) |
|---|
| 344 | | - return -EFAULT; |
|---|
| 345 | 351 | if (unlikely(c > prz->buffer_size)) { |
|---|
| 346 | 352 | s += c - prz->buffer_size; |
|---|
| 347 | 353 | c = prz->buffer_size; |
|---|
| .. | .. |
|---|
| 390 | 396 | persistent_ram_update_header_ecc(prz); |
|---|
| 391 | 397 | } |
|---|
| 392 | 398 | |
|---|
| 399 | +#define MEM_TYPE_WCOMBINE 0 |
|---|
| 400 | +#define MEM_TYPE_NONCACHED 1 |
|---|
| 401 | +#define MEM_TYPE_NORMAL 2 |
|---|
| 402 | + |
|---|
| 393 | 403 | static void *persistent_ram_vmap(phys_addr_t start, size_t size, |
|---|
| 394 | 404 | unsigned int memtype) |
|---|
| 395 | 405 | { |
|---|
| .. | .. |
|---|
| 403 | 413 | page_start = start - offset_in_page(start); |
|---|
| 404 | 414 | page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE); |
|---|
| 405 | 415 | |
|---|
| 406 | | - if (memtype) |
|---|
| 416 | + switch (memtype) { |
|---|
| 417 | + case MEM_TYPE_NORMAL: |
|---|
| 418 | + prot = PAGE_KERNEL; |
|---|
| 419 | + break; |
|---|
| 420 | + case MEM_TYPE_NONCACHED: |
|---|
| 407 | 421 | prot = pgprot_noncached(PAGE_KERNEL); |
|---|
| 408 | | - else |
|---|
| 422 | + break; |
|---|
| 423 | + case MEM_TYPE_WCOMBINE: |
|---|
| 409 | 424 | prot = pgprot_writecombine(PAGE_KERNEL); |
|---|
| 425 | + break; |
|---|
| 426 | + default: |
|---|
| 427 | + pr_err("invalid mem_type=%d\n", memtype); |
|---|
| 428 | + return NULL; |
|---|
| 429 | + } |
|---|
| 410 | 430 | |
|---|
| 411 | 431 | pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL); |
|---|
| 412 | 432 | if (!pages) { |
|---|
| .. | .. |
|---|
| 431 | 451 | } |
|---|
| 432 | 452 | |
|---|
| 433 | 453 | static void *persistent_ram_iomap(phys_addr_t start, size_t size, |
|---|
| 434 | | - unsigned int memtype) |
|---|
| 454 | + unsigned int memtype, char *label) |
|---|
| 435 | 455 | { |
|---|
| 436 | 456 | void *va; |
|---|
| 437 | 457 | |
|---|
| 438 | | - if (!request_mem_region(start, size, "persistent_ram")) { |
|---|
| 439 | | - pr_err("request mem region (0x%llx@0x%llx) failed\n", |
|---|
| 458 | + if (!request_mem_region(start, size, label ?: "ramoops")) { |
|---|
| 459 | + pr_err("request mem region (%s 0x%llx@0x%llx) failed\n", |
|---|
| 460 | + label ?: "ramoops", |
|---|
| 440 | 461 | (unsigned long long)size, (unsigned long long)start); |
|---|
| 441 | 462 | return NULL; |
|---|
| 442 | 463 | } |
|---|
| .. | .. |
|---|
| 463 | 484 | if (pfn_valid(start >> PAGE_SHIFT)) |
|---|
| 464 | 485 | prz->vaddr = persistent_ram_vmap(start, size, memtype); |
|---|
| 465 | 486 | else |
|---|
| 466 | | - prz->vaddr = persistent_ram_iomap(start, size, memtype); |
|---|
| 487 | + prz->vaddr = persistent_ram_iomap(start, size, memtype, |
|---|
| 488 | + prz->label); |
|---|
| 467 | 489 | |
|---|
| 468 | 490 | if (!prz->vaddr) { |
|---|
| 469 | 491 | pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__, |
|---|
| .. | .. |
|---|
| 481 | 503 | struct persistent_ram_ecc_info *ecc_info) |
|---|
| 482 | 504 | { |
|---|
| 483 | 505 | int ret; |
|---|
| 506 | + bool zap = !!(prz->flags & PRZ_FLAG_ZAP_OLD); |
|---|
| 484 | 507 | |
|---|
| 485 | 508 | ret = persistent_ram_init_ecc(prz, ecc_info); |
|---|
| 486 | | - if (ret) |
|---|
| 509 | + if (ret) { |
|---|
| 510 | + pr_warn("ECC failed %s\n", prz->label); |
|---|
| 487 | 511 | return ret; |
|---|
| 512 | + } |
|---|
| 488 | 513 | |
|---|
| 489 | 514 | sig ^= PERSISTENT_RAM_SIG; |
|---|
| 490 | 515 | |
|---|
| .. | .. |
|---|
| 495 | 520 | } |
|---|
| 496 | 521 | |
|---|
| 497 | 522 | if (buffer_size(prz) > prz->buffer_size || |
|---|
| 498 | | - buffer_start(prz) > buffer_size(prz)) |
|---|
| 523 | + buffer_start(prz) > buffer_size(prz)) { |
|---|
| 499 | 524 | pr_info("found existing invalid buffer, size %zu, start %zu\n", |
|---|
| 500 | 525 | buffer_size(prz), buffer_start(prz)); |
|---|
| 501 | | - else { |
|---|
| 526 | + zap = true; |
|---|
| 527 | + } else { |
|---|
| 502 | 528 | pr_debug("found existing buffer, size %zu, start %zu\n", |
|---|
| 503 | 529 | buffer_size(prz), buffer_start(prz)); |
|---|
| 504 | 530 | persistent_ram_save_old(prz); |
|---|
| 505 | | - return 0; |
|---|
| 506 | 531 | } |
|---|
| 507 | 532 | } else { |
|---|
| 508 | 533 | pr_debug("no valid data in buffer (sig = 0x%08x)\n", |
|---|
| 509 | 534 | prz->buffer->sig); |
|---|
| 535 | + prz->buffer->sig = sig; |
|---|
| 536 | + zap = true; |
|---|
| 510 | 537 | } |
|---|
| 511 | 538 | |
|---|
| 512 | | - /* Rewind missing or invalid memory area. */ |
|---|
| 513 | | - prz->buffer->sig = sig; |
|---|
| 514 | | - persistent_ram_zap(prz); |
|---|
| 539 | + /* Reset missing, invalid, or single-use memory area. */ |
|---|
| 540 | + if (zap) |
|---|
| 541 | + persistent_ram_zap(prz); |
|---|
| 515 | 542 | |
|---|
| 516 | 543 | return 0; |
|---|
| 517 | 544 | } |
|---|
| .. | .. |
|---|
| 539 | 566 | prz->ecc_info.par = NULL; |
|---|
| 540 | 567 | |
|---|
| 541 | 568 | persistent_ram_free_old(prz); |
|---|
| 569 | + kfree(prz->label); |
|---|
| 542 | 570 | kfree(prz); |
|---|
| 543 | 571 | } |
|---|
| 544 | 572 | |
|---|
| 545 | 573 | struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size, |
|---|
| 546 | 574 | u32 sig, struct persistent_ram_ecc_info *ecc_info, |
|---|
| 547 | | - unsigned int memtype, u32 flags) |
|---|
| 575 | + unsigned int memtype, u32 flags, char *label) |
|---|
| 548 | 576 | { |
|---|
| 549 | 577 | struct persistent_ram_zone *prz; |
|---|
| 550 | 578 | int ret = -ENOMEM; |
|---|
| .. | .. |
|---|
| 558 | 586 | /* Initialize general buffer state. */ |
|---|
| 559 | 587 | raw_spin_lock_init(&prz->buffer_lock); |
|---|
| 560 | 588 | prz->flags = flags; |
|---|
| 589 | + prz->label = kstrdup(label, GFP_KERNEL); |
|---|
| 561 | 590 | |
|---|
| 562 | 591 | ret = persistent_ram_buffer_map(start, size, prz, memtype); |
|---|
| 563 | 592 | if (ret) |
|---|
| .. | .. |
|---|
| 567 | 596 | if (ret) |
|---|
| 568 | 597 | goto err; |
|---|
| 569 | 598 | |
|---|
| 599 | + pr_debug("attached %s 0x%zx@0x%llx: %zu header, %zu data, %zu ecc (%d/%d)\n", |
|---|
| 600 | + prz->label, prz->size, (unsigned long long)prz->paddr, |
|---|
| 601 | + sizeof(*prz->buffer), prz->buffer_size, |
|---|
| 602 | + prz->size - sizeof(*prz->buffer) - prz->buffer_size, |
|---|
| 603 | + prz->ecc_info.ecc_size, prz->ecc_info.block_size); |
|---|
| 604 | + |
|---|
| 570 | 605 | return prz; |
|---|
| 571 | 606 | err: |
|---|
| 572 | 607 | persistent_ram_free(prz); |
|---|