2 * Copyright (c) 2004-2005 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"
36 #include "dev/ide_ctrl.hh"
37 #include "dev/ide_disk.hh"
38 #include "dev/pciconfigall.hh"
39 #include "dev/pcireg.h"
40 #include "dev/platform.hh"
41 #include "mem/bus/bus.hh"
42 #include "mem/bus/dma_interface.hh"
43 #include "mem/bus/pio_interface.hh"
44 #include "mem/bus/pio_interface_impl.hh"
45 #include "mem/functional/memory_control.hh"
46 #include "mem/functional/physical.hh"
47 #include "sim/builder.hh"
48 #include "sim/sim_object.hh"
53 // Initialization and destruction
56 IdeController::IdeController(Params
*p
)
59 // initialize the PIO interface addresses
61 pri_cmd_size
= BARSize
[0];
64 pri_ctrl_size
= BARSize
[1];
67 sec_cmd_size
= BARSize
[2];
70 sec_ctrl_size
= BARSize
[3];
72 // initialize the bus master interface (BMI) address to be configured
75 bmi_size
= BARSize
[4];
77 // zero out all of the registers
78 memset(bmi_regs
, 0, sizeof(bmi_regs
));
79 memset(pci_config_regs
.data
, 0, sizeof(pci_config_regs
.data
));
81 // setup initial values
82 pci_config_regs
.idetim
= htoa((uint32_t)0x80008000); // enable both channels
83 *(uint8_t *)&bmi_regs
[BMIS0
] = 0x60;
84 *(uint8_t *)&bmi_regs
[BMIS1
] = 0x60;
86 // reset all internal variables
89 memset(cmd_in_progress
, 0, sizeof(cmd_in_progress
));
91 // create the PIO and DMA interfaces
92 if (params()->host_bus
) {
93 pioInterface
= newPioInterface(name(), params()->hier
,
94 params()->host_bus
, this,
95 &IdeController::cacheAccess
);
97 dmaInterface
= new DMAInterface
<Bus
>(name() + ".dma",
99 params()->host_bus
, 1,
101 pioLatency
= params()->pio_latency
* params()->host_bus
->clockRate
;
104 // setup the disks attached to controller
105 memset(disks
, 0, sizeof(IdeDisk
*) * 4);
109 if (params()->disks
.size() > 3)
110 panic("IDE controllers support a maximum of 4 devices attached!\n");
112 for (int i
= 0; i
< params()->disks
.size(); i
++) {
113 disks
[i
] = params()->disks
[i
];
114 disks
[i
]->setController(this, dmaInterface
);
118 IdeController::~IdeController()
120 for (int i
= 0; i
< 4; i
++)
130 IdeController::parseAddr(const Addr
&addr
, Addr
&offset
, bool &primary
,
135 if (addr
>= pri_cmd_addr
&& addr
< (pri_cmd_addr
+ pri_cmd_size
)) {
136 offset
-= pri_cmd_addr
;
137 type
= COMMAND_BLOCK
;
139 } else if (addr
>= pri_ctrl_addr
&&
140 addr
< (pri_ctrl_addr
+ pri_ctrl_size
)) {
141 offset
-= pri_ctrl_addr
;
142 type
= CONTROL_BLOCK
;
144 } else if (addr
>= sec_cmd_addr
&&
145 addr
< (sec_cmd_addr
+ sec_cmd_size
)) {
146 offset
-= sec_cmd_addr
;
147 type
= COMMAND_BLOCK
;
149 } else if (addr
>= sec_ctrl_addr
&&
150 addr
< (sec_ctrl_addr
+ sec_ctrl_size
)) {
151 offset
-= sec_ctrl_addr
;
152 type
= CONTROL_BLOCK
;
154 } else if (addr
>= bmi_addr
&& addr
< (bmi_addr
+ bmi_size
)) {
157 primary
= (offset
< BMIC1
) ? true : false;
159 panic("IDE controller access to invalid address: %#x\n", addr
);
164 IdeController::getDisk(bool primary
)
167 uint8_t *devBit
= &dev
[0];
176 assert(*devBit
== 0 || *devBit
== 1);
182 IdeController::getDisk(IdeDisk
*diskPtr
)
184 for (int i
= 0; i
< 4; i
++) {
185 if ((long)diskPtr
== (long)disks
[i
])
192 IdeController::isDiskSelected(IdeDisk
*diskPtr
)
194 for (int i
= 0; i
< 4; i
++) {
195 if ((long)diskPtr
== (long)disks
[i
]) {
196 // is disk is on primary or secondary channel
198 // is disk the master or slave
201 return (dev
[channel
] == devID
);
204 panic("Unable to find disk by pointer!!\n");
208 // Command completion
212 IdeController::setDmaComplete(IdeDisk
*disk
)
214 int diskNum
= getDisk(disk
);
217 panic("Unable to find disk based on pointer %#x\n", disk
);
220 // clear the start/stop bit in the command register
221 bmi_regs
[BMIC0
] &= ~SSBM
;
222 // clear the bus master active bit in the status register
223 bmi_regs
[BMIS0
] &= ~BMIDEA
;
224 // set the interrupt bit
225 bmi_regs
[BMIS0
] |= IDEINTS
;
227 // clear the start/stop bit in the command register
228 bmi_regs
[BMIC1
] &= ~SSBM
;
229 // clear the bus master active bit in the status register
230 bmi_regs
[BMIS1
] &= ~BMIDEA
;
231 // set the interrupt bit
232 bmi_regs
[BMIS1
] |= IDEINTS
;
237 // Bus timing and bus access functions
241 IdeController::cacheAccess(MemReqPtr
&req
)
243 // @todo Add more accurate timing to cache access
244 return curTick
+ pioLatency
;
248 // Read and write handling
252 IdeController::ReadConfig(int offset
, int size
, uint8_t *data
)
262 if (offset
< PCI_DEVICE_SPECIFIC
) {
263 PciDev::ReadConfig(offset
, size
, data
);
264 } else if (offset
>= IDE_CTRL_CONFIG_START
&& (offset
+ size
) <= IDE_CTRL_CONFIG_END
) {
266 config_offset
= offset
- IDE_CTRL_CONFIG_START
;
270 case sizeof(uint8_t):
271 case sizeof(uint16_t):
272 case sizeof(uint32_t):
273 memcpy(&byte
, &pci_config_regs
.data
[config_offset
], size
);
277 panic("Invalid PCI configuration read size!\n");
281 case sizeof(uint8_t):
284 case sizeof(uint16_t):
285 *(uint16_t*)data
= htoa(word
);
287 case sizeof(uint32_t):
288 *(uint32_t*)data
= htoa(dword
);
292 DPRINTF(IdeCtrl
, "PCI read offset: %#x size: %#x data: %#x\n",
293 offset
, size
, htoa(dword
));
296 panic("Read of unimplemented PCI config. register: %x\n", offset
);
301 IdeController::WriteConfig(int offset
, int size
, uint32_t data
)
306 if (offset
< PCI_DEVICE_SPECIFIC
) {
307 PciDev::WriteConfig(offset
, size
, data
);
308 } else if (offset
>= IDE_CTRL_CONFIG_START
&& (offset
+ size
) <= IDE_CTRL_CONFIG_END
) {
310 config_offset
= offset
- IDE_CTRL_CONFIG_START
;
312 write_data
= htoa(data
);
315 case sizeof(uint8_t):
316 case sizeof(uint16_t):
317 case sizeof(uint32_t):
318 memcpy(&pci_config_regs
.data
[config_offset
], &write_data
, size
);
322 panic("Invalid PCI configuration write size!\n");
325 panic("Write of unimplemented PCI config. register: %x\n", offset
);
328 DPRINTF(IdeCtrl
, "PCI write offset: %#x size: %#x data: %#x\n",
331 // Catch the writes to specific PCI registers that have side affects
332 // (like updating the PIO ranges)
335 if (config
.data
[offset
] & PCI_CMD_IOSE
)
340 if (config
.data
[offset
] & PCI_CMD_BME
)
346 case PCI0_BASE_ADDR0
:
347 if (BARAddrs
[0] != 0) {
348 pri_cmd_addr
= BARAddrs
[0];
350 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
,
353 pri_cmd_addr
&= EV5::PAddrUncachedMask
;
357 case PCI0_BASE_ADDR1
:
358 if (BARAddrs
[1] != 0) {
359 pri_ctrl_addr
= BARAddrs
[1];
361 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
,
364 pri_ctrl_addr
&= EV5::PAddrUncachedMask
;
368 case PCI0_BASE_ADDR2
:
369 if (BARAddrs
[2] != 0) {
370 sec_cmd_addr
= BARAddrs
[2];
372 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
,
375 sec_cmd_addr
&= EV5::PAddrUncachedMask
;
379 case PCI0_BASE_ADDR3
:
380 if (BARAddrs
[3] != 0) {
381 sec_ctrl_addr
= BARAddrs
[3];
383 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
,
386 sec_ctrl_addr
&= EV5::PAddrUncachedMask
;
390 case PCI0_BASE_ADDR4
:
391 if (BARAddrs
[4] != 0) {
392 bmi_addr
= BARAddrs
[4];
394 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
396 bmi_addr
&= EV5::PAddrUncachedMask
;
403 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
416 parseAddr(req
->paddr
, offset
, primary
, type
);
421 // sanity check the size (allows byte, word, or dword access)
423 case sizeof(uint8_t):
424 case sizeof(uint16_t):
425 case sizeof(uint32_t):
428 panic("IDE controller read of invalid size: %#x\n", req
->size
);
433 memcpy(&byte
, &bmi_regs
[offset
], req
->size
);
435 case sizeof(uint8_t):
438 case sizeof(uint16_t):
439 *(uint16_t*)data
= htoa(word
[0]);
441 case sizeof(uint32_t):
442 *(uint32_t*)data
= htoa(dword
);
449 disk
= getDisk(primary
);
451 if (disks
[disk
] == NULL
)
457 case sizeof(uint16_t):
458 disks
[disk
]->read(offset
, type
, (uint8_t*)&word
[0]);
459 *(uint16_t*)data
= htoa(word
[0]);
462 case sizeof(uint32_t):
463 disks
[disk
]->read(offset
, type
, (uint8_t*)&word
[0]);
464 disks
[disk
]->read(offset
, type
, (uint8_t*)&word
[1]);
465 *(uint32_t*)data
= htoa(dword
);
469 panic("IDE read of data reg invalid size: %#x\n", req
->size
);
473 if (req
->size
== sizeof(uint8_t)) {
474 disks
[disk
]->read(offset
, type
, &byte
);
477 panic("IDE read of command reg of invalid size: %#x\n", req
->size
);
482 DPRINTF(IdeCtrl
, "read from offset: %#x size: %#x data: %#x\n",
483 offset
, req
->size
, htoa(dword
));
489 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
498 parseAddr(req
->paddr
, offset
, primary
, type
);
499 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
500 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
502 DPRINTF(IdeCtrl
, "write from offset: %#x size: %#x data: %#x\n",
504 (*(uint32_t *)data
) & (0xffffffff >> 8 * (4 - req
->size
)));
506 uint8_t oldVal
, newVal
;
511 if (type
== BMI_BLOCK
&& !bm_enabled
)
514 if (type
!= BMI_BLOCK
) {
515 // shadow the dev bit
516 if (type
== COMMAND_BLOCK
&& offset
== IDE_SELECT_OFFSET
) {
517 uint8_t *devBit
= (primary
? &dev
[0] : &dev
[1]);
518 *devBit
= ((*data
& IDE_SELECT_DEV_BIT
) ? 1 : 0);
521 assert(req
->size
!= sizeof(uint32_t));
523 disk
= getDisk(primary
);
525 disks
[disk
]->write(offset
, byte
, cmdBlk
, data
);
528 // Bus master IDE command register
531 if (req
->size
!= sizeof(uint8_t))
532 panic("Invalid BMIC write size: %x\n", req
->size
);
534 // select the current disk based on DEV bit
535 disk
= getDisk(primary
);
537 oldVal
= bmi_regs
[offset
];
540 // if a DMA transfer is in progress, R/W control cannot change
542 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
543 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
547 // see if the start/stop bit is being changed
548 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
550 // stopping DMA transfer
551 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
553 // clear the BMIDEA bit
554 bmi_regs
[offset
+ 0x2] &= ~BMIDEA
;
556 if (disks
[disk
] == NULL
)
557 panic("DMA stop for disk %d which does not exist\n",
560 // inform the disk of the DMA transfer abort
561 disks
[disk
]->abortDma();
563 // starting DMA transfer
564 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
566 // set the BMIDEA bit
567 bmi_regs
[offset
+ 0x2] |= BMIDEA
;
569 if (disks
[disk
] == NULL
)
570 panic("DMA start for disk %d which does not exist\n",
573 // inform the disk of the DMA transfer start
575 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP0
]);
577 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP1
]);
581 // update the register value
582 bmi_regs
[offset
] = newVal
;
585 // Bus master IDE status register
588 if (req
->size
!= sizeof(uint8_t))
589 panic("Invalid BMIS write size: %x\n", req
->size
);
591 oldVal
= bmi_regs
[offset
];
594 // the BMIDEA bit is RO
595 newVal
|= (oldVal
& BMIDEA
);
597 // to reset (set 0) IDEINTS and IDEDMAE, write 1 to each
598 if ((oldVal
& IDEINTS
) && (newVal
& IDEINTS
))
599 newVal
&= ~IDEINTS
; // clear the interrupt?
601 (oldVal
& IDEINTS
) ? newVal
|= IDEINTS
: newVal
&= ~IDEINTS
;
603 if ((oldVal
& IDEDMAE
) && (newVal
& IDEDMAE
))
606 (oldVal
& IDEDMAE
) ? newVal
|= IDEDMAE
: newVal
&= ~IDEDMAE
;
608 bmi_regs
[offset
] = newVal
;
611 // Bus master IDE descriptor table pointer register
614 if (req
->size
!= sizeof(uint32_t))
615 panic("Invalid BMIDTP write size: %x\n", req
->size
);
617 *(uint32_t *)&bmi_regs
[offset
] = *(uint32_t *)data
& ~0x3;
621 if (req
->size
!= sizeof(uint8_t) &&
622 req
->size
!= sizeof(uint16_t) &&
623 req
->size
!= sizeof(uint32_t))
624 panic("IDE controller write of invalid write size: %x\n",
627 // do a default copy of data into the registers
628 memcpy((void *)&bmi_regs
[offset
], data
, req
->size
);
640 IdeController::serialize(std::ostream
&os
)
642 // Serialize the PciDev base class
643 PciDev::serialize(os
);
645 // Serialize register addresses and sizes
646 SERIALIZE_SCALAR(pri_cmd_addr
);
647 SERIALIZE_SCALAR(pri_cmd_size
);
648 SERIALIZE_SCALAR(pri_ctrl_addr
);
649 SERIALIZE_SCALAR(pri_ctrl_size
);
650 SERIALIZE_SCALAR(sec_cmd_addr
);
651 SERIALIZE_SCALAR(sec_cmd_size
);
652 SERIALIZE_SCALAR(sec_ctrl_addr
);
653 SERIALIZE_SCALAR(sec_ctrl_size
);
654 SERIALIZE_SCALAR(bmi_addr
);
655 SERIALIZE_SCALAR(bmi_size
);
657 // Serialize registers
658 SERIALIZE_ARRAY(bmi_regs
, 16);
659 SERIALIZE_ARRAY(dev
, 2);
660 SERIALIZE_ARRAY(pci_config_regs
.data
, 22);
662 // Serialize internal state
663 SERIALIZE_SCALAR(io_enabled
);
664 SERIALIZE_SCALAR(bm_enabled
);
665 SERIALIZE_ARRAY(cmd_in_progress
, 4);
669 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
671 // Unserialize the PciDev base class
672 PciDev::unserialize(cp
, section
);
674 // Unserialize register addresses and sizes
675 UNSERIALIZE_SCALAR(pri_cmd_addr
);
676 UNSERIALIZE_SCALAR(pri_cmd_size
);
677 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
678 UNSERIALIZE_SCALAR(pri_ctrl_size
);
679 UNSERIALIZE_SCALAR(sec_cmd_addr
);
680 UNSERIALIZE_SCALAR(sec_cmd_size
);
681 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
682 UNSERIALIZE_SCALAR(sec_ctrl_size
);
683 UNSERIALIZE_SCALAR(bmi_addr
);
684 UNSERIALIZE_SCALAR(bmi_size
);
686 // Unserialize registers
687 UNSERIALIZE_ARRAY(bmi_regs
, 16);
688 UNSERIALIZE_ARRAY(dev
, 2);
689 UNSERIALIZE_ARRAY(pci_config_regs
.data
, 22);
691 // Unserialize internal state
692 UNSERIALIZE_SCALAR(io_enabled
);
693 UNSERIALIZE_SCALAR(bm_enabled
);
694 UNSERIALIZE_ARRAY(cmd_in_progress
, 4);
697 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
, pri_cmd_size
));
698 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
, pri_ctrl_size
));
699 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
, sec_cmd_size
));
700 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
, sec_ctrl_size
));
701 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
705 #ifndef DOXYGEN_SHOULD_SKIP_THIS
707 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
710 SimObjectVectorParam
<IdeDisk
*> disks
;
711 SimObjectParam
<MemoryController
*> mmu
;
712 SimObjectParam
<PciConfigAll
*> configspace
;
713 SimObjectParam
<PciConfigData
*> configdata
;
714 SimObjectParam
<Platform
*> platform
;
715 Param
<uint32_t> pci_bus
;
716 Param
<uint32_t> pci_dev
;
717 Param
<uint32_t> pci_func
;
718 SimObjectParam
<Bus
*> io_bus
;
719 Param
<Tick
> pio_latency
;
720 SimObjectParam
<HierParams
*> hier
;
722 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
724 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
726 INIT_PARAM(addr
, "Device Address"),
727 INIT_PARAM(disks
, "IDE disks attached to this controller"),
728 INIT_PARAM(mmu
, "Memory controller"),
729 INIT_PARAM(configspace
, "PCI Configspace"),
730 INIT_PARAM(configdata
, "PCI Config data"),
731 INIT_PARAM(platform
, "Platform pointer"),
732 INIT_PARAM(pci_bus
, "PCI bus ID"),
733 INIT_PARAM(pci_dev
, "PCI device number"),
734 INIT_PARAM(pci_func
, "PCI function code"),
735 INIT_PARAM_DFLT(io_bus
, "Host bus to attach to", NULL
),
736 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
737 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
739 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
741 CREATE_SIM_OBJECT(IdeController
)
743 IdeController::Params
*params
= new IdeController::Params
;
744 params
->name
= getInstanceName();
746 params
->configSpace
= configspace
;
747 params
->configData
= configdata
;
748 params
->plat
= platform
;
749 params
->busNum
= pci_bus
;
750 params
->deviceNum
= pci_dev
;
751 params
->functionNum
= pci_func
;
753 params
->disks
= disks
;
754 params
->host_bus
= io_bus
;
755 params
->pio_latency
= pio_latency
;
757 return new IdeController(params
);
760 REGISTER_SIM_OBJECT("IdeController", IdeController
)
762 #endif //DOXYGEN_SHOULD_SKIP_THIS