blob: 23fc4ab356710319577f8a751f5f35552af496a5 [file] [log] [blame]
/*
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
extern "C"
{
#include <pciaccess.h>
} // extern "C"
#include <cstdint>
namespace host_tool
{
/**
* @class PciAccess
* @brief Overridable interface to libpciacess for unit testing
*/
class PciAccess
{
public:
virtual struct pci_device_iterator* pci_id_match_iterator_create(
const struct pci_id_match* match) const = 0;
virtual void
pci_iterator_destroy(struct pci_device_iterator* iter) const = 0;
virtual void pci_device_enable(struct pci_device* dev) const = 0;
virtual struct pci_device*
pci_device_next(struct pci_device_iterator* iter) const = 0;
virtual int pci_device_probe(struct pci_device* dev) const = 0;
virtual int pci_device_cfg_read_u8(struct pci_device* dev,
std::uint8_t* data,
pciaddr_t offset) const = 0;
virtual int pci_device_cfg_write_u8(struct pci_device* dev,
std::uint8_t data,
pciaddr_t offset) const = 0;
virtual int pci_device_map_range(struct pci_device* dev, pciaddr_t base,
pciaddr_t size, unsigned map_flags,
void** addr) const = 0;
virtual int pci_device_unmap_range(struct pci_device* dev, void* memory,
pciaddr_t size) const = 0;
virtual ~PciAccess() = default;
};
/**
* @class PciAccessImpl
* @brief libpciaccess concrete implementation
* @details Passes through all calls to the underlying library.
*/
class PciAccessImpl : public PciAccess
{
public:
struct pci_device_iterator* pci_id_match_iterator_create(
const struct pci_id_match* match) const override;
void pci_iterator_destroy(struct pci_device_iterator* iter) const override;
void pci_device_enable(struct pci_device* dev) const override;
struct pci_device*
pci_device_next(struct pci_device_iterator* iter) const override;
int pci_device_probe(struct pci_device* dev) const override;
int pci_device_cfg_read_u8(struct pci_device* dev, std::uint8_t* data,
pciaddr_t offset) const override;
int pci_device_cfg_write_u8(struct pci_device* dev, std::uint8_t data,
pciaddr_t offset) const override;
int pci_device_map_range(struct pci_device* dev, pciaddr_t base,
pciaddr_t size, unsigned map_flags,
void** addr) const override;
int pci_device_unmap_range(struct pci_device* dev, void* memory,
pciaddr_t size) const override;
static PciAccessImpl& getInstance()
{
static PciAccessImpl instance;
return instance;
}
private:
PciAccessImpl();
virtual ~PciAccessImpl();
};
} // namespace host_tool