aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lufa/Projects/XPLAINBridge/Lib/SoftUART.c
Commit message (Expand)AuthorAgeFilesLines
* Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'Jack Humbert2017-07-071-0/+156
href='#n35'>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
/******************************************************************************
 * common/softirq.c
 * 
 * Modified from the Linux original. Softirqs in Xen are only executed in
 * an outermost activation (e.g., never within an interrupt activation).
 * This simplifies some things and generally seems a good thing.
 * 
 * Copyright (c) 2003, K A Fraser
 * 
 * Copyright (C) 1992 Linus Torvalds
 */

#include <xen/config.h>
#include <xen/mm.h>
#include <xen/sched.h>
#include <xen/interrupt.h>
#include <xen/init.h>
#include <xen/tqueue.h>

irq_cpustat_t irq_stat[NR_CPUS];

static struct softirq_action softirq_vec[32] __cacheline_aligned;

asmlinkage void do_softirq()
{
    unsigned int pending, cpu = smp_processor_id();
    struct softirq_action *h;

    if ( unlikely(in_interrupt()) )
        BUG();

    /*
     * XEN: This isn't real mutual-exclusion: it just ensures that in_softirq()
     * and in_interrupt() are both TRUE, allowing checks for erroneous reentry.
     */
    cpu_bh_disable(cpu);

    while ( (pending = xchg(&softirq_pending(cpu), 0)) != 0 )
    {
        h = softirq_vec;
        while ( pending )
        {
            if ( pending & 1 )
                h->action(h);
            h++;
            pending >>= 1;
        }
    }

    cpu_bh_enable(cpu);
}

inline void cpu_raise_softirq(unsigned int cpu, unsigned int nr)
{
    __cpu_raise_softirq(cpu, nr);
#ifdef CONFIG_SMP
    if ( cpu != smp_processor_id() )
        smp_send_event_check_cpu(cpu);
#endif
}

void raise_softirq(unsigned int nr)
{
    __cpu_raise_softirq(smp_processor_id(), nr);
}

void open_softirq(int nr, void (*action)(struct softirq_action*), void *data)
{
    softirq_vec[nr].data = data;
    softirq_vec[nr].action = action;
}


/* Tasklets */

struct tasklet_head tasklet_vec[NR_CPUS] __cacheline_aligned;
struct tasklet_head tasklet_hi_vec[NR_CPUS] __cacheline_aligned;

void __tasklet_schedule(struct tasklet_struct *t)
{
    int cpu = smp_processor_id();
    unsigned long flags;

    local_irq_save(flags);
    t->next = tasklet_vec[cpu].list;
    tasklet_vec[cpu].list = t;
    cpu_raise_softirq(cpu, TASKLET_SOFTIRQ);
    local_irq_restore(flags);
}

void __tasklet_hi_schedule(struct tasklet_struct *t)
{
    int cpu = smp_processor_id();
    unsigned long flags;

    local_irq_save(flags);
    t->next = tasklet_hi_vec[cpu].list;
    tasklet_hi_vec[cpu].list = t;
    cpu_raise_softirq(cpu, HI_SOFTIRQ);
    local_irq_restore(flags);
}

static void tasklet_action(struct softirq_action *a)
{
    int cpu = smp_processor_id();
    struct tasklet_struct *list;

    local_irq_disable();
    list = tasklet_vec[cpu].list;
    tasklet_vec[cpu].list = NULL;
    local_irq_enable();

    while ( list != NULL )
    {
        struct tasklet_struct *t = list;

        list = list->next;

        if ( likely(tasklet_trylock(t)) )
        {
            if ( likely(!atomic_read(&t->count)) )
            {
                if ( unlikely(!test_and_clear_bit(TASKLET_STATE_SCHED, 
                                                  &t->state)) )
                    BUG();
                t->func(t->data);
            }
            tasklet_unlock(t);
            continue;
        }

        local_irq_disable();
        t->next = tasklet_vec[cpu].list;
        tasklet_vec[cpu].list = t;
        __cpu_raise_softirq(cpu, TASKLET_SOFTIRQ);
        local_irq_enable();
    }
}

static void tasklet_hi_action(struct softirq_action *a)
{
    int cpu = smp_processor_id();
    struct tasklet_struct *list;

    local_irq_disable();
    list = tasklet_hi_vec[cpu].list;
    tasklet_hi_vec[cpu].list = NULL;
    local_irq_enable();

    while ( list != NULL )
    {
        struct tasklet_struct *t = list;

        list = list->next;

        if ( likely(tasklet_trylock(t)) )
        {
            if ( likely(!atomic_read(&t->count)) )
            {
                if ( unlikely(!test_and_clear_bit(TASKLET_STATE_SCHED, 
                                                  &t->state)) )
                    BUG();
                t->func(t->data);
            }
            tasklet_unlock(t);
            continue;
        }

        local_irq_disable();
        t->next = tasklet_hi_vec[cpu].list;
        tasklet_hi_vec[cpu].list = t;
        __cpu_raise_softirq(cpu, HI_SOFTIRQ);
        local_irq_enable();
    }
}


void tasklet_init(struct tasklet_struct *t,
		  void (*func)(unsigned long), unsigned long data)
{
    t->next = NULL;
    t->state = 0;
    atomic_set(&t->count, 0);
    t->func = func;
    t->data = data;
}

void tasklet_kill(struct tasklet_struct *t)
{
    if ( in_interrupt() )
        BUG();
    while ( test_and_set_bit(TASKLET_STATE_SCHED, &t->state) )
        while ( test_bit(TASKLET_STATE_SCHED, &t->state) )
            do_softirq();
    tasklet_unlock_wait(t);
    clear_bit(TASKLET_STATE_SCHED, &t->state);
}



/* Old style BHs */

static void (*bh_base[32])(void);
struct tasklet_struct bh_task_vec[32];

spinlock_t global_bh_lock = SPIN_LOCK_UNLOCKED;

static void bh_action(unsigned long nr)
{
    int cpu = smp_processor_id();

    if ( !spin_trylock(&global_bh_lock) )
        goto resched;

    if ( !hardirq_trylock(cpu) )
        goto resched_unlock;

    if ( likely(bh_base[nr] != NULL) )
        bh_base[nr]();

    hardirq_endlock(cpu);
    spin_unlock(&global_bh_lock);
    return;

 resched_unlock:
    spin_unlock(&global_bh_lock);
 resched:
    mark_bh(nr);
}

void init_bh(int nr, void (*routine)(void))
{
    bh_base[nr] = routine;
    mb();
}

void remove_bh(int nr)
{
    tasklet_kill(bh_task_vec+nr);
    bh_base[nr] = NULL;
}

void __init softirq_init()
{
    int i;

    for ( i = 0; i < 32; i++)
        tasklet_init(bh_task_vec+i, bh_action, i);

    open_softirq(TASKLET_SOFTIRQ, tasklet_action, NULL);
    open_softirq(HI_SOFTIRQ, tasklet_hi_action, NULL);
}

void __run_task_queue(task_queue *list)
{
    struct list_head  head, *next;
    unsigned long     flags;
    void              (*f) (void *);
    struct tq_struct *p;
    void             *data;

    spin_lock_irqsave(&tqueue_lock, flags);
    list_add(&head, list);
    list_del_init(list);
    spin_unlock_irqrestore(&tqueue_lock, flags);

    next = head.next;
    while ( next != &head )
    {
        p = list_entry(next, struct tq_struct, list);
        next = next->next;
        f = p->routine;
        data = p->data;
        wmb();
        p->sync = 0;
        if ( likely(f != NULL) )
            f(data);
    }
}