aml.h 24.7 KB
Newer Older
1 2 3
#ifndef AML_H
#define AML_H 1

4 5 6 7 8 9 10
#include <inttypes.h>
#include <numa.h>
#include <numaif.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
11
#include <stdlib.h>
12 13 14 15 16 17 18
#include <sys/mman.h>
#include <unistd.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

19

20 21 22 23 24
/*******************************************************************************
 * Forward Declarations:
 ******************************************************************************/

struct aml_area;
25
struct aml_binding;
26 27 28

/*******************************************************************************
 * Arenas:
29 30
 * In-memory allocator implementation. Dispatches actual memory mappings back to
 * areas.
31 32
 ******************************************************************************/

33 34 35 36 37 38
#define AML_ARENA_FLAG_ZERO 1

/* opaque handle to configuration data */
struct aml_arena_data;

struct aml_arena_ops {
39 40 41 42 43 44 45
	int (*register_arena)(struct aml_arena_data *arena,
			      struct aml_area *area);
	int (*deregister_arena)(struct aml_arena_data *arena);
	void *(*mallocx)(struct aml_arena_data *arena, size_t size, int flags);
	void (*dallocx)(struct aml_arena_data *arena, void *ptr, int flags);
	void *(*reallocx)(struct aml_arena_data *arena, void *ptr, size_t size,
			  int flags);
46 47
};

48
struct aml_arena {
49 50
	struct aml_arena_ops *ops;
	struct aml_arena_data *data;
51 52
};

53 54 55 56 57 58
int aml_arena_register(struct aml_arena *arena, struct aml_area *area);
int aml_arena_deregister(struct aml_arena *arena);
void *aml_arena_mallocx(struct aml_arena *arena, size_t size, int flags);
void aml_arena_dallocx(struct aml_arena *arena, void *ptr, int flags);
void *aml_arena_reallocx(struct aml_arena *arena, void *ptr, size_t size,
			 int flags);
59 60 61 62 63 64 65 66 67 68

/*******************************************************************************
 * Jemalloc Arena:
 ******************************************************************************/
extern struct aml_arena_ops aml_arena_jemalloc_ops;

struct aml_arena_jemalloc_data {
	unsigned int uid;
	int flags;
};
69

70 71 72 73 74 75 76 77 78 79 80
#define AML_ARENA_JEMALLOC_DECL(name) \
	struct aml_arena_jemalloc_data __ ##name## _inner_data; \
	struct aml_arena name = { \
		&aml_arena_jemalloc_ops, \
		(struct aml_arena_data *)&__ ## name ## _inner_data, \
	};

#define AML_ARENA_JEMALLOC_ALLOCSIZE \
	(sizeof(struct aml_arena_jemalloc_data) + \
	 sizeof(struct aml_arena))

81 82 83 84 85

#define AML_ARENA_JEMALLOC_TYPE_REGULAR 0
#define AML_ARENA_JEMALLOC_TYPE_ALIGNED 1
#define AML_ARENA_JEMALLOC_TYPE_GENERIC 2

86 87 88 89
int aml_arena_jemalloc_create(struct aml_arena **arena, int type, ...);
int aml_arena_jemalloc_init(struct aml_arena *arena, int type, ...);
int aml_arena_jemalloc_vinit(struct aml_arena *arena, int type, va_list args);
int aml_arena_jemalloc_destroy(struct aml_arena *arena);
90

91 92 93 94 95
/*******************************************************************************
 * Areas:
 * embeds information about a byte-addressable physical memory location and well
 * as binding policies over it.
 ******************************************************************************/
96

97 98 99 100
/* opaque handle to configuration data */
struct aml_area_data;

