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