Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2012 Advanced Micro Devices, Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  */
0023 
0024 #include <linux/acpi.h>
0025 #include <linux/pci.h>
0026 #include <linux/pm_runtime.h>
0027 #include <linux/power_supply.h>
0028 #include <linux/slab.h>
0029 
0030 #include <acpi/acpi_bus.h>
0031 #include <acpi/video.h>
0032 
0033 #include <drm/drm_crtc_helper.h>
0034 #include <drm/drm_probe_helper.h>
0035 
0036 #include "atom.h"
0037 #include "radeon.h"
0038 #include "radeon_acpi.h"
0039 #include "radeon_pm.h"
0040 
0041 #if defined(CONFIG_VGA_SWITCHEROO)
0042 bool radeon_atpx_dgpu_req_power_for_displays(void);
0043 #else
0044 static inline bool radeon_atpx_dgpu_req_power_for_displays(void) { return false; }
0045 #endif
0046 
0047 #define ACPI_AC_CLASS           "ac_adapter"
0048 
0049 struct atif_verify_interface {
0050     u16 size;       /* structure size in bytes (includes size field) */
0051     u16 version;        /* version */
0052     u32 notification_mask;  /* supported notifications mask */
0053     u32 function_bits;  /* supported functions bit vector */
0054 } __packed;
0055 
0056 struct atif_system_params {
0057     u16 size;       /* structure size in bytes (includes size field) */
0058     u32 valid_mask;     /* valid flags mask */
0059     u32 flags;      /* flags */
0060     u8 command_code;    /* notify command code */
0061 } __packed;
0062 
0063 struct atif_sbios_requests {
0064     u16 size;       /* structure size in bytes (includes size field) */
0065     u32 pending;        /* pending sbios requests */
0066     u8 panel_exp_mode;  /* panel expansion mode */
0067     u8 thermal_gfx;     /* thermal state: target gfx controller */
0068     u8 thermal_state;   /* thermal state: state id (0: exit state, non-0: state) */
0069     u8 forced_power_gfx;    /* forced power state: target gfx controller */
0070     u8 forced_power_state;  /* forced power state: state id */
0071     u8 system_power_src;    /* system power source */
0072     u8 backlight_level; /* panel backlight level (0-255) */
0073 } __packed;
0074 
0075 #define ATIF_NOTIFY_MASK    0x3
0076 #define ATIF_NOTIFY_NONE    0
0077 #define ATIF_NOTIFY_81      1
0078 #define ATIF_NOTIFY_N       2
0079 
0080 struct atcs_verify_interface {
0081     u16 size;       /* structure size in bytes (includes size field) */
0082     u16 version;        /* version */
0083     u32 function_bits;  /* supported functions bit vector */
0084 } __packed;
0085 
0086 #define ATCS_VALID_FLAGS_MASK   0x3
0087 
0088 struct atcs_pref_req_input {
0089     u16 size;       /* structure size in bytes (includes size field) */
0090     u16 client_id;      /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
0091     u16 valid_flags_mask;   /* valid flags mask */
0092     u16 flags;      /* flags */
0093     u8 req_type;        /* request type */
0094     u8 perf_req;        /* performance request */
0095 } __packed;
0096 
0097 struct atcs_pref_req_output {
0098     u16 size;       /* structure size in bytes (includes size field) */
0099     u8 ret_val;     /* return value */
0100 } __packed;
0101 
0102 /* Call the ATIF method
0103  */
0104 /**
0105  * radeon_atif_call - call an ATIF method
0106  *
0107  * @handle: acpi handle
0108  * @function: the ATIF function to execute
0109  * @params: ATIF function params
0110  *
0111  * Executes the requested ATIF function (all asics).
0112  * Returns a pointer to the acpi output buffer.
0113  */
0114 static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
0115         struct acpi_buffer *params)
0116 {
0117     acpi_status status;
0118     union acpi_object atif_arg_elements[2];
0119     struct acpi_object_list atif_arg;
0120     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0121 
0122     atif_arg.count = 2;
0123     atif_arg.pointer = &atif_arg_elements[0];
0124 
0125     atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
0126     atif_arg_elements[0].integer.value = function;
0127 
0128     if (params) {
0129         atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
0130         atif_arg_elements[1].buffer.length = params->length;
0131         atif_arg_elements[1].buffer.pointer = params->pointer;
0132     } else {
0133         /* We need a second fake parameter */
0134         atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
0135         atif_arg_elements[1].integer.value = 0;
0136     }
0137 
0138     status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
0139 
0140     /* Fail only if calling the method fails and ATIF is supported */
0141     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
0142         DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
0143                  acpi_format_exception(status));
0144         kfree(buffer.pointer);
0145         return NULL;
0146     }
0147 
0148     return buffer.pointer;
0149 }
0150 
0151 /**
0152  * radeon_atif_parse_notification - parse supported notifications
0153  *
0154  * @n: supported notifications struct
0155  * @mask: supported notifications mask from ATIF
0156  *
0157  * Use the supported notifications mask from ATIF function
0158  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
0159  * are supported (all asics).
0160  */
0161 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
0162 {
0163     n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
0164     n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
0165     n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
0166     n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
0167     n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
0168     n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
0169     n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
0170     n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
0171     n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
0172 }
0173 
0174 /**
0175  * radeon_atif_parse_functions - parse supported functions
0176  *
0177  * @f: supported functions struct
0178  * @mask: supported functions mask from ATIF
0179  *
0180  * Use the supported functions mask from ATIF function
0181  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
0182  * are supported (all asics).
0183  */
0184 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
0185 {
0186     f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
0187     f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
0188     f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
0189     f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
0190     f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
0191     f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
0192     f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
0193     f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
0194     f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
0195     f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
0196 }
0197 
0198 /**
0199  * radeon_atif_verify_interface - verify ATIF
0200  *
0201  * @handle: acpi handle
0202  * @atif: radeon atif struct
0203  *
0204  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
0205  * to initialize ATIF and determine what features are supported
0206  * (all asics).
0207  * returns 0 on success, error on failure.
0208  */
0209 static int radeon_atif_verify_interface(acpi_handle handle,
0210         struct radeon_atif *atif)
0211 {
0212     union acpi_object *info;
0213     struct atif_verify_interface output;
0214     size_t size;
0215     int err = 0;
0216 
0217     info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
0218     if (!info)
0219         return -EIO;
0220 
0221     memset(&output, 0, sizeof(output));
0222 
0223     size = *(u16 *) info->buffer.pointer;
0224     if (size < 12) {
0225         DRM_INFO("ATIF buffer is too small: %zu\n", size);
0226         err = -EINVAL;
0227         goto out;
0228     }
0229     size = min(sizeof(output), size);
0230 
0231     memcpy(&output, info->buffer.pointer, size);
0232 
0233     /* TODO: check version? */
0234     DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
0235 
0236     radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
0237     radeon_atif_parse_functions(&atif->functions, output.function_bits);
0238 
0239 out:
0240     kfree(info);
0241     return err;
0242 }
0243 
0244 /**
0245  * radeon_atif_get_notification_params - determine notify configuration
0246  *
0247  * @handle: acpi handle
0248  * @n: atif notification configuration struct
0249  *
0250  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
0251  * to determine if a notifier is used and if so which one
0252  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
0253  * where n is specified in the result if a notifier is used.
0254  * Returns 0 on success, error on failure.
0255  */
0256 static int radeon_atif_get_notification_params(acpi_handle handle,
0257         struct radeon_atif_notification_cfg *n)
0258 {
0259     union acpi_object *info;
0260     struct atif_system_params params;
0261     size_t size;
0262     int err = 0;
0263 
0264     info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
0265     if (!info) {
0266         err = -EIO;
0267         goto out;
0268     }
0269 
0270     size = *(u16 *) info->buffer.pointer;
0271     if (size < 10) {
0272         err = -EINVAL;
0273         goto out;
0274     }
0275 
0276     memset(&params, 0, sizeof(params));
0277     size = min(sizeof(params), size);
0278     memcpy(&params, info->buffer.pointer, size);
0279 
0280     DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
0281             params.flags, params.valid_mask);
0282     params.flags = params.flags & params.valid_mask;
0283 
0284     if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
0285         n->enabled = false;
0286         n->command_code = 0;
0287     } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
0288         n->enabled = true;
0289         n->command_code = 0x81;
0290     } else {
0291         if (size < 11) {
0292             err = -EINVAL;
0293             goto out;
0294         }
0295         n->enabled = true;
0296         n->command_code = params.command_code;
0297     }
0298 
0299 out:
0300     DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
0301             (n->enabled ? "enabled" : "disabled"),
0302             n->command_code);
0303     kfree(info);
0304     return err;
0305 }
0306 
0307 /**
0308  * radeon_atif_get_sbios_requests - get requested sbios event
0309  *
0310  * @handle: acpi handle
0311  * @req: atif sbios request struct
0312  *
0313  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
0314  * to determine what requests the sbios is making to the driver
0315  * (all asics).
0316  * Returns 0 on success, error on failure.
0317  */
0318 static int radeon_atif_get_sbios_requests(acpi_handle handle,
0319         struct atif_sbios_requests *req)
0320 {
0321     union acpi_object *info;
0322     size_t size;
0323     int count = 0;
0324 
0325     info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
0326     if (!info)
0327         return -EIO;
0328 
0329     size = *(u16 *)info->buffer.pointer;
0330     if (size < 0xd) {
0331         count = -EINVAL;
0332         goto out;
0333     }
0334     memset(req, 0, sizeof(*req));
0335 
0336     size = min(sizeof(*req), size);
0337     memcpy(req, info->buffer.pointer, size);
0338     DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
0339 
0340     count = hweight32(req->pending);
0341 
0342 out:
0343     kfree(info);
0344     return count;
0345 }
0346 
0347 /**
0348  * radeon_atif_handler - handle ATIF notify requests
0349  *
0350  * @rdev: radeon_device pointer
0351  * @event: atif sbios request struct
0352  *
0353  * Checks the acpi event and if it matches an atif event,
0354  * handles it.
0355  * Returns NOTIFY code
0356  */
0357 static int radeon_atif_handler(struct radeon_device *rdev,
0358         struct acpi_bus_event *event)
0359 {
0360     struct radeon_atif *atif = &rdev->atif;
0361     struct atif_sbios_requests req;
0362     acpi_handle handle;
0363     int count;
0364 
0365     DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
0366             event->device_class, event->type);
0367 
0368     if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
0369         return NOTIFY_DONE;
0370 
0371     if (!atif->notification_cfg.enabled ||
0372             event->type != atif->notification_cfg.command_code)
0373         /* Not our event */
0374         return NOTIFY_DONE;
0375 
0376     /* Check pending SBIOS requests */
0377     handle = ACPI_HANDLE(&rdev->pdev->dev);
0378     count = radeon_atif_get_sbios_requests(handle, &req);
0379 
0380     if (count <= 0)
0381         return NOTIFY_DONE;
0382 
0383     DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
0384 
0385     if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
0386         struct radeon_encoder *enc = atif->encoder_for_bl;
0387 
0388         if (enc) {
0389             DRM_DEBUG_DRIVER("Changing brightness to %d\n",
0390                     req.backlight_level);
0391 
0392             radeon_set_backlight_level(rdev, enc, req.backlight_level);
0393 
0394             if (rdev->is_atom_bios) {
0395                 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
0396                 backlight_force_update(dig->bl_dev,
0397                                BACKLIGHT_UPDATE_HOTKEY);
0398             } else {
0399                 struct radeon_encoder_lvds *dig = enc->enc_priv;
0400                 backlight_force_update(dig->bl_dev,
0401                                BACKLIGHT_UPDATE_HOTKEY);
0402             }
0403         }
0404     }
0405     if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
0406         if ((rdev->flags & RADEON_IS_PX) &&
0407             radeon_atpx_dgpu_req_power_for_displays()) {
0408             pm_runtime_get_sync(rdev->ddev->dev);
0409             /* Just fire off a uevent and let userspace tell us what to do */
0410             drm_helper_hpd_irq_event(rdev->ddev);
0411             pm_runtime_mark_last_busy(rdev->ddev->dev);
0412             pm_runtime_put_autosuspend(rdev->ddev->dev);
0413         }
0414     }
0415     /* TODO: check other events */
0416 
0417     /* We've handled the event, stop the notifier chain. The ACPI interface
0418      * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
0419      * userspace if the event was generated only to signal a SBIOS
0420      * request.
0421      */
0422     return NOTIFY_BAD;
0423 }
0424 
0425 /* Call the ATCS method
0426  */
0427 /**
0428  * radeon_atcs_call - call an ATCS method
0429  *
0430  * @handle: acpi handle
0431  * @function: the ATCS function to execute
0432  * @params: ATCS function params
0433  *
0434  * Executes the requested ATCS function (all asics).
0435  * Returns a pointer to the acpi output buffer.
0436  */
0437 static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
0438                        struct acpi_buffer *params)
0439 {
0440     acpi_status status;
0441     union acpi_object atcs_arg_elements[2];
0442     struct acpi_object_list atcs_arg;
0443     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0444 
0445     atcs_arg.count = 2;
0446     atcs_arg.pointer = &atcs_arg_elements[0];
0447 
0448     atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
0449     atcs_arg_elements[0].integer.value = function;
0450 
0451     if (params) {
0452         atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
0453         atcs_arg_elements[1].buffer.length = params->length;
0454         atcs_arg_elements[1].buffer.pointer = params->pointer;
0455     } else {
0456         /* We need a second fake parameter */
0457         atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
0458         atcs_arg_elements[1].integer.value = 0;
0459     }
0460 
0461     status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
0462 
0463     /* Fail only if calling the method fails and ATIF is supported */
0464     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
0465         DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
0466                  acpi_format_exception(status));
0467         kfree(buffer.pointer);
0468         return NULL;
0469     }
0470 
0471     return buffer.pointer;
0472 }
0473 
0474 /**
0475  * radeon_atcs_parse_functions - parse supported functions
0476  *
0477  * @f: supported functions struct
0478  * @mask: supported functions mask from ATCS
0479  *
0480  * Use the supported functions mask from ATCS function
0481  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
0482  * are supported (all asics).
0483  */
0484 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
0485 {
0486     f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
0487     f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
0488     f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
0489     f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
0490 }
0491 
0492 /**
0493  * radeon_atcs_verify_interface - verify ATCS
0494  *
0495  * @handle: acpi handle
0496  * @atcs: radeon atcs struct
0497  *
0498  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
0499  * to initialize ATCS and determine what features are supported
0500  * (all asics).
0501  * returns 0 on success, error on failure.
0502  */
0503 static int radeon_atcs_verify_interface(acpi_handle handle,
0504                     struct radeon_atcs *atcs)
0505 {
0506     union acpi_object *info;
0507     struct atcs_verify_interface output;
0508     size_t size;
0509     int err = 0;
0510 
0511     info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
0512     if (!info)
0513         return -EIO;
0514 
0515     memset(&output, 0, sizeof(output));
0516 
0517     size = *(u16 *) info->buffer.pointer;
0518     if (size < 8) {
0519         DRM_INFO("ATCS buffer is too small: %zu\n", size);
0520         err = -EINVAL;
0521         goto out;
0522     }
0523     size = min(sizeof(output), size);
0524 
0525     memcpy(&output, info->buffer.pointer, size);
0526 
0527     /* TODO: check version? */
0528     DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
0529 
0530     radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
0531 
0532 out:
0533     kfree(info);
0534     return err;
0535 }
0536 
0537 /**
0538  * radeon_acpi_is_pcie_performance_request_supported
0539  *
0540  * @rdev: radeon_device pointer
0541  *
0542  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
0543  * are supported (all asics).
0544  * returns true if supported, false if not.
0545  */
0546 bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
0547 {
0548     struct radeon_atcs *atcs = &rdev->atcs;
0549 
0550     if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
0551         return true;
0552 
0553     return false;
0554 }
0555 
0556 /**
0557  * radeon_acpi_pcie_notify_device_ready
0558  *
0559  * @rdev: radeon_device pointer
0560  *
0561  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
0562  * (all asics).
0563  * returns 0 on success, error on failure.
0564  */
0565 int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
0566 {
0567     acpi_handle handle;
0568     union acpi_object *info;
0569     struct radeon_atcs *atcs = &rdev->atcs;
0570 
0571     /* Get the device handle */
0572     handle = ACPI_HANDLE(&rdev->pdev->dev);
0573     if (!handle)
0574         return -EINVAL;
0575 
0576     if (!atcs->functions.pcie_dev_rdy)
0577         return -EINVAL;
0578 
0579     info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
0580     if (!info)
0581         return -EIO;
0582 
0583     kfree(info);
0584 
0585     return 0;
0586 }
0587 
0588 /**
0589  * radeon_acpi_pcie_performance_request
0590  *
0591  * @rdev: radeon_device pointer
0592  * @perf_req: requested perf level (pcie gen speed)
0593  * @advertise: set advertise caps flag if set
0594  *
0595  * Executes the PCIE_PERFORMANCE_REQUEST method to
0596  * change the pcie gen speed (all asics).
0597  * returns 0 on success, error on failure.
0598  */
0599 int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
0600                      u8 perf_req, bool advertise)
0601 {
0602     acpi_handle handle;
0603     union acpi_object *info;
0604     struct radeon_atcs *atcs = &rdev->atcs;
0605     struct atcs_pref_req_input atcs_input;
0606     struct atcs_pref_req_output atcs_output;
0607     struct acpi_buffer params;
0608     size_t size;
0609     u32 retry = 3;
0610 
0611     /* Get the device handle */
0612     handle = ACPI_HANDLE(&rdev->pdev->dev);
0613     if (!handle)
0614         return -EINVAL;
0615 
0616     if (!atcs->functions.pcie_perf_req)
0617         return -EINVAL;
0618 
0619     atcs_input.size = sizeof(struct atcs_pref_req_input);
0620     /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
0621     atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
0622     atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
0623     atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
0624     if (advertise)
0625         atcs_input.flags |= ATCS_ADVERTISE_CAPS;
0626     atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
0627     atcs_input.perf_req = perf_req;
0628 
0629     params.length = sizeof(struct atcs_pref_req_input);
0630     params.pointer = &atcs_input;
0631 
0632     while (retry--) {
0633         info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
0634         if (!info)
0635             return -EIO;
0636 
0637         memset(&atcs_output, 0, sizeof(atcs_output));
0638 
0639         size = *(u16 *) info->buffer.pointer;
0640         if (size < 3) {
0641             DRM_INFO("ATCS buffer is too small: %zu\n", size);
0642             kfree(info);
0643             return -EINVAL;
0644         }
0645         size = min(sizeof(atcs_output), size);
0646 
0647         memcpy(&atcs_output, info->buffer.pointer, size);
0648 
0649         kfree(info);
0650 
0651         switch (atcs_output.ret_val) {
0652         case ATCS_REQUEST_REFUSED:
0653         default:
0654             return -EINVAL;
0655         case ATCS_REQUEST_COMPLETE:
0656             return 0;
0657         case ATCS_REQUEST_IN_PROGRESS:
0658             udelay(10);
0659             break;
0660         }
0661     }
0662 
0663     return 0;
0664 }
0665 
0666 /**
0667  * radeon_acpi_event - handle notify events
0668  *
0669  * @nb: notifier block
0670  * @val: val
0671  * @data: acpi event
0672  *
0673  * Calls relevant radeon functions in response to various
0674  * acpi events.
0675  * Returns NOTIFY code
0676  */
0677 static int radeon_acpi_event(struct notifier_block *nb,
0678                  unsigned long val,
0679                  void *data)
0680 {
0681     struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
0682     struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
0683 
0684     if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
0685         if (power_supply_is_system_supplied() > 0)
0686             DRM_DEBUG_DRIVER("pm: AC\n");
0687         else
0688             DRM_DEBUG_DRIVER("pm: DC\n");
0689 
0690         radeon_pm_acpi_event_handler(rdev);
0691     }
0692 
0693     /* Check for pending SBIOS requests */
0694     return radeon_atif_handler(rdev, entry);
0695 }
0696 
0697 /* Call all ACPI methods here */
0698 /**
0699  * radeon_acpi_init - init driver acpi support
0700  *
0701  * @rdev: radeon_device pointer
0702  *
0703  * Verifies the AMD ACPI interfaces and registers with the acpi
0704  * notifier chain (all asics).
0705  * Returns 0 on success, error on failure.
0706  */
0707 int radeon_acpi_init(struct radeon_device *rdev)
0708 {
0709     acpi_handle handle;
0710     struct radeon_atif *atif = &rdev->atif;
0711     struct radeon_atcs *atcs = &rdev->atcs;
0712     int ret;
0713 
0714     /* Get the device handle */
0715     handle = ACPI_HANDLE(&rdev->pdev->dev);
0716 
0717     /* No need to proceed if we're sure that ATIF is not supported */
0718     if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
0719         return 0;
0720 
0721     /* Call the ATCS method */
0722     ret = radeon_atcs_verify_interface(handle, atcs);
0723     if (ret) {
0724         DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
0725     }
0726 
0727     /* Call the ATIF method */
0728     ret = radeon_atif_verify_interface(handle, atif);
0729     if (ret) {
0730         DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
0731         goto out;
0732     }
0733 
0734     if (atif->notifications.brightness_change) {
0735         struct drm_encoder *tmp;
0736         struct radeon_encoder *target = NULL;
0737 
0738         /* Find the encoder controlling the brightness */
0739         list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
0740                 head) {
0741             struct radeon_encoder *enc = to_radeon_encoder(tmp);
0742 
0743             if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
0744                 enc->enc_priv) {
0745                 if (rdev->is_atom_bios) {
0746                     struct radeon_encoder_atom_dig *dig = enc->enc_priv;
0747                     if (dig->bl_dev) {
0748                         target = enc;
0749                         break;
0750                     }
0751                 } else {
0752                     struct radeon_encoder_lvds *dig = enc->enc_priv;
0753                     if (dig->bl_dev) {
0754                         target = enc;
0755                         break;
0756                     }
0757                 }
0758             }
0759         }
0760 
0761         atif->encoder_for_bl = target;
0762     }
0763 
0764     if (atif->functions.sbios_requests && !atif->functions.system_params) {
0765         /* XXX check this workraround, if sbios request function is
0766          * present we have to see how it's configured in the system
0767          * params
0768          */
0769         atif->functions.system_params = true;
0770     }
0771 
0772     if (atif->functions.system_params) {
0773         ret = radeon_atif_get_notification_params(handle,
0774                 &atif->notification_cfg);
0775         if (ret) {
0776             DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
0777                     ret);
0778             /* Disable notification */
0779             atif->notification_cfg.enabled = false;
0780         }
0781     }
0782 
0783 out:
0784     rdev->acpi_nb.notifier_call = radeon_acpi_event;
0785     register_acpi_notifier(&rdev->acpi_nb);
0786 
0787     return ret;
0788 }
0789 
0790 /**
0791  * radeon_acpi_fini - tear down driver acpi support
0792  *
0793  * @rdev: radeon_device pointer
0794  *
0795  * Unregisters with the acpi notifier chain (all asics).
0796  */
0797 void radeon_acpi_fini(struct radeon_device *rdev)
0798 {
0799     unregister_acpi_notifier(&rdev->acpi_nb);
0800 }