Browse Source

[iobuf] Allow allocation of I/O buffers with a specified alignment offset

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
96be171be5
2 changed files with 32 additions and 15 deletions
  1. 30
    15
      src/core/iobuf.c
  2. 2
    0
      src/include/ipxe/iobuf.h

+ 30
- 15
src/core/iobuf.c View File

32
  */
32
  */
33
 
33
 
34
 /**
34
 /**
35
- * Allocate I/O buffer
35
+ * Allocate I/O buffer with specified alignment and offset
36
  *
36
  *
37
  * @v len	Required length of buffer
37
  * @v len	Required length of buffer
38
+ * @v align	Physical alignment
39
+ * @v offset	Offset from physical alignment
38
  * @ret iobuf	I/O buffer, or NULL if none available
40
  * @ret iobuf	I/O buffer, or NULL if none available
39
  *
41
  *
40
- * The I/O buffer will be physically aligned to a multiple of
41
- * @c IOBUF_SIZE.
42
+ * @c align will be rounded up to the nearest power of two.
42
  */
43
  */
43
-struct io_buffer * alloc_iob ( size_t len ) {
44
+struct io_buffer * alloc_iob_raw ( size_t len, size_t align, size_t offset ) {
44
 	struct io_buffer *iobuf;
45
 	struct io_buffer *iobuf;
45
-	size_t align;
46
 	void *data;
46
 	void *data;
47
 
47
 
48
-	/* Pad to minimum length */
49
-	if ( len < IOB_ZLEN )
50
-		len = IOB_ZLEN;
51
-
52
 	/* Align buffer length to ensure that struct io_buffer is aligned */
48
 	/* Align buffer length to ensure that struct io_buffer is aligned */
53
 	len = ( len + __alignof__ ( *iobuf ) - 1 ) &
49
 	len = ( len + __alignof__ ( *iobuf ) - 1 ) &
54
 		~( __alignof__ ( *iobuf ) - 1 );
50
 		~( __alignof__ ( *iobuf ) - 1 );
55
 
51
 
56
-	/* Align buffer on its own size to avoid potential problems
57
-	 * with boundary-crossing DMA.
58
-	 */
59
-	align = ( 1 << fls ( len - 1 ) );
52
+	/* Round up alignment to the nearest power of two */
53
+	align = ( 1 << fls ( align - 1 ) );
60
 
54
 
61
 	/* Allocate buffer plus descriptor as a single unit, unless
55
 	/* Allocate buffer plus descriptor as a single unit, unless
62
 	 * doing so will push the total size over the alignment
56
 	 * doing so will push the total size over the alignment
65
 	if ( ( len + sizeof ( *iobuf ) ) <= align ) {
59
 	if ( ( len + sizeof ( *iobuf ) ) <= align ) {
66
 
60
 
67
 		/* Allocate memory for buffer plus descriptor */
61
 		/* Allocate memory for buffer plus descriptor */
68
-		data = malloc_dma ( len + sizeof ( *iobuf ), align );
62
+		data = malloc_dma_offset ( len + sizeof ( *iobuf ), align,
63
+					   offset );
69
 		if ( ! data )
64
 		if ( ! data )
70
 			return NULL;
65
 			return NULL;
71
 		iobuf = ( data + len );
66
 		iobuf = ( data + len );
73
 	} else {
68
 	} else {
74
 
69
 
75
 		/* Allocate memory for buffer */
70
 		/* Allocate memory for buffer */
76
-		data = malloc_dma ( len, align );
71
+		data = malloc_dma_offset ( len, align, offset );
77
 		if ( ! data )
72
 		if ( ! data )
78
 			return NULL;
73
 			return NULL;
79
 
74
 
92
 	return iobuf;
87
 	return iobuf;
93
 }
88
 }
94
 
89
 
90
+/**
91
+ * Allocate I/O buffer
92
+ *
93
+ * @v len	Required length of buffer
94
+ * @ret iobuf	I/O buffer, or NULL if none available
95
+ *
96
+ * The I/O buffer will be physically aligned on its own size (rounded
97
+ * up to the nearest power of two).
98
+ */
99
+struct io_buffer * alloc_iob ( size_t len ) {
100
+
101
+	/* Pad to minimum length */
102
+	if ( len < IOB_ZLEN )
103
+		len = IOB_ZLEN;
104
+
105
+	/* Align buffer on its own size to avoid potential problems
106
+	 * with boundary-crossing DMA.
107
+	 */
108
+	return alloc_iob_raw ( len, len, 0 );
109
+}
95
 
110
 
96
 /**
111
 /**
97
  * Free I/O buffer
112
  * Free I/O buffer

+ 2
- 0
src/include/ipxe/iobuf.h View File

210
 	(iobuf) = NULL;					\
210
 	(iobuf) = NULL;					\
211
 	__iobuf; } )
211
 	__iobuf; } )
212
 
212
 
213
+extern struct io_buffer * __malloc alloc_iob_raw ( size_t len, size_t align,
214
+						   size_t offset );
213
 extern struct io_buffer * __malloc alloc_iob ( size_t len );
215
 extern struct io_buffer * __malloc alloc_iob ( size_t len );
214
 extern void free_iob ( struct io_buffer *iobuf );
216
 extern void free_iob ( struct io_buffer *iobuf );
215
 extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );
217
 extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );

Loading…
Cancel
Save