Merge remote-tracking branch 'lsk/v3.10/topic/usb' 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 // Tracepoint changed in 3.15 backported to older kernels. The Makefile tries to autodetect the correct value, but if it fails change the #if below
32 #if OLD_BLOCK_RQ_COMPLETE
33 GATOR_DEFINE_PROBE(block_rq_complete, TP_PROTO(struct request_queue *q, struct request *rq))
34 #else
35 GATOR_DEFINE_PROBE(block_rq_complete, TP_PROTO(struct request_queue *q, struct request *rq, unsigned int nr_bytes))
36 #endif
37 {
38         int write;
39         unsigned int size;
40
41         if (!rq)
42                 return;
43
44         write = rq->cmd_flags & EVENTWRITE;
45 #if OLD_BLOCK_RQ_COMPLETE
46         size = rq->resid_len;
47 #else
48         size = nr_bytes;
49 #endif
50
51         if (!size)
52                 return;
53
54         if (write) {
55                 if (block_rq_wr_enabled) {
56                         atomic_add(size, &blockCnt[BLOCK_RQ_WR]);
57                 }
58         } else {
59                 if (block_rq_rd_enabled) {
60                         atomic_add(size, &blockCnt[BLOCK_RQ_RD]);
61                 }
62         }
63 }
64
65 static int gator_events_block_create_files(struct super_block *sb, struct dentry *root)
66 {
67         struct dentry *dir;
68
69         /* block_complete_wr */
70         dir = gatorfs_mkdir(sb, root, "Linux_block_rq_wr");
71         if (!dir) {
72                 return -1;
73         }
74         gatorfs_create_ulong(sb, dir, "enabled", &block_rq_wr_enabled);
75         gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_wr_key);
76
77         /* block_complete_rd */
78         dir = gatorfs_mkdir(sb, root, "Linux_block_rq_rd");
79         if (!dir) {
80                 return -1;
81         }
82         gatorfs_create_ulong(sb, dir, "enabled", &block_rq_rd_enabled);
83         gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_rd_key);
84
85         return 0;
86 }
87
88 static int gator_events_block_start(void)
89 {
90         // register tracepoints
91         if (block_rq_wr_enabled || block_rq_rd_enabled)
92                 if (GATOR_REGISTER_TRACE(block_rq_complete))
93                         goto fail_block_rq_exit;
94         pr_debug("gator: registered block event tracepoints\n");
95
96         return 0;
97
98         // unregister tracepoints on error
99 fail_block_rq_exit:
100         pr_err("gator: block event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
101
102         return -1;
103 }
104
105 static void gator_events_block_stop(void)
106 {
107         if (block_rq_wr_enabled || block_rq_rd_enabled)
108                 GATOR_UNREGISTER_TRACE(block_rq_complete);
109         pr_debug("gator: unregistered block event tracepoints\n");
110
111         block_rq_wr_enabled = 0;
112         block_rq_rd_enabled = 0;
113 }
114
115 static int gator_events_block_read(int **buffer)
116 {
117         int len, value, data = 0;
118
119         if (!on_primary_core()) {
120                 return 0;
121         }
122
123         len = 0;
124         if (block_rq_wr_enabled && (value = atomic_read(&blockCnt[BLOCK_RQ_WR])) > 0) {
125                 atomic_sub(value, &blockCnt[BLOCK_RQ_WR]);
126                 blockGet[len++] = block_rq_wr_key;
127                 blockGet[len++] = 0;    // indicates to Streamline that value bytes were written now, not since the last message
128                 blockGet[len++] = block_rq_wr_key;
129                 blockGet[len++] = value;
130                 data += value;
131         }
132         if (block_rq_rd_enabled && (value = atomic_read(&blockCnt[BLOCK_RQ_RD])) > 0) {
133                 atomic_sub(value, &blockCnt[BLOCK_RQ_RD]);
134                 blockGet[len++] = block_rq_rd_key;
135                 blockGet[len++] = 0;    // indicates to Streamline that value bytes were read now, not since the last message
136                 blockGet[len++] = block_rq_rd_key;
137                 blockGet[len++] = value;
138                 data += value;
139         }
140
141         if (buffer)
142                 *buffer = blockGet;
143
144         return len;
145 }
146
147 static struct gator_interface gator_events_block_interface = {
148         .create_files = gator_events_block_create_files,
149         .start = gator_events_block_start,
150         .stop = gator_events_block_stop,
151         .read = gator_events_block_read,
152 };
153
154 int gator_events_block_init(void)
155 {
156         block_rq_wr_enabled = 0;
157         block_rq_rd_enabled = 0;
158
159         block_rq_wr_key = gator_events_get_key();
160         block_rq_rd_key = gator_events_get_key();
161
162         return gator_events_install(&gator_events_block_interface);
163 }