2 * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <IOKit/IOLib.h>
29 #include <IOKit/IOMapper.h>
30 #include <libkern/c++/OSData.h>
32 #include "IOCopyMapper.h"
35 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
38 #define super IOService
39 OSDefineMetaClassAndAbstractStructors(IOMapper
, IOService
);
41 OSMetaClassDefineReservedUsed(IOMapper
, 0);
42 OSMetaClassDefineReservedUnused(IOMapper
, 1);
43 OSMetaClassDefineReservedUnused(IOMapper
, 2);
44 OSMetaClassDefineReservedUnused(IOMapper
, 3);
45 OSMetaClassDefineReservedUnused(IOMapper
, 4);
46 OSMetaClassDefineReservedUnused(IOMapper
, 5);
47 OSMetaClassDefineReservedUnused(IOMapper
, 6);
48 OSMetaClassDefineReservedUnused(IOMapper
, 7);
49 OSMetaClassDefineReservedUnused(IOMapper
, 8);
50 OSMetaClassDefineReservedUnused(IOMapper
, 9);
51 OSMetaClassDefineReservedUnused(IOMapper
, 10);
52 OSMetaClassDefineReservedUnused(IOMapper
, 11);
53 OSMetaClassDefineReservedUnused(IOMapper
, 12);
54 OSMetaClassDefineReservedUnused(IOMapper
, 13);
55 OSMetaClassDefineReservedUnused(IOMapper
, 14);
56 OSMetaClassDefineReservedUnused(IOMapper
, 15);
58 IOMapper
* IOMapper::gSystem
= (IOMapper
*) IOMapper::kUnknown
;
63 IOMapperLock() { fWaitLock
= IOLockAlloc(); };
64 ~IOMapperLock() { IOLockFree(fWaitLock
); };
66 void lock() { IOLockLock(fWaitLock
); };
67 void unlock() { IOLockUnlock(fWaitLock
); };
68 void sleep(void *event
) { IOLockSleep(fWaitLock
, event
, THREAD_UNINT
); };
69 void wakeup(void *event
) { IOLockWakeup(fWaitLock
, event
, false); };
72 static IOMapperLock sMapperLock
;
74 bool IOMapper::start(IOService
*provider
)
76 if (!super::start(provider
))
79 if (!initHardware(provider
))
84 IOMapper::gSystem
= this;
85 sMapperLock
.wakeup(&IOMapper::gSystem
);
92 bool IOMapper::allocTable(IOByteCount size
)
97 fTableHandle
= NewARTTable(size
, &fTable
, &fTablePhys
);
98 return fTableHandle
!= 0;
101 void IOMapper::free()
104 FreeARTTable(fTableHandle
, fTableSize
);
111 void IOMapper::setMapperRequired(bool hasMapper
)
114 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
117 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
118 sMapperLock
.unlock();
119 sMapperLock
.wakeup(&IOMapper::gSystem
);
123 void IOMapper::waitForSystemMapper()
126 while ((vm_address_t
) IOMapper::gSystem
& kWaitMask
)
127 sMapperLock
.sleep(&IOMapper::gSystem
);
128 sMapperLock
.unlock();
131 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
132 ppnum_t
*pageList
, IOItemCount pageCount
)
135 iovmInsert(addr
, offset
++, *pageList
++);
138 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
139 upl_page_info_t
*pageList
, IOItemCount pageCount
)
141 for (IOItemCount i
= 0; i
< pageCount
; i
++)
142 iovmInsert(addr
, offset
+ i
, pageList
[i
].phys_addr
);
146 NewARTTable(IOByteCount size
, void ** virtAddrP
, ppnum_t
*physAddrP
)
148 if (!virtAddrP
|| !physAddrP
)
152 vm_address_t address
;
154 size
= round_page_32(size
);
155 kr
= kmem_alloc_contig(kernel_map
, &address
, size
, PAGE_MASK
, 0);
159 ppnum_t pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
) address
);
161 *physAddrP
= pagenum
;
163 FreeARTTable((OSData
*) address
, size
);
167 *virtAddrP
= (void *) address
;
169 return (OSData
*) address
;
172 void IOMapper::FreeARTTable(OSData
*artHandle
, IOByteCount size
)
174 vm_address_t address
= (vm_address_t
) artHandle
;
176 size
= round_page_32(size
);
177 kmem_free(kernel_map
, address
, size
); // Just panic if address is 0
180 bool IOMapper::getBypassMask(addr64_t
*maskP
) const
187 // These are C accessors to the system mapper for non-IOKit clients
188 ppnum_t
IOMapperIOVMAlloc(unsigned pages
)
190 IOMapper::checkForSystemMapper();
192 if (IOMapper::gSystem
)
193 return IOMapper::gSystem
->iovmAlloc((IOItemCount
) pages
);
198 void IOMapperIOVMFree(ppnum_t addr
, unsigned pages
)
200 if (IOMapper::gSystem
)
201 IOMapper::gSystem
->iovmFree(addr
, (IOItemCount
) pages
);
204 ppnum_t
IOMapperInsertPage(ppnum_t addr
, unsigned offset
, ppnum_t page
)
206 if (IOMapper::gSystem
) {
207 IOMapper::gSystem
->iovmInsert(addr
, (IOItemCount
) offset
, page
);
208 return addr
+ offset
;
214 void IOMapperInsertPPNPages(ppnum_t addr
, unsigned offset
,
215 ppnum_t
*pageList
, unsigned pageCount
)
217 if (!IOMapper::gSystem
)
218 panic("IOMapperInsertPPNPages no system mapper");
220 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
223 iovmInsert(addr
, (IOItemCount
) offset
, pageList
, pageCount
);
226 void IOMapperInsertUPLPages(ppnum_t addr
, unsigned offset
,
227 upl_page_info_t
*pageList
, unsigned pageCount
)
229 if (!IOMapper::gSystem
)
230 panic("IOMapperInsertUPLPages no system mapper");
232 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
234 IOMapper::gSystem
->iovmInsert(addr
,
235 (IOItemCount
) offset
,
237 (IOItemCount
) pageCount
);
240 /////////////////////////////////////////////////////////////////////////////
246 /////////////////////////////////////////////////////////////////////////////
248 #include <machine/machine_routines.h>
250 UInt8
IOMappedRead8(IOPhysicalAddress address
)
252 IOMapper::checkForSystemMapper();
254 if (IOMapper::gSystem
) {
255 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
256 return (UInt8
) ml_phys_read_byte_64(addr
);
259 return (UInt8
) ml_phys_read_byte((vm_offset_t
) address
);
262 UInt16
IOMappedRead16(IOPhysicalAddress address
)
264 IOMapper::checkForSystemMapper();
266 if (IOMapper::gSystem
) {
267 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
268 return (UInt16
) ml_phys_read_half_64(addr
);
271 return (UInt16
) ml_phys_read_half((vm_offset_t
) address
);
274 UInt32
IOMappedRead32(IOPhysicalAddress address
)
276 IOMapper::checkForSystemMapper();
278 if (IOMapper::gSystem
) {
279 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
280 return (UInt32
) ml_phys_read_word_64(addr
);
283 return (UInt32
) ml_phys_read_word((vm_offset_t
) address
);
286 UInt64
IOMappedRead64(IOPhysicalAddress address
)
288 IOMapper::checkForSystemMapper();
290 if (IOMapper::gSystem
) {
291 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
292 return (UInt64
) ml_phys_read_double_64(addr
);
295 return (UInt64
) ml_phys_read_double((vm_offset_t
) address
);
298 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
)
300 IOMapper::checkForSystemMapper();
302 if (IOMapper::gSystem
) {
303 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
304 ml_phys_write_byte_64(addr
, value
);
307 ml_phys_write_byte((vm_offset_t
) address
, value
);
310 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
)
312 IOMapper::checkForSystemMapper();
314 if (IOMapper::gSystem
) {
315 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
316 ml_phys_write_half_64(addr
, value
);
319 ml_phys_write_half((vm_offset_t
) address
, value
);
322 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
)
324 IOMapper::checkForSystemMapper();
326 if (IOMapper::gSystem
) {
327 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
328 ml_phys_write_word_64(addr
, value
);
331 ml_phys_write_word((vm_offset_t
) address
, value
);
334 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
)
336 IOMapper::checkForSystemMapper();
338 if (IOMapper::gSystem
) {
339 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
340 ml_phys_write_double_64(addr
, value
);
343 ml_phys_write_double((vm_offset_t
) address
, value
);
346 mach_vm_address_t
IOMallocPhysical(mach_vm_size_t size
, mach_vm_address_t mask
)
348 mach_vm_address_t address
= 0;
351 address
= ptoa_64(gIOCopyMapper
->iovmAlloc(atop_64(round_page(size
))));
357 void IOFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
361 gIOCopyMapper
->iovmFree(atop_64(address
), atop_64(round_page(size
)));