aml.h 25.8 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the AML project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/aml
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/

11 12 13 14 15 16 17
/**
 * \file aml.h
 *
 * \brief Main AML header file, contains all high level
 * abstractions declarations.
 **/

18 19 20
#ifndef AML_H
#define AML_H 1

21
#include <assert.h>
22 23 24 25 26 27 28
#include <inttypes.h>
#include <numa.h>
#include <numaif.h>
#include <pthread.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
29
#include <stdlib.h>
30 31 32
#include <sys/mman.h>
#include <unistd.h>

33
#include "aml/utils/bitmap.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
34
#include "aml/utils/error.h"
35
#include "aml/utils/vector.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
36
#include "aml/utils/version.h"
37

38
////////////////////////////////////////////////////////////////////////////////
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/**
 * @}
 * @defgroup aml "AML Library functions"
 * @brief Initialize/Finalize library.
 *
 * General functions of aml library.
 * Initialization of internal structures, cleanup of everything at the end.
 *
 * @see aml_error
 * @see aml_version
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
56
 * Initializes the library.
57 58 59 60 61 62
 * @param argc: pointer to the main()'s argc argument; contents can get
 *        modified.
 * @param argv: pointer to the main()'s argv argument; contents can get
 *        modified.
 * @return 0 if successful; an error code otherwise.
 **/
63
int aml_init(int *argc, char **argv[]);
64 65

/**
66
 * Terminates the library.
67 68
 * @return 0 if successful; an error code otherwise.
 **/
69
int aml_finalize(void);
70

71
////////////////////////////////////////////////////////////////////////////////
72

Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
73
/**
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 * @}
 * @defgroup aml_area "AML Area"
 * @brief Area High-Level API
 *
 * AML areas represent places where data can belong.
 * In shared memory systems, locality is a major concern for performance.
 * Beeing able to query memory from specific places is of a major interest
 * two achieve this goal. AML Areas provide mmap / munmap low level functions
 * to query memory from specific places materialized as areas. Available area
 * implementations dictate the way such places can be arranged and with which
 * properties. It is important to notice that function provided through Area API
 * are low-level functions and are not optimized for performance as allocator
 * are.
 *
 * @image html area.png "Illustration of areas on a copmlex system." width=700cm
 *
 * @see aml_area_linux
 *
 * @{
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
93 94
 **/

95 96 97 98 99 100
////////////////////////////////////////////////////////////////////////////////

/**
 * aml_area_data is an opaque handle defined by each aml_area
 * implementation. This not supposed to be used by end users.
 **/
101 102
struct aml_area_data;

103 104 105 106 107 108
/**
 * aml_area_ops is a structure containing implementations
 * of an area operations.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 **/
109 110
struct aml_area_ops {
	/**
111
	 * Building block for coarse grain allocator of virtual memory.
112
	 *
113 114 115
	 * @param data: Opaque handle to implementation specific data.
	 * @param ptr: A virtual address to be used by underlying
	 *        implementation.
116
	 *        Can be NULL.
117 118 119 120 121 122 123
	 * @param size: The minimum size of allocation.
	 *        Is greater than 0. Must not fail unless not enough
	 *        memory is available, or ptr argument does not point to a
	 *        suitable address.
	 *        In case of failure, aml_errno must be set to an appropriate
	 *        value.
	 * @return a pointer to allocated memory object.
124
	 **/
125
	void* (*mmap)(const struct aml_area_data  *data,
126 127 128 129
		      void                        *ptr,
		      size_t                       size);

	/**
130 131
	 * Building block for unmapping of virtual memory mapped with mmap()
	 * of the same area.
132
	 *
133 134 135 136 137 138
	 * @param data: An opaque handle to implementation specific data.
	 * @param ptr: Pointer to data mapped in physical memory. Cannot be
	 *        NULL.
	 * @param size: The size of data. Cannot be 0.
	 * @return: AML_AREA_* error code.
	 * @see mmap()
139
	 **/
140
	int (*munmap)(const struct aml_area_data *data,
141 142 143 144
		      void                       *ptr,
		      size_t                      size);
};

