2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/malloc.h>
30 #include <ipxe/iobuf.h>
39 * Allocate I/O buffer with specified alignment and offset
41 * @v len Required length of buffer
42 * @v align Physical alignment
43 * @v offset Offset from physical alignment
44 * @ret iobuf I/O buffer, or NULL if none available
46 * @c align will be rounded up to the nearest power of two.
48 struct io_buffer * alloc_iob_raw ( size_t len, size_t align, size_t offset ) {
49 struct io_buffer *iobuf;
52 /* Align buffer length to ensure that struct io_buffer is aligned */
53 len = ( len + __alignof__ ( *iobuf ) - 1 ) &
54 ~( __alignof__ ( *iobuf ) - 1 );
56 /* Round up alignment to the nearest power of two */
57 align = ( 1 << fls ( align - 1 ) );
59 /* Allocate buffer plus descriptor as a single unit, unless
60 * doing so will push the total size over the alignment
63 if ( ( len + sizeof ( *iobuf ) ) <= align ) {
65 /* Allocate memory for buffer plus descriptor */
66 data = malloc_dma_offset ( len + sizeof ( *iobuf ), align,
70 iobuf = ( data + len );
74 /* Allocate memory for buffer */
75 data = malloc_dma_offset ( len, align, offset );
79 /* Allocate memory for descriptor */
80 iobuf = malloc ( sizeof ( *iobuf ) );
82 free_dma ( data, len );
87 /* Populate descriptor */
88 iobuf->head = iobuf->data = iobuf->tail = data;
89 iobuf->end = ( data + len );
97 * @v len Required length of buffer
98 * @ret iobuf I/O buffer, or NULL if none available
100 * The I/O buffer will be physically aligned on its own size (rounded
101 * up to the nearest power of two).
103 struct io_buffer * alloc_iob ( size_t len ) {
105 /* Pad to minimum length */
106 if ( len < IOB_ZLEN )
109 /* Align buffer on its own size to avoid potential problems
110 * with boundary-crossing DMA.
112 return alloc_iob_raw ( len, len, 0 );
118 * @v iobuf I/O buffer
120 void free_iob ( struct io_buffer *iobuf ) {
123 /* Allow free_iob(NULL) to be valid */
128 assert ( iobuf->head <= iobuf->data );
129 assert ( iobuf->data <= iobuf->tail );
130 assert ( iobuf->tail <= iobuf->end );
133 len = ( iobuf->end - iobuf->head );
134 if ( iobuf->end == iobuf ) {
136 /* Descriptor is inline */
137 free_dma ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
141 /* Descriptor is detached */
142 free_dma ( iobuf->head, len );
148 * Ensure I/O buffer has sufficient headroom
150 * @v iobuf I/O buffer
151 * @v len Required headroom
153 * This function currently only checks for the required headroom; it
154 * does not reallocate the I/O buffer if required. If we ever have a
155 * code path that requires this functionality, it's a fairly trivial
158 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
160 if ( iob_headroom ( iobuf ) >= len )
166 * Concatenate I/O buffers into a single buffer
168 * @v list List of I/O buffers
169 * @ret iobuf Concatenated I/O buffer, or NULL on allocation failure
171 * After a successful concatenation, the list will be empty.
173 struct io_buffer * iob_concatenate ( struct list_head *list ) {
174 struct io_buffer *iobuf;
175 struct io_buffer *tmp;
176 struct io_buffer *concatenated;
179 /* If the list contains only a single entry, avoid an
180 * unnecessary additional allocation.
182 if ( list_is_singular ( list ) ) {
183 iobuf = list_first_entry ( list, struct io_buffer, list );
184 INIT_LIST_HEAD ( list );
188 /* Calculate total length */
189 list_for_each_entry ( iobuf, list, list )
190 len += iob_len ( iobuf );
192 /* Allocate new I/O buffer */
193 concatenated = alloc_iob_raw ( len, __alignof__ ( *iobuf ), 0 );
194 if ( ! concatenated )
197 /* Move data to new I/O buffer */
198 list_for_each_entry_safe ( iobuf, tmp, list, list ) {
199 list_del ( &iobuf->list );
200 memcpy ( iob_put ( concatenated, iob_len ( iobuf ) ),
201 iobuf->data, iob_len ( iobuf ) );
211 * @v iobuf I/O buffer
212 * @v len Length to split into a new I/O buffer
213 * @ret split New I/O buffer, or NULL on allocation failure
215 * Split the first @c len bytes of the existing I/O buffer into a
216 * separate I/O buffer. The resulting buffers are likely to have no
217 * headroom or tailroom.
219 * If this call fails, then the original buffer will be unmodified.
221 struct io_buffer * iob_split ( struct io_buffer *iobuf, size_t len ) {
222 struct io_buffer *split;
225 assert ( len <= iob_len ( iobuf ) );
227 /* Allocate new I/O buffer */
228 split = alloc_iob ( len );
233 memcpy ( iob_put ( split, len ), iobuf->data, len );
234 iob_pull ( iobuf, len );