blob: 7a1d64f6d26a6461bf964e8fc9bcd41476472985 [file] [log] [blame]
Priyanka Mathur9591fff2012-12-13 13:07:32 -08001/* Copyright (c) 2010-2011, 2013, 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);
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800112 tail_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
113 MSM_RPM_LOG_TAIL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 /* check if the message to be read is valid */
115 if (tail_idx - *read_idx > tail_idx - head_idx) {
116 *read_idx = head_idx;
117 continue;
118 }
119 /*
Anji Jonnala9554fa32012-07-20 03:33:53 +0530120 * Ensure that all indices are 4 byte aligned.
121 * This conditions is required to interact with a ULog buffer
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 * properly.
123 */
Anji Jonnala9554fa32012-07-20 03:33:53 +0530124 if (!IS_ALIGNED((tail_idx | head_idx | *read_idx), 4))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 break;
126
127 msg_len = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_BUFFER,
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800128 ((*read_idx) & pdata->log_len_mask) >> 2);
129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
131 /* handle messages that claim to be longer than the log */
132 if (PADDED_LENGTH(msg_len) > tail_idx - *read_idx - 4)
133 msg_len = tail_idx - *read_idx - 4;
134
135 /* check that the local buffer has enough space for this msg */
136 if (pos + PRINTED_LENGTH(msg_len) > buf_len)
137 break;
138
139 pos_start = pos;
140 pos += scnprintf(msg_buffer + pos, buf_len - pos, "- ");
141
142 /* copy message payload to local buffer */
143 for (i = 0; i < msg_len; i++) {
144 /* read from shared memory 4 bytes at a time */
145 if (IS_ALIGNED(i, 4))
146 *((u32 *)temp) = msm_rpm_log_read(pdata,
147 MSM_RPM_LOG_PAGE_BUFFER,
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800148 ((*read_idx + 4 + i) &
149 pdata->log_len_mask) >> 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150
151 pos += scnprintf(msg_buffer + pos, buf_len - pos,
152 "0x%02X, ", temp[i & 0x03]);
153 }
154
155 pos += scnprintf(msg_buffer + pos, buf_len - pos, "\n");
156
157 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
158 MSM_RPM_LOG_HEAD);
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800159 tail_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
160 MSM_RPM_LOG_TAIL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161
162 /* roll back if message that was read is not still valid */
163 if (tail_idx - *read_idx > tail_idx - head_idx)
164 pos = pos_start;
165
166 *read_idx += PADDED_LENGTH(msg_len) + 4;
167 }
168
169 return pos;
170}
171
172
173/*
174 * msm_rpm_log_file_read() - Reads in log buffer messages then outputs them to a
175 * user buffer
176 *
177 * Return value:
178 * 0: success
179 * -ENOMEM: no memory available
180 * -EINVAL: user buffer null or requested bytes 0
181 * -EFAULT: user buffer not writeable
182 * -EAGAIN: no bytes available at the moment
183 */
184static ssize_t msm_rpm_log_file_read(struct file *file, char __user *bufu,
185 size_t count, loff_t *ppos)
186{
187 u32 out_len, remaining;
188 struct msm_rpm_log_platform_data *pdata;
189 struct msm_rpm_log_buffer *buf;
190
191 buf = file->private_data;
192 pdata = buf->pdata;
193 if (!pdata)
194 return -EINVAL;
195 if (!buf)
196 return -ENOMEM;
197 if (!buf->data)
198 return -ENOMEM;
199 if (!bufu || count < 0)
200 return -EINVAL;
201 if (!access_ok(VERIFY_WRITE, bufu, count))
202 return -EFAULT;
203
204 /* check for more messages if local buffer empty */
205 if (buf->pos == buf->len) {
206 buf->pos = 0;
207 buf->len = msm_rpm_log_copy(pdata, buf->data, buf->max_len,
208 &(buf->read_idx));
209 }
210
211 if ((file->f_flags & O_NONBLOCK) && buf->len == 0)
212 return -EAGAIN;
213
214 /* loop until new messages arrive */
215 while (buf->len == 0) {
216 cond_resched();
217 if (msleep_interruptible(RECHECK_TIME))
218 break;
219 buf->len = msm_rpm_log_copy(pdata, buf->data, buf->max_len,
220 &(buf->read_idx));
221 }
222
223 out_len = ((buf->len - buf->pos) < count ? buf->len - buf->pos : count);
224
225 remaining = __copy_to_user(bufu, &(buf->data[buf->pos]), out_len);
226 buf->pos += out_len - remaining;
227
228 return out_len - remaining;
229}
230
231
232/*
233 * msm_rpm_log_file_open() - Allows a new reader to open the RPM log virtual
234 * file
235 *
236 * One local buffer is kmalloc'ed for each reader, so no resource sharing has
237 * to take place (besides the read only access to the RPM log buffer).
238 *
239 * Return value:
240 * 0: success
241 * -ENOMEM: no memory available
242 */
243static int msm_rpm_log_file_open(struct inode *inode, struct file *file)
244{
245 struct msm_rpm_log_buffer *buf;
246 struct msm_rpm_log_platform_data *pdata;
247
248 pdata = inode->i_private;
249 if (!pdata)
250 return -EINVAL;
251
252 file->private_data =
253 kmalloc(sizeof(struct msm_rpm_log_buffer), GFP_KERNEL);
254 if (!file->private_data) {
255 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
256 __func__, sizeof(struct msm_rpm_log_buffer));
257 return -ENOMEM;
258 }
259 buf = file->private_data;
260
261 buf->data = kmalloc(PRINTED_LENGTH(pdata->log_len), GFP_KERNEL);
262 if (!buf->data) {
263 kfree(file->private_data);
264 file->private_data = NULL;
265 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
266 __func__, PRINTED_LENGTH(pdata->log_len));
267 return -ENOMEM;
268 }
269
270 buf->pdata = pdata;
271 buf->len = 0;
272 buf->pos = 0;
273 buf->max_len = PRINTED_LENGTH(pdata->log_len);
274 buf->read_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
275 MSM_RPM_LOG_HEAD);
276 return 0;
277}
278
279static int msm_rpm_log_file_close(struct inode *inode, struct file *file)
280{
281 kfree(((struct msm_rpm_log_buffer *)file->private_data)->data);
282 kfree(file->private_data);
283 return 0;
284}
285
286
287static const struct file_operations msm_rpm_log_file_fops = {
288 .owner = THIS_MODULE,
289 .open = msm_rpm_log_file_open,
290 .read = msm_rpm_log_file_read,
291 .release = msm_rpm_log_file_close,
292};
293
294static int __devinit msm_rpm_log_probe(struct platform_device *pdev)
295{
296 struct dentry *dent;
297 struct msm_rpm_log_platform_data *pdata;
298
299 pdata = pdev->dev.platform_data;
300 if (!pdata)
301 return -EINVAL;
302
303 pdata->reg_base = ioremap(pdata->phys_addr_base, pdata->phys_size);
304 if (!pdata->reg_base) {
305 pr_err("%s: ERROR could not ioremap: start=%p, len=%u\n",
306 __func__, (void *) pdata->phys_addr_base,
307 pdata->phys_size);
308 return -EBUSY;
309 }
310
311 dent = debugfs_create_file("rpm_log", S_IRUGO, NULL,
312 pdev->dev.platform_data, &msm_rpm_log_file_fops);
313 if (!dent) {
314 pr_err("%s: ERROR debugfs_create_file failed\n", __func__);
315 return -ENOMEM;
316 }
317
318 platform_set_drvdata(pdev, dent);
319
320 pr_notice("%s: OK\n", __func__);
321 return 0;
322}
323
324static int __devexit msm_rpm_log_remove(struct platform_device *pdev)
325{
326 struct dentry *dent;
327 struct msm_rpm_log_platform_data *pdata;
328
329 pdata = pdev->dev.platform_data;
330
331 iounmap(pdata->reg_base);
332
333 dent = platform_get_drvdata(pdev);
334 debugfs_remove(dent);
335 platform_set_drvdata(pdev, NULL);
336
337 pr_notice("%s: OK\n", __func__);
338 return 0;
339}
340
341static struct platform_driver msm_rpm_log_driver = {
342 .probe = msm_rpm_log_probe,
343 .remove = __devexit_p(msm_rpm_log_remove),
344 .driver = {
345 .name = "msm_rpm_log",
346 .owner = THIS_MODULE,
347 },
348};
349
350static int __init msm_rpm_log_init(void)
351{
352 return platform_driver_register(&msm_rpm_log_driver);
353}
354
355static void __exit msm_rpm_log_exit(void)
356{
357 platform_driver_unregister(&msm_rpm_log_driver);
358}
359
360module_init(msm_rpm_log_init);
361module_exit(msm_rpm_log_exit);
362
363MODULE_LICENSE("GPL v2");
364MODULE_DESCRIPTION("MSM RPM Log driver");
365MODULE_VERSION("1.0");
366MODULE_ALIAS("platform:msm_rpm_log");