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 #ifndef ION_TEST_FIXTURE_H_
17 #define ION_TEST_FIXTURE_H_
18 
19 #include <string>
20 
21 #include <hardware/exynos/ion.h>
22 
23 #include <gtest/gtest.h>
24 
25 #include "ion_test.h"
26 #include "../ion_uapi.h"
27 
28 #define MAX_LEGACY_HEAP_IDS 7
29 
30 class IonTest : public ::testing::Test {
31     struct ion_id_table {
32         unsigned int legacy_id;
33         unsigned int heap_id;
34     };
35 
36     struct ion_heap_data *m_ionHeapData;
37     int m_ionFd;
38     unsigned int m_heapCount;
39     unsigned int m_allHeapMask;
40     struct ion_id_table m_idTable[MAX_LEGACY_HEAP_IDS]; /* see IonTest::SetUp() */
41 public:
42     IonTest();
~IonTest()43     virtual ~IonTest() {};
44     virtual void SetUp();
45     virtual void TearDown();
46 
getIonFd()47     int getIonFd() { return m_ionFd; }
getHeapCount()48     unsigned int getHeapCount() { return m_heapCount; }
getHeapType(unsigned int idx)49     ion_heap_type getHeapType(unsigned int idx) {
50         return static_cast<ion_heap_type>(m_ionHeapData[idx].type);
51     }
getHeapMask(unsigned int idx)52     unsigned int getHeapMask(unsigned int idx) {
53         return (m_heapCount > 0) ? 1 << m_ionHeapData[idx].heap_id : 0;
54     }
getHeapName(unsigned int idx)55     const char *getHeapName(unsigned int idx) {
56         return (m_heapCount > 0) ? m_ionHeapData[idx].name : "[NO HEAP]";
57     }
getHeapFlags(unsigned int idx)58     unsigned int getHeapFlags(unsigned int idx) {
59         return m_ionHeapData[idx].heap_flags;
60     }
getHeapSize(unsigned int idx)61     unsigned int getHeapSize(unsigned int idx) {
62         if (m_ionHeapData[idx].size == 0)
63             return -1;
64         return m_ionHeapData[idx].size;
65     }
getMaxHeapId()66     unsigned int getMaxHeapId() {
67         unsigned int heapid = 0;
68 
69         for (unsigned int i = 0; i < getHeapCount(); i++)
70             if (heapid < m_ionHeapData[i].heap_id)
71                 heapid = m_ionHeapData[i].heap_id;
72 
73         return heapid;
74     }
getAllHeapMask()75     unsigned int getAllHeapMask() {
76         return m_allHeapMask;
77     }
getModernHeapId(unsigned int idx)78     unsigned int getModernHeapId(unsigned int idx) {
79         return m_idTable[idx].heap_id;
80     }
81 
getLegacyHeapId(unsigned int idx)82     unsigned int getLegacyHeapId(unsigned int idx) {
83         return m_idTable[idx].legacy_id;
84     }
85     size_t getCmaUsed(std::string heapname);
86 };
87 
88 class IonAllocTest : public IonTest {
89     size_t m_memTotal;
90 public:
91     IonAllocTest();
~IonAllocTest()92     virtual ~IonAllocTest() {};
93     virtual void SetUp();
94     virtual void TearDown();
95 
getMemTotal()96     size_t getMemTotal() { return m_memTotal; }
97 };
98 
99 class IonSpecialTest : public IonTest {
100     int m_ionTestDevFd;
101 protected:
getTestDevFd()102     int getTestDevFd() { return m_ionTestDevFd; }
103     int ionAlloc(size_t size, unsigned int heapmask, unsigned int flags);
104 public:
105     IonSpecialTest();
~IonSpecialTest()106     virtual ~IonSpecialTest() {};
107     virtual void SetUp();
108     virtual void TearDown();
109 };
110 
111 class IonClientDeviceTest : public IonSpecialTest {
112 
113     void ionTestMapping(int fd, bool write, unsigned long cmd,
114                         void *ptr, size_t size, off_t offset);
115 protected:
116     char *ionMmap(int fd, size_t size);
117     void ionMunmap(void *ptr, size_t size);
118 
ionTestWriteDma(int fd,void * ptr,size_t size,off_t offset)119     void ionTestWriteDma(int fd, void *ptr, size_t size, off_t offset) {
120         ionTestMapping(fd, true, ION_IOC_TEST_DMA_MAPPING, ptr, size, offset);
121     }
ionTestReadDma(int fd,void * ptr,size_t size,off_t offset)122     void ionTestReadDma(int fd, void *ptr, size_t size, off_t offset) {
123         ionTestMapping(fd, false, ION_IOC_TEST_DMA_MAPPING, ptr, size, offset);
124     }
ionTestWriteKernel(int fd,void * ptr,size_t size,off_t offset)125     void ionTestWriteKernel(int fd, void *ptr, size_t size, off_t offset) {
126         ionTestMapping(fd, true, ION_IOC_TEST_KERNEL_MAPPING, ptr, size, offset);
127     }
ionTestReadKernel(int fd,void * ptr,size_t size,off_t offset)128     void ionTestReadKernel(int fd, void *ptr, size_t size, off_t offset) {
129         ionTestMapping(fd, false, ION_IOC_TEST_KERNEL_MAPPING, ptr, size, offset);
130     }
131 
132     void fill(void *ptr, size_t size, off_t offset);
133     bool check(void *ptr, size_t size, off_t offset);
134     void blowCache();
135     void dirtyCache(void *ptr, size_t size);
136 public:
IonClientDeviceTest()137     IonClientDeviceTest() { }
~IonClientDeviceTest()138     virtual ~IonClientDeviceTest() {}
139 };
140 
141 #endif /* ION_TEST_FIXTURE_H_ */
142