12#include <l4/sys/cxx/ipc_epiface>
14#include <l4/l4virtio/server/virtio>
15#include <l4/l4virtio/server/l4virtio>
16#include <l4/l4virtio/l4virtio>
19#include <l4/re/util/object_registry>
20#include <l4/re/util/br_manager>
21#include <l4/sys/cxx/ipc_epiface>
35struct I2c_request_flags
39 CXX_BITFIELD_MEMBER(0, 0, fail_next, raw);
40 CXX_BITFIELD_MEMBER(1, 1, m_rd, raw);
42static_assert(
sizeof(I2c_request_flags) == 4,
43 "I2c_request_flags contains padding bytes.");
49 I2c_request_flags flags;
51static_assert(
sizeof(I2c_out_hdr) == 8,
"I2c_out_hdr contains padding bytes.");
57static_assert(
sizeof(I2c_in_hdr) == 1,
"I2c_in_hdr contains padding bytes.");
61 struct I2c_out_hdr out_hdr;
64 struct I2c_in_hdr *in_hdr;
70 in_hdr->status = status;
84template <
typename Request_handler,
94 Num_request_queues = 1,
99 using I2c_request_handler = Request_handler;
113 _i2c->handle_queue();
135 Data_buffer(L4virtio::Svr::Driver_mem_region
const *r,
139 pos =
static_cast<char *
>(r->local(d.
addr));
147 : _q(q), _req_handler(hndlr), _i2c(i2c), _head(), _req(),
175 memcpy(&request.out_hdr, _req.pos,
sizeof(I2c_out_hdr));
178 request.write_size =
sizeof(I2c_in_hdr);
186 request.buf_len += req.left;
187 request.buf =
reinterpret_cast<l4_uint8_t *
>(req.pos);
194 if (request.out_hdr.flags.m_rd())
195 request.write_size += request.buf_len;
198 request.in_hdr =
reinterpret_cast<I2c_in_hdr *
>(req.pos);
203 request.in_hdr =
reinterpret_cast<I2c_in_hdr *
>(request.buf);
204 request.buf =
nullptr;
211 void handle_request()
217 using Consumed_entry =
219 std::vector<Consumed_entry> consumed;
226 r.set_status(I2c_msg_err);
227 _fail_next = r.out_hdr.flags.fail_next();
232 if (r.out_hdr.flags.m_rd())
233 ok = _req_handler->handle_read(r.buf, r.buf_len);
235 ok = _req_handler->handle_write(r.buf, r.buf_len);
238 r.set_status(I2c_msg_ok);
243 r.set_status(I2c_msg_err);
244 _fail_next = r.out_hdr.flags.fail_next();
247 consumed.emplace_back(_head, r.write_size);
252 _q->
finish(consumed.begin(), consumed.end(), _i2c);
259 I2c_request_handler *_req_handler;
268 Features() =
default;
273 CXX_BITFIELD_MEMBER(0, 0, zero_length_request,
raw);
277 :
L4virtio::Svr::Device(&_dev_config),
281 _request_processor(&_q, hndlr, this)
289 hf.ring_indirect_desc() =
true;
290 hf.zero_length_request() =
true;
291 _dev_config.host_features(0) = hf.raw;
293 _dev_config.reset_hdr();
307 _request_processor.handle_request();
340 long op_set_status(L4virtio::Device::Rights r,
unsigned status)
342 return L4virtio::Svr::Device::op_set_status(r, status);
345 long op_config_queue(L4virtio::Device::Rights r,
unsigned queue)
347 return L4virtio::Svr::Device::op_config_queue(r, queue);
350 long op_device_config(L4virtio::Device::Rights r,
354 return L4virtio::Svr::Device::op_device_config(r, config_ds, ds_offset);
359 return L4::cap_cast<L4::Irq>(_host_irq.
obj_cap());
365 (server_iface()->
template rcv_cap<L4::Irq>(0));
372 L4virtio::Svr::Dev_config_t<L4virtio::Svr::No_custom_data>_dev_config;
373 I2c_request_handler *_req_handler;
A registry that manages server objects and their attached IPC gates for a single server loop for a sp...
L4::Cap< L4::Irq > register_irq_obj(L4::Epiface *o) override
Register a handler for an interrupt.
C++ interface for capabilities.
Capability type for RPC interfaces (see L4::Cap<T>).
Interface for server-loop related functions.
IPC interface for virtio over L4 IPC.
Server-side L4-VIRTIO device stub.
bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
Enable/disable the specified queue.
void init_mem_info(unsigned num)
Initialize the memory region list to the given maximum.
Mem_list const * mem_info() const
Get the memory region list used for this device.
void reset_queue_config(unsigned idx, unsigned num_max, bool inc_generation=false)
Trigger reset for the configuration space for queue idx.
Encapsulate the state for processing a VIRTIO request.
bool next(DESC_MAN *dm, ARGS... args)
Switch to the next descriptor in a descriptor chain.
void start(DESC_MAN *dm, Virtqueue *ring, Virtqueue::Head_desc const &request, ARGS... args)
Start processing a new request.
Handler for the host irq.
Handler for the Virtio requests.
I2c_req get_request()
Linux prepares the I2C request in three data parts: 1st: out_hdr 2nd: buffer (optional) 3rd: in_hdr.
A server implementation of the virtio-i2c protocol.
void trigger_driver_config_irq() override
callback for triggering configuration change notification IRQ
void register_single_driver_irq() override
callback for registering a single guest IRQ for all queues (old-style)
void reset() override
reset callback, called for doing a device reset
bool check_queues() override
callback for checking if the queues at DRIVER_OK transition
L4::Cap< L4::Irq > device_notify_irq() const override
callback to gather the device notification IRQ (old-style)
int reconfig_queue(unsigned idx) override
callback for client queue-config request
VIRTIO request, essentially a descriptor from the available ring.
Virtqueue implementation for the device.
Request next_avail()
Get the next available descriptor from the available ring.
void finish(Head_desc &d, QUEUE_OBSERVER *o, l4_uint32_t len=0)
Add a descriptor to the used ring, and notify an observer.
Descriptor in the descriptor table.
l4_uint32_t len
Length of described buffer.
Ptr< void > addr
Address stored in descriptor.
bool no_notify_guest() const
Get the no IRQ flag of this queue.
unsigned long l4_addr_t
Address type.
unsigned char l4_uint8_t
Unsigned 8bit value.
unsigned int l4_uint32_t
Unsigned 32bit value.
unsigned short int l4_uint16_t
Unsigned 16bit value.
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
@ L4VIRTIO_ID_I2C
I2C device.
@ L4VIRTIO_IRQ_STATUS_VRING
VRING IRQ pending flag.
@ L4VIRTIO_IRQ_STATUS_CONFIG
CONFIG IRQ pending flag.
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
l4_msgtag_t chkipc(l4_msgtag_t tag, char const *extra="", l4_utcb_t *utcb=l4_utcb())
Test a message tag for IPC errors.
L4-VIRTIO Transport C++ API.
Epiface implementation for Kobject-based interface implementations.
Server_iface * server_iface() const
Get pointer to server interface at which the object is currently registered.
Epiface implementation for interrupt handlers.
Cap< L4::Irq > obj_cap() const
Get the (typed) capability to this object.
l4_msgtag_t trigger(l4_utcb_t *utcb=l4_utcb()) noexcept
Trigger the object.
l4_uint32_t left
Bytes left in buffer.
char * pos
Current buffer position.
Type for device feature bitmap.
l4_uint32_t raw
The raw value of the features bitmap.