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.
36 #include "nfp_target.h"
37 #include "nfp6000/nfp6000.h"
39 #define NFP_PCIE_BAR(_pf) (0x30000 + ((_pf) & 7) * 0xc0)
41 #define NFP_PCIE_BAR_PCIE2CPP_ACTION_BASEADDRESS(_x) (((_x) & 0x1f) << 16)
42 #define NFP_PCIE_BAR_PCIE2CPP_BASEADDRESS(_x) (((_x) & 0xffff) << 0)
43 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT(_x) (((_x) & 0x3) << 27)
44 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_32BIT 0
45 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_64BIT 1
46 #define NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_0BYTE 3
47 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE(_x) (((_x) & 0x7) << 29)
48 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_OF(_x) (((_x) >> 29) & 0x7)
49 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_FIXED 0
50 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_BULK 1
51 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_TARGET 2
52 #define NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_GENERAL 3
53 #define NFP_PCIE_BAR_PCIE2CPP_TARGET_BASEADDRESS(_x) (((_x) & 0xf) << 23)
54 #define NFP_PCIE_BAR_PCIE2CPP_TOKEN_BASEADDRESS(_x) (((_x) & 0x3) << 21)
57 * Minimal size of the PCIe cfg memory we depend on being mapped,
58 * queue controller and DMA controller don't have to be covered.
60 #define NFP_PCI_MIN_MAP_SIZE 0x080000
62 #define NFP_PCIE_P2C_FIXED_SIZE(bar) (1 << (bar)->bitsize)
63 #define NFP_PCIE_P2C_BULK_SIZE(bar) (1 << (bar)->bitsize)
64 #define NFP_PCIE_P2C_GENERAL_TARGET_OFFSET(bar, x) ((x) << ((bar)->bitsize - 2))
65 #define NFP_PCIE_P2C_GENERAL_TOKEN_OFFSET(bar, x) ((x) << ((bar)->bitsize - 4))
66 #define NFP_PCIE_P2C_GENERAL_SIZE(bar) (1 << ((bar)->bitsize - 4))
68 #define NFP_PCIE_CFG_BAR_PCIETOCPPEXPBAR(bar, slot) \
69 (NFP_PCIE_BAR(0) + ((bar) * 8 + (slot)) * 4)
71 #define NFP_PCIE_CPP_BAR_PCIETOCPPEXPBAR(bar, slot) \
72 (((bar) * 8 + (slot)) * 4)
75 * Define to enable a bit more verbose debug output.
76 * Set to 1 to enable a bit more verbose debug output.
79 struct nfp6000_area_priv;
82 * struct nfp_bar - describes BAR configuration and usage
83 * @nfp: backlink to owner
84 * @barcfg: cached contents of BAR config CSR
85 * @base: the BAR's base CPP offset
86 * @mask: mask for the BAR aperture (read only)
87 * @bitsize: bitsize of BAR aperture (read only)
88 * @index: index of the BAR
89 * @lock: lock to specify if bar is in use
90 * @refcnt: number of current users
91 * @iomem: mapped IO memory
95 struct nfp_pcie_user *nfp;
97 uint64_t base; /* CPP address base */
98 uint64_t mask; /* Bit mask of the bar */
99 uint32_t bitsize; /* Bit size of the bar */
108 struct nfp_pcie_user {
109 struct nfp_bar bar[NFP_BAR_MAX];
113 char busdev[BUSDEV_SZ];
119 nfp_bar_maptype(struct nfp_bar *bar)
121 return NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_OF(bar->barcfg);
124 #define TARGET_WIDTH_32 4
125 #define TARGET_WIDTH_64 8
128 nfp_compute_bar(const struct nfp_bar *bar, uint32_t *bar_config,
129 uint64_t *bar_base, int tgt, int act, int tok,
130 uint64_t offset, size_t size, int width)
142 NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT
143 (NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_64BIT);
147 NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT
148 (NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_32BIT);
152 NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT
153 (NFP_PCIE_BAR_PCIE2CPP_LENGTHSELECT_0BYTE);
159 if (act != NFP_CPP_ACTION_RW && act != 0) {
160 /* Fixed CPP mapping with specific action */
161 mask = ~(NFP_PCIE_P2C_FIXED_SIZE(bar) - 1);
164 NFP_PCIE_BAR_PCIE2CPP_MAPTYPE
165 (NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_FIXED);
166 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TARGET_BASEADDRESS(tgt);
167 newcfg |= NFP_PCIE_BAR_PCIE2CPP_ACTION_BASEADDRESS(act);
168 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TOKEN_BASEADDRESS(tok);
170 if ((offset & mask) != ((offset + size - 1) & mask)) {
171 printf("BAR%d: Won't use for Fixed mapping\n",
173 printf("\t<%#llx,%#llx>, action=%d\n",
174 (unsigned long long)offset,
175 (unsigned long long)(offset + size), act);
176 printf("\tBAR too small (0x%llx).\n",
177 (unsigned long long)mask);
183 printf("BAR%d: Created Fixed mapping\n", bar->index);
184 printf("\t%d:%d:%d:0x%#llx-0x%#llx>\n", tgt, act, tok,
185 (unsigned long long)offset,
186 (unsigned long long)(offset + mask));
191 mask = ~(NFP_PCIE_P2C_BULK_SIZE(bar) - 1);
195 NFP_PCIE_BAR_PCIE2CPP_MAPTYPE
196 (NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_BULK);
198 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TARGET_BASEADDRESS(tgt);
199 newcfg |= NFP_PCIE_BAR_PCIE2CPP_TOKEN_BASEADDRESS(tok);
201 if ((offset & mask) != ((offset + size - 1) & mask)) {
202 printf("BAR%d: Won't use for bulk mapping\n",
204 printf("\t<%#llx,%#llx>\n", (unsigned long long)offset,
205 (unsigned long long)(offset + size));
206 printf("\ttarget=%d, token=%d\n", tgt, tok);
207 printf("\tBAR too small (%#llx) - (%#llx != %#llx).\n",
208 (unsigned long long)mask,
209 (unsigned long long)(offset & mask),
210 (unsigned long long)(offset + size - 1) & mask);
218 printf("BAR%d: Created bulk mapping %d:x:%d:%#llx-%#llx\n",
219 bar->index, tgt, tok, (unsigned long long)offset,
220 (unsigned long long)(offset + ~mask));
226 if (bar->bitsize < bitsize) {
227 printf("BAR%d: Too small for %d:%d:%d\n", bar->index, tgt, tok,
232 newcfg |= offset >> bitsize;
238 *bar_config = newcfg;
244 nfp_bar_write(struct nfp_pcie_user *nfp, struct nfp_bar *bar,
249 base = bar->index >> 3;
250 slot = bar->index & 7;
255 bar->csr = nfp->cfg +
256 NFP_PCIE_CFG_BAR_PCIETOCPPEXPBAR(base, slot);
258 *(uint32_t *)(bar->csr) = newcfg;
260 bar->barcfg = newcfg;
262 printf("BAR%d: updated to 0x%08x\n", bar->index, newcfg);
269 nfp_reconfigure_bar(struct nfp_pcie_user *nfp, struct nfp_bar *bar, int tgt,
270 int act, int tok, uint64_t offset, size_t size, int width)
276 err = nfp_compute_bar(bar, &newcfg, &newbase, tgt, act, tok, offset,
283 return nfp_bar_write(nfp, bar, newcfg);
287 * Map all PCI bars. We assume that the BAR with the PCIe config block is
290 * BAR0.0: Reserved for General Mapping (for MSI-X access to PCIe SRAM)
293 nfp_enable_bars(struct nfp_pcie_user *nfp)
298 for (x = ARRAY_SIZE(nfp->bar); x > 0; x--) {
299 bar = &nfp->bar[x - 1];
303 bar->mask = (1 << (nfp->barsz - 3)) - 1;
304 bar->bitsize = nfp->barsz - 3;
308 bar->csr = nfp->cfg +
309 NFP_PCIE_CFG_BAR_PCIETOCPPEXPBAR(bar->index >> 3,
312 (char *)mmap(0, 1 << bar->bitsize, PROT_READ | PROT_WRITE,
313 MAP_SHARED, nfp->device,
314 bar->index << bar->bitsize);
316 if (bar->iomem == MAP_FAILED)
322 static struct nfp_bar *
323 nfp_alloc_bar(struct nfp_pcie_user *nfp)
328 for (x = ARRAY_SIZE(nfp->bar); x > 0; x--) {
329 bar = &nfp->bar[x - 1];
339 nfp_disable_bars(struct nfp_pcie_user *nfp)
344 for (x = ARRAY_SIZE(nfp->bar); x > 0; x--) {
345 bar = &nfp->bar[x - 1];
347 munmap(bar->iomem, 1 << (nfp->barsz - 3));
355 * Generic CPP bus access interface.
358 struct nfp6000_area_priv {
376 nfp6000_area_init(struct nfp_cpp_area *area, uint32_t dest,
377 unsigned long long address, unsigned long size)
379 struct nfp_pcie_user *nfp = nfp_cpp_priv(nfp_cpp_area_cpp(area));
380 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
381 uint32_t target = NFP_CPP_ID_TARGET_of(dest);
382 uint32_t action = NFP_CPP_ID_ACTION_of(dest);
383 uint32_t token = NFP_CPP_ID_TOKEN_of(dest);
386 pp = nfp6000_target_pushpull(NFP_CPP_ID(target, action, token),
391 priv->width.read = PUSH_WIDTH(pp);
392 priv->width.write = PULL_WIDTH(pp);
394 if (priv->width.read > 0 &&
395 priv->width.write > 0 && priv->width.read != priv->width.write)
398 if (priv->width.read > 0)
399 priv->width.bar = priv->width.read;
401 priv->width.bar = priv->width.write;
403 priv->bar = nfp_alloc_bar(nfp);
404 if (priv->bar == NULL)
407 priv->target = target;
408 priv->action = action;
410 priv->offset = address;
413 ret = nfp_reconfigure_bar(nfp, priv->bar, priv->target, priv->action,
414 priv->token, priv->offset, priv->size,
421 nfp6000_area_acquire(struct nfp_cpp_area *area)
423 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
425 /* Calculate offset into BAR. */
426 if (nfp_bar_maptype(priv->bar) ==
427 NFP_PCIE_BAR_PCIE2CPP_MAPTYPE_GENERAL) {
428 priv->bar_offset = priv->offset &
429 (NFP_PCIE_P2C_GENERAL_SIZE(priv->bar) - 1);
431 NFP_PCIE_P2C_GENERAL_TARGET_OFFSET(priv->bar,
434 NFP_PCIE_P2C_GENERAL_TOKEN_OFFSET(priv->bar, priv->token);
436 priv->bar_offset = priv->offset & priv->bar->mask;
439 /* Must have been too big. Sub-allocate. */
440 if (!priv->bar->iomem)
443 priv->iomem = priv->bar->iomem + priv->bar_offset;
449 nfp6000_area_mapped(struct nfp_cpp_area *area)
451 struct nfp6000_area_priv *area_priv = nfp_cpp_area_priv(area);
453 if (!area_priv->iomem)
456 return area_priv->iomem;
460 nfp6000_area_release(struct nfp_cpp_area *area)
462 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
469 nfp6000_area_iomem(struct nfp_cpp_area *area)
471 struct nfp6000_area_priv *priv = nfp_cpp_area_priv(area);
476 nfp6000_area_read(struct nfp_cpp_area *area, void *kernel_vaddr,
477 unsigned long offset, unsigned int length)
479 uint64_t *wrptr64 = kernel_vaddr;
480 const volatile uint64_t *rdptr64;
481 struct nfp6000_area_priv *priv;
482 uint32_t *wrptr32 = kernel_vaddr;
483 const volatile uint32_t *rdptr32;
488 priv = nfp_cpp_area_priv(area);
489 rdptr64 = (uint64_t *)(priv->iomem + offset);
490 rdptr32 = (uint32_t *)(priv->iomem + offset);
492 if (offset + length > priv->size)
495 width = priv->width.read;
500 /* Unaligned? Translate to an explicit access */
501 if ((priv->offset + offset) & (width - 1)) {
502 printf("aread_read unaligned!!!\n");
506 is_64 = width == TARGET_WIDTH_64;
508 /* MU reads via a PCIe2CPP BAR supports 32bit (and other) lengths */
509 if (priv->target == (NFP_CPP_TARGET_ID_MASK & NFP_CPP_TARGET_MU) &&
510 priv->action == NFP_CPP_ACTION_RW) {
515 if (offset % sizeof(uint64_t) != 0 ||
516 length % sizeof(uint64_t) != 0)
519 if (offset % sizeof(uint32_t) != 0 ||
520 length % sizeof(uint32_t) != 0)
528 for (n = 0; n < length; n += sizeof(uint64_t)) {
534 for (n = 0; n < length; n += sizeof(uint32_t)) {
544 nfp6000_area_write(struct nfp_cpp_area *area, const void *kernel_vaddr,
545 unsigned long offset, unsigned int length)
547 const uint64_t *rdptr64 = kernel_vaddr;
549 const uint32_t *rdptr32 = kernel_vaddr;
550 struct nfp6000_area_priv *priv;
556 priv = nfp_cpp_area_priv(area);
557 wrptr64 = (uint64_t *)(priv->iomem + offset);
558 wrptr32 = (uint32_t *)(priv->iomem + offset);
560 if (offset + length > priv->size)
563 width = priv->width.write;
568 /* Unaligned? Translate to an explicit access */
569 if ((priv->offset + offset) & (width - 1))
572 is_64 = width == TARGET_WIDTH_64;
574 /* MU writes via a PCIe2CPP BAR supports 32bit (and other) lengths */
575 if (priv->target == (NFP_CPP_TARGET_ID_MASK & NFP_CPP_TARGET_MU) &&
576 priv->action == NFP_CPP_ACTION_RW)
580 if (offset % sizeof(uint64_t) != 0 ||
581 length % sizeof(uint64_t) != 0)
584 if (offset % sizeof(uint32_t) != 0 ||
585 length % sizeof(uint32_t) != 0)
593 for (n = 0; n < length; n += sizeof(uint64_t)) {
599 for (n = 0; n < length; n += sizeof(uint32_t)) {
608 #define PCI_DEVICES "/sys/bus/pci/devices"
611 nfp_acquire_process_lock(struct nfp_pcie_user *desc)
617 memset(&lock, 0, sizeof(lock));
619 snprintf(lockname, sizeof(lockname), "/var/lock/nfp_%s", desc->busdev);
620 desc->lock = open(lockname, O_RDWR | O_CREAT, 0666);
624 lock.l_type = F_WRLCK;
625 lock.l_whence = SEEK_SET;
628 rc = fcntl(desc->lock, F_SETLKW, &lock);
630 if (errno != EAGAIN && errno != EACCES) {
641 nfp6000_set_model(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
647 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/config", PCI_DEVICES,
650 fp = open(tmp_str, O_RDONLY);
654 lseek(fp, 0x2e, SEEK_SET);
656 if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
657 printf("Error reading config file for model\n");
666 nfp_cpp_model_set(cpp, tmp);
672 nfp6000_set_interface(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
678 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/config", PCI_DEVICES,
681 fp = open(tmp_str, O_RDONLY);
685 lseek(fp, 0x154, SEEK_SET);
687 if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
688 printf("error reading config file for interface\n");
695 nfp_cpp_interface_set(cpp, tmp);
700 #define PCI_CFG_SPACE_SIZE 256
701 #define PCI_CFG_SPACE_EXP_SIZE 4096
702 #define PCI_EXT_CAP_ID(header) (int)(header & 0x0000ffff)
703 #define PCI_EXT_CAP_NEXT(header) ((header >> 20) & 0xffc)
704 #define PCI_EXT_CAP_ID_DSN 0x03
706 nfp_pci_find_next_ext_capability(int fp, int cap)
710 int pos = PCI_CFG_SPACE_SIZE;
712 /* minimum 8 bytes per capability */
713 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
715 lseek(fp, pos, SEEK_SET);
716 if (read(fp, &header, sizeof(header)) != sizeof(header)) {
717 printf("error reading config file for serial\n");
722 * If we have no capabilities, this is indicated by cap ID,
723 * cap version and next pointer all being 0.
729 if (PCI_EXT_CAP_ID(header) == cap)
732 pos = PCI_EXT_CAP_NEXT(header);
733 if (pos < PCI_CFG_SPACE_SIZE)
736 lseek(fp, pos, SEEK_SET);
737 if (read(fp, &header, sizeof(header)) != sizeof(header)) {
738 printf("error reading config file for serial\n");
747 nfp6000_set_serial(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
755 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/config", PCI_DEVICES,
758 fp = open(tmp_str, O_RDONLY);
762 pos = nfp_pci_find_next_ext_capability(fp, PCI_EXT_CAP_ID_DSN);
764 printf("PCI_EXT_CAP_ID_DSN not found. Using default offset\n");
765 lseek(fp, 0x156, SEEK_SET);
767 lseek(fp, pos + 6, SEEK_SET);
770 if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
771 printf("error reading config file for serial\n");
775 serial[4] = (uint8_t)((tmp >> 8) & 0xff);
776 serial[5] = (uint8_t)(tmp & 0xff);
778 if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
779 printf("error reading config file for serial\n");
783 serial[2] = (uint8_t)((tmp >> 8) & 0xff);
784 serial[3] = (uint8_t)(tmp & 0xff);
786 if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
787 printf("error reading config file for serial\n");
791 serial[0] = (uint8_t)((tmp >> 8) & 0xff);
792 serial[1] = (uint8_t)(tmp & 0xff);
797 nfp_cpp_serial_set(cpp, serial, serial_len);
803 nfp6000_set_barsz(struct nfp_pcie_user *desc)
806 unsigned long start, end, flags, tmp;
810 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/resource", PCI_DEVICES,
813 fp = fopen(tmp_str, "r");
817 if (fscanf(fp, "0x%lx 0x%lx 0x%lx", &start, &end, &flags) == 0) {
818 printf("error reading resource file for bar size\n");
822 if (fclose(fp) == -1)
825 tmp = (end - start) + 1;
834 nfp6000_init(struct nfp_cpp *cpp, const char *devname)
841 struct nfp_pcie_user *desc;
843 desc = malloc(sizeof(*desc));
848 memset(desc->busdev, 0, BUSDEV_SZ);
849 strncpy(desc->busdev, devname, strlen(devname));
851 ret = nfp_acquire_process_lock(desc);
855 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/driver", PCI_DEVICES,
858 size = readlink(tmp_str, link, sizeof(link));
863 if (size == sizeof(link))
866 snprintf(tmp_str, sizeof(tmp_str), "%s/%s/resource0", PCI_DEVICES,
869 desc->device = open(tmp_str, O_RDWR);
870 if (desc->device == -1)
873 if (nfp6000_set_model(desc, cpp) < 0)
875 if (nfp6000_set_interface(desc, cpp) < 0)
877 if (nfp6000_set_serial(desc, cpp) < 0)
879 if (nfp6000_set_barsz(desc) < 0)
882 desc->cfg = (char *)mmap(0, 1 << (desc->barsz - 3),
883 PROT_READ | PROT_WRITE,
884 MAP_SHARED, desc->device, 0);
886 if (desc->cfg == MAP_FAILED)
889 nfp_enable_bars(desc);
891 nfp_cpp_priv_set(cpp, desc);
893 model = __nfp_cpp_model_autodetect(cpp);
894 nfp_cpp_model_set(cpp, model);
900 nfp6000_free(struct nfp_cpp *cpp)
902 struct nfp_pcie_user *desc = nfp_cpp_priv(cpp);
905 /* Unmap may cause if there are any pending transaxctions */
906 nfp_disable_bars(desc);
907 munmap(desc->cfg, 1 << (desc->barsz - 3));
909 for (x = ARRAY_SIZE(desc->bar); x > 0; x--) {
910 if (desc->bar[x - 1].iomem)
911 munmap(desc->bar[x - 1].iomem, 1 << (desc->barsz - 3));
918 static const struct nfp_cpp_operations nfp6000_pcie_ops = {
919 .init = nfp6000_init,
920 .free = nfp6000_free,
922 .area_priv_size = sizeof(struct nfp6000_area_priv),
923 .area_init = nfp6000_area_init,
924 .area_acquire = nfp6000_area_acquire,
925 .area_release = nfp6000_area_release,
926 .area_mapped = nfp6000_area_mapped,
927 .area_read = nfp6000_area_read,
928 .area_write = nfp6000_area_write,
929 .area_iomem = nfp6000_area_iomem,
933 nfp_cpp_operations *nfp_cpp_transport_operations(void)
935 return &nfp6000_pcie_ops;