]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOMapper.cpp
xnu-3789.1.32.tar.gz
[apple/xnu.git] / iokit / Kernel / IOMapper.cpp
CommitLineData
55e303ae 1/*
39037602 2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
55e303ae 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
55e303ae 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
55e303ae
A
27 */
28#include <IOKit/IOLib.h>
29#include <IOKit/IOMapper.h>
b0d623f7 30#include <IOKit/IODMACommand.h>
55e303ae 31#include <libkern/c++/OSData.h>
99c3a104 32#include <libkern/OSDebug.h>
3e170ce0 33#include "IOKitKernelInternal.h"
55e303ae 34
0c530ab8
A
35__BEGIN_DECLS
36extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
37__END_DECLS
38
55e303ae
A
39#define super IOService
40OSDefineMetaClassAndAbstractStructors(IOMapper, IOService);
41
3e170ce0
A
42OSMetaClassDefineReservedUnused(IOMapper, 0);
43OSMetaClassDefineReservedUnused(IOMapper, 1);
44OSMetaClassDefineReservedUnused(IOMapper, 2);
45OSMetaClassDefineReservedUnused(IOMapper, 3);
55e303ae
A
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:
39037602
A
64 IOMapperLock() { fWaitLock = IOLockAlloc(); }
65 ~IOMapperLock() { IOLockFree(fWaitLock); }
55e303ae 66
39037602
A
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); }
55e303ae
A
71};
72
73static IOMapperLock sMapperLock;
74
75bool IOMapper::start(IOService *provider)
76{
b0d623f7 77 OSObject * obj;
55e303ae
A
78 if (!super::start(provider))
79 return false;
80
81 if (!initHardware(provider))
82 return false;
83
3e170ce0
A
84 fPageSize = getPageSize();
85
55e303ae
A
86 if (fIsSystem) {
87 sMapperLock.lock();
88 IOMapper::gSystem = this;
89 sMapperLock.wakeup(&IOMapper::gSystem);
90 sMapperLock.unlock();
91 }
92
b0d623f7
A
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 }
55e303ae
A
101 return true;
102}
103
55e303ae
A
104void IOMapper::free()
105{
55e303ae
A
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();
b0d623f7 124 while ((uintptr_t) IOMapper::gSystem & kWaitMask)
99c3a104
A
125 {
126 OSReportWithBacktrace("waitForSystemMapper");
55e303ae 127 sMapperLock.sleep(&IOMapper::gSystem);
99c3a104 128 }
55e303ae
A
129 sMapperLock.unlock();
130}
131
b0d623f7
A
132IOMapper * IOMapper::copyMapperForDevice(IOService * device)
133{
39236c6e
A
134 return copyMapperForDeviceWithIndex(device, 0);
135}
136
137IOMapper * IOMapper::copyMapperForDeviceWithIndex(IOService * device, unsigned int index)
138{
139 OSData *data;
b0d623f7 140 OSObject * obj;
39236c6e 141 IOMapper * mapper = NULL;
b0d623f7
A
142 OSDictionary * matching;
143
144 obj = device->copyProperty("iommu-parent");
145 if (!obj)
39236c6e 146 return (NULL);
b0d623f7
A
147
148 if ((mapper = OSDynamicCast(IOMapper, obj)))
39236c6e 149 return (mapper);
b0d623f7 150
39236c6e 151 if ((data = OSDynamicCast(OSData, obj)))
b0d623f7 152 {
39236c6e
A
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();
b0d623f7 162 }
b0d623f7 163 else
39236c6e
A
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();
b0d623f7
A
175 return (mapper);
176}
177
55e303ae
A
178__BEGIN_DECLS
179
180// These are C accessors to the system mapper for non-IOKit clients
181ppnum_t IOMapperIOVMAlloc(unsigned pages)
182{
3e170ce0
A
183 IOReturn ret;
184 uint64_t dmaAddress, dmaLength;
185
55e303ae
A
186 IOMapper::checkForSystemMapper();
187
3e170ce0 188 ret = kIOReturnUnsupported;
55e303ae 189 if (IOMapper::gSystem)
3e170ce0
A
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);
55e303ae
A
200}
201
202void IOMapperIOVMFree(ppnum_t addr, unsigned pages)
203{
204 if (IOMapper::gSystem)
3e170ce0
A
205 {
206 IOMapper::gSystem->iovmUnmapMemory(NULL, NULL, ptoa_64(addr), ptoa_64(pages));
55e303ae 207 }
55e303ae
A
208}
209
3e170ce0 210ppnum_t IOMapperInsertPage(ppnum_t addr, unsigned offset, ppnum_t page)
55e303ae 211{
3e170ce0
A
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);
55e303ae
A
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) {
3e170ce0 234 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 246 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 258 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 270 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 282 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 294 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 306 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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) {
3e170ce0 318 addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
55e303ae
A
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