145 146 147 148 149 150 151
/**
 * An AML area is an implementation of memory operations for several type
 * of devices through a consistent abstraction.
 * This abstraction is meant to be implemented for several kind of devices,
 * i.e the same function calls allocate different kinds of devices depending
 * on the area implementation provided.
 **/
152
struct aml_area {
153
	/** Basic memory operations implementation **/
154
	struct aml_area_ops *ops;
155
	/** Implementation specific data. Set to NULL at creation. **/
156 157
	struct aml_area_data *data;
};
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
158 159

/**
160 161 162 163 164
 * Low-level function for getting memory from an area.
 * @param area: A valid area implementing access to target memory.
 * @param ptr: Implementation specific argument. See specific header.
 * @param size: The usable size of memory returned.
 * @return virtual memory from this area with at least queried size bytes.
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
165
 **/
166 167 168
void *aml_area_mmap(const struct aml_area *area,
		    void                 **ptr,
		    size_t                 size);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
169 170 171

/**
 * Release data provided with aml_area_mmap() and the same area.
172 173
 * @param area: A valid area implementing access to target memory.
 * @param ptr: A pointer to memory address provided with aml_area_mmap()
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
174
 *        by same area and size.
175 176 177
 * @param size: The size of memory region pointed by "ptr".
 * @return an AML error code on operation success.
 * @see aml_area_mmap()
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
178 179 180 181 182
 **/
int
aml_area_munmap(const struct aml_area *area,
		void                  *ptr,
		size_t                 size);
183

184
////////////////////////////////////////////////////////////////////////////////
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/**
 * @}
 * @defgroup aml_tiling "AML Tiling"
 * @brief Tiling Data Structure High-Level API
 *
 * Tiling is an array representation of data structures.
 * AML tiling structure can be defined as 1D or 2D contiguous arrays.
 * Tiles in tilings can be of custom size and AML provides iterators to
 * easily access tiles element.
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Tiling types passed to some tiling routines.
 * Regular, linear tiling with uniform tile sizes.
 **/
204
#define AML_TILING_TYPE_1D 0
205 206 207 208 209 210

/**
 * Tiling types passed to some tiling routines.
 * 2-dimensional cartesian tiling with uniform tile sizes, stored
 * in rowmajor order
 **/
211
#define AML_TILING_TYPE_2D_ROWMAJOR 1
212 213 214 215 216 217

/**
 * Tiling types passed to some tiling routines.
 * 2-dimensional cartesian tiling with uniform tile sizes, stored
 * in colmajor order
 **/
218 219
#define AML_TILING_TYPE_2D_COLMAJOR 2

220 221 222 223
/**
 * aml_tiling_data is an opaque handle defined by each aml_tiling
 * implementation. This not supposed to be used by end users.
 **/
224
struct aml_tiling_data;
225 226 227 228 229 230

/**
 * aml_area_tiling_iterator_data is an opaque handle defined by each
 * aml_tiling_iterator implementation. This not supposed to be used
 * by end users.
 **/
231 232
struct aml_tiling_iterator_data;

233 234 235 236 237 238
/**
 * aml_tiling_iterator_ops contains the specific operations defined
 * by an aml_tiling_iterator.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 **/
239 240
struct aml_tiling_iterator_ops;

241 242 243 244 245 246
/**
 * \brief aml_tiling_iterator is a structure for iterating over
 * elements of an aml_tiling.
 * \todo Provide a detailed explanation of what is a tiling iterator.
 **/
struct aml_tiling_iterator;
247

248 249 250 251 252 253 254
/**
 * aml_tiling_ops is a structure containing a set of operation
 * over a tiling. These operation are the creation and destruction
 * of iterators, access to tiles indexing, size and tiling dimension.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 **/
