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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "device_table.h"
42 shm - map unix shared memory into psim address space
48 This device implements an area of memory which is mapped into UNIX
55 reg = <address> <size> (required)
57 Determine where the memory lives in the parents address space.
58 The SHM area is assumed to be of the same length.
60 key = <integer> (required)
62 This is the key of the unix shared memory area.
67 Enable tracing of the shm:
69 | bash$ psim -t shm-device \
72 Configure a 512 kilobytes of UNIX shared memory with the key 0x12345678
73 mapped into psim address space at 0x0c000000.
75 | -o '/shm@0x0c000000/reg 0x0c000000 0x80000' \
76 | -o '/shm@0x0c000000/key 0x12345678' \
78 sim/ppc/run -o '/#address-cells 1' \
79 -o '/shm@0x0c000000/reg 0x0c000000 0x80000' \
80 -o '/shm@0x0c000000/key 0x12345678' ../psim-hello/hello
88 typedef struct _hw_shm_device
{
89 unsigned_word physical_address
;
91 unsigned sizeof_memory
;
97 hw_shm_init_data(device
*me
)
99 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
100 const device_unit
*d
;
101 reg_property_spec reg
;
104 /* Obtain the Key Value */
105 if (device_find_property(me
, "key") == NULL
)
106 error("shm_init_data() required key property is missing\n");
108 shm
->key
= (key_t
) device_find_integer_property(me
, "key");
109 DTRACE(shm
, ("shm key (0x%08x)\n", shm
->key
) );
111 /* Figure out where this memory is in address space and how long it is */
112 if ( !device_find_reg_array_property(me
, "reg", 0, ®
) )
113 error("hw_shm_init_data() no address registered\n");
115 /* Determine the address and length being as paranoid as possible */
116 shm
->physical_address
= 0xffffffff;
117 shm
->sizeof_memory
= 0xffffffff;
119 for ( i
=0 ; i
<reg
.address
.nr_cells
; i
++ ) {
120 if (reg
.address
.cells
[0] == 0 && reg
.size
.cells
[0] == 0)
123 if ( shm
->physical_address
!= 0xffffffff )
124 device_error(me
, "Only single celled address ranges supported\n");
126 shm
->physical_address
= reg
.address
.cells
[i
];
127 DTRACE(shm
, ("shm physical_address=0x%x\n", shm
->physical_address
));
129 shm
->sizeof_memory
= reg
.size
.cells
[i
];
130 DTRACE(shm
, ("shm length=0x%x\n", shm
->sizeof_memory
));
133 if ( shm
->physical_address
== 0xffffffff )
134 device_error(me
, "Address not specified\n" );
136 if ( shm
->sizeof_memory
== 0xffffffff )
137 device_error(me
, "Length not specified\n" );
139 /* Now actually attach to or create the shared memory area */
140 shm
->id
= shmget(shm
->key
, shm
->sizeof_memory
, IPC_CREAT
| 0660);
142 error("hw_shm_init_data() shmget failed\n");
144 shm
->shm_address
= shmat(shm
->id
, (char *)0, SHM_RND
);
145 if (shm
->shm_address
== (void *)-1)
146 error("hw_shm_init_data() shmat failed\n");
150 hw_shm_attach_address_callback(device
*me
,
156 device
*client
) /*callback/default*/
158 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
161 error("shm_attach_address_callback() invalid address space\n");
164 error("shm_attach_address_callback() invalid size\n");
169 hw_shm_io_read_buffer(device
*me
,
177 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
179 /* do we need to worry about out of range addresses? */
181 DTRACE(shm
, ("read %p %x %x %x\n", \
182 shm
->shm_address
, shm
->physical_address
, addr
, nr_bytes
) );
184 memcpy(dest
, &shm
->shm_address
[addr
- shm
->physical_address
], nr_bytes
);
190 hw_shm_io_write_buffer(device
*me
,
198 hw_shm_device
*shm
= (hw_shm_device
*)device_data(me
);
200 /* do we need to worry about out of range addresses? */
202 DTRACE(shm
, ("write %p %x %x %x\n", \
203 shm
->shm_address
, shm
->physical_address
, addr
, nr_bytes
) );
205 memcpy(&shm
->shm_address
[addr
- shm
->physical_address
], source
, nr_bytes
);
209 static device_callbacks
const hw_shm_callbacks
= {
210 { generic_device_init_address
, hw_shm_init_data
},
211 { hw_shm_attach_address_callback
, }, /* address */
212 { hw_shm_io_read_buffer
,
213 hw_shm_io_write_buffer
}, /* IO */
215 { NULL
, }, /* interrupt */
216 { NULL
, }, /* unit */
221 hw_shm_create(const char *name
,
222 const device_unit
*unit_address
,
225 hw_shm_device
*shm
= ZALLOC(hw_shm_device
);
231 const device_descriptor hw_shm_device_descriptor
[] = {
232 { "shm", hw_shm_create
, &hw_shm_callbacks
},
236 #endif /* _HW_SHM_C_ */