2 * The PCI Library -- Generic Direct Access Functions
4 * Copyright (c) 1997--2022 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
16 pci_generic_scan_bus(struct pci_access *a, byte *busmap, int domain, int bus)
21 a->debug("Scanning bus %02x for devices...\n", bus);
24 a->warning("Bus %02x seen twice (firmware bug). Ignored.", bus);
31 for (dev=0; dev<32; dev++)
35 for (t->func=0; !t->func || multi && t->func<8; t->func++)
37 u32 vd = pci_read_long(t, PCI_VENDOR_ID);
40 if (!vd || vd == 0xffffffff)
42 ht = pci_read_byte(t, PCI_HEADER_TYPE);
47 d->domain = t->domain;
51 d->vendor_id = vd & 0xffff;
52 d->device_id = vd >> 16U;
53 d->known_fields = PCI_FILL_IDENT;
58 case PCI_HEADER_TYPE_NORMAL:
60 case PCI_HEADER_TYPE_BRIDGE:
61 case PCI_HEADER_TYPE_CARDBUS:
62 pci_generic_scan_bus(a, busmap, domain, pci_read_byte(t, PCI_SECONDARY_BUS));
65 a->debug("Device %04x:%02x:%02x.%d has unknown header type %02x.\n", d->domain, d->bus, d->dev, d->func, ht);
73 pci_generic_scan_domain(struct pci_access *a, int domain)
77 memset(busmap, 0, sizeof(busmap));
78 pci_generic_scan_bus(a, busmap, domain, 0);
82 pci_generic_scan(struct pci_access *a)
84 pci_generic_scan_domain(a, 0);
88 get_hdr_type(struct pci_dev *d)
91 d->hdrtype = pci_read_byte(d, PCI_HEADER_TYPE) & 0x7f;
96 pci_generic_fill_info(struct pci_dev *d, unsigned int flags)
98 struct pci_access *a = d->access;
101 if (want_fill(d, flags, PCI_FILL_IDENT))
103 d->vendor_id = pci_read_word(d, PCI_VENDOR_ID);
104 d->device_id = pci_read_word(d, PCI_DEVICE_ID);
107 if (want_fill(d, flags, PCI_FILL_CLASS))
108 d->device_class = pci_read_word(d, PCI_CLASS_DEVICE);
110 if (want_fill(d, flags, PCI_FILL_CLASS_EXT))
112 d->prog_if = pci_read_byte(d, PCI_CLASS_PROG);
113 d->rev_id = pci_read_byte(d, PCI_REVISION_ID);
116 if (want_fill(d, flags, PCI_FILL_SUBSYS))
118 switch (get_hdr_type(d))
120 case PCI_HEADER_TYPE_NORMAL:
121 d->subsys_vendor_id = pci_read_word(d, PCI_SUBSYSTEM_VENDOR_ID);
122 d->subsys_id = pci_read_word(d, PCI_SUBSYSTEM_ID);
124 case PCI_HEADER_TYPE_BRIDGE:
125 cap = pci_find_cap(d, PCI_CAP_ID_SSVID, PCI_CAP_NORMAL);
128 d->subsys_vendor_id = pci_read_word(d, cap->addr + PCI_SSVID_VENDOR);
129 d->subsys_id = pci_read_word(d, cap->addr + PCI_SSVID_DEVICE);
132 case PCI_HEADER_TYPE_CARDBUS:
133 d->subsys_vendor_id = pci_read_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
134 d->subsys_id = pci_read_word(d, PCI_CB_SUBSYSTEM_ID);
137 clear_fill(d, PCI_FILL_SUBSYS);
141 if (want_fill(d, flags, PCI_FILL_IRQ))
142 d->irq = pci_read_byte(d, PCI_INTERRUPT_LINE);
144 if (want_fill(d, flags, PCI_FILL_BASES))
147 memset(d->base_addr, 0, sizeof(d->base_addr));
148 switch (get_hdr_type(d))
150 case PCI_HEADER_TYPE_NORMAL:
153 case PCI_HEADER_TYPE_BRIDGE:
156 case PCI_HEADER_TYPE_CARDBUS:
162 for (i=0; i<cnt; i++)
164 u32 x = pci_read_long(d, PCI_BASE_ADDRESS_0 + i*4);
165 if (!x || x == (u32) ~0)
167 if ((x & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
171 if ((x & PCI_BASE_ADDRESS_MEM_TYPE_MASK) != PCI_BASE_ADDRESS_MEM_TYPE_64)
174 a->warning("%04x:%02x:%02x.%d: Invalid 64-bit address seen for BAR %d.", d->domain, d->bus, d->dev, d->func, i);
177 u32 y = pci_read_long(d, PCI_BASE_ADDRESS_0 + (++i)*4);
178 #ifdef PCI_HAVE_64BIT_ADDRESS
179 d->base_addr[i-1] = x | (((pciaddr_t) y) << 32);
182 a->warning("%04x:%02x:%02x.%d 64-bit device address ignored.", d->domain, d->bus, d->dev, d->func);
184 d->base_addr[i-1] = x;
192 if (want_fill(d, flags, PCI_FILL_ROM_BASE))
195 d->rom_base_addr = 0;
196 switch (get_hdr_type(d))
198 case PCI_HEADER_TYPE_NORMAL:
199 reg = PCI_ROM_ADDRESS;
201 case PCI_HEADER_TYPE_BRIDGE:
202 reg = PCI_ROM_ADDRESS1;
207 u32 u = pci_read_long(d, reg);
209 d->rom_base_addr = u;
213 pci_scan_caps(d, flags);
217 pci_generic_block_op(struct pci_dev *d, int pos, byte *buf, int len,
218 int (*r)(struct pci_dev *d, int pos, byte *buf, int len))
220 if ((pos & 1) && len >= 1)
222 if (!r(d, pos, buf, 1))
226 if ((pos & 3) && len >= 2)
228 if (!r(d, pos, buf, 2))
230 pos += 2; buf += 2; len -= 2;
234 if (!r(d, pos, buf, 4))
236 pos += 4; buf += 4; len -= 4;
240 if (!r(d, pos, buf, 2))
242 pos += 2; buf += 2; len -= 2;
244 if (len && !r(d, pos, buf, 1))
250 pci_generic_block_read(struct pci_dev *d, int pos, byte *buf, int len)
252 return pci_generic_block_op(d, pos, buf, len, d->access->methods->read);
256 pci_generic_block_write(struct pci_dev *d, int pos, byte *buf, int len)
258 return pci_generic_block_op(d, pos, buf, len, d->access->methods->write);