summaryrefslogtreecommitdiffstats
path: root/drivers/char/adi.c
blob: 751d7cc0da1b3be2beca3f362e3a316a7d35b33c (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Privileged ADI driver for sparc64
 *
 * Author: Tom Hromatka <tom.hromatka@oracle.com>
 */
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <asm/asi.h>

#define MAX_BUF_SZ	PAGE_SIZE

static int adi_open(struct inode *inode, struct file *file)
{
	file->f_mode |= FMODE_UNSIGNED_OFFSET;
	return 0;
}

static int read_mcd_tag(unsigned long addr)
{
	long err;
	int ver;

	__asm__ __volatile__(
		"1:	ldxa [%[addr]] %[asi], %[ver]\n"
		"	mov 0, %[err]\n"
		"2:\n"
		"	.section .fixup,#alloc,#execinstr\n"
		"	.align 4\n"
		"3:	sethi %%hi(2b), %%g1\n"
		"	jmpl  %%g1 + %%lo(2b), %%g0\n"
		"	mov %[invalid], %[err]\n"
		"	.previous\n"
		"	.section __ex_table, \"a\"\n"
		"	.align 4\n"
		"	.word  1b, 3b\n"
		"	.previous\n"
		: [ver] "=r" (ver), [err] "=r" (err)
		: [addr] "r"  (addr), [invalid] "i" (EFAULT),
		  [asi] "i" (ASI_MCD_REAL)
		: "memory", "g1"
		);

	if (err)
		return -EFAULT;
	else
		return ver;
}

static ssize_t adi_read(struct file *file, char __user *buf,
			size_t count, loff_t *offp)
{
	size_t ver_buf_sz, bytes_read = 0;
	int ver_buf_idx = 0;
	loff_t offset;
	u8 *ver_buf;
	ssize_t ret;

	ver_buf_sz = min_t(size_t, count, MAX_BUF_SZ);
	ver_buf = kmalloc(ver_buf_sz, GFP_KERNEL);
	if (!ver_buf)
		return -ENOMEM;

	offset = (*offp) * adi_blksize();

	while (bytes_read < count) {
		ret = read_mcd_tag(offset);
		if (ret < 0)
			goto out;

		ver_buf[ver_buf_idx] = (u8)ret;
		ver_buf_idx++;
		offset += adi_blksize();

		if (ver_buf_idx >= ver_buf_sz) {
			if (copy_to_user(buf + bytes_read, ver_buf,
					 ver_buf_sz)) {
				ret = -EFAULT;
				goto out;
			}

			bytes_read += ver_buf_sz;
			ver_buf_idx = 0;

			ver_buf_sz = min(count - bytes_read,
					 (size_t)MAX_BUF_SZ);
		}
	}

	(*offp) += bytes_read;
	ret = bytes_read;
out:
	kfree(ver_buf);
	return ret;
}

static int set_mcd_tag(unsigned long addr, u8 ver)
{
	long err;

	__asm__ __volatile__(
		"1:	stxa %[ver], [%[addr]] %[asi]\n"
		"	mov 0, %[err]\n"
		"2:\n"
		"	.section .fixup,#alloc,#execinstr\n"
		"	.align 4\n"
		"3:	sethi %%hi(2b), %%g1\n"
		"	jmpl %%g1 + %%lo(2b), %%g0\n"
		"	mov %[invalid], %[err]\n"
		"	.previous\n"
		"	.section __ex_table, \"a\"\n"
		"	.align 4\n"
		"	.word 1b, 3b\n"
		"	.previous\n"
		: [err] "=r" (err)
		: [ver] "r" (ver), [addr] "r"  (addr),
		  [invalid] "i" (EFAULT), [asi] "i" (ASI_MCD_REAL)
		: "memory", "g1"
		);

	if (err)
		return -EFAULT;
	else
		return ver;
}

static ssize_t adi_write(struct file *file, const char __user *buf,
			 size_t count, loff_t *offp)
{
	size_t ver_buf_sz, bytes_written = 0;
	loff_t offset;
	u8 *ver_buf;
	ssize_t ret;
	int i;

	if (count <= 0)
		return -EINVAL;

	ver_buf_sz = min_t(size_t, count, MAX_BUF_SZ);
	ver_buf = kmalloc(ver_buf_sz, GFP_KERNEL);
	if (!ver_buf)
		return -ENOMEM;

	offset = (*offp) * adi_blksize();

	do {
		if (copy_from_user(ver_buf, &buf[bytes_written],
				   ver_buf_sz)) {
			ret = -EFAULT;
			goto out;
		}

		for (i = 0; i < ver_buf_sz; i++) {
			ret = set_mcd_tag(offset, ver_buf[i]);
			if (ret < 0)
				goto out;

			offset += adi_blksize();
		}

		bytes_written += ver_buf_sz;
		ver_buf_sz = min(count - bytes_written, (size_t)MAX_BUF_SZ);
	} while (bytes_written < count);

	(*offp) += bytes_written;
	ret = bytes_written;
out:
	__asm__ __volatile__("membar #Sync");
	kfree(ver_buf);
	return ret;
}

static loff_t adi_llseek(struct file *file, loff_t offset, int whence)
{
	loff_t ret = -EINVAL;

	switch (whence) {
	case SEEK_END:
	case SEEK_DATA:
	case SEEK_HOLE:
		/* unsupported */
		return -EINVAL;
	case SEEK_CUR:
		if (offset == 0)
			return file->f_pos;

		offset += file->f_pos;
		break;
	case SEEK_SET:
		break;
	}

	if (offset != file->f_pos) {
		file->f_pos = offset;
		file->f_version = 0;
		ret = offset;
	}

	return ret;
}

static const struct file_operations adi_fops = {
	.owner		= THIS_MODULE,
	.llseek		= adi_llseek,
	.open		= adi_open,
	.read		= adi_read,
	.write		= adi_write,
};

static struct miscdevice adi_miscdev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = KBUILD_MODNAME,
	.fops = &adi_fops,
};

static int __init adi_init(void)
{
	if (!adi_capable())
		return -EPERM;

	return misc_register(&adi_miscdev);
}

static void __exit adi_exit(void)
{
	misc_deregister(&adi_miscdev);
}

module_init(adi_init);
module_exit(adi_exit);

MODULE_AUTHOR("Tom Hromatka <tom.hromatka@oracle.com>");
MODULE_DESCRIPTION("Privileged interface to ADI");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL v2");
OpenPOWER on IntegriCloud