2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/pci.h>
25 #include <linux/acpi.h>
26 #include <linux/slab.h>
27 #include <linux/power_supply.h>
28 #include <linux/pm_runtime.h>
29 #include <acpi/video.h>
31 #include <drm/drm_crtc_helper.h>
33 #include "amdgpu_pm.h"
37 /* Call the ATIF method
40 * amdgpu_atif_call - call an ATIF method
42 * @handle: acpi handle
43 * @function: the ATIF function to execute
44 * @params: ATIF function params
46 * Executes the requested ATIF function (all asics).
47 * Returns a pointer to the acpi output buffer.
49 static union acpi_object *amdgpu_atif_call(acpi_handle handle, int function,
50 struct acpi_buffer *params)
53 union acpi_object atif_arg_elements[2];
54 struct acpi_object_list atif_arg;
55 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
58 atif_arg.pointer = &atif_arg_elements[0];
60 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
61 atif_arg_elements[0].integer.value = function;
64 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
65 atif_arg_elements[1].buffer.length = params->length;
66 atif_arg_elements[1].buffer.pointer = params->pointer;
68 /* We need a second fake parameter */
69 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
70 atif_arg_elements[1].integer.value = 0;
73 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
75 /* Fail only if calling the method fails and ATIF is supported */
76 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
77 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
78 acpi_format_exception(status));
79 kfree(buffer.pointer);
83 return buffer.pointer;
87 * amdgpu_atif_parse_notification - parse supported notifications
89 * @n: supported notifications struct
90 * @mask: supported notifications mask from ATIF
92 * Use the supported notifications mask from ATIF function
93 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
94 * are supported (all asics).
96 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
98 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
99 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
100 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
101 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
102 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
103 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
104 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
105 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
106 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
110 * amdgpu_atif_parse_functions - parse supported functions
112 * @f: supported functions struct
113 * @mask: supported functions mask from ATIF
115 * Use the supported functions mask from ATIF function
116 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
117 * are supported (all asics).
119 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
121 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
122 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
123 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
124 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
125 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
126 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
127 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
128 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
129 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
130 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
134 * amdgpu_atif_verify_interface - verify ATIF
136 * @handle: acpi handle
137 * @atif: amdgpu atif struct
139 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
140 * to initialize ATIF and determine what features are supported
142 * returns 0 on success, error on failure.
144 static int amdgpu_atif_verify_interface(acpi_handle handle,
145 struct amdgpu_atif *atif)
147 union acpi_object *info;
148 struct atif_verify_interface output;
152 info = amdgpu_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
156 memset(&output, 0, sizeof(output));
158 size = *(u16 *) info->buffer.pointer;
160 DRM_INFO("ATIF buffer is too small: %zu\n", size);
164 size = min(sizeof(output), size);
166 memcpy(&output, info->buffer.pointer, size);
168 /* TODO: check version? */
169 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
171 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
172 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
180 * amdgpu_atif_get_notification_params - determine notify configuration
182 * @handle: acpi handle
183 * @n: atif notification configuration struct
185 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
186 * to determine if a notifier is used and if so which one
187 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
188 * where n is specified in the result if a notifier is used.
189 * Returns 0 on success, error on failure.
191 static int amdgpu_atif_get_notification_params(acpi_handle handle,
192 struct amdgpu_atif_notification_cfg *n)
194 union acpi_object *info;
195 struct atif_system_params params;
199 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
205 size = *(u16 *) info->buffer.pointer;
211 memset(¶ms, 0, sizeof(params));
212 size = min(sizeof(params), size);
213 memcpy(¶ms, info->buffer.pointer, size);
215 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
216 params.flags, params.valid_mask);
217 params.flags = params.flags & params.valid_mask;
219 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
222 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
224 n->command_code = 0x81;
231 n->command_code = params.command_code;
235 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
236 (n->enabled ? "enabled" : "disabled"),
243 * amdgpu_atif_get_sbios_requests - get requested sbios event
245 * @handle: acpi handle
246 * @req: atif sbios request struct
248 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
249 * to determine what requests the sbios is making to the driver
251 * Returns 0 on success, error on failure.
253 static int amdgpu_atif_get_sbios_requests(acpi_handle handle,
254 struct atif_sbios_requests *req)
256 union acpi_object *info;
260 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
264 size = *(u16 *)info->buffer.pointer;
269 memset(req, 0, sizeof(*req));
271 size = min(sizeof(*req), size);
272 memcpy(req, info->buffer.pointer, size);
273 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
275 count = hweight32(req->pending);
283 * amdgpu_atif_handler - handle ATIF notify requests
285 * @adev: amdgpu_device pointer
286 * @event: atif sbios request struct
288 * Checks the acpi event and if it matches an atif event,
290 * Returns NOTIFY code
292 static int amdgpu_atif_handler(struct amdgpu_device *adev,
293 struct acpi_bus_event *event)
295 struct amdgpu_atif *atif = &adev->atif;
296 struct atif_sbios_requests req;
300 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
301 event->device_class, event->type);
303 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
306 if (!atif->notification_cfg.enabled ||
307 event->type != atif->notification_cfg.command_code)
311 /* Check pending SBIOS requests */
312 handle = ACPI_HANDLE(&adev->pdev->dev);
313 count = amdgpu_atif_get_sbios_requests(handle, &req);
318 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
320 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
321 struct amdgpu_encoder *enc = atif->encoder_for_bl;
324 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
326 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
327 req.backlight_level);
329 amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
331 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
332 backlight_force_update(dig->bl_dev,
333 BACKLIGHT_UPDATE_HOTKEY);
337 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
338 if ((adev->flags & AMD_IS_PX) &&
339 amdgpu_atpx_dgpu_req_power_for_displays()) {
340 pm_runtime_get_sync(adev->ddev->dev);
341 /* Just fire off a uevent and let userspace tell us what to do */
342 drm_helper_hpd_irq_event(adev->ddev);
343 pm_runtime_mark_last_busy(adev->ddev->dev);
344 pm_runtime_put_autosuspend(adev->ddev->dev);
347 /* TODO: check other events */
349 /* We've handled the event, stop the notifier chain. The ACPI interface
350 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
351 * userspace if the event was generated only to signal a SBIOS
357 /* Call the ATCS method
360 * amdgpu_atcs_call - call an ATCS method
362 * @handle: acpi handle
363 * @function: the ATCS function to execute
364 * @params: ATCS function params
366 * Executes the requested ATCS function (all asics).
367 * Returns a pointer to the acpi output buffer.
369 static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
370 struct acpi_buffer *params)
373 union acpi_object atcs_arg_elements[2];
374 struct acpi_object_list atcs_arg;
375 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
378 atcs_arg.pointer = &atcs_arg_elements[0];
380 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
381 atcs_arg_elements[0].integer.value = function;
384 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
385 atcs_arg_elements[1].buffer.length = params->length;
386 atcs_arg_elements[1].buffer.pointer = params->pointer;
388 /* We need a second fake parameter */
389 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
390 atcs_arg_elements[1].integer.value = 0;
393 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
395 /* Fail only if calling the method fails and ATIF is supported */
396 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
397 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
398 acpi_format_exception(status));
399 kfree(buffer.pointer);
403 return buffer.pointer;
407 * amdgpu_atcs_parse_functions - parse supported functions
409 * @f: supported functions struct
410 * @mask: supported functions mask from ATCS
412 * Use the supported functions mask from ATCS function
413 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
414 * are supported (all asics).
416 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
418 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
419 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
420 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
421 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
425 * amdgpu_atcs_verify_interface - verify ATCS
427 * @handle: acpi handle
428 * @atcs: amdgpu atcs struct
430 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
431 * to initialize ATCS and determine what features are supported
433 * returns 0 on success, error on failure.
435 static int amdgpu_atcs_verify_interface(acpi_handle handle,
436 struct amdgpu_atcs *atcs)
438 union acpi_object *info;
439 struct atcs_verify_interface output;
443 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
447 memset(&output, 0, sizeof(output));
449 size = *(u16 *) info->buffer.pointer;
451 DRM_INFO("ATCS buffer is too small: %zu\n", size);
455 size = min(sizeof(output), size);
457 memcpy(&output, info->buffer.pointer, size);
459 /* TODO: check version? */
460 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
462 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
470 * amdgpu_acpi_is_pcie_performance_request_supported
472 * @adev: amdgpu_device pointer
474 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
475 * are supported (all asics).
476 * returns true if supported, false if not.
478 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
480 struct amdgpu_atcs *atcs = &adev->atcs;
482 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
489 * amdgpu_acpi_pcie_notify_device_ready
491 * @adev: amdgpu_device pointer
493 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
495 * returns 0 on success, error on failure.
497 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
500 union acpi_object *info;
501 struct amdgpu_atcs *atcs = &adev->atcs;
503 /* Get the device handle */
504 handle = ACPI_HANDLE(&adev->pdev->dev);
508 if (!atcs->functions.pcie_dev_rdy)
511 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
521 * amdgpu_acpi_pcie_performance_request
523 * @adev: amdgpu_device pointer
524 * @perf_req: requested perf level (pcie gen speed)
525 * @advertise: set advertise caps flag if set
527 * Executes the PCIE_PERFORMANCE_REQUEST method to
528 * change the pcie gen speed (all asics).
529 * returns 0 on success, error on failure.
531 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
532 u8 perf_req, bool advertise)
535 union acpi_object *info;
536 struct amdgpu_atcs *atcs = &adev->atcs;
537 struct atcs_pref_req_input atcs_input;
538 struct atcs_pref_req_output atcs_output;
539 struct acpi_buffer params;
543 if (amdgpu_acpi_pcie_notify_device_ready(adev))
546 /* Get the device handle */
547 handle = ACPI_HANDLE(&adev->pdev->dev);
551 if (!atcs->functions.pcie_perf_req)
554 atcs_input.size = sizeof(struct atcs_pref_req_input);
555 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
556 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
557 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
558 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
560 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
561 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
562 atcs_input.perf_req = perf_req;
564 params.length = sizeof(struct atcs_pref_req_input);
565 params.pointer = &atcs_input;
568 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
572 memset(&atcs_output, 0, sizeof(atcs_output));
574 size = *(u16 *) info->buffer.pointer;
576 DRM_INFO("ATCS buffer is too small: %zu\n", size);
580 size = min(sizeof(atcs_output), size);
582 memcpy(&atcs_output, info->buffer.pointer, size);
586 switch (atcs_output.ret_val) {
587 case ATCS_REQUEST_REFUSED:
590 case ATCS_REQUEST_COMPLETE:
592 case ATCS_REQUEST_IN_PROGRESS:
602 * amdgpu_acpi_event - handle notify events
604 * @nb: notifier block
608 * Calls relevant amdgpu functions in response to various
610 * Returns NOTIFY code
612 static int amdgpu_acpi_event(struct notifier_block *nb,
616 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
617 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
619 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
620 if (power_supply_is_system_supplied() > 0)
621 DRM_DEBUG_DRIVER("pm: AC\n");
623 DRM_DEBUG_DRIVER("pm: DC\n");
625 amdgpu_pm_acpi_event_handler(adev);
628 /* Check for pending SBIOS requests */
629 return amdgpu_atif_handler(adev, entry);
632 /* Call all ACPI methods here */
634 * amdgpu_acpi_init - init driver acpi support
636 * @adev: amdgpu_device pointer
638 * Verifies the AMD ACPI interfaces and registers with the acpi
639 * notifier chain (all asics).
640 * Returns 0 on success, error on failure.
642 int amdgpu_acpi_init(struct amdgpu_device *adev)
645 struct amdgpu_atif *atif = &adev->atif;
646 struct amdgpu_atcs *atcs = &adev->atcs;
649 /* Get the device handle */
650 handle = ACPI_HANDLE(&adev->pdev->dev);
652 if (!adev->bios || !handle)
655 /* Call the ATCS method */
656 ret = amdgpu_atcs_verify_interface(handle, atcs);
658 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
661 /* Call the ATIF method */
662 ret = amdgpu_atif_verify_interface(handle, atif);
664 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
668 if (atif->notifications.brightness_change) {
669 struct drm_encoder *tmp;
671 /* Find the encoder controlling the brightness */
672 list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
674 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
676 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
678 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
680 atif->encoder_for_bl = enc;
687 if (atif->functions.sbios_requests && !atif->functions.system_params) {
688 /* XXX check this workraround, if sbios request function is
689 * present we have to see how it's configured in the system
692 atif->functions.system_params = true;
695 if (atif->functions.system_params) {
696 ret = amdgpu_atif_get_notification_params(handle,
697 &atif->notification_cfg);
699 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
701 /* Disable notification */
702 atif->notification_cfg.enabled = false;
707 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
708 register_acpi_notifier(&adev->acpi_nb);
714 * amdgpu_acpi_fini - tear down driver acpi support
716 * @adev: amdgpu_device pointer
718 * Unregisters with the acpi notifier chain (all asics).
720 void amdgpu_acpi_fini(struct amdgpu_device *adev)
722 unregister_acpi_notifier(&adev->acpi_nb);