1 /*
2 * Copyright (c) 2018-2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #include "arm_compute/core/GPUTarget.h"
25 #include "arm_compute/core/Log.h"
26
27 #include <map>
28 #include <regex>
29
30 namespace
31 {
get_valhall_target(const std::string & version)32 arm_compute::GPUTarget get_valhall_target(const std::string &version)
33 {
34 if(version.find("G77") != std::string::npos)
35 {
36 return arm_compute::GPUTarget::G77;
37 }
38 else if(version.find("G57") != std::string::npos)
39 {
40 return arm_compute::GPUTarget::G57;
41 }
42 if(version.find("G68") != std::string::npos)
43 {
44 return arm_compute::GPUTarget::G68;
45 }
46 if(version.find("G78AE") != std::string::npos)
47 {
48 return arm_compute::GPUTarget::G78AE;
49 }
50 if(version.find("G78") != std::string::npos)
51 {
52 return arm_compute::GPUTarget::G78;
53 }
54 else if(version.find("G710") != std::string::npos)
55 {
56 return arm_compute::GPUTarget::G710;
57 }
58 else if(version.find("G610") != std::string::npos)
59 {
60 return arm_compute::GPUTarget::G610;
61 }
62 else if(version.find("G510") != std::string::npos)
63 {
64 return arm_compute::GPUTarget::G510;
65 }
66 else if(version.find("G310") != std::string::npos)
67 {
68 return arm_compute::GPUTarget::G310;
69 }
70 else if(version.find("G715") != std::string::npos)
71 {
72 return arm_compute::GPUTarget::G715;
73 }
74 else if(version.find("G615") != std::string::npos)
75 {
76 return arm_compute::GPUTarget::G615;
77 }
78 else
79 {
80 return arm_compute::GPUTarget::UNKNOWN;
81 }
82 }
83
get_bifrost_target(const std::string & version)84 arm_compute::GPUTarget get_bifrost_target(const std::string &version)
85 {
86 if(version.find("G71") != std::string::npos)
87 {
88 return arm_compute::GPUTarget::G71;
89 }
90 else if(version.find("G72") != std::string::npos)
91 {
92 return arm_compute::GPUTarget::G72;
93 }
94 else if(version.find("G51BIG") != std::string::npos)
95 {
96 return arm_compute::GPUTarget::G51BIG;
97 }
98 else if(version.find("G51LIT") != std::string::npos)
99 {
100 return arm_compute::GPUTarget::G51LIT;
101 }
102 else if(version.find("G51") != std::string::npos)
103 {
104 return arm_compute::GPUTarget::G51;
105 }
106 else if(version.find("G52LIT") != std::string::npos)
107 {
108 return arm_compute::GPUTarget::G52LIT;
109 }
110 else if(version.find("G52") != std::string::npos)
111 {
112 return arm_compute::GPUTarget::G52;
113 }
114 else if(version.find("G76") != std::string::npos)
115 {
116 return arm_compute::GPUTarget::G76;
117 }
118 else if(version.find("G31") != std::string::npos)
119 {
120 return arm_compute::GPUTarget::G31;
121 }
122 else
123 {
124 return arm_compute::GPUTarget::UNKNOWN;
125 }
126 }
127
get_midgard_target(const std::string & version)128 arm_compute::GPUTarget get_midgard_target(const std::string &version)
129 {
130 if(version.find("T600") != std::string::npos)
131 {
132 return arm_compute::GPUTarget::T600;
133 }
134 else if(version.find("T700") != std::string::npos)
135 {
136 return arm_compute::GPUTarget::T700;
137 }
138 else if(version.find("T800") != std::string::npos)
139 {
140 return arm_compute::GPUTarget::T800;
141 }
142 else
143 {
144 return arm_compute::GPUTarget::MIDGARD;
145 }
146 }
147 } // namespace
148
149 namespace arm_compute
150 {
string_from_target(GPUTarget target)151 const std::string &string_from_target(GPUTarget target)
152 {
153 static std::map<GPUTarget, const std::string> gpu_target_map =
154 {
155 { GPUTarget::MIDGARD, "midgard" },
156 { GPUTarget::BIFROST, "bifrost" },
157 { GPUTarget::VALHALL, "valhall" },
158 { GPUTarget::T600, "t600" },
159 { GPUTarget::T700, "t700" },
160 { GPUTarget::T800, "t800" },
161 { GPUTarget::G71, "g71" },
162 { GPUTarget::G72, "g72" },
163 { GPUTarget::G51, "g51" },
164 { GPUTarget::G51BIG, "g51big" },
165 { GPUTarget::G51LIT, "g51lit" },
166 { GPUTarget::G31, "g31" },
167 { GPUTarget::G76, "g76" },
168 { GPUTarget::G52, "g52" },
169 { GPUTarget::G52LIT, "g52lit" },
170 { GPUTarget::G77, "g77" },
171 { GPUTarget::G57, "g57" },
172 { GPUTarget::G78, "g78" },
173 { GPUTarget::G68, "g68" },
174 { GPUTarget::G78AE, "g78ae" },
175 { GPUTarget::G710, "g710" },
176 { GPUTarget::G610, "g610" },
177 { GPUTarget::G510, "g510" },
178 { GPUTarget::G310, "g310" },
179 { GPUTarget::G715, "g715" },
180 { GPUTarget::G615, "g615" },
181 };
182
183 return gpu_target_map[target];
184 }
185
get_target_from_name(const std::string & device_name)186 GPUTarget get_target_from_name(const std::string &device_name)
187 {
188 std::regex mali_regex(R"(Mali-(.*))");
189 std::smatch name_parts;
190 const bool found_mali = std::regex_search(device_name, name_parts, mali_regex);
191
192 if(!found_mali)
193 {
194 ARM_COMPUTE_LOG_INFO_MSG_CORE("Can't find valid Arm® Mali™ GPU. Target is set to default.");
195 return GPUTarget::MIDGARD;
196 }
197
198 const char target = name_parts.str(1)[0];
199 const std::string &version = name_parts.str(1);
200
201 std::regex future_regex(R"(.*X)");
202 const bool is_future_gpu = std::regex_search(version, future_regex);
203
204 // Work-out gpu target
205 GPUTarget gpu_target;
206 if(target == 'G' || is_future_gpu)
207 {
208 // Check for Valhall or Bifrost
209 gpu_target = get_valhall_target(version);
210 if(gpu_target == GPUTarget::UNKNOWN)
211 {
212 gpu_target = get_bifrost_target(version);
213 }
214
215 // Default GPUTarget
216 if(gpu_target == GPUTarget::UNKNOWN)
217 {
218 gpu_target = GPUTarget::VALHALL;
219 }
220 }
221 else if(target == 'T')
222 {
223 gpu_target = get_midgard_target(version);
224 }
225 else
226 {
227 gpu_target = GPUTarget::UNKNOWN;
228 }
229
230 // Report in case of unknown target
231 if(gpu_target == GPUTarget::UNKNOWN)
232 {
233 ARM_COMPUTE_LOG_INFO_MSG_CORE("Arm® Mali™ Mali GPU unknown. Target is set to the default one. (BIFROST)");
234 return GPUTarget::BIFROST;
235 }
236
237 return gpu_target;
238 }
239
get_arch_from_target(GPUTarget target)240 GPUTarget get_arch_from_target(GPUTarget target)
241 {
242 return (target & GPUTarget::GPU_ARCH_MASK);
243 }
244 } // namespace arm_compute
245