blob: a2c74a556cedc53c3cab95633b4f9b0b436ab486 [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>
Priyanka Mathur49c60802012-12-13 12:53:58 -080018#include <linux/of.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25
26#include <asm/uaccess.h>
27
28#include <mach/msm_iomap.h>
29
30#include "rpm_log.h"
31
32/* registers in MSM_RPM_LOG_PAGE_INDICES */
33enum {
34 MSM_RPM_LOG_TAIL,
35 MSM_RPM_LOG_HEAD
36};
37
38/* used to 4 byte align message lengths */
39#define PADDED_LENGTH(x) (0xFFFFFFFC & ((x) + 3))
40
41/* calculates the character string length of a message of byte length x */
42#define PRINTED_LENGTH(x) ((x) * 6 + 3)
43
44/* number of ms to wait between checking for new messages in the RPM log */
45#define RECHECK_TIME (50)
46
Priyanka Mathur49c60802012-12-13 12:53:58 -080047#define VERSION_8974 0x1000
48#define RPM_ULOG_LENGTH_SHIFT 16
49#define RPM_ULOG_LENGTH_MASK 0xFFFF0000
50
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051struct 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_relaxed(pdata->reg_base + pdata->reg_offsets[page]
69 + reg * 4);
70}
71
72/*
73 * msm_rpm_log_copy() - Copies messages from a volatile circular buffer in
74 * the RPM's shared memory into a private local buffer
75 * msg_buffer: pointer to local buffer (string)
76 * buf_len: length of local buffer in bytes
77 * read_start_idx: index into shared memory buffer
78 *
79 * Return value: number of bytes written to the local buffer
80 *
81 * Copies messages stored in a circular buffer in the RPM Message Memory into
82 * a specified local buffer. The RPM processor is unaware of these reading
83 * efforts, so care is taken to make sure that messages are valid both before
84 * and after reading. The RPM processor utilizes a ULog driver to write the
85 * log. The RPM processor maintains tail and head indices. These correspond
86 * to the next byte to write into, and the first valid byte, respectively.
87 * Both indices increase monotonically (except for rollover).
88 *
89 * Messages take the form of [(u32)length] [(char)data0,1,...] in which the
90 * length specifies the number of payload bytes. Messages must be 4 byte
91 * aligned, so padding is added at the end of a message as needed.
92 *
93 * Print format:
94 * - 0xXX, 0xXX, 0xXX
95 * - 0xXX
96 * etc...
97 */
98static u32 msm_rpm_log_copy(const struct msm_rpm_log_platform_data *pdata,
99 char *msg_buffer, u32 buf_len, u32 *read_idx)
100{
101 u32 head_idx, tail_idx;
102 u32 pos = 0;
103 u32 i = 0;
104 u32 msg_len;
105 u32 pos_start;
106 char temp[4];
107
108 tail_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
109 MSM_RPM_LOG_TAIL);
110 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
111 MSM_RPM_LOG_HEAD);
112
113 /* loop while the remote buffer has valid messages left to read */
114 while (tail_idx - head_idx > 0 && tail_idx - *read_idx > 0) {
115 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
116 MSM_RPM_LOG_HEAD);
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800117 tail_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
118 MSM_RPM_LOG_TAIL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 /* check if the message to be read is valid */
120 if (tail_idx - *read_idx > tail_idx - head_idx) {
121 *read_idx = head_idx;
122 continue;
123 }
Priyanka Mathur49c60802012-12-13 12:53:58 -0800124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 /*
Anji Jonnala9554fa32012-07-20 03:33:53 +0530126 * Ensure that all indices are 4 byte aligned.
127 * This conditions is required to interact with a ULog buffer
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 * properly.
129 */
Anji Jonnala9554fa32012-07-20 03:33:53 +0530130 if (!IS_ALIGNED((tail_idx | head_idx | *read_idx), 4))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 break;
132
133 msg_len = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_BUFFER,
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800134 ((*read_idx) & pdata->log_len_mask) >> 2);
135
Priyanka Mathur49c60802012-12-13 12:53:58 -0800136 /* Message length for 8974 is first 2 bytes.
137 * Exclude message length and format from message length.
138 */
139 if (pdata->version == VERSION_8974) {
140 msg_len = (msg_len & RPM_ULOG_LENGTH_MASK) >>
141 RPM_ULOG_LENGTH_SHIFT;
142 msg_len -= 4;
143 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144
145 /* handle messages that claim to be longer than the log */
146 if (PADDED_LENGTH(msg_len) > tail_idx - *read_idx - 4)
147 msg_len = tail_idx - *read_idx - 4;
148
149 /* check that the local buffer has enough space for this msg */
150 if (pos + PRINTED_LENGTH(msg_len) > buf_len)
151 break;
152
153 pos_start = pos;
154 pos += scnprintf(msg_buffer + pos, buf_len - pos, "- ");
155
156 /* copy message payload to local buffer */
157 for (i = 0; i < msg_len; i++) {
158 /* read from shared memory 4 bytes at a time */
159 if (IS_ALIGNED(i, 4))
160 *((u32 *)temp) = msm_rpm_log_read(pdata,
161 MSM_RPM_LOG_PAGE_BUFFER,
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800162 ((*read_idx + 4 + i) &
163 pdata->log_len_mask) >> 2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164
165 pos += scnprintf(msg_buffer + pos, buf_len - pos,
166 "0x%02X, ", temp[i & 0x03]);
167 }
168
169 pos += scnprintf(msg_buffer + pos, buf_len - pos, "\n");
170
171 head_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
172 MSM_RPM_LOG_HEAD);
Priyanka Mathur9591fff2012-12-13 13:07:32 -0800173 tail_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
174 MSM_RPM_LOG_TAIL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175
176 /* roll back if message that was read is not still valid */
177 if (tail_idx - *read_idx > tail_idx - head_idx)
178 pos = pos_start;
179
180 *read_idx += PADDED_LENGTH(msg_len) + 4;
181 }
182
183 return pos;
184}
185
186
187/*
188 * msm_rpm_log_file_read() - Reads in log buffer messages then outputs them to a
189 * user buffer
190 *
191 * Return value:
192 * 0: success
193 * -ENOMEM: no memory available
194 * -EINVAL: user buffer null or requested bytes 0
195 * -EFAULT: user buffer not writeable
196 * -EAGAIN: no bytes available at the moment
197 */
198static ssize_t msm_rpm_log_file_read(struct file *file, char __user *bufu,
199 size_t count, loff_t *ppos)
200{
201 u32 out_len, remaining;
202 struct msm_rpm_log_platform_data *pdata;
203 struct msm_rpm_log_buffer *buf;
204
205 buf = file->private_data;
206 pdata = buf->pdata;
207 if (!pdata)
208 return -EINVAL;
209 if (!buf)
210 return -ENOMEM;
211 if (!buf->data)
212 return -ENOMEM;
213 if (!bufu || count < 0)
214 return -EINVAL;
215 if (!access_ok(VERIFY_WRITE, bufu, count))
216 return -EFAULT;
217
218 /* check for more messages if local buffer empty */
219 if (buf->pos == buf->len) {
220 buf->pos = 0;
221 buf->len = msm_rpm_log_copy(pdata, buf->data, buf->max_len,
222 &(buf->read_idx));
223 }
224
225 if ((file->f_flags & O_NONBLOCK) && buf->len == 0)
226 return -EAGAIN;
227
228 /* loop until new messages arrive */
229 while (buf->len == 0) {
230 cond_resched();
231 if (msleep_interruptible(RECHECK_TIME))
232 break;
233 buf->len = msm_rpm_log_copy(pdata, buf->data, buf->max_len,
234 &(buf->read_idx));
235 }
236
237 out_len = ((buf->len - buf->pos) < count ? buf->len - buf->pos : count);
238
239 remaining = __copy_to_user(bufu, &(buf->data[buf->pos]), out_len);
240 buf->pos += out_len - remaining;
241
242 return out_len - remaining;
243}
244
245
246/*
247 * msm_rpm_log_file_open() - Allows a new reader to open the RPM log virtual
Priyanka Mathur49c60802012-12-13 12:53:58 -0800248 * file
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 *
250 * One local buffer is kmalloc'ed for each reader, so no resource sharing has
251 * to take place (besides the read only access to the RPM log buffer).
252 *
253 * Return value:
254 * 0: success
255 * -ENOMEM: no memory available
256 */
257static int msm_rpm_log_file_open(struct inode *inode, struct file *file)
258{
259 struct msm_rpm_log_buffer *buf;
260 struct msm_rpm_log_platform_data *pdata;
261
262 pdata = inode->i_private;
263 if (!pdata)
264 return -EINVAL;
265
266 file->private_data =
267 kmalloc(sizeof(struct msm_rpm_log_buffer), GFP_KERNEL);
268 if (!file->private_data) {
269 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
270 __func__, sizeof(struct msm_rpm_log_buffer));
271 return -ENOMEM;
272 }
273 buf = file->private_data;
274
275 buf->data = kmalloc(PRINTED_LENGTH(pdata->log_len), GFP_KERNEL);
276 if (!buf->data) {
277 kfree(file->private_data);
278 file->private_data = NULL;
279 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
280 __func__, PRINTED_LENGTH(pdata->log_len));
281 return -ENOMEM;
282 }
283
284 buf->pdata = pdata;
285 buf->len = 0;
286 buf->pos = 0;
287 buf->max_len = PRINTED_LENGTH(pdata->log_len);
288 buf->read_idx = msm_rpm_log_read(pdata, MSM_RPM_LOG_PAGE_INDICES,
289 MSM_RPM_LOG_HEAD);
290 return 0;
291}
292
293static int msm_rpm_log_file_close(struct inode *inode, struct file *file)
294{
295 kfree(((struct msm_rpm_log_buffer *)file->private_data)->data);
296 kfree(file->private_data);
297 return 0;
298}
299
300
301static const struct file_operations msm_rpm_log_file_fops = {
302 .owner = THIS_MODULE,
303 .open = msm_rpm_log_file_open,
304 .read = msm_rpm_log_file_read,
305 .release = msm_rpm_log_file_close,
306};
307
308static int __devinit msm_rpm_log_probe(struct platform_device *pdev)
309{
310 struct dentry *dent;
311 struct msm_rpm_log_platform_data *pdata;
Priyanka Mathur49c60802012-12-13 12:53:58 -0800312 struct resource *res = NULL;
313 struct device_node *node = NULL;
314 phys_addr_t page_buffer_address, rpm_addr_phys;
315 int ret = 0;
316 char *key = NULL;
317 uint32_t val = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318
Priyanka Mathur49c60802012-12-13 12:53:58 -0800319 node = pdev->dev.of_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320
Priyanka Mathur49c60802012-12-13 12:53:58 -0800321 if (node) {
322 pdata = kzalloc(sizeof(struct msm_rpm_log_platform_data),
323 GFP_KERNEL);
324 if (!pdata)
325 return -ENOMEM;
326
327 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
328 if (!res) {
329 kfree(pdata);
330 return -EINVAL;
331 }
332
333 pdata->phys_addr_base = res->start;
334 pdata->phys_size = resource_size(res);
335
336 pdata->reg_base = ioremap_nocache(pdata->phys_addr_base,
337 pdata->phys_size);
338 if (!pdata->reg_base) {
339 pr_err("%s: ERROR could not ioremap: start=%p, len=%u\n",
340 __func__, (void *) pdata->phys_addr_base,
341 pdata->phys_size);
342 kfree(pdata);
343 return -EBUSY;
344 }
345 /* Read various parameters from the header if the
346 * version of the RPM Ulog is 0x1000. This version
347 * corresponds to the node in the rpm header which
348 * holds RPM log on 8974.
349 *
350 * offset-page-buffer-addr: At this offset header
351 * contains address of the location where raw log
352 * starts
353 * offset-log-len: At this offset header contains
354 * the length of the log buffer.
355 * offset-log-len-mask: At this offset header contains
356 * the log length mask for the buffer.
357 * offset-page-indices: At this offset header contains
358 * the index for writer. */
359
360 key = "qcom,offset-version";
361 ret = of_property_read_u32(node, key, &val);
362 if (ret) {
363 pr_err("%s: Error in name %s key %s\n",
364 __func__, node->full_name, key);
365 ret = -EFAULT;
366 goto fail;
367 }
368
369 pdata->version = readl_relaxed(pdata->reg_base + val);
370 if (pdata->version == VERSION_8974) {
371 key = "qcom,rpm-addr-phys";
372 ret = of_property_read_u32(node, key, &val);
373 if (ret) {
374 pr_err("%s: Error in name %s key %s\n",
375 __func__, node->full_name, key);
376 ret = -EFAULT;
377 goto fail;
378 }
379
380 rpm_addr_phys = val;
381
382 key = "qcom,offset-page-buffer-addr";
383 ret = of_property_read_u32(node, key, &val);
384 if (ret) {
385 pr_err("%s: Error in name %s key %s\n",
386 __func__, node->full_name, key);
387 ret = -EFAULT;
388 goto fail;
389 }
390
391 page_buffer_address = rpm_addr_phys +
392 readl_relaxed(pdata->reg_base + val);
393 pdata->reg_offsets[MSM_RPM_LOG_PAGE_BUFFER] =
394 page_buffer_address - pdata->phys_addr_base;
395
396 key = "qcom,offset-log-len";
397 ret = of_property_read_u32(node, key, &val);
398 if (ret) {
399 pr_err("%s: Error in name %s key %s\n",
400 __func__, node->full_name, key);
401 ret = -EFAULT;
402 goto fail;
403 }
404 pdata->log_len = readl_relaxed(pdata->reg_base + val);
405
406 if (pdata->log_len > pdata->phys_size) {
407 pr_err("%s: Error phy size: %d should be atleast log length: %d\n",
408 __func__, pdata->phys_size,
409 pdata->log_len);
410
411 ret = -EINVAL;
412 goto fail;
413 }
414
415 key = "qcom,offset-log-len-mask";
416 ret = of_property_read_u32(node, key, &val);
417 if (ret) {
418 pr_err("%s: Error in name %s key %s\n",
419 __func__, node->full_name, key);
420 ret = -EFAULT;
421 goto fail;
422 }
423 pdata->log_len_mask = readl_relaxed(pdata->reg_base
424 + val);
425
426 key = "qcom,offset-page-indices";
427 ret = of_property_read_u32(node, key, &val);
428 if (ret) {
429 pr_err("%s: Error in name %s key %s\n",
430 __func__, node->full_name, key);
431 ret = -EFAULT;
432 goto fail;
433 }
434 pdata->reg_offsets[MSM_RPM_LOG_PAGE_INDICES] =
435 val;
436 } else{
437 ret = -EINVAL;
438 goto fail;
439 }
440
441 } else{
442 pdata = pdev->dev.platform_data;
443 if (!pdata)
444 return -EINVAL;
445
446 pdata->reg_base = ioremap(pdata->phys_addr_base,
447 pdata->phys_size);
448 if (!pdata->reg_base) {
449 pr_err("%s: ERROR could not ioremap: start=%p, len=%u\n",
450 __func__, (void *) pdata->phys_addr_base,
451 pdata->phys_size);
452 return -EBUSY;
453 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 }
455
456 dent = debugfs_create_file("rpm_log", S_IRUGO, NULL,
Priyanka Mathur49c60802012-12-13 12:53:58 -0800457 pdata, &msm_rpm_log_file_fops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 if (!dent) {
459 pr_err("%s: ERROR debugfs_create_file failed\n", __func__);
Priyanka Mathur49c60802012-12-13 12:53:58 -0800460 if (pdata->version == VERSION_8974) {
461 ret = -ENOMEM;
462 goto fail;
463 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 return -ENOMEM;
465 }
466
467 platform_set_drvdata(pdev, dent);
468
469 pr_notice("%s: OK\n", __func__);
470 return 0;
Priyanka Mathur49c60802012-12-13 12:53:58 -0800471
472fail:
473 iounmap(pdata->reg_base);
474 kfree(pdata);
475 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476}
477
478static int __devexit msm_rpm_log_remove(struct platform_device *pdev)
479{
480 struct dentry *dent;
481 struct msm_rpm_log_platform_data *pdata;
482
483 pdata = pdev->dev.platform_data;
484
485 iounmap(pdata->reg_base);
486
487 dent = platform_get_drvdata(pdev);
488 debugfs_remove(dent);
489 platform_set_drvdata(pdev, NULL);
490
491 pr_notice("%s: OK\n", __func__);
492 return 0;
493}
494
Priyanka Mathur49c60802012-12-13 12:53:58 -0800495static struct of_device_id rpm_log_table[] = {
496 {.compatible = "qcom,rpm-log"},
497 {},
498};
499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500static struct platform_driver msm_rpm_log_driver = {
501 .probe = msm_rpm_log_probe,
502 .remove = __devexit_p(msm_rpm_log_remove),
503 .driver = {
504 .name = "msm_rpm_log",
505 .owner = THIS_MODULE,
Priyanka Mathur49c60802012-12-13 12:53:58 -0800506 .of_match_table = rpm_log_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 },
508};
509
510static int __init msm_rpm_log_init(void)
511{
512 return platform_driver_register(&msm_rpm_log_driver);
513}
514
515static void __exit msm_rpm_log_exit(void)
516{
517 platform_driver_unregister(&msm_rpm_log_driver);
518}
519
520module_init(msm_rpm_log_init);
521module_exit(msm_rpm_log_exit);
522
523MODULE_LICENSE("GPL v2");
524MODULE_DESCRIPTION("MSM RPM Log driver");
525MODULE_VERSION("1.0");
526MODULE_ALIAS("platform:msm_rpm_log");