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/IOMapper.h>
27 #include <IOKit/IOBufferMemoryDescriptor.h>
29 #include "IOKitKernelInternal.h"
30 #include "IOCopyMapper.h"
33 void ipc_port_release_send(ipc_port_t port
);
36 vm_map_t
IOPageableMapForAddress( vm_address_t address
);
39 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41 volatile ppnum_t gIOHighestAllocatedPage
;
43 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45 #define super IOGeneralMemoryDescriptor
46 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
47 IOGeneralMemoryDescriptor
);
49 bool IOBufferMemoryDescriptor::initWithAddress(
50 void * /* address */ ,
51 IOByteCount
/* withLength */ ,
52 IODirection
/* withDirection */ )
57 bool IOBufferMemoryDescriptor::initWithAddress(
58 vm_address_t
/* address */ ,
59 IOByteCount
/* withLength */ ,
60 IODirection
/* withDirection */ ,
61 task_t
/* withTask */ )
66 bool IOBufferMemoryDescriptor::initWithPhysicalAddress(
67 IOPhysicalAddress
/* address */ ,
68 IOByteCount
/* withLength */ ,
69 IODirection
/* withDirection */ )
74 bool IOBufferMemoryDescriptor::initWithPhysicalRanges(
75 IOPhysicalRange
* /* ranges */ ,
76 UInt32
/* withCount */ ,
77 IODirection
/* withDirection */ ,
78 bool /* asReference */ )
83 bool IOBufferMemoryDescriptor::initWithRanges(
84 IOVirtualRange
* /* ranges */ ,
85 UInt32
/* withCount */ ,
86 IODirection
/* withDirection */ ,
87 task_t
/* withTask */ ,
88 bool /* asReference */ )
93 bool IOBufferMemoryDescriptor::initWithOptions(
96 vm_offset_t alignment
,
99 mach_vm_address_t physicalMask
= 0;
100 return (initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
));
103 bool IOBufferMemoryDescriptor::initWithPhysicalMask(
105 IOOptionBits options
,
106 mach_vm_size_t capacity
,
107 mach_vm_address_t alignment
,
108 mach_vm_address_t physicalMask
)
113 IOOptionBits iomdOptions
= kIOMemoryAsReference
| kIOMemoryTypeVirtual
;
119 _capacity
= capacity
;
124 // Grab the direction and the Auto Prepare bits from the Buffer MD options
125 iomdOptions
|= options
& (kIOMemoryDirectionMask
| kIOMemoryAutoPrepare
);
127 if ((options
& kIOMemorySharingTypeMask
) && (alignment
< page_size
))
128 alignment
= page_size
;
130 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
133 if (physicalMask
&& (alignment
<= 1))
134 alignment
= ((physicalMask
^ PAGE_MASK
) & PAGE_MASK
) + 1;
136 if ((options
& kIOMemoryPhysicallyContiguous
) && !physicalMask
)
137 physicalMask
= 0xFFFFFFFF;
139 _alignment
= alignment
;
140 if (options
& kIOMemoryPageable
)
142 iomdOptions
|= kIOMemoryBufferPageable
;
144 ipc_port_t sharedMem
;
145 vm_size_t size
= round_page_32(capacity
);
147 // must create the entry before any pages are allocated
149 // set flags for entry + object create
150 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
151 | MAP_MEM_NAMED_CREATE
;
153 if (options
& kIOMemoryPurgeable
)
154 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
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 kr
= mach_make_memory_entry( vmmap
,
183 memEntryCacheMode
, &sharedMem
,
186 if( (KERN_SUCCESS
== kr
) && (size
!= round_page_32(capacity
))) {
187 ipc_port_release_send( sharedMem
);
188 kr
= kIOReturnVMError
;
190 if( KERN_SUCCESS
!= kr
)
193 _memEntry
= (void *) sharedMem
;
195 debug_iomallocpageable_size
+= size
;
198 inTask
= kernel_task
;
199 else if (inTask
== kernel_task
)
206 reserved
= IONew( ExpansionData
, 1 );
210 vmmap
= get_task_map(inTask
);
211 vm_map_reference(vmmap
);
212 reserved
->map
= vmmap
;
217 if (IOMapper::gSystem
)
218 // assuming mapped space is 2G
219 lastIOAddr
= (1UL << 31) - PAGE_SIZE
;
221 lastIOAddr
= ptoa_64(gIOHighestAllocatedPage
);
223 if (physicalMask
&& (lastIOAddr
!= (lastIOAddr
& physicalMask
)))
225 mach_vm_address_t address
;
226 iomdOptions
&= ~kIOMemoryTypeVirtual
;
227 iomdOptions
|= kIOMemoryTypePhysical
;
229 address
= IOMallocPhysical(capacity
, physicalMask
);
230 _buffer
= (void *) address
;
234 if (inTask
== kernel_task
)
238 else if (NULL
!= inTask
)
241 reserved
= IONew( ExpansionData
, 1 );
245 vmmap
= get_task_map(inTask
);
246 vm_map_reference(vmmap
);
247 reserved
->map
= vmmap
;
253 // Buffer shouldn't auto prepare they should be prepared explicitly
254 // But it never was enforced so what are you going to do?
255 iomdOptions
|= kIOMemoryAutoPrepare
;
257 /* Allocate a wired-down buffer inside kernel space. */
258 if (options
& kIOMemoryPhysicallyContiguous
)
259 _buffer
= (void *) IOKernelAllocateContiguous(capacity
, alignment
);
260 else if (alignment
> 1)
261 _buffer
= IOMallocAligned(capacity
, alignment
);
263 _buffer
= IOMalloc(capacity
);
269 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
270 _singleRange
.v
.length
= capacity
;
272 if (!super::initWithOptions(&_singleRange
.v
, 1, 0,
273 inTask
, iomdOptions
, /* System mapper */ 0))
276 if (physicalMask
&& !IOMapper::gSystem
)
278 IOMDDMACharacteristics mdSummary
;
280 bzero(&mdSummary
, sizeof(mdSummary
));
281 IOReturn rtn
= dmaCommandOperation(
282 kIOMDGetCharacteristics
,
283 &mdSummary
, sizeof(mdSummary
));
287 if (mdSummary
.fHighestPage
)
290 while (mdSummary
.fHighestPage
> (highest
= gIOHighestAllocatedPage
))
292 if (OSCompareAndSwap(highest
, mdSummary
.fHighestPage
,
293 (UInt32
*) &gIOHighestAllocatedPage
))
296 lastIOAddr
= ptoa_64(mdSummary
.fHighestPage
);
299 lastIOAddr
= ptoa_64(gIOLastPage
);
301 if (lastIOAddr
!= (lastIOAddr
& physicalMask
))
303 if (kIOMemoryTypePhysical
!= (_flags
& kIOMemoryTypeMask
))
314 kr
= doMap(vmmap
, (IOVirtualAddress
*) &_buffer
, kIOMapAnywhere
, 0, capacity
);
315 if (KERN_SUCCESS
!= kr
)
321 if (kIOMemoryTypeVirtual
& iomdOptions
)
322 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
330 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
332 IOOptionBits options
,
334 vm_offset_t alignment
)
336 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
338 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, inTask
)) {
339 bool retry
= me
->_physSegCount
;
344 me
= new IOBufferMemoryDescriptor
;
345 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, inTask
))
355 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
357 IOOptionBits options
,
358 mach_vm_size_t capacity
,
359 mach_vm_address_t physicalMask
)
361 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
363 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
365 bool retry
= me
->_physSegCount
;
370 me
= new IOBufferMemoryDescriptor
;
371 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
381 bool IOBufferMemoryDescriptor::initWithOptions(
382 IOOptionBits options
,
384 vm_offset_t alignment
)
386 return( initWithOptions(options
, capacity
, alignment
, kernel_task
) );
389 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
390 IOOptionBits options
,
392 vm_offset_t alignment
)
394 return(IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task
, options
, capacity
, alignment
));
401 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
402 * hold capacity bytes. The descriptor's length is initially set to the capacity.
404 IOBufferMemoryDescriptor
*
405 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
406 IODirection inDirection
,
409 return( IOBufferMemoryDescriptor::withOptions(
410 inDirection
| kIOMemoryUnshared
411 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
412 inCapacity
, inContiguous
? inCapacity
: 1 ));
418 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
419 * The descriptor's length and capacity are set to the input buffer's size.
421 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
423 IODirection inDirection
,
426 if (!initWithOptions(
427 inDirection
| kIOMemoryUnshared
428 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
429 inLength
, inLength
))
432 // start out with no data
435 if (!appendBytes(inBytes
, inLength
))
444 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
445 * The descriptor's length and capacity are set to the input buffer's size.
447 IOBufferMemoryDescriptor
*
448 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
450 IODirection inDirection
,
453 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
455 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
))
457 bool retry
= me
->_physSegCount
;
462 me
= new IOBufferMemoryDescriptor
;
463 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
))
479 void IOBufferMemoryDescriptor::free()
481 // Cache all of the relevant information on the stack for use
482 // after we call super::free()!
483 IOOptionBits flags
= _flags
;
484 IOOptionBits options
= _options
;
485 vm_size_t size
= _capacity
;
486 void * buffer
= _buffer
;
487 IOVirtualAddress source
= _singleRange
.v
.address
;
489 vm_offset_t alignment
= _alignment
;
493 vmmap
= reserved
->map
;
494 IODelete( reserved
, ExpansionData
, 1 );
497 /* super::free may unwire - deallocate buffer afterwards */
500 if (options
& kIOMemoryPageable
)
503 if (!buffer
|| vmmap
)
504 debug_iomallocpageable_size
-= round_page_32(size
);
509 vm_deallocate(vmmap
, (vm_address_t
) buffer
, round_page_32(size
));
511 IOFreePageable(buffer
, size
);
516 if (kIOMemoryTypePhysical
== (flags
& kIOMemoryTypeMask
))
519 vm_deallocate(vmmap
, (vm_address_t
) buffer
, round_page_32(size
));
520 IOFreePhysical((mach_vm_address_t
) source
, size
);
522 else if (options
& kIOMemoryPhysicallyContiguous
)
523 IOKernelFreeContiguous((mach_vm_address_t
) buffer
, size
);
524 else if (alignment
> 1)
525 IOFreeAligned(buffer
, size
);
527 IOFree(buffer
, size
);
530 vm_map_deallocate(vmmap
);
536 * Get the buffer capacity
538 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
546 * Change the buffer length of the memory descriptor. When a new buffer
547 * is created, the initial length of the buffer is set to be the same as
548 * the capacity. The length can be adjusted via setLength for a shorter
549 * transfer (there is no need to create more buffer descriptors when you
550 * can reuse an existing one, even for different transfer sizes). Note
551 * that the specified length must not exceed the capacity of the buffer.
553 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
555 assert(length
<= _capacity
);
558 _singleRange
.v
.length
= length
;
564 * Change the direction of the transfer. This method allows one to redirect
565 * the descriptor's transfer direction. This eliminates the need to destroy
566 * and create new buffers when different transfer directions are needed.
568 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
570 _direction
= direction
;
576 * Add some data to the end of the buffer. This method automatically
577 * maintains the memory descriptor buffer length. Note that appendBytes
578 * will not copy past the end of the memory descriptor's current capacity.
581 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
583 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
586 assert(_length
<= _capacity
);
589 _length
+= actualBytesToCopy
;
590 _singleRange
.v
.length
+= actualBytesToCopy
;
592 if (_task
== kernel_task
)
593 bcopy(/* from */ bytes
, (void *)(_singleRange
.v
.address
+ offset
),
596 writeBytes(offset
, bytes
, actualBytesToCopy
);
604 * Return the virtual address of the beginning of the buffer
606 void * IOBufferMemoryDescriptor::getBytesNoCopy()
608 if (kIOMemoryTypePhysical
== (_flags
& kIOMemoryTypeMask
))
611 return (void *)_singleRange
.v
.address
;
618 * Return the virtual address of an offset from the beginning of the buffer
621 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
623 IOVirtualAddress address
;
624 if (kIOMemoryTypePhysical
== (_flags
& kIOMemoryTypeMask
))
625 address
= (IOVirtualAddress
) _buffer
;
627 address
= _singleRange
.v
.address
;
629 if (start
< _length
&& (start
+ withLength
) <= _length
)
630 return (void *)(address
+ start
);
634 /* DEPRECATED */ void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
635 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
637 void * bytes
= getBytesNoCopy(offset
, 0);
639 if (bytes
&& lengthOfSegment
)
640 *lengthOfSegment
= _length
- offset
;
645 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
646 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
647 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
648 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
649 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
650 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
651 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
652 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
653 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
654 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
655 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
656 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
657 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
658 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
659 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
660 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);