blob: 76eb1d0d1cea053f0156b356e208471bad886e6a [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/debugfs.h>
14#include <linux/delay.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/types.h>
24
25#include <asm/uaccess.h>
26
27#include <mach/msm_iomap.h>
28
29#include "rpm_log.h"
30
31/* registers in MSM_RPM_LOG_PAGE_INDICES */
32enum {
33 MSM_RPM_LOG_TAIL,
34 MSM_RPM_LOG_HEAD
35};
36
37/* used to 4 byte align message lengths */
38#define PADDED_LENGTH(x) (0xFFFFFFFC & ((x) + 3))
39
40/* calculates the character string length of a message of byte length x */
41#define PRINTED_LENGTH(x) ((x) * 6 + 3)
42
43/* number of ms to wait between checking for new messages in the RPM log */
44#define RECHECK_TIME (50)
45
46struct msm_rpm_log_buffer {
47 char *data;
48 u32 len;
49 u32 pos;
50 u32 max_len;
51 u32 read_idx;
52 struct msm_rpm_log_platform_data *pdata;
53};
54
55/******************************************************************************
56 * Internal functions
57 *****************************************************************************/
58
59static inline u32
60msm_rpm_log_read(const struct msm_rpm_log_platform_data *pdata, u32 page,
61 u32 reg)
62{
63 return readl_relaxed(pdata->reg_base + pdata->reg_offsets[page]
64 + reg * 4);
65}
66
67/*
68 * msm_rpm_log_copy() - Copies messages from a volatile circular buffer in
69 * the RPM's shared memory into a private local buffer
70 * msg_buffer: pointer to local buffer (string)
71 * buf_len: length of local buffer in bytes
72 * read_start_idx: index into shared memory buffer
73 *
74 * Return value: number of bytes written to the local buffer
75 *
76 * Copies messages stored in a circular buffer in the RPM Message Memory into
77 * a specified local buffer. The RPM processor is unaware of these reading
78 * efforts, so care is taken to make sure that messages are valid both before
79 * and after reading. The RPM processor utilizes a ULog driver to write the
80 * log. The RPM processor maintains tail and head indices. These correspond
81 * to the next byte to write into, and the first valid byte, respectively.
82 * Both indices increase monotonically (except for rollover).
83 *
84 * Messages take the form of [(u32)length] [(char)data0,1,...] in which the
85 * length specifies the number of payload bytes. Messages must be 4 byte
86 * aligned, so padding is added at the end of a message as needed.
87 *
88 * Print format:
89 * - 0xXX, 0xXX, 0xXX
90 * - 0xXX
91 * etc...
92 */
93static u32 msm_rpm_log_copy(const struct msm_rpm_log_platform_data *pdata,
94 char *msg_buffer, u32 buf_len, u32 *read_idx)
95{
96 u32 head_idx, tail_idx;
97 u32 pos = 0;
98 u32 i = 0;
99 u32 msg_len;
100 u32 pos_start;
101 char temp[4];
102
103 tail_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
104 MSM_RPM_LOG_TAIL);
105 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
106 MSM_RPM_LOG_HEAD);
107
108 /* loop while the remote buffer has valid messages left to read */
109 while (tail_idx - head_idx > 0 && tail_idx - *read_idx > 0) {
110 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
111 MSM_RPM_LOG_HEAD);
112 /* check if the message to be read is valid */
113 if (tail_idx - *read_idx > tail_idx - head_idx) {
114 *read_idx = head_idx;
115 continue;
116 }
117 /*
Anji Jonnala9554fa32012-07-20 03:33:53 +0530118 * Ensure that all indices are 4 byte aligned.
119 * This conditions is required to interact with a ULog buffer
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 * properly.
121 */
Anji Jonnala9554fa32012-07-20 03:33:53 +0530122 if (!IS_ALIGNED((tail_idx | head_idx | *read_idx), 4))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123 break;
124
125 msg_len = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_BUFFER,
126 (*read_idx >> 2) & pdata->log_len_mask);
127
128 /* handle messages that claim to be longer than the log */
129 if (PADDED_LENGTH(msg_len) > tail_idx - *read_idx - 4)
130 msg_len = tail_idx - *read_idx - 4;
131
132 /* check that the local buffer has enough space for this msg */
133 if (pos + PRINTED_LENGTH(msg_len) > buf_len)
134 break;
135
136 pos_start = pos;
137 pos += scnprintf(msg_buffer + pos, buf_len - pos, "- ");
138
139 /* copy message payload to local buffer */
140 for (i = 0; i < msg_len; i++) {
141 /* read from shared memory 4 bytes at a time */
142 if (IS_ALIGNED(i, 4))
143 *((u32 *)temp) = msm_rpm_log_read(pdata,
144 MSM_RPM_LOG_PAGE_BUFFER,
145 ((*read_idx + 4 + i) >> 2) &
146 pdata->log_len_mask);
147
148 pos += scnprintf(msg_buffer + pos, buf_len - pos,
149 "0x%02X, ", temp[i & 0x03]);
150 }
151
152 pos += scnprintf(msg_buffer + pos, buf_len - pos, "\n");
153
154 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
155 MSM_RPM_LOG_HEAD);
156
157 /* roll back if message that was read is not still valid */
158 if (tail_idx - *read_idx > tail_idx - head_idx)
159 pos = pos_start;
160
161 *read_idx += PADDED_LENGTH(msg_len) + 4;
162 }
163
164 return pos;
165}
166
167
168/*
169 * msm_rpm_log_file_read() - Reads in log buffer messages then outputs them to a
170 * user buffer
171 *
172 * Return value:
173 * 0: success
174 * -ENOMEM: no memory available
175 * -EINVAL: user buffer null or requested bytes 0
176 * -EFAULT: user buffer not writeable
177 * -EAGAIN: no bytes available at the moment
178 */
179static ssize_t msm_rpm_log_file_read(struct file *file, char __user *bufu,
180 size_t count, loff_t *ppos)
181{
182 u32 out_len, remaining;
183 struct msm_rpm_log_platform_data *pdata;
184 struct msm_rpm_log_buffer *buf;
185
186 buf = file->private_data;
187 pdata = buf->pdata;
188 if (!pdata)
189 return -EINVAL;
190 if (!buf)
191 return -ENOMEM;
192 if (!buf->data)
193 return -ENOMEM;
194 if (!bufu || count < 0)
195 return -EINVAL;
196 if (!access_ok(VERIFY_WRITE, bufu, count))
197 return -EFAULT;
198
199 /* check for more messages if local buffer empty */
200 if (buf->pos == buf->len) {
201 buf->pos = 0;
202 buf->len = msm_rpm_log_copy(pdata, buf->data, buf->max_len,
203 &(buf->read_idx));
204 }
205
206 if ((file->f_flags & O_NONBLOCK) && buf->len == 0)
207 return -EAGAIN;
208
209 /* loop until new messages arrive */
210 while (buf->len == 0) {
211 cond_resched();
212 if (msleep_interruptible(RECHECK_TIME))
213 break;
214 buf->len = msm_rpm_log_copy(pdata, buf->data, buf->max_len,
215 &(buf->read_idx));
216 }
217
218 out_len = ((buf->len - buf->pos) < count ? buf->len - buf->pos : count);
219
220 remaining = __copy_to_user(bufu, &(buf->data[buf->pos]), out_len);
221 buf->pos += out_len - remaining;
222
223 return out_len - remaining;
224}
225
226
227/*
228 * msm_rpm_log_file_open() - Allows a new reader to open the RPM log virtual
229 * file
230 *
231 * One local buffer is kmalloc'ed for each reader, so no resource sharing has
232 * to take place (besides the read only access to the RPM log buffer).
233 *
234 * Return value:
235 * 0: success
236 * -ENOMEM: no memory available
237 */
238static int msm_rpm_log_file_open(struct inode *inode, struct file *file)
239{
240 struct msm_rpm_log_buffer *buf;
241 struct msm_rpm_log_platform_data *pdata;
242
243 pdata = inode->i_private;
244 if (!pdata)
245 return -EINVAL;
246
247 file->private_data =
248 kmalloc(sizeof(struct msm_rpm_log_buffer), GFP_KERNEL);
249 if (!file->private_data) {
250 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
251 __func__, sizeof(struct msm_rpm_log_buffer));
252 return -ENOMEM;
253 }
254 buf = file->private_data;
255
256 buf->data = kmalloc(PRINTED_LENGTH(pdata->log_len), GFP_KERNEL);
257 if (!buf->data) {
258 kfree(file->private_data);
259 file->private_data = NULL;
260 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
261 __func__, PRINTED_LENGTH(pdata->log_len));
262 return -ENOMEM;
263 }
264
265 buf->pdata = pdata;
266 buf->len = 0;
267 buf->pos = 0;
268 buf->max_len = PRINTED_LENGTH(pdata->log_len);
269 buf->read_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
270 MSM_RPM_LOG_HEAD);
271 return 0;
272}
273
274static int msm_rpm_log_file_close(struct inode *inode, struct file *file)
275{
276 kfree(((struct msm_rpm_log_buffer *)file->private_data)->data);
277 kfree(file->private_data);
278 return 0;
279}
280
281
282static const struct file_operations msm_rpm_log_file_fops = {
283 .owner = THIS_MODULE,
284 .open = msm_rpm_log_file_open,
285 .read = msm_rpm_log_file_read,
286 .release = msm_rpm_log_file_close,
287};
288
289static int __devinit msm_rpm_log_probe(struct platform_device *pdev)
290{
291 struct dentry *dent;
292 struct msm_rpm_log_platform_data *pdata;
293
294 pdata = pdev->dev.platform_data;
295 if (!pdata)
296 return -EINVAL;
297
298 pdata->reg_base = ioremap(pdata->phys_addr_base, pdata->phys_size);
299 if (!pdata->reg_base) {
300 pr_err("%s: ERROR could not ioremap: start=%p, len=%u\n",
301 __func__, (void *) pdata->phys_addr_base,
302 pdata->phys_size);
303 return -EBUSY;
304 }
305
306 dent = debugfs_create_file("rpm_log", S_IRUGO, NULL,
307 pdev->dev.platform_data, &msm_rpm_log_file_fops);
308 if (!dent) {
309 pr_err("%s: ERROR debugfs_create_file failed\n", __func__);
310 return -ENOMEM;
311 }
312
313 platform_set_drvdata(pdev, dent);
314
315 pr_notice("%s: OK\n", __func__);
316 return 0;
317}
318
319static int __devexit msm_rpm_log_remove(struct platform_device *pdev)
320{
321 struct dentry *dent;
322 struct msm_rpm_log_platform_data *pdata;
323
324 pdata = pdev->dev.platform_data;
325
326 iounmap(pdata->reg_base);
327
328 dent = platform_get_drvdata(pdev);
329 debugfs_remove(dent);
330 platform_set_drvdata(pdev, NULL);
331
332 pr_notice("%s: OK\n", __func__);
333 return 0;
334}
335
336static struct platform_driver msm_rpm_log_driver = {
337 .probe = msm_rpm_log_probe,
338 .remove = __devexit_p(msm_rpm_log_remove),
339 .driver = {
340 .name = "msm_rpm_log",
341 .owner = THIS_MODULE,
342 },
343};
344
345static int __init msm_rpm_log_init(void)
346{
347 return platform_driver_register(&msm_rpm_log_driver);
348}
349
350static void __exit msm_rpm_log_exit(void)
351{
352 platform_driver_unregister(&msm_rpm_log_driver);
353}
354
355module_init(msm_rpm_log_init);
356module_exit(msm_rpm_log_exit);
357
358MODULE_LICENSE("GPL v2");
359MODULE_DESCRIPTION("MSM RPM Log driver");
360MODULE_VERSION("1.0");
361MODULE_ALIAS("platform:msm_rpm_log");