1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <ddk/protocol/platform-bus.h>
8#include <fbl/array.h>
9#include <fbl/vector.h>
10
11#include "proxy-protocol.h"
12
13namespace platform_bus {
14
15class DeviceResources {
16public:
17    DeviceResources(uint32_t index)
18        : index_(index) {}
19
20    // Initializes this instance from the resources in the provided pbus_dev_t.
21    // next_index keeps track of the index to be assigned to the children while inflating the tree.
22    zx_status_t Init(const pbus_dev_t* pdev, uint32_t* next_index);
23    // Variant of Init() used for initializing the root of the tree.
24    zx_status_t Init(const pbus_dev_t* pdev);
25
26    // Returns the total number of devices, including this device and all children.
27    size_t DeviceCount() const;
28    // Builds a flattened list of all DeviceResources.
29    void BuildDeviceIndex(fbl::Vector<const DeviceResources*>* index) const;
30
31    // Returns device ID for this device.
32    inline uint32_t index() const { return index_; }
33
34    // Returns the index of the ith child of this device.
35    inline uint32_t child_index(uint32_t i) const { return children_[i].index_; }
36
37    // Platform bus resources copied from the pbus_dev_t struct from the board driver.
38    inline const pbus_mmio_t& mmio(size_t i) const { return mmios_[i]; }
39    inline const pbus_irq_t& irq(size_t i) const { return irqs_[i]; }
40    inline const pbus_gpio_t& gpio(size_t i) const { return gpios_[i]; }
41    inline const pbus_i2c_channel_t& i2c_channel(size_t i) const { return i2c_channels_[i]; }
42    inline const pbus_clk_t& clk(size_t i) const { return clks_[i]; }
43    inline const pbus_bti_t& bti(size_t i) const { return btis_[i]; }
44    inline const pbus_metadata_t& metadata(size_t i) const { return metadata_[i]; }
45    inline const pbus_boot_metadata_t& boot_metadata(size_t i) const { return boot_metadata_[i]; }
46    inline const uint32_t* protocols() const { return protocols_.begin(); }
47
48    // Counts for the above resource lists.
49    inline size_t mmio_count() const { return mmios_.size(); }
50    inline size_t irq_count() const { return irqs_.size(); }
51    inline size_t gpio_count() const { return gpios_.size(); }
52    inline size_t i2c_channel_count() const { return i2c_channels_.size(); }
53    inline size_t clk_count() const { return clks_.size(); }
54    inline size_t bti_count() const { return btis_.size(); }
55    inline size_t metadata_count() const { return metadata_.size(); }
56    inline size_t boot_metadata_count() const { return boot_metadata_.size(); }
57    inline size_t child_count() const { return children_.size(); }
58    inline size_t protocol_count() const { return protocols_.size(); }
59
60private:
61    // Index of this DeviceResources instance in PlatformDevice::device_index_.
62    const uint32_t index_;
63
64    // Platform bus resources copied from the pbus_dev_t struct from the board driver.
65    fbl::Array<pbus_mmio_t> mmios_;
66    fbl::Array<pbus_irq_t> irqs_;
67    fbl::Array<pbus_gpio_t> gpios_;
68    fbl::Array<pbus_i2c_channel_t> i2c_channels_;
69    fbl::Array<pbus_clk_t> clks_;
70    fbl::Array<pbus_bti_t> btis_;
71    fbl::Array<pbus_metadata_t> metadata_;
72    fbl::Array<pbus_boot_metadata_t> boot_metadata_;
73    fbl::Array<uint32_t> protocols_;
74
75    // Resources for children of this device.
76    fbl::Vector<DeviceResources> children_;
77};
78
79} // namespace platform_bus
80