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/IOInterleavedMemoryDescriptor.h>
32 #define super IOMemoryDescriptor
33 OSDefineMetaClassAndStructors(IOInterleavedMemoryDescriptor
, IOMemoryDescriptor
)
35 IOInterleavedMemoryDescriptor
* IOInterleavedMemoryDescriptor::withCapacity(
37 IODirection direction
)
40 // Create a new IOInterleavedMemoryDescriptor. The "buffer" will be made up
41 // of several memory descriptors, that are to be chained end-to-end to make up
42 // a single memory descriptor.
45 IOInterleavedMemoryDescriptor
* me
= new IOInterleavedMemoryDescriptor
;
47 if ( me
&& !me
->initWithCapacity(
48 /* capacity */ capacity
,
49 /* direction */ direction
))
58 bool IOInterleavedMemoryDescriptor::initWithCapacity(
60 IODirection direction
)
63 // Initialize an IOInterleavedMemoryDescriptor. The "buffer" will be made up
64 // of several memory descriptors, that are to be chained end-to-end to make up
65 // a single memory descriptor.
70 // Ask our superclass' opinion.
71 if ( super::init() == false ) return false;
73 // Initialize our minimal state.
77 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
78 #endif /* !__LP64__ */
83 _descriptors
= IONew(IOMemoryDescriptor
*, capacity
);
84 _descriptorOffsets
= IONew(IOByteCount
, capacity
);
85 _descriptorLengths
= IONew(IOByteCount
, capacity
);
87 if ( (_descriptors
== 0) || (_descriptorOffsets
== 0) || (_descriptorLengths
== 0) )
90 _descriptorCapacity
= capacity
;
95 void IOInterleavedMemoryDescriptor::clearMemoryDescriptors( IODirection direction
)
99 for ( index
= 0; index
< _descriptorCount
; index
++ )
101 if ( _descriptorPrepared
)
102 _descriptors
[index
]->complete(getDirection());
104 _descriptors
[index
]->release();
105 _descriptors
[index
] = 0;
107 _descriptorOffsets
[index
] = 0;
108 _descriptorLengths
[index
] = 0;
111 if ( direction
!= kIODirectionNone
)
113 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
115 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
116 #endif /* !__LP64__ */
119 _descriptorCount
= 0;
126 bool IOInterleavedMemoryDescriptor::setMemoryDescriptor(
127 IOMemoryDescriptor
* descriptor
,
131 if ( _descriptorPrepared
|| (_descriptorCount
== _descriptorCapacity
) )
134 if ( (offset
+ length
) > descriptor
->getLength() )
137 // if ( descriptor->getDirection() != getDirection() )
140 descriptor
->retain();
141 _descriptors
[_descriptorCount
] = descriptor
;
142 _descriptorOffsets
[_descriptorCount
] = offset
;
143 _descriptorLengths
[_descriptorCount
] = length
;
152 void IOInterleavedMemoryDescriptor::free()
155 // Free all of this object's outstanding resources.
160 for ( unsigned index
= 0; index
< _descriptorCount
; index
++ )
161 _descriptors
[index
]->release();
163 if ( _descriptors
!= 0 )
164 IODelete(_descriptors
, IOMemoryDescriptor
*, _descriptorCapacity
);
166 if ( _descriptorOffsets
!= 0 )
167 IODelete(_descriptorOffsets
, IOMemoryDescriptor
*, _descriptorCapacity
);
169 if ( _descriptorLengths
!= 0 )
170 IODelete(_descriptorLengths
, IOMemoryDescriptor
*, _descriptorCapacity
);
176 IOReturn
IOInterleavedMemoryDescriptor::prepare(IODirection forDirection
)
179 // Prepare the memory for an I/O transfer.
181 // This involves paging in the memory and wiring it down for the duration
182 // of the transfer. The complete() method finishes the processing of the
183 // memory after the I/O transfer finishes.
187 IOReturn status
= kIOReturnSuccess
;
190 if ( forDirection
== kIODirectionNone
)
192 forDirection
= getDirection();
195 for ( index
= 0; index
< _descriptorCount
; index
++ )
197 status
= _descriptors
[index
]->prepare(forDirection
);
198 if ( status
!= kIOReturnSuccess
) break;
201 if ( status
!= kIOReturnSuccess
)
203 for ( unsigned indexUndo
= 0; indexUndo
< index
; indexUndo
++ )
205 statusUndo
= _descriptors
[index
]->complete(forDirection
);
206 assert(statusUndo
== kIOReturnSuccess
);
210 if ( status
== kIOReturnSuccess
) _descriptorPrepared
= true;
215 IOReturn
IOInterleavedMemoryDescriptor::complete(IODirection forDirection
)
218 // Complete processing of the memory after an I/O transfer finishes.
220 // This method shouldn't be called unless a prepare() was previously issued;
221 // the prepare() and complete() must occur in pairs, before and after an I/O
226 IOReturn statusFinal
= kIOReturnSuccess
;
228 if ( forDirection
== kIODirectionNone
)
230 forDirection
= getDirection();
233 for ( unsigned index
= 0; index
< _descriptorCount
; index
++ )
235 status
= _descriptors
[index
]->complete(forDirection
);
236 if ( status
!= kIOReturnSuccess
) statusFinal
= status
;
237 assert(status
== kIOReturnSuccess
);
240 _descriptorPrepared
= false;
245 addr64_t
IOInterleavedMemoryDescriptor::getPhysicalSegment(
247 IOByteCount
* length
,
248 IOOptionBits options
)
251 // This method returns the physical address of the byte at the given offset
252 // into the memory, and optionally the length of the physically contiguous
253 // segment from that offset.
258 assert(offset
<= _length
);
260 for ( unsigned index
= 0; index
< _descriptorCount
; index
++ )
262 if ( offset
< _descriptorLengths
[index
] )
264 pa
= _descriptors
[index
]->getPhysicalSegment(_descriptorOffsets
[index
] + offset
, length
, options
);
265 if ((_descriptorLengths
[index
] - offset
) < *length
) *length
= _descriptorLengths
[index
] - offset
;
268 offset
-= _descriptorLengths
[index
];
271 if ( length
) *length
= 0;