Loading...
--- xnu/xnu-3789.31.2/iokit/Kernel/IOMapper.cpp
+++ xnu/xnu-517/iokit/Kernel/IOMapper.cpp
@@ -1,19 +1,16 @@
/*
- * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
+ * Copyright (c) 1998-2003 Apple Computer, Inc. All rights reserved.
*
- * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ * @APPLE_LICENSE_HEADER_START@
+ *
+ * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. The rights granted to you under the License
- * may not be used to create, or enable the creation or redistribution of,
- * unlawful or unlicensed copies of an Apple operating system, or to
- * circumvent, violate, or enable the circumvention or violation of, any
- * terms of an Apple operating system software license agreement.
- *
- * Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
@@ -23,18 +20,11 @@
* Please see the License for the specific language governing rights and
* limitations under the License.
*
- * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
+ * @APPLE_LICENSE_HEADER_END@
*/
#include <IOKit/IOLib.h>
#include <IOKit/IOMapper.h>
-#include <IOKit/IODMACommand.h>
#include <libkern/c++/OSData.h>
-#include <libkern/OSDebug.h>
-#include "IOKitKernelInternal.h"
-
-__BEGIN_DECLS
-extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
-__END_DECLS
#define super IOService
OSDefineMetaClassAndAbstractStructors(IOMapper, IOService);
@@ -61,27 +51,24 @@
class IOMapperLock {
IOLock *fWaitLock;
public:
- IOMapperLock() { fWaitLock = IOLockAlloc(); }
- ~IOMapperLock() { IOLockFree(fWaitLock); }
-
- void lock() { IOLockLock(fWaitLock); }
- void unlock() { IOLockUnlock(fWaitLock); }
- void sleep(void *event) { IOLockSleep(fWaitLock, event, THREAD_UNINT); }
- void wakeup(void *event) { IOLockWakeup(fWaitLock, event, false); }
+ IOMapperLock() { fWaitLock = IOLockAlloc(); };
+ ~IOMapperLock() { IOLockFree(fWaitLock); };
+
+ void lock() { IOLockLock(fWaitLock); };
+ void unlock() { IOLockUnlock(fWaitLock); };
+ void sleep(void *event) { IOLockSleep(fWaitLock, event, THREAD_UNINT); };
+ void wakeup(void *event) { IOLockWakeup(fWaitLock, event, false); };
};
static IOMapperLock sMapperLock;
bool IOMapper::start(IOService *provider)
{
- OSObject * obj;
if (!super::start(provider))
return false;
if (!initHardware(provider))
return false;
-
- fPageSize = getPageSize();
if (fIsSystem) {
sMapperLock.lock();
@@ -90,19 +77,25 @@
sMapperLock.unlock();
}
- if (provider)
- {
- obj = provider->getProperty("iommu-id");
- if (!obj)
- obj = provider->getProperty("AAPL,phandle");
- if (obj)
- setProperty(gIOMapperIDKey, obj);
- }
return true;
}
+bool IOMapper::allocTable(IOByteCount size)
+{
+ assert(!fTable);
+
+ fTableSize = size;
+ fTableHandle = NewARTTable(size, &fTable, &fTablePhys);
+ return fTableHandle != 0;
+}
+
void IOMapper::free()
{
+ if (fTableHandle) {
+ FreeARTTable(fTableHandle, fTableSize);
+ fTableHandle = 0;
+ }
+
super::free();
}
@@ -121,58 +114,115 @@
void IOMapper::waitForSystemMapper()
{
sMapperLock.lock();
- while ((uintptr_t) IOMapper::gSystem & kWaitMask)
- {
- OSReportWithBacktrace("waitForSystemMapper");
+ while ((vm_address_t) IOMapper::gSystem & kWaitMask)
sMapperLock.sleep(&IOMapper::gSystem);
- }
sMapperLock.unlock();
}
-IOMapper * IOMapper::copyMapperForDevice(IOService * device)
-{
- return copyMapperForDeviceWithIndex(device, 0);
-}
-
-IOMapper * IOMapper::copyMapperForDeviceWithIndex(IOService * device, unsigned int index)
-{
- OSData *data;
- OSObject * obj;
- IOMapper * mapper = NULL;
- OSDictionary * matching;
-
- obj = device->copyProperty("iommu-parent");
- if (!obj)
- return (NULL);
-
- if ((mapper = OSDynamicCast(IOMapper, obj)))
- return (mapper);
-
- if ((data = OSDynamicCast(OSData, obj)))
- {
- if (index >= data->getLength() / sizeof(UInt32))
- goto done;
-
- data = OSData::withBytesNoCopy((UInt32 *)data->getBytesNoCopy() + index, sizeof(UInt32));
- if (!data)
- goto done;
-
- matching = IOService::propertyMatching(gIOMapperIDKey, data);
- data->release();
- }
- else
- matching = IOService::propertyMatching(gIOMapperIDKey, obj);
-
- if (matching)
- {
- mapper = OSDynamicCast(IOMapper, IOService::waitForMatchingService(matching));
- matching->release();
- }
-
-done:
- if (obj)
- obj->release();
- return (mapper);
+void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset,
+ ppnum_t *pageList, IOItemCount pageCount)
+{
+ while (pageCount--)
+ iovmInsert(addr, offset++, *pageList++);
+}
+
+void IOMapper::iovmInsert(ppnum_t addr, IOItemCount offset,
+ upl_page_info_t *pageList, IOItemCount pageCount)
+{
+ for (IOItemCount i = 0; i < pageCount; i++)
+ iovmInsert(addr, offset + i, pageList[i].phys_addr);
+}
+
+struct ARTTableData {
+ void *v;
+ upl_t u[0];
+};
+#define getARTDataP(data) ((ARTTableData *) (data)->getBytesNoCopy())
+
+OSData *
+IOMapper::NewARTTable(IOByteCount size,
+ void ** virtAddrP, ppnum_t *physAddrP)
+{
+ OSData *ret;
+ kern_return_t kr;
+ vm_address_t startUpl;
+ ARTTableData *dataP;
+ unsigned int dataSize;
+ upl_page_info_t *pl = 0;
+
+ // Each UPL can deal with about one meg at the moment
+ size = round_page_32(size);
+ dataSize = sizeof(ARTTableData) + sizeof(upl_t) * size / (1024 * 1024);
+ ret = OSData::withCapacity(dataSize);
+ if (!ret)
+ return 0;
+
+ // Append 0's to the buffer, in-other-words reset to nulls.
+ ret->appendBytes(NULL, sizeof(ARTTableData));
+ dataP = getARTDataP(ret);
+
+ kr = kmem_alloc_contig(kernel_map, &startUpl, size, PAGE_MASK, 0);
+ if (kr)
+ return 0;
+
+ dataP->v = (void *) startUpl;
+
+ do {
+ upl_t iopl;
+ int upl_flags = UPL_SET_INTERNAL | UPL_SET_LITE
+ | UPL_SET_IO_WIRE | UPL_COPYOUT_FROM;
+ vm_size_t iopl_size = size;
+
+ kr = vm_map_get_upl(kernel_map,
+ startUpl,
+ &iopl_size,
+ &iopl,
+ 0,
+ 0,
+ &upl_flags,
+ 0);
+ if (kr) {
+ panic("IOMapper:vm_map_get_upl returned 0x%x\n");
+ goto bail;
+ }
+
+ if (!ret->appendBytes(&iopl, sizeof(upl_t)))
+ goto bail;
+
+ startUpl += iopl_size;
+ size -= iopl_size;
+ } while(size);
+
+ // Need to re-establish the dataP as the OSData may have grown.
+ dataP = getARTDataP(ret);
+
+ // Now grab the page entry of the first page and get its phys addr
+ pl = UPL_GET_INTERNAL_PAGE_LIST(dataP->u[0]);
+ *physAddrP = pl->phys_addr;
+ *virtAddrP = dataP->v;
+
+ return ret;
+
+bail:
+ FreeARTTable(ret, size);
+ return 0;
+}
+
+void IOMapper::FreeARTTable(OSData *artHandle, IOByteCount size)
+{
+ assert(artHandle);
+
+ ARTTableData *dataP = getARTDataP(artHandle);
+
+ int numupls = ((artHandle->getLength() - sizeof(*dataP)) / sizeof(upl_t));
+ for (int i = 0; i < numupls; i++)
+ kernel_upl_abort(dataP->u[i], 0);
+
+ if (dataP->v) {
+ size = round_page_32(size);
+ kmem_free(kernel_map, (vm_address_t) dataP->v, size);
+ }
+ artHandle->release();
}
__BEGIN_DECLS
@@ -180,40 +230,54 @@
// These are C accessors to the system mapper for non-IOKit clients
ppnum_t IOMapperIOVMAlloc(unsigned pages)
{
- IOReturn ret;
- uint64_t dmaAddress, dmaLength;
-
- IOMapper::checkForSystemMapper();
-
- ret = kIOReturnUnsupported;
+ IOMapper::checkForSystemMapper();
+
if (IOMapper::gSystem)
- {
- ret = IOMapper::gSystem->iovmMapMemory(
- NULL, 0, ptoa_64(pages),
- (kIODMAMapReadAccess | kIODMAMapWriteAccess),
- NULL, NULL, NULL,
- &dmaAddress, &dmaLength);
- }
-
- if (kIOReturnSuccess == ret) return (atop_64(dmaAddress));
- return (0);
+ return IOMapper::gSystem->iovmAlloc((IOItemCount) pages);
+ else
+ return 0;
}
void IOMapperIOVMFree(ppnum_t addr, unsigned pages)
{
if (IOMapper::gSystem)
- {
- IOMapper::gSystem->iovmUnmapMemory(NULL, NULL, ptoa_64(addr), ptoa_64(pages));
- }
+ IOMapper::gSystem->iovmFree(addr, (IOItemCount) pages);
}
ppnum_t IOMapperInsertPage(ppnum_t addr, unsigned offset, ppnum_t page)
{
- if (!IOMapper::gSystem) return (page);
- if (!addr) panic("!addr");
- IOMapper::gSystem->iovmInsert((kIODMAMapReadAccess | kIODMAMapWriteAccess),
- ptoa_64(addr), ptoa_64(offset), ptoa_64(page), ptoa_64(1));
- return (addr + offset);
+ if (IOMapper::gSystem) {
+ IOMapper::gSystem->iovmInsert(addr, (IOItemCount) offset, page);
+ return addr + offset;
+ }
+ else
+ return page;
+}
+
+void IOMapperInsertPPNPages(ppnum_t addr, unsigned offset,
+ ppnum_t *pageList, unsigned pageCount)
+{
+ if (!IOMapper::gSystem)
+ panic("IOMapperInsertPPNPages no system mapper");
+ else
+ assert(!((vm_address_t) IOMapper::gSystem & 3));
+
+ IOMapper::gSystem->
+ iovmInsert(addr, (IOItemCount) offset, pageList, pageCount);
+}
+
+void IOMapperInsertUPLPages(ppnum_t addr, unsigned offset,
+ upl_page_info_t *pageList, unsigned pageCount)
+{
+ if (!IOMapper::gSystem)
+ panic("IOMapperInsertUPLPages no system mapper");
+ else
+ assert(!((vm_address_t) IOMapper::gSystem & 3));
+
+ IOMapper::gSystem->iovmInsert(addr,
+ (IOItemCount) offset,
+ pageList,
+ (IOItemCount) pageCount);
}
/////////////////////////////////////////////////////////////////////////////
@@ -231,7 +295,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
return (UInt8) ml_phys_read_byte_64(addr);
}
else
@@ -243,7 +307,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
return (UInt16) ml_phys_read_half_64(addr);
}
else
@@ -255,7 +319,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
return (UInt32) ml_phys_read_word_64(addr);
}
else
@@ -267,7 +331,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
return (UInt64) ml_phys_read_double_64(addr);
}
else
@@ -279,7 +343,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
ml_phys_write_byte_64(addr, value);
}
else
@@ -291,7 +355,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
ml_phys_write_half_64(addr, value);
}
else
@@ -303,7 +367,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
ml_phys_write_word_64(addr, value);
}
else
@@ -315,7 +379,7 @@
IOMapper::checkForSystemMapper();
if (IOMapper::gSystem) {
- addr64_t addr = IOMapper::gSystem->mapToPhysicalAddress(address);
+ addr64_t addr = IOMapper::gSystem->mapAddr(address);
ml_phys_write_double_64(addr, value);
}
else