nvs.c 3.2 KB

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