summaryrefslogtreecommitdiff
path: root/arch/arm/mach-mt2601/fiq_smp_call.c
blob: 3dcaf8ae7759a8850708aae9b7338161db25d7a5 (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
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cpu.h>
#include <linux/percpu.h>
#include <linux/smp.h>
#include <mach/smp.h>
#include <mach/irqs.h>
#include <mach/fiq_smp_call.h>
#include <asm/cputype.h>

#if defined(CONFIG_FIQ_GLUE)

enum {
	CSD_FLAG_LOCK = 0x01,
};

struct fiq_call_single_data {
	struct list_head list;
	smp_call_func_t func;
	void *info;
	cpumask_var_t cpumask;
	u16 flags;
	u16 priv;
};

struct call_function_data {
	struct fiq_call_single_data csd;
	fiq_smp_call_func_t func;
	atomic_t refs;
	cpumask_var_t cpumask;
};

static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, fiq_cfd_data);
static struct call_function_data *current_cfd_data;

extern void irq_raise_softirq(const struct cpumask *mask, unsigned int irq);

static int __csd_lock_wait(struct fiq_call_single_data *data)
{
	int cpu, nr_online_cpus = 0;

	while (data->flags & CSD_FLAG_LOCK) {
		for_each_cpu(cpu, data->cpumask) {
			if (cpu_online(cpu)) {
				nr_online_cpus++;
			}
		}
		if (!nr_online_cpus)
			return -ENXIO;
		cpu_relax();
	}

	return 0;
}

static void __csd_lock(struct fiq_call_single_data *data)
{
	__csd_lock_wait(data);
	data->flags = CSD_FLAG_LOCK;

	/*
	 * prevent CPU from reordering the above assignment
	 * to ->flags with any subsequent assignments to other
	 * fields of the specified fiq_call_single_data structure:
	 */
	smp_mb();
}

static void __csd_unlock(struct fiq_call_single_data *data)
{
	WARN_ON(!(data->flags & CSD_FLAG_LOCK));

	/*
	 * ensure we're all done before releasing data:
	 */
	smp_mb();

	data->flags &= ~CSD_FLAG_LOCK;
}

/*
 * fiq_smp_call_function: FIQ version of smp_call_function.
 * @func:
 * @info:
 * @wait:
 * Return 0 for success and error code for failure.
 *
 * This function is designed for the debugger only.
 * Other kernel code or drivers should NOT use this function.
 * This function can only be used in the FIQ-WDT handler.
 */
int fiq_smp_call_function(fiq_smp_call_func_t func, void *info, int wait)
{
	struct cpumask *mask = (struct cpumask *)cpu_online_mask;
	struct call_function_data *data;
	int refs, install_csd, this_cpu = 0;

	this_cpu = smp_processor_id();
	data = this_cpu_ptr(&fiq_cfd_data);
	__csd_lock(&data->csd);

	atomic_set(&data->refs, 0);

	data->func = func;
	data->csd.info = info;

	smp_wmb();

	cpumask_and(data->cpumask, mask, cpu_online_mask);
	cpumask_clear_cpu(this_cpu, data->cpumask);
	refs = cpumask_weight(data->cpumask);
	cpumask_and(data->csd.cpumask, data->cpumask, data->cpumask);

	if (unlikely(!refs)) {
		__csd_unlock(&data->csd);
		goto fiq_smp_call_function_exit;
	}

	/* poll to install data on current_cfd_data */
	install_csd = 0;
	do {
#if 0				/* no need to protect due to FIQ-WDT */
		spin_lock(&fiq_smp_call_lock);
#endif

		if (!current_cfd_data) {
			atomic_set(&data->refs, refs);
			current_cfd_data = data;
			install_csd = 1;
		}
#if 0
		spin_unlock(&fiq_smp_call_lock);
#endif
	} while (!install_csd);

	smp_mb();

	/* send a message to all CPUs in the map */
	irq_raise_softirq(data->cpumask, FIQ_SMP_CALL_SGI);

	if (wait)
		__csd_lock_wait(&data->csd);

 fiq_smp_call_function_exit:
	return 0;
}

static void fiq_smp_call_handler(void *arg, void *regs, void *svc_sp)
{
	struct call_function_data *data;
	int cpu = 0, refs;
	fiq_smp_call_func_t func;

	/* get the current cpu id */
	asm volatile ("MRC p15, 0, %0, c0, c0, 5\n" "AND %0, %0, #0xf\n":"+r" (cpu)
 :  : "cc");

	data = current_cfd_data;
	if (data) {
		func = data->func;
		func(data->csd.info, regs, svc_sp);

		cpumask_clear_cpu(cpu, data->csd.cpumask);
		refs = atomic_dec_return(&data->refs);

		if (refs == 0) {
			__csd_unlock(&data->csd);
			current_cfd_data = NULL;
		}
	}
}

static void __fiq_smp_call_init(void *info)
{
	int err;

	err = request_fiq(FIQ_SMP_CALL_SGI, fiq_smp_call_handler, 0, NULL);
	if (err) {
		pr_err("fail to request FIQ for FIQ_SMP_CALL_SGI\n");
	} else {
		pr_debug("Request FIQ for FIQ_SMP_CALL_SGI\n");
	}
}

static int __init fiq_smp_call_init(void)
{
	__fiq_smp_call_init(NULL);
	smp_call_function(__fiq_smp_call_init, NULL, 1);

	return 0;
}
arch_initcall(fiq_smp_call_init);

#endif				/* CONFIG_FIQ_GLUE */