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"
43 sem - provide access to a unix semaphore
49 This device implements an interface to a unix semaphore.
55 reg = <address> <size> (required)
57 Determine where the memory lives in the parents address space.
59 key = <integer> (required)
61 This is the key of the unix semaphore.
66 Enable tracing of the sem:
68 | bash$ psim -t sem-device \
71 Configure a UNIX semaphore using key 0x12345678 mapped into psim
72 address space at 0xfff00000:
74 | -o '/sem@0xfff00000/reg 0xfff00000 0x80000' \
75 | -o '/sem@0xfff00000/key 0x12345678' \
77 sim/ppc/run -o '/#address-cells 1' \
78 -o '/sem@0xfff00000/reg 0xfff00000 12' \
79 -o '/sem@0xfff00000/key 0x12345678' ../psim-hello/hello
84 offset 4 - lock operation
85 offset 8 - unlock operation
87 All reads return the current or resulting count.
95 typedef struct _hw_sem_device {
96 unsigned_word physical_address;
103 #ifndef HAVE_UNION_SEMUN
106 struct semid_ds *buf;
107 unsigned short int *array;
108 #if defined(__linux__)
109 struct seminfo *__buf;
115 hw_sem_init_data(device *me)
117 hw_sem_device *sem = (hw_sem_device*)device_data(me);
118 const device_unit *d;
122 /* initialize the properties of the sem */
124 if (device_find_property(me, "key") == NULL)
125 error("sem_init_data() required key property is missing\n");
127 if (device_find_property(me, "value") == NULL)
128 error("sem_init_data() required value property is missing\n");
130 sem->key = (key_t) device_find_integer_property(me, "key");
131 DTRACE(sem, ("semaphore key (%d)\n", sem->key) );
133 sem->initial = (int) device_find_integer_property(me, "value");
134 DTRACE(sem, ("semaphore initial value (%d)\n", sem->initial) );
136 d = device_unit_address(me);
137 sem->physical_address = d->cells[ d->nr_cells-1 ];
138 DTRACE(sem, ("semaphore physical_address=0x%x\n", sem->physical_address));
140 /* Now to initialize the semaphore */
142 if ( sem->initial != -1 ) {
144 sem->id = semget(sem->key, 1, IPC_CREAT | 0660);
146 error("hw_sem_init_data() semget failed\n");
148 help.val = sem->initial;
149 status = semctl( sem->id, 0, SETVAL, help );
151 error("hw_sem_init_data() semctl -- set value failed\n");
154 sem->id = semget(sem->key, 1, 0660);
156 error("hw_sem_init_data() semget failed\n");
159 sem->count = semctl( sem->id, 0, GETVAL, help );
160 if (sem->count == -1)
161 error("hw_sem_init_data() semctl -- get value failed\n");
162 DTRACE(sem, ("semaphore OS value (%d)\n", sem->count) );
166 hw_sem_attach_address_callback(device *me,
172 device *client) /*callback/default*/
174 hw_sem_device *sem = (hw_sem_device*)device_data(me);
177 error("sem_attach_address_callback() invalid address space\n");
180 error("sem_attach_address_callback() invalid size\n");
182 sem->physical_address = addr;
183 DTRACE(sem, ("semaphore physical_address=0x%x\n", addr));
187 hw_sem_io_read_buffer(device *me,
195 hw_sem_device *sem = (hw_sem_device*)device_data(me);
201 /* do we need to worry about out of range addresses? */
203 DTRACE(sem, ("semaphore read addr=0x%x length=%d\n", addr, nr_bytes));
205 if (!(addr >= sem->physical_address && addr <= sem->physical_address + 11))
206 error("hw_sem_io_read_buffer() invalid address - out of range\n");
209 error("hw_sem_io_read_buffer() invalid address - alignment\n");
212 error("hw_sem_io_read_buffer() invalid length\n");
214 switch ( (addr - sem->physical_address) / 4 ) {
216 case 0: /* OBTAIN CURRENT VALUE */
224 status = semop(sem->id, &sb, 1);
226 perror( "hw_sem.c: lock" );
227 error("hw_sem_io_read_buffer() sem lock\n");
230 DTRACE(sem, ("semaphore lock %d\n", sem->count));
238 status = semop(sem->id, &sb, 1);
240 perror( "hw_sem.c: unlock" );
241 error("hw_sem_io_read_buffer() sem unlock\n");
243 DTRACE(sem, ("semaphore unlock %d\n", sem->count));
247 error("hw_sem_io_read_buffer() invalid address - unknown error\n");
251 /* assume target is big endian */
252 u32 = H2T_4(semctl( sem->id, 0, GETVAL, help ));
254 DTRACE(sem, ("semaphore OS value (%d)\n", u32) );
255 if (u32 == 0xffffffff) {
256 perror( "hw_sem.c: getval" );
257 error("hw_sem_io_read_buffer() semctl -- get value failed\n");
260 memcpy(dest, &u32, nr_bytes);
265 static device_callbacks const hw_sem_callbacks = {
266 { generic_device_init_address, hw_sem_init_data },
267 { hw_sem_attach_address_callback, }, /* address */
268 { hw_sem_io_read_buffer, NULL }, /* IO */
270 { NULL, }, /* interrupt */
271 { NULL, }, /* unit */
276 hw_sem_create(const char *name,
277 const device_unit *unit_address,
280 hw_sem_device *sem = ZALLOC(hw_sem_device);
284 const device_descriptor hw_sem_device_descriptor[] = {
285 { "sem", hw_sem_create, &hw_sem_callbacks },
289 #endif /* _HW_SEM_C_ */