s3c-pm-check.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // originally in linux/arch/arm/plat-s3c24xx/pm.c
  4. //
  5. // Copyright (c) 2004-2008 Simtec Electronics
  6. // http://armlinux.simtec.co.uk
  7. // Ben Dooks <ben@simtec.co.uk>
  8. //
  9. // S3C Power Mangament - suspend/resume memory corruption check.
  10. #include <linux/kernel.h>
  11. #include <linux/suspend.h>
  12. #include <linux/init.h>
  13. #include <linux/crc32.h>
  14. #include <linux/ioport.h>
  15. #include <linux/slab.h>
  16. #include <linux/soc/samsung/s3c-pm.h>
  17. #if CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE < 1
  18. #error CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE must be a positive non-zero value
  19. #endif
  20. /* suspend checking code...
  21. *
  22. * this next area does a set of crc checks over all the installed
  23. * memory, so the system can verify if the resume was ok.
  24. *
  25. * CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
  26. * increasing it will mean that the area corrupted will be less easy to spot,
  27. * and reducing the size will cause the CRC save area to grow
  28. */
  29. #define CHECK_CHUNKSIZE (CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE * 1024)
  30. static u32 crc_size; /* size needed for the crc block */
  31. static u32 *crcs; /* allocated over suspend/resume */
  32. typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
  33. /* s3c_pm_run_res
  34. *
  35. * go through the given resource list, and look for system ram
  36. */
  37. static void s3c_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
  38. {
  39. while (ptr != NULL) {
  40. if (ptr->child != NULL)
  41. s3c_pm_run_res(ptr->child, fn, arg);
  42. if ((ptr->flags & IORESOURCE_SYSTEM_RAM)
  43. == IORESOURCE_SYSTEM_RAM) {
  44. S3C_PMDBG("Found system RAM at %08lx..%08lx\n",
  45. (unsigned long)ptr->start,
  46. (unsigned long)ptr->end);
  47. arg = (fn)(ptr, arg);
  48. }
  49. ptr = ptr->sibling;
  50. }
  51. }
  52. static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg)
  53. {
  54. s3c_pm_run_res(&iomem_resource, fn, arg);
  55. }
  56. static u32 *s3c_pm_countram(struct resource *res, u32 *val)
  57. {
  58. u32 size = (u32)resource_size(res);
  59. size += CHECK_CHUNKSIZE-1;
  60. size /= CHECK_CHUNKSIZE;
  61. S3C_PMDBG("Area %08lx..%08lx, %d blocks\n",
  62. (unsigned long)res->start, (unsigned long)res->end, size);
  63. *val += size * sizeof(u32);
  64. return val;
  65. }
  66. /* s3c_pm_prepare_check
  67. *
  68. * prepare the necessary information for creating the CRCs. This
  69. * must be done before the final save, as it will require memory
  70. * allocating, and thus touching bits of the kernel we do not
  71. * know about.
  72. */
  73. void s3c_pm_check_prepare(void)
  74. {
  75. crc_size = 0;
  76. s3c_pm_run_sysram(s3c_pm_countram, &crc_size);
  77. S3C_PMDBG("s3c_pm_prepare_check: %u checks needed\n", crc_size);
  78. crcs = kmalloc(crc_size+4, GFP_KERNEL);
  79. if (crcs == NULL)
  80. printk(KERN_ERR "Cannot allocated CRC save area\n");
  81. }
  82. static u32 *s3c_pm_makecheck(struct resource *res, u32 *val)
  83. {
  84. unsigned long addr, left;
  85. for (addr = res->start; addr < res->end;
  86. addr += CHECK_CHUNKSIZE) {
  87. left = res->end - addr;
  88. if (left > CHECK_CHUNKSIZE)
  89. left = CHECK_CHUNKSIZE;
  90. *val = crc32_le(~0, phys_to_virt(addr), left);
  91. val++;
  92. }
  93. return val;
  94. }
  95. /* s3c_pm_check_store
  96. *
  97. * compute the CRC values for the memory blocks before the final
  98. * sleep.
  99. */
  100. void s3c_pm_check_store(void)
  101. {
  102. if (crcs != NULL)
  103. s3c_pm_run_sysram(s3c_pm_makecheck, crcs);
  104. }
  105. /* in_region
  106. *
  107. * return TRUE if the area defined by ptr..ptr+size contains the
  108. * what..what+whatsz
  109. */
  110. static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
  111. {
  112. if ((what+whatsz) < ptr)
  113. return 0;
  114. if (what > (ptr+size))
  115. return 0;
  116. return 1;
  117. }
  118. /**
  119. * s3c_pm_runcheck() - helper to check a resource on restore.
  120. * @res: The resource to check
  121. * @vak: Pointer to list of CRC32 values to check.
  122. *
  123. * Called from the s3c_pm_check_restore() via s3c_pm_run_sysram(), this
  124. * function runs the given memory resource checking it against the stored
  125. * CRC to ensure that memory is restored. The function tries to skip as
  126. * many of the areas used during the suspend process.
  127. */
  128. static u32 *s3c_pm_runcheck(struct resource *res, u32 *val)
  129. {
  130. unsigned long addr;
  131. unsigned long left;
  132. void *stkpage;
  133. void *ptr;
  134. u32 calc;
  135. stkpage = (void *)((u32)&calc & ~PAGE_MASK);
  136. for (addr = res->start; addr < res->end;
  137. addr += CHECK_CHUNKSIZE) {
  138. left = res->end - addr;
  139. if (left > CHECK_CHUNKSIZE)
  140. left = CHECK_CHUNKSIZE;
  141. ptr = phys_to_virt(addr);
  142. if (in_region(ptr, left, stkpage, 4096)) {
  143. S3C_PMDBG("skipping %08lx, has stack in\n", addr);
  144. goto skip_check;
  145. }
  146. if (in_region(ptr, left, crcs, crc_size)) {
  147. S3C_PMDBG("skipping %08lx, has crc block in\n", addr);
  148. goto skip_check;
  149. }
  150. /* calculate and check the checksum */
  151. calc = crc32_le(~0, ptr, left);
  152. if (calc != *val) {
  153. printk(KERN_ERR "Restore CRC error at "
  154. "%08lx (%08x vs %08x)\n", addr, calc, *val);
  155. S3C_PMDBG("Restore CRC error at %08lx (%08x vs %08x)\n",
  156. addr, calc, *val);
  157. }
  158. skip_check:
  159. val++;
  160. }
  161. return val;
  162. }
  163. /**
  164. * s3c_pm_check_restore() - memory check called on resume
  165. *
  166. * check the CRCs after the restore event and free the memory used
  167. * to hold them
  168. */
  169. void s3c_pm_check_restore(void)
  170. {
  171. if (crcs != NULL)
  172. s3c_pm_run_sysram(s3c_pm_runcheck, crcs);
  173. }
  174. /**
  175. * s3c_pm_check_cleanup() - free memory resources
  176. *
  177. * Free the resources that where allocated by the suspend
  178. * memory check code. We do this separately from the
  179. * s3c_pm_check_restore() function as we cannot call any
  180. * functions that might sleep during that resume.
  181. */
  182. void s3c_pm_check_cleanup(void)
  183. {
  184. kfree(crcs);
  185. crcs = NULL;
  186. }