]>
Commit | Line | Data |
---|---|---|
55e303ae A |
1 | /* |
2 | * Copyright (c) 1998-2003 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 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. | |
55e303ae | 11 | * |
e5568f75 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 | |
55e303ae A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 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. | |
55e303ae A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | #include <IOKit/IOLib.h> | |
23 | #include <IOKit/IOMapper.h> | |
24 | #include <libkern/c++/OSData.h> | |
25 | ||
26 | #define super IOService | |
27 | OSDefineMetaClassAndAbstractStructors(IOMapper, IOService); | |
28 | ||
29 | OSMetaClassDefineReservedUnused(IOMapper, 0); | |
30 | OSMetaClassDefineReservedUnused(IOMapper, 1); | |
31 | OSMetaClassDefineReservedUnused(IOMapper, 2); | |
32 | OSMetaClassDefineReservedUnused(IOMapper, 3); | |
33 | OSMetaClassDefineReservedUnused(IOMapper, 4); | |
34 | OSMetaClassDefineReservedUnused(IOMapper, 5); | |
35 | OSMetaClassDefineReservedUnused(IOMapper, 6); | |
36 | OSMetaClassDefineReservedUnused(IOMapper, 7); | |
37 | OSMetaClassDefineReservedUnused(IOMapper, 8); | |
38 | OSMetaClassDefineReservedUnused(IOMapper, 9); | |
39 | OSMetaClassDefineReservedUnused(IOMapper, 10); | |
40 | OSMetaClassDefineReservedUnused(IOMapper, 11); | |
41 | OSMetaClassDefineReservedUnused(IOMapper, 12); | |
42 | OSMetaClassDefineReservedUnused(IOMapper, 13); | |
43 | OSMetaClassDefineReservedUnused(IOMapper, 14); | |
44 | OSMetaClassDefineReservedUnused(IOMapper, 15); | |
45 | ||
46 | IOMapper * IOMapper::gSystem = (IOMapper *) IOMapper::kUnknown; | |
47 | ||
48 | class IOMapperLock { | |
49 | IOLock *fWaitLock; | |
50 | public: | |
51 | IOMapperLock() { fWaitLock = IOLockAlloc(); }; | |
52 | ~IOMapperLock() { IOLockFree(fWaitLock); }; | |
53 | ||
54 | void lock() { IOLockLock(fWaitLock); }; | |
55 | void unlock() { IOLockUnlock(fWaitLock); }; | |
56 | void sleep(void *event) { IOLockSleep(fWaitLock, event, THREAD_UNINT); }; | |
57 | void wakeup(void *event) { IOLockWakeup(fWaitLock, event, false); }; | |
58 | }; | |
59 | ||
60 | static IOMapperLock sMapperLock; | |
61 | ||
62 | bool IOMapper::start(IOService *provider) | |
63 | { | |
64 | if (!super::start(provider)) | |
65 | return false; | |
66 | ||
67 | if (!initHardware(provider)) | |
68 | return false; | |
69 | ||
70 | if (fIsSystem) { | |
71 | sMapperLock.lock(); | |
72 | IOMapper::gSystem = this; | |
73 | sMapperLock.wakeup(&IOMapper::gSystem); | |
74 | sMapperLock.unlock(); | |
75 | } | |
76 | ||
77 | return true; | |
78 | } | |
79 | ||
80 | bool IOMapper::allocTable(IOByteCount size) | |
81 | { | |
82 | assert(!fTable); | |
83 | ||
84 | fTableSize = size; | |
85 | fTableHandle = NewARTTable(size, &fTable, &fTablePhys); | |
86 | return fTableHandle != 0; | |
87 | } | |
88 | ||
89 | void IOMapper::free() | |
90 | { | |
91 | if (fTableHandle) { | |
92 | FreeARTTable(fTableHandle, fTableSize); | |
93 | fTableHandle = 0; | |
94 | } | |
95 | ||
96 | super::free(); | |
97 | } | |
98 | ||
99 | void IOMapper::setMapperRequired(bool hasMapper) | |
100 | { | |
101 | if (hasMapper) | |
102 | IOMapper::gSystem = (IOMapper *) kHasMapper; | |
103 | else { | |
104 | sMapperLock.lock(); | |
105 | IOMapper::gSystem = (IOMapper *) kNoMapper; | |
106 | sMapperLock.unlock(); | |
107 | sMapperLock.wakeup(&IOMapper::gSystem); | |
108 | } | |
109 | } | |
110 | ||
111 | void IOMapper::waitForSystemMapper() | |
112 | { | |
113 | sMapperLock.lock(); | |
114 | while ((vm_address_t) IOMapper::gSystem & kWaitMask) | |
115 | sMapperLock.sleep(&IOMapper::gSystem); | |
116 | sMapperLock.unlock(); | |
117 | } | |
118 | ||
119 | void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset, | |
120 | ppnum_t *pageList, IOItemCount pageCount) | |
121 | { | |
122 | while (pageCount--) | |
123 | iovmInsert(addr, offset++, *pageList++); | |
124 | } | |
125 | ||
126 | void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset, | |
127 | upl_page_info_t *pageList, IOItemCount pageCount) | |
128 | { | |
129 | for (IOItemCount i = 0; i < pageCount; i++) | |
130 | iovmInsert(addr, offset + i, pageList[i].phys_addr); | |
131 | } | |
132 | ||
133 | struct ARTTableData { | |
134 | void *v; | |
135 | upl_t u[0]; | |
136 | }; | |
137 | #define getARTDataP(data) ((ARTTableData *) (data)->getBytesNoCopy()) | |
138 | ||
139 | OSData * | |
140 | IOMapper::NewARTTable(IOByteCount size, | |
141 | void ** virtAddrP, ppnum_t *physAddrP) | |
142 | { | |
143 | OSData *ret; | |
144 | kern_return_t kr; | |
145 | vm_address_t startUpl; | |
146 | ARTTableData *dataP; | |
147 | unsigned int dataSize; | |
148 | upl_page_info_t *pl = 0; | |
149 | ||
150 | // Each UPL can deal with about one meg at the moment | |
151 | size = round_page_32(size); | |
152 | dataSize = sizeof(ARTTableData) + sizeof(upl_t) * size / (1024 * 1024); | |
153 | ret = OSData::withCapacity(dataSize); | |
154 | if (!ret) | |
155 | return 0; | |
156 | ||
157 | // Append 0's to the buffer, in-other-words reset to nulls. | |
158 | ret->appendBytes(NULL, sizeof(ARTTableData)); | |
159 | dataP = getARTDataP(ret); | |
160 | ||
161 | kr = kmem_alloc_contig(kernel_map, &startUpl, size, PAGE_MASK, 0); | |
162 | if (kr) | |
163 | return 0; | |
164 | ||
165 | dataP->v = (void *) startUpl; | |
166 | ||
167 | do { | |
168 | upl_t iopl; | |
169 | int upl_flags = UPL_SET_INTERNAL | UPL_SET_LITE | |
170 | | UPL_SET_IO_WIRE | UPL_COPYOUT_FROM; | |
171 | vm_size_t iopl_size = size; | |
172 | ||
173 | kr = vm_map_get_upl(kernel_map, | |
174 | startUpl, | |
175 | &iopl_size, | |
176 | &iopl, | |
177 | 0, | |
178 | 0, | |
179 | &upl_flags, | |
180 | 0); | |
181 | if (kr) { | |
182 | panic("IOMapper:vm_map_get_upl returned 0x%x\n"); | |
183 | goto bail; | |
184 | } | |
185 | ||
186 | if (!ret->appendBytes(&iopl, sizeof(upl_t))) | |
187 | goto bail; | |
188 | ||
189 | startUpl += iopl_size; | |
190 | size -= iopl_size; | |
191 | } while(size); | |
192 | ||
193 | // Need to re-establish the dataP as the OSData may have grown. | |
194 | dataP = getARTDataP(ret); | |
195 | ||
196 | // Now grab the page entry of the first page and get its phys addr | |
197 | pl = UPL_GET_INTERNAL_PAGE_LIST(dataP->u[0]); | |
198 | *physAddrP = pl->phys_addr; | |
199 | *virtAddrP = dataP->v; | |
200 | ||
201 | return ret; | |
202 | ||
203 | bail: | |
204 | FreeARTTable(ret, size); | |
205 | return 0; | |
206 | } | |
207 | ||
208 | void IOMapper::FreeARTTable(OSData *artHandle, IOByteCount size) | |
209 | { | |
210 | assert(artHandle); | |
211 | ||
212 | ARTTableData *dataP = getARTDataP(artHandle); | |
213 | ||
214 | int numupls = ((artHandle->getLength() - sizeof(*dataP)) / sizeof(upl_t)); | |
215 | for (int i = 0; i < numupls; i++) | |
216 | kernel_upl_abort(dataP->u[i], 0); | |
217 | ||
218 | if (dataP->v) { | |
219 | size = round_page_32(size); | |
220 | kmem_free(kernel_map, (vm_address_t) dataP->v, size); | |
221 | } | |
222 | artHandle->release(); | |
223 | } | |
224 | ||
225 | __BEGIN_DECLS | |
226 | ||
227 | // These are C accessors to the system mapper for non-IOKit clients | |
228 | ppnum_t IOMapperIOVMAlloc(unsigned pages) | |
229 | { | |
230 | IOMapper::checkForSystemMapper(); | |
231 | ||
232 | if (IOMapper::gSystem) | |
233 | return IOMapper::gSystem->iovmAlloc((IOItemCount) pages); | |
234 | else | |
235 | return 0; | |
236 | } | |
237 | ||
238 | void IOMapperIOVMFree(ppnum_t addr, unsigned pages) | |
239 | { | |
240 | if (IOMapper::gSystem) | |
241 | IOMapper::gSystem->iovmFree(addr, (IOItemCount) pages); | |
242 | } | |
243 | ||
244 | ppnum_t IOMapperInsertPage(ppnum_t addr, unsigned offset, ppnum_t page) | |
245 | { | |
246 | if (IOMapper::gSystem) { | |
247 | IOMapper::gSystem->iovmInsert(addr, (IOItemCount) offset, page); | |
248 | return addr + offset; | |
249 | } | |
250 | else | |
251 | return page; | |
252 | } | |
253 | ||
254 | void IOMapperInsertPPNPages(ppnum_t addr, unsigned offset, | |
255 | ppnum_t *pageList, unsigned pageCount) | |
256 | { | |
257 | if (!IOMapper::gSystem) | |
258 | panic("IOMapperInsertPPNPages no system mapper"); | |
259 | else | |
260 | assert(!((vm_address_t) IOMapper::gSystem & 3)); | |
261 | ||
262 | IOMapper::gSystem-> | |
263 | iovmInsert(addr, (IOItemCount) offset, pageList, pageCount); | |
264 | } | |
265 | ||
266 | void IOMapperInsertUPLPages(ppnum_t addr, unsigned offset, | |
267 | upl_page_info_t *pageList, unsigned pageCount) | |
268 | { | |
269 | if (!IOMapper::gSystem) | |
270 | panic("IOMapperInsertUPLPages no system mapper"); | |
271 | else | |
272 | assert(!((vm_address_t) IOMapper::gSystem & 3)); | |
273 | ||
274 | IOMapper::gSystem->iovmInsert(addr, | |
275 | (IOItemCount) offset, | |
276 | pageList, | |
277 | (IOItemCount) pageCount); | |
278 | } | |
279 | ||
280 | ///////////////////////////////////////////////////////////////////////////// | |
281 | // | |
282 | // | |
283 | // IOLib.h APIs | |
284 | // | |
285 | // | |
286 | ///////////////////////////////////////////////////////////////////////////// | |
287 | ||
288 | #include <machine/machine_routines.h> | |
289 | ||
290 | UInt8 IOMappedRead8(IOPhysicalAddress address) | |
291 | { | |
292 | IOMapper::checkForSystemMapper(); | |
293 | ||
294 | if (IOMapper::gSystem) { | |
295 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
296 | return (UInt8) ml_phys_read_byte_64(addr); | |
297 | } | |
298 | else | |
299 | return (UInt8) ml_phys_read_byte((vm_offset_t) address); | |
300 | } | |
301 | ||
302 | UInt16 IOMappedRead16(IOPhysicalAddress address) | |
303 | { | |
304 | IOMapper::checkForSystemMapper(); | |
305 | ||
306 | if (IOMapper::gSystem) { | |
307 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
308 | return (UInt16) ml_phys_read_half_64(addr); | |
309 | } | |
310 | else | |
311 | return (UInt16) ml_phys_read_half((vm_offset_t) address); | |
312 | } | |
313 | ||
314 | UInt32 IOMappedRead32(IOPhysicalAddress address) | |
315 | { | |
316 | IOMapper::checkForSystemMapper(); | |
317 | ||
318 | if (IOMapper::gSystem) { | |
319 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
320 | return (UInt32) ml_phys_read_word_64(addr); | |
321 | } | |
322 | else | |
323 | return (UInt32) ml_phys_read_word((vm_offset_t) address); | |
324 | } | |
325 | ||
326 | UInt64 IOMappedRead64(IOPhysicalAddress address) | |
327 | { | |
328 | IOMapper::checkForSystemMapper(); | |
329 | ||
330 | if (IOMapper::gSystem) { | |
331 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
332 | return (UInt64) ml_phys_read_double_64(addr); | |
333 | } | |
334 | else | |
335 | return (UInt64) ml_phys_read_double((vm_offset_t) address); | |
336 | } | |
337 | ||
338 | void IOMappedWrite8(IOPhysicalAddress address, UInt8 value) | |
339 | { | |
340 | IOMapper::checkForSystemMapper(); | |
341 | ||
342 | if (IOMapper::gSystem) { | |
343 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
344 | ml_phys_write_byte_64(addr, value); | |
345 | } | |
346 | else | |
347 | ml_phys_write_byte((vm_offset_t) address, value); | |
348 | } | |
349 | ||
350 | void IOMappedWrite16(IOPhysicalAddress address, UInt16 value) | |
351 | { | |
352 | IOMapper::checkForSystemMapper(); | |
353 | ||
354 | if (IOMapper::gSystem) { | |
355 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
356 | ml_phys_write_half_64(addr, value); | |
357 | } | |
358 | else | |
359 | ml_phys_write_half((vm_offset_t) address, value); | |
360 | } | |
361 | ||
362 | void IOMappedWrite32(IOPhysicalAddress address, UInt32 value) | |
363 | { | |
364 | IOMapper::checkForSystemMapper(); | |
365 | ||
366 | if (IOMapper::gSystem) { | |
367 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
368 | ml_phys_write_word_64(addr, value); | |
369 | } | |
370 | else | |
371 | ml_phys_write_word((vm_offset_t) address, value); | |
372 | } | |
373 | ||
374 | void IOMappedWrite64(IOPhysicalAddress address, UInt64 value) | |
375 | { | |
376 | IOMapper::checkForSystemMapper(); | |
377 | ||
378 | if (IOMapper::gSystem) { | |
379 | addr64_t addr = IOMapper::gSystem->mapAddr(address); | |
380 | ml_phys_write_double_64(addr, value); | |
381 | } | |
382 | else | |
383 | ml_phys_write_double((vm_offset_t) address, value); | |
384 | } | |
385 | ||
386 | __END_DECLS |