/arch/arm/mach-msm/rpm_log.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase · C · 367 lines · 222 code · 58 blank · 87 comment · 29 complexity · bc764578ec5d2cad0a733557312bbfe5 MD5 · raw file

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