2 * Copyright (c) 1998-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <IOKit/IOLib.h>
26 #include <IOKit/IOMapper.h>
27 #include <libkern/c++/OSData.h>
29 #define super IOService
30 OSDefineMetaClassAndAbstractStructors(IOMapper
, IOService
);
32 OSMetaClassDefineReservedUnused(IOMapper
, 0);
33 OSMetaClassDefineReservedUnused(IOMapper
, 1);
34 OSMetaClassDefineReservedUnused(IOMapper
, 2);
35 OSMetaClassDefineReservedUnused(IOMapper
, 3);
36 OSMetaClassDefineReservedUnused(IOMapper
, 4);
37 OSMetaClassDefineReservedUnused(IOMapper
, 5);
38 OSMetaClassDefineReservedUnused(IOMapper
, 6);
39 OSMetaClassDefineReservedUnused(IOMapper
, 7);
40 OSMetaClassDefineReservedUnused(IOMapper
, 8);
41 OSMetaClassDefineReservedUnused(IOMapper
, 9);
42 OSMetaClassDefineReservedUnused(IOMapper
, 10);
43 OSMetaClassDefineReservedUnused(IOMapper
, 11);
44 OSMetaClassDefineReservedUnused(IOMapper
, 12);
45 OSMetaClassDefineReservedUnused(IOMapper
, 13);
46 OSMetaClassDefineReservedUnused(IOMapper
, 14);
47 OSMetaClassDefineReservedUnused(IOMapper
, 15);
49 IOMapper
* IOMapper::gSystem
= (IOMapper
*) IOMapper::kUnknown
;
54 IOMapperLock() { fWaitLock
= IOLockAlloc(); };
55 ~IOMapperLock() { IOLockFree(fWaitLock
); };
57 void lock() { IOLockLock(fWaitLock
); };
58 void unlock() { IOLockUnlock(fWaitLock
); };
59 void sleep(void *event
) { IOLockSleep(fWaitLock
, event
, THREAD_UNINT
); };
60 void wakeup(void *event
) { IOLockWakeup(fWaitLock
, event
, false); };
63 static IOMapperLock sMapperLock
;
65 bool IOMapper::start(IOService
*provider
)
67 if (!super::start(provider
))
70 if (!initHardware(provider
))
75 IOMapper::gSystem
= this;
76 sMapperLock
.wakeup(&IOMapper::gSystem
);
83 bool IOMapper::allocTable(IOByteCount size
)
88 fTableHandle
= NewARTTable(size
, &fTable
, &fTablePhys
);
89 return fTableHandle
!= 0;
95 FreeARTTable(fTableHandle
, fTableSize
);
102 void IOMapper::setMapperRequired(bool hasMapper
)
105 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
108 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
109 sMapperLock
.unlock();
110 sMapperLock
.wakeup(&IOMapper::gSystem
);
114 void IOMapper::waitForSystemMapper()
117 while ((vm_address_t
) IOMapper::gSystem
& kWaitMask
)
118 sMapperLock
.sleep(&IOMapper::gSystem
);
119 sMapperLock
.unlock();
122 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
123 ppnum_t
*pageList
, IOItemCount pageCount
)
126 iovmInsert(addr
, offset
++, *pageList
++);
129 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
130 upl_page_info_t
*pageList
, IOItemCount pageCount
)
132 for (IOItemCount i
= 0; i
< pageCount
; i
++)
133 iovmInsert(addr
, offset
+ i
, pageList
[i
].phys_addr
);
136 struct ARTTableData
{
140 #define getARTDataP(data) ((ARTTableData *) (data)->getBytesNoCopy())
143 IOMapper::NewARTTable(IOByteCount size
,
144 void ** virtAddrP
, ppnum_t
*physAddrP
)
148 vm_address_t startUpl
;
150 unsigned int dataSize
;
151 upl_page_info_t
*pl
= 0;
153 // Each UPL can deal with about one meg at the moment
154 size
= round_page_32(size
);
155 dataSize
= sizeof(ARTTableData
) + sizeof(upl_t
) * size
/ (1024 * 1024);
156 ret
= OSData::withCapacity(dataSize
);
160 // Append 0's to the buffer, in-other-words reset to nulls.
161 ret
->appendBytes(NULL
, sizeof(ARTTableData
));
162 dataP
= getARTDataP(ret
);
164 kr
= kmem_alloc_contig(kernel_map
, &startUpl
, size
, PAGE_MASK
, 0);
168 dataP
->v
= (void *) startUpl
;
172 int upl_flags
= UPL_SET_INTERNAL
| UPL_SET_LITE
173 | UPL_SET_IO_WIRE
| UPL_COPYOUT_FROM
;
174 vm_size_t iopl_size
= size
;
176 kr
= vm_map_get_upl(kernel_map
,
185 panic("IOMapper:vm_map_get_upl returned 0x%x\n");
189 if (!ret
->appendBytes(&iopl
, sizeof(upl_t
)))
192 startUpl
+= iopl_size
;
196 // Need to re-establish the dataP as the OSData may have grown.
197 dataP
= getARTDataP(ret
);
199 // Now grab the page entry of the first page and get its phys addr
200 pl
= UPL_GET_INTERNAL_PAGE_LIST(dataP
->u
[0]);
201 *physAddrP
= pl
->phys_addr
;
202 *virtAddrP
= dataP
->v
;
207 FreeARTTable(ret
, size
);
211 void IOMapper::FreeARTTable(OSData
*artHandle
, IOByteCount size
)
215 ARTTableData
*dataP
= getARTDataP(artHandle
);
217 int numupls
= ((artHandle
->getLength() - sizeof(*dataP
)) / sizeof(upl_t
));
218 for (int i
= 0; i
< numupls
; i
++)
219 kernel_upl_abort(dataP
->u
[i
], 0);
222 size
= round_page_32(size
);
223 kmem_free(kernel_map
, (vm_address_t
) dataP
->v
, size
);
225 artHandle
->release();
230 // These are C accessors to the system mapper for non-IOKit clients
231 ppnum_t
IOMapperIOVMAlloc(unsigned pages
)
233 IOMapper::checkForSystemMapper();
235 if (IOMapper::gSystem
)
236 return IOMapper::gSystem
->iovmAlloc((IOItemCount
) pages
);
241 void IOMapperIOVMFree(ppnum_t addr
, unsigned pages
)
243 if (IOMapper::gSystem
)
244 IOMapper::gSystem
->iovmFree(addr
, (IOItemCount
) pages
);
247 ppnum_t
IOMapperInsertPage(ppnum_t addr
, unsigned offset
, ppnum_t page
)
249 if (IOMapper::gSystem
) {
250 IOMapper::gSystem
->iovmInsert(addr
, (IOItemCount
) offset
, page
);
251 return addr
+ offset
;
257 void IOMapperInsertPPNPages(ppnum_t addr
, unsigned offset
,
258 ppnum_t
*pageList
, unsigned pageCount
)
260 if (!IOMapper::gSystem
)
261 panic("IOMapperInsertPPNPages no system mapper");
263 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
266 iovmInsert(addr
, (IOItemCount
) offset
, pageList
, pageCount
);
269 void IOMapperInsertUPLPages(ppnum_t addr
, unsigned offset
,
270 upl_page_info_t
*pageList
, unsigned pageCount
)
272 if (!IOMapper::gSystem
)
273 panic("IOMapperInsertUPLPages no system mapper");
275 assert(!((vm_address_t
) IOMapper::gSystem
& 3));
277 IOMapper::gSystem
->iovmInsert(addr
,
278 (IOItemCount
) offset
,
280 (IOItemCount
) pageCount
);
283 /////////////////////////////////////////////////////////////////////////////
289 /////////////////////////////////////////////////////////////////////////////
291 #include <machine/machine_routines.h>
293 UInt8
IOMappedRead8(IOPhysicalAddress address
)
295 IOMapper::checkForSystemMapper();
297 if (IOMapper::gSystem
) {
298 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
299 return (UInt8
) ml_phys_read_byte_64(addr
);
302 return (UInt8
) ml_phys_read_byte((vm_offset_t
) address
);
305 UInt16
IOMappedRead16(IOPhysicalAddress address
)
307 IOMapper::checkForSystemMapper();
309 if (IOMapper::gSystem
) {
310 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
311 return (UInt16
) ml_phys_read_half_64(addr
);
314 return (UInt16
) ml_phys_read_half((vm_offset_t
) address
);
317 UInt32
IOMappedRead32(IOPhysicalAddress address
)
319 IOMapper::checkForSystemMapper();
321 if (IOMapper::gSystem
) {
322 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
323 return (UInt32
) ml_phys_read_word_64(addr
);
326 return (UInt32
) ml_phys_read_word((vm_offset_t
) address
);
329 UInt64
IOMappedRead64(IOPhysicalAddress address
)
331 IOMapper::checkForSystemMapper();
333 if (IOMapper::gSystem
) {
334 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
335 return (UInt64
) ml_phys_read_double_64(addr
);
338 return (UInt64
) ml_phys_read_double((vm_offset_t
) address
);
341 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
)
343 IOMapper::checkForSystemMapper();
345 if (IOMapper::gSystem
) {
346 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
347 ml_phys_write_byte_64(addr
, value
);
350 ml_phys_write_byte((vm_offset_t
) address
, value
);
353 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
)
355 IOMapper::checkForSystemMapper();
357 if (IOMapper::gSystem
) {
358 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
359 ml_phys_write_half_64(addr
, value
);
362 ml_phys_write_half((vm_offset_t
) address
, value
);
365 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
)
367 IOMapper::checkForSystemMapper();
369 if (IOMapper::gSystem
) {
370 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
371 ml_phys_write_word_64(addr
, value
);
374 ml_phys_write_word((vm_offset_t
) address
, value
);
377 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
)
379 IOMapper::checkForSystemMapper();
381 if (IOMapper::gSystem
) {
382 addr64_t addr
= IOMapper::gSystem
->mapAddr(address
);
383 ml_phys_write_double_64(addr
, value
);
386 ml_phys_write_double((vm_offset_t
) address
, value
);