46a0e817933c9917b1c83bbab4a173ea1188a841
2 * Copyright (c) 2004 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 #include "base/trace.hh"
35 #include "cpu/intr_control.hh"
37 #include "dev/pcireg.h"
38 #include "dev/pciconfigall.hh"
39 #include "dev/ide_disk.hh"
40 #include "dev/ide_ctrl.hh"
41 #include "dev/tsunami_cchip.hh"
42 #include "mem/bus/bus.hh"
43 #include "mem/bus/pio_interface.hh"
44 #include "mem/bus/pio_interface_impl.hh"
45 #include "mem/bus/dma_interface.hh"
46 #include "dev/tsunami.hh"
47 #include "mem/functional_mem/memory_control.hh"
48 #include "mem/functional_mem/physical_memory.hh"
49 #include "sim/builder.hh"
50 #include "sim/sim_object.hh"
55 // Initialization and destruction
58 IdeController::IdeController(const string
&name
, IntrControl
*ic
,
59 const vector
<IdeDisk
*> &new_disks
,
60 MemoryController
*mmu
, PciConfigAll
*cf
,
61 PciConfigData
*cd
, Tsunami
*t
, uint32_t bus_num
,
62 uint32_t dev_num
, uint32_t func_num
,
63 Bus
*host_bus
, HierParams
*hier
)
64 : PciDev(name
, mmu
, cf
, cd
, bus_num
, dev_num
, func_num
), tsunami(t
)
66 // put back pointer into Tsunami
67 tsunami
->disk_controller
= this;
69 // initialize the PIO interface addresses
71 pri_cmd_size
= BARSize
[0];
74 pri_ctrl_size
= BARSize
[1];
77 sec_cmd_size
= BARSize
[2];
80 sec_ctrl_size
= BARSize
[3];
82 // initialize the bus master interface (BMI) address to be configured
85 bmi_size
= BARSize
[4];
87 // zero out all of the registers
88 memset(bmi_regs
, 0, sizeof(bmi_regs
));
89 memset(pci_regs
, 0, sizeof(pci_regs
));
91 // setup initial values
92 *(uint32_t *)&pci_regs
[IDETIM
] = 0x80008000; // enable both channels
93 *(uint8_t *)&bmi_regs
[BMIS0
] = 0x60;
94 *(uint8_t *)&bmi_regs
[BMIS1
] = 0x60;
96 // reset all internal variables
99 memset(cmd_in_progress
, 0, sizeof(cmd_in_progress
));
101 // create the PIO and DMA interfaces
103 pioInterface
= newPioInterface(name
, hier
, host_bus
, this,
104 &IdeController::cacheAccess
);
106 dmaInterface
= new DMAInterface
<Bus
>(name
+ ".dma", host_bus
,
110 // setup the disks attached to controller
111 memset(disks
, 0, sizeof(IdeDisk
*) * 4);
113 if (new_disks
.size() > 3)
114 panic("IDE controllers support a maximum of 4 devices attached!\n");
116 for (int i
= 0; i
< new_disks
.size(); i
++) {
117 disks
[i
] = new_disks
[i
];
118 disks
[i
]->setController(this, dmaInterface
);
122 IdeController::~IdeController()
124 for (int i
= 0; i
< 4; i
++)
134 IdeController::parseAddr(const Addr
&addr
, Addr
&offset
, bool &primary
,
139 if (addr
>= pri_cmd_addr
&& addr
< (pri_cmd_addr
+ pri_cmd_size
)) {
140 offset
-= pri_cmd_addr
;
141 type
= COMMAND_BLOCK
;
143 } else if (addr
>= pri_ctrl_addr
&&
144 addr
< (pri_ctrl_addr
+ pri_ctrl_size
)) {
145 offset
-= pri_ctrl_addr
;
146 type
= CONTROL_BLOCK
;
148 } else if (addr
>= sec_cmd_addr
&&
149 addr
< (sec_cmd_addr
+ sec_cmd_size
)) {
150 offset
-= sec_cmd_addr
;
151 type
= COMMAND_BLOCK
;
153 } else if (addr
>= sec_ctrl_addr
&&
154 addr
< (sec_ctrl_addr
+ sec_ctrl_size
)) {
155 offset
-= sec_ctrl_addr
;
156 type
= CONTROL_BLOCK
;
158 } else if (addr
>= bmi_addr
&& addr
< (bmi_addr
+ bmi_size
)) {
161 primary
= (offset
< BMIC1
) ? true : false;
163 panic("IDE controller access to invalid address: %#x\n", addr
);
168 IdeController::getDisk(bool primary
)
171 uint8_t *devBit
= &dev
[0];
180 assert(*devBit
== 0 || *devBit
== 1);
186 IdeController::getDisk(IdeDisk
*diskPtr
)
188 for (int i
= 0; i
< 4; i
++) {
189 if ((long)diskPtr
== (long)disks
[i
])
196 // Command completion
200 IdeController::setDmaComplete(IdeDisk
*disk
)
202 int diskNum
= getDisk(disk
);
205 panic("Unable to find disk based on pointer %#x\n", disk
);
208 // clear the start/stop bit in the command register
209 bmi_regs
[BMIC0
] &= ~SSBM
;
210 // clear the bus master active bit in the status register
211 bmi_regs
[BMIS0
] &= ~BMIDEA
;
212 // set the interrupt bit
213 bmi_regs
[BMIS0
] |= IDEINTS
;
215 // clear the start/stop bit in the command register
216 bmi_regs
[BMIC1
] &= ~SSBM
;
217 // clear the bus master active bit in the status register
218 bmi_regs
[BMIS1
] &= ~BMIDEA
;
219 // set the interrupt bit
220 bmi_regs
[BMIS1
] |= IDEINTS
;
225 // Interrupt handling
229 IdeController::intrPost()
231 tsunami
->cchip
->postDRIR(configData
->config
.hdr
.pci0
.interruptLine
);
235 IdeController::intrClear()
237 tsunami
->cchip
->clearDRIR(configData
->config
.hdr
.pci0
.interruptLine
);
241 // Bus timing and bus access functions
245 IdeController::cacheAccess(MemReqPtr
&req
)
247 // @todo Add more accurate timing to cache access
248 return curTick
+ 1000;
252 // Read and write handling
256 IdeController::ReadConfig(int offset
, int size
, uint8_t *data
)
260 Addr origOffset
= offset
;
263 if (offset
< PCI_DEVICE_SPECIFIC
) {
264 PciDev::ReadConfig(offset
, size
, data
);
266 if (offset
>= PCI_IDE_TIMING
&& offset
< (PCI_IDE_TIMING
+ 4)) {
267 offset
-= PCI_IDE_TIMING
;
270 if ((offset
+ size
) > (IDETIM
+ 4))
271 panic("PCI read of IDETIM with invalid size\n");
272 } else if (offset
== PCI_SLAVE_TIMING
) {
273 offset
-= PCI_SLAVE_TIMING
;
276 if ((offset
+ size
) > (SIDETIM
+ 1))
277 panic("PCI read of SIDETIM with invalid size\n");
278 } else if (offset
== PCI_UDMA33_CTRL
) {
279 offset
-= PCI_UDMA33_CTRL
;
282 if ((offset
+ size
) > (UDMACTL
+ 1))
283 panic("PCI read of UDMACTL with invalid size\n");
284 } else if (offset
>= PCI_UDMA33_TIMING
&&
285 offset
< (PCI_UDMA33_TIMING
+ 2)) {
286 offset
-= PCI_UDMA33_TIMING
;
289 if ((offset
+ size
) > (UDMATIM
+ 2))
290 panic("PCI read of UDMATIM with invalid size\n");
292 panic("PCI read of unimplemented register: %x\n", offset
);
295 memcpy((void *)data
, (void *)&pci_regs
[offset
], size
);
298 DPRINTF(IdeCtrl
, "IDE PCI read offset: %#x (%#x) size: %#x data: %#x\n",
299 origOffset
, offset
, size
, *(uint32_t *)data
);
303 IdeController::WriteConfig(int offset
, int size
, uint32_t data
)
305 DPRINTF(IdeCtrl
, "IDE PCI write offset: %#x size: %#x data: %#x\n",
308 // do standard write stuff if in standard PCI space
309 if (offset
< PCI_DEVICE_SPECIFIC
) {
310 PciDev::WriteConfig(offset
, size
, data
);
312 if (offset
>= PCI_IDE_TIMING
&& offset
< (PCI_IDE_TIMING
+ 4)) {
313 offset
-= PCI_IDE_TIMING
;
316 if ((offset
+ size
) > (IDETIM
+ 4))
317 panic("PCI write to IDETIM with invalid size\n");
318 } else if (offset
== PCI_SLAVE_TIMING
) {
319 offset
-= PCI_SLAVE_TIMING
;
322 if ((offset
+ size
) > (SIDETIM
+ 1))
323 panic("PCI write to SIDETIM with invalid size\n");
324 } else if (offset
== PCI_UDMA33_CTRL
) {
325 offset
-= PCI_UDMA33_CTRL
;
328 if ((offset
+ size
) > (UDMACTL
+ 1))
329 panic("PCI write to UDMACTL with invalid size\n");
330 } else if (offset
>= PCI_UDMA33_TIMING
&&
331 offset
< (PCI_UDMA33_TIMING
+ 2)) {
332 offset
-= PCI_UDMA33_TIMING
;
335 if ((offset
+ size
) > (UDMATIM
+ 2))
336 panic("PCI write to UDMATIM with invalid size\n");
338 panic("PCI write to unimplemented register: %x\n", offset
);
341 memcpy((void *)&pci_regs
[offset
], (void *)&data
, size
);
344 // Catch the writes to specific PCI registers that have side affects
345 // (like updating the PIO ranges)
348 if (config
.data
[offset
] & PCI_CMD_IOSE
)
353 if (config
.data
[offset
] & PCI_CMD_BME
)
359 case PCI0_BASE_ADDR0
:
360 if (BARAddrs
[0] != 0) {
361 pri_cmd_addr
= BARAddrs
[0];
363 pioInterface
->addAddrRange(pri_cmd_addr
,
364 pri_cmd_addr
+ pri_cmd_size
- 1);
366 pri_cmd_addr
&= PA_UNCACHED_MASK
;
370 case PCI0_BASE_ADDR1
:
371 if (BARAddrs
[1] != 0) {
372 pri_ctrl_addr
= BARAddrs
[1];
374 pioInterface
->addAddrRange(pri_ctrl_addr
,
375 pri_ctrl_addr
+ pri_ctrl_size
- 1);
377 pri_ctrl_addr
&= PA_UNCACHED_MASK
;
381 case PCI0_BASE_ADDR2
:
382 if (BARAddrs
[2] != 0) {
383 sec_cmd_addr
= BARAddrs
[2];
385 pioInterface
->addAddrRange(sec_cmd_addr
,
386 sec_cmd_addr
+ sec_cmd_size
- 1);
388 sec_cmd_addr
&= PA_UNCACHED_MASK
;
392 case PCI0_BASE_ADDR3
:
393 if (BARAddrs
[3] != 0) {
394 sec_ctrl_addr
= BARAddrs
[3];
396 pioInterface
->addAddrRange(sec_ctrl_addr
,
397 sec_ctrl_addr
+ sec_ctrl_size
- 1);
399 sec_ctrl_addr
&= PA_UNCACHED_MASK
;
403 case PCI0_BASE_ADDR4
:
404 if (BARAddrs
[4] != 0) {
405 bmi_addr
= BARAddrs
[4];
407 pioInterface
->addAddrRange(bmi_addr
, bmi_addr
+ bmi_size
- 1);
409 bmi_addr
&= PA_UNCACHED_MASK
;
416 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
425 parseAddr(req
->paddr
, offset
, primary
, type
);
426 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
427 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
432 // sanity check the size (allows byte, word, or dword access)
433 if (req
->size
!= sizeof(uint8_t) && req
->size
!= sizeof(uint16_t) &&
434 req
->size
!= sizeof(uint32_t))
435 panic("IDE controller read of invalid size: %#x\n", req
->size
);
437 if (type
!= BMI_BLOCK
) {
438 assert(req
->size
!= sizeof(uint32_t));
440 disk
= getDisk(primary
);
442 disks
[disk
]->read(offset
, byte
, cmdBlk
, data
);
444 memcpy((void *)data
, &bmi_regs
[offset
], req
->size
);
447 DPRINTF(IdeCtrl
, "IDE read from offset: %#x size: %#x data: %#x\n",
448 offset
, req
->size
, *(uint32_t *)data
);
454 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
463 parseAddr(req
->paddr
, offset
, primary
, type
);
464 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
465 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
467 DPRINTF(IdeCtrl
, "IDE write from offset: %#x size: %#x data: %#x\n",
468 offset
, req
->size
, *(uint32_t *)data
);
470 uint8_t oldVal
, newVal
;
475 if (type
== BMI_BLOCK
&& !bm_enabled
)
478 if (type
!= BMI_BLOCK
) {
479 // shadow the dev bit
480 if (type
== COMMAND_BLOCK
&& offset
== IDE_SELECT_OFFSET
) {
481 uint8_t *devBit
= (primary
? &dev
[0] : &dev
[1]);
482 *devBit
= ((*data
& IDE_SELECT_DEV_BIT
) ? 1 : 0);
485 assert(req
->size
!= sizeof(uint32_t));
487 disk
= getDisk(primary
);
489 disks
[disk
]->write(offset
, byte
, cmdBlk
, data
);
492 // Bus master IDE command register
495 if (req
->size
!= sizeof(uint8_t))
496 panic("Invalid BMIC write size: %x\n", req
->size
);
498 // select the current disk based on DEV bit
499 disk
= getDisk(primary
);
501 oldVal
= bmi_regs
[offset
];
504 // if a DMA transfer is in progress, R/W control cannot change
506 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
507 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
511 // see if the start/stop bit is being changed
512 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
514 // stopping DMA transfer
515 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
517 // clear the BMIDEA bit
518 bmi_regs
[offset
+ 0x2] &= ~BMIDEA
;
520 if (disks
[disk
] == NULL
)
521 panic("DMA stop for disk %d which does not exist\n",
524 // inform the disk of the DMA transfer abort
525 disks
[disk
]->abortDma();
527 // starting DMA transfer
528 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
530 // set the BMIDEA bit
531 bmi_regs
[offset
+ 0x2] |= BMIDEA
;
533 if (disks
[disk
] == NULL
)
534 panic("DMA start for disk %d which does not exist\n",
537 // inform the disk of the DMA transfer start
539 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP0
]);
541 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP1
]);
545 // update the register value
546 bmi_regs
[offset
] = newVal
;
549 // Bus master IDE status register
552 if (req
->size
!= sizeof(uint8_t))
553 panic("Invalid BMIS write size: %x\n", req
->size
);
555 oldVal
= bmi_regs
[offset
];
558 // the BMIDEA bit is RO
559 newVal
|= (oldVal
& BMIDEA
);
561 // to reset (set 0) IDEINTS and IDEDMAE, write 1 to each
562 if ((oldVal
& IDEINTS
) && (newVal
& IDEINTS
))
563 newVal
&= ~IDEINTS
; // clear the interrupt?
565 (oldVal
& IDEINTS
) ? newVal
|= IDEINTS
: newVal
&= ~IDEINTS
;
567 if ((oldVal
& IDEDMAE
) && (newVal
& IDEDMAE
))
570 (oldVal
& IDEDMAE
) ? newVal
|= IDEDMAE
: newVal
&= ~IDEDMAE
;
572 bmi_regs
[offset
] = newVal
;
575 // Bus master IDE descriptor table pointer register
578 if (req
->size
!= sizeof(uint32_t))
579 panic("Invalid BMIDTP write size: %x\n", req
->size
);
581 *(uint32_t *)&bmi_regs
[offset
] = *(uint32_t *)data
& ~0x3;
585 if (req
->size
!= sizeof(uint8_t) &&
586 req
->size
!= sizeof(uint16_t) &&
587 req
->size
!= sizeof(uint32_t))
588 panic("IDE controller write of invalid write size: %x\n",
591 // do a default copy of data into the registers
592 memcpy((void *)&bmi_regs
[offset
], data
, req
->size
);
604 IdeController::serialize(std::ostream
&os
)
606 // Serialize the PciDev base class
607 PciDev::serialize(os
);
609 // Serialize register addresses and sizes
610 SERIALIZE_SCALAR(pri_cmd_addr
);
611 SERIALIZE_SCALAR(pri_cmd_size
);
612 SERIALIZE_SCALAR(pri_ctrl_addr
);
613 SERIALIZE_SCALAR(pri_ctrl_size
);
614 SERIALIZE_SCALAR(sec_cmd_addr
);
615 SERIALIZE_SCALAR(sec_cmd_size
);
616 SERIALIZE_SCALAR(sec_ctrl_addr
);
617 SERIALIZE_SCALAR(sec_ctrl_size
);
618 SERIALIZE_SCALAR(bmi_addr
);
619 SERIALIZE_SCALAR(bmi_size
);
621 // Serialize registers
622 SERIALIZE_ARRAY(bmi_regs
, 16);
623 SERIALIZE_ARRAY(dev
, 2);
624 SERIALIZE_ARRAY(pci_regs
, 8);
626 // Serialize internal state
627 SERIALIZE_SCALAR(io_enabled
);
628 SERIALIZE_SCALAR(bm_enabled
);
629 SERIALIZE_ARRAY(cmd_in_progress
, 4);
633 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
635 // Unserialize the PciDev base class
636 PciDev::unserialize(cp
, section
);
638 // Unserialize register addresses and sizes
639 UNSERIALIZE_SCALAR(pri_cmd_addr
);
640 UNSERIALIZE_SCALAR(pri_cmd_size
);
641 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
642 UNSERIALIZE_SCALAR(pri_ctrl_size
);
643 UNSERIALIZE_SCALAR(sec_cmd_addr
);
644 UNSERIALIZE_SCALAR(sec_cmd_size
);
645 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
646 UNSERIALIZE_SCALAR(sec_ctrl_size
);
647 UNSERIALIZE_SCALAR(bmi_addr
);
648 UNSERIALIZE_SCALAR(bmi_size
);
650 // Unserialize registers
651 UNSERIALIZE_ARRAY(bmi_regs
, 16);
652 UNSERIALIZE_ARRAY(dev
, 2);
653 UNSERIALIZE_ARRAY(pci_regs
, 8);
655 // Unserialize internal state
656 UNSERIALIZE_SCALAR(io_enabled
);
657 UNSERIALIZE_SCALAR(bm_enabled
);
658 UNSERIALIZE_ARRAY(cmd_in_progress
, 4);
661 pioInterface
->addAddrRange(pri_cmd_addr
, pri_cmd_addr
+
663 pioInterface
->addAddrRange(pri_ctrl_addr
, pri_ctrl_addr
+
665 pioInterface
->addAddrRange(sec_cmd_addr
, sec_cmd_addr
+
667 pioInterface
->addAddrRange(sec_ctrl_addr
, sec_ctrl_addr
+
669 pioInterface
->addAddrRange(bmi_addr
, bmi_addr
+ bmi_size
- 1);
673 #ifndef DOXYGEN_SHOULD_SKIP_THIS
675 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
677 SimObjectParam
<IntrControl
*> intr_ctrl
;
678 SimObjectVectorParam
<IdeDisk
*> disks
;
679 SimObjectParam
<MemoryController
*> mmu
;
680 SimObjectParam
<PciConfigAll
*> configspace
;
681 SimObjectParam
<PciConfigData
*> configdata
;
682 SimObjectParam
<Tsunami
*> tsunami
;
683 Param
<uint32_t> pci_bus
;
684 Param
<uint32_t> pci_dev
;
685 Param
<uint32_t> pci_func
;
686 SimObjectParam
<Bus
*> io_bus
;
687 SimObjectParam
<HierParams
*> hier
;
689 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
691 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
693 INIT_PARAM(intr_ctrl
, "Interrupt Controller"),
694 INIT_PARAM(disks
, "IDE disks attached to this controller"),
695 INIT_PARAM(mmu
, "Memory controller"),
696 INIT_PARAM(configspace
, "PCI Configspace"),
697 INIT_PARAM(configdata
, "PCI Config data"),
698 INIT_PARAM(tsunami
, "Tsunami chipset pointer"),
699 INIT_PARAM(pci_bus
, "PCI bus ID"),
700 INIT_PARAM(pci_dev
, "PCI device number"),
701 INIT_PARAM(pci_func
, "PCI function code"),
702 INIT_PARAM_DFLT(io_bus
, "Host bus to attach to", NULL
),
703 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
705 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
707 CREATE_SIM_OBJECT(IdeController
)
709 return new IdeController(getInstanceName(), intr_ctrl
, disks
, mmu
,
710 configspace
, configdata
, tsunami
, pci_bus
,
711 pci_dev
, pci_func
, io_bus
, hier
);
714 REGISTER_SIM_OBJECT("IdeController", IdeController
)
716 #endif //DOXYGEN_SHOULD_SKIP_THIS