ram_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/list.h>
  21. #include <linux/memblock.h>
  22. #include <linux/rslib.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/pstore_ram.h>
  26. #include <asm/page.h>
  27. struct persistent_ram_buffer {
  28. uint32_t sig;
  29. atomic_t start;
  30. atomic_t size;
  31. uint8_t data[0];
  32. };
  33. #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
  34. static __initdata LIST_HEAD(persistent_ram_list);
  35. static inline size_t buffer_size(struct persistent_ram_zone *prz)
  36. {
  37. return atomic_read(&prz->buffer->size);
  38. }
  39. static inline size_t buffer_start(struct persistent_ram_zone *prz)
  40. {
  41. return atomic_read(&prz->buffer->start);
  42. }
  43. /* increase and wrap the start pointer, returning the old value */
  44. static inline size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
  45. {
  46. int old;
  47. int new;
  48. do {
  49. old = atomic_read(&prz->buffer->start);
  50. new = old + a;
  51. while (unlikely(new > prz->buffer_size))
  52. new -= prz->buffer_size;
  53. } while (atomic_cmpxchg(&prz->buffer->start, old, new) != old);
  54. return old;
  55. }
  56. /* increase the size counter until it hits the max size */
  57. static inline void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
  58. {
  59. size_t old;
  60. size_t new;
  61. if (atomic_read(&prz->buffer->size) == prz->buffer_size)
  62. return;
  63. do {
  64. old = atomic_read(&prz->buffer->size);
  65. new = old + a;
  66. if (new > prz->buffer_size)
  67. new = prz->buffer_size;
  68. } while (atomic_cmpxchg(&prz->buffer->size, old, new) != old);
  69. }
  70. static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
  71. uint8_t *data, size_t len, uint8_t *ecc)
  72. {
  73. int i;
  74. uint16_t par[prz->ecc_size];
  75. /* Initialize the parity buffer */
  76. memset(par, 0, sizeof(par));
  77. encode_rs8(prz->rs_decoder, data, len, par, 0);
  78. for (i = 0; i < prz->ecc_size; i++)
  79. ecc[i] = par[i];
  80. }
  81. static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
  82. void *data, size_t len, uint8_t *ecc)
  83. {
  84. int i;
  85. uint16_t par[prz->ecc_size];
  86. for (i = 0; i < prz->ecc_size; i++)
  87. par[i] = ecc[i];
  88. return decode_rs8(prz->rs_decoder, data, par, len,
  89. NULL, 0, NULL, 0, NULL);
  90. }
  91. static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
  92. unsigned int start, unsigned int count)
  93. {
  94. struct persistent_ram_buffer *buffer = prz->buffer;
  95. uint8_t *buffer_end = buffer->data + prz->buffer_size;
  96. uint8_t *block;
  97. uint8_t *par;
  98. int ecc_block_size = prz->ecc_block_size;
  99. int ecc_size = prz->ecc_size;
  100. int size = prz->ecc_block_size;
  101. if (!prz->ecc)
  102. return;
  103. block = buffer->data + (start & ~(ecc_block_size - 1));
  104. par = prz->par_buffer + (start / ecc_block_size) * prz->ecc_size;
  105. do {
  106. if (block + ecc_block_size > buffer_end)
  107. size = buffer_end - block;
  108. persistent_ram_encode_rs8(prz, block, size, par);
  109. block += ecc_block_size;
  110. par += ecc_size;
  111. } while (block < buffer->data + start + count);
  112. }
  113. static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
  114. {
  115. struct persistent_ram_buffer *buffer = prz->buffer;
  116. if (!prz->ecc)
  117. return;
  118. persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
  119. prz->par_header);
  120. }
  121. static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
  122. {
  123. struct persistent_ram_buffer *buffer = prz->buffer;
  124. uint8_t *block;
  125. uint8_t *par;
  126. if (!prz->ecc)
  127. return;
  128. block = buffer->data;
  129. par = prz->par_buffer;
  130. while (block < buffer->data + buffer_size(prz)) {
  131. int numerr;
  132. int size = prz->ecc_block_size;
  133. if (block + size > buffer->data + prz->buffer_size)
  134. size = buffer->data + prz->buffer_size - block;
  135. numerr = persistent_ram_decode_rs8(prz, block, size, par);
  136. if (numerr > 0) {
  137. pr_devel("persistent_ram: error in block %p, %d\n",
  138. block, numerr);
  139. prz->corrected_bytes += numerr;
  140. } else if (numerr < 0) {
  141. pr_devel("persistent_ram: uncorrectable error in block %p\n",
  142. block);
  143. prz->bad_blocks++;
  144. }
  145. block += prz->ecc_block_size;
  146. par += prz->ecc_size;
  147. }
  148. }
  149. static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
  150. size_t buffer_size)
  151. {
  152. int numerr;
  153. struct persistent_ram_buffer *buffer = prz->buffer;
  154. int ecc_blocks;
  155. if (!prz->ecc)
  156. return 0;
  157. prz->ecc_block_size = 128;
  158. prz->ecc_size = 16;
  159. prz->ecc_symsize = 8;
  160. prz->ecc_poly = 0x11d;
  161. ecc_blocks = DIV_ROUND_UP(prz->buffer_size, prz->ecc_block_size);
  162. prz->buffer_size -= (ecc_blocks + 1) * prz->ecc_size;
  163. if (prz->buffer_size > buffer_size) {
  164. pr_err("persistent_ram: invalid size %zu, non-ecc datasize %zu\n",
  165. buffer_size, prz->buffer_size);
  166. return -EINVAL;
  167. }
  168. prz->par_buffer = buffer->data + prz->buffer_size;
  169. prz->par_header = prz->par_buffer + ecc_blocks * prz->ecc_size;
  170. /*
  171. * first consecutive root is 0
  172. * primitive element to generate roots = 1
  173. */
  174. prz->rs_decoder = init_rs(prz->ecc_symsize, prz->ecc_poly, 0, 1,
  175. prz->ecc_size);
  176. if (prz->rs_decoder == NULL) {
  177. pr_info("persistent_ram: init_rs failed\n");
  178. return -EINVAL;
  179. }
  180. prz->corrected_bytes = 0;
  181. prz->bad_blocks = 0;
  182. numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
  183. prz->par_header);
  184. if (numerr > 0) {
  185. pr_info("persistent_ram: error in header, %d\n", numerr);
  186. prz->corrected_bytes += numerr;
  187. } else if (numerr < 0) {
  188. pr_info("persistent_ram: uncorrectable error in header\n");
  189. prz->bad_blocks++;
  190. }
  191. return 0;
  192. }
  193. ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
  194. char *str, size_t len)
  195. {
  196. ssize_t ret;
  197. if (prz->corrected_bytes || prz->bad_blocks)
  198. ret = snprintf(str, len, ""
  199. "\n%d Corrected bytes, %d unrecoverable blocks\n",
  200. prz->corrected_bytes, prz->bad_blocks);
  201. else
  202. ret = snprintf(str, len, "\nNo errors detected\n");
  203. return ret;
  204. }
  205. static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
  206. const void *s, unsigned int start, unsigned int count)
  207. {
  208. struct persistent_ram_buffer *buffer = prz->buffer;
  209. memcpy(buffer->data + start, s, count);
  210. persistent_ram_update_ecc(prz, start, count);
  211. }
  212. void persistent_ram_save_old(struct persistent_ram_zone *prz)
  213. {
  214. struct persistent_ram_buffer *buffer = prz->buffer;
  215. size_t size = buffer_size(prz);
  216. size_t start = buffer_start(prz);
  217. if (!size)
  218. return;
  219. if (!prz->old_log) {
  220. persistent_ram_ecc_old(prz);
  221. prz->old_log = kmalloc(size, GFP_KERNEL);
  222. }
  223. if (!prz->old_log) {
  224. pr_err("persistent_ram: failed to allocate buffer\n");
  225. return;
  226. }
  227. prz->old_log_size = size;
  228. memcpy(prz->old_log, &buffer->data[start], size - start);
  229. memcpy(prz->old_log + size - start, &buffer->data[0], start);
  230. }
  231. int notrace persistent_ram_write(struct persistent_ram_zone *prz,
  232. const void *s, unsigned int count)
  233. {
  234. int rem;
  235. int c = count;
  236. size_t start;
  237. if (unlikely(c > prz->buffer_size)) {
  238. s += c - prz->buffer_size;
  239. c = prz->buffer_size;
  240. }
  241. buffer_size_add(prz, c);
  242. start = buffer_start_add(prz, c);
  243. rem = prz->buffer_size - start;
  244. if (unlikely(rem < c)) {
  245. persistent_ram_update(prz, s, start, rem);
  246. s += rem;
  247. c -= rem;
  248. start = 0;
  249. }
  250. persistent_ram_update(prz, s, start, c);
  251. persistent_ram_update_header_ecc(prz);
  252. return count;
  253. }
  254. size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
  255. {
  256. return prz->old_log_size;
  257. }
  258. void *persistent_ram_old(struct persistent_ram_zone *prz)
  259. {
  260. return prz->old_log;
  261. }
  262. void persistent_ram_free_old(struct persistent_ram_zone *prz)
  263. {
  264. kfree(prz->old_log);
  265. prz->old_log = NULL;
  266. prz->old_log_size = 0;
  267. }
  268. void persistent_ram_zap(struct persistent_ram_zone *prz)
  269. {
  270. atomic_set(&prz->buffer->start, 0);
  271. atomic_set(&prz->buffer->size, 0);
  272. persistent_ram_update_header_ecc(prz);
  273. }
  274. static void *persistent_ram_vmap(phys_addr_t start, size_t size)
  275. {
  276. struct page **pages;
  277. phys_addr_t page_start;
  278. unsigned int page_count;
  279. pgprot_t prot;
  280. unsigned int i;
  281. void *vaddr;
  282. page_start = start - offset_in_page(start);
  283. page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
  284. prot = pgprot_noncached(PAGE_KERNEL);
  285. pages = kmalloc(sizeof(struct page *) * page_count, GFP_KERNEL);
  286. if (!pages) {
  287. pr_err("%s: Failed to allocate array for %u pages\n", __func__,
  288. page_count);
  289. return NULL;
  290. }
  291. for (i = 0; i < page_count; i++) {
  292. phys_addr_t addr = page_start + i * PAGE_SIZE;
  293. pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
  294. }
  295. vaddr = vmap(pages, page_count, VM_MAP, prot);
  296. kfree(pages);
  297. return vaddr;
  298. }
  299. static void *persistent_ram_iomap(phys_addr_t start, size_t size)
  300. {
  301. if (!request_mem_region(start, size, "persistent_ram")) {
  302. pr_err("request mem region (0x%llx@0x%llx) failed\n",
  303. (unsigned long long)size, (unsigned long long)start);
  304. return NULL;
  305. }
  306. return ioremap(start, size);
  307. }
  308. static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
  309. struct persistent_ram_zone *prz)
  310. {
  311. prz->paddr = start;
  312. prz->size = size;
  313. if (pfn_valid(start >> PAGE_SHIFT))
  314. prz->vaddr = persistent_ram_vmap(start, size);
  315. else
  316. prz->vaddr = persistent_ram_iomap(start, size);
  317. if (!prz->vaddr) {
  318. pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
  319. (unsigned long long)size, (unsigned long long)start);
  320. return -ENOMEM;
  321. }
  322. prz->buffer = prz->vaddr + offset_in_page(start);
  323. prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
  324. return 0;
  325. }
  326. static int __init persistent_ram_post_init(struct persistent_ram_zone *prz, bool ecc)
  327. {
  328. int ret;
  329. prz->ecc = ecc;
  330. ret = persistent_ram_init_ecc(prz, prz->buffer_size);
  331. if (ret)
  332. return ret;
  333. if (prz->buffer->sig == PERSISTENT_RAM_SIG) {
  334. if (buffer_size(prz) > prz->buffer_size ||
  335. buffer_start(prz) > buffer_size(prz))
  336. pr_info("persistent_ram: found existing invalid buffer,"
  337. " size %zu, start %zu\n",
  338. buffer_size(prz), buffer_start(prz));
  339. else {
  340. pr_info("persistent_ram: found existing buffer,"
  341. " size %zu, start %zu\n",
  342. buffer_size(prz), buffer_start(prz));
  343. persistent_ram_save_old(prz);
  344. return 0;
  345. }
  346. } else {
  347. pr_info("persistent_ram: no valid data in buffer"
  348. " (sig = 0x%08x)\n", prz->buffer->sig);
  349. }
  350. prz->buffer->sig = PERSISTENT_RAM_SIG;
  351. persistent_ram_zap(prz);
  352. return 0;
  353. }
  354. void persistent_ram_free(struct persistent_ram_zone *prz)
  355. {
  356. if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
  357. vunmap(prz->vaddr);
  358. } else {
  359. iounmap(prz->vaddr);
  360. release_mem_region(prz->paddr, prz->size);
  361. }
  362. persistent_ram_free_old(prz);
  363. kfree(prz);
  364. }
  365. struct persistent_ram_zone * __init persistent_ram_new(phys_addr_t start,
  366. size_t size,
  367. bool ecc)
  368. {
  369. struct persistent_ram_zone *prz;
  370. int ret = -ENOMEM;
  371. prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
  372. if (!prz) {
  373. pr_err("persistent_ram: failed to allocate persistent ram zone\n");
  374. goto err;
  375. }
  376. ret = persistent_ram_buffer_map(start, size, prz);
  377. if (ret)
  378. goto err;
  379. persistent_ram_post_init(prz, ecc);
  380. return prz;
  381. err:
  382. kfree(prz);
  383. return ERR_PTR(ret);
  384. }
  385. #ifndef MODULE
  386. static int __init persistent_ram_buffer_init(const char *name,
  387. struct persistent_ram_zone *prz)
  388. {
  389. int i;
  390. struct persistent_ram *ram;
  391. struct persistent_ram_descriptor *desc;
  392. phys_addr_t start;
  393. list_for_each_entry(ram, &persistent_ram_list, node) {
  394. start = ram->start;
  395. for (i = 0; i < ram->num_descs; i++) {
  396. desc = &ram->descs[i];
  397. if (!strcmp(desc->name, name))
  398. return persistent_ram_buffer_map(start,
  399. desc->size, prz);
  400. start += desc->size;
  401. }
  402. }
  403. return -EINVAL;
  404. }
  405. static __init
  406. struct persistent_ram_zone *__persistent_ram_init(struct device *dev, bool ecc)
  407. {
  408. struct persistent_ram_zone *prz;
  409. int ret = -ENOMEM;
  410. prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
  411. if (!prz) {
  412. pr_err("persistent_ram: failed to allocate persistent ram zone\n");
  413. goto err;
  414. }
  415. ret = persistent_ram_buffer_init(dev_name(dev), prz);
  416. if (ret) {
  417. pr_err("persistent_ram: failed to initialize buffer\n");
  418. goto err;
  419. }
  420. persistent_ram_post_init(prz, ecc);
  421. return prz;
  422. err:
  423. kfree(prz);
  424. return ERR_PTR(ret);
  425. }
  426. struct persistent_ram_zone * __init
  427. persistent_ram_init_ringbuffer(struct device *dev, bool ecc)
  428. {
  429. return __persistent_ram_init(dev, ecc);
  430. }
  431. int __init persistent_ram_early_init(struct persistent_ram *ram)
  432. {
  433. int ret;
  434. ret = memblock_reserve(ram->start, ram->size);
  435. if (ret) {
  436. pr_err("Failed to reserve persistent memory from %08lx-%08lx\n",
  437. (long)ram->start, (long)(ram->start + ram->size - 1));
  438. return ret;
  439. }
  440. list_add_tail(&ram->node, &persistent_ram_list);
  441. pr_info("Initialized persistent memory from %08lx-%08lx\n",
  442. (long)ram->start, (long)(ram->start + ram->size - 1));
  443. return 0;
  444. }
  445. #endif