6636a5ff61f0d15b2f1cd27976c8538204994852
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
)
257 Addr origOffset
= offset
;
260 if (offset
< PCI_DEVICE_SPECIFIC
) {
261 PciDev::ReadConfig(offset
, size
, data
);
262 } else if (offset
>= IDE_CTRL_CONFIG_START
&& (offset
+ size
) <= IDE_CTRL_CONFIG_END
) {
264 config_offset
= offset
- IDE_CTRL_CONFIG_START
;
267 case sizeof(uint32_t):
268 memcpy(data
, &pci_config_regs
.data
[config_offset
], sizeof(uint32_t));
269 *(uint32_t*)data
= htoa(*(uint32_t*)data
);
272 case sizeof(uint16_t):
273 memcpy(data
, &pci_config_regs
.data
[config_offset
], sizeof(uint16_t));
274 *(uint16_t*)data
= htoa(*(uint16_t*)data
);
277 case sizeof(uint8_t):
278 memcpy(data
, &pci_config_regs
.data
[config_offset
], sizeof(uint8_t));
282 panic("Invalid PCI configuration read size!\n");
285 panic("Read of unimplemented PCI config. register: %x\n", offset
);
288 DPRINTF(IdeCtrl
, "PCI read offset: %#x (%#x) size: %#x data: %#x\n",
289 origOffset
, offset
, size
,
290 *(uint32_t *)data
& (0xffffffff >> 8 * (4 - size
)));
294 IdeController::WriteConfig(int offset
, int size
, uint32_t data
)
298 if (offset
< PCI_DEVICE_SPECIFIC
) {
299 PciDev::WriteConfig(offset
, size
, data
);
300 } else if (offset
>= IDE_CTRL_CONFIG_START
&& (offset
+ size
) <= IDE_CTRL_CONFIG_END
) {
302 config_offset
= offset
- IDE_CTRL_CONFIG_START
;
305 case sizeof(uint32_t):
306 case sizeof(uint16_t):
307 case sizeof(uint8_t):
308 memcpy(&pci_config_regs
.data
[config_offset
], &data
, size
);
312 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",
320 offset
, size
, data
& (0xffffffff >> 8 * (4 - size
)));
323 // Catch the writes to specific PCI registers that have side affects
324 // (like updating the PIO ranges)
327 if (config
.data
[offset
] & PCI_CMD_IOSE
)
332 if (config
.data
[offset
] & PCI_CMD_BME
)
338 case PCI0_BASE_ADDR0
:
339 if (BARAddrs
[0] != 0) {
340 pri_cmd_addr
= BARAddrs
[0];
342 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
,
345 pri_cmd_addr
&= EV5::PAddrUncachedMask
;
349 case PCI0_BASE_ADDR1
:
350 if (BARAddrs
[1] != 0) {
351 pri_ctrl_addr
= BARAddrs
[1];
353 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
,
356 pri_ctrl_addr
&= EV5::PAddrUncachedMask
;
360 case PCI0_BASE_ADDR2
:
361 if (BARAddrs
[2] != 0) {
362 sec_cmd_addr
= BARAddrs
[2];
364 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
,
367 sec_cmd_addr
&= EV5::PAddrUncachedMask
;
371 case PCI0_BASE_ADDR3
:
372 if (BARAddrs
[3] != 0) {
373 sec_ctrl_addr
= BARAddrs
[3];
375 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
,
378 sec_ctrl_addr
&= EV5::PAddrUncachedMask
;
382 case PCI0_BASE_ADDR4
:
383 if (BARAddrs
[4] != 0) {
384 bmi_addr
= BARAddrs
[4];
386 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
388 bmi_addr
&= EV5::PAddrUncachedMask
;
395 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
402 parseAddr(req
->paddr
, offset
, primary
, type
);
407 // sanity check the size (allows byte, word, or dword access)
409 case sizeof(uint8_t):
410 case sizeof(uint16_t):
411 case sizeof(uint32_t):
414 panic("IDE controller read of invalid size: %#x\n", req
->size
);
417 if (type
!= BMI_BLOCK
) {
419 disk
= getDisk(primary
);
421 if (req
->size
== sizeof(uint32_t) && offset
== DATA_OFFSET
) {
422 ((uint16_t*)data
)[0] = disks
[disk
]->read(offset
, type
);
423 ((uint16_t*)data
)[1] = disks
[disk
]->read(offset
, type
);
425 else if (req
->size
== sizeof(uint8_t) && offset
== DATA_OFFSET
) {
426 panic("IDE read of data reg invalid size: %#x\n", req
->size
);
429 *data
= disks
[disk
]->read(offset
, type
);
432 memcpy((void *)data
, &bmi_regs
[offset
], req
->size
);
435 DPRINTF(IdeCtrl
, "read from offset: %#x size: %#x data: %#x\n",
437 (*(uint32_t *)data
) & (0xffffffff >> 8 * (4 - req
->size
)));
443 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
452 parseAddr(req
->paddr
, offset
, primary
, type
);
453 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
454 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
456 DPRINTF(IdeCtrl
, "write from offset: %#x size: %#x data: %#x\n",
458 (*(uint32_t *)data
) & (0xffffffff >> 8 * (4 - req
->size
)));
460 uint8_t oldVal
, newVal
;
465 if (type
== BMI_BLOCK
&& !bm_enabled
)
468 if (type
!= BMI_BLOCK
) {
469 // shadow the dev bit
470 if (type
== COMMAND_BLOCK
&& offset
== IDE_SELECT_OFFSET
) {
471 uint8_t *devBit
= (primary
? &dev
[0] : &dev
[1]);
472 *devBit
= ((*data
& IDE_SELECT_DEV_BIT
) ? 1 : 0);
475 assert(req
->size
!= sizeof(uint32_t));
477 disk
= getDisk(primary
);
479 disks
[disk
]->write(offset
, byte
, cmdBlk
, data
);
482 // Bus master IDE command register
485 if (req
->size
!= sizeof(uint8_t))
486 panic("Invalid BMIC write size: %x\n", req
->size
);
488 // select the current disk based on DEV bit
489 disk
= getDisk(primary
);
491 oldVal
= bmi_regs
[offset
];
494 // if a DMA transfer is in progress, R/W control cannot change
496 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
497 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
501 // see if the start/stop bit is being changed
502 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
504 // stopping DMA transfer
505 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
507 // clear the BMIDEA bit
508 bmi_regs
[offset
+ 0x2] &= ~BMIDEA
;
510 if (disks
[disk
] == NULL
)
511 panic("DMA stop for disk %d which does not exist\n",
514 // inform the disk of the DMA transfer abort
515 disks
[disk
]->abortDma();
517 // starting DMA transfer
518 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
520 // set the BMIDEA bit
521 bmi_regs
[offset
+ 0x2] |= BMIDEA
;
523 if (disks
[disk
] == NULL
)
524 panic("DMA start for disk %d which does not exist\n",
527 // inform the disk of the DMA transfer start
529 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP0
]);
531 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP1
]);
535 // update the register value
536 bmi_regs
[offset
] = 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
[offset
];
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
[offset
] = newVal
;
565 // Bus master IDE descriptor table pointer register
568 if (req
->size
!= sizeof(uint32_t))
569 panic("Invalid BMIDTP write size: %x\n", req
->size
);
571 *(uint32_t *)&bmi_regs
[offset
] = *(uint32_t *)data
& ~0x3;
575 if (req
->size
!= sizeof(uint8_t) &&
576 req
->size
!= sizeof(uint16_t) &&
577 req
->size
!= sizeof(uint32_t))
578 panic("IDE controller write of invalid write size: %x\n",
581 // do a default copy of data into the registers
582 memcpy((void *)&bmi_regs
[offset
], data
, req
->size
);
594 IdeController::serialize(std::ostream
&os
)
596 // Serialize the PciDev base class
597 PciDev::serialize(os
);
599 // Serialize register addresses and sizes
600 SERIALIZE_SCALAR(pri_cmd_addr
);
601 SERIALIZE_SCALAR(pri_cmd_size
);
602 SERIALIZE_SCALAR(pri_ctrl_addr
);
603 SERIALIZE_SCALAR(pri_ctrl_size
);
604 SERIALIZE_SCALAR(sec_cmd_addr
);
605 SERIALIZE_SCALAR(sec_cmd_size
);
606 SERIALIZE_SCALAR(sec_ctrl_addr
);
607 SERIALIZE_SCALAR(sec_ctrl_size
);
608 SERIALIZE_SCALAR(bmi_addr
);
609 SERIALIZE_SCALAR(bmi_size
);
611 // Serialize registers
612 SERIALIZE_ARRAY(bmi_regs
, 16);
613 SERIALIZE_ARRAY(dev
, 2);
614 SERIALIZE_ARRAY(pci_config_regs
.data
, 22);
616 // Serialize internal state
617 SERIALIZE_SCALAR(io_enabled
);
618 SERIALIZE_SCALAR(bm_enabled
);
619 SERIALIZE_ARRAY(cmd_in_progress
, 4);
623 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
625 // Unserialize the PciDev base class
626 PciDev::unserialize(cp
, section
);
628 // Unserialize register addresses and sizes
629 UNSERIALIZE_SCALAR(pri_cmd_addr
);
630 UNSERIALIZE_SCALAR(pri_cmd_size
);
631 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
632 UNSERIALIZE_SCALAR(pri_ctrl_size
);
633 UNSERIALIZE_SCALAR(sec_cmd_addr
);
634 UNSERIALIZE_SCALAR(sec_cmd_size
);
635 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
636 UNSERIALIZE_SCALAR(sec_ctrl_size
);
637 UNSERIALIZE_SCALAR(bmi_addr
);
638 UNSERIALIZE_SCALAR(bmi_size
);
640 // Unserialize registers
641 UNSERIALIZE_ARRAY(bmi_regs
, 16);
642 UNSERIALIZE_ARRAY(dev
, 2);
643 UNSERIALIZE_ARRAY(pci_config_regs
.data
, 22);
645 // Unserialize internal state
646 UNSERIALIZE_SCALAR(io_enabled
);
647 UNSERIALIZE_SCALAR(bm_enabled
);
648 UNSERIALIZE_ARRAY(cmd_in_progress
, 4);
651 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
, pri_cmd_size
));
652 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
, pri_ctrl_size
));
653 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
, sec_cmd_size
));
654 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
, sec_ctrl_size
));
655 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
659 #ifndef DOXYGEN_SHOULD_SKIP_THIS
661 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
664 SimObjectVectorParam
<IdeDisk
*> disks
;
665 SimObjectParam
<MemoryController
*> mmu
;
666 SimObjectParam
<PciConfigAll
*> configspace
;
667 SimObjectParam
<PciConfigData
*> configdata
;
668 SimObjectParam
<Platform
*> platform
;
669 Param
<uint32_t> pci_bus
;
670 Param
<uint32_t> pci_dev
;
671 Param
<uint32_t> pci_func
;
672 SimObjectParam
<Bus
*> io_bus
;
673 Param
<Tick
> pio_latency
;
674 SimObjectParam
<HierParams
*> hier
;
676 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
678 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
680 INIT_PARAM(addr
, "Device Address"),
681 INIT_PARAM(disks
, "IDE disks attached to this controller"),
682 INIT_PARAM(mmu
, "Memory controller"),
683 INIT_PARAM(configspace
, "PCI Configspace"),
684 INIT_PARAM(configdata
, "PCI Config data"),
685 INIT_PARAM(platform
, "Platform pointer"),
686 INIT_PARAM(pci_bus
, "PCI bus ID"),
687 INIT_PARAM(pci_dev
, "PCI device number"),
688 INIT_PARAM(pci_func
, "PCI function code"),
689 INIT_PARAM_DFLT(io_bus
, "Host bus to attach to", NULL
),
690 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
691 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
693 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
695 CREATE_SIM_OBJECT(IdeController
)
697 IdeController::Params
*params
= new IdeController::Params
;
698 params
->name
= getInstanceName();
700 params
->configSpace
= configspace
;
701 params
->configData
= configdata
;
702 params
->plat
= platform
;
703 params
->busNum
= pci_bus
;
704 params
->deviceNum
= pci_dev
;
705 params
->functionNum
= pci_func
;
707 params
->disks
= disks
;
708 params
->host_bus
= io_bus
;
709 params
->pio_latency
= pio_latency
;
711 return new IdeController(params
);
714 REGISTER_SIM_OBJECT("IdeController", IdeController
)
716 #endif //DOXYGEN_SHOULD_SKIP_THIS