struct aml_area_ops {
101 102 103 104 105 106 107
	void *(*malloc)(struct aml_area_data *area, size_t size);
	void (*free)(struct aml_area_data *area, void *ptr);
	void *(*calloc)(struct aml_area_data *area, size_t num, size_t size);
	void *(*realloc)(struct aml_area_data *area, void *ptr, size_t size);
	void *(*acquire)(struct aml_area_data *area, size_t size);
	void (*release)(struct aml_area_data *area, void *ptr);
	void *(*mmap)(struct aml_area_data *area, void *ptr, size_t size);
Kamil Iskra's avatar
Kamil Iskra committed
108 109
	int (*available)(const struct aml_area_data *area);
	int (*binding)(const struct aml_area_data *area,
110
		       struct aml_binding **binding);
111 112
};

113
struct aml_area {
114 115
	struct aml_area_ops *ops;
	struct aml_area_data *data;
116 117
};

118 119 120 121 122 123 124 125
/*******************************************************************************
 * POSIX Area:
 ******************************************************************************/

extern struct aml_area_ops aml_area_posix_ops;

struct aml_area_posix_data {
};
126

127 128 129 130 131 132 133 134 135 136 137
#define AML_AREA_POSIX_DECL(name) \
	struct aml_area_posix_data __ ##name## _inner_data; \
	struct aml_area name = { \
		&aml_area_posix_ops, \
		(struct aml_area_data *)&__ ## name ## _inner_data, \
	};

#define AML_AREA_POSIX_ALLOCSIZE \
	(sizeof(struct aml_area_posix_data) + \
	 sizeof(struct aml_area))

138 139 140 141
int aml_area_posix_create(struct aml_area **area);
int aml_area_posix_vinit(struct aml_area *area);
int aml_area_posix_init(struct aml_area *area);
int aml_area_posix_destroy(struct aml_area *area);
142 143

/*******************************************************************************
144 145 146 147 148 149 150 151 152 153 154
 * Linux Area:
 ******************************************************************************/

extern struct aml_area_ops aml_area_linux_ops;

struct aml_area_linux_manager_data {
	struct aml_arena *pool;
	size_t pool_size;
};

struct aml_area_linux_manager_ops {
Kamil Iskra's avatar
Kamil Iskra committed
155
	struct aml_arena *(*get_arena)(const struct aml_area_linux_manager_data *data);
156 157 158 159
};

extern struct aml_area_linux_manager_ops aml_area_linux_manager_single_ops;

160 161 162
int aml_area_linux_manager_single_init(struct aml_area_linux_manager_data *data,
				       struct aml_arena *arena);
int aml_area_linux_manager_single_destroy(struct aml_area_linux_manager_data *data);
163 164 165 166 167

#define AML_MAX_NUMA_NODES 128
#define AML_NODEMASK_BYTES (AML_MAX_NUMA_NODES/8)
#define AML_NODEMASK_SZ (AML_NODEMASK_BYTES/sizeof(unsigned long))

168 169 170 171 172
#define AML_NODEMASK_NBITS (8*sizeof(unsigned long))
#define AML_NODEMASK_ELT(i) ((i) / AML_NODEMASK_NBITS)
#define AML_NODEMASK_BITMASK(i) ((unsigned long)1 << ((i) % AML_NODEMASK_NBITS))
#define AML_NODEMASK_ISSET(mask, i) \
	((mask[AML_NODEMASK_ELT(i)] & AML_NODEMASK_BITMASK(i)) != 0)
173 174 175 176 177 178
#define AML_NODEMASK_SET(mask, i) (mask[AML_NODEMASK_ELT(i)] |= AML_NODEMASK_BITMASK(i))
#define AML_NODEMASK_ZERO(mask) \
	do {								\
		for(unsigned int __i = 0; __i < AML_NODEMASK_SZ; __i++)	\
			mask[__i] = 0;					\
	} while(0)
179 180


181 182 183 184 185 186
struct aml_area_linux_mbind_data {
	unsigned long nodemask[AML_NODEMASK_SZ];
	int policy;
};

