ram_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. uint16_t par[prz->ecc_info.ecc_size];
  85. /* Initialize the parity buffer */
  86. memset(par, 0, sizeof(par));
  87. encode_rs8(prz->rs_decoder, data, len, par, 0);
  88. for (i = 0; i < prz->ecc_info.ecc_size; i++)
  89. ecc[i] = 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. uint16_t par[prz->ecc_info.ecc_size];
  96. for (i = 0; i < prz->ecc_info.ecc_size; i++)
  97. par[i] = ecc[i];
  98. return decode_rs8(prz->rs_decoder, data, par, len,
  99. NULL, 0, NULL, 0, NULL);
  100. }
  101. static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
  102. unsigned int start, unsigned int count)
  103. {
  104. struct persistent_ram_buffer *buffer = prz->buffer;
  105. uint8_t *buffer_end = buffer->data + prz->buffer_size;
  106. uint8_t *block;
  107. uint8_t *par;
  108. int ecc_block_size = prz->ecc_info.block_size;
  109. int ecc_size = prz->ecc_info.ecc_size;
  110. int size = ecc_block_size;
  111. if (!ecc_size)
  112. return;
  113. block = buffer->data + (start & ~(ecc_block_size - 1));
  114. par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
  115. do {
  116. if (block + ecc_block_size > buffer_end)
  117. size = buffer_end - block;
  118. persistent_ram_encode_rs8(prz, block, size, par);
  119. block += ecc_block_size;
  120. par += ecc_size;
  121. } while (block < buffer->data + start + count);
  122. }
  123. static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
  124. {
  125. struct persistent_ram_buffer *buffer = prz->buffer;
  126. if (!prz->ecc_info.ecc_size)
  127. return;
  128. persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
  129. prz->par_header);
  130. }
  131. static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
  132. {
  133. struct persistent_ram_buffer *buffer = prz->buffer;
  134. uint8_t *block;
  135. uint8_t *par;
  136. if (!prz->ecc_info.ecc_size)
  137. return;
  138. block = buffer->data;
  139. par = prz->par_buffer;
  140. while (block < buffer->data + buffer_size(prz)) {
  141. int numerr;
  142. int size = prz->ecc_info.block_size;
  143. if (block + size > buffer->data + prz->buffer_size)
  144. size = buffer->data + prz->buffer_size - block;
  145. numerr = persistent_ram_decode_rs8(prz, block, size, par);
  146. if (numerr > 0) {
  147. pr_devel("error in block %p, %d\n", block, numerr);
  148. prz->corrected_bytes += numerr;
  149. } else if (numerr < 0) {
  150. pr_devel("uncorrectable error in block %p\n", block);
  151. prz->bad_blocks++;
  152. }
  153. block += prz->ecc_info.block_size;
  154. par += prz->ecc_info.ecc_size;
  155. }
  156. }
  157. static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
  158. struct persistent_ram_ecc_info *ecc_info)
  159. {
  160. int numerr;
  161. struct persistent_ram_buffer *buffer = prz->buffer;
  162. int ecc_blocks;
  163. size_t ecc_total;
  164. if (!ecc_info || !ecc_info->ecc_size)
  165. return 0;
  166. prz->ecc_info.block_size = ecc_info->block_size ?: 128;
  167. prz->ecc_info.ecc_size = ecc_info->ecc_size ?: 16;
  168. prz->ecc_info.symsize = ecc_info->symsize ?: 8;
  169. prz->ecc_info.poly = ecc_info->poly ?: 0x11d;
  170. ecc_blocks = DIV_ROUND_UP(prz->buffer_size - prz->ecc_info.ecc_size,
  171. prz->ecc_info.block_size +
  172. prz->ecc_info.ecc_size);
  173. ecc_total = (ecc_blocks + 1) * prz->ecc_info.ecc_size;
  174. if (ecc_total >= prz->buffer_size) {
  175. pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
  176. __func__, prz->ecc_info.ecc_size,
  177. ecc_total, prz->buffer_size);
  178. return -EINVAL;
  179. }
  180. prz->buffer_size -= ecc_total;
  181. prz->par_buffer = buffer->data + prz->buffer_size;
  182. prz->par_header = prz->par_buffer +
  183. ecc_blocks * prz->ecc_info.ecc_size;
  184. /*
  185. * first consecutive root is 0
  186. * primitive element to generate roots = 1
  187. */
  188. prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly,
  189. 0, 1, prz->ecc_info.ecc_size);
  190. if (prz->rs_decoder == NULL) {
  191. pr_info("init_rs failed\n");
  192. return -EINVAL;
  193. }
  194. prz->corrected_bytes = 0;
  195. prz->bad_blocks = 0;
  196. numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
  197. prz->par_header);
  198. if (numerr > 0) {
  199. pr_info("error in header, %d\n", numerr);
  200. prz->corrected_bytes += numerr;
  201. } else if (numerr < 0) {
  202. pr_info("uncorrectable error in header\n");
  203. prz->bad_blocks++;
  204. }
  205. return 0;
  206. }
  207. ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
  208. char *str, size_t len)
  209. {
  210. ssize_t ret;
  211. if (!prz->ecc_info.ecc_size)
  212. return 0;
  213. if (prz->corrected_bytes || prz->bad_blocks)
  214. ret = snprintf(str, len, ""
  215. "\n%d Corrected bytes, %d unrecoverable blocks\n",
  216. prz->corrected_bytes, prz->bad_blocks);
  217. else
  218. ret = snprintf(str, len, "\nNo errors detected\n");
  219. return ret;
  220. }
  221. static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
  222. const void *s, unsigned int start, unsigned int count)
  223. {
  224. struct persistent_ram_buffer *buffer = prz->buffer;
  225. memcpy_toio(buffer->data + start, s, count);
  226. persistent_ram_update_ecc(prz, start, count);
  227. }
  228. static int notrace persistent_ram_update_user(struct persistent_ram_zone *prz,
  229. const void __user *s, unsigned int start, unsigned int count)
  230. {
  231. struct persistent_ram_buffer *buffer = prz->buffer;
  232. int ret = unlikely(__copy_from_user(buffer->data + start, s, count)) ?
  233. -EFAULT : 0;
  234. persistent_ram_update_ecc(prz, start, count);
  235. return ret;
  236. }
  237. void persistent_ram_save_old(struct persistent_ram_zone *prz)
  238. {
  239. struct persistent_ram_buffer *buffer = prz->buffer;
  240. size_t size = buffer_size(prz);
  241. size_t start = buffer_start(prz);
  242. if (!size)
  243. return;
  244. if (!prz->old_log) {
  245. persistent_ram_ecc_old(prz);
  246. prz->old_log = kmalloc(size, GFP_KERNEL);
  247. }
  248. if (!prz->old_log) {
  249. pr_err("failed to allocate buffer\n");
  250. return;
  251. }
  252. prz->old_log_size = size;
  253. memcpy_fromio(prz->old_log, &buffer->data[start], size - start);
  254. memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start);
  255. }
  256. int notrace persistent_ram_write(struct persistent_ram_zone *prz,
  257. const void *s, unsigned int count)
  258. {
  259. int rem;
  260. int c = count;
  261. size_t start;
  262. if (unlikely(c > prz->buffer_size)) {
  263. s += c - prz->buffer_size;
  264. c = prz->buffer_size;
  265. }
  266. buffer_size_add(prz, c);
  267. start = buffer_start_add(prz, c);
  268. rem = prz->buffer_size - start;
  269. if (unlikely(rem < c)) {
  270. persistent_ram_update(prz, s, start, rem);
  271. s += rem;
  272. c -= rem;
  273. start = 0;
  274. }
  275. persistent_ram_update(prz, s, start, c);
  276. persistent_ram_update_header_ecc(prz);
  277. return count;
  278. }
  279. int notrace persistent_ram_write_user(struct persistent_ram_zone *prz,
  280. const void __user *s, unsigned int count)
  281. {
  282. int rem, ret = 0, c = count;
  283. size_t start;
  284. if (unlikely(!access_ok(VERIFY_READ, s, count)))
  285. return -EFAULT;
  286. if (unlikely(c > prz->buffer_size)) {
  287. s += c - prz->buffer_size;
  288. c = prz->buffer_size;
  289. }
  290. buffer_size_add(prz, c);
  291. start = buffer_start_add(prz, c);
  292. rem = prz->buffer_size - start;
  293. if (unlikely(rem < c)) {
  294. ret = persistent_ram_update_user(prz, s, start, rem);
  295. s += rem;
  296. c -= rem;
  297. start = 0;
  298. }
  299. if (likely(!ret))
  300. ret = persistent_ram_update_user(prz, s, start, c);
  301. persistent_ram_update_header_ecc(prz);
  302. return unlikely(ret) ? ret : count;
  303. }
  304. size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
  305. {
  306. return prz->old_log_size;
  307. }
  308. void *persistent_ram_old(struct persistent_ram_zone *prz)
  309. {
  310. return prz->old_log;
  311. }
  312. void persistent_ram_free_old(struct persistent_ram_zone *prz)
  313. {
  314. kfree(prz->old_log);
  315. prz->old_log = NULL;
  316. prz->old_log_size = 0;
  317. }
  318. void persistent_ram_zap(struct persistent_ram_zone *prz)
  319. {
  320. atomic_set(&prz->buffer->start, 0);
  321. atomic_set(&prz->buffer->size, 0);
  322. persistent_ram_update_header_ecc(prz);
  323. }
  324. static void *persistent_ram_vmap(phys_addr_t start, size_t size,
  325. unsigned int memtype)
  326. {
  327. struct page **pages;
  328. phys_addr_t page_start;
  329. unsigned int page_count;
  330. pgprot_t prot;
  331. unsigned int i;
  332. void *vaddr;
  333. page_start = start - offset_in_page(start);
  334. page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
  335. if (memtype)
  336. prot = pgprot_noncached(PAGE_KERNEL);
  337. else
  338. prot = pgprot_writecombine(PAGE_KERNEL);
  339. pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
  340. if (!pages) {
  341. pr_err("%s: Failed to allocate array for %u pages\n",
  342. __func__, page_count);
  343. return NULL;
  344. }
  345. for (i = 0; i < page_count; i++) {
  346. phys_addr_t addr = page_start + i * PAGE_SIZE;
  347. pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
  348. }
  349. vaddr = vmap(pages, page_count, VM_MAP, prot);
  350. kfree(pages);
  351. return vaddr;
  352. }
  353. static void *persistent_ram_iomap(phys_addr_t start, size_t size,
  354. unsigned int memtype)
  355. {
  356. void *va;
  357. if (!request_mem_region(start, size, "persistent_ram")) {
  358. pr_err("request mem region (0x%llx@0x%llx) failed\n",
  359. (unsigned long long)size, (unsigned long long)start);
  360. return NULL;
  361. }
  362. if (memtype)
  363. va = ioremap(start, size);
  364. else
  365. va = ioremap_wc(start, size);
  366. return va;
  367. }
  368. static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
  369. struct persistent_ram_zone *prz, int memtype)
  370. {
  371. prz->paddr = start;
  372. prz->size = size;
  373. if (pfn_valid(start >> PAGE_SHIFT))
  374. prz->vaddr = persistent_ram_vmap(start, size, memtype);
  375. else
  376. prz->vaddr = persistent_ram_iomap(start, size, memtype);
  377. if (!prz->vaddr) {
  378. pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
  379. (unsigned long long)size, (unsigned long long)start);
  380. return -ENOMEM;
  381. }
  382. prz->buffer = prz->vaddr + offset_in_page(start);
  383. prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
  384. return 0;
  385. }
  386. static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
  387. struct persistent_ram_ecc_info *ecc_info)
  388. {
  389. int ret;
  390. ret = persistent_ram_init_ecc(prz, ecc_info);
  391. if (ret)
  392. return ret;
  393. sig ^= PERSISTENT_RAM_SIG;
  394. if (prz->buffer->sig == sig) {
  395. if (buffer_size(prz) > prz->buffer_size ||
  396. buffer_start(prz) > buffer_size(prz))
  397. pr_info("found existing invalid buffer, size %zu, start %zu\n",
  398. buffer_size(prz), buffer_start(prz));
  399. else {
  400. pr_debug("found existing buffer, size %zu, start %zu\n",
  401. buffer_size(prz), buffer_start(prz));
  402. persistent_ram_save_old(prz);
  403. return 0;
  404. }
  405. } else {
  406. pr_debug("no valid data in buffer (sig = 0x%08x)\n",
  407. prz->buffer->sig);
  408. }
  409. /* Rewind missing or invalid memory area. */
  410. prz->buffer->sig = sig;
  411. persistent_ram_zap(prz);
  412. return 0;
  413. }
  414. void persistent_ram_free(struct persistent_ram_zone *prz)
  415. {
  416. if (!prz)
  417. return;
  418. if (prz->vaddr) {
  419. if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
  420. vunmap(prz->vaddr);
  421. } else {
  422. iounmap(prz->vaddr);
  423. release_mem_region(prz->paddr, prz->size);
  424. }
  425. prz->vaddr = NULL;
  426. }
  427. persistent_ram_free_old(prz);
  428. kfree(prz);
  429. }
  430. struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
  431. u32 sig, struct persistent_ram_ecc_info *ecc_info,
  432. unsigned int memtype, u32 flags)
  433. {
  434. struct persistent_ram_zone *prz;
  435. int ret = -ENOMEM;
  436. prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
  437. if (!prz) {
  438. pr_err("failed to allocate persistent ram zone\n");
  439. goto err;
  440. }
  441. /* Initialize general buffer state. */
  442. raw_spin_lock_init(&prz->buffer_lock);
  443. prz->flags = flags;
  444. ret = persistent_ram_buffer_map(start, size, prz, memtype);
  445. if (ret)
  446. goto err;
  447. ret = persistent_ram_post_init(prz, sig, ecc_info);
  448. if (ret)
  449. goto err;
  450. return prz;
  451. err:
  452. persistent_ram_free(prz);
  453. return ERR_PTR(ret);
  454. }