summaryrefslogtreecommitdiff
path: root/big-little/switcher/context/sh_vgic.c
blob: 7672fdd73a948a9073260b9aae4480fa4831fafb (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
/*
 * 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 "gic_registers.h"
#include "misc.h"
#include "context.h"

/*
 * Private data structure that maps each cpuid in a
 * multicluster system to its physical cpu interface
 * id when a shared vGIC is used.
 */
static unsigned int cpuif_map[MAX_CLUSTERS][MAX_CORES];

/*
 * Private data structure that maps each cpu interface
 * id to the corresponding cpuid & clusterid. In each
 * entry top 4 bits store the cluster id while the bottom
 * 4 store the cpuid.
 *
 * TODO:
 * No real need for this data structure. Should be 
 * possible to get this info from the previous data 
 * structure and the knowledge of number of clusters
 * and cpus from the KFSCB
 */
static unsigned int cpuinfo_map[MAX_CPUIFS];

/*
 * IPI to use for cpu interface discovery.
 */
#define CPUIF_IPI    0xf

/*
 * In the presence of the Switcher and the shared vGIC
 * find the mapping between the cpu interface and the
 * cpu id. This is required to:
 * a) Set processor targets correctly during context
 *    save & restore and normal operation (IPI handling)
 * b) Restoring the context of pending IPIs on the inbound
 *    cluster.
 * Ideally a platform defined register should have done the
 * trick. However, we rely on a software mechanism to obtain
 * this information. 
 *
 * Assumptions:
 * a) Expected to be used only in the "Switching" case when
 *    there is a mismatch between the cpuids and the cpuif ids
 *    on the "other" cluster
 * b) In the "Switching" case with external vGIC, the distributor
 *    interface should never get disabled.
 * c) Always called in Secure world
 *
 * Idea is that, without disturbing the existing GIC state too
 * much (outbound might be doing things with it), we need to
 * ensure that only the IPI which we choose gets through our
 * cpu interface. This should not be a problem as the SPIs will
 * be targetted to the outbound cluster cpus & there will be no
 * local peripheral interrupts expected. There is paranoia about
 * getting IPIs from the outbound but this can be dealt with by
 * manipulating the IPI priorities so that we only see what we 
 * want to see.
 *
 * TODO:
 * Assuming no IPIs will be received at this point of time. So
 * no changes will be made to the priority mask registers.
 */
unsigned map_cpuif(unsigned cluster_id, unsigned cpu_id)
{
        unsigned cpuif_id = 0;

        cpuif_id = bitindex(read32(GIC_ID_PHY_BASE + GICD_CPUS) & 0xff);
        cpuif_map[cluster_id][cpu_id] = cpuif_id;
        cpuinfo_map[cpuif_id] = (cluster_id << 4) | cpu_id;

        return 0;
}

/*
 * Given a cpu and cluster id find the cpu interface it maps to.
 */
unsigned get_cpuif(unsigned cluster_id, unsigned cpu_id)
{
        return cpuif_map[cluster_id][cpu_id];
}

/*
 * Given a cpu interface id, find what cpu and cluster id it maps to.
 */
unsigned get_cpuinfo(unsigned cpuif)
{
        return cpuinfo_map[cpuif];
}

/*
 * Given a cpu interface mask, find the corresponding cpuid mask on that cluster.
 */
unsigned get_cpu_mask(unsigned cpuif_mask)
{
        unsigned num_bytes = sizeof(unsigned int) / sizeof(unsigned char), ctr;
        unsigned cpuif = 0, clusterid = read_clusterid(), cpu_mask = 0;
        unsigned cpuid = 0;

        for (ctr = 0; ctr < num_bytes; ctr++) { /* Iterate through the cpu_mask byte wise */
                unsigned byte = 0;
                unsigned char lz = 0;

                byte = (cpuif_mask >> (ctr << 3)) & 0xff;
                while ((lz = __clz(byte)) != 0x20) {
                        cpuif = 31 - lz;
                        byte &= ~(1 << cpuif);  /* Clear the bit just discovered */
                        cpuid = get_cpuinfo(cpuif) & 0xf;
                        cpu_mask |= (1 << cpuid) << (ctr << 3);
                }
        }

        return cpu_mask;
}

/*
 * Given a cpu mask, find the corresponding cpu interface mask on that cluster.
 */
unsigned get_cpuif_mask(unsigned cpu_mask)
{
        unsigned num_bytes = sizeof(unsigned int) / sizeof(unsigned char), ctr;
        unsigned cpuif = 0, clusterid = read_clusterid(), cpuif_mask = 0;
        unsigned cpuid = 0;

        for (ctr = 0; ctr < num_bytes; ctr++) { /* Iterate through the cpu_mask byte wise */
                unsigned byte = 0;
                unsigned char lz = 0;

                byte = (cpu_mask >> (ctr << 3)) & 0xff;
                while ((lz = __clz(byte)) != 0x20) {
                        cpuid = 31 - lz;
                        byte &= ~(1 << cpuid);  /* Clear the bit just discovered */
                        cpuif = get_cpuif(clusterid, cpuid);
                        cpuif_mask |= (1 << cpuif) << (ctr << 3);
                }
        }

        return cpuif_mask;
}

/*
 * Given a cpu interface mask, find its corresponding mask on the other cluster 
 * NOTE: Creates the new mask in-place.
 */
#if 1
/*
 * This is the fast version of remapping cpu interface ids to cpuids. Instead of
 * remapping each bit (target interface) in the arg passed, it simply shifts all
 * the bits by the number of cpus available. 
 */
unsigned remap_cpuif(unsigned *cpuif_mask)
{
        unsigned cluster_id = read_clusterid(), num_cpus = num_secondaries() + 1;
    

        if(cluster_id == EAGLE)
                *cpuif_mask = *cpuif_mask >> num_cpus;
        else
                *cpuif_mask = *cpuif_mask << num_cpus;

        return 0;
}
#else
unsigned remap_cpuif(unsigned *cpuif_mask)
{
        unsigned ib_cpuif_mask = 0, ob_cpuif = 0, ib_cpuif = 0, ob_cpuid =
                0, ob_clusterid = 0, ib_cpuid = 0, ib_clusterid = 0;
        unsigned num_bytes = sizeof(unsigned int) / sizeof(unsigned char), ctr;

        for (ctr = 0; ctr < num_bytes; ctr++) {
                unsigned byte = 0;
                unsigned char lz = 0;

                byte = (*cpuif_mask >> (ctr << 3)) & 0xff;

                while ((lz = __clz(byte)) != 0x20) {
                        ob_cpuif = 31 - lz;
                        byte &= ~(1 << ob_cpuif);       /* Clear the bit just discovered */
                        ob_cpuid = get_cpuinfo(ob_cpuif) & 0xf;
                        ob_clusterid = (get_cpuinfo(ob_cpuif) >> 4) & 0xf;

                        /* 
                         * TODO: Can we assume that the inbound and outbound clusters will
                         * always be logical complements of each other
                         */
                        ib_clusterid = !ob_clusterid;

                        /* 
                         * TODO: Assuming that the cpuids have a 1:1 mapping i.e. cpuX on
                         * one cluster will always map to cpuX on the other cluster.
                         */
                        ib_cpuid = ob_cpuid;
                        ib_cpuif = get_cpuif(ib_clusterid, ib_cpuid);
                        ib_cpuif_mask |= (1 << ib_cpuif) << (ctr << 3);
                }
        }

        *cpuif_mask = ib_cpuif_mask;
        return 0;
}
#endif