1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Netronome Systems, Inc.
8 * Authors: Vinayak Tammineedi <vinayak.tammineedi@netronome.com>
10 * Multiplexes the NFP BARs between NFP internal resources and
11 * implements the PCIe specific interface for generic CPP bus access.
13 * The BARs are managed and allocated if they are available.
14 * The generic CPP bus abstraction builds upon this BAR interface.
34 #include <rte_ethdev_pci.h>
35 #include <rte_string_fns.h>
38 #include "nfp_target.h"
39 #include "nfp6000/nfp6000.h"
41 #define NFP_PCIE_BAR(_pf) (0x30000 + ((_pf) & 7) * 0xc0)
43 #define NFP_PCIE_BAR_PCIE2CPP_ACTION_BASEADDRESS(_x) (((_x) & 0x1f) << 16)
44 #define NFP_PCIE_BAR_PCIE2CPP_BASEADDRESS(_x) (((_x) & 0xffff) << 0)
45 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT(_x) (((_x) & 0x3) << 27)
46 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_32BIT 0
47 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_64BIT 1
48 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_0BYTE 3
49 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE(_x) (((_x) & 0x7) << 29)
50 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_OF(_x) (((_x) >> 29) & 0x7)
51 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_FIXED 0
52 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_BULK 1
53 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_TARGET 2
54 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_GENERAL 3
55 #define NFP_PCIE_BAR_PCIE2CPP_TARGET_BASEADDRESS(_x) (((_x) & 0xf) << 23)
56 #define NFP_PCIE_BAR_PCIE2CPP_TOKEN_BASEADDRESS(_x) (((_x) & 0x3) << 21)
59 * Minimal size of the PCIe cfg memory we depend on being mapped,
60 * queue controller and DMA controller don't have to be covered.
62 #define NFP_PCI_MIN_MAP_SIZE 0x080000
64 #define NFP_PCIE_P2C_FIXED_SIZE(bar) (1 << (bar)->bitsize)
65 #define NFP_PCIE_P2C_BULK_SIZE(bar) (1 << (bar)->bitsize)
66 #define NFP_PCIE_P2C_GENERAL_TARGET_OFFSET(bar, x) ((x) << ((bar)->bitsize - 2))
67 #define NFP_PCIE_P2C_GENERAL_TOKEN_OFFSET(bar, x) ((x) << ((bar)->bitsize - 4))
68 #define NFP_PCIE_P2C_GENERAL_SIZE(bar) (1 << ((bar)->bitsize - 4))
70 #define NFP_PCIE_CFG_BAR_PCIETOCPPEXPBAR(bar, slot) \
71 (NFP_PCIE_BAR(0) + ((bar) * 8 + (slot)) * 4)
73 #define NFP_PCIE_CPP_BAR_PCIETOCPPEXPBAR(bar, slot) \
74 (((bar) * 8 + (slot)) * 4)
77 * Define to enable a bit more verbose debug output.
78 * Set to 1 to enable a bit more verbose debug output.
81 struct nfp6000_area_priv;
84 * struct nfp_bar - describes BAR configuration and usage
85 * @nfp: backlink to owner
86 * @barcfg: cached contents of BAR config CSR
87 * @base: the BAR's base CPP offset
88 * @mask: mask for the BAR aperture (read only)
89 * @bitsize: bitsize of BAR aperture (read only)
90 * @index: index of the BAR
91 * @lock: lock to specify if bar is in use
92 * @refcnt: number of current users
93 * @iomem: mapped IO memory
97 struct nfp_pcie_user *nfp;
99 uint64_t base; /* CPP address base */
100 uint64_t mask; /* Bit mask of the bar */
101 uint32_t bitsize; /* Bit size of the bar */
110 struct nfp_pcie_user {
111 struct nfp_bar bar[NFP_BAR_MAX];
115 char busdev[BUSDEV_SZ];
121 nfp_bar_maptype(struct nfp_bar *bar)
123 return NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_OF(bar->barcfg);
126 #define TARGET_WIDTH_32 4
127 #define TARGET_WIDTH_64 8
130 nfp_compute_bar(const struct nfp_bar *bar, uint32_t *bar_config,
131 uint64_t *bar_base, int tgt, int act, int tok,
132 uint64_t offset, size_t size, int width)
144 NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT
145 (NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_64BIT);
149 NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT
150 (NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_32BIT);
154 NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT
155 (NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_0BYTE);
161 if (act != NFP_CPP_ACTION_RW && act != 0) {
162 /* Fixed CPP mapping with specific action */
163 mask = ~(NFP_PCIE_P2C_FIXED_SIZE(bar) - 1);
166 NFP_PCIE_BAR_PCIE2CPP_MAPTYPE
167 (NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_FIXED);
168 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TARGET_BASEADDRESS(tgt);
169 newcfg |= NFP_PCIE_BAR_PCIE2CPP_ACTION_BASEADDRESS(act);
170 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TOKEN_BASEADDRESS(tok);
172 if ((offset & mask) != ((offset + size - 1) & mask)) {
173 printf("BAR%d: Won't use for Fixed mapping\n",
175 printf("\t<%#llx,%#llx>, action=%d\n",
176 (unsigned long long)offset,
177 (unsigned long long)(offset + size), act);
178 printf("\tBAR too small (0x%llx).\n",
179 (unsigned long long)mask);
185 printf("BAR%d: Created Fixed mapping\n", bar->index);
186 printf("\t%d:%d:%d:0x%#llx-0x%#llx>\n", tgt, act, tok,
187 (unsigned long long)offset,
188 (unsigned long long)(offset + mask));
193 mask = ~(NFP_PCIE_P2C_BULK_SIZE(bar) - 1);
197 NFP_PCIE_BAR_PCIE2CPP_MAPTYPE
198 (NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_BULK);
200 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TARGET_BASEADDRESS(tgt);
201 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TOKEN_BASEADDRESS(tok);
203 if ((offset & mask) != ((offset + size - 1) & mask)) {
204 printf("BAR%d: Won't use for bulk mapping\n",
206 printf("\t<%#llx,%#llx>\n", (unsigned long long)offset,
207 (unsigned long long)(offset + size));
208 printf("\ttarget=%d, token=%d\n", tgt, tok);
209 printf("\tBAR too small (%#llx) - (%#llx != %#llx).\n",
210 (unsigned long long)mask,
211 (unsigned long long)(offset & mask),
212 (unsigned long long)(offset + size - 1) & mask);
220 printf("BAR%d: Created bulk mapping %d:x:%d:%#llx-%#llx\n",
221 bar->index, tgt, tok, (unsigned long long)offset,
222 (unsigned long long)(offset + ~mask));
228 if (bar->bitsize < bitsize) {
229 printf("BAR%d: Too small for %d:%d:%d\n", bar->index, tgt, tok,
234 newcfg |= offset >> bitsize;
240 *bar_config = newcfg;
246 nfp_bar_write(struct nfp_pcie_user *nfp, struct nfp_bar *bar,
251 base = bar->index >> 3;
252 slot = bar->index & 7;
257 bar->csr = nfp->cfg +
258 NFP_PCIE_CFG_BAR_PCIETOCPPEXPBAR(base, slot);
260 *(uint32_t *)(bar->csr) = newcfg;
262 bar->barcfg = newcfg;
264 printf("BAR%d: updated to 0x%08x\n", bar->index, newcfg);
271 nfp_reconfigure_bar(struct nfp_pcie_user *nfp, struct nfp_bar *bar, int tgt,
272 int act, int tok, uint64_t offset, size_t size, int width)
278 err = nfp_compute_bar(bar, &newcfg, &newbase, tgt, act, tok, offset,
285 return nfp_bar_write(nfp, bar, newcfg);
289 * Map all PCI bars. We assume that the BAR with the PCIe config block is
292 * BAR0.0: Reserved for General Mapping (for MSI-X access to PCIe SRAM)
295 nfp_enable_bars(struct nfp_pcie_user *nfp)
300 for (x = ARRAY_SIZE(nfp->bar); x > 0; x--) {
301 bar = &nfp->bar[x - 1];
305 bar->mask = (1 << (nfp->barsz - 3)) - 1;
306 bar->bitsize = nfp->barsz - 3;
310 bar->csr = nfp->cfg +
311 NFP_PCIE_CFG_BAR_PCIETOCPPEXPBAR(bar->index >> 3,
314 (char *)mmap(0, 1 << bar->bitsize, PROT_READ | PROT_WRITE,
315 MAP_SHARED, nfp->device,
316 bar->index << bar->bitsize);
318 if (bar->iomem == MAP_FAILED)
324 static struct nfp_bar *
325 nfp_alloc_bar(struct nfp_pcie_user *nfp)
330 for (x = ARRAY_SIZE(nfp->bar); x > 0; x--) {
331 bar = &nfp->bar[x - 1];
341 nfp_disable_bars(struct nfp_pcie_user *nfp)
346 for (x = ARRAY_SIZE(nfp->bar); x > 0; x--) {
347 bar = &nfp->bar[x - 1];
349 munmap(bar->iomem, 1 << (nfp->barsz - 3));
357 * Generic CPP bus access interface.
360 struct nfp6000_area_priv {
378 nfp6000_area_init(struct nfp_cpp_area *area, uint32_t dest,
379 unsigned long long address, unsigned long size)
381 struct nfp_pcie_user *nfp = nfp_cpp_priv(nfp_cpp_area_cpp(area));
382 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
383 uint32_t target = NFP_CPP_ID_TARGET_of(dest);
384 uint32_t action = NFP_CPP_ID_ACTION_of(dest);
385 uint32_t token = NFP_CPP_ID_TOKEN_of(dest);
388 pp = nfp6000_target_pushpull(NFP_CPP_ID(target, action, token),
393 priv->width.read = PUSH_WIDTH(pp);
394 priv->width.write = PULL_WIDTH(pp);
396 if (priv->width.read > 0 &&
397 priv->width.write > 0 && priv->width.read != priv->width.write)
400 if (priv->width.read > 0)
401 priv->width.bar = priv->width.read;
403 priv->width.bar = priv->width.write;
405 priv->bar = nfp_alloc_bar(nfp);
406 if (priv->bar == NULL)
409 priv->target = target;
410 priv->action = action;
412 priv->offset = address;
415 ret = nfp_reconfigure_bar(nfp, priv->bar, priv->target, priv->action,
416 priv->token, priv->offset, priv->size,
423 nfp6000_area_acquire(struct nfp_cpp_area *area)
425 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
427 /* Calculate offset into BAR. */
428 if (nfp_bar_maptype(priv->bar) ==
429 NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_GENERAL) {
430 priv->bar_offset = priv->offset &
431 (NFP_PCIE_P2C_GENERAL_SIZE(priv->bar) - 1);
433 NFP_PCIE_P2C_GENERAL_TARGET_OFFSET(priv->bar,
436 NFP_PCIE_P2C_GENERAL_TOKEN_OFFSET(priv->bar, priv->token);
438 priv->bar_offset = priv->offset & priv->bar->mask;
441 /* Must have been too big. Sub-allocate. */
442 if (!priv->bar->iomem)
445 priv->iomem = priv->bar->iomem + priv->bar_offset;
451 nfp6000_area_mapped(struct nfp_cpp_area *area)
453 struct nfp6000_area_priv *area_priv = nfp_cpp_area_priv(area);
455 if (!area_priv->iomem)
458 return area_priv->iomem;
462 nfp6000_area_release(struct nfp_cpp_area *area)
464 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
471 nfp6000_area_iomem(struct nfp_cpp_area *area)
473 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
478 nfp6000_area_read(struct nfp_cpp_area *area, void *kernel_vaddr,
479 unsigned long offset, unsigned int length)
481 uint64_t *wrptr64 = kernel_vaddr;
482 const volatile uint64_t *rdptr64;
483 struct nfp6000_area_priv *priv;
484 uint32_t *wrptr32 = kernel_vaddr;
485 const volatile uint32_t *rdptr32;
490 priv = nfp_cpp_area_priv(area);
491 rdptr64 = (uint64_t *)(priv->iomem + offset);
492 rdptr32 = (uint32_t *)(priv->iomem + offset);
494 if (offset + length > priv->size)
497 width = priv->width.read;
502 /* Unaligned? Translate to an explicit access */
503 if ((priv->offset + offset) & (width - 1)) {
504 printf("aread_read unaligned!!!\n");
508 is_64 = width == TARGET_WIDTH_64;
510 /* MU reads via a PCIe2CPP BAR supports 32bit (and other) lengths */
511 if (priv->target == (NFP_CPP_TARGET_ID_MASK & NFP_CPP_TARGET_MU) &&
512 priv->action == NFP_CPP_ACTION_RW) {
517 if (offset % sizeof(uint64_t) != 0 ||
518 length % sizeof(uint64_t) != 0)
521 if (offset % sizeof(uint32_t) != 0 ||
522 length % sizeof(uint32_t) != 0)
530 for (n = 0; n < length; n += sizeof(uint64_t)) {
536 for (n = 0; n < length; n += sizeof(uint32_t)) {
546 nfp6000_area_write(struct nfp_cpp_area *area, const void *kernel_vaddr,
547 unsigned long offset, unsigned int length)
549 const uint64_t *rdptr64 = kernel_vaddr;
551 const uint32_t *rdptr32 = kernel_vaddr;
552 struct nfp6000_area_priv *priv;
558 priv = nfp_cpp_area_priv(area);
559 wrptr64 = (uint64_t *)(priv->iomem + offset);
560 wrptr32 = (uint32_t *)(priv->iomem + offset);
562 if (offset + length > priv->size)
565 width = priv->width.write;
570 /* Unaligned? Translate to an explicit access */
571 if ((priv->offset + offset) & (width - 1))
574 is_64 = width == TARGET_WIDTH_64;
576 /* MU writes via a PCIe2CPP BAR supports 32bit (and other) lengths */
577 if (priv->target == (NFP_CPP_TARGET_ID_MASK & NFP_CPP_TARGET_MU) &&
578 priv->action == NFP_CPP_ACTION_RW)
582 if (offset % sizeof(uint64_t) != 0 ||
583 length % sizeof(uint64_t) != 0)
586 if (offset % sizeof(uint32_t) != 0 ||
587 length % sizeof(uint32_t) != 0)
595 for (n = 0; n < length; n += sizeof(uint64_t)) {
601 for (n = 0; n < length; n += sizeof(uint32_t)) {
610 #define PCI_DEVICES "/sys/bus/pci/devices"
613 nfp_acquire_process_lock(struct nfp_pcie_user *desc)
619 memset(&lock, 0, sizeof(lock));
621 snprintf(lockname, sizeof(lockname), "/var/lock/nfp_%s", desc->busdev);
622 desc->lock = open(lockname, O_RDWR | O_CREAT, 0666);
626 lock.l_type = F_WRLCK;
627 lock.l_whence = SEEK_SET;
630 rc = fcntl(desc->lock, F_SETLKW, &lock);
632 if (errno != EAGAIN && errno != EACCES) {
643 nfp6000_set_model(struct rte_pci_device *dev, struct nfp_cpp *cpp)
647 if (rte_pci_read_config(dev, &model, 4, 0x2e) < 0) {
648 printf("nfp set model failed\n");
653 nfp_cpp_model_set(cpp, model);
659 nfp6000_set_interface(struct rte_pci_device *dev, struct nfp_cpp *cpp)
663 if (rte_pci_read_config(dev, &interface, 2, 0x154) < 0) {
664 printf("nfp set interface failed\n");
668 nfp_cpp_interface_set(cpp, interface);
673 #define PCI_CFG_SPACE_SIZE 256
674 #define PCI_CFG_SPACE_EXP_SIZE 4096
675 #define PCI_EXT_CAP_ID(header) (int)(header & 0x0000ffff)
676 #define PCI_EXT_CAP_NEXT(header) ((header >> 20) & 0xffc)
677 #define PCI_EXT_CAP_ID_DSN 0x03
679 nfp_pci_find_next_ext_capability(struct rte_pci_device *dev, int cap)
683 int pos = PCI_CFG_SPACE_SIZE;
685 /* minimum 8 bytes per capability */
686 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
688 if (rte_pci_read_config(dev, &header, 4, pos) < 0) {
689 printf("nfp error reading extended capabilities\n");
694 * If we have no capabilities, this is indicated by cap ID,
695 * cap version and next pointer all being 0.
701 if (PCI_EXT_CAP_ID(header) == cap)
704 pos = PCI_EXT_CAP_NEXT(header);
705 if (pos < PCI_CFG_SPACE_SIZE)
708 if (rte_pci_read_config(dev, &header, 4, pos) < 0) {
709 printf("nfp error reading extended capabilities\n");
718 nfp6000_set_serial(struct rte_pci_device *dev, struct nfp_cpp *cpp)
725 pos = nfp_pci_find_next_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
727 printf("PCI_EXT_CAP_ID_DSN not found. nfp set serial failed\n");
733 if (rte_pci_read_config(dev, &tmp, 2, pos) < 0) {
734 printf("nfp set serial failed\n");
738 serial[4] = (uint8_t)((tmp >> 8) & 0xff);
739 serial[5] = (uint8_t)(tmp & 0xff);
742 if (rte_pci_read_config(dev, &tmp, 2, pos) < 0) {
743 printf("nfp set serial failed\n");
747 serial[2] = (uint8_t)((tmp >> 8) & 0xff);
748 serial[3] = (uint8_t)(tmp & 0xff);
751 if (rte_pci_read_config(dev, &tmp, 2, pos) < 0) {
752 printf("nfp set serial failed\n");
756 serial[0] = (uint8_t)((tmp >> 8) & 0xff);
757 serial[1] = (uint8_t)(tmp & 0xff);
759 nfp_cpp_serial_set(cpp, serial, serial_len);
765 nfp6000_set_barsz(struct nfp_pcie_user *desc)
768 unsigned long start, end, flags, tmp;
772 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/resource", PCI_DEVICES,
775 fp = fopen(tmp_str, "r");
779 if (fscanf(fp, "0x%lx 0x%lx 0x%lx", &start, &end, &flags) == 0) {
780 printf("error reading resource file for bar size\n");
785 if (fclose(fp) == -1)
788 tmp = (end - start) + 1;
797 nfp6000_init(struct nfp_cpp *cpp, struct rte_pci_device *dev)
804 struct nfp_pcie_user *desc;
806 desc = malloc(sizeof(*desc));
811 memset(desc->busdev, 0, BUSDEV_SZ);
812 strlcpy(desc->busdev, dev->device.name, sizeof(desc->busdev));
814 if (cpp->driver_lock_needed) {
815 ret = nfp_acquire_process_lock(desc);
820 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/driver", PCI_DEVICES,
823 size = readlink(tmp_str, link, sizeof(link));
828 if (size == sizeof(link))
831 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/resource0", PCI_DEVICES,
834 desc->device = open(tmp_str, O_RDWR);
835 if (desc->device == -1)
838 if (nfp6000_set_model(dev, cpp) < 0)
840 if (nfp6000_set_interface(dev, cpp) < 0)
842 if (nfp6000_set_serial(dev, cpp) < 0)
844 if (nfp6000_set_barsz(desc) < 0)
847 desc->cfg = (char *)mmap(0, 1 << (desc->barsz - 3),
848 PROT_READ | PROT_WRITE,
849 MAP_SHARED, desc->device, 0);
851 if (desc->cfg == MAP_FAILED)
854 nfp_enable_bars(desc);
856 nfp_cpp_priv_set(cpp, desc);
858 model = __nfp_cpp_model_autodetect(cpp);
859 nfp_cpp_model_set(cpp, model);
865 nfp6000_free(struct nfp_cpp *cpp)
867 struct nfp_pcie_user *desc = nfp_cpp_priv(cpp);
870 /* Unmap may cause if there are any pending transaxctions */
871 nfp_disable_bars(desc);
872 munmap(desc->cfg, 1 << (desc->barsz - 3));
874 for (x = ARRAY_SIZE(desc->bar); x > 0; x--) {
875 if (desc->bar[x - 1].iomem)
876 munmap(desc->bar[x - 1].iomem, 1 << (desc->barsz - 3));
878 if (cpp->driver_lock_needed)
884 static const struct nfp_cpp_operations nfp6000_pcie_ops = {
885 .init = nfp6000_init,
886 .free = nfp6000_free,
888 .area_priv_size = sizeof(struct nfp6000_area_priv),
889 .area_init = nfp6000_area_init,
890 .area_acquire = nfp6000_area_acquire,
891 .area_release = nfp6000_area_release,
892 .area_mapped = nfp6000_area_mapped,
893 .area_read = nfp6000_area_read,
894 .area_write = nfp6000_area_write,
895 .area_iomem = nfp6000_area_iomem,
899 nfp_cpp_operations *nfp_cpp_transport_operations(void)
901 return &nfp6000_pcie_ops;