summaryrefslogtreecommitdiffstats
path: root/src/include/ipxe/dma.h
blob: 385e4baf761df94aefa848558e3f4228cc894379 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#ifndef _IPXE_DMA_H
#define _IPXE_DMA_H

/** @file
 *
 * DMA mappings
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <ipxe/api.h>
#include <ipxe/io.h>
#include <ipxe/malloc.h>
#include <ipxe/umalloc.h>
#include <config/ioapi.h>

#ifdef DMAAPI_OP
#define DMAAPI_PREFIX_op
#else
#define DMAAPI_PREFIX_op __op_
#endif

#ifdef DMAAPI_FLAT
#define DMAAPI_PREFIX_flat
#else
#define DMAAPI_PREFIX_flat __flat_
#endif

/** A DMA mapping */
struct dma_mapping {
	/** Address offset
	 *
	 * This is the value that must be added to a physical address
	 * within the mapping in order to produce the corresponding
	 * device-side DMA address.
	 */
	physaddr_t offset;
	/** DMA device (if unmapping is required) */
	struct dma_device *dma;
	/** Platform mapping token */
	void *token;
};

/** A DMA-capable device */
struct dma_device {
	/** DMA operations */
	struct dma_operations *op;
	/** Addressable space mask */
	physaddr_t mask;
	/** Total number of mappings (for debugging) */
	unsigned int mapped;
	/** Total number of allocations (for debugging) */
	unsigned int allocated;
};

/** DMA operations */
struct dma_operations {
	/**
	 * Map buffer for DMA
	 *
	 * @v dma		DMA device
	 * @v map		DMA mapping to fill in
	 * @v addr		Buffer address
	 * @v len		Length of buffer
	 * @v flags		Mapping flags
	 * @ret rc		Return status code
	 */
	int ( * map ) ( struct dma_device *dma, struct dma_mapping *map,
			physaddr_t addr, size_t len, int flags );
	/**
	 * Unmap buffer
	 *
	 * @v dma		DMA device
	 * @v map		DMA mapping
	 */
	void ( * unmap ) ( struct dma_device *dma, struct dma_mapping *map );
	/**
	 * Allocate and map DMA-coherent buffer
	 *
	 * @v dma		DMA device
	 * @v map		DMA mapping to fill in
	 * @v len		Length of buffer
	 * @v align		Physical alignment
	 * @ret addr		Buffer address, or NULL on error
	 */
	void * ( * alloc ) ( struct dma_device *dma, struct dma_mapping *map,
			     size_t len, size_t align );
	/**
	 * Unmap and free DMA-coherent buffer
	 *
	 * @v dma		DMA device
	 * @v map		DMA mapping
	 * @v addr		Buffer address
	 * @v len		Length of buffer
	 */
	void ( * free ) ( struct dma_device *dma, struct dma_mapping *map,
			  void *addr, size_t len );
	/**
	 * Allocate and map DMA-coherent buffer from external (user) memory
	 *
	 * @v dma		DMA device
	 * @v map		DMA mapping to fill in
	 * @v len		Length of buffer
	 * @v align		Physical alignment
	 * @ret addr		Buffer address, or NULL on error
	 */
	userptr_t ( * umalloc ) ( struct dma_device *dma,
				  struct dma_mapping *map,
				  size_t len, size_t align );
	/**
	 * Unmap and free DMA-coherent buffer from external (user) memory
	 *
	 * @v dma		DMA device
	 * @v map		DMA mapping
	 * @v addr		Buffer address
	 * @v len		Length of buffer
	 */
	void ( * ufree ) ( struct dma_device *dma, struct dma_mapping *map,
			   userptr_t addr, size_t len );
	/**
	 * Set addressable space mask
	 *
	 * @v dma		DMA device
	 * @v mask		Addressable space mask
	 */
	void ( * set_mask ) ( struct dma_device *dma, physaddr_t mask );
};

/** Device will read data from host memory */
#define DMA_TX 0x01

/** Device will write data to host memory */
#define DMA_RX 0x02

/** Device will both read data from and write data to host memory */
#define DMA_BI ( DMA_TX | DMA_RX )

/**
 * Calculate static inline DMA I/O API function name
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 * @ret _subsys_func	Subsystem API function
 */
