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.
14 pci_generic_scan_bus(struct pci_access *a, byte *busmap, int bus)
19 a->debug("Scanning bus %02x for devices...\n", bus);
22 a->warning("Bus %02x seen twice (firmware bug). Ignored.", bus);
28 for (dev=0; dev<32; dev++)
32 for (t->func=0; !t->func || multi && t->func<8; t->func++)
34 u32 vd = pci_read_long(t, PCI_VENDOR_ID);
37 if (!vd || vd == 0xffffffff)
39 ht = pci_read_byte(t, PCI_HEADER_TYPE);
47 d->vendor_id = vd & 0xffff;
48 d->device_id = vd >> 16U;
49 d->known_fields = PCI_FILL_IDENT;
54 case PCI_HEADER_TYPE_NORMAL:
56 case PCI_HEADER_TYPE_BRIDGE:
57 case PCI_HEADER_TYPE_CARDBUS:
58 pci_generic_scan_bus(a, busmap, pci_read_byte(t, PCI_SECONDARY_BUS));
61 a->debug("Device %04x:%02x:%02x.%d has unknown header type %02x.\n", d->domain, d->bus, d->dev, d->func, ht);
69 pci_generic_scan(struct pci_access *a)
73 memset(busmap, 0, sizeof(busmap));
74 pci_generic_scan_bus(a, busmap, 0);
78 get_hdr_type(struct pci_dev *d)
81 d->hdrtype = pci_read_byte(d, PCI_HEADER_TYPE) & 0x7f;
86 pci_generic_fill_info(struct pci_dev *d, unsigned int flags)
88 struct pci_access *a = d->access;
91 if (want_fill(d, flags, PCI_FILL_IDENT))
93 d->vendor_id = pci_read_word(d, PCI_VENDOR_ID);
94 d->device_id = pci_read_word(d, PCI_DEVICE_ID);
97 if (want_fill(d, flags, PCI_FILL_CLASS))
98 d->device_class = pci_read_word(d, PCI_CLASS_DEVICE);
100 if (want_fill(d, flags, PCI_FILL_CLASS_EXT))
102 d->prog_if = pci_read_byte(d, PCI_CLASS_PROG);
103 d->rev_id = pci_read_byte(d, PCI_REVISION_ID);
106 if (want_fill(d, flags, PCI_FILL_SUBSYS))
108 switch (get_hdr_type(d))
110 case PCI_HEADER_TYPE_NORMAL:
111 d->subsys_vendor_id = pci_read_word(d, PCI_SUBSYSTEM_VENDOR_ID);
112 d->subsys_id = pci_read_word(d, PCI_SUBSYSTEM_ID);
114 case PCI_HEADER_TYPE_BRIDGE:
115 cap = pci_find_cap(d, PCI_CAP_ID_SSVID, PCI_CAP_NORMAL);
118 d->subsys_vendor_id = pci_read_word(d, cap->addr + PCI_SSVID_VENDOR);
119 d->subsys_id = pci_read_word(d, cap->addr + PCI_SSVID_DEVICE);
122 case PCI_HEADER_TYPE_CARDBUS:
123 d->subsys_vendor_id = pci_read_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
124 d->subsys_id = pci_read_word(d, PCI_CB_SUBSYSTEM_ID);
127 clear_fill(d, PCI_FILL_SUBSYS);
131 if (want_fill(d, flags, PCI_FILL_IRQ))
132 d->irq = pci_read_byte(d, PCI_INTERRUPT_LINE);
134 if (want_fill(d, flags, PCI_FILL_BASES))
137 memset(d->base_addr, 0, sizeof(d->base_addr));
138 switch (get_hdr_type(d))
140 case PCI_HEADER_TYPE_NORMAL:
143 case PCI_HEADER_TYPE_BRIDGE:
146 case PCI_HEADER_TYPE_CARDBUS:
152 for (i=0; i<cnt; i++)
154 u32 x = pci_read_long(d, PCI_BASE_ADDRESS_0 + i*4);
155 if (!x || x == (u32) ~0)
157 if ((x & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
161 if ((x & PCI_BASE_ADDRESS_MEM_TYPE_MASK) != PCI_BASE_ADDRESS_MEM_TYPE_64)
164 a->warning("%04x:%02x:%02x.%d: Invalid 64-bit address seen for BAR %d.", d->domain, d->bus, d->dev, d->func, i);
167 u32 y = pci_read_long(d, PCI_BASE_ADDRESS_0 + (++i)*4);
168 #ifdef PCI_HAVE_64BIT_ADDRESS
169 d->base_addr[i-1] = x | (((pciaddr_t) y) << 32);
172 a->warning("%04x:%02x:%02x.%d 64-bit device address ignored.", d->domain, d->bus, d->dev, d->func);
174 d->base_addr[i-1] = x;
182 if (want_fill(d, flags, PCI_FILL_ROM_BASE))
185 d->rom_base_addr = 0;
186 switch (get_hdr_type(d))
188 case PCI_HEADER_TYPE_NORMAL:
189 reg = PCI_ROM_ADDRESS;
191 case PCI_HEADER_TYPE_BRIDGE:
192 reg = PCI_ROM_ADDRESS1;
197 u32 u = pci_read_long(d, reg);
199 d->rom_base_addr = u;
203 pci_scan_caps(d, flags);
207 pci_generic_block_op(struct pci_dev *d, int pos, byte *buf, int len,
208 int (*r)(struct pci_dev *d, int pos, byte *buf, int len))
210 if ((pos & 1) && len >= 1)
212 if (!r(d, pos, buf, 1))
216 if ((pos & 3) && len >= 2)
218 if (!r(d, pos, buf, 2))
220 pos += 2; buf += 2; len -= 2;
224 if (!r(d, pos, buf, 4))
226 pos += 4; buf += 4; len -= 4;
230 if (!r(d, pos, buf, 2))
232 pos += 2; buf += 2; len -= 2;
234 if (len && !r(d, pos, buf, 1))
240 pci_generic_block_read(struct pci_dev *d, int pos, byte *buf, int len)
242 return pci_generic_block_op(d, pos, buf, len, d->access->methods->read);
246 pci_generic_block_write(struct pci_dev *d, int pos, byte *buf, int len)
248 return pci_generic_block_op(d, pos, buf, len, d->access->methods->write);