summaryrefslogtreecommitdiff
path: root/gcip-kernel-driver/include/gcip/gcip-mem-pool.h
blob: c77030079c396e912bc87d03d2a2b9328aa9a96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * A simple memory allocator to help allocating reserved memory pools.
 *
 * Copyright (C) 2022 Google LLC
 */

#ifndef __GCIP_MEM_POOL_H__
#define __GCIP_MEM_POOL_H__

#include <linux/device.h>
#include <linux/genalloc.h>
#include <linux/types.h>

struct gcip_mem_pool {
	struct device *dev;
	struct gen_pool *gen_pool;
	phys_addr_t base_paddr;
	size_t granule;
};

/*
 * Initializes the memory pool object.
 *
 * @pool: The memory pool object to be initialized.
 * @dev: Used for logging only.
 * @base_paddr: The base physical address of the pool. Must be greater than 0 and a multiple of
 *              @granule.
 * @size: The size of the pool. @size should be a multiple of @granule.
 * @granule: The granule when invoking the allocator. Should be a power of 2.
 *
 * Returns 0 on success, a negative errno otherwise.
 *
 * Call gcip_mem_pool_exit() to release the resources of @pool.
 */
int gcip_mem_pool_init(struct gcip_mem_pool *pool, struct device *dev, phys_addr_t base_paddr,
		       size_t size, size_t granule);
/*
 * Releases resources of @pool.
 *
 * Note: you must release (by calling gcip_mem_pool_free) all allocations before calling this
 * function.
 */
void gcip_mem_pool_exit(struct gcip_mem_pool *pool);

/*
 * Allocates and returns the allocated physical address.
 *
 * @size: Size to be allocated.
 *
 * Returns the allocated address. Returns 0 on allocation failure.
 */
phys_addr_t gcip_mem_pool_alloc(struct gcip_mem_pool *pool, size_t size);
/*
 * Returns the address previously allocated by gcip_mem_pool_alloc().
 *
 * The size and address must match what previously passed to / returned by gcip_mem_pool_alloc().
 */
void gcip_mem_pool_free(struct gcip_mem_pool *pool, phys_addr_t paddr, size_t size);

/*
 * Returns the offset between @paddr and @base_paddr passed to gcip_mem_pool_init().
 *
 * @paddr must be a value returned by gcip_mem_pool_alloc().
 */
static inline size_t gcip_mem_pool_offset(struct gcip_mem_pool *pool, phys_addr_t paddr)
{
	return paddr - pool->base_paddr;
}

#endif /* __GCIP_MEM_POOL_H__ */