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 struct amdgpu_atif_notification_cfg {
42 struct amdgpu_atif_notifications {
44 bool expansion_mode_change;
46 bool forced_power_state;
47 bool system_power_state;
48 bool display_conf_change;
50 bool brightness_change;
51 bool dgpu_display_event;
54 struct amdgpu_atif_functions {
57 bool select_active_disp;
61 bool get_panel_expansion_mode;
62 bool set_panel_expansion_mode;
63 bool temperature_change;
64 bool graphics_device_types;
68 struct amdgpu_atif_notifications notifications;
69 struct amdgpu_atif_functions functions;
70 struct amdgpu_atif_notification_cfg notification_cfg;
71 struct amdgpu_encoder *encoder_for_bl;
74 /* Call the ATIF method
77 * amdgpu_atif_call - call an ATIF method
79 * @handle: acpi handle
80 * @function: the ATIF function to execute
81 * @params: ATIF function params
83 * Executes the requested ATIF function (all asics).
84 * Returns a pointer to the acpi output buffer.
86 static union acpi_object *amdgpu_atif_call(acpi_handle handle, int function,
87 struct acpi_buffer *params)
90 union acpi_object atif_arg_elements[2];
91 struct acpi_object_list atif_arg;
92 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
95 atif_arg.pointer = &atif_arg_elements[0];
97 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
98 atif_arg_elements[0].integer.value = function;
101 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
102 atif_arg_elements[1].buffer.length = params->length;
103 atif_arg_elements[1].buffer.pointer = params->pointer;
105 /* We need a second fake parameter */
106 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
107 atif_arg_elements[1].integer.value = 0;
110 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
112 /* Fail only if calling the method fails and ATIF is supported */
113 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
114 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
115 acpi_format_exception(status));
116 kfree(buffer.pointer);
120 return buffer.pointer;
124 * amdgpu_atif_parse_notification - parse supported notifications
126 * @n: supported notifications struct
127 * @mask: supported notifications mask from ATIF
129 * Use the supported notifications mask from ATIF function
130 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
131 * are supported (all asics).
133 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
135 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
136 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
137 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
138 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
139 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
140 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
141 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
142 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
143 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
147 * amdgpu_atif_parse_functions - parse supported functions
149 * @f: supported functions struct
150 * @mask: supported functions mask from ATIF
152 * Use the supported functions mask from ATIF function
153 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
154 * are supported (all asics).
156 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
158 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
159 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
160 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
161 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
162 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
163 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
164 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
165 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
166 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
167 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
171 * amdgpu_atif_verify_interface - verify ATIF
173 * @handle: acpi handle
174 * @atif: amdgpu atif struct
176 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
177 * to initialize ATIF and determine what features are supported
179 * returns 0 on success, error on failure.
181 static int amdgpu_atif_verify_interface(acpi_handle handle,
182 struct amdgpu_atif *atif)
184 union acpi_object *info;
185 struct atif_verify_interface output;
189 info = amdgpu_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
193 memset(&output, 0, sizeof(output));
195 size = *(u16 *) info->buffer.pointer;
197 DRM_INFO("ATIF buffer is too small: %zu\n", size);
201 size = min(sizeof(output), size);
203 memcpy(&output, info->buffer.pointer, size);
205 /* TODO: check version? */
206 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
208 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
209 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
217 * amdgpu_atif_get_notification_params - determine notify configuration
219 * @handle: acpi handle
220 * @n: atif notification configuration struct
222 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
223 * to determine if a notifier is used and if so which one
224 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
225 * where n is specified in the result if a notifier is used.
226 * Returns 0 on success, error on failure.
228 static int amdgpu_atif_get_notification_params(acpi_handle handle,
229 struct amdgpu_atif_notification_cfg *n)
231 union acpi_object *info;
232 struct atif_system_params params;
236 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
242 size = *(u16 *) info->buffer.pointer;
248 memset(¶ms, 0, sizeof(params));
249 size = min(sizeof(params), size);
250 memcpy(¶ms, info->buffer.pointer, size);
252 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
253 params.flags, params.valid_mask);
254 params.flags = params.flags & params.valid_mask;
256 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
259 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
261 n->command_code = 0x81;
268 n->command_code = params.command_code;
272 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
273 (n->enabled ? "enabled" : "disabled"),
280 * amdgpu_atif_get_sbios_requests - get requested sbios event
282 * @handle: acpi handle
283 * @req: atif sbios request struct
285 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
286 * to determine what requests the sbios is making to the driver
288 * Returns 0 on success, error on failure.
290 static int amdgpu_atif_get_sbios_requests(acpi_handle handle,
291 struct atif_sbios_requests *req)
293 union acpi_object *info;
297 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
301 size = *(u16 *)info->buffer.pointer;
306 memset(req, 0, sizeof(*req));
308 size = min(sizeof(*req), size);
309 memcpy(req, info->buffer.pointer, size);
310 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
312 count = hweight32(req->pending);
320 * amdgpu_atif_handler - handle ATIF notify requests
322 * @adev: amdgpu_device pointer
323 * @event: atif sbios request struct
325 * Checks the acpi event and if it matches an atif event,
327 * Returns NOTIFY code
329 static int amdgpu_atif_handler(struct amdgpu_device *adev,
330 struct acpi_bus_event *event)
332 struct amdgpu_atif *atif = adev->atif;
333 struct atif_sbios_requests req;
337 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
338 event->device_class, event->type);
340 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
344 !atif->notification_cfg.enabled ||
345 event->type != atif->notification_cfg.command_code)
349 /* Check pending SBIOS requests */
350 handle = ACPI_HANDLE(&adev->pdev->dev);
351 count = amdgpu_atif_get_sbios_requests(handle, &req);
356 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
358 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
359 struct amdgpu_encoder *enc = atif->encoder_for_bl;
362 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
364 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
365 req.backlight_level);
367 amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
369 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
370 backlight_force_update(dig->bl_dev,
371 BACKLIGHT_UPDATE_HOTKEY);
375 if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
376 if ((adev->flags & AMD_IS_PX) &&
377 amdgpu_atpx_dgpu_req_power_for_displays()) {
378 pm_runtime_get_sync(adev->ddev->dev);
379 /* Just fire off a uevent and let userspace tell us what to do */
380 drm_helper_hpd_irq_event(adev->ddev);
381 pm_runtime_mark_last_busy(adev->ddev->dev);
382 pm_runtime_put_autosuspend(adev->ddev->dev);
385 /* TODO: check other events */
387 /* We've handled the event, stop the notifier chain. The ACPI interface
388 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
389 * userspace if the event was generated only to signal a SBIOS
395 /* Call the ATCS method
398 * amdgpu_atcs_call - call an ATCS method
400 * @handle: acpi handle
401 * @function: the ATCS function to execute
402 * @params: ATCS function params
404 * Executes the requested ATCS function (all asics).
405 * Returns a pointer to the acpi output buffer.
407 static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
408 struct acpi_buffer *params)
411 union acpi_object atcs_arg_elements[2];
412 struct acpi_object_list atcs_arg;
413 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
416 atcs_arg.pointer = &atcs_arg_elements[0];
418 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
419 atcs_arg_elements[0].integer.value = function;
422 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
423 atcs_arg_elements[1].buffer.length = params->length;
424 atcs_arg_elements[1].buffer.pointer = params->pointer;
426 /* We need a second fake parameter */
427 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
428 atcs_arg_elements[1].integer.value = 0;
431 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
433 /* Fail only if calling the method fails and ATIF is supported */
434 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
435 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
436 acpi_format_exception(status));
437 kfree(buffer.pointer);
441 return buffer.pointer;
445 * amdgpu_atcs_parse_functions - parse supported functions
447 * @f: supported functions struct
448 * @mask: supported functions mask from ATCS
450 * Use the supported functions mask from ATCS function
451 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
452 * are supported (all asics).
454 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
456 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
457 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
458 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
459 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
463 * amdgpu_atcs_verify_interface - verify ATCS
465 * @handle: acpi handle
466 * @atcs: amdgpu atcs struct
468 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
469 * to initialize ATCS and determine what features are supported
471 * returns 0 on success, error on failure.
473 static int amdgpu_atcs_verify_interface(acpi_handle handle,
474 struct amdgpu_atcs *atcs)
476 union acpi_object *info;
477 struct atcs_verify_interface output;
481 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
485 memset(&output, 0, sizeof(output));
487 size = *(u16 *) info->buffer.pointer;
489 DRM_INFO("ATCS buffer is too small: %zu\n", size);
493 size = min(sizeof(output), size);
495 memcpy(&output, info->buffer.pointer, size);
497 /* TODO: check version? */
498 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
500 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
508 * amdgpu_acpi_is_pcie_performance_request_supported
510 * @adev: amdgpu_device pointer
512 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
513 * are supported (all asics).
514 * returns true if supported, false if not.
516 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
518 struct amdgpu_atcs *atcs = &adev->atcs;
520 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
527 * amdgpu_acpi_pcie_notify_device_ready
529 * @adev: amdgpu_device pointer
531 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
533 * returns 0 on success, error on failure.
535 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
538 union acpi_object *info;
539 struct amdgpu_atcs *atcs = &adev->atcs;
541 /* Get the device handle */
542 handle = ACPI_HANDLE(&adev->pdev->dev);
546 if (!atcs->functions.pcie_dev_rdy)
549 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
559 * amdgpu_acpi_pcie_performance_request
561 * @adev: amdgpu_device pointer
562 * @perf_req: requested perf level (pcie gen speed)
563 * @advertise: set advertise caps flag if set
565 * Executes the PCIE_PERFORMANCE_REQUEST method to
566 * change the pcie gen speed (all asics).
567 * returns 0 on success, error on failure.
569 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
570 u8 perf_req, bool advertise)
573 union acpi_object *info;
574 struct amdgpu_atcs *atcs = &adev->atcs;
575 struct atcs_pref_req_input atcs_input;
576 struct atcs_pref_req_output atcs_output;
577 struct acpi_buffer params;
581 if (amdgpu_acpi_pcie_notify_device_ready(adev))
584 /* Get the device handle */
585 handle = ACPI_HANDLE(&adev->pdev->dev);
589 if (!atcs->functions.pcie_perf_req)
592 atcs_input.size = sizeof(struct atcs_pref_req_input);
593 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
594 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
595 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
596 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
598 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
599 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
600 atcs_input.perf_req = perf_req;
602 params.length = sizeof(struct atcs_pref_req_input);
603 params.pointer = &atcs_input;
606 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
610 memset(&atcs_output, 0, sizeof(atcs_output));
612 size = *(u16 *) info->buffer.pointer;
614 DRM_INFO("ATCS buffer is too small: %zu\n", size);
618 size = min(sizeof(atcs_output), size);
620 memcpy(&atcs_output, info->buffer.pointer, size);
624 switch (atcs_output.ret_val) {
625 case ATCS_REQUEST_REFUSED:
628 case ATCS_REQUEST_COMPLETE:
630 case ATCS_REQUEST_IN_PROGRESS:
640 * amdgpu_acpi_event - handle notify events
642 * @nb: notifier block
646 * Calls relevant amdgpu functions in response to various
648 * Returns NOTIFY code
650 static int amdgpu_acpi_event(struct notifier_block *nb,
654 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
655 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
657 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
658 if (power_supply_is_system_supplied() > 0)
659 DRM_DEBUG_DRIVER("pm: AC\n");
661 DRM_DEBUG_DRIVER("pm: DC\n");
663 amdgpu_pm_acpi_event_handler(adev);
666 /* Check for pending SBIOS requests */
667 return amdgpu_atif_handler(adev, entry);
670 /* Call all ACPI methods here */
672 * amdgpu_acpi_init - init driver acpi support
674 * @adev: amdgpu_device pointer
676 * Verifies the AMD ACPI interfaces and registers with the acpi
677 * notifier chain (all asics).
678 * Returns 0 on success, error on failure.
680 int amdgpu_acpi_init(struct amdgpu_device *adev)
683 struct amdgpu_atif *atif;
684 struct amdgpu_atcs *atcs = &adev->atcs;
687 /* Get the device handle */
688 handle = ACPI_HANDLE(&adev->pdev->dev);
690 if (!adev->bios || !handle)
693 /* Call the ATCS method */
694 ret = amdgpu_atcs_verify_interface(handle, atcs);
696 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
699 /* Call the ATIF method */
700 atif = kzalloc(sizeof(*atif), GFP_KERNEL);
702 DRM_WARN("Not enough memory to initialize ATIF\n");
706 ret = amdgpu_atif_verify_interface(handle, atif);
708 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
714 if (atif->notifications.brightness_change) {
715 struct drm_encoder *tmp;
717 /* Find the encoder controlling the brightness */
718 list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
720 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
722 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
724 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
726 atif->encoder_for_bl = enc;
733 if (atif->functions.sbios_requests && !atif->functions.system_params) {
734 /* XXX check this workraround, if sbios request function is
735 * present we have to see how it's configured in the system
738 atif->functions.system_params = true;
741 if (atif->functions.system_params) {
742 ret = amdgpu_atif_get_notification_params(handle,
743 &atif->notification_cfg);
745 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
747 /* Disable notification */
748 atif->notification_cfg.enabled = false;
753 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
754 register_acpi_notifier(&adev->acpi_nb);
760 * amdgpu_acpi_fini - tear down driver acpi support
762 * @adev: amdgpu_device pointer
764 * Unregisters with the acpi notifier chain (all asics).
766 void amdgpu_acpi_fini(struct amdgpu_device *adev)
768 unregister_acpi_notifier(&adev->acpi_nb);