Serenity Operating System
at master 137 lines 6.5 kB view raw
1/* 2 * Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <Kernel/API/Ioctl.h> 8#include <Kernel/API/VirGL.h> 9#include <Kernel/Graphics/GraphicsManagement.h> 10#include <Kernel/Graphics/VirtIOGPU/Console.h> 11#include <Kernel/Graphics/VirtIOGPU/GPU3DDevice.h> 12#include <Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h> 13#include <Kernel/Graphics/VirtIOGPU/Protocol.h> 14#include <Kernel/Random.h> 15 16namespace Kernel { 17 18VirtIOGPU3DDevice::PerContextState::PerContextState(Graphics::VirtIOGPU::ContextID context_id, OwnPtr<Memory::Region> transfer_buffer_region) 19 : m_context_id(context_id) 20 , m_transfer_buffer_region(move(transfer_buffer_region)) 21{ 22} 23 24ErrorOr<NonnullLockRefPtr<VirtIOGPU3DDevice>> VirtIOGPU3DDevice::try_create(VirtIOGraphicsAdapter& adapter) 25{ 26 // Setup memory transfer region 27 auto region_result = TRY(MM.allocate_kernel_region( 28 NUM_TRANSFER_REGION_PAGES * PAGE_SIZE, 29 "VIRGL3D kernel upload buffer"sv, 30 Memory::Region::Access::ReadWrite, 31 AllocationStrategy::AllocateNow)); 32 auto kernel_context_id = TRY(adapter.create_context()); 33 return TRY(DeviceManagement::try_create_device<VirtIOGPU3DDevice>(adapter, move(region_result), kernel_context_id)); 34} 35 36VirtIOGPU3DDevice::VirtIOGPU3DDevice(VirtIOGraphicsAdapter const& graphics_adapter, NonnullOwnPtr<Memory::Region> transfer_buffer_region, Graphics::VirtIOGPU::ContextID kernel_context_id) 37 : CharacterDevice(28, 0) 38 , m_graphics_adapter(graphics_adapter) 39 , m_kernel_context_id(kernel_context_id) 40 , m_transfer_buffer_region(move(transfer_buffer_region)) 41{ 42} 43 44void VirtIOGPU3DDevice::detach(OpenFileDescription& description) 45{ 46 m_context_state_lookup.remove(&description); 47 CharacterDevice::detach(description); 48} 49 50ErrorOr<LockRefPtr<VirtIOGPU3DDevice::PerContextState>> VirtIOGPU3DDevice::get_context_for_description(OpenFileDescription& description) 51{ 52 auto res = m_context_state_lookup.get(&description); 53 if (!res.has_value()) 54 return EBADF; 55 return res.value(); 56} 57 58ErrorOr<void> VirtIOGPU3DDevice::ioctl(OpenFileDescription& description, unsigned request, Userspace<void*> arg) 59{ 60 // TODO: We really should have ioctls for destroying resources as well 61 switch (request) { 62 case VIRGL_IOCTL_CREATE_CONTEXT: { 63 if (m_context_state_lookup.contains(&description)) 64 return EEXIST; 65 SpinlockLocker locker(m_graphics_adapter->operation_lock()); 66 // TODO: Delete the context if it fails to be set in m_context_state_lookup 67 auto context_id = TRY(m_graphics_adapter->create_context()); 68 LockRefPtr<PerContextState> per_context_state = TRY(PerContextState::try_create(context_id)); 69 TRY(m_context_state_lookup.try_set(&description, per_context_state)); 70 return {}; 71 } 72 case VIRGL_IOCTL_TRANSFER_DATA: { 73 auto& transfer_buffer_region = TRY(get_context_for_description(description))->transfer_buffer_region(); 74 auto user_transfer_descriptor = static_ptr_cast<VirGLTransferDescriptor const*>(arg); 75 auto transfer_descriptor = TRY(copy_typed_from_user(user_transfer_descriptor)); 76 if (Checked<size_t>::addition_would_overflow(transfer_descriptor.offset_in_region, transfer_descriptor.num_bytes)) { 77 return EOVERFLOW; 78 } 79 if (transfer_descriptor.offset_in_region + transfer_descriptor.num_bytes > NUM_TRANSFER_REGION_PAGES * PAGE_SIZE) { 80 return EOVERFLOW; 81 } 82 if (transfer_descriptor.direction == VIRGL_DATA_DIR_GUEST_TO_HOST) { 83 auto target = transfer_buffer_region.vaddr().offset(transfer_descriptor.offset_in_region).as_ptr(); 84 return copy_from_user(target, transfer_descriptor.data, transfer_descriptor.num_bytes); 85 } else if (transfer_descriptor.direction == VIRGL_DATA_DIR_HOST_TO_GUEST) { 86 auto source = transfer_buffer_region.vaddr().offset(transfer_descriptor.offset_in_region).as_ptr(); 87 return copy_to_user(transfer_descriptor.data, source, transfer_descriptor.num_bytes); 88 } else { 89 return EINVAL; 90 } 91 } 92 case VIRGL_IOCTL_SUBMIT_CMD: { 93 auto context_id = TRY(get_context_for_description(description))->context_id(); 94 SpinlockLocker locker(m_graphics_adapter->operation_lock()); 95 auto user_command_buffer = static_ptr_cast<VirGLCommandBuffer const*>(arg); 96 auto command_buffer = TRY(copy_typed_from_user(user_command_buffer)); 97 TRY(m_graphics_adapter->submit_command_buffer(context_id, [&](Bytes buffer) { 98 auto num_bytes = command_buffer.num_elems * sizeof(u32); 99 VERIFY(num_bytes <= buffer.size()); 100 MUST(copy_from_user(buffer.data(), command_buffer.data, num_bytes)); 101 return num_bytes; 102 })); 103 return {}; 104 } 105 case VIRGL_IOCTL_CREATE_RESOURCE: { 106 auto per_context_state = TRY(get_context_for_description(description)); 107 auto user_spec = static_ptr_cast<VirGL3DResourceSpec const*>(arg); 108 VirGL3DResourceSpec spec = TRY(copy_typed_from_user(user_spec)); 109 110 Graphics::VirtIOGPU::Protocol::Resource3DSpecification const resource_spec = { 111 .target = static_cast<Graphics::VirtIOGPU::Protocol::Gallium::PipeTextureTarget>(spec.target), 112 .format = spec.format, 113 .bind = spec.bind, 114 .width = spec.width, 115 .height = spec.height, 116 .depth = spec.depth, 117 .array_size = spec.array_size, 118 .last_level = spec.last_level, 119 .nr_samples = spec.nr_samples, 120 .flags = spec.flags, 121 .padding = 0, 122 }; 123 SpinlockLocker locker(m_graphics_adapter->operation_lock()); 124 // FIXME: What would be an appropriate resource free-ing mechanism to use in case anything 125 // after this fails? 126 auto resource_id = TRY(m_graphics_adapter->create_3d_resource(resource_spec)); 127 TRY(m_graphics_adapter->attach_resource_to_context(resource_id, per_context_state->context_id())); 128 TRY(m_graphics_adapter->ensure_backing_storage(resource_id, per_context_state->transfer_buffer_region(), 0, NUM_TRANSFER_REGION_PAGES * PAGE_SIZE)); 129 spec.created_resource_id = resource_id.value(); 130 // FIXME: We should delete the resource we just created if we fail to copy the resource id out 131 return copy_to_user(static_ptr_cast<VirGL3DResourceSpec*>(arg), &spec); 132 } 133 } 134 return EINVAL; 135} 136 137}