These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / iobuf.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <strings.h>
28 #include <errno.h>
29 #include <ipxe/malloc.h>
30 #include <ipxe/iobuf.h>
31
32 /** @file
33  *
34  * I/O buffers
35  *
36  */
37
38 /**
39  * Allocate I/O buffer with specified alignment and offset
40  *
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
45  *
46  * @c align will be rounded up to the nearest power of two.
47  */
48 struct io_buffer * alloc_iob_raw ( size_t len, size_t align, size_t offset ) {
49         struct io_buffer *iobuf;
50         void *data;
51
52         /* Align buffer length to ensure that struct io_buffer is aligned */
53         len = ( len + __alignof__ ( *iobuf ) - 1 ) &
54                 ~( __alignof__ ( *iobuf ) - 1 );
55
56         /* Round up alignment to the nearest power of two */
57         align = ( 1 << fls ( align - 1 ) );
58
59         /* Allocate buffer plus descriptor as a single unit, unless
60          * doing so will push the total size over the alignment
61          * boundary.
62          */
63         if ( ( len + sizeof ( *iobuf ) ) <= align ) {
64
65                 /* Allocate memory for buffer plus descriptor */
66                 data = malloc_dma_offset ( len + sizeof ( *iobuf ), align,
67                                            offset );
68                 if ( ! data )
69                         return NULL;
70                 iobuf = ( data + len );
71
72         } else {
73
74                 /* Allocate memory for buffer */
75                 data = malloc_dma_offset ( len, align, offset );
76                 if ( ! data )
77                         return NULL;
78
79                 /* Allocate memory for descriptor */
80                 iobuf = malloc ( sizeof ( *iobuf ) );
81                 if ( ! iobuf ) {
82                         free_dma ( data, len );
83                         return NULL;
84                 }
85         }
86
87         /* Populate descriptor */
88         iobuf->head = iobuf->data = iobuf->tail = data;
89         iobuf->end = ( data + len );
90
91         return iobuf;
92 }
93
94 /**
95  * Allocate I/O buffer
96  *
97  * @v len       Required length of buffer
98  * @ret iobuf   I/O buffer, or NULL if none available
99  *
100  * The I/O buffer will be physically aligned on its own size (rounded
101  * up to the nearest power of two).
102  */
103 struct io_buffer * alloc_iob ( size_t len ) {
104
105         /* Pad to minimum length */
106         if ( len < IOB_ZLEN )
107                 len = IOB_ZLEN;
108
109         /* Align buffer on its own size to avoid potential problems
110          * with boundary-crossing DMA.
111          */
112         return alloc_iob_raw ( len, len, 0 );
113 }
114
115 /**
116  * Free I/O buffer
117  *
118  * @v iobuf     I/O buffer
119  */
120 void free_iob ( struct io_buffer *iobuf ) {
121         size_t len;
122
123         /* Allow free_iob(NULL) to be valid */
124         if ( ! iobuf )
125                 return;
126
127         /* Sanity checks */
128         assert ( iobuf->head <= iobuf->data );
129         assert ( iobuf->data <= iobuf->tail );
130         assert ( iobuf->tail <= iobuf->end );
131
132         /* Free buffer */
133         len = ( iobuf->end - iobuf->head );
134         if ( iobuf->end == iobuf ) {
135
136                 /* Descriptor is inline */
137                 free_dma ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
138
139         } else {
140
141                 /* Descriptor is detached */
142                 free_dma ( iobuf->head, len );
143                 free ( iobuf );
144         }
145 }
146
147 /**
148  * Ensure I/O buffer has sufficient headroom
149  *
150  * @v iobuf     I/O buffer
151  * @v len       Required headroom
152  *
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
156  * change to make.
157  */
158 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
159
160         if ( iob_headroom ( iobuf ) >= len )
161                 return 0;
162         return -ENOBUFS;
163 }
164
165 /**
166  * Concatenate I/O buffers into a single buffer
167  *
168  * @v list      List of I/O buffers
169  * @ret iobuf   Concatenated I/O buffer, or NULL on allocation failure
170  *
171  * After a successful concatenation, the list will be empty.
172  */
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;
177         size_t len = 0;
178
179         /* If the list contains only a single entry, avoid an
180          * unnecessary additional allocation.
181          */
182         if ( list_is_singular ( list ) ) {
183                 iobuf = list_first_entry ( list, struct io_buffer, list );
184                 INIT_LIST_HEAD ( list );
185                 return iobuf;
186         }
187
188         /* Calculate total length */
189         list_for_each_entry ( iobuf, list, list )
190                 len += iob_len ( iobuf );
191
192         /* Allocate new I/O buffer */
193         concatenated = alloc_iob_raw ( len, __alignof__ ( *iobuf ), 0 );
194         if ( ! concatenated )
195                 return NULL;
196
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 ) );
202                 free_iob ( iobuf );
203         }
204
205         return concatenated;
206 }
207
208 /**
209  * Split I/O buffer
210  *
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
214  *
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.
218  *
219  * If this call fails, then the original buffer will be unmodified.
220  */
221 struct io_buffer * iob_split ( struct io_buffer *iobuf, size_t len ) {
222         struct io_buffer *split;
223
224         /* Sanity checks */
225         assert ( len <= iob_len ( iobuf ) );
226
227         /* Allocate new I/O buffer */
228         split = alloc_iob ( len );
229         if ( ! split )
230                 return NULL;
231
232         /* Copy in data */
233         memcpy ( iob_put ( split, len ), iobuf->data, len );
234         iob_pull ( iobuf, len );
235         return split;
236 }