driver.h 2.9 KB
Newer Older
Z
zhangyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <map>

#include "common/log.h"

namespace paddle_mobile {
namespace fpga {

#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))

#define FPGA_REG_PHY_ADDR 0xa0000000
#define FPGA_REG_SIZE 0x1000
#define FPGA_MEM_PHY_ADDR 0x20000000
#define FPGA_MEM_SIZE 0x20000000

#define CPU_FREQ 1000000000

#define FPGA_PAGE_SIZE (16UL * 1024UL)

// PE related macros
const int MAX_NUM_PES = 6;
const size_t MAX_TYPE_NAME_LENTH = 8;

const int PE_IDX_CONV = 0;
const int PE_IDX_POOLING = 1;
const int PE_IDX_EW = 2;
const int PE_IDX_BYPASS = 3;

enum pe_status { IDLE = 0, BUSY = 1 };

struct fpga_pe {
  char type_name[MAX_TYPE_NAME_LENTH + 1];
  struct pe_data_s *outer;
  pe_status status;  // 0=idle 1=busy -1=fail
  uint64_t interrupt_cnt;
};

struct pe_data_s {
  pthread_mutex_t mutex;
  struct fpga_pe pe_conv;
  struct fpga_pe pe_pooling;
  struct fpga_pe pe_ew;
  struct fpga_pe pe_bypass;

  struct fpga_pe *pes[MAX_NUM_PES];
  int pe_num;
};

struct fpga_memory {
  pthread_mutex_t mutex;
  uint64_t *bitmap;
  unsigned int *nr;
  unsigned int page_num;
  unsigned int page_num_long;
  uint64_t mem_start;
  uint64_t mem_end;
};

struct FPGA_INFO {
  uint64_t FpgaRegPhyAddr;
  uint64_t FpgaMemPhyAddr;
  pthread_t poll_pid;
  void *FpgaRegVirAddr;
  struct pe_data_s *pe_data;

  std::map<void *, size_t> fpga_addr2size_map;
  std::map<void *, uint64_t> fpga_vaddr2paddr_map;
  const char *drvdevice_path;
  const char *memdevice_path;
  struct fpga_memory *memory_info;
  int fd_drv;
  int fd_mem;
};

extern struct FPGA_INFO g_fpgainfo;

inline uint64_t reg_readq(uint32_t offset) {
  // DLOG << "offset : " << offset;
  uint64_t value =
      *(uint64_t *)((uint8_t *)g_fpgainfo.FpgaRegVirAddr + offset);  // NOLINT

  return value;
}

inline void reg_writeq(uint64_t value, uint32_t offset) {
  // DLOG << "offset : " << offset << ", value : " << value;
  *(uint64_t *)((uint8_t *)g_fpgainfo.FpgaRegVirAddr + offset) =  // NOLINT
      value;
}

int open_device_driver();
int close_device_driver();
void *fpga_malloc_driver(size_t size);
void fpga_free_driver(void *ptr);
/*pe*/

uint64_t vaddr_to_paddr(void *address);
int fpga_regpoll(uint64_t reg, uint64_t val, int time);

}  // namespace fpga
}  // namespace paddle_mobile