]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/hibernate_ppc.c
e90a52b0e9cc87f935399ec74f3b91d6fb0aeba2
[apple/xnu.git] / osfmk / ppc / hibernate_ppc.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <kern/kern_types.h>
30 #include <kern/kalloc.h>
31 #include <kern/machine.h>
32 #include <kern/misc_protos.h>
33 #include <kern/thread.h>
34 #include <kern/processor.h>
35 #include <mach/machine.h>
36 #include <mach/processor_info.h>
37 #include <mach/mach_types.h>
38 #include <ppc/proc_reg.h>
39 #include <ppc/misc_protos.h>
40 #include <ppc/machine_routines.h>
41 #include <ppc/machine_cpu.h>
42 #include <ppc/exception.h>
43 #include <ppc/asm.h>
44 #include <ppc/hw_perfmon.h>
45 #include <pexpert/pexpert.h>
46 #include <kern/cpu_data.h>
47 #include <ppc/mappings.h>
48 #include <ppc/Diagnostics.h>
49 #include <ppc/trap.h>
50 #include <ppc/mem.h>
51 #include <IOKit/IOPlatformExpert.h>
52 #define KERNEL
53
54 #include <IOKit/IOHibernatePrivate.h>
55 #include <vm/vm_page.h>
56
57 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
58
59 hibernate_page_list_t *
60 hibernate_page_list_allocate(void)
61 {
62 vm_size_t size;
63 uint32_t bank;
64 uint32_t pages, page_count;
65 hibernate_page_list_t * list;
66 hibernate_bitmap_t * bitmap;
67
68 page_count = 0;
69 size = sizeof(hibernate_page_list_t);
70
71 for (bank = 0; bank < (uint32_t) pmap_mem_regions_count; bank++)
72 {
73 size += sizeof(hibernate_bitmap_t);
74 pages = pmap_mem_regions[bank].mrEnd + 1 - pmap_mem_regions[bank].mrStart;
75 page_count += pages;
76 size += ((pages + 31) >> 5) * sizeof(uint32_t);
77 }
78
79 list = kalloc(size);
80 if (!list)
81 return (list);
82
83 list->list_size = size;
84 list->page_count = page_count;
85 list->bank_count = pmap_mem_regions_count;
86
87 bitmap = &list->bank_bitmap[0];
88 for (bank = 0; bank < list->bank_count; bank++)
89 {
90 bitmap->first_page = pmap_mem_regions[bank].mrStart;
91 bitmap->last_page = pmap_mem_regions[bank].mrEnd;
92 bitmap->bitmapwords = (pmap_mem_regions[bank].mrEnd + 1
93 - pmap_mem_regions[bank].mrStart + 31) >> 5;
94
95 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
96 }
97 return (list);
98 }
99
100 void
101 hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,
102 hibernate_page_list_t * page_list_wired,
103 uint32_t * pagesOut)
104 {
105 uint32_t page, count, PCAsize;
106
107 /* Get total size of PCA table */
108 PCAsize = round_page((hash_table_size / PerProcTable[0].ppe_vaddr->pf.pfPTEG)
109 * sizeof(PCA_t));
110
111 page = atop_64(hash_table_base - PCAsize);
112 count = atop_64(hash_table_size + PCAsize);
113
114 hibernate_set_page_state(page_list, page_list_wired, page, count, 0);
115 pagesOut -= count;
116
117 HIBLOG("removed hash, pca: %d pages\n", count);
118
119 save_snapshot();
120 }
121
122 kern_return_t
123 hibernate_processor_setup(IOHibernateImageHeader * header)
124 {
125 header->processorFlags = PerProcTable[0].ppe_vaddr->pf.Available;
126
127 PerProcTable[0].ppe_vaddr->hibernate = 1;
128
129 return (KERN_SUCCESS);
130 }
131
132 void
133 hibernate_vm_lock(void)
134 {
135 if (getPerProc()->hibernate)
136 {
137 vm_page_lock_queues();
138 mutex_lock(&vm_page_queue_free_lock);
139 }
140 }
141
142 void
143 hibernate_vm_unlock(void)
144 {
145 if (getPerProc()->hibernate)
146 {
147 mutex_unlock(&vm_page_queue_free_lock);
148 vm_page_unlock_queues();
149 }
150 }
151
152 void ml_ppc_sleep(void)
153 {
154 struct per_proc_info *proc_info;
155 boolean_t dohalt;
156
157 proc_info = getPerProc();
158 if (!proc_info->hibernate)
159 {
160 ml_ppc_do_sleep();
161 return;
162 }
163
164 {
165 uint64_t start, end, nsec;
166
167 HIBLOG("mapping_hibernate_flush start\n");
168 clock_get_uptime(&start);
169
170 mapping_hibernate_flush();
171
172 clock_get_uptime(&end);
173 absolutetime_to_nanoseconds(end - start, &nsec);
174 HIBLOG("mapping_hibernate_flush time: %qd ms\n", nsec / 1000000ULL);
175 }
176
177 dohalt = hibernate_write_image();
178
179 if (dohalt)
180 {
181 // off
182 HIBLOG("power off\n");
183 if (PE_halt_restart)
184 (*PE_halt_restart)(kPEHaltCPU);
185 }
186 else
187 {
188 // sleep
189 HIBLOG("sleep\n");
190
191 // should we come back via regular wake, set the state in memory.
192 PerProcTable[0].ppe_vaddr->hibernate = 0;
193
194 PE_cpu_machine_quiesce(proc_info->cpu_id);
195 return;
196 }
197 }
198