summaryrefslogtreecommitdiff
path: root/drivers/soc/qcom/msm_bus/msm-buspm-dev.c
blob: 867e7378448c37a82df85e2d297ff71b82e64943 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Copyright (c) 2011-2014, 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.
 */

/* #define DEBUG */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/dma-mapping.h>
#include <soc/qcom/rpm-smd.h>
#include <uapi/linux/msm-buspm-dev.h>

#define MSM_BUSPM_DRV_NAME "msm-buspm-dev"

#ifdef CONFIG_COMPAT
static long
msm_buspm_dev_compat_ioctl(struct file *filp, unsigned int cmd,
						unsigned long arg);
#else
#define msm_buspm_dev_compat_ioctl NULL
#endif

static long
msm_buspm_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
static int msm_buspm_dev_mmap(struct file *filp, struct vm_area_struct *vma);
static int msm_buspm_dev_release(struct inode *inode, struct file *filp);
static int msm_buspm_dev_open(struct inode *inode, struct file *filp);

static const struct file_operations msm_buspm_dev_fops = {
	.owner		= THIS_MODULE,
	.mmap		= msm_buspm_dev_mmap,
	.open		= msm_buspm_dev_open,
	.unlocked_ioctl	= msm_buspm_dev_ioctl,
	.compat_ioctl	= msm_buspm_dev_compat_ioctl,
	.llseek		= noop_llseek,
	.release	= msm_buspm_dev_release,
};

struct miscdevice msm_buspm_misc = {
	.minor	= MISC_DYNAMIC_MINOR,
	.name	= MSM_BUSPM_DRV_NAME,
	.fops	= &msm_buspm_dev_fops,
};


enum msm_buspm_spdm_res {
	SPDM_RES_ID = 0,
	SPDM_RES_TYPE = 0x63707362,
	SPDM_KEY = 0x00006e65,
	SPDM_SIZE = 4,
};
/*
 * Allocate kernel buffer.
 * Currently limited to one buffer per file descriptor.  If alloc() is
 * called twice for the same descriptor, the original buffer is freed.
 * There is also no locking protection so the same descriptor can not be shared.
 */

static inline void *msm_buspm_dev_get_vaddr(struct file *filp)
{
	struct msm_buspm_map_dev *dev = filp->private_data;

	return (dev) ? dev->vaddr : NULL;
}

static inline unsigned int msm_buspm_dev_get_buflen(struct file *filp)
{
	struct msm_buspm_map_dev *dev = filp->private_data;

	return dev ? dev->buflen : 0;
}

static inline unsigned long msm_buspm_dev_get_paddr(struct file *filp)
{
	struct msm_buspm_map_dev *dev = filp->private_data;

	return (dev) ? dev->paddr : 0L;
}

static void msm_buspm_dev_free(struct file *filp)
{
	struct msm_buspm_map_dev *dev = filp->private_data;

	if (dev && dev->vaddr) {
		pr_debug("freeing memory at 0x%p\n", dev->vaddr);
		dma_free_coherent(msm_buspm_misc.this_device, dev->buflen,
							dev->vaddr, dev->paddr);
		dev->paddr = 0L;
		dev->vaddr = NULL;
	}
}

static int msm_buspm_dev_open(struct inode *inode, struct file *filp)
{
	struct msm_buspm_map_dev *dev;

	if (capable(CAP_SYS_ADMIN)) {
		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
		if (dev)
			filp->private_data = dev;
		else
			return -ENOMEM;
	} else {
		return -EPERM;
	}

	return 0;
}

static int
msm_buspm_dev_alloc(struct file *filp, struct buspm_alloc_params data)
{
	dma_addr_t paddr;
	void *vaddr;
	struct msm_buspm_map_dev *dev = filp->private_data;

	/* If buffer already allocated, then free it */
	if (dev->vaddr)
		msm_buspm_dev_free(filp);

	/* Allocate uncached memory */
	vaddr = dma_alloc_coherent(msm_buspm_misc.this_device, data.size,
							&paddr, GFP_KERNEL);

	if (vaddr == NULL) {
		pr_err("allocation of 0x%zu bytes failed", data.size);
		return -ENOMEM;
	}

	dev->vaddr = vaddr;
	dev->paddr = paddr;
	dev->buflen = data.size;
	filp->f_pos = 0;
	pr_debug("virt addr = 0x%p\n", dev->vaddr);
	pr_debug("phys addr = 0x%lx\n", dev->paddr);

	return 0;
}

static int msm_bus_rpm_req(u32 rsc_type, u32 key, u32 hwid,
	int ctx, u32 val)
{
	struct msm_rpm_request *rpm_req;
	int ret, msg_id;

	rpm_req = msm_rpm_create_request(ctx, rsc_type, SPDM_RES_ID, 1);
	if (rpm_req == NULL) {
		pr_err("RPM: Couldn't create RPM Request\n");
		return -ENXIO;
	}

	ret = msm_rpm_add_kvp_data(rpm_req, key, (const uint8_t *)&val,
		(int)(sizeof(uint32_t)));
	if (ret) {
		pr_err("RPM: Add KVP failed for RPM Req:%u\n",
			rsc_type);
		goto err;
	}

	pr_debug("Added Key: %d, Val: %u, size: %zu\n", key,
		(uint32_t)val, sizeof(uint32_t));
	msg_id = msm_rpm_send_request(rpm_req);
	if (!msg_id) {
		pr_err("RPM: No message ID for req\n");
		ret = -ENXIO;
		goto err;
	}

	ret = msm_rpm_wait_for_ack(msg_id);
	if (ret) {
		pr_err("RPM: Ack failed\n");
		goto err;
	}

err:
	msm_rpm_free_request(rpm_req);
	return ret;
}

