aml.h 67.6 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
#include <sys/mman.h>
#include <unistd.h>

15 16
/* Used by bindings, specifically in aml_binding_nbpages() and
 * aml_binding_pages().  */
17 18 19 20
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

21

22 23 24 25 26
/*******************************************************************************
 * Forward Declarations:
 ******************************************************************************/

struct aml_area;
27
struct aml_binding;
28

29 30 31 32 33
/*******************************************************************************
 * Generic vector type:
 * Vector of nbelems, each of size sz, with a comparison key at offset off
 ******************************************************************************/

34
/* Pointer to the key within element "e" of a vector "v".  */
35
#define AML_VECTOR_ELTKEY_P(v,e) ((int *)(((intptr_t) e) + v->off))
36
/* Pointer to the key within element index "i" of a vector "v".  */
37
#define AML_VECTOR_KEY_P(v,i) ((int *)(((intptr_t) v->ptr) + i*v->sz + v->off))
38
/* Pointer to the element index "i" of a vector "v".  */
39 40 41 42 43 44 45 46 47 48 49 50 51 52
#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))

53 54 55 56 57 58
/*
 * Provides the total number of elements in the vector, including currently
 * unused ones.
 * "vector": an initialized vector structure.
 * Returns the number of elements in the vector.
 */
59
size_t aml_vector_size(const struct aml_vector *vector);
60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*
 * Provides a pointer of element with index "index" within the vector.
 * "vector": an initialized vector structure.
 * "index": a valid index within "vector".  The index must not equal "na" and
 *          must be lower than the size of the vector.
 * Returns a pointer to the requested element.
 */
void *aml_vector_get(struct aml_vector *vector, int index);
/*
 * Find the first element with a particular key.
 * "vector": an initialized vector structure.
 * "key": the key to look for.
 * Returns the index of the found element or "na" if not found.
 */
74
int aml_vector_find(const struct aml_vector *vector, int key);
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
/*
 * Resizes the vector.  The keys of the newly allocated elements are set to the
 * "na" value.
 * "vector": an initialized vector structure.
 * "newsize": a new vector size.  Only sizes greater than the current one will
 *            be honored; smaller sizes will result in a no-op.
 * Returns 0 if successful; an error code otherwise.
 */
int aml_vector_resize(struct aml_vector *vector, size_t newsize);
/*
 * Provides the pointer to the first unused element.  If the vector is full,
 * it automatically gets enlarged.
 * "vector": an initialized vector structure.
 * Returns the pointer to the first unused element.
 */
void *aml_vector_add(struct aml_vector *vector);
/*
 * Removes an element from the vector.  The key of the element is set to the
 * "na" value.
 * "vector": an initialized vector structure.
 * "elem": an element within the vector.
 */
void aml_vector_remove(struct aml_vector *vector, void *elem);

/*
 * Initializes a vector.  Allocates elements and sets their keys to the "na"
 * value.
 * "vector": an allocated vector structure.
 * "num": the number of elements to allocate.
 * "size": the size of each individual element.
 * "key": the offset within each element where the key (of type int) is stored.
 * "na": a "null" key value used to indicate an unused element.
 * Returns 0 if successful; an error code otherwise.
 */
int aml_vector_init(struct aml_vector *vector, size_t num, size_t size,
110
		    size_t key, int na);
111 112 113 114 115 116 117
/*
 * Tears down an initialized vector.  Releases the memory buffer holding the
 * elements.
 * "vector": an initialized vector structure.
 * Returns 0 if successful; an error code otherwise.
 */
int aml_vector_destroy(struct aml_vector *vector);
118

119 120
/*******************************************************************************
 * Arenas:
121 122
 * In-memory allocator implementation. Dispatches actual memory mappings back to
 * areas.
123 124
 ******************************************************************************/

125
/* Arena Flags: access to useful jemalloc flags, with the same values. */
126
/* If passed as a flag to arena's mallocx()/reallocx() routines, the newly
127 128
 * allocated memory will be 0-initialized. */
#define AML_ARENA_FLAG_ZERO ((int)0x40)
129 130
/* As a flag to arena mallocx/reallocx, will align on (1<<a). */
#define AML_ARENA_FLAG_ALIGN(a) ((int)(a))
131 132 133 134 135

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

struct aml_arena_ops {
136 137 138 139 140 141 142
	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);
143 144
};

145
struct aml_arena {
146 147
	struct aml_arena_ops *ops;
	struct aml_arena_data *data;
148 149
};

150 151 152 153 154 155 156
/*
 * Registers a new memory arena with the system.  After this call the arena
 * is ready for use.
 * "arena": an initialized arena structure (see aml_arena_jemalloc_create()).
 * "area": a memory area that will be used as the backing store.
 * Returns 0 if successful; an error code otherwise.
 */
157
int aml_arena_register(struct aml_arena *arena, struct aml_area *area);
158 159 160 161 162 163 164
/*
 * Unregisters a memory arena from the system.  Also purges the contents of
 * the memory, so any buffers allocated from the arena should be considered
 * invalid after this call.
 * "arena": a registered arena structure (see aml_arena_register()).
 * Returns 0 if successful; an error code otherwise.
 */
165
int aml_arena_deregister(struct aml_arena *arena);
166 167 168 169 170 171 172
/*
 * Allocates a new memory buffer from the arena.
 * "arena": a registered arena structure (see aml_arena_register()).
 * "size": the buffer size in bytes; if 0 is passed, NULL will be returned.
 * "flags": see AML_ARENA_FLAG_*.
 * Returns a pointer to the newly allocated memory buffer; NULL if unsuccessful.
 */
173
void *aml_arena_mallocx(struct aml_arena *arena, size_t size, int flags);
174 175 176 177 178 179
/*
 * Releases a memory buffer back to the arena.
 * "arena": a registered arena structure (see aml_arena_register()).
 * "ptr": a pointer to the memory buffer or NULL (resulting in a no-op).
 * "flags": see AML_ARENA_FLAG_* (currently unused).
 */
180
void aml_arena_dallocx(struct aml_arena *arena, void *ptr, int flags);
181 182 183 184 185 186 187 188 189 190
/*
 * Changes the size of a previously allocated memory buffer.
 * "arena": a registered arena structure (see aml_arena_register()).
 * "ptr": a pointer to the memory buffer; if NULL is passed, acts just like
 *        aml_arena_mallocx().
 * "size": the new buffer size in bytes; if 0 is passed, acts just like
 *         aml_arena_dallocx() and returns NULL.
 * "flags": see AML_ARENA_FLAG_*.
 * Returns a pointer to the resized memory buffer; NULL if unsuccessful.
 */
191 192
void *aml_arena_reallocx(struct aml_arena *arena, void *ptr, size_t size,
			 int flags);
193 194 195 196 197 198 199 200 201 202

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

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

204 205 206 207 208 209 210 211 212 213 214
#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))

