blob: 36d8edb09057afde2cecec4f84885d5f6a041648 [file] [log] [blame]
Dhoat Harpalf76b2362018-03-22 20:37:38 +05301/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -06002 *
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
Dhoat Harpalf76b2362018-03-22 20:37:38 +0530298 if (ilctxt->disabled)
299 return;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600300 read_lock_irqsave(&context_list_lock_lha1, flags);
301 spin_lock(&ilctxt->context_lock_lhb1);
302 while (ilctxt->write_avail <= ectxt->offset)
303 msg_drop(ilctxt);
304
305 bytes_to_write = MIN(LOG_PAGE_DATA_SIZE
306 - ilctxt->write_page->hdr.write_offset,
307 ectxt->offset);
308 memcpy((ilctxt->write_page->data +
309 ilctxt->write_page->hdr.write_offset),
310 ectxt->buff, bytes_to_write);
311
312 if (bytes_to_write != ectxt->offset) {
313 uint64_t t_now = sched_clock();
314
315 ilctxt->write_page->hdr.write_offset += bytes_to_write;
316 ilctxt->write_page->hdr.end_time = t_now;
317
318 ilctxt->write_page = get_next_page(ilctxt, ilctxt->write_page);
Chris Lew273eea682017-06-15 18:35:11 -0700319 if (WARN_ON(ilctxt->write_page == NULL)) {
320 spin_unlock(&ilctxt->context_lock_lhb1);
321 read_unlock_irqrestore(&context_list_lock_lha1, flags);
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600322 return;
Chris Lew273eea682017-06-15 18:35:11 -0700323 }
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600324 ilctxt->write_page->hdr.write_offset = 0;
325 ilctxt->write_page->hdr.start_time = t_now;
326 memcpy((ilctxt->write_page->data +
327 ilctxt->write_page->hdr.write_offset),
328 (ectxt->buff + bytes_to_write),
329 (ectxt->offset - bytes_to_write));
330 bytes_to_write = (ectxt->offset - bytes_to_write);
331 }
332 ilctxt->write_page->hdr.write_offset += bytes_to_write;
333 ilctxt->write_avail -= ectxt->offset;
334 complete(&ilctxt->read_avail);
335 spin_unlock(&ilctxt->context_lock_lhb1);
336 read_unlock_irqrestore(&context_list_lock_lha1, flags);
337}
338EXPORT_SYMBOL(ipc_log_write);
339
340/*
341 * Starts a new message after which you can add serialized data and
342 * then complete the message by calling msg_encode_end().
343 */
344void msg_encode_start(struct encode_context *ectxt, uint32_t type)
345{
346 if (!ectxt) {
347 pr_err("%s: Invalid encode context\n", __func__);
348 return;
349 }
350
351 ectxt->hdr.type = type;
352 ectxt->hdr.size = 0;
353 ectxt->offset = sizeof(ectxt->hdr);
354}
355EXPORT_SYMBOL(msg_encode_start);
356
357/*
358 * Completes the message
359 */
360void msg_encode_end(struct encode_context *ectxt)
361{
362 if (!ectxt) {
363 pr_err("%s: Invalid encode context\n", __func__);
364 return;
365 }
366
367 /* finalize data size */
368 ectxt->hdr.size = ectxt->offset - sizeof(ectxt->hdr);
369 if (WARN_ON(ectxt->hdr.size > MAX_MSG_SIZE))
370 return;
371 memcpy(ectxt->buff, &ectxt->hdr, sizeof(ectxt->hdr));
372}
373EXPORT_SYMBOL(msg_encode_end);
374
375/*
376 * Helper function used to write data to a message context.
377 *
378 * @ectxt context initialized by calling msg_encode_start()
379 * @data data to write
380 * @size number of bytes of data to write
381 */
382static inline int tsv_write_data(struct encode_context *ectxt,
383 void *data, uint32_t size)
384{
385 if (!ectxt) {
386 pr_err("%s: Invalid encode context\n", __func__);
387 return -EINVAL;
388 }
389 if ((ectxt->offset + size) > MAX_MSG_SIZE) {
390 pr_err("%s: No space to encode further\n", __func__);
391 return -EINVAL;
392 }
393
394 memcpy((void *)(ectxt->buff + ectxt->offset), data, size);
395 ectxt->offset += size;
396 return 0;
397}
398
399/*
400 * Helper function that writes a type to the context.
401 *
402 * @ectxt context initialized by calling msg_encode_start()
403 * @type primitive type
404 * @size size of primitive in bytes
405 */
406static inline int tsv_write_header(struct encode_context *ectxt,
407 uint32_t type, uint32_t size)
408{
409 struct tsv_header hdr;
410
411 hdr.type = (unsigned char)type;
412 hdr.size = (unsigned char)size;
413 return tsv_write_data(ectxt, &hdr, sizeof(hdr));
414}
415
416/*
417 * Writes the current timestamp count.
418 *
419 * @ectxt context initialized by calling msg_encode_start()
420 */
421int tsv_timestamp_write(struct encode_context *ectxt)
422{
423 int ret;
424 uint64_t t_now = sched_clock();
425
426 ret = tsv_write_header(ectxt, TSV_TYPE_TIMESTAMP, sizeof(t_now));
427 if (ret)
428 return ret;
429 return tsv_write_data(ectxt, &t_now, sizeof(t_now));
430}
431EXPORT_SYMBOL(tsv_timestamp_write);
432
433/*
434 * Writes the current QTimer timestamp count.
435 *
436 * @ectxt context initialized by calling msg_encode_start()
437 */
438int tsv_qtimer_write(struct encode_context *ectxt)
439{
440 int ret;
441 uint64_t t_now = arch_counter_get_cntvct();
442
443 ret = tsv_write_header(ectxt, TSV_TYPE_QTIMER, sizeof(t_now));
444 if (ret)
445 return ret;
446 return tsv_write_data(ectxt, &t_now, sizeof(t_now));
447}
448EXPORT_SYMBOL(tsv_qtimer_write);
449
450/*
451 * Writes a data pointer.
452 *
453 * @ectxt context initialized by calling msg_encode_start()
454 * @pointer pointer value to write
455 */
456int tsv_pointer_write(struct encode_context *ectxt, void *pointer)
457{
458 int ret;
459
460 ret = tsv_write_header(ectxt, TSV_TYPE_POINTER, sizeof(pointer));
461 if (ret)
462 return ret;
463 return tsv_write_data(ectxt, &pointer, sizeof(pointer));
464}
465EXPORT_SYMBOL(tsv_pointer_write);
466
467/*
468 * Writes a 32-bit integer value.
469 *
470 * @ectxt context initialized by calling msg_encode_start()
471 * @n integer to write
472 */
473int tsv_int32_write(struct encode_context *ectxt, int32_t n)
474{
475 int ret;
476
477 ret = tsv_write_header(ectxt, TSV_TYPE_INT32, sizeof(n));
478 if (ret)
479 return ret;
480 return tsv_write_data(ectxt, &n, sizeof(n));
481}
482EXPORT_SYMBOL(tsv_int32_write);
483
484/*
485 * Writes a byte array.
486 *
487 * @ectxt context initialized by calling msg_write_start()
488 * @data Beginning address of data
489 * @data_size Size of data to be written
490 */
491int tsv_byte_array_write(struct encode_context *ectxt,
492 void *data, int data_size)
493{
494 int ret;
495
496 ret = tsv_write_header(ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
497 if (ret)
498 return ret;
499 return tsv_write_data(ectxt, data, data_size);
500}
501EXPORT_SYMBOL(tsv_byte_array_write);
502
503/*
504 * Helper function to log a string
505 *
506 * @ilctxt ipc_log_context created using ipc_log_context_create()
507 * @fmt Data specified using format specifiers
508 */
509int ipc_log_string(void *ilctxt, const char *fmt, ...)
510{
511 struct encode_context ectxt;
512 int avail_size, data_size, hdr_size = sizeof(struct tsv_header);
Dhoat Harpalf76b2362018-03-22 20:37:38 +0530513 struct ipc_log_context *ctxt = (struct ipc_log_context *)ilctxt;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600514 va_list arg_list;
515
516 if (!ilctxt)
517 return -EINVAL;
518
Dhoat Harpalf76b2362018-03-22 20:37:38 +0530519 if (ctxt->disabled)
520 return -EBUSY;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600521 msg_encode_start(&ectxt, TSV_TYPE_STRING);
522 tsv_timestamp_write(&ectxt);
523 tsv_qtimer_write(&ectxt);
524 avail_size = (MAX_MSG_SIZE - (ectxt.offset + hdr_size));
525 va_start(arg_list, fmt);
Jack Phamf123c9a2017-03-30 09:53:53 -0700526 data_size = vscnprintf((ectxt.buff + ectxt.offset + hdr_size),
527 avail_size, fmt, arg_list);
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600528 va_end(arg_list);
529 tsv_write_header(&ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
530 ectxt.offset += data_size;
531 msg_encode_end(&ectxt);
532 ipc_log_write(ilctxt, &ectxt);
533 return 0;
534}
535EXPORT_SYMBOL(ipc_log_string);
536
Dhoat Harpalf76b2362018-03-22 20:37:38 +0530537/*
538 * ipc_log_ctrl_all - disable/enable logging in all clients
539 *
540 * @ Data specified using format specifiers
541 */
542void ipc_log_ctrl_all(bool disable)
543{
544 struct ipc_log_context *ctxt = NULL;
545 unsigned long flags;
546
547 read_lock_irqsave(&context_list_lock_lha1, flags);
548 list_for_each_entry(ctxt, &ipc_log_context_list, list) {
549 if (disable) {
550 ipc_log_string(ctxt,
551 "LOGGING DISABLED FOR ALL CLIENTS!!\n");
552 ctxt->disabled = disable;
553 } else {
554 ctxt->disabled = disable;
555 ipc_log_string(ctxt,
556 "LOGGING ENABLED FOR ALL CLIENTS!!\n");
557 }
558 }
559 read_unlock_irqrestore(&context_list_lock_lha1, flags);
560}
561EXPORT_SYMBOL(ipc_log_ctrl_all);
562
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600563/**
564 * ipc_log_extract - Reads and deserializes log
565 *
566 * @ctxt: logging context
567 * @buff: buffer to receive the data
568 * @size: size of the buffer
569 * @returns: 0 if no data read; >0 number of bytes read; < 0 error
570 *
571 * If no data is available to be read, then the ilctxt::read_avail
572 * completion is reinitialized. This allows clients to block
573 * until new log data is save.
574 */
575int ipc_log_extract(void *ctxt, char *buff, int size)
576{
577 struct encode_context ectxt;
578 struct decode_context dctxt;
579 void (*deserialize_func)(struct encode_context *ectxt,
580 struct decode_context *dctxt);
581 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
582 unsigned long flags;
Lynus Vaz802d7382017-06-30 15:10:26 +0530583 int ret;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600584
585 if (size < MAX_MSG_DECODED_SIZE)
586 return -EINVAL;
587
588 dctxt.output_format = OUTPUT_DEBUGFS;
589 dctxt.buff = buff;
590 dctxt.size = size;
591 read_lock_irqsave(&context_list_lock_lha1, flags);
592 spin_lock(&ilctxt->context_lock_lhb1);
Lynus Vaz802d7382017-06-30 15:10:26 +0530593 if (ilctxt->destroyed) {
594 ret = -EIO;
595 goto done;
596 }
597
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600598 while (dctxt.size >= MAX_MSG_DECODED_SIZE &&
599 !is_nd_read_empty(ilctxt)) {
600 msg_read(ilctxt, &ectxt);
601 deserialize_func = get_deserialization_func(ilctxt,
602 ectxt.hdr.type);
603 spin_unlock(&ilctxt->context_lock_lhb1);
604 read_unlock_irqrestore(&context_list_lock_lha1, flags);
605 if (deserialize_func)
606 deserialize_func(&ectxt, &dctxt);
607 else
608 pr_err("%s: unknown message 0x%x\n",
609 __func__, ectxt.hdr.type);
610 read_lock_irqsave(&context_list_lock_lha1, flags);
611 spin_lock(&ilctxt->context_lock_lhb1);
612 }
Lynus Vaz802d7382017-06-30 15:10:26 +0530613 ret = size - dctxt.size;
614 if (ret == 0) {
615 if (!ilctxt->destroyed)
616 reinit_completion(&ilctxt->read_avail);
617 else
618 ret = -EIO;
619 }
620done:
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600621 spin_unlock(&ilctxt->context_lock_lhb1);
622 read_unlock_irqrestore(&context_list_lock_lha1, flags);
Lynus Vaz802d7382017-06-30 15:10:26 +0530623 return ret;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600624}
625EXPORT_SYMBOL(ipc_log_extract);
626
627/*
628 * Helper function used to read data from a message context.
629 *
630 * @ectxt context initialized by calling msg_read()
631 * @data data to read
632 * @size number of bytes of data to read
633 */
634static void tsv_read_data(struct encode_context *ectxt,
635 void *data, uint32_t size)
636{
Kyle Yan74fdd732017-03-22 13:37:08 -0700637 if (WARN_ON((ectxt->offset + size) > MAX_MSG_SIZE)) {
638 memcpy(data, (ectxt->buff + ectxt->offset),
639 MAX_MSG_SIZE - ectxt->offset - 1);
640 ectxt->offset += MAX_MSG_SIZE - ectxt->offset - 1;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600641 return;
Kyle Yan74fdd732017-03-22 13:37:08 -0700642 }
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600643 memcpy(data, (ectxt->buff + ectxt->offset), size);
644 ectxt->offset += size;
645}
646
647/*
648 * Helper function that reads a type from the context and updates the
649 * context pointers.
650 *
651 * @ectxt context initialized by calling msg_read()
652 * @hdr type header
653 */
654static void tsv_read_header(struct encode_context *ectxt,
655 struct tsv_header *hdr)
656{
Kyle Yan74fdd732017-03-22 13:37:08 -0700657 if (WARN_ON((ectxt->offset + sizeof(*hdr)) > MAX_MSG_SIZE)) {
658 memcpy(hdr, (ectxt->buff + ectxt->offset),
659 MAX_MSG_SIZE - ectxt->offset - 1);
660 ectxt->offset += MAX_MSG_SIZE - ectxt->offset - 1;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600661 return;
Kyle Yan74fdd732017-03-22 13:37:08 -0700662 }
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600663 memcpy(hdr, (ectxt->buff + ectxt->offset), sizeof(*hdr));
664 ectxt->offset += sizeof(*hdr);
665}
666
667/*
668 * Reads a timestamp.
669 *
670 * @ectxt context initialized by calling msg_read()
671 * @dctxt deserialization context
672 * @format output format (appended to %6u.09u timestamp format)
673 */
674void tsv_timestamp_read(struct encode_context *ectxt,
675 struct decode_context *dctxt, const char *format)
676{
677 struct tsv_header hdr;
678 uint64_t val;
679 unsigned long nanosec_rem;
680
681 tsv_read_header(ectxt, &hdr);
682 if (WARN_ON(hdr.type != TSV_TYPE_TIMESTAMP))
683 return;
684 tsv_read_data(ectxt, &val, sizeof(val));
685 nanosec_rem = do_div(val, 1000000000U);
686 IPC_SPRINTF_DECODE(dctxt, "[%6u.%09lu%s/",
687 (unsigned int)val, nanosec_rem, format);
688}
689EXPORT_SYMBOL(tsv_timestamp_read);
690
691/*
692 * Reads a QTimer timestamp.
693 *
694 * @ectxt context initialized by calling msg_read()
695 * @dctxt deserialization context
696 * @format output format (appended to %#18llx timestamp format)
697 */
698void tsv_qtimer_read(struct encode_context *ectxt,
699 struct decode_context *dctxt, const char *format)
700{
701 struct tsv_header hdr;
702 uint64_t val;
703
704 tsv_read_header(ectxt, &hdr);
705 if (WARN_ON(hdr.type != TSV_TYPE_QTIMER))
706 return;
707 tsv_read_data(ectxt, &val, sizeof(val));
708
709 /*
710 * This gives 16 hex digits of output. The # prefix prepends
711 * a 0x, and these characters count as part of the number.
712 */
713 IPC_SPRINTF_DECODE(dctxt, "%#18llx]%s", val, format);
714}
715EXPORT_SYMBOL(tsv_qtimer_read);
716
717/*
718 * Reads a data pointer.
719 *
720 * @ectxt context initialized by calling msg_read()
721 * @dctxt deserialization context
722 * @format output format
723 */
724void tsv_pointer_read(struct encode_context *ectxt,
725 struct decode_context *dctxt, const char *format)
726{
727 struct tsv_header hdr;
728 void *val;
729
730 tsv_read_header(ectxt, &hdr);
731 if (WARN_ON(hdr.type != TSV_TYPE_POINTER))
732 return;
733 tsv_read_data(ectxt, &val, sizeof(val));
734
735 IPC_SPRINTF_DECODE(dctxt, format, val);
736}
737EXPORT_SYMBOL(tsv_pointer_read);
738
739/*
740 * Reads a 32-bit integer value.
741 *
742 * @ectxt context initialized by calling msg_read()
743 * @dctxt deserialization context
744 * @format output format
745 */
746int32_t tsv_int32_read(struct encode_context *ectxt,
747 struct decode_context *dctxt, const char *format)
748{
749 struct tsv_header hdr;
750 int32_t val;
751
752 tsv_read_header(ectxt, &hdr);
753 if (WARN_ON(hdr.type != TSV_TYPE_INT32))
754 return -EINVAL;
755 tsv_read_data(ectxt, &val, sizeof(val));
756
757 IPC_SPRINTF_DECODE(dctxt, format, val);
758 return val;
759}
760EXPORT_SYMBOL(tsv_int32_read);
761
762/*
763 * Reads a byte array/string.
764 *
765 * @ectxt context initialized by calling msg_read()
766 * @dctxt deserialization context
767 * @format output format
768 */
769void tsv_byte_array_read(struct encode_context *ectxt,
770 struct decode_context *dctxt, const char *format)
771{
772 struct tsv_header hdr;
773
774 tsv_read_header(ectxt, &hdr);
775 if (WARN_ON(hdr.type != TSV_TYPE_BYTE_ARRAY))
776 return;
777 tsv_read_data(ectxt, dctxt->buff, hdr.size);
778 dctxt->buff += hdr.size;
779 dctxt->size -= hdr.size;
780}
781EXPORT_SYMBOL(tsv_byte_array_read);
782
783int add_deserialization_func(void *ctxt, int type,
784 void (*dfunc)(struct encode_context *,
785 struct decode_context *))
786{
787 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
788 struct dfunc_info *df_info;
789 unsigned long flags;
790
791 if (!ilctxt || !dfunc)
792 return -EINVAL;
793
794 df_info = kmalloc(sizeof(struct dfunc_info), GFP_KERNEL);
795 if (!df_info)
796 return -ENOSPC;
797
798 read_lock_irqsave(&context_list_lock_lha1, flags);
799 spin_lock(&ilctxt->context_lock_lhb1);
800 df_info->type = type;
801 df_info->dfunc = dfunc;
802 list_add_tail(&df_info->list, &ilctxt->dfunc_info_list);
803 spin_unlock(&ilctxt->context_lock_lhb1);
804 read_unlock_irqrestore(&context_list_lock_lha1, flags);
805 return 0;
806}
807EXPORT_SYMBOL(add_deserialization_func);
808
809static void *get_deserialization_func(struct ipc_log_context *ilctxt,
810 int type)
811{
812 struct dfunc_info *df_info = NULL;
813
814 if (!ilctxt)
815 return NULL;
816
817 list_for_each_entry(df_info, &ilctxt->dfunc_info_list, list) {
818 if (df_info->type == type)
819 return df_info->dfunc;
820 }
821 return NULL;
822}
823
824/**
825 * ipc_log_context_create: Create a debug log context
826 * Should not be called from atomic context
827 *
828 * @max_num_pages: Number of pages of logging space required (max. 10)
829 * @mod_name : Name of the directory entry under DEBUGFS
830 * @user_version : Version number of user-defined message formats
831 *
832 * returns context id on success, NULL on failure
833 */
834void *ipc_log_context_create(int max_num_pages,
835 const char *mod_name, uint16_t user_version)
836{
837 struct ipc_log_context *ctxt;
838 struct ipc_log_page *pg = NULL;
839 int page_cnt;
840 unsigned long flags;
841
842 ctxt = kzalloc(sizeof(struct ipc_log_context), GFP_KERNEL);
843 if (!ctxt)
844 return 0;
845
846 init_completion(&ctxt->read_avail);
847 INIT_LIST_HEAD(&ctxt->page_list);
848 INIT_LIST_HEAD(&ctxt->dfunc_info_list);
849 spin_lock_init(&ctxt->context_lock_lhb1);
850 for (page_cnt = 0; page_cnt < max_num_pages; page_cnt++) {
851 pg = kzalloc(sizeof(struct ipc_log_page), GFP_KERNEL);
852 if (!pg) {
853 pr_err("%s: cannot create ipc_log_page\n", __func__);
854 goto release_ipc_log_context;
855 }
856 pg->hdr.log_id = (uint64_t)(uintptr_t)ctxt;
857 pg->hdr.page_num = LOG_PAGE_FLAG | page_cnt;
858 pg->hdr.ctx_offset = (int64_t)((uint64_t)(uintptr_t)ctxt -
859 (uint64_t)(uintptr_t)&pg->hdr);
860
861 /* set magic last to signal that page init is complete */
862 pg->hdr.magic = IPC_LOGGING_MAGIC_NUM;
863 pg->hdr.nmagic = ~(IPC_LOGGING_MAGIC_NUM);
864
865 spin_lock_irqsave(&ctxt->context_lock_lhb1, flags);
866 list_add_tail(&pg->hdr.list, &ctxt->page_list);
867 spin_unlock_irqrestore(&ctxt->context_lock_lhb1, flags);
868 }
869
870 ctxt->log_id = (uint64_t)(uintptr_t)ctxt;
871 ctxt->version = IPC_LOG_VERSION;
872 strlcpy(ctxt->name, mod_name, IPC_LOG_MAX_CONTEXT_NAME_LEN);
873 ctxt->user_version = user_version;
874 ctxt->first_page = get_first_page(ctxt);
875 ctxt->last_page = pg;
876 ctxt->write_page = ctxt->first_page;
877 ctxt->read_page = ctxt->first_page;
878 ctxt->nd_read_page = ctxt->first_page;
879 ctxt->write_avail = max_num_pages * LOG_PAGE_DATA_SIZE;
880 ctxt->header_size = sizeof(struct ipc_log_page_header);
Lynus Vaz802d7382017-06-30 15:10:26 +0530881 kref_init(&ctxt->refcount);
882 ctxt->destroyed = false;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600883 create_ctx_debugfs(ctxt, mod_name);
884
885 /* set magic last to signal context init is complete */
886 ctxt->magic = IPC_LOG_CONTEXT_MAGIC_NUM;
887 ctxt->nmagic = ~(IPC_LOG_CONTEXT_MAGIC_NUM);
888
889 write_lock_irqsave(&context_list_lock_lha1, flags);
890 list_add_tail(&ctxt->list, &ipc_log_context_list);
891 write_unlock_irqrestore(&context_list_lock_lha1, flags);
892 return (void *)ctxt;
893
894release_ipc_log_context:
895 while (page_cnt-- > 0) {
896 pg = get_first_page(ctxt);
897 list_del(&pg->hdr.list);
898 kfree(pg);
899 }
900 kfree(ctxt);
901 return 0;
902}
903EXPORT_SYMBOL(ipc_log_context_create);
904
Lynus Vaz802d7382017-06-30 15:10:26 +0530905void ipc_log_context_free(struct kref *kref)
906{
907 struct ipc_log_context *ilctxt = container_of(kref,
908 struct ipc_log_context, refcount);
909 struct ipc_log_page *pg = NULL;
910
911 while (!list_empty(&ilctxt->page_list)) {
912 pg = get_first_page(ilctxt);
913 list_del(&pg->hdr.list);
914 kfree(pg);
915 }
916
917 kfree(ilctxt);
918}
919
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600920/*
921 * Destroy debug log context
922 *
923 * @ctxt: debug log context created by calling ipc_log_context_create API.
924 */
925int ipc_log_context_destroy(void *ctxt)
926{
927 struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
Arun Kumar Neelakantam2f1fbe42018-09-21 15:19:09 +0530928 struct dfunc_info *df_info = NULL, *tmp = NULL;
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600929 unsigned long flags;
930
931 if (!ilctxt)
932 return 0;
933
Lynus Vaz802d7382017-06-30 15:10:26 +0530934 debugfs_remove_recursive(ilctxt->dent);
935
936 spin_lock(&ilctxt->context_lock_lhb1);
937 ilctxt->destroyed = true;
938 complete_all(&ilctxt->read_avail);
Arun Kumar Neelakantam2f1fbe42018-09-21 15:19:09 +0530939 list_for_each_entry_safe(df_info, tmp, &ilctxt->dfunc_info_list, list) {
940 list_del(&df_info->list);
941 kfree(df_info);
942 }
Lynus Vaz802d7382017-06-30 15:10:26 +0530943 spin_unlock(&ilctxt->context_lock_lhb1);
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600944
945 write_lock_irqsave(&context_list_lock_lha1, flags);
946 list_del(&ilctxt->list);
947 write_unlock_irqrestore(&context_list_lock_lha1, flags);
948
Lynus Vaz802d7382017-06-30 15:10:26 +0530949 ipc_log_context_put(ilctxt);
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600950
Karthikeyan Ramasubramanian44dc7702016-09-16 15:39:37 -0600951 return 0;
952}
953EXPORT_SYMBOL(ipc_log_context_destroy);
954
955static int __init ipc_logging_init(void)
956{
957 check_and_create_debugfs();
958 return 0;
959}
960
961module_init(ipc_logging_init);
962
963MODULE_DESCRIPTION("ipc logging");
964MODULE_LICENSE("GPL v2");