static int msm_buspm_ioc_cmds(uint32_t arg)
{
	switch (arg) {
	case MSM_BUSPM_SPDM_CLK_DIS:
	case MSM_BUSPM_SPDM_CLK_EN:
		return msm_bus_rpm_req(SPDM_RES_TYPE, SPDM_KEY, 0,
				MSM_RPM_CTX_ACTIVE_SET, arg);
	default:
		pr_warn("Unsupported ioctl command: %d\n", arg);
		return -EINVAL;
	}
}



static long
msm_buspm_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct buspm_xfer_req xfer;
	struct buspm_alloc_params alloc_data;
	unsigned long paddr;
	int retval = 0;
	void *buf = msm_buspm_dev_get_vaddr(filp);
	unsigned int buflen = msm_buspm_dev_get_buflen(filp);
	unsigned char *dbgbuf = buf;

	if (_IOC_TYPE(cmd) != MSM_BUSPM_IOC_MAGIC) {
		pr_err("Wrong IOC_MAGIC.Exiting\n");
		return -ENOTTY;
	}

	switch (cmd) {
	case MSM_BUSPM_IOC_FREE:
		pr_debug("cmd = 0x%x (FREE)\n", cmd);
		msm_buspm_dev_free(filp);
		break;

	case MSM_BUSPM_IOC_ALLOC:
		pr_debug("cmd = 0x%x (ALLOC)\n", cmd);
		retval = __get_user(alloc_data.size, (uint32_t __user *)arg);

		if (retval == 0)
			retval = msm_buspm_dev_alloc(filp, alloc_data);
		break;

	case MSM_BUSPM_IOC_RD_PHYS_ADDR:
		pr_debug("Read Physical Address\n");
		paddr = msm_buspm_dev_get_paddr(filp);
		if (paddr == 0L) {
			retval = -EINVAL;
		} else {
			pr_debug("phys addr = 0x%lx\n", paddr);
			retval = __put_user(paddr,
				(unsigned long __user *)arg);
		}
		break;

	case MSM_BUSPM_IOC_RDBUF:
		if (!buf) {
			retval = -EINVAL;
			break;
		}

		pr_debug("Read Buffer: 0x%x%x%x%x\n",
				dbgbuf[0], dbgbuf[1], dbgbuf[2], dbgbuf[3]);

		if (copy_from_user(&xfer, (void __user *)arg, sizeof(xfer))) {
			retval = -EFAULT;
			break;
		}

		if ((xfer.size <= buflen) &&
			(copy_to_user((void __user *)xfer.data, buf,
					xfer.size))) {
			retval = -EFAULT;
			break;
		}
		break;

	case MSM_BUSPM_IOC_WRBUF:
		pr_debug("Write Buffer\n");

		if (!buf) {
			retval = -EINVAL;
			break;
		}

		if (copy_from_user(&xfer, (void __user *)arg, sizeof(xfer))) {
			retval = -EFAULT;
			break;
		}

		if ((buflen <= xfer.size) &&
			(copy_from_user(buf, (void __user *)xfer.data,
			xfer.size))) {
			retval = -EFAULT;
			break;
		}
		break;

	case MSM_BUSPM_IOC_CMD:
		pr_debug("IOCTL command: cmd: %d arg: %lu\n", cmd, arg);
		retval = msm_buspm_ioc_cmds(arg);
		break;

	default:
		pr_debug("Unknown command 0x%x\n", cmd);
		retval = -EINVAL;
		break;
	}

	return retval;
}

static int msm_buspm_dev_release(struct inode *inode, struct file *filp)
{
	struct msm_buspm_map_dev *dev = filp->private_data;

	msm_buspm_dev_free(filp);
	kfree(dev);
	filp->private_data = NULL;

	return 0;
}

static int msm_buspm_dev_mmap(struct file *filp, struct vm_area_struct *vma)
{
	pr_debug("vma = 0x%p\n", vma);

	/* Mappings are uncached */
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
		vma->vm_end - vma->vm_start, vma->vm_page_prot))
		return -EFAULT;

	return 0;
}

#ifdef CONFIG_COMPAT
static long
msm_buspm_dev_compat_ioctl(struct file *filp, unsigned int cmd,
						unsigned long arg)
{
	return msm_buspm_dev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
}
#endif

static int __init msm_buspm_dev_init(void)
{
	int ret = 0;

	ret = misc_register(&msm_buspm_misc);
	if (ret < 0)
		pr_err("%s: Cannot register misc device\n", __func__);

	if (msm_buspm_misc.this_device->coherent_dma_mask == 0)
		msm_buspm_misc.this_device->coherent_dma_mask =
							DMA_BIT_MASK(32);

	return ret;
}

static void __exit msm_buspm_dev_exit(void)
{
	misc_deregister(&msm_buspm_misc);
}
module_init(msm_buspm_dev_init);
module_exit(msm_buspm_dev_exit);

MODULE_LICENSE("GPL v2");
MODULE_VERSION("1.0");
MODULE_ALIAS("platform:"MSM_BUSPM_DRV_NAME);