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 IdeController::isDiskSelected(IdeDisk
*diskPtr
)
198 for (int i
= 0; i
< 4; i
++) {
199 if ((long)diskPtr
== (long)disks
[i
]) {
200 // is disk is on primary or secondary channel
202 // is disk the master or slave
205 return (dev
[channel
] == devID
);
208 panic("Unable to find disk by pointer!!\n");
212 // Command completion
216 IdeController::setDmaComplete(IdeDisk
*disk
)
218 int diskNum
= getDisk(disk
);
221 panic("Unable to find disk based on pointer %#x\n", disk
);
224 // clear the start/stop bit in the command register
225 bmi_regs
[BMIC0
] &= ~SSBM
;
226 // clear the bus master active bit in the status register
227 bmi_regs
[BMIS0
] &= ~BMIDEA
;
228 // set the interrupt bit
229 bmi_regs
[BMIS0
] |= IDEINTS
;
231 // clear the start/stop bit in the command register
232 bmi_regs
[BMIC1
] &= ~SSBM
;
233 // clear the bus master active bit in the status register
234 bmi_regs
[BMIS1
] &= ~BMIDEA
;
235 // set the interrupt bit
236 bmi_regs
[BMIS1
] |= IDEINTS
;
241 // Interrupt handling
245 IdeController::intrPost()
247 tsunami
->cchip
->postDRIR(configData
->config
.hdr
.pci0
.interruptLine
);
251 IdeController::intrClear()
253 tsunami
->cchip
->clearDRIR(configData
->config
.hdr
.pci0
.interruptLine
);
257 // Bus timing and bus access functions
261 IdeController::cacheAccess(MemReqPtr
&req
)
263 // @todo Add more accurate timing to cache access
264 return curTick
+ 1000;
268 // Read and write handling
272 IdeController::ReadConfig(int offset
, int size
, uint8_t *data
)
276 Addr origOffset
= offset
;
279 if (offset
< PCI_DEVICE_SPECIFIC
) {
280 PciDev::ReadConfig(offset
, size
, data
);
282 if (offset
>= PCI_IDE_TIMING
&& offset
< (PCI_IDE_TIMING
+ 4)) {
283 offset
-= PCI_IDE_TIMING
;
286 if ((offset
+ size
) > (IDETIM
+ 4))
287 panic("PCI read of IDETIM with invalid size\n");
288 } else if (offset
== PCI_SLAVE_TIMING
) {
289 offset
-= PCI_SLAVE_TIMING
;
292 if ((offset
+ size
) > (SIDETIM
+ 1))
293 panic("PCI read of SIDETIM with invalid size\n");
294 } else if (offset
== PCI_UDMA33_CTRL
) {
295 offset
-= PCI_UDMA33_CTRL
;
298 if ((offset
+ size
) > (UDMACTL
+ 1))
299 panic("PCI read of UDMACTL with invalid size\n");
300 } else if (offset
>= PCI_UDMA33_TIMING
&&
301 offset
< (PCI_UDMA33_TIMING
+ 2)) {
302 offset
-= PCI_UDMA33_TIMING
;
305 if ((offset
+ size
) > (UDMATIM
+ 2))
306 panic("PCI read of UDMATIM with invalid size\n");
308 panic("PCI read of unimplemented register: %x\n", offset
);
311 memcpy((void *)data
, (void *)&pci_regs
[offset
], size
);
314 DPRINTF(IdeCtrl
, "IDE PCI read offset: %#x (%#x) size: %#x data: %#x\n",
315 origOffset
, offset
, size
, *(uint32_t *)data
);
319 IdeController::WriteConfig(int offset
, int size
, uint32_t data
)
321 DPRINTF(IdeCtrl
, "IDE PCI write offset: %#x size: %#x data: %#x\n",
324 // do standard write stuff if in standard PCI space
325 if (offset
< PCI_DEVICE_SPECIFIC
) {
326 PciDev::WriteConfig(offset
, size
, data
);
328 if (offset
>= PCI_IDE_TIMING
&& offset
< (PCI_IDE_TIMING
+ 4)) {
329 offset
-= PCI_IDE_TIMING
;
332 if ((offset
+ size
) > (IDETIM
+ 4))
333 panic("PCI write to IDETIM with invalid size\n");
334 } else if (offset
== PCI_SLAVE_TIMING
) {
335 offset
-= PCI_SLAVE_TIMING
;
338 if ((offset
+ size
) > (SIDETIM
+ 1))
339 panic("PCI write to SIDETIM with invalid size\n");
340 } else if (offset
== PCI_UDMA33_CTRL
) {
341 offset
-= PCI_UDMA33_CTRL
;
344 if ((offset
+ size
) > (UDMACTL
+ 1))
345 panic("PCI write to UDMACTL with invalid size\n");
346 } else if (offset
>= PCI_UDMA33_TIMING
&&
347 offset
< (PCI_UDMA33_TIMING
+ 2)) {
348 offset
-= PCI_UDMA33_TIMING
;
351 if ((offset
+ size
) > (UDMATIM
+ 2))
352 panic("PCI write to UDMATIM with invalid size\n");
354 panic("PCI write to unimplemented register: %x\n", offset
);
357 memcpy((void *)&pci_regs
[offset
], (void *)&data
, size
);
360 // Catch the writes to specific PCI registers that have side affects
361 // (like updating the PIO ranges)
364 if (config
.data
[offset
] & PCI_CMD_IOSE
)
369 if (config
.data
[offset
] & PCI_CMD_BME
)
375 case PCI0_BASE_ADDR0
:
376 if (BARAddrs
[0] != 0) {
377 pri_cmd_addr
= BARAddrs
[0];
379 pioInterface
->addAddrRange(pri_cmd_addr
,
380 pri_cmd_addr
+ pri_cmd_size
- 1);
382 pri_cmd_addr
&= PA_UNCACHED_MASK
;
386 case PCI0_BASE_ADDR1
:
387 if (BARAddrs
[1] != 0) {
388 pri_ctrl_addr
= BARAddrs
[1];
390 pioInterface
->addAddrRange(pri_ctrl_addr
,
391 pri_ctrl_addr
+ pri_ctrl_size
- 1);
393 pri_ctrl_addr
&= PA_UNCACHED_MASK
;
397 case PCI0_BASE_ADDR2
:
398 if (BARAddrs
[2] != 0) {
399 sec_cmd_addr
= BARAddrs
[2];
401 pioInterface
->addAddrRange(sec_cmd_addr
,
402 sec_cmd_addr
+ sec_cmd_size
- 1);
404 sec_cmd_addr
&= PA_UNCACHED_MASK
;
408 case PCI0_BASE_ADDR3
:
409 if (BARAddrs
[3] != 0) {
410 sec_ctrl_addr
= BARAddrs
[3];
412 pioInterface
->addAddrRange(sec_ctrl_addr
,
413 sec_ctrl_addr
+ sec_ctrl_size
- 1);
415 sec_ctrl_addr
&= PA_UNCACHED_MASK
;
419 case PCI0_BASE_ADDR4
:
420 if (BARAddrs
[4] != 0) {
421 bmi_addr
= BARAddrs
[4];
423 pioInterface
->addAddrRange(bmi_addr
, bmi_addr
+ bmi_size
- 1);
425 bmi_addr
&= PA_UNCACHED_MASK
;
432 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
441 parseAddr(req
->paddr
, offset
, primary
, type
);
442 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
443 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
448 // sanity check the size (allows byte, word, or dword access)
449 if (req
->size
!= sizeof(uint8_t) && req
->size
!= sizeof(uint16_t) &&
450 req
->size
!= sizeof(uint32_t))
451 panic("IDE controller read of invalid size: %#x\n", req
->size
);
453 if (type
!= BMI_BLOCK
) {
454 assert(req
->size
!= sizeof(uint32_t));
456 disk
= getDisk(primary
);
458 disks
[disk
]->read(offset
, byte
, cmdBlk
, data
);
460 memcpy((void *)data
, &bmi_regs
[offset
], req
->size
);
463 DPRINTF(IdeCtrl
, "IDE read from offset: %#x size: %#x data: %#x\n",
464 offset
, req
->size
, *(uint32_t *)data
);
470 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
479 parseAddr(req
->paddr
, offset
, primary
, type
);
480 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
481 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
483 DPRINTF(IdeCtrl
, "IDE write from offset: %#x size: %#x data: %#x\n",
484 offset
, req
->size
, *(uint32_t *)data
);
486 uint8_t oldVal
, newVal
;
491 if (type
== BMI_BLOCK
&& !bm_enabled
)
494 if (type
!= BMI_BLOCK
) {
495 // shadow the dev bit
496 if (type
== COMMAND_BLOCK
&& offset
== IDE_SELECT_OFFSET
) {
497 uint8_t *devBit
= (primary
? &dev
[0] : &dev
[1]);
498 *devBit
= ((*data
& IDE_SELECT_DEV_BIT
) ? 1 : 0);
501 assert(req
->size
!= sizeof(uint32_t));
503 disk
= getDisk(primary
);
505 disks
[disk
]->write(offset
, byte
, cmdBlk
, data
);
508 // Bus master IDE command register
511 if (req
->size
!= sizeof(uint8_t))
512 panic("Invalid BMIC write size: %x\n", req
->size
);
514 // select the current disk based on DEV bit
515 disk
= getDisk(primary
);
517 oldVal
= bmi_regs
[offset
];
520 // if a DMA transfer is in progress, R/W control cannot change
522 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
523 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
527 // see if the start/stop bit is being changed
528 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
530 // stopping DMA transfer
531 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
533 // clear the BMIDEA bit
534 bmi_regs
[offset
+ 0x2] &= ~BMIDEA
;
536 if (disks
[disk
] == NULL
)
537 panic("DMA stop for disk %d which does not exist\n",
540 // inform the disk of the DMA transfer abort
541 disks
[disk
]->abortDma();
543 // starting DMA transfer
544 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
546 // set the BMIDEA bit
547 bmi_regs
[offset
+ 0x2] |= BMIDEA
;
549 if (disks
[disk
] == NULL
)
550 panic("DMA start for disk %d which does not exist\n",
553 // inform the disk of the DMA transfer start
555 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP0
]);
557 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP1
]);
561 // update the register value
562 bmi_regs
[offset
] = newVal
;
565 // Bus master IDE status register
568 if (req
->size
!= sizeof(uint8_t))
569 panic("Invalid BMIS write size: %x\n", req
->size
);
571 oldVal
= bmi_regs
[offset
];
574 // the BMIDEA bit is RO
575 newVal
|= (oldVal
& BMIDEA
);
577 // to reset (set 0) IDEINTS and IDEDMAE, write 1 to each
578 if ((oldVal
& IDEINTS
) && (newVal
& IDEINTS
))
579 newVal
&= ~IDEINTS
; // clear the interrupt?
581 (oldVal
& IDEINTS
) ? newVal
|= IDEINTS
: newVal
&= ~IDEINTS
;
583 if ((oldVal
& IDEDMAE
) && (newVal
& IDEDMAE
))
586 (oldVal
& IDEDMAE
) ? newVal
|= IDEDMAE
: newVal
&= ~IDEDMAE
;
588 bmi_regs
[offset
] = newVal
;
591 // Bus master IDE descriptor table pointer register
594 if (req
->size
!= sizeof(uint32_t))
595 panic("Invalid BMIDTP write size: %x\n", req
->size
);
597 *(uint32_t *)&bmi_regs
[offset
] = *(uint32_t *)data
& ~0x3;
601 if (req
->size
!= sizeof(uint8_t) &&
602 req
->size
!= sizeof(uint16_t) &&
603 req
->size
!= sizeof(uint32_t))
604 panic("IDE controller write of invalid write size: %x\n",
607 // do a default copy of data into the registers
608 memcpy((void *)&bmi_regs
[offset
], data
, req
->size
);
620 IdeController::serialize(std::ostream
&os
)
622 // Serialize the PciDev base class
623 PciDev::serialize(os
);
625 // Serialize register addresses and sizes
626 SERIALIZE_SCALAR(pri_cmd_addr
);
627 SERIALIZE_SCALAR(pri_cmd_size
);
628 SERIALIZE_SCALAR(pri_ctrl_addr
);
629 SERIALIZE_SCALAR(pri_ctrl_size
);
630 SERIALIZE_SCALAR(sec_cmd_addr
);
631 SERIALIZE_SCALAR(sec_cmd_size
);
632 SERIALIZE_SCALAR(sec_ctrl_addr
);
633 SERIALIZE_SCALAR(sec_ctrl_size
);
634 SERIALIZE_SCALAR(bmi_addr
);
635 SERIALIZE_SCALAR(bmi_size
);
637 // Serialize registers
638 SERIALIZE_ARRAY(bmi_regs
, 16);
639 SERIALIZE_ARRAY(dev
, 2);
640 SERIALIZE_ARRAY(pci_regs
, 8);
642 // Serialize internal state
643 SERIALIZE_SCALAR(io_enabled
);
644 SERIALIZE_SCALAR(bm_enabled
);
645 SERIALIZE_ARRAY(cmd_in_progress
, 4);
649 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
651 // Unserialize the PciDev base class
652 PciDev::unserialize(cp
, section
);
654 // Unserialize register addresses and sizes
655 UNSERIALIZE_SCALAR(pri_cmd_addr
);
656 UNSERIALIZE_SCALAR(pri_cmd_size
);
657 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
658 UNSERIALIZE_SCALAR(pri_ctrl_size
);
659 UNSERIALIZE_SCALAR(sec_cmd_addr
);
660 UNSERIALIZE_SCALAR(sec_cmd_size
);
661 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
662 UNSERIALIZE_SCALAR(sec_ctrl_size
);
663 UNSERIALIZE_SCALAR(bmi_addr
);
664 UNSERIALIZE_SCALAR(bmi_size
);
666 // Unserialize registers
667 UNSERIALIZE_ARRAY(bmi_regs
, 16);
668 UNSERIALIZE_ARRAY(dev
, 2);
669 UNSERIALIZE_ARRAY(pci_regs
, 8);
671 // Unserialize internal state
672 UNSERIALIZE_SCALAR(io_enabled
);
673 UNSERIALIZE_SCALAR(bm_enabled
);
674 UNSERIALIZE_ARRAY(cmd_in_progress
, 4);
677 pioInterface
->addAddrRange(pri_cmd_addr
, pri_cmd_addr
+
679 pioInterface
->addAddrRange(pri_ctrl_addr
, pri_ctrl_addr
+
681 pioInterface
->addAddrRange(sec_cmd_addr
, sec_cmd_addr
+
683 pioInterface
->addAddrRange(sec_ctrl_addr
, sec_ctrl_addr
+
685 pioInterface
->addAddrRange(bmi_addr
, bmi_addr
+ bmi_size
- 1);
689 #ifndef DOXYGEN_SHOULD_SKIP_THIS
691 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
693 SimObjectParam
<IntrControl
*> intr_ctrl
;
694 SimObjectVectorParam
<IdeDisk
*> disks
;
695 SimObjectParam
<MemoryController
*> mmu
;
696 SimObjectParam
<PciConfigAll
*> configspace
;
697 SimObjectParam
<PciConfigData
*> configdata
;
698 SimObjectParam
<Tsunami
*> tsunami
;
699 Param
<uint32_t> pci_bus
;
700 Param
<uint32_t> pci_dev
;
701 Param
<uint32_t> pci_func
;
702 SimObjectParam
<Bus
*> io_bus
;
703 SimObjectParam
<HierParams
*> hier
;
705 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
707 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
709 INIT_PARAM(intr_ctrl
, "Interrupt Controller"),
710 INIT_PARAM(disks
, "IDE disks attached to this controller"),
711 INIT_PARAM(mmu
, "Memory controller"),
712 INIT_PARAM(configspace
, "PCI Configspace"),
713 INIT_PARAM(configdata
, "PCI Config data"),
714 INIT_PARAM(tsunami
, "Tsunami chipset pointer"),
715 INIT_PARAM(pci_bus
, "PCI bus ID"),
716 INIT_PARAM(pci_dev
, "PCI device number"),
717 INIT_PARAM(pci_func
, "PCI function code"),
718 INIT_PARAM_DFLT(io_bus
, "Host bus to attach to", NULL
),
719 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
721 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
723 CREATE_SIM_OBJECT(IdeController
)
725 return new IdeController(getInstanceName(), intr_ctrl
, disks
, mmu
,
726 configspace
, configdata
, tsunami
, pci_bus
,
727 pci_dev
, pci_func
, io_bus
, hier
);
730 REGISTER_SIM_OBJECT("IdeController", IdeController
)
732 #endif //DOXYGEN_SHOULD_SKIP_THIS