]>
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 | |
0b4c1975 A |
47 | extern ppnum_t max_ppnum; |
48 | ||
0c530ab8 | 49 | #define MAX_BANKS 32 |
6601e61a | 50 | |
0c530ab8 | 51 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
6601e61a | 52 | |
3a60a9f5 A |
53 | hibernate_page_list_t * |
54 | hibernate_page_list_allocate(void) | |
55 | { | |
0c530ab8 | 56 | ppnum_t base, num; |
3a60a9f5 | 57 | vm_size_t size; |
0c530ab8 | 58 | uint32_t bank, num_banks; |
3a60a9f5 A |
59 | uint32_t pages, page_count; |
60 | hibernate_page_list_t * list; | |
61 | hibernate_bitmap_t * bitmap; | |
6601e61a | 62 | |
0c530ab8 A |
63 | EfiMemoryRange * mptr; |
64 | uint32_t mcount, msize, i; | |
65 | hibernate_bitmap_t dram_ranges[MAX_BANKS]; | |
66 | boot_args * args = (boot_args *) PE_state.bootArgs; | |
89b3af67 | 67 | |
b0d623f7 | 68 | mptr = (EfiMemoryRange *)ml_static_ptovirt(args->MemoryMap); |
0c530ab8 A |
69 | if (args->MemoryMapDescriptorSize == 0) |
70 | panic("Invalid memory map descriptor size"); | |
71 | msize = args->MemoryMapDescriptorSize; | |
72 | mcount = args->MemoryMapSize / msize; | |
4452a7af | 73 | |
0c530ab8 A |
74 | num_banks = 0; |
75 | for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) | |
6601e61a | 76 | { |
0c530ab8 A |
77 | base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT); |
78 | num = (ppnum_t) mptr->NumberOfPages; | |
0b4c1975 A |
79 | |
80 | if (base > max_ppnum) | |
81 | continue; | |
82 | if ((base + num - 1) > max_ppnum) | |
83 | num = max_ppnum - base + 1; | |
0c530ab8 | 84 | if (!num) |
6d2010ae | 85 | continue; |
0c530ab8 A |
86 | |
87 | switch (mptr->Type) | |
88 | { | |
89 | // any kind of dram | |
90 | case kEfiLoaderCode: | |
91 | case kEfiLoaderData: | |
92 | case kEfiBootServicesCode: | |
93 | case kEfiBootServicesData: | |
94 | case kEfiConventionalMemory: | |
0c530ab8 A |
95 | case kEfiACPIMemoryNVS: |
96 | case kEfiPalCode: | |
97 | ||
b0d623f7 A |
98 | for (bank = 0; bank < num_banks; bank++) |
99 | { | |
100 | if (dram_ranges[bank].first_page <= base) | |
101 | continue; | |
102 | if ((base + num) == dram_ranges[bank].first_page) | |
103 | { | |
104 | dram_ranges[bank].first_page = base; | |
105 | num = 0; | |
106 | } | |
107 | break; | |
108 | } | |
109 | if (!num) break; | |
110 | ||
111 | if (bank && (base == (1 + dram_ranges[bank - 1].last_page))) | |
112 | bank--; | |
113 | else | |
0c530ab8 A |
114 | { |
115 | num_banks++; | |
b0d623f7 A |
116 | if (num_banks >= MAX_BANKS) break; |
117 | bcopy(&dram_ranges[bank], | |
118 | &dram_ranges[bank + 1], | |
119 | (num_banks - bank - 1) * sizeof(hibernate_bitmap_t)); | |
120 | dram_ranges[bank].first_page = base; | |
0c530ab8 | 121 | } |
b0d623f7 | 122 | dram_ranges[bank].last_page = base + num - 1; |
0c530ab8 A |
123 | break; |
124 | ||
125 | // runtime services will be restarted, so no save | |
126 | case kEfiRuntimeServicesCode: | |
127 | case kEfiRuntimeServicesData: | |
d1ecb069 A |
128 | // contents are volatile once the platform expert starts |
129 | case kEfiACPIReclaimMemory: | |
0c530ab8 A |
130 | // non dram |
131 | case kEfiReservedMemoryType: | |
132 | case kEfiUnusableMemory: | |
133 | case kEfiMemoryMappedIO: | |
134 | case kEfiMemoryMappedIOPortSpace: | |
135 | default: | |
136 | break; | |
137 | } | |
6601e61a | 138 | } |
4452a7af | 139 | |
0c530ab8 A |
140 | if (num_banks >= MAX_BANKS) |
141 | return (NULL); | |
142 | ||
143 | // size the hibernation bitmap | |
144 | ||
3a60a9f5 A |
145 | size = sizeof(hibernate_page_list_t); |
146 | page_count = 0; | |
0c530ab8 A |
147 | for (bank = 0; bank < num_banks; bank++) { |
148 | pages = dram_ranges[bank].last_page + 1 - dram_ranges[bank].first_page; | |
3a60a9f5 A |
149 | page_count += pages; |
150 | size += sizeof(hibernate_bitmap_t) + ((pages + 31) >> 5) * sizeof(uint32_t); | |
151 | } | |
152 | ||
153 | list = (hibernate_page_list_t *)kalloc(size); | |
154 | if (!list) | |
155 | return (list); | |
156 | ||
b0d623f7 | 157 | list->list_size = (uint32_t)size; |
3a60a9f5 | 158 | list->page_count = page_count; |
0c530ab8 A |
159 | list->bank_count = num_banks; |
160 | ||
161 | // convert to hibernation bitmap. | |
3a60a9f5 | 162 | |
3a60a9f5 | 163 | bitmap = &list->bank_bitmap[0]; |
0c530ab8 A |
164 | for (bank = 0; bank < num_banks; bank++) |
165 | { | |
166 | bitmap->first_page = dram_ranges[bank].first_page; | |
167 | bitmap->last_page = dram_ranges[bank].last_page; | |
3a60a9f5 A |
168 | bitmap->bitmapwords = (bitmap->last_page + 1 |
169 | - bitmap->first_page + 31) >> 5; | |
0c530ab8 A |
170 | kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", bank, |
171 | bitmap->first_page, | |
172 | bitmap->last_page); | |
3a60a9f5 A |
173 | bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords]; |
174 | } | |
175 | ||
3a60a9f5 A |
176 | return (list); |
177 | } | |
178 | ||
0c530ab8 A |
179 | // mark pages not to be saved, but available for scratch usage during restore |
180 | ||
181 | void | |
182 | hibernate_page_list_setall_machine( __unused hibernate_page_list_t * page_list, | |
183 | __unused hibernate_page_list_t * page_list_wired, | |
184 | __unused uint32_t * pagesOut) | |
185 | { | |
186 | } | |
187 | ||
188 | // mark pages not to be saved and not for scratch usage during restore | |
5d5c5d0d | 189 | void |
0c530ab8 A |
190 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list, |
191 | hibernate_page_list_t * page_list_wired, | |
192 | uint32_t * pagesOut) | |
5d5c5d0d | 193 | { |
0c530ab8 A |
194 | boot_args * args = (boot_args *) PE_state.bootArgs; |
195 | ||
b0d623f7 | 196 | #if !defined(x86_64) |
0c530ab8 A |
197 | hibernate_set_page_state(page_list, page_list_wired, |
198 | I386_HIB_PAGETABLE, I386_HIB_PAGETABLE_COUNT, | |
199 | kIOHibernatePageStateFree); | |
200 | *pagesOut -= I386_HIB_PAGETABLE_COUNT; | |
b0d623f7 | 201 | #endif |
0c530ab8 A |
202 | |
203 | if (args->efiRuntimeServicesPageStart) | |
204 | { | |
205 | hibernate_set_page_state(page_list, page_list_wired, | |
206 | args->efiRuntimeServicesPageStart, args->efiRuntimeServicesPageCount, | |
207 | kIOHibernatePageStateFree); | |
208 | *pagesOut -= args->efiRuntimeServicesPageCount; | |
3a60a9f5 A |
209 | } |
210 | } | |
211 | ||
212 | kern_return_t | |
213 | hibernate_processor_setup(IOHibernateImageHeader * header) | |
214 | { | |
0c530ab8 A |
215 | boot_args * args = (boot_args *) PE_state.bootArgs; |
216 | ||
217 | cpu_datap(0)->cpu_hibernate = 1; | |
3a60a9f5 | 218 | header->processorFlags = 0; |
0c530ab8 A |
219 | |
220 | header->runtimePages = args->efiRuntimeServicesPageStart; | |
221 | header->runtimePageCount = args->efiRuntimeServicesPageCount; | |
d1ecb069 | 222 | header->runtimeVirtualPages = args->efiRuntimeServicesVirtualPageStart; |
6d2010ae A |
223 | header->performanceDataStart = args->performanceDataStart; |
224 | header->performanceDataSize = args->performanceDataSize; | |
0c530ab8 | 225 | |
3a60a9f5 A |
226 | return (KERN_SUCCESS); |
227 | } | |
228 | ||
229 | void | |
230 | hibernate_vm_lock(void) | |
231 | { | |
0c530ab8 | 232 | if (current_cpu_datap()->cpu_hibernate) |
3a60a9f5 A |
233 | { |
234 | vm_page_lock_queues(); | |
b0d623f7 A |
235 | lck_mtx_lock(&vm_page_queue_free_lock); |
236 | ||
237 | if (vm_page_local_q) { | |
238 | uint32_t i; | |
239 | ||
240 | for (i = 0; i < vm_page_local_q_count; i++) { | |
241 | struct vpl *lq; | |
242 | ||
243 | lq = &vm_page_local_q[i].vpl_un.vpl; | |
244 | ||
245 | VPL_LOCK(&lq->vpl_lock); | |
246 | } | |
247 | } | |
3a60a9f5 A |
248 | } |
249 | } | |
250 | ||
251 | void | |
252 | hibernate_vm_unlock(void) | |
253 | { | |
0c530ab8 | 254 | if (current_cpu_datap()->cpu_hibernate) |
3a60a9f5 | 255 | { |
b0d623f7 A |
256 | if (vm_page_local_q) { |
257 | uint32_t i; | |
258 | ||
259 | for (i = 0; i < vm_page_local_q_count; i++) { | |
260 | struct vpl *lq; | |
261 | ||
262 | lq = &vm_page_local_q[i].vpl_un.vpl; | |
263 | ||
264 | VPL_UNLOCK(&lq->vpl_lock); | |
265 | } | |
266 | } | |
267 | lck_mtx_unlock(&vm_page_queue_free_lock); | |
3a60a9f5 A |
268 | vm_page_unlock_queues(); |
269 | } | |
270 | } |