2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOMultiMemoryDescriptor.h>
32 #define super IOMemoryDescriptor
33 OSDefineMetaClassAndStructors(IOMultiMemoryDescriptor
, IOMemoryDescriptor
)
35 IOMultiMemoryDescriptor
* IOMultiMemoryDescriptor::withDescriptors(
36 IOMemoryDescriptor
** descriptors
,
38 IODirection withDirection
,
42 // Create a new IOMultiMemoryDescriptor. The "buffer" is made up of several
43 // memory descriptors, that are to be chained end-to-end to make up a single
46 // Passing the ranges as a reference will avoid an extra allocation.
49 IOMultiMemoryDescriptor
* me
= new IOMultiMemoryDescriptor
;
51 if ( me
&& me
->initWithDescriptors(
52 /* descriptors */ descriptors
,
53 /* withCount */ withCount
,
54 /* withDirection */ withDirection
,
55 /* asReference */ asReference
) == false )
64 bool IOMultiMemoryDescriptor::initWithDescriptors(
65 IOMemoryDescriptor
** descriptors
,
67 IODirection withDirection
,
71 // Initialize an IOMultiMemoryDescriptor. The "buffer" is made up of several
72 // memory descriptors, that are to be chained end-to-end to make up a single
75 // Passing the ranges as a reference will avoid an extra allocation.
81 // Release existing descriptors, if any
84 for ( unsigned index
= 0; index
< _descriptorsCount
; index
++ )
85 _descriptors
[index
]->release();
87 if ( _descriptorsIsAllocated
)
88 IODelete(_descriptors
, IOMemoryDescriptor
*, _descriptorsCount
);
90 // Ask our superclass' opinion.
91 if ( super::init() == false ) return false;
94 // Initialize our minimal state.
97 _descriptorsCount
= withCount
;
98 _descriptorsIsAllocated
= asReference
? false : true;
99 _flags
= withDirection
;
101 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
102 #endif /* !__LP64__ */
109 _descriptors
= descriptors
;
113 _descriptors
= IONew(IOMemoryDescriptor
*, withCount
);
114 if ( _descriptors
== 0 ) return false;
116 bcopy( /* from */ descriptors
,
117 /* to */ _descriptors
,
118 /* bytes */ withCount
* sizeof(IOMemoryDescriptor
*) );
121 for ( unsigned index
= 0; index
< withCount
; index
++ )
123 descriptors
[index
]->retain();
124 _length
+= descriptors
[index
]->getLength();
125 if ( _tag
== 0 ) _tag
= descriptors
[index
]->getTag();
126 assert(descriptors
[index
]->getDirection() == withDirection
);
132 void IOMultiMemoryDescriptor::free()
135 // Free all of this object's outstanding resources.
140 for ( unsigned index
= 0; index
< _descriptorsCount
; index
++ )
141 _descriptors
[index
]->release();
143 if ( _descriptorsIsAllocated
)
144 IODelete(_descriptors
, IOMemoryDescriptor
*, _descriptorsCount
);
150 IOReturn
IOMultiMemoryDescriptor::prepare(IODirection forDirection
)
153 // Prepare the memory for an I/O transfer.
155 // This involves paging in the memory and wiring it down for the duration
156 // of the transfer. The complete() method finishes the processing of the
157 // memory after the I/O transfer finishes.
161 IOReturn status
= kIOReturnInternalError
;
164 if ( forDirection
== kIODirectionNone
)
166 forDirection
= getDirection();
169 for ( index
= 0; index
< _descriptorsCount
; index
++ )
171 status
= _descriptors
[index
]->prepare(forDirection
);
172 if ( status
!= kIOReturnSuccess
) break;
175 if ( status
!= kIOReturnSuccess
)
177 for ( unsigned indexUndo
= 0; indexUndo
<= index
; indexUndo
++ )
179 statusUndo
= _descriptors
[index
]->complete(forDirection
);
180 assert(statusUndo
== kIOReturnSuccess
);
187 IOReturn
IOMultiMemoryDescriptor::complete(IODirection forDirection
)
190 // Complete processing of the memory after an I/O transfer finishes.
192 // This method shouldn't be called unless a prepare() was previously issued;
193 // the prepare() and complete() must occur in pairs, before and after an I/O
198 IOReturn statusFinal
= kIOReturnSuccess
;
200 if ( forDirection
== kIODirectionNone
)
202 forDirection
= getDirection();
205 for ( unsigned index
= 0; index
< _descriptorsCount
; index
++ )
207 status
= _descriptors
[index
]->complete(forDirection
);
208 if ( status
!= kIOReturnSuccess
) statusFinal
= status
;
209 assert(status
== kIOReturnSuccess
);
215 addr64_t
IOMultiMemoryDescriptor::getPhysicalSegment(
217 IOByteCount
* length
,
218 IOOptionBits options
)
221 // This method returns the physical address of the byte at the given offset
222 // into the memory, and optionally the length of the physically contiguous
223 // segment from that offset.
226 assert(offset
<= _length
);
228 for ( unsigned index
= 0; index
< _descriptorsCount
; index
++ )
230 if ( offset
< _descriptors
[index
]->getLength() )
232 return _descriptors
[index
]->getPhysicalSegment(offset
, length
, options
);
234 offset
-= _descriptors
[index
]->getLength();
237 if ( length
) *length
= 0;