pm-check.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. ptr->start, ptr->end);
  49. arg = (fn)(ptr, arg);
  50. }
  51. ptr = ptr->sibling;
  52. }
  53. }
  54. static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg)
  55. {
  56. s3c_pm_run_res(&iomem_resource, fn, arg);
  57. }
  58. static u32 *s3c_pm_countram(struct resource *res, u32 *val)
  59. {
  60. u32 size = (u32)(res->end - res->start)+1;
  61. size += CHECK_CHUNKSIZE-1;
  62. size /= CHECK_CHUNKSIZE;
  63. S3C_PMDBG("Area %08lx..%08lx, %d blocks\n", res->start, res->end, size);
  64. *val += size * sizeof(u32);
  65. return val;
  66. }
  67. /* s3c_pm_prepare_check
  68. *
  69. * prepare the necessary information for creating the CRCs. This
  70. * must be done before the final save, as it will require memory
  71. * allocating, and thus touching bits of the kernel we do not
  72. * know about.
  73. */
  74. void s3c_pm_check_prepare(void)
  75. {
  76. crc_size = 0;
  77. s3c_pm_run_sysram(s3c_pm_countram, &crc_size);
  78. S3C_PMDBG("s3c_pm_prepare_check: %u checks needed\n", crc_size);
  79. crcs = kmalloc(crc_size+4, GFP_KERNEL);
  80. if (crcs == NULL)
  81. printk(KERN_ERR "Cannot allocated CRC save area\n");
  82. }
  83. static u32 *s3c_pm_makecheck(struct resource *res, u32 *val)
  84. {
  85. unsigned long addr, left;
  86. for (addr = res->start; addr < res->end;
  87. addr += CHECK_CHUNKSIZE) {
  88. left = res->end - addr;
  89. if (left > CHECK_CHUNKSIZE)
  90. left = CHECK_CHUNKSIZE;
  91. *val = crc32_le(~0, phys_to_virt(addr), left);
  92. val++;
  93. }
  94. return val;
  95. }
  96. /* s3c_pm_check_store
  97. *
  98. * compute the CRC values for the memory blocks before the final
  99. * sleep.
  100. */
  101. void s3c_pm_check_store(void)
  102. {
  103. if (crcs != NULL)
  104. s3c_pm_run_sysram(s3c_pm_makecheck, crcs);
  105. }
  106. /* in_region
  107. *
  108. * return TRUE if the area defined by ptr..ptr+size contains the
  109. * what..what+whatsz
  110. */
  111. static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
  112. {
  113. if ((what+whatsz) < ptr)
  114. return 0;
  115. if (what > (ptr+size))
  116. return 0;
  117. return 1;
  118. }
  119. /**
  120. * s3c_pm_runcheck*() - helper to check a resource on restore.
  121. * @res: The resource to check
  122. * @vak: Pointer to list of CRC32 values to check.
  123. *
  124. * Called from the s3c_pm_check_restore() via s3c_pm_run_sysram(), this
  125. * function runs the given memory resource checking it against the stored
  126. * CRC to ensure that memory is restored. The function tries to skip as
  127. * many of the areas used during the suspend process.
  128. */
  129. static u32 *s3c_pm_runcheck(struct resource *res, u32 *val)
  130. {
  131. void *save_at = phys_to_virt(s3c_sleep_save_phys);
  132. unsigned long addr;
  133. unsigned long left;
  134. void *ptr;
  135. u32 calc;
  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, crcs, crc_size)) {
  143. S3C_PMDBG("skipping %08lx, has crc block in\n", addr);
  144. goto skip_check;
  145. }
  146. if (in_region(ptr, left, save_at, 32*4 )) {
  147. S3C_PMDBG("skipping %08lx, has save 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. kfree(crcs);
  174. crcs = NULL;
  175. }
  176. }