summaryrefslogtreecommitdiff
path: root/drivers/dma/qcom/hidma_mgmt_sys.c
blob: d61f1068a34b31087c1ad3978aebcb3d79bd3af1 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Qualcomm Technologies HIDMA Management SYS interface
 *
 * Copyright (c) 2015, The Linux Foundation. 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.
 */

#include <linux/sysfs.h>
#include <linux/platform_device.h>

#include "hidma_mgmt.h"

struct hidma_chan_attr {
	struct hidma_mgmt_dev *mdev;
	int index;
	struct kobj_attribute attr;
};

struct hidma_mgmt_fileinfo {
	char *name;
	int mode;
	int (*get)(struct hidma_mgmt_dev *mdev);
	int (*set)(struct hidma_mgmt_dev *mdev, u64 val);
};

#define IMPLEMENT_GETSET(name)					\
static int get_##name(struct hidma_mgmt_dev *mdev)		\
{								\
	return mdev->name;					\
}								\
static int set_##name(struct hidma_mgmt_dev *mdev, u64 val)	\
{								\
	u64 tmp;						\
	int rc;							\
								\
	tmp = mdev->name;					\
	mdev->name = val;					\
	rc = hidma_mgmt_setup(mdev);				\
	if (rc)							\
		mdev->name = tmp;				\
	return rc;						\
}

#define DECLARE_ATTRIBUTE(name, mode)				\
	{#name, mode, get_##name, set_##name}

IMPLEMENT_GETSET(hw_version_major)
IMPLEMENT_GETSET(hw_version_minor)
IMPLEMENT_GETSET(max_wr_xactions)
IMPLEMENT_GETSET(max_rd_xactions)
IMPLEMENT_GETSET(max_write_request)
IMPLEMENT_GETSET(max_read_request)
IMPLEMENT_GETSET(dma_channels)
IMPLEMENT_GETSET(chreset_timeout_cycles)

static int set_priority(struct hidma_mgmt_dev *mdev, unsigned int i, u64 val)
{
	u64 tmp;
	int rc;

	if (i >= mdev->dma_channels)
		return -EINVAL;

	tmp = mdev->priority[i];
	mdev->priority[i] = val;
	rc = hidma_mgmt_setup(mdev);
	if (rc)
		mdev->priority[i] = tmp;
	return rc;
}

static int set_weight(struct hidma_mgmt_dev *mdev, unsigned int i, u64 val)
{
	u64 tmp;
	int rc;

	if (i >= mdev->dma_channels)
		return -EINVAL;

	tmp = mdev->weight[i];
	mdev->weight[i] = val;
	rc = hidma_mgmt_setup(mdev);
	if (rc)
		mdev->weight[i] = tmp;
	return rc;
}

static struct hidma_mgmt_fileinfo hidma_mgmt_files[] = {
	DECLARE_ATTRIBUTE(hw_version_major, S_IRUGO),
	DECLARE_ATTRIBUTE(hw_version_minor, S_IRUGO),
	DECLARE_ATTRIBUTE(dma_channels, S_IRUGO),
	DECLARE_ATTRIBUTE(chreset_timeout_cycles, S_IRUGO),
	DECLARE_ATTRIBUTE(max_wr_xactions, S_IRUGO),
	DECLARE_ATTRIBUTE(max_rd_xactions, S_IRUGO),
	DECLARE_ATTRIBUTE(max_write_request, S_IRUGO),
	DECLARE_ATTRIBUTE(max_read_request, S_IRUGO),
};

static ssize_t show_values(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct hidma_mgmt_dev *mdev = platform_get_drvdata(pdev);
	unsigned int i;

	buf[0] = 0;

	for (i = 0; i < ARRAY_SIZE(hidma_mgmt_files); i++) {
		if (strcmp(attr->attr.name, hidma_mgmt_files[i].name) == 0) {
			sprintf(buf, "%d\n", hidma_mgmt_files[i].get(mdev));
			break;
		}
	}
	return strlen(buf);
}

static ssize_t set_values(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t count)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct hidma_mgmt_dev *mdev = platform_get_drvdata(pdev);
	unsigned long tmp;
	unsigned int i;
	int rc;

	rc = kstrtoul(buf, 0, &tmp);
	if (rc)
		return rc;

	for (i = 0; i < ARRAY_SIZE(hidma_mgmt_files); i++) {
		if (strcmp(attr->attr.name, hidma_mgmt_files[i].name) == 0) {
			rc = hidma_mgmt_files[i].set(mdev, tmp);
			if (rc)
				return rc;

			break;
		}
	}
	return count;
}

static ssize_t show_values_channel(struct kobject *kobj,
				   struct kobj_attribute *attr, char *buf)
{
	struct hidma_chan_attr *chattr;
	struct hidma_mgmt_dev *mdev;

