pm-check.c 5.6 KB

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