1 /* This file is part of the program psim.
3 Copyright (C) 1997,2008, Joel Sherrill <joel@OARcorp.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "device_table.h"
34 shm - map unix shared memory into psim address space
40 This device implements an area of memory which is mapped into UNIX
47 reg = <address> <size> (required)
49 Determine where the memory lives in the parents address space.
50 The SHM area is assumed to be of the same length.
52 key = <integer> (required)
54 This is the key of the unix shared memory area.
59 Enable tracing of the shm:
61 | bash$ psim -t shm-device \
64 Configure a 512 kilobytes of UNIX shared memory with the key 0x12345678
65 mapped into psim address space at 0x0c000000.
67 | -o '/shm@0x0c000000/reg 0x0c000000 0x80000' \
68 | -o '/shm@0x0c000000/key 0x12345678' \
70 sim/ppc/run -o '/#address-cells 1' \
71 -o '/shm@0x0c000000/reg 0x0c000000 0x80000' \
72 -o '/shm@0x0c000000/key 0x12345678' ../psim-hello/hello
80 typedef struct _hw_shm_device
{
81 unsigned_word physical_address
;
83 unsigned sizeof_memory
;
89 hw_shm_init_data(device
*me
)
91 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
93 reg_property_spec reg
;
96 /* Obtain the Key Value */
97 if (device_find_property(me
, "key") == NULL
)
98 error("shm_init_data() required key property is missing\n");
100 shm
->key
= (key_t
) device_find_integer_property(me
, "key");
101 DTRACE(shm
, ("shm key (0x%08x)\n", shm
->key
) );
103 /* Figure out where this memory is in address space and how long it is */
104 if ( !device_find_reg_array_property(me
, "reg", 0, ®
) )
105 error("hw_shm_init_data() no address registered\n");
107 /* Determine the address and length being as paranoid as possible */
108 shm
->physical_address
= 0xffffffff;
109 shm
->sizeof_memory
= 0xffffffff;
111 for ( i
=0 ; i
<reg
.address
.nr_cells
; i
++ ) {
112 if (reg
.address
.cells
[0] == 0 && reg
.size
.cells
[0] == 0)
115 if ( shm
->physical_address
!= 0xffffffff )
116 device_error(me
, "Only single celled address ranges supported\n");
118 shm
->physical_address
= reg
.address
.cells
[i
];
119 DTRACE(shm
, ("shm physical_address=0x%x\n", shm
->physical_address
));
121 shm
->sizeof_memory
= reg
.size
.cells
[i
];
122 DTRACE(shm
, ("shm length=0x%x\n", shm
->sizeof_memory
));
125 if ( shm
->physical_address
== 0xffffffff )
126 device_error(me
, "Address not specified\n" );
128 if ( shm
->sizeof_memory
== 0xffffffff )
129 device_error(me
, "Length not specified\n" );
131 /* Now actually attach to or create the shared memory area */
132 shm
->id
= shmget(shm
->key
, shm
->sizeof_memory
, IPC_CREAT
| 0660);
134 error("hw_shm_init_data() shmget failed\n");
136 shm
->shm_address
= shmat(shm
->id
, (char *)0, SHM_RND
);
137 if (shm
->shm_address
== (void *)-1)
138 error("hw_shm_init_data() shmat failed\n");
142 hw_shm_attach_address_callback(device
*me
,
148 device
*client
) /*callback/default*/
150 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
153 error("shm_attach_address_callback() invalid address space\n");
156 error("shm_attach_address_callback() invalid size\n");
161 hw_shm_io_read_buffer(device
*me
,
169 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
171 /* do we need to worry about out of range addresses? */
173 DTRACE(shm
, ("read %p %x %x %x\n", \
174 shm
->shm_address
, shm
->physical_address
, addr
, nr_bytes
) );
176 memcpy(dest
, &shm
->shm_address
[addr
- shm
->physical_address
], nr_bytes
);
182 hw_shm_io_write_buffer(device
*me
,
190 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
192 /* do we need to worry about out of range addresses? */
194 DTRACE(shm
, ("write %p %x %x %x\n", \
195 shm
->shm_address
, shm
->physical_address
, addr
, nr_bytes
) );
197 memcpy(&shm
->shm_address
[addr
- shm
->physical_address
], source
, nr_bytes
);
201 static device_callbacks
const hw_shm_callbacks
= {
202 { generic_device_init_address
, hw_shm_init_data
},
203 { hw_shm_attach_address_callback
, }, /* address */
204 { hw_shm_io_read_buffer
,
205 hw_shm_io_write_buffer
}, /* IO */
207 { NULL
, }, /* interrupt */
208 { NULL
, }, /* unit */
213 hw_shm_create(const char *name
,
214 const device_unit
*unit_address
,
217 hw_shm_device
*shm
= ZALLOC(hw_shm_device
);
223 const device_descriptor hw_shm_device_descriptor
[] = {
224 { "shm", hw_shm_create
, &hw_shm_callbacks
},
228 #endif /* _HW_SHM_C_ */