struct aml_area_linux_mbind_ops {
187 188 189
	int (*pre_bind)(struct aml_area_linux_mbind_data *data);
	int (*post_bind)(struct aml_area_linux_mbind_data *data, void *ptr,
			 size_t size);
Kamil Iskra's avatar
Kamil Iskra committed
190
	int (*binding)(const struct aml_area_linux_mbind_data *data,
191 192 193 194
		       struct aml_binding **binding);
};

int aml_area_linux_mbind_setdata(struct aml_area_linux_mbind_data *data,
Kamil Iskra's avatar
Kamil Iskra committed
195 196
				 int policy, const unsigned long *nodemask);
int aml_area_linux_mbind_generic_binding(const struct aml_area_linux_mbind_data *data,
197 198 199 200 201 202 203 204
					 struct aml_binding **binding);
int aml_area_linux_mbind_regular_pre_bind(struct aml_area_linux_mbind_data *data);
int aml_area_linux_mbind_regular_post_bind(struct aml_area_linux_mbind_data *data,
					   void *ptr, size_t size);
int aml_area_linux_mbind_mempolicy_pre_bind(struct aml_area_linux_mbind_data *data);
int aml_area_linux_mbind_mempolicy_post_bind(struct aml_area_linux_mbind_data *data,
					   void *ptr, size_t size);
int aml_area_linux_mbind_init(struct aml_area_linux_mbind_data *data,
Kamil Iskra's avatar
Kamil Iskra committed
205
			      int policy, const unsigned long *nodemask);
206
int aml_area_linux_mbind_destroy(struct aml_area_linux_mbind_data *data);
207 208 209 210 211 212 213 214 215 216 217 218

extern struct aml_area_linux_mbind_ops aml_area_linux_mbind_regular_ops;
extern struct aml_area_linux_mbind_ops aml_area_linux_mbind_mempolicy_ops;

struct aml_area_linux_mmap_data {
	int prot;
	int flags;
	int fildes;
	off_t off;
};

struct aml_area_linux_mmap_ops {
219 220
	void *(*mmap)(struct aml_area_linux_mmap_data *data, void *ptr,
		      size_t size);
221 222
};

223 224 225 226
void *aml_area_linux_mmap_generic(struct aml_area_linux_mmap_data *data,
				  void *ptr, size_t size);
int aml_area_linux_mmap_anonymous_init(struct aml_area_linux_mmap_data *data);
int aml_area_linux_mmap_fd_init(struct aml_area_linux_mmap_data *data, int fd,
227
				off_t offset);
228 229 230 231 232
int aml_area_linux_mmap_tmpfile_init(struct aml_area_linux_mmap_data *data,
				     char *template, size_t max);
int aml_area_linux_mmap_anonymous_destroy(struct aml_area_linux_mmap_data *data);
int aml_area_linux_mmap_fd_destroy(struct aml_area_linux_mmap_data *data);
int aml_area_linux_mmap_tmpfile_destroy(struct aml_area_linux_mmap_data *data);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

extern struct aml_area_linux_mmap_ops aml_area_linux_mmap_generic_ops;

struct aml_area_linux_data {
	struct aml_area_linux_manager_data manager;
	struct aml_area_linux_mbind_data mbind;
	struct aml_area_linux_mmap_data mmap;
};

struct aml_area_linux_ops {
	struct aml_area_linux_manager_ops manager;
	struct aml_area_linux_mbind_ops mbind;
	struct aml_area_linux_mmap_ops mmap;
};

struct aml_area_linux {
	struct aml_area_linux_data data;
	struct aml_area_linux_ops ops;
};

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#define AML_AREA_LINUX_DECL(name) \
	struct aml_area_linux __ ##name## _inner_data; \
	struct aml_area name = { \
		&aml_area_linux_ops, \
		(struct aml_area_data *)&__ ## name ## _inner_data, \
	};

#define AML_AREA_LINUX_ALLOCSIZE \
	(sizeof(struct aml_area_linux) + \
	 sizeof(struct aml_area))

