aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/android/ion/hisilicon/hisi_ion.c
blob: 20f68e573cf330c1634dd5c49719a3d66bf0d59f (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#define pr_fmt(fmt) "ion: " fmt

#include <linux/module.h>
#include <linux/err.h>
#include <linux/hisi_ion.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/of.h>
#include "../ion_priv.h"

struct hisi_ion_name_id_table {
	const char *name;
	unsigned int id;
};

static struct hisi_ion_name_id_table name_id_table[] __initdata = {
	{"fb", ION_FB_HEAP_ID},
	{"vpu", ION_VPU_HEAP_ID},
	{"jpu", ION_JPU_HEAP_ID},
	{"gralloc-carveout", ION_GRALLOC_HEAP_ID},
	{"overlay", ION_OVERLAY_HEAP_ID},
	{"sys_user", ION_SYSTEM_HEAP_ID},
	{"sys_contig", ION_SYSTEM_CONTIG_HEAP_ID},
};

struct hisi_ion_type_id_table {
	const char *name;
	enum ion_heap_type type;
};

static struct hisi_ion_type_id_table type_id_table[] = {
	{"ion_system_contig", ION_HEAP_TYPE_SYSTEM_CONTIG},
	{"ion_system", ION_HEAP_TYPE_SYSTEM},
	{"ion_carveout", ION_HEAP_TYPE_CARVEOUT},
	{"ion_chunk", ION_HEAP_TYPE_CHUNK},
	{"ion_dma", ION_HEAP_TYPE_DMA},
	{"ion_custom", ION_HEAP_TYPE_CUSTOM},
};

#define HISI_ION_HEAP_NUM 16

static struct ion_platform_data hisi_ion_platform_data = {0};
static struct ion_platform_heap hisi_ion_platform_heap[HISI_ION_HEAP_NUM] = {{0} };

static struct ion_device *hisi_ion_device;
static struct ion_heap *hisi_ion_heap[HISI_ION_HEAP_NUM] = {NULL};

int hisi_ion_get_heap_info(unsigned int id, struct ion_heap_info_data *data)
{
	int i;

	BUG_ON(!data);

	for (i = 0; i < hisi_ion_platform_data.nr; i++) {
		if (hisi_ion_platform_heap[i].id == id) {
			data->heap_phy  = hisi_ion_platform_heap[i].base;
			data->heap_size = hisi_ion_platform_heap[i].size;
			strncpy((void *)data->name, (void *)hisi_ion_platform_heap[i].name, HISI_ION_NAME_LEN);
			pr_info("heap info : id %d name %s phy 0x%llx size %u\n",
					id, data->name, data->heap_phy, data->heap_size);
			return 0;
		}
	}
	pr_err("in %s please check the id %d\n", __func__, id);

	return -EINVAL;
}
EXPORT_SYMBOL(hisi_ion_get_heap_info);

struct ion_device *get_ion_device(void)
{
	return hisi_ion_device;
}
EXPORT_SYMBOL(get_ion_device);

static int __init get_id_by_name(const char *name, unsigned int *id)
{
	int i, n;

	n = sizeof(name_id_table)/sizeof(name_id_table[0]);
	for (i = 0; i < n; i++) {
		if (strncmp(name, name_id_table[i].name, HISI_ION_NAME_LEN))
			continue;

		*id = name_id_table[i].id;
		return 0;
	}
	return -1;
}

static int __init get_type_by_name(const char *name, enum ion_heap_type *type)
{
	int i, n;

	n = sizeof(type_id_table)/sizeof(type_id_table[0]);
	for (i = 0; i < n; i++) {
		if (strncmp(name, type_id_table[i].name, HISI_ION_NAME_LEN))
			continue;

		*type = type_id_table[i].type;
		return 0;
	}

	return -1;
}

static int __init hisi_ion_setup_platform_data(struct platform_device *dev)
{
	struct device_node *node, *np;
	const char *heap_name;
	const char *type_name;
	unsigned int id;
	unsigned int range[2] = {0, 0};
	enum ion_heap_type type;
	int ret;
	int index = 0;

	node = dev->dev.of_node;
	for_each_child_of_node(node, np) {
		ret = of_property_read_string(np, "heap-name", &heap_name);
		if (ret < 0) {
			pr_err("in node %s please check the name property of node %s\n", __func__, np->name);
			continue;
		}

		ret = get_id_by_name(heap_name, &id);
		if (ret < 0) {
			pr_err("in node %s please check the name %s\n", __func__, heap_name);
			continue;
		}

		ret = of_property_read_u32_array(np, "heap-range", range, ARRAY_SIZE(range));
		if (ret < 0) {
			pr_err("in node %s please check the range property of node %s\n", __func__, np->name);
			continue;
		}


		ret = of_property_read_string(np, "heap-type", &type_name);
		if (ret < 0) {
			pr_err("in node %s please check the type property of node %s\n", __func__, np->name);
			continue;
		}

		ret = get_type_by_name(type_name, &type);
		if (ret < 0) {
			pr_err("in node %s please check the type %s\n", __func__, type_name);
			continue;
		}

		hisi_ion_platform_heap[index].name = heap_name;
		hisi_ion_platform_heap[index].base = range[0];
		hisi_ion_platform_heap[index].size = range[1];
		hisi_ion_platform_heap[index].id = id;
		hisi_ion_platform_heap[index].type = type;

		index++;
	}

	hisi_ion_platform_data.nr = index;
	hisi_ion_platform_data.heaps = hisi_ion_platform_heap;

	return 0;
}

static int __init hisi_ion_probe(struct platform_device *pdev)
{
	int i, err;
	struct ion_heap *heap;
	struct ion_platform_heap *heap_data;

	if (hisi_ion_setup_platform_data(pdev)) {
		pr_err("hisi_ion_setup_platform_data is failed\n");
		return -EINVAL;
	}

	hisi_ion_device = ion_device_create(NULL);
	if (IS_ERR_OR_NULL(hisi_ion_device))
		return PTR_ERR(hisi_ion_device);
	/*
	 * create the heaps as specified in the board file
	 */
	for (i = 0; i < hisi_ion_platform_data.nr; i++) {
		heap_data = &hisi_ion_platform_data.heaps[i];
		heap = ion_heap_create(heap_data);
		if (IS_ERR_OR_NULL(heap)) {
			err = PTR_ERR(heap);
			goto out;
		}

		ion_device_add_heap(hisi_ion_device, heap);
		hisi_ion_heap[i] = heap;
	}
	platform_set_drvdata(pdev, hisi_ion_device);

	return 0;
out:
	for (i = 0; i < HISI_ION_HEAP_NUM; i++) {
		if (!hisi_ion_heap[i])
			continue;
		ion_heap_destroy(hisi_ion_heap[i]);
		hisi_ion_heap[i] = NULL;
	}
	return err;
}

static int hisi_ion_remove(struct platform_device *pdev)
{
	int i;

	ion_device_destroy(hisi_ion_device);
	for (i = 0; i < HISI_ION_HEAP_NUM; i++) {
		if (!hisi_ion_heap[i])
			continue;
		ion_heap_destroy(hisi_ion_heap[i]);
		hisi_ion_heap[i] = NULL;
	}

	return 0;
}

static struct of_device_id hisi_ion_match_table[] = {
	{.compatible = "hisilicon,ion"},
	{},
};

static struct platform_driver hisi_ion_driver = {
	.probe = hisi_ion_probe,
	.remove = hisi_ion_remove,
	.driver = {
		.name = "ion",
		.of_match_table = hisi_ion_match_table,
	},
};

module_platform_driver(hisi_ion_driver);