1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019-2020 Collabora, Ltd.
3bf215546Sopenharmony_ci * Author: Antonio Caggiano <antonio.caggiano@collabora.com>
4bf215546Sopenharmony_ci * Author: Rohan Garg <rohan.garg@collabora.com>
5bf215546Sopenharmony_ci * Author: Robert Beckett <bob.beckett@collabora.com>
6bf215546Sopenharmony_ci * Author: Corentin Noël <corentin.noel@collabora.com>
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci * SPDX-License-Identifier: MIT
9bf215546Sopenharmony_ci */
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ci#include "pps_driver.h"
12bf215546Sopenharmony_ci
13bf215546Sopenharmony_ci#include <iterator>
14bf215546Sopenharmony_ci#include <sstream>
15bf215546Sopenharmony_ci
16bf215546Sopenharmony_ci#ifdef PPS_FREEDRENO
17bf215546Sopenharmony_ci#include "freedreno/ds/fd_pps_driver.h"
18bf215546Sopenharmony_ci#endif // PPS_FREEDRENO
19bf215546Sopenharmony_ci
20bf215546Sopenharmony_ci#ifdef PPS_INTEL
21bf215546Sopenharmony_ci#include "intel/ds/intel_pps_driver.h"
22bf215546Sopenharmony_ci#endif // PPS_INTEL
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifdef PPS_PANFROST
25bf215546Sopenharmony_ci#include "panfrost/ds/pan_pps_driver.h"
26bf215546Sopenharmony_ci#endif // PPS_PANFROST
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "pps.h"
29bf215546Sopenharmony_ci#include "pps_algorithm.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cinamespace pps
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_cistd::unordered_map<std::string, std::unique_ptr<Driver>> create_supported_drivers()
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   std::unordered_map<std::string, std::unique_ptr<Driver>> map;
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#ifdef PPS_FREEDRENO
38bf215546Sopenharmony_ci   map.emplace("msm", std::make_unique<FreedrenoDriver>());
39bf215546Sopenharmony_ci#endif // PPS_FREEDRENO
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci#ifdef PPS_INTEL
42bf215546Sopenharmony_ci   map.emplace("i915", std::make_unique<IntelDriver>());
43bf215546Sopenharmony_ci#endif // PPS_INTEL
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci#ifdef PPS_PANFROST
46bf215546Sopenharmony_ci   map.emplace("panfrost", std::make_unique<PanfrostDriver>());
47bf215546Sopenharmony_ci#endif // PPS_PANFROST
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   return map;
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ciconst std::unordered_map<std::string, std::unique_ptr<Driver>> &Driver::get_supported_drivers()
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   static auto map = create_supported_drivers();
55bf215546Sopenharmony_ci   return map;
56bf215546Sopenharmony_ci}
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ciconst std::vector<std::string> Driver::supported_device_names()
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci   std::vector<std::string> supported_device_names;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   for (auto &entry : get_supported_drivers()) {
63bf215546Sopenharmony_ci      supported_device_names.emplace_back(entry.first);
64bf215546Sopenharmony_ci   }
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   return supported_device_names;
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ciDriver *Driver::get_driver(DrmDevice &&drm_device)
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   auto &supported_drivers = get_supported_drivers();
72bf215546Sopenharmony_ci   auto it = supported_drivers.find(drm_device.name);
73bf215546Sopenharmony_ci   if (it == std::end(supported_drivers)) {
74bf215546Sopenharmony_ci      PERFETTO_FATAL("Failed to find a driver for DRM device %s", drm_device.name.c_str());
75bf215546Sopenharmony_ci   }
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   Driver *driver = it->second.get();
78bf215546Sopenharmony_ci   driver->drm_device = std::move(drm_device);
79bf215546Sopenharmony_ci   return driver;
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistd::string Driver::default_driver_name()
83bf215546Sopenharmony_ci{
84bf215546Sopenharmony_ci   auto supported_devices = Driver::supported_device_names();
85bf215546Sopenharmony_ci   auto devices = DrmDevice::create_all();
86bf215546Sopenharmony_ci   for (auto &device : devices) {
87bf215546Sopenharmony_ci      if (CONTAINS(supported_devices, device.name)) {
88bf215546Sopenharmony_ci         PPS_LOG_IMPORTANT("Driver selected: %s", device.name.c_str());
89bf215546Sopenharmony_ci         return device.name;
90bf215546Sopenharmony_ci      }
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci   PPS_LOG_FATAL("Failed to find any driver");
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistd::string Driver::find_driver_name(const char *requested)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci   auto supported_devices = Driver::supported_device_names();
98bf215546Sopenharmony_ci   auto devices = DrmDevice::create_all();
99bf215546Sopenharmony_ci   for (auto &device : devices) {
100bf215546Sopenharmony_ci      if (device.name == requested) {
101bf215546Sopenharmony_ci         PPS_LOG_IMPORTANT("Driver selected: %s", device.name.c_str());
102bf215546Sopenharmony_ci         return device.name;
103bf215546Sopenharmony_ci      }
104bf215546Sopenharmony_ci   }
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   std::ostringstream drivers_os;
107bf215546Sopenharmony_ci   std::copy(supported_devices.begin(),
108bf215546Sopenharmony_ci      supported_devices.end() - 1,
109bf215546Sopenharmony_ci      std::ostream_iterator<std::string>(drivers_os, ", "));
110bf215546Sopenharmony_ci   drivers_os << supported_devices.back();
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   PPS_LOG_ERROR(
113bf215546Sopenharmony_ci      "Device '%s' not found (supported drivers: %s)", requested, drivers_os.str().c_str());
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   return default_driver_name();
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci} // namespace pps
119