driver.h 3.4 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
/* 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 {
Z
zhangyang 已提交
27
namespace driver {
Z
zhangyang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#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 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 };

Z
zhangyang 已提交
49 50 51 52 53
struct MemoryCacheArgs {
  void *offset;
  size_t size;
};

Z
zhangyang 已提交
54
#define IOCTL_FPGA_MAGIC 'F'
Z
zhangyang 已提交
55 56 57
#define IOCTL_MEMCACHE_INVAL _IOW(IOCTL_FPGA_MAGIC, 12, struct MemoryCacheArgs)
#define IOCTL_MEMCACHE_FLUSH _IOW(IOCTL_FPGA_MAGIC, 13, struct MemoryCacheArgs)

Z
zhangyang 已提交
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
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;
Z
zhangyang 已提交
106 107 108
  uint64_t value =
      *(volatile uint64_t *)((uint8_t *)g_fpgainfo.FpgaRegVirAddr +  // NOLINT
                             offset);                                // NOLINT
Z
zhangyang 已提交
109 110 111 112 113 114

  return value;
}

inline void reg_writeq(uint64_t value, uint32_t offset) {
  // DLOG << "offset : " << offset << ", value : " << value;
Z
zhangyang 已提交
115 116
  *(volatile uint64_t *)((uint8_t *)g_fpgainfo.FpgaRegVirAddr +  // NOLINT
                         offset) = value;
Z
zhangyang 已提交
117 118 119
}

int open_device_driver();
Z
zhangyang 已提交
120

Z
zhangyang 已提交
121
int close_device_driver();
Z
zhangyang 已提交
122

Z
zhangyang 已提交
123
void *fpga_malloc_driver(size_t size);
Z
zhangyang 已提交
124

Z
zhangyang 已提交
125
void fpga_free_driver(void *ptr);
Z
zhangyang 已提交
126 127 128 129 130 131 132

void fpga_copy_driver(void *dest, const void *src, size_t num);

int fpga_flush_driver(void *address, size_t size);

int fpga_invalidate_driver(void *address, size_t size);

Z
zhangyang 已提交
133 134 135
/*pe*/

uint64_t vaddr_to_paddr(void *address);
Z
zhangyang 已提交
136

Z
zhangyang 已提交
137 138
int fpga_regpoll(uint64_t reg, uint64_t val, int time);

Z
zhangyang 已提交
139
}  // namespace driver
Z
zhangyang 已提交
140 141
}  // namespace fpga
}  // namespace paddle_mobile