1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Ali Saidi
29 */
30
31/** @file
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.
34 */
35
36#include "dev/sparc/mm_disk.hh"
37
38#include <cstring>
39
40#include "base/trace.hh"
41#include "debug/IdeDisk.hh"
42#include "dev/platform.hh"
43#include "mem/packet_access.hh"
44#include "mem/port.hh"
45#include "sim/byteswap.hh"
46#include "sim/system.hh"
47
48MmDisk::MmDisk(const Params *p)
49    : BasicPioDevice(p, p->image->size() * SectorSize),
50      image(p->image), curSector((off_t)-1), dirty(false)
51{
52    std::memset(&diskData, 0, SectorSize);
53}
54
55Tick
56MmDisk::read(PacketPtr pkt)
57{
58    Addr accessAddr;
59    off_t sector;
60    uint16_t d16;
61    uint32_t d32;
62    uint64_t d64;
63
64    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
65    accessAddr = pkt->getAddr() - pioAddr;
66
67    sector = accessAddr / SectorSize;
68
69    if (sector != curSector) {
70        if (dirty) {
71#ifndef NDEBUG
72            off_t bytes_written =
73#endif
74                image->write(diskData, curSector);
75            assert(bytes_written == SectorSize);
76        }
77#ifndef NDEBUG
78        off_t bytes_read =
79#endif
80            image->read(diskData, sector);
81        assert(bytes_read == SectorSize);
82        curSector = sector;
83    }
84    switch (pkt->getSize()) {
85      case sizeof(uint8_t):
86        pkt->setRaw(diskData[accessAddr % SectorSize]);
87        DPRINTF(IdeDisk, "reading byte %#x value= %#x\n",
88                accessAddr, diskData[accessAddr % SectorSize]);
89        break;
90      case sizeof(uint16_t):
91        memcpy(&d16, diskData + (accessAddr % SectorSize), 2);
92        pkt->setRaw(d16);
93        DPRINTF(IdeDisk, "reading word %#x value= %#x\n", accessAddr, d16);
94        break;
95      case sizeof(uint32_t):
96        memcpy(&d32, diskData + (accessAddr % SectorSize), 4);
97        pkt->setRaw(d32);
98        DPRINTF(IdeDisk, "reading dword %#x value= %#x\n", accessAddr, d32);
99        break;
100      case sizeof(uint64_t):
101        memcpy(&d64, diskData + (accessAddr % SectorSize), 8);
102        pkt->setRaw(d64);
103        DPRINTF(IdeDisk, "reading qword %#x value= %#x\n", accessAddr, d64);
104        break;
105      default:
106        panic("Invalid access size\n");
107    }
108
109    pkt->makeAtomicResponse();
110    return pioDelay;
111}
112
113Tick
114MmDisk::write(PacketPtr pkt)
115{
116    Addr accessAddr;
117    off_t sector;
118    uint16_t d16;
119    uint32_t d32;
120    uint64_t d64;
121
122    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
123    accessAddr = pkt->getAddr() - pioAddr;
124
125    sector = accessAddr / SectorSize;
126
127    if (sector != curSector) {
128        if (dirty) {
129#ifndef NDEBUG
130            off_t bytes_written =
131#endif
132                image->write(diskData, curSector);
133            assert(bytes_written == SectorSize);
134        }
135#ifndef NDEBUG
136        off_t bytes_read =
137#endif
138            image->read(diskData,  sector);
139        assert(bytes_read == SectorSize);
140        curSector = sector;
141    }
142    dirty = true;
143
144    switch (pkt->getSize()) {
145      case sizeof(uint8_t):
146        diskData[accessAddr % SectorSize] = htobe(pkt->getRaw<uint8_t>());
147        DPRINTF(IdeDisk, "writing byte %#x value= %#x\n",
148                accessAddr, diskData[accessAddr % SectorSize]);
149        break;
150      case sizeof(uint16_t):
151        d16 = pkt->getRaw<uint16_t>();
152        memcpy(diskData + (accessAddr % SectorSize), &d16, 2);
153        DPRINTF(IdeDisk, "writing word %#x value= %#x\n", accessAddr, d16);
154        break;
155      case sizeof(uint32_t):
156        d32 = pkt->getRaw<uint32_t>();
157        memcpy(diskData + (accessAddr % SectorSize), &d32, 4);
158        DPRINTF(IdeDisk, "writing dword %#x value= %#x\n", accessAddr, d32);
159        break;
160      case sizeof(uint64_t):
161        d64 = pkt->getRaw<uint64_t>();
162        memcpy(diskData + (accessAddr % SectorSize), &d64, 8);
163        DPRINTF(IdeDisk, "writing qword %#x value= %#x\n", accessAddr, d64);
164        break;
165      default:
166        panic("Invalid access size\n");
167    }
168
169    pkt->makeAtomicResponse();
170    return pioDelay;
171}
172
173void
174MmDisk::serialize(CheckpointOut &cp) const
175{
176    // just write any dirty changes to the cow layer it will take care of
177    // serialization
178    if (dirty) {
179#ifndef NDEBUG
180        int bytes_read =
181#endif
182            image->write(diskData, curSector);
183        assert(bytes_read == SectorSize);
184    }
185    ClockedObject::serialize(cp);
186}
187
188MmDisk *
189MmDiskParams::create()
190{
191    return new MmDisk(this);
192}
193