ram_core.c 11 KB

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