255
struct aml_tiling_ops {
256 257 258
	/**
	 * \todo Doc
	 **/
259 260 261
	int (*create_iterator)(struct aml_tiling_data *tiling,
			       struct aml_tiling_iterator **iterator,
			       int flags);
262 263 264
	/**
	 * \todo Doc
	 **/
265 266
	int (*init_iterator)(struct aml_tiling_data *tiling,
			     struct aml_tiling_iterator *iterator, int flags);
267 268 269
	/**
	 * \todo Doc
	 **/
270
	int (*fini_iterator)(struct aml_tiling_data *tiling,
271
				struct aml_tiling_iterator *iterator);
272 273 274
	/**
	 * \todo Doc
	 **/
275 276
	int (*destroy_iterator)(struct aml_tiling_data *tiling,
				struct aml_tiling_iterator **iterator);
277 278 279
	/**
	 * \todo Doc
	 **/
280
	int (*tileid)(const struct aml_tiling_data *tiling, va_list coords);
281 282 283
	/**
	 * \todo Doc
	 **/
Kamil Iskra's avatar
Kamil Iskra committed
284
	size_t (*tilesize)(const struct aml_tiling_data *tiling, int tileid);
285 286 287
	/**
	 * \todo Doc
	 **/
Kamil Iskra's avatar
Kamil Iskra committed
288 289
	void* (*tilestart)(const struct aml_tiling_data *tiling,
			   const void *ptr, int tileid);
290 291 292
	/**
	 * \todo Doc
	 **/
293
	int (*ndims)(const struct aml_tiling_data *tiling, va_list results);
294 295
};

296 297 298 299 300 301 302
/**
 * An aml_tiling is a multi-dimensional grid of data, e.g a matrix, a stencil
 * etc...
 * Tilings are used in AML as a description of a macro data structure that will
 * be used by a library for doing its own work. This structure is exploitable
 * by AML to perform optimized movement operations.
 **/
303
struct aml_tiling {
304
	/** @see aml_tiling_ops **/
305
	struct aml_tiling_ops *ops;
306
	/** @see aml_tiling_data **/
307 308 309
	struct aml_tiling_data *data;
};

310
/**
311
 * Provides the tile id of a tile.
312 313 314 315 316 317
 * @param tiling: an initialized tiling structure.
 * @param coordinates: a list of size_t coordinates, one per dimension of the
 *        tiling.
 * @return -1 in case of invalid coordinates, else the id of the tile
 *         (that is, its order in memory), to use with other functions.
 **/
318 319
int aml_tiling_tileid(const struct aml_tiling *tiling, ...);

320
/**
321
 * Provides the information on the size of a tile.
322 323 324 325 326
 * @param tiling: an initialized tiling structure.
 * @param tileid: an identifier of a tile (a value between 0 and the number
 *        of tiles minus 1).
 * @return the size of a tile.
 **/
Kamil Iskra's avatar
Kamil Iskra committed
327
size_t aml_tiling_tilesize(const struct aml_tiling *tiling, int tileid);
328

329
/**
330
 * Provides the information on the location of a tile in memory.
331 332 333 334 335 336
 * @param tiling: an initialized tiling structure.
 * @param ptr: an address of the start of the complete user data structure
 *        that this tiling describes.
 * @param tileid: an identifier of a tile (a value between 0 and the number
 *        of tiles minus 1).
 * @return the address of the start of the tile identified by "tileid", within
337
 * the provided user data structure.
338 339 340
 **/
void *aml_tiling_tilestart(const struct aml_tiling *tiling,
			   const void *ptr,
Kamil Iskra's avatar
Kamil Iskra committed
341
			   int tileid);
342

343
/**
344
 * Provides the dimensions of the entire tiling in tiles.
345 346 347 348 349
 * @param tiling: an initialized tiling structure.
 * @param sizes: a list of output (size_t *), one per dimension of the tiling.
 *               Will contain the size of each dimension in tiles upon return.
 * @return 0 if successful, an error code otherwise.
 **/
350 351
int aml_tiling_ndims(const struct aml_tiling *tiling, ...);

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
/**
 * \todo Doc
 **/
struct aml_tiling_iterator_ops {
	/**
	 * \todo Doc
	 **/
	int (*reset)(struct aml_tiling_iterator_data *iterator);
	/**
	 * \todo Doc
	 **/
	int (*next)(struct aml_tiling_iterator_data *iterator);
	/**
	 * \todo Doc
	 **/
	int (*end)(const struct aml_tiling_iterator_data *iterator);
	/**
	 * \todo Doc
	 **/
	int (*get)(const struct aml_tiling_iterator_data *iterator,
		   va_list args);
};

/**
 * \todo Doc
 **/