215 216
/* Arena types passed to jemalloc arena's create()/init()/vinit() routines.  */
/* Standard arena type.  */
217
#define AML_ARENA_JEMALLOC_TYPE_REGULAR 0
218
/* Arena type allocating memory-aligned buffers.  */
219
#define AML_ARENA_JEMALLOC_TYPE_ALIGNED 1
220
/* Arena type identical to an existing arena.  */
221 222
#define AML_ARENA_JEMALLOC_TYPE_GENERIC 2

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/*
 * Allocates and initializes a new jemalloc arena.
 * "arena": an address where the pointer to the newly allocated arena structure
 *          will be stored.
 * "type": see AML_ARENA_JEMALLOC_TYPE_*.
 * Variadic arguments:
 * - if AML_ARENA_JEMALLOC_TYPE_REGULAR is passed as "type", no additional
 *   arguments are needed.
 * - if AML_ARENA_JEMALLOC_TYPE_ALIGNED is passed as "type", an alignment
 *   argument of type size_t and value that is a power of 2 must be provided.
 * - if AML_ARENA_JEMALLOC_TYPE_GENERIC is passed as "type", a pointer argument
 *   to type "aml_arena_data" (obtained from the "data" field of an existing
 *   jemalloc arena structure) must be provided.
 * Returns 0 if successful; an error code otherwise.
 */
238
int aml_arena_jemalloc_create(struct aml_arena **arena, int type, ...);
239 240
/*
 * Initializes a jemalloc arena.  This is a varargs-variant of the
241
 * aml_arena_jemalloc_vinit() routine.
242 243 244 245 246
 * "arena": an allocated jemalloc arena structure.
 * "type": see aml_arena_jemalloc_create().
 * Variadic arguments: see aml_arena_jemalloc_create().
 * Returns 0 if successful; an error code otherwise.
 */
247
int aml_arena_jemalloc_init(struct aml_arena *arena, int type, ...);
248 249 250 251 252 253 254
/*
 * Initializes a jemalloc arena.
 * "arena": an allocated jemalloc arena structure.
 * "type": see aml_arena_jemalloc_create().
 * "args": see the variadic arguments of aml_arena_jemalloc_create().
 * Returns 0 if successful; an error code otherwise.
 */
255
int aml_arena_jemalloc_vinit(struct aml_arena *arena, int type, va_list args);
256 257 258 259 260
/*
 * Tears down an initialized jemalloc arena.
 * "arena": an initialized jemalloc arena structure.
 * Returns 0 if successful; an error code otherwise.
 */
261
int aml_arena_jemalloc_destroy(struct aml_arena *arena);
262

263 264
/*******************************************************************************
 * Areas:
265
 * embeds information about a byte-addressable physical memory location as well
266 267
 * as binding policies over it.
 ******************************************************************************/
268

269 270 271 272
/* opaque handle to configuration data */
struct aml_area_data;

struct aml_area_ops {
273 274 275
	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);
276
	void *(*memalign)(struct aml_area_data *area, size_t align, size_t size);
277 278 279 280
	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
281 282
	int (*available)(const struct aml_area_data *area);
	int (*binding)(const struct aml_area_data *area,
283
		       struct aml_binding **binding);
284 285
};

286
struct aml_area {
287 288
	struct aml_area_ops *ops;
	struct aml_area_data *data;
289 290
};

291 292 293 294 295 296 297 298
/*******************************************************************************
 * POSIX Area:
 ******************************************************************************/

extern struct aml_area_ops aml_area_posix_ops;

struct aml_area_posix_data {
};
299

300 301 302 303 304 305 306 307 308 309 310
#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))

311 312 313 314 315 316
/*
 * Allocates and initializes a new POSIX memory area.
 * "area": an address where the pointer to the newly allocated area structure
 *         will be stored.
 * Returns 0 if successful; an error code otherwise.
 */
317
int aml_area_posix_create(struct aml_area **area);
318 319 320 321 322
/*
 * Initializes a POSIX memory area.
 * "area": an allocated POSIX memory area structure.
 * Returns 0 if successful; an error code otherwise.
 */
323
int aml_area_posix_vinit(struct aml_area *area);
324 325 326 327 328 329
/*
 * Initializes a POSIX memory area.  This is identical to the
 * aml_area_posix_vinit() routine.
 * "area": an allocated POSIX memory area structure.
 * Returns 0 if successful; an error code otherwise.
 */
330
int aml_area_posix_init(struct aml_area *area);
331 332 333 334 335
/*
 * Tears down an initialized POSIX memory area.
 * "area": an initialized POSIX memory area structure.
 * Returns 0 if successful; an error code otherwise.
 */
336
int aml_area_posix_destroy(struct aml_area *area);
337 338

/*******************************************************************************
339 340 341 342 343 344 345 346 347 348 349
 * 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
350
	struct aml_arena *(*get_arena)(const struct aml_area_linux_manager_data *data);
351 352 353 354
};

extern struct aml_area_linux_manager_ops aml_area_linux_manager_single_ops;

355 356 357 358 359 360 361
/*
 * Initializes a Linux memory area manager.  A manager determines which arena
 * to use for allocations.
 * "data": an allocated Linux manager structure.
 * "arena": an arena to use for future allocations.
 * Returns 0 if successful; an error code otherwise.
 */
362 363
int aml_area_linux_manager_single_init(struct aml_area_linux_manager_data *data,
				       struct aml_arena *arena);
364 365 366 367 368
/*
 * Tears down an initialized Linux memory area manager.
 * "data": an initialized Linux manager structure.
 * Returns 0 if successful; an error code otherwise.
 */
369
int aml_area_linux_manager_single_destroy(struct aml_area_linux_manager_data *data);
370

371
/* Size of the bitmask (in bits) passed to aml_area_linux_mbind_init().  */
372
#define AML_MAX_NUMA_NODES 128
373
/* Size of the bitmask (in bytes) passed to aml_area_linux_mbind_init().  */
374
#define AML_NODEMASK_BYTES (AML_MAX_NUMA_NODES/8)
375 376
/* Size of the bitmask (in array elements) passed to
   aml_area_linux_mbind_init().  */
377 378
#define AML_NODEMASK_SZ (AML_NODEMASK_BYTES/sizeof(unsigned long))

379 380 381 382 383
#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)
384 385 386 387 388
/*
 * Sets a bit in a nodemask.
 * "mask": an array of type "unsigned long", at least AML_NODEMASK_SZ long.
 * "i": bit to set, indicating a NUMA node.
 */
389
#define AML_NODEMASK_SET(mask, i) (mask[AML_NODEMASK_ELT(i)] |= AML_NODEMASK_BITMASK(i))
390 391 392 393 394 395
/*
 * Clears a bit in a nodemask.
 * "mask": an array of type "unsigned long", at least AML_NODEMASK_SZ long.
 * "i": bit to clear, indicating a NUMA node.
 */
#define AML_NODEMASK_CLR(mask, i) (mask[AML_NODEMASK_ELT(i)] &= ~AML_NODEMASK_BITMASK(i))
396 397 398 399
/*
 * Zero-initializes a nodemask.
 * "mask": an array of type "unsigned long", at least AML_NODEMASK_SZ long.
 */
400 401 402 403 404
#define AML_NODEMASK_ZERO(mask) \
	do {								\
		for(unsigned int __i = 0; __i < AML_NODEMASK_SZ; __i++)	\
			mask[__i] = 0;					\
	} while(0)
405 406


407 408 409 410 411 412
struct aml_area_linux_mbind_data {
	unsigned long nodemask[AML_NODEMASK_SZ];
	int policy;
};

struct aml_area_linux_mbind_ops {
413 414 415
	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
416
	int (*binding)(const struct aml_area_linux_mbind_data *data,
417 418 419
		       struct aml_binding **binding);
};

420 421 422
/*
 * Sets memory policy of a Linux memory area.
 * "data": an initialized Linux memory policy structure.
423
 * "policy", "nodemask": see aml_area_linux_mbind_init().
424 425
 * Returns 0 if successful; an error code otherwise.
 */
