Merge zizzer:/bk/newmem
[gem5.git] / src / dev / sparc / mm_disk.cc
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 <cstring>
37
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"
46
47 MmDisk::MmDisk(Params *p)
48 : BasicPioDevice(p), image(p->image), curSector((uint64_t)-1), dirty(false)
49 {
50 std::memset(&bytes, 0, SectorSize);
51 pioSize = image->size() * SectorSize;
52 }
53
54 Tick
55 MmDisk::read(PacketPtr pkt)
56 {
57 Addr accessAddr;
58 off_t sector;
59 off_t bytes_read;
60 uint16_t *d16;
61 uint32_t *d32;
62 uint64_t *d64;
63
64 assert(pkt->result == Packet::Unknown);
65 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
66 accessAddr = pkt->getAddr() - pioAddr;
67
68 sector = accessAddr / SectorSize;
69
70 if (sector != curSector) {
71 if (dirty)
72 bytes_read = image->write(bytes, curSector);
73 bytes_read = image->read(bytes, sector);
74 curSector = sector;
75 }
76 switch (pkt->getSize()) {
77 case sizeof(uint8_t):
78 pkt->set(bytes[accessAddr % SectorSize]);
79 break;
80 case sizeof(uint16_t):
81 d16 = (uint16_t*)bytes + (accessAddr % SectorSize)/2;
82 pkt->set(htobe(*d16));
83 break;
84 case sizeof(uint32_t):
85 d32 = (uint32_t*)bytes + (accessAddr % SectorSize)/4;
86 pkt->set(htobe(*d32));
87 break;
88 case sizeof(uint64_t):
89 d64 = (uint64_t*)bytes + (accessAddr % SectorSize)/8;
90 pkt->set(htobe(*d64));
91 break;
92 default:
93 panic("Invalid access size\n");
94 }
95
96 pkt->result = Packet::Success;
97 return pioDelay;
98 }
99
100 Tick
101 MmDisk::write(PacketPtr pkt)
102 {
103 panic("need to implement\n");
104 M5_DUMMY_RETURN
105 }
106
107
108 BEGIN_DECLARE_SIM_OBJECT_PARAMS(MmDisk)
109 Param<Addr> pio_addr;
110 Param<Tick> pio_latency;
111 Param<Addr> pio_size;
112 SimObjectParam<Platform *> platform;
113 SimObjectParam<System *> system;
114 SimObjectParam<DiskImage *> image;
115 END_DECLARE_SIM_OBJECT_PARAMS(MmDisk)
116
117 BEGIN_INIT_SIM_OBJECT_PARAMS(MmDisk)
118
119 INIT_PARAM(pio_addr, "Device Address"),
120 INIT_PARAM(pio_latency, "Programmed IO latency"),
121 INIT_PARAM(pio_size, "Size of address range"),
122 INIT_PARAM(platform, "platform"),
123 INIT_PARAM(system, "system object"),
124 INIT_PARAM(image, "disk image")
125
126 END_INIT_SIM_OBJECT_PARAMS(MmDisk)
127
128 CREATE_SIM_OBJECT(MmDisk)
129 {
130 MmDisk::Params *p = new MmDisk::Params;
131 p->name = getInstanceName();
132 p->pio_addr = pio_addr;
133 p->pio_delay = pio_latency;
134 p->platform = platform;
135 p->system = system;
136 p->image = image;
137 return new MmDisk(p);
138 }
139
140 REGISTER_SIM_OBJECT("MmDisk", MmDisk)