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