Benjamin Fair | 20a1809 | 2020-06-08 11:12:21 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | extern "C" |
| 20 | { |
| 21 | #include <pciaccess.h> |
| 22 | } // extern "C" |
| 23 | |
Benjamin Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 24 | #include <cstdint> |
| 25 | |
Benjamin Fair | 20a1809 | 2020-06-08 11:12:21 -0700 | [diff] [blame] | 26 | namespace host_tool |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * @class PciAccess |
| 31 | * @brief Overridable interface to libpciacess for unit testing |
| 32 | */ |
| 33 | class 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 Veeracholan | 55b1a71 | 2021-02-18 12:06:37 -0800 | [diff] [blame] | 40 | virtual void pci_device_enable(struct pci_device* dev) const = 0; |
Benjamin Fair | 20a1809 | 2020-06-08 11:12:21 -0700 | [diff] [blame] | 41 | 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 Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 44 | 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 Fair | 20a1809 | 2020-06-08 11:12:21 -0700 | [diff] [blame] | 50 | 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 | */ |
| 64 | class 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 Veeracholan | 55b1a71 | 2021-02-18 12:06:37 -0800 | [diff] [blame] | 70 | void pci_device_enable(struct pci_device* dev) const override; |
Benjamin Fair | 20a1809 | 2020-06-08 11:12:21 -0700 | [diff] [blame] | 71 | 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 Fair | c1a30c0 | 2020-06-09 11:46:34 -0700 | [diff] [blame] | 74 | 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 Fair | 20a1809 | 2020-06-08 11:12:21 -0700 | [diff] [blame] | 78 | 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 |