driver.cpp 7.5 KB
Newer Older
Z
zhangyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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. */

#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Z
zhangyang 已提交
20
#include <sys/ioctl.h>
Z
zhangyang 已提交
21 22 23 24 25 26 27 28 29 30
#include <sys/mman.h>
#include <unistd.h>
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>

#include "common/enforce.h"
Z
zhangyang 已提交
31
#include "fpga/common/driver.h"
Z
zhangyang 已提交
32 33 34

namespace paddle_mobile {
namespace fpga {
Z
zhangyang 已提交
35
namespace driver {
Z
zhangyang 已提交
36 37 38 39 40 41 42 43 44 45 46
struct FPGA_INFO g_fpgainfo;

int open_drvdevice() {
  if (g_fpgainfo.fd_drv == -1) {
    g_fpgainfo.fd_drv = open(g_fpgainfo.drvdevice_path, O_RDWR);
  }
  return g_fpgainfo.fd_drv;
}

int open_memdevice() {
  if (g_fpgainfo.fd_mem == -1) {
Z
zhangyang 已提交
47 48
    // g_fpgainfo.fd_mem = open(g_fpgainfo.memdevice_path, O_RDWR | O_DSYNC);
    g_fpgainfo.fd_mem = open(g_fpgainfo.memdevice_path, O_RDWR);
Z
zhangyang 已提交
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  }
  return g_fpgainfo.fd_mem;
}

void pl_reset() {
  // DLOG << "PL RESET";

  usleep(100 * 1000);
}

void setup_pe(struct pe_data_s *pe_data, struct fpga_pe *pe,
              char const *type_name, int pe_idx) {
  memset(pe, 0, sizeof(struct fpga_pe));

  pe->outer = pe_data;
  snprintf(pe->type_name, MAX_TYPE_NAME_LENTH, "%s", type_name);

  pe->status = IDLE;
  pe->interrupt_cnt = 0;
  pe_data->pes[pe_idx] = pe;
  pe_data->pe_num++;
}

void pl_init() {
  struct pe_data_s *pe_data = nullptr;

  pl_reset();

  pe_data = (struct pe_data_s *)malloc(sizeof(struct pe_data_s));
  if (pe_data == nullptr) {
    DLOG << "pe_data malloc error!";
    return;
  }
  memset(pe_data, 0, sizeof(struct pe_data_s));
  pthread_mutex_init(&pe_data->mutex, 0);

  setup_pe(pe_data, &pe_data->pe_conv, "CONV", PE_IDX_CONV);
  setup_pe(pe_data, &pe_data->pe_pooling, "POOLING", PE_IDX_POOLING);
  setup_pe(pe_data, &pe_data->pe_ew, "EW", PE_IDX_EW);
  setup_pe(pe_data, &pe_data->pe_bypass, "BYPASS", PE_IDX_BYPASS);

  g_fpgainfo.pe_data = pe_data;
}

void pl_destroy() {
  struct pe_data_s *pe_data = g_fpgainfo.pe_data;
  pthread_mutex_destroy(&pe_data->mutex);
  free(pe_data);
}

void pl_start() {
  struct pe_data_s *pe_data = g_fpgainfo.pe_data;

  pthread_mutex_unlock(&pe_data->mutex);
}

void pl_stop() {
  struct pe_data_s *pe_data = g_fpgainfo.pe_data;

  pthread_mutex_lock(&pe_data->mutex);
}

void pl_reinit() {
  struct pe_data_s *pe_data = g_fpgainfo.pe_data;
  struct fpga_pe *pe = nullptr;
  int i = 0;

  pl_stop();
  pl_reset();
  pl_start();

  for (i = 0; i < pe_data->pe_num; i++) {
    pe = pe_data->pes[i];
    pe->status = IDLE;
    pe->interrupt_cnt = 0;
  }

  pl_start();
}

int pl_get_status() { return 0; }

/*tmie单位us*/
int fpga_regpoll(uint64_t reg, uint64_t val, int time) {
  uint64_t i = 0;
  /*timeout精确性待确认*/
Z
zhangyang 已提交
135
  int64_t timeout = time * 6;
Z
zhangyang 已提交
136 137 138 139 140 141 142

  for (i = 0; i < timeout; i++) {
    if (val == reg_readq(reg)) {
      break;
    }
  }

Z
zhangyang 已提交
143
  if (i < timeout) {
Z
zhangyang 已提交
144 145 146 147 148 149 150 151 152
    return 0;
  } else {
    return -1;
  }
}



void memory_release(struct fpga_memory *memory) {
Z
zhangyang 已提交
153 154 155 156 157 158 159 160
  void *ptr = nullptr;

  /*unmap memory*/
  std::map<void *, size_t> map = g_fpgainfo.fpga_addr2size_map;
  std::map<void *, size_t>::iterator iter;
  for (iter = map.begin(); iter != map.end(); iter++) {
    fpga_free_driver(ptr);
  }
Z
zhangyang 已提交
161 162 163 164
}



165
uint64_t vaddr_to_paddr_driver(void *address) {
Z
zhangyang 已提交
166 167 168 169 170
  uint64_t paddr = 0;
  auto iter = g_fpgainfo.fpga_vaddr2paddr_map.find(address);
  if (iter != g_fpgainfo.fpga_vaddr2paddr_map.end()) {
    paddr = iter->second;
  } else {
Z
zhangyang 已提交
171
    DLOG << "Invalid pointer: " << address;
Z
zhangyang 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  }

  return paddr;
}

void *fpga_reg_malloc(size_t size) {
  void *ret = nullptr;
  ret = mmap64(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,
               g_fpgainfo.fd_drv, FPGA_REG_PHY_ADDR);
  // PADDLE_MOBILE_ENFORCE(ret != (void *)-1, "Should not be -1");

  g_fpgainfo.fpga_addr2size_map.insert(std::make_pair(ret, size));

  return ret;
}

Z
zhangyang 已提交
188 189 190 191 192 193 194 195 196
void *fpga_reg_free(void *ptr) {
  size_t size = 0;

  auto iter = g_fpgainfo.fpga_addr2size_map.find(ptr);
  if (iter != g_fpgainfo.fpga_addr2size_map.end()) {
    size = iter->second;
    g_fpgainfo.fpga_addr2size_map.erase(iter);
    munmap(ptr, size);
  } else {
197
    DLOG << "Invalid pointer" << ptr;
Z
zhangyang 已提交
198 199 200
  }
}

201 202 203 204
static inline int do_ioctl(int64_t req, const void *arg) {
  return ioctl(g_fpgainfo.fd_mem, req, arg);
}

Z
zhangyang 已提交
205 206 207
void *fpga_malloc_driver(size_t size) {
  void *ret = nullptr;
  uint64_t phy_addr = 0;
Z
zhangyang 已提交
208
  int i = 0;
209 210
  struct MemoryVM2PHYArgs args;
  struct MemoryCacheArgs args_c;
Z
zhangyang 已提交
211

212
 // memory_request(g_fpgainfo.memory_info, size, &phy_addr);
Z
zhangyang 已提交
213 214

  ret = mmap64(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,
215
               g_fpgainfo.fd_mem, FPGA_MEM_PHY_ADDR);
Z
zhangyang 已提交
216 217
  PADDLE_MOBILE_ENFORCE(ret != (void *)-1, "Should not be -1");

218 219 220 221 222
  args.pVM= (void *)ret;
  args.pPHY =(void *)0;
  do_ioctl(IOCTL_MEMORY_VM2PHY, &args);
  phy_addr = (uint64_t)args.pPHY;

Z
zhangyang 已提交
223 224 225 226 227 228 229 230
  g_fpgainfo.fpga_vaddr2paddr_map.insert(std::make_pair(ret, phy_addr));
  g_fpgainfo.fpga_addr2size_map.insert(std::make_pair(ret, size));

  return ret;
}

void fpga_free_driver(void *ptr) {
  size_t size = 0;
Z
zhangyang 已提交
231 232
  uint32_t pos = 0;
  uint64_t p_addr = 0;
Z
zhangyang 已提交
233 234 235 236 237 238

  auto iter = g_fpgainfo.fpga_addr2size_map.find(ptr);
  if (iter != g_fpgainfo.fpga_addr2size_map.end()) {
    size = iter->second;
    g_fpgainfo.fpga_addr2size_map.erase(iter);
    munmap(ptr, size);
Z
zhangyang 已提交
239

240
    p_addr = vaddr_to_paddr_driver(ptr);
Z
zhangyang 已提交
241 242
    pos = (p_addr - g_fpgainfo.memory_info->mem_start) / FPGA_PAGE_SIZE;

Z
zhangyang 已提交
243 244 245 246 247

    auto iter = g_fpgainfo.fpga_vaddr2paddr_map.find(ptr);
    if (iter != g_fpgainfo.fpga_vaddr2paddr_map.end()) {
      g_fpgainfo.fpga_vaddr2paddr_map.erase(iter);
    }
Z
zhangyang 已提交
248
  } else {
249
    DLOG << "Invalid pointer" << ptr;
Z
zhangyang 已提交
250 251 252
  }
}

Z
zhangyang 已提交
253 254 255 256
int fpga_flush_driver(void *address, size_t size) {
  struct MemoryCacheArgs args;
  uint64_t p_addr;

257
  p_addr = vaddr_to_paddr_driver(address);
Z
zhangyang 已提交
258

Z
zhangyang 已提交
259
  args.offset = (void *)(p_addr - FPGA_MEM_PHY_ADDR);  // NOLINT
Z
zhangyang 已提交
260 261 262 263 264 265 266 267 268
  args.size = size;

  return do_ioctl(IOCTL_MEMCACHE_FLUSH, &args);
}

int fpga_invalidate_driver(void *address, size_t size) {
  struct MemoryCacheArgs args;
  uint64_t p_addr;

269
  p_addr = vaddr_to_paddr_driver(address);
Z
zhangyang 已提交
270

Z
zhangyang 已提交
271
  args.offset = (void *)(p_addr - FPGA_MEM_PHY_ADDR);  // NOLINT
Z
zhangyang 已提交
272 273 274 275 276 277 278 279
  args.size = size;

  return do_ioctl(IOCTL_MEMCACHE_INVAL, &args);
}

void fpga_copy_driver(void *dest, const void *src, size_t num) {
  uint64_t i;
  for (i = 0; i < num; i++) {
Z
zhangyang 已提交
280
    *((int8_t *)dest + i) = *((int8_t *)src + i);  // NOLINT
Z
zhangyang 已提交
281 282 283 284 285
  }

  return;
}

Z
zhangyang 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
int open_device_driver() {
  g_fpgainfo.FpgaRegPhyAddr = FPGA_REG_PHY_ADDR;
  g_fpgainfo.FpgaMemPhyAddr = FPGA_MEM_PHY_ADDR;
  g_fpgainfo.FpgaRegVirAddr = nullptr;
  g_fpgainfo.pe_data = nullptr;
  g_fpgainfo.drvdevice_path = "/dev/fpgadrv0";
  g_fpgainfo.memdevice_path = "/dev/fpgamem0";
  g_fpgainfo.fd_drv = -1;
  g_fpgainfo.fd_mem = -1;

  int ret = 0;
  ret = open_drvdevice();
  ret |= open_memdevice();

  g_fpgainfo.FpgaRegVirAddr =
      (uint64_t *)fpga_reg_malloc(FPGA_REG_SIZE);  // NOLINT
302
  //fpga_memory_add();
Z
zhangyang 已提交
303 304 305 306 307 308 309 310

  pl_init();

  return ret;
}

int close_device_driver() {
  pl_destroy();
Z
zhangyang 已提交
311
  fpga_reg_free(g_fpgainfo.FpgaRegVirAddr);
Z
zhangyang 已提交
312
  memory_release(g_fpgainfo.memory_info);
313
  
Z
zhangyang 已提交
314 315 316
  return 0;
}

Z
zhangyang 已提交
317
}  // namespace driver
Z
zhangyang 已提交
318 319
}  // namespace fpga
}  // namespace paddle_mobile