From c9f705b12195b7c1ff9814f76c78c76d4b8498b1 Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Thu, 24 Jun 2010 14:15:49 +0000 Subject: Incomplete implementation of libpci's (of pciutils) interface. No pciutils code was harmed in its production - this code was written by looking at flashrom's expectations, so there's no license pollution. Signed-off-by: Patrick Georgi Acked-by: Peter Stuge git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5645 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1 --- payloads/libpayload/Makefile | 4 +- payloads/libpayload/include/pci/pci.h | 76 +++++++++++ payloads/libpayload/libpci/Makefile.inc | 30 +++++ payloads/libpayload/libpci/libpci.c | 216 ++++++++++++++++++++++++++++++++ 4 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 payloads/libpayload/include/pci/pci.h create mode 100644 payloads/libpayload/libpci/Makefile.inc create mode 100644 payloads/libpayload/libpci/libpci.c (limited to 'payloads') diff --git a/payloads/libpayload/Makefile b/payloads/libpayload/Makefile index dd105431cf..bb00613921 100644 --- a/payloads/libpayload/Makefile +++ b/payloads/libpayload/Makefile @@ -77,7 +77,7 @@ ARCHDIR-$(CONFIG_TARGET_POWERPC) := powerpc PLATFORM-y += arch/$(ARCHDIR-y)/Makefile.inc TARGETS-y := -BUILD-y := crypto/Makefile.inc libc/Makefile.inc drivers/Makefile.inc +BUILD-y := crypto/Makefile.inc libc/Makefile.inc drivers/Makefile.inc libpci/Makefile.inc BUILD-$(CONFIG_TINYCURSES) += curses/Makefile.inc # The primary target needs to be here before we include the @@ -145,7 +145,7 @@ prepare: $(Q)mkdir -p $(obj)/util/kconfig/lxdialog $(Q)mkdir -p $(obj)/crypto $(obj)/curses $(obj)/drivers/video $(Q)mkdir -p $(obj)/drivers/usb - $(Q)mkdir -p $(obj)/arch/$(ARCHDIR-y) $(obj)/lib/$(ARCHDIR-y) $(obj)/libc + $(Q)mkdir -p $(obj)/arch/$(ARCHDIR-y) $(obj)/lib/$(ARCHDIR-y) $(obj)/libc $(obj)/libpci $(Q)mkdir -p $(obj)/lib/$(ARCHDIR-y) $(Q)mkdir -p $(obj)/include diff --git a/payloads/libpayload/include/pci/pci.h b/payloads/libpayload/include/pci/pci.h new file mode 100644 index 0000000000..0a06604e5d --- /dev/null +++ b/payloads/libpayload/include/pci/pci.h @@ -0,0 +1,76 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2010 coresystems GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _PCI_PCI_H +#define _PCI_PCI_H + +#include + +#define PCI_CLASS_DEVICE REG_CLASS_DEV +#define PCI_SUBSYSTEM_VENDOR_ID REG_SUBSYS_VENDOR_ID +#define PCI_SUBSYSTEM_ID REG_SUBSYS_ID + +struct pci_dev { + u16 domain; + u8 bus, dev, func; + u16 vendor_id, device_id; + struct pci_dev *next; +}; + +/* + * values to match devices against. + * "-1" means "don't care", everything else requires an exact match + */ +struct pci_filter { + int domain, bus, dev, func; + int vendor, device; + struct pci_dev *devices; +}; + +struct pci_access { + struct pci_dev *devices; +}; + +u8 pci_read_byte(struct pci_dev *dev, int pos); +u16 pci_read_word(struct pci_dev *dev, int pos); +u32 pci_read_long(struct pci_dev *dev, int pos); + +int pci_write_byte(struct pci_dev *dev, int pos, u8 data); +int pci_write_word(struct pci_dev *dev, int pos, u16 data); +int pci_write_long(struct pci_dev *dev, int pos, u32 data); + +struct pci_access *pci_alloc(void); +void pci_init(struct pci_access*); +char *pci_filter_parse_slot(struct pci_filter*, const char*); +int pci_filter_match(struct pci_filter*, struct pci_dev*); +void pci_filter_init(struct pci_access*, struct pci_filter*); +void pci_scan_bus(struct pci_access*); +struct pci_dev *pci_get_dev(struct pci_access*, u16, u8, u8, u8); + +#endif diff --git a/payloads/libpayload/libpci/Makefile.inc b/payloads/libpayload/libpci/Makefile.inc new file mode 100644 index 0000000000..5c3c3ea95f --- /dev/null +++ b/payloads/libpayload/libpci/Makefile.inc @@ -0,0 +1,30 @@ +## +## This file is part of the libpayload project. +## +## Copyright (C) 2010 coresystems GmbH +## +## Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions +## are met: +## 1. Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## 2. Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in the +## documentation and/or other materials provided with the distribution. +## 3. The name of the author may not be used to endorse or promote products +## derived from this software without specific prior written permission. +## +## THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +## SUCH DAMAGE. +## + +TARGETS-y += libpci/libpci.o diff --git a/payloads/libpayload/libpci/libpci.c b/payloads/libpayload/libpci/libpci.c new file mode 100644 index 0000000000..09b51e5133 --- /dev/null +++ b/payloads/libpayload/libpci/libpci.c @@ -0,0 +1,216 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2010 coresystems GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +/* libpci shim */ +static pcidev_t libpci_to_lb(struct pci_dev *dev) +{ + return PCI_DEV(dev->bus, dev->dev, dev->func); +} + +/* libpci interface */ +u8 pci_read_byte(struct pci_dev *dev, int pos) +{ + return pci_read_config8(libpci_to_lb(dev), pos); +} + +u16 pci_read_word(struct pci_dev *dev, int pos) +{ + return pci_read_config16(libpci_to_lb(dev), pos); +} + +u32 pci_read_long(struct pci_dev *dev, int pos) +{ + return pci_read_config32(libpci_to_lb(dev), pos); +} + +int pci_write_byte(struct pci_dev *dev, int pos, u8 data) +{ + pci_write_config8(libpci_to_lb(dev), pos, data); + return 1; /* success */ +} + +int pci_write_word(struct pci_dev *dev, int pos, u16 data) +{ + pci_write_config16(libpci_to_lb(dev), pos, data); + return 1; /* success */ +} + +int pci_write_long(struct pci_dev *dev, int pos, u32 data) +{ + pci_write_config32(libpci_to_lb(dev), pos, data); + return 1; /* success */ +} + +struct pci_access *pci_alloc(void) +{ + struct pci_access *pacc = malloc(sizeof(*pacc)); + return pacc; +} + +void pci_init(struct pci_access* pacc) +{ + memset(pacc, 0, sizeof(*pacc)); +} + +void pci_filter_init(struct pci_access* pacc, struct pci_filter* pf) +{ + pf->domain = -1; + pf->bus = -1; + pf->dev = -1; + pf->func = -1; + pf->vendor = -1; + pf->device = -1; +} + +/* parse domain:bus:dev.func (with all components but "dev" optional) + * into filter. + * Returns NULL on success, a string pointer to the error message otherwise. + */ +char *pci_filter_parse_slot(struct pci_filter* filter, const char* id) +{ + char *endptr; + + filter->func = filter->dev = filter->bus = filter->domain = -1; + + char *funcp = strrchr(id, '.'); + if (funcp) { + filter->func = strtoul(funcp+1, &endptr, 0); + if (endptr[0] != '\0') return "invalid pci device string"; + } + + char *devp = strrchr(id, ':'); + if (!devp) { + filter->dev = strtoul(id, &endptr, 0); + } else { + filter->dev = strtoul(devp+1, &endptr, 0); + } + if (endptr != funcp) return "invalid pci device string"; + if (!devp) return NULL; + + char *busp = strchr(id, ':'); + if (busp == devp) { + filter->bus = strtoul(id, &endptr, 0); + } else { + filter->bus = strtoul(busp+1, &endptr, 0); + } + if (endptr != funcp) return "invalid pci device string"; + if (busp == devp) return NULL; + + filter->domain = strtoul(id, &endptr, 0); + if (endptr != busp) return "invalid pci device string"; + + return NULL; +} + +int pci_filter_match(struct pci_filter* pf, struct pci_dev* dev) +{ + if ((pf->domain > -1) && (pf->domain != dev->domain)) + return 0; + if ((pf->bus > -1) && (pf->bus != dev->bus)) + return 0; + if ((pf->dev > -1) && (pf->dev != dev->dev)) + return 0; + if ((pf->func > -1) && (pf->func != dev->func)) + return 0; + if ((pf->vendor > -1) && (pf->vendor != dev->vendor_id)) + return 0; + if ((pf->device > -1) && (pf->device != dev->device_id)) + return 0; + return 1; +} + +static struct pci_dev *pci_scan_single_bus(struct pci_dev *dev, int bus) +{ + int devfn; + u32 val; + unsigned char hdr; + + for (devfn = 0; devfn < 0x100; devfn++) { + int func = devfn & 0x7; + int slot = (devfn >> 3) & 0x1f; + + val = pci_read_config32(PCI_DEV(bus, slot, func), + REG_VENDOR_ID); + + if (val == 0xffffffff || val == 0x00000000 || + val == 0x0000ffff || val == 0xffff0000) + continue; + + dev->next = malloc(sizeof(struct pci_dev)); + dev = dev->next; + dev->domain = 0; + dev->bus = bus; + dev->dev = slot; + dev->func = func; + dev->vendor_id = val & 0xffff; + dev->device_id = val >> 16; + dev->next = 0; + + hdr = pci_read_config8(PCI_DEV(bus, slot, func), + REG_HEADER_TYPE); + hdr &= 0x7F; + + if (hdr == HEADER_TYPE_BRIDGE || hdr == HEADER_TYPE_CARDBUS) { + unsigned int busses; + busses = pci_read_config32(PCI_DEV(bus, slot, func), + REG_PRIMARY_BUS); + busses = (busses >> 8) & 0xFF; + + /* Avoid recursion if the new bus is the same as + * the old bus (insert lame The Who joke here) */ + + if (busses != bus) + dev = pci_scan_single_bus(dev, busses); + } + } + + return dev; +} + +void pci_scan_bus(struct pci_access* pacc) +{ + struct pci_dev rootdev; + pci_scan_single_bus(&rootdev, 0); + pacc->devices = rootdev.next; +} + +struct pci_dev *pci_get_dev(struct pci_access* pacc, u16 domain, u8 bus, u8 dev, u8 func) +{ + struct pci_dev *cur = malloc(sizeof(*cur)); + cur->domain = domain; + cur->bus = bus; + cur->dev = dev; + cur->func = func; + return cur; +} + -- cgit v1.2.3