426
int aml_area_linux_mbind_setdata(struct aml_area_linux_mbind_data *data,
Kamil Iskra's avatar
Kamil Iskra committed
427
				 int policy, const unsigned long *nodemask);
428 429 430 431 432 433 434 435
/*
 * Creates a new binding structure based on an existing Linux memory policy
 * structure.
 * "data": an initialized Linux memory policy structure.
 * "binding": an address where the pointer to the newly allocated binding
 *            structure will be stored.
 * Returns 0 if successful; an error code otherwise.
 */
Kamil Iskra's avatar
Kamil Iskra committed
436
int aml_area_linux_mbind_generic_binding(const struct aml_area_linux_mbind_data *data,
437
					 struct aml_binding **binding);
438 439 440 441 442 443 444 445
/*
 * Sets current memory policy before memory allocation using the Linux memory
 * area.
 * This variant is used with AML_AREA_LINUX_MBIND_TYPE_REGULAR mbind type (see
 * aml_area_linux_create()) and is basically a no-op.
 * "data": an initialized Linux memory policy structure.
 * Returns 0 if successful; an error code otherwise.
 */
446
int aml_area_linux_mbind_regular_pre_bind(struct aml_area_linux_mbind_data *data);
447 448 449 450 451 452 453 454 455 456
/*
 * Sets current memory policy on a new memory region allocated using the Linux
 * memory area.
 * This variant is used with AML_AREA_LINUX_MBIND_TYPE_REGULAR mbind type (see
 * aml_area_linux_create()).
 * "data": an initialized Linux memory policy structure.
 * "ptr": an address of the newly allocated memory region.
 * "size": the size of the newly allocated memory region.
 * Returns 0 if successful; an error code otherwise.
 */
457 458
int aml_area_linux_mbind_regular_post_bind(struct aml_area_linux_mbind_data *data,
					   void *ptr, size_t size);
459 460 461 462 463 464 465 466
/*
 * Sets current memory policy before memory allocation using the Linux memory
 * area.
 * This variant is used with AML_AREA_LINUX_MBIND_TYPE_MEMPOLICY mbind type (see
 * aml_area_linux_create()).
 * "data": an initialized Linux memory policy structure.
 * Returns 0 if successful; an error code otherwise.
 */
467
int aml_area_linux_mbind_mempolicy_pre_bind(struct aml_area_linux_mbind_data *data);
468 469 470 471 472 473 474 475 476 477
/*
 * Sets current memory policy on a new memory region allocated using the Linux
 * memory area.
 * This variant is used with AML_AREA_LINUX_MBIND_TYPE_MEMPOLICY mbind type (see
 * aml_area_linux_create()).
 * "data": an initialized Linux memory policy structure.
 * "ptr": an address of the newly allocated memory region.
 * "size": the size of the newly allocated memory region.
 * Returns 0 if successful; an error code otherwise.
 */
478 479
int aml_area_linux_mbind_mempolicy_post_bind(struct aml_area_linux_mbind_data *data,
					   void *ptr, size_t size);
480 481 482 483 484 485 486 487 488
/*
 * Initializes memory policy of a Linux memory area.
 * "data": an allocated Linux memory policy structure.
 * "policy": see MPOL_* in mbind(2).
 * "nodemask": an AML_MAX_NUMA_NODES-bit array (a AML_NODEMASK_SZ-element array)
 *             containing the NUMA node mask to use (see mbind(2) for more
 *             information).
 * Returns 0 if successful; an error code otherwise.
 */
489
int aml_area_linux_mbind_init(struct aml_area_linux_mbind_data *data,
Kamil Iskra's avatar
Kamil Iskra committed
490
			      int policy, const unsigned long *nodemask);
491 492 493 494 495
/*
 * Tears down an initialized Linx memory policy.
 * "data": an initialized Linux memory policy structure.
 * Returns 0 if successful; an error code otherwise.
 */
496
int aml_area_linux_mbind_destroy(struct aml_area_linux_mbind_data *data);
497 498 499 500 501 502 503 504 505 506 507 508

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 {
509 510
	void *(*mmap)(struct aml_area_linux_mmap_data *data, void *ptr,
		      size_t size);
511 512
};

513 514 515 516 517 518 519 520 521
/*
 * Allocates a memory region from a Linux memory area.
 * "data": an initialized Linux memory map structure.
 * "ptr": an address where the new memory region should be allocated (hint only;
 *        can be NULL to let the kernel decide).
 * "size": the requested size of thew new memory region to allocate.
 * Returns the address of the newly allocated region or MAP_FAILED (see mmap(2))
 * if unsuccessful.
 */
522 523
void *aml_area_linux_mmap_generic(struct aml_area_linux_mmap_data *data,
				  void *ptr, size_t size);
524 525 526 527 528 529 530 531
/*
 * Initializes memory map of a Linux memory area to use an anonymous
 * (0-initialized) mapping.
 * This variant is used with AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS map type (see
 * aml_area_linux_create()).
 * "data": an allocated Linux memory map structure.
 * Returns 0 if successful; an error code otherwise.
 */
532
int aml_area_linux_mmap_anonymous_init(struct aml_area_linux_mmap_data *data);
533 534 535 536 537 538 539 540 541 542
/*
 * Initializes memory map of a Linux memory area to use an existing file
 * mapping.
 * This variant is used with AML_AREA_LINUX_MMAP_TYPE_FD map type (see
 * aml_area_linux_create()).
 * "data": an allocated Linux memory map structure.
 * "fd": an open file descriptor.
 * "offset": the offset within the file to allocate from.
 * Returns 0 if successful; an error code otherwise.
 */
543
int aml_area_linux_mmap_fd_init(struct aml_area_linux_mmap_data *data, int fd,
544
				off_t offset);
545 546 547 548 549 550 551 552 553 554 555 556
/*
 * Initializes memory map of a Linux memory area to use a newly created,
 * temporary file mapping.
 * This variant is used with AML_AREA_LINUX_MMAP_TYPE_TMPFILE map type (see
 * aml_area_linux_create()).
 * "data": an allocated Linux memory map structure.
 * "template": a file name template, ending in "XXXXXX"; the last six characters
 *             will be replaced with the actual name on successful file creation
 *             (see mkstemp(3) for more information).
 * "max": the size of the temporary file to create.
 * Returns 0 if successful; an error code otherwise.
 */
557 558
int aml_area_linux_mmap_tmpfile_init(struct aml_area_linux_mmap_data *data,
				     char *template, size_t max);
559 560 561 562 563 564 565
/*
 * Tears down an initialized Linux memory map.
 * This variant is used with AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS map type (see
 * aml_area_linux_create()).
 * "data": an initialized Linux memory map structure.
 * Returns 0 if successful; an error code otherwise.
 */
566
int aml_area_linux_mmap_anonymous_destroy(struct aml_area_linux_mmap_data *data);
567 568 569 570 571 572 573
/*
 * Tears down an initialized Linux memory map.
 * This variant is used with AML_AREA_LINUX_MMAP_TYPE_FD map type (see
 * aml_area_linux_create()).
 * "data": an initialized Linux memory map structure.
 * Returns 0 if successful; an error code otherwise.
 */
