Blame opae-libs/plugins/xfpga/mmio.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 "opae_drv.h"
Packit 534379
#include "intel-fpga.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 <stdbool.h>
Packit 534379
#include <stdint.h>
Packit 534379
Packit 534379
/* Port UAFU */
Packit 534379
#define AFU_PERMISSION (FPGA_REGION_READ | FPGA_REGION_WRITE | FPGA_REGION_MMAP)
Packit 534379
#define AFU_SIZE	0x40000
Packit 534379
#define AFU_OFFSET	0
Packit 534379
Packit 534379
STATIC fpga_result port_mmap_region(fpga_handle handle,
Packit 534379
			     void **vaddr,
Packit 534379
			     uint64_t size,
Packit 534379
			     uint32_t flags,
Packit 534379
			     uint64_t offset,
Packit 534379
			     uint32_t mmio_num)
Packit 534379
{
Packit 534379
	void *addr;
Packit 534379
	int err;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	UNUSED_PARAM(mmio_num);
Packit 534379
Packit 534379
	/* Assure returning pointer contains allocated memory */
Packit 534379
	ASSERT_NOT_NULL(vaddr);
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	/* Map MMIO memory */
Packit 534379
	addr = (void *) mmap(NULL, size, flags, MAP_SHARED, _handle->fddev, offset);
Packit 534379
	if (addr == MAP_FAILED) {
Packit 534379
		OPAE_MSG("Unable to map MMIO region. Error value is : %s",
Packit 534379
			 strerror(errno));
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	/* Save return address */
Packit 534379
	*vaddr = addr;
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
STATIC fpga_result map_mmio_region(fpga_handle handle, uint32_t mmio_num)
Packit 534379
{
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	void     *addr  = NULL;
Packit 534379
	uint64_t wsid   = 0;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
	opae_port_region_info rinfo = { 0 };
Packit 534379
Packit 534379
	result = opae_get_port_region_info(_handle->fddev, mmio_num, &rinfo);
Packit 534379
	// TODO: process result seperately of rinfo.flags
Packit 534379
	if (result || (rinfo.flags != AFU_PERMISSION)) {
Packit 534379
		OPAE_MSG("Invalid MMIO permission flags");
Packit 534379
		result = FPGA_NO_ACCESS;
Packit 534379
		return result;
Packit 534379
	}
Packit 534379
Packit 534379
	/* Map UAFU MMIO */
Packit 534379
	result = port_mmap_region(handle,
Packit 534379
				    (void **) &addr,
Packit 534379
				    rinfo.size,
Packit 534379
				    PROT_READ | PROT_WRITE,
Packit 534379
				    rinfo.offset,
Packit 534379
				    mmio_num);
Packit 534379
	if (result != FPGA_OK)
Packit 534379
		return result;
Packit 534379
Packit 534379
	/* Add to MMIO list */
Packit 534379
	wsid = wsid_gen();
Packit 534379
	if (!wsid_add(_handle->mmio_root,
Packit 534379
		      wsid,
Packit 534379
		      (uint64_t) addr,
Packit 534379
		      (uint64_t) NULL,
Packit 534379
		      rinfo.size,
Packit 534379
		      (uint64_t) addr,
Packit 534379
		      mmio_num,
Packit 534379
		      0)) {
Packit 534379
		if (munmap(addr, rinfo.size)) {
Packit 534379
			OPAE_MSG("munmap failed. Error value is : %s",
Packit 534379
				 strerror(errno));
Packit 534379
			return FPGA_INVALID_PARAM;
Packit 534379
		} else {
Packit 534379
			OPAE_MSG("Failed to add MMIO id: %d", mmio_num);
Packit 534379
			return FPGA_NO_MEMORY;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	return FPGA_OK;
Packit 534379
}
Packit 534379
Packit 534379
/* Lazy mapping of MMIO region (only map if not already mapped) */
Packit 534379
STATIC fpga_result find_or_map_wm(fpga_handle handle, uint32_t mmio_num,
Packit 534379
				struct wsid_map **wm_out)
Packit 534379
{
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	struct wsid_map *wm = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	wm = wsid_find_by_index(_handle->mmio_root, mmio_num);
Packit 534379
	if (!wm) {
Packit 534379
		result = map_mmio_region(handle, mmio_num);
Packit 534379
		if (result != FPGA_OK) {
Packit 534379
			OPAE_ERR("failed to map mmio region %d", mmio_num);
Packit 534379
			return result;
Packit 534379
		}
Packit 534379
		wm = wsid_find_by_index(_handle->mmio_root, mmio_num);
Packit 534379
		if (!wm) {
Packit 534379
			OPAE_ERR("unable to map wsid for mmio region %d", mmio_num);
Packit 534379
			return FPGA_NO_MEMORY;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	*wm_out = wm;
Packit 534379
	return FPGA_OK;
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__ xfpga_fpgaWriteMMIO32(fpga_handle handle,
Packit 534379
					 uint32_t mmio_num,
Packit 534379
					 uint64_t offset,
Packit 534379
					 uint32_t value)
Packit 534379
{
Packit 534379
Packit 534379
	int err;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	struct wsid_map *wm = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	if (offset % sizeof(uint32_t) != 0) {
Packit 534379
		OPAE_MSG("Misaligned MMIO access");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	result = find_or_map_wm(handle, mmio_num, &wm;;
Packit 534379
	if (result)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	if (offset > wm->len) {
Packit 534379
		OPAE_MSG("offset out of bounds");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	*((volatile uint32_t *) ((uint8_t *)wm->offset + offset)) = value;
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_fpgaReadMMIO32(fpga_handle handle,
Packit 534379
					uint32_t mmio_num,
Packit 534379
					uint64_t offset,
Packit 534379
					uint32_t *value)
Packit 534379
{
Packit 534379
	int err;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	struct wsid_map *wm = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	if (offset % sizeof(uint32_t) != 0) {
Packit 534379
		OPAE_MSG("Misaligned MMIO access");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	result = find_or_map_wm(handle, mmio_num, &wm;;
Packit 534379
	if (result)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	if (offset > wm->len) {
Packit 534379
		OPAE_MSG("offset out of bounds");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	*value = *((volatile uint32_t *) ((uint8_t *)wm->offset + offset));
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_fpgaWriteMMIO64(fpga_handle handle,
Packit 534379
					 uint32_t mmio_num,
Packit 534379
					 uint64_t offset,
Packit 534379
					 uint64_t value)
Packit 534379
{
Packit 534379
	int err;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	struct wsid_map *wm = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	if (offset % sizeof(uint64_t) != 0) {
Packit 534379
		OPAE_MSG("Misaligned MMIO access");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	result = find_or_map_wm(handle, mmio_num, &wm;;
Packit 534379
	if (result)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	if (offset > wm->len) {
Packit 534379
		OPAE_MSG("offset out of bounds");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	*((volatile uint64_t *) ((uint8_t *)wm->offset + offset)) = value;
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_fpgaReadMMIO64(fpga_handle handle,
Packit 534379
					uint32_t mmio_num,
Packit 534379
					uint64_t offset,
Packit 534379
					uint64_t *value)
Packit 534379
{
Packit 534379
	int err;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	struct wsid_map *wm = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	if (offset % sizeof(uint64_t) != 0) {
Packit 534379
		OPAE_MSG("Misaligned MMIO access");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	result = find_or_map_wm(handle, mmio_num, &wm;;
Packit 534379
	if (result)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	if (offset > wm->len) {
Packit 534379
		OPAE_MSG("offset out of bounds");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	*value = *((volatile uint64_t *) ((uint8_t *)wm->offset + offset));
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
static inline void copy512(const void *src, void *dst)
Packit 534379
{
Packit 534379
    asm volatile("vmovdqu64 (%0), %%zmm0;"
Packit 534379
		 "vmovdqu64 %%zmm0, (%1);"
Packit 534379
		 :
Packit 534379
		 : "r"(src), "r"(dst));
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__ xfpga_fpgaWriteMMIO512(fpga_handle handle,
Packit 534379
					 uint32_t mmio_num,
Packit 534379
					 uint64_t offset,
Packit 534379
					 const void *value)
Packit 534379
{
Packit 534379
	int err;
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *) handle;
Packit 534379
	struct wsid_map *wm = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	if (offset % 64 != 0) {
Packit 534379
		OPAE_MSG("Misaligned MMIO access");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	if (!(_handle->flags & OPAE_FLAG_HAS_MMX512)) {
Packit 534379
		result = FPGA_NOT_SUPPORTED;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	result = find_or_map_wm(handle, mmio_num, &wm;;
Packit 534379
	if (result)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	if (offset > wm->len) {
Packit 534379
		OPAE_MSG("offset out of bounds");
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	copy512(value, (uint8_t *)wm->offset + offset);
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_fpgaMapMMIO(fpga_handle handle,
Packit 534379
				     uint32_t mmio_num,
Packit 534379
				     uint64_t **mmio_ptr)
Packit 534379
{
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	struct wsid_map *wm = NULL;
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
	result = find_or_map_wm(handle, mmio_num, &wm;;
Packit 534379
	if (result)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	/* Store return value only if return pointer has allocated memory */
Packit 534379
	if (mmio_ptr)
Packit 534379
		*mmio_ptr = (uint64_t *)wm->addr;
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_fpgaUnmapMMIO(fpga_handle handle,
Packit 534379
				       uint32_t mmio_num)
Packit 534379
{
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	void *mmio_ptr;
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
	/* Fetch the MMIO physical address and length */
Packit 534379
	struct wsid_map *wm = wsid_find_by_index(_handle->mmio_root, mmio_num);
Packit 534379
	if (!wm) {
Packit 534379
		OPAE_MSG("MMIO region %d not found", mmio_num);
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	/* Unmap UAFU MMIO */
Packit 534379
	mmio_ptr = (void *) wm->offset;
Packit 534379
	if (munmap((void *) mmio_ptr, wm->len)) {
Packit 534379
		OPAE_MSG("munmap failed: %s",
Packit 534379
			 strerror(errno));
Packit 534379
		result = FPGA_INVALID_PARAM;
Packit 534379
		goto out_unlock;
Packit 534379
	}
Packit 534379
Packit 534379
	/* Remove MMIO */
Packit 534379
	wsid_del(_handle->mmio_root, wm->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
}