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
.data
, 0, sizeof(bmi_regs
));
79 memset(config_regs
.data
, 0, sizeof(config_regs
.data
));
81 // setup initial values
82 // enable both channels
83 config_regs
.idetim0
= htole((uint16_t)IDETIM_DECODE_EN
);
84 config_regs
.idetim1
= htole((uint16_t)IDETIM_DECODE_EN
);
85 bmi_regs
.bmis0
= DMA1CAP
| DMA0CAP
;
86 bmi_regs
.bmis1
= DMA1CAP
| DMA0CAP
;
88 // reset all internal variables
91 memset(cmd_in_progress
, 0, sizeof(cmd_in_progress
));
95 // create the PIO and DMA interfaces
96 if (params()->pio_bus
) {
97 pioInterface
= newPioInterface(name() + ".pio", params()->hier
,
98 params()->pio_bus
, this,
99 &IdeController::cacheAccess
);
100 pioLatency
= params()->pio_latency
* params()->pio_bus
->clockRate
;
103 if (params()->dma_bus
) {
104 dmaInterface
= new DMAInterface
<Bus
>(name() + ".dma",
106 params()->dma_bus
, 1, true);
109 // setup the disks attached to controller
110 memset(disks
, 0, sizeof(disks
));
114 if (params()->disks
.size() > 3)
115 panic("IDE controllers support a maximum of 4 devices attached!\n");
117 for (int i
= 0; i
< params()->disks
.size(); i
++) {
118 disks
[i
] = params()->disks
[i
];
119 disks
[i
]->setController(this, dmaInterface
);
123 IdeController::~IdeController()
125 for (int i
= 0; i
< 4; i
++)
135 IdeController::parseAddr(const Addr
&addr
, Addr
&offset
, IdeChannel
&channel
,
136 IdeRegType
®_type
)
140 if (addr
>= pri_cmd_addr
&& addr
< (pri_cmd_addr
+ pri_cmd_size
)) {
141 offset
-= pri_cmd_addr
;
142 reg_type
= COMMAND_BLOCK
;
144 } else if (addr
>= pri_ctrl_addr
&&
145 addr
< (pri_ctrl_addr
+ pri_ctrl_size
)) {
146 offset
-= pri_ctrl_addr
;
147 reg_type
= CONTROL_BLOCK
;
149 } else if (addr
>= sec_cmd_addr
&&
150 addr
< (sec_cmd_addr
+ sec_cmd_size
)) {
151 offset
-= sec_cmd_addr
;
152 reg_type
= COMMAND_BLOCK
;
154 } else if (addr
>= sec_ctrl_addr
&&
155 addr
< (sec_ctrl_addr
+ sec_ctrl_size
)) {
156 offset
-= sec_ctrl_addr
;
157 reg_type
= CONTROL_BLOCK
;
159 } else if (addr
>= bmi_addr
&& addr
< (bmi_addr
+ bmi_size
)) {
161 reg_type
= BMI_BLOCK
;
162 channel
= (offset
< BMIC1
) ? PRIMARY
: SECONDARY
;
164 panic("IDE controller access to invalid address: %#x\n", addr
);
169 IdeController::getDisk(IdeChannel channel
)
172 uint8_t *devBit
= &dev
[0];
174 if (channel
== SECONDARY
) {
181 assert(*devBit
== 0 || *devBit
== 1);
187 IdeController::getDisk(IdeDisk
*diskPtr
)
189 for (int i
= 0; i
< 4; i
++) {
190 if ((long)diskPtr
== (long)disks
[i
])
197 IdeController::isDiskSelected(IdeDisk
*diskPtr
)
199 for (int i
= 0; i
< 4; i
++) {
200 if ((long)diskPtr
== (long)disks
[i
]) {
201 // is disk is on primary or secondary channel
203 // is disk the master or slave
206 return (dev
[channel
] == devID
);
209 panic("Unable to find disk by pointer!!\n");
213 // Command completion
217 IdeController::setDmaComplete(IdeDisk
*disk
)
219 int diskNum
= getDisk(disk
);
222 panic("Unable to find disk based on pointer %#x\n", disk
);
225 // clear the start/stop bit in the command register
226 bmi_regs
.bmic0
&= ~SSBM
;
227 // clear the bus master active bit in the status register
228 bmi_regs
.bmis0
&= ~BMIDEA
;
229 // set the interrupt bit
230 bmi_regs
.bmis0
|= IDEINTS
;
232 // clear the start/stop bit in the command register
233 bmi_regs
.bmic1
&= ~SSBM
;
234 // clear the bus master active bit in the status register
235 bmi_regs
.bmis1
&= ~BMIDEA
;
236 // set the interrupt bit
237 bmi_regs
.bmis1
|= IDEINTS
;
242 // Bus timing and bus access functions
246 IdeController::cacheAccess(MemReqPtr
&req
)
248 // @todo Add more accurate timing to cache access
249 return curTick
+ pioLatency
;
253 // Read and write handling
257 IdeController::readConfig(int offset
, int size
, uint8_t *data
)
261 if (offset
< PCI_DEVICE_SPECIFIC
) {
262 PciDev::readConfig(offset
, size
, data
);
263 } else if (offset
>= IDE_CTRL_CONF_START
&&
264 (offset
+ size
) <= IDE_CTRL_CONF_END
) {
266 config_offset
= offset
- IDE_CTRL_CONF_START
;
269 case sizeof(uint8_t):
270 *data
= config_regs
.data
[config_offset
];
272 case sizeof(uint16_t):
273 *(uint16_t*)data
= *(uint16_t*)&config_regs
.data
[config_offset
];
275 case sizeof(uint32_t):
276 *(uint32_t*)data
= *(uint32_t*)&config_regs
.data
[config_offset
];
279 panic("Invalid PCI configuration read size!\n");
282 DPRINTF(IdeCtrl
, "PCI read offset: %#x size: %#x data: %#x\n",
283 offset
, size
, *(uint32_t*)data
);
286 panic("Read of unimplemented PCI config. register: %x\n", offset
);
291 IdeController::writeConfig(int offset
, int size
, const uint8_t *data
)
295 if (offset
< PCI_DEVICE_SPECIFIC
) {
296 PciDev::writeConfig(offset
, size
, data
);
297 } else if (offset
>= IDE_CTRL_CONF_START
&&
298 (offset
+ size
) <= IDE_CTRL_CONF_END
) {
300 config_offset
= offset
- IDE_CTRL_CONF_START
;
303 case sizeof(uint8_t):
304 config_regs
.data
[config_offset
] = *data
;
306 case sizeof(uint16_t):
307 *(uint16_t*)&config_regs
.data
[config_offset
] = *(uint16_t*)data
;
309 case sizeof(uint32_t):
310 *(uint32_t*)&config_regs
.data
[config_offset
] = *(uint32_t*)data
;
313 panic("Invalid PCI configuration write size!\n");
316 panic("Write of unimplemented PCI config. register: %x\n", offset
);
319 DPRINTF(IdeCtrl
, "PCI write offset: %#x size: %#x data: %#x\n",
322 // Catch the writes to specific PCI registers that have side affects
323 // (like updating the PIO ranges)
326 if (letoh(config
.command
) & PCI_CMD_IOSE
)
331 if (letoh(config
.command
) & PCI_CMD_BME
)
337 case PCI0_BASE_ADDR0
:
338 if (BARAddrs
[0] != 0) {
339 pri_cmd_addr
= BARAddrs
[0];
341 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
,
344 pri_cmd_addr
&= EV5::PAddrUncachedMask
;
348 case PCI0_BASE_ADDR1
:
349 if (BARAddrs
[1] != 0) {
350 pri_ctrl_addr
= BARAddrs
[1];
352 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
,
355 pri_ctrl_addr
&= EV5::PAddrUncachedMask
;
359 case PCI0_BASE_ADDR2
:
360 if (BARAddrs
[2] != 0) {
361 sec_cmd_addr
= BARAddrs
[2];
363 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
,
366 sec_cmd_addr
&= EV5::PAddrUncachedMask
;
370 case PCI0_BASE_ADDR3
:
371 if (BARAddrs
[3] != 0) {
372 sec_ctrl_addr
= BARAddrs
[3];
374 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
,
377 sec_ctrl_addr
&= EV5::PAddrUncachedMask
;
381 case PCI0_BASE_ADDR4
:
382 if (BARAddrs
[4] != 0) {
383 bmi_addr
= BARAddrs
[4];
385 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
387 bmi_addr
&= EV5::PAddrUncachedMask
;
394 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
401 parseAddr(req
->paddr
, offset
, channel
, reg_type
);
409 case sizeof(uint8_t):
410 *data
= bmi_regs
.data
[offset
];
412 case sizeof(uint16_t):
413 *(uint16_t*)data
= *(uint16_t*)&bmi_regs
.data
[offset
];
415 case sizeof(uint32_t):
416 *(uint32_t*)data
= *(uint32_t*)&bmi_regs
.data
[offset
];
419 panic("IDE read of BMI reg invalid size: %#x\n", req
->size
);
425 disk
= getDisk(channel
);
427 if (disks
[disk
] == NULL
)
433 case sizeof(uint16_t):
434 disks
[disk
]->read(offset
, reg_type
, data
);
437 case sizeof(uint32_t):
438 disks
[disk
]->read(offset
, reg_type
, data
);
439 disks
[disk
]->read(offset
, reg_type
, &data
[2]);
443 panic("IDE read of data reg invalid size: %#x\n", req
->size
);
447 if (req
->size
== sizeof(uint8_t)) {
448 disks
[disk
]->read(offset
, reg_type
, data
);
450 panic("IDE read of command reg of invalid size: %#x\n", req
->size
);
454 panic("IDE controller read of unknown register block type!\n");
457 DPRINTF(IdeCtrl
, "read from offset: %#x size: %#x data: %#x\n",
458 offset
, req
->size
, *(uint32_t*)data
);
464 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
470 uint8_t oldVal
, newVal
;
472 parseAddr(req
->paddr
, offset
, channel
, reg_type
);
483 // Bus master IDE command register
486 if (req
->size
!= sizeof(uint8_t))
487 panic("Invalid BMIC write size: %x\n", req
->size
);
489 // select the current disk based on DEV bit
490 disk
= getDisk(channel
);
492 oldVal
= bmi_regs
.chan
[channel
].bmic
;
495 // if a DMA transfer is in progress, R/W control cannot change
497 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
498 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
502 // see if the start/stop bit is being changed
503 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
505 // stopping DMA transfer
506 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
508 // clear the BMIDEA bit
509 bmi_regs
.chan
[channel
].bmis
=
510 bmi_regs
.chan
[channel
].bmis
& ~BMIDEA
;
512 if (disks
[disk
] == NULL
)
513 panic("DMA stop for disk %d which does not exist\n",
516 // inform the disk of the DMA transfer abort
517 disks
[disk
]->abortDma();
519 // starting DMA transfer
520 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
522 // set the BMIDEA bit
523 bmi_regs
.chan
[channel
].bmis
=
524 bmi_regs
.chan
[channel
].bmis
| BMIDEA
;
526 if (disks
[disk
] == NULL
)
527 panic("DMA start for disk %d which does not exist\n",
530 // inform the disk of the DMA transfer start
531 disks
[disk
]->startDma(letoh(bmi_regs
.chan
[channel
].bmidtp
));
535 // update the register value
536 bmi_regs
.chan
[channel
].bmic
= newVal
;
539 // Bus master IDE status register
542 if (req
->size
!= sizeof(uint8_t))
543 panic("Invalid BMIS write size: %x\n", req
->size
);
545 oldVal
= bmi_regs
.chan
[channel
].bmis
;
548 // the BMIDEA bit is RO
549 newVal
|= (oldVal
& BMIDEA
);
551 // to reset (set 0) IDEINTS and IDEDMAE, write 1 to each
552 if ((oldVal
& IDEINTS
) && (newVal
& IDEINTS
))
553 newVal
&= ~IDEINTS
; // clear the interrupt?
555 (oldVal
& IDEINTS
) ? newVal
|= IDEINTS
: newVal
&= ~IDEINTS
;
557 if ((oldVal
& IDEDMAE
) && (newVal
& IDEDMAE
))
560 (oldVal
& IDEDMAE
) ? newVal
|= IDEDMAE
: newVal
&= ~IDEDMAE
;
562 bmi_regs
.chan
[channel
].bmis
= newVal
;
565 // Bus master IDE descriptor table pointer register
569 if (req
->size
!= sizeof(uint32_t))
570 panic("Invalid BMIDTP write size: %x\n", req
->size
);
572 uint32_t host_data
= letoh(*(uint32_t*)data
);
574 bmi_regs
.chan
[channel
].bmidtp
= htole(host_data
);
579 if (req
->size
!= sizeof(uint8_t) &&
580 req
->size
!= sizeof(uint16_t) &&
581 req
->size
!= sizeof(uint32_t))
582 panic("IDE controller write of invalid write size: %x\n",
585 // do a default copy of data into the registers
586 memcpy(&bmi_regs
.data
[offset
], data
, req
->size
);
590 if (offset
== IDE_SELECT_OFFSET
) {
591 uint8_t *devBit
= &dev
[channel
];
592 *devBit
= (letoh(*data
) & IDE_SELECT_DEV_BIT
) ? 1 : 0;
596 disk
= getDisk(channel
);
598 if (disks
[disk
] == NULL
)
604 case sizeof(uint16_t):
605 disks
[disk
]->write(offset
, reg_type
, data
);
608 case sizeof(uint32_t):
609 disks
[disk
]->write(offset
, reg_type
, data
);
610 disks
[disk
]->write(offset
, reg_type
, &data
[2]);
613 panic("IDE write of data reg invalid size: %#x\n", req
->size
);
617 if (req
->size
== sizeof(uint8_t)) {
618 disks
[disk
]->write(offset
, reg_type
, data
);
620 panic("IDE write of command reg of invalid size: %#x\n", req
->size
);
624 panic("IDE controller write of unknown register block type!\n");
627 DPRINTF(IdeCtrl
, "write to offset: %#x size: %#x data: %#x\n",
628 offset
, req
->size
, *(uint32_t*)data
);
638 IdeController::serialize(std::ostream
&os
)
640 // Serialize the PciDev base class
641 PciDev::serialize(os
);
643 // Serialize register addresses and sizes
644 SERIALIZE_SCALAR(pri_cmd_addr
);
645 SERIALIZE_SCALAR(pri_cmd_size
);
646 SERIALIZE_SCALAR(pri_ctrl_addr
);
647 SERIALIZE_SCALAR(pri_ctrl_size
);
648 SERIALIZE_SCALAR(sec_cmd_addr
);
649 SERIALIZE_SCALAR(sec_cmd_size
);
650 SERIALIZE_SCALAR(sec_ctrl_addr
);
651 SERIALIZE_SCALAR(sec_ctrl_size
);
652 SERIALIZE_SCALAR(bmi_addr
);
653 SERIALIZE_SCALAR(bmi_size
);
655 // Serialize registers
656 SERIALIZE_ARRAY(bmi_regs
.data
,
657 sizeof(bmi_regs
.data
) / sizeof(bmi_regs
.data
[0]));
658 SERIALIZE_ARRAY(dev
, sizeof(dev
) / sizeof(dev
[0]));
659 SERIALIZE_ARRAY(config_regs
.data
,
660 sizeof(config_regs
.data
) / sizeof(config_regs
.data
[0]));
662 // Serialize internal state
663 SERIALIZE_SCALAR(io_enabled
);
664 SERIALIZE_SCALAR(bm_enabled
);
665 SERIALIZE_ARRAY(cmd_in_progress
,
666 sizeof(cmd_in_progress
) / sizeof(cmd_in_progress
[0]));
670 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
672 // Unserialize the PciDev base class
673 PciDev::unserialize(cp
, section
);
675 // Unserialize register addresses and sizes
676 UNSERIALIZE_SCALAR(pri_cmd_addr
);
677 UNSERIALIZE_SCALAR(pri_cmd_size
);
678 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
679 UNSERIALIZE_SCALAR(pri_ctrl_size
);
680 UNSERIALIZE_SCALAR(sec_cmd_addr
);
681 UNSERIALIZE_SCALAR(sec_cmd_size
);
682 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
683 UNSERIALIZE_SCALAR(sec_ctrl_size
);
684 UNSERIALIZE_SCALAR(bmi_addr
);
685 UNSERIALIZE_SCALAR(bmi_size
);
687 // Unserialize registers
688 UNSERIALIZE_ARRAY(bmi_regs
.data
,
689 sizeof(bmi_regs
.data
) / sizeof(bmi_regs
.data
[0]));
690 UNSERIALIZE_ARRAY(dev
, sizeof(dev
) / sizeof(dev
[0]));
691 UNSERIALIZE_ARRAY(config_regs
.data
,
692 sizeof(config_regs
.data
) / sizeof(config_regs
.data
[0]));
694 // Unserialize internal state
695 UNSERIALIZE_SCALAR(io_enabled
);
696 UNSERIALIZE_SCALAR(bm_enabled
);
697 UNSERIALIZE_ARRAY(cmd_in_progress
,
698 sizeof(cmd_in_progress
) / sizeof(cmd_in_progress
[0]));
701 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
, pri_cmd_size
));
702 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
, pri_ctrl_size
));
703 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
, sec_cmd_size
));
704 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
, sec_ctrl_size
));
705 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
709 #ifndef DOXYGEN_SHOULD_SKIP_THIS
711 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
714 SimObjectVectorParam
<IdeDisk
*> disks
;
715 SimObjectParam
<MemoryController
*> mmu
;
716 SimObjectParam
<PciConfigAll
*> configspace
;
717 SimObjectParam
<PciConfigData
*> configdata
;
718 SimObjectParam
<Platform
*> platform
;
719 Param
<uint32_t> pci_bus
;
720 Param
<uint32_t> pci_dev
;
721 Param
<uint32_t> pci_func
;
722 SimObjectParam
<Bus
*> pio_bus
;
723 SimObjectParam
<Bus
*> dma_bus
;
724 Param
<Tick
> pio_latency
;
725 SimObjectParam
<HierParams
*> hier
;
727 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
729 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
731 INIT_PARAM(addr
, "Device Address"),
732 INIT_PARAM(disks
, "IDE disks attached to this controller"),
733 INIT_PARAM(mmu
, "Memory controller"),
734 INIT_PARAM(configspace
, "PCI Configspace"),
735 INIT_PARAM(configdata
, "PCI Config data"),
736 INIT_PARAM(platform
, "Platform pointer"),
737 INIT_PARAM(pci_bus
, "PCI bus ID"),
738 INIT_PARAM(pci_dev
, "PCI device number"),
739 INIT_PARAM(pci_func
, "PCI function code"),
740 INIT_PARAM(pio_bus
, ""),
741 INIT_PARAM(dma_bus
, ""),
742 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
743 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
745 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
747 CREATE_SIM_OBJECT(IdeController
)
749 IdeController::Params
*params
= new IdeController::Params
;
750 params
->name
= getInstanceName();
752 params
->configSpace
= configspace
;
753 params
->configData
= configdata
;
754 params
->plat
= platform
;
755 params
->busNum
= pci_bus
;
756 params
->deviceNum
= pci_dev
;
757 params
->functionNum
= pci_func
;
759 params
->disks
= disks
;
760 params
->pio_bus
= pio_bus
;
761 params
->dma_bus
= dma_bus
;
762 params
->pio_latency
= pio_latency
;
764 return new IdeController(params
);
767 REGISTER_SIM_OBJECT("IdeController", IdeController
)
769 #endif //DOXYGEN_SHOULD_SKIP_THIS