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