2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-03-05 15:23:08 +03:00
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-03-05 15:23:08 +03:00
|
|
|
#include <AK/Checked.h>
|
|
|
|
#include <Kernel/Debug.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2021-03-05 15:23:08 +03:00
|
|
|
#include <Kernel/Graphics/FramebufferDevice.h>
|
|
|
|
#include <Kernel/Graphics/GraphicsManagement.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2019-08-18 07:54:52 +03:00
|
|
|
#include <Kernel/Process.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2019-08-18 07:54:52 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
|
|
|
|
2021-05-22 09:51:55 +03:00
|
|
|
#define MAX_RESOLUTION_WIDTH 4096
|
|
|
|
#define MAX_RESOLUTION_HEIGHT 2160
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-05-22 09:51:55 +03:00
|
|
|
NonnullRefPtr<FramebufferDevice> FramebufferDevice::create(const GraphicsDevice& adapter, size_t output_port_index, PhysicalAddress paddr, size_t width, size_t height, size_t pitch)
|
|
|
|
{
|
2021-09-11 09:19:20 +03:00
|
|
|
auto framebuffer_device_or_error = DeviceManagement::try_create_device<FramebufferDevice>(adapter, output_port_index, paddr, width, height, pitch);
|
2021-09-10 14:44:46 +03:00
|
|
|
// FIXME: Find a way to propagate errors
|
|
|
|
VERIFY(!framebuffer_device_or_error.is_error());
|
|
|
|
return framebuffer_device_or_error.release_value();
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
KResultOr<Memory::Region*> FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
|
2019-08-18 07:54:52 +03:00
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(m_activation_lock);
|
2020-01-12 04:17:30 +03:00
|
|
|
REQUIRE_PROMISE(video);
|
2020-02-28 22:47:27 +03:00
|
|
|
if (!shared)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENODEV;
|
2021-01-28 07:23:20 +03:00
|
|
|
if (offset != 0)
|
|
|
|
return ENXIO;
|
2021-08-06 14:49:36 +03:00
|
|
|
if (range.size() != Memory::page_round_up(framebuffer_size_in_bytes()))
|
2021-01-28 07:23:20 +03:00
|
|
|
return EOVERFLOW;
|
|
|
|
|
2021-09-06 03:09:42 +03:00
|
|
|
m_userspace_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_size_in_bytes())));
|
|
|
|
m_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_size_in_bytes())));
|
|
|
|
m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_size_in_bytes()), AllocationStrategy::AllocateNow));
|
2021-09-06 02:36:14 +03:00
|
|
|
m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_size_in_bytes()), "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_size_in_bytes()), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite));
|
2021-05-18 21:34:22 +03:00
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
RefPtr<Memory::VMObject> chosen_vmobject;
|
2021-05-20 21:28:18 +03:00
|
|
|
if (m_graphical_writes_enabled) {
|
|
|
|
chosen_vmobject = m_real_framebuffer_vmobject;
|
|
|
|
} else {
|
|
|
|
chosen_vmobject = m_swapped_framebuffer_vmobject;
|
|
|
|
}
|
2021-09-06 03:09:42 +03:00
|
|
|
m_userspace_framebuffer_region = TRY(process.address_space().allocate_region_with_vmobject(
|
2021-01-25 16:52:36 +03:00
|
|
|
range,
|
2021-05-20 21:28:18 +03:00
|
|
|
chosen_vmobject.release_nonnull(),
|
2019-08-18 07:54:52 +03:00
|
|
|
0,
|
2021-03-05 15:23:08 +03:00
|
|
|
"Framebuffer",
|
2021-01-02 18:38:05 +03:00
|
|
|
prot,
|
2021-09-06 03:09:42 +03:00
|
|
|
shared));
|
|
|
|
return m_userspace_framebuffer_region;
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
|
2021-05-21 06:53:31 +03:00
|
|
|
void FramebufferDevice::deactivate_writes()
|
2021-04-16 22:58:51 +03:00
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(m_activation_lock);
|
2021-04-16 22:58:51 +03:00
|
|
|
if (!m_userspace_framebuffer_region)
|
|
|
|
return;
|
2021-08-06 14:49:36 +03:00
|
|
|
memcpy(m_swapped_framebuffer_region->vaddr().as_ptr(), m_real_framebuffer_region->vaddr().as_ptr(), Memory::page_round_up(framebuffer_size_in_bytes()));
|
2021-04-16 22:58:51 +03:00
|
|
|
auto vmobject = m_swapped_framebuffer_vmobject;
|
|
|
|
m_userspace_framebuffer_region->set_vmobject(vmobject.release_nonnull());
|
|
|
|
m_userspace_framebuffer_region->remap();
|
2021-05-20 21:28:18 +03:00
|
|
|
m_graphical_writes_enabled = false;
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
void FramebufferDevice::activate_writes()
|
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(m_activation_lock);
|
2021-04-16 22:58:51 +03:00
|
|
|
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...
|
2021-08-06 14:49:36 +03:00
|
|
|
memcpy(m_real_framebuffer_region->vaddr().as_ptr(), m_swapped_framebuffer_region->vaddr().as_ptr(), Memory::page_round_up(framebuffer_size_in_bytes()));
|
2021-04-16 22:58:51 +03:00
|
|
|
auto vmobject = m_userspace_real_framebuffer_vmobject;
|
|
|
|
m_userspace_framebuffer_region->set_vmobject(vmobject.release_nonnull());
|
|
|
|
m_userspace_framebuffer_region->remap();
|
2021-05-20 21:28:18 +03:00
|
|
|
m_graphical_writes_enabled = true;
|
2019-08-18 07:54:52 +03:00
|
|
|
}
|
|
|
|
|
2021-08-15 12:07:59 +03:00
|
|
|
UNMAP_AFTER_INIT KResult FramebufferDevice::initialize()
|
2021-04-16 22:58:51 +03:00
|
|
|
{
|
2021-08-15 12:07:59 +03:00
|
|
|
// FIXME: Would be nice to be able to unify this with mmap above, but this
|
|
|
|
// function is UNMAP_AFTER_INIT for the time being.
|
2021-09-06 03:09:42 +03:00
|
|
|
m_real_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(m_framebuffer_address, Memory::page_round_up(framebuffer_size_in_bytes())));
|
|
|
|
m_swapped_framebuffer_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(Memory::page_round_up(framebuffer_size_in_bytes()), AllocationStrategy::AllocateNow));
|
2021-09-06 02:36:14 +03:00
|
|
|
m_real_framebuffer_region = TRY(MM.allocate_kernel_region_with_vmobject(*m_real_framebuffer_vmobject, Memory::page_round_up(framebuffer_size_in_bytes()), "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_size_in_bytes()), "Framebuffer Swap (Blank)", Memory::Region::Access::ReadWrite));
|
2021-08-15 12:07:59 +03:00
|
|
|
return KSuccess;
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
|
2021-05-22 09:51:55 +03:00
|
|
|
UNMAP_AFTER_INIT FramebufferDevice::FramebufferDevice(const GraphicsDevice& adapter, size_t output_port_index, PhysicalAddress addr, size_t width, size_t height, size_t pitch)
|
2021-05-16 21:03:33 +03:00
|
|
|
: BlockDevice(29, GraphicsManagement::the().allocate_minor_device_number())
|
2021-03-05 15:23:08 +03:00
|
|
|
, m_framebuffer_address(addr)
|
|
|
|
, m_framebuffer_pitch(pitch)
|
|
|
|
, m_framebuffer_width(width)
|
|
|
|
, m_framebuffer_height(height)
|
2021-05-22 09:51:55 +03:00
|
|
|
, m_output_port_index(output_port_index)
|
|
|
|
, m_graphics_adapter(adapter)
|
2021-03-05 15:23:08 +03:00
|
|
|
{
|
2021-04-16 22:58:51 +03:00
|
|
|
VERIFY(!m_framebuffer_address.is_null());
|
|
|
|
VERIFY(m_framebuffer_pitch);
|
|
|
|
VERIFY(m_framebuffer_width);
|
|
|
|
VERIFY(m_framebuffer_height);
|
2021-03-05 15:23:08 +03:00
|
|
|
dbgln("Framebuffer {}: address={}, pitch={}, width={}, height={}", minor(), addr, pitch, width, height);
|
|
|
|
}
|
|
|
|
|
2021-05-22 09:51:55 +03:00
|
|
|
size_t FramebufferDevice::framebuffer_size_in_bytes() const
|
2021-03-05 15:23:08 +03:00
|
|
|
{
|
2021-05-22 09:51:55 +03:00
|
|
|
if (m_graphics_adapter->double_framebuffering_capable())
|
|
|
|
return m_framebuffer_pitch * m_framebuffer_height * 2;
|
|
|
|
return m_framebuffer_pitch * m_framebuffer_height;
|
2021-03-05 15:23:08 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
KResult FramebufferDevice::ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg)
|
2019-08-18 07:54:52 +03:00
|
|
|
{
|
2020-01-12 04:17:30 +03:00
|
|
|
REQUIRE_PROMISE(video);
|
2019-08-18 07:54:52 +03:00
|
|
|
switch (request) {
|
|
|
|
case FB_IOCTL_GET_SIZE_IN_BYTES: {
|
2021-07-26 12:47:00 +03:00
|
|
|
auto user_size = static_ptr_cast<size_t*>(arg);
|
2020-07-31 01:17:25 +03:00
|
|
|
size_t value = framebuffer_size_in_bytes();
|
2021-09-05 18:38:37 +03:00
|
|
|
return copy_to_user(user_size, &value);
|
2019-08-18 07:54:52 +03:00
|
|
|
}
|
|
|
|
case FB_IOCTL_GET_BUFFER: {
|
2021-07-26 12:47:00 +03:00
|
|
|
auto user_index = static_ptr_cast<int*>(arg);
|
2021-05-22 09:51:55 +03:00
|
|
|
int value = m_y_offset == 0 ? 0 : 1;
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_to_user(user_index, &value));
|
2021-05-22 09:51:55 +03:00
|
|
|
if (!m_graphics_adapter->double_framebuffering_capable())
|
2021-07-26 13:47:25 +03:00
|
|
|
return ENOTIMPL;
|
|
|
|
return KSuccess;
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
|
|
|
case FB_IOCTL_SET_BUFFER: {
|
2021-07-26 12:47:00 +03:00
|
|
|
auto buffer = static_cast<int>(arg.ptr());
|
|
|
|
if (buffer != 0 && buffer != 1)
|
2021-07-26 13:47:25 +03:00
|
|
|
return EINVAL;
|
2021-05-22 09:51:55 +03:00
|
|
|
if (!m_graphics_adapter->double_framebuffering_capable())
|
2021-07-26 13:47:25 +03:00
|
|
|
return ENOTIMPL;
|
2021-07-26 12:47:00 +03:00
|
|
|
m_graphics_adapter->set_y_offset(m_output_port_index, buffer == 0 ? 0 : m_framebuffer_height);
|
2021-07-26 13:47:25 +03:00
|
|
|
return KSuccess;
|
2019-08-18 07:54:52 +03:00
|
|
|
}
|
|
|
|
case FB_IOCTL_GET_RESOLUTION: {
|
2021-07-26 12:47:00 +03:00
|
|
|
auto user_resolution = static_ptr_cast<FBResolution*>(arg);
|
|
|
|
FBResolution resolution {};
|
2020-07-31 01:17:25 +03:00
|
|
|
resolution.pitch = m_framebuffer_pitch;
|
|
|
|
resolution.width = m_framebuffer_width;
|
|
|
|
resolution.height = m_framebuffer_height;
|
2021-09-05 18:38:37 +03:00
|
|
|
return copy_to_user(user_resolution, &resolution);
|
2019-08-18 07:54:52 +03:00
|
|
|
}
|
|
|
|
case FB_IOCTL_SET_RESOLUTION: {
|
2021-07-26 12:47:00 +03:00
|
|
|
auto user_resolution = static_ptr_cast<FBResolution*>(arg);
|
2020-07-31 01:17:25 +03:00
|
|
|
FBResolution resolution;
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_from_user(&resolution, user_resolution));
|
2021-05-22 09:51:55 +03:00
|
|
|
if (resolution.width > MAX_RESOLUTION_WIDTH || resolution.height > MAX_RESOLUTION_HEIGHT)
|
2021-07-26 13:47:25 +03:00
|
|
|
return EINVAL;
|
2021-05-22 09:51:55 +03:00
|
|
|
|
|
|
|
if (!m_graphics_adapter->modesetting_capable()) {
|
|
|
|
resolution.pitch = m_framebuffer_pitch;
|
|
|
|
resolution.width = m_framebuffer_width;
|
|
|
|
resolution.height = m_framebuffer_height;
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_to_user(user_resolution, &resolution));
|
2021-07-26 13:47:25 +03:00
|
|
|
return ENOTIMPL;
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_graphics_adapter->try_to_set_resolution(m_output_port_index, resolution.width, resolution.height)) {
|
|
|
|
m_framebuffer_pitch = m_framebuffer_width * sizeof(u32);
|
|
|
|
dbgln_if(FRAMEBUFFER_DEVICE_DEBUG, "Reverting resolution: [{}x{}]", m_framebuffer_width, m_framebuffer_height);
|
|
|
|
// Note: We try to revert everything back, and if it doesn't work, just assert.
|
|
|
|
if (!m_graphics_adapter->try_to_set_resolution(m_output_port_index, m_framebuffer_width, m_framebuffer_height)) {
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
resolution.pitch = m_framebuffer_pitch;
|
|
|
|
resolution.width = m_framebuffer_width;
|
|
|
|
resolution.height = m_framebuffer_height;
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_to_user(user_resolution, &resolution));
|
2021-07-26 13:47:25 +03:00
|
|
|
return EINVAL;
|
2021-05-22 09:51:55 +03:00
|
|
|
}
|
|
|
|
m_framebuffer_width = resolution.width;
|
|
|
|
m_framebuffer_height = resolution.height;
|
|
|
|
m_framebuffer_pitch = m_framebuffer_width * sizeof(u32);
|
|
|
|
|
|
|
|
dbgln_if(FRAMEBUFFER_DEVICE_DEBUG, "New resolution: [{}x{}]", m_framebuffer_width, m_framebuffer_height);
|
2020-07-31 01:17:25 +03:00
|
|
|
resolution.pitch = m_framebuffer_pitch;
|
|
|
|
resolution.width = m_framebuffer_width;
|
|
|
|
resolution.height = m_framebuffer_height;
|
2021-09-05 18:38:37 +03:00
|
|
|
return copy_to_user(user_resolution, &resolution);
|
2019-08-18 07:54:52 +03:00
|
|
|
}
|
2021-07-04 05:42:38 +03:00
|
|
|
case FB_IOCTL_GET_BUFFER_OFFSET: {
|
2021-07-26 12:47:00 +03:00
|
|
|
auto user_buffer_offset = static_ptr_cast<FBBufferOffset*>(arg);
|
2021-07-04 05:42:38 +03:00
|
|
|
FBBufferOffset buffer_offset;
|
2021-09-05 18:38:37 +03:00
|
|
|
TRY(copy_from_user(&buffer_offset, user_buffer_offset));
|
2021-07-04 05:42:38 +03:00
|
|
|
if (buffer_offset.buffer_index != 0 && buffer_offset.buffer_index != 1)
|
2021-07-26 13:47:25 +03:00
|
|
|
return EINVAL;
|
2021-07-04 05:42:38 +03:00
|
|
|
buffer_offset.offset = (size_t)buffer_offset.buffer_index * m_framebuffer_pitch * m_framebuffer_height;
|
2021-09-05 18:38:37 +03:00
|
|
|
return copy_to_user(user_buffer_offset, &buffer_offset);
|
2021-07-04 05:42:38 +03:00
|
|
|
}
|
2021-06-26 20:04:01 +03:00
|
|
|
case FB_IOCTL_FLUSH_BUFFERS:
|
2021-07-26 13:47:25 +03:00
|
|
|
return ENOTSUP;
|
2019-08-18 07:54:52 +03:00
|
|
|
default:
|
2021-07-26 13:47:25 +03:00
|
|
|
return EINVAL;
|
2019-08-18 07:54:52 +03:00
|
|
|
};
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|