17 #include "libocxl_internal.h" 20 #include <sys/types.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;
95 char path[PATH_MAX + 1];
96 int length = snprintf(path,
sizeof(path),
"%s/global_mmio_area", afu->sysfs_path);
97 if (length >= (
int)
sizeof(path)) {
99 errmsg(afu, rc,
"global MMIO path truncated");
103 int fd = open(path, O_RDWR | O_CLOEXEC);
106 errmsg(afu, rc,
"Could not open global MMIO '%s': Error %d: %s", path, errno, strerror(errno));
110 afu->global_mmio_fd = fd;
138 static ocxl_err global_mmio_map(ocxl_afu *afu,
size_t size,
int prot, uint64_t flags, off_t offset,
141 if (afu->global_mmio.length == 0) {
143 errmsg(afu, rc,
"Cannot map Global MMIO as there is 0 bytes allocated by the AFU");
149 errmsg(afu, rc,
"MMIO flags of 0x%llx is not supported by this version of libocxl", flags);
154 size = afu->global_mmio.length;
157 void *addr = mmap(NULL, size, prot, MAP_SHARED, afu->global_mmio_fd, offset);
158 if (addr == MAP_FAILED) {
160 errmsg(afu, rc,
"Could not map global MMIO, %d: %s", errno, strerror(errno));
167 errmsg(afu, rc,
"Could not register global MMIO region");
171 *region = mmio_region;
199 static 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 on AFU '%s' as it has not been opened",
210 afu->identifier.afu_name);
214 void *addr = mmap(NULL, afu->per_pasid_mmio.length, prot, MAP_SHARED, afu->fd, offset);
215 if (addr == MAP_FAILED) {
217 errmsg(afu, rc,
"Could not map per-PASID MMIO: %d: %s", errno, strerror(errno));
224 errmsg(afu, rc,
"Could not register global MMIO region", afu->identifier.afu_name);
228 *region = mmio_region;
257 ocxl_afu *my_afu = (ocxl_afu *) afu;
262 return global_mmio_map(my_afu, size, prot, flags, offset, region);
266 return mmio_map(my_afu, size, prot, flags, offset, region);
270 errmsg(my_afu, rc,
"Unknown MMIO type %d", type);
305 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
311 munmap(mmio->start, mmio->length);
331 ocxl_afu *my_afu = (ocxl_afu *) afu;
335 return my_afu->global_mmio_fd;
358 ocxl_afu *my_afu = (ocxl_afu *) afu;
362 return my_afu->global_mmio.length;
365 return my_afu->per_pasid_mmio.length;
388 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
392 errmsg(mmio->afu, rc,
"MMIO region has already been unmapped");
396 *address = mmio->start;
397 *size = mmio->length;
418 errmsg(NULL, rc,
"MMIO region is invalid");
422 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
426 errmsg(mmio->afu, rc,
"MMIO region has already been unmapped");
430 if (offset >= (off_t)(mmio->length - (size - 1))) {
432 errmsg(mmio->afu, rc,
"%s MMIO access of 0x%016lx exceeds limit of 0x%016lx",
434 offset, mmio->length);
461 ocxl_err ret = mmio_check(region, offset, 4);
466 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
468 __sync_synchronize();
469 *out = *(
volatile uint32_t *)(mmio->start + offset);
470 __sync_synchronize();
472 TRACE(mmio->afu,
"%s MMIO Read32@0x%04lx=0x%08x",
499 ocxl_err ret = mmio_check(region, offset, 8);
504 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
506 __sync_synchronize();
507 *out = *(
volatile uint64_t *)(mmio->start + offset);
508 __sync_synchronize();
510 TRACE(mmio->afu,
"%s MMIO Read64@0x%04lx=0x%016lx",
537 inline static ocxl_err mmio_write32_native(
ocxl_mmio_h region, off_t offset, uint32_t value)
539 ocxl_err ret = mmio_check(region, offset, 4);
544 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
546 TRACE(mmio->afu,
"%s MMIO Write32@0x%04lx=0x%08x",
550 volatile uint32_t * addr = (uint32_t *)(mmio->start + offset);
552 __sync_synchronize();
554 __sync_synchronize();
577 inline static ocxl_err mmio_write64_native(
ocxl_mmio_h region, off_t offset, uint64_t value)
579 ocxl_err ret = mmio_check(region, offset, 8);
584 ocxl_mmio_area *mmio = (ocxl_mmio_area *)region;
586 TRACE(mmio->afu,
"%s MMIO Write64@0x%04lx=0x%016lx",
590 volatile uint64_t * addr = (uint64_t *)(mmio->start + offset);
592 __sync_synchronize();
594 __sync_synchronize();
620 ocxl_err ret = mmio_read32_native(mmio, offset, &val);
660 ocxl_err ret = mmio_read64_native(mmio, offset, &val);
701 value = htobe32(value);
705 value = htole32(value);
711 return mmio_write32_native(mmio, offset, value);
736 value = htobe64(value);
740 value = htole64(value);
746 return mmio_write64_native(mmio, offset, value);
AFU data is little-endian.
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_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 endianess.
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
Potential return values from ocxl_* functions.
size_t ocxl_mmio_size(ocxl_afu_h afu, ocxl_mmio_type type)
Get the size of an MMIO region for an AFU.
The action requested falls outside the permitted area.
The call requires an open context on the 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 endianess.
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.
void * ocxl_mmio_h
A handle for an MMIO region on an AFU.
One or more arguments are invalid.
ocxl_endian
Defines the endianess of an AFU MMIO area.
The OpenCAPI device is not available.
ocxl_mmio_type
Defines the type of an MMIO area.
ocxl_err ocxl_mmio_write32(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint32_t value)
Convert endianess and write a 32-bit value to an AFU's MMIO region.
ocxl_err global_mmio_open(ocxl_afu *afu)
Open the global MMIO descriptor on an AFU.
void * ocxl_afu_h
A handle for an AFU.
An out of memory error occurred.
ocxl_err ocxl_mmio_write64(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint64_t value)
Convert endianess 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.