]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOBufferMemoryDescriptor.h
fcfba8f8a1d40ce9f8cd21f49a8590bb120cb189
[apple/xnu.git] / iokit / IOKit / IOBufferMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 #ifndef _IOBUFFERMEMORYDESCRIPTOR_H
31 #define _IOBUFFERMEMORYDESCRIPTOR_H
32
33 #include <IOKit/IOMemoryDescriptor.h>
34
35 enum {
36 kIOMemoryPhysicallyContiguous = 0x00000010,
37 kIOMemoryPageable = 0x00000020,
38 kIOMemoryPurgeable = 0x00000040,
39 kIOMemorySharingTypeMask = 0x000f0000,
40 kIOMemoryUnshared = 0x00000000,
41 kIOMemoryKernelUserShared = 0x00010000
42 };
43
44 #define _IOBUFFERMEMORYDESCRIPTOR_INTASKWITHOPTIONS_ 1
45 /*!
46 @class IOBufferMemoryDescriptor
47 @abstract Provides a simple memory descriptor that allocates its own buffer memory.
48 */
49
50 class IOBufferMemoryDescriptor : public IOGeneralMemoryDescriptor
51 {
52 OSDeclareDefaultStructors(IOBufferMemoryDescriptor);
53
54 protected:
55 /*! @struct ExpansionData
56 @discussion This structure will be used to expand the capablilties of this class in the future.
57 */
58 struct ExpansionData {
59 vm_map_t map;
60 };
61
62 /*! @var reserved
63 Reserved for future use. (Internal use only) */
64 ExpansionData * reserved;
65
66 protected:
67 void * _buffer;
68 vm_size_t _capacity;
69 vm_offset_t _alignment;
70 IOOptionBits _options;
71 IOPhysicalAddress * _physAddrs;
72 unsigned _physSegCount;
73
74 private:
75 virtual bool initWithOptions(
76 IOOptionBits options,
77 vm_size_t capacity,
78 vm_offset_t alignment,
79 task_t inTask);
80 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 0);
81
82 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
83 virtual bool initWithPhysicalMask(
84 task_t inTask,
85 IOOptionBits options,
86 mach_vm_size_t capacity,
87 mach_vm_address_t alignment,
88 mach_vm_address_t physicalMask);
89 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 1);
90 #else
91 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 1);
92 #endif
93 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 2);
94 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 3);
95 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 4);
96 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 5);
97 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 6);
98 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 7);
99 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 8);
100 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 9);
101 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 10);
102 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 11);
103 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 12);
104 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 13);
105 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 14);
106 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 15);
107
108 protected:
109 virtual void free();
110
111 virtual bool initWithAddress( void * address, /* not supported */
112 IOByteCount withLength,
113 IODirection withDirection );
114
115 virtual bool initWithAddress( vm_address_t address, /* not supported */
116 IOByteCount withLength,
117 IODirection withDirection,
118 task_t withTask );
119
120 virtual bool initWithPhysicalAddress(
121 IOPhysicalAddress address, /* not supported */
122 IOByteCount withLength,
123 IODirection withDirection );
124
125 virtual bool initWithPhysicalRanges(
126 IOPhysicalRange * ranges, /* not supported */
127 UInt32 withCount,
128 IODirection withDirection,
129 bool asReference = false );
130
131 virtual bool initWithRanges( IOVirtualRange * ranges, /* not supported */
132 UInt32 withCount,
133 IODirection withDirection,
134 task_t withTask,
135 bool asReference = false );
136
137 IOGeneralMemoryDescriptor::withAddress; /* not supported */
138 IOGeneralMemoryDescriptor::withPhysicalAddress; /* not supported */
139 IOGeneralMemoryDescriptor::withPhysicalRanges; /* not supported */
140 IOGeneralMemoryDescriptor::withRanges; /* not supported */
141 IOGeneralMemoryDescriptor::withSubRange; /* not supported */
142
143 public:
144
145 /*
146 * withOptions:
147 *
148 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
149 * hold capacity bytes. The descriptor's length is initially set to the
150 * capacity.
151 */
152 virtual bool initWithOptions( IOOptionBits options,
153 vm_size_t capacity,
154 vm_offset_t alignment);
155
156 static IOBufferMemoryDescriptor * withOptions( IOOptionBits options,
157 vm_size_t capacity,
158 vm_offset_t alignment = 1);
159
160 /*! @function inTaskWithOptions
161 @abstract Creates a memory buffer with memory descriptor for that buffer.
162 @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.
163 @param inTask The task the buffer will be allocated in.
164 @param options Options for the allocation:<br>
165 kIODirectionOut, kIODirectionIn - set the direction of the I/O transfer.<br>
166 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>
167 kIOMemoryPageable - pass to request memory be non-wired - the default for kernel allocated memory is wired.<br>
168 kIOMemoryPurgeable - pass to request memory that may later have its purgeable state set with IOMemoryDescriptor::setPurgeable. Only supported for kIOMemoryPageable allocations.<br>
169 kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.
170 @param capacity The number of bytes to allocate.
171 @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.
172 @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
173
174 static IOBufferMemoryDescriptor * inTaskWithOptions(
175 task_t inTask,
176 IOOptionBits options,
177 vm_size_t capacity,
178 vm_offset_t alignment = 1);
179
180 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
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 #endif
199
200 /*
201 * withCapacity:
202 *
203 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
204 * hold capacity bytes. The descriptor's length is initially set to the
205 * capacity.
206 */
207 static IOBufferMemoryDescriptor * withCapacity(
208 vm_size_t capacity,
209 IODirection withDirection,
210 bool withContiguousMemory = false);
211 /*
212 * initWithBytes:
213 *
214 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
215 * The descriptor's length and capacity are set to the input buffer's size.
216 */
217 virtual bool initWithBytes(const void * bytes,
218 vm_size_t withLength,
219 IODirection withDirection,
220 bool withContiguousMemory = false);
221
222 /*
223 * withBytes:
224 *
225 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
226 * The descriptor's length and capacity are set to the input buffer's size.
227 */
228 static IOBufferMemoryDescriptor * withBytes(
229 const void * bytes,
230 vm_size_t withLength,
231 IODirection withDirection,
232 bool withContiguousMemory = false);
233
234 /*
235 * setLength:
236 *
237 * Change the buffer length of the memory descriptor. When a new buffer
238 * is created, the initial length of the buffer is set to be the same as
239 * the capacity. The length can be adjusted via setLength for a shorter
240 * transfer (there is no need to create more buffer descriptors when you
241 * can reuse an existing one, even for different transfer sizes). Note
242 * that the specified length must not exceed the capacity of the buffer.
243 */
244 virtual void setLength(vm_size_t length);
245
246 /*
247 * setDirection:
248 *
249 * Change the direction of the transfer. This method allows one to redirect
250 * the descriptor's transfer direction. This eliminates the need to destroy
251 * and create new buffers when different transfer directions are needed.
252 */
253 virtual void setDirection(IODirection direction);
254
255 /*
256 * getCapacity:
257 *
258 * Get the buffer capacity
259 */
260 virtual vm_size_t getCapacity() const;
261
262 /*
263 * getBytesNoCopy:
264 *
265 * Return the virtual address of the beginning of the buffer
266 */
267 virtual void *getBytesNoCopy();
268
269 /*
270 * getBytesNoCopy:
271 *
272 * Return the virtual address of an offset from the beginning of the buffer
273 */
274 virtual void *getBytesNoCopy(vm_size_t start, vm_size_t withLength);
275
276 /*
277 * appendBytes:
278 *
279 * Add some data to the end of the buffer. This method automatically
280 * maintains the memory descriptor buffer length. Note that appendBytes
281 * will not copy past the end of the memory descriptor's current capacity.
282 */
283 virtual bool appendBytes(const void *bytes, vm_size_t withLength);
284
285 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
286 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
287 /* DEPRECATED */ IOByteCount * length);
288 #endif
289 };
290
291 #endif /* !_IOBUFFERMEMORYDESCRIPTOR_H */