summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libcpu/hppa1.1/semaphore/semaphore.c
blob: aa8b768b3ad14503426c703f22f7d6c7dae25c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *  File:       $RCSfile$
 *  Project:    PixelFlow
 *  Created:    94/11/29
 *  RespEngr:   tony bennett
 *  Revision:   $Revision$
 *  Last Mod:   $Date$
 *
 *  COPYRIGHT (c) 1994 by Division Incorporated
 *
 *  To anyone who acknowledges that this file is provided "AS IS"
 *  without any express or implied warranty:
 *      permission to use, copy, modify, and distribute this file
 *      for any purpose is hereby granted without fee, provided that
 *      the above copyright notice and this notice appears in all
 *      copies, and that the name of Division Incorporated not be
 *      used in advertising or publicity pertaining to distribution
 *      of the software without specific, written prior permission.
 *      Division Incorporated makes no representations about the
 *      suitability of this software for any purpose.
 *
 *  Description:
 *      HPPA fast spinlock semaphores based on LDCWX instruction.
 *      These semaphores are not known to RTEMS.
 *
 *  TODO:
 *      Put node number in high 16 bits of flag??
 *      XXX: Need h_s_deallocate
 *
 *  $Id$
 */

#include <bsp.h>

#include "semaphore.h"

/*
 * Report fatal semaphore error
 */

#define SEM_FATAL_ERROR(sp)  rtems_fatal_error_occurred((rtems_unsigned32) sp)

#define SEM_CHECK(sp)   do { \
                              if (((sp) == 0) || (int) (sp) & 0xf) \
                              { \
                                  SEM_FATAL_ERROR(sp); \
                              } \
                         } while (0)

/*
 * Init a semaphore to be free
 */

#define SEM_FREE_INIT(sp) \
    do { \
        (sp)->lock = 1; \
        (sp)->flags = 0; \
        (sp)->owner_tcb  = 0; \
    } while (0)

/*
 * Grab a semaphore recording its owner.
 */

#define SEM_MARK_GRABBED(sp) \
    do { \
        (sp)->owner_tcb  = _Thread_Executing; \
    } while (0)

/*
 * Mark the semaphore busy
 */

#define SEM_MARK_BUSY(sp)    ((sp)->flags |= HPPA_SEM_IN_USE)

/*
 * Is a semaphore available?
 */

#define SEM_IS_AVAILABLE(sp)  ((sp)->owner_tcb == 0)

/*
 * The pool control semaphore is the first in the pool
 */

#define SEM_CONTROL  (&hppa_semaphore_pool[0])
#define SEM_FIRST    (&hppa_semaphore_pool[1])

#define SEM_PRIVATE(cookie)  rtems_interrupt_disable(cookie)

#define SEM_PUBLIC(cookie)   rtems_interrupt_enable(cookie)


/*
 * Control variables for the pool
 */

hppa_semaphore_t *hppa_semaphore_pool;        /* ptr to first */
int               hppa_semaphores;
int               hppa_semaphores_available;

void
hppa_semaphore_pool_initialize(void *pool_base,
                               int   pool_size)
{
    hppa_semaphore_t *sp;
    int align_factor;
    rtems_unsigned32 isr_level;

    /*
     * round pool_base up to be a multiple of SEM_ALIGN
     */

    align_factor = SEM_ALIGN - (((int) pool_base) & (SEM_ALIGN-1));
    if (align_factor != SEM_ALIGN)
    {
        pool_base += align_factor;
        pool_size -= align_factor;
    }

    /*
     * How many can the pool hold?
     * Assumes the semaphores are SEM_ALIGN bytes each
     */

    if (sizeof(hppa_semaphore_t) != SEM_ALIGN)
        rtems_fatal_error_occurred(RTEMS_INVALID_SIZE);

    pool_size &= ~(SEM_ALIGN - 1);

    SEM_PRIVATE(isr_level);

    hppa_semaphore_pool = pool_base;
    hppa_semaphores = pool_size / SEM_ALIGN;

    /*
     * If we are node0, then init all in the pool
     */

    if (cpu_number == 0)
    {
        /*
         * Tell other cpus we are not done, jic
         */
        SEM_CONTROL->user = rtems_build_name('!', 'D', 'N', 'E');

        for (sp=SEM_FIRST; sp < &hppa_semaphore_pool[hppa_semaphores]; sp++)
            SEM_FREE_INIT(sp);
        SEM_FREE_INIT(SEM_CONTROL);
    }

    /*
     * Tell other cpus we are done, or wait for it to be done if on another cpu
     */

    if (cpu_number == 0)
        SEM_CONTROL->user = rtems_build_name('D', 'O', 'N', 'E');
    else
        while (SEM_CONTROL->user != rtems_build_name('D', 'O', 'N', 'E'))
            ;

    hppa_semaphores_available = hppa_semaphores;

    SEM_PUBLIC(isr_level);
}

