ram_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Google, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/memblock.h>
  14. #include <linux/rslib.h>
  15. #include <linux/slab.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/mm.h>
  19. #include <asm/page.h>
  20. #include "ram_internal.h"
  21. /**
  22. * struct persistent_ram_buffer - persistent circular RAM buffer
  23. *
  24. * @sig: Signature to indicate header (PERSISTENT_RAM_SIG xor PRZ-type value)
  25. * @start: First valid byte in the buffer.
  26. * @size: Number of valid bytes in the buffer.
  27. * @data: The contents of the buffer.
  28. */
  29. struct persistent_ram_buffer {
  30. uint32_t sig;
  31. atomic_t start;
  32. atomic_t size;
  33. uint8_t data[];
  34. };
  35. #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
  36. static inline size_t buffer_size(struct persistent_ram_zone *prz)
  37. {
  38. return atomic_read(&prz->buffer->size);
  39. }
  40. static inline size_t buffer_start(struct persistent_ram_zone *prz)
  41. {
  42. return atomic_read(&prz->buffer->start);
  43. }
  44. /* increase and wrap the start pointer, returning the old value */
  45. static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
  46. {
  47. int old;
  48. int new;
  49. unsigned long flags = 0;
  50. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  51. raw_spin_lock_irqsave(&prz->buffer_lock, flags);
  52. old = atomic_read(&prz->buffer->start);
  53. new = old + a;
  54. while (unlikely(new >= prz->buffer_size))
  55. new -= prz->buffer_size;
  56. atomic_set(&prz->buffer->start, new);
  57. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  58. raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
  59. return old;
  60. }
  61. /* increase the size counter until it hits the max size */
  62. static void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
  63. {
  64. size_t old;
  65. size_t new;
  66. unsigned long flags = 0;
  67. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  68. raw_spin_lock_irqsave(&prz->buffer_lock, flags);
  69. old = atomic_read(&prz->buffer->size);
  70. if (old == prz->buffer_size)
  71. goto exit;
  72. new = old + a;
  73. if (new > prz->buffer_size)
  74. new = prz->buffer_size;
  75. atomic_set(&prz->buffer->size, new);
  76. exit:
  77. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  78. raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
  79. }
  80. static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
  81. uint8_t *data, size_t len, uint8_t *ecc)
  82. {
  83. int i;
  84. /* Initialize the parity buffer */
  85. memset(prz->ecc_info.par, 0,
  86. prz->ecc_info.ecc_size * sizeof(prz->ecc_info.par[0]));
  87. encode_rs8(prz->rs_decoder, data, len, prz->ecc_info.par, 0);
  88. for (i = 0; i < prz->ecc_info.ecc_size; i++)
  89. ecc[i] = prz->ecc_info.par[i];
  90. }
  91. static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
  92. void *data, size_t len, uint8_t *ecc)
  93. {
  94. int i;
  95. for (i = 0; i < prz->ecc_info.ecc_size; i++)
  96. prz->ecc_info.par[i] = ecc[i];
  97. return decode_rs8(prz->rs_decoder, data, prz->ecc_info.par, len,
  98. NULL, 0, NULL, 0, NULL);
  99. }
  100. static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
  101. unsigned int start, unsigned int count)
  102. {
  103. struct persistent_ram_buffer *buffer = prz->buffer;
  104. uint8_t *buffer_end = buffer->data + prz->buffer_size;
  105. uint8_t *block;
  106. uint8_t *par;
  107. int ecc_block_size = prz->ecc_info.block_size;
  108. int ecc_size = prz->ecc_info.ecc_size;
  109. int size = ecc_block_size;
  110. if (!ecc_size)
  111. return;
  112. block = buffer->data + (start & ~(ecc_block_size - 1));
  113. par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
  114. do {
  115. if (block + ecc_block_size > buffer_end)
  116. size = buffer_end - block;
  117. persistent_ram_encode_rs8(prz, block, size, par);
  118. block += ecc_block_size;
  119. par += ecc_size;
  120. } while (block < buffer->data + start + count);
  121. }
  122. static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
  123. {
  124. struct persistent_ram_buffer *buffer = prz->buffer;
  125. if (!prz->ecc_info.ecc_size)
  126. return;
  127. persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
  128. prz->par_header);
  129. }
  130. static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
  131. {
  132. struct persistent_ram_buffer *buffer = prz->buffer;
  133. uint8_t *block;
  134. uint8_t *par;
  135. if (!prz->ecc_info.ecc_size)
  136. return;
  137. block = buffer->data;
  138. par = prz->par_buffer;
  139. while (block < buffer->data + buffer_size(prz)) {
  140. int numerr;
  141. int size = prz->ecc_info.block_size;
  142. if (block + size > buffer->data + prz->buffer_size)
  143. size = buffer->data + prz->buffer_size - block;
  144. numerr = persistent_ram_decode_rs8(prz, block, size, par);
  145. if (numerr > 0) {
  146. pr_devel("error in block %p, %d\n", block, numerr);
  147. prz->corrected_bytes += numerr;
  148. } else if (numerr < 0) {
  149. pr_devel("uncorrectable error in block %p\n", block);
  150. prz->bad_blocks++;
  151. }
  152. block += prz->ecc_info.block_size;
  153. par += prz->ecc_info.ecc_size;
  154. }
  155. }
  156. static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
  157. struct persistent_ram_ecc_info *ecc_info)
  158. {
  159. int numerr;
  160. struct persistent_ram_buffer *buffer = prz->buffer;
  161. size_t ecc_blocks;
  162. size_t ecc_total;
  163. if (!ecc_info || !ecc_info->ecc_size)
  164. return 0;
  165. prz->ecc_info.block_size = ecc_info->block_size ?: 128;
  166. prz->ecc_info.ecc_size = ecc_info->ecc_size ?: 16;
  167. prz->ecc_info.symsize = ecc_info->symsize ?: 8;
  168. prz->ecc_info.poly = ecc_info->poly ?: 0x11d;
  169. ecc_blocks = DIV_ROUND_UP(prz->buffer_size - prz->ecc_info.ecc_size,
  170. prz->ecc_info.block_size +
  171. prz->ecc_info.ecc_size);
  172. ecc_total = (ecc_blocks + 1) * prz->ecc_info.ecc_size;
  173. if (ecc_total >= prz->buffer_size) {
  174. pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
  175. __func__, prz->ecc_info.ecc_size,
  176. ecc_total, prz->buffer_size);
  177. return -EINVAL;
  178. }
  179. prz->buffer_size -= ecc_total;
  180. prz->par_buffer = buffer->data + prz->buffer_size;
  181. prz->par_header = prz->par_buffer +
  182. ecc_blocks * prz->ecc_info.ecc_size;
  183. /*
  184. * first consecutive root is 0
  185. * primitive element to generate roots = 1
  186. */
  187. prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly,
  188. 0, 1, prz->ecc_info.ecc_size);
  189. if (prz->rs_decoder == NULL) {
  190. pr_info("init_rs failed\n");
  191. return -EINVAL;
  192. }
  193. /* allocate workspace instead of using stack VLA */
  194. prz->ecc_info.par = kmalloc_array(prz->ecc_info.ecc_size,
  195. sizeof(*prz->ecc_info.par),
  196. GFP_KERNEL);
  197. if (!prz->ecc_info.par) {
  198. pr_err("cannot allocate ECC parity workspace\n");
  199. return -ENOMEM;
  200. }
  201. prz->corrected_bytes = 0;
  202. prz->bad_blocks = 0;
  203. numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
  204. prz->par_header);
  205. if (numerr > 0) {
  206. pr_info("error in header, %d\n", numerr);
  207. prz->corrected_bytes += numerr;
  208. } else if (numerr < 0) {
  209. pr_info_ratelimited("uncorrectable error in header\n");
  210. prz->bad_blocks++;
  211. }
  212. return 0;
  213. }
  214. ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
  215. char *str, size_t len)
  216. {
  217. ssize_t ret;
  218. if (!prz->ecc_info.ecc_size)
  219. return 0;
  220. if (prz->corrected_bytes || prz->bad_blocks)
  221. ret = snprintf(str, len, ""
  222. "\nECC: %d Corrected bytes, %d unrecoverable blocks\n",
  223. prz->corrected_bytes, prz->bad_blocks);
  224. else
  225. ret = snprintf(str, len, "\nECC: No errors detected\n");
  226. return ret;
  227. }
  228. static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
  229. const void *s, unsigned int start, unsigned int count)
  230. {
  231. struct persistent_ram_buffer *buffer = prz->buffer;
  232. memcpy_toio(buffer->data + start, s, count);
  233. persistent_ram_update_ecc(prz, start, count);
  234. }
  235. static int notrace persistent_ram_update_user(struct persistent_ram_zone *prz,
  236. const void __user *s, unsigned int start, unsigned int count)
  237. {
  238. struct persistent_ram_buffer *buffer = prz->buffer;
  239. int ret = unlikely(copy_from_user(buffer->data + start, s, count)) ?
  240. -EFAULT : 0;
  241. persistent_ram_update_ecc(prz, start, count);
  242. return ret;
  243. }
  244. void persistent_ram_save_old(struct persistent_ram_zone *prz)
  245. {
  246. struct persistent_ram_buffer *buffer = prz->buffer;
  247. size_t size = buffer_size(prz);
  248. size_t start = buffer_start(prz);
  249. if (!size)
  250. return;
  251. if (!prz->old_log) {
  252. persistent_ram_ecc_old(prz);
  253. prz->old_log = kvzalloc(size, GFP_KERNEL);
  254. }
  255. if (!prz->old_log) {
  256. pr_err("failed to allocate buffer\n");
  257. return;
  258. }
  259. prz->old_log_size = size;
  260. memcpy_fromio(prz->old_log, &buffer->data[start], size - start);
  261. memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start);
  262. }
  263. int notrace persistent_ram_write(struct persistent_ram_zone *prz,
  264. const void *s, unsigned int count)
  265. {
  266. int rem;
  267. int c = count;
  268. size_t start;
  269. if (unlikely(c > prz->buffer_size)) {
  270. s += c - prz->buffer_size;
  271. c = prz->buffer_size;
  272. }
  273. buffer_size_add(prz, c);
  274. start = buffer_start_add(prz, c);
  275. rem = prz->buffer_size - start;
  276. if (unlikely(rem < c)) {
  277. persistent_ram_update(prz, s, start, rem);
  278. s += rem;
  279. c -= rem;
  280. start = 0;
  281. }
  282. persistent_ram_update(prz, s, start, c);
  283. persistent_ram_update_header_ecc(prz);
  284. return count;
  285. }
  286. int notrace persistent_ram_write_user(struct persistent_ram_zone *prz,
  287. const void __user *s, unsigned int count)
  288. {
  289. int rem, ret = 0, c = count;
  290. size_t start;
  291. if (unlikely(c > prz->buffer_size)) {
  292. s += c - prz->buffer_size;
  293. c = prz->buffer_size;
  294. }
  295. buffer_size_add(prz, c);
  296. start = buffer_start_add(prz, c);
  297. rem = prz->buffer_size - start;
  298. if (unlikely(rem < c)) {
  299. ret = persistent_ram_update_user(prz, s, start, rem);
  300. s += rem;
  301. c -= rem;
  302. start = 0;
  303. }
  304. if (likely(!ret))
  305. ret = persistent_ram_update_user(prz, s, start, c);
  306. persistent_ram_update_header_ecc(prz);
  307. return unlikely(ret) ? ret : count;
  308. }
  309. size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
  310. {
  311. return prz->old_log_size;
  312. }
  313. void *persistent_ram_old(struct persistent_ram_zone *prz)
  314. {
  315. return prz->old_log;
  316. }
  317. void persistent_ram_free_old(struct persistent_ram_zone *prz)
  318. {
  319. kvfree(prz->old_log);
  320. prz->old_log = NULL;
  321. prz->old_log_size = 0;
  322. }
  323. void persistent_ram_zap(struct persistent_ram_zone *prz)
  324. {
  325. atomic_set(&prz->buffer->start, 0);
  326. atomic_set(&prz->buffer->size, 0);
  327. persistent_ram_update_header_ecc(prz);
  328. }
  329. #define MEM_TYPE_WCOMBINE 0
  330. #define MEM_TYPE_NONCACHED 1
  331. #define MEM_TYPE_NORMAL 2
  332. static void *persistent_ram_vmap(phys_addr_t start, size_t size,
  333. unsigned int memtype)
  334. {
  335. struct page **pages;
  336. phys_addr_t page_start;
  337. unsigned int page_count;
  338. pgprot_t prot;
  339. unsigned int i;
  340. void *vaddr;
  341. page_start = start - offset_in_page(start);
  342. page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
  343. switch (memtype) {
  344. case MEM_TYPE_NORMAL:
  345. prot = PAGE_KERNEL;
  346. break;
  347. case MEM_TYPE_NONCACHED:
  348. prot = pgprot_noncached(PAGE_KERNEL);
  349. break;
  350. case MEM_TYPE_WCOMBINE:
  351. prot = pgprot_writecombine(PAGE_KERNEL);
  352. break;
  353. default:
  354. pr_err("invalid mem_type=%d\n", memtype);
  355. return NULL;
  356. }
  357. pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
  358. if (!pages) {
  359. pr_err("%s: Failed to allocate array for %u pages\n",
  360. __func__, page_count);
  361. return NULL;
  362. }
  363. for (i = 0; i < page_count; i++) {
  364. phys_addr_t addr = page_start + i * PAGE_SIZE;
  365. pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
  366. }
  367. /*
  368. * VM_IOREMAP used here to bypass this region during vread()
  369. * and kmap_atomic() (i.e. kcore) to avoid __va() failures.
  370. */
  371. vaddr = vmap(pages, page_count, VM_MAP | VM_IOREMAP, prot);
  372. kfree(pages);
  373. /*
  374. * Since vmap() uses page granularity, we must add the offset
  375. * into the page here, to get the byte granularity address
  376. * into the mapping to represent the actual "start" location.
  377. */
  378. return vaddr + offset_in_page(start);
  379. }
  380. static void *persistent_ram_iomap(phys_addr_t start, size_t size,
  381. unsigned int memtype, char *label)
  382. {
  383. void *va;
  384. if (!request_mem_region(start, size, label ?: "ramoops")) {
  385. pr_err("request mem region (%s 0x%llx@0x%llx) failed\n",
  386. label ?: "ramoops",
  387. (unsigned long long)size, (unsigned long long)start);
  388. return NULL;
  389. }
  390. if (memtype)
  391. va = ioremap(start, size);
  392. else
  393. va = ioremap_wc(start, size);
  394. /*
  395. * Since request_mem_region() and ioremap() are byte-granularity
  396. * there is no need handle anything special like we do when the
  397. * vmap() case in persistent_ram_vmap() above.
  398. */
  399. return va;
  400. }
  401. static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
  402. struct persistent_ram_zone *prz, int memtype)
  403. {
  404. prz->paddr = start;
  405. prz->size = size;
  406. if (pfn_valid(start >> PAGE_SHIFT))
  407. prz->vaddr = persistent_ram_vmap(start, size, memtype);
  408. else
  409. prz->vaddr = persistent_ram_iomap(start, size, memtype,
  410. prz->label);
  411. if (!prz->vaddr) {
  412. pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
  413. (unsigned long long)size, (unsigned long long)start);
  414. return -ENOMEM;
  415. }
  416. prz->buffer = prz->vaddr;
  417. prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
  418. return 0;
  419. }
  420. static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
  421. struct persistent_ram_ecc_info *ecc_info)
  422. {
  423. int ret;
  424. bool zap = !!(prz->flags & PRZ_FLAG_ZAP_OLD);
  425. ret = persistent_ram_init_ecc(prz, ecc_info);
  426. if (ret) {
  427. pr_warn("ECC failed %s\n", prz->label);
  428. return ret;
  429. }
  430. sig ^= PERSISTENT_RAM_SIG;
  431. if (prz->buffer->sig == sig) {
  432. if (buffer_size(prz) == 0 && buffer_start(prz) == 0) {
  433. pr_debug("found existing empty buffer\n");
  434. return 0;
  435. }
  436. if (buffer_size(prz) > prz->buffer_size ||
  437. buffer_start(prz) > buffer_size(prz)) {
  438. pr_info("found existing invalid buffer, size %zu, start %zu\n",
  439. buffer_size(prz), buffer_start(prz));
  440. zap = true;
  441. } else {
  442. pr_debug("found existing buffer, size %zu, start %zu\n",
  443. buffer_size(prz), buffer_start(prz));
  444. persistent_ram_save_old(prz);
  445. }
  446. } else {
  447. pr_debug("no valid data in buffer (sig = 0x%08x)\n",
  448. prz->buffer->sig);
  449. prz->buffer->sig = sig;
  450. zap = true;
  451. }
  452. /* Reset missing, invalid, or single-use memory area. */
  453. if (zap)
  454. persistent_ram_zap(prz);
  455. return 0;
  456. }
  457. void persistent_ram_free(struct persistent_ram_zone **_prz)
  458. {
  459. struct persistent_ram_zone *prz;
  460. if (!_prz)
  461. return;
  462. prz = *_prz;
  463. if (!prz)
  464. return;
  465. if (prz->vaddr) {
  466. if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
  467. /* We must vunmap() at page-granularity. */
  468. vunmap(prz->vaddr - offset_in_page(prz->paddr));
  469. } else {
  470. iounmap(prz->vaddr);
  471. release_mem_region(prz->paddr, prz->size);
  472. }
  473. prz->vaddr = NULL;
  474. }
  475. if (prz->rs_decoder) {
  476. free_rs(prz->rs_decoder);
  477. prz->rs_decoder = NULL;
  478. }
  479. kfree(prz->ecc_info.par);
  480. prz->ecc_info.par = NULL;
  481. persistent_ram_free_old(prz);
  482. kfree(prz->label);
  483. kfree(prz);
  484. *_prz = NULL;
  485. }
  486. struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
  487. u32 sig, struct persistent_ram_ecc_info *ecc_info,
  488. unsigned int memtype, u32 flags, char *label)
  489. {
  490. struct persistent_ram_zone *prz;
  491. int ret = -ENOMEM;
  492. prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
  493. if (!prz) {
  494. pr_err("failed to allocate persistent ram zone\n");
  495. goto err;
  496. }
  497. /* Initialize general buffer state. */
  498. raw_spin_lock_init(&prz->buffer_lock);
  499. prz->flags = flags;
  500. prz->label = kstrdup(label, GFP_KERNEL);
  501. if (!prz->label)
  502. goto err;
  503. ret = persistent_ram_buffer_map(start, size, prz, memtype);
  504. if (ret)
  505. goto err;
  506. ret = persistent_ram_post_init(prz, sig, ecc_info);
  507. if (ret)
  508. goto err;
  509. pr_debug("attached %s 0x%zx@0x%llx: %zu header, %zu data, %zu ecc (%d/%d)\n",
  510. prz->label, prz->size, (unsigned long long)prz->paddr,
  511. sizeof(*prz->buffer), prz->buffer_size,
  512. prz->size - sizeof(*prz->buffer) - prz->buffer_size,
  513. prz->ecc_info.ecc_size, prz->ecc_info.block_size);
  514. return prz;
  515. err:
  516. persistent_ram_free(&prz);
  517. return ERR_PTR(ret);
  518. }