Skip to content

Commit

Permalink
[VTA] de10-nano driver (apache#3394)
Browse files Browse the repository at this point in the history
* rework;

* `de10-nano` -> `de10nano`;

* fix compilation error;

* bug fix;

* Update install.md

* Update install.md

* Update install.md

* update with current runtime;

* add debug messages;

* bug fix in cma kernel module;
  • Loading branch information
liangfu authored and tmoreau89 committed Sep 5, 2019
1 parent db39351 commit 94311b3
Show file tree
Hide file tree
Showing 7 changed files with 515 additions and 5 deletions.
4 changes: 3 additions & 1 deletion config/vta_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def main():
cflags_str = " ".join(pkg.cflags)
if pkg.TARGET == "pynq":
cflags_str += " -DVTA_TARGET_PYNQ"
if pkg.TARGET == "ultra96":
elif cfg.TARGET == "de10nano":
cflags_str += " -DVTA_TARGET_DE10_NANO"
elif pkg.TARGET == "ultra96":
cflags_str += " -DVTA_TARGET_ULTRA96"
print(cflags_str)

Expand Down
6 changes: 3 additions & 3 deletions python/vta/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ def target(self):
@property
def target_host(self):
"""The target host"""
if self.TARGET == "pynq":
if self.TARGET in ["pynq", "de10nano"]:
return "llvm -target=armv7-none-linux-gnueabihf"
if self.TARGET == "ultra96":
elif self.TARGET == "ultra96":
return "llvm -target=aarch64-linux-gnu"
if self.TARGET == "sim" or self.TARGET == "tsim":
elif self.TARGET in ["sim", "tsim"]:
return "llvm"
raise ValueError("Unknown target %s" % self.TARGET)

Expand Down
2 changes: 1 addition & 1 deletion python/vta/testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def run(run_func):
assert simulator.enabled()
run_func(env, rpc.LocalSession())

elif env.TARGET in ["pynq", "ultra96"]:
elif env.TARGET in ["pynq", "ultra96", "de10nano"]:
# The environment variables below should be set if we are using
# a tracker to obtain a remote for a test device
tracker_host = os.environ.get("TVM_TRACKER_HOST", None)
Expand Down
198 changes: 198 additions & 0 deletions src/de10nano/cma_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/*
* The MIT License (MIT)
*
* COPYRIGHT (C) 2017 Institute of Electronics and Computer Science (EDI), Latvia.
* AUTHOR: Rihards Novickis (rihards.novickis@edi.lv)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

/*!
* Copyright (c) 2018 by Contributors
* \file cma_api.cc
* \brief Application layer implementation for contigous memory allocation.
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include "cma_api.h"

#ifndef CMA_IOCTL_MAGIC
#define CMA_IOCTL_MAGIC 0xf2
#endif

#define CMA_ALLOC_CACHED _IOC(_IOC_WRITE|_IOC_READ, CMA_IOCTL_MAGIC, 1, 4)
#define CMA_ALLOC_NONCACHED _IOC(_IOC_WRITE|_IOC_READ, CMA_IOCTL_MAGIC, 2, 4)
#define CMA_FREE _IOC(_IOC_WRITE, CMA_IOCTL_MAGIC, 3, 4)
#define CMA_GET_PHY_ADDR _IOC(_IOC_WRITE|_IOC_READ, CMA_IOCTL_MAGIC, 4, 4)
#define CMA_GET_SIZE _IOC(_IOC_WRITE|_IOC_READ, CMA_IOCTL_MAGIC, 5, 4)

#define CMA_IOCTL_MAXNR 5

#ifndef CMA_DEBUG
#define CMA_DEBUG 0
#endif
#ifndef DRIVER_NODE_NAME
#define DRIVER_NODE_NAME "cma"
#endif

#if CMA_DEBUG == 1
#define __DEBUG(fmt, args...) printf("CMA_API_DEBUG: " fmt, ##args)
#else
#define __DEBUG(fmt, args...)
#endif

#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))


/* Private functions */
void *cma_alloc(size_t size, unsigned ioctl_cmd);

/* Global file descriptor */
int cma_fd = 0;

int cma_init(void) {
__DEBUG("Opening \"/dev/" DRIVER_NODE_NAME "\" file\n");

cma_fd = open("/dev/" DRIVER_NODE_NAME, O_RDWR);
if (cma_fd == -1) {
__DEBUG("Failed to initialize api - \"%s\"\n", strerror(errno));
return -1;
}

return 0;
}

int cma_release(void) {
__DEBUG("Closing \"/dev/" DRIVER_NODE_NAME "\" file\n");

if (close(cma_fd) == -1) {
__DEBUG("Failed to finilize api - \"%s\"\n", strerror(errno));
return -1;
}

return 0;
}

void *cma_alloc_cached(size_t size) {
return cma_alloc(size, CMA_ALLOC_CACHED);
}

void *cma_alloc_noncached(size_t size) {
return cma_alloc(size, CMA_ALLOC_NONCACHED);
}

int cma_free(void *mem) {
__DEBUG("Releasing contigous memory from 0x%x\n", (unsigned)mem);
unsigned data, v_addr;

/* save user space pointer value */
data = (unsigned)mem;
v_addr = (unsigned)mem;

if ( ioctl(cma_fd, CMA_GET_SIZE, &data) == -1 ) {
__DEBUG("cma_free - ioctl command unsuccsessful - 0\n");
return -1;
}
/* data now contains size */

/* unmap memory */
munmap(mem, data);

/* free cma entry */
if ( ioctl(cma_fd, CMA_FREE, &v_addr) == -1 ) {
__DEBUG("cma_free - ioctl command unsuccsessful - 1\n");
return -1;
}

return 0;
}

unsigned cma_get_phy_addr(void *mem) {
unsigned data;
__DEBUG("Getting physical address from 0x%x\n", (unsigned)mem);

/* save user space pointer value */
data = (unsigned)mem;

/* get physical address */
if ( ioctl(cma_fd, CMA_GET_PHY_ADDR, &data) == -1 ) {
__DEBUG("cma_free - ioctl command unsuccsessful\n");
return 0;
}
/* data now contains physical address */

return data;
}


void *cma_alloc(size_t size, unsigned ioctl_cmd) {
unsigned data;
void *mem;
__DEBUG("Allocating 0x%x bytes of contigous memory\n", size);

/* Page align size */
size = ROUND_UP(size, getpagesize());

/* ioctl cmd to allocate contigous memory */
data = (unsigned)size;
if ( ioctl(cma_fd, ioctl_cmd, &data) == -1 ) {
__DEBUG("cma_alloc - ioctl command unsuccsessful\n");
return NULL;
}

/* at this point phy_addr is written to data */

/* mmap memory */
mem = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, cma_fd, data);
if (mem == MAP_FAILED) {
__DEBUG("cma_alloc - mmap unsuccsessful\n");
return NULL;
}

return mem;
}
93 changes: 93 additions & 0 deletions src/de10nano/cma_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* \file cma_api.h
* \brief API for contigous memory allocation driver.
*/

