2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <IOKit/assert.h>
23 #include <IOKit/system.h>
25 #include <IOKit/IOLib.h>
26 #include <IOKit/IOBufferMemoryDescriptor.h>
29 void ipc_port_release_send(ipc_port_t port
);
32 extern "C" vm_map_t
IOPageableMapForAddress( vm_address_t address
);
34 #define super IOGeneralMemoryDescriptor
35 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
36 IOGeneralMemoryDescriptor
);
38 bool IOBufferMemoryDescriptor::initWithAddress(
39 void * /* address */ ,
40 IOByteCount
/* withLength */ ,
41 IODirection
/* withDirection */ )
46 bool IOBufferMemoryDescriptor::initWithAddress(
47 vm_address_t
/* address */ ,
48 IOByteCount
/* withLength */ ,
49 IODirection
/* withDirection */ ,
50 task_t
/* withTask */ )
55 bool IOBufferMemoryDescriptor::initWithPhysicalAddress(
56 IOPhysicalAddress
/* address */ ,
57 IOByteCount
/* withLength */ ,
58 IODirection
/* withDirection */ )
63 bool IOBufferMemoryDescriptor::initWithPhysicalRanges(
64 IOPhysicalRange
* /* ranges */ ,
65 UInt32
/* withCount */ ,
66 IODirection
/* withDirection */ ,
67 bool /* asReference */ )
72 bool IOBufferMemoryDescriptor::initWithRanges(
73 IOVirtualRange
* /* ranges */ ,
74 UInt32
/* withCount */ ,
75 IODirection
/* withDirection */ ,
76 task_t
/* withTask */ ,
77 bool /* asReference */ )
82 bool IOBufferMemoryDescriptor::initWithOptions(
85 vm_offset_t alignment
)
96 if ((options
& kIOMemorySharingTypeMask
) && (alignment
< page_size
))
97 alignment
= page_size
;
99 _alignment
= alignment
;
100 if (options
& kIOMemoryPageable
)
101 /* Allocate some kernel address space. */
102 _buffer
= IOMallocPageable(capacity
, alignment
);
103 /* Allocate a wired-down buffer inside kernel space. */
104 else if (options
& kIOMemoryPhysicallyContiguous
)
105 _buffer
= IOMallocContiguous(capacity
, alignment
, 0);
106 else if (alignment
> 1)
107 _buffer
= IOMallocAligned(capacity
, alignment
);
109 _buffer
= IOMalloc(capacity
);
114 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
115 _singleRange
.v
.length
= capacity
;
117 if (!super::initWithRanges(&_singleRange
.v
, 1,
118 (IODirection
) (options
& kIOMemoryDirectionMask
),
122 if (options
& kIOMemoryPageable
) {
123 _flags
|= kIOMemoryRequiresWire
;
126 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
127 vm_size_t size
= _ranges
.v
[0].length
;
129 // must create the entry before any pages are allocated
130 if( 0 == sharedMem
) {
131 kr
= mach_make_memory_entry( IOPageableMapForAddress( _ranges
.v
[0].address
),
132 &size
, _ranges
.v
[0].address
,
133 VM_PROT_READ
| VM_PROT_WRITE
, &sharedMem
,
135 if( (KERN_SUCCESS
== kr
) && (size
!= _ranges
.v
[0].length
)) {
136 ipc_port_release_send( sharedMem
);
137 kr
= kIOReturnVMError
;
139 if( KERN_SUCCESS
!= kr
)
141 _memEntry
= (void *) sharedMem
;
145 /* Precompute virtual-to-physical page mappings. */
146 vm_address_t inBuffer
= (vm_address_t
) _buffer
;
147 _physSegCount
= atop(trunc_page(inBuffer
+ capacity
- 1) -
148 trunc_page(inBuffer
)) + 1;
149 _physAddrs
= IONew(IOPhysicalAddress
, _physSegCount
);
153 inBuffer
= trunc_page(inBuffer
);
154 for (unsigned i
= 0; i
< _physSegCount
; i
++) {
155 _physAddrs
[i
] = pmap_extract(get_task_pmap(kernel_task
), inBuffer
);
156 assert(_physAddrs
[i
]); /* supposed to be wired */
157 inBuffer
+= page_size
;
166 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
167 IOOptionBits options
,
169 vm_offset_t alignment
= 1)
171 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
173 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
)) {
184 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
185 * hold capacity bytes. The descriptor's length is initially set to the capacity.
187 IOBufferMemoryDescriptor
*
188 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
189 IODirection inDirection
,
192 return( IOBufferMemoryDescriptor::withOptions(
193 inDirection
| kIOMemoryUnshared
194 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
195 inCapacity
, inContiguous
? inCapacity
: 1 ));
201 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
202 * The descriptor's length and capacity are set to the input buffer's size.
204 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
206 IODirection inDirection
,
209 if (!initWithOptions(
210 inDirection
| kIOMemoryUnshared
211 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
212 inLength
, inLength
))
215 // start out with no data
218 if (!appendBytes(inBytes
, inLength
))
227 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
228 * The descriptor's length and capacity are set to the input buffer's size.
230 IOBufferMemoryDescriptor
*
231 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
233 IODirection inDirection
,
236 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
238 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
)){
250 void IOBufferMemoryDescriptor::free()
252 IOOptionBits options
= _options
;
253 vm_size_t size
= _capacity
;
254 void * buffer
= _buffer
;
255 vm_offset_t alignment
= _alignment
;
258 IODelete(_physAddrs
, IOPhysicalAddress
, _physSegCount
);
260 /* super::free may unwire - deallocate buffer afterwards */
264 if (options
& kIOMemoryPageable
)
265 IOFreePageable(buffer
, size
);
267 if (options
& kIOMemoryPhysicallyContiguous
)
268 IOFreeContiguous(buffer
, size
);
269 else if (alignment
> 1)
270 IOFreeAligned(buffer
, size
);
272 IOFree(buffer
, size
);
280 * Get the buffer capacity
282 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
290 * Change the buffer length of the memory descriptor. When a new buffer
291 * is created, the initial length of the buffer is set to be the same as
292 * the capacity. The length can be adjusted via setLength for a shorter
293 * transfer (there is no need to create more buffer descriptors when you
294 * can reuse an existing one, even for different transfer sizes). Note
295 * that the specified length must not exceed the capacity of the buffer.
297 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
299 assert(length
<= _capacity
);
302 _singleRange
.v
.length
= length
;
308 * Change the direction of the transfer. This method allows one to redirect
309 * the descriptor's transfer direction. This eliminates the need to destroy
310 * and create new buffers when different transfer directions are needed.
312 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
314 _direction
= direction
;
320 * Add some data to the end of the buffer. This method automatically
321 * maintains the memory descriptor buffer length. Note that appendBytes
322 * will not copy past the end of the memory descriptor's current capacity.
325 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
327 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
329 assert(_length
<= _capacity
);
330 bcopy(/* from */ bytes
, (void *)(_singleRange
.v
.address
+ _length
),
332 _length
+= actualBytesToCopy
;
333 _singleRange
.v
.length
+= actualBytesToCopy
;
341 * Return the virtual address of the beginning of the buffer
343 void * IOBufferMemoryDescriptor::getBytesNoCopy()
345 return (void *)_singleRange
.v
.address
;
351 * Return the virtual address of an offset from the beginning of the buffer
354 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
356 if (start
< _length
&& (start
+ withLength
) <= _length
)
357 return (void *)(_singleRange
.v
.address
+ start
);
362 * getPhysicalSegment:
364 * Get the physical address of the buffer, relative to the current position.
365 * If the current position is at the end of the buffer, a zero is returned.
368 IOBufferMemoryDescriptor::getPhysicalSegment(IOByteCount offset
,
369 IOByteCount
* lengthOfSegment
)
371 IOPhysicalAddress physAddr
;
373 if( offset
!= _position
)
374 setPosition( offset
);
376 assert(_position
<= _length
);
378 /* Fail gracefully if the position is at (or past) the end-of-buffer. */
379 if (_position
>= _length
) {
380 *lengthOfSegment
= 0;
384 if (_options
& kIOMemoryPageable
) {
385 physAddr
= super::getPhysicalSegment(offset
, lengthOfSegment
);
388 /* Compute the largest contiguous physical length possible. */
389 vm_address_t actualPos
= _singleRange
.v
.address
+ _position
;
390 vm_address_t actualPage
= trunc_page(actualPos
);
391 unsigned physInd
= atop(actualPage
-trunc_page(_singleRange
.v
.address
));
393 vm_size_t physicalLength
= actualPage
+ page_size
- actualPos
;
394 for (unsigned index
= physInd
+ 1; index
< _physSegCount
&&
395 _physAddrs
[index
] == _physAddrs
[index
-1] + page_size
; index
++) {
396 physicalLength
+= page_size
;
399 /* Clip contiguous physical length at the end-of-buffer. */
400 if (physicalLength
> _length
- _position
)
401 physicalLength
= _length
- _position
;
403 *lengthOfSegment
= physicalLength
;
404 physAddr
= _physAddrs
[physInd
] + (actualPos
- actualPage
);
410 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
411 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
412 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
413 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
414 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
415 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
416 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
417 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
418 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
419 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
420 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
421 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
422 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
423 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
424 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
425 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);