2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <kern/machine.h>
24 #include <kern/misc_protos.h>
25 #include <kern/thread.h>
26 #include <kern/processor.h>
27 #include <kern/kalloc.h>
28 #include <mach/machine.h>
29 #include <mach/processor_info.h>
30 #include <mach/mach_types.h>
31 #include <i386/pmap.h>
32 #include <kern/cpu_data.h>
33 #include <IOKit/IOPlatformExpert.h>
35 #include <pexpert/i386/efi.h>
37 #include <IOKit/IOHibernatePrivate.h>
38 #include <vm/vm_page.h>
39 #include "i386_lowmem.h"
43 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45 hibernate_page_list_t
*
46 hibernate_page_list_allocate(void)
50 uint32_t bank
, num_banks
;
51 uint32_t pages
, page_count
;
52 hibernate_page_list_t
* list
;
53 hibernate_bitmap_t
* bitmap
;
55 EfiMemoryRange
* mptr
;
56 uint32_t mcount
, msize
, i
;
57 hibernate_bitmap_t dram_ranges
[MAX_BANKS
];
58 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
60 mptr
= args
->MemoryMap
;
61 if (args
->MemoryMapDescriptorSize
== 0)
62 panic("Invalid memory map descriptor size");
63 msize
= args
->MemoryMapDescriptorSize
;
64 mcount
= args
->MemoryMapSize
/ msize
;
67 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
))
69 base
= (ppnum_t
) (mptr
->PhysicalStart
>> I386_PGSHIFT
);
70 num
= (ppnum_t
) mptr
->NumberOfPages
;
79 case kEfiBootServicesCode
:
80 case kEfiBootServicesData
:
81 case kEfiConventionalMemory
:
82 case kEfiACPIReclaimMemory
:
83 case kEfiACPIMemoryNVS
:
86 if (!num_banks
|| (base
!= (1 + dram_ranges
[num_banks
- 1].last_page
)))
89 if (num_banks
>= MAX_BANKS
)
91 dram_ranges
[num_banks
- 1].first_page
= base
;
93 dram_ranges
[num_banks
- 1].last_page
= base
+ num
- 1;
96 // runtime services will be restarted, so no save
97 case kEfiRuntimeServicesCode
:
98 case kEfiRuntimeServicesData
:
100 case kEfiReservedMemoryType
:
101 case kEfiUnusableMemory
:
102 case kEfiMemoryMappedIO
:
103 case kEfiMemoryMappedIOPortSpace
:
109 if (num_banks
>= MAX_BANKS
)
112 // size the hibernation bitmap
114 size
= sizeof(hibernate_page_list_t
);
116 for (bank
= 0; bank
< num_banks
; bank
++) {
117 pages
= dram_ranges
[bank
].last_page
+ 1 - dram_ranges
[bank
].first_page
;
119 size
+= sizeof(hibernate_bitmap_t
) + ((pages
+ 31) >> 5) * sizeof(uint32_t);
122 list
= (hibernate_page_list_t
*)kalloc(size
);
126 list
->list_size
= size
;
127 list
->page_count
= page_count
;
128 list
->bank_count
= num_banks
;
130 // convert to hibernation bitmap.
132 bitmap
= &list
->bank_bitmap
[0];
133 for (bank
= 0; bank
< num_banks
; bank
++)
135 bitmap
->first_page
= dram_ranges
[bank
].first_page
;
136 bitmap
->last_page
= dram_ranges
[bank
].last_page
;
137 bitmap
->bitmapwords
= (bitmap
->last_page
+ 1
138 - bitmap
->first_page
+ 31) >> 5;
139 kprintf("hib bank[%d]: 0x%x000 end 0x%xfff\n", bank
,
142 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
148 // mark pages not to be saved, but available for scratch usage during restore
151 hibernate_page_list_setall_machine( __unused hibernate_page_list_t
* page_list
,
152 __unused hibernate_page_list_t
* page_list_wired
,
153 __unused
uint32_t * pagesOut
)
157 // mark pages not to be saved and not for scratch usage during restore
159 hibernate_page_list_set_volatile( hibernate_page_list_t
* page_list
,
160 hibernate_page_list_t
* page_list_wired
,
163 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
165 hibernate_set_page_state(page_list
, page_list_wired
,
166 I386_HIB_PAGETABLE
, I386_HIB_PAGETABLE_COUNT
,
167 kIOHibernatePageStateFree
);
168 *pagesOut
-= I386_HIB_PAGETABLE_COUNT
;
170 if (args
->efiRuntimeServicesPageStart
)
172 hibernate_set_page_state(page_list
, page_list_wired
,
173 args
->efiRuntimeServicesPageStart
, args
->efiRuntimeServicesPageCount
,
174 kIOHibernatePageStateFree
);
175 *pagesOut
-= args
->efiRuntimeServicesPageCount
;
180 hibernate_processor_setup(IOHibernateImageHeader
* header
)
182 boot_args
* args
= (boot_args
*) PE_state
.bootArgs
;
184 cpu_datap(0)->cpu_hibernate
= 1;
185 header
->processorFlags
= 0;
187 header
->runtimePages
= args
->efiRuntimeServicesPageStart
;
188 header
->runtimePageCount
= args
->efiRuntimeServicesPageCount
;
190 return (KERN_SUCCESS
);
194 hibernate_vm_lock(void)
196 if (current_cpu_datap()->cpu_hibernate
)
198 vm_page_lock_queues();
199 mutex_lock(&vm_page_queue_free_lock
);
204 hibernate_vm_unlock(void)
206 if (current_cpu_datap()->cpu_hibernate
)
208 mutex_unlock(&vm_page_queue_free_lock
);
209 vm_page_unlock_queues();