2020-05-14 13:35:40 +00:00
|
|
|
/*
|
|
|
|
* \brief Connection to Platform service
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2020-05-13
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
|
|
|
*/
|
|
|
|
|
2021-12-22 11:04:09 +00:00
|
|
|
#ifndef _INCLUDE__PLATFORM_SESSION__CONNECTION_H_
|
|
|
|
#define _INCLUDE__PLATFORM_SESSION__CONNECTION_H_
|
2020-05-14 13:35:40 +00:00
|
|
|
|
2020-06-30 22:34:22 +00:00
|
|
|
#include <base/attached_dataspace.h>
|
2020-05-14 13:35:40 +00:00
|
|
|
#include <base/connection.h>
|
|
|
|
#include <base/env.h>
|
2022-05-02 09:37:04 +00:00
|
|
|
#include <base/signal.h>
|
2020-05-14 13:35:40 +00:00
|
|
|
#include <platform_session/client.h>
|
|
|
|
#include <rom_session/client.h>
|
|
|
|
#include <util/xml_node.h>
|
|
|
|
|
2021-04-14 15:19:37 +00:00
|
|
|
namespace Platform {
|
|
|
|
|
|
|
|
struct Device;
|
2022-02-02 13:59:21 +00:00
|
|
|
struct Dma_buffer;
|
2021-04-14 15:19:37 +00:00
|
|
|
struct Connection;
|
|
|
|
}
|
2020-05-14 13:35:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Platform::Connection : public Genode::Connection<Session>,
|
|
|
|
public Client
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
/* 'Device' and 'Dma_buffer' access the '_env' member */
|
2022-02-02 13:59:21 +00:00
|
|
|
friend class Device;
|
|
|
|
friend class Dma_buffer;
|
2021-04-14 15:19:37 +00:00
|
|
|
|
2022-09-02 11:35:13 +00:00
|
|
|
Env & _env;
|
|
|
|
Rom_session_client _rom { devices_rom() };
|
|
|
|
Constructible<Attached_dataspace> _ds {};
|
|
|
|
Io_signal_handler<Connection> _handler { _env.ep(), *this,
|
|
|
|
&Connection::_handle_io };
|
2020-05-14 13:35:40 +00:00
|
|
|
|
|
|
|
void _try_attach()
|
|
|
|
{
|
|
|
|
_ds.destruct();
|
2022-05-02 09:37:04 +00:00
|
|
|
try { _ds.construct(_env.rm(), _rom.dataspace()); }
|
2020-05-14 13:35:40 +00:00
|
|
|
catch (Attached_dataspace::Invalid_dataspace) {
|
|
|
|
warning("Invalid devices rom dataspace returned!");}
|
|
|
|
}
|
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
void _handle_io() {}
|
|
|
|
|
|
|
|
template <typename FN>
|
|
|
|
Capability<Device_interface> _wait_for_device(FN const & fn)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
/* repeatedly check for availability of device */
|
|
|
|
Capability<Device_interface> cap = fn();
|
2022-09-02 11:35:13 +00:00
|
|
|
if (cap.valid())
|
2022-05-02 09:37:04 +00:00
|
|
|
return cap;
|
|
|
|
|
|
|
|
_env.ep().wait_and_dispatch_one_io_signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:35:40 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
Connection(Env &env)
|
2021-04-14 15:19:37 +00:00
|
|
|
:
|
2023-05-05 06:18:36 +00:00
|
|
|
Genode::Connection<Session>(env, Label(), Ram_quota { 56*1024 }, Args()),
|
2021-04-14 15:19:37 +00:00
|
|
|
Client(cap()),
|
2022-05-02 09:37:04 +00:00
|
|
|
_env(env)
|
2021-04-14 15:19:37 +00:00
|
|
|
{
|
|
|
|
_try_attach();
|
2022-09-02 11:35:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initially register dummy handler, to be able to receive signals
|
|
|
|
* if _wait_for_device probes for a valid devices rom
|
|
|
|
*/
|
|
|
|
sigh(_handler);
|
2021-04-14 15:19:37 +00:00
|
|
|
}
|
2020-05-14 13:35:40 +00:00
|
|
|
|
2020-07-17 16:28:06 +00:00
|
|
|
void update()
|
|
|
|
{
|
2021-04-14 15:19:37 +00:00
|
|
|
if (_ds.constructed() && _rom.update() == true)
|
|
|
|
return;
|
|
|
|
|
2020-07-17 16:28:06 +00:00
|
|
|
_try_attach();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigh(Signal_context_capability sigh) { _rom.sigh(sigh); }
|
|
|
|
|
2021-04-14 15:19:37 +00:00
|
|
|
Capability<Device_interface> acquire_device(Device_name const &name) override
|
2020-05-14 13:35:40 +00:00
|
|
|
{
|
2022-05-02 09:37:04 +00:00
|
|
|
return _wait_for_device([&] () {
|
|
|
|
return retry_with_upgrade(Ram_quota{6*1024}, Cap_quota{6}, [&] () {
|
|
|
|
return Client::acquire_device(name); });
|
|
|
|
});
|
2020-05-14 13:35:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 13:08:21 +00:00
|
|
|
Capability<Device_interface> acquire_device()
|
|
|
|
{
|
2022-05-02 09:37:04 +00:00
|
|
|
return _wait_for_device([&] () {
|
|
|
|
return retry_with_upgrade(Ram_quota{6*1024}, Cap_quota{6}, [&] () {
|
|
|
|
return Client::acquire_single_device(); });
|
|
|
|
});
|
2021-04-16 13:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:44:11 +00:00
|
|
|
Ram_dataspace_capability alloc_dma_buffer(size_t size, Cache cache) override
|
2020-05-14 13:35:40 +00:00
|
|
|
{
|
2023-03-22 13:58:51 +00:00
|
|
|
return retry_with_upgrade(Ram_quota{max((size_t)4096, size)}, Cap_quota{2}, [&] () {
|
2021-04-08 15:44:11 +00:00
|
|
|
return Client::alloc_dma_buffer(size, cache); });
|
2020-05-14 13:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FN>
|
|
|
|
void with_xml(FN const & fn)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (_ds.constructed() && _ds->local_addr<void const>()) {
|
|
|
|
Xml_node xml(_ds->local_addr<char>(), _ds->size());
|
|
|
|
fn(xml);
|
|
|
|
}
|
|
|
|
} catch (Xml_node::Invalid_syntax) {
|
|
|
|
warning("Devices rom has invalid XML syntax"); }
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:19:37 +00:00
|
|
|
Capability<Device_interface> device_by_type(char const * type)
|
2020-05-14 13:35:40 +00:00
|
|
|
{
|
2022-05-02 09:37:04 +00:00
|
|
|
return _wait_for_device([&] () {
|
|
|
|
|
|
|
|
using String = Genode::String<64>;
|
2020-05-14 13:35:40 +00:00
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
Capability<Device_interface> cap;
|
2020-05-14 13:35:40 +00:00
|
|
|
|
2022-11-16 11:51:00 +00:00
|
|
|
update();
|
2022-05-02 09:37:04 +00:00
|
|
|
with_xml([&] (Xml_node & xml) {
|
|
|
|
xml.for_each_sub_node("device", [&] (Xml_node node) {
|
2021-04-14 15:19:37 +00:00
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
/* already found a device? */
|
|
|
|
if (cap.valid())
|
|
|
|
return;
|
2020-05-14 13:35:40 +00:00
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
if (node.attribute_value("type", String()) != type)
|
|
|
|
return;
|
2021-04-15 12:30:15 +00:00
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
Device_name name = node.attribute_value("name", Device_name());
|
|
|
|
cap = acquire_device(name);
|
|
|
|
});
|
2020-05-14 13:35:40 +00:00
|
|
|
});
|
|
|
|
|
2022-05-02 09:37:04 +00:00
|
|
|
return cap;
|
|
|
|
});
|
2020-05-14 13:35:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-22 11:04:09 +00:00
|
|
|
#endif /* _INCLUDE__PLATFORM_SESSION__CONNECTION_H_ */
|