summaryrefslogtreecommitdiff
path: root/gcip-kernel-driver/include/gcip/gcip-alloc-helper.h
blob: 3d2c11005a513339c5aa6988b0d5af3040611627 (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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * GCIP helpers for allocating memories.
 *
 * Copyright (C) 2022 Google LLC
 */

#ifndef __GCIP_ALLOC_HELPER_H__
#define __GCIP_ALLOC_HELPER_H__

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

/*
 * The actual return value from the alloc_noncontiguous function.
 * The user should only care about @sgt. @pages is used internally for freeing memory.
 */
struct gcip_sgt_handle {
	struct sg_table sgt;
	void *mem;
};

/*
 * Allocates non-contiguous memory with size @size bytes.
 *
 * @dev: pointer to device structure. Is used for logging or the NUMA node for page allocation.
 * @size: Total size in bytes. Will be page aligned.
 * @gfp: The GFP flag for malloc internal structures.
 *
 * Returns the SG table represents the non-contiguous region.
 * Returns NULL on any error.
 */
struct sg_table *gcip_alloc_noncontiguous(struct device *dev, size_t size, gfp_t gfp);
/* Frees the memory allocated by gcip_alloc_noncontiguous. */
void gcip_free_noncontiguous(struct sg_table *sgt);

/*
 * Returns the virtual memory that was used to allocate @sgt.
 *
 * @sgt must be the return pointer of gcip_alloc_noncontiguous.
 */
static inline void *gcip_noncontiguous_sgt_to_mem(struct sg_table *sgt)
{
	struct gcip_sgt_handle *sh = container_of(sgt, struct gcip_sgt_handle, sgt);

	return sh->mem;
}

#endif /* __GCIP_ALLOC_HELPER_H__ */