1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
8 #include "ifpga_feature_dev.h"
11 * Enable Port by clear the port soft reset bit, which is set by default.
12 * The AFU is unable to respond to any MMIO access while in reset.
13 * __fpga_port_enable function should only be used after __fpga_port_disable
16 void __fpga_port_enable(struct ifpga_port_hw *port)
18 struct feature_port_header *port_hdr;
19 struct feature_port_control control;
21 WARN_ON(!port->disable_count);
23 if (--port->disable_count != 0)
26 port_hdr = get_port_feature_ioaddr_by_index(port,
27 PORT_FEATURE_ID_HEADER);
30 control.csr = readq(&port_hdr->control);
31 control.port_sftrst = 0x0;
32 writeq(control.csr, &port_hdr->control);
35 int __fpga_port_disable(struct ifpga_port_hw *port)
37 struct feature_port_header *port_hdr;
38 struct feature_port_control control;
40 if (port->disable_count++ != 0)
43 port_hdr = get_port_feature_ioaddr_by_index(port,
44 PORT_FEATURE_ID_HEADER);
47 /* Set port soft reset */
48 control.csr = readq(&port_hdr->control);
49 control.port_sftrst = 0x1;
50 writeq(control.csr, &port_hdr->control);
53 * HW sets ack bit to 1 when all outstanding requests have been drained
54 * on this port and minimum soft reset pulse width has elapsed.
55 * Driver polls port_soft_reset_ack to determine if reset done by HW.
57 control.port_sftrst_ack = 1;
59 if (fpga_wait_register_field(port_sftrst_ack, control,
60 &port_hdr->control, RST_POLL_TIMEOUT,
62 dev_err(port, "timeout, fail to reset device\n");
69 int fpga_get_afu_uuid(struct ifpga_port_hw *port, struct uuid *uuid)
71 struct feature_port_header *port_hdr;
77 port_hdr = get_port_feature_ioaddr_by_index(port, PORT_FEATURE_ID_UAFU);
79 spinlock_lock(&port->lock);
80 guidl = readq(&port_hdr->afu_header.guid.b[0]);
81 guidh = readq(&port_hdr->afu_header.guid.b[8]);
82 spinlock_unlock(&port->lock);
84 opae_memcpy(uuid->b, &guidl, sizeof(u64));
85 opae_memcpy(uuid->b + 8, &guidh, sizeof(u64));
90 /* Mask / Unmask Port Errors by the Error Mask register. */
91 void port_err_mask(struct ifpga_port_hw *port, bool mask)
93 struct feature_port_error *port_err;
94 struct feature_port_err_key err_mask;
96 port_err = get_port_feature_ioaddr_by_index(port,
97 PORT_FEATURE_ID_ERROR);
100 err_mask.csr = PORT_ERR_MASK;
104 writeq(err_mask.csr, &port_err->error_mask);
107 /* Clear All Port Errors. */
108 int port_err_clear(struct ifpga_port_hw *port, u64 err)
110 struct feature_port_header *port_hdr;
111 struct feature_port_error *port_err;
112 struct feature_port_err_key mask;
113 struct feature_port_first_err_key first;
114 struct feature_port_status status;
117 port_err = get_port_feature_ioaddr_by_index(port,
118 PORT_FEATURE_ID_ERROR);
119 port_hdr = get_port_feature_ioaddr_by_index(port,
120 PORT_FEATURE_ID_HEADER);
123 * Clear All Port Errors
125 * - Check for AP6 State
126 * - Halt Port by keeping Port in reset
127 * - Set PORT Error mask to all 1 to mask errors
129 * - Set Port mask to all 0 to enable errors
130 * - All errors start capturing new errors
131 * - Enable Port by pulling the port out of reset
134 /* If device is still in AP6 state, can not clear any error.*/
135 status.csr = readq(&port_hdr->status);
136 if (status.power_state == PORT_POWER_STATE_AP6) {
137 dev_err(dev, "Could not clear errors, device in AP6 state.\n");
141 /* Halt Port by keeping Port in reset */
142 ret = __fpga_port_disable(port);
146 /* Mask all errors */
147 port_err_mask(port, true);
149 /* Clear errors if err input matches with current port errors.*/
150 mask.csr = readq(&port_err->port_error);
152 if (mask.csr == err) {
153 writeq(mask.csr, &port_err->port_error);
155 first.csr = readq(&port_err->port_first_error);
156 writeq(first.csr, &port_err->port_first_error);
162 port_err_mask(port, false);
164 /* Enable the Port by clear the reset */
165 __fpga_port_enable(port);
170 int port_clear_error(struct ifpga_port_hw *port)
172 struct feature_port_error *port_err;
173 struct feature_port_err_key error;
175 port_err = get_port_feature_ioaddr_by_index(port,
176 PORT_FEATURE_ID_ERROR);
177 error.csr = readq(&port_err->port_error);
179 dev_info(port, "read port error: 0x%lx\n", (unsigned long)error.csr);
181 return port_err_clear(port, error.csr);
184 static struct feature_driver fme_feature_drvs[] = {
185 {FEATURE_DRV(FME_FEATURE_ID_HEADER, FME_FEATURE_HEADER,
187 {FEATURE_DRV(FME_FEATURE_ID_THERMAL_MGMT, FME_FEATURE_THERMAL_MGMT,
188 &fme_thermal_mgmt_ops),},
189 {FEATURE_DRV(FME_FEATURE_ID_POWER_MGMT, FME_FEATURE_POWER_MGMT,
190 &fme_power_mgmt_ops),},
191 {FEATURE_DRV(FME_FEATURE_ID_GLOBAL_ERR, FME_FEATURE_GLOBAL_ERR,
192 &fme_global_err_ops),},
193 {FEATURE_DRV(FME_FEATURE_ID_PR_MGMT, FME_FEATURE_PR_MGMT,
195 {FEATURE_DRV(FME_FEATURE_ID_GLOBAL_DPERF, FME_FEATURE_GLOBAL_DPERF,
196 &fme_global_dperf_ops),},
197 {FEATURE_DRV(FME_FEATURE_ID_HSSI_ETH, FME_FEATURE_HSSI_ETH,
198 &fme_hssi_eth_ops),},
199 {FEATURE_DRV(FME_FEATURE_ID_EMIF_MGMT, FME_FEATURE_EMIF_MGMT,
201 {FEATURE_DRV(FME_FEATURE_ID_MAX10_SPI, FME_FEATURE_MAX10_SPI,
202 &fme_spi_master_ops),},
203 {FEATURE_DRV(FME_FEATURE_ID_NIOS_SPI, FME_FEATURE_NIOS_SPI,
204 &fme_nios_spi_master_ops),},
205 {FEATURE_DRV(FME_FEATURE_ID_I2C_MASTER, FME_FEATURE_I2C_MASTER,
206 &fme_i2c_master_ops),},
207 {FEATURE_DRV(FME_FEATURE_ID_ETH_GROUP, FME_FEATURE_ETH_GROUP,
208 &fme_eth_group_ops),},
209 {0, NULL, NULL}, /* end of arrary */
212 static struct feature_driver port_feature_drvs[] = {
213 {FEATURE_DRV(PORT_FEATURE_ID_HEADER, PORT_FEATURE_HEADER,
214 &ifpga_rawdev_port_hdr_ops)},
215 {FEATURE_DRV(PORT_FEATURE_ID_ERROR, PORT_FEATURE_ERR,
216 &ifpga_rawdev_port_error_ops)},
217 {FEATURE_DRV(PORT_FEATURE_ID_UINT, PORT_FEATURE_UINT,
218 &ifpga_rawdev_port_uint_ops)},
219 {FEATURE_DRV(PORT_FEATURE_ID_STP, PORT_FEATURE_STP,
220 &ifpga_rawdev_port_stp_ops)},
221 {FEATURE_DRV(PORT_FEATURE_ID_UAFU, PORT_FEATURE_UAFU,
222 &ifpga_rawdev_port_afu_ops)},
223 {0, NULL, NULL}, /* end of array */
226 const char *get_fme_feature_name(unsigned int id)
228 struct feature_driver *drv = fme_feature_drvs;
240 const char *get_port_feature_name(unsigned int id)
242 struct feature_driver *drv = port_feature_drvs;
254 static void feature_uinit(struct ifpga_feature_list *list)
256 struct ifpga_feature *feature;
258 TAILQ_FOREACH(feature, list, next) {
259 if (feature->state != IFPGA_FEATURE_ATTACHED)
261 if (feature->ops && feature->ops->uinit)
262 feature->ops->uinit(feature);
266 static int feature_init(struct feature_driver *drv,
267 struct ifpga_feature_list *list)
269 struct ifpga_feature *feature;
273 TAILQ_FOREACH(feature, list, next) {
274 if (feature->state != IFPGA_FEATURE_ATTACHED)
276 if (feature->id == drv->id) {
277 feature->ops = drv->ops;
278 feature->name = drv->name;
279 if (feature->ops->init) {
280 ret = feature->ops->init(feature);
295 int fme_hw_init(struct ifpga_fme_hw *fme)
299 if (fme->state != IFPGA_FME_IMPLEMENTED)
302 ret = feature_init(fme_feature_drvs, &fme->feature_list);
309 void fme_hw_uinit(struct ifpga_fme_hw *fme)
311 feature_uinit(&fme->feature_list);
314 void port_hw_uinit(struct ifpga_port_hw *port)
316 feature_uinit(&port->feature_list);
319 int port_hw_init(struct ifpga_port_hw *port)
323 if (port->state == IFPGA_PORT_UNUSED)
326 ret = feature_init(port_feature_drvs, &port->feature_list);
336 #define FPGA_MAX_MSIX_VEC_COUNT 128
337 /* irq set buffer length for interrupt */
338 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
339 sizeof(int) * FPGA_MAX_MSIX_VEC_COUNT)
341 /* only support msix for now*/
342 static int vfio_msix_enable_block(s32 vfio_dev_fd, unsigned int vec_start,
343 unsigned int count, s32 *fds)
345 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
346 struct vfio_irq_set *irq_set;
350 len = sizeof(irq_set_buf);
352 irq_set = (struct vfio_irq_set *)irq_set_buf;
353 irq_set->argsz = len;
354 /* 0 < irq_set->count < FPGA_MAX_MSIX_VEC_COUNT */
355 irq_set->count = count ?
356 (count > FPGA_MAX_MSIX_VEC_COUNT ?
357 FPGA_MAX_MSIX_VEC_COUNT : count) : 1;
358 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
359 VFIO_IRQ_SET_ACTION_TRIGGER;
360 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
361 irq_set->start = vec_start;
363 fd_ptr = (int *)&irq_set->data;
364 opae_memcpy(fd_ptr, fds, sizeof(int) * count);
366 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
368 printf("Error enabling MSI-X interrupts\n");
373 int fpga_msix_set_block(struct ifpga_feature *feature, unsigned int start,
374 unsigned int count, s32 *fds)
376 struct feature_irq_ctx *ctx = feature->ctx;
380 if (start >= feature->ctx_num || start + count > feature->ctx_num)
383 /* assume that each feature has continuous vector space in msix*/
384 ret = vfio_msix_enable_block(feature->vfio_dev_fd,
385 ctx[start].idx, count, fds);
387 for (i = 0; i < count; i++)
388 ctx[i].eventfd = fds[i];