2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
41 #include <pexpert/i386/efi.h>
43 #include <IOKit/IOHibernatePrivate.h>
44 #include <vm/vm_page.h>
45 #include <i386/i386_lowmem.h>
47 extern ppnum_t max_ppnum
;
51 int hibernate_page_list_allocate_avoided
;
53 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
55 hibernate_page_list_t
*
56 hibernate_page_list_allocate(void)
60 uint32_t bank
, num_banks
;
61 uint32_t pages
, page_count
;
62 hibernate_page_list_t
* list
;
63 hibernate_bitmap_t
* bitmap
;
65 EfiMemoryRange
* mptr
;
66 uint32_t mcount
, msize
, i
;
67 hibernate_bitmap_t dram_ranges
[MAX_BANKS
];
68 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
70 mptr
= (EfiMemoryRange
*)ml_static_ptovirt(args
->MemoryMap
);
71 if (args
->MemoryMapDescriptorSize
== 0)
72 panic("Invalid memory map descriptor size");
73 msize
= args
->MemoryMapDescriptorSize
;
74 mcount
= args
->MemoryMapSize
/ msize
;
76 hibernate_page_list_allocate_avoided
= 0;
79 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
))
81 base
= (ppnum_t
) (mptr
->PhysicalStart
>> I386_PGSHIFT
);
82 num
= (ppnum_t
) mptr
->NumberOfPages
;
86 if ((base
+ num
- 1) > max_ppnum
)
87 num
= max_ppnum
- base
+ 1;
96 case kEfiBootServicesCode
:
97 case kEfiBootServicesData
:
98 case kEfiConventionalMemory
:
99 case kEfiACPIMemoryNVS
:
102 for (bank
= 0; bank
< num_banks
; bank
++)
104 if (dram_ranges
[bank
].first_page
<= base
)
106 if ((base
+ num
) == dram_ranges
[bank
].first_page
)
108 dram_ranges
[bank
].first_page
= base
;
115 if (bank
&& (base
== (1 + dram_ranges
[bank
- 1].last_page
)))
120 if (num_banks
>= MAX_BANKS
) break;
121 bcopy(&dram_ranges
[bank
],
122 &dram_ranges
[bank
+ 1],
123 (num_banks
- bank
- 1) * sizeof(hibernate_bitmap_t
));
124 dram_ranges
[bank
].first_page
= base
;
126 dram_ranges
[bank
].last_page
= base
+ num
- 1;
129 // runtime services will be restarted, so no save
130 case kEfiRuntimeServicesCode
:
131 case kEfiRuntimeServicesData
:
132 // contents are volatile once the platform expert starts
133 case kEfiACPIReclaimMemory
:
134 hibernate_page_list_allocate_avoided
+= num
;
138 case kEfiReservedMemoryType
:
139 case kEfiUnusableMemory
:
140 case kEfiMemoryMappedIO
:
141 case kEfiMemoryMappedIOPortSpace
:
147 if (num_banks
>= MAX_BANKS
)
150 // size the hibernation bitmap
152 size
= sizeof(hibernate_page_list_t
);
154 for (bank
= 0; bank
< num_banks
; bank
++) {
155 pages
= dram_ranges
[bank
].last_page
+ 1 - dram_ranges
[bank
].first_page
;
157 size
+= sizeof(hibernate_bitmap_t
) + ((pages
+ 31) >> 5) * sizeof(uint32_t);
160 list
= (hibernate_page_list_t
*)kalloc(size
);
164 list
->list_size
= (uint32_t)size
;
165 list
->page_count
= page_count
;
166 list
->bank_count
= num_banks
;
168 // convert to hibernation bitmap.
170 bitmap
= &list
->bank_bitmap
[0];
171 for (bank
= 0; bank
< num_banks
; bank
++)
173 bitmap
->first_page
= dram_ranges
[bank
].first_page
;
174 bitmap
->last_page
= dram_ranges
[bank
].last_page
;
175 bitmap
->bitmapwords
= (bitmap
->last_page
+ 1
176 - bitmap
->first_page
+ 31) >> 5;
177 kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", bank
,
180 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
186 // mark pages not to be saved, but available for scratch usage during restore
189 hibernate_page_list_setall_machine( __unused hibernate_page_list_t
* page_list
,
190 __unused hibernate_page_list_t
* page_list_wired
,
191 __unused
uint32_t * pagesOut
)
195 // mark pages not to be saved and not for scratch usage during restore
197 hibernate_page_list_set_volatile( hibernate_page_list_t
* page_list
,
198 hibernate_page_list_t
* page_list_wired
,
201 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
204 hibernate_set_page_state(page_list
, page_list_wired
,
205 I386_HIB_PAGETABLE
, I386_HIB_PAGETABLE_COUNT
,
206 kIOHibernatePageStateFree
);
207 *pagesOut
-= I386_HIB_PAGETABLE_COUNT
;
210 if (args
->efiRuntimeServicesPageStart
)
212 hibernate_set_page_state(page_list
, page_list_wired
,
213 args
->efiRuntimeServicesPageStart
, args
->efiRuntimeServicesPageCount
,
214 kIOHibernatePageStateFree
);
215 *pagesOut
-= args
->efiRuntimeServicesPageCount
;
220 hibernate_processor_setup(IOHibernateImageHeader
* header
)
222 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
224 cpu_datap(0)->cpu_hibernate
= 1;
225 header
->processorFlags
= 0;
227 header
->runtimePages
= args
->efiRuntimeServicesPageStart
;
228 header
->runtimePageCount
= args
->efiRuntimeServicesPageCount
;
229 header
->runtimeVirtualPages
= args
->efiRuntimeServicesVirtualPageStart
;
230 if (args
->Version
== kBootArgsVersion1
&& args
->Revision
>= kBootArgsRevision1_6
) {
231 header
->performanceDataStart
= args
->performanceDataStart
;
232 header
->performanceDataSize
= args
->performanceDataSize
;
234 header
->performanceDataStart
= 0;
235 header
->performanceDataSize
= 0;
238 return (KERN_SUCCESS
);
242 hibernate_vm_lock(void)
244 if (current_cpu_datap()->cpu_hibernate
)
246 vm_page_lock_queues();
247 lck_mtx_lock(&vm_page_queue_free_lock
);
249 if (vm_page_local_q
) {
252 for (i
= 0; i
< vm_page_local_q_count
; i
++) {
255 lq
= &vm_page_local_q
[i
].vpl_un
.vpl
;
257 VPL_LOCK(&lq
->vpl_lock
);
264 hibernate_vm_unlock(void)
266 if (current_cpu_datap()->cpu_hibernate
)
268 if (vm_page_local_q
) {
271 for (i
= 0; i
< vm_page_local_q_count
; i
++) {
274 lq
= &vm_page_local_q
[i
].vpl_un
.vpl
;
276 VPL_UNLOCK(&lq
->vpl_lock
);
279 lck_mtx_unlock(&vm_page_queue_free_lock
);
280 vm_page_unlock_queues();