2 * Copyright (c) 2006 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.
32 * This device acts as a disk similar to the memory mapped disk device
33 * in legion. Any access is translated to an offset in the disk image.
38 #include "base/trace.hh"
39 #include "dev/sparc/mm_disk.hh"
40 #include "dev/platform.hh"
41 #include "mem/port.hh"
42 #include "mem/packet_access.hh"
43 #include "sim/builder.hh"
44 #include "sim/byteswap.hh"
45 #include "sim/system.hh"
47 MmDisk::MmDisk(Params
*p
)
48 : BasicPioDevice(p
), image(p
->image
), curSector((off_t
)-1), dirty(false)
50 std::memset(&diskData
, 0, SectorSize
);
51 pioSize
= image
->size() * SectorSize
;
55 MmDisk::read(PacketPtr pkt
)
64 assert(pkt
->result
== Packet::Unknown
);
65 assert(pkt
->getAddr() >= pioAddr
&& pkt
->getAddr() < pioAddr
+ pioSize
);
66 accessAddr
= pkt
->getAddr() - pioAddr
;
68 sector
= accessAddr
/ SectorSize
;
70 if (sector
!= curSector
) {
72 bytes_read
= image
->write(diskData
, curSector
);
73 assert(bytes_read
== SectorSize
);
75 bytes_read
= image
->read(diskData
, sector
);
76 assert(bytes_read
== SectorSize
);
79 switch (pkt
->getSize()) {
81 pkt
->set(diskData
[accessAddr
% SectorSize
]);
82 DPRINTF(IdeDisk
, "reading byte %#x value= %#x\n", accessAddr
, diskData
[accessAddr
%
85 case sizeof(uint16_t):
86 memcpy(&d16
, diskData
+ (accessAddr
% SectorSize
), 2);
88 DPRINTF(IdeDisk
, "reading word %#x value= %#x\n", accessAddr
, d16
);
90 case sizeof(uint32_t):
91 memcpy(&d32
, diskData
+ (accessAddr
% SectorSize
), 4);
93 DPRINTF(IdeDisk
, "reading dword %#x value= %#x\n", accessAddr
, d32
);
95 case sizeof(uint64_t):
96 memcpy(&d64
, diskData
+ (accessAddr
% SectorSize
), 8);
98 DPRINTF(IdeDisk
, "reading qword %#x value= %#x\n", accessAddr
, d64
);
101 panic("Invalid access size\n");
104 pkt
->result
= Packet::Success
;
109 MmDisk::write(PacketPtr pkt
)
118 assert(pkt
->result
== Packet::Unknown
);
119 assert(pkt
->getAddr() >= pioAddr
&& pkt
->getAddr() < pioAddr
+ pioSize
);
120 accessAddr
= pkt
->getAddr() - pioAddr
;
122 sector
= accessAddr
/ SectorSize
;
124 if (sector
!= curSector
) {
126 bytes_read
= image
->write(diskData
, curSector
);
127 assert(bytes_read
== SectorSize
);
129 bytes_read
= image
->read(diskData
, sector
);
130 assert(bytes_read
== SectorSize
);
135 switch (pkt
->getSize()) {
136 case sizeof(uint8_t):
137 diskData
[accessAddr
% SectorSize
] = htobe(pkt
->get
<uint8_t>());
138 DPRINTF(IdeDisk
, "writing byte %#x value= %#x\n", accessAddr
, diskData
[accessAddr
%
141 case sizeof(uint16_t):
142 d16
= htobe(pkt
->get
<uint16_t>());
143 memcpy(diskData
+ (accessAddr
% SectorSize
), &d16
, 2);
144 DPRINTF(IdeDisk
, "writing word %#x value= %#x\n", accessAddr
, d16
);
146 case sizeof(uint32_t):
147 d32
= htobe(pkt
->get
<uint32_t>());
148 memcpy(diskData
+ (accessAddr
% SectorSize
), &d32
, 4);
149 DPRINTF(IdeDisk
, "writing dword %#x value= %#x\n", accessAddr
, d32
);
151 case sizeof(uint64_t):
152 d64
= htobe(pkt
->get
<uint64_t>());
153 memcpy(diskData
+ (accessAddr
% SectorSize
), &d64
, 8);
154 DPRINTF(IdeDisk
, "writing qword %#x value= %#x\n", accessAddr
, d64
);
157 panic("Invalid access size\n");
160 pkt
->result
= Packet::Success
;
165 MmDisk::serialize(std::ostream
&os
)
167 // just write any dirty changes to the cow layer it will take care of
171 bytes_read
= image
->write(diskData
, curSector
);
172 assert(bytes_read
== SectorSize
);
179 BEGIN_DECLARE_SIM_OBJECT_PARAMS(MmDisk
)
180 Param
<Addr
> pio_addr
;
181 Param
<Tick
> pio_latency
;
182 Param
<Addr
> pio_size
;
183 SimObjectParam
<Platform
*> platform
;
184 SimObjectParam
<System
*> system
;
185 SimObjectParam
<DiskImage
*> image
;
186 END_DECLARE_SIM_OBJECT_PARAMS(MmDisk
)
188 BEGIN_INIT_SIM_OBJECT_PARAMS(MmDisk
)
190 INIT_PARAM(pio_addr
, "Device Address"),
191 INIT_PARAM(pio_latency
, "Programmed IO latency"),
192 INIT_PARAM(pio_size
, "Size of address range"),
193 INIT_PARAM(platform
, "platform"),
194 INIT_PARAM(system
, "system object"),
195 INIT_PARAM(image
, "disk image")
197 END_INIT_SIM_OBJECT_PARAMS(MmDisk
)
199 CREATE_SIM_OBJECT(MmDisk
)
201 MmDisk::Params
*p
= new MmDisk::Params
;
202 p
->name
= getInstanceName();
203 p
->pio_addr
= pio_addr
;
204 p
->pio_delay
= pio_latency
;
205 p
->platform
= platform
;
208 return new MmDisk(p
);
211 REGISTER_SIM_OBJECT("MmDisk", MmDisk
)