aml.h 26.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 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
/*******************************************************************************
 * Generic vector type:
 * Vector of nbelems, each of size sz, with a comparison key at offset off
 ******************************************************************************/

#define AML_VECTOR_ELTKEY_P(v,e) ((int *)(((intptr_t) e) + v->off))
#define AML_VECTOR_KEY_P(v,i) ((int *)(((intptr_t) v->ptr) + i*v->sz + v->off))
#define AML_VECTOR_ELT_P(v,i) ((void *)(((intptr_t) v->ptr) + i*v->sz))

struct aml_vector {
	int na;
	size_t nbelems;
	size_t sz;
	size_t off;
	void *ptr;
};

/* not needed, here for consistency */
#define AML_VECTOR_DECL(name) struct vector ##name;
#define AML_VECTOR_ALLOCSIZE (sizeof(struct vector))

size_t aml_vector_size(struct aml_vector *vec);
void *aml_vector_get(struct aml_vector *vec, int idx);
int aml_vector_find(struct aml_vector *vec, int key);
int aml_vector_resize(struct aml_vector *vec, size_t newsize);
void *aml_vector_add(struct aml_vector *vec);
void aml_vector_remove(struct aml_vector *vec, void *elem);

int aml_vector_init(struct aml_vector *vec, size_t num, size_t size,
		    size_t key, int na);
int aml_vector_destroy(struct aml_vector *vec);

59 60
/*******************************************************************************
 * Arenas:
61 62
 * In-memory allocator implementation. Dispatches actual memory mappings back to
 * areas.
63 64
 ******************************************************************************/

65 66 67 68 69 70
#define AML_ARENA_FLAG_ZERO 1

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

struct aml_arena_ops {
71 72 73 74 75 76 77
	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);
78 79
};

80
struct aml_arena {
81 82
	struct aml_arena_ops *ops;
	struct aml_arena_data *data;
83 84
};

85 86 87 88 89 90
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);
91 92 93 94 95 96 97 98 99 100

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

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

102 103 104 105 106 107 108 109 110 111 112
#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))

113 114 115 116 117

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

118 119 120 121
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);
122

123 124 125 126 127
/*******************************************************************************
 * Areas:
 * embeds information about a byte-addressable physical memory location and well
 * as binding policies over it.
 ******************************************************************************/
128

129 130 131 132
/* opaque handle to configuration data */
struct aml_area_data;

struct aml_area_ops {
133 134 135 136 137 138 139
	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
140 141
	int (*available)(const struct aml_area_data *area);
	int (*binding)(const struct aml_area_data *area,
142
		       struct aml_binding **binding);
143 144
};

145
struct aml_area {
146 147
	struct aml_area_ops *ops;
	struct aml_area_data *data;
148 149
};

150 151 152 153 154 155 156 157
/*******************************************************************************
 * POSIX Area:
 ******************************************************************************/

extern struct aml_area_ops aml_area_posix_ops;

struct aml_area_posix_data {
};
158

159 160 161 162 163 164 165 166 167 168 169
#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))

170 171 172 173
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);
174 175

