]> git.saurik.com Git - apple/xnu.git/blame_incremental - iokit/Kernel/IOMapper.cpp
xnu-3789.31.2.tar.gz
[apple/xnu.git] / iokit / Kernel / IOMapper.cpp
... / ...
CommitLineData
1/*
2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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>
33#include "IOKitKernelInternal.h"
34
35__BEGIN_DECLS
36extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
37__END_DECLS
38
39#define super IOService
40OSDefineMetaClassAndAbstractStructors(IOMapper, IOService);
41
42OSMetaClassDefineReservedUnused(IOMapper, 0);
43OSMetaClassDefineReservedUnused(IOMapper, 1);
44OSMetaClassDefineReservedUnused(IOMapper, 2);
45OSMetaClassDefineReservedUnused(IOMapper, 3);
46OSMetaClassDefineReservedUnused(IOMapper, 4);
47OSMetaClassDefineReservedUnused(IOMapper, 5);
48OSMetaClassDefineReservedUnused(IOMapper, 6);
49OSMetaClassDefineReservedUnused(IOMapper, 7);
50OSMetaClassDefineReservedUnused(IOMapper, 8);
51OSMetaClassDefineReservedUnused(IOMapper, 9);
52OSMetaClassDefineReservedUnused(IOMapper, 10);
53OSMetaClassDefineReservedUnused(IOMapper, 11);
54OSMetaClassDefineReservedUnused(IOMapper, 12);
55OSMetaClassDefineReservedUnused(IOMapper, 13);
56OSMetaClassDefineReservedUnused(IOMapper, 14);
57OSMetaClassDefineReservedUnused(IOMapper, 15);
58
59IOMapper * IOMapper::gSystem = (IOMapper *) IOMapper::kUnknown;
60
61class IOMapperLock {
62 IOLock *fWaitLock;
63public:
64 IOMapperLock() { fWaitLock = IOLockAlloc(); }
65 ~IOMapperLock() { IOLockFree(fWaitLock); }
66
67 void lock() { IOLockLock(fWaitLock); }
68 void unlock() { IOLockUnlock(fWaitLock); }
69 void sleep(void *event) { IOLockSleep(fWaitLock, event, THREAD_UNINT); }
70 void wakeup(void *event) { IOLockWakeup(fWaitLock, event, false); }
71};
72
73static IOMapperLock sMapperLock;
74
75bool IOMapper::start(IOService *provider)
76{
77 OSObject * obj;
78 if (!super::start(provider))
79 return false;
80
81 if (!initHardware(provider))
82 return false;
83
84 fPageSize = getPageSize();
85
86 if (fIsSystem) {
87 sMapperLock.lock();
88 IOMapper::gSystem = this;
89 sMapperLock.wakeup(&IOMapper::gSystem);
90 sMapperLock.unlock();
91 }
92
93 if (provider)
94 {
95 obj = provider->getProperty("iommu-id");
96 if (!obj)
97 obj = provider->getProperty("AAPL,phandle");
98 if (obj)
99 setProperty(gIOMapperIDKey, obj);
100 }
101 return true;
102}
103
104void IOMapper::free()
105{
106 super::free();
107}
108
109void IOMapper::setMapperRequired(bool hasMapper)
110{
111 if (hasMapper)
112 IOMapper::gSystem = (IOMapper *) kHasMapper;
113 else {
114 sMapperLock.lock();
115 IOMapper::gSystem = (IOMapper *) kNoMapper;
116 sMapperLock.unlock();
117 sMapperLock.wakeup(&IOMapper::gSystem);
118 }
119}
120
121void IOMapper::waitForSystemMapper()
122{
123 sMapperLock.lock();
124 while ((uintptr_t) IOMapper::gSystem & kWaitMask)
125 {
126 OSReportWithBacktrace("waitForSystemMapper");
127 sMapperLock.sleep(&IOMapper::gSystem);
128 }
129 sMapperLock.unlock();
130}
131
132IOMapper * IOMapper::copyMapperForDevice(IOService * device)
133{
134 return copyMapperForDeviceWithIndex(device, 0);
135}
136
137IOMapper * IOMapper::copyMapperForDeviceWithIndex(IOService * device, unsigned int index)
138{
139 OSData *data;
140 OSObject * obj;
141 IOMapper * mapper = NULL;
142 OSDictionary * matching;
143
144 obj = device->copyProperty("iommu-parent");
145 if (!obj)
146 return (NULL);
147
148 if ((mapper = OSDynamicCast(IOMapper, obj)))
149 return (mapper);
150
151 if ((data = OSDynamicCast(OSData, obj)))
152 {
153 if (index >= data->getLength() / sizeof(UInt32))
154 goto done;
155
156 data = OSData::withBytesNoCopy((UInt32 *)data->getBytesNoCopy() + index, sizeof(UInt32));
157 if (!data)
158 goto done;
159
160 matching = IOService::propertyMatching(gIOMapperIDKey, data);
161 data->release();
162 }
163 else
164 matching = IOService::propertyMatching(gIOMapperIDKey, obj);
165
166 if (matching)
167 {
168 mapper = OSDynamicCast(IOMapper, IOService::waitForMatchingService(matching));
169 matching->release();
170 }
171
172done:
173 if (obj)
174 obj->release();
175 return (mapper);
176}
177
178__BEGIN_DECLS
179
180// These are C accessors to the system mapper for non-IOKit clients
181ppnum_t IOMapperIOVMAlloc(unsigned pages)
182{
183 IOReturn ret;
184 uint64_t dmaAddress, dmaLength;
185
186 IOMapper::checkForSystemMapper();
187
188 ret = kIOReturnUnsupported;
189 if (IOMapper::gSystem)
190 {
191 ret = IOMapper::gSystem->iovmMapMemory(
192 NULL, 0, ptoa_64(pages),
193 (kIODMAMapReadAccess | kIODMAMapWriteAccess),
194 NULL, NULL, NULL,
195 &dmaAddress, &dmaLength);
196 }
197
198 if (kIOReturnSuccess == ret) return (atop_64(dmaAddress));
199 return (0);
200}
201
202void IOMapperIOVMFree(ppnum_t addr, unsigned pages)
203{
204 if (IOMapper::gSystem)
205 {
206 IOMapper::gSystem->iovmUnmapMemory(NULL, NULL, ptoa_64(addr), ptoa_64(pages));
207 }
208}
209
210ppnum_t IOMapperInsertPage(ppnum_t addr, unsigned offset, ppnum_t page)
211{
212 if (!IOMapper::gSystem) return (page);
213 if (!addr) panic("!addr");
214 IOMapper::gSystem->iovmInsert((kIODMAMapReadAccess | kIODMAMapWriteAccess),
215 ptoa_64(addr), ptoa_64(offset), ptoa_64(page), ptoa_64(1));
216 return (addr + offset);
217}
218
219/////////////////////////////////////////////////////////////////////////////
220//
221//
222// IOLib.h APIs
223//
224//
225/////////////////////////////////////////////////////////////////////////////
226
227#include <machine/machine_routines.h>
228
229UInt8 IOMappedRead8(IOPhysicalAddress address)
230{
231 IOMapper::checkForSystemMapper();
232
233 if (IOMapper::gSystem) {
234 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
235 return (UInt8) ml_phys_read_byte_64(addr);
236 }
237 else
238 return (UInt8) ml_phys_read_byte((vm_offset_t) address);
239}
240
241UInt16 IOMappedRead16(IOPhysicalAddress address)
242{
243 IOMapper::checkForSystemMapper();
244
245 if (IOMapper::gSystem) {
246 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
247 return (UInt16) ml_phys_read_half_64(addr);
248 }
249 else
250 return (UInt16) ml_phys_read_half((vm_offset_t) address);
251}
252
253UInt32 IOMappedRead32(IOPhysicalAddress address)
254{
255 IOMapper::checkForSystemMapper();
256
257 if (IOMapper::gSystem) {
258 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
259 return (UInt32) ml_phys_read_word_64(addr);
260 }
261 else
262 return (UInt32) ml_phys_read_word((vm_offset_t) address);
263}
264
265UInt64 IOMappedRead64(IOPhysicalAddress address)
266{
267 IOMapper::checkForSystemMapper();
268
269 if (IOMapper::gSystem) {
270 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
271 return (UInt64) ml_phys_read_double_64(addr);
272 }
273 else
274 return (UInt64) ml_phys_read_double((vm_offset_t) address);
275}
276
277void IOMappedWrite8(IOPhysicalAddress address, UInt8 value)
278{
279 IOMapper::checkForSystemMapper();
280
281 if (IOMapper::gSystem) {
282 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
283 ml_phys_write_byte_64(addr, value);
284 }
285 else
286 ml_phys_write_byte((vm_offset_t) address, value);
287}
288
289void IOMappedWrite16(IOPhysicalAddress address, UInt16 value)
290{
291 IOMapper::checkForSystemMapper();
292
293 if (IOMapper::gSystem) {
294 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
295 ml_phys_write_half_64(addr, value);
296 }
297 else
298 ml_phys_write_half((vm_offset_t) address, value);
299}
300
301void IOMappedWrite32(IOPhysicalAddress address, UInt32 value)
302{
303 IOMapper::checkForSystemMapper();
304
305 if (IOMapper::gSystem) {
306 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
307 ml_phys_write_word_64(addr, value);
308 }
309 else
310 ml_phys_write_word((vm_offset_t) address, value);
311}
312
313void IOMappedWrite64(IOPhysicalAddress address, UInt64 value)
314{
315 IOMapper::checkForSystemMapper();
316
317 if (IOMapper::gSystem) {
318 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
319 ml_phys_write_double_64(addr, value);
320 }
321 else
322 ml_phys_write_double((vm_offset_t) address, value);
323}
324
325__END_DECLS