1 /*
2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2011 NetApp, Inc.
5 * Copyright (c) 2018-2022 Intel Corporation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``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 NETAPP, INC 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 * $FreeBSD$
29 *
30 * Only support Type 0 and Type 1 PCI(e) device. Remove PC-Card type support.
31 *
32 */
33 #include <types.h>
34 #include <asm/lib/spinlock.h>
35 #include <asm/io.h>
36 #include <asm/pgtable.h>
37 #include <asm/mmu.h>
38 #include <pci.h>
39 #include <uart16550.h>
40 #include <logmsg.h>
41 #include <asm/pci_dev.h>
42 #include <asm/vtd.h>
43 #include <asm/lib/bits.h>
44 #include <asm/board.h>
45 #include <platform_acpi_info.h>
46 #include <hash.h>
47 #include <util.h>
48
49 #define PDEV_HLIST_HASHBITS 6U
50 #define PDEV_HLIST_HASHSIZE (1U << PDEV_HLIST_HASHBITS)
51
52 static uint32_t num_pci_pdev;
53 static struct pci_pdev pci_pdevs[CONFIG_MAX_PCI_DEV_NUM];
54 static struct hlist_head pdevs_hlist_heads[PDEV_HLIST_HASHSIZE];
55
56 /* For HV owned pdev */
57 static uint32_t num_hv_owned_pci_pdev;
58 static struct pci_pdev *hv_owned_pci_pdevs[CONFIG_MAX_PCI_DEV_NUM];
59
get_hv_owned_pdev_num(void)60 uint32_t get_hv_owned_pdev_num(void)
61 {
62 return num_hv_owned_pci_pdev;
63 }
64
get_hv_owned_pdevs(void)65 const struct pci_pdev **get_hv_owned_pdevs(void)
66 {
67 return (const struct pci_pdev **)hv_owned_pci_pdevs;
68 }
69
70 static struct pci_mmcfg_region phys_pci_mmcfg = {
71 .address = DEFAULT_PCI_MMCFG_BASE,
72 .start_bus = DEFAULT_PCI_MMCFG_START_BUS,
73 .end_bus = DEFAULT_PCI_MMCFG_END_BUS,
74 };
75
76 #ifdef CONFIG_ACPI_PARSE_ENABLED
set_mmcfg_region(struct pci_mmcfg_region * region)77 void set_mmcfg_region(struct pci_mmcfg_region *region)
78 {
79 phys_pci_mmcfg = *region;
80 }
81 #endif
82
get_mmcfg_region(void)83 struct pci_mmcfg_region *get_mmcfg_region(void)
84 {
85 return &phys_pci_mmcfg;
86 }
87
88 #if defined(HV_DEBUG)
pio_off_to_address(union pci_bdf bdf,uint32_t offset)89 static inline uint32_t pio_off_to_address(union pci_bdf bdf, uint32_t offset)
90
91 {
92 uint32_t addr = (uint32_t)bdf.value;
93
94 addr <<= 8U;
95 addr |= (offset | PCI_CFG_ENABLE);
96 return addr;
97 }
98
pci_pio_read_cfg(union pci_bdf bdf,uint32_t offset,uint32_t bytes)99 static uint32_t pci_pio_read_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes)
100 {
101 uint32_t addr;
102 uint32_t val;
103
104 addr = pio_off_to_address(bdf, offset);
105
106 /* Write address to ADDRESS register */
107 pio_write32(addr, (uint16_t)PCI_CONFIG_ADDR);
108
109 /* Read result from DATA register */
110 switch (bytes) {
111 case 1U:
112 val = (uint32_t)pio_read8((uint16_t)PCI_CONFIG_DATA + ((uint16_t)offset & 3U));
113 break;
114 case 2U:
115 val = (uint32_t)pio_read16((uint16_t)PCI_CONFIG_DATA + ((uint16_t)offset & 2U));
116 break;
117 default:
118 val = pio_read32((uint16_t)PCI_CONFIG_DATA);
119 break;
120 }
121
122 return val;
123 }
124
pci_pio_write_cfg(union pci_bdf bdf,uint32_t offset,uint32_t bytes,uint32_t val)125 static void pci_pio_write_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes, uint32_t val)
126 {
127 uint32_t addr = pio_off_to_address(bdf, offset);
128
129 /* Write address to ADDRESS register */
130 pio_write32(addr, (uint16_t)PCI_CONFIG_ADDR);
131
132 /* Write value to DATA register */
133 switch (bytes) {
134 case 1U:
135 pio_write8((uint8_t)val, (uint16_t)PCI_CONFIG_DATA + ((uint16_t)offset & 3U));
136 break;
137 case 2U:
138 pio_write16((uint16_t)val, (uint16_t)PCI_CONFIG_DATA + ((uint16_t)offset & 2U));
139 break;
140 default:
141 pio_write32(val, (uint16_t)PCI_CONFIG_DATA);
142 break;
143 }
144 }
145 #else
pci_pio_read_cfg(__unused union pci_bdf bdf,__unused uint32_t offset,__unused uint32_t bytes)146 static uint32_t pci_pio_read_cfg(__unused union pci_bdf bdf,
147 __unused uint32_t offset, __unused uint32_t bytes)
148 {
149 return ~0U;
150 }
151
pci_pio_write_cfg(__unused union pci_bdf bdf,__unused uint32_t offset,__unused uint32_t bytes,__unused uint32_t val)152 static void pci_pio_write_cfg(__unused union pci_bdf bdf,
153 __unused uint32_t offset, __unused uint32_t bytes, __unused uint32_t val)
154 {
155 }
156 #endif
157
158 static const struct pci_cfg_ops pci_pio_cfg_ops = {
159 .pci_read_cfg = pci_pio_read_cfg,
160 .pci_write_cfg = pci_pio_write_cfg,
161 };
162
163 /*
164 * @pre offset < 0x1000U
165 * @pre phys_pci_mmcfg.address 4K-byte alignment
166 */
mmcfg_off_to_address(union pci_bdf bdf,uint32_t offset)167 static inline uint32_t mmcfg_off_to_address(union pci_bdf bdf, uint32_t offset)
168 {
169 return (uint32_t)phys_pci_mmcfg.address + (((uint32_t)bdf.value << 12U) | offset);
170 }
171
172 /*
173 * @pre bytes == 1U || bytes == 2U || bytes == 4U
174 * @pre offset 1-byte alignment if byte == 1U
175 * 2-byte alignment if byte == 2U
176 * 4-byte alignment if byte == 4U
177 */
pci_mmcfg_read_cfg(union pci_bdf bdf,uint32_t offset,uint32_t bytes)178 static uint32_t pci_mmcfg_read_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes)
179 {
180 uint32_t addr = mmcfg_off_to_address(bdf, offset);
181 void *hva = hpa2hva(addr);
182
183 ASSERT(pci_is_valid_access(offset, bytes), "the offset should be aligned with 2/4 byte\n");
184
185 return (uint32_t)mmio_read(hva, bytes);
186 }
187
188 /*
189 * @pre bytes == 1U || bytes == 2U || bytes == 4U
190 * @pre offset 1-byte alignment if byte == 1U
191 * 2-byte alignment if byte == 2U
192 * 4-byte alignment if byte == 4U
193 */
pci_mmcfg_write_cfg(union pci_bdf bdf,uint32_t offset,uint32_t bytes,uint32_t val)194 static void pci_mmcfg_write_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes, uint32_t val)
195 {
196 uint32_t addr = mmcfg_off_to_address(bdf, offset);
197 void *hva = hpa2hva(addr);
198
199 ASSERT(pci_is_valid_access(offset, bytes), "the offset should be aligned with 2/4 byte\n");
200
201 mmio_write(hva, bytes, val);
202 }
203
204 static const struct pci_cfg_ops pci_mmcfg_cfg_ops = {
205 .pci_read_cfg = pci_mmcfg_read_cfg,
206 .pci_write_cfg = pci_mmcfg_write_cfg,
207 };
208
209 static const struct pci_cfg_ops *acrn_pci_cfg_ops = &pci_pio_cfg_ops;
210
pci_switch_to_mmio_cfg_ops(void)211 void pci_switch_to_mmio_cfg_ops(void)
212 {
213 acrn_pci_cfg_ops = &pci_mmcfg_cfg_ops;
214 }
215
216 /*
217 * @pre bytes == 1U || bytes == 2U || bytes == 4U
218 * @pre must not be called before pci_switch_to_mmio_cfg_ops in release version.
219 */
pci_pdev_read_cfg(union pci_bdf bdf,uint32_t offset,uint32_t bytes)220 uint32_t pci_pdev_read_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes)
221 {
222 uint32_t val = ~0U;
223
224 val = acrn_pci_cfg_ops->pci_read_cfg(bdf, offset, bytes);
225
226 return val;
227 }
228
229 /*
230 * @pre bytes == 1U || bytes == 2U || bytes == 4U
231 * @pre must not be called before pci_switch_to_mmio_cfg_ops in release version.
232 */
pci_pdev_write_cfg(union pci_bdf bdf,uint32_t offset,uint32_t bytes,uint32_t val)233 void pci_pdev_write_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes, uint32_t val)
234 {
235 acrn_pci_cfg_ops->pci_write_cfg(bdf, offset, bytes, val);
236 }
237
pdev_need_bar_restore(const struct pci_pdev * pdev)238 bool pdev_need_bar_restore(const struct pci_pdev *pdev)
239 {
240 bool need_restore = false;
241 uint32_t idx, bar;
242
243 for (idx = 0U; idx < pdev->nr_bars; idx++) {
244 bar = pci_pdev_read_cfg(pdev->bdf, pci_bar_offset(idx), 4U);
245 if (bar != pdev->bars[idx].phy_bar) {
246 need_restore = true;
247 break;
248 }
249 }
250
251 return need_restore;
252 }
253
get_pci_bar_resource(union pci_bdf bdf,uint32_t offset,struct pci_bar_resource * res)254 static void get_pci_bar_resource(union pci_bdf bdf, uint32_t offset, struct pci_bar_resource *res)
255 {
256 res->phy_bar = pci_pdev_read_cfg(bdf, offset, 4U);
257
258 pci_pdev_write_cfg(bdf, offset, 4U, ~0U);
259 res->size_mask = pci_pdev_read_cfg(bdf, offset, 4U);
260 pci_pdev_write_cfg(bdf, offset, 4U, res->phy_bar);
261 }
262
pdev_save_bar(struct pci_pdev * pdev)263 static inline void pdev_save_bar(struct pci_pdev *pdev)
264 {
265 uint32_t idx;
266
267 for (idx = 0U; idx < pdev->nr_bars; idx++) {
268 get_pci_bar_resource(pdev->bdf, pci_bar_offset(idx), &pdev->bars[idx]);
269 }
270 }
271
pdev_restore_bar(const struct pci_pdev * pdev)272 void pdev_restore_bar(const struct pci_pdev *pdev)
273 {
274 uint32_t idx;
275
276 for (idx = 0U; idx < pdev->nr_bars; idx++) {
277 pci_pdev_write_cfg(pdev->bdf, pci_bar_offset(idx), 4U, pdev->bars[idx].phy_bar);
278 }
279 }
280
pci_find_pdev(uint16_t pbdf)281 static const struct pci_pdev *pci_find_pdev(uint16_t pbdf)
282 {
283 struct hlist_node *n;
284 const struct pci_pdev *found = NULL, *tmp;
285
286 hlist_for_each (n, &pdevs_hlist_heads[hash64(pbdf, PDEV_HLIST_HASHBITS)]) {
287 tmp = hlist_entry(n, struct pci_pdev, link);
288 if (pbdf == tmp->bdf.value) {
289 found = tmp;
290 break;
291 }
292 }
293 return found;
294 }
295
296 /* @brief: Find the DRHD index corresponding to a PCI device
297 * Runs through the pci_pdevs and returns the value in drhd_idx
298 * member from pdev structure that matches matches B:D.F
299 *
300 * @pbdf[in] B:D.F of a PCI device
301 *
302 * @return if there is a matching pbdf in pci_pdevs, pdev->drhd_idx, else INVALID_DRHD_INDEX
303 */
304
pci_lookup_drhd_for_pbdf(uint16_t pbdf)305 uint32_t pci_lookup_drhd_for_pbdf(uint16_t pbdf)
306 {
307 const struct pci_pdev *pdev = pci_find_pdev(pbdf);
308 return (pdev != NULL) ? pdev->drhd_index : INVALID_DRHD_INDEX;
309 }
310
311 /* enable: 1: enable INTx; 0: Disable INTx */
enable_disable_pci_intx(union pci_bdf bdf,bool enable)312 void enable_disable_pci_intx(union pci_bdf bdf, bool enable)
313 {
314 uint32_t cmd, new_cmd;
315
316 /* Set or clear the INTXDIS bit in COMMAND register */
317 cmd = pci_pdev_read_cfg(bdf, PCIR_COMMAND, 2U);
318 if (enable) {
319 new_cmd = cmd & ~PCIM_CMD_INTxDIS;
320 } else {
321 new_cmd = cmd | PCIM_CMD_INTxDIS;
322 }
323
324 if ((cmd ^ new_cmd) != 0U) {
325 pci_pdev_write_cfg(bdf, PCIR_COMMAND, 0x2U, new_cmd);
326 }
327 }
328
is_plat_hidden_pdev(union pci_bdf bdf)329 bool is_plat_hidden_pdev(union pci_bdf bdf)
330 {
331 static uint32_t hidden_pdevs_num = MAX_HIDDEN_PDEVS_NUM;
332 uint32_t hidden_idx;
333 bool hidden = false;
334
335 for (hidden_idx = 0U; hidden_idx < hidden_pdevs_num; hidden_idx++) {
336 if (bdf_is_equal(plat_hidden_pdevs[hidden_idx], bdf)) {
337 hidden = true;
338 break;
339 }
340 }
341
342 return hidden;
343 }
344
is_hv_owned_pdev(union pci_bdf pbdf)345 bool is_hv_owned_pdev(union pci_bdf pbdf)
346 {
347 bool ret = false;
348 uint32_t i;
349
350 for (i = 0U; i < num_hv_owned_pci_pdev; i++) {
351 if (bdf_is_equal(pbdf, hv_owned_pci_pdevs[i]->bdf)) {
352 pr_info("hv owned dev: (%x:%x:%x)", pbdf.bits.b, pbdf.bits.d, pbdf.bits.f);
353 ret = true;
354 break;
355 }
356 }
357 return ret;
358 }
359
360 /*
361 * quantity of uint64_t to encode a bitmap of all bus values
362 * TODO: PCI_BUSMAX is a good candidate to move to
363 * generated platform files.
364 */
365 #define BUSES_BITMAP_LEN ((PCI_BUSMAX + 1U) >> 6U)
366
367 /*
368 * must be >= total Endpoints in all DRDH devscope
369 * TODO: BDF_MAPPING_NUM is a good candidate to move to
370 * generated platform files.
371 */
372 #define BDF_MAPPING_NUM 32U
373
374 struct pci_bdf_to_drhd_index_mapping {
375 union pci_bdf dev_scope_bdf;
376 uint32_t dev_scope_drhd_index;
377 };
378
379 struct pci_bdf_mapping_group {
380 uint32_t pci_bdf_map_count;
381 struct pci_bdf_to_drhd_index_mapping bdf_map[BDF_MAPPING_NUM];
382 };
383
384 struct pci_bus_num_to_drhd_index_mapping {
385 uint8_t bus_under_scan;
386 uint32_t bus_drhd_index;
387 };
388
pci_check_override_drhd_index(union pci_bdf pbdf,const struct pci_bdf_mapping_group * const bdfs_from_drhds,uint32_t current_drhd_index)389 static uint32_t pci_check_override_drhd_index(union pci_bdf pbdf,
390 const struct pci_bdf_mapping_group *const bdfs_from_drhds,
391 uint32_t current_drhd_index)
392 {
393 uint32_t bdfi;
394 uint32_t bdf_drhd_index = current_drhd_index;
395
396 for (bdfi = 0U; bdfi < bdfs_from_drhds->pci_bdf_map_count; bdfi++) {
397 if (bdfs_from_drhds->bdf_map[bdfi].dev_scope_bdf.value == pbdf.value) {
398 /*
399 * Override current_drhd_index
400 */
401 bdf_drhd_index =
402 bdfs_from_drhds->bdf_map[bdfi].dev_scope_drhd_index;
403 }
404 }
405
406 return bdf_drhd_index;
407 }
408
409 /* @brief: scan PCI physical devices from specific bus.
410 * walks through the bus to scan PCI physical devices and using the discovered physical devices
411 * to initialize pdevs, each pdev can only provdie a physical device information (like bdf, bar,
412 * capabilities) before using this pdev, it needs to use the pdev to initialize a per VM device
413 * configuration(acrn_vm_pci_dev_config), call init_one_dev_config or init_all_dev_config to do this.
414 */
scan_pci_hierarchy(uint8_t bus,uint64_t buses_visited[BUSES_BITMAP_LEN],const struct pci_bdf_mapping_group * const bdfs_from_drhds,uint32_t drhd_index)415 static void scan_pci_hierarchy(uint8_t bus, uint64_t buses_visited[BUSES_BITMAP_LEN],
416 const struct pci_bdf_mapping_group *const bdfs_from_drhds, uint32_t drhd_index)
417 {
418 uint32_t vendor;
419 uint8_t hdr_type, dev, func, func_max;
420 union pci_bdf pbdf;
421 uint8_t current_bus_index;
422 uint32_t current_drhd_index, bdf_drhd_index;
423 struct pci_pdev *pdev;
424
425 struct pci_bus_num_to_drhd_index_mapping bus_map[PCI_BUSMAX + 1U]; /* FIFO queue of buses to walk */
426 uint32_t s = 0U, e = 0U; /* start and end index into queue */
427
428 bus_map[e].bus_under_scan = bus;
429 bus_map[e].bus_drhd_index = drhd_index;
430 e = e + 1U;
431 while (s < e) {
432 current_bus_index = bus_map[s].bus_under_scan;
433 current_drhd_index = bus_map[s].bus_drhd_index;
434 s = s + 1U;
435
436 bitmap_set_nolock(current_bus_index,
437 &buses_visited[current_bus_index >> 6U]);
438
439 pbdf.bits.b = current_bus_index;
440 if ((pbdf.bits.b < phys_pci_mmcfg.start_bus) || (pbdf.bits.b > phys_pci_mmcfg.end_bus)) {
441 continue;
442 }
443
444 for (dev = 0U; dev <= PCI_SLOTMAX; dev++) {
445 pbdf.bits.d = dev;
446 pbdf.bits.f = 0U;
447 hdr_type = (uint8_t)pci_pdev_read_cfg(pbdf, PCIR_HDRTYPE, 1U);
448
449 /* Do not probe beyond function 0 if not a multi-function device
450 * unless device supports ARI or SR-IOV (PCIe spec r5.0 §7.5.1.1.9)
451 */
452 func_max = is_pci_cfg_multifunction(hdr_type) ? PCI_FUNCMAX : 0U;
453 for (func = 0U; func <= func_max; func++) {
454
455 pbdf.bits.f = func;
456
457 vendor = pci_pdev_read_cfg(pbdf, PCIR_VENDOR, 2U);
458
459 if (!is_pci_vendor_valid(vendor)) {
460 continue;
461 }
462
463 bdf_drhd_index = pci_check_override_drhd_index(pbdf, bdfs_from_drhds,
464 current_drhd_index);
465 pdev = pci_init_pdev(pbdf, bdf_drhd_index);
466 /* NOTE: This touch logic change: if a bridge own by HV as its children */
467 if ((pdev != NULL) && is_bridge(pdev)) {
468 bus_map[e].bus_under_scan =
469 (uint8_t)pci_pdev_read_cfg(pbdf, PCIR_SECBUS_1, 1U);
470 bus_map[e].bus_drhd_index = bdf_drhd_index;
471 e = e + 1U;
472 }
473 }
474 }
475 }
476 }
477
478 /*
479 * @brief: Setup bdfs_from_drhds data structure as the DMAR tables are walked searching
480 * for PCI device scopes. bdfs_from_drhds is used later in scan_pci_hierarchy
481 * to map the right DRHD unit to the PCI device
482 */
pci_add_bdf_from_drhd(union pci_bdf bdf,struct pci_bdf_mapping_group * const bdfs_from_drhds,uint32_t drhd_index)483 static void pci_add_bdf_from_drhd(union pci_bdf bdf, struct pci_bdf_mapping_group *const bdfs_from_drhds,
484 uint32_t drhd_index)
485 {
486 if (bdfs_from_drhds->pci_bdf_map_count < BDF_MAPPING_NUM) {
487 bdfs_from_drhds->bdf_map[bdfs_from_drhds->pci_bdf_map_count].dev_scope_bdf = bdf;
488 bdfs_from_drhds->bdf_map[bdfs_from_drhds->pci_bdf_map_count].dev_scope_drhd_index = drhd_index;
489 bdfs_from_drhds->pci_bdf_map_count++;
490 } else {
491 ASSERT(bdfs_from_drhds->pci_bdf_map_count < BDF_MAPPING_NUM,
492 "Compare value in BDF_MAPPING_NUM against those in ACPI DMAR tables");
493 }
494 }
495
496
497 /*
498 * @brief: Setup bdfs_from_drhds data structure as the DMAR tables are walked searching
499 * for PCI device scopes. bdfs_from_drhds is used later in scan_pci_hierarchy
500 * to map the right DRHD unit to the PCI device.
501 * TODO: bdfs_from_drhds is a good candidate to be part of generated platform
502 * info.
503 */
pci_parse_iommu_devscopes(struct pci_bdf_mapping_group * const bdfs_from_drhds,uint32_t * drhd_idx_pci_all)504 static void pci_parse_iommu_devscopes(struct pci_bdf_mapping_group *const bdfs_from_drhds,
505 uint32_t *drhd_idx_pci_all)
506 {
507 union pci_bdf bdf;
508 uint32_t drhd_index, devscope_index;
509
510 for (drhd_index = 0U; drhd_index < plat_dmar_info.drhd_count; drhd_index++) {
511 for (devscope_index = 0U; devscope_index < plat_dmar_info.drhd_units[drhd_index].dev_cnt;
512 devscope_index++) {
513 bdf.fields.bus = plat_dmar_info.drhd_units[drhd_index].devices[devscope_index].bus;
514 bdf.fields.devfun = plat_dmar_info.drhd_units[drhd_index].devices[devscope_index].devfun;
515
516 if ((plat_dmar_info.drhd_units[drhd_index].devices[devscope_index].type ==
517 ACPI_DMAR_SCOPE_TYPE_ENDPOINT) ||
518 (plat_dmar_info.drhd_units[drhd_index].devices[devscope_index].type ==
519 ACPI_DMAR_SCOPE_TYPE_BRIDGE)) {
520 pci_add_bdf_from_drhd(bdf, bdfs_from_drhds, drhd_index);
521 } else {
522 /*
523 * Do nothing for IOAPIC, ACPI namespace and
524 * MSI Capable HPET device scope
525 */
526 }
527 }
528
529 if ((plat_dmar_info.drhd_units[drhd_index].flags & DRHD_FLAG_INCLUDE_PCI_ALL_MASK)
530 == DRHD_FLAG_INCLUDE_PCI_ALL_MASK) {
531 *drhd_idx_pci_all = drhd_index;
532 }
533 }
534 }
535
536 /*
537 * There are some rules to config PCI bridge: try to avoid interference between Service VM and RTVM or
538 * pre-launched VM; and to support some features like SRIOV by default, so as following:
539 * 1. disable interrupt, including INTx and MSI.
540 * 2. enable ARI if it's a PCIe bridge and all its sub devices support ARI (need check further).
541 * 3. enable ACS. (now assume BIOS does it), could check and do it in HV in the future.
542 *
543 */
config_pci_bridge(const struct pci_pdev * pdev)544 static void config_pci_bridge(const struct pci_pdev *pdev)
545 {
546 uint32_t offset, val, msgctrl;
547
548 /* for command regsiters, disable INTx */
549 val = pci_pdev_read_cfg(pdev->bdf, PCIR_COMMAND, 2U);
550 pci_pdev_write_cfg(pdev->bdf, PCIR_COMMAND, 2U, (uint16_t)val | PCIM_CMD_INTxDIS);
551
552 /* disale MSI */
553 if (pdev->msi_capoff != 0x00UL) {
554 offset = pdev->msi_capoff + PCIR_MSI_CTRL;
555
556 msgctrl = pci_pdev_read_cfg(pdev->bdf, offset, 2U);
557 msgctrl &= ~PCIM_MSICTRL_MSI_ENABLE;
558 pci_pdev_write_cfg(pdev->bdf, offset, 2U, msgctrl);
559 }
560
561 /* Enable ARI if PCIe bridge could support it for SRIOV needs it */
562 if (pdev->pcie_capoff != 0x00UL) {
563 offset = pdev->pcie_capoff + PCIR_PCIE_DEVCAP2;
564 val = pci_pdev_read_cfg(pdev->bdf, offset, 2U);
565
566 if ((val & PCIM_PCIE_DEVCAP2_ARI) != 0U) {
567 offset = pdev->pcie_capoff + PCIR_PCIE_DEVCTL2;
568
569 val = pci_pdev_read_cfg(pdev->bdf, offset, 2U);
570 val |= PCIM_PCIE_DEVCTL2_ARI;
571 pci_pdev_write_cfg(pdev->bdf, offset, 2U, val);
572 }
573 }
574 }
575
576 /*
577 * @brief: walks through all pdevs that have been initialized and determine
578 * which pdevs need to be added to pci dev_config. The pdevs added to pci
579 * dev_config will be exposed to Service VM finally.
580 */
init_all_dev_config(void)581 static void init_all_dev_config(void)
582 {
583 uint32_t idx, cnt = 0U;
584 uint32_t total = num_pci_pdev;
585 struct pci_pdev *pdev = NULL;
586
587 for (idx = 0U; idx < num_pci_pdev; idx++) {
588 pdev = &pci_pdevs[idx];
589
590 if (is_bridge(pdev)) {
591 config_pci_bridge(pdev);
592 }
593
594 /*
595 * FIXME: Mask the SR-IOV capability instead drop the device
596 * when supporting PCIe extended capabilities whitelist.
597 */
598 if (pdev->sriov.capoff != 0U) {
599 cnt = pci_pdev_read_cfg(pdev->bdf,
600 pdev->sriov.capoff + PCIR_SRIOV_TOTAL_VFS, 2U);
601 /*
602 * For SRIOV-Capable device, drop the device
603 * if no room for its all of virtual functions.
604 */
605 if ((total + cnt) > CONFIG_MAX_PCI_DEV_NUM) {
606 pr_err("%s, %x:%x.%x is dropped since no room for %u VFs",
607 __func__, pdev->bdf.bits.b, pdev->bdf.bits.d, pdev->bdf.bits.f, cnt);
608 continue;
609 } else {
610 total += cnt;
611 }
612 }
613 (void)init_one_dev_config(pdev);
614 }
615 }
616
617 /*
618 * @brief Walks the PCI heirarchy and initializes array of pci_pdev structs
619 * Uses DRHD info from ACPI DMAR tables to cover the endpoints and
620 * bridges along with their hierarchy captured in the device scope entries
621 * Walks through rest of the devices starting at bus 0 and thru PCI_BUSMAX
622 */
init_pci_pdev_list(void)623 void init_pci_pdev_list(void)
624 {
625 uint64_t buses_visited[BUSES_BITMAP_LEN] = {0UL};
626 struct pci_bdf_mapping_group bdfs_from_drhds = {.pci_bdf_map_count = 0U};
627 uint32_t drhd_idx_pci_all = INVALID_DRHD_INDEX;
628 uint16_t bus;
629 bool was_visited = false;
630
631 set_paging_supervisor(phys_pci_mmcfg.address, get_pci_mmcfg_size(&phys_pci_mmcfg));
632
633 pci_parse_iommu_devscopes(&bdfs_from_drhds, &drhd_idx_pci_all);
634
635 /* TODO: iterate over list of PCI Host Bridges found in ACPI namespace */
636 for (bus = 0U; bus <= PCI_BUSMAX; bus++) {
637 was_visited = bitmap_test((bus & 0x3FU), &buses_visited[bus >> 6U]);
638 if (!was_visited) {
639 scan_pci_hierarchy((uint8_t)bus, buses_visited, &bdfs_from_drhds, drhd_idx_pci_all);
640 }
641 }
642 init_all_dev_config();
643 }
644
pci_pdev_get_nr_bars(uint8_t hdr_type)645 static inline uint32_t pci_pdev_get_nr_bars(uint8_t hdr_type)
646 {
647 uint32_t nr_bars = 0U;
648
649 switch (hdr_type & PCIM_HDRTYPE) {
650 case PCIM_HDRTYPE_NORMAL:
651 nr_bars = PCI_STD_NUM_BARS;
652 break;
653
654 case PCIM_HDRTYPE_BRIDGE:
655 nr_bars = 2U;
656 break;
657
658 default:
659 /*no actions are required for other cases.*/
660 break;
661 }
662
663 return nr_bars;
664 }
665
666 /**
667 * @pre pdev != NULL
668 */
pci_enable_ptm_root(struct pci_pdev * pdev,uint32_t pos)669 static void pci_enable_ptm_root(struct pci_pdev *pdev, uint32_t pos)
670 {
671 uint32_t ctrl = 0, cap;
672
673 cap = pci_pdev_read_cfg(pdev->bdf, pos + PCIR_PTM_CAP, PCI_PTM_CAP_LEN);
674
675 if (cap & PCIM_PTM_CAP_ROOT_CAPABLE) {
676 ctrl = PCIM_PTM_CTRL_ENABLED | PCIM_PTM_CTRL_ROOT_SELECTED;
677
678 pci_pdev_write_cfg(pdev->bdf, pos + PCIR_PTM_CTRL, 4, ctrl);
679
680 ctrl = pci_pdev_read_cfg(pdev->bdf, pos + PCIR_PTM_CTRL, 4);
681
682 pr_acrnlog("ptm info [%x:%x.%x]: pos=%x, enabled=%d, root_select=%d, granularity=%d.\n",
683 pdev->bdf.bits.b, pdev->bdf.bits.d,
684 pdev->bdf.bits.f, pos, (ctrl & PCIM_PTM_CTRL_ENABLED) != 0,
685 (ctrl & PCIM_PTM_CTRL_ROOT_SELECTED) != 0,
686 (ctrl & PCIM_PTM_GRANULARITY_MASK) >> 8);
687 } else {
688 /* acrn doesn't support this hw config currently */
689 pr_err("%s: root port %x:%x.%x is not PTM root.\n", __func__,
690 pdev->bdf.bits.b, pdev->bdf.bits.d, pdev->bdf.bits.f);
691
692 }
693
694 return;
695 }
696
697 /**
698 * @pre pdev != NULL
699 */
pci_enumerate_ext_cap(struct pci_pdev * pdev)700 static void pci_enumerate_ext_cap(struct pci_pdev *pdev)
701 {
702 uint32_t hdr, pos, pre_pos = 0U;
703 uint8_t pcie_dev_type;
704
705 /* guard against malformed list */
706 int node_limit;
707
708 pos = PCI_ECAP_BASE_PTR;
709
710 /* minimum 8 bytes per cap */
711 node_limit = (PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / 8;
712
713 /* PCI Express Extended Capability must have 4 bytes header */
714 hdr = pci_pdev_read_cfg(pdev->bdf, pos, 4U);
715 while ((hdr != 0U) && (node_limit > 0)) {
716 if (PCI_ECAP_ID(hdr) == PCIZ_SRIOV) {
717 pdev->sriov.capoff = pos;
718 pdev->sriov.caplen = PCI_SRIOV_CAP_LEN;
719 pdev->sriov.pre_pos = pre_pos;
720 } else if (PCI_ECAP_ID(hdr) == PCIZ_PTM) {
721 pcie_dev_type = (((uint8_t)pci_pdev_read_cfg(pdev->bdf,
722 pdev->pcie_capoff + PCIER_FLAGS, 1)) & PCIEM_FLAGS_TYPE) >> 4;
723
724 if ((pcie_dev_type == PCIEM_TYPE_ENDPOINT) ||
725 (pcie_dev_type == PCIEM_TYPE_ROOT_INT_EP)) {
726 /* No need to enable ptm on ep device. If a PTM-capable ep pass
727 * through to guest, guest OS will enable it
728 */
729 pr_acrnlog("%s: [%x:%x.%x] is PTM capable.\n", __func__,
730 pdev->bdf.bits.b,
731 pdev->bdf.bits.d, pdev->bdf.bits.f);
732 } else if (pcie_dev_type == PCIEM_TYPE_ROOTPORT) {
733 /* if root port is PTM root capable, we need to make sure that
734 * ptm is enabled in h/w
735 */
736 pci_enable_ptm_root(pdev, pos);
737 } else {
738 /* Acrn supports a simple PTM hierarchy: ptm capable ep is
739 * directly connected to a ptm-root capable root port or ep itself
740 * is rcie. Report error for all other cases.
741 * */
742 pr_err("%s: Do NOT enable PTM on [%x:%x.%x].\n", __func__,
743 pdev->bdf.bits.b, pdev->bdf.bits.d, pdev->bdf.bits.f);
744 }
745 } else {
746 /* reserved for future use */
747 }
748
749 pre_pos = pos;
750 pos = PCI_ECAP_NEXT(hdr);
751 if (pos == 0U) {
752 break;
753 }
754 if (pos < PCI_CONFIG_SPACE_SIZE) {
755 pr_err("pdev %x:%x.%x: Illegal PCIe extended capability offset %x",
756 pdev->bdf.bits.b, pdev->bdf.bits.d, pdev->bdf.bits.f, pos);
757 break;
758 }
759 hdr = pci_pdev_read_cfg(pdev->bdf, pos, 4U);
760 node_limit--;
761 };
762
763 if (node_limit <= 0) {
764 pr_err("%s: pdev[%x:%x.%x] Malformed linked list in PCIe extended \
765 capability region detected\n", __func__, pdev->bdf.bits.b,
766 pdev->bdf.bits.d, pdev->bdf.bits.f);
767 }
768 }
769
770 /*
771 * @pre pdev != NULL
772 */
pci_enumerate_cap(struct pci_pdev * pdev)773 static void pci_enumerate_cap(struct pci_pdev *pdev)
774 {
775 uint8_t pos, cap;
776 uint32_t msgctrl;
777 uint32_t len, idx;
778 uint32_t table_info;
779 uint32_t pcie_devcap, val;
780 bool is_pcie = false;
781
782 pos = (uint8_t)pci_pdev_read_cfg(pdev->bdf, PCIR_CAP_PTR, 1U);
783
784 while ((pos != 0U) && (pos != 0xFFU)) {
785 cap = (uint8_t)pci_pdev_read_cfg(pdev->bdf, pos + PCICAP_ID, 1U);
786
787 if (cap == PCIY_MSI) {
788 pdev->msi_capoff = pos;
789 } else if (cap == PCIY_MSIX) {
790 pdev->msix.capoff = pos;
791 pdev->msix.caplen = MSIX_CAPLEN;
792 len = pdev->msix.caplen;
793
794 msgctrl = pci_pdev_read_cfg(pdev->bdf, pdev->msix.capoff + PCIR_MSIX_CTRL, 2U);
795
796 /* Read Table Offset and Table BIR */
797 table_info = pci_pdev_read_cfg(pdev->bdf, pdev->msix.capoff + PCIR_MSIX_TABLE, 4U);
798
799 pdev->msix.table_bar = (uint8_t)(table_info & PCIM_MSIX_BIR_MASK);
800
801 pdev->msix.table_offset = table_info & ~PCIM_MSIX_BIR_MASK;
802 pdev->msix.table_count = (msgctrl & PCIM_MSIXCTRL_TABLE_SIZE) + 1U;
803
804 ASSERT(pdev->msix.table_count <= CONFIG_MAX_MSIX_TABLE_NUM);
805
806 /* Copy MSIX capability struct into buffer */
807 for (idx = 0U; idx < len; idx++) {
808 pdev->msix.cap[idx] = (uint8_t)pci_pdev_read_cfg(pdev->bdf, (uint32_t)pos + idx, 1U);
809 }
810 } else if (cap == PCIY_PMC) {
811 val = pci_pdev_read_cfg(pdev->bdf, pos + PCIR_PMCSR, 4U);
812 pdev->has_pm_reset = ((val & PCIM_PMCSR_NO_SOFT_RST) == 0U);
813 } else if (cap == PCIY_PCIE) {
814 is_pcie = true;
815 pcie_devcap = pci_pdev_read_cfg(pdev->bdf, pos + PCIR_PCIE_DEVCAP, 4U);
816 pdev->pcie_capoff = pos;
817 pdev->has_flr = ((pcie_devcap & PCIM_PCIE_FLRCAP) != 0U);
818 } else if (cap == PCIY_AF) {
819 val = pci_pdev_read_cfg(pdev->bdf, pos, 4U);
820 pdev->has_af_flr = ((val & PCIM_AF_FLR_CAP) != 0U);
821 } else {
822 /* Ignore all other Capability IDs for now */
823 }
824
825 pos = (uint8_t)pci_pdev_read_cfg(pdev->bdf, pos + PCICAP_NEXTPTR, 1U);
826 }
827
828 if (is_pcie) {
829 pci_enumerate_ext_cap(pdev);
830 }
831 }
832
833 /*
834 * @brief Initialize a pdev data structure.
835 *
836 * Initialize a pdev data structure with a physical device BDF(pbdf) and DRHD index(drhd_index).
837 * The caller of the function init_pdev should guarantee execution atomically.
838 *
839 * @param pbdf Physical device BDF
840 * @param drhd_index DRHD index
841 *
842 * @return If there's a successfully initialized pdev return it, otherwise return NULL;
843 */
pci_init_pdev(union pci_bdf bdf,uint32_t drhd_index)844 struct pci_pdev *pci_init_pdev(union pci_bdf bdf, uint32_t drhd_index)
845 {
846 uint8_t hdr_type, hdr_layout;
847 struct pci_pdev *pdev = NULL;
848 bool is_hv_owned = false;
849
850 if (num_pci_pdev < CONFIG_MAX_PCI_DEV_NUM) {
851 hdr_type = (uint8_t)pci_pdev_read_cfg(bdf, PCIR_HDRTYPE, 1U);
852 hdr_layout = (hdr_type & PCIM_HDRTYPE);
853
854 if ((hdr_layout == PCIM_HDRTYPE_NORMAL) || (hdr_layout == PCIM_HDRTYPE_BRIDGE)) {
855 pdev = &pci_pdevs[num_pci_pdev];
856 pdev->bdf = bdf;
857 pdev->hdr_type = hdr_type;
858 pdev->base_class = (uint8_t)pci_pdev_read_cfg(bdf, PCIR_CLASS, 1U);
859 pdev->sub_class = (uint8_t)pci_pdev_read_cfg(bdf, PCIR_SUBCLASS, 1U);
860 pdev->nr_bars = pci_pdev_get_nr_bars(hdr_type);
861 pdev_save_bar(pdev);
862
863 if ((pci_pdev_read_cfg(bdf, PCIR_STATUS, 2U) & PCIM_STATUS_CAPPRESENT) != 0U) {
864 pci_enumerate_cap(pdev);
865 }
866
867 #if (PRE_VM_NUM != 0U)
868 /* HV owned pdev: 1.typ1 pdev if pre-launched VM exist; 2.pci debug uart */
869 is_hv_owned = (hdr_layout == PCIM_HDRTYPE_BRIDGE) || is_pci_dbg_uart(bdf);
870 #else
871 /* HV owned pdev: 1.pci debug uart */
872 is_hv_owned = is_pci_dbg_uart(bdf);
873 #endif
874 if (is_hv_owned) {
875 hv_owned_pci_pdevs[num_hv_owned_pci_pdev] = pdev;
876 num_hv_owned_pci_pdev++;
877 }
878 hlist_add_head(&pdev->link, &pdevs_hlist_heads[hash64(bdf.value, PDEV_HLIST_HASHBITS)]);
879 pdev->drhd_index = drhd_index;
880 num_pci_pdev++;
881 reserve_vmsix_on_msi_irtes(pdev);
882 } else {
883 pr_err("%s, %x:%x.%x unsupported headed type: 0x%x\n",
884 __func__, bdf.bits.b, bdf.bits.d, bdf.bits.f, hdr_type);
885 }
886 } else {
887 pr_err("%s, failed to alloc pci_pdev!\n", __func__);
888 }
889
890 return pdev;
891 }
892