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"
22 #ifndef PCI_OS_WINDOWS
27 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
28 #include <sys/sysctl.h>
31 #if defined (__FreeBSD__) || defined (__DragonFly__)
36 #include "win32-helpers.h"
61 char asl_compiler_id[4];
62 u32 asl_compiler_revision;
96 // Back-end data linked to struct pci_access
98 struct acpi_mcfg *mcfg;
99 struct mmap_cache *cache;
100 struct physmem *physmem;
105 get_rsdt_addresses_count(struct acpi_rsdt *rsdt)
107 return (rsdt->sdt.length - ((unsigned char*)&rsdt->sdt_addresses - (unsigned char *)rsdt)) / sizeof(rsdt->sdt_addresses[0]);
111 get_xsdt_addresses_count(struct acpi_xsdt *xsdt)
113 return (xsdt->sdt.length - ((unsigned char*)&xsdt->sdt_addresses - (unsigned char *)xsdt)) / sizeof(xsdt->sdt_addresses[0]);
117 get_mcfg_allocations_count(struct acpi_mcfg *mcfg)
119 return (mcfg->sdt.length - ((unsigned char *)&mcfg->allocations - (unsigned char *)mcfg)) / sizeof(mcfg->allocations[0]);
123 calculate_checksum(const u8 *bytes, int len)
128 checksum -= *(bytes++);
132 static struct acpi_sdt *
133 check_and_map_sdt(struct physmem *physmem, long pagesize, u64 addr, const char *signature, void **map_addr, u32 *map_length)
135 struct acpi_sdt *sdt;
136 char sdt_signature[sizeof(sdt->signature)];
140 if (addr + sizeof(*sdt) < addr)
143 map = physmem_map(physmem, addr & ~(pagesize-1), sizeof(*sdt) + (addr & (pagesize-1)), 0);
144 if (map == (void *)-1)
147 sdt = (struct acpi_sdt *)((unsigned char *)map + (addr & (pagesize-1)));
148 length = sdt->length;
149 memcpy(sdt_signature, sdt->signature, sizeof(sdt->signature));
151 physmem_unmap(physmem, map, sizeof(*sdt) + (addr & (pagesize-1)));
153 if (memcmp(sdt_signature, signature, sizeof(sdt_signature)) != 0)
155 if (length < sizeof(*sdt))
158 map = physmem_map(physmem, addr & ~(pagesize-1), length + (addr & (pagesize-1)), 0);
159 if (map == (void *)-1)
162 sdt = (struct acpi_sdt *)((unsigned char *)map + (addr & (pagesize-1)));
164 if (calculate_checksum((u8 *)sdt, sdt->length) != 0)
166 physmem_unmap(physmem, map, length + (addr & (pagesize-1)));
171 *map_length = length + (addr & (pagesize-1));
176 check_rsdp(struct acpi_rsdp *rsdp)
178 if (memcmp(rsdp->signature, "RSD PTR ", sizeof(rsdp->signature)) != 0)
180 if (calculate_checksum((u8 *)rsdp, sizeof(*rsdp)) != 0)
186 check_and_parse_rsdp(struct physmem *physmem, long pagesize, u64 addr, u32 *rsdt_address, u64 *xsdt_address)
188 struct acpi_rsdp *rsdp;
189 unsigned char buf[sizeof(*rsdp) + sizeof(*rsdp->rsdp20)];
192 map = physmem_map(physmem, addr & ~(pagesize-1), sizeof(buf) + (addr & (pagesize-1)), 0);
193 if (map == (void *)-1)
196 rsdp = (struct acpi_rsdp *)buf;
197 memcpy(rsdp, (unsigned char *)map + (addr & (pagesize-1)), sizeof(buf));
199 physmem_unmap(physmem, map, sizeof(buf));
201 if (!check_rsdp(rsdp))
204 *rsdt_address = rsdp->rsdt_address;
206 if (rsdp->revision != 0 &&
207 (*rsdp->rsdp20).length == sizeof(*rsdp) + sizeof(*rsdp->rsdp20) &&
208 calculate_checksum((u8 *)rsdp, (*rsdp->rsdp20).length) == 0)
209 *xsdt_address = (*rsdp->rsdp20).xsdt_address;
217 find_rsdp_address(struct pci_access *a, const char *efisystab, int use_bsd UNUSED, int use_x86bios UNUSED)
220 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
227 #if defined(__amd64__) || defined(__i386__)
228 struct ecam_access *eacc = a->backend_data;
229 struct physmem *physmem = eacc->physmem;
230 long pagesize = eacc->pagesize;
243 a->debug("reading EFI system table: %s...", efisystab);
244 f = fopen(efisystab, "r");
247 while (fgets(buf, sizeof(buf), f))
250 while (len > 0 && buf[len-1] == '\n')
252 if (strncmp(buf, "ACPI20=", 7) == 0 && isxdigit(buf[7]))
255 ullnum = strtoull(buf+7, &endptr, 16);
256 if (!errno && !*endptr)
259 else if (strncmp(buf, "ACPI=", 5) == 0 && isxdigit(buf[5]))
262 ullnum = strtoull(buf+5, &endptr, 16);
263 if (!errno && !*endptr)
270 a->debug("opening failed: %s...", strerror(errno));
278 #if defined (__FreeBSD__) || defined (__DragonFly__)
281 /* First try FreeBSD kenv hint.acpi.0.rsdp */
282 a->debug("calling kenv hint.acpi.0.rsdp...");
283 if (kenv(KENV_GET, "hint.acpi.0.rsdp", buf, sizeof(buf)) > 0)
286 ullnum = strtoull(buf, &endptr, 16);
287 if (!errno && !*endptr)
291 /* Then try FreeBSD sysctl machdep.acpi_root */
292 a->debug("calling sysctl machdep.acpi_root...");
294 if (sysctlbyname("machdep.acpi_root", &ulnum, &len, NULL, 0) == 0)
299 #if defined(__NetBSD__)
302 /* Try NetBSD sysctl hw.acpi.root */
303 a->debug("calling sysctl hw.acpi.root...");
305 if (sysctlbyname("hw.acpi.root", &ulnum, &len, NULL, 0) == 0)
310 #if defined(__amd64__) || defined(__i386__)
315 /* Scan first kB of Extended BIOS Data Area */
316 a->debug("reading EBDA location from BDA...");
317 map = physmem_map(physmem, 0, 0x40E + 2, 0);
318 if (map != (void *)-1)
320 ebda = (u64)physmem_readw((unsigned char *)map + 0x40E) << 4;
321 if (physmem_unmap(physmem, map, 0x40E + 2) != 0)
322 a->debug("unmapping of BDA failed: %s...", strerror(errno));
325 a->debug("scanning first kB of EBDA at 0x%" PCI_U64_FMT_X "...", ebda);
326 map = physmem_map(physmem, ebda & ~(pagesize-1), 1024 + (ebda & (pagesize-1)), 0);
327 if (map != (void *)-1)
329 for (addr = ebda & (pagesize-1); addr < (ebda & (pagesize-1)) + 1024; addr += 16)
331 if (check_rsdp((struct acpi_rsdp *)((unsigned char *)map + addr)))
333 rsdp_addr = (ebda & ~(pagesize-1)) + addr;
337 if (physmem_unmap(physmem, map, 1024 + (ebda & (pagesize-1))) != 0)
338 a->debug("unmapping of EBDA failed: %s...", strerror(errno));
341 a->debug("mapping of EBDA failed: %s...", strerror(errno));
344 a->debug("EBDA location 0x%" PCI_U64_FMT_X " is insane...", ebda);
347 a->debug("mapping of BDA failed: %s...", strerror(errno));
353 /* Scan the main BIOS area below 1 MB */
354 a->debug("scanning BIOS below 1 MB...");
355 map = physmem_map(physmem, 0xE0000, 0x20000, 0);
356 if (map != (void *)-1)
358 for (addr = 0x0; addr < 0x20000; addr += 16)
360 if (check_rsdp((struct acpi_rsdp *)((unsigned char *)map + addr)))
362 rsdp_addr = 0xE0000 + addr;
366 if (physmem_unmap(physmem, map, 0x20000) != 0)
367 a->debug("unmapping of BIOS failed: %s...", strerror(errno));
370 a->debug("mapping of BIOS failed: %s...", strerror(errno));
380 #ifdef PCI_OS_WINDOWS
381 #ifndef ERROR_NOT_FOUND
382 #define ERROR_NOT_FOUND 1168
384 static struct acpi_mcfg *
385 get_system_firmware_table_acpi_mcfg(struct pci_access *a)
387 UINT (*WINAPI MyGetSystemFirmwareTable)(DWORD FirmwareTableProviderSignature, DWORD FirmwareTableID, PVOID pFirmwareTableBuffer, DWORD BufferSize);
388 struct acpi_mcfg *mcfg;
393 kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
397 /* Function GetSystemFirmwareTable() is available since Windows Vista. */
398 MyGetSystemFirmwareTable = (void *)GetProcAddress(kernel32, "GetSystemFirmwareTable");
399 if (!MyGetSystemFirmwareTable)
402 /* 0x41435049 = 'ACPI', 0x4746434D = 'MCFG' */
403 size = MyGetSystemFirmwareTable(0x41435049, 0x4746434D, NULL, 0);
406 error = GetLastError();
407 if (error == ERROR_INVALID_FUNCTION) /* ACPI is not present. */
409 else if (error == ERROR_NOT_FOUND) /* MCFG table is not present. */
411 a->debug("Cannot retrieve ACPI MCFG table: %s.\n", win32_strerror(error));
415 mcfg = pci_malloc(a, size);
417 if (MyGetSystemFirmwareTable(0x41435049, 0x4746434D, mcfg, size) != size)
419 error = GetLastError();
420 a->debug("Cannot retrieve ACPI MCFG table: %s.\n", win32_strerror(error));
425 if (size < sizeof(*mcfg) || size < mcfg->sdt.length || calculate_checksum((u8 *)mcfg, mcfg->sdt.length) != 0)
427 a->debug("ACPI MCFG table is broken.\n");
436 static struct acpi_mcfg *
437 find_mcfg(struct pci_access *a, const char *acpimcfg, const char *efisystab, int use_bsd, int use_x86bios)
439 struct ecam_access *eacc = a->backend_data;
440 struct physmem *physmem = eacc->physmem;
441 long pagesize = eacc->pagesize;
442 struct acpi_xsdt *xsdt;
443 struct acpi_rsdt *rsdt;
444 struct acpi_mcfg *mcfg;
445 struct acpi_sdt *sdt;
446 unsigned int i, count;
457 #ifndef PCI_OS_WINDOWS
464 #ifdef PCI_OS_WINDOWS
465 if (strcmp(acpimcfg, "GetSystemFirmwareTable()") == 0)
467 a->debug("reading acpi mcfg via GetSystemFirmwareTable()...");
468 mcfg = get_system_firmware_table_acpi_mcfg(a);
471 a->debug("failed...");
477 ret = glob(acpimcfg, GLOB_NOCHECK, NULL, &mcfg_glob);
479 a->debug("glob(%s) failed: %d...", acpimcfg, ret);
482 path = mcfg_glob.gl_pathv[0];
484 a->debug("reading acpi mcfg file: %s...", path);
485 mcfg_file = fopen(path, "rb");
486 #ifndef PCI_OS_WINDOWS
487 globfree(&mcfg_glob);
491 if (fseek(mcfg_file, 0, SEEK_END) == 0)
492 length = ftell(mcfg_file);
495 if (length > 0 && (size_t)length > sizeof(*mcfg))
498 mcfg = pci_malloc(a, length);
499 if (fread(mcfg, 1, length, mcfg_file) == (size_t)length &&
500 memcmp(mcfg->sdt.signature, "MCFG", 4) == 0 &&
501 mcfg->sdt.length <= (size_t)length &&
502 calculate_checksum((u8 *)mcfg, mcfg->sdt.length) == 0)
510 a->debug("failed...");
514 a->debug("searching for ACPI RSDP...");
515 rsdp_address = find_rsdp_address(a, efisystab, use_bsd, use_x86bios);
518 a->debug("not found...");
521 a->debug("found at 0x%" PCI_U64_FMT_X "...", rsdp_address);
523 if (!check_and_parse_rsdp(physmem, pagesize, rsdp_address, &rsdt_address, &xsdt_address))
525 a->debug("invalid...");
530 a->debug("searching for ACPI MCFG (XSDT=0x%" PCI_U64_FMT_X ", RSDT=0x%lx)...", xsdt_address, (unsigned long)rsdt_address);
532 xsdt = xsdt_address ? (struct acpi_xsdt *)check_and_map_sdt(physmem, pagesize, xsdt_address, "XSDT", &map_addr, &map_length) : NULL;
535 a->debug("via XSDT...");
536 count = get_xsdt_addresses_count(xsdt);
537 for (i = 0; i < count; i++)
539 sdt = check_and_map_sdt(physmem, pagesize, xsdt->sdt_addresses[i], "MCFG", &map2_addr, &map2_length);
542 mcfg = pci_malloc(a, sdt->length);
543 memcpy(mcfg, sdt, sdt->length);
544 physmem_unmap(physmem, map2_addr, map2_length);
548 physmem_unmap(physmem, map_addr, map_length);
551 a->debug("found...");
556 rsdt = (struct acpi_rsdt *)check_and_map_sdt(physmem, pagesize, rsdt_address, "RSDT", &map_addr, &map_length);
559 a->debug("via RSDT...");
560 count = get_rsdt_addresses_count(rsdt);
561 for (i = 0; i < count; i++)
563 sdt = check_and_map_sdt(physmem, pagesize, rsdt->sdt_addresses[i], "MCFG", &map2_addr, &map2_length);
566 mcfg = pci_malloc(a, sdt->length);
567 memcpy(mcfg, sdt, sdt->length);
568 physmem_unmap(physmem, map2_addr, map2_length);
572 physmem_unmap(physmem, map_addr, map_length);
575 a->debug("found...");
580 a->debug("not found...");
585 get_mcfg_allocation(struct acpi_mcfg *mcfg, unsigned int i, int *domain, u8 *start_bus, u8 *end_bus, u64 *addr, u32 *length)
587 int buses = (int)mcfg->allocations[i].end_bus_number - (int)mcfg->allocations[i].start_bus_number + 1;
590 *domain = mcfg->allocations[i].pci_segment;
592 *start_bus = mcfg->allocations[i].start_bus_number;
594 *end_bus = mcfg->allocations[i].end_bus_number;
596 *addr = mcfg->allocations[i].address;
598 *length = (buses > 0) ? (buses * 32 * 8 * 4096) : 0;
602 parse_next_addrs(const char *addrs, const char **next, int *domain, u8 *start_bus, u8 *end_bus, u64 *addr, u32 *length)
605 const char *sep1, *sep2;
619 endptr = strchr(addrs, ',');
621 addr_len = endptr - addrs;
623 addr_len = strlen(addrs);
626 *next = endptr ? (endptr+1) : NULL;
628 sep1 = memchr(addrs, ':', addr_len);
632 sep2 = memchr(sep1+1, ':', addr_len - (sep1+1 - addrs));
646 if (!isxdigit(*addrs))
649 num = strtol(addrs, &endptr, 16);
650 if (errno || endptr != sep1 || num < 0 || num > INT_MAX)
657 num = strtol(sep1 ? (sep1+1) : addrs, &endptr, 16);
658 if (errno || num < 0 || num > 0xff)
670 num = strtol(endptr+1, &endptr, 16);
671 if (errno || endptr != sep2 || num < 0 || num > 0xff)
673 buses = num - -buses + 1;
680 if (!isxdigit(*(sep2+1)))
684 ullnum = strtoull(sep2+1, &endptr, 16);
685 if (errno || (ullnum & 3))
691 if (endptr == addrs + addr_len)
695 buses = 0xff - -buses + 1;
699 if (start_addr + (unsigned)buses * 32 * 8 * 4096 < start_addr)
702 *length = buses * 32 * 8 * 4096;
706 if (*endptr != '+' || !isxdigit(*(endptr+1)))
709 ullnum = strtoull(endptr+1, &endptr, 16);
710 if (errno || endptr != addrs + addr_len || (ullnum & 3) || ullnum > 256 * 32 * 8 * 4096)
712 if (start_addr + ullnum < start_addr)
714 if (buses > 0 && ullnum > (unsigned)buses * 32 * 8 * 4096)
716 if (buses <= 0 && ullnum > (0xff - (unsigned)-buses + 1) * 32 * 8 * 4096)
720 if (buses <= 0 && end_bus)
721 *end_bus = -buses + (ullnum + 32 * 8 * 4096 - 1) / (32 * 8 * 4096);
728 validate_addrs(const char *addrs)
734 if (!parse_next_addrs(addrs, &addrs, NULL, NULL, NULL, NULL, NULL))
741 calculate_bus_addr(u8 start_bus, u64 start_addr, u32 total_length, u8 bus, u64 *addr, u32 *length)
745 offset = 32*8*4096 * (bus - start_bus);
746 if (offset >= total_length)
749 *addr = start_addr + offset;
750 *length = total_length - offset;
752 if (*length > 32*8*4096)
759 get_bus_addr(struct acpi_mcfg *mcfg, const char *addrs, int domain, u8 bus, u64 *addr, u32 *length)
770 count = get_mcfg_allocations_count(mcfg);
771 for (i = 0; i < count; i++)
773 get_mcfg_allocation(mcfg, i, &cur_domain, &start_bus, &end_bus, &start_addr, &total_length);
774 if (domain == cur_domain && bus >= start_bus && bus <= end_bus)
775 return calculate_bus_addr(start_bus, start_addr, total_length, bus, addr, length);
783 if (!parse_next_addrs(addrs, &addrs, &cur_domain, &start_bus, &end_bus, &start_addr, &total_length))
785 if (domain == cur_domain && bus >= start_bus && bus <= end_bus)
786 return calculate_bus_addr(start_bus, start_addr, total_length, bus, addr, length);
793 munmap_reg(struct pci_access *a)
795 struct ecam_access *eacc = a->backend_data;
796 struct mmap_cache *cache = eacc->cache;
797 struct physmem *physmem = eacc->physmem;
798 long pagesize = eacc->pagesize;
803 physmem_unmap(physmem, cache->map, cache->length + (cache->addr & (pagesize-1)));
809 mmap_reg(struct pci_access *a, int w, int domain, u8 bus, u8 dev, u8 func, int pos, volatile void **reg)
811 struct ecam_access *eacc = a->backend_data;
812 struct mmap_cache *cache = eacc->cache;
813 struct physmem *physmem = eacc->physmem;
814 long pagesize = eacc->pagesize;
821 if (cache && cache->domain == domain && cache->bus == bus && !!cache->w == !!w)
825 length = cache->length;
829 addrs = pci_get_param(a, "ecam.addrs");
830 if (!get_bus_addr(eacc->mcfg, addrs, domain, bus, &addr, &length))
833 map = physmem_map(physmem, addr & ~(pagesize-1), length + (addr & (pagesize-1)), w);
834 if (map == (void *)-1)
838 physmem_unmap(physmem, cache->map, cache->length + (cache->addr & (pagesize-1)));
840 cache = eacc->cache = pci_malloc(a, sizeof(*cache));
844 cache->length = length;
845 cache->domain = domain;
851 * Enhanced Configuration Access Mechanism (ECAM) offset according to:
852 * PCI Express Base Specification, Revision 5.0, Version 1.0, Section 7.2.2, Table 7-1, p. 677
854 offset = ((dev & 0x1f) << 15) | ((func & 0x7) << 12) | (pos & 0xfff);
856 if (offset + 4 > length)
859 *reg = (unsigned char *)map + (addr & (pagesize-1)) + offset;
864 ecam_config(struct pci_access *a)
866 physmem_init_config(a);
867 pci_define_param(a, "ecam.acpimcfg", PCI_PATH_ACPI_MCFG, "Path to the ACPI MCFG table");
868 pci_define_param(a, "ecam.efisystab", PCI_PATH_EFI_SYSTAB, "Path to the EFI system table");
869 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
870 pci_define_param(a, "ecam.bsd", "1", "Use BSD kenv or sysctl to find ACPI MCFG table");
872 #if defined(__amd64__) || defined(__i386__)
873 pci_define_param(a, "ecam.x86bios", "1", "Scan x86 BIOS memory for ACPI MCFG table");
875 pci_define_param(a, "ecam.addrs", "", "Physical addresses of memory mapped PCIe ECAM interface"); /* format: [domain:]start_bus[-end_bus]:start_addr[+length],... */
879 ecam_detect(struct pci_access *a)
881 int use_addrs = 1, use_acpimcfg = 1, use_efisystab = 1, use_bsd = 1, use_x86bios = 1;
882 const char *acpimcfg = pci_get_param(a, "ecam.acpimcfg");
883 const char *efisystab = pci_get_param(a, "ecam.efisystab");
884 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
885 const char *bsd = pci_get_param(a, "ecam.bsd");
887 #if defined(__amd64__) || defined(__i386__)
888 const char *x86bios = pci_get_param(a, "ecam.x86bios");
890 const char *addrs = pci_get_param(a, "ecam.addrs");
891 struct ecam_access *eacc;
892 #ifndef PCI_OS_WINDOWS
899 a->debug("ecam.addrs was not specified...");
905 #ifndef PCI_OS_WINDOWS
906 ret = glob(acpimcfg, GLOB_NOCHECK, NULL, &mcfg_glob);
909 if (access(mcfg_glob.gl_pathv[0], R_OK))
911 a->debug("cannot access acpimcfg: %s: %s...", mcfg_glob.gl_pathv[0], strerror(errno));
914 globfree(&mcfg_glob);
918 a->debug("glob(%s) failed: %d...", acpimcfg, ret);
926 #ifndef PCI_OS_WINDOWS
927 if (!efisystab[0] || access(efisystab, R_OK))
930 a->debug("cannot access efisystab: %s: %s...", efisystab, strerror(errno));
935 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
936 if (strcmp(bsd, "0") == 0)
938 a->debug("not using BSD kenv/sysctl...");
945 #if defined(__amd64__) || defined(__i386__)
946 if (strcmp(x86bios, "0") == 0)
948 a->debug("not using x86 BIOS...");
955 if (!use_addrs && !use_acpimcfg && !use_efisystab && !use_bsd && !use_x86bios)
957 a->debug("no ecam source provided");
961 if (!validate_addrs(addrs))
963 a->debug("ecam.addrs has invalid format %s", addrs);
967 if (physmem_access(a, 0))
969 a->debug("cannot access physical memory: %s", strerror(errno));
975 eacc = pci_malloc(a, sizeof(*eacc));
977 eacc->physmem = physmem_open(a, a->writeable);
980 a->debug("cannot open physcal memory: %s.", strerror(errno));
985 eacc->pagesize = physmem_get_pagesize(eacc->physmem);
986 if (eacc->pagesize <= 0)
988 a->debug("Cannot get page size: %s.", strerror(errno));
989 physmem_close(eacc->physmem);
996 a->backend_data = eacc;
997 eacc->mcfg = find_mcfg(a, acpimcfg, efisystab, use_bsd, use_x86bios);
1000 physmem_close(eacc->physmem);
1002 a->backend_data = NULL;
1008 a->debug("using with ecam addresses %s", addrs);
1010 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" : "");
1016 ecam_init(struct pci_access *a)
1018 const char *acpimcfg = pci_get_param(a, "ecam.acpimcfg");
1019 const char *efisystab = pci_get_param(a, "ecam.efisystab");
1020 #if defined (__FreeBSD__) || defined (__DragonFly__) || defined(__NetBSD__)
1021 const char *bsd = pci_get_param(a, "ecam.bsd");
1023 #if defined(__amd64__) || defined(__i386__)
1024 const char *x86bios = pci_get_param(a, "ecam.x86bios");
1026 const char *addrs = pci_get_param(a, "ecam.addrs");
1027 struct physmem *physmem = NULL;
1028 struct ecam_access *eacc = a->backend_data;
1031 int use_x86bios = 0;
1032 int test_domain = 0;
1034 volatile void *test_reg;
1036 if (!validate_addrs(addrs))
1037 a->error("Option ecam.addrs has invalid address format \"%s\".", addrs);
1041 physmem = physmem_open(a, a->writeable);
1043 a->error("Cannot open physcal memory: %s.", strerror(errno));
1045 pagesize = physmem_get_pagesize(physmem);
1047 a->error("Cannot get page size: %s.", strerror(errno));
1049 eacc = pci_malloc(a, sizeof(*eacc));
1052 eacc->physmem = physmem;
1053 eacc->pagesize = pagesize;
1054 a->backend_data = eacc;
1059 #if defined (__FreeBSD__) || defined (__DragonFly__)
1060 if (strcmp(bsd, "0") != 0)
1063 #if defined(__amd64__) || defined(__i386__)
1064 if (strcmp(x86bios, "0") != 0)
1068 eacc->mcfg = find_mcfg(a, acpimcfg, efisystab, use_bsd, use_x86bios);
1070 a->error("Option ecam.addrs was not specified and ACPI MCFG table cannot be found.");
1074 get_mcfg_allocation(eacc->mcfg, 0, &test_domain, &test_bus, NULL, NULL, NULL);
1076 parse_next_addrs(addrs, NULL, &test_domain, &test_bus, NULL, NULL, NULL);
1079 if (!mmap_reg(a, 0, test_domain, test_bus, 0, 0, 0, &test_reg))
1080 a->error("Cannot map ecam region: %s.", errno ? strerror(errno) : "Unknown error");
1084 ecam_cleanup(struct pci_access *a)
1086 struct ecam_access *eacc = a->backend_data;
1089 physmem_close(eacc->physmem);
1090 pci_mfree(eacc->mcfg);
1092 a->backend_data = NULL;
1096 ecam_scan(struct pci_access *a)
1098 const char *addrs = pci_get_param(a, "ecam.addrs");
1099 struct ecam_access *eacc = a->backend_data;
1104 segments = pci_malloc(a, 0xFFFF/8);
1105 memset(segments, 0, 0xFFFF/8);
1109 count = get_mcfg_allocations_count(eacc->mcfg);
1110 for (i = 0; i < count; i++)
1111 segments[eacc->mcfg->allocations[i].pci_segment / 32] |= 1 << (eacc->mcfg->allocations[i].pci_segment % 32);
1117 if (parse_next_addrs(addrs, &addrs, &domain, NULL, NULL, NULL, NULL))
1118 segments[domain / 32] |= 1 << (domain % 32);
1122 for (i = 0; i < 0xFFFF/32; i++)
1126 for (j = 0; j < 32; j++)
1127 if (segments[i] & (1 << j))
1128 pci_generic_scan_domain(a, 32*i + j);
1131 pci_mfree(segments);
1135 ecam_read(struct pci_dev *d, int pos, byte *buf, int len)
1142 if (len != 1 && len != 2 && len != 4)
1143 return pci_generic_block_read(d, pos, buf, len);
1145 if (!mmap_reg(d->access, 0, d->domain, d->bus, d->dev, d->func, pos, ®))
1151 buf[0] = physmem_readb(reg);
1154 ((u16 *) buf)[0] = physmem_readw(reg);
1157 ((u32 *) buf)[0] = physmem_readl(reg);
1165 ecam_write(struct pci_dev *d, int pos, byte *buf, int len)
1172 if (len != 1 && len != 2 && len != 4)
1173 return pci_generic_block_read(d, pos, buf, len);
1175 if (!mmap_reg(d->access, 1, d->domain, d->bus, d->dev, d->func, pos, ®))
1181 physmem_writeb(buf[0], reg);
1184 physmem_writew(((u16 *) buf)[0], reg);
1187 physmem_writel(((u32 *) buf)[0], reg);
1194 struct pci_methods pm_ecam = {
1196 .help = "Raw memory mapped access using PCIe ECAM interface",
1197 .config = ecam_config,
1198 .detect = ecam_detect,
1200 .cleanup = ecam_cleanup,
1202 .fill_info = pci_generic_fill_info,
1204 .write = ecam_write,