]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOMapper.cpp
xnu-792.25.20.tar.gz
[apple/xnu.git] / iokit / Kernel / IOMapper.cpp
CommitLineData
55e303ae 1/*
91447636 2 * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
55e303ae 3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
55e303ae 5 *
6601e61a
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
8f6c56a5 11 *
6601e61a
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
55e303ae
A
21 */
22#include <IOKit/IOLib.h>
23#include <IOKit/IOMapper.h>
24#include <libkern/c++/OSData.h>
25
0c530ab8
A
26#include "IOCopyMapper.h"
27
28__BEGIN_DECLS
29extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
30__END_DECLS
31
55e303ae
A
32#define super IOService
33OSDefineMetaClassAndAbstractStructors(IOMapper, IOService);
34
0c530ab8 35OSMetaClassDefineReservedUsed(IOMapper, 0);
55e303ae
A
36OSMetaClassDefineReservedUnused(IOMapper, 1);
37OSMetaClassDefineReservedUnused(IOMapper, 2);
38OSMetaClassDefineReservedUnused(IOMapper, 3);
39OSMetaClassDefineReservedUnused(IOMapper, 4);
40OSMetaClassDefineReservedUnused(IOMapper, 5);
41OSMetaClassDefineReservedUnused(IOMapper, 6);
42OSMetaClassDefineReservedUnused(IOMapper, 7);
43OSMetaClassDefineReservedUnused(IOMapper, 8);
44OSMetaClassDefineReservedUnused(IOMapper, 9);
45OSMetaClassDefineReservedUnused(IOMapper, 10);
46OSMetaClassDefineReservedUnused(IOMapper, 11);
47OSMetaClassDefineReservedUnused(IOMapper, 12);
48OSMetaClassDefineReservedUnused(IOMapper, 13);
49OSMetaClassDefineReservedUnused(IOMapper, 14);
50OSMetaClassDefineReservedUnused(IOMapper, 15);
51
52IOMapper * IOMapper::gSystem = (IOMapper *) IOMapper::kUnknown;
53
54class IOMapperLock {
55 IOLock *fWaitLock;
56public:
57 IOMapperLock() { fWaitLock = IOLockAlloc(); };
58 ~IOMapperLock() { IOLockFree(fWaitLock); };
59
60 void lock() { IOLockLock(fWaitLock); };
61 void unlock() { IOLockUnlock(fWaitLock); };
62 void sleep(void *event) { IOLockSleep(fWaitLock, event, THREAD_UNINT); };
63 void wakeup(void *event) { IOLockWakeup(fWaitLock, event, false); };
64};
65
66static IOMapperLock sMapperLock;
67
68bool IOMapper::start(IOService *provider)
69{
70 if (!super::start(provider))
71 return false;
72
73 if (!initHardware(provider))
74 return false;
75
76 if (fIsSystem) {
77 sMapperLock.lock();
78 IOMapper::gSystem = this;
79 sMapperLock.wakeup(&IOMapper::gSystem);
80 sMapperLock.unlock();
81 }
82
83 return true;
84}
85
86bool IOMapper::allocTable(IOByteCount size)
87{
88 assert(!fTable);
89
90 fTableSize = size;
91 fTableHandle = NewARTTable(size, &fTable, &fTablePhys);
92 return fTableHandle != 0;
93}
94
95void IOMapper::free()
96{
97 if (fTableHandle) {
98 FreeARTTable(fTableHandle, fTableSize);
99 fTableHandle = 0;
100 }
101
102 super::free();
103}
104
105void IOMapper::setMapperRequired(bool hasMapper)
106{
107 if (hasMapper)
108 IOMapper::gSystem = (IOMapper *) kHasMapper;
109 else {
110 sMapperLock.lock();
111 IOMapper::gSystem = (IOMapper *) kNoMapper;
112 sMapperLock.unlock();
113 sMapperLock.wakeup(&IOMapper::gSystem);
114 }
115}
116
117void IOMapper::waitForSystemMapper()
118{
119 sMapperLock.lock();
120 while ((vm_address_t) IOMapper::gSystem & kWaitMask)
121 sMapperLock.sleep(&IOMapper::gSystem);
122 sMapperLock.unlock();
123}
124
125void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset,
126 ppnum_t *pageList, IOItemCount pageCount)
127{
128 while (pageCount--)
129 iovmInsert(addr, offset++, *pageList++);
130}
131
132void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset,
133 upl_page_info_t *pageList, IOItemCount pageCount)
134{
135 for (IOItemCount i = 0; i < pageCount; i++)
136 iovmInsert(addr, offset + i, pageList[i].phys_addr);
137}
138
0c530ab8
A
139OSData * IOMapper::
140NewARTTable(IOByteCount size, void ** virtAddrP, ppnum_t *physAddrP)
6601e61a 141{
0c530ab8
A
142 if (!virtAddrP || !physAddrP)
143 return 0;
144
55e303ae 145 kern_return_t kr;
0c530ab8 146 vm_address_t address;
55e303ae 147
55e303ae 148 size = round_page_32(size);
0c530ab8
A
149 kr = kmem_alloc_contig(kernel_map, &address, size, PAGE_MASK, 0);
150 if (kr)
55e303ae
A
151 return 0;
152
0c530ab8
A
153 ppnum_t pagenum = pmap_find_phys(kernel_pmap, (addr64_t) address);
154 if (pagenum)
155 *physAddrP = pagenum;
156 else {
157 FreeARTTable((OSData *) address, size);
158 address = 0;
159 }
55e303ae 160
0c530ab8 161 *virtAddrP = (void *) address;
55e303ae 162
0c530ab8 163 return (OSData *) address;
55e303ae
A
164}
165
166void IOMapper::FreeARTTable(OSData *artHandle, IOByteCount size)
167{
0c530ab8 168 vm_address_t address = (vm_address_t) artHandle;
21362eb3 169
0c530ab8
A
170 size = round_page_32(size);
171 kmem_free(kernel_map, address, size); // Just panic if address is 0
172}
6601e61a 173
0c530ab8
A
174bool IOMapper::getBypassMask(addr64_t *maskP) const
175{
176 return false;
55e303ae
A
177}
178
179__BEGIN_DECLS
180
181// These are C accessors to the system mapper for non-IOKit clients
182ppnum_t IOMapperIOVMAlloc(unsigned pages)
183{
184 IOMapper::checkForSystemMapper();
185
186 if (IOMapper::gSystem)
187 return IOMapper::gSystem->iovmAlloc((IOItemCount) pages);
188 else
189 return 0;
190}
191
192void IOMapperIOVMFree(ppnum_t addr, unsigned pages)
193{
194 if (IOMapper::gSystem)
195 IOMapper::gSystem->iovmFree(addr, (IOItemCount) pages);
196}
197
198ppnum_t IOMapperInsertPage(ppnum_t addr, unsigned offset, ppnum_t page)
199{
200 if (IOMapper::gSystem) {
201 IOMapper::gSystem->iovmInsert(addr, (IOItemCount) offset, page);
202 return addr + offset;
203 }
204 else
205 return page;
206}
207
208void IOMapperInsertPPNPages(ppnum_t addr, unsigned offset,
209 ppnum_t *pageList, unsigned pageCount)
210{
211 if (!IOMapper::gSystem)
212 panic("IOMapperInsertPPNPages no system mapper");
213 else
214 assert(!((vm_address_t) IOMapper::gSystem & 3));
215
216 IOMapper::gSystem->
217 iovmInsert(addr, (IOItemCount) offset, pageList, pageCount);
218}
219
220void IOMapperInsertUPLPages(ppnum_t addr, unsigned offset,
221 upl_page_info_t *pageList, unsigned pageCount)
222{
223 if (!IOMapper::gSystem)
224 panic("IOMapperInsertUPLPages no system mapper");
225 else
226 assert(!((vm_address_t) IOMapper::gSystem & 3));
227
228 IOMapper::gSystem->iovmInsert(addr,
229 (IOItemCount) offset,
230 pageList,
231 (IOItemCount) pageCount);
232}
233
234/////////////////////////////////////////////////////////////////////////////
235//
236//
237// IOLib.h APIs
238//
239//
240/////////////////////////////////////////////////////////////////////////////
241
242#include <machine/machine_routines.h>
243
244UInt8 IOMappedRead8(IOPhysicalAddress address)
245{
246 IOMapper::checkForSystemMapper();
247
248 if (IOMapper::gSystem) {
249 addr64_t addr = IOMapper::gSystem->mapAddr(address);
250 return (UInt8) ml_phys_read_byte_64(addr);
251 }
252 else
253 return (UInt8) ml_phys_read_byte((vm_offset_t) address);
254}
255
256UInt16 IOMappedRead16(IOPhysicalAddress address)
257{
258 IOMapper::checkForSystemMapper();
259
260 if (IOMapper::gSystem) {
261 addr64_t addr = IOMapper::gSystem->mapAddr(address);
262 return (UInt16) ml_phys_read_half_64(addr);
263 }
264 else
265 return (UInt16) ml_phys_read_half((vm_offset_t) address);
266}
267
268UInt32 IOMappedRead32(IOPhysicalAddress address)
269{
270 IOMapper::checkForSystemMapper();
271
272 if (IOMapper::gSystem) {
273 addr64_t addr = IOMapper::gSystem->mapAddr(address);
274 return (UInt32) ml_phys_read_word_64(addr);
275 }
276 else
277 return (UInt32) ml_phys_read_word((vm_offset_t) address);
278}
279
280UInt64 IOMappedRead64(IOPhysicalAddress address)
281{
282 IOMapper::checkForSystemMapper();
283
284 if (IOMapper::gSystem) {
285 addr64_t addr = IOMapper::gSystem->mapAddr(address);
286 return (UInt64) ml_phys_read_double_64(addr);
287 }
288 else
289 return (UInt64) ml_phys_read_double((vm_offset_t) address);
290}
291
292void IOMappedWrite8(IOPhysicalAddress address, UInt8 value)
293{
294 IOMapper::checkForSystemMapper();
295
296 if (IOMapper::gSystem) {
297 addr64_t addr = IOMapper::gSystem->mapAddr(address);
298 ml_phys_write_byte_64(addr, value);
299 }
300 else
301 ml_phys_write_byte((vm_offset_t) address, value);
302}
303
304void IOMappedWrite16(IOPhysicalAddress address, UInt16 value)
305{
306 IOMapper::checkForSystemMapper();
307
308 if (IOMapper::gSystem) {
309 addr64_t addr = IOMapper::gSystem->mapAddr(address);
310 ml_phys_write_half_64(addr, value);
311 }
312 else
313 ml_phys_write_half((vm_offset_t) address, value);
314}
315
316void IOMappedWrite32(IOPhysicalAddress address, UInt32 value)
317{
318 IOMapper::checkForSystemMapper();
319
320 if (IOMapper::gSystem) {
321 addr64_t addr = IOMapper::gSystem->mapAddr(address);
322 ml_phys_write_word_64(addr, value);
323 }
324 else
325 ml_phys_write_word((vm_offset_t) address, value);
326}
327
328void IOMappedWrite64(IOPhysicalAddress address, UInt64 value)
329{
330 IOMapper::checkForSystemMapper();
331
332 if (IOMapper::gSystem) {
333 addr64_t addr = IOMapper::gSystem->mapAddr(address);
334 ml_phys_write_double_64(addr, value);
335 }
336 else
337 ml_phys_write_double((vm_offset_t) address, value);
338}
339
0c530ab8
A
340mach_vm_address_t IOMallocPhysical(mach_vm_size_t size, mach_vm_address_t mask)
341{
342 mach_vm_address_t address = 0;
343 if (gIOCopyMapper)
344 {
345 address = ptoa_64(gIOCopyMapper->iovmAlloc(atop_64(round_page(size))));
346 }
347
348 return (address);
349}
350
351void IOFreePhysical(mach_vm_address_t address, mach_vm_size_t size)
352{
353 if (gIOCopyMapper)
354 {
355 gIOCopyMapper->iovmFree(atop_64(address), atop_64(round_page(size)));
356 }
357}
358
359
55e303ae 360__END_DECLS