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