76392ccfe3d251b161242b617392f284bc17e0a5
[gem5.git] / src / dev / pcidev.cc
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 /* @file
30 * A single PCI device configuration space entry.
31 */
32
33 #include <list>
34 #include <string>
35 #include <vector>
36
37 #include "base/inifile.hh"
38 #include "base/misc.hh"
39 #include "base/str.hh" // for to_number
40 #include "base/trace.hh"
41 #include "dev/pciconfigall.hh"
42 #include "dev/pcidev.hh"
43 #include "dev/tsunamireg.h"
44 #include "mem/packet.hh"
45 #include "sim/builder.hh"
46 #include "sim/byteswap.hh"
47 #include "sim/param.hh"
48 #include "sim/root.hh"
49
50 using namespace std;
51
52 PciDev::PciDev(Params *p)
53 : DmaDevice(p), plat(p->platform), configData(p->configData),
54 pioDelay(p->pio_delay)
55 {
56 // copy the config data from the PciConfigData object
57 if (configData) {
58 memcpy(config.data, configData->config.data, sizeof(config.data));
59 memcpy(BARSize, configData->BARSize, sizeof(BARSize));
60 memcpy(BARAddrs, configData->BARAddrs, sizeof(BARAddrs));
61 } else
62 panic("NULL pointer to configuration data");
63
64 // Setup pointer in config space to point to this entry
65 if (p->configSpace->deviceExists(p->deviceNum, p->functionNum))
66 panic("Two PCI devices occuping same dev: %#x func: %#x",
67 p->deviceNum, p->functionNum);
68 else
69 p->configSpace->registerDevice(p->deviceNum, p->functionNum, this);
70 }
71
72 void
73 PciDev::readConfig(int offset, uint8_t *data)
74 {
75 if (offset >= PCI_DEVICE_SPECIFIC)
76 panic("Device specific PCI config space not implemented!\n");
77
78 *data = config.data[offset];
79
80 DPRINTF(PCIDEV,
81 "read device: %#x function: %#x register: %#x 1 bytes: data: %#x\n",
82 params()->deviceNum, params()->functionNum, offset, *data);
83 }
84
85 void
86 PciDev::addressRanges(AddrRangeList &range_list)
87 {
88 int x = 0;
89 range_list.clear();
90 for (x = 0; x < 6; x++)
91 if (BARAddrs[x] != 0)
92 range_list.push_back(RangeSize(BARAddrs[x],BARSize[x]));
93 }
94
95 void
96 PciDev::readConfig(int offset, uint16_t *data)
97 {
98 if (offset >= PCI_DEVICE_SPECIFIC)
99 panic("Device specific PCI config space not implemented!\n");
100
101 *data = *(uint16_t*)&config.data[offset];
102
103 DPRINTF(PCIDEV,
104 "read device: %#x function: %#x register: %#x 2 bytes: data: %#x\n",
105 params()->deviceNum, params()->functionNum, offset, *data);
106 }
107
108 void
109 PciDev::readConfig(int offset, uint32_t *data)
110 {
111 if (offset >= PCI_DEVICE_SPECIFIC)
112 panic("Device specific PCI config space not implemented!\n");
113
114 *data = *(uint32_t*)&config.data[offset];
115
116 DPRINTF(PCIDEV,
117 "read device: %#x function: %#x register: %#x 4 bytes: data: %#x\n",
118 params()->deviceNum, params()->functionNum, offset, *data);
119 }
120
121
122 void
123 PciDev::writeConfig(int offset, const uint8_t data)
124 {
125 if (offset >= PCI_DEVICE_SPECIFIC)
126 panic("Device specific PCI config space not implemented!\n");
127
128 DPRINTF(PCIDEV,
129 "write device: %#x function: %#x reg: %#x size: 1 data: %#x\n",
130 params()->deviceNum, params()->functionNum, offset, data);
131
132 switch (offset) {
133 case PCI0_INTERRUPT_LINE:
134 config.interruptLine = data;
135 case PCI_CACHE_LINE_SIZE:
136 config.cacheLineSize = data;
137 case PCI_LATENCY_TIMER:
138 config.latencyTimer = data;
139 break;
140 /* Do nothing for these read-only registers */
141 case PCI0_INTERRUPT_PIN:
142 case PCI0_MINIMUM_GRANT:
143 case PCI0_MAXIMUM_LATENCY:
144 case PCI_CLASS_CODE:
145 case PCI_REVISION_ID:
146 break;
147 default:
148 panic("writing to a read only register");
149 }
150 }
151
152 void
153 PciDev::writeConfig(int offset, const uint16_t data)
154 {
155 if (offset >= PCI_DEVICE_SPECIFIC)
156 panic("Device specific PCI config space not implemented!\n");
157
158 DPRINTF(PCIDEV,
159 "write device: %#x function: %#x reg: %#x size: 2 data: %#x\n",
160 params()->deviceNum, params()->functionNum, offset, data);
161
162 switch (offset) {
163 case PCI_COMMAND:
164 config.command = data;
165 case PCI_STATUS:
166 config.status = data;
167 case PCI_CACHE_LINE_SIZE:
168 config.cacheLineSize = data;
169 break;
170 default:
171 panic("writing to a read only register");
172 }
173 }
174
175
176 void
177 PciDev::writeConfig(int offset, const uint32_t data)
178 {
179 if (offset >= PCI_DEVICE_SPECIFIC)
180 panic("Device specific PCI config space not implemented!\n");
181
182 DPRINTF(PCIDEV,
183 "write device: %#x function: %#x reg: %#x size: 4 data: %#x\n",
184 params()->deviceNum, params()->functionNum, offset, data);
185
186 switch (offset) {
187 case PCI0_BASE_ADDR0:
188 case PCI0_BASE_ADDR1:
189 case PCI0_BASE_ADDR2:
190 case PCI0_BASE_ADDR3:
191 case PCI0_BASE_ADDR4:
192 case PCI0_BASE_ADDR5:
193
194 uint32_t barnum, bar_mask;
195 Addr base_addr, base_size, space_base;
196
197 barnum = BAR_NUMBER(offset);
198
199 if (BAR_IO_SPACE(letoh(config.baseAddr[barnum]))) {
200 bar_mask = BAR_IO_MASK;
201 space_base = TSUNAMI_PCI0_IO;
202 } else {
203 bar_mask = BAR_MEM_MASK;
204 space_base = TSUNAMI_PCI0_MEMORY;
205 }
206
207 // Writing 0xffffffff to a BAR tells the card to set the
208 // value of the bar to size of memory it needs
209 if (letoh(data) == 0xffffffff) {
210 // This is I/O Space, bottom two bits are read only
211
212 config.baseAddr[barnum] = letoh(
213 (~(BARSize[barnum] - 1) & ~bar_mask) |
214 (letoh(config.baseAddr[barnum]) & bar_mask));
215 } else {
216 config.baseAddr[barnum] = letoh(
217 (letoh(data) & ~bar_mask) |
218 (letoh(config.baseAddr[barnum]) & bar_mask));
219
220 if (letoh(config.baseAddr[barnum]) & ~bar_mask) {
221 base_addr = (letoh(data) & ~bar_mask) + space_base;
222 base_size = BARSize[barnum];
223 BARAddrs[barnum] = base_addr;
224
225 pioPort->sendStatusChange(Port::RangeChange);
226 }
227 }
228 break;
229
230 case PCI0_ROM_BASE_ADDR:
231 if (letoh(data) == 0xfffffffe)
232 config.expansionROM = htole((uint32_t)0xffffffff);
233 else
234 config.expansionROM = data;
235 break;
236
237 case PCI_COMMAND:
238 // This could also clear some of the error bits in the Status
239 // register. However they should never get set, so lets ignore
240 // it for now
241 config.command = data;
242 break;
243
244 default:
245 DPRINTF(PCIDEV, "Writing to a read only register");
246 }
247 }
248
249 void
250 PciDev::serialize(ostream &os)
251 {
252 SERIALIZE_ARRAY(BARSize, sizeof(BARSize) / sizeof(BARSize[0]));
253 SERIALIZE_ARRAY(BARAddrs, sizeof(BARAddrs) / sizeof(BARAddrs[0]));
254 SERIALIZE_ARRAY(config.data, sizeof(config.data) / sizeof(config.data[0]));
255 }
256
257 void
258 PciDev::unserialize(Checkpoint *cp, const std::string &section)
259 {
260 UNSERIALIZE_ARRAY(BARSize, sizeof(BARSize) / sizeof(BARSize[0]));
261 UNSERIALIZE_ARRAY(BARAddrs, sizeof(BARAddrs) / sizeof(BARAddrs[0]));
262 UNSERIALIZE_ARRAY(config.data,
263 sizeof(config.data) / sizeof(config.data[0]));
264 }
265
266 #ifndef DOXYGEN_SHOULD_SKIP_THIS
267
268 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PciConfigData)
269
270 Param<uint16_t> VendorID;
271 Param<uint16_t> DeviceID;
272 Param<uint16_t> Command;
273 Param<uint16_t> Status;
274 Param<uint8_t> Revision;
275 Param<uint8_t> ProgIF;
276 Param<uint8_t> SubClassCode;
277 Param<uint8_t> ClassCode;
278 Param<uint8_t> CacheLineSize;
279 Param<uint8_t> LatencyTimer;
280 Param<uint8_t> HeaderType;
281 Param<uint8_t> BIST;
282 Param<uint32_t> BAR0;
283 Param<uint32_t> BAR1;
284 Param<uint32_t> BAR2;
285 Param<uint32_t> BAR3;
286 Param<uint32_t> BAR4;
287 Param<uint32_t> BAR5;
288 Param<uint32_t> CardbusCIS;
289 Param<uint16_t> SubsystemVendorID;
290 Param<uint16_t> SubsystemID;
291 Param<uint32_t> ExpansionROM;
292 Param<uint8_t> InterruptLine;
293 Param<uint8_t> InterruptPin;
294 Param<uint8_t> MinimumGrant;
295 Param<uint8_t> MaximumLatency;
296 Param<uint32_t> BAR0Size;
297 Param<uint32_t> BAR1Size;
298 Param<uint32_t> BAR2Size;
299 Param<uint32_t> BAR3Size;
300 Param<uint32_t> BAR4Size;
301 Param<uint32_t> BAR5Size;
302
303 END_DECLARE_SIM_OBJECT_PARAMS(PciConfigData)
304
305 BEGIN_INIT_SIM_OBJECT_PARAMS(PciConfigData)
306
307 INIT_PARAM(VendorID, "Vendor ID"),
308 INIT_PARAM(DeviceID, "Device ID"),
309 INIT_PARAM_DFLT(Command, "Command Register", 0x00),
310 INIT_PARAM_DFLT(Status, "Status Register", 0x00),
311 INIT_PARAM_DFLT(Revision, "Device Revision", 0x00),
312 INIT_PARAM_DFLT(ProgIF, "Programming Interface", 0x00),
313 INIT_PARAM(SubClassCode, "Sub-Class Code"),
314 INIT_PARAM(ClassCode, "Class Code"),
315 INIT_PARAM_DFLT(CacheLineSize, "System Cacheline Size", 0x00),
316 INIT_PARAM_DFLT(LatencyTimer, "PCI Latency Timer", 0x00),
317 INIT_PARAM_DFLT(HeaderType, "PCI Header Type", 0x00),
318 INIT_PARAM_DFLT(BIST, "Built In Self Test", 0x00),
319 INIT_PARAM_DFLT(BAR0, "Base Address Register 0", 0x00),
320 INIT_PARAM_DFLT(BAR1, "Base Address Register 1", 0x00),
321 INIT_PARAM_DFLT(BAR2, "Base Address Register 2", 0x00),
322 INIT_PARAM_DFLT(BAR3, "Base Address Register 3", 0x00),
323 INIT_PARAM_DFLT(BAR4, "Base Address Register 4", 0x00),
324 INIT_PARAM_DFLT(BAR5, "Base Address Register 5", 0x00),
325 INIT_PARAM_DFLT(CardbusCIS, "Cardbus Card Information Structure", 0x00),
326 INIT_PARAM_DFLT(SubsystemVendorID, "Subsystem Vendor ID", 0x00),
327 INIT_PARAM_DFLT(SubsystemID, "Subsystem ID", 0x00),
328 INIT_PARAM_DFLT(ExpansionROM, "Expansion ROM Base Address Register", 0x00),
329 INIT_PARAM(InterruptLine, "Interrupt Line Register"),
330 INIT_PARAM(InterruptPin, "Interrupt Pin Register"),
331 INIT_PARAM_DFLT(MinimumGrant, "Minimum Grant", 0x00),
332 INIT_PARAM_DFLT(MaximumLatency, "Maximum Latency", 0x00),
333 INIT_PARAM_DFLT(BAR0Size, "Base Address Register 0 Size", 0x00),
334 INIT_PARAM_DFLT(BAR1Size, "Base Address Register 1 Size", 0x00),
335 INIT_PARAM_DFLT(BAR2Size, "Base Address Register 2 Size", 0x00),
336 INIT_PARAM_DFLT(BAR3Size, "Base Address Register 3 Size", 0x00),
337 INIT_PARAM_DFLT(BAR4Size, "Base Address Register 4 Size", 0x00),
338 INIT_PARAM_DFLT(BAR5Size, "Base Address Register 5 Size", 0x00)
339
340 END_INIT_SIM_OBJECT_PARAMS(PciConfigData)
341
342 CREATE_SIM_OBJECT(PciConfigData)
343 {
344 PciConfigData *data = new PciConfigData(getInstanceName());
345
346 data->config.vendor = htole(VendorID);
347 data->config.device = htole(DeviceID);
348 data->config.command = htole(Command);
349 data->config.status = htole(Status);
350 data->config.revision = htole(Revision);
351 data->config.progIF = htole(ProgIF);
352 data->config.subClassCode = htole(SubClassCode);
353 data->config.classCode = htole(ClassCode);
354 data->config.cacheLineSize = htole(CacheLineSize);
355 data->config.latencyTimer = htole(LatencyTimer);
356 data->config.headerType = htole(HeaderType);
357 data->config.bist = htole(BIST);
358
359 data->config.baseAddr0 = htole(BAR0);
360 data->config.baseAddr1 = htole(BAR1);
361 data->config.baseAddr2 = htole(BAR2);
362 data->config.baseAddr3 = htole(BAR3);
363 data->config.baseAddr4 = htole(BAR4);
364 data->config.baseAddr5 = htole(BAR5);
365 data->config.cardbusCIS = htole(CardbusCIS);
366 data->config.subsystemVendorID = htole(SubsystemVendorID);
367 data->config.subsystemID = htole(SubsystemVendorID);
368 data->config.expansionROM = htole(ExpansionROM);
369 data->config.interruptLine = htole(InterruptLine);
370 data->config.interruptPin = htole(InterruptPin);
371 data->config.minimumGrant = htole(MinimumGrant);
372 data->config.maximumLatency = htole(MaximumLatency);
373
374 data->BARSize[0] = BAR0Size;
375 data->BARSize[1] = BAR1Size;
376 data->BARSize[2] = BAR2Size;
377 data->BARSize[3] = BAR3Size;
378 data->BARSize[4] = BAR4Size;
379 data->BARSize[5] = BAR5Size;
380
381 return data;
382 }
383
384 REGISTER_SIM_OBJECT("PciConfigData", PciConfigData)
385
386 #endif // DOXYGEN_SHOULD_SKIP_THIS