gator: Merge gator version 5.23.1
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / gator / gator_buffer_write.c
1 /**
2  * Copyright (C) ARM Limited 2010-2015. 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 static void gator_buffer_write_packed_int(int cpu, int buftype, int x)
11 {
12         uint32_t write = per_cpu(gator_buffer_write, cpu)[buftype];
13         uint32_t mask = gator_buffer_mask[buftype];
14         char *buffer = per_cpu(gator_buffer, cpu)[buftype];
15         int packedBytes = 0;
16         int more = true;
17
18         while (more) {
19                 /* low order 7 bits of x */
20                 char b = x & 0x7f;
21
22                 x >>= 7;
23
24                 if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0))
25                         more = false;
26                 else
27                         b |= 0x80;
28
29                 buffer[(write + packedBytes) & mask] = b;
30                 packedBytes++;
31         }
32
33         per_cpu(gator_buffer_write, cpu)[buftype] = (write + packedBytes) & mask;
34 }
35
36 static void gator_buffer_write_packed_int64(int cpu, int buftype, long long x)
37 {
38         uint32_t write = per_cpu(gator_buffer_write, cpu)[buftype];
39         uint32_t mask = gator_buffer_mask[buftype];
40         char *buffer = per_cpu(gator_buffer, cpu)[buftype];
41         int packedBytes = 0;
42         int more = true;
43
44         while (more) {
45                 /* low order 7 bits of x */
46                 char b = x & 0x7f;
47
48                 x >>= 7;
49
50                 if ((x == 0 && (b & 0x40) == 0) || (x == -1 && (b & 0x40) != 0))
51                         more = false;
52                 else
53                         b |= 0x80;
54
55                 buffer[(write + packedBytes) & mask] = b;
56                 packedBytes++;
57         }
58
59         per_cpu(gator_buffer_write, cpu)[buftype] = (write + packedBytes) & mask;
60 }
61
62 static void gator_buffer_write_bytes(int cpu, int buftype, const char *x, int len)
63 {
64         int i;
65         u32 write = per_cpu(gator_buffer_write, cpu)[buftype];
66         u32 mask = gator_buffer_mask[buftype];
67         char *buffer = per_cpu(gator_buffer, cpu)[buftype];
68
69         for (i = 0; i < len; i++) {
70                 buffer[write] = x[i];
71                 write = (write + 1) & mask;
72         }
73
74         per_cpu(gator_buffer_write, cpu)[buftype] = write;
75 }
76
77 static void gator_buffer_write_string(int cpu, int buftype, const char *x)
78 {
79         int len = strlen(x);
80
81         gator_buffer_write_packed_int(cpu, buftype, len);
82         gator_buffer_write_bytes(cpu, buftype, x, len);
83 }