xref: /aosp_15_r20/external/coreboot/util/vgabios/pci-userspace.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdio.h>
4 #include <pci/pci.h>
5 #include "pci-userspace.h"
6 
7 #define DEBUG_PCI 1
8 
9 static struct pci_access *pacc;
10 
pci_initialize(void)11 int pci_initialize(void)
12 {
13 	struct pci_dev *dev;
14 
15 	pacc = pci_alloc();
16 
17 	pci_init(pacc);
18 	pci_scan_bus(pacc);
19 	for (dev = pacc->devices; dev; dev = dev->next) {
20 		pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
21 	}
22 	return 0;
23 }
24 
pci_exit(void)25 int pci_exit(void)
26 {
27 	pci_cleanup(pacc);
28 	return 0;
29 }
30 
pci_read_config8(struct device * dev,unsigned int where)31 u8 pci_read_config8(struct device *dev, unsigned int where)
32 {
33 	struct pci_dev *d;
34 	if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
35 		return pci_read_byte(d, where);
36 #ifdef DEBUG_PCI
37 	printf("PCI: device not found while read byte (%x:%x.%x)\n",
38 	       dev->busno, dev->slot, dev->func);
39 #endif
40 	return 0;
41 }
42 
pci_read_config16(struct device * dev,unsigned int where)43 u16 pci_read_config16(struct device *dev, unsigned int where)
44 {
45 	struct pci_dev *d;
46 	if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
47 		return pci_read_word(d, where);
48 #ifdef DEBUG_PCI
49 	printf("PCI: device not found while read word (%x:%x.%x)\n",
50 	       dev->busno, dev->slot, dev->func);
51 #endif
52 	return 0;
53 }
54 
pci_read_config32(struct device * dev,unsigned int where)55 u32 pci_read_config32(struct device *dev, unsigned int where)
56 {
57 	struct pci_dev *d;
58 	if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
59 		return pci_read_long(d, where);
60 #ifdef DEBUG_PCI
61 	printf("PCI: device not found while read dword (%x:%x.%x)\n",
62 	       dev->busno, dev->slot, dev->func);
63 #endif
64 	return 0;
65 }
66 
pci_write_config8(struct device * dev,unsigned int where,u8 val)67 void pci_write_config8(struct device *dev, unsigned int where, u8 val)
68 {
69 	struct pci_dev *d;
70 	if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
71 		pci_write_byte(d, where, val);
72 #ifdef DEBUG_PCI
73 	else
74 		printf("PCI: device not found while write byte (%x:%x.%x)\n",
75 		       dev->busno, dev->slot, dev->func);
76 #endif
77 }
78 
pci_write_config16(struct device * dev,unsigned int where,u16 val)79 void pci_write_config16(struct device *dev, unsigned int where, u16 val)
80 {
81 	struct pci_dev *d;
82 	if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
83 		pci_write_word(d, where, val);
84 #ifdef DEBUG_PCI
85 	else
86 		printf("PCI: device not found while write word (%x:%x.%x)\n",
87 		       dev->busno, dev->slot, dev->func);
88 #endif
89 }
90 
pci_write_config32(struct device * dev,unsigned int where,u32 val)91 void pci_write_config32(struct device *dev, unsigned int where, u32 val)
92 {
93 	struct pci_dev *d;
94 	if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
95 		pci_write_long(d, where, val);
96 #ifdef DEBUG_PCI
97 	else
98 		printf("PCI: device not found while write dword (%x:%x.%x)\n",
99 		       dev->busno, dev->slot, dev->func);
100 #endif
101 }
102