]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOBufferMemoryDescriptor.h
f42f66489733d327bc891fd03a8a7539a9be835e
[apple/xnu.git] / iokit / IOKit / IOBufferMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #ifndef _IOBUFFERMEMORYDESCRIPTOR_H
29 #define _IOBUFFERMEMORYDESCRIPTOR_H
30
31 #include <IOKit/IOMemoryDescriptor.h>
32
33 enum {
34 kIOMemoryPhysicallyContiguous = 0x00000010,
35 kIOMemoryPageable = 0x00000020,
36 kIOMemoryPurgeable = 0x00000040,
37 kIOMemorySharingTypeMask = 0x000f0000,
38 kIOMemoryUnshared = 0x00000000,
39 kIOMemoryKernelUserShared = 0x00010000,
40 // shared IOMemoryDescriptor options for IOBufferMemoryDescriptor:
41 kIOBufferDescriptorMemoryFlags = kIOMemoryDirectionMask
42 #ifdef XNU_KERNEL_PRIVATE
43 | kIOMemoryAutoPrepare
44 #endif
45 | kIOMemoryThreadSafe
46 };
47
48 #define _IOBUFFERMEMORYDESCRIPTOR_INTASKWITHOPTIONS_ 1
49 /*!
50 @class IOBufferMemoryDescriptor
51 @abstract Provides a simple memory descriptor that allocates its own buffer memory.
52 */
53
54 class IOBufferMemoryDescriptor : public IOGeneralMemoryDescriptor
55 {
56 OSDeclareDefaultStructors(IOBufferMemoryDescriptor);
57
58 private:
59 /*! @struct ExpansionData
60 @discussion This structure will be used to expand the capablilties of this class in the future.
61 */
62 struct ExpansionData {
63 IOMemoryMap * map;
64 };
65
66 /*! @var reserved
67 Reserved for future use. (Internal use only) */
68 ExpansionData * reserved;
69
70 protected:
71 void * _buffer;
72 vm_size_t _capacity;
73 vm_offset_t _alignment;
74 IOOptionBits _options;
75 IOPhysicalAddress * _physAddrs;
76 unsigned _physSegCount;
77
78 private:
79 virtual bool initWithOptions(
80 IOOptionBits options,
81 vm_size_t capacity,
82 vm_offset_t alignment,
83 task_t inTask);
84
85 virtual bool initWithPhysicalMask(
86 task_t inTask,
87 IOOptionBits options,
88 mach_vm_size_t capacity,
89 mach_vm_address_t alignment,
90 mach_vm_address_t physicalMask);
91
92 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 0);
93 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 1);
94 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 2);
95 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 3);
96 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 4);
97 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 5);
98 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 6);
99 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 7);
100 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 8);
101 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 9);
102 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 10);
103 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 11);
104 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 12);
105 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 13);
106 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 14);
107 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 15);
108
109 protected:
110 virtual void free();
111
112 virtual bool initWithAddress( void * address, /* not supported */
113 IOByteCount withLength,
114 IODirection withDirection );
115
116 virtual bool initWithAddress( vm_address_t address, /* not supported */
117 IOByteCount withLength,
118 IODirection withDirection,
119 task_t withTask );
120
121 virtual bool initWithPhysicalAddress(
122 IOPhysicalAddress address, /* not supported */
123 IOByteCount withLength,
124 IODirection withDirection );
125
126 virtual bool initWithPhysicalRanges(
127 IOPhysicalRange * ranges, /* not supported */
128 UInt32 withCount,
129 IODirection withDirection,
130 bool asReference = false );
131
132 virtual bool initWithRanges( IOVirtualRange * ranges, /* not supported */
133 UInt32 withCount,
134 IODirection withDirection,
135 task_t withTask,
136 bool asReference = false );
137
138 IOGeneralMemoryDescriptor::withAddress; /* not supported */
139 IOGeneralMemoryDescriptor::withPhysicalAddress; /* not supported */
140 IOGeneralMemoryDescriptor::withPhysicalRanges; /* not supported */
141 IOGeneralMemoryDescriptor::withRanges; /* not supported */
142 IOGeneralMemoryDescriptor::withSubRange; /* not supported */
143
144 public:
145
146 /*
147 * withOptions:
148 *
149 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
150 * hold capacity bytes. The descriptor's length is initially set to the
151 * capacity.
152 */
153 virtual bool initWithOptions( IOOptionBits options,
154 vm_size_t capacity,
155 vm_offset_t alignment);
156
157 static IOBufferMemoryDescriptor * withOptions( IOOptionBits options,
158 vm_size_t capacity,
159 vm_offset_t alignment = 1);
160
161 /*! @function inTaskWithOptions
162 @abstract Creates a memory buffer with memory descriptor for that buffer.
163 @discussion Added in Mac OS X 10.2, this method allocates a memory buffer with a given size and alignment in the task's address space specified, and returns a memory descriptor instance representing the memory. It is recommended that memory allocated for I/O or sharing via mapping be created via IOBufferMemoryDescriptor. Options passed with the request specify the kind of memory to be allocated - pageablity and sharing are specified with option bits. This function may block and so should not be called from interrupt level or while a simple lock is held.
164 @param inTask The task the buffer will be allocated in.
165 @param options Options for the allocation:<br>
166 kIODirectionOut, kIODirectionIn - set the direction of the I/O transfer.<br>
167 kIOMemoryPhysicallyContiguous - pass to request memory be physically contiguous. This option is heavily discouraged. The request may fail if memory is fragmented, may cause large amounts of paging activity, and may take a very long time to execute.<br>
168 kIOMemoryPageable - pass to request memory be non-wired - the default for kernel allocated memory is wired.<br>
169 kIOMemoryPurgeable - pass to request memory that may later have its purgeable state set with IOMemoryDescriptor::setPurgeable. Only supported for kIOMemoryPageable allocations.<br>
170 kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.
171 @param capacity The number of bytes to allocate.
172 @param alignment The minimum required alignment of the buffer in bytes - 1 is the default for no required alignment. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
173 @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
174
175 static IOBufferMemoryDescriptor * inTaskWithOptions(
176 task_t inTask,
177 IOOptionBits options,
178 vm_size_t capacity,
179 vm_offset_t alignment = 1);
180
181 /*! @function inTaskWithPhysicalMask
182 @abstract Creates a memory buffer with memory descriptor for that buffer.
183 @discussion Added in Mac OS X 10.5, this method allocates a memory buffer with a given size and alignment in the task's address space specified, and returns a memory descriptor instance representing the memory. It is recommended that memory allocated for I/O or sharing via mapping be created via IOBufferMemoryDescriptor. Options passed with the request specify the kind of memory to be allocated - pageablity and sharing are specified with option bits. This function may block and so should not be called from interrupt level or while a simple lock is held.
184 @param inTask The task the buffer will be mapped in. Pass NULL to create memory unmapped in any task (eg. for use as a DMA buffer).
185 @param options Options for the allocation:<br>
186 kIODirectionOut, kIODirectionIn - set the direction of the I/O transfer.<br>
187 kIOMemoryPhysicallyContiguous - pass to request memory be physically contiguous. This option is heavily discouraged. The request may fail if memory is fragmented, may cause large amounts of paging activity, and may take a very long time to execute.<br>
188 kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.
189 @param capacity The number of bytes to allocate.
190 @param mask The buffer will be allocated with pages such that physical addresses will only have bits set present in physicalMask. For example, pass 0x00000000FFFFFFFFULL for a buffer to be accessed by hardware that has 32 address bits.
191 @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
192
193 static IOBufferMemoryDescriptor * inTaskWithPhysicalMask(
194 task_t inTask,
195 IOOptionBits options,
196 mach_vm_size_t capacity,
197 mach_vm_address_t physicalMask);
198
199 /*
200 * withCapacity:
201 *
202 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
203 * hold capacity bytes. The descriptor's length is initially set to the
204 * capacity.
205 */
206 static IOBufferMemoryDescriptor * withCapacity(
207 vm_size_t capacity,
208 IODirection withDirection,
209 bool withContiguousMemory = false);
210 /*
211 * initWithBytes:
212 *
213 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
214 * The descriptor's length and capacity are set to the input buffer's size.
215 */
216 virtual bool initWithBytes(const void * bytes,
217 vm_size_t withLength,
218 IODirection withDirection,
219 bool withContiguousMemory = false);
220
221 /*
222 * withBytes:
223 *
224 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
225 * The descriptor's length and capacity are set to the input buffer's size.
226 */
227 static IOBufferMemoryDescriptor * withBytes(
228 const void * bytes,
229 vm_size_t withLength,
230 IODirection withDirection,
231 bool withContiguousMemory = false);
232
233 /*
234 * setLength:
235 *
236 * Change the buffer length of the memory descriptor. When a new buffer
237 * is created, the initial length of the buffer is set to be the same as
238 * the capacity. The length can be adjusted via setLength for a shorter
239 * transfer (there is no need to create more buffer descriptors when you
240 * can reuse an existing one, even for different transfer sizes). Note
241 * that the specified length must not exceed the capacity of the buffer.
242 */
243 virtual void setLength(vm_size_t length);
244
245 /*
246 * setDirection:
247 *
248 * Change the direction of the transfer. This method allows one to redirect
249 * the descriptor's transfer direction. This eliminates the need to destroy
250 * and create new buffers when different transfer directions are needed.
251 */
252 virtual void setDirection(IODirection direction);
253
254 /*
255 * getCapacity:
256 *
257 * Get the buffer capacity
258 */
259 virtual vm_size_t getCapacity() const;
260
261 /*
262 * getBytesNoCopy:
263 *
264 * Return the virtual address of the beginning of the buffer
265 */
266 virtual void *getBytesNoCopy();
267
268 /*
269 * getBytesNoCopy:
270 *
271 * Return the virtual address of an offset from the beginning of the buffer
272 */
273 virtual void *getBytesNoCopy(vm_size_t start, vm_size_t withLength);
274
275 /*
276 * appendBytes:
277 *
278 * Add some data to the end of the buffer. This method automatically
279 * maintains the memory descriptor buffer length. Note that appendBytes
280 * will not copy past the end of the memory descriptor's current capacity.
281 */
282 virtual bool appendBytes(const void *bytes, vm_size_t withLength);
283
284 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
285 /* DEPRECATED */ IOByteCount * length);
286 };
287
288 #endif /* !_IOBUFFERMEMORYDESCRIPTOR_H */