- 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,
- (vm_map_offset_t)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++) {
- upl_abort(dataP->u[i], 0);
- upl_deallocate(dataP->u[i]);
- }
-
- if (dataP->v) {
- size = round_page_32(size);
- kmem_free(kernel_map, (vm_address_t) dataP->v, size);
- }
- artHandle->release();
+ 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;