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>
32 #include <libkern/OSDebug.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 OSMetaClassDefineReservedUsed(IOMapper
, 1);
43 OSMetaClassDefineReservedUsed(IOMapper
, 2);
44 OSMetaClassDefineReservedUsed(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
)
77 if (!super::start(provider
))
80 if (!initHardware(provider
))
85 IOMapper::gSystem
= this;
86 sMapperLock
.wakeup(&IOMapper::gSystem
);
92 obj
= provider
->getProperty("iommu-id");
94 obj
= provider
->getProperty("AAPL,phandle");
96 setProperty(gIOMapperIDKey
, obj
);
101 bool IOMapper::allocTable(IOByteCount size
)
106 fTableHandle
= NewARTTable(size
, &fTable
, &fTablePhys
);
107 return fTableHandle
!= 0;
110 void IOMapper::free()
113 FreeARTTable(fTableHandle
, fTableSize
);
120 void IOMapper::setMapperRequired(bool hasMapper
)
123 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
126 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
127 sMapperLock
.unlock();
128 sMapperLock
.wakeup(&IOMapper::gSystem
);
132 void IOMapper::waitForSystemMapper()
135 while ((uintptr_t) IOMapper::gSystem
& kWaitMask
)
137 OSReportWithBacktrace("waitForSystemMapper");
138 sMapperLock
.sleep(&IOMapper::gSystem
);
140 sMapperLock
.unlock();
143 IOMapper
* IOMapper::copyMapperForDevice(IOService
* device
)
147 OSDictionary
* matching
;
149 obj
= device
->copyProperty("iommu-parent");
153 if ((mapper
= OSDynamicCast(IOMapper
, obj
)))
156 matching
= IOService::propertyMatching(gIOMapperIDKey
, obj
);
159 mapper
= OSDynamicCast(IOMapper
, IOService::waitForMatchingService(matching
));
163 device
->setProperty("iommu-parent", mapper
);
170 ppnum_t
IOMapper::iovmAllocDMACommand(IODMACommand
* command
, IOItemCount pageCount
)
175 void IOMapper::iovmFreeDMACommand(IODMACommand
* command
,
176 ppnum_t addr
, IOItemCount pageCount
)
180 ppnum_t
IOMapper::iovmMapMemory(
181 OSObject
* memory
, // dma command or iomd
185 upl_page_info_t
* pageList
,
186 const IODMAMapSpecification
* mapSpecification
)
191 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
192 ppnum_t
*pageList
, IOItemCount pageCount
)
195 iovmInsert(addr
, offset
++, *pageList
++);
198 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
199 upl_page_info_t
*pageList
, IOItemCount pageCount
)
201 for (IOItemCount i
= 0; i
< pageCount
; i
++)
202 iovmInsert(addr
, offset
+ i
, pageList
[i
].phys_addr
);
206 NewARTTable(IOByteCount size
, void ** virtAddrP
, ppnum_t
*physAddrP
)
208 if (!virtAddrP
|| !physAddrP
)
212 vm_address_t address
;
214 size
= round_page(size
);
215 kr
= kmem_alloc_contig(kernel_map
, &address
, size
, PAGE_MASK
, 0 /*max_pnum*/, 0 /*pnum_mask*/, false);
219 ppnum_t pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
) address
);
221 *physAddrP
= pagenum
;
223 FreeARTTable((OSData
*) address
, size
);
227 *virtAddrP
= (void *) address
;
229 return (OSData
*) address
;
232 void IOMapper::FreeARTTable(OSData
*artHandle
, IOByteCount size
)
234 vm_address_t address
= (vm_address_t
) artHandle
;
236 size
= round_page(size
);
237 kmem_free(kernel_map
, address
, size
); // Just panic if address is 0
240 bool IOMapper::getBypassMask(addr64_t
*maskP
) const
247 // These are C accessors to the system mapper for non-IOKit clients
248 ppnum_t
IOMapperIOVMAlloc(unsigned pages
)
250 IOMapper::checkForSystemMapper();
252 if (IOMapper::gSystem
)
253 return IOMapper::gSystem
->iovmAlloc((IOItemCount
) pages
);
258 void IOMapperIOVMFree(ppnum_t addr
, unsigned pages
)
260 if (IOMapper::gSystem
)
261 IOMapper::gSystem
->iovmFree(addr
, (IOItemCount
) pages
);
264 ppnum_t
IOMapperInsertPage(ppnum_t addr
, unsigned offset
, ppnum_t page
)
266 if (IOMapper::gSystem
) {
267 if (!addr
) panic("!addr");
268 IOMapper::gSystem
->iovmInsert(addr
, (IOItemCount
) offset
, page
);
269 return addr
+ offset
;
275 void IOMapperInsertPPNPages(ppnum_t addr
, unsigned offset
,
276 ppnum_t
*pageList
, unsigned pageCount
)
278 if (!IOMapper::gSystem
)
279 panic("IOMapperInsertPPNPages no system mapper");
281 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
284 iovmInsert(addr
, (IOItemCount
) offset
, pageList
, pageCount
);
287 void IOMapperInsertUPLPages(ppnum_t addr
, unsigned offset
,
288 upl_page_info_t
*pageList
, unsigned pageCount
)
290 if (!IOMapper::gSystem
)
291 panic("IOMapperInsertUPLPages no system mapper");
293 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
295 IOMapper::gSystem
->iovmInsert(addr
,
296 (IOItemCount
) offset
,
298 (IOItemCount
) pageCount
);
301 /////////////////////////////////////////////////////////////////////////////
307 /////////////////////////////////////////////////////////////////////////////
309 #include <machine/machine_routines.h>
311 UInt8
IOMappedRead8(IOPhysicalAddress address
)
313 IOMapper::checkForSystemMapper();
315 if (IOMapper::gSystem
) {
316 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
317 return (UInt8
) ml_phys_read_byte_64(addr
);
320 return (UInt8
) ml_phys_read_byte((vm_offset_t
) address
);
323 UInt16
IOMappedRead16(IOPhysicalAddress address
)
325 IOMapper::checkForSystemMapper();
327 if (IOMapper::gSystem
) {
328 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
329 return (UInt16
) ml_phys_read_half_64(addr
);
332 return (UInt16
) ml_phys_read_half((vm_offset_t
) address
);
335 UInt32
IOMappedRead32(IOPhysicalAddress address
)
337 IOMapper::checkForSystemMapper();
339 if (IOMapper::gSystem
) {
340 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
341 return (UInt32
) ml_phys_read_word_64(addr
);
344 return (UInt32
) ml_phys_read_word((vm_offset_t
) address
);
347 UInt64
IOMappedRead64(IOPhysicalAddress address
)
349 IOMapper::checkForSystemMapper();
351 if (IOMapper::gSystem
) {
352 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
353 return (UInt64
) ml_phys_read_double_64(addr
);
356 return (UInt64
) ml_phys_read_double((vm_offset_t
) address
);
359 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
)
361 IOMapper::checkForSystemMapper();
363 if (IOMapper::gSystem
) {
364 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
365 ml_phys_write_byte_64(addr
, value
);
368 ml_phys_write_byte((vm_offset_t
) address
, value
);
371 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
)
373 IOMapper::checkForSystemMapper();
375 if (IOMapper::gSystem
) {
376 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
377 ml_phys_write_half_64(addr
, value
);
380 ml_phys_write_half((vm_offset_t
) address
, value
);
383 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
)
385 IOMapper::checkForSystemMapper();
387 if (IOMapper::gSystem
) {
388 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
389 ml_phys_write_word_64(addr
, value
);
392 ml_phys_write_word((vm_offset_t
) address
, value
);
395 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
)
397 IOMapper::checkForSystemMapper();
399 if (IOMapper::gSystem
) {
400 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
401 ml_phys_write_double_64(addr
, value
);
404 ml_phys_write_double((vm_offset_t
) address
, value
);