Loading...
Searching...
No Matches
virtio-i2c-device
// vi:set ft=cpp: -*- Mode: C++ -*-
/*
* Copyright (C) 2024 Kernkonzept GmbH.
* Author(s): Martin Kuettler <martin.kuettler@kernkonzept.com>
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <l4/re/error_helper>
#include <l4/sys/cxx/ipc_epiface>
#include <l4/l4virtio/server/virtio>
#include <l4/l4virtio/server/l4virtio>
#include <l4/l4virtio/l4virtio>
#include <l4/re/error_helper>
#include <l4/re/util/object_registry>
#include <l4/re/util/br_manager>
#include <l4/sys/cxx/ipc_epiface>
#include <vector>
#include <l4/cxx/pair>
namespace L4virtio {
namespace Svr {
enum : l4_uint8_t
{
I2c_msg_ok = 0,
I2c_msg_err = 1,
};
struct I2c_request_flags
{
l4_uint32_t raw;
CXX_BITFIELD_MEMBER(0, 0, fail_next, raw);
CXX_BITFIELD_MEMBER(1, 1, m_rd, raw);
};
static_assert(sizeof(I2c_request_flags) == 4,
"I2c_request_flags contains padding bytes.");
struct I2c_out_hdr
{
l4_uint16_t addr;
l4_uint16_t padding;
I2c_request_flags flags;
};
static_assert(sizeof(I2c_out_hdr) == 8, "I2c_out_hdr contains padding bytes.");
struct I2c_in_hdr
{
l4_uint8_t status;
};
static_assert(sizeof(I2c_in_hdr) == 1, "I2c_in_hdr contains padding bytes.");
struct I2c_req
{
struct I2c_out_hdr out_hdr;
unsigned buf_len;
l4_uint8_t* buf;
struct I2c_in_hdr *in_hdr;
unsigned write_size;
void set_status(l4_uint8_t status)
{
in_hdr->status = status;
}
};
template <typename Request_handler,
typename Epiface = L4virtio::Device>
class Virtio_i2c : public L4virtio::Svr::Device,
public L4::Epiface_t<Virtio_i2c<Request_handler,
Epiface>,
Epiface>
{
private:
enum
{
Num_request_queues = 1,
queue_size = 128,
};
public:
using I2c_request_handler = Request_handler;
class Host_irq : public L4::Irqep_t<Host_irq>
{
public:
explicit Host_irq(Virtio_i2c *i2c) : L4::Irqep_t<Host_irq>(), _i2c(i2c) {}
void handle_irq()
{
_i2c->handle_queue();
}
private:
Virtio_i2c *_i2c;
};
class Request_processor : public L4virtio::Svr::Request_processor
{
public:
struct Data_buffer : public L4virtio::Svr::Data_buffer
{
Data_buffer()
{
pos = nullptr;
left = 0;
}
// This constructor is called from within start, so make it available.
Data_buffer(L4virtio::Svr::Driver_mem_region const *r,
L4virtio::Svr::Virtqueue::Desc const &d,
L4virtio::Svr::Request_processor const *)
{
pos = static_cast<char *>(r->local(d.addr));
left = d.len;
}
};
Request_processor(L4virtio::Svr::Virtqueue *q, I2c_request_handler *hndlr,
Virtio_i2c *i2c)
: _q(q), _req_handler(hndlr), _i2c(i2c), _head(), _req(),
_fail_next(false)
{}
bool init_queue()
{
auto r = _q->next_avail();
if (L4_UNLIKELY(!r))
return false;
_head = start(_i2c->mem_info(), r, &_req);
return true;
}
I2c_req get_request()
{
I2c_req request;
memcpy(&request.out_hdr, _req.pos, sizeof(I2c_out_hdr));
// number of bytes to be written in the answer.
request.write_size = sizeof(I2c_in_hdr);
request.buf_len = 0;
Data_buffer req;
// 2nd part: either the optional buffer or the in_hdr
if (next(_i2c->mem_info(), &req))
{
request.buf_len += req.left;
request.buf = reinterpret_cast< l4_uint8_t *>(req.pos);
}
// 3rd part: in_hdr
if (next(_i2c->mem_info(), &req))
{
// 2nd part was indeed a buffer
if (request.out_hdr.flags.m_rd())
request.write_size += request.buf_len;
// actual 3rd part
request.in_hdr = reinterpret_cast<I2c_in_hdr *>(req.pos);
}
else
{
// no 3rd part, 2nd part is in_hdr;
request.in_hdr = reinterpret_cast<I2c_in_hdr *>(request.buf);
request.buf = nullptr;
request.buf_len = 0;
}
return request;
}
void handle_request()
{
if (!_head)
if (!init_queue())
return;
using Consumed_entry =
cxx::Pair<L4virtio::Svr::Virtqueue::Head_desc, l4_uint32_t>;
std::vector<Consumed_entry> consumed;
for (;;)
{
auto r = get_request();
if (_fail_next)
{
r.set_status(I2c_msg_err);
_fail_next = r.out_hdr.flags.fail_next();
}
else
{
bool ok;
l4_uint16_t i2c_addr = r.out_hdr.addr >> 1;
if (r.out_hdr.flags.m_rd())
ok = _req_handler->handle_read(i2c_addr, r.buf, r.buf_len);
else
ok = _req_handler->handle_write(i2c_addr, r.buf, r.buf_len);
if (ok)
{
r.set_status(I2c_msg_ok);
_fail_next = false;
}
else
{
r.set_status(I2c_msg_err);
_fail_next = r.out_hdr.flags.fail_next();
}
}
consumed.emplace_back(_head, r.write_size);
if (!init_queue())
break;
}
_q->finish(consumed.begin(), consumed.end(), _i2c);
_head = Virtqueue::Head_desc();
}
private:
L4virtio::Svr::Virtqueue *_q;
I2c_request_handler *_req_handler;
Virtio_i2c *_i2c;
L4virtio::Svr::Virtqueue::Head_desc _head;
Data_buffer _req;
bool _fail_next;
};
struct Features : public L4virtio::Svr::Dev_config::Features
{
Features() = default;
Features(l4_uint32_t raw) : L4virtio::Svr::Dev_config::Features(raw) {}
// This feature is mandatory. The driver is requested to abort communication
// if this is not offered.
CXX_BITFIELD_MEMBER(0, 0, zero_length_request, raw);
};
Virtio_i2c(I2c_request_handler *hndlr, L4Re::Util::Object_registry *registry)
: L4virtio::Svr::Device(&_dev_config),
_dev_config(L4VIRTIO_VENDOR_KK, L4VIRTIO_ID_I2C, Num_request_queues),
_req_handler(hndlr),
_host_irq(this),
_request_processor(&_q, hndlr, this)
{
init_mem_info(2);
reset_queue_config(0, queue_size);
setup_queue(&_q, 0, queue_size);
registry->register_irq_obj(&_host_irq);
Features hf(0);
hf.ring_indirect_desc() = true;
hf.zero_length_request() = true;
_dev_config.host_features(0) = hf.raw;
_dev_config.set_host_feature(L4VIRTIO_FEATURE_VERSION_1);
_dev_config.reset_hdr();
}
void notify_queue(L4virtio::Svr::Virtqueue *)
{
if (_q.no_notify_guest())
return;
_dev_config.add_irq_status(L4VIRTIO_IRQ_STATUS_VRING);
L4Re::chkipc(_notify_guest_irq->trigger(), "trigger guest irq");
}
void handle_queue()
{
_request_processor.handle_request();
}
void reset() override
{
}
bool check_queues() override
{
return true;
}
int reconfig_queue(unsigned idx) override
{
if (idx != 0)
return -L4_ERANGE;
setup_queue(&_q, 0, queue_size);
return L4_EOK;
}
void trigger_driver_config_irq() override
{
_dev_config.add_irq_status(L4VIRTIO_IRQ_STATUS_CONFIG);
_notify_guest_irq->trigger();
}
L4::Ipc_svr::Server_iface *server_iface() const override
{
return L4::Epiface::server_iface();
}
long op_set_status(L4virtio::Device::Rights r, unsigned status)
{
return L4virtio::Svr::Device::op_set_status(r, status);
}
long op_config_queue(L4virtio::Device::Rights r, unsigned queue)
{
return L4virtio::Svr::Device::op_config_queue(r, queue);
}
long op_device_config(L4virtio::Device::Rights r,
L4::Ipc::Cap<L4Re::Dataspace> &config_ds,
l4_addr_t &ds_offset)
{
return L4virtio::Svr::Device::op_device_config(r, config_ds, ds_offset);
}
L4::Cap<L4::Irq> device_notify_irq() const override
{
return L4::cap_cast<L4::Irq>(_host_irq.obj_cap());
}
void register_single_driver_irq() override
{
_notify_guest_irq = L4Re::chkcap
(server_iface()->template rcv_cap<L4::Irq>(0));
L4Re::chksys(server_iface()->realloc_rcv_cap(0));
}
private:
L4virtio::Svr::Dev_config_t<L4virtio::Svr::No_custom_data>_dev_config;
I2c_request_handler *_req_handler;
L4virtio::Svr::Virtqueue _q;
Host_irq _host_irq;
L4::Cap<L4::Irq> _notify_guest_irq;
Request_processor _request_processor;
};
} // namespace Svr
} // namespace L4virtio
L4Re::Util::Object_registry::register_irq_obj
L4::Cap< L4::Irq > register_irq_obj(L4::Epiface *o) override
Register a handler for an interrupt.
Definition object_registry:227
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
L4::Ipc_svr::Server_iface
Interface for server-loop related functions.
Definition ipc_epiface:37
L4virtio::Svr::Device_t< No_custom_data >::setup_queue
bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
Definition l4virtio:1047
L4virtio::Svr::Device_t< No_custom_data >::init_mem_info
void init_mem_info(unsigned num)
Definition l4virtio:1012
L4virtio::Svr::Device_t< No_custom_data >::reset_queue_config
void reset_queue_config(unsigned idx, unsigned num_max, bool inc_generation=false)
Definition l4virtio:1002
L4virtio::Svr::Driver_mem_region_t::local
T * local(Ptr< T > p) const
Get the local address for driver address p.
Definition l4virtio:616
L4virtio::Svr::Request_processor
Encapsulate the state for processing a VIRTIO request.
Definition virtio:473
L4virtio::Svr::Request_processor::next
bool next(DESC_MAN *dm, ARGS... args)
Switch to the next descriptor in a descriptor chain.
Definition virtio:570
L4virtio::Svr::Request_processor::start
void start(DESC_MAN *dm, Virtqueue *ring, Virtqueue::Head_desc const &request, ARGS... args)
Start processing a new request.
Definition virtio:501
L4virtio::Svr::Virtio_i2c::Request_processor::get_request
I2c_req get_request()
Linux prepares the I2C request in three data parts: 1st: out_hdr 2nd: buffer (optional) 3rd: in_hdr.
Definition virtio-i2c-device:172
L4virtio::Svr::Virtio_i2c::trigger_driver_config_irq
void trigger_driver_config_irq() override
callback for triggering configuration change notification IRQ
Definition virtio-i2c-device:330
L4virtio::Svr::Virtio_i2c::register_single_driver_irq
void register_single_driver_irq() override
callback for registering a single guest IRQ for all queues (old-style)
Definition virtio-i2c-device:363
L4virtio::Svr::Virtio_i2c::reset
void reset() override
reset callback, called for doing a device reset
Definition virtio-i2c-device:311
L4virtio::Svr::Virtio_i2c::check_queues
bool check_queues() override
callback for checking if the queues at DRIVER_OK transition
Definition virtio-i2c-device:315
L4virtio::Svr::Virtio_i2c::device_notify_irq
L4::Cap< L4::Irq > device_notify_irq() const override
callback to gather the device notification IRQ (old-style)
Definition virtio-i2c-device:358
L4virtio::Svr::Virtio_i2c::reconfig_queue
int reconfig_queue(unsigned idx) override
callback for client queue-config request
Definition virtio-i2c-device:320
L4virtio::Svr::Virtqueue
Virtqueue implementation for the device.
Definition virtio:88
L4virtio::Virtqueue::Desc
Descriptor in the descriptor table.
Definition virtqueue:87
L4virtio::Virtqueue::Desc::len
l4_uint32_t len
Length of described buffer.
Definition virtqueue:109
L4virtio::Virtqueue::Desc::addr
Ptr< void > addr
Address stored in descriptor.
Definition virtqueue:108
error_helper
Error helper.
l4_addr_t
unsigned long l4_addr_t
Address type.
Definition l4int.h:34
l4_uint8_t
unsigned char l4_uint8_t
Unsigned 8bit value.
Definition l4int.h:25
l4_uint32_t
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
l4_uint16_t
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition l4int.h:27
L4_ERANGE
@ L4_ERANGE
Range error.
Definition err.h:48
L4_EOK
@ L4_EOK
Ok.
Definition err.h:32
L4_UNLIKELY
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition compiler.h:284
L4VIRTIO_FEATURE_VERSION_1
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
Definition virtio.h:101
L4VIRTIO_ID_I2C
@ L4VIRTIO_ID_I2C
I2C device.
Definition virtio.h:80
L4VIRTIO_IRQ_STATUS_VRING
@ L4VIRTIO_IRQ_STATUS_VRING
VRING IRQ pending flag.
Definition virtio.h:112
L4VIRTIO_IRQ_STATUS_CONFIG
@ L4VIRTIO_IRQ_STATUS_CONFIG
CONFIG IRQ pending flag.
Definition virtio.h:113
L4Re::chksys
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Definition error_helper:72
L4Re::chkcap
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Definition error_helper:149
L4Re::chkipc
l4_msgtag_t chkipc(l4_msgtag_t tag, char const *extra="", l4_utcb_t *utcb=l4_utcb())
Test a message tag for IPC errors.
Definition error_helper:180
L4::cap_cast
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
Definition capability.h:416
L4virtio
L4-VIRTIO Transport C++ API.
Definition l4virtio:26
pair
Pair implementation.
L4::Epiface_t
Epiface implementation for Kobject-based interface implementations.
Definition ipc_epiface:504
L4::Epiface::server_iface
Server_iface * server_iface() const
Get pointer to server interface at which the object is currently registered.
Definition ipc_epiface:213
L4::Irqep_t
Epiface implementation for interrupt handlers.
Definition ipc_epiface:283
L4virtio::Svr::Data_buffer
Abstract data buffer.
Definition virtio:307
L4virtio::Svr::Data_buffer::left
l4_uint32_t left
Bytes left in buffer.
Definition virtio:309
L4virtio::Svr::Data_buffer::pos
char * pos
Current buffer position.
Definition virtio:308
L4virtio::Svr::Dev_features::raw
l4_uint32_t raw
The raw value of the features bitmap.
Definition virtio:68
cxx::Pair
Pair of two values.
Definition pair:28