Blame opae-libs/include/opae/cxx/core/handle.h

Packit 534379
// Copyright(c) 2018, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
#pragma once
Packit 534379
#include <memory>
Packit 534379
#include <vector>
Packit 534379
Packit 534379
#include <opae/cxx/core/token.h>
Packit 534379
#include <opae/enum.h>
Packit 534379
#include <opae/types.h>
Packit 534379
Packit 534379
namespace opae {
Packit 534379
namespace fpga {
Packit 534379
namespace types {
Packit 534379
Packit 534379
/** An allocated accelerator resource
Packit 534379
 *
Packit 534379
 * Represents an accelerator resource that has
Packit 534379
 * been allocated by OPAE. Depending on the type
Packit 534379
 * of resource, its register space may be
Packit 534379
 * read/written using a handle object.
Packit 534379
 */
Packit 534379
class handle {
Packit 534379
 public:
Packit 534379
  typedef std::shared_ptr<handle> ptr_t;
Packit 534379
Packit 534379
  handle(const handle &) = delete;
Packit 534379
  handle &operator=(const handle &) = delete;
Packit 534379
Packit 534379
  virtual ~handle();
Packit 534379
Packit 534379
  /** Retrieve the underlying OPAE handle.
Packit 534379
   */
Packit 534379
  fpga_handle c_type() const { return handle_; }
Packit 534379
Packit 534379
  /** Retrieve the underlying OPAE handle.
Packit 534379
   */
Packit 534379
  operator fpga_handle() const { return handle_; }
Packit 534379
Packit 534379
  /**
Packit 534379
   * @brief Load a bitstream into an FPGA slot
Packit 534379
   *
Packit 534379
   * @param slot The slot number to program
Packit 534379
   * @param bitstream The bitstream binary data
Packit 534379
   * @param size The size of the bitstream
Packit 534379
   * @param flags Flags that control behavior of reconfiguration.
Packit 534379
   *              Value of 0 indicates no flags. FPGA_RECONF_FORCE
Packit 534379
   *              indicates that the bitstream is programmed into
Packit 534379
   *              the slot without checking if the resource is
Packit 534379
   *              currently in use.
Packit 534379
   *
Packit 534379
   * @throws invalid_param if the handle is not an FPGA device handle
Packit 534379
   *         or if the other parameters are not valid.
Packit 534379
   * @throws exception if an internal error is encountered.
Packit 534379
   * @throws busy if the accelerator for the given slot is in use.
Packit 534379
   * @throws reconf_error if errors are reported by the driver
Packit 534379
   *         (CRC or protocol errors).
Packit 534379
   */
Packit 534379
  void reconfigure(uint32_t slot, const uint8_t *bitstream, size_t size,
Packit 534379
                   int flags);
Packit 534379
Packit 534379
  /**
Packit 534379
   * @brief Read 32 bits from a CSR belonging to a resource associated
Packit 534379
   * with a handle.
Packit 534379
   *
Packit 534379
   * @param[in] offset The register offset
Packit 534379
   * @param[in] csr_space The CSR space to read from. Default is 0.
Packit 534379
   *
Packit 534379
   * @return The 32-bit value read from the CSR
Packit 534379
   */
Packit 534379
  uint32_t read_csr32(uint64_t offset, uint32_t csr_space = 0) const;
Packit 534379
Packit 534379
  /**
Packit 534379
   * @brief Write 32 bit to a CSR belonging to a resource associated
Packit 534379
   * with a handle.
Packit 534379
   *
Packit 534379
   * @param[in] offset The register offset.
Packit 534379
   * @param[in] value The 32-bit value to write to the register.
Packit 534379
   * @param[in] csr_space The CSR space to read from. Default is 0.
Packit 534379
   *
Packit 534379
   */
Packit 534379
  void write_csr32(uint64_t offset, uint32_t value, uint32_t csr_space = 0);
Packit 534379
Packit 534379
  /**
Packit 534379
   * @brief Read 64 bits from a CSR belonging to a resource associated
Packit 534379
   * with a handle.
Packit 534379
   *
Packit 534379
   * @param[in] offset The register offset
Packit 534379
   * @param[in] csr_space The CSR space to read from. Default is 0.
Packit 534379
   *
Packit 534379
   * @return The 64-bit value read from the CSR
Packit 534379
   */
Packit 534379
  uint64_t read_csr64(uint64_t offset, uint32_t csr_space = 0) const;
Packit 534379
Packit 534379
  /**
Packit 534379
   * @brief Write 64 bits to a CSR belonging to a resource associated
Packit 534379
   * with a handle.
Packit 534379
   *
Packit 534379
   * @param[in] offset The register offset.
Packit 534379
   * @param[in] value The 64-bit value to write to the register.
Packit 534379
   * @param[in] csr_space The CSR space to read from. Default is 0.
Packit 534379
   *
Packit 534379
   */
Packit 534379
  void write_csr64(uint64_t offset, uint64_t value, uint32_t csr_space = 0);
Packit 534379
Packit 534379
  /**
Packit 534379
   * @brief Write 512 bits to a CSR belonging to a resource associated
Packit 534379
   * with a handle.
Packit 534379
   *
Packit 534379
   * @param[in] offset The register offset.
Packit 534379
   * @param[in] value Pointer to the 512-bit value to write to the register.
Packit 534379
   * @param[in] csr_space The CSR space to read from. Default is 0.
Packit 534379
   *
Packit 534379
   */
Packit 534379
  void write_csr512(uint64_t offset, const void *value, uint32_t csr_space = 0);
Packit 534379
Packit 534379
  /** Retrieve a pointer to the MMIO region.
Packit 534379
   * @param[in] offset The byte offset to add to MMIO base.
Packit 534379
   * @param[in] csr_space The desired CSR space. Default is 0.
Packit 534379
   * @return MMIO base + offset
Packit 534379
   */
Packit 534379
  uint8_t *mmio_ptr(uint64_t offset, uint32_t csr_space = 0) const;
Packit 534379
Packit 534379
  /** Open an accelerator resource, given a raw fpga_token
Packit 534379
   *
Packit 534379
   * @param[in] token A token describing the accelerator
Packit 534379
   * resource to be allocated.
Packit 534379
   *
Packit 534379
   * @param[in] flags The flags parameter to fpgaOpen().
Packit 534379
   *
Packit 534379
   * @return pointer to the mmio base + offset for the given
Packit 534379
   * csr space
Packit 534379
   *
Packit 534379
   */
Packit 534379
  static handle::ptr_t open(fpga_token token, int flags);
Packit 534379
Packit 534379
  /** Open an accelerator resource, given a token object
Packit 534379
   *
Packit 534379
   * @param[in] token A token object describing the
Packit 534379
   * accelerator resource to be allocated.
Packit 534379
   *
Packit 534379
   * @param[in] flags The flags parameter to fpgaOpen().
Packit 534379
   *
Packit 534379
   * @return shared ptr to a handle object
Packit 534379
   */
Packit 534379
  static handle::ptr_t open(token::ptr_t token, int flags);
Packit 534379
Packit 534379
  /** Reset the accelerator identified by this handle
Packit 534379
   */
Packit 534379
  virtual void reset();
Packit 534379
Packit 534379
  /** Close an accelerator resource (if opened)
Packit 534379
   *
Packit 534379
   * @return fpga_result indication the result of closing the
Packit 534379
   * handle or FPGA_EXCEPTION if handle is not opened
Packit 534379
   *
Packit 534379
   * @note This is available for explicitly closing a handle.
Packit 534379
   * The destructor for handle will call close.
Packit 534379
   */
Packit 534379
  fpga_result close();
Packit 534379
Packit 534379
 private:
Packit 534379
  handle(fpga_handle h);
Packit 534379
Packit 534379
  fpga_handle handle_;
Packit 534379
  fpga_token token_;
Packit 534379
};
Packit 534379
Packit 534379
}  // end of namespace types
Packit 534379
}  // end of namespace fpga
Packit 534379
}  // end of namespace opae