blob: 23fc4ab356710319577f8a751f5f35552af496a5 [file] [log] [blame]
Benjamin Fair20a18092020-06-08 11:12:21 -07001/*
2 * Copyright 2020 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19extern "C"
20{
21#include <pciaccess.h>
22} // extern "C"
23
Benjamin Fairc1a30c02020-06-09 11:46:34 -070024#include <cstdint>
25
Benjamin Fair20a18092020-06-08 11:12:21 -070026namespace host_tool
27{
28
29/**
30 * @class PciAccess
31 * @brief Overridable interface to libpciacess for unit testing
32 */
33class PciAccess
34{
35 public:
36 virtual struct pci_device_iterator* pci_id_match_iterator_create(
37 const struct pci_id_match* match) const = 0;
38 virtual void
39 pci_iterator_destroy(struct pci_device_iterator* iter) const = 0;
Vivekanand Veeracholan55b1a712021-02-18 12:06:37 -080040 virtual void pci_device_enable(struct pci_device* dev) const = 0;
Benjamin Fair20a18092020-06-08 11:12:21 -070041 virtual struct pci_device*
42 pci_device_next(struct pci_device_iterator* iter) const = 0;
43 virtual int pci_device_probe(struct pci_device* dev) const = 0;
Benjamin Fairc1a30c02020-06-09 11:46:34 -070044 virtual int pci_device_cfg_read_u8(struct pci_device* dev,
45 std::uint8_t* data,
46 pciaddr_t offset) const = 0;
47 virtual int pci_device_cfg_write_u8(struct pci_device* dev,
48 std::uint8_t data,
49 pciaddr_t offset) const = 0;
Benjamin Fair20a18092020-06-08 11:12:21 -070050 virtual int pci_device_map_range(struct pci_device* dev, pciaddr_t base,
51 pciaddr_t size, unsigned map_flags,
52 void** addr) const = 0;
53 virtual int pci_device_unmap_range(struct pci_device* dev, void* memory,
54 pciaddr_t size) const = 0;
55
56 virtual ~PciAccess() = default;
57};
58
59/**
60 * @class PciAccessImpl
61 * @brief libpciaccess concrete implementation
62 * @details Passes through all calls to the underlying library.
63 */
64class PciAccessImpl : public PciAccess
65{
66 public:
67 struct pci_device_iterator* pci_id_match_iterator_create(
68 const struct pci_id_match* match) const override;
69 void pci_iterator_destroy(struct pci_device_iterator* iter) const override;
Vivekanand Veeracholan55b1a712021-02-18 12:06:37 -080070 void pci_device_enable(struct pci_device* dev) const override;
Benjamin Fair20a18092020-06-08 11:12:21 -070071 struct pci_device*
72 pci_device_next(struct pci_device_iterator* iter) const override;
73 int pci_device_probe(struct pci_device* dev) const override;
Benjamin Fairc1a30c02020-06-09 11:46:34 -070074 int pci_device_cfg_read_u8(struct pci_device* dev, std::uint8_t* data,
75 pciaddr_t offset) const override;
76 int pci_device_cfg_write_u8(struct pci_device* dev, std::uint8_t data,
77 pciaddr_t offset) const override;
Benjamin Fair20a18092020-06-08 11:12:21 -070078 int pci_device_map_range(struct pci_device* dev, pciaddr_t base,
79 pciaddr_t size, unsigned map_flags,
80 void** addr) const override;
81 int pci_device_unmap_range(struct pci_device* dev, void* memory,
82 pciaddr_t size) const override;
83
84 static PciAccessImpl& getInstance()
85 {
86 static PciAccessImpl instance;
87 return instance;
88 }
89
90 private:
91 PciAccessImpl();
92 virtual ~PciAccessImpl();
93};
94
95} // namespace host_tool