2 * The PCI Library -- Direct Configuration access via PCIe ECAM
4 * Copyright (c) 2023 Pali Rohár <pali@kernel.org>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "physmem-access.h"
25 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
26 #include <sys/sysctl.h>
29 #if defined (__FreeBSD__) || defined (__DragonFly__)
55 char asl_compiler_id[4];
56 u32 asl_compiler_revision;
90 // Back-end data linked to struct pci_access
92 struct acpi_mcfg *mcfg;
93 struct mmap_cache *cache;
94 struct physmem *physmem;
99 get_rsdt_addresses_count(struct acpi_rsdt *rsdt)
101 return (rsdt->sdt.length - ((unsigned char*)&rsdt->sdt_addresses - (unsigned char *)rsdt)) / sizeof(rsdt->sdt_addresses[0]);
105 get_xsdt_addresses_count(struct acpi_xsdt *xsdt)
107 return (xsdt->sdt.length - ((unsigned char*)&xsdt->sdt_addresses - (unsigned char *)xsdt)) / sizeof(xsdt->sdt_addresses[0]);
111 get_mcfg_allocations_count(struct acpi_mcfg *mcfg)
113 return (mcfg->sdt.length - ((unsigned char *)&mcfg->allocations - (unsigned char *)mcfg)) / sizeof(mcfg->allocations[0]);
117 calculate_checksum(const u8 *bytes, int len)
122 checksum -= *(bytes++);
126 static struct acpi_sdt *
127 check_and_map_sdt(struct physmem *physmem, long pagesize, u64 addr, const char *signature, void **map_addr, u32 *map_length)
129 struct acpi_sdt *sdt;
130 char sdt_signature[sizeof(sdt->signature)];
134 if (addr + sizeof(*sdt) < addr)
137 map = physmem_map(physmem, addr & ~(pagesize-1), sizeof(*sdt) + (addr & (pagesize-1)), 0);
138 if (map == (void *)-1)
141 sdt = (struct acpi_sdt *)((unsigned char *)map + (addr & (pagesize-1)));
142 length = sdt->length;
143 memcpy(sdt_signature, sdt->signature, sizeof(sdt->signature));
145 physmem_unmap(physmem, map, sizeof(*sdt) + (addr & (pagesize-1)));
147 if (memcmp(sdt_signature, signature, sizeof(sdt_signature)) != 0)
149 if (length < sizeof(*sdt))
152 map = physmem_map(physmem, addr & ~(pagesize-1), length + (addr & (pagesize-1)), 0);
153 if (map == (void *)-1)
156 sdt = (struct acpi_sdt *)((unsigned char *)map + (addr & (pagesize-1)));
158 if (calculate_checksum((u8 *)sdt, sdt->length) != 0)
160 physmem_unmap(physmem, map, length + (addr & (pagesize-1)));
165 *map_length = length + (addr & (pagesize-1));
170 check_rsdp(struct acpi_rsdp *rsdp)
172 if (memcmp(rsdp->signature, "RSD PTR ", sizeof(rsdp->signature)) != 0)
174 if (calculate_checksum((u8 *)rsdp, sizeof(*rsdp)) != 0)
180 check_and_parse_rsdp(struct physmem *physmem, long pagesize, u64 addr, u32 *rsdt_address, u64 *xsdt_address)
182 struct acpi_rsdp *rsdp;
183 unsigned char buf[sizeof(*rsdp) + sizeof(*rsdp->rsdp20)];
186 map = physmem_map(physmem, addr & ~(pagesize-1), sizeof(buf) + (addr & (pagesize-1)), 0);
187 if (map == (void *)-1)
190 rsdp = (struct acpi_rsdp *)buf;
191 memcpy(rsdp, (unsigned char *)map + (addr & (pagesize-1)), sizeof(buf));
193 physmem_unmap(physmem, map, sizeof(buf));
195 if (!check_rsdp(rsdp))
198 *rsdt_address = rsdp->rsdt_address;
200 if (rsdp->revision != 0 &&
201 (*rsdp->rsdp20).length == sizeof(*rsdp) + sizeof(*rsdp->rsdp20) &&
202 calculate_checksum((u8 *)rsdp, (*rsdp->rsdp20).length) == 0)
203 *xsdt_address = (*rsdp->rsdp20).xsdt_address;
211 find_rsdp_address(struct pci_access *a, const char *efisystab, int use_bsd UNUSED, int use_x86bios UNUSED)
214 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
221 #if defined(__amd64__) || defined(__i386__)
222 struct ecam_access *eacc = a->backend_data;
223 struct physmem *physmem = eacc->physmem;
235 a->debug("reading EFI system table: %s...", efisystab);
236 f = fopen(efisystab, "r");
239 while (fgets(buf, sizeof(buf), f))
242 while (len > 0 && buf[len-1] == '\n')
244 if (strncmp(buf, "ACPI20=", 7) == 0 && isxdigit(buf[7]))
247 ullnum = strtoull(buf+7, &endptr, 16);
248 if (!errno && !*endptr)
251 else if (strncmp(buf, "ACPI=", 5) == 0 && isxdigit(buf[5]))
254 ullnum = strtoull(buf+5, &endptr, 16);
255 if (!errno && !*endptr)
262 a->debug("opening failed: %s...", strerror(errno));
270 #if defined (__FreeBSD__) || defined (__DragonFly__)
273 /* First try FreeBSD kenv hint.acpi.0.rsdp */
274 a->debug("calling kenv hint.acpi.0.rsdp...");
275 if (kenv(KENV_GET, "hint.acpi.0.rsdp", buf, sizeof(buf)) > 0)
278 ullnum = strtoull(buf, &endptr, 16);
279 if (!errno && !*endptr)
283 /* Then try FreeBSD sysctl machdep.acpi_root */
284 a->debug("calling sysctl machdep.acpi_root...");
286 if (sysctlbyname("machdep.acpi_root", &ulnum, &len, NULL, 0) == 0)
291 #if defined(__NetBSD__)
294 /* Try NetBSD sysctl hw.acpi.root */
295 a->debug("calling sysctl hw.acpi.root...");
297 if (sysctlbyname("hw.acpi.root", &ulnum, &len, NULL, 0) == 0)
302 #if defined(__amd64__) || defined(__i386__)
307 /* Scan first kB of Extended BIOS Data Area */
308 a->debug("scanning first kB of EBDA...");
309 map = physmem_map(physmem, 0, 0x40E + 1024, 0);
310 if (map != (void *)-1)
312 for (addr = 0x40E; addr < 0x40E + 1024; addr += 16)
314 if (check_rsdp((struct acpi_rsdp *)((unsigned char *)map + addr)))
320 if (physmem_unmap(physmem, map, 0x40E + 1024) != 0)
321 a->debug("unmapping of EBDA failed: %s...", strerror(errno));
324 a->debug("mapping of EBDA failed: %s...", strerror(errno));
329 /* Scan the main BIOS area below 1 MB */
330 a->debug("scanning BIOS below 1 MB...");
331 map = physmem_map(physmem, 0xE0000, 0x20000, 0);
332 if (map != (void *)-1)
334 for (addr = 0x0; addr < 0x20000; addr += 16)
336 if (check_rsdp((struct acpi_rsdp *)((unsigned char *)map + addr)))
338 rsdp_addr = 0xE0000 + addr;
342 if (physmem_unmap(physmem, map, 0x20000) != 0)
343 a->debug("unmapping of BIOS failed: %s...", strerror(errno));
346 a->debug("mapping of BIOS failed: %s...", strerror(errno));
356 static struct acpi_mcfg *
357 find_mcfg(struct pci_access *a, const char *acpimcfg, const char *efisystab, int use_bsd, int use_x86bios)
359 struct ecam_access *eacc = a->backend_data;
360 struct physmem *physmem = eacc->physmem;
361 long pagesize = eacc->pagesize;
362 struct acpi_xsdt *xsdt;
363 struct acpi_rsdt *rsdt;
364 struct acpi_mcfg *mcfg;
365 struct acpi_sdt *sdt;
366 unsigned int i, count;
382 ret = glob(acpimcfg, GLOB_NOCHECK, NULL, &mcfg_glob);
385 path = mcfg_glob.gl_pathv[0];
386 a->debug("reading acpi mcfg file: %s...", path);
387 mcfg_file = fopen(path, "rb");
388 globfree(&mcfg_glob);
391 if (fseek(mcfg_file, 0, SEEK_END) == 0)
392 length = ftell(mcfg_file);
395 if (length > 0 && (size_t)length > sizeof(*mcfg))
398 mcfg = pci_malloc(a, length);
399 if (fread(mcfg, 1, length, mcfg_file) == (size_t)length &&
400 memcmp(mcfg->sdt.signature, "MCFG", 4) == 0 &&
401 mcfg->sdt.length <= (size_t)length &&
402 calculate_checksum((u8 *)mcfg, mcfg->sdt.length) == 0)
410 a->debug("failed...");
413 a->debug("glob(%s) failed: %d...", acpimcfg, ret);
416 a->debug("searching for ACPI RSDP...");
417 rsdp_address = find_rsdp_address(a, efisystab, use_bsd, use_x86bios);
420 a->debug("not found...");
423 a->debug("found at 0x%" PCI_U64_FMT_X "...", rsdp_address);
425 if (!check_and_parse_rsdp(physmem, pagesize, rsdp_address, &rsdt_address, &xsdt_address))
427 a->debug("invalid...");
432 a->debug("searching for ACPI MCFG (XSDT=0x%" PCI_U64_FMT_X ", RSDT=0x%lx)...", xsdt_address, (unsigned long)rsdt_address);
434 xsdt = xsdt_address ? (struct acpi_xsdt *)check_and_map_sdt(physmem, pagesize, xsdt_address, "XSDT", &map_addr, &map_length) : NULL;
437 a->debug("via XSDT...");
438 count = get_xsdt_addresses_count(xsdt);
439 for (i = 0; i < count; i++)
441 sdt = check_and_map_sdt(physmem, pagesize, xsdt->sdt_addresses[i], "MCFG", &map2_addr, &map2_length);
444 mcfg = pci_malloc(a, sdt->length);
445 memcpy(mcfg, sdt, sdt->length);
446 physmem_unmap(physmem, map2_addr, map2_length);
450 physmem_unmap(physmem, map_addr, map_length);
453 a->debug("found...");
458 rsdt = (struct acpi_rsdt *)check_and_map_sdt(physmem, pagesize, rsdt_address, "RSDT", &map_addr, &map_length);
461 a->debug("via RSDT...");
462 count = get_rsdt_addresses_count(rsdt);
463 for (i = 0; i < count; i++)
465 sdt = check_and_map_sdt(physmem, pagesize, rsdt->sdt_addresses[i], "MCFG", &map2_addr, &map2_length);
468 mcfg = pci_malloc(a, sdt->length);
469 memcpy(mcfg, sdt, sdt->length);
470 physmem_unmap(physmem, map2_addr, map2_length);
474 physmem_unmap(physmem, map_addr, map_length);
477 a->debug("found...");
482 a->debug("not found...");
487 get_mcfg_allocation(struct acpi_mcfg *mcfg, unsigned int i, int *domain, u8 *start_bus, u8 *end_bus, u64 *addr, u32 *length)
489 int buses = (int)mcfg->allocations[i].end_bus_number - (int)mcfg->allocations[i].start_bus_number + 1;
492 *domain = mcfg->allocations[i].pci_segment;
494 *start_bus = mcfg->allocations[i].start_bus_number;
496 *end_bus = mcfg->allocations[i].end_bus_number;
498 *addr = mcfg->allocations[i].address;
500 *length = (buses > 0) ? (buses * 32 * 8 * 4096) : 0;
504 parse_next_addrs(const char *addrs, const char **next, int *domain, u8 *start_bus, u8 *end_bus, u64 *addr, u32 *length)
507 const char *sep1, *sep2;
521 endptr = strchr(addrs, ',');
523 addr_len = endptr - addrs;
525 addr_len = strlen(addrs);
528 *next = endptr ? (endptr+1) : NULL;
530 sep1 = memchr(addrs, ':', addr_len);
534 sep2 = memchr(sep1+1, ':', addr_len - (sep1+1 - addrs));
548 if (!isxdigit(*addrs))
551 num = strtol(addrs, &endptr, 16);
552 if (errno || endptr != sep1 || num < 0 || num > INT_MAX)
559 num = strtol(sep1 ? (sep1+1) : addrs, &endptr, 16);
560 if (errno || num < 0 || num > 0xff)
572 num = strtol(endptr+1, &endptr, 16);
573 if (errno || endptr != sep2 || num < 0 || num > 0xff)
575 buses = num - -buses + 1;
582 if (!isxdigit(*(sep2+1)))
586 ullnum = strtoull(sep2+1, &endptr, 16);
587 if (errno || (ullnum & 3))
593 if (endptr == addrs + addr_len)
597 buses = 0xff - -buses + 1;
601 if (start_addr + (unsigned)buses * 32 * 8 * 4096 < start_addr)
604 *length = buses * 32 * 8 * 4096;
608 if (*endptr != '+' || !isxdigit(*(endptr+1)))
611 ullnum = strtoull(endptr+1, &endptr, 16);
612 if (errno || endptr != addrs + addr_len || (ullnum & 3) || ullnum > 256 * 32 * 8 * 4096)
614 if (start_addr + ullnum < start_addr)
616 if (buses > 0 && ullnum > (unsigned)buses * 32 * 8 * 4096)
618 if (buses <= 0 && ullnum > (0xff - (unsigned)-buses + 1) * 32 * 8 * 4096)
622 if (buses <= 0 && end_bus)
623 *end_bus = -buses + (ullnum + 32 * 8 * 4096 - 1) / (32 * 8 * 4096);
630 validate_addrs(const char *addrs)
636 if (!parse_next_addrs(addrs, &addrs, NULL, NULL, NULL, NULL, NULL))
643 calculate_bus_addr(u8 start_bus, u64 start_addr, u32 total_length, u8 bus, u64 *addr, u32 *length)
647 offset = 32*8*4096 * (bus - start_bus);
648 if (offset >= total_length)
651 *addr = start_addr + offset;
652 *length = total_length - offset;
654 if (*length > 32*8*4096)
661 get_bus_addr(struct acpi_mcfg *mcfg, const char *addrs, int domain, u8 bus, u64 *addr, u32 *length)
672 count = get_mcfg_allocations_count(mcfg);
673 for (i = 0; i < count; i++)
675 get_mcfg_allocation(mcfg, i, &cur_domain, &start_bus, &end_bus, &start_addr, &total_length);
676 if (domain == cur_domain && bus >= start_bus && bus <= end_bus)
677 return calculate_bus_addr(start_bus, start_addr, total_length, bus, addr, length);
685 if (!parse_next_addrs(addrs, &addrs, &cur_domain, &start_bus, &end_bus, &start_addr, &total_length))
687 if (domain == cur_domain && bus >= start_bus && bus <= end_bus)
688 return calculate_bus_addr(start_bus, start_addr, total_length, bus, addr, length);
695 munmap_reg(struct pci_access *a)
697 struct ecam_access *eacc = a->backend_data;
698 struct mmap_cache *cache = eacc->cache;
699 struct physmem *physmem = eacc->physmem;
700 long pagesize = eacc->pagesize;
705 physmem_unmap(physmem, cache->map, cache->length + (cache->addr & (pagesize-1)));
711 mmap_reg(struct pci_access *a, int w, int domain, u8 bus, u8 dev, u8 func, int pos, volatile void **reg)
713 struct ecam_access *eacc = a->backend_data;
714 struct mmap_cache *cache = eacc->cache;
715 struct physmem *physmem = eacc->physmem;
716 long pagesize = eacc->pagesize;
723 if (cache && cache->domain == domain && cache->bus == bus && !!cache->w == !!w)
727 length = cache->length;
731 addrs = pci_get_param(a, "ecam.addrs");
732 if (!get_bus_addr(eacc->mcfg, addrs, domain, bus, &addr, &length))
735 map = physmem_map(physmem, addr & ~(pagesize-1), length + (addr & (pagesize-1)), w);
736 if (map == (void *)-1)
740 physmem_unmap(physmem, cache->map, cache->length + (cache->addr & (pagesize-1)));
742 cache = eacc->cache = pci_malloc(a, sizeof(*cache));
746 cache->length = length;
747 cache->domain = domain;
753 * Enhanced Configuration Access Mechanism (ECAM) offset according to:
754 * PCI Express Base Specification, Revision 5.0, Version 1.0, Section 7.2.2, Table 7-1, p. 677
756 offset = ((dev & 0x1f) << 15) | ((func & 0x7) << 12) | (pos & 0xfff);
758 if (offset + 4 > length)
761 *reg = (unsigned char *)map + (addr & (pagesize-1)) + offset;
766 ecam_config(struct pci_access *a)
768 physmem_init_config(a);
769 pci_define_param(a, "ecam.acpimcfg", PCI_PATH_ACPI_MCFG, "Path to the ACPI MCFG table");
770 pci_define_param(a, "ecam.efisystab", PCI_PATH_EFI_SYSTAB, "Path to the EFI system table");
771 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
772 pci_define_param(a, "ecam.bsd", "1", "Use BSD kenv or sysctl to find ACPI MCFG table");
774 #if defined(__amd64__) || defined(__i386__)
775 pci_define_param(a, "ecam.x86bios", "1", "Scan x86 BIOS memory for ACPI MCFG table");
777 pci_define_param(a, "ecam.addrs", "", "Physical addresses of memory mapped PCIe ECAM interface"); /* format: [domain:]start_bus[-end_bus]:start_addr[+length],... */
781 ecam_detect(struct pci_access *a)
783 int use_addrs = 1, use_acpimcfg = 1, use_efisystab = 1, use_bsd = 1, use_x86bios = 1;
784 const char *acpimcfg = pci_get_param(a, "ecam.acpimcfg");
785 const char *efisystab = pci_get_param(a, "ecam.efisystab");
786 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
787 const char *bsd = pci_get_param(a, "ecam.bsd");
789 #if defined(__amd64__) || defined(__i386__)
790 const char *x86bios = pci_get_param(a, "ecam.x86bios");
792 const char *addrs = pci_get_param(a, "ecam.addrs");
798 a->debug("ecam.addrs was not specified...");
804 ret = glob(acpimcfg, GLOB_NOCHECK, NULL, &mcfg_glob);
807 if (access(mcfg_glob.gl_pathv[0], R_OK))
809 a->debug("cannot access acpimcfg: %s: %s...", mcfg_glob.gl_pathv[0], strerror(errno));
812 globfree(&mcfg_glob);
816 a->debug("glob(%s) failed: %d...", acpimcfg, ret);
823 if (!efisystab[0] || access(efisystab, R_OK))
826 a->debug("cannot access efisystab: %s: %s...", efisystab, strerror(errno));
830 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
831 if (strcmp(bsd, "0") == 0)
833 a->debug("not using BSD kenv/sysctl...");
840 #if defined(__amd64__) || defined(__i386__)
841 if (strcmp(x86bios, "0") == 0)
843 a->debug("not using x86 BIOS...");
850 if (!use_addrs && !use_acpimcfg && !use_efisystab && !use_bsd && !use_x86bios)
852 a->debug("no ecam source provided");
856 if (!validate_addrs(addrs))
858 a->debug("ecam.addrs has invalid format %s", addrs);
862 if (physmem_access(a, 0))
864 a->debug("cannot access physical memory: %s", strerror(errno));
869 a->debug("using with ecam addresses %s", addrs);
871 a->debug("using with%s%s%s%s%s%s", use_acpimcfg ? " acpimcfg=" : "", use_acpimcfg ? acpimcfg : "", use_efisystab ? " efisystab=" : "", use_efisystab ? efisystab : "", use_bsd ? " bsd" : "", use_x86bios ? " x86bios" : "");
877 ecam_init(struct pci_access *a)
879 const char *acpimcfg = pci_get_param(a, "ecam.acpimcfg");
880 const char *efisystab = pci_get_param(a, "ecam.efisystab");
881 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
882 const char *bsd = pci_get_param(a, "ecam.bsd");
884 #if defined(__amd64__) || defined(__i386__)
885 const char *x86bios = pci_get_param(a, "ecam.x86bios");
887 const char *addrs = pci_get_param(a, "ecam.addrs");
888 struct physmem *physmem = NULL;
889 struct ecam_access *eacc = NULL;
895 volatile void *test_reg;
897 if (!validate_addrs(addrs))
898 a->error("Option ecam.addrs has invalid address format \"%s\".", addrs);
900 physmem = physmem_open(a, a->writeable);
902 a->error("Cannot open physcal memory: %s.", strerror(errno));
904 pagesize = physmem_get_pagesize(physmem);
906 a->error("Cannot get page size: %s.", strerror(errno));
908 eacc = pci_malloc(a, sizeof(*eacc));
911 eacc->physmem = physmem;
912 eacc->pagesize = pagesize;
913 a->backend_data = eacc;
917 #if defined (__FreeBSD__) || defined (__DragonFly__)
918 if (strcmp(bsd, "0") != 0)
921 #if defined(__amd64__) || defined(__i386__)
922 if (strcmp(x86bios, "0") != 0)
925 eacc->mcfg = find_mcfg(a, acpimcfg, efisystab, use_bsd, use_x86bios);
927 a->error("Option ecam.addrs was not specified and ACPI MCFG table cannot be found.");
931 get_mcfg_allocation(eacc->mcfg, 0, &test_domain, &test_bus, NULL, NULL, NULL);
933 parse_next_addrs(addrs, NULL, &test_domain, &test_bus, NULL, NULL, NULL);
936 if (!mmap_reg(a, 0, test_domain, test_bus, 0, 0, 0, &test_reg))
937 a->error("Cannot map ecam region: %s.", errno ? strerror(errno) : "Unknown error");
941 ecam_cleanup(struct pci_access *a)
943 struct ecam_access *eacc = a->backend_data;
946 physmem_close(eacc->physmem);
947 pci_mfree(eacc->mcfg);
949 a->backend_data = NULL;
953 ecam_scan(struct pci_access *a)
955 const char *addrs = pci_get_param(a, "ecam.addrs");
956 struct ecam_access *eacc = a->backend_data;
961 segments = pci_malloc(a, 0xFFFF/8);
962 memset(segments, 0, 0xFFFF/8);
966 count = get_mcfg_allocations_count(eacc->mcfg);
967 for (i = 0; i < count; i++)
968 segments[eacc->mcfg->allocations[i].pci_segment / 32] |= 1 << (eacc->mcfg->allocations[i].pci_segment % 32);
974 if (parse_next_addrs(addrs, &addrs, &domain, NULL, NULL, NULL, NULL))
975 segments[domain / 32] |= 1 << (domain % 32);
979 for (i = 0; i < 0xFFFF/32; i++)
983 for (j = 0; j < 32; j++)
984 if (segments[i] & (1 << j))
985 pci_generic_scan_domain(a, 32*i + j);
992 ecam_read(struct pci_dev *d, int pos, byte *buf, int len)
999 if (len != 1 && len != 2 && len != 4)
1000 return pci_generic_block_read(d, pos, buf, len);
1002 if (!mmap_reg(d->access, 0, d->domain, d->bus, d->dev, d->func, pos, ®))
1008 buf[0] = physmem_readb(reg);
1011 ((u16 *) buf)[0] = physmem_readw(reg);
1014 ((u32 *) buf)[0] = physmem_readl(reg);
1022 ecam_write(struct pci_dev *d, int pos, byte *buf, int len)
1029 if (len != 1 && len != 2 && len != 4)
1030 return pci_generic_block_read(d, pos, buf, len);
1032 if (!mmap_reg(d->access, 1, d->domain, d->bus, d->dev, d->func, pos, ®))
1038 physmem_writeb(buf[0], reg);
1041 physmem_writew(((u16 *) buf)[0], reg);
1044 physmem_writel(((u32 *) buf)[0], reg);
1051 struct pci_methods pm_ecam = {
1053 "Raw memory mapped access using PCIe ECAM interface",
1059 pci_generic_fill_info,
1062 NULL, /* read_vpd */
1063 NULL, /* init_dev */
1064 NULL /* cleanup_dev */