iPXE
iobuf.c
Go to the documentation of this file.
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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdint.h>
28 #include <strings.h>
29 #include <errno.h>
30 #include <ipxe/malloc.h>
31 #include <ipxe/iobuf.h>
32 
33 /** @file
34  *
35  * I/O buffers
36  *
37  */
38 
39 /**
40  * Allocate I/O buffer with specified alignment and offset
41  *
42  * @v len Required length of buffer
43  * @v align Physical alignment
44  * @v offset Offset from physical alignment
45  * @ret iobuf I/O buffer, or NULL if none available
46  *
47  * @c align will be rounded up to the nearest power of two.
48  */
49 struct io_buffer * alloc_iob_raw ( size_t len, size_t align, size_t offset ) {
50  struct io_buffer *iobuf;
51  size_t headroom;
52  size_t tailroom;
53  size_t padding;
54  size_t threshold;
55  unsigned int align_log2;
56  void *data;
57 
58  /* Round up requested alignment and calculate initial headroom
59  * and tailroom to ensure that no cachelines are shared
60  * between I/O buffer data and other data structures.
61  */
62  if ( align < IOB_ZLEN )
63  align = IOB_ZLEN;
64  headroom = ( offset & ( IOB_ZLEN - 1 ) );
65  tailroom = ( ( - len - offset ) & ( IOB_ZLEN - 1 ) );
66  padding = ( headroom + tailroom );
67  offset -= headroom;
68  len += padding;
69  if ( len < padding )
70  return NULL;
71 
72  /* Round up alignment to the nearest power of two, avoiding
73  * a potentially undefined shift operation.
74  */
75  align_log2 = fls ( align - 1 );
76  if ( align_log2 >= ( 8 * sizeof ( align ) ) )
77  return NULL;
78  align = ( 1UL << align_log2 );
79 
80  /* Calculate length threshold */
81  assert ( align >= sizeof ( *iobuf ) );
82  threshold = ( align - sizeof ( *iobuf ) );
83 
84  /* Allocate buffer plus an inline descriptor as a single unit,
85  * unless doing so would push the total size over the
86  * alignment boundary.
87  */
88  if ( len <= threshold ) {
89 
90  /* Allocate memory for buffer plus descriptor */
91  data = malloc_phys_offset ( len + sizeof ( *iobuf ), align,
92  offset );
93  if ( ! data )
94  return NULL;
95  iobuf = ( data + len );
96 
97  } else {
98 
99  /* Allocate memory for buffer */
100  data = malloc_phys_offset ( len, align, offset );
101  if ( ! data )
102  return NULL;
103 
104  /* Allocate memory for descriptor */
105  iobuf = malloc ( sizeof ( *iobuf ) );
106  if ( ! iobuf ) {
107  free_phys ( data, len );
108  return NULL;
109  }
110  }
111 
112  /* Populate descriptor */
113  memset ( &iobuf->map, 0, sizeof ( iobuf->map ) );
114  iobuf->head = data;
115  iobuf->data = iobuf->tail = ( data + headroom );
116  iobuf->end = ( data + len );
117 
118  return iobuf;
119 }
120 
121 /**
122  * Allocate I/O buffer
123  *
124  * @v len Required length of buffer
125  * @ret iobuf I/O buffer, or NULL if none available
126  *
127  * The I/O buffer will be physically aligned on its own size (rounded
128  * up to the nearest power of two), up to a maximum of page-size
129  * alignment.
130  */
131 struct io_buffer * alloc_iob ( size_t len ) {
132  size_t align;
133 
134  /* Pad to minimum length */
135  if ( len < IOB_ZLEN )
136  len = IOB_ZLEN;
137 
138  /* Align buffer on its own size (up to page size) to avoid
139  * potential problems with boundary-crossing DMA.
140  */
141  align = len;
142  if ( align > PAGE_SIZE )
143  align = PAGE_SIZE;
144 
145  return alloc_iob_raw ( len, align, 0 );
146 }
147 
148 /**
149  * Free I/O buffer
150  *
151  * @v iobuf I/O buffer
152  */
153 void free_iob ( struct io_buffer *iobuf ) {
154  size_t len;
155 
156  /* Allow free_iob(NULL) to be valid */
157  if ( ! iobuf )
158  return;
159 
160  /* Sanity checks */
161  assert ( iobuf->head <= iobuf->data );
162  assert ( iobuf->data <= iobuf->tail );
163  assert ( iobuf->tail <= iobuf->end );
164  assert ( ! dma_mapped ( &iobuf->map ) );
165 
166  /* Free buffer */
167  len = ( iobuf->end - iobuf->head );
168  if ( iobuf->end == iobuf ) {
169 
170  /* Descriptor is inline */
171  free_phys ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
172 
173  } else {
174 
175  /* Descriptor is detached */
176  free_phys ( iobuf->head, len );
177  free ( iobuf );
178  }
179 }
180 
181 /**
182  * Allocate and map I/O buffer for receive DMA
183  *
184  * @v len Length of I/O buffer
185  * @v dma DMA device
186  * @ret iobuf I/O buffer, or NULL on error
187  */
188 struct io_buffer * alloc_rx_iob ( size_t len, struct dma_device *dma ) {
189  struct io_buffer *iobuf;
190  int rc;
191 
192  /* Allocate I/O buffer */
193  iobuf = alloc_iob ( len );
194  if ( ! iobuf )
195  goto err_alloc;
196 
197  /* Map I/O buffer */
198  if ( ( rc = iob_map_rx ( iobuf, dma ) ) != 0 )
199  goto err_map;
200 
201  return iobuf;
202 
203  iob_unmap ( iobuf );
204  err_map:
205  free_iob ( iobuf );
206  err_alloc:
207  return NULL;
208 }
209 
210 /**
211  * Unmap and free I/O buffer for receive DMA
212  *
213  * @v iobuf I/O buffer
214  */
215 void free_rx_iob ( struct io_buffer *iobuf ) {
216 
217  /* Unmap I/O buffer */
218  iob_unmap ( iobuf );
219 
220  /* Free I/O buffer */
221  free_iob ( iobuf );
222 }
223 
224 /**
225  * Ensure I/O buffer has sufficient headroom
226  *
227  * @v iobuf I/O buffer
228  * @v len Required headroom
229  *
230  * This function currently only checks for the required headroom; it
231  * does not reallocate the I/O buffer if required. If we ever have a
232  * code path that requires this functionality, it's a fairly trivial
233  * change to make.
234  */
235 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
236 
237  if ( iob_headroom ( iobuf ) >= len )
238  return 0;
239  return -ENOBUFS;
240 }
241 
242 /**
243  * Concatenate I/O buffers into a single buffer
244  *
245  * @v list List of I/O buffers
246  * @ret iobuf Concatenated I/O buffer, or NULL on allocation failure
247  *
248  * After a successful concatenation, the list will be empty.
249  */
250 struct io_buffer * iob_concatenate ( struct list_head *list ) {
251  struct io_buffer *iobuf;
252  struct io_buffer *tmp;
253  struct io_buffer *concatenated;
254  size_t len = 0;
255 
256  /* If the list contains only a single entry, avoid an
257  * unnecessary additional allocation.
258  */
259  if ( list_is_singular ( list ) ) {
260  iobuf = list_first_entry ( list, struct io_buffer, list );
261  INIT_LIST_HEAD ( list );
262  return iobuf;
263  }
264 
265  /* Calculate total length */
266  list_for_each_entry ( iobuf, list, list )
267  len += iob_len ( iobuf );
268 
269  /* Allocate new I/O buffer */
270  concatenated = alloc_iob_raw ( len, IOB_ZLEN, 0 );
271  if ( ! concatenated )
272  return NULL;
273 
274  /* Move data to new I/O buffer */
275  list_for_each_entry_safe ( iobuf, tmp, list, list ) {
276  list_del ( &iobuf->list );
277  memcpy ( iob_put ( concatenated, iob_len ( iobuf ) ),
278  iobuf->data, iob_len ( iobuf ) );
279  free_iob ( iobuf );
280  }
281 
282  return concatenated;
283 }
284 
285 /**
286  * Split I/O buffer
287  *
288  * @v iobuf I/O buffer
289  * @v len Length to split into a new I/O buffer
290  * @ret split New I/O buffer, or NULL on allocation failure
291  *
292  * Split the first @c len bytes of the existing I/O buffer into a
293  * separate I/O buffer. The resulting buffers are likely to have no
294  * headroom or tailroom.
295  *
296  * If this call fails, then the original buffer will be unmodified.
297  */
298 struct io_buffer * iob_split ( struct io_buffer *iobuf, size_t len ) {
299  struct io_buffer *split;
300 
301  /* Sanity checks */
302  assert ( len <= iob_len ( iobuf ) );
303 
304  /* Allocate new I/O buffer */
305  split = alloc_iob ( len );
306  if ( ! split )
307  return NULL;
308 
309  /* Copy in data */
310  memcpy ( iob_put ( split, len ), iobuf->data, len );
311  iob_pull ( iobuf, len );
312  return split;
313 }
#define iob_pull(iobuf, len)
Definition: iobuf.h:107
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define iob_put(iobuf, len)
Definition: iobuf.h:125
struct dma_mapping map
DMA mapping.
Definition: iobuf.h:48
FILE_SECBOOT(PERMITTED)
Error codes.
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:153
struct io_buffer * alloc_iob_raw(size_t len, size_t align, size_t offset)
Allocate I/O buffer with specified alignment and offset.
Definition: iobuf.c:49
#define PAGE_SIZE
Page size.
Definition: io.h:28
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:131
A doubly-linked list entry (or list head)
Definition: list.h:19
Dynamic memory allocation.
unsigned long tmp
Definition: linux_pci.h:65
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:334
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
void * tail
End of data.
Definition: iobuf.h:55
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
static __always_inline void iob_unmap(struct io_buffer *iobuf)
Unmap I/O buffer for DMA.
Definition: iobuf.h:279
ring len
Length.
Definition: dwmac.h:231
void * malloc_phys_offset(size_t size, size_t phys_align, size_t offset)
Allocate memory with specified physical alignment and offset.
Definition: malloc.c:685
static __always_inline int dma_mapped(struct dma_mapping *map)
Check if DMA unmapping is required.
Definition: dma.h:442
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:459
struct io_buffer * iob_concatenate(struct list_head *list)
Concatenate I/O buffers into a single buffer.
Definition: iobuf.c:250
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
struct io_buffer * alloc_rx_iob(size_t len, struct dma_device *dma)
Allocate and map I/O buffer for receive DMA.
Definition: iobuf.c:188
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:160
void * end
End of the buffer.
Definition: iobuf.h:57
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:621
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:46
static size_t iob_headroom(struct io_buffer *iobuf)
Calculate available space at start of an I/O buffer.
Definition: iobuf.h:170
#define list_is_singular(list)
Test whether a list has just one entry.
Definition: list.h:150
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:45
static __always_inline int iob_map_rx(struct io_buffer *iobuf, struct dma_device *dma)
Map empty I/O buffer for receive DMA.
Definition: iobuf.h:256
void free_rx_iob(struct io_buffer *iobuf)
Unmap and free I/O buffer for receive DMA.
Definition: iobuf.c:215
#define ENOBUFS
No buffer space available.
Definition: errno.h:499
void * head
Start of the buffer.
Definition: iobuf.h:51
struct io_buffer * iob_split(struct io_buffer *iobuf, size_t len)
Split I/O buffer.
Definition: iobuf.c:298
#define IOB_ZLEN
Minimum I/O buffer length and alignment.
Definition: iobuf.h:29
void * data
Start of data.
Definition: iobuf.h:53
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.c:723
uint8_t data[48]
Additional event data.
Definition: ena.h:22
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define fls(x)
Find last (i.e.
Definition: strings.h:167
int iob_ensure_headroom(struct io_buffer *iobuf, size_t len)
Ensure I/O buffer has sufficient headroom.
Definition: iobuf.c:235
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
String functions.
A DMA-capable device.
Definition: dma.h:48
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:38