	buf[0] = 0;
	chattr = container_of(attr, struct hidma_chan_attr, attr);
	mdev = chattr->mdev;
	if (strcmp(attr->attr.name, "priority") == 0)
		sprintf(buf, "%d\n", mdev->priority[chattr->index]);
	else if (strcmp(attr->attr.name, "weight") == 0)
		sprintf(buf, "%d\n", mdev->weight[chattr->index]);

	return strlen(buf);
}

static ssize_t set_values_channel(struct kobject *kobj,
				  struct kobj_attribute *attr, const char *buf,
				  size_t count)
{
	struct hidma_chan_attr *chattr;
	struct hidma_mgmt_dev *mdev;
	unsigned long tmp;
	int rc;

	chattr = container_of(attr, struct hidma_chan_attr, attr);
	mdev = chattr->mdev;

	rc = kstrtoul(buf, 0, &tmp);
	if (rc)
		return rc;

	if (strcmp(attr->attr.name, "priority") == 0) {
		rc = set_priority(mdev, chattr->index, tmp);
		if (rc)
			return rc;
	} else if (strcmp(attr->attr.name, "weight") == 0) {
		rc = set_weight(mdev, chattr->index, tmp);
		if (rc)
			return rc;
	}
	return count;
}

static int create_sysfs_entry(struct hidma_mgmt_dev *dev, char *name, int mode)
{
	struct device_attribute *attrs;
	char *name_copy;

	attrs = devm_kmalloc(&dev->pdev->dev,
			     sizeof(struct device_attribute), GFP_KERNEL);
	if (!attrs)
		return -ENOMEM;

	name_copy = devm_kstrdup(&dev->pdev->dev, name, GFP_KERNEL);
	if (!name_copy)
		return -ENOMEM;

	attrs->attr.name = name_copy;
	attrs->attr.mode = mode;
	attrs->show = show_values;
	attrs->store = set_values;
	sysfs_attr_init(&attrs->attr);

	return device_create_file(&dev->pdev->dev, attrs);
}

static int create_sysfs_entry_channel(struct hidma_mgmt_dev *mdev, char *name,
				      int mode, int index,
				      struct kobject *parent)
{
	struct hidma_chan_attr *chattr;
	char *name_copy;

	chattr = devm_kmalloc(&mdev->pdev->dev, sizeof(*chattr), GFP_KERNEL);
	if (!chattr)
		return -ENOMEM;

	name_copy = devm_kstrdup(&mdev->pdev->dev, name, GFP_KERNEL);
	if (!name_copy)
		return -ENOMEM;

	chattr->mdev = mdev;
	chattr->index = index;
	chattr->attr.attr.name = name_copy;
	chattr->attr.attr.mode = mode;
	chattr->attr.show = show_values_channel;
	chattr->attr.store = set_values_channel;
	sysfs_attr_init(&chattr->attr.attr);

	return sysfs_create_file(parent, &chattr->attr.attr);
}

int hidma_mgmt_init_sys(struct hidma_mgmt_dev *mdev)
{
	unsigned int i;
	int rc;
	int required;
	struct kobject *chanops;

	required = sizeof(*mdev->chroots) * mdev->dma_channels;
	mdev->chroots = devm_kmalloc(&mdev->pdev->dev, required, GFP_KERNEL);
	if (!mdev->chroots)
		return -ENOMEM;

	chanops = kobject_create_and_add("chanops", &mdev->pdev->dev.kobj);
	if (!chanops)
		return -ENOMEM;

	/* create each channel directory here */
	for (i = 0; i < mdev->dma_channels; i++) {
		char name[20];

		snprintf(name, sizeof(name), "chan%d", i);
		mdev->chroots[i] = kobject_create_and_add(name, chanops);
		if (!mdev->chroots[i])
			return -ENOMEM;
	}

	/* populate common parameters */
	for (i = 0; i < ARRAY_SIZE(hidma_mgmt_files); i++) {
		rc = create_sysfs_entry(mdev, hidma_mgmt_files[i].name,
					hidma_mgmt_files[i].mode);
		if (rc)
			return rc;
	}

	/* populate parameters that are per channel */
	for (i = 0; i < mdev->dma_channels; i++) {
		rc = create_sysfs_entry_channel(mdev, "priority",
						(S_IRUGO | S_IWUGO), i,
						mdev->chroots[i]);
		if (rc)
			return rc;

		rc = create_sysfs_entry_channel(mdev, "weight",
						(S_IRUGO | S_IWUGO), i,
						mdev->chroots[i]);
		if (rc)
			return rc;
	}

	return 0;
}
EXPORT_SYMBOL_GPL(hidma_mgmt_init_sys);