ram_core.c 13 KB

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