1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <ipxe/pci.h>
28 #include <realmode.h>
29 
30 /** @file
31  *
32  * PCI configuration space access via PCI BIOS
33  *
34  */
35 
36 /**
37  * Determine number of PCI buses within system
38  *
39  * @ret num_bus		Number of buses
40  */
pcibios_num_bus(void)41 static int pcibios_num_bus ( void ) {
42 	int discard_a, discard_D;
43 	uint8_t max_bus;
44 
45 	/* We issue this call using flat real mode, to work around a
46 	 * bug in some HP BIOSes.
47 	 */
48 	__asm__ __volatile__ ( REAL_CODE ( "call flatten_real_mode\n\t"
49 					   "stc\n\t"
50 					   "int $0x1a\n\t"
51 					   "jnc 1f\n\t"
52 					   "xorw %%cx, %%cx\n\t"
53 					   "\n1:\n\t" )
54 			       : "=c" ( max_bus ), "=a" ( discard_a ),
55 				 "=D" ( discard_D )
56 			       : "a" ( PCIBIOS_INSTALLATION_CHECK >> 16 ),
57 				 "D" ( 0 )
58 			       : "ebx", "edx" );
59 
60 	return ( max_bus + 1 );
61 }
62 
63 /**
64  * Read configuration space via PCI BIOS
65  *
66  * @v pci	PCI device
67  * @v command	PCI BIOS command
68  * @v value	Value read
69  * @ret rc	Return status code
70  */
pcibios_read(struct pci_device * pci,uint32_t command,uint32_t * value)71 int pcibios_read ( struct pci_device *pci, uint32_t command, uint32_t *value ){
72 	int discard_b, discard_D;
73 	uint16_t status;
74 
75 	__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
76 					   "int $0x1a\n\t"
77 					   "jnc 1f\n\t"
78 					   "xorl %%eax, %%eax\n\t"
79 					   "decl %%eax\n\t"
80 					   "movl %%eax, %%ecx\n\t"
81 					   "\n1:\n\t" )
82 			       : "=a" ( status ), "=b" ( discard_b ),
83 				 "=c" ( *value ), "=D" ( discard_D )
84 			       : "a" ( command >> 16 ), "D" ( command ),
85 				 "b" ( pci->busdevfn )
86 			       : "edx" );
87 
88 	return ( status >> 8 );
89 }
90 
91 /**
92  * Write configuration space via PCI BIOS
93  *
94  * @v pci	PCI device
95  * @v command	PCI BIOS command
96  * @v value	Value to be written
97  * @ret rc	Return status code
98  */
pcibios_write(struct pci_device * pci,uint32_t command,uint32_t value)99 int pcibios_write ( struct pci_device *pci, uint32_t command, uint32_t value ){
100 	int discard_b, discard_c, discard_D;
101 	uint16_t status;
102 
103 	__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
104 					   "int $0x1a\n\t"
105 					   "jnc 1f\n\t"
106 					   "movb $0xff, %%ah\n\t"
107 					   "\n1:\n\t" )
108 			       : "=a" ( status ), "=b" ( discard_b ),
109 				 "=c" ( discard_c ), "=D" ( discard_D )
110 			       : "a" ( command >> 16 ),	"D" ( command ),
111 			         "b" ( pci->busdevfn ), "c" ( value )
112 			       : "edx" );
113 
114 	return ( status >> 8 );
115 }
116 
117 PROVIDE_PCIAPI ( pcbios, pci_num_bus, pcibios_num_bus );
118 PROVIDE_PCIAPI_INLINE ( pcbios, pci_read_config_byte );
119 PROVIDE_PCIAPI_INLINE ( pcbios, pci_read_config_word );
120 PROVIDE_PCIAPI_INLINE ( pcbios, pci_read_config_dword );
121 PROVIDE_PCIAPI_INLINE ( pcbios, pci_write_config_byte );
122 PROVIDE_PCIAPI_INLINE ( pcbios, pci_write_config_word );
123 PROVIDE_PCIAPI_INLINE ( pcbios, pci_write_config_dword );
124