/*
 *  Function:   hppa_semaphore_acquire
 *  Created:    94/11/29
 *  RespEngr:   tony bennett
 *
 *  Description:
 *      Acquire a semaphore.   Will spin on the semaphore unless
 *      'flag' says not to.
 *
 *  Parameters:
 *
 *
 *  Returns:
 *      0        -- if did not acquire
 *      non-zero -- if acquired semaphore
 *                      (actually this is the spin count)
 *
 *  Notes:
 *      There is no requirement that the semaphore be within the pool
 *
 *  Deficiencies/ToDo:
 *
 */


rtems_unsigned32
hppa_semaphore_acquire(hppa_semaphore_t *sp,
                       int               flag)
{
    rtems_unsigned32 lock_value;
    rtems_unsigned32 spin_count = 1;

    SEM_CHECK(sp);

    for (;;)
    {
        HPPA_ASM_LDCWS(0, 0, sp, lock_value);

        if (lock_value)         /* we now own the lock */
        {
            SEM_MARK_GRABBED(sp);
            return spin_count ? spin_count : ~0;        /* jic */
        }

        if (flag & HPPA_SEM_NO_SPIN)
            return 0;

        spin_count++;
    }
}

void
hppa_semaphore_release(hppa_semaphore_t *sp)
{
    SEM_CHECK(sp);

    if (sp->owner_tcb != _Thread_Executing)
        SEM_FATAL_ERROR("owner mismatch");

    sp->lock = 1;
}


/*
 *  Function:   hppa_semaphore_allocate
 *  Created:    94/11/29
 *  RespEngr:   tony bennett
 *
 *  Description:
 *      Get a pointer to a semaphore.
 *
 *  Parameters:
 *      which -- if 0, then allocate a free semaphore from the pool
 *               if non-zero, then return pointer to that one, even
 *                  if it is already busy.
 *
 *  Returns:
 *      successful -- pointer to semaphore
 *                    NULL otherwise
 *
 *  Notes:
 *
 *
 *  Deficiencies/ToDo:
 *
 *
 */

hppa_semaphore_t *
hppa_semaphore_allocate(rtems_unsigned32 which,
                        int              flag)
{
    hppa_semaphore_t *sp = 0;

    /*
     * grab the control semaphore
     */

    if (hppa_semaphore_acquire(SEM_CONTROL, 0) == 0)
        SEM_FATAL_ERROR("could not grab control semaphore");

    /*
     * Find a free one and init it
     */

    if (which)
    {
        if (which >= hppa_semaphores)
            SEM_FATAL_ERROR("requested non-existent semaphore");
        sp = &hppa_semaphore_pool[which];

        /*
         * if it is "free", then mark it claimed now.
         * If it is not free then we are done.
         */

        if (SEM_IS_AVAILABLE(sp))
            goto allmine;
    }
    else for (sp = SEM_FIRST;
              sp < &hppa_semaphore_pool[hppa_semaphores];
              sp++)
    {
        if (SEM_IS_AVAILABLE(sp))
        {
allmine:    SEM_FREE_INIT(sp);
            SEM_MARK_BUSY(sp);
            if ( ! (flag & HPPA_SEM_INITIALLY_FREE))
                SEM_MARK_GRABBED(sp);
            break;
        }
    }

    /*
     * Free up the control semaphore
     */

    hppa_semaphore_release(SEM_CONTROL);

    return sp;
}