#ifndef VTA_DE10NANO_CMA_API_H_
#define VTA_DE10NANO_CMA_API_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
* \brief Initialize CMA api (basically perform open() syscall).
*
* \return Returns 0 on SUCCESS. On FAILURE returns -1 and errno is set
* accordingly.
*/
int cma_init(void);


/**
* \brief Release CMA api (basically perform close() syscall).
*
* \return Returns 0 on SUCCESS. On FAILURE returns -1 and errno is set
* accordingly.
*/
int cma_release(void);


/**
* \brief Allocate cached, physically contigous memory.
*
* \param size Size in bytes.
*
* \return Returns NULL on FAILURE. Otherwise pointer to valid userspace
* memory.
*/
void *cma_alloc_cached(size_t size);


/**
* \brief Allocate noncached, physically contigous memory.
*
* \param size Size in bytes.
*
* \return Returns NULL on FAILURE. Otherwise pointer to valid userspace
* memory.
*/
void *cma_alloc_noncached(size_t size);


/**
* \brief Release physically contigous memory.
*
* \param mem Pointer to previously allocated contiguous memory.
*
* \return Returns 0 on SUCCESS, -1 on FAILURE.
*/
int cma_free(void *mem);


/**
* \brief Get physical memory of cma memory block (should be used for DMA).
*
* \param mem Pointer to previously allocated contiguous memory.
*
* \return Returns address on SUCCESS, 0 on FAILURE.
*/
unsigned cma_get_phy_addr(void *mem);


#ifdef __cplusplus
}
#endif
#endif // VTA_DE10NANO_CMA_API_H_
Loading

0 comments on commit 94311b3

Please sign in to comment.