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