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>
34 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
37 #define super IOService
38 OSDefineMetaClassAndAbstractStructors(IOMapper
, IOService
);
40 OSMetaClassDefineReservedUsed(IOMapper
, 0);
41 OSMetaClassDefineReservedUsed(IOMapper
, 1);
42 OSMetaClassDefineReservedUsed(IOMapper
, 2);
43 OSMetaClassDefineReservedUnused(IOMapper
, 3);
44 OSMetaClassDefineReservedUnused(IOMapper
, 4);
45 OSMetaClassDefineReservedUnused(IOMapper
, 5);
46 OSMetaClassDefineReservedUnused(IOMapper
, 6);
47 OSMetaClassDefineReservedUnused(IOMapper
, 7);
48 OSMetaClassDefineReservedUnused(IOMapper
, 8);
49 OSMetaClassDefineReservedUnused(IOMapper
, 9);
50 OSMetaClassDefineReservedUnused(IOMapper
, 10);
51 OSMetaClassDefineReservedUnused(IOMapper
, 11);
52 OSMetaClassDefineReservedUnused(IOMapper
, 12);
53 OSMetaClassDefineReservedUnused(IOMapper
, 13);
54 OSMetaClassDefineReservedUnused(IOMapper
, 14);
55 OSMetaClassDefineReservedUnused(IOMapper
, 15);
57 IOMapper
* IOMapper::gSystem
= (IOMapper
*) IOMapper::kUnknown
;
62 IOMapperLock() { fWaitLock
= IOLockAlloc(); };
63 ~IOMapperLock() { IOLockFree(fWaitLock
); };
65 void lock() { IOLockLock(fWaitLock
); };
66 void unlock() { IOLockUnlock(fWaitLock
); };
67 void sleep(void *event
) { IOLockSleep(fWaitLock
, event
, THREAD_UNINT
); };
68 void wakeup(void *event
) { IOLockWakeup(fWaitLock
, event
, false); };
71 static IOMapperLock sMapperLock
;
73 bool IOMapper::start(IOService
*provider
)
76 if (!super::start(provider
))
79 if (!initHardware(provider
))
84 IOMapper::gSystem
= this;
85 sMapperLock
.wakeup(&IOMapper::gSystem
);
91 obj
= provider
->getProperty("iommu-id");
93 obj
= provider
->getProperty("AAPL,phandle");
95 setProperty(gIOMapperIDKey
, obj
);
100 bool IOMapper::allocTable(IOByteCount size
)
105 fTableHandle
= NewARTTable(size
, &fTable
, &fTablePhys
);
106 return fTableHandle
!= 0;
109 void IOMapper::free()
112 FreeARTTable(fTableHandle
, fTableSize
);
119 void IOMapper::setMapperRequired(bool hasMapper
)
122 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
125 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
126 sMapperLock
.unlock();
127 sMapperLock
.wakeup(&IOMapper::gSystem
);
131 void IOMapper::waitForSystemMapper()
134 while ((uintptr_t) IOMapper::gSystem
& kWaitMask
)
135 sMapperLock
.sleep(&IOMapper::gSystem
);
136 sMapperLock
.unlock();
139 IOMapper
* IOMapper::copyMapperForDevice(IOService
* device
)
143 OSDictionary
* matching
;
145 obj
= device
->copyProperty("iommu-parent");
149 if ((mapper
= OSDynamicCast(IOMapper
, obj
)))
152 matching
= IOService::propertyMatching(gIOMapperIDKey
, obj
);
155 mapper
= OSDynamicCast(IOMapper
, IOService::waitForMatchingService(matching
));
159 device
->setProperty("iommu-parent", mapper
);
166 ppnum_t
IOMapper::iovmAllocDMACommand(IODMACommand
* command
, IOItemCount pageCount
)
171 void IOMapper::iovmFreeDMACommand(IODMACommand
* command
,
172 ppnum_t addr
, IOItemCount pageCount
)
176 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
177 ppnum_t
*pageList
, IOItemCount pageCount
)
180 iovmInsert(addr
, offset
++, *pageList
++);
183 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
184 upl_page_info_t
*pageList
, IOItemCount pageCount
)
186 for (IOItemCount i
= 0; i
< pageCount
; i
++)
187 iovmInsert(addr
, offset
+ i
, pageList
[i
].phys_addr
);
191 NewARTTable(IOByteCount size
, void ** virtAddrP
, ppnum_t
*physAddrP
)
193 if (!virtAddrP
|| !physAddrP
)
197 vm_address_t address
;
199 size
= round_page(size
);
200 kr
= kmem_alloc_contig(kernel_map
, &address
, size
, PAGE_MASK
, 0 /*max_pnum*/, 0 /*pnum_mask*/, false);
204 ppnum_t pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
) address
);
206 *physAddrP
= pagenum
;
208 FreeARTTable((OSData
*) address
, size
);
212 *virtAddrP
= (void *) address
;
214 return (OSData
*) address
;
217 void IOMapper::FreeARTTable(OSData
*artHandle
, IOByteCount size
)
219 vm_address_t address
= (vm_address_t
) artHandle
;
221 size
= round_page(size
);
222 kmem_free(kernel_map
, address
, size
); // Just panic if address is 0
225 bool IOMapper::getBypassMask(addr64_t
*maskP
) const
232 // These are C accessors to the system mapper for non-IOKit clients
233 ppnum_t
IOMapperIOVMAlloc(unsigned pages
)
235 IOMapper::checkForSystemMapper();
237 if (IOMapper::gSystem
)
238 return IOMapper::gSystem
->iovmAlloc((IOItemCount
) pages
);
243 void IOMapperIOVMFree(ppnum_t addr
, unsigned pages
)
245 if (IOMapper::gSystem
)
246 IOMapper::gSystem
->iovmFree(addr
, (IOItemCount
) pages
);
249 ppnum_t
IOMapperInsertPage(ppnum_t addr
, unsigned offset
, ppnum_t page
)
251 if (IOMapper::gSystem
) {
252 IOMapper::gSystem
->iovmInsert(addr
, (IOItemCount
) offset
, page
);
253 return addr
+ offset
;
259 void IOMapperInsertPPNPages(ppnum_t addr
, unsigned offset
,
260 ppnum_t
*pageList
, unsigned pageCount
)
262 if (!IOMapper::gSystem
)
263 panic("IOMapperInsertPPNPages no system mapper");
265 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
268 iovmInsert(addr
, (IOItemCount
) offset
, pageList
, pageCount
);
271 void IOMapperInsertUPLPages(ppnum_t addr
, unsigned offset
,
272 upl_page_info_t
*pageList
, unsigned pageCount
)
274 if (!IOMapper::gSystem
)
275 panic("IOMapperInsertUPLPages no system mapper");
277 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
279 IOMapper::gSystem
->iovmInsert(addr
,
280 (IOItemCount
) offset
,
282 (IOItemCount
) pageCount
);
285 /////////////////////////////////////////////////////////////////////////////
291 /////////////////////////////////////////////////////////////////////////////
293 #include <machine/machine_routines.h>
295 UInt8
IOMappedRead8(IOPhysicalAddress address
)
297 IOMapper::checkForSystemMapper();
299 if (IOMapper::gSystem
) {
300 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
301 return (UInt8
) ml_phys_read_byte_64(addr
);
304 return (UInt8
) ml_phys_read_byte((vm_offset_t
) address
);
307 UInt16
IOMappedRead16(IOPhysicalAddress address
)
309 IOMapper::checkForSystemMapper();
311 if (IOMapper::gSystem
) {
312 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
313 return (UInt16
) ml_phys_read_half_64(addr
);
316 return (UInt16
) ml_phys_read_half((vm_offset_t
) address
);
319 UInt32
IOMappedRead32(IOPhysicalAddress address
)
321 IOMapper::checkForSystemMapper();
323 if (IOMapper::gSystem
) {
324 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
325 return (UInt32
) ml_phys_read_word_64(addr
);
328 return (UInt32
) ml_phys_read_word((vm_offset_t
) address
);
331 UInt64
IOMappedRead64(IOPhysicalAddress address
)
333 IOMapper::checkForSystemMapper();
335 if (IOMapper::gSystem
) {
336 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
337 return (UInt64
) ml_phys_read_double_64(addr
);
340 return (UInt64
) ml_phys_read_double((vm_offset_t
) address
);
343 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
)
345 IOMapper::checkForSystemMapper();
347 if (IOMapper::gSystem
) {
348 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
349 ml_phys_write_byte_64(addr
, value
);
352 ml_phys_write_byte((vm_offset_t
) address
, value
);
355 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
)
357 IOMapper::checkForSystemMapper();
359 if (IOMapper::gSystem
) {
360 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
361 ml_phys_write_half_64(addr
, value
);
364 ml_phys_write_half((vm_offset_t
) address
, value
);
367 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
)
369 IOMapper::checkForSystemMapper();
371 if (IOMapper::gSystem
) {
372 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
373 ml_phys_write_word_64(addr
, value
);
376 ml_phys_write_word((vm_offset_t
) address
, value
);
379 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
)
381 IOMapper::checkForSystemMapper();
383 if (IOMapper::gSystem
) {
384 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
385 ml_phys_write_double_64(addr
, value
);
388 ml_phys_write_double((vm_offset_t
) address
, value
);