#define AML_AREA_LINUX_MANAGER_TYPE_SINGLE 0

#define AML_AREA_LINUX_MBIND_TYPE_REGULAR 0
#define AML_AREA_LINUX_MBIND_TYPE_MEMPOLICY 1

#define AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS 0
#define AML_AREA_LINUX_MMAP_TYPE_FD 1
#define AML_AREA_LINUX_MMAP_TYPE_TMPFILE 2

273 274 275 276 277 278 279
int aml_area_linux_create(struct aml_area **area, int manager_type,
			  int mbind_type, int mmap_type, ...);
int aml_area_linux_init(struct aml_area *area, int manager_type, int mbind_type,
			int mmap_type, ...);
int aml_area_linux_vinit(struct aml_area *area, int manager_type,
			 int mbind_type, int mmap_type, va_list args);
int aml_area_linux_destroy(struct aml_area *area);
280 281 282 283 284

/*******************************************************************************
 * Generic Area API:
 * Low-level, direct access to area logic.
 * For memory allocation function, follows the POSIX spec.
285
 ******************************************************************************/
286

287 288 289 290 291 292 293
void *aml_area_malloc(struct aml_area *area, size_t size);
void aml_area_free(struct aml_area *area, void *ptr);
void *aml_area_calloc(struct aml_area *area, size_t num, size_t size);
void *aml_area_realloc(struct aml_area *area, void *ptr, size_t size);
void *aml_area_acquire(struct aml_area *area, size_t size);
void aml_area_release(struct aml_area *area, void *ptr);
void *aml_area_mmap(struct aml_area *area, void *ptr, size_t size);
Kamil Iskra's avatar
Kamil Iskra committed
294 295
int aml_area_available(const struct aml_area *area);
int aml_area_binding(const struct aml_area *area, struct aml_binding **binding);
296

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/*******************************************************************************
 * Tiling:
 * Representation of a data structure organization in memory.
 ******************************************************************************/

/* opaque handle to all tilings */
struct aml_tiling_data;
struct aml_tiling_iterator_data;

/*forward declarations */
struct aml_tiling_iterator_ops;
struct aml_tiling_iterator;


struct aml_tiling_ops {
312 313 314 315 316 317 318
	int (*create_iterator)(struct aml_tiling_data *tiling,
			       struct aml_tiling_iterator **iterator,
			       int flags);
	int (*init_iterator)(struct aml_tiling_data *tiling,
			     struct aml_tiling_iterator *iterator, int flags);
	int (*destroy_iterator)(struct aml_tiling_data *tiling,
				struct aml_tiling_iterator *iterator);
Kamil Iskra's avatar
Kamil Iskra committed
319 320 321
	size_t (*tilesize)(const struct aml_tiling_data *tiling, int tileid);
	void* (*tilestart)(const struct aml_tiling_data *tiling,
			   const void *ptr, int tileid);
322 323 324 325 326 327 328
};

struct aml_tiling {
	struct aml_tiling_ops *ops;
	struct aml_tiling_data *data;
};

Kamil Iskra's avatar
Kamil Iskra committed
329 330 331
size_t aml_tiling_tilesize(const struct aml_tiling *tiling, int tileid);
void* aml_tiling_tilestart(const struct aml_tiling *tiling, const void *ptr,
			   int tileid);
332 333


334 335 336 337 338 339 340
int aml_tiling_create_iterator(struct aml_tiling *tiling,
			       struct aml_tiling_iterator **iterator,
			       int flags);
int aml_tiling_init_iterator(struct aml_tiling *tiling,
			     struct aml_tiling_iterator *iterator, int flags);
int aml_tiling_destroy_iterator(struct aml_tiling *tiling,
				struct aml_tiling_iterator *iterator);
341 342

