1 /*
2  * Copyright (C) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <unistd.h>
17 
18 #include "ion_test.h"
19 #include "../ion_uapi.h"
20 
21 #include <gtest/gtest.h>
22 
23 #include "ion_test_fixture.h"
24 #include "ion_test_define.h"
25 
26 #define ALIGN(x,y) (((x) + ((y) - 1)) & ~((y) - 1))
27 
28 static const size_t g_allocation_sizes[] = {
29     mkb(16, 716), mkb(12, 4), mkb(8, 912), mkb(4, 60), mkb(2, 520), mkb(1, 92),
30     mb(16), mb(12), mb(8), mb(4), mb(2), mb(1), kb(64), kb(4),
31 };
32 
33 class Device : public IonClientDeviceTest {
34 };
35 
TEST_F(Device,KernelRead)36 TEST_F(Device, KernelRead)
37 {
38     for (unsigned int i = 0; i < getHeapCount(); i++) {
39         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
40             continue;
41 
42         if (getCmaUsed(getHeapName(i)) > 0)
43             continue;
44 
45         for (size_t size : g_allocation_sizes) {
46             if (size > getHeapSize(i))
47                 continue;
48 
49             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
50             SCOPED_TRACE(::testing::Message() << "size " << size);
51 
52             int fd = ionAlloc(size, getHeapMask(i), 0);
53             ASSERT_LT(2, fd);
54 
55             char *dst = new char[size];
56             void *src = ionMmap(fd, size);
57 
58             fill(src, size, 0);
59 
60             ionTestReadKernel(fd, dst, size, 0);
61 
62             EXPECT_TRUE(check(dst, size, 0));
63 
64             delete [] dst;
65 
66             ionMunmap(src, size);
67 
68             EXPECT_EQ(0, close(fd));
69         }
70     }
71 }
72 
TEST_F(Device,KernelWrite)73 TEST_F(Device, KernelWrite)
74 {
75     for (unsigned int i = 0; i < getHeapCount(); i++) {
76         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
77             continue;
78 
79         if (getCmaUsed(getHeapName(i)) > 0)
80             continue;
81 
82         for (size_t size : g_allocation_sizes) {
83             if (size > getHeapSize(i))
84                 continue;
85 
86             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
87             SCOPED_TRACE(::testing::Message() << "size " << size);
88 
89             int fd = ionAlloc(size, getHeapMask(i), 0);
90             ASSERT_LT(2, fd);
91 
92             char *src = new char[size];
93             void *dst = ionMmap(fd, size);
94 
95             fill(src, size, 0);
96             dirtyCache(dst, size);
97 
98             ionTestWriteKernel(fd, src, size, 0);
99 
100             EXPECT_TRUE(check(dst, size, 0));
101 
102             delete [] src;
103 
104             ionMunmap(dst, size);
105 
106             EXPECT_EQ(0, close(fd));
107         }
108     }
109 }
110 
TEST_F(Device,KernelReadCached)111 TEST_F(Device, KernelReadCached)
112 {
113     for (unsigned int i = 0; i < getHeapCount(); i++) {
114         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
115             continue;
116 
117         if (getCmaUsed(getHeapName(i)) > 0)
118             continue;
119 
120         for (size_t size : g_allocation_sizes) {
121             if (size > getHeapSize(i))
122                 continue;
123 
124             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
125             SCOPED_TRACE(::testing::Message() << "size " << size);
126 
127             int fd = ionAlloc(size, getHeapMask(i), ION_FLAG_CACHED);
128             ASSERT_LT(2, fd);
129 
130             char *dst = new char[size];
131             void *src = ionMmap(fd, size);
132 
133             fill(src, size, 0);
134 
135             ionTestReadKernel(fd, dst, size, 0);
136 
137             EXPECT_TRUE(check(dst, size, 0));
138 
139             delete [] dst;
140 
141             ionMunmap(src, size);
142 
143             EXPECT_EQ(0, close(fd));
144         }
145     }
146 }
147 
TEST_F(Device,KernelWriteCached)148 TEST_F(Device, KernelWriteCached)
149 {
150     for (unsigned int i = 0; i < getHeapCount(); i++) {
151         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
152             continue;
153 
154         if (getCmaUsed(getHeapName(i)) > 0)
155             continue;
156 
157         for (size_t size : g_allocation_sizes) {
158             if (size > getHeapSize(i))
159                 continue;
160 
161             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
162             SCOPED_TRACE(::testing::Message() << "size " << size);
163 
164             int fd = ionAlloc(size, getHeapMask(i), ION_FLAG_CACHED);
165             ASSERT_LT(2, fd);
166 
167             char *src = new char[size];
168             void *dst = ionMmap(fd, size);
169 
170             fill(src, size, 0);
171             dirtyCache(dst, size);
172 
173             ionTestWriteKernel(fd, src, size, 0);
174 
175             EXPECT_TRUE(check(dst, size, 0));
176 
177             delete [] src;
178 
179             ionMunmap(dst, size);
180 
181             EXPECT_EQ(0, close(fd));
182         }
183     }
184 }
185 
TEST_F(Device,DeviceRead)186 TEST_F(Device, DeviceRead)
187 {
188     for (unsigned int i = 0; i < getHeapCount(); i++) {
189         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
190             continue;
191 
192         if (getCmaUsed(getHeapName(i)) > 0)
193             continue;
194 
195         for (size_t size : g_allocation_sizes) {
196             if (size > getHeapSize(i))
197                 continue;
198 
199             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
200             SCOPED_TRACE(::testing::Message() << "size " << size);
201 
202             int fd = ionAlloc(size, getHeapMask(i), 0);
203             ASSERT_LT(2, fd);
204 
205             char *dst = new char[size];
206             void *src = ionMmap(fd, size);
207 
208             fill(src, size, 0);
209 
210             ionTestReadDma(fd, dst, size, 0);
211 
212             EXPECT_TRUE(check(dst, size, 0));
213 
214             delete [] dst;
215 
216             ionMunmap(src, size);
217 
218             EXPECT_EQ(0, close(fd));
219         }
220     }
221 }
222 
TEST_F(Device,DeviceWrite)223 TEST_F(Device, DeviceWrite)
224 {
225     for (unsigned int i = 0; i < getHeapCount(); i++) {
226         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
227             continue;
228 
229         if (getCmaUsed(getHeapName(i)) > 0)
230             continue;
231 
232         for (size_t size : g_allocation_sizes) {
233             if (size > getHeapSize(i))
234                 continue;
235 
236             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
237             SCOPED_TRACE(::testing::Message() << "size " << size);
238 
239             int fd = ionAlloc(size, getHeapMask(i), 0);
240             ASSERT_LT(2, fd);
241 
242             char *src = new char[size];
243             void *dst = ionMmap(fd, size);
244 
245             fill(src, size, 0);
246             dirtyCache(dst, size);
247 
248             ionTestWriteDma(fd, src, size, 0);
249 
250             EXPECT_TRUE(check(dst, size, 0));
251 
252             delete [] src;
253 
254             ionMunmap(dst, size);
255 
256             EXPECT_EQ(0, close(fd));
257         }
258     }
259 }
260 
TEST_F(Device,DeviceReadCached)261 TEST_F(Device, DeviceReadCached)
262 {
263     for (unsigned int i = 0; i < getHeapCount(); i++) {
264         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
265             continue;
266 
267         if (getCmaUsed(getHeapName(i)) > 0)
268             continue;
269 
270         for (size_t size : g_allocation_sizes) {
271             if (size > getHeapSize(i))
272                 continue;
273 
274             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
275             SCOPED_TRACE(::testing::Message() << "size " << size);
276 
277             int fd = ionAlloc(size, getHeapMask(i), ION_FLAG_CACHED);
278             ASSERT_LT(2, fd);
279 
280             char *dst = new char[size];
281             void *src = ionMmap(fd, size);
282 
283             fill(src, size, 0);
284 
285             ionTestReadDma(fd, dst, size, 0);
286 
287             EXPECT_TRUE(check(dst, size, 0));
288 
289             delete [] dst;
290 
291             ionMunmap(src, size);
292 
293             EXPECT_EQ(0, close(fd));
294         }
295     }
296 }
297 
TEST_F(Device,DeviceWriteCached)298 TEST_F(Device, DeviceWriteCached)
299 {
300     for (unsigned int i = 0; i < getHeapCount(); i++) {
301         if (getHeapFlags(i) & ION_HEAPDATA_FLAGS_UNTOUCHABLE)
302             continue;
303 
304         if (getCmaUsed(getHeapName(i)) > 0)
305             continue;
306 
307         for (size_t size : g_allocation_sizes) {
308             if (size > getHeapSize(i))
309                 continue;
310 
311             SCOPED_TRACE(::testing::Message() << "heap " << getHeapName(i) << " mask " << getHeapMask(i));
312             SCOPED_TRACE(::testing::Message() << "size " << size);
313 
314             int fd = ionAlloc(size, getHeapMask(i), ION_FLAG_CACHED);
315             ASSERT_LT(2, fd);
316 
317             char *src = new char[size];
318             void *dst = ionMmap(fd, size);
319 
320             fill(src, size, 0);
321             dirtyCache(dst, size);
322 
323             ionTestWriteDma(fd, src, size, 0);
324 
325             EXPECT_TRUE(check(dst, size, 0));
326 
327             delete [] src;
328 
329             ionMunmap(dst, size);
330 
331             EXPECT_EQ(0, close(fd));
332         }
333     }
334 }
335