powerpc/vphn: move VPHN parsing logic to a separate file
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / mm / vphn.c
1 #include <asm/byteorder.h>
2 #include "vphn.h"
3
4 /*
5  * Convert the associativity domain numbers returned from the hypervisor
6  * to the sequence they would appear in the ibm,associativity property.
7  */
8 int vphn_unpack_associativity(const long *packed, __be32 *unpacked)
9 {
10         __be64 be_packed[VPHN_REGISTER_COUNT];
11         int i, nr_assoc_doms = 0;
12         const __be16 *field = (const __be16 *) be_packed;
13
14 #define VPHN_FIELD_UNUSED       (0xffff)
15 #define VPHN_FIELD_MSB          (0x8000)
16 #define VPHN_FIELD_MASK         (~VPHN_FIELD_MSB)
17
18         /* Let's recreate the original stream. */
19         for (i = 0; i < VPHN_REGISTER_COUNT; i++)
20                 be_packed[i] = cpu_to_be64(packed[i]);
21
22         for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
23                 if (be16_to_cpup(field) == VPHN_FIELD_UNUSED) {
24                         /* All significant fields processed, and remaining
25                          * fields contain the reserved value of all 1's.
26                          * Just store them.
27                          */
28                         unpacked[i] = *((__be32 *)field);
29                         field += 2;
30                 } else if (be16_to_cpup(field) & VPHN_FIELD_MSB) {
31                         /* Data is in the lower 15 bits of this field */
32                         unpacked[i] = cpu_to_be32(
33                                 be16_to_cpup(field) & VPHN_FIELD_MASK);
34                         field++;
35                         nr_assoc_doms++;
36                 } else {
37                         /* Data is in the lower 15 bits of this field
38                          * concatenated with the next 16 bit field
39                          */
40                         unpacked[i] = *((__be32 *)field);
41                         field += 2;
42                         nr_assoc_doms++;
43                 }
44         }
45
46         /* The first cell contains the length of the property */
47         unpacked[0] = cpu_to_be32(nr_assoc_doms);
48
49         return nr_assoc_doms;
50 }