blob: 62110a33b5f1f755e33d45d52607ef8eb7cc8d7d [file] [log] [blame]
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -06001/* Copyright (c) 2012-2016, The Linux Foundation. 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 <asm/arch_timer.h>
15#include <linux/slab.h>
16#include <linux/uaccess.h>
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/jiffies.h>
22#include <linux/debugfs.h>
23#include <linux/io.h>
24#include <linux/idr.h>
25#include <linux/string.h>
26#include <linux/sched.h>
27#include <linux/wait.h>
28#include <linux/delay.h>
29#include <linux/completion.h>
30#include <linux/ipc_logging.h>
31
32#include "ipc_logging_private.h"
33
34#define LOG_PAGE_DATA_SIZE sizeof(((struct ipc_log_page *)0)->data)
35#define LOG_PAGE_FLAG (1 << 31)
36
37static LIST_HEAD(ipc_log_context_list);
38static DEFINE_RWLOCK(context_list_lock_lha1);
39static void *get_deserialization_func(struct ipc_log_context *ilctxt,
40 int type);
41
42static struct ipc_log_page *get_first_page(struct ipc_log_context *ilctxt)
43{
44 struct ipc_log_page_header *p_pghdr;
45 struct ipc_log_page *pg = NULL;
46
47 if (!ilctxt)
48 return NULL;
49 p_pghdr = list_first_entry(&ilctxt->page_list,
50 struct ipc_log_page_header, list);
51 pg = container_of(p_pghdr, struct ipc_log_page, hdr);
52 return pg;
53}
54
55/**
56 * is_nd_read_empty - Returns true if no data is available to read in log
57 *
58 * @ilctxt: logging context
59 * @returns: > 1 if context is empty; 0 if not empty; <0 for failure
60 *
61 * This is for the debugfs read pointer which allows for a non-destructive read.
62 * There may still be data in the log, but it may have already been read.
63 */
64static int is_nd_read_empty(struct ipc_log_context *ilctxt)
65{
66 if (!ilctxt)
67 return -EINVAL;
68
69 return ((ilctxt->nd_read_page == ilctxt->write_page) &&
70 (ilctxt->nd_read_page->hdr.nd_read_offset ==
71 ilctxt->write_page->hdr.write_offset));
72}
73
74/**
75 * is_read_empty - Returns true if no data is available in log
76 *
77 * @ilctxt: logging context
78 * @returns: > 1 if context is empty; 0 if not empty; <0 for failure
79 *
80 * This is for the actual log contents. If it is empty, then there
81 * is no data at all in the log.
82 */
83static int is_read_empty(struct ipc_log_context *ilctxt)
84{
85 if (!ilctxt)
86 return -EINVAL;
87
88 return ((ilctxt->read_page == ilctxt->write_page) &&
89 (ilctxt->read_page->hdr.read_offset ==
90 ilctxt->write_page->hdr.write_offset));
91}
92
93/**
94 * is_nd_read_equal_read - Return true if the non-destructive read is equal to
95 * the destructive read
96 *
97 * @ilctxt: logging context
98 * @returns: true if nd read is equal to read; false otherwise
99 */
100static bool is_nd_read_equal_read(struct ipc_log_context *ilctxt)
101{
102 uint16_t read_offset;
103 uint16_t nd_read_offset;
104
105 if (ilctxt->nd_read_page == ilctxt->read_page) {
106 read_offset = ilctxt->read_page->hdr.read_offset;
107 nd_read_offset = ilctxt->nd_read_page->hdr.nd_read_offset;
108
109 if (read_offset == nd_read_offset)
110 return true;
111 }
112
113 return false;
114}
115
116
117static struct ipc_log_page *get_next_page(struct ipc_log_context *ilctxt,
118 struct ipc_log_page *cur_pg)
119{
120 struct ipc_log_page_header *p_pghdr;
121 struct ipc_log_page *pg = NULL;
122
123 if (!ilctxt || !cur_pg)
124 return NULL;
125
126 if (ilctxt->last_page == cur_pg)
127 return ilctxt->first_page;
128
129 p_pghdr = list_first_entry(&cur_pg->hdr.list,
130 struct ipc_log_page_header, list);
131 pg = container_of(p_pghdr, struct ipc_log_page, hdr);
132
133 return pg;
134}
135
136/**
137 * ipc_log_read - do non-destructive read of the log
138 *
139 * @ilctxt: Logging context
140 * @data: Data pointer to receive the data
141 * @data_size: Number of bytes to read (must be <= bytes available in log)
142 *
143 * This read will update a runtime read pointer, but will not affect the actual
144 * contents of the log which allows for reading the logs continuously while
145 * debugging and if the system crashes, then the full logs can still be
146 * extracted.
147 */
148static void ipc_log_read(struct ipc_log_context *ilctxt,
149 void *data, int data_size)
150{
151 int bytes_to_read;
152
153 bytes_to_read = MIN(LOG_PAGE_DATA_SIZE
154 - ilctxt->nd_read_page->hdr.nd_read_offset,
155 data_size);
156
157 memcpy(data, (ilctxt->nd_read_page->data +
158 ilctxt->nd_read_page->hdr.nd_read_offset), bytes_to_read);
159
160 if (bytes_to_read != data_size) {
161 /* not enough space, wrap read to next page */
162 ilctxt->nd_read_page->hdr.nd_read_offset = 0;
163 ilctxt->nd_read_page = get_next_page(ilctxt,
164 ilctxt->nd_read_page);
165 if (WARN_ON(ilctxt->nd_read_page == NULL))
166 return;
167
168 memcpy((data + bytes_to_read),
169 (ilctxt->nd_read_page->data +
170 ilctxt->nd_read_page->hdr.nd_read_offset),
171 (data_size - bytes_to_read));
172 bytes_to_read = (data_size - bytes_to_read);
173 }
174 ilctxt->nd_read_page->hdr.nd_read_offset += bytes_to_read;
175}
176
177/**
178 * ipc_log_drop - do destructive read of the log
179 *
180 * @ilctxt: Logging context
181 * @data: Data pointer to receive the data (or NULL)
182 * @data_size: Number of bytes to read (must be <= bytes available in log)
183 */
184static void ipc_log_drop(struct ipc_log_context *ilctxt, void *data,
185 int data_size)
186{
187 int bytes_to_read;
188 bool push_nd_read;
189
190 bytes_to_read = MIN(LOG_PAGE_DATA_SIZE
191 - ilctxt->read_page->hdr.read_offset,
192 data_size);
193 if (data)
194 memcpy(data, (ilctxt->read_page->data +
195 ilctxt->read_page->hdr.read_offset), bytes_to_read);
196
197 if (bytes_to_read != data_size) {
198 /* not enough space, wrap read to next page */
199 push_nd_read = is_nd_read_equal_read(ilctxt);
200
201 ilctxt->read_page->hdr.read_offset = 0;
202 if (push_nd_read) {
203 ilctxt->read_page->hdr.nd_read_offset = 0;
204 ilctxt->read_page = get_next_page(ilctxt,
205 ilctxt->read_page);
206 if (WARN_ON(ilctxt->read_page == NULL))
207 return;
208 ilctxt->nd_read_page = ilctxt->read_page;
209 } else {
210 ilctxt->read_page = get_next_page(ilctxt,
211 ilctxt->read_page);
212 if (WARN_ON(ilctxt->read_page == NULL))
213 return;
214 }
215
216 if (data)
217 memcpy((data + bytes_to_read),
218 (ilctxt->read_page->data +
219 ilctxt->read_page->hdr.read_offset),
220 (data_size - bytes_to_read));
221
222 bytes_to_read = (data_size - bytes_to_read);
223 }
224
225 /* update non-destructive read pointer if necessary */
226 push_nd_read = is_nd_read_equal_read(ilctxt);
227 ilctxt->read_page->hdr.read_offset += bytes_to_read;
228 ilctxt->write_avail += data_size;
229
230 if (push_nd_read)
231 ilctxt->nd_read_page->hdr.nd_read_offset += bytes_to_read;
232}
233
234/**
235 * msg_read - Reads a message.
236 *
237 * If a message is read successfully, then the message context
238 * will be set to:
239 * .hdr message header .size and .type values
240 * .offset beginning of message data
241 *
242 * @ilctxt Logging context
243 * @ectxt Message context
244 *
245 * @returns 0 - no message available; >0 message size; <0 error
246 */
247static int msg_read(struct ipc_log_context *ilctxt,
248 struct encode_context *ectxt)
249{
250 struct tsv_header hdr;
251
252 if (!ectxt)
253 return -EINVAL;
254
255 if (is_nd_read_empty(ilctxt))
256 return 0;
257
258 ipc_log_read(ilctxt, &hdr, sizeof(hdr));
259 ectxt->hdr.type = hdr.type;
260 ectxt->hdr.size = hdr.size;
261 ectxt->offset = sizeof(hdr);
262 ipc_log_read(ilctxt, (ectxt->buff + ectxt->offset),
263 (int)hdr.size);
264
265 return sizeof(hdr) + (int)hdr.size;
266}
267
268/**
269 * msg_drop - Drops a message.
270 *
271 * @ilctxt Logging context
272 */
273static void msg_drop(struct ipc_log_context *ilctxt)
274{
275 struct tsv_header hdr;
276
277 if (!is_read_empty(ilctxt)) {
278 ipc_log_drop(ilctxt, &hdr, sizeof(hdr));
279 ipc_log_drop(ilctxt, NULL, (int)hdr.size);
280 }
281}
282
283/*
284 * Commits messages to the FIFO. If the FIFO is full, then enough
285 * messages are dropped to create space for the new message.
286 */
287void ipc_log_write(void *ctxt, struct encode_context *ectxt)
288{
289 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
290 int bytes_to_write;
291 unsigned long flags;
292
293 if (!ilctxt || !ectxt) {
294 pr_err("%s: Invalid ipc_log or encode context\n", __func__);
295 return;
296 }
297
298 read_lock_irqsave(&context_list_lock_lha1, flags);
299 spin_lock(&ilctxt->context_lock_lhb1);
300 while (ilctxt->write_avail <= ectxt->offset)
301 msg_drop(ilctxt);
302
303 bytes_to_write = MIN(LOG_PAGE_DATA_SIZE
304 - ilctxt->write_page->hdr.write_offset,
305 ectxt->offset);
306 memcpy((ilctxt->write_page->data +
307 ilctxt->write_page->hdr.write_offset),
308 ectxt->buff, bytes_to_write);
309
310 if (bytes_to_write != ectxt->offset) {
311 uint64_t t_now = sched_clock();
312
313 ilctxt->write_page->hdr.write_offset += bytes_to_write;
314 ilctxt->write_page->hdr.end_time = t_now;
315
316 ilctxt->write_page = get_next_page(ilctxt, ilctxt->write_page);
317 if (WARN_ON(ilctxt->write_page == NULL))
318 return;
319 ilctxt->write_page->hdr.write_offset = 0;
320 ilctxt->write_page->hdr.start_time = t_now;
321 memcpy((ilctxt->write_page->data +
322 ilctxt->write_page->hdr.write_offset),
323 (ectxt->buff + bytes_to_write),
324 (ectxt->offset - bytes_to_write));
325 bytes_to_write = (ectxt->offset - bytes_to_write);
326 }
327 ilctxt->write_page->hdr.write_offset += bytes_to_write;
328 ilctxt->write_avail -= ectxt->offset;
329 complete(&ilctxt->read_avail);
330 spin_unlock(&ilctxt->context_lock_lhb1);
331 read_unlock_irqrestore(&context_list_lock_lha1, flags);
332}
333EXPORT_SYMBOL(ipc_log_write);
334
335/*
336 * Starts a new message after which you can add serialized data and
337 * then complete the message by calling msg_encode_end().
338 */
339void msg_encode_start(struct encode_context *ectxt, uint32_t type)
340{
341 if (!ectxt) {
342 pr_err("%s: Invalid encode context\n", __func__);
343 return;
344 }
345
346 ectxt->hdr.type = type;
347 ectxt->hdr.size = 0;
348 ectxt->offset = sizeof(ectxt->hdr);
349}
350EXPORT_SYMBOL(msg_encode_start);
351
352/*
353 * Completes the message
354 */
355void msg_encode_end(struct encode_context *ectxt)
356{
357 if (!ectxt) {
358 pr_err("%s: Invalid encode context\n", __func__);
359 return;
360 }
361
362 /* finalize data size */
363 ectxt->hdr.size = ectxt->offset - sizeof(ectxt->hdr);
364 if (WARN_ON(ectxt->hdr.size > MAX_MSG_SIZE))
365 return;
366 memcpy(ectxt->buff, &ectxt->hdr, sizeof(ectxt->hdr));
367}
368EXPORT_SYMBOL(msg_encode_end);
369
370/*
371 * Helper function used to write data to a message context.
372 *
373 * @ectxt context initialized by calling msg_encode_start()
374 * @data data to write
375 * @size number of bytes of data to write
376 */
377static inline int tsv_write_data(struct encode_context *ectxt,
378 void *data, uint32_t size)
379{
380 if (!ectxt) {
381 pr_err("%s: Invalid encode context\n", __func__);
382 return -EINVAL;
383 }
384 if ((ectxt->offset + size) > MAX_MSG_SIZE) {
385 pr_err("%s: No space to encode further\n", __func__);
386 return -EINVAL;
387 }
388
389 memcpy((void *)(ectxt->buff + ectxt->offset), data, size);
390 ectxt->offset += size;
391 return 0;
392}
393
394/*
395 * Helper function that writes a type to the context.
396 *
397 * @ectxt context initialized by calling msg_encode_start()
398 * @type primitive type
399 * @size size of primitive in bytes
400 */
401static inline int tsv_write_header(struct encode_context *ectxt,
402 uint32_t type, uint32_t size)
403{
404 struct tsv_header hdr;
405
406 hdr.type = (unsigned char)type;
407 hdr.size = (unsigned char)size;
408 return tsv_write_data(ectxt, &hdr, sizeof(hdr));
409}
410
411/*
412 * Writes the current timestamp count.
413 *
414 * @ectxt context initialized by calling msg_encode_start()
415 */
416int tsv_timestamp_write(struct encode_context *ectxt)
417{
418 int ret;
419 uint64_t t_now = sched_clock();
420
421 ret = tsv_write_header(ectxt, TSV_TYPE_TIMESTAMP, sizeof(t_now));
422 if (ret)
423 return ret;
424 return tsv_write_data(ectxt, &t_now, sizeof(t_now));
425}
426EXPORT_SYMBOL(tsv_timestamp_write);
427
428/*
429 * Writes the current QTimer timestamp count.
430 *
431 * @ectxt context initialized by calling msg_encode_start()
432 */
433int tsv_qtimer_write(struct encode_context *ectxt)
434{
435 int ret;
436 uint64_t t_now = arch_counter_get_cntvct();
437
438 ret = tsv_write_header(ectxt, TSV_TYPE_QTIMER, sizeof(t_now));
439 if (ret)
440 return ret;
441 return tsv_write_data(ectxt, &t_now, sizeof(t_now));
442}
443EXPORT_SYMBOL(tsv_qtimer_write);
444
445/*
446 * Writes a data pointer.
447 *
448 * @ectxt context initialized by calling msg_encode_start()
449 * @pointer pointer value to write
450 */
451int tsv_pointer_write(struct encode_context *ectxt, void *pointer)
452{
453 int ret;
454
455 ret = tsv_write_header(ectxt, TSV_TYPE_POINTER, sizeof(pointer));
456 if (ret)
457 return ret;
458 return tsv_write_data(ectxt, &pointer, sizeof(pointer));
459}
460EXPORT_SYMBOL(tsv_pointer_write);
461
462/*
463 * Writes a 32-bit integer value.
464 *
465 * @ectxt context initialized by calling msg_encode_start()
466 * @n integer to write
467 */
468int tsv_int32_write(struct encode_context *ectxt, int32_t n)
469{
470 int ret;
471
472 ret = tsv_write_header(ectxt, TSV_TYPE_INT32, sizeof(n));
473 if (ret)
474 return ret;
475 return tsv_write_data(ectxt, &n, sizeof(n));
476}
477EXPORT_SYMBOL(tsv_int32_write);
478
479/*
480 * Writes a byte array.
481 *
482 * @ectxt context initialized by calling msg_write_start()
483 * @data Beginning address of data
484 * @data_size Size of data to be written
485 */
486int tsv_byte_array_write(struct encode_context *ectxt,
487 void *data, int data_size)
488{
489 int ret;
490
491 ret = tsv_write_header(ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
492 if (ret)
493 return ret;
494 return tsv_write_data(ectxt, data, data_size);
495}
496EXPORT_SYMBOL(tsv_byte_array_write);
497
498/*
499 * Helper function to log a string
500 *
501 * @ilctxt ipc_log_context created using ipc_log_context_create()
502 * @fmt Data specified using format specifiers
503 */
504int ipc_log_string(void *ilctxt, const char *fmt, ...)
505{
506 struct encode_context ectxt;
507 int avail_size, data_size, hdr_size = sizeof(struct tsv_header);
508 va_list arg_list;
509
510 if (!ilctxt)
511 return -EINVAL;
512
513 msg_encode_start(&ectxt, TSV_TYPE_STRING);
514 tsv_timestamp_write(&ectxt);
515 tsv_qtimer_write(&ectxt);
516 avail_size = (MAX_MSG_SIZE - (ectxt.offset + hdr_size));
517 va_start(arg_list, fmt);
518 data_size = vsnprintf((ectxt.buff + ectxt.offset + hdr_size),
519 avail_size, fmt, arg_list);
520 va_end(arg_list);
521 tsv_write_header(&ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
522 ectxt.offset += data_size;
523 msg_encode_end(&ectxt);
524 ipc_log_write(ilctxt, &ectxt);
525 return 0;
526}
527EXPORT_SYMBOL(ipc_log_string);
528
529/**
530 * ipc_log_extract - Reads and deserializes log
531 *
532 * @ctxt: logging context
533 * @buff: buffer to receive the data
534 * @size: size of the buffer
535 * @returns: 0 if no data read; >0 number of bytes read; < 0 error
536 *
537 * If no data is available to be read, then the ilctxt::read_avail
538 * completion is reinitialized. This allows clients to block
539 * until new log data is save.
540 */
541int ipc_log_extract(void *ctxt, char *buff, int size)
542{
543 struct encode_context ectxt;
544 struct decode_context dctxt;
545 void (*deserialize_func)(struct encode_context *ectxt,
546 struct decode_context *dctxt);
547 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
548 unsigned long flags;
549
550 if (size < MAX_MSG_DECODED_SIZE)
551 return -EINVAL;
552
553 dctxt.output_format = OUTPUT_DEBUGFS;
554 dctxt.buff = buff;
555 dctxt.size = size;
556 read_lock_irqsave(&context_list_lock_lha1, flags);
557 spin_lock(&ilctxt->context_lock_lhb1);
558 while (dctxt.size >= MAX_MSG_DECODED_SIZE &&
559 !is_nd_read_empty(ilctxt)) {
560 msg_read(ilctxt, &ectxt);
561 deserialize_func = get_deserialization_func(ilctxt,
562 ectxt.hdr.type);
563 spin_unlock(&ilctxt->context_lock_lhb1);
564 read_unlock_irqrestore(&context_list_lock_lha1, flags);
565 if (deserialize_func)
566 deserialize_func(&ectxt, &dctxt);
567 else
568 pr_err("%s: unknown message 0x%x\n",
569 __func__, ectxt.hdr.type);
570 read_lock_irqsave(&context_list_lock_lha1, flags);
571 spin_lock(&ilctxt->context_lock_lhb1);
572 }
573 if ((size - dctxt.size) == 0)
574 reinit_completion(&ilctxt->read_avail);
575 spin_unlock(&ilctxt->context_lock_lhb1);
576 read_unlock_irqrestore(&context_list_lock_lha1, flags);
577 return size - dctxt.size;
578}
579EXPORT_SYMBOL(ipc_log_extract);
580
581/*
582 * Helper function used to read data from a message context.
583 *
584 * @ectxt context initialized by calling msg_read()
585 * @data data to read
586 * @size number of bytes of data to read
587 */
588static void tsv_read_data(struct encode_context *ectxt,
589 void *data, uint32_t size)
590{
591 if (WARN_ON((ectxt->offset + size) > MAX_MSG_SIZE))
592 return;
593 memcpy(data, (ectxt->buff + ectxt->offset), size);
594 ectxt->offset += size;
595}
596
597/*
598 * Helper function that reads a type from the context and updates the
599 * context pointers.
600 *
601 * @ectxt context initialized by calling msg_read()
602 * @hdr type header
603 */
604static void tsv_read_header(struct encode_context *ectxt,
605 struct tsv_header *hdr)
606{
607 if (WARN_ON((ectxt->offset + sizeof(*hdr)) > MAX_MSG_SIZE))
608 return;
609 memcpy(hdr, (ectxt->buff + ectxt->offset), sizeof(*hdr));
610 ectxt->offset += sizeof(*hdr);
611}
612
613/*
614 * Reads a timestamp.
615 *
616 * @ectxt context initialized by calling msg_read()
617 * @dctxt deserialization context
618 * @format output format (appended to %6u.09u timestamp format)
619 */
620void tsv_timestamp_read(struct encode_context *ectxt,
621 struct decode_context *dctxt, const char *format)
622{
623 struct tsv_header hdr;
624 uint64_t val;
625 unsigned long nanosec_rem;
626
627 tsv_read_header(ectxt, &hdr);
628 if (WARN_ON(hdr.type != TSV_TYPE_TIMESTAMP))
629 return;
630 tsv_read_data(ectxt, &val, sizeof(val));
631 nanosec_rem = do_div(val, 1000000000U);
632 IPC_SPRINTF_DECODE(dctxt, "[%6u.%09lu%s/",
633 (unsigned int)val, nanosec_rem, format);
634}
635EXPORT_SYMBOL(tsv_timestamp_read);
636
637/*
638 * Reads a QTimer timestamp.
639 *
640 * @ectxt context initialized by calling msg_read()
641 * @dctxt deserialization context
642 * @format output format (appended to %#18llx timestamp format)
643 */
644void tsv_qtimer_read(struct encode_context *ectxt,
645 struct decode_context *dctxt, const char *format)
646{
647 struct tsv_header hdr;
648 uint64_t val;
649
650 tsv_read_header(ectxt, &hdr);
651 if (WARN_ON(hdr.type != TSV_TYPE_QTIMER))
652 return;
653 tsv_read_data(ectxt, &val, sizeof(val));
654
655 /*
656 * This gives 16 hex digits of output. The # prefix prepends
657 * a 0x, and these characters count as part of the number.
658 */
659 IPC_SPRINTF_DECODE(dctxt, "%#18llx]%s", val, format);
660}
661EXPORT_SYMBOL(tsv_qtimer_read);
662
663/*
664 * Reads a data pointer.
665 *
666 * @ectxt context initialized by calling msg_read()
667 * @dctxt deserialization context
668 * @format output format
669 */
670void tsv_pointer_read(struct encode_context *ectxt,
671 struct decode_context *dctxt, const char *format)
672{
673 struct tsv_header hdr;
674 void *val;
675
676 tsv_read_header(ectxt, &hdr);
677 if (WARN_ON(hdr.type != TSV_TYPE_POINTER))
678 return;
679 tsv_read_data(ectxt, &val, sizeof(val));
680
681 IPC_SPRINTF_DECODE(dctxt, format, val);
682}
683EXPORT_SYMBOL(tsv_pointer_read);
684
685/*
686 * Reads a 32-bit integer value.
687 *
688 * @ectxt context initialized by calling msg_read()
689 * @dctxt deserialization context
690 * @format output format
691 */
692int32_t tsv_int32_read(struct encode_context *ectxt,
693 struct decode_context *dctxt, const char *format)
694{
695 struct tsv_header hdr;
696 int32_t val;
697
698 tsv_read_header(ectxt, &hdr);
699 if (WARN_ON(hdr.type != TSV_TYPE_INT32))
700 return -EINVAL;
701 tsv_read_data(ectxt, &val, sizeof(val));
702
703 IPC_SPRINTF_DECODE(dctxt, format, val);
704 return val;
705}
706EXPORT_SYMBOL(tsv_int32_read);
707
708/*
709 * Reads a byte array/string.
710 *
711 * @ectxt context initialized by calling msg_read()
712 * @dctxt deserialization context
713 * @format output format
714 */
715void tsv_byte_array_read(struct encode_context *ectxt,
716 struct decode_context *dctxt, const char *format)
717{
718 struct tsv_header hdr;
719
720 tsv_read_header(ectxt, &hdr);
721 if (WARN_ON(hdr.type != TSV_TYPE_BYTE_ARRAY))
722 return;
723 tsv_read_data(ectxt, dctxt->buff, hdr.size);
724 dctxt->buff += hdr.size;
725 dctxt->size -= hdr.size;
726}
727EXPORT_SYMBOL(tsv_byte_array_read);
728
729int add_deserialization_func(void *ctxt, int type,
730 void (*dfunc)(struct encode_context *,
731 struct decode_context *))
732{
733 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
734 struct dfunc_info *df_info;
735 unsigned long flags;
736
737 if (!ilctxt || !dfunc)
738 return -EINVAL;
739
740 df_info = kmalloc(sizeof(struct dfunc_info), GFP_KERNEL);
741 if (!df_info)
742 return -ENOSPC;
743
744 read_lock_irqsave(&context_list_lock_lha1, flags);
745 spin_lock(&ilctxt->context_lock_lhb1);
746 df_info->type = type;
747 df_info->dfunc = dfunc;
748 list_add_tail(&df_info->list, &ilctxt->dfunc_info_list);
749 spin_unlock(&ilctxt->context_lock_lhb1);
750 read_unlock_irqrestore(&context_list_lock_lha1, flags);
751 return 0;
752}
753EXPORT_SYMBOL(add_deserialization_func);
754
755static void *get_deserialization_func(struct ipc_log_context *ilctxt,
756 int type)
757{
758 struct dfunc_info *df_info = NULL;
759
760 if (!ilctxt)
761 return NULL;
762
763 list_for_each_entry(df_info, &ilctxt->dfunc_info_list, list) {
764 if (df_info->type == type)
765 return df_info->dfunc;
766 }
767 return NULL;
768}
769
770/**
771 * ipc_log_context_create: Create a debug log context
772 * Should not be called from atomic context
773 *
774 * @max_num_pages: Number of pages of logging space required (max. 10)
775 * @mod_name : Name of the directory entry under DEBUGFS
776 * @user_version : Version number of user-defined message formats
777 *
778 * returns context id on success, NULL on failure
779 */
780void *ipc_log_context_create(int max_num_pages,
781 const char *mod_name, uint16_t user_version)
782{
783 struct ipc_log_context *ctxt;
784 struct ipc_log_page *pg = NULL;
785 int page_cnt;
786 unsigned long flags;
787
788 ctxt = kzalloc(sizeof(struct ipc_log_context), GFP_KERNEL);
789 if (!ctxt)
790 return 0;
791
792 init_completion(&ctxt->read_avail);
793 INIT_LIST_HEAD(&ctxt->page_list);
794 INIT_LIST_HEAD(&ctxt->dfunc_info_list);
795 spin_lock_init(&ctxt->context_lock_lhb1);
796 for (page_cnt = 0; page_cnt < max_num_pages; page_cnt++) {
797 pg = kzalloc(sizeof(struct ipc_log_page), GFP_KERNEL);
798 if (!pg) {
799 pr_err("%s: cannot create ipc_log_page\n", __func__);
800 goto release_ipc_log_context;
801 }
802 pg->hdr.log_id = (uint64_t)(uintptr_t)ctxt;
803 pg->hdr.page_num = LOG_PAGE_FLAG | page_cnt;
804 pg->hdr.ctx_offset = (int64_t)((uint64_t)(uintptr_t)ctxt -
805 (uint64_t)(uintptr_t)&pg->hdr);
806
807 /* set magic last to signal that page init is complete */
808 pg->hdr.magic = IPC_LOGGING_MAGIC_NUM;
809 pg->hdr.nmagic = ~(IPC_LOGGING_MAGIC_NUM);
810
811 spin_lock_irqsave(&ctxt->context_lock_lhb1, flags);
812 list_add_tail(&pg->hdr.list, &ctxt->page_list);
813 spin_unlock_irqrestore(&ctxt->context_lock_lhb1, flags);
814 }
815
816 ctxt->log_id = (uint64_t)(uintptr_t)ctxt;
817 ctxt->version = IPC_LOG_VERSION;
818 strlcpy(ctxt->name, mod_name, IPC_LOG_MAX_CONTEXT_NAME_LEN);
819 ctxt->user_version = user_version;
820 ctxt->first_page = get_first_page(ctxt);
821 ctxt->last_page = pg;
822 ctxt->write_page = ctxt->first_page;
823 ctxt->read_page = ctxt->first_page;
824 ctxt->nd_read_page = ctxt->first_page;
825 ctxt->write_avail = max_num_pages * LOG_PAGE_DATA_SIZE;
826 ctxt->header_size = sizeof(struct ipc_log_page_header);
827 create_ctx_debugfs(ctxt, mod_name);
828
829 /* set magic last to signal context init is complete */
830 ctxt->magic = IPC_LOG_CONTEXT_MAGIC_NUM;
831 ctxt->nmagic = ~(IPC_LOG_CONTEXT_MAGIC_NUM);
832
833 write_lock_irqsave(&context_list_lock_lha1, flags);
834 list_add_tail(&ctxt->list, &ipc_log_context_list);
835 write_unlock_irqrestore(&context_list_lock_lha1, flags);
836 return (void *)ctxt;
837
838release_ipc_log_context:
839 while (page_cnt-- > 0) {
840 pg = get_first_page(ctxt);
841 list_del(&pg->hdr.list);
842 kfree(pg);
843 }
844 kfree(ctxt);
845 return 0;
846}
847EXPORT_SYMBOL(ipc_log_context_create);
848
849/*
850 * Destroy debug log context
851 *
852 * @ctxt: debug log context created by calling ipc_log_context_create API.
853 */
854int ipc_log_context_destroy(void *ctxt)
855{
856 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
857 struct ipc_log_page *pg = NULL;
858 unsigned long flags;
859
860 if (!ilctxt)
861 return 0;
862
863 while (!list_empty(&ilctxt->page_list)) {
864 pg = get_first_page(ctxt);
865 list_del(&pg->hdr.list);
866 kfree(pg);
867 }
868
869 write_lock_irqsave(&context_list_lock_lha1, flags);
870 list_del(&ilctxt->list);
871 write_unlock_irqrestore(&context_list_lock_lha1, flags);
872
873 debugfs_remove_recursive(ilctxt->dent);
874
875 kfree(ilctxt);
876 return 0;
877}
878EXPORT_SYMBOL(ipc_log_context_destroy);
879
880static int __init ipc_logging_init(void)
881{
882 check_and_create_debugfs();
883 return 0;
884}
885
886module_init(ipc_logging_init);
887
888MODULE_DESCRIPTION("ipc logging");
889MODULE_LICENSE("GPL v2");