1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Checked.h>
#include <AK/Try.h>
#include <Kernel/API/POSIX/errno.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Graphics/FramebufferDevice.h>
#include <Kernel/Graphics/GraphicsManagement.h>
#include <Kernel/Memory/AnonymousVMObject.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Process.h>
#include <Kernel/Sections.h>
#include <LibC/sys/ioctl_numbers.h>
namespace Kernel {
NonnullRefPtr<FramebufferDevice> FramebufferDevice::create(const GenericGraphicsAdapter& adapter, PhysicalAddress paddr, size_t width, size_t height, size_t pitch)
{
auto framebuffer_device_or_error = DeviceManagement::try_create_device<FramebufferDevice>(adapter, paddr, width, height, pitch);
// FIXME: Find a way to propagate errors
VERIFY(!framebuffer_device_or_error.is_error());
return framebuffer_device_or_error.release_value();
}
ErrorOr<Memory::Region*> FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
{
SpinlockLocker lock(m_activation_lock);
REQUIRE_PROMISE(video);
if (!shared)
return ENODEV;
if (offset != 0)
return ENXIO;
auto framebuffer_length = TRY(buffer_length(0));
if (range.size() != Memory::page_round_up(framebuffer_length))
return EOVERFLOW;
m_userspace_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_length)));
m_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_length)));
m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_length), AllocationStrategy::AllocateNow));
m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer", Memory::Region::Access::ReadWrite));
m_swapped_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_swapped_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite));
RefPtr<Memory::VMObject> chosen_vmobject;
if (m_graphical_writes_enabled) {
chosen_vmobject = m_real_framebuffer_vmobject;
} else {
chosen_vmobject = m_swapped_framebuffer_vmobject;
}
m_userspace_framebuffer_region = TRY(process.address_space().allocate_region_with_vmobject(
range,
chosen_vmobject.release_nonnull(),
0,
"Framebuffer",
prot,
shared));
return m_userspace_framebuffer_region;
}
void FramebufferDevice::deactivate_writes()
{
SpinlockLocker lock(m_activation_lock);
if (!m_userspace_framebuffer_region)
return;
auto framebuffer_length_or_error = buffer_length(0);
VERIFY(!framebuffer_length_or_error.is_error());
memcpy(m_swapped_framebuffer_region->vaddr().as_ptr(), m_real_framebuffer_region->vaddr().as_ptr(), Memory::page_round_up(framebuffer_length_or_error.release_value()));
auto vmobject = m_swapped_framebuffer_vmobject;
m_userspace_framebuffer_region->set_vmobject(vmobject.release_nonnull());
m_userspace_framebuffer_region->remap();
m_graphical_writes_enabled = false;
}
void FramebufferDevice::activate_writes()
{
SpinlockLocker lock(m_activation_lock);
if (!m_userspace_framebuffer_region || !m_real_framebuffer_vmobject)
return;
// restore the image we had in the void area
// FIXME: if we happen to have multiple Framebuffers that are writing to that location
// we will experience glitches...
auto framebuffer_length_or_error = buffer_length(0);
VERIFY(!framebuffer_length_or_error.is_error());
memcpy(m_real_framebuffer_region->vaddr().as_ptr(), m_swapped_framebuffer_region->vaddr().as_ptr(), Memory::page_round_up(framebuffer_length_or_error.release_value()));
auto vmobject = m_userspace_real_framebuffer_vmobject;
m_userspace_framebuffer_region->set_vmobject(vmobject.release_nonnull());
m_userspace_framebuffer_region->remap();
m_graphical_writes_enabled = true;
}
UNMAP_AFTER_INIT ErrorOr<void> FramebufferDevice::try_to_initialize()
{
// FIXME: Would be nice to be able to unify this with mmap above, but this
// function is UNMAP_AFTER_INIT for the time being.
auto framebuffer_length = TRY(buffer_length(0));
m_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_length)));
m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_length), AllocationStrategy::AllocateNow));
m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer", Memory::Region::Access::ReadWrite));
m_swapped_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_swapped_framebuffer_vmobject, Memory::page_round_up(framebuffer_length), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite));
return {};
}
UNMAP_AFTER_INIT FramebufferDevice::FramebufferDevice(const GenericGraphicsAdapter& adapter, PhysicalAddress addr, size_t width, size_t height, size_t pitch)
: GenericFramebufferDevice(adapter)
, m_framebuffer_address(addr)
, m_framebuffer_pitch(pitch)
, m_framebuffer_width(width)
, m_framebuffer_height(height)
{
VERIFY(!m_framebuffer_address.is_null());
VERIFY(m_framebuffer_pitch);
VERIFY(m_framebuffer_width);
VERIFY(m_framebuffer_height);
dbgln("Framebuffer {}: address={}, pitch={}, width={}, height={}", minor(), addr, pitch, width, height);
}
ErrorOr<size_t> FramebufferDevice::buffer_length(size_t head) const
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_resolution_lock);
auto adapter = m_graphics_adapter.strong_ref();
if (!adapter)
return Error::from_errno(EIO);
if (adapter->double_framebuffering_capable())
return m_framebuffer_pitch * m_framebuffer_height * 2;
return m_framebuffer_pitch * m_framebuffer_height;
}
ErrorOr<size_t> FramebufferDevice::pitch(size_t head) const
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_resolution_lock);
return m_framebuffer_pitch;
}
ErrorOr<size_t> FramebufferDevice::height(size_t head) const
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_resolution_lock);
return m_framebuffer_height;
}
ErrorOr<size_t> FramebufferDevice::width(size_t head) const
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_resolution_lock);
return m_framebuffer_width;
}
ErrorOr<size_t> FramebufferDevice::vertical_offset(size_t head) const
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_buffer_offset_lock);
return m_y_offset;
}
ErrorOr<bool> FramebufferDevice::vertical_offsetted(size_t head) const
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_buffer_offset_lock);
return m_y_offset == 0 ? 0 : 1;
}
ErrorOr<void> FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t)
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker buffer_offset_locker(m_buffer_offset_lock);
MutexLocker resolution_locker(m_resolution_lock);
auto adapter = m_graphics_adapter.strong_ref();
if (!adapter)
return Error::from_errno(EIO);
auto result = adapter->try_to_set_resolution(0, width, height);
// FIXME: Find a better way to return here a ErrorOr<void>.
if (!result)
return Error::from_errno(ENOTSUP);
m_framebuffer_width = width;
m_framebuffer_height = height;
m_framebuffer_pitch = width * sizeof(u32);
return {};
}
ErrorOr<void> FramebufferDevice::set_head_buffer(size_t head, bool second_buffer)
{
// Note: This FramebufferDevice class doesn't support multihead setup.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally have a value different than 0, assert.
VERIFY(head == 0);
MutexLocker locker(m_buffer_offset_lock);
auto adapter = m_graphics_adapter.strong_ref();
if (!adapter)
return Error::from_errno(EIO);
if (second_buffer) {
if (!adapter->set_y_offset(0, m_framebuffer_height)) {
// FIXME: Find a better ErrorOr<void> here.
return Error::from_errno(ENOTSUP);
}
m_y_offset = m_framebuffer_height;
} else {
if (!adapter->set_y_offset(0, 0)) {
// FIXME: Find a better ErrorOr<void> here.
return Error::from_errno(ENOTSUP);
}
m_y_offset = 0;
}
return {};
}
ErrorOr<void> FramebufferDevice::flush_head_buffer(size_t)
{
// Note: This FramebufferDevice class doesn't support flushing.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally reach this code, assert.
VERIFY_NOT_REACHED();
}
ErrorOr<void> FramebufferDevice::flush_rectangle(size_t, FBRect const&)
{
// Note: This FramebufferDevice class doesn't support partial flushing.
// We take care to verify this at the GenericFramebufferDevice::ioctl method
// so if we happen to accidentally reach this code, assert.
VERIFY_NOT_REACHED();
}
}
|