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