Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2021 Intel Corporation
3 : : */
4 : :
5 : : #include <bus_pci_driver.h>
6 : : #include <rte_devargs.h>
7 : : #include <rte_dmadev_pmd.h>
8 : : #include <rte_malloc.h>
9 : :
10 : : #include "idxd_internal.h"
11 : :
12 : : #define IDXD_VENDOR_ID 0x8086
13 : : #define IDXD_DEVICE_ID_SPR 0x0B25
14 : :
15 : : #define IDXD_PMD_DMADEV_NAME_PCI dmadev_idxd_pci
16 : :
17 : : const struct rte_pci_id pci_id_idxd_map[] = {
18 : : { RTE_PCI_DEVICE(IDXD_VENDOR_ID, IDXD_DEVICE_ID_SPR) },
19 : : { .vendor_id = 0, /* sentinel */ },
20 : : };
21 : :
22 : : static inline int
23 : 0 : idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
24 : : {
25 : : uint32_t err_code;
26 : 0 : uint16_t qid = idxd->qid;
27 : : int i = 0;
28 : :
29 [ # # ]: 0 : if (command >= idxd_disable_wq && command <= idxd_reset_wq)
30 : 0 : qid = (1 << qid);
31 : 0 : rte_spinlock_lock(&idxd->u.pci->lk);
32 : 0 : idxd->u.pci->regs->cmd = (command << IDXD_CMD_SHIFT) | qid;
33 : :
34 : : do {
35 : : rte_pause();
36 : 0 : err_code = idxd->u.pci->regs->cmdstatus;
37 [ # # ]: 0 : if (++i >= 1000) {
38 : 0 : IDXD_PMD_ERR("Timeout waiting for command response from HW");
39 : 0 : rte_spinlock_unlock(&idxd->u.pci->lk);
40 : 0 : err_code &= CMDSTATUS_ERR_MASK;
41 : 0 : return err_code;
42 : : }
43 [ # # ]: 0 : } while (err_code & CMDSTATUS_ACTIVE_MASK);
44 : 0 : rte_spinlock_unlock(&idxd->u.pci->lk);
45 : :
46 : 0 : err_code &= CMDSTATUS_ERR_MASK;
47 : 0 : return err_code;
48 : : }
49 : :
50 : : static uint32_t *
51 : : idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
52 : : {
53 : 0 : return RTE_PTR_ADD(pci->wq_regs_base,
54 : : (uintptr_t)wq_idx << (5 + pci->wq_cfg_sz));
55 : : }
56 : :
57 : : static int
58 : : idxd_is_wq_enabled(struct idxd_dmadev *idxd)
59 : : {
60 : 0 : uint32_t state = idxd_get_wq_cfg(idxd->u.pci, idxd->qid)[wq_state_idx];
61 : 0 : return ((state >> WQ_STATE_SHIFT) & WQ_STATE_MASK) == 0x1;
62 : : }
63 : :
64 : : static int
65 : 0 : idxd_pci_dev_stop(struct rte_dma_dev *dev)
66 : : {
67 : 0 : struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
68 : : uint8_t err_code;
69 : :
70 [ # # ]: 0 : if (!idxd_is_wq_enabled(idxd)) {
71 : 0 : IDXD_PMD_ERR("Work queue %d already disabled", idxd->qid);
72 : 0 : return 0;
73 : : }
74 : :
75 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_disable_wq);
76 [ # # # # ]: 0 : if (err_code || idxd_is_wq_enabled(idxd)) {
77 : 0 : IDXD_PMD_ERR("Failed disabling work queue %d, error code: %#x",
78 : : idxd->qid, err_code);
79 [ # # ]: 0 : return err_code == 0 ? -1 : -err_code;
80 : : }
81 : 0 : IDXD_PMD_DEBUG("Work queue %d disabled OK", idxd->qid);
82 : :
83 : 0 : return 0;
84 : : }
85 : :
86 : : static int
87 : 0 : idxd_pci_dev_start(struct rte_dma_dev *dev)
88 : : {
89 : 0 : struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
90 : : uint8_t err_code;
91 : :
92 [ # # ]: 0 : if (idxd_is_wq_enabled(idxd)) {
93 : 0 : IDXD_PMD_WARN("WQ %d already enabled", idxd->qid);
94 : 0 : return 0;
95 : : }
96 : :
97 [ # # ]: 0 : if (idxd->desc_ring == NULL) {
98 : 0 : IDXD_PMD_ERR("WQ %d has not been fully configured", idxd->qid);
99 : 0 : return -EINVAL;
100 : : }
101 : :
102 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_enable_wq);
103 [ # # # # ]: 0 : if (err_code || !idxd_is_wq_enabled(idxd)) {
104 : 0 : IDXD_PMD_ERR("Failed enabling work queue %d, error code: %#x",
105 : : idxd->qid, err_code);
106 [ # # ]: 0 : return err_code == 0 ? -1 : -err_code;
107 : : }
108 : 0 : IDXD_PMD_DEBUG("Work queue %d enabled OK", idxd->qid);
109 : :
110 : 0 : return 0;
111 : : }
112 : :
113 : : static int
114 : 0 : idxd_pci_dev_close(struct rte_dma_dev *dev)
115 : : {
116 : 0 : struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
117 : : uint8_t err_code;
118 : : int is_last_wq;
119 : :
120 [ # # ]: 0 : if (idxd_is_wq_enabled(idxd)) {
121 : : /* disable the wq */
122 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_disable_wq);
123 [ # # ]: 0 : if (err_code) {
124 : 0 : IDXD_PMD_ERR("Error disabling wq: code %#x", err_code);
125 : 0 : return err_code;
126 : : }
127 : 0 : IDXD_PMD_DEBUG("IDXD WQ disabled OK");
128 : : }
129 : :
130 : : /* free device memory */
131 : 0 : IDXD_PMD_DEBUG("Freeing device driver memory");
132 : 0 : rte_free(idxd->batch_comp_ring);
133 : 0 : rte_free(idxd->desc_ring);
134 : :
135 : : /* if this is the last WQ on the device, disable the device and free
136 : : * the PCI struct
137 : : */
138 : : /* NOTE: review for potential ordering optimization */
139 : 0 : is_last_wq = (__atomic_fetch_sub(&idxd->u.pci->ref_count, 1, __ATOMIC_SEQ_CST) == 1);
140 [ # # ]: 0 : if (is_last_wq) {
141 : : /* disable the device */
142 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_disable_dev);
143 [ # # ]: 0 : if (err_code) {
144 : 0 : IDXD_PMD_ERR("Error disabling device: code %#x", err_code);
145 : 0 : return err_code;
146 : : }
147 : 0 : IDXD_PMD_DEBUG("IDXD device disabled OK");
148 : 0 : rte_free(idxd->u.pci);
149 : : }
150 : :
151 : : return 0;
152 : : }
153 : :
154 : : static const struct rte_dma_dev_ops idxd_pci_ops = {
155 : : .dev_close = idxd_pci_dev_close,
156 : : .dev_dump = idxd_dump,
157 : : .dev_configure = idxd_configure,
158 : : .vchan_setup = idxd_vchan_setup,
159 : : .dev_info_get = idxd_info_get,
160 : : .stats_get = idxd_stats_get,
161 : : .stats_reset = idxd_stats_reset,
162 : : .dev_start = idxd_pci_dev_start,
163 : : .dev_stop = idxd_pci_dev_stop,
164 : : .vchan_status = idxd_vchan_status,
165 : : };
166 : :
167 : : /* each portal uses 4 x 4k pages */
168 : : #define IDXD_PORTAL_SIZE (4096 * 4)
169 : :
170 : : static int
171 : 0 : init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
172 : : unsigned int max_queues)
173 : : {
174 : : struct idxd_pci_common *pci;
175 : : uint8_t nb_groups, nb_engines, nb_wqs;
176 : : uint16_t grp_offset, wq_offset; /* how far into bar0 the regs are */
177 : : uint16_t wq_size, total_wq_size;
178 : : uint8_t lg2_max_batch, lg2_max_copy_size;
179 : : unsigned int i, err_code;
180 : :
181 : 0 : pci = rte_malloc(NULL, sizeof(*pci), 0);
182 [ # # ]: 0 : if (pci == NULL) {
183 : 0 : IDXD_PMD_ERR("%s: Can't allocate memory", __func__);
184 : : err_code = -1;
185 : 0 : goto err;
186 : : }
187 : : memset(pci, 0, sizeof(*pci));
188 : : rte_spinlock_init(&pci->lk);
189 : :
190 : : /* assign the bar registers, and then configure device */
191 : 0 : pci->regs = dev->mem_resource[0].addr;
192 : 0 : grp_offset = (uint16_t)pci->regs->offsets[0];
193 : 0 : pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
194 : 0 : wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
195 : 0 : pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
196 : 0 : pci->portals = dev->mem_resource[2].addr;
197 : 0 : pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
198 : :
199 : : /* reset */
200 : 0 : idxd->u.pci = pci;
201 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_reset_device);
202 [ # # ]: 0 : if (err_code) {
203 : 0 : IDXD_PMD_ERR("Error reset device: code %#x", err_code);
204 : 0 : goto err;
205 : : }
206 : :
207 : : /* sanity check device status */
208 [ # # ]: 0 : if (pci->regs->gensts & GENSTS_DEV_STATE_MASK) {
209 : : /* need function-level-reset (FLR) or is enabled */
210 : 0 : IDXD_PMD_ERR("Device status is not disabled, cannot init");
211 : : err_code = -1;
212 : 0 : goto err;
213 : : }
214 [ # # ]: 0 : if (pci->regs->cmdstatus & CMDSTATUS_ACTIVE_MASK) {
215 : : /* command in progress */
216 : 0 : IDXD_PMD_ERR("Device has a command in progress, cannot init");
217 : : err_code = -1;
218 : 0 : goto err;
219 : : }
220 : :
221 : : /* read basic info about the hardware for use when configuring */
222 : 0 : nb_groups = (uint8_t)pci->regs->grpcap;
223 : 0 : nb_engines = (uint8_t)pci->regs->engcap;
224 : 0 : nb_wqs = (uint8_t)(pci->regs->wqcap >> 16);
225 : 0 : total_wq_size = (uint16_t)pci->regs->wqcap;
226 : 0 : lg2_max_copy_size = (uint8_t)(pci->regs->gencap >> 16) & 0x1F;
227 : 0 : lg2_max_batch = (uint8_t)(pci->regs->gencap >> 21) & 0x0F;
228 : :
229 : 0 : IDXD_PMD_DEBUG("nb_groups = %u, nb_engines = %u, nb_wqs = %u",
230 : : nb_groups, nb_engines, nb_wqs);
231 : :
232 : : /* zero out any old config */
233 [ # # ]: 0 : for (i = 0; i < nb_groups; i++) {
234 : 0 : pci->grp_regs[i].grpengcfg = 0;
235 : 0 : pci->grp_regs[i].grpwqcfg[0] = 0;
236 : : }
237 [ # # ]: 0 : for (i = 0; i < nb_wqs; i++)
238 : 0 : idxd_get_wq_cfg(pci, i)[0] = 0;
239 : :
240 : : /* limit queues if necessary */
241 [ # # # # ]: 0 : if (max_queues != 0 && nb_wqs > max_queues) {
242 : 0 : nb_wqs = max_queues;
243 [ # # ]: 0 : if (nb_engines > max_queues)
244 : : nb_engines = max_queues;
245 [ # # ]: 0 : if (nb_groups > max_queues)
246 : : nb_engines = max_queues;
247 : 0 : IDXD_PMD_DEBUG("Limiting queues to %u", nb_wqs);
248 : : }
249 : :
250 : : /* put each engine into a separate group to avoid reordering */
251 : : if (nb_groups > nb_engines)
252 : : nb_groups = nb_engines;
253 : : if (nb_groups < nb_engines)
254 : : nb_engines = nb_groups;
255 : :
256 : : /* assign engines to groups, round-robin style */
257 [ # # ]: 0 : for (i = 0; i < nb_engines; i++) {
258 : 0 : IDXD_PMD_DEBUG("Assigning engine %u to group %u",
259 : : i, i % nb_groups);
260 : 0 : pci->grp_regs[i % nb_groups].grpengcfg |= (1ULL << i);
261 : : }
262 : :
263 : : /* now do the same for queues and give work slots to each queue */
264 : 0 : wq_size = total_wq_size / nb_wqs;
265 : 0 : IDXD_PMD_DEBUG("Work queue size = %u, max batch = 2^%u, max copy = 2^%u",
266 : : wq_size, lg2_max_batch, lg2_max_copy_size);
267 [ # # ]: 0 : for (i = 0; i < nb_wqs; i++) {
268 : : /* add engine "i" to a group */
269 : 0 : IDXD_PMD_DEBUG("Assigning work queue %u to group %u",
270 : : i, i % nb_groups);
271 : 0 : pci->grp_regs[i % nb_groups].grpwqcfg[0] |= (1ULL << i);
272 : : /* now configure it, in terms of size, max batch, mode */
273 : 0 : idxd_get_wq_cfg(pci, i)[wq_size_idx] = wq_size;
274 : 0 : idxd_get_wq_cfg(pci, i)[wq_mode_idx] = (1 << WQ_PRIORITY_SHIFT) |
275 : : WQ_MODE_DEDICATED;
276 : 0 : idxd_get_wq_cfg(pci, i)[wq_sizes_idx] = lg2_max_copy_size |
277 : 0 : (lg2_max_batch << WQ_BATCH_SZ_SHIFT);
278 : : }
279 : :
280 : : /* dump the group configuration to output */
281 [ # # ]: 0 : for (i = 0; i < nb_groups; i++) {
282 : 0 : IDXD_PMD_DEBUG("## Group %d", i);
283 : 0 : IDXD_PMD_DEBUG(" GRPWQCFG: %"PRIx64, pci->grp_regs[i].grpwqcfg[0]);
284 : 0 : IDXD_PMD_DEBUG(" GRPENGCFG: %"PRIx64, pci->grp_regs[i].grpengcfg);
285 : 0 : IDXD_PMD_DEBUG(" GRPFLAGS: %"PRIx32, pci->grp_regs[i].grpflags);
286 : : }
287 : :
288 : 0 : idxd->u.pci = pci;
289 : 0 : idxd->max_batches = wq_size;
290 : 0 : idxd->max_batch_size = 1 << lg2_max_batch;
291 : :
292 : : /* enable the device itself */
293 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_enable_dev);
294 [ # # ]: 0 : if (err_code) {
295 : 0 : IDXD_PMD_ERR("Error enabling device: code %#x", err_code);
296 : 0 : goto err;
297 : : }
298 : 0 : IDXD_PMD_DEBUG("IDXD Device enabled OK");
299 : :
300 : 0 : return nb_wqs;
301 : :
302 : 0 : err:
303 : 0 : free(pci);
304 : 0 : return err_code;
305 : : }
306 : :
307 : : static int
308 : 0 : idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
309 : : {
310 : 0 : struct idxd_dmadev idxd = {0};
311 : : uint8_t nb_wqs;
312 : : int qid, ret = 0;
313 : : char name[PCI_PRI_STR_SIZE];
314 : 0 : unsigned int max_queues = 0;
315 : :
316 : 0 : rte_pci_device_name(&dev->addr, name, sizeof(name));
317 : 0 : IDXD_PMD_INFO("Init %s on NUMA node %d", name, dev->device.numa_node);
318 : 0 : dev->device.driver = &drv->driver;
319 : :
320 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
321 : : char qname[32];
322 : : int max_qid;
323 : :
324 : : /* look up queue 0 to get the PCI structure */
325 : : snprintf(qname, sizeof(qname), "%s-q0", name);
326 : 0 : IDXD_PMD_INFO("Looking up %s\n", qname);
327 : 0 : ret = idxd_dmadev_create(qname, &dev->device, NULL, &idxd_pci_ops);
328 [ # # ]: 0 : if (ret != 0) {
329 : 0 : IDXD_PMD_ERR("Failed to create dmadev %s", name);
330 : 0 : return ret;
331 : : }
332 : 0 : qid = rte_dma_get_dev_id_by_name(qname);
333 : 0 : max_qid = __atomic_load_n(
334 : 0 : &((struct idxd_dmadev *)rte_dma_fp_objs[qid].dev_private)->u.pci->ref_count,
335 : : __ATOMIC_SEQ_CST);
336 : :
337 : : /* we have queue 0 done, now configure the rest of the queues */
338 [ # # ]: 0 : for (qid = 1; qid < max_qid; qid++) {
339 : : /* add the queue number to each device name */
340 : : snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
341 : 0 : IDXD_PMD_INFO("Looking up %s\n", qname);
342 : 0 : ret = idxd_dmadev_create(qname, &dev->device, NULL, &idxd_pci_ops);
343 [ # # ]: 0 : if (ret != 0) {
344 : 0 : IDXD_PMD_ERR("Failed to create dmadev %s", name);
345 : 0 : return ret;
346 : : }
347 : : }
348 : : return 0;
349 : : }
350 : :
351 [ # # # # ]: 0 : if (dev->device.devargs && dev->device.devargs->args[0] != '\0') {
352 : : /* if the number of devargs grows beyond just 1, use rte_kvargs */
353 [ # # ]: 0 : if (sscanf(dev->device.devargs->args,
354 : : "max_queues=%u", &max_queues) != 1) {
355 : 0 : IDXD_PMD_ERR("Invalid device parameter: '%s'",
356 : : dev->device.devargs->args);
357 : 0 : return -1;
358 : : }
359 : : }
360 : :
361 : 0 : ret = init_pci_device(dev, &idxd, max_queues);
362 [ # # ]: 0 : if (ret < 0) {
363 : 0 : IDXD_PMD_ERR("Error initializing PCI hardware");
364 : 0 : return ret;
365 : : }
366 [ # # ]: 0 : if (idxd.u.pci->portals == NULL) {
367 : 0 : IDXD_PMD_ERR("Error, invalid portal assigned during initialization\n");
368 : 0 : free(idxd.u.pci);
369 : 0 : return -EINVAL;
370 : : }
371 : : nb_wqs = (uint8_t)ret;
372 : :
373 : : /* set up one device for each queue */
374 [ # # ]: 0 : for (qid = 0; qid < nb_wqs; qid++) {
375 : : char qname[32];
376 : :
377 : : /* add the queue number to each device name */
378 : : snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
379 : 0 : idxd.qid = qid;
380 : 0 : idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
381 : : qid * IDXD_PORTAL_SIZE);
382 [ # # ]: 0 : if (idxd_is_wq_enabled(&idxd))
383 : 0 : IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
384 : 0 : ret = idxd_dmadev_create(qname, &dev->device,
385 : : &idxd, &idxd_pci_ops);
386 [ # # ]: 0 : if (ret != 0) {
387 : 0 : IDXD_PMD_ERR("Failed to create dmadev %s", name);
388 [ # # ]: 0 : if (qid == 0) /* if no devices using this, free pci */
389 : 0 : free(idxd.u.pci);
390 : 0 : return ret;
391 : : }
392 : 0 : __atomic_fetch_add(&idxd.u.pci->ref_count, 1, __ATOMIC_SEQ_CST);
393 : : }
394 : :
395 : : return 0;
396 : : }
397 : :
398 : : static int
399 : 0 : idxd_dmadev_destroy(const char *name)
400 : : {
401 : : int ret = 0;
402 : :
403 : : /* rte_dma_close is called by pmd_release */
404 : 0 : ret = rte_dma_pmd_release(name);
405 [ # # ]: 0 : if (ret)
406 : 0 : IDXD_PMD_DEBUG("Device cleanup failed");
407 : :
408 : 0 : return ret;
409 : : }
410 : :
411 : : static int
412 : 0 : idxd_dmadev_remove_pci(struct rte_pci_device *dev)
413 : : {
414 : : int i = 0;
415 : : char name[PCI_PRI_STR_SIZE];
416 : :
417 : 0 : rte_pci_device_name(&dev->addr, name, sizeof(name));
418 : :
419 : 0 : IDXD_PMD_INFO("Closing %s on NUMA node %d", name, dev->device.numa_node);
420 : :
421 [ # # ]: 0 : RTE_DMA_FOREACH_DEV(i) {
422 : : struct rte_dma_info info;
423 : 0 : rte_dma_info_get(i, &info);
424 [ # # ]: 0 : if (strncmp(name, info.dev_name, strlen(name)) == 0)
425 : 0 : idxd_dmadev_destroy(info.dev_name);
426 : : }
427 : :
428 : 0 : return 0;
429 : : }
430 : :
431 : : struct rte_pci_driver idxd_pmd_drv_pci = {
432 : : .id_table = pci_id_idxd_map,
433 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
434 : : .probe = idxd_dmadev_probe_pci,
435 : : .remove = idxd_dmadev_remove_pci,
436 : : };
437 : :
438 : 235 : RTE_PMD_REGISTER_PCI(IDXD_PMD_DMADEV_NAME_PCI, idxd_pmd_drv_pci);
439 : : RTE_PMD_REGISTER_PCI_TABLE(IDXD_PMD_DMADEV_NAME_PCI, pci_id_idxd_map);
440 : : RTE_PMD_REGISTER_KMOD_DEP(IDXD_PMD_DMADEV_NAME_PCI, "vfio-pci");
441 : : RTE_PMD_REGISTER_PARAM_STRING(dmadev_idxd_pci, "max_queues=0");
|