1 /*
2 * Copyright (c) 2017 - 2018, 2021, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <xf86drm.h>
35 #include <xf86drmMode.h>
36 // Intentionally included after xf86 headers so that they in-turn include libdrm version of drm.h
37 // that doesn't use keyword "virtual" for a variable name. Not doing so leads to the kernel version
38 // of drm.h being included causing compilation to fail
39 #include <drm/msm_drm.h>
40 #ifdef KERNEL_5_4
41 #include <drm/sde_drm.h>
42 #endif
43 #include <algorithm>
44 #include <iterator>
45
46 #include "drm_master.h"
47
48 #define __CLASS__ "DRMMaster"
49
50 using std::mutex;
51 using std::lock_guard;
52 using std::begin;
53 using std::copy;
54 using std::end;
55 using std::fill;
56
57 namespace drm_utils {
58
59 DRMMaster *DRMMaster::s_instance = nullptr;
60 mutex DRMMaster::s_lock;
61
GetInstance(DRMMaster ** master)62 int DRMMaster::GetInstance(DRMMaster **master) {
63 lock_guard<mutex> obj(s_lock);
64
65 if (!s_instance) {
66 s_instance = new DRMMaster();
67 if (s_instance->Init() < 0) {
68 delete s_instance;
69 s_instance = nullptr;
70 return -ENODEV;
71 }
72 }
73
74 *master = s_instance;
75 return 0;
76 }
77
DestroyInstance()78 void DRMMaster::DestroyInstance() {
79 lock_guard<mutex> obj(s_lock);
80 delete s_instance;
81 s_instance = nullptr;
82 }
83
Init()84 int DRMMaster::Init() {
85 dev_fd_ = drmOpen("msm_drm", nullptr);
86 if (dev_fd_ < 0) {
87 DRM_LOGE("drmOpen failed with error %d", dev_fd_);
88 return -ENODEV;
89 }
90
91 return 0;
92 }
93
~DRMMaster()94 DRMMaster::~DRMMaster() {
95 drmClose(dev_fd_);
96 dev_fd_ = -1;
97 }
98
CreateFbId(const DRMBuffer & drm_buffer,uint32_t * fb_id)99 int DRMMaster::CreateFbId(const DRMBuffer &drm_buffer, uint32_t *fb_id) {
100 uint32_t gem_handle = 0;
101 int ret = drmPrimeFDToHandle(dev_fd_, drm_buffer.fd, &gem_handle);
102 if (ret) {
103 DRM_LOGE("drmPrimeFDToHandle failed with error %d", ret);
104 return ret;
105 }
106
107 struct drm_mode_fb_cmd2 cmd2 {};
108 cmd2.width = drm_buffer.width;
109 cmd2.height = drm_buffer.height;
110 cmd2.pixel_format = drm_buffer.drm_format;
111 cmd2.flags = DRM_MODE_FB_MODIFIERS;
112 fill(begin(cmd2.handles), begin(cmd2.handles) + drm_buffer.num_planes, gem_handle);
113 copy(begin(drm_buffer.stride), end(drm_buffer.stride), begin(cmd2.pitches));
114 copy(begin(drm_buffer.offset), end(drm_buffer.offset), begin(cmd2.offsets));
115 fill(begin(cmd2.modifier), begin(cmd2.modifier) + drm_buffer.num_planes,
116 drm_buffer.drm_format_modifier);
117
118 if ((ret = drmIoctl(dev_fd_, DRM_IOCTL_MODE_ADDFB2, &cmd2))) {
119 DRM_LOGE("DRM_IOCTL_MODE_ADDFB2 failed with error %d", ret);
120 } else {
121 *fb_id = cmd2.fb_id;
122 }
123
124 struct drm_gem_close gem_close = {};
125 gem_close.handle = gem_handle;
126 int ret1 = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
127 if (ret1) {
128 DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret1);
129 return ret1;
130 }
131
132 return ret;
133 }
134
RemoveFbId(uint32_t fb_id)135 int DRMMaster::RemoveFbId(uint32_t fb_id) {
136 int ret = 0;
137 #ifdef DRM_IOCTL_MSM_RMFB2
138 ret = drmIoctl(dev_fd_, DRM_IOCTL_MSM_RMFB2, &fb_id);
139 if (ret) {
140 DRM_LOGE("drmIoctl::DRM_IOCTL_MSM_RMFB2 failed for fb_id %d with error %d", fb_id, errno);
141 }
142 #else
143 ret = drmModeRmFB(dev_fd_, fb_id);
144 if (ret) {
145 DRM_LOGE("drmModeRmFB failed for fb_id %d with error %d", fb_id, ret);
146 }
147 #endif
148 return ret;
149 }
150
IsRmFbRefCounted()151 bool DRMMaster::IsRmFbRefCounted() {
152 #ifdef DRM_IOCTL_MSM_RMFB2
153 return true;
154 #endif
155 return false;
156 }
157
158 } // namespace drm_utils
159