20#include "libocxl_internal.h"
57 int available_mmio = -1;
60 for (uint16_t mmio = 0; mmio < afu->mmio_count; mmio++) {
61 if (!afu->mmios[mmio].start) {
62 available_mmio = mmio;
67 if (available_mmio == -1) {
68 if (afu->mmio_count == afu->mmio_max_count) {
69 ocxl_err rc = grow_buffer(afu, (
void **)&afu->mmios, &afu->mmio_max_count,
sizeof(ocxl_mmio_area), INITIAL_MMIO_COUNT);
71 errmsg(afu, rc,
"Could not grow MMIO buffer");
76 available_mmio = afu->mmio_count++;
79 afu->mmios[available_mmio].start = addr;
80 afu->mmios[available_mmio].length = size;
81 afu->mmios[available_mmio].type = type;
82 afu->mmios[available_mmio].afu = afu;
84 *handle = &afu->mmios[available_mmio];
86 TRACE(afu,
"Mapped %ld bytes of %s MMIO at %p",
101 char path[PATH_MAX + 1];
102 int length = snprintf(path,
sizeof(path),
"%s/global_mmio_area", afu->sysfs_path);
103 if (length >= (
int)
sizeof(path)) {
105 errmsg(afu, rc,
"global MMIO path truncated");
109 int fd = open(path, O_RDWR | O_CLOEXEC);
112 errmsg(afu, rc,
"Could not open global MMIO '%s': Error %d: %s", path, errno, strerror(errno));
116 afu->global_mmio_fd = fd;
144static ocxl_err global_mmio_map(ocxl_afu *afu,
size_t size,
int prot, uint64_t flags, off_t offset,
147 if (afu->global_mmio.length == 0) {
149 errmsg(afu, rc,
"Cannot map Global MMIO as there is 0 bytes allocated by the AFU");
155 errmsg(afu, rc,
"MMIO flags of 0x%llx is not supported by this version of libocxl", flags);
159 void *addr = mmap(NULL, size, prot, MAP_SHARED, afu->global_mmio_fd, offset);
160 if (addr == MAP_FAILED) {
162 errmsg(afu, rc,
"Could not map global MMIO, %d: %s", errno, strerror(errno));
169 errmsg(afu, rc,
"Could not register global MMIO region");
174 *region = mmio_region;
202static ocxl_err mmio_map(ocxl_afu *afu,
size_t size,
int prot, uint64_t flags, off_t offset,
ocxl_mmio_h *region)
206 errmsg(afu, rc,
"MMIO flags of 0x%llx is not supported by this version of libocxl", flags);
212 errmsg(afu, rc,
"Could not map per-PASID MMIO as the AFU has not been opened");
216 if (!afu->attached) {
218 errmsg(afu, rc,
"Could not map per-PASID MMIO as the AFU has not been attached");
222 void *addr = mmap(NULL, size, prot, MAP_SHARED, afu->fd, offset);
223 if (addr == MAP_FAILED) {
225 errmsg(afu, rc,
"Could not map per-PASID MMIO: %d: %s", errno, strerror(errno));
232 errmsg(afu, rc,
"Could not register global MMIO region", afu->identifier.afu_name);
237 *region = mmio_region;
271 size = afu->per_pasid_mmio.length;
274 size = afu->global_mmio.length;
283 if (offset + size > afu->global_mmio.length) {
285 errmsg(afu, rc,
"Offset(%#x) + size(%#x) of global MMIO map request exceeds available size of %#x",
286 offset, size, afu->global_mmio.length);
289 return global_mmio_map(afu, size, prot, flags, offset, region);
292 if (offset + size > afu->per_pasid_mmio.length) {
294 errmsg(afu, rc,
"Offset(%#x) + size(%#x) of per-pasid MMIO map request exceeds available size of %#x",
295 offset, size, afu->global_mmio.length);
298 return mmio_map(afu, size, prot, flags, offset, region);
301 errmsg(afu, rc,
"Unknown MMIO type %d", type);
338 if (!region->start) {
342 munmap(region->start, region->length);
343 region->start = NULL;
364 return afu->global_mmio_fd;
387 return afu->global_mmio.length;
390 return afu->per_pasid_mmio.length;
413 if (!region->start) {
415 errmsg(region->afu, rc,
"MMIO region has already been unmapped");
419 *address = region->start;
420 *size = region->length;
441 errmsg(NULL, rc,
"MMIO region is invalid");
445 if (!region->start) {
447 errmsg(region->afu, rc,
"MMIO region has already been unmapped");
451 if (offset >= (off_t)(region->length - (size - 1))) {
453 errmsg(region->afu, rc,
"%s MMIO access of 0x%016lx exceeds limit of 0x%016lx",
455 offset, region->length);
482 ocxl_err ret = mmio_check(region, offset, 4);
487 __sync_synchronize();
488 *out = *(
volatile uint32_t *)(region->start + offset);
489 __sync_synchronize();
491 TRACE(region->afu,
"%s MMIO Read32@0x%04lx=0x%08x",
518 ocxl_err ret = mmio_check(region, offset, 8);
523 __sync_synchronize();
524 *out = *(
volatile uint64_t *)(region->start + offset);
525 __sync_synchronize();
527 TRACE(region->afu,
"%s MMIO Read64@0x%04lx=0x%016lx",
554inline static ocxl_err mmio_write32_native(
ocxl_mmio_h region, off_t offset, uint32_t value)
556 ocxl_err ret = mmio_check(region, offset, 4);
561 TRACE(region->afu,
"%s MMIO Write32@0x%04lx=0x%08x",
565 volatile uint32_t *addr = (uint32_t *)(region->start + offset);
567 __sync_synchronize();
569 __sync_synchronize();
592inline static ocxl_err mmio_write64_native(
ocxl_mmio_h region, off_t offset, uint64_t value)
594 ocxl_err ret = mmio_check(region, offset, 8);
599 TRACE(region->afu,
"%s MMIO Write64@0x%04lx=0x%016lx",
603 volatile uint64_t *addr = (uint64_t *)(region->start + offset);
605 __sync_synchronize();
607 __sync_synchronize();
633 ocxl_err ret = mmio_read32_native(mmio, offset, &val);
635 if (UNLIKELY(ret !=
OCXL_OK)) {
677 ocxl_err ret = mmio_read64_native(mmio, offset, &val);
679 if (UNLIKELY(ret !=
OCXL_OK)) {
722 value = htobe32(value);
726 value = htole32(value);
732 return mmio_write32_native(mmio, offset, value);
757 value = htobe64(value);
761 value = htole64(value);
767 return mmio_write64_native(mmio, offset, value);
size_t ocxl_mmio_size(ocxl_afu_h afu, ocxl_mmio_type type)
Get the size of an MMIO region for an AFU.
ocxl_err ocxl_mmio_write64(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint64_t value)
Convert endianness and write a 64-bit value to an AFU's MMIO region.
int ocxl_mmio_get_fd(ocxl_afu_h afu, ocxl_mmio_type type)
Get a file descriptor for an MMIO area of an AFU.
ocxl_err ocxl_mmio_map_advanced(ocxl_afu_h afu, ocxl_mmio_type type, size_t size, int prot, uint64_t flags, off_t offset, ocxl_mmio_h *region)
Map an MMIO area of an AFU.
ocxl_err ocxl_mmio_read64(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint64_t *out)
Read a 64-bit value from an AFU's MMIO region & convert endianness.
void ocxl_mmio_unmap(ocxl_mmio_h region)
Unmap an MMIO region from an AFU.
ocxl_err ocxl_mmio_map(ocxl_afu_h afu, ocxl_mmio_type type, ocxl_mmio_h *region)
Map an MMIO area of an AFU.
ocxl_err ocxl_mmio_write32(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint32_t value)
Convert endianness and write a 32-bit value to an AFU's MMIO region.
ocxl_err ocxl_mmio_read32(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint32_t *out)
Read a 32-bit value from an AFU's MMIO region & convert endianness.
ocxl_err ocxl_mmio_get_info(ocxl_mmio_h region, void **address, size_t *size)
Get the address & size of a mapped MMIO region.
ocxl_err global_mmio_open(ocxl_afu *afu)
Open the global MMIO descriptor on an AFU.
ocxl_err
Potential return values from ocxl_* functions.
@ OCXL_NO_DEV
The OpenCAPI device is not available.
@ OCXL_INVALID_ARGS
One or more arguments are invalid.
@ OCXL_OUT_OF_BOUNDS
The action requested falls outside the permitted area.
@ OCXL_OK
The call succeeded.
@ OCXL_NO_MEM
An out of memory error occurred.
@ OCXL_NO_CONTEXT
The call requires an open context on the AFU.
ocxl_mmio_type
Defines the type of an MMIO area.
struct ocxl_afu * ocxl_afu_h
A handle for an AFU.
struct ocxl_mmio_area * ocxl_mmio_h
A handle for an MMIO region on an AFU.
ocxl_endian
Defines the endianness of an AFU MMIO area.
@ OCXL_MMIO_LITTLE_ENDIAN
AFU data is little-endian.
@ OCXL_MMIO_BIG_ENDIAN
AFU data is big-endian.