1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2015 IBM Corp.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/compiler.h>
8*4882a593Smuzhiyun #include <linux/types.h>
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <asm/byteorder.h>
11*4882a593Smuzhiyun #include "hcalls.h"
12*4882a593Smuzhiyun #include "trace.h"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define CXL_HCALL_TIMEOUT 60000
15*4882a593Smuzhiyun #define CXL_HCALL_TIMEOUT_DOWNLOAD 120000
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define H_ATTACH_CA_PROCESS 0x344
18*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION 0x348
19*4882a593Smuzhiyun #define H_DETACH_CA_PROCESS 0x34C
20*4882a593Smuzhiyun #define H_COLLECT_CA_INT_INFO 0x350
21*4882a593Smuzhiyun #define H_CONTROL_CA_FAULTS 0x354
22*4882a593Smuzhiyun #define H_DOWNLOAD_CA_FUNCTION 0x35C
23*4882a593Smuzhiyun #define H_DOWNLOAD_CA_FACILITY 0x364
24*4882a593Smuzhiyun #define H_CONTROL_CA_FACILITY 0x368
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_RESET 1 /* perform a reset */
27*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_SUSPEND_PROCESS 2 /* suspend a process from being executed */
28*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_RESUME_PROCESS 3 /* resume a process to be executed */
29*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_READ_ERR_STATE 4 /* read the error state */
30*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_GET_AFU_ERR 5 /* collect the AFU error buffer */
31*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_GET_CONFIG 6 /* collect configuration record */
32*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_GET_DOWNLOAD_STATE 7 /* query to return download status */
33*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_TERMINATE_PROCESS 8 /* terminate the process before completion */
34*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_COLLECT_VPD 9 /* collect VPD */
35*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_GET_FUNCTION_ERR_INT 11 /* read the function-wide error data based on an interrupt */
36*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_ACK_FUNCTION_ERR_INT 12 /* acknowledge function-wide error data based on an interrupt */
37*4882a593Smuzhiyun #define H_CONTROL_CA_FUNCTION_GET_ERROR_LOG 13 /* retrieve the Platform Log ID (PLID) of an error log */
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define H_CONTROL_CA_FAULTS_RESPOND_PSL 1
40*4882a593Smuzhiyun #define H_CONTROL_CA_FAULTS_RESPOND_AFU 2
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define H_CONTROL_CA_FACILITY_RESET 1 /* perform a reset */
43*4882a593Smuzhiyun #define H_CONTROL_CA_FACILITY_COLLECT_VPD 2 /* collect VPD */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #define H_DOWNLOAD_CA_FACILITY_DOWNLOAD 1 /* download adapter image */
46*4882a593Smuzhiyun #define H_DOWNLOAD_CA_FACILITY_VALIDATE 2 /* validate adapter image */
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define _CXL_LOOP_HCALL(call, rc, retbuf, fn, ...) \
50*4882a593Smuzhiyun { \
51*4882a593Smuzhiyun unsigned int delay, total_delay = 0; \
52*4882a593Smuzhiyun u64 token = 0; \
53*4882a593Smuzhiyun \
54*4882a593Smuzhiyun memset(retbuf, 0, sizeof(retbuf)); \
55*4882a593Smuzhiyun while (1) { \
56*4882a593Smuzhiyun rc = call(fn, retbuf, __VA_ARGS__, token); \
57*4882a593Smuzhiyun token = retbuf[0]; \
58*4882a593Smuzhiyun if (rc != H_BUSY && !H_IS_LONG_BUSY(rc)) \
59*4882a593Smuzhiyun break; \
60*4882a593Smuzhiyun \
61*4882a593Smuzhiyun if (rc == H_BUSY) \
62*4882a593Smuzhiyun delay = 10; \
63*4882a593Smuzhiyun else \
64*4882a593Smuzhiyun delay = get_longbusy_msecs(rc); \
65*4882a593Smuzhiyun \
66*4882a593Smuzhiyun total_delay += delay; \
67*4882a593Smuzhiyun if (total_delay > CXL_HCALL_TIMEOUT) { \
68*4882a593Smuzhiyun WARN(1, "Warning: Giving up waiting for CXL hcall " \
69*4882a593Smuzhiyun "%#x after %u msec\n", fn, total_delay); \
70*4882a593Smuzhiyun rc = H_BUSY; \
71*4882a593Smuzhiyun break; \
72*4882a593Smuzhiyun } \
73*4882a593Smuzhiyun msleep(delay); \
74*4882a593Smuzhiyun } \
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun #define CXL_H_WAIT_UNTIL_DONE(...) _CXL_LOOP_HCALL(plpar_hcall, __VA_ARGS__)
77*4882a593Smuzhiyun #define CXL_H9_WAIT_UNTIL_DONE(...) _CXL_LOOP_HCALL(plpar_hcall9, __VA_ARGS__)
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun #define _PRINT_MSG(rc, format, ...) \
80*4882a593Smuzhiyun { \
81*4882a593Smuzhiyun if ((rc != H_SUCCESS) && (rc != H_CONTINUE)) \
82*4882a593Smuzhiyun pr_err(format, __VA_ARGS__); \
83*4882a593Smuzhiyun else \
84*4882a593Smuzhiyun pr_devel(format, __VA_ARGS__); \
85*4882a593Smuzhiyun } \
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun static char *afu_op_names[] = {
89*4882a593Smuzhiyun "UNKNOWN_OP", /* 0 undefined */
90*4882a593Smuzhiyun "RESET", /* 1 */
91*4882a593Smuzhiyun "SUSPEND_PROCESS", /* 2 */
92*4882a593Smuzhiyun "RESUME_PROCESS", /* 3 */
93*4882a593Smuzhiyun "READ_ERR_STATE", /* 4 */
94*4882a593Smuzhiyun "GET_AFU_ERR", /* 5 */
95*4882a593Smuzhiyun "GET_CONFIG", /* 6 */
96*4882a593Smuzhiyun "GET_DOWNLOAD_STATE", /* 7 */
97*4882a593Smuzhiyun "TERMINATE_PROCESS", /* 8 */
98*4882a593Smuzhiyun "COLLECT_VPD", /* 9 */
99*4882a593Smuzhiyun "UNKNOWN_OP", /* 10 undefined */
100*4882a593Smuzhiyun "GET_FUNCTION_ERR_INT", /* 11 */
101*4882a593Smuzhiyun "ACK_FUNCTION_ERR_INT", /* 12 */
102*4882a593Smuzhiyun "GET_ERROR_LOG", /* 13 */
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun static char *control_adapter_op_names[] = {
106*4882a593Smuzhiyun "UNKNOWN_OP", /* 0 undefined */
107*4882a593Smuzhiyun "RESET", /* 1 */
108*4882a593Smuzhiyun "COLLECT_VPD", /* 2 */
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun static char *download_op_names[] = {
112*4882a593Smuzhiyun "UNKNOWN_OP", /* 0 undefined */
113*4882a593Smuzhiyun "DOWNLOAD", /* 1 */
114*4882a593Smuzhiyun "VALIDATE", /* 2 */
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun
op_str(unsigned int op,char * name_array[],int array_len)117*4882a593Smuzhiyun static char *op_str(unsigned int op, char *name_array[], int array_len)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun if (op >= array_len)
120*4882a593Smuzhiyun return "UNKNOWN_OP";
121*4882a593Smuzhiyun return name_array[op];
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun #define OP_STR(op, name_array) op_str(op, name_array, ARRAY_SIZE(name_array))
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun #define OP_STR_AFU(op) OP_STR(op, afu_op_names)
127*4882a593Smuzhiyun #define OP_STR_CONTROL_ADAPTER(op) OP_STR(op, control_adapter_op_names)
128*4882a593Smuzhiyun #define OP_STR_DOWNLOAD_ADAPTER(op) OP_STR(op, download_op_names)
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun
cxl_h_attach_process(u64 unit_address,struct cxl_process_element_hcall * element,u64 * process_token,u64 * mmio_addr,u64 * mmio_size)131*4882a593Smuzhiyun long cxl_h_attach_process(u64 unit_address,
132*4882a593Smuzhiyun struct cxl_process_element_hcall *element,
133*4882a593Smuzhiyun u64 *process_token, u64 *mmio_addr, u64 *mmio_size)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
136*4882a593Smuzhiyun long rc;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun CXL_H_WAIT_UNTIL_DONE(rc, retbuf, H_ATTACH_CA_PROCESS, unit_address, virt_to_phys(element));
139*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_attach_process(%#.16llx, %#.16lx): %li\n",
140*4882a593Smuzhiyun unit_address, virt_to_phys(element), rc);
141*4882a593Smuzhiyun trace_cxl_hcall_attach(unit_address, virt_to_phys(element), retbuf[0], retbuf[1], retbuf[2], rc);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun pr_devel("token: 0x%.8lx mmio_addr: 0x%lx mmio_size: 0x%lx\nProcess Element Structure:\n",
144*4882a593Smuzhiyun retbuf[0], retbuf[1], retbuf[2]);
145*4882a593Smuzhiyun cxl_dump_debug_buffer(element, sizeof(*element));
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun switch (rc) {
148*4882a593Smuzhiyun case H_SUCCESS: /* The process info is attached to the coherent platform function */
149*4882a593Smuzhiyun *process_token = retbuf[0];
150*4882a593Smuzhiyun if (mmio_addr)
151*4882a593Smuzhiyun *mmio_addr = retbuf[1];
152*4882a593Smuzhiyun if (mmio_size)
153*4882a593Smuzhiyun *mmio_size = retbuf[2];
154*4882a593Smuzhiyun return 0;
155*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied. */
156*4882a593Smuzhiyun case H_FUNCTION: /* The function is not supported. */
157*4882a593Smuzhiyun return -EINVAL;
158*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall */
159*4882a593Smuzhiyun case H_RESOURCE: /* The coherent platform function does not have enough additional resource to attach the process */
160*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the attach operation */
161*4882a593Smuzhiyun case H_STATE: /* The coherent platform function is not in a valid state */
162*4882a593Smuzhiyun case H_BUSY:
163*4882a593Smuzhiyun return -EBUSY;
164*4882a593Smuzhiyun default:
165*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
166*4882a593Smuzhiyun return -EINVAL;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * cxl_h_detach_process - Detach a process element from a coherent
172*4882a593Smuzhiyun * platform function.
173*4882a593Smuzhiyun */
cxl_h_detach_process(u64 unit_address,u64 process_token)174*4882a593Smuzhiyun long cxl_h_detach_process(u64 unit_address, u64 process_token)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
177*4882a593Smuzhiyun long rc;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun CXL_H_WAIT_UNTIL_DONE(rc, retbuf, H_DETACH_CA_PROCESS, unit_address, process_token);
180*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_detach_process(%#.16llx, 0x%.8llx): %li\n", unit_address, process_token, rc);
181*4882a593Smuzhiyun trace_cxl_hcall_detach(unit_address, process_token, rc);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun switch (rc) {
184*4882a593Smuzhiyun case H_SUCCESS: /* The process was detached from the coherent platform function */
185*4882a593Smuzhiyun return 0;
186*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied. */
187*4882a593Smuzhiyun return -EINVAL;
188*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall */
189*4882a593Smuzhiyun case H_RESOURCE: /* The function has page table mappings for MMIO */
190*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the detach operation */
191*4882a593Smuzhiyun case H_STATE: /* The coherent platform function is not in a valid state */
192*4882a593Smuzhiyun case H_BUSY:
193*4882a593Smuzhiyun return -EBUSY;
194*4882a593Smuzhiyun default:
195*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
196*4882a593Smuzhiyun return -EINVAL;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * cxl_h_control_function - This H_CONTROL_CA_FUNCTION hypervisor call allows
202*4882a593Smuzhiyun * the partition to manipulate or query
203*4882a593Smuzhiyun * certain coherent platform function behaviors.
204*4882a593Smuzhiyun */
cxl_h_control_function(u64 unit_address,u64 op,u64 p1,u64 p2,u64 p3,u64 p4,u64 * out)205*4882a593Smuzhiyun static long cxl_h_control_function(u64 unit_address, u64 op,
206*4882a593Smuzhiyun u64 p1, u64 p2, u64 p3, u64 p4, u64 *out)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
209*4882a593Smuzhiyun long rc;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun CXL_H9_WAIT_UNTIL_DONE(rc, retbuf, H_CONTROL_CA_FUNCTION, unit_address, op, p1, p2, p3, p4);
212*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_control_function(%#.16llx, %s(%#llx, %#llx, %#llx, %#llx, R4: %#lx)): %li\n",
213*4882a593Smuzhiyun unit_address, OP_STR_AFU(op), p1, p2, p3, p4, retbuf[0], rc);
214*4882a593Smuzhiyun trace_cxl_hcall_control_function(unit_address, OP_STR_AFU(op), p1, p2, p3, p4, retbuf[0], rc);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun switch (rc) {
217*4882a593Smuzhiyun case H_SUCCESS: /* The operation is completed for the coherent platform function */
218*4882a593Smuzhiyun if ((op == H_CONTROL_CA_FUNCTION_GET_FUNCTION_ERR_INT ||
219*4882a593Smuzhiyun op == H_CONTROL_CA_FUNCTION_READ_ERR_STATE ||
220*4882a593Smuzhiyun op == H_CONTROL_CA_FUNCTION_COLLECT_VPD))
221*4882a593Smuzhiyun *out = retbuf[0];
222*4882a593Smuzhiyun return 0;
223*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied. */
224*4882a593Smuzhiyun case H_FUNCTION: /* The function is not supported. */
225*4882a593Smuzhiyun case H_NOT_FOUND: /* The operation supplied was not valid */
226*4882a593Smuzhiyun case H_NOT_AVAILABLE: /* The operation cannot be performed because the AFU has not been downloaded */
227*4882a593Smuzhiyun case H_SG_LIST: /* An block list entry was invalid */
228*4882a593Smuzhiyun return -EINVAL;
229*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall */
230*4882a593Smuzhiyun case H_RESOURCE: /* The function has page table mappings for MMIO */
231*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the attach operation */
232*4882a593Smuzhiyun case H_STATE: /* The coherent platform function is not in a valid state */
233*4882a593Smuzhiyun case H_BUSY:
234*4882a593Smuzhiyun return -EBUSY;
235*4882a593Smuzhiyun default:
236*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
237*4882a593Smuzhiyun return -EINVAL;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun * cxl_h_reset_afu - Perform a reset to the coherent platform function.
243*4882a593Smuzhiyun */
cxl_h_reset_afu(u64 unit_address)244*4882a593Smuzhiyun long cxl_h_reset_afu(u64 unit_address)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
247*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_RESET,
248*4882a593Smuzhiyun 0, 0, 0, 0,
249*4882a593Smuzhiyun NULL);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /*
253*4882a593Smuzhiyun * cxl_h_suspend_process - Suspend a process from being executed
254*4882a593Smuzhiyun * Parameter1 = process-token as returned from H_ATTACH_CA_PROCESS when
255*4882a593Smuzhiyun * process was attached.
256*4882a593Smuzhiyun */
cxl_h_suspend_process(u64 unit_address,u64 process_token)257*4882a593Smuzhiyun long cxl_h_suspend_process(u64 unit_address, u64 process_token)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
260*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_SUSPEND_PROCESS,
261*4882a593Smuzhiyun process_token, 0, 0, 0,
262*4882a593Smuzhiyun NULL);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /*
266*4882a593Smuzhiyun * cxl_h_resume_process - Resume a process to be executed
267*4882a593Smuzhiyun * Parameter1 = process-token as returned from H_ATTACH_CA_PROCESS when
268*4882a593Smuzhiyun * process was attached.
269*4882a593Smuzhiyun */
cxl_h_resume_process(u64 unit_address,u64 process_token)270*4882a593Smuzhiyun long cxl_h_resume_process(u64 unit_address, u64 process_token)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
273*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_RESUME_PROCESS,
274*4882a593Smuzhiyun process_token, 0, 0, 0,
275*4882a593Smuzhiyun NULL);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * cxl_h_read_error_state - Checks the error state of the coherent
280*4882a593Smuzhiyun * platform function.
281*4882a593Smuzhiyun * R4 contains the error state
282*4882a593Smuzhiyun */
cxl_h_read_error_state(u64 unit_address,u64 * state)283*4882a593Smuzhiyun long cxl_h_read_error_state(u64 unit_address, u64 *state)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
286*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_READ_ERR_STATE,
287*4882a593Smuzhiyun 0, 0, 0, 0,
288*4882a593Smuzhiyun state);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun * cxl_h_get_afu_err - collect the AFU error buffer
293*4882a593Smuzhiyun * Parameter1 = byte offset into error buffer to retrieve, valid values
294*4882a593Smuzhiyun * are between 0 and (ibm,error-buffer-size - 1)
295*4882a593Smuzhiyun * Parameter2 = 4K aligned real address of error buffer, to be filled in
296*4882a593Smuzhiyun * Parameter3 = length of error buffer, valid values are 4K or less
297*4882a593Smuzhiyun */
cxl_h_get_afu_err(u64 unit_address,u64 offset,u64 buf_address,u64 len)298*4882a593Smuzhiyun long cxl_h_get_afu_err(u64 unit_address, u64 offset,
299*4882a593Smuzhiyun u64 buf_address, u64 len)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
302*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_GET_AFU_ERR,
303*4882a593Smuzhiyun offset, buf_address, len, 0,
304*4882a593Smuzhiyun NULL);
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /*
308*4882a593Smuzhiyun * cxl_h_get_config - collect configuration record for the
309*4882a593Smuzhiyun * coherent platform function
310*4882a593Smuzhiyun * Parameter1 = # of configuration record to retrieve, valid values are
311*4882a593Smuzhiyun * between 0 and (ibm,#config-records - 1)
312*4882a593Smuzhiyun * Parameter2 = byte offset into configuration record to retrieve,
313*4882a593Smuzhiyun * valid values are between 0 and (ibm,config-record-size - 1)
314*4882a593Smuzhiyun * Parameter3 = 4K aligned real address of configuration record buffer,
315*4882a593Smuzhiyun * to be filled in
316*4882a593Smuzhiyun * Parameter4 = length of configuration buffer, valid values are 4K or less
317*4882a593Smuzhiyun */
cxl_h_get_config(u64 unit_address,u64 cr_num,u64 offset,u64 buf_address,u64 len)318*4882a593Smuzhiyun long cxl_h_get_config(u64 unit_address, u64 cr_num, u64 offset,
319*4882a593Smuzhiyun u64 buf_address, u64 len)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
322*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_GET_CONFIG,
323*4882a593Smuzhiyun cr_num, offset, buf_address, len,
324*4882a593Smuzhiyun NULL);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun /*
328*4882a593Smuzhiyun * cxl_h_terminate_process - Terminate the process before completion
329*4882a593Smuzhiyun * Parameter1 = process-token as returned from H_ATTACH_CA_PROCESS when
330*4882a593Smuzhiyun * process was attached.
331*4882a593Smuzhiyun */
cxl_h_terminate_process(u64 unit_address,u64 process_token)332*4882a593Smuzhiyun long cxl_h_terminate_process(u64 unit_address, u64 process_token)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
335*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_TERMINATE_PROCESS,
336*4882a593Smuzhiyun process_token, 0, 0, 0,
337*4882a593Smuzhiyun NULL);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /*
341*4882a593Smuzhiyun * cxl_h_collect_vpd - Collect VPD for the coherent platform function.
342*4882a593Smuzhiyun * Parameter1 = # of VPD record to retrieve, valid values are between 0
343*4882a593Smuzhiyun * and (ibm,#config-records - 1).
344*4882a593Smuzhiyun * Parameter2 = 4K naturally aligned real buffer containing block
345*4882a593Smuzhiyun * list entries
346*4882a593Smuzhiyun * Parameter3 = number of block list entries in the block list, valid
347*4882a593Smuzhiyun * values are between 0 and 256
348*4882a593Smuzhiyun */
cxl_h_collect_vpd(u64 unit_address,u64 record,u64 list_address,u64 num,u64 * out)349*4882a593Smuzhiyun long cxl_h_collect_vpd(u64 unit_address, u64 record, u64 list_address,
350*4882a593Smuzhiyun u64 num, u64 *out)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
353*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_COLLECT_VPD,
354*4882a593Smuzhiyun record, list_address, num, 0,
355*4882a593Smuzhiyun out);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun * cxl_h_get_fn_error_interrupt - Read the function-wide error data based on an interrupt
360*4882a593Smuzhiyun */
cxl_h_get_fn_error_interrupt(u64 unit_address,u64 * reg)361*4882a593Smuzhiyun long cxl_h_get_fn_error_interrupt(u64 unit_address, u64 *reg)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
364*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_GET_FUNCTION_ERR_INT,
365*4882a593Smuzhiyun 0, 0, 0, 0, reg);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /*
369*4882a593Smuzhiyun * cxl_h_ack_fn_error_interrupt - Acknowledge function-wide error data
370*4882a593Smuzhiyun * based on an interrupt
371*4882a593Smuzhiyun * Parameter1 = value to write to the function-wide error interrupt register
372*4882a593Smuzhiyun */
cxl_h_ack_fn_error_interrupt(u64 unit_address,u64 value)373*4882a593Smuzhiyun long cxl_h_ack_fn_error_interrupt(u64 unit_address, u64 value)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
376*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_ACK_FUNCTION_ERR_INT,
377*4882a593Smuzhiyun value, 0, 0, 0,
378*4882a593Smuzhiyun NULL);
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun /*
382*4882a593Smuzhiyun * cxl_h_get_error_log - Retrieve the Platform Log ID (PLID) of
383*4882a593Smuzhiyun * an error log
384*4882a593Smuzhiyun */
cxl_h_get_error_log(u64 unit_address,u64 value)385*4882a593Smuzhiyun long cxl_h_get_error_log(u64 unit_address, u64 value)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun return cxl_h_control_function(unit_address,
388*4882a593Smuzhiyun H_CONTROL_CA_FUNCTION_GET_ERROR_LOG,
389*4882a593Smuzhiyun 0, 0, 0, 0,
390*4882a593Smuzhiyun NULL);
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun /*
394*4882a593Smuzhiyun * cxl_h_collect_int_info - Collect interrupt info about a coherent
395*4882a593Smuzhiyun * platform function after an interrupt occurred.
396*4882a593Smuzhiyun */
cxl_h_collect_int_info(u64 unit_address,u64 process_token,struct cxl_irq_info * info)397*4882a593Smuzhiyun long cxl_h_collect_int_info(u64 unit_address, u64 process_token,
398*4882a593Smuzhiyun struct cxl_irq_info *info)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun long rc;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun BUG_ON(sizeof(*info) != sizeof(unsigned long[PLPAR_HCALL9_BUFSIZE]));
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun rc = plpar_hcall9(H_COLLECT_CA_INT_INFO, (unsigned long *) info,
405*4882a593Smuzhiyun unit_address, process_token);
406*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_collect_int_info(%#.16llx, 0x%llx): %li\n",
407*4882a593Smuzhiyun unit_address, process_token, rc);
408*4882a593Smuzhiyun trace_cxl_hcall_collect_int_info(unit_address, process_token, rc);
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun switch (rc) {
411*4882a593Smuzhiyun case H_SUCCESS: /* The interrupt info is returned in return registers. */
412*4882a593Smuzhiyun pr_devel("dsisr:%#llx, dar:%#llx, dsr:%#llx, pid_tid:%#llx, afu_err:%#llx, errstat:%#llx\n",
413*4882a593Smuzhiyun info->dsisr, info->dar, info->dsr, info->reserved,
414*4882a593Smuzhiyun info->afu_err, info->errstat);
415*4882a593Smuzhiyun return 0;
416*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied. */
417*4882a593Smuzhiyun return -EINVAL;
418*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall. */
419*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the collection of the interrupt info.*/
420*4882a593Smuzhiyun case H_STATE: /* The coherent platform function is not in a valid state to collect interrupt info. */
421*4882a593Smuzhiyun return -EBUSY;
422*4882a593Smuzhiyun default:
423*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
424*4882a593Smuzhiyun return -EINVAL;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /*
429*4882a593Smuzhiyun * cxl_h_control_faults - Control the operation of a coherent platform
430*4882a593Smuzhiyun * function after a fault occurs.
431*4882a593Smuzhiyun *
432*4882a593Smuzhiyun * Parameters
433*4882a593Smuzhiyun * control-mask: value to control the faults
434*4882a593Smuzhiyun * looks like PSL_TFC_An shifted >> 32
435*4882a593Smuzhiyun * reset-mask: mask to control reset of function faults
436*4882a593Smuzhiyun * Set reset_mask = 1 to reset PSL errors
437*4882a593Smuzhiyun */
cxl_h_control_faults(u64 unit_address,u64 process_token,u64 control_mask,u64 reset_mask)438*4882a593Smuzhiyun long cxl_h_control_faults(u64 unit_address, u64 process_token,
439*4882a593Smuzhiyun u64 control_mask, u64 reset_mask)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
442*4882a593Smuzhiyun long rc;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun memset(retbuf, 0, sizeof(retbuf));
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun rc = plpar_hcall(H_CONTROL_CA_FAULTS, retbuf, unit_address,
447*4882a593Smuzhiyun H_CONTROL_CA_FAULTS_RESPOND_PSL, process_token,
448*4882a593Smuzhiyun control_mask, reset_mask);
449*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_control_faults(%#.16llx, 0x%llx, %#llx, %#llx): %li (%#lx)\n",
450*4882a593Smuzhiyun unit_address, process_token, control_mask, reset_mask,
451*4882a593Smuzhiyun rc, retbuf[0]);
452*4882a593Smuzhiyun trace_cxl_hcall_control_faults(unit_address, process_token,
453*4882a593Smuzhiyun control_mask, reset_mask, retbuf[0], rc);
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun switch (rc) {
456*4882a593Smuzhiyun case H_SUCCESS: /* Faults were successfully controlled for the function. */
457*4882a593Smuzhiyun return 0;
458*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied. */
459*4882a593Smuzhiyun return -EINVAL;
460*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the control of faults. */
461*4882a593Smuzhiyun case H_STATE: /* The function was in an invalid state. */
462*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall; the coherent platform facilities may need to be licensed. */
463*4882a593Smuzhiyun return -EBUSY;
464*4882a593Smuzhiyun case H_FUNCTION: /* The function is not supported */
465*4882a593Smuzhiyun case H_NOT_FOUND: /* The operation supplied was not valid */
466*4882a593Smuzhiyun return -EINVAL;
467*4882a593Smuzhiyun default:
468*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
469*4882a593Smuzhiyun return -EINVAL;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun * cxl_h_control_facility - This H_CONTROL_CA_FACILITY hypervisor call
475*4882a593Smuzhiyun * allows the partition to manipulate or query
476*4882a593Smuzhiyun * certain coherent platform facility behaviors.
477*4882a593Smuzhiyun */
cxl_h_control_facility(u64 unit_address,u64 op,u64 p1,u64 p2,u64 p3,u64 p4,u64 * out)478*4882a593Smuzhiyun static long cxl_h_control_facility(u64 unit_address, u64 op,
479*4882a593Smuzhiyun u64 p1, u64 p2, u64 p3, u64 p4, u64 *out)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
482*4882a593Smuzhiyun long rc;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun CXL_H9_WAIT_UNTIL_DONE(rc, retbuf, H_CONTROL_CA_FACILITY, unit_address, op, p1, p2, p3, p4);
485*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_control_facility(%#.16llx, %s(%#llx, %#llx, %#llx, %#llx, R4: %#lx)): %li\n",
486*4882a593Smuzhiyun unit_address, OP_STR_CONTROL_ADAPTER(op), p1, p2, p3, p4, retbuf[0], rc);
487*4882a593Smuzhiyun trace_cxl_hcall_control_facility(unit_address, OP_STR_CONTROL_ADAPTER(op), p1, p2, p3, p4, retbuf[0], rc);
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun switch (rc) {
490*4882a593Smuzhiyun case H_SUCCESS: /* The operation is completed for the coherent platform facility */
491*4882a593Smuzhiyun if (op == H_CONTROL_CA_FACILITY_COLLECT_VPD)
492*4882a593Smuzhiyun *out = retbuf[0];
493*4882a593Smuzhiyun return 0;
494*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied. */
495*4882a593Smuzhiyun case H_FUNCTION: /* The function is not supported. */
496*4882a593Smuzhiyun case H_NOT_FOUND: /* The operation supplied was not valid */
497*4882a593Smuzhiyun case H_NOT_AVAILABLE: /* The operation cannot be performed because the AFU has not been downloaded */
498*4882a593Smuzhiyun case H_SG_LIST: /* An block list entry was invalid */
499*4882a593Smuzhiyun return -EINVAL;
500*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall */
501*4882a593Smuzhiyun case H_RESOURCE: /* The function has page table mappings for MMIO */
502*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the attach operation */
503*4882a593Smuzhiyun case H_STATE: /* The coherent platform facility is not in a valid state */
504*4882a593Smuzhiyun case H_BUSY:
505*4882a593Smuzhiyun return -EBUSY;
506*4882a593Smuzhiyun default:
507*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
508*4882a593Smuzhiyun return -EINVAL;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun /*
513*4882a593Smuzhiyun * cxl_h_reset_adapter - Perform a reset to the coherent platform facility.
514*4882a593Smuzhiyun */
cxl_h_reset_adapter(u64 unit_address)515*4882a593Smuzhiyun long cxl_h_reset_adapter(u64 unit_address)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun return cxl_h_control_facility(unit_address,
518*4882a593Smuzhiyun H_CONTROL_CA_FACILITY_RESET,
519*4882a593Smuzhiyun 0, 0, 0, 0,
520*4882a593Smuzhiyun NULL);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun /*
524*4882a593Smuzhiyun * cxl_h_collect_vpd - Collect VPD for the coherent platform function.
525*4882a593Smuzhiyun * Parameter1 = 4K naturally aligned real buffer containing block
526*4882a593Smuzhiyun * list entries
527*4882a593Smuzhiyun * Parameter2 = number of block list entries in the block list, valid
528*4882a593Smuzhiyun * values are between 0 and 256
529*4882a593Smuzhiyun */
cxl_h_collect_vpd_adapter(u64 unit_address,u64 list_address,u64 num,u64 * out)530*4882a593Smuzhiyun long cxl_h_collect_vpd_adapter(u64 unit_address, u64 list_address,
531*4882a593Smuzhiyun u64 num, u64 *out)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun return cxl_h_control_facility(unit_address,
534*4882a593Smuzhiyun H_CONTROL_CA_FACILITY_COLLECT_VPD,
535*4882a593Smuzhiyun list_address, num, 0, 0,
536*4882a593Smuzhiyun out);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /*
540*4882a593Smuzhiyun * cxl_h_download_facility - This H_DOWNLOAD_CA_FACILITY
541*4882a593Smuzhiyun * hypervisor call provide platform support for
542*4882a593Smuzhiyun * downloading a base adapter image to the coherent
543*4882a593Smuzhiyun * platform facility, and for validating the entire
544*4882a593Smuzhiyun * image after the download.
545*4882a593Smuzhiyun * Parameters
546*4882a593Smuzhiyun * op: operation to perform to the coherent platform function
547*4882a593Smuzhiyun * Download: operation = 1, the base image in the coherent platform
548*4882a593Smuzhiyun * facility is first erased, and then
549*4882a593Smuzhiyun * programmed using the image supplied
550*4882a593Smuzhiyun * in the scatter/gather list.
551*4882a593Smuzhiyun * Validate: operation = 2, the base image in the coherent platform
552*4882a593Smuzhiyun * facility is compared with the image
553*4882a593Smuzhiyun * supplied in the scatter/gather list.
554*4882a593Smuzhiyun * list_address: 4K naturally aligned real buffer containing
555*4882a593Smuzhiyun * scatter/gather list entries.
556*4882a593Smuzhiyun * num: number of block list entries in the scatter/gather list.
557*4882a593Smuzhiyun */
cxl_h_download_facility(u64 unit_address,u64 op,u64 list_address,u64 num,u64 * out)558*4882a593Smuzhiyun static long cxl_h_download_facility(u64 unit_address, u64 op,
559*4882a593Smuzhiyun u64 list_address, u64 num,
560*4882a593Smuzhiyun u64 *out)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
563*4882a593Smuzhiyun unsigned int delay, total_delay = 0;
564*4882a593Smuzhiyun u64 token = 0;
565*4882a593Smuzhiyun long rc;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun if (*out != 0)
568*4882a593Smuzhiyun token = *out;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun memset(retbuf, 0, sizeof(retbuf));
571*4882a593Smuzhiyun while (1) {
572*4882a593Smuzhiyun rc = plpar_hcall(H_DOWNLOAD_CA_FACILITY, retbuf,
573*4882a593Smuzhiyun unit_address, op, list_address, num,
574*4882a593Smuzhiyun token);
575*4882a593Smuzhiyun token = retbuf[0];
576*4882a593Smuzhiyun if (rc != H_BUSY && !H_IS_LONG_BUSY(rc))
577*4882a593Smuzhiyun break;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun if (rc != H_BUSY) {
580*4882a593Smuzhiyun delay = get_longbusy_msecs(rc);
581*4882a593Smuzhiyun total_delay += delay;
582*4882a593Smuzhiyun if (total_delay > CXL_HCALL_TIMEOUT_DOWNLOAD) {
583*4882a593Smuzhiyun WARN(1, "Warning: Giving up waiting for CXL hcall "
584*4882a593Smuzhiyun "%#x after %u msec\n",
585*4882a593Smuzhiyun H_DOWNLOAD_CA_FACILITY, total_delay);
586*4882a593Smuzhiyun rc = H_BUSY;
587*4882a593Smuzhiyun break;
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun msleep(delay);
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun _PRINT_MSG(rc, "cxl_h_download_facility(%#.16llx, %s(%#llx, %#llx), %#lx): %li\n",
593*4882a593Smuzhiyun unit_address, OP_STR_DOWNLOAD_ADAPTER(op), list_address, num, retbuf[0], rc);
594*4882a593Smuzhiyun trace_cxl_hcall_download_facility(unit_address, OP_STR_DOWNLOAD_ADAPTER(op), list_address, num, retbuf[0], rc);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun switch (rc) {
597*4882a593Smuzhiyun case H_SUCCESS: /* The operation is completed for the coherent platform facility */
598*4882a593Smuzhiyun return 0;
599*4882a593Smuzhiyun case H_PARAMETER: /* An incorrect parameter was supplied */
600*4882a593Smuzhiyun case H_FUNCTION: /* The function is not supported. */
601*4882a593Smuzhiyun case H_SG_LIST: /* An block list entry was invalid */
602*4882a593Smuzhiyun case H_BAD_DATA: /* Image verification failed */
603*4882a593Smuzhiyun return -EINVAL;
604*4882a593Smuzhiyun case H_AUTHORITY: /* The partition does not have authority to perform this hcall */
605*4882a593Smuzhiyun case H_RESOURCE: /* The function has page table mappings for MMIO */
606*4882a593Smuzhiyun case H_HARDWARE: /* A hardware event prevented the attach operation */
607*4882a593Smuzhiyun case H_STATE: /* The coherent platform facility is not in a valid state */
608*4882a593Smuzhiyun case H_BUSY:
609*4882a593Smuzhiyun return -EBUSY;
610*4882a593Smuzhiyun case H_CONTINUE:
611*4882a593Smuzhiyun *out = retbuf[0];
612*4882a593Smuzhiyun return 1; /* More data is needed for the complete image */
613*4882a593Smuzhiyun default:
614*4882a593Smuzhiyun WARN(1, "Unexpected return code: %lx", rc);
615*4882a593Smuzhiyun return -EINVAL;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun /*
620*4882a593Smuzhiyun * cxl_h_download_adapter_image - Download the base image to the coherent
621*4882a593Smuzhiyun * platform facility.
622*4882a593Smuzhiyun */
cxl_h_download_adapter_image(u64 unit_address,u64 list_address,u64 num,u64 * out)623*4882a593Smuzhiyun long cxl_h_download_adapter_image(u64 unit_address,
624*4882a593Smuzhiyun u64 list_address, u64 num,
625*4882a593Smuzhiyun u64 *out)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun return cxl_h_download_facility(unit_address,
628*4882a593Smuzhiyun H_DOWNLOAD_CA_FACILITY_DOWNLOAD,
629*4882a593Smuzhiyun list_address, num, out);
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /*
633*4882a593Smuzhiyun * cxl_h_validate_adapter_image - Validate the base image in the coherent
634*4882a593Smuzhiyun * platform facility.
635*4882a593Smuzhiyun */
cxl_h_validate_adapter_image(u64 unit_address,u64 list_address,u64 num,u64 * out)636*4882a593Smuzhiyun long cxl_h_validate_adapter_image(u64 unit_address,
637*4882a593Smuzhiyun u64 list_address, u64 num,
638*4882a593Smuzhiyun u64 *out)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun return cxl_h_download_facility(unit_address,
641*4882a593Smuzhiyun H_DOWNLOAD_CA_FACILITY_VALIDATE,
642*4882a593Smuzhiyun list_address, num, out);
643*4882a593Smuzhiyun }
644