blob: 27347e3bf88d99f13b7d1afa5eecd39cf4ea62f0 [file] [log] [blame]
Patrick Georgic9f705b2010-06-24 14:15:49 +00001/*
Patrick Georgic9f705b2010-06-24 14:15:49 +00002 *
3 * Copyright (C) 2010 coresystems GmbH
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <libpayload.h>
30#include <pci.h>
31#include <pci/pci.h>
32
33/* libpci shim */
34static pcidev_t libpci_to_lb(struct pci_dev *dev)
35{
36 return PCI_DEV(dev->bus, dev->dev, dev->func);
37}
38
39/* libpci interface */
40u8 pci_read_byte(struct pci_dev *dev, int pos)
41{
Patrick Georgic34ebab2019-11-25 16:42:56 +010042 return pci_read_config8(libpci_to_lb(dev), (uint16_t)pos);
Patrick Georgic9f705b2010-06-24 14:15:49 +000043}
44
45u16 pci_read_word(struct pci_dev *dev, int pos)
46{
Patrick Georgic34ebab2019-11-25 16:42:56 +010047 return pci_read_config16(libpci_to_lb(dev), (uint16_t)pos);
Patrick Georgic9f705b2010-06-24 14:15:49 +000048}
49
50u32 pci_read_long(struct pci_dev *dev, int pos)
51{
Patrick Georgic34ebab2019-11-25 16:42:56 +010052 return pci_read_config32(libpci_to_lb(dev), (uint16_t)pos);
Patrick Georgic9f705b2010-06-24 14:15:49 +000053}
54
55int pci_write_byte(struct pci_dev *dev, int pos, u8 data)
56{
Patrick Georgic34ebab2019-11-25 16:42:56 +010057 pci_write_config8(libpci_to_lb(dev), (uint16_t)pos, data);
Patrick Georgic9f705b2010-06-24 14:15:49 +000058 return 1; /* success */
59}
60
61int pci_write_word(struct pci_dev *dev, int pos, u16 data)
62{
Patrick Georgic34ebab2019-11-25 16:42:56 +010063 pci_write_config16(libpci_to_lb(dev), (uint16_t)pos, data);
Patrick Georgic9f705b2010-06-24 14:15:49 +000064 return 1; /* success */
65}
66
67int pci_write_long(struct pci_dev *dev, int pos, u32 data)
68{
Patrick Georgic34ebab2019-11-25 16:42:56 +010069 pci_write_config32(libpci_to_lb(dev), (uint16_t)pos, data);
Patrick Georgic9f705b2010-06-24 14:15:49 +000070 return 1; /* success */
71}
72
73struct pci_access *pci_alloc(void)
74{
75 struct pci_access *pacc = malloc(sizeof(*pacc));
76 return pacc;
77}
78
Patrick Georgifb2d29e2011-03-01 07:24:53 +000079void pci_init(struct pci_access *pacc)
Patrick Georgic9f705b2010-06-24 14:15:49 +000080{
81 memset(pacc, 0, sizeof(*pacc));
82}
83
Stefan Reinauer6a001132017-07-13 02:20:27 +020084void pci_cleanup(__attribute__((unused)) struct pci_access *pacc)
Patrick Georgifb2d29e2011-03-01 07:24:53 +000085{
86}
87
Patrick Georgic9f705b2010-06-24 14:15:49 +000088void pci_filter_init(struct pci_access* pacc, struct pci_filter* pf)
89{
90 pf->domain = -1;
91 pf->bus = -1;
92 pf->dev = -1;
93 pf->func = -1;
94 pf->vendor = -1;
95 pf->device = -1;
96}
97
Mathias Krause67997d32012-04-03 20:42:01 +020098static char invalid_pci_device_string[] = "invalid pci device string";
Patrick Georgi7f965832011-04-21 18:57:16 +020099
Patrick Georgic9f705b2010-06-24 14:15:49 +0000100/* parse domain:bus:dev.func (with all components but "dev" optional)
101 * into filter.
102 * Returns NULL on success, a string pointer to the error message otherwise.
103 */
104char *pci_filter_parse_slot(struct pci_filter* filter, const char* id)
105{
106 char *endptr;
107
108 filter->func = filter->dev = filter->bus = filter->domain = -1;
109
110 char *funcp = strrchr(id, '.');
111 if (funcp) {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100112 filter->func = strtol(funcp+1, &endptr, 0);
Patrick Georgi7f965832011-04-21 18:57:16 +0200113 if (endptr[0] != '\0') return invalid_pci_device_string;
Patrick Georgic9f705b2010-06-24 14:15:49 +0000114 }
115
116 char *devp = strrchr(id, ':');
117 if (!devp) {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100118 filter->dev = strtol(id, &endptr, 0);
Patrick Georgic9f705b2010-06-24 14:15:49 +0000119 } else {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100120 filter->dev = strtol(devp+1, &endptr, 0);
Patrick Georgic9f705b2010-06-24 14:15:49 +0000121 }
Patrick Georgi7f965832011-04-21 18:57:16 +0200122 if (endptr != funcp) return invalid_pci_device_string;
Patrick Georgic9f705b2010-06-24 14:15:49 +0000123 if (!devp) return NULL;
124
125 char *busp = strchr(id, ':');
126 if (busp == devp) {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100127 filter->bus = strtol(id, &endptr, 0);
Patrick Georgic9f705b2010-06-24 14:15:49 +0000128 } else {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100129 filter->bus = strtol(busp+1, &endptr, 0);
Patrick Georgic9f705b2010-06-24 14:15:49 +0000130 }
Patrick Georgi7f965832011-04-21 18:57:16 +0200131 if (endptr != funcp) return invalid_pci_device_string;
Patrick Georgic9f705b2010-06-24 14:15:49 +0000132 if (busp == devp) return NULL;
133
Patrick Georgic34ebab2019-11-25 16:42:56 +0100134 filter->domain = strtol(id, &endptr, 0);
Patrick Georgi7f965832011-04-21 18:57:16 +0200135 if (endptr != busp) return invalid_pci_device_string;
Patrick Georgic9f705b2010-06-24 14:15:49 +0000136
137 return NULL;
138}
139
140int pci_filter_match(struct pci_filter* pf, struct pci_dev* dev)
141{
142 if ((pf->domain > -1) && (pf->domain != dev->domain))
143 return 0;
144 if ((pf->bus > -1) && (pf->bus != dev->bus))
145 return 0;
146 if ((pf->dev > -1) && (pf->dev != dev->dev))
147 return 0;
148 if ((pf->func > -1) && (pf->func != dev->func))
149 return 0;
150 if ((pf->vendor > -1) && (pf->vendor != dev->vendor_id))
151 return 0;
152 if ((pf->device > -1) && (pf->device != dev->device_id))
153 return 0;
154 return 1;
155}
156
Patrick Georgic34ebab2019-11-25 16:42:56 +0100157static struct pci_dev *pci_scan_single_bus(struct pci_dev *dev, uint8_t bus)
Patrick Georgic9f705b2010-06-24 14:15:49 +0000158{
159 int devfn;
160 u32 val;
161 unsigned char hdr;
162
163 for (devfn = 0; devfn < 0x100; devfn++) {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100164 uint8_t func = devfn & 0x7;
165 uint8_t slot = (devfn >> 3) & 0x1f;
Patrick Georgic9f705b2010-06-24 14:15:49 +0000166
167 val = pci_read_config32(PCI_DEV(bus, slot, func),
168 REG_VENDOR_ID);
169
170 if (val == 0xffffffff || val == 0x00000000 ||
171 val == 0x0000ffff || val == 0xffff0000)
172 continue;
173
174 dev->next = malloc(sizeof(struct pci_dev));
175 dev = dev->next;
176 dev->domain = 0;
177 dev->bus = bus;
178 dev->dev = slot;
179 dev->func = func;
180 dev->vendor_id = val & 0xffff;
Patrick Georgic34ebab2019-11-25 16:42:56 +0100181 dev->device_id = (uint16_t)(val >> 16);
Patrick Georgic9f705b2010-06-24 14:15:49 +0000182 dev->next = 0;
Stefan Reinauere11835e2011-10-31 12:54:00 -0700183
Patrick Georgic9f705b2010-06-24 14:15:49 +0000184 hdr = pci_read_config8(PCI_DEV(bus, slot, func),
185 REG_HEADER_TYPE);
186 hdr &= 0x7F;
187
188 if (hdr == HEADER_TYPE_BRIDGE || hdr == HEADER_TYPE_CARDBUS) {
Patrick Georgic34ebab2019-11-25 16:42:56 +0100189 uint8_t busses;
190 busses = (uint8_t)(pci_read_config32(
191 PCI_DEV(bus, slot, func),
192 REG_PRIMARY_BUS) >> 8);
Patrick Georgic9f705b2010-06-24 14:15:49 +0000193
194 /* Avoid recursion if the new bus is the same as
195 * the old bus (insert lame The Who joke here) */
196
197 if (busses != bus)
198 dev = pci_scan_single_bus(dev, busses);
199 }
200 }
201
202 return dev;
203}
204
205void pci_scan_bus(struct pci_access* pacc)
206{
207 struct pci_dev rootdev;
208 pci_scan_single_bus(&rootdev, 0);
209 pacc->devices = rootdev.next;
210}
211
212struct pci_dev *pci_get_dev(struct pci_access* pacc, u16 domain, u8 bus, u8 dev, u8 func)
213{
214 struct pci_dev *cur = malloc(sizeof(*cur));
215 cur->domain = domain;
216 cur->bus = bus;
217 cur->dev = dev;
218 cur->func = func;
219 return cur;
220}
Nico Huber47926892017-07-26 14:34:09 +0200221
222void pci_free_dev(struct pci_dev *const dev)
223{
224 free(dev);
225}