gallium/util: move endian detect into a separate file
[mesa.git] / src / gallium / winsys / amdgpu / drm / addrlib / core / addrobject.cpp
1 /*
2 * Copyright © 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 /**
28 ***************************************************************************************************
29 * @file addrobject.cpp
30 * @brief Contains the AddrObject base class implementation.
31 ***************************************************************************************************
32 */
33
34 #include "addrinterface.h"
35 #include "addrobject.h"
36
37 /**
38 ***************************************************************************************************
39 * AddrObject::AddrObject
40 *
41 * @brief
42 * Constructor for the AddrObject class.
43 ***************************************************************************************************
44 */
45 AddrObject::AddrObject()
46 {
47 m_client.handle = NULL;
48 m_client.callbacks.allocSysMem = NULL;
49 m_client.callbacks.freeSysMem = NULL;
50 m_client.callbacks.debugPrint = NULL;
51 }
52
53 /**
54 ***************************************************************************************************
55 * AddrObject::AddrObject
56 *
57 * @brief
58 * Constructor for the AddrObject class.
59 ***************************************************************************************************
60 */
61 AddrObject::AddrObject(const AddrClient* pClient)
62 {
63 m_client = *pClient;
64 }
65
66 /**
67 ***************************************************************************************************
68 * AddrObject::~AddrObject
69 *
70 * @brief
71 * Destructor for the AddrObject class.
72 ***************************************************************************************************
73 */
74 AddrObject::~AddrObject()
75 {
76 }
77
78 /**
79 ***************************************************************************************************
80 * AddrObject::ClientAlloc
81 *
82 * @brief
83 * Calls instanced allocSysMem inside AddrClient
84 ***************************************************************************************************
85 */
86 VOID* AddrObject::ClientAlloc(
87 size_t objSize, ///< [in] Size to allocate
88 const AddrClient* pClient) ///< [in] Client pointer
89 {
90 VOID* pObjMem = NULL;
91
92 if (pClient->callbacks.allocSysMem != NULL)
93 {
94 ADDR_ALLOCSYSMEM_INPUT allocInput = {0};
95
96 allocInput.size = sizeof(ADDR_ALLOCSYSMEM_INPUT);
97 allocInput.flags.value = 0;
98 allocInput.sizeInBytes = static_cast<UINT_32>(objSize);
99 allocInput.hClient = pClient->handle;
100
101 pObjMem = pClient->callbacks.allocSysMem(&allocInput);
102 }
103
104 return pObjMem;
105 }
106
107 /**
108 ***************************************************************************************************
109 * AddrObject::AddrMalloc
110 *
111 * @brief
112 * A wrapper of ClientAlloc
113 ***************************************************************************************************
114 */
115 VOID* AddrObject::AddrMalloc(
116 size_t objSize) const ///< [in] Size to allocate
117 {
118 return ClientAlloc(objSize, &m_client);
119 }
120
121 /**
122 ***************************************************************************************************
123 * AddrObject::ClientFree
124 *
125 * @brief
126 * Calls freeSysMem inside AddrClient
127 ***************************************************************************************************
128 */
129 VOID AddrObject::ClientFree(
130 VOID* pObjMem, ///< [in] User virtual address to free.
131 const AddrClient* pClient) ///< [in] Client pointer
132 {
133 if (pClient->callbacks.freeSysMem != NULL)
134 {
135 if (pObjMem != NULL)
136 {
137 ADDR_FREESYSMEM_INPUT freeInput = {0};
138
139 freeInput.size = sizeof(ADDR_FREESYSMEM_INPUT);
140 freeInput.hClient = pClient->handle;
141 freeInput.pVirtAddr = pObjMem;
142
143 pClient->callbacks.freeSysMem(&freeInput);
144 }
145 }
146 }
147
148 /**
149 ***************************************************************************************************
150 * AddrObject::AddrFree
151 *
152 * @brief
153 * A wrapper of ClientFree
154 ***************************************************************************************************
155 */
156 VOID AddrObject::AddrFree(
157 VOID* pObjMem) const ///< [in] User virtual address to free.
158 {
159 ClientFree(pObjMem, &m_client);
160 }
161
162 /**
163 ***************************************************************************************************
164 * AddrObject::operator new
165 *
166 * @brief
167 * Allocates memory needed for AddrObject object. (with ADDR_CLIENT_HANDLE)
168 *
169 * @return
170 * Returns NULL if unsuccessful.
171 ***************************************************************************************************
172 */
173 VOID* AddrObject::operator new(
174 size_t objSize, ///< [in] Size to allocate
175 const AddrClient* pClient) ///< [in] Client pointer
176 {
177 return ClientAlloc(objSize, pClient);
178 }
179
180
181 /**
182 ***************************************************************************************************
183 * AddrObject::operator delete
184 *
185 * @brief
186 * Frees AddrObject object memory.
187 ***************************************************************************************************
188 */
189 VOID AddrObject::operator delete(
190 VOID* pObjMem, ///< [in] User virtual address to free.
191 const AddrClient* pClient) ///< [in] Client handle
192 {
193 ClientFree(pObjMem, pClient);
194 }
195
196 /**
197 ***************************************************************************************************
198 * AddrObject::operator delete
199 *
200 * @brief
201 * Frees AddrObject object memory.
202 ***************************************************************************************************
203 */
204 VOID AddrObject::operator delete(
205 VOID* pObjMem) ///< [in] User virtual address to free.
206 {
207 AddrObject* pObj = static_cast<AddrObject*>(pObjMem);
208 ClientFree(pObjMem, &pObj->m_client);
209 }
210
211 /**
212 ***************************************************************************************************
213 * AddrObject::DebugPrint
214 *
215 * @brief
216 * Print debug message
217 *
218 * @return
219 * N/A
220 ***************************************************************************************************
221 */
222 VOID AddrObject::DebugPrint(
223 const CHAR* pDebugString, ///< [in] Debug string
224 ...) const
225 {
226 #if DEBUG
227 if (m_client.callbacks.debugPrint != NULL)
228 {
229 va_list ap;
230
231 va_start(ap, pDebugString);
232
233 ADDR_DEBUGPRINT_INPUT debugPrintInput = {0};
234
235 debugPrintInput.size = sizeof(ADDR_DEBUGPRINT_INPUT);
236 debugPrintInput.pDebugString = const_cast<CHAR*>(pDebugString);
237 debugPrintInput.hClient = m_client.handle;
238 va_copy(debugPrintInput.ap, ap);
239
240 m_client.callbacks.debugPrint(&debugPrintInput);
241
242 va_end(ap);
243 }
244 #endif
245 }
246