2 * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <IOKit/IOLib.h>
24 #include <IOKit/IOMapper.h>
25 #include <libkern/c++/OSData.h>
27 #define super IOService
28 OSDefineMetaClassAndAbstractStructors(IOMapper
, IOService
);
30 OSMetaClassDefineReservedUnused(IOMapper
, 0);
31 OSMetaClassDefineReservedUnused(IOMapper
, 1);
32 OSMetaClassDefineReservedUnused(IOMapper
, 2);
33 OSMetaClassDefineReservedUnused(IOMapper
, 3);
34 OSMetaClassDefineReservedUnused(IOMapper
, 4);
35 OSMetaClassDefineReservedUnused(IOMapper
, 5);
36 OSMetaClassDefineReservedUnused(IOMapper
, 6);
37 OSMetaClassDefineReservedUnused(IOMapper
, 7);
38 OSMetaClassDefineReservedUnused(IOMapper
, 8);
39 OSMetaClassDefineReservedUnused(IOMapper
, 9);
40 OSMetaClassDefineReservedUnused(IOMapper
, 10);
41 OSMetaClassDefineReservedUnused(IOMapper
, 11);
42 OSMetaClassDefineReservedUnused(IOMapper
, 12);
43 OSMetaClassDefineReservedUnused(IOMapper
, 13);
44 OSMetaClassDefineReservedUnused(IOMapper
, 14);
45 OSMetaClassDefineReservedUnused(IOMapper
, 15);
47 IOMapper
* IOMapper::gSystem
= (IOMapper
*) IOMapper::kUnknown
;
52 IOMapperLock() { fWaitLock
= IOLockAlloc(); };
53 ~IOMapperLock() { IOLockFree(fWaitLock
); };
55 void lock() { IOLockLock(fWaitLock
); };
56 void unlock() { IOLockUnlock(fWaitLock
); };
57 void sleep(void *event
) { IOLockSleep(fWaitLock
, event
, THREAD_UNINT
); };
58 void wakeup(void *event
) { IOLockWakeup(fWaitLock
, event
, false); };
61 static IOMapperLock sMapperLock
;
63 bool IOMapper::start(IOService
*provider
)
65 if (!super::start(provider
))
68 if (!initHardware(provider
))
73 IOMapper::gSystem
= this;
74 sMapperLock
.wakeup(&IOMapper::gSystem
);
81 bool IOMapper::allocTable(IOByteCount size
)
86 fTableHandle
= NewARTTable(size
, &fTable
, &fTablePhys
);
87 return fTableHandle
!= 0;
93 FreeARTTable(fTableHandle
, fTableSize
);
100 void IOMapper::setMapperRequired(bool hasMapper
)
103 IOMapper::gSystem
= (IOMapper
*) kHasMapper
;
106 IOMapper::gSystem
= (IOMapper
*) kNoMapper
;
107 sMapperLock
.unlock();
108 sMapperLock
.wakeup(&IOMapper::gSystem
);
112 void IOMapper::waitForSystemMapper()
115 while ((vm_address_t
) IOMapper::gSystem
& kWaitMask
)
116 sMapperLock
.sleep(&IOMapper::gSystem
);
117 sMapperLock
.unlock();
120 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
121 ppnum_t
*pageList
, IOItemCount pageCount
)
124 iovmInsert(addr
, offset
++, *pageList
++);
127 void IOMapper::iovmInsert(ppnum_t addr
, IOItemCount offset
,
128 upl_page_info_t
*pageList
, IOItemCount pageCount
)
130 for (IOItemCount i
= 0; i
< pageCount
; i
++)
131 iovmInsert(addr
, offset
+ i
, pageList
[i
].phys_addr
);
134 struct ARTTableData
{
138 #define getARTDataP(data) ((ARTTableData *) (data)->getBytesNoCopy())
141 IOMapper::NewARTTable(IOByteCount size
,
142 void ** virtAddrP
, ppnum_t
*physAddrP
)
146 vm_address_t startUpl
;
148 unsigned int dataSize
;
149 upl_page_info_t
*pl
= 0;
151 // Each UPL can deal with about one meg at the moment
152 size
= round_page_32(size
);
153 dataSize
= sizeof(ARTTableData
) + sizeof(upl_t
) * size
/ (1024 * 1024);
154 ret
= OSData::withCapacity(dataSize
);
158 // Append 0's to the buffer, in-other-words reset to nulls.
159 ret
->appendBytes(NULL
, sizeof(ARTTableData
));
160 dataP
= getARTDataP(ret
);
162 kr
= kmem_alloc_contig(kernel_map
, &startUpl
, size
, PAGE_MASK
, 0);
166 dataP
->v
= (void *) startUpl
;
170 int upl_flags
= UPL_SET_INTERNAL
| UPL_SET_LITE
171 | UPL_SET_IO_WIRE
| UPL_COPYOUT_FROM
;
172 vm_size_t iopl_size
= size
;
174 kr
= vm_map_get_upl(kernel_map
,
175 (vm_map_offset_t
)startUpl
,
183 panic("IOMapper:vm_map_get_upl returned 0x%x\n");
187 if (!ret
->appendBytes(&iopl
, sizeof(upl_t
)))
190 startUpl
+= iopl_size
;
194 // Need to re-establish the dataP as the OSData may have grown.
195 dataP
= getARTDataP(ret
);
197 // Now grab the page entry of the first page and get its phys addr
198 pl
= UPL_GET_INTERNAL_PAGE_LIST(dataP
->u
[0]);
199 *physAddrP
= pl
->phys_addr
;
200 *virtAddrP
= dataP
->v
;
205 FreeARTTable(ret
, size
);
209 void IOMapper::FreeARTTable(OSData
*artHandle
, IOByteCount size
)
213 ARTTableData
*dataP
= getARTDataP(artHandle
);
215 int numupls
= ((artHandle
->getLength() - sizeof(*dataP
)) / sizeof(upl_t
));
216 for (int i
= 0; i
< numupls
; i
++) {
217 upl_abort(dataP
->u
[i
], 0);
218 upl_deallocate(dataP
->u
[i
]);
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
);