]>
Commit | Line | Data |
---|---|---|
3a60a9f5 A |
1 | /* |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
3a60a9f5 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
3a60a9f5 A |
27 | */ |
28 | ||
29 | #include <kern/machine.h> | |
30 | #include <kern/misc_protos.h> | |
31 | #include <kern/thread.h> | |
32 | #include <kern/processor.h> | |
33 | #include <kern/kalloc.h> | |
34 | #include <mach/machine.h> | |
35 | #include <mach/processor_info.h> | |
36 | #include <mach/mach_types.h> | |
37 | #include <i386/pmap.h> | |
38 | #include <kern/cpu_data.h> | |
39 | #include <IOKit/IOPlatformExpert.h> | |
0c530ab8 A |
40 | |
41 | #include <pexpert/i386/efi.h> | |
3a60a9f5 A |
42 | |
43 | #include <IOKit/IOHibernatePrivate.h> | |
44 | #include <vm/vm_page.h> | |
b0d623f7 | 45 | #include <i386/i386_lowmem.h> |
21362eb3 | 46 | |
0c530ab8 | 47 | #define MAX_BANKS 32 |
6601e61a | 48 | |
0c530ab8 | 49 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
6601e61a | 50 | |
3a60a9f5 A |
51 | hibernate_page_list_t * |
52 | hibernate_page_list_allocate(void) | |
53 | { | |
0c530ab8 | 54 | ppnum_t base, num; |
3a60a9f5 | 55 | vm_size_t size; |
0c530ab8 | 56 | uint32_t bank, num_banks; |
3a60a9f5 A |
57 | uint32_t pages, page_count; |
58 | hibernate_page_list_t * list; | |
59 | hibernate_bitmap_t * bitmap; | |
6601e61a | 60 | |
0c530ab8 A |
61 | EfiMemoryRange * mptr; |
62 | uint32_t mcount, msize, i; | |
63 | hibernate_bitmap_t dram_ranges[MAX_BANKS]; | |
64 | boot_args * args = (boot_args *) PE_state.bootArgs; | |
89b3af67 | 65 | |
b0d623f7 | 66 | mptr = (EfiMemoryRange *)ml_static_ptovirt(args->MemoryMap); |
0c530ab8 A |
67 | if (args->MemoryMapDescriptorSize == 0) |
68 | panic("Invalid memory map descriptor size"); | |
69 | msize = args->MemoryMapDescriptorSize; | |
70 | mcount = args->MemoryMapSize / msize; | |
4452a7af | 71 | |
0c530ab8 A |
72 | num_banks = 0; |
73 | for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) | |
6601e61a | 74 | { |
0c530ab8 A |
75 | base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT); |
76 | num = (ppnum_t) mptr->NumberOfPages; | |
77 | if (!num) | |
78 | continue; | |
79 | ||
80 | switch (mptr->Type) | |
81 | { | |
82 | // any kind of dram | |
83 | case kEfiLoaderCode: | |
84 | case kEfiLoaderData: | |
85 | case kEfiBootServicesCode: | |
86 | case kEfiBootServicesData: | |
87 | case kEfiConventionalMemory: | |
88 | case kEfiACPIReclaimMemory: | |
89 | case kEfiACPIMemoryNVS: | |
90 | case kEfiPalCode: | |
91 | ||
b0d623f7 A |
92 | for (bank = 0; bank < num_banks; bank++) |
93 | { | |
94 | if (dram_ranges[bank].first_page <= base) | |
95 | continue; | |
96 | if ((base + num) == dram_ranges[bank].first_page) | |
97 | { | |
98 | dram_ranges[bank].first_page = base; | |
99 | num = 0; | |
100 | } | |
101 | break; | |
102 | } | |
103 | if (!num) break; | |
104 | ||
105 | if (bank && (base == (1 + dram_ranges[bank - 1].last_page))) | |
106 | bank--; | |
107 | else | |
0c530ab8 A |
108 | { |
109 | num_banks++; | |
b0d623f7 A |
110 | if (num_banks >= MAX_BANKS) break; |
111 | bcopy(&dram_ranges[bank], | |
112 | &dram_ranges[bank + 1], | |
113 | (num_banks - bank - 1) * sizeof(hibernate_bitmap_t)); | |
114 | dram_ranges[bank].first_page = base; | |
0c530ab8 | 115 | } |
b0d623f7 | 116 | dram_ranges[bank].last_page = base + num - 1; |
0c530ab8 A |
117 | break; |
118 | ||
119 | // runtime services will be restarted, so no save | |
120 | case kEfiRuntimeServicesCode: | |
121 | case kEfiRuntimeServicesData: | |
122 | // non dram | |
123 | case kEfiReservedMemoryType: | |
124 | case kEfiUnusableMemory: | |
125 | case kEfiMemoryMappedIO: | |
126 | case kEfiMemoryMappedIOPortSpace: | |
127 | default: | |
128 | break; | |
129 | } | |
6601e61a | 130 | } |
4452a7af | 131 | |
0c530ab8 A |
132 | if (num_banks >= MAX_BANKS) |
133 | return (NULL); | |
134 | ||
135 | // size the hibernation bitmap | |
136 | ||
3a60a9f5 A |
137 | size = sizeof(hibernate_page_list_t); |
138 | page_count = 0; | |
0c530ab8 A |
139 | for (bank = 0; bank < num_banks; bank++) { |
140 | pages = dram_ranges[bank].last_page + 1 - dram_ranges[bank].first_page; | |
3a60a9f5 A |
141 | page_count += pages; |
142 | size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t); | |
143 | } | |
144 | ||
145 | list = (hibernate_page_list_t *)kalloc(size); | |
146 | if (!list) | |
147 | return (list); | |
148 | ||
b0d623f7 | 149 | list->list_size = (uint32_t)size; |
3a60a9f5 | 150 | list->page_count = page_count; |
0c530ab8 A |
151 | list->bank_count = num_banks; |
152 | ||
153 | // convert to hibernation bitmap. | |
3a60a9f5 | 154 | |
3a60a9f5 | 155 | bitmap = &list->bank_bitmap[0]; |
0c530ab8 A |
156 | for (bank = 0; bank < num_banks; bank++) |
157 | { | |
158 | bitmap->first_page = dram_ranges[bank].first_page; | |
159 | bitmap->last_page = dram_ranges[bank].last_page; | |
3a60a9f5 A |
160 | bitmap->bitmapwords = (bitmap->last_page + 1 |
161 | - bitmap->first_page + 31) >> 5; | |
0c530ab8 A |
162 | kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", bank, |
163 | bitmap->first_page, | |
164 | bitmap->last_page); | |
3a60a9f5 A |
165 | bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords]; |
166 | } | |
167 | ||
3a60a9f5 A |
168 | return (list); |
169 | } | |
170 | ||
0c530ab8 A |
171 | // mark pages not to be saved, but available for scratch usage during restore |
172 | ||
173 | void | |
174 | hibernate_page_list_setall_machine( __unused hibernate_page_list_t * page_list, | |
175 | __unused hibernate_page_list_t * page_list_wired, | |
176 | __unused uint32_t * pagesOut) | |
177 | { | |
178 | } | |
179 | ||
180 | // mark pages not to be saved and not for scratch usage during restore | |
5d5c5d0d | 181 | void |
0c530ab8 A |
182 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list, |
183 | hibernate_page_list_t * page_list_wired, | |
184 | uint32_t * pagesOut) | |
5d5c5d0d | 185 | { |
0c530ab8 A |
186 | boot_args * args = (boot_args *) PE_state.bootArgs; |
187 | ||
b0d623f7 | 188 | #if !defined(x86_64) |
0c530ab8 A |
189 | hibernate_set_page_state(page_list, page_list_wired, |
190 | I386_HIB_PAGETABLE, I386_HIB_PAGETABLE_COUNT, | |
191 | kIOHibernatePageStateFree); | |
192 | *pagesOut -= I386_HIB_PAGETABLE_COUNT; | |
b0d623f7 | 193 | #endif |
0c530ab8 A |
194 | |
195 | if (args->efiRuntimeServicesPageStart) | |
196 | { | |
197 | hibernate_set_page_state(page_list, page_list_wired, | |
198 | args->efiRuntimeServicesPageStart, args->efiRuntimeServicesPageCount, | |
199 | kIOHibernatePageStateFree); | |
200 | *pagesOut -= args->efiRuntimeServicesPageCount; | |
3a60a9f5 A |
201 | } |
202 | } | |
203 | ||
204 | kern_return_t | |
205 | hibernate_processor_setup(IOHibernateImageHeader * header) | |
206 | { | |
0c530ab8 A |
207 | boot_args * args = (boot_args *) PE_state.bootArgs; |
208 | ||
209 | cpu_datap(0)->cpu_hibernate = 1; | |
3a60a9f5 | 210 | header->processorFlags = 0; |
0c530ab8 A |
211 | |
212 | header->runtimePages = args->efiRuntimeServicesPageStart; | |
213 | header->runtimePageCount = args->efiRuntimeServicesPageCount; | |
b0d623f7 A |
214 | if (args->Version == kBootArgsVersion1 && args->Revision >= kBootArgsRevision1_5) { |
215 | header->runtimeVirtualPages = args->efiRuntimeServicesVirtualPageStart; | |
216 | } else { | |
217 | header->runtimeVirtualPages = 0; | |
218 | } | |
0c530ab8 | 219 | |
3a60a9f5 A |
220 | return (KERN_SUCCESS); |
221 | } | |
222 | ||
223 | void | |
224 | hibernate_vm_lock(void) | |
225 | { | |
0c530ab8 | 226 | if (current_cpu_datap()->cpu_hibernate) |
3a60a9f5 A |
227 | { |
228 | vm_page_lock_queues(); | |
b0d623f7 A |
229 | lck_mtx_lock(&vm_page_queue_free_lock); |
230 | ||
231 | if (vm_page_local_q) { | |
232 | uint32_t i; | |
233 | ||
234 | for (i = 0; i < vm_page_local_q_count; i++) { | |
235 | struct vpl *lq; | |
236 | ||
237 | lq = &vm_page_local_q[i].vpl_un.vpl; | |
238 | ||
239 | VPL_LOCK(&lq->vpl_lock); | |
240 | } | |
241 | } | |
3a60a9f5 A |
242 | } |
243 | } | |
244 | ||
245 | void | |
246 | hibernate_vm_unlock(void) | |
247 | { | |
0c530ab8 | 248 | if (current_cpu_datap()->cpu_hibernate) |
3a60a9f5 | 249 | { |
b0d623f7 A |
250 | if (vm_page_local_q) { |
251 | uint32_t i; | |
252 | ||
253 | for (i = 0; i < vm_page_local_q_count; i++) { | |
254 | struct vpl *lq; | |
255 | ||
256 | lq = &vm_page_local_q[i].vpl_un.vpl; | |
257 | ||
258 | VPL_UNLOCK(&lq->vpl_lock); | |
259 | } | |
260 | } | |
261 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3a60a9f5 A |
262 | vm_page_unlock_queues(); |
263 | } | |
264 | } |