summaryrefslogtreecommitdiff
path: root/big-little/switcher/context/gic.c
blob: d4bb1b1a32d55de78d268eea5c01818e5c2fefd7 (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
/*
 * Copyright (c) 2012, ARM Limited. All rights reserved.
 *       
 * Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that the
 * following conditions are met:
 *     
 * Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the 
 * following disclaimer.
 *
 * Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 *      
 * Neither the name of ARM nor the names of its
 * contributors may be used to endorse or promote products
 * derived from this software without specific prior written
 * permission.                        
 */

#include "virt_helpers.h"
#include "misc.h"

struct set_and_clear_regs {
	volatile unsigned int set[32], clear[32];
};

typedef struct {
	/* 0x000 */ volatile unsigned int control;
	const unsigned int controller_type;
	const unsigned int implementer;
	const char padding1[116];
	/* 0x080 */ volatile unsigned int security[32];
	/* 0x100 */ struct set_and_clear_regs enable;
	/* 0x200 */ struct set_and_clear_regs pending;
	/* 0x300 */ struct set_and_clear_regs active;
	/* 0x400 */ volatile unsigned int priority[256];
	/* 0x800 */ volatile unsigned int target[256];
	/* 0xC00 */ volatile unsigned int configuration[64];
	/* 0xD00 */ const char padding3[512];
	/* 0xF00 */ volatile unsigned int software_interrupt;
	const char padding4[12];
	/* 0xF10 */ volatile unsigned int sgi_clr_pending[4];
	/* 0xF20 */ volatile unsigned int sgi_set_pending[4];
	const char padding5[176];
	/* 0xFE0 */ unsigned const int peripheral_id[4];
	/* 0xFF0 */ unsigned const int primecell_id[4];
} interrupt_distributor;

typedef struct {
	/* 0x00 */ volatile unsigned int control;
	/* 0x04 */ volatile unsigned int priority_mask;
	/* 0x08 */ volatile unsigned int binary_point;
	/* 0x0c */ volatile unsigned const int interrupt_ack;
	/* 0x10 */ volatile unsigned int end_of_interrupt;
	/* 0x14 */ volatile unsigned const int running_priority;
	/* 0x18 */ volatile unsigned const int highest_pending;
} cpu_interface;

static void wcpymem2io(void volatile *dest, void const *src, size_t count)
{
	unsigned long volatile *_dest = dest;
	unsigned long const *_src = src;

	while (count--)
		*_dest++ = *_src++;
}

static void wcpyio2mem(void *dest, void volatile const *src, size_t count)
{
	unsigned long volatile const *_src = src;
	unsigned long *_dest = dest;

	while (count--)
		*_dest++ = *_src++;
}

/*
 * Saves the GIC CPU interface context
 * Requires 3 words of memory
 */
void save_gic_interface(unsigned int *pointer, unsigned gic_interface_address)
{
	cpu_interface *ci = (cpu_interface *) gic_interface_address;

	pointer[0] = ci->control;
	pointer[1] = ci->priority_mask;
	pointer[2] = ci->binary_point;

}

/*
 * Saves this CPU's banked parts of the distributor
 * Returns non-zero if an SGI/PPI interrupt is pending (after saving all required context)
 * Requires 19 words of memory
 */
int save_gic_distributor_private(unsigned int *pointer,
				 unsigned gic_distributor_address)
{
	interrupt_distributor *id =
	    (interrupt_distributor *) gic_distributor_address;
	unsigned int *ptr = 0x0;

	*pointer = id->enable.set[0];
	++pointer;
	wcpyio2mem(pointer, id->priority, 8);
	pointer += 8;
	wcpyio2mem(pointer, id->target, 8);
	pointer += 8;

	/* Save just the PPI configurations (SGIs are not configurable) */
	*pointer = id->configuration[1];
	++pointer;

	/*
	 * Private peripheral interrupts need to be replayed on 
	 * the destination cpu interface for consistency. This
	 * is the responsibility of the peripheral driver. When
	 * it sees a pending interrupt while saving its context
	 * it should record enough information to recreate the 
	 * interrupt while restoring.
	 * We don't save the Pending/Active status and clear it
	 * so that it does not interfere when we are back.
	 */
	id->pending.clear[0] = 0xffffffff;
	id->active.clear[0] = 0xffffffff;

	/*
	 * IPIs are different and can be replayed just by saving
	 * and restoring the set/clear pending registers
	 */
	ptr = pointer;
	wcpyio2mem(pointer, id->sgi_set_pending, 4);
	pointer += 8;

	/*
	 * Clear the pending SGIs on this cpuif so that they don't
	 * interfere with the wfi later on.
	 */
	wcpymem2io(id->sgi_clr_pending, ptr, 4);

	if (*pointer) {
		return -1;
	} else {
		return 0;
	}
}

/*
 * Saves the shared parts of the distributor
 * Requires 1 word of memory, plus 20 words for each block of 32 SPIs (max 641 words)
 * Returns non-zero if an SPI interrupt is pending (after saving all required context)
 */
int save_gic_distributor_shared(unsigned int *pointer,
				unsigned gic_distributor_address)
{
	int retval = 0;
	interrupt_distributor *id =
	    (interrupt_distributor *) gic_distributor_address;
	unsigned num_spis = 0;

	/* Calculate how many SPIs the GIC supports */
	num_spis = 32 * (id->controller_type & 0x1f);

	/* Save rest of GIC configuration */
	if (num_spis) {
		wcpyio2mem(pointer, id->target + 8, num_spis / 4);
		pointer += num_spis / 4;
	}

	/* Save control register */
	*pointer = id->control;
	++pointer;

	return retval;
}

void restore_gic_interface(unsigned int *pointer,
			   unsigned gic_interface_address)
{
	cpu_interface *ci = (cpu_interface *) gic_interface_address;

	ci->priority_mask = pointer[1];
	ci->binary_point = pointer[2];

	/* Restore control register last */
	ci->control = pointer[0];
}

void restore_gic_distributor_private(unsigned int *pointer,
				     unsigned gic_distributor_address)
{
	interrupt_distributor *id =
	    (interrupt_distributor *) gic_distributor_address;
	unsigned ctr, prev_val = 0, prev_ctr = 0;

	id->enable.set[0] = *pointer;
	++pointer;

	wcpymem2io(id->priority, pointer, 8);
	pointer += 8;
	wcpymem2io(id->target, pointer, 8);
	pointer += 8;

	/* Restore just the PPI configurations (SGIs are not configurable) */
	id->configuration[1] = *pointer;
	++pointer;

	/*
	 * Clear active and  pending PPIs as they will be recreated by the 
	 * peripiherals
	 */
	id->active.clear[0] = 0xffffffff;
	id->pending.clear[0] = 0xffffffff;

	/*
	 * Restore pending IPIs
	 */
	for (ctr = 0; ctr < 4; ctr++) {
		if (!pointer[ctr])
			continue;

		if (pointer[ctr] == prev_val) {
			pointer[ctr] = pointer[prev_ctr];
		} else {
			prev_val = pointer[ctr];
			prev_ctr = ctr;
			remap_cpuif(&pointer[ctr]);
		}
	}

	wcpymem2io(id->sgi_set_pending, pointer, 4);
	pointer += 4;

	id->pending.set[0] = *pointer;

	return;
}

/*
 * Optimized routine to restore the shared vgic distributor interface.
 * Saving on outbound and restoring on inbound is redundant as the
 * context is non-volatile across a switch. Hence, simply R-M-W on
 * the inbound and remove the 'save' function from the outbound
 * critical path.
 */
void restore_gic_distributor_shared(unsigned int *pointer,
				    unsigned gic_distributor_address)
{
	interrupt_distributor *id =
	    (interrupt_distributor *) gic_distributor_address;
	unsigned num_spis;
	unsigned ctr, prev_val = 0, prev_ctr = 0;

	/* Calculate how many SPIs the GIC supports */
	num_spis = 32 * ((id->controller_type) & 0x1f);

	/* Restore rest of GIC configuration */
	if (num_spis) {
		wcpyio2mem(pointer, id->target + 8, num_spis / 4);

		for (ctr = 0; ctr < num_spis / 4; ctr++) {
			if (!pointer[ctr])
				continue;

			if (pointer[ctr] == prev_val) {
				pointer[ctr] = pointer[prev_ctr];
			} else {
				prev_val = pointer[ctr];
				prev_ctr = ctr;
				remap_cpuif(&pointer[ctr]);
			}
		}

		wcpymem2io(id->target + 8, pointer, num_spis / 4);
	}

	return;
}