574
int aml_area_linux_mmap_fd_destroy(struct aml_area_linux_mmap_data *data);
575 576 577 578 579 580 581
/*
 * Tears down an initialized Linux memory map.
 * This variant is used with AML_AREA_LINUX_MMAP_TYPE_TMPFILE map type (see
 * aml_area_linux_create()).
 * "data": an initialized Linux memory map structure.
 * Returns 0 if successful; an error code otherwise.
 */
582
int aml_area_linux_mmap_tmpfile_destroy(struct aml_area_linux_mmap_data *data);
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

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

603 604 605 606 607 608 609 610 611 612 613
#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))

614 615 616
/* Linux memory area manager types, passed to Linux memory area's
   create()/init()/vinit() routines.  */
/* Single-arena manager.  */
617 618
#define AML_AREA_LINUX_MANAGER_TYPE_SINGLE 0

619 620 621
/* Linux memory area mbind types, passed to Linux memory area's
   create()/init()/vinit() routines.  */
/* Regular type using mbind() after mmap().  */
622
#define AML_AREA_LINUX_MBIND_TYPE_REGULAR 0
623 624
/* Calls set_mempolicy() before and after mmap() to change the memory policy
   globally.  */
625 626
#define AML_AREA_LINUX_MBIND_TYPE_MEMPOLICY 1

627 628 629
/* Linux memory area map types, passed to Linux memory area's
   create()/init()/vinit() routines.  */
/* Zero-initialized, anonymous mapping.  */
630
#define AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS 0
631
/* Mapping using an existing file.  */
632
#define AML_AREA_LINUX_MMAP_TYPE_FD 1
633
/* Mapping using a newly created temporary file.  */
634 635
#define AML_AREA_LINUX_MMAP_TYPE_TMPFILE 2

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/*
 * Allocates and initializes a new Linux memory area.
 * "area": an address where the pointer to the newly allocated Linux memory area
 *         will be stored.
 * "manager_type": see AML_AREA_LINUX_MANAGER_TYPE_*.
 * "mbind_type": see AML_AREA_LINUX_MBIND_TYPE_*.
 * "mmap_type": see AML_AREA_LINUX_MMAP_TYPE_*.
 * Variadic arguments:
 * - "policy": an argument of type int; see aml_area_linux_mbind_init().
 * - "nodemask": an argument of type const unsigned long*;
 *               see aml_area_linux_mbind_init().
 * - if AML_AREA_LINUX_MMAP_TYPE_ANONYMOUS is passed as "mmap_type", no
 *   additional arguments are needed.
 * - if AML_AREA_LINUX_MMAP_TYPE_FD is passed as "mmap_type", two additional
 *   arguments are needed:
 *   - "fd": an argument of type int; see aml_area_linux_mmap_fd_init().
 *   - "offset": an argument of type off_t; see aml_area_linux_mmap_fd_init().
 * - if AML_AREA_LINUX_MMAP_TYPE_TMPFILE is passed as "mmap_type", two
 *   additional arguments are needed:
 *   - template: an argument of type char*; see
 *     aml_area_linux_mmap_tmpfile_init().
 *   - max: an argument of type size_t; see aml_area_linux_mmap_tmpfile_init().
 * Returns 0 if successful; an error code otherwise.
 */
660 661
int aml_area_linux_create(struct aml_area **area, int manager_type,
			  int mbind_type, int mmap_type, ...);
662 663 664 665 666 667 668 669
/*
 * Initializes a Linux memory area.  This is a varargs-variant of the
 * aml_area_linux_vinit() routine.
 * "area": an allocated Linux memory area structure.
 * "manager_type", "mbind_type", "mmap_type": see aml_area_linux_create().
 * Variadic arguments: see aml_area_linux_create().
 * Returns 0 if successful; an error code otherwise.
 */
670 671
int aml_area_linux_init(struct aml_area *area, int manager_type, int mbind_type,
			int mmap_type, ...);
672 673 674 675 676 677 678
/*
 * Initializes a Linux memory area.
 * "area": an allocated Linux memory area structure.
 * "manager_type", "mbind_type", "mmap_type": see aml_area_linux_create().
 * "args": see the variadic arguments of aml_area_linux_create().
 * Returns 0 if successful; an error code otherwise.
 */
679 680
int aml_area_linux_vinit(struct aml_area *area, int manager_type,
			 int mbind_type, int mmap_type, va_list args);
681 682 683 684 685
/*
 * Tears down an initialized Linux memory area.
 * "area": an initialized Linux memory area structure.
 * Returns 0 if successful; an error code otherwise.
 */
686
int aml_area_linux_destroy(struct aml_area *area);
687 688 689 690 691

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

694 695 696 697 698 699
/*
 * Allocates a new memory buffer from a memory area.
 * "area": an initialized memory area structure.
 * "size": the buffer size in bytes; if 0 is passed, NULL will be returned.
 * Returns a pointer to the newly allocated memory buffer; NULL if unsuccessful.
 */
700
void *aml_area_malloc(struct aml_area *area, size_t size);
701 702 703 704 705
/*
 * Releases a memory buffer back to the memory area.
 * "area": an initialized memory area structure.
 * "ptr": a pointer to the memory buffer or NULL (resulting in a no-op).
 */
706
void aml_area_free(struct aml_area *area, void *ptr);
707 708 709 710 711 712 713 714 715
/*
 * Allocates a new, zero-initialized memory buffer from a memory area.
 * "area": an initialized memory area structure.
 * "num": the number of elements of size "size" to allocate; if 0 is passed,
 *        NULL will be returned
 * "size": the size of each individual element to allocate, in bytes; if 0 is
 *         passed, NULL will be returned.
 * Returns a pointer to the newly allocated memory buffer; NULL if unsuccessful.
 */
716
void *aml_area_calloc(struct aml_area *area, size_t num, size_t size);
717 718 719 720 721 722 723 724
/*
 * Allocates a new, aligned, memory buffer from a memory area.
 * "area": an initialized memory area structure.
 * "align": an alignment for the returned pointer.
 * "size": the buffer size in bytes; if 0 is passed, NULL will be returned.
 * Returns a pointer to the newly allocated memory buffer; NULL if unsuccessful.
 */
void *aml_area_memalign(struct aml_area *area, size_t align, size_t size);
725 726 727 728 729 730 731 732 733
/*
 * Changes the size of a previously allocated memory buffer.
 * "area": an initialized memory area structure.
 * "ptr": a pointer to the memory buffer; if NULL is passed, acts just like
 *        aml_area_malloc().
 * "size": the new buffer size in bytes; if 0 is passed, acts just like
 *         aml_area_free() and returns NULL.
 * Returns a pointer to the resized memory buffer; NULL if unsuccessful.
 */
734
void *aml_area_realloc(struct aml_area *area, void *ptr, size_t size);
735
/* FIXME! */
736
void *aml_area_acquire(struct aml_area *area, size_t size);
737
/* FIXME! */
738
void aml_area_release(struct aml_area *area, void *ptr);
739 740 741 742 743 744 745 746 747 748
/*
 * Allocates a memory region from a Linux memory area, respecting memory policy
 * settings (see aml_area_linux_mbind_init()).
 * "area": an initialized memory area structure.
 * "ptr": an address where the new memory region should be allocated (hint only;
 *        can be NULL to let the kernel decide).
 * "size": the requested size of thew new memory region to allocate.
 * Returns the address of the newly allocated region or MAP_FAILED (see mmap(2))
 * if unsuccessful.
 */
