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 <acpi/video.h>
30 #include <drm/drm_crtc_helper.h>
35 extern void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev);
36 /* Call the ATIF method
39 * amdgpu_atif_call - call an ATIF method
41 * @handle: acpi handle
42 * @function: the ATIF function to execute
43 * @params: ATIF function params
45 * Executes the requested ATIF function (all asics).
46 * Returns a pointer to the acpi output buffer.
48 static union acpi_object *amdgpu_atif_call(acpi_handle handle, int function,
49 struct acpi_buffer *params)
52 union acpi_object atif_arg_elements[2];
53 struct acpi_object_list atif_arg;
54 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
57 atif_arg.pointer = &atif_arg_elements[0];
59 atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
60 atif_arg_elements[0].integer.value = function;
63 atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
64 atif_arg_elements[1].buffer.length = params->length;
65 atif_arg_elements[1].buffer.pointer = params->pointer;
67 /* We need a second fake parameter */
68 atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
69 atif_arg_elements[1].integer.value = 0;
72 status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
74 /* Fail only if calling the method fails and ATIF is supported */
75 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
76 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
77 acpi_format_exception(status));
78 kfree(buffer.pointer);
82 return buffer.pointer;
86 * amdgpu_atif_parse_notification - parse supported notifications
88 * @n: supported notifications struct
89 * @mask: supported notifications mask from ATIF
91 * Use the supported notifications mask from ATIF function
92 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
93 * are supported (all asics).
95 static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
97 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
98 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
99 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
100 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
101 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
102 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
103 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
104 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
105 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
109 * amdgpu_atif_parse_functions - parse supported functions
111 * @f: supported functions struct
112 * @mask: supported functions mask from ATIF
114 * Use the supported functions mask from ATIF function
115 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
116 * are supported (all asics).
118 static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
120 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
121 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
122 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
123 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
124 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
125 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
126 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
127 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
128 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
129 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
133 * amdgpu_atif_verify_interface - verify ATIF
135 * @handle: acpi handle
136 * @atif: amdgpu atif struct
138 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
139 * to initialize ATIF and determine what features are supported
141 * returns 0 on success, error on failure.
143 static int amdgpu_atif_verify_interface(acpi_handle handle,
144 struct amdgpu_atif *atif)
146 union acpi_object *info;
147 struct atif_verify_interface output;
151 info = amdgpu_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
155 memset(&output, 0, sizeof(output));
157 size = *(u16 *) info->buffer.pointer;
159 DRM_INFO("ATIF buffer is too small: %zu\n", size);
163 size = min(sizeof(output), size);
165 memcpy(&output, info->buffer.pointer, size);
167 /* TODO: check version? */
168 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
170 amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
171 amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
179 * amdgpu_atif_get_notification_params - determine notify configuration
181 * @handle: acpi handle
182 * @n: atif notification configuration struct
184 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
185 * to determine if a notifier is used and if so which one
186 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
187 * where n is specified in the result if a notifier is used.
188 * Returns 0 on success, error on failure.
190 static int amdgpu_atif_get_notification_params(acpi_handle handle,
191 struct amdgpu_atif_notification_cfg *n)
193 union acpi_object *info;
194 struct atif_system_params params;
198 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
204 size = *(u16 *) info->buffer.pointer;
210 memset(¶ms, 0, sizeof(params));
211 size = min(sizeof(params), size);
212 memcpy(¶ms, info->buffer.pointer, size);
214 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
215 params.flags, params.valid_mask);
216 params.flags = params.flags & params.valid_mask;
218 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
221 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
223 n->command_code = 0x81;
230 n->command_code = params.command_code;
234 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
235 (n->enabled ? "enabled" : "disabled"),
242 * amdgpu_atif_get_sbios_requests - get requested sbios event
244 * @handle: acpi handle
245 * @req: atif sbios request struct
247 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
248 * to determine what requests the sbios is making to the driver
250 * Returns 0 on success, error on failure.
252 static int amdgpu_atif_get_sbios_requests(acpi_handle handle,
253 struct atif_sbios_requests *req)
255 union acpi_object *info;
259 info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
263 size = *(u16 *)info->buffer.pointer;
268 memset(req, 0, sizeof(*req));
270 size = min(sizeof(*req), size);
271 memcpy(req, info->buffer.pointer, size);
272 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
274 count = hweight32(req->pending);
282 * amdgpu_atif_handler - handle ATIF notify requests
284 * @adev: amdgpu_device pointer
285 * @event: atif sbios request struct
287 * Checks the acpi event and if it matches an atif event,
289 * Returns NOTIFY code
291 int amdgpu_atif_handler(struct amdgpu_device *adev,
292 struct acpi_bus_event *event)
294 struct amdgpu_atif *atif = &adev->atif;
295 struct atif_sbios_requests req;
299 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
300 event->device_class, event->type);
302 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
305 if (!atif->notification_cfg.enabled ||
306 event->type != atif->notification_cfg.command_code)
310 /* Check pending SBIOS requests */
311 handle = ACPI_HANDLE(&adev->pdev->dev);
312 count = amdgpu_atif_get_sbios_requests(handle, &req);
317 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
319 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
320 struct amdgpu_encoder *enc = atif->encoder_for_bl;
323 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
325 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
326 req.backlight_level);
328 amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
330 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
331 backlight_force_update(dig->bl_dev,
332 BACKLIGHT_UPDATE_HOTKEY);
336 /* TODO: check other events */
338 /* We've handled the event, stop the notifier chain. The ACPI interface
339 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
340 * userspace if the event was generated only to signal a SBIOS
346 /* Call the ATCS method
349 * amdgpu_atcs_call - call an ATCS method
351 * @handle: acpi handle
352 * @function: the ATCS function to execute
353 * @params: ATCS function params
355 * Executes the requested ATCS function (all asics).
356 * Returns a pointer to the acpi output buffer.
358 static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
359 struct acpi_buffer *params)
362 union acpi_object atcs_arg_elements[2];
363 struct acpi_object_list atcs_arg;
364 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
367 atcs_arg.pointer = &atcs_arg_elements[0];
369 atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
370 atcs_arg_elements[0].integer.value = function;
373 atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
374 atcs_arg_elements[1].buffer.length = params->length;
375 atcs_arg_elements[1].buffer.pointer = params->pointer;
377 /* We need a second fake parameter */
378 atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
379 atcs_arg_elements[1].integer.value = 0;
382 status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
384 /* Fail only if calling the method fails and ATIF is supported */
385 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
386 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
387 acpi_format_exception(status));
388 kfree(buffer.pointer);
392 return buffer.pointer;
396 * amdgpu_atcs_parse_functions - parse supported functions
398 * @f: supported functions struct
399 * @mask: supported functions mask from ATCS
401 * Use the supported functions mask from ATCS function
402 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
403 * are supported (all asics).
405 static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
407 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
408 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
409 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
410 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
414 * amdgpu_atcs_verify_interface - verify ATCS
416 * @handle: acpi handle
417 * @atcs: amdgpu atcs struct
419 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
420 * to initialize ATCS and determine what features are supported
422 * returns 0 on success, error on failure.
424 static int amdgpu_atcs_verify_interface(acpi_handle handle,
425 struct amdgpu_atcs *atcs)
427 union acpi_object *info;
428 struct atcs_verify_interface output;
432 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
436 memset(&output, 0, sizeof(output));
438 size = *(u16 *) info->buffer.pointer;
440 DRM_INFO("ATCS buffer is too small: %zu\n", size);
444 size = min(sizeof(output), size);
446 memcpy(&output, info->buffer.pointer, size);
448 /* TODO: check version? */
449 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
451 amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
459 * amdgpu_acpi_is_pcie_performance_request_supported
461 * @adev: amdgpu_device pointer
463 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
464 * are supported (all asics).
465 * returns true if supported, false if not.
467 bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
469 struct amdgpu_atcs *atcs = &adev->atcs;
471 if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
478 * amdgpu_acpi_pcie_notify_device_ready
480 * @adev: amdgpu_device pointer
482 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
484 * returns 0 on success, error on failure.
486 int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
489 union acpi_object *info;
490 struct amdgpu_atcs *atcs = &adev->atcs;
492 /* Get the device handle */
493 handle = ACPI_HANDLE(&adev->pdev->dev);
497 if (!atcs->functions.pcie_dev_rdy)
500 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
510 * amdgpu_acpi_pcie_performance_request
512 * @adev: amdgpu_device pointer
513 * @perf_req: requested perf level (pcie gen speed)
514 * @advertise: set advertise caps flag if set
516 * Executes the PCIE_PERFORMANCE_REQUEST method to
517 * change the pcie gen speed (all asics).
518 * returns 0 on success, error on failure.
520 int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
521 u8 perf_req, bool advertise)
524 union acpi_object *info;
525 struct amdgpu_atcs *atcs = &adev->atcs;
526 struct atcs_pref_req_input atcs_input;
527 struct atcs_pref_req_output atcs_output;
528 struct acpi_buffer params;
532 /* Get the device handle */
533 handle = ACPI_HANDLE(&adev->pdev->dev);
537 if (!atcs->functions.pcie_perf_req)
540 atcs_input.size = sizeof(struct atcs_pref_req_input);
541 /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
542 atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
543 atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
544 atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
546 atcs_input.flags |= ATCS_ADVERTISE_CAPS;
547 atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
548 atcs_input.perf_req = perf_req;
550 params.length = sizeof(struct atcs_pref_req_input);
551 params.pointer = &atcs_input;
554 info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, ¶ms);
558 memset(&atcs_output, 0, sizeof(atcs_output));
560 size = *(u16 *) info->buffer.pointer;
562 DRM_INFO("ATCS buffer is too small: %zu\n", size);
566 size = min(sizeof(atcs_output), size);
568 memcpy(&atcs_output, info->buffer.pointer, size);
572 switch (atcs_output.ret_val) {
573 case ATCS_REQUEST_REFUSED:
576 case ATCS_REQUEST_COMPLETE:
578 case ATCS_REQUEST_IN_PROGRESS:
588 * amdgpu_acpi_event - handle notify events
590 * @nb: notifier block
594 * Calls relevant amdgpu functions in response to various
596 * Returns NOTIFY code
598 static int amdgpu_acpi_event(struct notifier_block *nb,
602 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
603 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
605 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
606 if (power_supply_is_system_supplied() > 0)
607 DRM_DEBUG_DRIVER("pm: AC\n");
609 DRM_DEBUG_DRIVER("pm: DC\n");
611 amdgpu_pm_acpi_event_handler(adev);
614 /* Check for pending SBIOS requests */
615 return amdgpu_atif_handler(adev, entry);
618 /* Call all ACPI methods here */
620 * amdgpu_acpi_init - init driver acpi support
622 * @adev: amdgpu_device pointer
624 * Verifies the AMD ACPI interfaces and registers with the acpi
625 * notifier chain (all asics).
626 * Returns 0 on success, error on failure.
628 int amdgpu_acpi_init(struct amdgpu_device *adev)
631 struct amdgpu_atif *atif = &adev->atif;
632 struct amdgpu_atcs *atcs = &adev->atcs;
635 /* Get the device handle */
636 handle = ACPI_HANDLE(&adev->pdev->dev);
638 if (!adev->bios || !handle)
641 /* Call the ATCS method */
642 ret = amdgpu_atcs_verify_interface(handle, atcs);
644 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
647 /* Call the ATIF method */
648 ret = amdgpu_atif_verify_interface(handle, atif);
650 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
654 if (atif->notifications.brightness_change) {
655 struct drm_encoder *tmp;
657 /* Find the encoder controlling the brightness */
658 list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
660 struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
662 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
664 if (adev->is_atom_bios) {
665 struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
667 atif->encoder_for_bl = enc;
675 if (atif->functions.sbios_requests && !atif->functions.system_params) {
676 /* XXX check this workraround, if sbios request function is
677 * present we have to see how it's configured in the system
680 atif->functions.system_params = true;
683 if (atif->functions.system_params) {
684 ret = amdgpu_atif_get_notification_params(handle,
685 &atif->notification_cfg);
687 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
689 /* Disable notification */
690 atif->notification_cfg.enabled = false;
695 adev->acpi_nb.notifier_call = amdgpu_acpi_event;
696 register_acpi_notifier(&adev->acpi_nb);
702 * amdgpu_acpi_fini - tear down driver acpi support
704 * @adev: amdgpu_device pointer
706 * Unregisters with the acpi notifier chain (all asics).
708 void amdgpu_acpi_fini(struct amdgpu_device *adev)
710 unregister_acpi_notifier(&adev->acpi_nb);