blob: 2cd30ded331fddb048af467b7cdf53e862dcb876 [file] [log] [blame]
Karthikeyan Ramasubramaniane1f4f732011-08-08 13:34:47 -06001/* Copyright (c) 2012, 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
14#include <linux/slab.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/jiffies.h>
21#include <linux/debugfs.h>
22#include <linux/io.h>
23#include <linux/idr.h>
24#include <linux/string.h>
25#include <linux/sched.h>
26#include <linux/wait.h>
27#include <linux/delay.h>
28#include <linux/completion.h>
29
30#include <mach/msm_ipc_logging.h>
31
32#include "ipc_logging.h"
33
34static LIST_HEAD(ipc_log_context_list);
35DEFINE_SPINLOCK(ipc_log_context_list_lock);
36static atomic_t next_log_id = ATOMIC_INIT(0);
37
38static struct ipc_log_page *get_first_page(struct ipc_log_context *ilctxt)
39{
40 struct ipc_log_page_header *p_pghdr;
41 struct ipc_log_page *pg = NULL;
42
43 if (!ilctxt)
44 return NULL;
45 p_pghdr = list_first_entry(&ilctxt->page_list,
46 struct ipc_log_page_header, list);
47 pg = container_of(p_pghdr, struct ipc_log_page, hdr);
48 return pg;
49}
50
51static struct ipc_log_page *get_next_page(struct ipc_log_context *ilctxt,
52 struct ipc_log_page *cur_pg)
53{
54 struct ipc_log_page_header *p_pghdr;
55 struct ipc_log_page *pg = NULL;
56
57 if (!ilctxt || !cur_pg)
58 return NULL;
59
60 if (ilctxt->last_page == cur_pg)
61 return ilctxt->first_page;
62
63 p_pghdr = list_first_entry(&cur_pg->hdr.list,
64 struct ipc_log_page_header, list);
65 pg = container_of(p_pghdr, struct ipc_log_page, hdr);
66
67 return pg;
68}
69
70/* If data == NULL, drop the log of size data_size*/
71static void ipc_log_read(struct ipc_log_context *ilctxt,
72 void *data, int data_size)
73{
74 int bytes_to_read;
75
76 bytes_to_read = MIN(((PAGE_SIZE - sizeof(struct ipc_log_page_header))
77 - ilctxt->read_page->hdr.read_offset),
78 data_size);
79 if (data)
80 memcpy(data, (ilctxt->read_page->data +
81 ilctxt->read_page->hdr.read_offset), bytes_to_read);
82 if (bytes_to_read != data_size) {
83 ilctxt->read_page->hdr.read_offset = 0xFFFF;
84 ilctxt->read_page = get_next_page(ilctxt, ilctxt->read_page);
85 ilctxt->read_page->hdr.read_offset = 0;
86 if (data)
87 memcpy((data + bytes_to_read),
88 (ilctxt->read_page->data +
89 ilctxt->read_page->hdr.read_offset),
90 (data_size - bytes_to_read));
91 bytes_to_read = (data_size - bytes_to_read);
92 }
93 ilctxt->read_page->hdr.read_offset += bytes_to_read;
94 ilctxt->write_avail += data_size;
95}
96
97/*
98 * Reads a message.
99 *
100 * If a message is read successfully, then the the message context
101 * will be set to:
102 * .hdr message header .size and .type values
103 * .offset beginning of message data
104 *
105 * @ectxt Message context and if NULL, drops the message.
106 *
107 * @returns 0 - no message available
108 * 1 - message read
109 */
110int msg_read(struct ipc_log_context *ilctxt,
111 struct encode_context *ectxt)
112{
113 struct tsv_header hdr;
114
115 ipc_log_read(ilctxt, &hdr, sizeof(hdr));
116 if (ectxt) {
117 ectxt->hdr.type = hdr.type;
118 ectxt->hdr.size = hdr.size;
119 ectxt->offset = sizeof(hdr);
120 ipc_log_read(ilctxt, (ectxt->buff + ectxt->offset),
121 (int)hdr.size);
122 } else {
123 ipc_log_read(ilctxt, NULL, (int)hdr.size);
124 }
125 return sizeof(hdr) + (int)hdr.size;
126}
127
128/*
129 * Commits messages to the FIFO. If the FIFO is full, then enough
130 * messages are dropped to create space for the new message.
131 */
132void ipc_log_write(void *ctxt, struct encode_context *ectxt)
133{
134 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
135 int bytes_to_write;
136 unsigned long flags;
137
138 if (!ilctxt || !ectxt) {
139 pr_err("%s: Invalid ipc_log or encode context\n", __func__);
140 return;
141 }
142
143 spin_lock_irqsave(&ipc_log_context_list_lock, flags);
144 spin_lock(&ilctxt->ipc_log_context_lock);
145 while (ilctxt->write_avail < ectxt->offset)
146 msg_read(ilctxt, NULL);
147
148 bytes_to_write = MIN(((PAGE_SIZE - sizeof(struct ipc_log_page_header))
149 - ilctxt->write_page->hdr.write_offset),
150 ectxt->offset);
151 memcpy((ilctxt->write_page->data +
152 ilctxt->write_page->hdr.write_offset),
153 ectxt->buff, bytes_to_write);
154 if (bytes_to_write != ectxt->offset) {
155 ilctxt->write_page->hdr.write_offset = 0xFFFF;
156 ilctxt->write_page = get_next_page(ilctxt, ilctxt->write_page);
157 ilctxt->write_page->hdr.write_offset = 0;
158 memcpy((ilctxt->write_page->data +
159 ilctxt->write_page->hdr.write_offset),
160 (ectxt->buff + bytes_to_write),
161 (ectxt->offset - bytes_to_write));
162 bytes_to_write = (ectxt->offset - bytes_to_write);
163 }
164 ilctxt->write_page->hdr.write_offset += bytes_to_write;
165 ilctxt->write_avail -= ectxt->offset;
166 complete(&ilctxt->read_avail);
167 spin_unlock(&ilctxt->ipc_log_context_lock);
168 spin_unlock_irqrestore(&ipc_log_context_list_lock, flags);
169}
170EXPORT_SYMBOL(ipc_log_write);
171
172/*
173 * Starts a new message after which you can add serialized data and
174 * then complete the message by calling msg_encode_end().
175 */
176void msg_encode_start(struct encode_context *ectxt, uint32_t type)
177{
178 if (!ectxt) {
179 pr_err("%s: Invalid encode context\n", __func__);
180 return;
181 }
182
183 ectxt->hdr.type = type;
184 ectxt->hdr.size = 0;
185 ectxt->offset = sizeof(ectxt->hdr);
186}
187EXPORT_SYMBOL(msg_encode_start);
188
189/*
190 * Completes the message
191 */
192void msg_encode_end(struct encode_context *ectxt)
193{
194 if (!ectxt) {
195 pr_err("%s: Invalid encode context\n", __func__);
196 return;
197 }
198
199 /* finalize data size */
200 ectxt->hdr.size = ectxt->offset - sizeof(ectxt->hdr);
201 BUG_ON(ectxt->hdr.size > MAX_MSG_SIZE);
202 memcpy(ectxt->buff, &ectxt->hdr, sizeof(ectxt->hdr));
203}
204EXPORT_SYMBOL(msg_encode_end);
205
206/*
207 * Helper funtion used to write data to a message context.
208 *
209 * @ectxt context initialized by calling msg_encode_start()
210 * @data data to write
211 * @size number of bytes of data to write
212 */
213static inline int tsv_write_data(struct encode_context *ectxt,
214 void *data, uint32_t size)
215{
216 if (!ectxt) {
217 pr_err("%s: Invalid encode context\n", __func__);
218 return -EINVAL;
219 }
220 if ((ectxt->offset + size) > MAX_MSG_SIZE) {
221 pr_err("%s: No space to encode further\n", __func__);
222 return -EINVAL;
223 }
224
225 memcpy((void *)(ectxt->buff + ectxt->offset), data, size);
226 ectxt->offset += size;
227 return 0;
228}
229
230/*
231 * Helper function that writes a type to the context.
232 *
233 * @ectxt context initialized by calling msg_encode_start()
234 * @type primitive type
235 * @size size of primitive in bytes
236 */
237static inline int tsv_write_header(struct encode_context *ectxt,
238 uint32_t type, uint32_t size)
239{
240 struct tsv_header hdr;
241
242 hdr.type = (unsigned char)type;
243 hdr.size = (unsigned char)size;
244 return tsv_write_data(ectxt, &hdr, sizeof(hdr));
245}
246
247/*
248 * Writes the current timestamp count.
249 *
250 * @ectxt context initialized by calling msg_encode_start()
251 */
252int tsv_timestamp_write(struct encode_context *ectxt)
253{
254 int ret;
255 unsigned long long t_now = sched_clock();
256
257 ret = tsv_write_header(ectxt, TSV_TYPE_TIMESTAMP, sizeof(t_now));
258 if (ret)
259 return ret;
260 return tsv_write_data(ectxt, &t_now, sizeof(t_now));
261}
262EXPORT_SYMBOL(tsv_timestamp_write);
263
264/*
265 * Writes a data pointer.
266 *
267 * @ectxt context initialized by calling msg_encode_start()
268 * @pointer pointer value to write
269 */
270int tsv_pointer_write(struct encode_context *ectxt, void *pointer)
271{
272 int ret;
273 ret = tsv_write_header(ectxt, TSV_TYPE_POINTER, sizeof(pointer));
274 if (ret)
275 return ret;
276 return tsv_write_data(ectxt, &pointer, sizeof(pointer));
277}
278EXPORT_SYMBOL(tsv_pointer_write);
279
280/*
281 * Writes a 32-bit integer value.
282 *
283 * @ectxt context initialized by calling msg_encode_start()
284 * @n integer to write
285 */
286int tsv_int32_write(struct encode_context *ectxt, int32_t n)
287{
288 int ret;
289 ret = tsv_write_header(ectxt, TSV_TYPE_INT32, sizeof(n));
290 if (ret)
291 return ret;
292 return tsv_write_data(ectxt, &n, sizeof(n));
293}
294EXPORT_SYMBOL(tsv_int32_write);
295
296/*
297 * Writes a byte array.
298 *
299 * @ectxt context initialized by calling msg_write_start()
300 * @data Beginning address of data
301 * @data_size Size of data to be written
302 */
303int tsv_byte_array_write(struct encode_context *ectxt,
304 void *data, int data_size)
305{
306 int ret;
307 ret = tsv_write_header(ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
308 if (ret)
309 return ret;
310 return tsv_write_data(ectxt, data, data_size);
311}
312EXPORT_SYMBOL(tsv_byte_array_write);
313
314/*
315 * Helper function to log a string
316 *
317 * @ilctxt ipc_log_context created using ipc_log_context_create()
318 * @fmt Data specified using format specifiers
319 */
320int ipc_log_string(void *ilctxt, const char *fmt, ...)
321{
322 struct encode_context ectxt;
323 int avail_size, data_size, hdr_size = sizeof(struct tsv_header);
324 va_list arg_list;
325
326 if (!ilctxt)
327 return -EINVAL;
328
329 msg_encode_start(&ectxt, TSV_TYPE_STRING);
330 tsv_timestamp_write(&ectxt);
331 avail_size = (MAX_MSG_SIZE - (ectxt.offset + hdr_size));
332 va_start(arg_list, fmt);
333 data_size = vsnprintf((ectxt.buff + ectxt.offset + hdr_size),
334 avail_size, fmt, arg_list);
335 va_end(arg_list);
336 tsv_write_header(&ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
337 ectxt.offset += data_size;
338 msg_encode_end(&ectxt);
339 ipc_log_write(ilctxt, &ectxt);
340 return 0;
341}
342
343/*
344 * Helper funtion used to read data from a message context.
345 *
346 * @ectxt context initialized by calling msg_read()
347 * @data data to read
348 * @size number of bytes of data to read
349 */
350static void tsv_read_data(struct encode_context *ectxt,
351 void *data, uint32_t size)
352{
353 BUG_ON((ectxt->offset + size) > MAX_MSG_SIZE);
354 memcpy(data, (ectxt->buff + ectxt->offset), size);
355 ectxt->offset += size;
356}
357
358/*
359 * Helper function that reads a type from the context and updates the
360 * context pointers.
361 *
362 * @ectxt context initialized by calling msg_read()
363 * @hdr type header
364 */
365static void tsv_read_header(struct encode_context *ectxt,
366 struct tsv_header *hdr)
367{
368 BUG_ON((ectxt->offset + sizeof(*hdr)) > MAX_MSG_SIZE);
369 memcpy(hdr, (ectxt->buff + ectxt->offset), sizeof(*hdr));
370 ectxt->offset += sizeof(*hdr);
371}
372
373/*
374 * Reads a timestamp.
375 *
376 * @ectxt context initialized by calling msg_read()
377 * @dctxt deserialization context
378 * @format output format (appended to %6u.%09u timestamp format)
379 */
380void tsv_timestamp_read(struct encode_context *ectxt,
381 struct decode_context *dctxt, const char *format)
382{
383 struct tsv_header hdr;
384 unsigned long long val;
385 unsigned long nanosec_rem;
386
387 tsv_read_header(ectxt, &hdr);
388 BUG_ON(hdr.type != TSV_TYPE_TIMESTAMP);
389 tsv_read_data(ectxt, &val, sizeof(val));
390 nanosec_rem = do_div(val, 1000000000U);
391 IPC_SPRINTF_DECODE(dctxt, "[%6u.%09lu]%s",
392 (unsigned)val, nanosec_rem, format);
393}
394EXPORT_SYMBOL(tsv_timestamp_read);
395
396/*
397 * Reads a data pointer.
398 *
399 * @ectxt context initialized by calling msg_read()
400 * @dctxt deserialization context
401 * @format output format
402 */
403void tsv_pointer_read(struct encode_context *ectxt,
404 struct decode_context *dctxt, const char *format)
405{
406 struct tsv_header hdr;
407 void *val;
408
409 tsv_read_header(ectxt, &hdr);
410 BUG_ON(hdr.type != TSV_TYPE_POINTER);
411 tsv_read_data(ectxt, &val, sizeof(val));
412
413 IPC_SPRINTF_DECODE(dctxt, format, val);
414}
415EXPORT_SYMBOL(tsv_pointer_read);
416
417/*
418 * Reads a 32-bit integer value.
419 *
420 * @ectxt context initialized by calling msg_read()
421 * @dctxt deserialization context
422 * @format output format
423 */
424int32_t tsv_int32_read(struct encode_context *ectxt,
425 struct decode_context *dctxt, const char *format)
426{
427 struct tsv_header hdr;
428 int32_t val;
429
430 tsv_read_header(ectxt, &hdr);
431 BUG_ON(hdr.type != TSV_TYPE_INT32);
432 tsv_read_data(ectxt, &val, sizeof(val));
433
434 IPC_SPRINTF_DECODE(dctxt, format, val);
435 return val;
436}
437EXPORT_SYMBOL(tsv_int32_read);
438
439/*
440 * Reads a byte array/string.
441 *
442 * @ectxt context initialized by calling msg_read()
443 * @dctxt deserialization context
444 * @format output format
445 */
446void tsv_byte_array_read(struct encode_context *ectxt,
447 struct decode_context *dctxt, const char *format)
448{
449 struct tsv_header hdr;
450
451 tsv_read_header(ectxt, &hdr);
452 BUG_ON(hdr.type != TSV_TYPE_BYTE_ARRAY);
453 tsv_read_data(ectxt, dctxt->buff, hdr.size);
454 dctxt->buff += hdr.size;
455 dctxt->size -= hdr.size;
456}
457EXPORT_SYMBOL(tsv_byte_array_read);
458
459int add_deserialization_func(void *ctxt, int type,
460 void (*dfunc)(struct encode_context *,
461 struct decode_context *))
462{
463 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
464 struct dfunc_info *df_info;
465 unsigned long flags;
466
467 if (!ilctxt || !dfunc)
468 return -EINVAL;
469
470 df_info = kmalloc(sizeof(struct dfunc_info), GFP_KERNEL);
471 if (!df_info)
472 return -ENOSPC;
473
474 spin_lock_irqsave(&ipc_log_context_list_lock, flags);
475 spin_lock(&ilctxt->ipc_log_context_lock);
476 df_info->type = type;
477 df_info->dfunc = dfunc;
478 list_add_tail(&df_info->list, &ilctxt->dfunc_info_list);
479 spin_unlock(&ilctxt->ipc_log_context_lock);
480 spin_unlock_irqrestore(&ipc_log_context_list_lock, flags);
481 return 0;
482}
483EXPORT_SYMBOL(add_deserialization_func);
484
485void *ipc_log_context_create(int max_num_pages,
486 const char *mod_name)
487{
488 struct ipc_log_context *ctxt;
489 struct ipc_log_page *pg = NULL;
490 int page_cnt, local_log_id;
491 unsigned long flags;
492
493 ctxt = kzalloc(sizeof(struct ipc_log_context), GFP_KERNEL);
494 if (!ctxt) {
495 pr_err("%s: cannot create ipc_log_context\n", __func__);
496 return 0;
497 }
498
499 local_log_id = atomic_add_return(1, &next_log_id);
500 init_completion(&ctxt->read_avail);
501 INIT_LIST_HEAD(&ctxt->page_list);
502 INIT_LIST_HEAD(&ctxt->dfunc_info_list);
503 spin_lock_init(&ctxt->ipc_log_context_lock);
504 for (page_cnt = 0; page_cnt < max_num_pages; page_cnt++) {
505 pg = kzalloc(sizeof(struct ipc_log_page), GFP_KERNEL);
506 if (!pg) {
507 pr_err("%s: cannot create ipc_log_page\n", __func__);
508 goto release_ipc_log_context;
509 }
510 pg->hdr.magic = IPC_LOGGING_MAGIC_NUM;
511 pg->hdr.nmagic = ~(IPC_LOGGING_MAGIC_NUM);
512 pg->hdr.log_id = (uint32_t)local_log_id;
513 pg->hdr.page_num = page_cnt;
514 pg->hdr.read_offset = 0xFFFF;
515 pg->hdr.write_offset = 0xFFFF;
516 spin_lock_irqsave(&ctxt->ipc_log_context_lock, flags);
517 list_add_tail(&pg->hdr.list, &ctxt->page_list);
518 spin_unlock_irqrestore(&ctxt->ipc_log_context_lock, flags);
519 }
520 ctxt->first_page = get_first_page(ctxt);
521 ctxt->last_page = pg;
522 ctxt->write_page = ctxt->first_page;
523 ctxt->read_page = ctxt->first_page;
524 ctxt->write_page->hdr.write_offset = 0;
525 ctxt->read_page->hdr.read_offset = 0;
526 ctxt->write_avail = max_num_pages * (PAGE_SIZE -
527 sizeof(struct ipc_log_page_header));
528
529 create_ctx_debugfs(ctxt, mod_name);
530
531 spin_lock_irqsave(&ipc_log_context_list_lock, flags);
532 list_add_tail(&ctxt->list, &ipc_log_context_list);
533 spin_unlock_irqrestore(&ipc_log_context_list_lock, flags);
534 return (void *)ctxt;
535
536release_ipc_log_context:
537 while (page_cnt-- > 0) {
538 pg = get_first_page(ctxt);
539 list_del(&pg->hdr.list);
540 kfree(pg);
541 }
542 kfree(ctxt);
543 return 0;
544}
545EXPORT_SYMBOL(ipc_log_context_create);
546
547static int __init ipc_logging_init(void)
548{
549 check_and_create_debugfs();
550 return 0;
551}
552
553module_init(ipc_logging_init);
554
555MODULE_DESCRIPTION("ipc logging");
556MODULE_LICENSE("GPL v2");