749
void *aml_area_mmap(struct aml_area *area, void *ptr, size_t size);
750
/* FIXME! */
Kamil Iskra's avatar
Kamil Iskra committed
751
int aml_area_available(const struct aml_area *area);
752 753 754 755 756 757 758
/*
 * Creates a new binding structure based on an existing Linux memory area.
 * "area": an initialized memory area structure.
 * "binding": an address where the pointer to the newly allocated binding
 *            structure will be stored.
 * Returns 0 if successful; an error code otherwise.
 */
Kamil Iskra's avatar
Kamil Iskra committed
759
int aml_area_binding(const struct aml_area *area, struct aml_binding **binding);
760

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
/*******************************************************************************
 * 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 {
776 777 778 779 780 781 782
	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
783
	size_t (*tilesize)(const struct aml_tiling_data *tiling, int tileid);
784 785
	size_t (*tilerowsize)(const struct aml_tiling_data *tiling, int tileid);
	size_t (*tilecolsize)(const struct aml_tiling_data *tiling, int tileid);
Kamil Iskra's avatar
Kamil Iskra committed
786 787
	void* (*tilestart)(const struct aml_tiling_data *tiling,
			   const void *ptr, int tileid);
788
	int (*ndims)(const struct aml_tiling_data *tiling, va_list);
789 790 791 792 793 794 795
};

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

796 797 798 799 800 801 802
/*
 * Provides the information on the size of a tile.
 * "tiling": an initialized tiling structure.
 * "tileid": an identifier of a tile (a value between 0 and the number of tiles
 *           minus 1).
 * Returns the size of a tile.
 */
Kamil Iskra's avatar
Kamil Iskra committed
803
size_t aml_tiling_tilesize(const struct aml_tiling *tiling, int tileid);
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

/*
 * Provides the information on the size of a tile row.
 * "tiling": an initialized tiling structure.
 * "tileid": an identifier of a tile (a value between 0 and the number of tiles
 *           minus 1).
 * Returns the size of a tile row.
 */
size_t aml_tiling_tilerowsize(const struct aml_tiling *tiling, int tileid);

/*
 * Provides the information on the size of a tile column.
 * "tiling": an initialized tiling structure.
 * "tileid": an identifier of a tile (a value between 0 and the number of tiles
 *           minus 1).
819
 * Returns the size of a tile column.
820 821 822
 */
size_t aml_tiling_tilecolsize(const struct aml_tiling *tiling, int tileid);

823 824 825 826 827 828 829 830 831 832
/*
 * Provides the information on the location of a tile in memory.
 * "tiling": an initialized tiling structure.
 * "ptr": an address of the start of the complete user data structure that this
 *        tiling describes.
 * "tileid": an identifier of a tile (a value between 0 and the number of tiles
 *           minus 1).
 * Returns the address of the start of the tile identified by "tileid", within
 * the provided user data structure.
 */
Kamil Iskra's avatar
Kamil Iskra committed
833 834
void* aml_tiling_tilestart(const struct aml_tiling *tiling, const void *ptr,
			   int tileid);
835

836 837 838 839 840 841 842 843 844 845
/*
 * Provides the dimensions of the entire tiling in tiles.
 * "tiling": an initialized tiling structure.
 * Variadic arguments:
 *  - a list of (size_t *), one per dimension of the tiling.
 *  Will contain the size of each dimension in tiles upon return.
 * Returns 0 if successful, an error code otherwise.
 */
int aml_tiling_ndims(const struct aml_tiling *tiling, ...);

846 847 848 849 850 851 852 853
/*
 * Allocates and initializes a new tiling iterator.
 * "tiling": an initialized tiling structure.
 * "iterator": an address where the pointer to the newly allocated iterator
 *             structure will be stored.
 * "flags": reserved for future use; pass 0 for now.
 * Returns 0 if successful; an error code otherwise.
 */
854 855 856
int aml_tiling_create_iterator(struct aml_tiling *tiling,
			       struct aml_tiling_iterator **iterator,
			       int flags);
857 858 859 860 861 862 863
/*
 * Initializes a tiling iterator.
 * "tiling": an initialized tiling structure.
 * "iterator": an allocated tiling iterator structure.
 * "flags": reserved for future use; pass 0 for now.
 * Returns 0 if successful; an error code otherwise.
 */
864 865
int aml_tiling_init_iterator(struct aml_tiling *tiling,
			     struct aml_tiling_iterator *iterator, int flags);
866 867 868
/*
 * Tears down an initialized tiling iterator.
 * "tiling": an initialized tiling structure.
869
 * "iterator": an initialized tiling iterator structure.
870 871
 * Returns 0 if successful; an error code otherwise.
 */
872 873
int aml_tiling_destroy_iterator(struct aml_tiling *tiling,
				struct aml_tiling_iterator *iterator);
874 875

struct aml_tiling_iterator_ops {
876 877
	int (*reset)(struct aml_tiling_iterator_data *iterator);
	int (*next)(struct aml_tiling_iterator_data *iterator);
Kamil Iskra's avatar
Kamil Iskra committed
878 879 880
	int (*end)(const struct aml_tiling_iterator_data *iterator);
	int (*get)(const struct aml_tiling_iterator_data *iterator,
		   va_list args);
881 882 883 884 885 886 887
};

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

888 889
/*
 * Resets a tiling iterator to the first tile.
890
 * "iterator": an initialized tiling iterator structure.
891 892
 * Returns 0 if successful; an error code otherwise.
 */
893
int aml_tiling_iterator_reset(struct aml_tiling_iterator *iterator);
894 895
/*
 * Advances a tiling iterator to the next tile.
896
 * "iterator": an initialized tiling iterator structure.
897 898
 * Returns 0 if successful; an error code otherwise.
 */
899
int aml_tiling_iterator_next(struct aml_tiling_iterator *iterator);
900 901
/*
 * Checks whether the iterator is past the last tile.
902
 * "iterator": an initialized tiling iterator structure.
903 904 905
 * Returns 0 if the iterator points at a valid tile; 1 if it's past the last
 * tile.
 */
Kamil Iskra's avatar
Kamil Iskra committed
906
int aml_tiling_iterator_end(const struct aml_tiling_iterator *iterator);
907 908
/*
 * Queries the iterator.
909
 * "iterator": an initialized tiling iterator structure.
910 911 912 913 914
 * Variadic arguments:
 * - "x": an argument of type unsigned long*; on return gets filled with the
 *        identifier of the tile currently pointed to.
 * Returns 0 if successful; an error code otherwise.
 */
Kamil Iskra's avatar
Kamil Iskra committed
915
int aml_tiling_iterator_get(const struct aml_tiling_iterator *iterator, ...);
916

917 918
/* Tiling types passed to the tiling create()/init()/vinit() routines.  */
/* Regular, linear tiling with uniform tile sizes.  */
919
#define AML_TILING_TYPE_1D 0
920
#define AML_TILING_TYPE_2D 2
921 922
#define AML_TILING_TYPE_2D_CONTIG_ROWMAJOR 3
#define AML_TILING_TYPE_2D_CONTIG_COLMAJOR 4
923

