pm-check.c 5.5 KB

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