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 <IOKit/IODMACommand.h>
31 #include <libkern/c++/OSData.h>
33 #include "IOCopyMapper.h"
36 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
39 #define super IOService
40 OSDefineMetaClassAndAbstractStructors(IOMapper
, IOService
);
42 OSMetaClassDefineReservedUsed(IOMapper
, 0);
43 OSMetaClassDefineReservedUsed(IOMapper
, 1);
44 OSMetaClassDefineReservedUsed(IOMapper
, 2);
45 OSMetaClassDefineReservedUnused(IOMapper
, 3);
46 OSMetaClassDefineReservedUnused(IOMapper
, 4);
47 OSMetaClassDefineReservedUnused(IOMapper
, 5);
48 OSMetaClassDefineReservedUnused(IOMapper
, 6);
49 OSMetaClassDefineReservedUnused(IOMapper
, 7);
50 OSMetaClassDefineReservedUnused(IOMapper
, 8);
51 OSMetaClassDefineReservedUnused(IOMapper
, 9);
52 OSMetaClassDefineReservedUnused(IOMapper
, 10);
53 OSMetaClassDefineReservedUnused(IOMapper
, 11);
54 OSMetaClassDefineReservedUnused(IOMapper
, 12);
55 OSMetaClassDefineReservedUnused(IOMapper
, 13);
56 OSMetaClassDefineReservedUnused(IOMapper
, 14);
57 OSMetaClassDefineReservedUnused(IOMapper
, 15);
59 IOMapper
* IOMapper::gSystem
= (IOMapper
*) IOMapper::kUnknown
;
64 IOMapperLock() { fWaitLock
= IOLockAlloc(); };
65 ~IOMapperLock() { IOLockFree(fWaitLock
); };
67 void lock() { IOLockLock(fWaitLock
); };
68 void unlock() { IOLockUnlock(fWaitLock
); };
69 void sleep(void *event
) { IOLockSleep(fWaitLock
, event
, THREAD_UNINT
); };
70 void wakeup(void *event
) { IOLockWakeup(fWaitLock
, event
, false); };
73 static IOMapperLock sMapperLock
;
75 bool IOMapper::start(IOService
*provider
)
78 if (!super::start(provider
))
81 if (!initHardware(provider
))
86 IOMapper::gSystem
= this;
87 sMapperLock
.wakeup(&IOMapper::gSystem
);
93 obj
= provider
->getProperty("iommu-id");
95 obj
= provider
->getProperty("AAPL,phandle");
97 setProperty(gIOMapperIDKey
, obj
);
102 bool IOMapper::allocTable(IOByteCount size
)
107 fTableHandle
= NewARTTable(size
, &fTable
, &fTablePhys
);
108 return fTableHandle
!= 0;
111 void IOMapper::free()
114 FreeARTTable(fTableHandle
, fTableSize
);
121 void IOMapper::setMapperRequired(bool hasMapper
)
124 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
127 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
128 sMapperLock
.unlock();
129 sMapperLock
.wakeup(&IOMapper::gSystem
);
133 void IOMapper::waitForSystemMapper()
136 while ((uintptr_t) IOMapper::gSystem
& kWaitMask
)
137 sMapperLock
.sleep(&IOMapper::gSystem
);
138 sMapperLock
.unlock();
141 IOMapper
* IOMapper::copyMapperForDevice(IOService
* device
)
145 OSDictionary
* matching
;
147 obj
= device
->copyProperty("iommu-parent");
151 if ((mapper
= OSDynamicCast(IOMapper
, obj
)))
154 matching
= IOService::propertyMatching(gIOMapperIDKey
, obj
);
157 mapper
= OSDynamicCast(IOMapper
, IOService::waitForMatchingService(matching
));
161 device
->setProperty("iommu-parent", mapper
);
168 ppnum_t
IOMapper::iovmAllocDMACommand(IODMACommand
* command
, IOItemCount pageCount
)
173 void IOMapper::iovmFreeDMACommand(IODMACommand
* command
,
174 ppnum_t addr
, IOItemCount pageCount
)
178 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
179 ppnum_t
*pageList
, IOItemCount pageCount
)
182 iovmInsert(addr
, offset
++, *pageList
++);
185 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
186 upl_page_info_t
*pageList
, IOItemCount pageCount
)
188 for (IOItemCount i
= 0; i
< pageCount
; i
++)
189 iovmInsert(addr
, offset
+ i
, pageList
[i
].phys_addr
);
193 NewARTTable(IOByteCount size
, void ** virtAddrP
, ppnum_t
*physAddrP
)
195 if (!virtAddrP
|| !physAddrP
)
199 vm_address_t address
;
201 size
= round_page(size
);
202 kr
= kmem_alloc_contig(kernel_map
, &address
, size
, PAGE_MASK
, 0 /*max_pnum*/, 0 /*pnum_mask*/, false);
206 ppnum_t pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
) address
);
208 *physAddrP
= pagenum
;
210 FreeARTTable((OSData
*) address
, size
);
214 *virtAddrP
= (void *) address
;
216 return (OSData
*) address
;
219 void IOMapper::FreeARTTable(OSData
*artHandle
, IOByteCount size
)
221 vm_address_t address
= (vm_address_t
) artHandle
;
223 size
= round_page(size
);
224 kmem_free(kernel_map
, address
, size
); // Just panic if address is 0
227 bool IOMapper::getBypassMask(addr64_t
*maskP
) const
234 // These are C accessors to the system mapper for non-IOKit clients
235 ppnum_t
IOMapperIOVMAlloc(unsigned pages
)
237 IOMapper::checkForSystemMapper();
239 if (IOMapper::gSystem
)
240 return IOMapper::gSystem
->iovmAlloc((IOItemCount
) pages
);
245 void IOMapperIOVMFree(ppnum_t addr
, unsigned pages
)
247 if (IOMapper::gSystem
)
248 IOMapper::gSystem
->iovmFree(addr
, (IOItemCount
) pages
);
251 ppnum_t
IOMapperInsertPage(ppnum_t addr
, unsigned offset
, ppnum_t page
)
253 if (IOMapper::gSystem
) {
254 IOMapper::gSystem
->iovmInsert(addr
, (IOItemCount
) offset
, page
);
255 return addr
+ offset
;
261 void IOMapperInsertPPNPages(ppnum_t addr
, unsigned offset
,
262 ppnum_t
*pageList
, unsigned pageCount
)
264 if (!IOMapper::gSystem
)
265 panic("IOMapperInsertPPNPages no system mapper");
267 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
270 iovmInsert(addr
, (IOItemCount
) offset
, pageList
, pageCount
);
273 void IOMapperInsertUPLPages(ppnum_t addr
, unsigned offset
,
274 upl_page_info_t
*pageList
, unsigned pageCount
)
276 if (!IOMapper::gSystem
)
277 panic("IOMapperInsertUPLPages no system mapper");
279 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
281 IOMapper::gSystem
->iovmInsert(addr
,
282 (IOItemCount
) offset
,
284 (IOItemCount
) pageCount
);
287 /////////////////////////////////////////////////////////////////////////////
293 /////////////////////////////////////////////////////////////////////////////
295 #include <machine/machine_routines.h>
297 UInt8
IOMappedRead8(IOPhysicalAddress address
)
299 IOMapper::checkForSystemMapper();
301 if (IOMapper::gSystem
) {
302 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
303 return (UInt8
) ml_phys_read_byte_64(addr
);
306 return (UInt8
) ml_phys_read_byte((vm_offset_t
) address
);
309 UInt16
IOMappedRead16(IOPhysicalAddress address
)
311 IOMapper::checkForSystemMapper();
313 if (IOMapper::gSystem
) {
314 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
315 return (UInt16
) ml_phys_read_half_64(addr
);
318 return (UInt16
) ml_phys_read_half((vm_offset_t
) address
);
321 UInt32
IOMappedRead32(IOPhysicalAddress address
)
323 IOMapper::checkForSystemMapper();
325 if (IOMapper::gSystem
) {
326 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
327 return (UInt32
) ml_phys_read_word_64(addr
);
330 return (UInt32
) ml_phys_read_word((vm_offset_t
) address
);
333 UInt64
IOMappedRead64(IOPhysicalAddress address
)
335 IOMapper::checkForSystemMapper();
337 if (IOMapper::gSystem
) {
338 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
339 return (UInt64
) ml_phys_read_double_64(addr
);
342 return (UInt64
) ml_phys_read_double((vm_offset_t
) address
);
345 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
)
347 IOMapper::checkForSystemMapper();
349 if (IOMapper::gSystem
) {
350 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
351 ml_phys_write_byte_64(addr
, value
);
354 ml_phys_write_byte((vm_offset_t
) address
, value
);
357 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
)
359 IOMapper::checkForSystemMapper();
361 if (IOMapper::gSystem
) {
362 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
363 ml_phys_write_half_64(addr
, value
);
366 ml_phys_write_half((vm_offset_t
) address
, value
);
369 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
)
371 IOMapper::checkForSystemMapper();
373 if (IOMapper::gSystem
) {
374 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
375 ml_phys_write_word_64(addr
, value
);
378 ml_phys_write_word((vm_offset_t
) address
, value
);
381 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
)
383 IOMapper::checkForSystemMapper();
385 if (IOMapper::gSystem
) {
386 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
387 ml_phys_write_double_64(addr
, value
);
390 ml_phys_write_double((vm_offset_t
) address
, value
);
393 mach_vm_address_t
IOMallocPhysical(mach_vm_size_t size
, mach_vm_address_t mask
)
395 mach_vm_address_t address
= 0;
398 address
= ptoa_64(gIOCopyMapper
->iovmAlloc(atop_64(round_page(size
))));
403 void IOFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
407 gIOCopyMapper
->iovmFree(atop_64(address
), atop_64(round_page(size
)));