struct aml_tiling_iterator_ops {
343 344
	int (*reset)(struct aml_tiling_iterator_data *iterator);
	int (*next)(struct aml_tiling_iterator_data *iterator);
Kamil Iskra's avatar
Kamil Iskra committed
345 346 347
	int (*end)(const struct aml_tiling_iterator_data *iterator);
	int (*get)(const struct aml_tiling_iterator_data *iterator,
		   va_list args);
348 349 350 351 352 353 354
};

struct aml_tiling_iterator {
	struct aml_tiling_iterator_ops *ops;
	struct aml_tiling_iterator_data *data;
};

355 356
int aml_tiling_iterator_reset(struct aml_tiling_iterator *iterator);
int aml_tiling_iterator_next(struct aml_tiling_iterator *iterator);
Kamil Iskra's avatar
Kamil Iskra committed
357 358
int aml_tiling_iterator_end(const struct aml_tiling_iterator *iterator);
int aml_tiling_iterator_get(const struct aml_tiling_iterator *iterator, ...);
359 360 361

#define AML_TILING_TYPE_1D 0

362 363 364 365
int aml_tiling_create(struct aml_tiling **tiling, int type, ...);
int aml_tiling_init(struct aml_tiling *tiling, int type, ...);
int aml_tiling_vinit(struct aml_tiling *tiling, int type, va_list args);
int aml_tiling_destroy(struct aml_tiling *tiling, int type);
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

/*******************************************************************************
 * Tiling 1D:
 ******************************************************************************/

extern struct aml_tiling_ops aml_tiling_1d_ops;
extern struct aml_tiling_iterator_ops aml_tiling_iterator_1d_ops;

struct aml_tiling_1d_data {
	size_t blocksize;
	size_t totalsize;
};

struct aml_tiling_iterator_1d_data {
	size_t i;
	struct aml_tiling_1d_data *tiling;
};

