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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/malloc.h>
26 #include <ipxe/iobuf.h>
35 * Allocate I/O buffer with specified alignment and offset
37 * @v len Required length of buffer
38 * @v align Physical alignment
39 * @v offset Offset from physical alignment
40 * @ret iobuf I/O buffer, or NULL if none available
42 * @c align will be rounded up to the nearest power of two.
44 struct io_buffer * alloc_iob_raw ( size_t len, size_t align, size_t offset ) {
45 struct io_buffer *iobuf;
48 /* Align buffer length to ensure that struct io_buffer is aligned */
49 len = ( len + __alignof__ ( *iobuf ) - 1 ) &
50 ~( __alignof__ ( *iobuf ) - 1 );
52 /* Round up alignment to the nearest power of two */
53 align = ( 1 << fls ( align - 1 ) );
55 /* Allocate buffer plus descriptor as a single unit, unless
56 * doing so will push the total size over the alignment
59 if ( ( len + sizeof ( *iobuf ) ) <= align ) {
61 /* Allocate memory for buffer plus descriptor */
62 data = malloc_dma_offset ( len + sizeof ( *iobuf ), align,
66 iobuf = ( data + len );
70 /* Allocate memory for buffer */
71 data = malloc_dma_offset ( len, align, offset );
75 /* Allocate memory for descriptor */
76 iobuf = malloc ( sizeof ( *iobuf ) );
78 free_dma ( data, len );
83 /* Populate descriptor */
84 iobuf->head = iobuf->data = iobuf->tail = data;
85 iobuf->end = ( data + len );
93 * @v len Required length of buffer
94 * @ret iobuf I/O buffer, or NULL if none available
96 * The I/O buffer will be physically aligned on its own size (rounded
97 * up to the nearest power of two).
99 struct io_buffer * alloc_iob ( size_t len ) {
101 /* Pad to minimum length */
102 if ( len < IOB_ZLEN )
105 /* Align buffer on its own size to avoid potential problems
106 * with boundary-crossing DMA.
108 return alloc_iob_raw ( len, len, 0 );
114 * @v iobuf I/O buffer
116 void free_iob ( struct io_buffer *iobuf ) {
119 /* Allow free_iob(NULL) to be valid */
124 assert ( iobuf->head <= iobuf->data );
125 assert ( iobuf->data <= iobuf->tail );
126 assert ( iobuf->tail <= iobuf->end );
129 len = ( iobuf->end - iobuf->head );
130 if ( iobuf->end == iobuf ) {
132 /* Descriptor is inline */
133 free_dma ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
137 /* Descriptor is detached */
138 free_dma ( iobuf->head, len );
144 * Ensure I/O buffer has sufficient headroom
146 * @v iobuf I/O buffer
147 * @v len Required headroom
149 * This function currently only checks for the required headroom; it
150 * does not reallocate the I/O buffer if required. If we ever have a
151 * code path that requires this functionality, it's a fairly trivial
154 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
156 if ( iob_headroom ( iobuf ) >= len )
162 * Concatenate I/O buffers into a single buffer
164 * @v list List of I/O buffers
165 * @ret iobuf Concatenated I/O buffer, or NULL on allocation failure
167 * After a successful concatenation, the list will be empty.
169 struct io_buffer * iob_concatenate ( struct list_head *list ) {
170 struct io_buffer *iobuf;
171 struct io_buffer *tmp;
172 struct io_buffer *concatenated;
175 /* If the list contains only a single entry, avoid an
176 * unnecessary additional allocation.
178 if ( list_is_singular ( list ) ) {
179 iobuf = list_first_entry ( list, struct io_buffer, list );
180 INIT_LIST_HEAD ( list );
184 /* Calculate total length */
185 list_for_each_entry ( iobuf, list, list )
186 len += iob_len ( iobuf );
188 /* Allocate new I/O buffer */
189 concatenated = alloc_iob_raw ( len, __alignof__ ( *iobuf ), 0 );
190 if ( ! concatenated )
193 /* Move data to new I/O buffer */
194 list_for_each_entry_safe ( iobuf, tmp, list, list ) {
195 list_del ( &iobuf->list );
196 memcpy ( iob_put ( concatenated, iob_len ( iobuf ) ),
197 iobuf->data, iob_len ( iobuf ) );