1 // SPDX-License-Identifier: GPL-2.0-only
3 * nvs.c - Routines for saving and restoring ACPI NVS memory region
5 * Copyright (C) 2008-2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
8 #define pr_fmt(fmt) "ACPI: PM: " fmt
11 #include <linux/kernel.h>
12 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
19 /* ACPI NVS regions, APEI may use it */
24 struct list_head node
;
27 static LIST_HEAD(nvs_region_list
);
29 #ifdef CONFIG_ACPI_SLEEP
30 static int suspend_nvs_register(unsigned long start
, unsigned long size
);
32 static inline int suspend_nvs_register(unsigned long a
, unsigned long b
)
38 int acpi_nvs_register(__u64 start
, __u64 size
)
40 struct nvs_region
*region
;
42 region
= kmalloc(sizeof(*region
), GFP_KERNEL
);
45 region
->phys_start
= start
;
47 list_add_tail(®ion
->node
, &nvs_region_list
);
49 return suspend_nvs_register(start
, size
);
52 int acpi_nvs_for_each_region(int (*func
)(__u64 start
, __u64 size
, void *data
),
56 struct nvs_region
*region
;
58 list_for_each_entry(region
, &nvs_region_list
, node
) {
59 rc
= func(region
->phys_start
, region
->size
, data
);
68 #ifdef CONFIG_ACPI_SLEEP
70 * Platforms, like ACPI, may want us to save some memory used by them during
71 * suspend and to restore the contents of this memory during the subsequent
72 * resume. The code below implements a mechanism allowing us to do that.
76 unsigned long phys_start
;
81 struct list_head node
;
84 static LIST_HEAD(nvs_list
);
87 * suspend_nvs_register - register platform NVS memory region to save
88 * @start: Physical address of the region.
89 * @size: Size of the region.
91 * The NVS region need not be page-aligned (both ends) and we arrange
92 * things so that the data from page-aligned addresses in this region will
93 * be copied into separate RAM pages.
95 static int suspend_nvs_register(unsigned long start
, unsigned long size
)
97 struct nvs_page
*entry
, *next
;
99 pr_info("Registering ACPI NVS region [mem %#010lx-%#010lx] (%ld bytes)\n",
100 start
, start
+ size
- 1, size
);
103 unsigned int nr_bytes
;
105 entry
= kzalloc(sizeof(struct nvs_page
), GFP_KERNEL
);
109 list_add_tail(&entry
->node
, &nvs_list
);
110 entry
->phys_start
= start
;
111 nr_bytes
= PAGE_SIZE
- (start
& ~PAGE_MASK
);
112 entry
->size
= (size
< nr_bytes
) ? size
: nr_bytes
;
114 start
+= entry
->size
;
120 list_for_each_entry_safe(entry
, next
, &nvs_list
, node
) {
121 list_del(&entry
->node
);
128 * suspend_nvs_free - free data pages allocated for saving NVS regions
130 void suspend_nvs_free(void)
132 struct nvs_page
*entry
;
134 list_for_each_entry(entry
, &nvs_list
, node
)
136 free_page((unsigned long)entry
->data
);
140 iounmap(entry
->kaddr
);
141 entry
->unmap
= false;
143 acpi_os_unmap_iomem(entry
->kaddr
,
152 * suspend_nvs_alloc - allocate memory necessary for saving NVS regions
154 int suspend_nvs_alloc(void)
156 struct nvs_page
*entry
;
158 list_for_each_entry(entry
, &nvs_list
, node
) {
159 entry
->data
= (void *)__get_free_page(GFP_KERNEL
);
169 * suspend_nvs_save - save NVS memory regions
171 int suspend_nvs_save(void)
173 struct nvs_page
*entry
;
175 pr_info("Saving platform NVS memory\n");
177 list_for_each_entry(entry
, &nvs_list
, node
)
179 unsigned long phys
= entry
->phys_start
;
180 unsigned int size
= entry
->size
;
182 entry
->kaddr
= acpi_os_get_iomem(phys
, size
);
184 entry
->kaddr
= acpi_os_ioremap(phys
, size
);
185 entry
->unmap
= !!entry
->kaddr
;
191 memcpy(entry
->data
, entry
->kaddr
, entry
->size
);
198 * suspend_nvs_restore - restore NVS memory regions
200 * This function is going to be called with interrupts disabled, so it
201 * cannot iounmap the virtual addresses used to access the NVS region.
203 void suspend_nvs_restore(void)
205 struct nvs_page
*entry
;
207 pr_info("Restoring platform NVS memory\n");
209 list_for_each_entry(entry
, &nvs_list
, node
)
211 memcpy(entry
->kaddr
, entry
->data
, entry
->size
);