#define DMAAPI_INLINE( _subsys, _api_func ) \
	SINGLE_API_INLINE ( DMAAPI_PREFIX_ ## _subsys, _api_func )

/**
 * Provide a DMA I/O API implementation
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 * @v _func		Implementing function
 */
#define PROVIDE_DMAAPI( _subsys, _api_func, _func ) \
	PROVIDE_SINGLE_API ( DMAAPI_PREFIX_ ## _subsys, _api_func, _func )

/**
 * Provide a static inline DMA I/O API implementation
 *
 * @v _prefix		Subsystem prefix
 * @v _api_func		API function
 */
#define PROVIDE_DMAAPI_INLINE( _subsys, _api_func ) \
	PROVIDE_SINGLE_API_INLINE ( DMAAPI_PREFIX_ ## _subsys, _api_func )

/**
 * Map buffer for DMA
 *
 * @v dma		DMA device
 * @v map		DMA mapping to fill in
 * @v addr		Buffer address
 * @v len		Length of buffer
 * @v flags		Mapping flags
 * @ret rc		Return status code
 */
static inline __always_inline int
DMAAPI_INLINE ( flat, dma_map ) ( struct dma_device *dma,
				  struct dma_mapping *map,
				  physaddr_t addr __unused,
				  size_t len __unused, int flags __unused ) {

	/* Increment mapping count (for debugging) */
	if ( DBG_LOG ) {
		map->dma = dma;
		dma->mapped++;
	}

	return 0;
}

/**
 * Unmap buffer
 *
 * @v map		DMA mapping
 */
static inline __always_inline void
DMAAPI_INLINE ( flat, dma_unmap ) ( struct dma_mapping *map ) {

	/* Decrement mapping count (for debugging) */
	if ( DBG_LOG ) {
		assert ( map->dma != NULL );
		map->dma->mapped--;
		map->dma = NULL;
	}
}

/**
 * Allocate and map DMA-coherent buffer
 *
 * @v dma		DMA device
 * @v map		DMA mapping to fill in
 * @v len		Length of buffer
 * @v align		Physical alignment
 * @ret addr		Buffer address, or NULL on error
 */
static inline __always_inline void *
DMAAPI_INLINE ( flat, dma_alloc ) ( struct dma_device *dma,
				    struct dma_mapping *map,
				    size_t len, size_t align ) {
	void *addr;

	/* Allocate buffer */
	addr = malloc_phys ( len, align );

	/* Increment mapping count (for debugging) */
	if ( DBG_LOG && addr ) {
		map->dma = dma;
		dma->mapped++;
	}

	return addr;
}

/**
 * Unmap and free DMA-coherent buffer
 *
 * @v map		DMA mapping
 * @v addr		Buffer address
 * @v len		Length of buffer
 */
static inline __always_inline void
DMAAPI_INLINE ( flat, dma_free ) ( struct dma_mapping *map,
				   void *addr, size_t len ) {

	/* Free buffer */
	free_phys ( addr, len );

	/* Decrement mapping count (for debugging) */
	if ( DBG_LOG ) {
		assert ( map->dma != NULL );
		map->dma->mapped--;
		map->dma = NULL;
	}
}

/**
 * Allocate and map DMA-coherent buffer from external (user) memory
 *
 * @v dma		DMA device
 * @v map		DMA mapping to fill in
 * @v len		Length of buffer
 * @v align		Physical alignment
 * @ret addr		Buffer address, or NULL on error
 */
static inline __always_inline userptr_t
DMAAPI_INLINE ( flat, dma_umalloc ) ( struct dma_device *dma,
				      struct dma_mapping *map,
				      size_t len, size_t align __unused ) {
	userptr_t addr;

	/* Allocate buffer */
	addr = umalloc ( len );

	/* Increment mapping count (for debugging) */
	if ( DBG_LOG && addr ) {
		map->dma = dma;
		dma->mapped++;
	}

	return addr;
}

/**
 * Unmap and free DMA-coherent buffer from external (user) memory
 *
 * @v map		DMA mapping
 * @v addr		Buffer address
 * @v len		Length of buffer
 */
static inline __always_inline void
DMAAPI_INLINE ( flat, dma_ufree ) ( struct dma_mapping *map,
				    userptr_t addr, size_t len __unused ) {

	/* Free buffer */
	ufree ( addr );

	/* Decrement mapping count (for debugging) */
	if ( DBG_LOG ) {
		assert ( map->dma != NULL );
		map->dma->mapped--;
		map->dma = NULL;
	}
}

/**
 * Set addressable space mask
 *
 * @v dma		DMA device
 * @v mask		Addressable space mask
 */
static inline __always_inline void
DMAAPI_INLINE ( flat, dma_set_mask ) ( struct dma_device *dma __unused,
				       physaddr_t mask __unused ) {

	/* Nothing to do */
}

/**
 * Get DMA address from physical address
 *
 * @v map		DMA mapping
 * @v addr		Physical address within the mapped region
 * @ret addr		Device-side DMA address
 */
static inline __always_inline physaddr_t
DMAAPI_INLINE ( flat, dma_phys ) ( struct dma_mapping *map __unused,
				   physaddr_t addr ) {

	/* Use physical address as device address */
	return addr;
}

/**
 * Get DMA address from physical address
 *
 * @v map		DMA mapping
 * @v addr		Physical address within the mapped region
 * @ret addr		Device-side DMA address
 */
static inline __always_inline physaddr_t
DMAAPI_INLINE ( op, dma_phys ) ( struct dma_mapping *map, physaddr_t addr ) {

	/* Adjust physical address using mapping offset */
	return ( addr + map->offset );
}

/**
 * Map buffer for DMA
 *
 * @v dma		DMA device
 * @v map		DMA mapping to fill in
 * @v addr		Buffer address
 * @v len		Length of buffer
 * @v flags		Mapping flags
 * @ret rc		Return status code
 */
int dma_map ( struct dma_device *dma, struct dma_mapping *map,
	      physaddr_t addr, size_t len, int flags );

/**
 * Unmap buffer
 *
 * @v map		DMA mapping
 */
void dma_unmap ( struct dma_mapping *map );

/**
 * Allocate and map DMA-coherent buffer
 *
 * @v dma		DMA device
 * @v map		DMA mapping to fill in
 * @v len		Length of buffer
 * @v align		Physical alignment
 * @ret addr		Buffer address, or NULL on error
 */
void * dma_alloc ( struct dma_device *dma, struct dma_mapping *map,
		   size_t len, size_t align );

/**
 * Unmap and free DMA-coherent buffer
 *
 * @v map		DMA mapping
 * @v addr		Buffer address
 * @v len		Length of buffer
 */
void dma_free ( struct dma_mapping *map, void *addr, size_t len );

/**
 * Allocate and map DMA-coherent buffer from external (user) memory
 *
 * @v dma		DMA device
 * @v map		DMA mapping to fill in
 * @v len		Length of buffer
 * @v align		Physical alignment
 * @ret addr		Buffer address, or NULL on error
 */
userptr_t dma_umalloc ( struct dma_device *dma, struct dma_mapping *map,
			size_t len, size_t align );

/**
 * Unmap and free DMA-coherent buffer from external (user) memory
 *
 * @v map		DMA mapping
 * @v addr		Buffer address
 * @v len		Length of buffer
 */
void dma_ufree ( struct dma_mapping *map, userptr_t addr, size_t len );

/**
 * Set addressable space mask
 *
 * @v dma		DMA device
 * @v mask		Addressable space mask
 */
void dma_set_mask ( struct dma_device *dma, physaddr_t mask );

/**
 * Get DMA address from physical address
 *
 * @v map		DMA mapping
 * @v addr		Physical address within the mapped region
 * @ret addr		Device-side DMA address
 */
physaddr_t dma_phys ( struct dma_mapping *map, physaddr_t addr );

/**
 * Get DMA address from virtual address
 *
 * @v map		DMA mapping
 * @v addr		Virtual address within the mapped region
 * @ret addr		Device-side DMA address
 */
static inline __always_inline physaddr_t dma ( struct dma_mapping *map,
					       void *addr ) {

	/* Get DMA address from corresponding physical address */
	return dma_phys ( map, virt_to_phys ( addr ) );
}

/**
 * Check if DMA unmapping is required
 *
 * @v map		DMA mapping
 * @v unmap		Unmapping is required
 */
static inline __always_inline int dma_mapped ( struct dma_mapping *map ) {

	/* Unmapping is required if a DMA device was recorded */
	return ( map->dma != NULL );
}

/**
 * Initialise DMA device
 *
 * @v dma		DMA device
 * @v op		DMA operations
 */
static inline __always_inline void dma_init ( struct dma_device *dma,
					      struct dma_operations *op ) {

	/* Set operations table */
	dma->op = op;
}

/**
 * Set 64-bit addressable space mask
 *
 * @v dma		DMA device
 */
static inline __always_inline void
dma_set_mask_64bit ( struct dma_device *dma ) {

	/* Set mask to maximum physical address */
	dma_set_mask ( dma, ~( ( physaddr_t ) 0 ) );
}

#endif /* _IPXE_DMA_H */