/*******************************************************************************
176 177 178 179 180 181 182 183 184 185 186
 * 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
187
	struct aml_arena *(*get_arena)(const struct aml_area_linux_manager_data *data);
188 189 190 191
};

extern struct aml_area_linux_manager_ops aml_area_linux_manager_single_ops;

192 193 194
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);
195 196 197 198 199

#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))

200 201 202 203 204
#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)
205 206 207 208 209 210
#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)
211 212


213 214 215 216 217 218
struct aml_area_linux_mbind_data {
	unsigned long nodemask[AML_NODEMASK_SZ];
	int policy;
};

struct aml_area_linux_mbind_ops {
219 220 221
	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
222
	int (*binding)(const struct aml_area_linux_mbind_data *data,
223 224 225 226
		       struct aml_binding **binding);
};

int aml_area_linux_mbind_setdata(struct aml_area_linux_mbind_data *data,
Kamil Iskra's avatar
Kamil Iskra committed
227 228
				 int policy, const unsigned long *nodemask);
int aml_area_linux_mbind_generic_binding(const struct aml_area_linux_mbind_data *data,
229 230 231 232 233 234 235 236
					 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
237
			      int policy, const unsigned long *nodemask);
238
int aml_area_linux_mbind_destroy(struct aml_area_linux_mbind_data *data);
239 240 241 242 243 244 245 246 247 248 249 250

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 {
251 252
	void *(*mmap)(struct aml_area_linux_mmap_data *data, void *ptr,
		      size_t size);
253 254
};

255 256 257 258
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,
259
				off_t offset);
260 261 262 263 264
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);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

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;
};

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#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

305 306 307 308 309 310 311
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);
312 313 314 315 316

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

319 320 321 322 323 324 325
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
326 327
int aml_area_available(const struct aml_area *area);
int aml_area_binding(const struct aml_area *area, struct aml_binding **binding);
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/*******************************************************************************
 * 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 {
344 345 346 347 348 349 350
	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
351 352 353
	size_t (*tilesize)(const struct aml_tiling_data *tiling, int tileid);
	void* (*tilestart)(const struct aml_tiling_data *tiling,
			   const void *ptr, int tileid);
354 355 356 357 358 359 360
};

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

Kamil Iskra's avatar
Kamil Iskra committed
361 362 363
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);
364 365


366 367 368 369 370 371 372
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);
373 374

struct aml_tiling_iterator_ops {
375 376
	int (*reset)(struct aml_tiling_iterator_data *iterator);
	int (*next)(struct aml_tiling_iterator_data *iterator);
Kamil Iskra's avatar
Kamil Iskra committed
377 378 379
	int (*end)(const struct aml_tiling_iterator_data *iterator);
	int (*get)(const struct aml_tiling_iterator_data *iterator,
		   va_list args);
380 381 382 383 384 385 386
};

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

387 388
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
389 390
int aml_tiling_iterator_end(const struct aml_tiling_iterator *iterator);
int aml_tiling_iterator_get(const struct aml_tiling_iterator *iterator, ...);
391 392 393

#define AML_TILING_TYPE_1D 0

394 395 396 397
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);
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

/*******************************************************************************
 * 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
446 447 448 449 450 451 452 453 454
	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);
455 456 457 458 459 460 461
};

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

Kamil Iskra's avatar
Kamil Iskra committed
462 463 464 465 466 467 468 469 470
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);
471 472 473 474

#define AML_BINDING_TYPE_SINGLE 0
#define AML_BINDING_TYPE_INTERLEAVE 1

475 476 477 478
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);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

/*******************************************************************************
 * 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))

524 525 526 527 528 529 530 531 532
/*******************************************************************************
 * 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

533
struct aml_dma_request;
534 535 536
struct aml_dma_data;

struct aml_dma_ops {
537
	int (*create_request)(struct aml_dma_data *dma,
538
			      struct aml_dma_request **req, int type,
539 540 541 542 543
			      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);
544 545 546 547 548 549 550
};

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

551
int aml_dma_copy(struct aml_dma *dma, ...);
552
int aml_dma_async_copy(struct aml_dma *dma, struct aml_dma_request **req, ...);
553
int aml_dma_move(struct aml_dma *dma, ...);
554
int aml_dma_async_move(struct aml_dma *dma, struct aml_dma_request **req, ...);
555 556
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);
557 558 559 560 561 562 563

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

564 565 566
extern struct aml_dma_ops aml_dma_linux_seq_ops;

struct aml_dma_request_linux_seq {
567 568 569 570 571 572 573 574 575 576
	int type;
	void *dest;
	void *src;
	size_t size;
	int count;
	void **pages;
	int *nodes;
};

struct aml_dma_linux_seq_data {
577
	struct aml_vector requests;
578
	pthread_mutex_t lock;
579 580 581
};

struct aml_dma_linux_seq_ops {
582 583 584 585
	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);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
};

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))

604 605 606 607
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);
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
/*******************************************************************************
 * 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 nbthreads;
637
	struct aml_vector requests;
638
	pthread_mutex_t lock;
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
};

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);
};

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 *);

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
/*******************************************************************************
 * 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);
691
	void *(*baseptr)(struct aml_scratch_data *scratch);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
};

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);
709
void* aml_scratch_baseptr(struct aml_scratch *scratch);
710

711 712
/*******************************************************************************
 * Sequential scratchpad API:
713
 * Scratchpad uses calling thread to trigger asynchronous dma movements.
714 715 716 717 718 719
 ******************************************************************************/

extern struct aml_scratch_ops aml_scratch_seq_ops;

struct aml_scratch_request_seq {
	int type;
720
	struct aml_tiling *tiling;
721 722 723 724 725 726 727 728
	void *srcptr;
	int srcid;
	void *dstptr;
	int dstid;
	struct aml_dma_request *dma_req;
};

struct aml_scratch_seq_data {
729 730
	struct aml_area *src_area, *sch_area;
	struct aml_tiling *tiling;
731
	struct aml_dma *dma;
732
	void * sch_ptr;
733 734
	struct aml_vector tilemap;
	struct aml_vector requests;
735
	pthread_mutex_t lock;
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
};

struct aml_scratch_seq_ops {
	int (*doit)(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);
763

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
/*******************************************************************************
 * Parallel scratchpad API:
 * Scratchpad creates one thread to trigger synchronous dma movements.
 ******************************************************************************/

extern struct aml_scratch_ops aml_scratch_par_ops;

struct aml_scratch_request_par {
	int type;
	void *srcptr;
	int srcid;
	void *dstptr;
	int dstid;
	struct aml_scratch_par *scratch;
	pthread_t thread;
};

struct aml_scratch_par_data {
	struct aml_area *src_area, *sch_area;
	struct aml_tiling *tiling;
	struct aml_dma *dma;
	void * sch_ptr;
	struct aml_vector tilemap;
	struct aml_vector requests;
788
	pthread_mutex_t lock;
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
};

struct aml_scratch_par_ops {
	void *(*do_thread)(void *);
};

struct aml_scratch_par {
	struct aml_scratch_par_ops ops;
	struct aml_scratch_par_data data;
};

#define AML_SCRATCH_PAR_DECL(name) \
	struct aml_scratch_par __ ##name## _inner_data; \
	struct aml_scratch name = { \
		&aml_scratch_par_ops, \
		(struct aml_scratch_data *)&__ ## name ## _inner_data, \
	};

#define AML_SCRATCH_PAR_ALLOCSIZE \
	(sizeof(struct aml_scratch_par) + \
	 sizeof(struct aml_scratch))

int aml_scratch_par_create(struct aml_scratch **scratch, ...);
int aml_scratch_par_init(struct aml_scratch *scratch, ...);
int aml_scratch_par_vinit(struct aml_scratch *scratch, va_list args);
int aml_scratch_par_destroy(struct aml_scratch *scratch);

816 817 818 819
/*******************************************************************************
 * General functions:
 * Initialize internal structures, cleanup everything at the end.
 ******************************************************************************/
820

821 822
int aml_init(int *argc, char **argv[]);
int aml_finalize(void);
823

824
#endif