The Pedigree Project 0.1
Virtqueue.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef PEDIGREE_VIRTIO_QUEUE_H
3#define PEDIGREE_VIRTIO_QUEUE_H
4
5#include "pedigree/kernel/compiler.h"
6#include "pedigree/kernel/process/Mutex.h"
7#include "pedigree/kernel/processor/MemoryRegion.h"
8#include "pedigree/kernel/processor/types.h"
9
10namespace Virtio {
11
12class PciTransport;
13
14struct Buffer {
15 uint64_t address;
16 uint32_t length;
17 bool deviceWrites;
18};
19
20struct Completion {
21 void* cookie;
22 uint32_t length;
23};
24
25class EXPORTED_PUBLIC Queue {
26 public:
27 Queue();
28 ~Queue();
29
30 // Payload addresses must refer to DMA-safe, physically contiguous memory.
31 // The caller retains them until pop() or a successful transport reset.
32 bool submit(const Buffer* buffers, size_t count, void* cookie);
33 bool pop(Completion& completion);
34 uint16_t depth() const {
35 return m_Depth;
36 }
37 void stop();
38
39 private:
40 friend class PciTransport;
41 static constexpr uint16_t MaxDepth = 256;
42 static constexpr uint16_t Invalid = 0xffff;
43
44 struct Descriptor {
45 uint64_t address;
46 uint32_t length;
47 uint16_t flags;
48 uint16_t next;
49 } __attribute__((packed));
50
51 bool initialise(uint16_t depth, PciTransport* transport);
52 uint64_t descriptorAddress() const {
53 return m_Descriptors.physicalAddress();
54 }
55 uint64_t availableAddress() const {
56 return m_Available.physicalAddress();
57 }
58 uint64_t usedAddress() const {
59 return m_Used.physicalAddress();
60 }
61
62 MemoryRegion m_Descriptors;
63 MemoryRegion m_Available;
64 MemoryRegion m_Used;
65 Mutex m_Lock;
66 PciTransport* m_Transport;
67 void* m_Cookies[MaxDepth];
68 uint16_t m_Next[MaxDepth];
69 uint16_t m_ChainLength[MaxDepth];
70 uint16_t m_FreeHead;
71 uint16_t m_FreeCount;
72 uint16_t m_Depth;
73 uint16_t m_AvailableIndex;
74 uint16_t m_UsedIndex;
75 bool m_Active[MaxDepth];
76 bool m_Online;
77 bool m_DmaArmed;
78 bool m_Attached;
79};
80
81} // namespace Virtio
82#endif
Special memory entity in the kernel's virtual address space.
Definition Mutex.h:56