#define AML_TILING_1D_DECL(name) \
	struct aml_tiling_1d_data __ ##name## _inner_data; \
	struct aml_tiling name = { \
		&aml_tiling_1d_ops, \
		(struct aml_tiling_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_ITERATOR_1D_DECL(name) \
	struct aml_tiling_iterator_1d_data __ ##name## _inner_data; \
	struct aml_tiling_iterator name = { \
		&aml_tiling_iterator_1d_ops, \
		(struct aml_tiling_iterator_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_1D_ALLOCSIZE (sizeof(struct aml_tiling_1d_data) + \
				 sizeof(struct aml_tiling))

#define AML_TILING_ITERATOR_1D_ALLOCSIZE \
	(sizeof(struct aml_tiling_iterator_1d_data) + \
	 sizeof(struct aml_tiling_iterator))

/*******************************************************************************
 * Binding:
 * Representation of page bindings in an area
 ******************************************************************************/

/* opaque handle to all bindings */
struct aml_binding_data;

struct aml_binding_ops {
Kamil Iskra's avatar
Kamil Iskra committed
414 415 416 417 418 419 420 421 422
	int (*nbpages)(const struct aml_binding_data *binding,
		       const struct aml_tiling *tiling, const void *ptr,
		       int tileid);
	int (*pages)(const struct aml_binding_data *binding, void **pages,
		     const struct aml_tiling *tiling, const void *ptr,
		     int tileid);
	int (*nodes)(const struct aml_binding_data *binding, int *nodes,
		     const struct aml_tiling *tiling, const void *ptr,
		     int tileid);
423 424 425 426 427 428 429
};

struct aml_binding {
	struct aml_binding_ops *ops;
	struct aml_binding_data *data;
};

Kamil Iskra's avatar
Kamil Iskra committed
430 431 432 433 434 435 436 437 438
int aml_binding_nbpages(const struct aml_binding *binding,
			const struct aml_tiling *tiling,
			const void *ptr, int tileid);
int aml_binding_pages(const struct aml_binding *binding, void **pages,
		      const struct aml_tiling *tiling, const void *ptr,
		      int tileid);
int aml_binding_nodes(const struct aml_binding *binding, int *nodes,
		      const struct aml_tiling *tiling, const void *ptr,
		      int tileid);
439 440 441 442

#define AML_BINDING_TYPE_SINGLE 0
#define AML_BINDING_TYPE_INTERLEAVE 1

443 444 445 446
int aml_binding_create(struct aml_binding **binding, int type, ...);
int aml_binding_init(struct aml_binding *binding, int type, ...);
int aml_binding_vinit(struct aml_binding *binding, int type, va_list args);
int aml_binding_destroy(struct aml_binding *binding, int type);
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 481 482 483 484 485 486 487 488 489 490 491

/*******************************************************************************
 * Single Binding:
 * All pages on the same node
 ******************************************************************************/

extern struct aml_binding_ops aml_binding_single_ops;

struct aml_binding_single_data {
	int node;
};

#define AML_BINDING_SINGLE_DECL(name) \
	struct aml_binding_single_data __ ##name## _inner_data; \
	struct aml_binding name = { \
		&aml_binding_single_ops, \
		(struct aml_binding_data *)&__ ## name ## _inner_data, \
	};

#define AML_BINDING_SINGLE_ALLOCSIZE (sizeof(struct aml_binding_single_data) + \
				      sizeof(struct aml_binding))

/*******************************************************************************
 * Interleave Binding:
 * each page, of each tile, interleaved across nodes.
 ******************************************************************************/

extern struct aml_binding_ops aml_binding_interleave_ops;

struct aml_binding_interleave_data {
	int nodes[AML_MAX_NUMA_NODES];
	int count;
};

#define AML_BINDING_INTERLEAVE_DECL(name) \
	struct aml_binding_interleave_data __ ##name## _inner_data; \
	struct aml_binding name = { \
		&aml_binding_interleave_ops, \
		(struct aml_binding_data *)&__ ## name ## _inner_data, \
	};

#define AML_BINDING_INTERLEAVE_ALLOCSIZE \
	(sizeof(struct aml_binding_interleave_data) + \
	 sizeof(struct aml_binding))

492 493 494 495 496 497 498 499 500
/*******************************************************************************
 * DMA:
 * Management of low-level movement of memory.
 ******************************************************************************/

#define AML_DMA_REQUEST_TYPE_INVALID -1
#define AML_DMA_REQUEST_TYPE_COPY 0
#define AML_DMA_REQUEST_TYPE_MOVE 1

501
struct aml_dma_request;
502 503 504
struct aml_dma_data;

struct aml_dma_ops {
505
	int (*create_request)(struct aml_dma_data *dma,
506
			      struct aml_dma_request **req, int type,
507 508 509 510 511
			      va_list args);
	int (*destroy_request)(struct aml_dma_data *dma,
			       struct aml_dma_request *req);
	int (*wait_request)(struct aml_dma_data *dma,
			    struct aml_dma_request *req);
512 513 514 515 516 517 518
};

struct aml_dma {
	struct aml_dma_ops *ops;
	struct aml_dma_data *data;
};

519
int aml_dma_copy(struct aml_dma *dma, ...);
520
int aml_dma_async_copy(struct aml_dma *dma, struct aml_dma_request **req, ...);
521
int aml_dma_move(struct aml_dma *dma, ...);
522
int aml_dma_async_move(struct aml_dma *dma, struct aml_dma_request **req, ...);
523 524
int aml_dma_wait(struct aml_dma *dma, struct aml_dma_request *req);
int aml_dma_cancel(struct aml_dma *dma, struct aml_dma_request *req);
525 526 527 528 529 530 531

/*******************************************************************************
 * Linux Sequential DMA API:
 * DMA logic implemented based on general linux API, with the caller thread
 * used as the only execution thread.
 ******************************************************************************/

532 533 534
extern struct aml_dma_ops aml_dma_linux_seq_ops;

struct aml_dma_request_linux_seq {
535 536 537 538 539 540 541 542 543 544 545
	int type;
	void *dest;
	void *src;
	size_t size;
	int count;
	void **pages;
	int *nodes;
};

struct aml_dma_linux_seq_data {
	size_t size;
546
	struct aml_dma_request_linux_seq *requests;
547 548 549
};

struct aml_dma_linux_seq_ops {
550 551 552 553
	int (*do_copy)(struct aml_dma_linux_seq_data *dma,
		       struct aml_dma_request_linux_seq *req);
	int (*do_move)(struct aml_dma_linux_seq_data *dma,
		       struct aml_dma_request_linux_seq *req);
554
	int (*add_request)(struct aml_dma_linux_seq_data *dma,
555
			   struct aml_dma_request_linux_seq **req);
556
	int (*remove_request)(struct aml_dma_linux_seq_data *dma,
557
			      struct aml_dma_request_linux_seq **req);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
};

struct aml_dma_linux_seq {
	struct aml_dma_linux_seq_ops ops;
	struct aml_dma_linux_seq_data data;
};

#define AML_DMA_LINUX_SEQ_DECL(name) \
	struct aml_dma_linux_seq __ ##name## _inner_data; \
	struct aml_dma name = { \
		&aml_dma_linux_seq_ops, \
		(struct aml_dma_data *)&__ ## name ## _inner_data, \
	};

#define AML_DMA_LINUX_SEQ_ALLOCSIZE \
	(sizeof(struct aml_dma_linux_seq) + \
	 sizeof(struct aml_dma))

576 577 578 579
int aml_dma_linux_seq_create(struct aml_dma **dma, ...);
int aml_dma_linux_seq_init(struct aml_dma *dma, ...);
int aml_dma_linux_seq_vinit(struct aml_dma *dma, va_list args);
int aml_dma_linux_seq_destroy(struct aml_dma *dma);
580

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
/*******************************************************************************
 * Linux Parallel DMA API:
 * DMA logic implemented based on general linux API, with the caller thread
 * used as the only execution thread.
 ******************************************************************************/

extern struct aml_dma_ops aml_dma_linux_par_ops;

struct aml_dma_linux_par_thread_data {
	int tid;
	pthread_t thread;
	struct aml_dma_linux_par *dma;
	struct aml_dma_request_linux_par *req;
};

struct aml_dma_request_linux_par {
	int type;
	void *dest;
	void *src;
	size_t size;
	int count;
	void **pages;
	int *nodes;
	struct aml_dma_linux_par_thread_data *thread_data;
};

struct aml_dma_linux_par_data {
	size_t nbrequests;
	size_t nbthreads;
	struct aml_dma_request_linux_par *requests;
};

struct aml_dma_linux_par_ops {
	void *(*do_thread)(void *);
	int (*do_copy)(struct aml_dma_linux_par_data *,
		       struct aml_dma_request_linux_par *, int tid);
	int (*do_move)(struct aml_dma_linux_par_data *,
		       struct aml_dma_request_linux_par *, int tid);
	int (*add_request)(struct aml_dma_linux_par_data *,
			   struct aml_dma_request_linux_par **);
	int (*remove_request)(struct aml_dma_linux_par_data *,
			      struct aml_dma_request_linux_par **);
};

struct aml_dma_linux_par {
	struct aml_dma_linux_par_ops ops;
	struct aml_dma_linux_par_data data;
};

#define AML_DMA_LINUX_PAR_DECL(name) \
	struct aml_dma_linux_par __ ##name## _inner_data; \
	struct aml_dma name = { \
		&aml_dma_linux_par_ops, \
		(struct aml_dma_data *)&__ ## name ## _inner_data, \
	};

#define AML_DMA_LINUX_PAR_ALLOCSIZE \
	(sizeof(struct aml_dma_linux_par) + \
	 sizeof(struct aml_dma))

int aml_dma_linux_par_create(struct aml_dma **, ...);
int aml_dma_linux_par_init(struct aml_dma *, ...);
int aml_dma_linux_par_vinit(struct aml_dma *, va_list);
int aml_dma_linux_par_destroy(struct aml_dma *);

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
/*******************************************************************************
 * Scratchpad:
 * Use an area to stage data from an another area in and out.
 * A dma handles the movement itself.
 ******************************************************************************/

struct aml_scratch_request;
struct aml_scratch_data;

#define AML_SCRATCH_REQUEST_TYPE_INVALID -1
#define AML_SCRATCH_REQUEST_TYPE_PUSH 0
#define AML_SCRATCH_REQUEST_TYPE_PULL 1

struct aml_scratch_ops {
	int (*create_request)(struct aml_scratch_data *scratch,
			      struct aml_scratch_request **req, int type,
			      va_list args);
	int (*destroy_request)(struct aml_scratch_data *scratch,
			       struct aml_scratch_request *req);
	int (*wait_request)(struct aml_scratch_data *scratch,
			    struct aml_scratch_request *req);
};

struct aml_scratch {
	struct aml_scratch_ops *ops;
	struct aml_scratch_data *data;
};

int aml_scratch_pull(struct aml_scratch *scratch, ...);
int aml_scratch_async_pull(struct aml_scratch *scratch,
			   struct aml_scratch_request **req, ...);
int aml_scratch_push(struct aml_scratch *scratch, ...);
int aml_scratch_async_push(struct aml_scratch *scratch,
			   struct aml_scratch_request **req, ...);
int aml_scratch_wait(struct aml_scratch *scratch,
		     struct aml_scratch_request *req);
int aml_scratch_cancel(struct aml_scratch *scratch,
		       struct aml_scratch_request *req);

/*******************************************************************************
 * Sequential scratchpad API:
 * Scratchpad uses calling thread to trigger dma movements.
 ******************************************************************************/

extern struct aml_scratch_ops aml_scratch_seq_ops;

struct aml_scratch_request_seq {
	int type;
	struct aml_tiling *stiling;
	void *srcptr;
	int srcid;
	struct aml_tiling *dtiling;
	void *dstptr;
	int dstid;
	struct aml_dma_request *dma_req;
};

struct aml_scratch_seq_data {
	struct aml_area *srcarea, *scratcharea;
	struct aml_tiling *srctiling, *scratchtiling;
	struct aml_dma *dma;
	size_t nbrequests;
	struct aml_scratch_request_seq *requests;
};

struct aml_scratch_seq_ops {
	int (*doit)(struct aml_scratch_seq_data *scratch,
		    struct aml_scratch_request_seq *req);
	int (*add_request)(struct aml_scratch_seq_data *scratch,
			   struct aml_scratch_request_seq **req);
	int (*remove_request)(struct aml_scratch_seq_data *scratch,
			      struct aml_scratch_request_seq **req);
};

struct aml_scratch_seq {
	struct aml_scratch_seq_ops ops;
	struct aml_scratch_seq_data data;
};

#define AML_SCRATCH_SEQ_DECL(name) \
	struct aml_scratch_seq __ ##name## _inner_data; \
	struct aml_scratch name = { \
		&aml_scratch_seq_ops, \
		(struct aml_scratch_data *)&__ ## name ## _inner_data, \
	};

#define AML_SCRATCH_SEQ_ALLOCSIZE \
	(sizeof(struct aml_scratch_seq) + \
	 sizeof(struct aml_scratch))

int aml_scratch_seq_create(struct aml_scratch **scratch, ...);
int aml_scratch_seq_init(struct aml_scratch *scratch, ...);
int aml_scratch_seq_vinit(struct aml_scratch *scratch, va_list args);
int aml_scratch_seq_destroy(struct aml_scratch *scratch);
740

741 742 743 744
/*******************************************************************************
 * General functions:
 * Initialize internal structures, cleanup everything at the end.
 ******************************************************************************/
745

746 747
int aml_init(int *argc, char **argv[]);
int aml_finalize(void);
748

749
#endif