- if (!virtAddrP || !physAddrP)
- return 0;
-
- kern_return_t kr;
- vm_address_t address;
-
- size = round_page_32(size);
- kr = kmem_alloc_contig(kernel_map, &address, size, PAGE_MASK, 0);
- if (kr)
- return 0;
-
- ppnum_t pagenum = pmap_find_phys(kernel_pmap, (addr64_t) address);
- if (pagenum)
- *physAddrP = pagenum;
- else {
- FreeARTTable((OSData *) address, size);
- address = 0;
- }
-
- *virtAddrP = (void *) address;
-
- return (OSData *) address;
-}
-
-void IOMapper::FreeARTTable(OSData *artHandle, IOByteCount size)
-{
- vm_address_t address = (vm_address_t) artHandle;
-
- size = round_page_32(size);
- kmem_free(kernel_map, address, size); // Just panic if address is 0
-}
-
-bool IOMapper::getBypassMask(addr64_t *maskP) const
-{
- return false;
+ OSData *data;
+ OSObject * obj;
+ IOMapper * mapper = NULL;
+ OSDictionary * matching;
+
+ obj = device->copyProperty("iommu-parent");
+ if (!obj) {
+ return NULL;
+ }
+
+ if ((mapper = OSDynamicCast(IOMapper, obj))) {
+ goto found;
+ }
+
+ 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();
+ }
+found:
+ if (mapper) {
+ if (!mapper->fAllocName) {
+ char name[MACH_ZONE_NAME_MAX_LEN];
+ char kmodname[KMOD_MAX_NAME];
+ vm_tag_t tag;
+ uint32_t kmodid;
+
+ tag = IOMemoryTag(kernel_map);
+ if (!(kmodid = vm_tag_get_kext(tag, &kmodname[0], KMOD_MAX_NAME))) {
+ snprintf(kmodname, sizeof(kmodname), "%d", tag);
+ }
+ snprintf(name, sizeof(name), "%s.DMA.%s", kmodname, device->getName());
+ mapper->fAllocName = kern_allocation_name_allocate(name, 16);
+ }
+ }
+
+ return mapper;