]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOMapper.cpp
f2f0633c3f96ff865e5059fe3e498951c224ac1c
[apple/xnu.git] / iokit / Kernel / IOMapper.cpp
1 /*
2 * Copyright (c) 1998-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 #include <IOKit/IOLib.h>
23 #include <IOKit/IOMapper.h>
24 #include <libkern/c++/OSData.h>
25
26 #include "IOCopyMapper.h"
27
28 __BEGIN_DECLS
29 extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
30 __END_DECLS
31
32 #define super IOService
33 OSDefineMetaClassAndAbstractStructors(IOMapper, IOService);
34
35 OSMetaClassDefineReservedUsed(IOMapper, 0);
36 OSMetaClassDefineReservedUnused(IOMapper, 1);
37 OSMetaClassDefineReservedUnused(IOMapper, 2);
38 OSMetaClassDefineReservedUnused(IOMapper, 3);
39 OSMetaClassDefineReservedUnused(IOMapper, 4);
40 OSMetaClassDefineReservedUnused(IOMapper, 5);
41 OSMetaClassDefineReservedUnused(IOMapper, 6);
42 OSMetaClassDefineReservedUnused(IOMapper, 7);
43 OSMetaClassDefineReservedUnused(IOMapper, 8);
44 OSMetaClassDefineReservedUnused(IOMapper, 9);
45 OSMetaClassDefineReservedUnused(IOMapper, 10);
46 OSMetaClassDefineReservedUnused(IOMapper, 11);
47 OSMetaClassDefineReservedUnused(IOMapper, 12);
48 OSMetaClassDefineReservedUnused(IOMapper, 13);
49 OSMetaClassDefineReservedUnused(IOMapper, 14);
50 OSMetaClassDefineReservedUnused(IOMapper, 15);
51
52 IOMapper * IOMapper::gSystem = (IOMapper *) IOMapper::kUnknown;
53
54 class IOMapperLock {
55 IOLock *fWaitLock;
56 public:
57 IOMapperLock() { fWaitLock = IOLockAlloc(); };
58 ~IOMapperLock() { IOLockFree(fWaitLock); };
59
60 void lock() { IOLockLock(fWaitLock); };
61 void unlock() { IOLockUnlock(fWaitLock); };
62 void sleep(void *event) { IOLockSleep(fWaitLock, event, THREAD_UNINT); };
63 void wakeup(void *event) { IOLockWakeup(fWaitLock, event, false); };
64 };
65
66 static IOMapperLock sMapperLock;
67
68 bool IOMapper::start(IOService *provider)
69 {
70 if (!super::start(provider))
71 return false;
72
73 if (!initHardware(provider))
74 return false;
75
76 if (fIsSystem) {
77 sMapperLock.lock();
78 IOMapper::gSystem = this;
79 sMapperLock.wakeup(&IOMapper::gSystem);
80 sMapperLock.unlock();
81 }
82
83 return true;
84 }
85
86 bool IOMapper::allocTable(IOByteCount size)
87 {
88 assert(!fTable);
89
90 fTableSize = size;
91 fTableHandle = NewARTTable(size, &fTable, &fTablePhys);
92 return fTableHandle != 0;
93 }
94
95 void IOMapper::free()
96 {
97 if (fTableHandle) {
98 FreeARTTable(fTableHandle, fTableSize);
99 fTableHandle = 0;
100 }
101
102 super::free();
103 }
104
105 void IOMapper::setMapperRequired(bool hasMapper)
106 {
107 if (hasMapper)
108 IOMapper::gSystem = (IOMapper *) kHasMapper;
109 else {
110 sMapperLock.lock();
111 IOMapper::gSystem = (IOMapper *) kNoMapper;
112 sMapperLock.unlock();
113 sMapperLock.wakeup(&IOMapper::gSystem);
114 }
115 }
116
117 void IOMapper::waitForSystemMapper()
118 {
119 sMapperLock.lock();
120 while ((vm_address_t) IOMapper::gSystem & kWaitMask)
121 sMapperLock.sleep(&IOMapper::gSystem);
122 sMapperLock.unlock();
123 }
124
125 void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset,
126 ppnum_t *pageList, IOItemCount pageCount)
127 {
128 while (pageCount--)
129 iovmInsert(addr, offset++, *pageList++);
130 }
131
132 void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset,
133 upl_page_info_t *pageList, IOItemCount pageCount)
134 {
135 for (IOItemCount i = 0; i < pageCount; i++)
136 iovmInsert(addr, offset + i, pageList[i].phys_addr);
137 }
138
139 OSData * IOMapper::
140 NewARTTable(IOByteCount size, void ** virtAddrP, ppnum_t *physAddrP)
141 {
142 if (!virtAddrP || !physAddrP)
143 return 0;
144
145 kern_return_t kr;
146 vm_address_t address;
147
148 size = round_page_32(size);
149 kr = kmem_alloc_contig(kernel_map, &address, size, PAGE_MASK, 0);
150 if (kr)
151 return 0;
152
153 ppnum_t pagenum = pmap_find_phys(kernel_pmap, (addr64_t) address);
154 if (pagenum)
155 *physAddrP = pagenum;
156 else {
157 FreeARTTable((OSData *) address, size);
158 address = 0;
159 }
160
161 *virtAddrP = (void *) address;
162
163 return (OSData *) address;
164 }
165
166 void IOMapper::FreeARTTable(OSData *artHandle, IOByteCount size)
167 {
168 vm_address_t address = (vm_address_t) artHandle;
169
170 size = round_page_32(size);
171 kmem_free(kernel_map, address, size); // Just panic if address is 0
172 }
173
174 bool IOMapper::getBypassMask(addr64_t *maskP) const
175 {
176 return false;
177 }
178
179 __BEGIN_DECLS
180
181 // These are C accessors to the system mapper for non-IOKit clients
182 ppnum_t IOMapperIOVMAlloc(unsigned pages)
183 {
184 IOMapper::checkForSystemMapper();
185
186 if (IOMapper::gSystem)
187 return IOMapper::gSystem->iovmAlloc((IOItemCount) pages);
188 else
189 return 0;
190 }
191
192 void IOMapperIOVMFree(ppnum_t addr, unsigned pages)
193 {
194 if (IOMapper::gSystem)
195 IOMapper::gSystem->iovmFree(addr, (IOItemCount) pages);
196 }
197
198 ppnum_t IOMapperInsertPage(ppnum_t addr, unsigned offset, ppnum_t page)
199 {
200 if (IOMapper::gSystem) {
201 IOMapper::gSystem->iovmInsert(addr, (IOItemCount) offset, page);
202 return addr + offset;
203 }
204 else
205 return page;
206 }
207
208 void IOMapperInsertPPNPages(ppnum_t addr, unsigned offset,
209 ppnum_t *pageList, unsigned pageCount)
210 {
211 if (!IOMapper::gSystem)
212 panic("IOMapperInsertPPNPages no system mapper");
213 else
214 assert(!((vm_address_t) IOMapper::gSystem & 3));
215
216 IOMapper::gSystem->
217 iovmInsert(addr, (IOItemCount) offset, pageList, pageCount);
218 }
219
220 void IOMapperInsertUPLPages(ppnum_t addr, unsigned offset,
221 upl_page_info_t *pageList, unsigned pageCount)
222 {
223 if (!IOMapper::gSystem)
224 panic("IOMapperInsertUPLPages no system mapper");
225 else
226 assert(!((vm_address_t) IOMapper::gSystem & 3));
227
228 IOMapper::gSystem->iovmInsert(addr,
229 (IOItemCount) offset,
230 pageList,
231 (IOItemCount) pageCount);
232 }
233
234 /////////////////////////////////////////////////////////////////////////////
235 //
236 //
237 // IOLib.h APIs
238 //
239 //
240 /////////////////////////////////////////////////////////////////////////////
241
242 #include <machine/machine_routines.h>
243
244 UInt8 IOMappedRead8(IOPhysicalAddress address)
245 {
246 IOMapper::checkForSystemMapper();
247
248 if (IOMapper::gSystem) {
249 addr64_t addr = IOMapper::gSystem->mapAddr(address);
250 return (UInt8) ml_phys_read_byte_64(addr);
251 }
252 else
253 return (UInt8) ml_phys_read_byte((vm_offset_t) address);
254 }
255
256 UInt16 IOMappedRead16(IOPhysicalAddress address)
257 {
258 IOMapper::checkForSystemMapper();
259
260 if (IOMapper::gSystem) {
261 addr64_t addr = IOMapper::gSystem->mapAddr(address);
262 return (UInt16) ml_phys_read_half_64(addr);
263 }
264 else
265 return (UInt16) ml_phys_read_half((vm_offset_t) address);
266 }
267
268 UInt32 IOMappedRead32(IOPhysicalAddress address)
269 {
270 IOMapper::checkForSystemMapper();
271
272 if (IOMapper::gSystem) {
273 addr64_t addr = IOMapper::gSystem->mapAddr(address);
274 return (UInt32) ml_phys_read_word_64(addr);
275 }
276 else
277 return (UInt32) ml_phys_read_word((vm_offset_t) address);
278 }
279
280 UInt64 IOMappedRead64(IOPhysicalAddress address)
281 {
282 IOMapper::checkForSystemMapper();
283
284 if (IOMapper::gSystem) {
285 addr64_t addr = IOMapper::gSystem->mapAddr(address);
286 return (UInt64) ml_phys_read_double_64(addr);
287 }
288 else
289 return (UInt64) ml_phys_read_double((vm_offset_t) address);
290 }
291
292 void IOMappedWrite8(IOPhysicalAddress address, UInt8 value)
293 {
294 IOMapper::checkForSystemMapper();
295
296 if (IOMapper::gSystem) {
297 addr64_t addr = IOMapper::gSystem->mapAddr(address);
298 ml_phys_write_byte_64(addr, value);
299 }
300 else
301 ml_phys_write_byte((vm_offset_t) address, value);
302 }
303
304 void IOMappedWrite16(IOPhysicalAddress address, UInt16 value)
305 {
306 IOMapper::checkForSystemMapper();
307
308 if (IOMapper::gSystem) {
309 addr64_t addr = IOMapper::gSystem->mapAddr(address);
310 ml_phys_write_half_64(addr, value);
311 }
312 else
313 ml_phys_write_half((vm_offset_t) address, value);
314 }
315
316 void IOMappedWrite32(IOPhysicalAddress address, UInt32 value)
317 {
318 IOMapper::checkForSystemMapper();
319
320 if (IOMapper::gSystem) {
321 addr64_t addr = IOMapper::gSystem->mapAddr(address);
322 ml_phys_write_word_64(addr, value);
323 }
324 else
325 ml_phys_write_word((vm_offset_t) address, value);
326 }
327
328 void IOMappedWrite64(IOPhysicalAddress address, UInt64 value)
329 {
330 IOMapper::checkForSystemMapper();
331
332 if (IOMapper::gSystem) {
333 addr64_t addr = IOMapper::gSystem->mapAddr(address);
334 ml_phys_write_double_64(addr, value);
335 }
336 else
337 ml_phys_write_double((vm_offset_t) address, value);
338 }
339
340 mach_vm_address_t IOMallocPhysical(mach_vm_size_t size, mach_vm_address_t mask)
341 {
342 mach_vm_address_t address = 0;
343 if (gIOCopyMapper)
344 {
345 address = ptoa_64(gIOCopyMapper->iovmAlloc(atop_64(round_page(size))));
346 }
347
348 return (address);
349 }
350
351 void IOFreePhysical(mach_vm_address_t address, mach_vm_size_t size)
352 {
353 if (gIOCopyMapper)
354 {
355 gIOCopyMapper->iovmFree(atop_64(address), atop_64(round_page(size)));
356 }
357 }
358
359
360 __END_DECLS