struct aml_tiling_iterator {
	/** @see aml_tiling_iterator_ops **/
	struct aml_tiling_iterator_ops *ops;
	/** @see aml_tiling_iterator_data **/
	struct aml_tiling_iterator_data *data;
};

/**
386
 * Allocates and initializes a new tiling iterator.
387 388 389 390 391 392
 * @param tiling: an initialized tiling structure.
 * @param iterator: an address where the pointer to the newly allocated iterator
 *        structure will be stored.
 * @param flags: reserved for future use; pass 0 for now.
 * @return 0 if successful; an error code otherwise.
 **/
393 394 395
int aml_tiling_create_iterator(struct aml_tiling *tiling,
			       struct aml_tiling_iterator **iterator,
			       int flags);
396
/**
397
 * Initializes a tiling iterator.
398 399 400 401 402
 * @param tiling: an initialized tiling structure.
 * @param iterator: an allocated tiling iterator structure.
 * @param flags: reserved for future use; pass 0 for now.
 * @return 0 if successful; an error code otherwise.
 **/
403 404
int aml_tiling_init_iterator(struct aml_tiling *tiling,
			     struct aml_tiling_iterator *iterator, int flags);
405

406
/**
407
 * Finalize an initialized tiling iterator.
408 409 410
 * @param tiling: an initialized tiling structure.
 * @param iterator: an initialized tiling iterator structure.
 **/
411 412
void aml_tiling_fini_iterator(struct aml_tiling *tiling,
			      struct aml_tiling_iterator *iterator);
413
/**
414
 * Tears down an initialized tiling iterator.
415 416 417 418
 * @param tiling: an initialized tiling structure.
 * @param iterator: an initialized tiling iterator structure.
 * @return 0 if successful; an error code otherwise.
 **/
419 420
void aml_tiling_destroy_iterator(struct aml_tiling *tiling,
				struct aml_tiling_iterator **iterator);
421 422


423
/**
424
 * Resets a tiling iterator to the first tile.
425 426 427
 * @param iterator: an initialized tiling iterator structure.
 * @return 0 if successful; an error code otherwise.
 **/
428
int aml_tiling_iterator_reset(struct aml_tiling_iterator *iterator);
429 430

/**
431
 * Advances a tiling iterator to the next tile.
432 433 434
 * @param iterator: an initialized tiling iterator structure.
 * @return 0 if successful; an error code otherwise.
 **/
435
int aml_tiling_iterator_next(struct aml_tiling_iterator *iterator);
436 437

/**
438
 * Checks whether the iterator is past the last tile.
439 440
 * @param iterator: an initialized tiling iterator structure.
 * @return 0 if the iterator points at a valid tile; 1 if it's past the last
441
 * tile.
442
 **/
Kamil Iskra's avatar
Kamil Iskra committed
443
int aml_tiling_iterator_end(const struct aml_tiling_iterator *iterator);
444 445

/**
446
 * Queries the iterator.
447 448
 * @param iterator: an initialized tiling iterator structure.
 * @param x: an argument of type unsigned long*; on return gets filled with the
449
 *        identifier of the tile currently pointed to.
450 451
 * @return 0 if successful; an error code otherwise.
 **/
Kamil Iskra's avatar
Kamil Iskra committed
452
int aml_tiling_iterator_get(const struct aml_tiling_iterator *iterator, ...);
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
////////////////////////////////////////////////////////////////////////////////

/**
 * @}
 * @defgroup aml_dma "AML DMA"
 * @brief Management of low-level memory movements.
 *
 * AML DMA is the abstraction for handling memory movements.
 * AML DMA can asynchronously move data from one area to another.
 * While performing a movement, DMA operation
 * may also translates from a source tiling to a different
 * destination tiling.
 *
 * @image html dma.png width=600
 * @{
 **/
470

471 472 473 474 475 476
////////////////////////////////////////////////////////////////////////////////

/**
 * Internal macros used for tracking DMA request types.
 * Invalid request type.  Used for marking inactive requests in the vector.
 **/
477
#define AML_DMA_REQUEST_TYPE_INVALID -1
478 479 480 481 482

