summaryrefslogtreecommitdiff
path: root/gcip-kernel-driver/drivers/gcip/gcip-alloc-helper.c
blob: 33c95e2e9db7a43ea2fa49d9eec8c6eecf756d2c (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-License-Identifier: GPL-2.0
/*
 * GCIP helpers for allocating memories.
 *
 * Copyright (C) 2022 Google LLC
 */

#include <asm/page.h>
#include <linux/device.h>
#include <linux/mm_types.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

#include <gcip/gcip-alloc-helper.h>

/*
 * Set @pages to the pages @mem represents.
 * @mem must be a pointer returned by vmalloc.
 *
 * Returns 0 on success, -ENOMEM when any page is NULL.
 */
static int gcip_vmalloc_to_pages(void *mem, size_t count, struct page **pages)
{
	size_t i = 0;

	while (count--) {
		pages[i] = vmalloc_to_page(mem);
		if (!pages[i])
			return -ENOMEM;
		i++;
		mem += PAGE_SIZE;
	}
	return 0;
}

struct sg_table *gcip_alloc_noncontiguous(struct device *dev, size_t size, gfp_t gfp)
{
	struct gcip_sgt_handle *sh = kmalloc(sizeof(*sh), gfp);
	void *mem;
	struct page **pages;
	size_t count;
	int ret;

	if (!sh)
		return NULL;

	size = PAGE_ALIGN(size);
	count = size >> PAGE_SHIFT;
	mem = vzalloc_node(size, dev_to_node(dev));
	if (!mem) {
		dev_err(dev, "GCIP noncontiguous alloc size=%#zx failed", size);
		goto err_free_sh;
	}

	pages = kmalloc_array(count, sizeof(*pages), gfp);
	if (!pages) {
		dev_err(dev, "GCIP alloc pages array count=%zu failed", count);
		goto err_free_mem;
	}

	if (gcip_vmalloc_to_pages(mem, count, pages)) {
		dev_err(dev, "convert memory to pages failed");
		goto err_free_pages;
	}

	ret = sg_alloc_table_from_pages(&sh->sgt, pages, count, 0, size, gfp);
	if (ret) {
		dev_err(dev, "alloc SG table with size=%#zx failed: %d", size, ret);
		goto err_free_pages;
	}

	kfree(pages);
	sh->mem = mem;
	return &sh->sgt;

err_free_pages:
	kfree(pages);
err_free_mem:
	vfree(mem);
err_free_sh:
	kfree(sh);
	return NULL;
}

void gcip_free_noncontiguous(struct sg_table *sgt)
{
	struct gcip_sgt_handle *sh = container_of(sgt, struct gcip_sgt_handle, sgt);

	sg_free_table(&sh->sgt);
	vfree(sh->mem);
	kfree(sh);
}