924 925 926 927 928 929 930 931 932 933
/*
 * Allocates and initializes a new tiling.
 * "tiling": an address where the pointer to the newly allocated tiling
 *           structure will be stored.
 * "type": see AML_TILING_TYPE_*.
 * Variadic arguments:
 * - if "type" equals AML_TILING_TYPE_1D, two additional arguments are needed:
 *   - "tilesize": an argument of type size_t; provides the size of each tile.
 *   - "totalsize": an argument of type size_t; provides the size of the
 *                  complete user data structure to be tiled.
934 935 936 937 938 939 940
 * - if "type" equals AML_TILING_TYPE_2D, three additional arguments are needed:
 *   - "rowsize": an argument of type size_t; provides the size of the row of
 *      each tile.
 *   - "columnsize": an argument of type size_t; provides the size of the column
 *      of each tile.
 *   - "totalsize": an argument of type size_t; provides the size of the
 *                  complete user data structure to be tiled.
941 942
 * Returns 0 if successful; an error code otherwise.
 */
943
int aml_tiling_create(struct aml_tiling **tiling, int type, ...);
944 945 946 947 948 949 950 951
/*
 * Initializes a tiling.  This is a varargs-variant of the aml_tiling_vinit()
 * routine.
 * "tiling": an allocated tiling structure.
 * "type": see aml_tiling_create().
 * Variadic arguments: see aml_tiling_create().
 * Returns 0 if successful; an error code otherwise.
 */
952
int aml_tiling_init(struct aml_tiling *tiling, int type, ...);
953 954 955 956 957 958 959
/*
 * Initializes a tiling.
 * "tiling": an allocated tiling structure.
 * "type": see aml_tiling_create().
 * "args": see the variadic arguments of aml_tiling_create().
 * Returns 0 if successful; an error code otherwise.
 */
960
int aml_tiling_vinit(struct aml_tiling *tiling, int type, va_list args);
961 962 963 964 965 966
/*
 * Tears down an initialized tiling.
 * "tiling": an initialized tiling structure.
 * "type": see aml_tiling_create().
 * Returns 0 if successful; an error code otherwise.
 */
967
int aml_tiling_destroy(struct aml_tiling *tiling, int type);
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

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

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
/*******************************************************************************
 * Tiling 2D:
 ******************************************************************************/

extern struct aml_tiling_ops aml_tiling_2d_ops;
extern struct aml_tiling_iterator_ops aml_tiling_iterator_2d_ops;

struct aml_tiling_2d_data {
	size_t blocksize;
	size_t tilerowsize;
	size_t tilecolsize;
	size_t totalsize;
};

struct aml_tiling_iterator_2d_data {
	size_t i;
	struct aml_tiling_2d_data *tiling;
};

