2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * A single PCI device configuration space entry.
41 #include "base/inifile.hh"
42 #include "base/intmath.hh" // for isPowerOf2(
43 #include "base/misc.hh"
44 #include "base/str.hh" // for to_number
45 #include "base/trace.hh"
46 #include "dev/pciconfigall.hh"
47 #include "dev/pcidev.hh"
48 #include "dev/alpha/tsunamireg.h"
49 #include "mem/packet.hh"
50 #include "mem/packet_access.hh"
51 #include "sim/builder.hh"
52 #include "sim/byteswap.hh"
53 #include "sim/param.hh"
54 #include "sim/root.hh"
59 PciDev::PciConfigPort::PciConfigPort(PciDev
*dev
, int busid
, int devid
,
60 int funcid
, Platform
*p
)
61 : SimpleTimingPort(dev
->name() + "-pciconf"), device(dev
), platform(p
),
62 busId(busid
), deviceId(devid
), functionId(funcid
)
64 configAddr
= platform
->calcConfigAddr(busId
, deviceId
, functionId
);
69 PciDev::PciConfigPort::recvAtomic(PacketPtr pkt
)
71 assert(pkt
->result
== Packet::Unknown
);
72 assert(pkt
->getAddr() >= configAddr
&&
73 pkt
->getAddr() < configAddr
+ PCI_CONFIG_SIZE
);
74 return pkt
->isRead() ? device
->readConfig(pkt
) : device
->writeConfig(pkt
);
78 PciDev::PciConfigPort::getDeviceAddressRanges(AddrRangeList
&resp
,
82 resp
.push_back(RangeSize(configAddr
, PCI_CONFIG_SIZE
+1));
86 PciDev::PciDev(Params
*p
)
87 : DmaDevice(p
), plat(p
->platform
), configData(p
->configData
),
88 pioDelay(p
->pio_delay
), configDelay(p
->config_delay
),
91 // copy the config data from the PciConfigData object
93 memcpy(config
.data
, configData
->config
.data
, sizeof(config
.data
));
94 memcpy(BARSize
, configData
->BARSize
, sizeof(BARSize
));
96 panic("NULL pointer to configuration data");
98 memset(BARAddrs
, 0, sizeof(BARAddrs
));
100 plat
->registerPciDevice(0, p
->deviceNum
, p
->functionNum
,
101 letoh(configData
->config
.interruptLine
));
108 panic("pci config port not connected to anything!");
109 configPort
->sendStatusChange(Port::RangeChange
);
114 PciDev::drain(Event
*de
)
117 count
= pioPort
->drain(de
) + dmaPort
->drain(de
) + configPort
->drain(de
);
119 changeState(Draining
);
121 changeState(Drained
);
126 PciDev::readConfig(PacketPtr pkt
)
128 int offset
= pkt
->getAddr() & PCI_CONFIG_SIZE
;
129 if (offset
>= PCI_DEVICE_SPECIFIC
)
130 panic("Device specific PCI config space not implemented!\n");
134 switch (pkt
->getSize()) {
135 case sizeof(uint8_t):
136 pkt
->set
<uint8_t>(config
.data
[offset
]);
138 "readConfig: dev %#x func %#x reg %#x 1 bytes: data = %#x\n",
139 params()->deviceNum
, params()->functionNum
, offset
,
140 (uint32_t)pkt
->get
<uint8_t>());
142 case sizeof(uint16_t):
143 pkt
->set
<uint16_t>(*(uint16_t*)&config
.data
[offset
]);
145 "readConfig: dev %#x func %#x reg %#x 2 bytes: data = %#x\n",
146 params()->deviceNum
, params()->functionNum
, offset
,
147 (uint32_t)pkt
->get
<uint16_t>());
149 case sizeof(uint32_t):
150 pkt
->set
<uint32_t>(*(uint32_t*)&config
.data
[offset
]);
152 "readConfig: dev %#x func %#x reg %#x 4 bytes: data = %#x\n",
153 params()->deviceNum
, params()->functionNum
, offset
,
154 (uint32_t)pkt
->get
<uint32_t>());
157 panic("invalid access size(?) for PCI configspace!\n");
159 pkt
->result
= Packet::Success
;
165 PciDev::addressRanges(AddrRangeList
&range_list
)
169 for (x
= 0; x
< 6; x
++)
170 if (BARAddrs
[x
] != 0)
171 range_list
.push_back(RangeSize(BARAddrs
[x
],BARSize
[x
]));
175 PciDev::writeConfig(PacketPtr pkt
)
177 int offset
= pkt
->getAddr() & PCI_CONFIG_SIZE
;
178 if (offset
>= PCI_DEVICE_SPECIFIC
)
179 panic("Device specific PCI config space not implemented!\n");
181 switch (pkt
->getSize()) {
182 case sizeof(uint8_t):
184 case PCI0_INTERRUPT_LINE
:
185 config
.interruptLine
= pkt
->get
<uint8_t>();
186 case PCI_CACHE_LINE_SIZE
:
187 config
.cacheLineSize
= pkt
->get
<uint8_t>();
188 case PCI_LATENCY_TIMER
:
189 config
.latencyTimer
= pkt
->get
<uint8_t>();
191 /* Do nothing for these read-only registers */
192 case PCI0_INTERRUPT_PIN
:
193 case PCI0_MINIMUM_GRANT
:
194 case PCI0_MAXIMUM_LATENCY
:
196 case PCI_REVISION_ID
:
199 panic("writing to a read only register");
202 "writeConfig: dev %#x func %#x reg %#x 1 bytes: data = %#x\n",
203 params()->deviceNum
, params()->functionNum
, offset
,
204 (uint32_t)pkt
->get
<uint8_t>());
206 case sizeof(uint16_t):
209 config
.command
= pkt
->get
<uint8_t>();
211 config
.status
= pkt
->get
<uint8_t>();
212 case PCI_CACHE_LINE_SIZE
:
213 config
.cacheLineSize
= pkt
->get
<uint8_t>();
216 panic("writing to a read only register");
219 "writeConfig: dev %#x func %#x reg %#x 2 bytes: data = %#x\n",
220 params()->deviceNum
, params()->functionNum
, offset
,
221 (uint32_t)pkt
->get
<uint16_t>());
223 case sizeof(uint32_t):
225 case PCI0_BASE_ADDR0
:
226 case PCI0_BASE_ADDR1
:
227 case PCI0_BASE_ADDR2
:
228 case PCI0_BASE_ADDR3
:
229 case PCI0_BASE_ADDR4
:
230 case PCI0_BASE_ADDR5
:
232 int barnum
= BAR_NUMBER(offset
);
234 // convert BAR values to host endianness
235 uint32_t he_old_bar
= letoh(config
.baseAddr
[barnum
]);
236 uint32_t he_new_bar
= letoh(pkt
->get
<uint32_t>());
239 BAR_IO_SPACE(he_old_bar
) ? BAR_IO_MASK
: BAR_MEM_MASK
;
241 // Writing 0xffffffff to a BAR tells the card to set the
242 // value of the bar to a bitmask indicating the size of
244 if (he_new_bar
== 0xffffffff) {
245 he_new_bar
= ~(BARSize
[barnum
] - 1);
247 // does it mean something special to write 0 to a BAR?
248 he_new_bar
&= ~bar_mask
;
250 Addr space_base
= BAR_IO_SPACE(he_old_bar
) ?
251 TSUNAMI_PCI0_IO
: TSUNAMI_PCI0_MEMORY
;
252 BARAddrs
[barnum
] = he_new_bar
+ space_base
;
253 pioPort
->sendStatusChange(Port::RangeChange
);
256 config
.baseAddr
[barnum
] = htole((he_new_bar
& ~bar_mask
) |
257 (he_old_bar
& bar_mask
));
261 case PCI0_ROM_BASE_ADDR
:
262 if (letoh(pkt
->get
<uint32_t>()) == 0xfffffffe)
263 config
.expansionROM
= htole((uint32_t)0xffffffff);
265 config
.expansionROM
= pkt
->get
<uint32_t>();
269 // This could also clear some of the error bits in the Status
270 // register. However they should never get set, so lets ignore
272 config
.command
= pkt
->get
<uint32_t>();
276 DPRINTF(PCIDEV
, "Writing to a read only register");
279 "writeConfig: dev %#x func %#x reg %#x 4 bytes: data = %#x\n",
280 params()->deviceNum
, params()->functionNum
, offset
,
281 (uint32_t)pkt
->get
<uint32_t>());
284 panic("invalid access size(?) for PCI configspace!\n");
286 pkt
->result
= Packet::Success
;
292 PciDev::serialize(ostream
&os
)
294 SERIALIZE_ARRAY(BARSize
, sizeof(BARSize
) / sizeof(BARSize
[0]));
295 SERIALIZE_ARRAY(BARAddrs
, sizeof(BARAddrs
) / sizeof(BARAddrs
[0]));
296 SERIALIZE_ARRAY(config
.data
, sizeof(config
.data
) / sizeof(config
.data
[0]));
300 PciDev::unserialize(Checkpoint
*cp
, const std::string
§ion
)
302 UNSERIALIZE_ARRAY(BARSize
, sizeof(BARSize
) / sizeof(BARSize
[0]));
303 UNSERIALIZE_ARRAY(BARAddrs
, sizeof(BARAddrs
) / sizeof(BARAddrs
[0]));
304 UNSERIALIZE_ARRAY(config
.data
,
305 sizeof(config
.data
) / sizeof(config
.data
[0]));
306 pioPort
->sendStatusChange(Port::RangeChange
);
310 #ifndef DOXYGEN_SHOULD_SKIP_THIS
312 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PciConfigData
)
314 Param
<uint16_t> VendorID
;
315 Param
<uint16_t> DeviceID
;
316 Param
<uint16_t> Command
;
317 Param
<uint16_t> Status
;
318 Param
<uint8_t> Revision
;
319 Param
<uint8_t> ProgIF
;
320 Param
<uint8_t> SubClassCode
;
321 Param
<uint8_t> ClassCode
;
322 Param
<uint8_t> CacheLineSize
;
323 Param
<uint8_t> LatencyTimer
;
324 Param
<uint8_t> HeaderType
;
326 Param
<uint32_t> BAR0
;
327 Param
<uint32_t> BAR1
;
328 Param
<uint32_t> BAR2
;
329 Param
<uint32_t> BAR3
;
330 Param
<uint32_t> BAR4
;
331 Param
<uint32_t> BAR5
;
332 Param
<uint32_t> CardbusCIS
;
333 Param
<uint16_t> SubsystemVendorID
;
334 Param
<uint16_t> SubsystemID
;
335 Param
<uint32_t> ExpansionROM
;
336 Param
<uint8_t> InterruptLine
;
337 Param
<uint8_t> InterruptPin
;
338 Param
<uint8_t> MinimumGrant
;
339 Param
<uint8_t> MaximumLatency
;
340 Param
<uint32_t> BAR0Size
;
341 Param
<uint32_t> BAR1Size
;
342 Param
<uint32_t> BAR2Size
;
343 Param
<uint32_t> BAR3Size
;
344 Param
<uint32_t> BAR4Size
;
345 Param
<uint32_t> BAR5Size
;
347 END_DECLARE_SIM_OBJECT_PARAMS(PciConfigData
)
349 BEGIN_INIT_SIM_OBJECT_PARAMS(PciConfigData
)
351 INIT_PARAM(VendorID
, "Vendor ID"),
352 INIT_PARAM(DeviceID
, "Device ID"),
353 INIT_PARAM_DFLT(Command
, "Command Register", 0x00),
354 INIT_PARAM_DFLT(Status
, "Status Register", 0x00),
355 INIT_PARAM_DFLT(Revision
, "Device Revision", 0x00),
356 INIT_PARAM_DFLT(ProgIF
, "Programming Interface", 0x00),
357 INIT_PARAM(SubClassCode
, "Sub-Class Code"),
358 INIT_PARAM(ClassCode
, "Class Code"),
359 INIT_PARAM_DFLT(CacheLineSize
, "System Cacheline Size", 0x00),
360 INIT_PARAM_DFLT(LatencyTimer
, "PCI Latency Timer", 0x00),
361 INIT_PARAM_DFLT(HeaderType
, "PCI Header Type", 0x00),
362 INIT_PARAM_DFLT(BIST
, "Built In Self Test", 0x00),
363 INIT_PARAM_DFLT(BAR0
, "Base Address Register 0", 0x00),
364 INIT_PARAM_DFLT(BAR1
, "Base Address Register 1", 0x00),
365 INIT_PARAM_DFLT(BAR2
, "Base Address Register 2", 0x00),
366 INIT_PARAM_DFLT(BAR3
, "Base Address Register 3", 0x00),
367 INIT_PARAM_DFLT(BAR4
, "Base Address Register 4", 0x00),
368 INIT_PARAM_DFLT(BAR5
, "Base Address Register 5", 0x00),
369 INIT_PARAM_DFLT(CardbusCIS
, "Cardbus Card Information Structure", 0x00),
370 INIT_PARAM_DFLT(SubsystemVendorID
, "Subsystem Vendor ID", 0x00),
371 INIT_PARAM_DFLT(SubsystemID
, "Subsystem ID", 0x00),
372 INIT_PARAM_DFLT(ExpansionROM
, "Expansion ROM Base Address Register", 0x00),
373 INIT_PARAM(InterruptLine
, "Interrupt Line Register"),
374 INIT_PARAM(InterruptPin
, "Interrupt Pin Register"),
375 INIT_PARAM_DFLT(MinimumGrant
, "Minimum Grant", 0x00),
376 INIT_PARAM_DFLT(MaximumLatency
, "Maximum Latency", 0x00),
377 INIT_PARAM_DFLT(BAR0Size
, "Base Address Register 0 Size", 0x00),
378 INIT_PARAM_DFLT(BAR1Size
, "Base Address Register 1 Size", 0x00),
379 INIT_PARAM_DFLT(BAR2Size
, "Base Address Register 2 Size", 0x00),
380 INIT_PARAM_DFLT(BAR3Size
, "Base Address Register 3 Size", 0x00),
381 INIT_PARAM_DFLT(BAR4Size
, "Base Address Register 4 Size", 0x00),
382 INIT_PARAM_DFLT(BAR5Size
, "Base Address Register 5 Size", 0x00)
384 END_INIT_SIM_OBJECT_PARAMS(PciConfigData
)
386 CREATE_SIM_OBJECT(PciConfigData
)
388 PciConfigData
*data
= new PciConfigData(getInstanceName());
390 data
->config
.vendor
= htole(VendorID
.returnValue());
391 data
->config
.device
= htole(DeviceID
.returnValue());
392 data
->config
.command
= htole(Command
.returnValue());
393 data
->config
.status
= htole(Status
.returnValue());
394 data
->config
.revision
= htole(Revision
.returnValue());
395 data
->config
.progIF
= htole(ProgIF
.returnValue());
396 data
->config
.subClassCode
= htole(SubClassCode
.returnValue());
397 data
->config
.classCode
= htole(ClassCode
.returnValue());
398 data
->config
.cacheLineSize
= htole(CacheLineSize
.returnValue());
399 data
->config
.latencyTimer
= htole(LatencyTimer
.returnValue());
400 data
->config
.headerType
= htole(HeaderType
.returnValue());
401 data
->config
.bist
= htole(BIST
.returnValue());
403 data
->config
.baseAddr
[0] = htole(BAR0
.returnValue());
404 data
->config
.baseAddr
[1] = htole(BAR1
.returnValue());
405 data
->config
.baseAddr
[2] = htole(BAR2
.returnValue());
406 data
->config
.baseAddr
[3] = htole(BAR3
.returnValue());
407 data
->config
.baseAddr
[4] = htole(BAR4
.returnValue());
408 data
->config
.baseAddr
[5] = htole(BAR5
.returnValue());
409 data
->config
.cardbusCIS
= htole(CardbusCIS
.returnValue());
410 data
->config
.subsystemVendorID
= htole(SubsystemVendorID
.returnValue());
411 data
->config
.subsystemID
= htole(SubsystemID
.returnValue());
412 data
->config
.expansionROM
= htole(ExpansionROM
.returnValue());
413 data
->config
.interruptLine
= htole(InterruptLine
.returnValue());
414 data
->config
.interruptPin
= htole(InterruptPin
.returnValue());
415 data
->config
.minimumGrant
= htole(MinimumGrant
.returnValue());
416 data
->config
.maximumLatency
= htole(MaximumLatency
.returnValue());
418 data
->BARSize
[0] = BAR0Size
;
419 data
->BARSize
[1] = BAR1Size
;
420 data
->BARSize
[2] = BAR2Size
;
421 data
->BARSize
[3] = BAR3Size
;
422 data
->BARSize
[4] = BAR4Size
;
423 data
->BARSize
[5] = BAR5Size
;
425 for (int i
= 0; i
< 6; ++i
) {
426 uint32_t barsize
= data
->BARSize
[i
];
427 if (barsize
!= 0 && !isPowerOf2(barsize
)) {
428 fatal("%s: BAR %d size %d is not a power of 2\n",
429 getInstanceName(), i
, data
->BARSize
[i
]);
436 REGISTER_SIM_OBJECT("PciConfigData", PciConfigData
)
438 #endif // DOXYGEN_SHOULD_SKIP_THIS