| Cornelia Huck | fce2b111 | 2009-06-10 01:28:19 +0200 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/kernel/power/hibernate_nvs.c - Routines for handling NVS memory | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2008,2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. | 
 | 5 |  * | 
 | 6 |  * This file is released under the GPLv2. | 
 | 7 |  */ | 
 | 8 |  | 
 | 9 | #include <linux/io.h> | 
 | 10 | #include <linux/kernel.h> | 
 | 11 | #include <linux/list.h> | 
 | 12 | #include <linux/mm.h> | 
 | 13 | #include <linux/suspend.h> | 
 | 14 |  | 
 | 15 | /* | 
 | 16 |  * Platforms, like ACPI, may want us to save some memory used by them during | 
 | 17 |  * hibernation and to restore the contents of this memory during the subsequent | 
 | 18 |  * resume.  The code below implements a mechanism allowing us to do that. | 
 | 19 |  */ | 
 | 20 |  | 
 | 21 | struct nvs_page { | 
 | 22 | 	unsigned long phys_start; | 
 | 23 | 	unsigned int size; | 
 | 24 | 	void *kaddr; | 
 | 25 | 	void *data; | 
 | 26 | 	struct list_head node; | 
 | 27 | }; | 
 | 28 |  | 
 | 29 | static LIST_HEAD(nvs_list); | 
 | 30 |  | 
 | 31 | /** | 
 | 32 |  *	hibernate_nvs_register - register platform NVS memory region to save | 
 | 33 |  *	@start - physical address of the region | 
 | 34 |  *	@size - size of the region | 
 | 35 |  * | 
 | 36 |  *	The NVS region need not be page-aligned (both ends) and we arrange | 
 | 37 |  *	things so that the data from page-aligned addresses in this region will | 
 | 38 |  *	be copied into separate RAM pages. | 
 | 39 |  */ | 
 | 40 | int hibernate_nvs_register(unsigned long start, unsigned long size) | 
 | 41 | { | 
 | 42 | 	struct nvs_page *entry, *next; | 
 | 43 |  | 
 | 44 | 	while (size > 0) { | 
 | 45 | 		unsigned int nr_bytes; | 
 | 46 |  | 
 | 47 | 		entry = kzalloc(sizeof(struct nvs_page), GFP_KERNEL); | 
 | 48 | 		if (!entry) | 
 | 49 | 			goto Error; | 
 | 50 |  | 
 | 51 | 		list_add_tail(&entry->node, &nvs_list); | 
 | 52 | 		entry->phys_start = start; | 
 | 53 | 		nr_bytes = PAGE_SIZE - (start & ~PAGE_MASK); | 
 | 54 | 		entry->size = (size < nr_bytes) ? size : nr_bytes; | 
 | 55 |  | 
 | 56 | 		start += entry->size; | 
 | 57 | 		size -= entry->size; | 
 | 58 | 	} | 
 | 59 | 	return 0; | 
 | 60 |  | 
 | 61 |  Error: | 
 | 62 | 	list_for_each_entry_safe(entry, next, &nvs_list, node) { | 
 | 63 | 		list_del(&entry->node); | 
 | 64 | 		kfree(entry); | 
 | 65 | 	} | 
 | 66 | 	return -ENOMEM; | 
 | 67 | } | 
 | 68 |  | 
 | 69 | /** | 
 | 70 |  *	hibernate_nvs_free - free data pages allocated for saving NVS regions | 
 | 71 |  */ | 
 | 72 | void hibernate_nvs_free(void) | 
 | 73 | { | 
 | 74 | 	struct nvs_page *entry; | 
 | 75 |  | 
 | 76 | 	list_for_each_entry(entry, &nvs_list, node) | 
 | 77 | 		if (entry->data) { | 
 | 78 | 			free_page((unsigned long)entry->data); | 
 | 79 | 			entry->data = NULL; | 
 | 80 | 			if (entry->kaddr) { | 
 | 81 | 				iounmap(entry->kaddr); | 
 | 82 | 				entry->kaddr = NULL; | 
 | 83 | 			} | 
 | 84 | 		} | 
 | 85 | } | 
 | 86 |  | 
 | 87 | /** | 
 | 88 |  *	hibernate_nvs_alloc - allocate memory necessary for saving NVS regions | 
 | 89 |  */ | 
 | 90 | int hibernate_nvs_alloc(void) | 
 | 91 | { | 
 | 92 | 	struct nvs_page *entry; | 
 | 93 |  | 
 | 94 | 	list_for_each_entry(entry, &nvs_list, node) { | 
 | 95 | 		entry->data = (void *)__get_free_page(GFP_KERNEL); | 
 | 96 | 		if (!entry->data) { | 
 | 97 | 			hibernate_nvs_free(); | 
 | 98 | 			return -ENOMEM; | 
 | 99 | 		} | 
 | 100 | 	} | 
 | 101 | 	return 0; | 
 | 102 | } | 
 | 103 |  | 
 | 104 | /** | 
 | 105 |  *	hibernate_nvs_save - save NVS memory regions | 
 | 106 |  */ | 
 | 107 | void hibernate_nvs_save(void) | 
 | 108 | { | 
 | 109 | 	struct nvs_page *entry; | 
 | 110 |  | 
 | 111 | 	printk(KERN_INFO "PM: Saving platform NVS memory\n"); | 
 | 112 |  | 
 | 113 | 	list_for_each_entry(entry, &nvs_list, node) | 
 | 114 | 		if (entry->data) { | 
 | 115 | 			entry->kaddr = ioremap(entry->phys_start, entry->size); | 
 | 116 | 			memcpy(entry->data, entry->kaddr, entry->size); | 
 | 117 | 		} | 
 | 118 | } | 
 | 119 |  | 
 | 120 | /** | 
 | 121 |  *	hibernate_nvs_restore - restore NVS memory regions | 
 | 122 |  * | 
 | 123 |  *	This function is going to be called with interrupts disabled, so it | 
 | 124 |  *	cannot iounmap the virtual addresses used to access the NVS region. | 
 | 125 |  */ | 
 | 126 | void hibernate_nvs_restore(void) | 
 | 127 | { | 
 | 128 | 	struct nvs_page *entry; | 
 | 129 |  | 
 | 130 | 	printk(KERN_INFO "PM: Restoring platform NVS memory\n"); | 
 | 131 |  | 
 | 132 | 	list_for_each_entry(entry, &nvs_list, node) | 
 | 133 | 		if (entry->data) | 
 | 134 | 			memcpy(entry->kaddr, entry->data, entry->size); | 
 | 135 | } |