ram_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * 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. */
  14. #define pr_fmt(fmt) "persistent_ram: " fmt
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/memblock.h>
  23. #include <linux/pstore_ram.h>
  24. #include <linux/rslib.h>
  25. #include <linux/slab.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/page.h>
  29. struct persistent_ram_buffer {
  30. uint32_t sig;
  31. atomic_t start;
  32. atomic_t size;
  33. uint8_t data[0];
  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. int 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("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. "\n%d Corrected bytes, %d unrecoverable blocks\n",
  223. prz->corrected_bytes, prz->bad_blocks);
  224. else
  225. ret = snprintf(str, len, "\nNo 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 = kmalloc(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(!access_ok(VERIFY_READ, s, count)))
  292. return -EFAULT;
  293. if (unlikely(c > prz->buffer_size)) {
  294. s += c - prz->buffer_size;
  295. c = prz->buffer_size;
  296. }
  297. buffer_size_add(prz, c);
  298. start = buffer_start_add(prz, c);
  299. rem = prz->buffer_size - start;
  300. if (unlikely(rem < c)) {
  301. ret = persistent_ram_update_user(prz, s, start, rem);
  302. s += rem;
  303. c -= rem;
  304. start = 0;
  305. }
  306. if (likely(!ret))
  307. ret = persistent_ram_update_user(prz, s, start, c);
  308. persistent_ram_update_header_ecc(prz);
  309. return unlikely(ret) ? ret : count;
  310. }
  311. size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
  312. {
  313. return prz->old_log_size;
  314. }
  315. void *persistent_ram_old(struct persistent_ram_zone *prz)
  316. {
  317. return prz->old_log;
  318. }
  319. void persistent_ram_free_old(struct persistent_ram_zone *prz)
  320. {
  321. kfree(prz->old_log);
  322. prz->old_log = NULL;
  323. prz->old_log_size = 0;
  324. }
  325. void persistent_ram_zap(struct persistent_ram_zone *prz)
  326. {
  327. atomic_set(&prz->buffer->start, 0);
  328. atomic_set(&prz->buffer->size, 0);
  329. persistent_ram_update_header_ecc(prz);
  330. }
  331. static void *persistent_ram_vmap(phys_addr_t start, size_t size,
  332. unsigned int memtype)
  333. {
  334. struct page **pages;
  335. phys_addr_t page_start;
  336. unsigned int page_count;
  337. pgprot_t prot;
  338. unsigned int i;
  339. void *vaddr;
  340. page_start = start - offset_in_page(start);
  341. page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
  342. if (memtype)
  343. prot = pgprot_noncached(PAGE_KERNEL);
  344. else
  345. prot = pgprot_writecombine(PAGE_KERNEL);
  346. pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
  347. if (!pages) {
  348. pr_err("%s: Failed to allocate array for %u pages\n",
  349. __func__, page_count);
  350. return NULL;
  351. }
  352. for (i = 0; i < page_count; i++) {
  353. phys_addr_t addr = page_start + i * PAGE_SIZE;
  354. pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
  355. }
  356. vaddr = vmap(pages, page_count, VM_MAP, prot);
  357. kfree(pages);
  358. /*
  359. * Since vmap() uses page granularity, we must add the offset
  360. * into the page here, to get the byte granularity address
  361. * into the mapping to represent the actual "start" location.
  362. */
  363. return vaddr + offset_in_page(start);
  364. }
  365. static void *persistent_ram_iomap(phys_addr_t start, size_t size,
  366. unsigned int memtype)
  367. {
  368. void *va;
  369. if (!request_mem_region(start, size, "persistent_ram")) {
  370. pr_err("request mem region (0x%llx@0x%llx) failed\n",
  371. (unsigned long long)size, (unsigned long long)start);
  372. return NULL;
  373. }
  374. if (memtype)
  375. va = ioremap(start, size);
  376. else
  377. va = ioremap_wc(start, size);
  378. /*
  379. * Since request_mem_region() and ioremap() are byte-granularity
  380. * there is no need handle anything special like we do when the
  381. * vmap() case in persistent_ram_vmap() above.
  382. */
  383. return va;
  384. }
  385. static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
  386. struct persistent_ram_zone *prz, int memtype)
  387. {
  388. prz->paddr = start;
  389. prz->size = size;
  390. if (pfn_valid(start >> PAGE_SHIFT))
  391. prz->vaddr = persistent_ram_vmap(start, size, memtype);
  392. else
  393. prz->vaddr = persistent_ram_iomap(start, size, memtype);
  394. if (!prz->vaddr) {
  395. pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
  396. (unsigned long long)size, (unsigned long long)start);
  397. return -ENOMEM;
  398. }
  399. prz->buffer = prz->vaddr;
  400. prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
  401. return 0;
  402. }
  403. static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
  404. struct persistent_ram_ecc_info *ecc_info)
  405. {
  406. int ret;
  407. ret = persistent_ram_init_ecc(prz, ecc_info);
  408. if (ret)
  409. return ret;
  410. sig ^= PERSISTENT_RAM_SIG;
  411. if (prz->buffer->sig == sig) {
  412. if (buffer_size(prz) == 0) {
  413. pr_debug("found existing empty buffer\n");
  414. return 0;
  415. }
  416. if (buffer_size(prz) > prz->buffer_size ||
  417. buffer_start(prz) > buffer_size(prz))
  418. pr_info("found existing invalid buffer, size %zu, start %zu\n",
  419. buffer_size(prz), buffer_start(prz));
  420. else {
  421. pr_debug("found existing buffer, size %zu, start %zu\n",
  422. buffer_size(prz), buffer_start(prz));
  423. persistent_ram_save_old(prz);
  424. return 0;
  425. }
  426. } else {
  427. pr_debug("no valid data in buffer (sig = 0x%08x)\n",
  428. prz->buffer->sig);
  429. }
  430. /* Rewind missing or invalid memory area. */
  431. prz->buffer->sig = sig;
  432. persistent_ram_zap(prz);
  433. return 0;
  434. }
  435. void persistent_ram_free(struct persistent_ram_zone *prz)
  436. {
  437. if (!prz)
  438. return;
  439. if (prz->vaddr) {
  440. if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
  441. /* We must vunmap() at page-granularity. */
  442. vunmap(prz->vaddr - offset_in_page(prz->paddr));
  443. } else {
  444. iounmap(prz->vaddr);
  445. release_mem_region(prz->paddr, prz->size);
  446. }
  447. prz->vaddr = NULL;
  448. }
  449. if (prz->rs_decoder) {
  450. free_rs(prz->rs_decoder);
  451. prz->rs_decoder = NULL;
  452. }
  453. kfree(prz->ecc_info.par);
  454. prz->ecc_info.par = NULL;
  455. persistent_ram_free_old(prz);
  456. kfree(prz);
  457. }
  458. struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
  459. u32 sig, struct persistent_ram_ecc_info *ecc_info,
  460. unsigned int memtype, u32 flags)
  461. {
  462. struct persistent_ram_zone *prz;
  463. int ret = -ENOMEM;
  464. prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
  465. if (!prz) {
  466. pr_err("failed to allocate persistent ram zone\n");
  467. goto err;
  468. }
  469. /* Initialize general buffer state. */
  470. raw_spin_lock_init(&prz->buffer_lock);
  471. prz->flags = flags;
  472. ret = persistent_ram_buffer_map(start, size, prz, memtype);
  473. if (ret)
  474. goto err;
  475. ret = persistent_ram_post_init(prz, sig, ecc_info);
  476. if (ret)
  477. goto err;
  478. return prz;
  479. err:
  480. persistent_ram_free(prz);
  481. return ERR_PTR(ret);
  482. }