Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // originally in linux/arch/arm/plat-s3c24xx/pm.c
0004 //
0005 // Copyright (c) 2004-2008 Simtec Electronics
0006 //  http://armlinux.simtec.co.uk
0007 //  Ben Dooks <ben@simtec.co.uk>
0008 //
0009 // S3C Power Mangament - suspend/resume memory corruption check.
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/suspend.h>
0013 #include <linux/init.h>
0014 #include <linux/crc32.h>
0015 #include <linux/ioport.h>
0016 #include <linux/slab.h>
0017 
0018 #include <linux/soc/samsung/s3c-pm.h>
0019 
0020 #if CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE < 1
0021 #error CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE must be a positive non-zero value
0022 #endif
0023 
0024 /* suspend checking code...
0025  *
0026  * this next area does a set of crc checks over all the installed
0027  * memory, so the system can verify if the resume was ok.
0028  *
0029  * CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
0030  * increasing it will mean that the area corrupted will be less easy to spot,
0031  * and reducing the size will cause the CRC save area to grow
0032 */
0033 
0034 #define CHECK_CHUNKSIZE (CONFIG_SAMSUNG_PM_CHECK_CHUNKSIZE * 1024)
0035 
0036 static u32 crc_size;    /* size needed for the crc block */
0037 static u32 *crcs;   /* allocated over suspend/resume */
0038 
0039 typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
0040 
0041 /* s3c_pm_run_res
0042  *
0043  * go through the given resource list, and look for system ram
0044 */
0045 
0046 static void s3c_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
0047 {
0048     while (ptr != NULL) {
0049         if (ptr->child != NULL)
0050             s3c_pm_run_res(ptr->child, fn, arg);
0051 
0052         if ((ptr->flags & IORESOURCE_SYSTEM_RAM)
0053                 == IORESOURCE_SYSTEM_RAM) {
0054             S3C_PMDBG("Found system RAM at %08lx..%08lx\n",
0055                   (unsigned long)ptr->start,
0056                   (unsigned long)ptr->end);
0057             arg = (fn)(ptr, arg);
0058         }
0059 
0060         ptr = ptr->sibling;
0061     }
0062 }
0063 
0064 static void s3c_pm_run_sysram(run_fn_t fn, u32 *arg)
0065 {
0066     s3c_pm_run_res(&iomem_resource, fn, arg);
0067 }
0068 
0069 static u32 *s3c_pm_countram(struct resource *res, u32 *val)
0070 {
0071     u32 size = (u32)resource_size(res);
0072 
0073     size += CHECK_CHUNKSIZE-1;
0074     size /= CHECK_CHUNKSIZE;
0075 
0076     S3C_PMDBG("Area %08lx..%08lx, %d blocks\n",
0077           (unsigned long)res->start, (unsigned long)res->end, size);
0078 
0079     *val += size * sizeof(u32);
0080     return val;
0081 }
0082 
0083 /* s3c_pm_prepare_check
0084  *
0085  * prepare the necessary information for creating the CRCs. This
0086  * must be done before the final save, as it will require memory
0087  * allocating, and thus touching bits of the kernel we do not
0088  * know about.
0089 */
0090 
0091 void s3c_pm_check_prepare(void)
0092 {
0093     crc_size = 0;
0094 
0095     s3c_pm_run_sysram(s3c_pm_countram, &crc_size);
0096 
0097     S3C_PMDBG("s3c_pm_prepare_check: %u checks needed\n", crc_size);
0098 
0099     crcs = kmalloc(crc_size+4, GFP_KERNEL);
0100     if (crcs == NULL)
0101         printk(KERN_ERR "Cannot allocated CRC save area\n");
0102 }
0103 
0104 static u32 *s3c_pm_makecheck(struct resource *res, u32 *val)
0105 {
0106     unsigned long addr, left;
0107 
0108     for (addr = res->start; addr < res->end;
0109          addr += CHECK_CHUNKSIZE) {
0110         left = res->end - addr;
0111 
0112         if (left > CHECK_CHUNKSIZE)
0113             left = CHECK_CHUNKSIZE;
0114 
0115         *val = crc32_le(~0, phys_to_virt(addr), left);
0116         val++;
0117     }
0118 
0119     return val;
0120 }
0121 
0122 /* s3c_pm_check_store
0123  *
0124  * compute the CRC values for the memory blocks before the final
0125  * sleep.
0126 */
0127 
0128 void s3c_pm_check_store(void)
0129 {
0130     if (crcs != NULL)
0131         s3c_pm_run_sysram(s3c_pm_makecheck, crcs);
0132 }
0133 
0134 /* in_region
0135  *
0136  * return TRUE if the area defined by ptr..ptr+size contains the
0137  * what..what+whatsz
0138 */
0139 
0140 static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
0141 {
0142     if ((what+whatsz) < ptr)
0143         return 0;
0144 
0145     if (what > (ptr+size))
0146         return 0;
0147 
0148     return 1;
0149 }
0150 
0151 /**
0152  * s3c_pm_runcheck() - helper to check a resource on restore.
0153  * @res: The resource to check
0154  * @val: Pointer to list of CRC32 values to check.
0155  *
0156  * Called from the s3c_pm_check_restore() via s3c_pm_run_sysram(), this
0157  * function runs the given memory resource checking it against the stored
0158  * CRC to ensure that memory is restored. The function tries to skip as
0159  * many of the areas used during the suspend process.
0160  */
0161 static u32 *s3c_pm_runcheck(struct resource *res, u32 *val)
0162 {
0163     unsigned long addr;
0164     unsigned long left;
0165     void *stkpage;
0166     void *ptr;
0167     u32 calc;
0168 
0169     stkpage = (void *)((u32)&calc & ~PAGE_MASK);
0170 
0171     for (addr = res->start; addr < res->end;
0172          addr += CHECK_CHUNKSIZE) {
0173         left = res->end - addr;
0174 
0175         if (left > CHECK_CHUNKSIZE)
0176             left = CHECK_CHUNKSIZE;
0177 
0178         ptr = phys_to_virt(addr);
0179 
0180         if (in_region(ptr, left, stkpage, 4096)) {
0181             S3C_PMDBG("skipping %08lx, has stack in\n", addr);
0182             goto skip_check;
0183         }
0184 
0185         if (in_region(ptr, left, crcs, crc_size)) {
0186             S3C_PMDBG("skipping %08lx, has crc block in\n", addr);
0187             goto skip_check;
0188         }
0189 
0190         /* calculate and check the checksum */
0191 
0192         calc = crc32_le(~0, ptr, left);
0193         if (calc != *val) {
0194             printk(KERN_ERR "Restore CRC error at "
0195                    "%08lx (%08x vs %08x)\n", addr, calc, *val);
0196 
0197             S3C_PMDBG("Restore CRC error at %08lx (%08x vs %08x)\n",
0198                 addr, calc, *val);
0199         }
0200 
0201     skip_check:
0202         val++;
0203     }
0204 
0205     return val;
0206 }
0207 
0208 /**
0209  * s3c_pm_check_restore() - memory check called on resume
0210  *
0211  * check the CRCs after the restore event and free the memory used
0212  * to hold them
0213 */
0214 void s3c_pm_check_restore(void)
0215 {
0216     if (crcs != NULL)
0217         s3c_pm_run_sysram(s3c_pm_runcheck, crcs);
0218 }
0219 
0220 /**
0221  * s3c_pm_check_cleanup() - free memory resources
0222  *
0223  * Free the resources that where allocated by the suspend
0224  * memory check code. We do this separately from the
0225  * s3c_pm_check_restore() function as we cannot call any
0226  * functions that might sleep during that resume.
0227  */
0228 void s3c_pm_check_cleanup(void)
0229 {
0230     kfree(crcs);
0231     crcs = NULL;
0232 }
0233