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@
28 #include <IOKit/assert.h>
29 #include <IOKit/system.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOMapper.h>
33 #include <IOKit/IOBufferMemoryDescriptor.h>
35 #include "IOKitKernelInternal.h"
36 #include "IOCopyMapper.h"
39 void ipc_port_release_send(ipc_port_t port
);
42 vm_map_t
IOPageableMapForAddress( vm_address_t address
);
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 volatile ppnum_t gIOHighestAllocatedPage
;
49 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51 #define super IOGeneralMemoryDescriptor
52 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
53 IOGeneralMemoryDescriptor
);
55 bool IOBufferMemoryDescriptor::initWithAddress(
56 void * /* address */ ,
57 IOByteCount
/* withLength */ ,
58 IODirection
/* withDirection */ )
63 bool IOBufferMemoryDescriptor::initWithAddress(
64 vm_address_t
/* address */ ,
65 IOByteCount
/* withLength */ ,
66 IODirection
/* withDirection */ ,
67 task_t
/* withTask */ )
72 bool IOBufferMemoryDescriptor::initWithPhysicalAddress(
73 IOPhysicalAddress
/* address */ ,
74 IOByteCount
/* withLength */ ,
75 IODirection
/* withDirection */ )
80 bool IOBufferMemoryDescriptor::initWithPhysicalRanges(
81 IOPhysicalRange
* /* ranges */ ,
82 UInt32
/* withCount */ ,
83 IODirection
/* withDirection */ ,
84 bool /* asReference */ )
89 bool IOBufferMemoryDescriptor::initWithRanges(
90 IOVirtualRange
* /* ranges */ ,
91 UInt32
/* withCount */ ,
92 IODirection
/* withDirection */ ,
93 task_t
/* withTask */ ,
94 bool /* asReference */ )
99 bool IOBufferMemoryDescriptor::initWithOptions(
100 IOOptionBits options
,
102 vm_offset_t alignment
,
105 mach_vm_address_t physicalMask
= 0;
106 return (initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
));
109 bool IOBufferMemoryDescriptor::initWithPhysicalMask(
111 IOOptionBits options
,
112 mach_vm_size_t capacity
,
113 mach_vm_address_t alignment
,
114 mach_vm_address_t physicalMask
)
117 task_t mapTask
= NULL
;
118 vm_map_t vmmap
= NULL
;
120 IOAddressRange range
;
121 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
;
127 _capacity
= capacity
;
133 _ranges
.v64
= &range
;
135 // Grab the direction and the Auto Prepare bits from the Buffer MD options
136 iomdOptions
|= options
& (kIOMemoryDirectionMask
| kIOMemoryAutoPrepare
);
138 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
)) && (alignment
< page_size
))
139 alignment
= page_size
;
141 if (physicalMask
&& (alignment
<= 1))
142 alignment
= ((physicalMask
^ PAGE_MASK
) & PAGE_MASK
) + 1;
144 _alignment
= alignment
;
146 if (((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
)) ||
147 (physicalMask
&& (options
& kIOMapCacheMask
)))
150 if ((options
& kIOMemoryPhysicallyContiguous
) && !physicalMask
)
151 physicalMask
= 0xFFFFFFFF;
153 // set flags for entry + object create
154 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
;
156 // set memory entry cache mode
157 switch (options
& kIOMapCacheMask
)
159 case kIOMapInhibitCache
:
160 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
163 case kIOMapWriteThruCache
:
164 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
167 case kIOMapWriteCombineCache
:
168 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
171 case kIOMapCopybackCache
:
172 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
175 case kIOMapDefaultCache
:
177 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
181 if (options
& kIOMemoryPageable
)
183 iomdOptions
|= kIOMemoryBufferPageable
;
185 // must create the entry before any pages are allocated
187 // set flags for entry + object create
188 memEntryCacheMode
|= MAP_MEM_NAMED_CREATE
;
190 if (options
& kIOMemoryPurgeable
)
191 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
195 memEntryCacheMode
|= MAP_MEM_NAMED_REUSE
;
197 if (IOMapper::gSystem
)
198 // assuming mapped space is 2G
199 lastIOAddr
= (1UL << 31) - PAGE_SIZE
;
201 lastIOAddr
= ptoa_64(gIOHighestAllocatedPage
);
203 if (physicalMask
&& (lastIOAddr
!= (lastIOAddr
& physicalMask
)))
205 mach_vm_address_t address
;
206 iomdOptions
&= ~kIOMemoryTypeVirtual64
;
207 iomdOptions
|= kIOMemoryTypePhysical64
;
209 address
= IOMallocPhysical(capacity
, physicalMask
);
210 _buffer
= (void *) address
;
221 // Buffer shouldn't auto prepare they should be prepared explicitly
222 // But it never was enforced so what are you going to do?
223 iomdOptions
|= kIOMemoryAutoPrepare
;
225 /* Allocate a wired-down buffer inside kernel space. */
226 if (options
& kIOMemoryPhysicallyContiguous
)
227 _buffer
= (void *) IOKernelAllocateContiguous(capacity
, alignment
);
228 else if (alignment
> 1)
229 _buffer
= IOMallocAligned(capacity
, alignment
);
231 _buffer
= IOMalloc(capacity
);
237 if( (kIOMemoryTypePhysical64
!= (kIOMemoryTypeMask
& iomdOptions
))
238 && (options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
239 ipc_port_t sharedMem
;
240 vm_size_t size
= round_page_32(capacity
);
242 kr
= mach_make_memory_entry(vmmap
,
243 &size
, (vm_offset_t
)_buffer
,
244 memEntryCacheMode
, &sharedMem
,
247 if( (KERN_SUCCESS
== kr
) && (size
!= round_page_32(capacity
))) {
248 ipc_port_release_send( sharedMem
);
249 kr
= kIOReturnVMError
;
251 if( KERN_SUCCESS
!= kr
)
254 _memEntry
= (void *) sharedMem
;
256 if( options
& kIOMemoryPageable
) {
258 debug_iomallocpageable_size
+= size
;
262 inTask
= kernel_task
;
264 else if (options
& kIOMapCacheMask
)
266 // Prefetch each page to put entries into the pmap
267 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
268 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
270 while (startAddr
< endAddr
)
273 startAddr
+= page_size
;
278 range
.address
= (mach_vm_address_t
) _buffer
;
279 range
.length
= capacity
;
281 if (!super::initWithOptions(&range
, 1, 0,
282 inTask
, iomdOptions
, /* System mapper */ 0))
285 if (physicalMask
&& !IOMapper::gSystem
)
287 IOMDDMACharacteristics mdSummary
;
289 bzero(&mdSummary
, sizeof(mdSummary
));
290 IOReturn rtn
= dmaCommandOperation(
291 kIOMDGetCharacteristics
,
292 &mdSummary
, sizeof(mdSummary
));
296 if (mdSummary
.fHighestPage
)
299 while (mdSummary
.fHighestPage
> (highest
= gIOHighestAllocatedPage
))
301 if (OSCompareAndSwap(highest
, mdSummary
.fHighestPage
,
302 (UInt32
*) &gIOHighestAllocatedPage
))
305 lastIOAddr
= ptoa_64(mdSummary
.fHighestPage
);
308 lastIOAddr
= ptoa_64(gIOLastPage
);
310 if (lastIOAddr
!= (lastIOAddr
& physicalMask
))
312 if (kIOMemoryTypePhysical64
!= (_flags
& kIOMemoryTypeMask
))
324 reserved
= IONew( ExpansionData
, 1 );
328 reserved
->map
= map(mapTask
, 0, kIOMapAnywhere
, 0, 0);
334 release(); // map took a retain on this
335 mach_vm_address_t buffer
= reserved
->map
->getAddress();
336 _buffer
= (void *) buffer
;
337 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
))
338 _ranges
.v64
->address
= buffer
;
346 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
348 IOOptionBits options
,
350 vm_offset_t alignment
)
352 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
354 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, inTask
)) {
355 bool retry
= me
->_physSegCount
;
360 me
= new IOBufferMemoryDescriptor
;
361 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, inTask
))
371 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
373 IOOptionBits options
,
374 mach_vm_size_t capacity
,
375 mach_vm_address_t physicalMask
)
377 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
379 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
381 bool retry
= me
->_physSegCount
;
386 me
= new IOBufferMemoryDescriptor
;
387 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
397 bool IOBufferMemoryDescriptor::initWithOptions(
398 IOOptionBits options
,
400 vm_offset_t alignment
)
402 return( initWithOptions(options
, capacity
, alignment
, kernel_task
) );
405 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
406 IOOptionBits options
,
408 vm_offset_t alignment
)
410 return(IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task
, options
, capacity
, alignment
));
417 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
418 * hold capacity bytes. The descriptor's length is initially set to the capacity.
420 IOBufferMemoryDescriptor
*
421 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
422 IODirection inDirection
,
425 return( IOBufferMemoryDescriptor::withOptions(
426 inDirection
| kIOMemoryUnshared
427 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
428 inCapacity
, inContiguous
? inCapacity
: 1 ));
434 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
435 * The descriptor's length and capacity are set to the input buffer's size.
437 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
439 IODirection inDirection
,
442 if (!initWithOptions(
443 inDirection
| kIOMemoryUnshared
444 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
445 inLength
, inLength
))
448 // start out with no data
451 if (!appendBytes(inBytes
, inLength
))
460 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
461 * The descriptor's length and capacity are set to the input buffer's size.
463 IOBufferMemoryDescriptor
*
464 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
466 IODirection inDirection
,
469 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
471 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
))
473 bool retry
= me
->_physSegCount
;
478 me
= new IOBufferMemoryDescriptor
;
479 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
))
495 void IOBufferMemoryDescriptor::free()
497 // Cache all of the relevant information on the stack for use
498 // after we call super::free()!
499 IOOptionBits flags
= _flags
;
500 IOOptionBits options
= _options
;
501 vm_size_t size
= _capacity
;
502 void * buffer
= _buffer
;
503 mach_vm_address_t source
= (_ranges
.v
) ? _ranges
.v64
->address
: 0;
504 IOMemoryMap
* map
= 0;
505 vm_offset_t alignment
= _alignment
;
510 IODelete( reserved
, ExpansionData
, 1 );
515 /* super::free may unwire - deallocate buffer afterwards */
518 if (options
& kIOMemoryPageable
)
521 debug_iomallocpageable_size
-= round_page_32(size
);
526 if (kIOMemoryTypePhysical64
== (flags
& kIOMemoryTypeMask
))
527 IOFreePhysical(source
, size
);
528 else if (options
& kIOMemoryPhysicallyContiguous
)
529 IOKernelFreeContiguous((mach_vm_address_t
) buffer
, size
);
530 else if (alignment
> 1)
531 IOFreeAligned(buffer
, size
);
533 IOFree(buffer
, size
);
540 * Get the buffer capacity
542 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
550 * Change the buffer length of the memory descriptor. When a new buffer
551 * is created, the initial length of the buffer is set to be the same as
552 * the capacity. The length can be adjusted via setLength for a shorter
553 * transfer (there is no need to create more buffer descriptors when you
554 * can reuse an existing one, even for different transfer sizes). Note
555 * that the specified length must not exceed the capacity of the buffer.
557 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
559 assert(length
<= _capacity
);
562 _ranges
.v64
->length
= length
;
568 * Change the direction of the transfer. This method allows one to redirect
569 * the descriptor's transfer direction. This eliminates the need to destroy
570 * and create new buffers when different transfer directions are needed.
572 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
574 _direction
= direction
;
580 * Add some data to the end of the buffer. This method automatically
581 * maintains the memory descriptor buffer length. Note that appendBytes
582 * will not copy past the end of the memory descriptor's current capacity.
585 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
587 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
590 assert(_length
<= _capacity
);
593 _length
+= actualBytesToCopy
;
594 _ranges
.v64
->length
+= actualBytesToCopy
;
596 if (_task
== kernel_task
)
597 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
600 writeBytes(offset
, bytes
, actualBytesToCopy
);
608 * Return the virtual address of the beginning of the buffer
610 void * IOBufferMemoryDescriptor::getBytesNoCopy()
612 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
615 return (void *)_ranges
.v64
->address
;
622 * Return the virtual address of an offset from the beginning of the buffer
625 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
627 IOVirtualAddress address
;
628 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
629 address
= (IOVirtualAddress
) _buffer
;
631 address
= _ranges
.v64
->address
;
633 if (start
< _length
&& (start
+ withLength
) <= _length
)
634 return (void *)(address
+ start
);
638 /* DEPRECATED */ void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
639 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
641 void * bytes
= getBytesNoCopy(offset
, 0);
643 if (bytes
&& lengthOfSegment
)
644 *lengthOfSegment
= _length
- offset
;
649 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
650 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
651 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
652 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
653 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
654 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
655 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
656 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
657 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
658 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
659 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
660 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
661 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
662 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
663 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
664 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);