1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <asm/byteorder.h>
3*4882a593Smuzhiyun #include <asm/lppaca.h>
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * The associativity domain numbers are returned from the hypervisor as a
7*4882a593Smuzhiyun * stream of mixed 16-bit and 32-bit fields. The stream is terminated by the
8*4882a593Smuzhiyun * special value of "all ones" (aka. 0xffff) and its size may not exceed 48
9*4882a593Smuzhiyun * bytes.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * --- 16-bit fields -->
12*4882a593Smuzhiyun * _________________________
13*4882a593Smuzhiyun * | 0 | 1 | 2 | 3 | be_packed[0]
14*4882a593Smuzhiyun * ------+-----+-----+------
15*4882a593Smuzhiyun * _________________________
16*4882a593Smuzhiyun * | 4 | 5 | 6 | 7 | be_packed[1]
17*4882a593Smuzhiyun * -------------------------
18*4882a593Smuzhiyun * ...
19*4882a593Smuzhiyun * _________________________
20*4882a593Smuzhiyun * | 20 | 21 | 22 | 23 | be_packed[5]
21*4882a593Smuzhiyun * -------------------------
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Convert to the sequence they would appear in the ibm,associativity property.
24*4882a593Smuzhiyun */
vphn_unpack_associativity(const long * packed,__be32 * unpacked)25*4882a593Smuzhiyun static int vphn_unpack_associativity(const long *packed, __be32 *unpacked)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun __be64 be_packed[VPHN_REGISTER_COUNT];
28*4882a593Smuzhiyun int i, nr_assoc_doms = 0;
29*4882a593Smuzhiyun const __be16 *field = (const __be16 *) be_packed;
30*4882a593Smuzhiyun u16 last = 0;
31*4882a593Smuzhiyun bool is_32bit = false;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define VPHN_FIELD_UNUSED (0xffff)
34*4882a593Smuzhiyun #define VPHN_FIELD_MSB (0x8000)
35*4882a593Smuzhiyun #define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Let's fix the values returned by plpar_hcall9() */
38*4882a593Smuzhiyun for (i = 0; i < VPHN_REGISTER_COUNT; i++)
39*4882a593Smuzhiyun be_packed[i] = cpu_to_be64(packed[i]);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
42*4882a593Smuzhiyun u16 new = be16_to_cpup(field++);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun if (is_32bit) {
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * Let's concatenate the 16 bits of this field to the
47*4882a593Smuzhiyun * 15 lower bits of the previous field
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun unpacked[++nr_assoc_doms] =
50*4882a593Smuzhiyun cpu_to_be32(last << 16 | new);
51*4882a593Smuzhiyun is_32bit = false;
52*4882a593Smuzhiyun } else if (new == VPHN_FIELD_UNUSED)
53*4882a593Smuzhiyun /* This is the list terminator */
54*4882a593Smuzhiyun break;
55*4882a593Smuzhiyun else if (new & VPHN_FIELD_MSB) {
56*4882a593Smuzhiyun /* Data is in the lower 15 bits of this field */
57*4882a593Smuzhiyun unpacked[++nr_assoc_doms] =
58*4882a593Smuzhiyun cpu_to_be32(new & VPHN_FIELD_MASK);
59*4882a593Smuzhiyun } else {
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * Data is in the lower 15 bits of this field
62*4882a593Smuzhiyun * concatenated with the next 16 bit field
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun last = new;
65*4882a593Smuzhiyun is_32bit = true;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* The first cell contains the length of the property */
70*4882a593Smuzhiyun unpacked[0] = cpu_to_be32(nr_assoc_doms);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun return nr_assoc_doms;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* NOTE: This file is included by a selftest and built in userspace. */
76*4882a593Smuzhiyun #ifdef __KERNEL__
77*4882a593Smuzhiyun #include <asm/hvcall.h>
78*4882a593Smuzhiyun
hcall_vphn(unsigned long cpu,u64 flags,__be32 * associativity)79*4882a593Smuzhiyun long hcall_vphn(unsigned long cpu, u64 flags, __be32 *associativity)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun long rc;
82*4882a593Smuzhiyun long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, cpu);
85*4882a593Smuzhiyun if (rc == H_SUCCESS)
86*4882a593Smuzhiyun vphn_unpack_associativity(retbuf, associativity);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return rc;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun #endif
91