]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/hibernate_ppc.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / osfmk / ppc / hibernate_ppc.c
CommitLineData
3a60a9f5
A
1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
3a60a9f5 5 *
8ad349bb
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
3a60a9f5
A
29 */
30
31#include <kern/kern_types.h>
32#include <kern/kalloc.h>
33#include <kern/machine.h>
34#include <kern/misc_protos.h>
35#include <kern/thread.h>
36#include <kern/processor.h>
37#include <mach/machine.h>
38#include <mach/processor_info.h>
39#include <mach/mach_types.h>
40#include <ppc/proc_reg.h>
41#include <ppc/misc_protos.h>
42#include <ppc/machine_routines.h>
43#include <ppc/machine_cpu.h>
44#include <ppc/exception.h>
45#include <ppc/asm.h>
46#include <ppc/hw_perfmon.h>
47#include <pexpert/pexpert.h>
48#include <kern/cpu_data.h>
49#include <ppc/mappings.h>
50#include <ppc/Diagnostics.h>
51#include <ppc/trap.h>
52#include <ppc/mem.h>
53#include <IOKit/IOPlatformExpert.h>
54#define KERNEL
55
56#include <IOKit/IOHibernatePrivate.h>
57#include <vm/vm_page.h>
58
59/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
60
61hibernate_page_list_t *
62hibernate_page_list_allocate(void)
63{
64 vm_size_t size;
65 uint32_t bank;
66 uint32_t pages, page_count;
67 hibernate_page_list_t * list;
68 hibernate_bitmap_t * bitmap;
69
70 page_count = 0;
71 size = sizeof(hibernate_page_list_t);
72
73 for (bank = 0; bank < (uint32_t) pmap_mem_regions_count; bank++)
74 {
75 size += sizeof(hibernate_bitmap_t);
76 pages = pmap_mem_regions[bank].mrEnd + 1 - pmap_mem_regions[bank].mrStart;
77 page_count += pages;
78 size += ((pages + 31) >> 5) * sizeof(uint32_t);
79 }
80
81 list = kalloc(size);
82 if (!list)
83 return (list);
84
85 list->list_size = size;
86 list->page_count = page_count;
87 list->bank_count = pmap_mem_regions_count;
88
89 bitmap = &list->bank_bitmap[0];
90 for (bank = 0; bank < list->bank_count; bank++)
91 {
92 bitmap->first_page = pmap_mem_regions[bank].mrStart;
93 bitmap->last_page = pmap_mem_regions[bank].mrEnd;
94 bitmap->bitmapwords = (pmap_mem_regions[bank].mrEnd + 1
95 - pmap_mem_regions[bank].mrStart + 31) >> 5;
96
97 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
98 }
99 return (list);
100}
101
102void
103hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,
104 hibernate_page_list_t * page_list_wired,
105 uint32_t * pagesOut)
106{
107 uint32_t page, count, PCAsize;
108
109 /* Get total size of PCA table */
110 PCAsize = round_page((hash_table_size / PerProcTable[0].ppe_vaddr->pf.pfPTEG)
111 * sizeof(PCA_t));
112
113 page = atop_64(hash_table_base - PCAsize);
114 count = atop_64(hash_table_size + PCAsize);
115
116 hibernate_set_page_state(page_list, page_list_wired, page, count, 0);
117 pagesOut -= count;
118
119 HIBLOG("removed hash, pca: %d pages\n", count);
120
121 save_snapshot();
122}
123
124kern_return_t
125hibernate_processor_setup(IOHibernateImageHeader * header)
126{
127 header->processorFlags = PerProcTable[0].ppe_vaddr->pf.Available;
128
129 PerProcTable[0].ppe_vaddr->hibernate = 1;
130
131 return (KERN_SUCCESS);
132}
133
134void
135hibernate_vm_lock(void)
136{
137 if (getPerProc()->hibernate)
138 {
139 vm_page_lock_queues();
140 mutex_lock(&vm_page_queue_free_lock);
141 }
142}
143
144void
145hibernate_vm_unlock(void)
146{
147 if (getPerProc()->hibernate)
148 {
149 mutex_unlock(&vm_page_queue_free_lock);
150 vm_page_unlock_queues();
151 }
152}
153
154void ml_ppc_sleep(void)
155{
156 struct per_proc_info *proc_info;
157 boolean_t dohalt;
158
159 proc_info = getPerProc();
160 if (!proc_info->hibernate)
161 {
162 ml_ppc_do_sleep();
163 return;
164 }
165
166 {
167 uint64_t start, end, nsec;
168
169 HIBLOG("mapping_hibernate_flush start\n");
170 clock_get_uptime(&start);
171
172 mapping_hibernate_flush();
173
174 clock_get_uptime(&end);
175 absolutetime_to_nanoseconds(end - start, &nsec);
176 HIBLOG("mapping_hibernate_flush time: %qd ms\n", nsec / 1000000ULL);
177 }
178
179 dohalt = hibernate_write_image();
180
181 if (dohalt)
182 {
183 // off
184 HIBLOG("power off\n");
185 if (PE_halt_restart)
186 (*PE_halt_restart)(kPEHaltCPU);
187 }
188 else
189 {
190 // sleep
191 HIBLOG("sleep\n");
192
193 // should we come back via regular wake, set the state in memory.
194 PerProcTable[0].ppe_vaddr->hibernate = 0;
195
196 PE_cpu_machine_quiesce(proc_info->cpu_id);
197 return;
198 }
199}
200