Signed-off-by: Fotis Xenakis <fo...@windowslive.com>
---
 drivers/virtio-device.hh     |  2 ++
 drivers/virtio-mmio.hh       |  1 +
 drivers/virtio-pci-device.cc | 24 ++++++++++++++++++++++++
 drivers/virtio-pci-device.hh |  4 ++++
 4 files changed, 31 insertions(+)

diff --git a/drivers/virtio-device.hh b/drivers/virtio-device.hh
index 1327a159..732f9b18 100644
--- a/drivers/virtio-device.hh
+++ b/drivers/virtio-device.hh
@@ -76,6 +76,8 @@ public:
     virtual u8 read_config(u32 offset) = 0;
     virtual void dump_config() = 0;
 
+    virtual bool get_shm(u8 id, mmioaddr_t &addr, u64 &length) = 0;
+
     virtual bool is_modern() = 0;
     virtual size_t get_vring_alignment() = 0;
 };
diff --git a/drivers/virtio-mmio.hh b/drivers/virtio-mmio.hh
index 024190ac..0cbf7000 100644
--- a/drivers/virtio-mmio.hh
+++ b/drivers/virtio-mmio.hh
@@ -136,6 +136,7 @@ public:
 
     bool parse_config();
 
+    virtual bool get_shm(u8 id, mmioaddr_t &addr, u64 &length) { /* TODO */ 
return false; }
 private:
     mmio_device_info _dev_info;
     //u64 _id;
diff --git a/drivers/virtio-pci-device.cc b/drivers/virtio-pci-device.cc
index a88e3d2b..d484f3df 100644
--- a/drivers/virtio-pci-device.cc
+++ b/drivers/virtio-pci-device.cc
@@ -253,6 +253,30 @@ u8 virtio_modern_pci_device::read_and_ack_isr()
     return _isr_cfg->virtio_conf_readb(0);
 }
 
+// Stores the address and length of the shared memory region identified by @id
+// in @addr and @length respectively. Returns false and doesn't modify @addr 
and
+// @length if no region with a matching id is found.
+bool virtio_modern_pci_device::get_shm(u8 id, mmioaddr_t &addr, u64 &length)
+{
+    auto cap = std::find_if(_shm_cfgs.cbegin(), _shm_cfgs.cend(),
+        [id, this] (const std::unique_ptr<virtio_capability>& cap) {
+            u8 cap_id = _dev->pci_readb(cap->get_cfg_offset() +
+                offsetof(virtio_pci_cap, id));
+            return cap_id == id;
+        });
+    if (cap == _shm_cfgs.cend()) {
+        return false;
+    }
+
+    auto bar = cap->get()->get_bar();
+    if (!bar->is_mmio()) {
+        return false;
+    }
+    addr = bar->get_mmio();
+    length = bar->get_size();
+    return true;
+}
+
 bool virtio_modern_pci_device::parse_pci_config()
 {
     // Check ABI version
diff --git a/drivers/virtio-pci-device.hh b/drivers/virtio-pci-device.hh
index a2c70e28..851b562a 100644
--- a/drivers/virtio-pci-device.hh
+++ b/drivers/virtio-pci-device.hh
@@ -116,6 +116,8 @@ public:
     virtual u8 read_and_ack_isr();
 
     virtual bool is_modern() { return false; }
+
+    virtual bool get_shm(u8 id, mmioaddr_t &addr, u64 &length) { return false; 
}
 protected:
     virtual bool parse_pci_config();
 
@@ -243,6 +245,7 @@ public:
             _bar->writel(_bar_offset + offset, val);
         };
         u32 get_cfg_offset() { return _cfg_offset; }
+        pci::bar* get_bar() { return _bar; }
 
         void print(const char *prefix) {
             virtio_d("%s bar=%d, offset=%x, size=%x", prefix, _bar_no, 
_bar_offset, _length);
@@ -284,6 +287,7 @@ public:
 
     virtual bool is_modern() { return true; };
 
+    virtual bool get_shm(u8 id, mmioaddr_t &addr, u64 &length);
 protected:
     virtual bool parse_pci_config();
 private:
-- 
2.25.1

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/VI1PR03MB4383690324FC94DCDD3B4F53A6F90%40VI1PR03MB4383.eurprd03.prod.outlook.com.

Reply via email to