Blame opae-libs/plugins/xfpga/buffer.c

Packit 534379
// Copyright(c) 2017-2020, 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
Packit 534379
#ifdef HAVE_CONFIG_H
Packit 534379
#include <config.h>
Packit 534379
#endif // HAVE_CONFIG_H
Packit 534379
Packit 534379
#include "opae/access.h"
Packit 534379
#include "opae/utils.h"
Packit 534379
#include "common_int.h"
Packit 534379
#include "intel-fpga.h"
Packit 534379
Packit 534379
#include "opae_drv.h"
Packit 534379
Packit 534379
#include <sys/types.h>
Packit 534379
#include <sys/stat.h>
Packit 534379
#include <sys/ioctl.h>
Packit 534379
#include <sys/mman.h>
Packit 534379
#include <stdint.h>
Packit 534379
#include <stdbool.h>
Packit 534379
#include <unistd.h>
Packit 534379
Packit 534379
/*
Packit 534379
 * Allocate (mmap) new buffer
Packit 534379
 */
Packit 534379
STATIC fpga_result buffer_allocate(void **addr, uint64_t len, int flags)
Packit 534379
{
Packit 534379
	void *addr_local = NULL;
Packit 534379
Packit 534379
	UNUSED_PARAM(flags);
Packit 534379
Packit 534379
	ASSERT_NOT_NULL(addr);
Packit 534379
Packit 534379
	/* ! FPGA_BUF_PREALLOCATED, allocate memory using huge pages
Packit 534379
	   For buffer > 2M, use 1G-hugepage to ensure pages are
Packit 534379
	   contiguous */
Packit 534379
	if (len > 2 * MB)
Packit 534379
		addr_local = mmap(ADDR, len, PROTECTION, FLAGS_1G, 0, 0);
Packit 534379
	else if (len > 4 * KB)
Packit 534379
		addr_local = mmap(ADDR, len, PROTECTION, FLAGS_2M, 0, 0);
Packit 534379
	else
Packit 534379
		addr_local = mmap(ADDR, len, PROTECTION, FLAGS_4K, 0, 0);
Packit 534379
	if (addr_local == MAP_FAILED) {
Packit 534379
		if (errno == ENOMEM) {
Packit 534379
			if (len > 2 * MB)
Packit 534379
				OPAE_MSG("Could not allocate buffer (no free 1 "
Packit 534379
					 "GiB huge pages)");
Packit 534379
			if (len > 4 * KB)
Packit 534379
				OPAE_MSG("Could not allocate buffer (no free 2 "
Packit 534379
					 "MiB huge pages)");
Packit 534379
			else
Packit 534379
				OPAE_MSG("Could not allocate buffer (out of "
Packit 534379
					 "memory)");
Packit 534379
			return FPGA_NO_MEMORY;
Packit 534379
		}
Packit 534379
		OPAE_MSG("FPGA buffer mmap failed: %s", strerror(errno));
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	*addr = addr_local;
Packit 534379
	return FPGA_OK;
Packit 534379
}
Packit 534379
Packit 534379
/*
Packit 534379
 * Release (unmap) allocated buffer
Packit 534379
 */
Packit 534379
STATIC fpga_result buffer_release(void *addr, uint64_t len)
Packit 534379
{
Packit 534379
	/* If the buffer allocation was backed by hugepages, then
Packit 534379
	 * len must be rounded up to the nearest hugepage size,
Packit 534379
	 * otherwise munmap will fail.
Packit 534379
	 *
Packit 534379
	 * Buffer with size larger than 2MB is backed by 1GB page(s),
Packit 534379
	 * round up the size to the nearest GB boundary.
Packit 534379
	 *
Packit 534379
	 * Buffer with size smaller than 2MB but larger than 4KB is
Packit 534379
	 * backed by a 2MB pages, round up the size to 2MB.
Packit 534379
	 *
Packit 534379
	 * Buffer with size smaller than 4KB is backed by a 4KB page,
Packit 534379
	 * and its size is already 4KB aligned.
Packit 534379
	 */
Packit 534379
Packit 534379
	if (len > 2 * MB)
Packit 534379
		len = (len + (1 * GB - 1)) & (~(1 * GB - 1));
Packit 534379
	else if (len > 4 * KB)
Packit 534379
		len = 2 * MB;
Packit 534379
Packit 534379
	if (munmap(addr, len)) {
Packit 534379
		OPAE_MSG("FPGA buffer munmap failed: %s",
Packit 534379
			 strerror(errno));
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	return FPGA_OK;
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__ xfpga_fpgaPrepareBuffer(fpga_handle handle, uint64_t len,
Packit 534379
					   void **buf_addr, uint64_t *wsid,
Packit 534379
					   int flags)
Packit 534379
{
Packit 534379
	void *addr = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
	uint64_t io_addr = 0;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	int err;
Packit 534379
Packit 534379
	bool preallocated = (flags & FPGA_BUF_PREALLOCATED);
Packit 534379
	bool quiet = (flags & FPGA_BUF_QUIET);
Packit 534379
Packit 534379
	bool read_only = (flags & FPGA_BUF_READ_ONLY);
Packit 534379
	uint32_t map_flags = (read_only ? FPGA_DMA_TO_DEV : 0);
Packit 534379
Packit 534379
	uint64_t pg_size;
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	/* Assure wsid is a valid pointer */
Packit 534379
	if (!wsid) {
Packit 534379
		OPAE_MSG("WSID is NULL");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	if (flags & (~(FPGA_BUF_PREALLOCATED | FPGA_BUF_QUIET |
Packit 534379
		       FPGA_BUF_READ_ONLY))) {
Packit 534379
		OPAE_MSG("Unrecognized flags");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	pg_size = (uint64_t) sysconf(_SC_PAGE_SIZE);
Packit 534379
Packit 534379
	if (preallocated) {
Packit 534379
		/* A special case: respond FPGA_OK when !buf_addr and !len
Packit 534379
		 * as an indication that FPGA_BUF_PREALLOCATED is supported
Packit 534379
		 * by the library. */
Packit 534379
		if (!buf_addr && !len) {
Packit 534379
			result = FPGA_OK;
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
Packit 534379
		/* buffer is already allocated, check addresses */
Packit 534379
		if (!buf_addr) {
Packit 534379
			OPAE_MSG("No preallocated buffer address given");
Packit 534379
			result = FPGA_INVALID_PARAM;
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
		if (!(*buf_addr)) {
Packit 534379
			OPAE_MSG("Preallocated buffer address is NULL");
Packit 534379
			result = FPGA_INVALID_PARAM;
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
		/* check length */
Packit 534379
		if (!len || (len & (pg_size - 1))) {
Packit 534379
			OPAE_MSG("Preallocated buffer size is not a non-zero multiple of page size");
Packit 534379
			result = FPGA_INVALID_PARAM;
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
		addr = *buf_addr;
Packit 534379
	} else {
Packit 534379
Packit 534379
		if (!buf_addr) {
Packit 534379
			OPAE_MSG("buffer address is NULL");
Packit 534379
			result = FPGA_INVALID_PARAM;
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
Packit 534379
		if (!len) {
Packit 534379
			OPAE_MSG("buffer length is zero");
Packit 534379
			result = FPGA_INVALID_PARAM;
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
Packit 534379
		/* round up to nearest page boundary */
Packit 534379
		if (len & (pg_size - 1)) {
Packit 534379
			len = pg_size + (len & ~(pg_size - 1));
Packit 534379
		}
Packit 534379
Packit 534379
		result = buffer_allocate(&addr, len, flags);
Packit 534379
		if (result != FPGA_OK) {
Packit 534379
			goto out_unlock;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	if (opae_port_map(_handle->fddev, addr, len, map_flags, &io_addr)) {
Packit 534379
		if (!preallocated) {
Packit 534379
			buffer_release(addr, len);
Packit 534379
		}
Packit 534379
Packit 534379
		if (!quiet) {
Packit 534379
			OPAE_MSG("FPGA_PORT_DMA_MAP ioctl failed: %s",
Packit 534379
				 strerror(errno));
Packit 534379
		}
Packit 534379
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
Packit 534379
	/* Generate unique workspace ID */
Packit 534379
	*wsid = wsid_gen();
Packit 534379
Packit 534379
	/* Add to workspace id in order to store buffer length */
Packit 534379
	if (!wsid_add(_handle->wsid_root, *wsid, (uint64_t)addr, io_addr, len,
Packit 534379
		      0, 0, flags)) {
Packit 534379
		if (!preallocated) {
Packit 534379
			buffer_release(addr, len);
Packit 534379
		}
Packit 534379
Packit 534379
		OPAE_MSG("Failed to add workspace id %lu", *wsid);
Packit 534379
		result = FPGA_NO_MEMORY;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
Packit 534379
	/* Update buf_addr */
Packit 534379
	if (buf_addr)
Packit 534379
		*buf_addr = addr;
Packit 534379
Packit 534379
	/* Return */
Packit 534379
	result = FPGA_OK;
Packit 534379
Packit 534379
out_unlock:
Packit 534379
	err = pthread_mutex_unlock(&_handle->lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %s", strerror(err));
Packit 534379
	}
Packit 534379
	return result;
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__
Packit 534379
xfpga_fpgaReleaseBuffer(fpga_handle handle, uint64_t wsid)
Packit 534379
{
Packit 534379
	void *buf_addr;
Packit 534379
	uint64_t iova;
Packit 534379
	uint64_t len;
Packit 534379
	int err;
Packit 534379
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	fpga_result result = FPGA_NOT_FOUND;
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	/* Fetch the buffer physical address and length */
Packit 534379
	struct wsid_map *wm = wsid_find(_handle->wsid_root, wsid);
Packit 534379
	if (!wm) {
Packit 534379
		OPAE_MSG("WSID not found");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	buf_addr = (void *) wm->addr;
Packit 534379
	iova = wm->phys;
Packit 534379
	len = wm->len;
Packit 534379
Packit 534379
	bool preallocated = (wm->flags & FPGA_BUF_PREALLOCATED);
Packit 534379
Packit 534379
	if (opae_port_unmap(_handle->fddev, iova)) {
Packit 534379
		OPAE_MSG("FPGA_PORT_DMA_UNMAP ioctl failed: %s",
Packit 534379
			 strerror(errno));
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto ws_free;
Packit 534379
	}
Packit 534379
Packit 534379
	/* If the buffer was allocated in xfpga_fpgaPrepareBuffer() (i.e. it was not
Packit 534379
	 * preallocated), we need to unmap it here. Otherwise (if it was
Packit 534379
	 * preallocated) the mapping needs to stay intact. */
Packit 534379
	if (!preallocated) {
Packit 534379
		result = buffer_release(buf_addr, len);
Packit 534379
		if (result != FPGA_OK) {
Packit 534379
			OPAE_MSG("Buffer release failed");
Packit 534379
			goto ws_free;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	/* Return */
Packit 534379
	result = FPGA_OK;
Packit 534379
Packit 534379
ws_free:
Packit 534379
	/* Remove workspace */
Packit 534379
	wsid_del(_handle->wsid_root, wsid);
Packit 534379
Packit 534379
out_unlock:
Packit 534379
	err = pthread_mutex_unlock(&_handle->lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %s", strerror(err));
Packit 534379
	}
Packit 534379
	return result;
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__ xfpga_fpgaGetIOAddress(fpga_handle handle, uint64_t wsid,
Packit 534379
					  uint64_t *ioaddr)
Packit 534379
{
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	struct wsid_map *wm;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
	int err;
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	wm = wsid_find(_handle->wsid_root, wsid);
Packit 534379
	if (!wm) {
Packit 534379
		OPAE_MSG("WSID not found");
Packit 534379
		result = FPGA_NOT_FOUND;
Packit 534379
	} else {
Packit 534379
		*ioaddr = wm->phys;
Packit 534379
	}
Packit 534379
Packit 534379
	err = pthread_mutex_unlock(&_handle->lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %s", strerror(err));
Packit 534379
	}
Packit 534379
	return result;
Packit 534379
}