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/ide_ctrl.hh"
38 #include "dev/ide_disk.hh"
39 #include "dev/pciconfigall.hh"
40 #include "dev/pcireg.h"
41 #include "dev/platform.hh"
42 #include "mem/bus/bus.hh"
43 #include "mem/bus/dma_interface.hh"
44 #include "mem/bus/pio_interface.hh"
45 #include "mem/bus/pio_interface_impl.hh"
46 #include "mem/functional_mem/memory_control.hh"
47 #include "mem/functional_mem/physical_memory.hh"
48 #include "sim/builder.hh"
49 #include "sim/sim_object.hh"
54 // Initialization and destruction
57 IdeController::IdeController(Params
*p
)
60 // initialize the PIO interface addresses
62 pri_cmd_size
= BARSize
[0];
65 pri_ctrl_size
= BARSize
[1];
68 sec_cmd_size
= BARSize
[2];
71 sec_ctrl_size
= BARSize
[3];
73 // initialize the bus master interface (BMI) address to be configured
76 bmi_size
= BARSize
[4];
78 // zero out all of the registers
79 memset(bmi_regs
, 0, sizeof(bmi_regs
));
80 memset(pci_regs
, 0, sizeof(pci_regs
));
82 // setup initial values
83 *(uint32_t *)&pci_regs
[IDETIM
] = 0x80008000; // enable both channels
84 *(uint8_t *)&bmi_regs
[BMIS0
] = 0x60;
85 *(uint8_t *)&bmi_regs
[BMIS1
] = 0x60;
87 // reset all internal variables
90 memset(cmd_in_progress
, 0, sizeof(cmd_in_progress
));
92 // create the PIO and DMA interfaces
93 if (params()->host_bus
) {
94 pioInterface
= newPioInterface(name(), params()->hier
,
95 params()->host_bus
, this,
96 &IdeController::cacheAccess
);
98 dmaInterface
= new DMAInterface
<Bus
>(name() + ".dma",
100 params()->host_bus
, 1,
102 pioLatency
= params()->pio_latency
* params()->host_bus
->clockRatio
;
105 // setup the disks attached to controller
106 memset(disks
, 0, sizeof(IdeDisk
*) * 4);
108 if (params()->disks
.size() > 3)
109 panic("IDE controllers support a maximum of 4 devices attached!\n");
111 for (int i
= 0; i
< params()->disks
.size(); i
++) {
112 disks
[i
] = params()->disks
[i
];
113 disks
[i
]->setController(this, dmaInterface
);
117 IdeController::~IdeController()
119 for (int i
= 0; i
< 4; i
++)
129 IdeController::parseAddr(const Addr
&addr
, Addr
&offset
, bool &primary
,
134 if (addr
>= pri_cmd_addr
&& addr
< (pri_cmd_addr
+ pri_cmd_size
)) {
135 offset
-= pri_cmd_addr
;
136 type
= COMMAND_BLOCK
;
138 } else if (addr
>= pri_ctrl_addr
&&
139 addr
< (pri_ctrl_addr
+ pri_ctrl_size
)) {
140 offset
-= pri_ctrl_addr
;
141 type
= CONTROL_BLOCK
;
143 } else if (addr
>= sec_cmd_addr
&&
144 addr
< (sec_cmd_addr
+ sec_cmd_size
)) {
145 offset
-= sec_cmd_addr
;
146 type
= COMMAND_BLOCK
;
148 } else if (addr
>= sec_ctrl_addr
&&
149 addr
< (sec_ctrl_addr
+ sec_ctrl_size
)) {
150 offset
-= sec_ctrl_addr
;
151 type
= CONTROL_BLOCK
;
153 } else if (addr
>= bmi_addr
&& addr
< (bmi_addr
+ bmi_size
)) {
156 primary
= (offset
< BMIC1
) ? true : false;
158 panic("IDE controller access to invalid address: %#x\n", addr
);
163 IdeController::getDisk(bool primary
)
166 uint8_t *devBit
= &dev
[0];
175 assert(*devBit
== 0 || *devBit
== 1);
181 IdeController::getDisk(IdeDisk
*diskPtr
)
183 for (int i
= 0; i
< 4; i
++) {
184 if ((long)diskPtr
== (long)disks
[i
])
191 IdeController::isDiskSelected(IdeDisk
*diskPtr
)
193 for (int i
= 0; i
< 4; i
++) {
194 if ((long)diskPtr
== (long)disks
[i
]) {
195 // is disk is on primary or secondary channel
197 // is disk the master or slave
200 return (dev
[channel
] == devID
);
203 panic("Unable to find disk by pointer!!\n");
207 // Command completion
211 IdeController::setDmaComplete(IdeDisk
*disk
)
213 int diskNum
= getDisk(disk
);
216 panic("Unable to find disk based on pointer %#x\n", disk
);
219 // clear the start/stop bit in the command register
220 bmi_regs
[BMIC0
] &= ~SSBM
;
221 // clear the bus master active bit in the status register
222 bmi_regs
[BMIS0
] &= ~BMIDEA
;
223 // set the interrupt bit
224 bmi_regs
[BMIS0
] |= IDEINTS
;
226 // clear the start/stop bit in the command register
227 bmi_regs
[BMIC1
] &= ~SSBM
;
228 // clear the bus master active bit in the status register
229 bmi_regs
[BMIS1
] &= ~BMIDEA
;
230 // set the interrupt bit
231 bmi_regs
[BMIS1
] |= IDEINTS
;
236 // Bus timing and bus access functions
240 IdeController::cacheAccess(MemReqPtr
&req
)
242 // @todo Add more accurate timing to cache access
243 return curTick
+ pioLatency
;
247 // Read and write handling
251 IdeController::ReadConfig(int offset
, int size
, uint8_t *data
)
255 Addr origOffset
= offset
;
258 if (offset
< PCI_DEVICE_SPECIFIC
) {
259 PciDev::ReadConfig(offset
, size
, data
);
261 if (offset
>= PCI_IDE_TIMING
&& offset
< (PCI_IDE_TIMING
+ 4)) {
262 offset
-= PCI_IDE_TIMING
;
265 if ((offset
+ size
) > (IDETIM
+ 4))
266 panic("PCI read of IDETIM with invalid size\n");
267 } else if (offset
== PCI_SLAVE_TIMING
) {
268 offset
-= PCI_SLAVE_TIMING
;
271 if ((offset
+ size
) > (SIDETIM
+ 1))
272 panic("PCI read of SIDETIM with invalid size\n");
273 } else if (offset
== PCI_UDMA33_CTRL
) {
274 offset
-= PCI_UDMA33_CTRL
;
277 if ((offset
+ size
) > (UDMACTL
+ 1))
278 panic("PCI read of UDMACTL with invalid size\n");
279 } else if (offset
>= PCI_UDMA33_TIMING
&&
280 offset
< (PCI_UDMA33_TIMING
+ 2)) {
281 offset
-= PCI_UDMA33_TIMING
;
284 if ((offset
+ size
) > (UDMATIM
+ 2))
285 panic("PCI read of UDMATIM with invalid size\n");
287 panic("PCI read of unimplemented register: %x\n", offset
);
290 memcpy((void *)data
, (void *)&pci_regs
[offset
], size
);
293 DPRINTF(IdeCtrl
, "PCI read offset: %#x (%#x) size: %#x data: %#x\n",
294 origOffset
, offset
, size
,
295 (*(uint32_t *)data
) & (0xffffffff >> 8 * (4 - size
)));
299 IdeController::WriteConfig(int offset
, int size
, uint32_t data
)
301 DPRINTF(IdeCtrl
, "PCI write offset: %#x size: %#x data: %#x\n",
302 offset
, size
, data
& (0xffffffff >> 8 * (4 - size
)));
304 // do standard write stuff if in standard PCI space
305 if (offset
< PCI_DEVICE_SPECIFIC
) {
306 PciDev::WriteConfig(offset
, size
, data
);
308 if (offset
>= PCI_IDE_TIMING
&& offset
< (PCI_IDE_TIMING
+ 4)) {
309 offset
-= PCI_IDE_TIMING
;
312 if ((offset
+ size
) > (IDETIM
+ 4))
313 panic("PCI write to IDETIM with invalid size\n");
314 } else if (offset
== PCI_SLAVE_TIMING
) {
315 offset
-= PCI_SLAVE_TIMING
;
318 if ((offset
+ size
) > (SIDETIM
+ 1))
319 panic("PCI write to SIDETIM with invalid size\n");
320 } else if (offset
== PCI_UDMA33_CTRL
) {
321 offset
-= PCI_UDMA33_CTRL
;
324 if ((offset
+ size
) > (UDMACTL
+ 1))
325 panic("PCI write to UDMACTL with invalid size\n");
326 } else if (offset
>= PCI_UDMA33_TIMING
&&
327 offset
< (PCI_UDMA33_TIMING
+ 2)) {
328 offset
-= PCI_UDMA33_TIMING
;
331 if ((offset
+ size
) > (UDMATIM
+ 2))
332 panic("PCI write to UDMATIM with invalid size\n");
334 panic("PCI write to unimplemented register: %x\n", offset
);
337 memcpy((void *)&pci_regs
[offset
], (void *)&data
, size
);
340 // Catch the writes to specific PCI registers that have side affects
341 // (like updating the PIO ranges)
344 if (config
.data
[offset
] & PCI_CMD_IOSE
)
349 if (config
.data
[offset
] & PCI_CMD_BME
)
355 case PCI0_BASE_ADDR0
:
356 if (BARAddrs
[0] != 0) {
357 pri_cmd_addr
= BARAddrs
[0];
359 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
,
362 pri_cmd_addr
&= EV5::PAddrUncachedMask
;
366 case PCI0_BASE_ADDR1
:
367 if (BARAddrs
[1] != 0) {
368 pri_ctrl_addr
= BARAddrs
[1];
370 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
,
373 pri_ctrl_addr
&= EV5::PAddrUncachedMask
;
377 case PCI0_BASE_ADDR2
:
378 if (BARAddrs
[2] != 0) {
379 sec_cmd_addr
= BARAddrs
[2];
381 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
,
384 sec_cmd_addr
&= EV5::PAddrUncachedMask
;
388 case PCI0_BASE_ADDR3
:
389 if (BARAddrs
[3] != 0) {
390 sec_ctrl_addr
= BARAddrs
[3];
392 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
,
395 sec_ctrl_addr
&= EV5::PAddrUncachedMask
;
399 case PCI0_BASE_ADDR4
:
400 if (BARAddrs
[4] != 0) {
401 bmi_addr
= BARAddrs
[4];
403 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
405 bmi_addr
&= EV5::PAddrUncachedMask
;
412 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
421 parseAddr(req
->paddr
, offset
, primary
, type
);
422 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
423 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
428 // sanity check the size (allows byte, word, or dword access)
429 if (req
->size
!= sizeof(uint8_t) && req
->size
!= sizeof(uint16_t) &&
430 req
->size
!= sizeof(uint32_t))
431 panic("IDE controller read of invalid size: %#x\n", req
->size
);
433 if (type
!= BMI_BLOCK
) {
434 assert(req
->size
!= sizeof(uint32_t));
436 disk
= getDisk(primary
);
438 disks
[disk
]->read(offset
, byte
, cmdBlk
, data
);
440 memcpy((void *)data
, &bmi_regs
[offset
], req
->size
);
443 DPRINTF(IdeCtrl
, "read from offset: %#x size: %#x data: %#x\n",
445 (*(uint32_t *)data
) & (0xffffffff >> 8 * (4 - req
->size
)));
451 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
460 parseAddr(req
->paddr
, offset
, primary
, type
);
461 byte
= (req
->size
== sizeof(uint8_t)) ? true : false;
462 cmdBlk
= (type
== COMMAND_BLOCK
) ? true : false;
464 DPRINTF(IdeCtrl
, "write from offset: %#x size: %#x data: %#x\n",
466 (*(uint32_t *)data
) & (0xffffffff >> 8 * (4 - req
->size
)));
468 uint8_t oldVal
, newVal
;
473 if (type
== BMI_BLOCK
&& !bm_enabled
)
476 if (type
!= BMI_BLOCK
) {
477 // shadow the dev bit
478 if (type
== COMMAND_BLOCK
&& offset
== IDE_SELECT_OFFSET
) {
479 uint8_t *devBit
= (primary
? &dev
[0] : &dev
[1]);
480 *devBit
= ((*data
& IDE_SELECT_DEV_BIT
) ? 1 : 0);
483 assert(req
->size
!= sizeof(uint32_t));
485 disk
= getDisk(primary
);
487 disks
[disk
]->write(offset
, byte
, cmdBlk
, data
);
490 // Bus master IDE command register
493 if (req
->size
!= sizeof(uint8_t))
494 panic("Invalid BMIC write size: %x\n", req
->size
);
496 // select the current disk based on DEV bit
497 disk
= getDisk(primary
);
499 oldVal
= bmi_regs
[offset
];
502 // if a DMA transfer is in progress, R/W control cannot change
504 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
505 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
509 // see if the start/stop bit is being changed
510 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
512 // stopping DMA transfer
513 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
515 // clear the BMIDEA bit
516 bmi_regs
[offset
+ 0x2] &= ~BMIDEA
;
518 if (disks
[disk
] == NULL
)
519 panic("DMA stop for disk %d which does not exist\n",
522 // inform the disk of the DMA transfer abort
523 disks
[disk
]->abortDma();
525 // starting DMA transfer
526 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
528 // set the BMIDEA bit
529 bmi_regs
[offset
+ 0x2] |= BMIDEA
;
531 if (disks
[disk
] == NULL
)
532 panic("DMA start for disk %d which does not exist\n",
535 // inform the disk of the DMA transfer start
537 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP0
]);
539 disks
[disk
]->startDma(*(uint32_t *)&bmi_regs
[BMIDTP1
]);
543 // update the register value
544 bmi_regs
[offset
] = newVal
;
547 // Bus master IDE status register
550 if (req
->size
!= sizeof(uint8_t))
551 panic("Invalid BMIS write size: %x\n", req
->size
);
553 oldVal
= bmi_regs
[offset
];
556 // the BMIDEA bit is RO
557 newVal
|= (oldVal
& BMIDEA
);
559 // to reset (set 0) IDEINTS and IDEDMAE, write 1 to each
560 if ((oldVal
& IDEINTS
) && (newVal
& IDEINTS
))
561 newVal
&= ~IDEINTS
; // clear the interrupt?
563 (oldVal
& IDEINTS
) ? newVal
|= IDEINTS
: newVal
&= ~IDEINTS
;
565 if ((oldVal
& IDEDMAE
) && (newVal
& IDEDMAE
))
568 (oldVal
& IDEDMAE
) ? newVal
|= IDEDMAE
: newVal
&= ~IDEDMAE
;
570 bmi_regs
[offset
] = newVal
;
573 // Bus master IDE descriptor table pointer register
576 if (req
->size
!= sizeof(uint32_t))
577 panic("Invalid BMIDTP write size: %x\n", req
->size
);
579 *(uint32_t *)&bmi_regs
[offset
] = *(uint32_t *)data
& ~0x3;
583 if (req
->size
!= sizeof(uint8_t) &&
584 req
->size
!= sizeof(uint16_t) &&
585 req
->size
!= sizeof(uint32_t))
586 panic("IDE controller write of invalid write size: %x\n",
589 // do a default copy of data into the registers
590 memcpy((void *)&bmi_regs
[offset
], data
, req
->size
);
602 IdeController::serialize(std::ostream
&os
)
604 // Serialize the PciDev base class
605 PciDev::serialize(os
);
607 // Serialize register addresses and sizes
608 SERIALIZE_SCALAR(pri_cmd_addr
);
609 SERIALIZE_SCALAR(pri_cmd_size
);
610 SERIALIZE_SCALAR(pri_ctrl_addr
);
611 SERIALIZE_SCALAR(pri_ctrl_size
);
612 SERIALIZE_SCALAR(sec_cmd_addr
);
613 SERIALIZE_SCALAR(sec_cmd_size
);
614 SERIALIZE_SCALAR(sec_ctrl_addr
);
615 SERIALIZE_SCALAR(sec_ctrl_size
);
616 SERIALIZE_SCALAR(bmi_addr
);
617 SERIALIZE_SCALAR(bmi_size
);
619 // Serialize registers
620 SERIALIZE_ARRAY(bmi_regs
, 16);
621 SERIALIZE_ARRAY(dev
, 2);
622 SERIALIZE_ARRAY(pci_regs
, 8);
624 // Serialize internal state
625 SERIALIZE_SCALAR(io_enabled
);
626 SERIALIZE_SCALAR(bm_enabled
);
627 SERIALIZE_ARRAY(cmd_in_progress
, 4);
631 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
633 // Unserialize the PciDev base class
634 PciDev::unserialize(cp
, section
);
636 // Unserialize register addresses and sizes
637 UNSERIALIZE_SCALAR(pri_cmd_addr
);
638 UNSERIALIZE_SCALAR(pri_cmd_size
);
639 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
640 UNSERIALIZE_SCALAR(pri_ctrl_size
);
641 UNSERIALIZE_SCALAR(sec_cmd_addr
);
642 UNSERIALIZE_SCALAR(sec_cmd_size
);
643 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
644 UNSERIALIZE_SCALAR(sec_ctrl_size
);
645 UNSERIALIZE_SCALAR(bmi_addr
);
646 UNSERIALIZE_SCALAR(bmi_size
);
648 // Unserialize registers
649 UNSERIALIZE_ARRAY(bmi_regs
, 16);
650 UNSERIALIZE_ARRAY(dev
, 2);
651 UNSERIALIZE_ARRAY(pci_regs
, 8);
653 // Unserialize internal state
654 UNSERIALIZE_SCALAR(io_enabled
);
655 UNSERIALIZE_SCALAR(bm_enabled
);
656 UNSERIALIZE_ARRAY(cmd_in_progress
, 4);
659 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
, pri_cmd_size
));
660 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
, pri_ctrl_size
));
661 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
, sec_cmd_size
));
662 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
, sec_ctrl_size
));
663 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
667 #ifndef DOXYGEN_SHOULD_SKIP_THIS
669 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
672 SimObjectVectorParam
<IdeDisk
*> disks
;
673 SimObjectParam
<MemoryController
*> mmu
;
674 SimObjectParam
<PciConfigAll
*> configspace
;
675 SimObjectParam
<PciConfigData
*> configdata
;
676 SimObjectParam
<Platform
*> platform
;
677 Param
<uint32_t> pci_bus
;
678 Param
<uint32_t> pci_dev
;
679 Param
<uint32_t> pci_func
;
680 SimObjectParam
<Bus
*> io_bus
;
681 Param
<Tick
> pio_latency
;
682 SimObjectParam
<HierParams
*> hier
;
684 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
686 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
688 INIT_PARAM(addr
, "Device Address"),
689 INIT_PARAM(disks
, "IDE disks attached to this controller"),
690 INIT_PARAM(mmu
, "Memory controller"),
691 INIT_PARAM(configspace
, "PCI Configspace"),
692 INIT_PARAM(configdata
, "PCI Config data"),
693 INIT_PARAM(platform
, "Platform pointer"),
694 INIT_PARAM(pci_bus
, "PCI bus ID"),
695 INIT_PARAM(pci_dev
, "PCI device number"),
696 INIT_PARAM(pci_func
, "PCI function code"),
697 INIT_PARAM_DFLT(io_bus
, "Host bus to attach to", NULL
),
698 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
699 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
701 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
703 CREATE_SIM_OBJECT(IdeController
)
705 IdeController::Params
*params
= new IdeController::Params
;
706 params
->name
= getInstanceName();
708 params
->configSpace
= configspace
;
709 params
->configData
= configdata
;
710 params
->plat
= platform
;
711 params
->busNum
= pci_bus
;
712 params
->deviceNum
= pci_dev
;
713 params
->functionNum
= pci_func
;
715 params
->disks
= disks
;
716 params
->host_bus
= io_bus
;
717 params
->pio_latency
= pio_latency
;
719 return new IdeController(params
);
722 REGISTER_SIM_OBJECT("IdeController", IdeController
)
724 #endif //DOXYGEN_SHOULD_SKIP_THIS