17#include "libocxl_internal.h"
54 int available_mmio = -1;
57 for (uint16_t mmio = 0; mmio < afu->mmio_count; mmio++) {
58 if (!afu->mmios[mmio].start) {
59 available_mmio = mmio;
64 if (available_mmio == -1) {
65 if (afu->mmio_count == afu->mmio_max_count) {
66 ocxl_err rc = grow_buffer(afu, (
void **)&afu->mmios, &afu->mmio_max_count,
sizeof(ocxl_mmio_area), INITIAL_MMIO_COUNT);
68 errmsg(afu, rc,
"Could not grow MMIO buffer");
73 available_mmio = afu->mmio_count++;
76 afu->mmios[available_mmio].start = addr;
77 afu->mmios[available_mmio].length = size;
78 afu->mmios[available_mmio].type = type;
79 afu->mmios[available_mmio].afu = afu;
81 *handle = &afu->mmios[available_mmio];
83 TRACE(afu,
"Mapped %ld bytes of %s MMIO at %p",
98 char path[PATH_MAX + 1];
99 int length = snprintf(path,
sizeof(path),
"%s/global_mmio_area", afu->sysfs_path);
100 if (length >= (
int)
sizeof(path)) {
102 errmsg(afu, rc,
"global MMIO path truncated");
106 int fd = open(path, O_RDWR | O_CLOEXEC);
109 errmsg(afu, rc,
"Could not open global MMIO '%s': Error %d: %s", path, errno, strerror(errno));
113 afu->global_mmio_fd = fd;
141static ocxl_err global_mmio_map(ocxl_afu *afu,
size_t size,
int prot, uint64_t flags, off_t offset,
144 if (afu->global_mmio.length == 0) {
146 errmsg(afu, rc,
"Cannot map Global MMIO as there is 0 bytes allocated by the AFU");
152 errmsg(afu, rc,
"MMIO flags of 0x%llx is not supported by this version of libocxl", flags);
156 void *addr = mmap(NULL, size, prot, MAP_SHARED, afu->global_mmio_fd, offset);
157 if (addr == MAP_FAILED) {
159 errmsg(afu, rc,
"Could not map global MMIO, %d: %s", errno, strerror(errno));
166 errmsg(afu, rc,
"Could not register global MMIO region");
171 *region = mmio_region;
199static ocxl_err mmio_map(ocxl_afu *afu,
size_t size,
int prot, uint64_t flags, off_t offset,
ocxl_mmio_h *region)
203 errmsg(afu, rc,
"MMIO flags of 0x%llx is not supported by this version of libocxl", flags);
209 errmsg(afu, rc,
"Could not map per-PASID MMIO as the AFU has not been opened");
213 if (!afu->attached) {
215 errmsg(afu, rc,
"Could not map per-PASID MMIO as the AFU has not been attached");
219 void *addr = mmap(NULL, size, prot, MAP_SHARED, afu->fd, offset);
220 if (addr == MAP_FAILED) {
222 errmsg(afu, rc,
"Could not map per-PASID MMIO: %d: %s", errno, strerror(errno));
229 errmsg(afu, rc,
"Could not register global MMIO region", afu->identifier.afu_name);
234 *region = mmio_region;
268 size = afu->per_pasid_mmio.length;
271 size = afu->global_mmio.length;
280 if (offset + size > afu->global_mmio.length) {
282 errmsg(afu, rc,
"Offset(%#x) + size(%#x) of global MMIO map request exceeds available size of %#x",
283 offset, size, afu->global_mmio.length);
286 return global_mmio_map(afu, size, prot, flags, offset, region);
289 if (offset + size > afu->per_pasid_mmio.length) {
291 errmsg(afu, rc,
"Offset(%#x) + size(%#x) of per-pasid MMIO map request exceeds available size of %#x",
292 offset, size, afu->global_mmio.length);
295 return mmio_map(afu, size, prot, flags, offset, region);
298 errmsg(afu, rc,
"Unknown MMIO type %d", type);
335 if (!region->start) {
339 munmap(region->start, region->length);
340 region->start = NULL;
361 return afu->global_mmio_fd;
384 return afu->global_mmio.length;
387 return afu->per_pasid_mmio.length;
410 if (!region->start) {
412 errmsg(region->afu, rc,
"MMIO region has already been unmapped");
416 *address = region->start;
417 *size = region->length;
438 errmsg(NULL, rc,
"MMIO region is invalid");
442 if (!region->start) {
444 errmsg(region->afu, rc,
"MMIO region has already been unmapped");
448 if (offset >= (off_t)(region->length - (size - 1))) {
450 errmsg(region->afu, rc,
"%s MMIO access of 0x%016lx exceeds limit of 0x%016lx",
452 offset, region->length);
479 ocxl_err ret = mmio_check(region, offset, 4);
484 __sync_synchronize();
485 *out = *(
volatile uint32_t *)(region->start + offset);
486 __sync_synchronize();
488 TRACE(region->afu,
"%s MMIO Read32@0x%04lx=0x%08x",
515 ocxl_err ret = mmio_check(region, offset, 8);
520 __sync_synchronize();
521 *out = *(
volatile uint64_t *)(region->start + offset);
522 __sync_synchronize();
524 TRACE(region->afu,
"%s MMIO Read64@0x%04lx=0x%016lx",
551inline static ocxl_err mmio_write32_native(
ocxl_mmio_h region, off_t offset, uint32_t value)
553 ocxl_err ret = mmio_check(region, offset, 4);
558 TRACE(region->afu,
"%s MMIO Write32@0x%04lx=0x%08x",
562 volatile uint32_t *addr = (uint32_t *)(region->start + offset);
564 __sync_synchronize();
566 __sync_synchronize();
589inline static ocxl_err mmio_write64_native(
ocxl_mmio_h region, off_t offset, uint64_t value)
591 ocxl_err ret = mmio_check(region, offset, 8);
596 TRACE(region->afu,
"%s MMIO Write64@0x%04lx=0x%016lx",
600 volatile uint64_t *addr = (uint64_t *)(region->start + offset);
602 __sync_synchronize();
604 __sync_synchronize();
630 ocxl_err ret = mmio_read32_native(mmio, offset, &val);
632 if (UNLIKELY(ret !=
OCXL_OK)) {
674 ocxl_err ret = mmio_read64_native(mmio, offset, &val);
676 if (UNLIKELY(ret !=
OCXL_OK)) {
719 value = htobe32(value);
723 value = htole32(value);
729 return mmio_write32_native(mmio, offset, value);
754 value = htobe64(value);
758 value = htole64(value);
764 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.