/**
 * Internal macros used for tracking DMA request types.
 * Copy request type.  Uses memcpy() for data migration.
 **/
483 484
#define AML_DMA_REQUEST_TYPE_COPY 0

485 486 487 488 489 490 491
/**
 * aml_dma is mainly used to asynchronously move data.
 * aml_dma_request is an opaque structure containing information
 * about ongoing request for data movement in a dma operation.
 * @see aml_dma_ops
 * @see aml_dma_async_copy()
 **/
492
struct aml_dma_request;
493 494 495 496 497

/**
 * Opaque handle implemented by each aml_dma implementations.
 * Should not be used by end-users.
 **/
498 499
struct aml_dma_data;

500 501 502 503 504 505 506 507 508 509 510 511
/**
 aml_dma_ops is a structure containing operations for a specific
 * aml_dma implementation.
 * These operation are operation are detailed in the structure.
 * They are specific in:
 * - the type of aml_area source and destination,
 * - the progress engine performing the operation,
 * - the type of of source and destination data structures.
 *
 * Each different combination of these three points may require a different
 * set of dma operations.
 **/
512
struct aml_dma_ops {
513 514 515 516 517 518 519 520 521 522 523
	/**
	 * Initiate a data movement, from a source pointer to a destination
	 * pointer, and output a request handler for managing the transfer.
	 * @param dma: dma_implementation internal data.
	 * @param req: Output the request handle to manage termination
	 *        of the movement.
	 * @param type: A valid AML_DMA_REQUEST_TYPE_* specifying the kind
	 *        of operation to perform.
	 * @param args: list of variadic arguments provided to aml_dma_copy()
	 * @return an AML error code.
	 **/
524
	int (*create_request)(struct aml_dma_data *dma,
525
			      struct aml_dma_request **req, int type,
526
			      va_list args);
527 528 529 530 531 532 533 534 535

	/**
	 * Destroy the request handle. If the data movement is still ongoing,
	 * then cancel it.
	 *
	 * @param dma: dma_implementation internal data.
	 * @param req: the request handle to manage termination of the movement.
	 * @return an AML error code.
	 **/
536 537
	int (*destroy_request)(struct aml_dma_data *dma,
			       struct aml_dma_request *req);
538 539 540 541 542 543 544 545 546

	/**
	 * Wait for termination of a data movement and destroy the request
	 * handle.
	 *
	 * @param dma: dma_implementation internal data.
	 * @param req: the request handle to manage termination of the movement.
	 * @return an AML error code.
	 **/
547 548
	int (*wait_request)(struct aml_dma_data *dma,
			    struct aml_dma_request *req);
549 550
};

551 552 553 554 555 556 557
/**
 * aml_dma is an abstraction for (asynchronously) moving data
 * from one area to another. The implementation of dma to use
 * is depends on the source and destination areas. The appropriate
 * dma choice is delegated to the user.
 * @see struct aml_area.
 **/
558
struct aml_dma {
559
	/** @see aml_dma_ops **/
560
	struct aml_dma_ops *ops;
561
	/** @see aml_dma_data **/
562 563 564
	struct aml_dma_data *data;
};

565
/**
566 567
 * Requests a synchronous data copy between two different tiles, using
 * memcpy() or equivalent.
568 569 570 571 572 573 574 575 576 577 578 579 580
 * @param dma: an initialized DMA structure.
 * @param dt: an argument of type struct aml_tiling*; the destination tiling
 *        structure.
 * @param dptr: an argument of type void*; the start address of the complete
 *        destination user data structure.
 * @param dtid: an argument of type int; the destination tile identifier.
 * @param st: an argument of type struct aml_tiling*; the source tiling
 *        structure.
 * @param sptr: an argument of type void*; the start address of the
 *        complete source user data structure.
 * @param stid: an argument of type int; the source tile identifier.
 * @return 0 if successful; an error code otherwise.
 **/
581
int aml_dma_copy(struct aml_dma *dma, ...);
582 583

/**
584 585
 * Requests a data copy between two different tiles.  This is an asynchronous
 * version of aml_dma_copy().
586 587 588
 * @param dma: an initialized DMA structure.
 * @param req: an address where the pointer to the newly assigned DMA request
 *        will be stored.
589
 * Variadic arguments: see aml_dma_copy().
590 591
 * @return 0 if successful; an error code otherwise.
 **/
