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