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