Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upstream-linus
[sfrench/cifs-2.6.git] / drivers / gpu / drm / amd / powerplay / hwmgr / pp_acpi.c
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include "hwmgr.h"
27 #include "amd_acpi.h"
28 #include "pp_acpi.h"
29
30 bool acpi_atcs_functions_supported(void *device, uint32_t index)
31 {
32         int32_t result;
33         struct atcs_verify_interface output_buf = {0};
34
35         int32_t temp_buffer = 1;
36
37         result = cgs_call_acpi_method(device, CGS_ACPI_METHOD_ATCS,
38                                                 ATCS_FUNCTION_VERIFY_INTERFACE,
39                                                 &temp_buffer,
40                                                 &output_buf,
41                                                 1,
42                                                 sizeof(temp_buffer),
43                                                 sizeof(output_buf));
44
45         return result == 0 ? (output_buf.function_bits & (1 << (index - 1))) != 0 : false;
46 }
47
48 bool acpi_atcs_notify_pcie_device_ready(void *device)
49 {
50         int32_t temp_buffer = 1;
51
52         return cgs_call_acpi_method(device, CGS_ACPI_METHOD_ATCS,
53                                 ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION,
54                                                 &temp_buffer,
55                                                 NULL,
56                                                 0,
57                                                 sizeof(temp_buffer),
58                                                 0);
59 }
60
61
62 int acpi_pcie_perf_request(void *device, uint8_t perf_req, bool advertise)
63 {
64         struct atcs_pref_req_input atcs_input;
65         struct atcs_pref_req_output atcs_output;
66         u32 retry = 3;
67         int result;
68         struct cgs_system_info info = {0};
69
70         if (acpi_atcs_notify_pcie_device_ready(device))
71                 return -EINVAL;
72
73         info.size = sizeof(struct cgs_system_info);
74         info.info_id = CGS_SYSTEM_INFO_ADAPTER_BDF_ID;
75         result = cgs_query_system_info(device, &info);
76         if (result != 0)
77                 return -EINVAL;
78         atcs_input.client_id = (uint16_t)info.value;
79         atcs_input.size = sizeof(struct atcs_pref_req_input);
80         atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
81         atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
82         if (advertise)
83                 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
84         atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
85         atcs_input.perf_req = perf_req;
86
87         atcs_output.size = sizeof(struct atcs_pref_req_input);
88
89         while (retry--) {
90                 result = cgs_call_acpi_method(device,
91                                                 CGS_ACPI_METHOD_ATCS,
92                                                 ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST,
93                                                 &atcs_input,
94                                                 &atcs_output,
95                                                 1,
96                                                 sizeof(atcs_input),
97                                                 sizeof(atcs_output));
98                 if (result != 0)
99                         return -EIO;
100
101                 switch (atcs_output.ret_val) {
102                 case ATCS_REQUEST_REFUSED:
103                 default:
104                         return -EINVAL;
105                 case ATCS_REQUEST_COMPLETE:
106                         return 0;
107                 case ATCS_REQUEST_IN_PROGRESS:
108                         udelay(10);
109                         break;
110                 }
111         }
112
113         return 0;
114 }