1 /////////////////////////////////////////////////////////////////////////
2 // $Id: pci.cc,v 1.56 2008/01/26 22:24:02 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2002 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 // i440FX Support - PMC/DBX
31 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
32 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
33 // is used to know when we are exporting symbols and when we are importing.
39 #define LOG_THIS thePciBridge->
41 bx_pci_bridge_c
*thePciBridge
= NULL
;
43 int libpci_LTX_plugin_init(plugin_t
*plugin
, plugintype_t type
, int argc
, char *argv
[])
45 thePciBridge
= new bx_pci_bridge_c();
46 bx_devices
.pluginPciBridge
= thePciBridge
;
47 BX_REGISTER_DEVICE_DEVMODEL(plugin
, type
, thePciBridge
, BX_PLUGIN_PCI
);
51 void libpci_LTX_plugin_fini(void)
56 bx_pci_bridge_c::bx_pci_bridge_c()
62 bx_pci_bridge_c::~bx_pci_bridge_c()
68 void bx_pci_bridge_c::init(void)
70 // called once when bochs initializes
72 BX_PCI_THIS num_pci_handlers
= 0;
74 /* set unused elements to appropriate values */
75 for (i
=0; i
< BX_MAX_PCI_DEVICES
; i
++) {
76 BX_PCI_THIS pci_handler
[i
].handler
= NULL
;
79 for (i
=0; i
< 0x100; i
++) {
80 BX_PCI_THIS pci_handler_id
[i
] = BX_MAX_PCI_DEVICES
; // not assigned
83 for (i
=0; i
< BX_N_PCI_SLOTS
; i
++) {
84 BX_PCI_THIS slot_used
[i
] = 0; // no device connected
86 BX_PCI_THIS slots_checked
= 0;
88 // confAddr accepts dword i/o only
89 DEV_register_ioread_handler(this, read_handler
, 0x0CF8, "i440FX", 4);
90 DEV_register_iowrite_handler(this, write_handler
, 0x0CF8, "i440FX", 4);
92 for (i
=0x0CFC; i
<=0x0CFF; i
++) {
93 DEV_register_ioread_handler(this, read_handler
, i
, "i440FX", 7);
95 for (i
=0x0CFC; i
<=0x0CFF; i
++) {
96 DEV_register_iowrite_handler(this, write_handler
, i
, "i440FX", 7);
99 Bit8u devfunc
= BX_PCI_DEVICE(0,0);
100 DEV_register_pci_handlers(this, &devfunc
, BX_PLUGIN_PCI
, "440FX Host bridge");
102 for (i
=0; i
<256; i
++)
103 BX_PCI_THIS s
.i440fx
.pci_conf
[i
] = 0x0;
104 // readonly registers
105 BX_PCI_THIS s
.i440fx
.pci_conf
[0x00] = 0x86;
106 BX_PCI_THIS s
.i440fx
.pci_conf
[0x01] = 0x80;
107 BX_PCI_THIS s
.i440fx
.pci_conf
[0x02] = 0x37;
108 BX_PCI_THIS s
.i440fx
.pci_conf
[0x03] = 0x12;
109 BX_PCI_THIS s
.i440fx
.pci_conf
[0x0b] = 0x06;
113 bx_pci_bridge_c::reset(unsigned type
)
119 if (!BX_PCI_THIS slots_checked
) {
120 for (i
=0; i
<BX_N_PCI_SLOTS
; i
++) {
121 sprintf(devname
, "pci.slot.%d", i
+1);
122 device
= SIM
->get_param_string(devname
)->getptr();
123 if ((strlen(device
) > 0) && !BX_PCI_THIS slot_used
[i
]) {
124 BX_PANIC(("Unknown plugin '%s' at PCI slot #%d", device
, i
+1));
127 BX_PCI_THIS slots_checked
= 1;
130 BX_PCI_THIS s
.i440fx
.confAddr
= 0;
131 BX_PCI_THIS s
.i440fx
.confData
= 0;
133 BX_PCI_THIS s
.i440fx
.pci_conf
[0x04] = 0x06;
134 BX_PCI_THIS s
.i440fx
.pci_conf
[0x05] = 0x00;
135 BX_PCI_THIS s
.i440fx
.pci_conf
[0x06] = 0x80;
136 BX_PCI_THIS s
.i440fx
.pci_conf
[0x07] = 0x02;
137 BX_PCI_THIS s
.i440fx
.pci_conf
[0x0d] = 0x00;
138 BX_PCI_THIS s
.i440fx
.pci_conf
[0x0f] = 0x00;
139 BX_PCI_THIS s
.i440fx
.pci_conf
[0x50] = 0x00;
140 BX_PCI_THIS s
.i440fx
.pci_conf
[0x51] = 0x01;
141 BX_PCI_THIS s
.i440fx
.pci_conf
[0x52] = 0x00;
142 BX_PCI_THIS s
.i440fx
.pci_conf
[0x53] = 0x80;
143 BX_PCI_THIS s
.i440fx
.pci_conf
[0x54] = 0x00;
144 BX_PCI_THIS s
.i440fx
.pci_conf
[0x55] = 0x00;
145 BX_PCI_THIS s
.i440fx
.pci_conf
[0x56] = 0x00;
146 BX_PCI_THIS s
.i440fx
.pci_conf
[0x57] = 0x01;
147 BX_PCI_THIS s
.i440fx
.pci_conf
[0x58] = 0x10;
148 for (i
=0x59; i
<0x60; i
++)
149 BX_PCI_THIS s
.i440fx
.pci_conf
[i
] = 0x00;
150 BX_PCI_THIS s
.i440fx
.pci_conf
[0x72] = 0x02;
153 void bx_pci_bridge_c::register_state(void)
155 bx_list_c
*list
= new bx_list_c(SIM
->get_bochs_root(), "pci_bridge", "PCI Bridge State", 3);
156 BXRS_HEX_PARAM_FIELD(list
, confAddr
, BX_PCI_THIS s
.i440fx
.confAddr
);
157 BXRS_HEX_PARAM_FIELD(list
, confData
, BX_PCI_THIS s
.i440fx
.confData
);
158 bx_list_c
*pci_conf
= new bx_list_c(list
, "pci_conf", 256);
159 for (unsigned i
=0; i
<256; i
++) {
161 sprintf(name
, "0x%02x", i
);
162 new bx_shadow_num_c(pci_conf
, name
, &BX_PCI_THIS s
.i440fx
.pci_conf
[i
], BASE_HEX
);
166 void bx_pci_bridge_c::after_restore_state(void)
168 BX_PCI_THIS
smram_control(BX_PCI_THIS s
.i440fx
.pci_conf
[0x72]);
171 // static IO port read callback handler
172 // redirects to non-static class handler to avoid virtual functions
174 Bit32u
bx_pci_bridge_c::read_handler(void *this_ptr
, Bit32u address
, unsigned io_len
)
177 bx_pci_bridge_c
*class_ptr
= (bx_pci_bridge_c
*) this_ptr
;
178 return class_ptr
->read(address
, io_len
);
181 Bit32u
bx_pci_bridge_c::read(Bit32u address
, unsigned io_len
)
185 #endif // !BX_USE_PCI_SMF
189 return BX_PCI_THIS s
.i440fx
.confAddr
;
195 Bit32u handle
, retval
;
196 Bit8u devfunc
, regnum
;
198 if ((BX_PCI_THIS s
.i440fx
.confAddr
& 0x80FF0000) == 0x80000000) {
199 devfunc
= (BX_PCI_THIS s
.i440fx
.confAddr
>> 8) & 0xff;
200 regnum
= (BX_PCI_THIS s
.i440fx
.confAddr
& 0xfc) + (address
& 0x03);
201 handle
= BX_PCI_THIS pci_handler_id
[devfunc
];
202 if ((io_len
<= 4) && (handle
< BX_MAX_PCI_DEVICES
))
203 retval
= BX_PCI_THIS pci_handler
[handle
].handler
->pci_read_handler(regnum
, io_len
);
209 BX_PCI_THIS s
.i440fx
.confData
= retval
;
214 BX_PANIC(("unsupported IO read to port 0x%x", (unsigned) address
));
218 // static IO port write callback handler
219 // redirects to non-static class handler to avoid virtual functions
221 void bx_pci_bridge_c::write_handler(void *this_ptr
, Bit32u address
, Bit32u value
, unsigned io_len
)
224 bx_pci_bridge_c
*class_ptr
= (bx_pci_bridge_c
*) this_ptr
;
225 class_ptr
->write(address
, value
, io_len
);
228 void bx_pci_bridge_c::write(Bit32u address
, Bit32u value
, unsigned io_len
)
232 #endif // !BX_USE_PCI_SMF
236 BX_PCI_THIS s
.i440fx
.confAddr
= value
;
237 if ((value
& 0x80FFFF00) == 0x80000000) {
238 BX_DEBUG(("440FX PMC register 0x%02x selected", value
& 0xfc));
239 } else if ((value
& 0x80000000) == 0x80000000) {
240 BX_DEBUG(("440FX request for bus 0x%02x device 0x%02x function 0x%02x",
241 (value
>> 16) & 0xFF, (value
>> 11) & 0x1F, (value
>> 8) & 0x07));
249 if ((BX_PCI_THIS s
.i440fx
.confAddr
& 0x80FF0000) == 0x80000000) {
250 Bit8u devfunc
= (BX_PCI_THIS s
.i440fx
.confAddr
>> 8) & 0xff;
251 Bit8u regnum
= (BX_PCI_THIS s
.i440fx
.confAddr
& 0xfc) + (address
& 0x03);
252 Bit32u handle
= BX_PCI_THIS pci_handler_id
[devfunc
];
253 if ((io_len
<= 4) && (handle
< BX_MAX_PCI_DEVICES
)) {
254 if (((regnum
>=4) && (regnum
<=7)) || (regnum
==12) || (regnum
==13) || (regnum
>14)) {
255 BX_PCI_THIS pci_handler
[handle
].handler
->pci_write_handler(regnum
, value
, io_len
);
256 BX_PCI_THIS s
.i440fx
.confData
= value
<< (8 * (address
& 0x03));
259 BX_DEBUG(("read only register, write ignored"));
265 BX_PANIC(("IO write to port 0x%x", (unsigned) address
));
269 // pci configuration space read callback handler
270 Bit32u
bx_pci_bridge_c::pci_read_handler(Bit8u address
, unsigned io_len
)
275 for (unsigned i
=0; i
<io_len
; i
++) {
276 val440fx
|= (BX_PCI_THIS s
.i440fx
.pci_conf
[address
+i
] << (i
*8));
278 BX_DEBUG(("440FX PMC read register 0x%02x value 0x%08x", address
, val440fx
));
285 // pci configuration space write callback handler
286 void bx_pci_bridge_c::pci_write_handler(Bit8u address
, Bit32u value
, unsigned io_len
)
290 if ((address
>= 0x10) && (address
< 0x34))
293 for (unsigned i
=0; i
<io_len
; i
++) {
294 value8
= (value
>> (i
*8)) & 0xFF;
297 BX_PCI_THIS s
.i440fx
.pci_conf
[address
+i
] = (value8
& 0x40) | 0x06;
309 BX_INFO(("440FX PMC write to PAM register %x (TLB Flush)", address
+i
));
310 BX_PCI_THIS s
.i440fx
.pci_conf
[address
+i
] = value8
;
311 bx_pc_system
.MemoryMappingChanged();
314 smram_control(value
); // SMRAM conrol register
317 BX_PCI_THIS s
.i440fx
.pci_conf
[address
+i
] = value8
;
318 BX_DEBUG(("440FX PMC write register 0x%02x value 0x%02x", address
+i
, value8
));
324 void bx_pci_bridge_c::smram_control(Bit8u value8
)
327 // From i440FX chipset manual:
330 // [6:6] SMM Space Open (DOPEN), when DOPEN=1 and DLCK=0, SMM space DRAM
331 // became visible even CPU not indicte SMM mode access. This is
332 // indended to help BIOS to initialize SMM space.
333 // [5:5] SMM Space Closed (DCLS), when DCLS=1, SMM space is not accessible
334 // for data references, even if CPU indicates SMM mode access. Code
335 // references may still access SMM space DRAM.
336 // [4:4] SMM Space Locked (DLCK), when DLCK=1, DOPEN is set to 0 and
337 // both DLCK and DOPEN became R/O. DLCK can only be cleared by
339 // [3:3] SMRAM Enable (SMRAME)
340 // [2:0] SMM space base segment, program the location of SMM space
344 // SMRAM space access cycles:
346 // | SMRAME | DLCK | DCLS | DOPEN | CPU_SMM | | Code | Data |
347 // ------------------------------------------ ---------------
348 // | 0 | X | X | X | X | -> | PCI | PCI |
349 // | 1 | 0 | X | 0 | 0 | -> | PCI | PCI |
350 // | 1 | 0 | 0 | 0 | 1 | -> | DRAM | DRAM |
351 // | 1 | 0 | 0 | 1 | X | -> | DRAM | DRAM |
352 // | 1 | 1 | 0 | X | 1 | -> | DRAM | DRAM |
353 // | 1 | 0 | 1 | 0 | 1 | -> | DRAM | PCI |
354 // | 1 | 0 | 1 | 1 | X | -> | ---- | ---- |
355 // | 1 | 1 | X | X | 0 | -> | PCI | PCI |
356 // | 1 | 1 | 1 | X | 1 | -> | DRAM | PCI |
357 // ------------------------------------------ ---------------
359 value8
= (value8
& 0x78) | 0x2; // ignore reserved bits
361 if (BX_PCI_THIS s
.i440fx
.pci_conf
[0x72] & 0x10)
363 value8
&= 0xbf; // set DOPEN=0, DLCK=1
367 if ((value8
& 0x08) == 0) {
368 bx_devices
.mem
->disable_smram();
371 bx_bool DOPEN
= (value8
& 0x40) > 0, DCLS
= (value8
& 0x20) > 0;
372 if(DOPEN
&& DCLS
) BX_PANIC(("SMRAM control: DOPEN not mutually exclusive with DCLS !"));
373 bx_devices
.mem
->enable_smram(DOPEN
, DCLS
);
376 BX_INFO(("setting SMRAM control register to 0x%02x", value8
));
377 BX_PCI_THIS s
.i440fx
.pci_conf
[0x72] = value8
;
380 Bit8u
bx_pci_bridge_c::rd_memType(Bit32u addr
)
382 switch ((addr
& 0xFC000) >> 12) {
384 return (BX_PCI_THIS s
.i440fx
.pci_conf
[0x5A] & 0x1);
386 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5A] >> 4) & 0x1);
388 return (BX_PCI_THIS s
.i440fx
.pci_conf
[0x5B] & 0x1);
390 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5B] >> 4) & 0x1);
393 return (BX_PCI_THIS s
.i440fx
.pci_conf
[0x5C] & 0x1);
395 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5C] >> 4) & 0x1);
397 return (BX_PCI_THIS s
.i440fx
.pci_conf
[0x5D] & 0x1);
399 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5D] >> 4) & 0x1);
402 return (BX_PCI_THIS s
.i440fx
.pci_conf
[0x5E] & 0x1);
404 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5E] >> 4) & 0x1);
406 return (BX_PCI_THIS s
.i440fx
.pci_conf
[0x5F] & 0x1);
408 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5F] >> 4) & 0x1);
414 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x59] >> 4) & 0x1);
417 BX_PANIC(("rd_memType () Error: Memory Type not known !"));
421 return(0); // keep compiler happy
424 Bit8u
bx_pci_bridge_c::wr_memType(Bit32u addr
)
426 switch ((addr
& 0xFC000) >> 12) {
428 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5A] >> 1) & 0x1);
430 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5A] >> 5) & 0x1);
432 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5B] >> 1) & 0x1);
434 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5B] >> 5) & 0x1);
437 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5C] >> 1) & 0x1);
439 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5C] >> 5) & 0x1);
441 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5D] >> 1) & 0x1);
443 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5D] >> 5) & 0x1);
446 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5E] >> 1) & 0x1);
448 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5E] >> 5) & 0x1);
450 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5F] >> 1) & 0x1);
452 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x5F] >> 5) & 0x1);
458 return ((BX_PCI_THIS s
.i440fx
.pci_conf
[0x59] >> 5) & 0x1);
461 BX_PANIC(("wr_memType () Error: Memory Type not known !"));
465 return(0); // keep compiler happy
468 void bx_pci_bridge_c::print_i440fx_state()
472 BX_DEBUG(("i440fxConfAddr:0x%08x", BX_PCI_THIS s
.i440fx
.confAddr
));
473 BX_DEBUG(("i440fxConfData:0x%08x", BX_PCI_THIS s
.i440fx
.confData
));
475 #ifdef DUMP_FULL_I440FX
476 for (i
=0; i
<256; i
++) {
477 BX_DEBUG(("i440fxArray%02x:0x%02x", i
, BX_PCI_THIS s
.i440fx
.pci_conf
[i
]));
479 #else /* DUMP_FULL_I440FX */
480 for (i
=0x59; i
<0x60; i
++) {
481 BX_DEBUG(("i440fxArray%02x:0x%02x", i
, BX_PCI_THIS s
.i440fx
.pci_conf
[i
]));
483 #endif /* DUMP_FULL_I440FX */
486 bx_bool
bx_pci_bridge_c::register_pci_handlers(bx_pci_device_stub_c
*dev
,
487 Bit8u
*devfunc
, const char *name
,
494 if (strcmp(name
, "pci") && strcmp(name
, "pci2isa") && strcmp(name
, "pci_ide")
495 && (*devfunc
== 0x00)) {
496 for (i
= 0; i
< BX_N_PCI_SLOTS
; i
++) {
497 sprintf(devname
, "pci.slot.%d", i
+1);
498 device
= SIM
->get_param_string(devname
)->getptr();
499 if ((strlen(device
) > 0) && (!strcmp(name
, device
))) {
500 *devfunc
= (i
+ 2) << 3;
501 BX_PCI_THIS slot_used
[i
] = 1;
502 BX_INFO(("PCI slot #%d used by plugin '%s'", i
+1, name
));
506 if (*devfunc
== 0x00) {
507 BX_ERROR(("Plugin '%s' not connected to a PCI slot", name
));
510 /* check if device/function is available */
511 if (BX_PCI_THIS pci_handler_id
[*devfunc
] == BX_MAX_PCI_DEVICES
) {
512 if (BX_PCI_THIS num_pci_handlers
>= BX_MAX_PCI_DEVICES
) {
513 BX_INFO(("too many PCI devices installed."));
514 BX_PANIC((" try increasing BX_MAX_PCI_DEVICES"));
517 handle
= BX_PCI_THIS num_pci_handlers
++;
518 BX_PCI_THIS pci_handler
[handle
].handler
= dev
;
519 BX_PCI_THIS pci_handler_id
[*devfunc
] = handle
;
520 BX_INFO(("%s present at device %d, function %d", descr
, *devfunc
>> 3,
522 return true; // device/function mapped successfully
525 return false; // device/function not available, return false.
529 bx_bool
bx_pci_bridge_c::is_pci_device(const char *name
)
535 for (i
= 0; i
< BX_N_PCI_SLOTS
; i
++) {
536 sprintf(devname
, "pci.slot.%d", i
+1);
537 device
= SIM
->get_param_string(devname
)->getptr();
538 if ((strlen(device
) > 0) && (!strcmp(name
, device
))) {
545 bx_bool
bx_pci_bridge_c::pci_set_base_mem(void *this_ptr
, memory_handler_t f1
, memory_handler_t f2
,
546 Bit32u
*addr
, Bit8u
*pci_conf
, unsigned size
)
550 Bit32u oldbase
= *addr
;
551 Bit32u mask
= ~(size
- 1);
552 Bit8u pci_flags
= pci_conf
[0x00] & 0x0f;
553 pci_conf
[0x00] &= (mask
& 0xf0);
554 pci_conf
[0x01] &= (mask
>> 8) & 0xff;
555 pci_conf
[0x02] &= (mask
>> 16) & 0xff;
556 pci_conf
[0x03] &= (mask
>> 24) & 0xff;
557 ReadHostDWordFromLittleEndian(pci_conf
, newbase
);
558 pci_conf
[0x00] |= pci_flags
;
559 if ((newbase
!= mask
) && (newbase
!= oldbase
)) { // skip PCI probe
561 DEV_unregister_memory_handlers(f1
, f2
, oldbase
, oldbase
+ size
- 1);
564 DEV_register_memory_handlers(this_ptr
, f1
, f2
, newbase
, newbase
+ size
- 1);
572 bx_bool
bx_pci_bridge_c::pci_set_base_io(void *this_ptr
, bx_read_handler_t f1
, bx_write_handler_t f2
,
573 Bit32u
*addr
, Bit8u
*pci_conf
, unsigned size
,
574 const Bit8u
*iomask
, const char *name
)
579 Bit32u oldbase
= *addr
;
580 Bit16u mask
= ~(size
- 1);
581 Bit8u pci_flags
= pci_conf
[0x00] & 0x03;
582 pci_conf
[0x00] &= (mask
& 0xfc);
583 pci_conf
[0x01] &= (mask
>> 8);
584 ReadHostDWordFromLittleEndian(pci_conf
, newbase
);
585 pci_conf
[0x00] |= pci_flags
;
586 if (((newbase
& 0xfffc) != mask
) && (newbase
!= oldbase
)) { // skip PCI probe
588 for (i
=0; i
<size
; i
++) {
590 DEV_unregister_ioread_handler(this_ptr
, f1
, oldbase
+ i
, iomask
[i
]);
591 DEV_unregister_iowrite_handler(this_ptr
, f2
, oldbase
+ i
, iomask
[i
]);
596 for (i
=0; i
<size
; i
++) {
598 DEV_register_ioread_handler(this_ptr
, f1
, newbase
+ i
, name
, iomask
[i
]);
599 DEV_register_iowrite_handler(this_ptr
, f2
, newbase
+ i
, name
, iomask
[i
]);
609 #endif /* BX_SUPPORT_PCI */