libocxl
libocxl.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 International Business Machines
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _LIBOCXL_H
18 #define _LIBOCXL_H
19 
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <limits.h>
23 #include <sys/select.h>
24 #include <sys/mman.h> // Required for PROT_* for MMIO map calls
25 #include <endian.h> // Required for htobe32 & friends in MMIO access wrappers
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define OCXL_NO_MESSAGES 0
32 #define OCXL_ERRORS (1 << 0)
33 #define OCXL_TRACING (1 << 1)
39 typedef enum {
43 } ocxl_endian;
44 
48 typedef enum {
52 
53 #define AFU_NAME_MAX 24
58 typedef struct ocxl_identifier {
59  uint8_t afu_index;
60  const char afu_name[AFU_NAME_MAX + 1];
62 
66 typedef void *ocxl_afu_h;
67 
68 #define OCXL_INVALID_AFU NULL
73 typedef uint16_t ocxl_irq_h;
74 
78 typedef void *ocxl_mmio_h;
79 
80 
84 typedef enum {
85  OCXL_OK = 0,
86  OCXL_NO_MEM = -1,
87  OCXL_NO_DEV = -2,
89  OCXL_NO_IRQ = -4,
95  /* Adding something? Update setup.c: ocxl_err_to_string too */
96 } ocxl_err;
97 
101 typedef enum {
105 
106 
110 typedef struct {
111  uint16_t irq;
112  uint64_t handle;
113  void *info;
114  uint64_t count;
116 
120 typedef struct {
121  void *addr;
122 #ifdef _ARCH_PPC64
123  uint64_t dsisr;
124 #endif
125  uint64_t count;
127 
135 typedef struct ocxl_event {
137  union {
138  ocxl_event_irq irq;
139  ocxl_event_translation_fault translation_fault;
140  uint64_t padding[16];
141  };
142 } ocxl_event;
143 
144 #define OCXL_ATTACH_FLAGS_NONE (0)
145 
146 /* setup.c */
147 void ocxl_enable_messages(uint64_t sources);
148 void ocxl_set_error_message_handler(void (*handler)(ocxl_err error, const char *message));
149 const char *ocxl_err_to_string(ocxl_err err);
150 
151 /* afu.c */
152 /* AFU getters */
154 const char *ocxl_afu_get_device_path(ocxl_afu_h afu);
155 const char *ocxl_afu_get_sysfs_path(ocxl_afu_h afu);
156 void ocxl_afu_get_version(ocxl_afu_h afu, uint8_t *major, uint8_t *minor);
157 uint32_t ocxl_afu_get_pasid(ocxl_afu_h afu);
158 
159 /* AFU operations */
160 ocxl_err ocxl_afu_open_specific(const char *name, const char *physical_function, int16_t afu_index, ocxl_afu_h *afu);
161 ocxl_err ocxl_afu_open_from_dev(const char *path, ocxl_afu_h *afu);
162 ocxl_err ocxl_afu_open(const char *name, ocxl_afu_h *afu);
163 void ocxl_afu_enable_messages(ocxl_afu_h afu, uint64_t sources);
164 void ocxl_afu_set_error_message_handler(ocxl_afu_h afu, void (*handler)(ocxl_afu_h afu, ocxl_err error,
165  const char *message));
167 ocxl_err ocxl_afu_attach(ocxl_afu_h afu, uint64_t flags);
168 
169 /* irq.c */
170 /* AFU IRQ functions */
171 ocxl_err ocxl_irq_alloc(ocxl_afu_h afu, void *info, ocxl_irq_h *irq_handle);
172 uint64_t ocxl_irq_get_handle(ocxl_afu_h afu, ocxl_irq_h irq);
175 int ocxl_afu_event_check_versioned(ocxl_afu_h afu, int timeout, ocxl_event *events, uint16_t event_count,
176  uint16_t event_api_version);
177 
178 
203 #ifndef _DOXYGEN_
204 static
205 #endif
206 inline int ocxl_afu_event_check(ocxl_afu_h afu, int timeout, ocxl_event *events, uint16_t event_count)
207 {
208  uint16_t event_api_version = 0;
209  return ocxl_afu_event_check_versioned(afu, timeout, events, event_count, event_api_version);
210 }
211 
216 /* Platform specific: PPC64 */
217 #ifdef _ARCH_PPC64
218 ocxl_err ocxl_afu_set_ppc64_amr(ocxl_afu_h afu, uint64_t amr);
219 #endif
220 
221 /* mmio.c */
222 ocxl_err ocxl_mmio_map_advanced(ocxl_afu_h afu, ocxl_mmio_type type, size_t size, int prot, uint64_t flags,
223  off_t offset, ocxl_mmio_h *region);
225 void ocxl_mmio_unmap(ocxl_mmio_h region);
227 size_t ocxl_mmio_size(ocxl_afu_h afu, ocxl_mmio_type type);
228 ocxl_err ocxl_mmio_get_info(ocxl_mmio_h region, void **address, size_t *size);
229 ocxl_err ocxl_mmio_read32(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint32_t *out);
230 ocxl_err ocxl_mmio_read64(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint64_t *out);
231 ocxl_err ocxl_mmio_write32(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint32_t value);
232 ocxl_err ocxl_mmio_write64(ocxl_mmio_h mmio, off_t offset, ocxl_endian endian, uint64_t value);
233 
234 #ifdef __cplusplus
235 }
236 #endif
237 #endif /* _LIBOCXL_H */
238 
An AFU IRQ.
Definition: libocxl.h:102
const char * ocxl_err_to_string(ocxl_err err)
Convert an error value to a string.
Definition: setup.c:111
AFU data is little-endian.
Definition: libocxl.h:41
ocxl_err ocxl_afu_set_ppc64_amr(ocxl_afu_h afu, uint64_t amr)
Set the PPC64-specific PSL AMR register value for restricting access to the AFU.
Definition: afu.c:779
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.
Definition: mmio.c:254
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&#39;s MMIO region & convert endianess.
Definition: mmio.c:617
void * info
An opaque pointer associated with the IRQ.
Definition: libocxl.h:113
ocxl_err ocxl_mmio_get_info(ocxl_mmio_h region, void **address, size_t *size)
Get the address & size of a mapped MMIO region.
Definition: mmio.c:386
int ocxl_irq_get_fd(ocxl_afu_h afu, ocxl_irq_h irq)
Get the file descriptor associated with an IRQ.
Definition: irq.c:222
#define AFU_NAME_MAX
The maximum length of an AFU name.
Definition: libocxl.h:53
const ocxl_identifier * ocxl_afu_get_identifier(ocxl_afu_h afu)
Get the identifier of the AFU.
Definition: afu.c:77
int ocxl_afu_get_event_fd(ocxl_afu_h afu)
Get a descriptor that will trigger a poll when an AFU event occurs.
Definition: irq.c:247
ocxl_err
Potential return values from ocxl_* functions.
Definition: libocxl.h:84
size_t ocxl_mmio_size(ocxl_afu_h afu, ocxl_mmio_type type)
Get the size of an MMIO region for an AFU.
Definition: mmio.c:356
void ocxl_set_error_message_handler(void(*handler)(ocxl_err error, const char *message))
Override the default handler for emitting error messages from open calls.
Definition: setup.c:93
uint16_t ocxl_irq_h
A handle for an IRQ on an AFU.
Definition: libocxl.h:73
The action requested falls outside the permitted area.
Definition: libocxl.h:92
The call requires an open context on the AFU.
Definition: libocxl.h:88
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&#39;s MMIO region & convert endianess.
Definition: mmio.c:657
void * addr
The address that triggered the fault.
Definition: libocxl.h:121
void ocxl_mmio_unmap(ocxl_mmio_h region)
Unmap an MMIO region from an AFU.
Definition: mmio.c:303
uint32_t ocxl_afu_get_pasid(ocxl_afu_h afu)
Get the PASID for the currently open context.
Definition: afu.c:60
ocxl_err ocxl_mmio_map(ocxl_afu_h afu, ocxl_mmio_type type, ocxl_mmio_h *region)
Map an MMIO area of an AFU.
Definition: mmio.c:291
void * ocxl_mmio_h
A handle for an MMIO region on an AFU.
Definition: libocxl.h:78
One or more arguments are invalid.
Definition: libocxl.h:94
an internal error has occurred
Definition: libocxl.h:90
uint8_t afu_index
The AFU Index.
Definition: libocxl.h:59
ocxl_err ocxl_irq_alloc(ocxl_afu_h afu, void *info, ocxl_irq_h *irq_handle)
Allocate an IRQ for an open AFU.
Definition: irq.c:166
ocxl_err ocxl_afu_open(const char *name, ocxl_afu_h *afu)
Open an AFU context with a specified name.
Definition: afu.c:640
ocxl_endian
Defines the endianess of an AFU MMIO area.
Definition: libocxl.h:39
uint64_t handle
The 64 bit handle of the triggered IRQ.
Definition: libocxl.h:112
const char * ocxl_afu_get_device_path(ocxl_afu_h afu)
Get the canonical device path of the AFU.
Definition: afu.c:95
The OpenCAPI device is not available.
Definition: libocxl.h:87
ocxl_event_type type
Definition: libocxl.h:136
The data for a triggered IRQ event.
Definition: libocxl.h:110
A memory translation fault occurred on the AFU.
Definition: libocxl.h:103
uint64_t dsisr
The value of the PPC64 specific DSISR (Data storage interrupt status register)
Definition: libocxl.h:123
void ocxl_afu_enable_messages(ocxl_afu_h afu, uint64_t sources)
Enable messages from an AFU.
Definition: afu.c:161
AFU identification information.
Definition: libocxl.h:58
ocxl_err ocxl_afu_open_from_dev(const char *path, ocxl_afu_h *afu)
Open an AFU context at a specified path.
Definition: afu.c:541
void ocxl_enable_messages(uint64_t sources)
Enable messages from libocxl open calls.
Definition: setup.c:70
void ocxl_afu_get_version(ocxl_afu_h afu, uint8_t *major, uint8_t *minor)
Get the version of the AFU.
Definition: afu.c:129
const char * ocxl_afu_get_sysfs_path(ocxl_afu_h afu)
Get the canonical sysfs path of the AFU.
Definition: afu.c:111
no further interrupts are available, or the interrupt is invalid
Definition: libocxl.h:89
uint64_t ocxl_irq_get_handle(ocxl_afu_h afu, ocxl_irq_h irq)
Get the 64 bit IRQ handle for an IRQ.
Definition: irq.c:201
ocxl_mmio_type
Defines the type of an MMIO area.
Definition: libocxl.h:48
ocxl_err ocxl_afu_close(ocxl_afu_h afu)
Close an AFU and detach it from the context.
Definition: afu.c:701
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&#39;s MMIO region.
Definition: mmio.c:697
int ocxl_afu_event_check(ocxl_afu_h afu, int timeout, ocxl_event *events, uint16_t event_count)
Check for pending IRQs and other events.
Definition: libocxl.h:206
AFU data is big-endian.
Definition: libocxl.h:40
void * ocxl_afu_h
A handle for an AFU.
Definition: libocxl.h:66
AFU data is the same endianess as the host.
Definition: libocxl.h:42
An out of memory error occurred.
Definition: libocxl.h:86
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&#39;s MMIO region.
Definition: mmio.c:732
int ocxl_mmio_get_fd(ocxl_afu_h afu, ocxl_mmio_type type)
Get a file descriptor for an MMIO area of an AFU.
Definition: mmio.c:329
An OCXL event.
Definition: libocxl.h:135
void ocxl_afu_set_error_message_handler(ocxl_afu_h afu, void(*handler)(ocxl_afu_h afu, ocxl_err error, const char *message))
Override the default handler for emitting error messages for an AFU.
Definition: afu.c:187
int ocxl_afu_event_check_versioned(ocxl_afu_h afu, int timeout, ocxl_event *events, uint16_t event_count, uint16_t event_api_version)
Check for pending IRQs and other events.
Definition: irq.c:407
The data for a triggered translation fault error event.
Definition: libocxl.h:120
No more contexts can be opened on the AFU.
Definition: libocxl.h:93
ocxl_err ocxl_afu_attach(ocxl_afu_h afu, uint64_t flags)
The action requested has already been performed.
Definition: libocxl.h:91
uint16_t irq
The IRQ number of the AFU.
Definition: libocxl.h:111
ocxl_err ocxl_afu_open_specific(const char *name, const char *physical_function, int16_t afu_index, ocxl_afu_h *afu)
Open an AFU context with a specified name on a specific card/afu index.
Definition: afu.c:573
uint64_t count
The number of times the interrupt has been triggered since last checked.
Definition: libocxl.h:114
ocxl_event_type
OCXL Event types.
Definition: libocxl.h:101
The call succeeded.
Definition: libocxl.h:85