Merge remote-tracking branch 'lsk/v3.10/topic/arm64-misc' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_block.c
1 /**
2  * Copyright (C) ARM Limited 2010-2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9
10 #include "gator.h"
11 #include <trace/events/block.h>
12
13 #define BLOCK_RQ_WR             0
14 #define BLOCK_RQ_RD             1
15
16 #define BLOCK_TOTAL             (BLOCK_RQ_RD+1)
17
18 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
19 #define EVENTWRITE REQ_RW
20 #else
21 #define EVENTWRITE REQ_WRITE
22 #endif
23
24 static ulong block_rq_wr_enabled;
25 static ulong block_rq_rd_enabled;
26 static ulong block_rq_wr_key;
27 static ulong block_rq_rd_key;
28 static atomic_t blockCnt[BLOCK_TOTAL];
29 static int blockGet[BLOCK_TOTAL * 4];
30
31 GATOR_DEFINE_PROBE(block_rq_complete, TP_PROTO(struct request_queue *q, struct request *rq))
32 {
33         int write, size;
34
35         if (!rq)
36                 return;
37
38         write = rq->cmd_flags & EVENTWRITE;
39         size = rq->resid_len;
40
41         if (!size)
42                 return;
43
44         if (write) {
45                 if (block_rq_wr_enabled) {
46                         atomic_add(size, &blockCnt[BLOCK_RQ_WR]);
47                 }
48         } else {
49                 if (block_rq_rd_enabled) {
50                         atomic_add(size, &blockCnt[BLOCK_RQ_RD]);
51                 }
52         }
53 }
54
55 static int gator_events_block_create_files(struct super_block *sb, struct dentry *root)
56 {
57         struct dentry *dir;
58
59         /* block_complete_wr */
60         dir = gatorfs_mkdir(sb, root, "Linux_block_rq_wr");
61         if (!dir) {
62                 return -1;
63         }
64         gatorfs_create_ulong(sb, dir, "enabled", &block_rq_wr_enabled);
65         gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_wr_key);
66
67         /* block_complete_rd */
68         dir = gatorfs_mkdir(sb, root, "Linux_block_rq_rd");
69         if (!dir) {
70                 return -1;
71         }
72         gatorfs_create_ulong(sb, dir, "enabled", &block_rq_rd_enabled);
73         gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_rd_key);
74
75         return 0;
76 }
77
78 static int gator_events_block_start(void)
79 {
80         // register tracepoints
81         if (block_rq_wr_enabled || block_rq_rd_enabled)
82                 if (GATOR_REGISTER_TRACE(block_rq_complete))
83                         goto fail_block_rq_exit;
84         pr_debug("gator: registered block event tracepoints\n");
85
86         return 0;
87
88         // unregister tracepoints on error
89 fail_block_rq_exit:
90         pr_err("gator: block event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
91
92         return -1;
93 }
94
95 static void gator_events_block_stop(void)
96 {
97         if (block_rq_wr_enabled || block_rq_rd_enabled)
98                 GATOR_UNREGISTER_TRACE(block_rq_complete);
99         pr_debug("gator: unregistered block event tracepoints\n");
100
101         block_rq_wr_enabled = 0;
102         block_rq_rd_enabled = 0;
103 }
104
105 static int gator_events_block_read(int **buffer)
106 {
107         int len, value, data = 0;
108
109         if (!on_primary_core()) {
110                 return 0;
111         }
112
113         len = 0;
114         if (block_rq_wr_enabled && (value = atomic_read(&blockCnt[BLOCK_RQ_WR])) > 0) {
115                 atomic_sub(value, &blockCnt[BLOCK_RQ_WR]);
116                 blockGet[len++] = block_rq_wr_key;
117                 blockGet[len++] = 0;    // indicates to Streamline that value bytes were written now, not since the last message
118                 blockGet[len++] = block_rq_wr_key;
119                 blockGet[len++] = value;
120                 data += value;
121         }
122         if (block_rq_rd_enabled && (value = atomic_read(&blockCnt[BLOCK_RQ_RD])) > 0) {
123                 atomic_sub(value, &blockCnt[BLOCK_RQ_RD]);
124                 blockGet[len++] = block_rq_rd_key;
125                 blockGet[len++] = 0;    // indicates to Streamline that value bytes were read now, not since the last message
126                 blockGet[len++] = block_rq_rd_key;
127                 blockGet[len++] = value;
128                 data += value;
129         }
130
131         if (buffer)
132                 *buffer = blockGet;
133
134         return len;
135 }
136
137 static struct gator_interface gator_events_block_interface = {
138         .create_files = gator_events_block_create_files,
139         .start = gator_events_block_start,
140         .stop = gator_events_block_stop,
141         .read = gator_events_block_read,
142 };
143
144 int gator_events_block_init(void)
145 {
146         block_rq_wr_enabled = 0;
147         block_rq_rd_enabled = 0;
148
149         block_rq_wr_key = gator_events_get_key();
150         block_rq_rd_key = gator_events_get_key();
151
152         return gator_events_install(&gator_events_block_interface);
153 }