2 * Copyright (c) 1998-2016 Apple 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>
32 #include <libkern/OSDebug.h>
33 #include "IOKitKernelInternal.h"
36 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
39 #define super IOService
40 OSDefineMetaClassAndAbstractStructors(IOMapper
, IOService
);
42 OSMetaClassDefineReservedUnused(IOMapper
, 0);
43 OSMetaClassDefineReservedUnused(IOMapper
, 1);
44 OSMetaClassDefineReservedUnused(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
))
84 fPageSize
= getPageSize();
88 IOMapper::gSystem
= this;
89 sMapperLock
.wakeup(&IOMapper::gSystem
);
95 obj
= provider
->getProperty("iommu-id");
97 obj
= provider
->getProperty("AAPL,phandle");
99 setProperty(gIOMapperIDKey
, obj
);
104 void IOMapper::free()
109 void IOMapper::setMapperRequired(bool hasMapper
)
112 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
115 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
116 sMapperLock
.unlock();
117 sMapperLock
.wakeup(&IOMapper::gSystem
);
121 void IOMapper::waitForSystemMapper()
124 while ((uintptr_t) IOMapper::gSystem
& kWaitMask
)
126 OSReportWithBacktrace("waitForSystemMapper");
127 sMapperLock
.sleep(&IOMapper::gSystem
);
129 sMapperLock
.unlock();
132 IOMapper
* IOMapper::copyMapperForDevice(IOService
* device
)
134 return copyMapperForDeviceWithIndex(device
, 0);
137 IOMapper
* IOMapper::copyMapperForDeviceWithIndex(IOService
* device
, unsigned int index
)
141 IOMapper
* mapper
= NULL
;
142 OSDictionary
* matching
;
144 obj
= device
->copyProperty("iommu-parent");
148 if ((mapper
= OSDynamicCast(IOMapper
, obj
)))
151 if ((data
= OSDynamicCast(OSData
, obj
)))
153 if (index
>= data
->getLength() / sizeof(UInt32
))
156 data
= OSData::withBytesNoCopy((UInt32
*)data
->getBytesNoCopy() + index
, sizeof(UInt32
));
160 matching
= IOService::propertyMatching(gIOMapperIDKey
, data
);
164 matching
= IOService::propertyMatching(gIOMapperIDKey
, obj
);
168 mapper
= OSDynamicCast(IOMapper
, IOService::waitForMatchingService(matching
));
180 // These are C accessors to the system mapper for non-IOKit clients
181 ppnum_t
IOMapperIOVMAlloc(unsigned pages
)
184 uint64_t dmaAddress
, dmaLength
;
186 IOMapper::checkForSystemMapper();
188 ret
= kIOReturnUnsupported
;
189 if (IOMapper::gSystem
)
191 ret
= IOMapper::gSystem
->iovmMapMemory(
192 NULL
, 0, ptoa_64(pages
),
193 (kIODMAMapReadAccess
| kIODMAMapWriteAccess
),
195 &dmaAddress
, &dmaLength
);
198 if (kIOReturnSuccess
== ret
) return (atop_64(dmaAddress
));
202 void IOMapperIOVMFree(ppnum_t addr
, unsigned pages
)
204 if (IOMapper::gSystem
)
206 IOMapper::gSystem
->iovmUnmapMemory(NULL
, NULL
, ptoa_64(addr
), ptoa_64(pages
));
210 ppnum_t
IOMapperInsertPage(ppnum_t addr
, unsigned offset
, ppnum_t page
)
212 if (!IOMapper::gSystem
) return (page
);
213 if (!addr
) panic("!addr");
214 IOMapper::gSystem
->iovmInsert((kIODMAMapReadAccess
| kIODMAMapWriteAccess
),
215 ptoa_64(addr
), ptoa_64(offset
), ptoa_64(page
), ptoa_64(1));
216 return (addr
+ offset
);
219 /////////////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////////////
227 #include <machine/machine_routines.h>
229 UInt8
IOMappedRead8(IOPhysicalAddress address
)
231 IOMapper::checkForSystemMapper();
233 if (IOMapper::gSystem
) {
234 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
235 return (UInt8
) ml_phys_read_byte_64(addr
);
238 return (UInt8
) ml_phys_read_byte((vm_offset_t
) address
);
241 UInt16
IOMappedRead16(IOPhysicalAddress address
)
243 IOMapper::checkForSystemMapper();
245 if (IOMapper::gSystem
) {
246 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
247 return (UInt16
) ml_phys_read_half_64(addr
);
250 return (UInt16
) ml_phys_read_half((vm_offset_t
) address
);
253 UInt32
IOMappedRead32(IOPhysicalAddress address
)
255 IOMapper::checkForSystemMapper();
257 if (IOMapper::gSystem
) {
258 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
259 return (UInt32
) ml_phys_read_word_64(addr
);
262 return (UInt32
) ml_phys_read_word((vm_offset_t
) address
);
265 UInt64
IOMappedRead64(IOPhysicalAddress address
)
267 IOMapper::checkForSystemMapper();
269 if (IOMapper::gSystem
) {
270 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
271 return (UInt64
) ml_phys_read_double_64(addr
);
274 return (UInt64
) ml_phys_read_double((vm_offset_t
) address
);
277 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
)
279 IOMapper::checkForSystemMapper();
281 if (IOMapper::gSystem
) {
282 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
283 ml_phys_write_byte_64(addr
, value
);
286 ml_phys_write_byte((vm_offset_t
) address
, value
);
289 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
)
291 IOMapper::checkForSystemMapper();
293 if (IOMapper::gSystem
) {
294 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
295 ml_phys_write_half_64(addr
, value
);
298 ml_phys_write_half((vm_offset_t
) address
, value
);
301 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
)
303 IOMapper::checkForSystemMapper();
305 if (IOMapper::gSystem
) {
306 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
307 ml_phys_write_word_64(addr
, value
);
310 ml_phys_write_word((vm_offset_t
) address
, value
);
313 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
)
315 IOMapper::checkForSystemMapper();
317 if (IOMapper::gSystem
) {
318 addr64_t addr
= IOMapper::gSystem
->mapToPhysicalAddress(address
);
319 ml_phys_write_double_64(addr
, value
);
322 ml_phys_write_double((vm_offset_t
) address
, value
);