592
int aml_dma_async_copy(struct aml_dma *dma, struct aml_dma_request **req, ...);
593 594

/**
595
 * Waits for an asynchronous DMA request to complete.
596 597 598 599
 * @param dma: an initialized DMA structure.
 * @param req: a DMA request obtained using aml_dma_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
600
int aml_dma_wait(struct aml_dma *dma, struct aml_dma_request *req);
601 602

/**
603
 * Tears down an asynchronous DMA request before it completes.
604 605 606 607
 * @param dma: an initialized DMA structure.
 * @param req: a DMA request obtained using aml_dma_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
608
int aml_dma_cancel(struct aml_dma *dma, struct aml_dma_request *req);
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
/**
 * @}
 * @defgroup aml_scratch "AML Scratchpad"
 * @brief Stage-in, Stage-out High Level Abstraction.
 *
 * Scratchpad in an abstraction fro moving data back and forth from
 * a data representation in an area to another data representation in another
 * areas. This is especially usefull from moving to user data representation
 * to an architecure optimized representation for heavy computational work,
 * then returning the to user representation.
 * Data movement is performed with two dma engines from one area and tiling to
 * another area and tiling.
 *
 * @image html scratch.png width=600
 * @see aml_dma
 * @{
 **/

////////////////////////////////////////////////////////////////////////////////

/**
 * Scratch is mainly used to asynchronously move data back and forth between
 * two areas. aml_scratch_request is an opaque structure containing information
 * about ongoing requests for data movement.
 **/
637
struct aml_scratch_request;
638 639 640 641 642

/**
 * Opaque handle implemented by each scratches implementation.
 * Should not be used by end users.
 **/
643 644
struct aml_scratch_data;

645 646 647 648
/**
 * Scratchpad request types.
 * Invalid request type.  Used for marking inactive requests in the vector.
 **/
649
#define AML_SCRATCH_REQUEST_TYPE_INVALID -1
650 651 652 653 654

/**
 * Scratchpad request types.
 * Push from the scratchpad to regular memory.
 **/
655
#define AML_SCRATCH_REQUEST_TYPE_PUSH 0
656 657 658 659 660

/**
 * Scratchpad request types.
 * Pull from regular memory to the scratchpad.
 **/
661
#define AML_SCRATCH_REQUEST_TYPE_PULL 1
662 663 664 665 666

/**
 * Scratchpad request types.
 * No-op/empty request
 **/
667
#define AML_SCRATCH_REQUEST_TYPE_NOOP 2
668

669 670 671 672 673 674 675 676 677
/**
 * aml_scratch_ops contain a scratch implementation specific operations.
 * These operations implementation may vary depending on the source and
 * destination of data, and thus scratch implementations use different
 * operations.
 * Aware users may create or modify implementation by assembling
 * appropriate operations in such a structure.
 * @see struct aml_scratch
 **/
678
struct aml_scratch_ops {
679 680 681
	/**
	 * \todo Doc
	 **/
682 683 684
	int (*create_request)(struct aml_scratch_data *scratch,
			      struct aml_scratch_request **req, int type,
			      va_list args);
685 686 687
	/**
	 * \todo Doc
	 **/
688 689
	int (*destroy_request)(struct aml_scratch_data *scratch,
			       struct aml_scratch_request *req);
690 691 692
	/**
	 * \todo Doc
	 **/
693 694
	int (*wait_request)(struct aml_scratch_data *scratch,
			    struct aml_scratch_request *req);
695 696 697
	/**
	 * \todo Doc
	 **/
698
	void *(*baseptr)(const struct aml_scratch_data *scratch);
699 700 701
	/**
	 * \todo Doc
	 **/
702
	int (*release)(struct aml_scratch_data *scratch, int scratchid);
703 704
};

705 706 707 708 709 710
/**
 * An aml_scratch is abstraction aimed toward temporary use of a data structures
 * in a different area than the one where data currently resides. Scratches in
 * AML take care of asynchornously allocating and moving the data back and forth
 * between areas.
 **/