#define AML_TILING_2D_DECL(name) \
	struct aml_tiling_2d_data __ ##name## _inner_data; \
	struct aml_tiling name = { \
		&aml_tiling_2d_ops, \
		(struct aml_tiling_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_ITERATOR_2D_DECL(name) \
	struct aml_tiling_iterator_2d_data __ ##name## _inner_data; \
	struct aml_tiling_iterator name = { \
		&aml_tiling_iterator_2d_ops, \
		(struct aml_tiling_iterator_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_2D_ALLOCSIZE (sizeof(struct aml_tiling_2d_data) + \
				 sizeof(struct aml_tiling))

#define AML_TILING_ITERATOR_2D_ALLOCSIZE \
	(sizeof(struct aml_tiling_iterator_2d_data) + \
	 sizeof(struct aml_tiling_iterator))

1047 1048 1049 1050 1051
/*******************************************************************************
 * Tiling 2D CONTIG:
 * a contiguous memory area composed of contiguous tiles arranged in 2D grid.
 ******************************************************************************/

1052 1053
extern struct aml_tiling_ops aml_tiling_2d_contig_rowmajor_ops;
extern struct aml_tiling_ops aml_tiling_2d_contig_colmajor_ops;
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
extern struct aml_tiling_iterator_ops aml_tiling_iterator_2d_contig_ops;

struct aml_tiling_2d_contig_data {
	size_t blocksize;
	size_t totalsize;
	size_t ndims[2]; /* # of rows in tiles, # of columns in tiles */
};

struct aml_tiling_iterator_2d_contig_data {
	size_t i;
	struct aml_tiling_2d_contig_data *tiling;
};

1067
#define AML_TILING_2D_CONTIG_ROWMAJOR_DECL(name) \
1068 1069
	struct aml_tiling_2d_contig_data __ ##name## _inner_data; \
	struct aml_tiling name = { \
1070 1071 1072 1073 1074 1075 1076 1077
		&aml_tiling_2d_contig_rowmajor_ops, \
		(struct aml_tiling_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_2D_CONTIG_COLMAJOR_DECL(name) \
	struct aml_tiling_2d_contig_data __ ##name## _inner_data; \
	struct aml_tiling name = { \
		&aml_tiling_2d_contig_colmajor_ops, \
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
		(struct aml_tiling_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_ITERATOR_2D_CONTIG_DECL(name) \
	struct aml_tiling_iterator_2d_contig_data __ ##name## _inner_data; \
	struct aml_tiling_iterator name = { \
		&aml_tiling_iterator_2d_contig_ops, \
		(struct aml_tiling_iterator_data *)&__ ## name ## _inner_data, \
	};

#define AML_TILING_2D_CONTIG_ALLOCSIZE (sizeof(struct aml_tiling_2d_contig_data) + \
				 sizeof(struct aml_tiling))

#define AML_TILING_ITERATOR_2D_CONTIG_ALLOCSIZE \
	(sizeof(struct aml_tiling_iterator_2d_contig_data) + \
	 sizeof(struct aml_tiling_iterator))

1095 1096 1097 1098 1099 1100 1101 1102 1103
/*******************************************************************************
 * 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
1104 1105 1106 1107 1108 1109 1110 1111 1112
	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);
1113 1114 1115 1116 1117 1118 1119
};

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

1120 1121
/*
 * Provides the size of a tile in memory, in pages.
1122
 * "binding": an initialized binding structure.
1123 1124 1125 1126 1127
 * "tiling": an initialized tiling structure.
 * "ptr", "tileid": see aml_tiling_tilestart().
 * Returns the total number of pages that a tile occupies, including partial
 * pages.
 */
Kamil Iskra's avatar
Kamil Iskra committed
1128 1129 1130
int aml_binding_nbpages(const struct aml_binding *binding,
			const struct aml_tiling *tiling,
			const void *ptr, int tileid);
1131 1132
/*
 * Provides the addresses of pages that a tile occupies.
1133
 * "binding": an initialized binding structure.
1134 1135 1136 1137 1138 1139 1140
 * "pages": an array that will be filled with start addresses of all pages
 *          that a tile occupies.  The array must be at least
 *          aml_binding_nbpages() elements long.
 * "tiling": an initialized tiling structure.
 * "ptr", "tileid": see aml_tiling_tilestart().
 * Returns 0 if successful; an error code otherwise.
 */
Kamil Iskra's avatar
Kamil Iskra committed
1141 1142 1143
int aml_binding_pages(const struct aml_binding *binding, void **pages,
		      const struct aml_tiling *tiling, const void *ptr,
		      int tileid);
1144 1145
/*
 * Provides the NUMA node information of pages that a tile occupies.
1146
 * "binding": an initialized binding structure.
1147 1148 1149 1150 1151 1152 1153
 * "nodes": an array that will be filled with NUMA node id's of all pages
 *          that a tile occupies.  The array must be at least
 *          aml_binding_nbpages() elements long.
 * "tiling": an initialized tiling structure.
 * "ptr", "tileid": see aml_tiling_tilestart().
 * Returns 0 if successful; an error code otherwise.
 */
Kamil Iskra's avatar
Kamil Iskra committed
1154 1155 1156
int aml_binding_nodes(const struct aml_binding *binding, int *nodes,
		      const struct aml_tiling *tiling, const void *ptr,
		      int tileid);
1157

1158 1159
/* Binding types passed to the binding create()/init()/vinit() routines.  */
/* Binding where all pages are bound to the same NUMA node.  */
1160
#define AML_BINDING_TYPE_SINGLE 0
1161
/* Binding where pages are interleaved among multiple NUMA nodes.  */
1162 1163
#define AML_BINDING_TYPE_INTERLEAVE 1

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
/*
 * Allocates and initializes a new binding.
 * "binding": an address where the pointer to the newly allocated binding
 *            structure will be stored.
 * "type": see AML_BINDING_TYPE_*.
 * Variadic arguments:
 * - if "type" equals AML_BINDING_TYPE_SINGLE, one additional argument is
 *   needed:
 *   - "node": an argument of type int; provides a NUMA node id where pages
 *             will be allocated from.
 * - if "type" equals AML_BINDING_TYPE_INTERLEAVE, one additional argument is
 *   needed:
 *   - "mask": an argument of type const unsigned long*; provides an array
 *             at least AML_NODEMASK_SZ elements long, storing a bitmask of
 *             NUMA node ids where pages will be allocated from.  See
 *             AML_NODEMASK_* macros for more information.
 * Returns 0 if successful; an error code otherwise.
 */
1182
int aml_binding_create(struct aml_binding **binding, int type, ...);
1183 1184 1185 1186 1187 1188 1189 1190
/*
 * Initializes a new binding.  This is a varags-variant of the
 * aml_binding_vinit() routine.
 * "binding": an allocated binding structure.
 * "type": see aml_binding_create().
 * Variadic arguments: see aml_binding_create().
 * Returns 0 if successful; an error code otherwise.
 */
1191
int aml_binding_init(struct aml_binding *binding, int type, ...);
1192 1193 1194 1195 1196 1197 1198
/*
 * Initializes a new binding.
 * "binding": an allocated binding structure.
 * "type": see aml_binding_create().
 * "args": see the variadic arguments of aml_binding_create().
 * Returns 0 if successful; an error code otherwise.
 */
1199
int aml_binding_vinit(struct aml_binding *binding, int type, va_list args);
1200 1201 1202 1203 1204 1205
/*
 * Tears down an initialized binding.
 * "binding": an initialized binding structure.
 * "type": see aml_binding_create().
 * Returns 0 if successful; an error code otherwise.
 */
1206
int aml_binding_destroy(struct aml_binding *binding, int type);
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

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

1252 1253 1254 1255 1256
/*******************************************************************************
 * DMA:
 * Management of low-level movement of memory.
 ******************************************************************************/

1257 1258
/* Internal macros used for tracking DMA request types.  */
/* Invalid request type.  Used for marking inactive requests in the vector.  */
1259
#define AML_DMA_REQUEST_TYPE_INVALID -1
1260
/* Copy request type.  Uses memcpy() for data migration.  */
1261
#define AML_DMA_REQUEST_TYPE_COPY 0
1262
/* Move request type.  Uses move_pages() for data migration.  */
1263 1264
#define AML_DMA_REQUEST_TYPE_MOVE 1

1265
struct aml_dma_request;
1266 1267 1268
struct aml_dma_data;

struct aml_dma_ops {
1269
	int (*create_request)(struct aml_dma_data *dma,
1270
			      struct aml_dma_request **req, int type,
1271 1272 1273 1274 1275
			      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);
1276 1277 1278 1279 1280 1281 1282
};

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

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
/*
 * Requests a synchronous data copy between two different tiles, using
 * memcpy() or equivalent.
 * "dma": an initialized DMA structure.
 * Variadic arguments:
 * - "dt": an argument of type struct aml_tiling*; the destination tiling
 *         structure.
 * - "dptr": an argument of type void*; the start address of the complete
 *           destination user data structure.
 * - "dtid": an argument of type int; the destination tile identifier.
 * - "st": an argument of type struct aml_tiling*; the source tiling structure.
 * - "sptr": an argument of type void*; the start address of the complete
 *           source user data structure.
 * - "stid": an argument of type int; the source tile identifier.
 * Returns 0 if successful; an error code otherwise.
 */
1299
int aml_dma_copy(struct aml_dma *dma, ...);
1300 1301 1302 1303 1304 1305 1306 1307 1308
/*
 * Requests a data copy between two different tiles.  This is an asynchronous
 * version of aml_dma_copy().
 * "dma": an initialized DMA structure.
 * "req": an address where the pointer to the newly assigned DMA request will be
 *        stored.
 * Variadic arguments: see aml_dma_copy().
 * Returns 0 if successful; an error code otherwise.
 */
1309
int aml_dma_async_copy(struct aml_dma *dma, struct aml_dma_request **req, ...);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
/*
 * Requests a synchronous data move of a tile to a new memory area, using
 * move_pages() or equivalent.
 * "dma": an initialized DMA structure.
 * Variadic arguments:
 * - "darea": an argument of type struct aml_area*; the destination memory area
 *         structure.
 * - "st": an argument of type struct aml_tiling*; the tiling structure.
 * - "sptr": an argument of type void*; the start address of the complete
 *           user data structure.
 * - "stid": an argument of type int; the tile identifier.
 * Returns 0 if successful; an error code otherwise.
 */
1323
int aml_dma_move(struct aml_dma *dma, ...);
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
/*
 * Requests a data move of a tile to a new memory area.  This is an asynchronous
 * version of aml_dma_move().
 * "dma": an initialized DMA structure.
 * "req": an address where the pointer to the newly assigned DMA request will be
 *        stored.
 * Variadic arguments: see aml_dma_move().
 * Returns 0 if successful; an error code otherwise.
 *
 */
1334
int aml_dma_async_move(struct aml_dma *dma, struct aml_dma_request **req, ...);
1335 1336 1337 1338 1339 1340
/*
 * Waits for an asynchronous DMA request to complete.
 * "dma": an initialized DMA structure.
 * "req": a DMA request obtained using aml_dma_async_*() calls.
 * Returns 0 if successful; an error code otherwise.
 */
1341
int aml_dma_wait(struct aml_dma *dma, struct aml_dma_request *req);
1342 1343 1344 1345 1346 1347
/*
 * Tears down an asynchronous DMA request before it completes.
 * "dma": an initialized DMA structure.
 * "req": a DMA request obtained using aml_dma_async_*() calls.
 * Returns 0 if successful; an error code otherwise.
 */
1348
int aml_dma_cancel(struct aml_dma *dma, struct aml_dma_request *req);
1349 1350 1351 1352 1353 1354 1355

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

1356 1357 1358
extern struct aml_dma_ops aml_dma_linux_seq_ops;

struct aml_dma_request_linux_seq {
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	int type;
	void *dest;
	void *src;
	size_t size;
	int count;
	void **pages;
	int *nodes;
};

struct aml_dma_linux_seq_data {
1369
	struct aml_vector requests;
1370
	pthread_mutex_t lock;
1371 1372 1373
};

struct aml_dma_linux_seq_ops {
1374 1375 1376 1377
	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);
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
};

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

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
/*
 * Allocates and initializes a new sequential DMA.
 * "dma": an address where the pointer to the newly allocated DMA structure
 *        will be stored.
 * Variadic arguments:
 * - "nbreqs": an argument of type size_t; the initial number of slots for
 *             asynchronous request that are in-flight (will be increased
 *             automatically if necessary).
 * Returns 0 if successful; an error code otherwise.
 */
1406
int aml_dma_linux_seq_create(struct aml_dma **dma, ...);
1407 1408
/*
 * Initializes a new sequential DMA.  This is a varargs-variant of the
1409
 * aml_dma_linux_seq_vinit() routine.
1410 1411 1412 1413
 * "dma": an allocated DMA structure.
 * Variadic arguments: see aml_dma_linux_seq_create().
 * Returns 0 if successful; an error code otherwise.
 */
1414
int aml_dma_linux_seq_init(struct aml_dma *dma, ...);
1415 1416 1417 1418 1419 1420
/*
 * Initializes a new sequential DMA.
 * "dma": an allocated DMA structure.
 * "args": see the variadic arguments of aml_dma_linux_seq_create().
 * Returns 0 if successful; an error code otherwise.
 */
1421
int aml_dma_linux_seq_vinit(struct aml_dma *dma, va_list args);
1422 1423
/*
 * Tears down an initialized sequential DMA.
1424
 * "dma": an initialized DMA structure.
1425 1426
 * Returns 0 if successful; an error code otherwise.
 */
1427
int aml_dma_linux_seq_destroy(struct aml_dma *dma);
1428

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
/*******************************************************************************
 * 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;
1457
	struct aml_vector requests;
1458
	pthread_mutex_t lock;
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
};

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

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
/*
 * Allocates and initializes a new parallel DMA.
 * "dma": an address where the pointer to the newly allocated DMA structure
 *        will be stored.
 * Variadic arguments:
 * - "nbreqs": an argument of type size_t; the initial number of slots for
 *             asynchronous request that are in-flight (will be increased
 *             automatically if necessary).
 * - "nbthreads": an argument of type size_t; the number of threads to launch
 *                for each request.
 * Returns 0 if successful; an error code otherwise.
 */
1497
int aml_dma_linux_par_create(struct aml_dma **, ...);
1498 1499
/*
 * Initializes a new parallel DMA.  This is a varargs-variant of the
1500
 * aml_dma_linux_par_vinit() routine.
1501 1502 1503 1504
 * "dma": an allocated DMA structure.
 * Variadic arguments: see aml_dma_linux_par_create().
 * Returns 0 if successful; an error code otherwise.
 */
1505
int aml_dma_linux_par_init(struct aml_dma *, ...);
1506 1507 1508 1509 1510 1511
/*
 * Initializes a new parallel DMA.
 * "dma": an allocated DMA structure.
 * "args": see the variadic arguments of aml_dma_linux_par_create().
 * Returns 0 if successful; an error code otherwise.
 */
1512
int aml_dma_linux_par_vinit(struct aml_dma *, va_list);
1513 1514
/*
 * Tears down an initialized parallel DMA.
1515
 * "dma": an initialized DMA structure.
1516 1517
 * Returns 0 if successful; an error code otherwise.
 */
1518 1519
int aml_dma_linux_par_destroy(struct aml_dma *);

1520 1521
/*******************************************************************************
 * Scratchpad:
1522
 * Use an area to stage data from another area in and out.
1523 1524 1525 1526 1527 1528
 * A dma handles the movement itself.
 ******************************************************************************/

struct aml_scratch_request;
struct aml_scratch_data;

1529 1530
/* Internal macros used for tracking scratchpad request types.  */
/* Invalid request type.  Used for marking inactive requests in the vector.  */
1531
#define AML_SCRATCH_REQUEST_TYPE_INVALID -1
1532
/* Push from the scratchpad to regular memory.  */
1533
#define AML_SCRATCH_REQUEST_TYPE_PUSH 0
1534
/* Pull from regular memory to the scratchpad.  */
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
#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);
1545
	void *(*baseptr)(const struct aml_scratch_data *scratch);
1546
	int (*release)(struct aml_scratch_data *scratch, int scratchid);
1547 1548 1549 1550 1551 1552 1553
};

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

1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
/*
 * Requests a synchronous pull from regular memory to the scratchpad.
 * "scratch": an initialized scratchpad structure.
 * Variadic arguments:
 * - "scratchptr": an argument of type void*; the scratchpad base pointer (see
 *                 aml_scratch_baseptr()).
 * - "scratchid": an argument of type int*; gets filled with the scratch tile
 *                identifier where the data will be pulled into.
 * - "srcptr": an argument of type void*; the start address of the complete
 *             source user data structure.
 * - "srcid": an argument of type int; the source tile identifier.
 * Returns 0 if successful; an error code otherwise.
 */
1567
int aml_scratch_pull(struct aml_scratch *scratch, ...);
1568 1569 1570 1571 1572 1573 1574 1575 1576
/*
 * Requests a pull from regular memory to the scratchpad.  This is an
 * asynchronous version of aml_scratch_pull().
 * "scratch": an initialized scratchpad structure.
 * "req": an address where the pointer to the newly assigned scratch request
 *        will be stored.
 * Variadic arguments: see aml_scratch_pull().
 * Returns 0 if successful; an error code otherwise.
 */
1577 1578
int aml_scratch_async_pull(struct aml_scratch *scratch,
			   struct aml_scratch_request **req, ...);
1579 1580 1581 1582
/*
 * Requests a synchronous push from the scratchpad to regular memory.
 * "scratch": an initialized scratchpad structure.
 * Variadic arguments:
1583 1584 1585
 * - "dstptr": an argument of type void*; the start address of the complete
 *             destination user data structure.
 * - "dstid": an argument of type int*; gets filled with the destination tile
1586 1587 1588 1589 1590 1591 1592
 *            identifier where the data will be pushed into (and where it was
 *            pulled from in the first place).
 * - "scratchptr": an argument of type void*; the scratchpad base pointer (see
 *                 aml_scratch_baseptr()).
 * - "scratchid": an argument of type int; the scratchpad tile identifier.
 * Returns 0 if successful; an error code otherwise.
 */
1593
int aml_scratch_push(struct aml_scratch *scratch, ...);
1594 1595 1596 1597 1598 1599 1600 1601 1602
/*
 * Requests a push from the scratchpad to regular memory.  This is an
 * asynchronous version of aml_scratch_push().
 * "scratch": an initialized scratchpad structure.
 * "req": an address where the pointer to the newly assigned scratch request
 *        will be stored.
 * Variadic arguments: see aml_scratch_push().
 * Returns 0 if successful; an error code otherwise.
 */
1603 1604
int aml_scratch_async_push(struct aml_scratch *scratch,
			   struct aml_scratch_request **req, ...);
1605 1606 1607 1608 1609 1610
/*
 * Waits for an asynchronous scratch request to complete.
 * "scratch": an initialized scratchpad structure.
 * "req": a scratch request obtained using aml_scratch_async_*() calls.
 * Returns 0 if successful; an error code otherwise.
 */
1611 1612
int aml_scratch_wait(struct aml_scratch *scratch,
		     struct aml_scratch_request *req);
1613

1614 1615
/*
 * Tears down an asynchronous scratch request before it completes.
1616
 * "scratch": an initialized scratchpad structure.
1617 1618 1619
 * "req": a scratch request obtained using aml_scratch_async_*() calls.
 * Returns 0 if successful; an error code otherwise.
 */
1620 1621
int aml_scratch_cancel(struct aml_scratch *scratch,
		       struct aml_scratch_request *req);
1622 1623
/*
 * Provides the location of the scratchpad.