711
struct aml_scratch {
712
	/** @see aml_scratch_ops **/
713
	struct aml_scratch_ops *ops;
714
	/** @see aml_scratch_data **/
715 716 717
	struct aml_scratch_data *data;
};

718
/**
719
 * Requests a synchronous pull from regular memory to the scratchpad.
720 721 722 723 724
 * @param scratch: an initialized scratchpad structure.
 * @param scratchptr: an argument of type void*; the scratchpad base pointer.
 * @param scratchid: an argument of type int*; gets filled with the scratch tile
 *        identifier where the data will be pulled into.
 * @param srcptr: an argument of type void*; the start address of the complete
725
 *             source user data structure.
726 727 728 729
 * @param srcid: an argument of type int; the source tile identifier.
 * @see aml_scratch_baseptr()
 * @return 0 if successful; an error code otherwise.
 **/
730
int aml_scratch_pull(struct aml_scratch *scratch, ...);
731 732 733

/**
 * Requests a pull from regular memory to the scratchpad. This is an
734
 * asynchronous version of aml_scratch_pull().
735 736 737 738 739 740 741
 * @param scratch: an initialized scratchpad structure.
 * @param req: an address where the pointer to the newly assigned scratch
 *        request will be stored.
 * @param variadic arguments: see aml_scratch_pull().
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_pull()
 **/
742 743
int aml_scratch_async_pull(struct aml_scratch *scratch,
			   struct aml_scratch_request **req, ...);
744
/**
745
 * Requests a synchronous push from the scratchpad to regular memory.
746 747 748 749 750 751 752 753 754 755 756
 * @param scratch: an initialized scratchpad structure.
 * @param dstptr: an argument of type void*; the start address of the complete
 *        destination user data structure.
 * @param dstid: an argument of type int*; gets filled with the destination tile
 *        identifier where the data will be pushed into (and where it was
 *        pulled from in the first place).
 * @param scratchptr: an argument of type void*; the scratchpad base pointer.
 * @param scratchid: an argument of type int; the scratchpad tile identifier.
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_baseptr()
 **/
757
int aml_scratch_push(struct aml_scratch *scratch, ...);
758 759

/**
760 761
 * Requests a push from the scratchpad to regular memory.  This is an
 * asynchronous version of aml_scratch_push().
762 763 764
 * @param scratch: an initialized scratchpad structure.
 * @param req: an address where the pointer to the newly assigned scratch
 *        request will be stored.
765
 * Variadic arguments: see aml_scratch_push().
766 767 768
 * @return 0 if successful; an error code otherwise.
 * @see aml_scratch_push()
 **/
769 770
int aml_scratch_async_push(struct aml_scratch *scratch,
			   struct aml_scratch_request **req, ...);
771
/**
772
 * Waits for an asynchronous scratch request to complete.
773 774 775 776
 * @param scratch: an initialized scratchpad structure.
 * @param req: a scratch request obtained using aml_scratch_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
777 778
int aml_scratch_wait(struct aml_scratch *scratch,
		     struct aml_scratch_request *req);
779

780
/**
781
 * Tears down an asynchronous scratch request before it completes.
782 783 784 785
 * @param scratch: an initialized scratchpad structure.
 * @param req: a scratch request obtained using aml_scratch_async_*() calls.
 * @return 0 if successful; an error code otherwise.
 **/
786 787
int aml_scratch_cancel(struct aml_scratch *scratch,
		       struct aml_scratch_request *req);
788
/**
789
 * Provides the location of the scratchpad.
790 791 792
 * @param scratch: an initialized scratchpad structure.
 * @return a base pointer to the scratchpad memory buffer.
 **/
793
void *aml_scratch_baseptr(const struct aml_scratch *scratch);
794

795
/**
796
 * Release a scratch tile for immediate reuse.
797 798 799 800
 * @param scratch: an initialized scratchpad structure.
 * @param scratchid: a scratchpad tile identifier.
 * @return 0 if successful; an error code otherwise.
 **/
801 802
int aml_scratch_release(struct aml_scratch *scratch, int scratchid);

803 804 805 806 807 808
////////////////////////////////////////////////////////////////////////////////

/**
 * @}
 **/

809
#endif