radeon_acpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright 2012 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. #include <linux/pci.h>
  24. #include <linux/acpi.h>
  25. #include <linux/slab.h>
  26. #include <linux/power_supply.h>
  27. #include <acpi/acpi_drivers.h>
  28. #include <acpi/acpi_bus.h>
  29. #include <acpi/video.h>
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "drm_sarea.h"
  33. #include "drm_crtc_helper.h"
  34. #include "radeon.h"
  35. #include "radeon_acpi.h"
  36. #include "atom.h"
  37. #include <linux/vga_switcheroo.h>
  38. #define ACPI_AC_CLASS "ac_adapter"
  39. extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
  40. struct atif_verify_interface {
  41. u16 size; /* structure size in bytes (includes size field) */
  42. u16 version; /* version */
  43. u32 notification_mask; /* supported notifications mask */
  44. u32 function_bits; /* supported functions bit vector */
  45. } __packed;
  46. struct atif_system_params {
  47. u16 size; /* structure size in bytes (includes size field) */
  48. u32 valid_mask; /* valid flags mask */
  49. u32 flags; /* flags */
  50. u8 command_code; /* notify command code */
  51. } __packed;
  52. struct atif_sbios_requests {
  53. u16 size; /* structure size in bytes (includes size field) */
  54. u32 pending; /* pending sbios requests */
  55. u8 panel_exp_mode; /* panel expansion mode */
  56. u8 thermal_gfx; /* thermal state: target gfx controller */
  57. u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
  58. u8 forced_power_gfx; /* forced power state: target gfx controller */
  59. u8 forced_power_state; /* forced power state: state id */
  60. u8 system_power_src; /* system power source */
  61. u8 backlight_level; /* panel backlight level (0-255) */
  62. } __packed;
  63. #define ATIF_NOTIFY_MASK 0x3
  64. #define ATIF_NOTIFY_NONE 0
  65. #define ATIF_NOTIFY_81 1
  66. #define ATIF_NOTIFY_N 2
  67. /* Call the ATIF method
  68. */
  69. static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
  70. struct acpi_buffer *params)
  71. {
  72. acpi_status status;
  73. union acpi_object atif_arg_elements[2];
  74. struct acpi_object_list atif_arg;
  75. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  76. atif_arg.count = 2;
  77. atif_arg.pointer = &atif_arg_elements[0];
  78. atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
  79. atif_arg_elements[0].integer.value = function;
  80. if (params) {
  81. atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
  82. atif_arg_elements[1].buffer.length = params->length;
  83. atif_arg_elements[1].buffer.pointer = params->pointer;
  84. } else {
  85. /* We need a second fake parameter */
  86. atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
  87. atif_arg_elements[1].integer.value = 0;
  88. }
  89. status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
  90. /* Fail only if calling the method fails and ATIF is supported */
  91. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  92. DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
  93. acpi_format_exception(status));
  94. kfree(buffer.pointer);
  95. return NULL;
  96. }
  97. return buffer.pointer;
  98. }
  99. static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
  100. {
  101. n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
  102. n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
  103. n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
  104. n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
  105. n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
  106. n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
  107. n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
  108. n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
  109. n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
  110. }
  111. static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
  112. {
  113. f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
  114. f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
  115. f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
  116. f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
  117. f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
  118. f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
  119. f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
  120. f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
  121. f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
  122. f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
  123. }
  124. static int radeon_atif_verify_interface(acpi_handle handle,
  125. struct radeon_atif *atif)
  126. {
  127. union acpi_object *info;
  128. struct atif_verify_interface output;
  129. size_t size;
  130. int err = 0;
  131. info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
  132. if (!info)
  133. return -EIO;
  134. memset(&output, 0, sizeof(output));
  135. size = *(u16 *) info->buffer.pointer;
  136. if (size < 12) {
  137. DRM_INFO("ATIF buffer is too small: %lu\n", size);
  138. err = -EINVAL;
  139. goto out;
  140. }
  141. size = min(sizeof(output), size);
  142. memcpy(&output, info->buffer.pointer, size);
  143. /* TODO: check version? */
  144. DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
  145. radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
  146. radeon_atif_parse_functions(&atif->functions, output.function_bits);
  147. out:
  148. kfree(info);
  149. return err;
  150. }
  151. static int radeon_atif_get_notification_params(acpi_handle handle,
  152. struct radeon_atif_notification_cfg *n)
  153. {
  154. union acpi_object *info;
  155. struct atif_system_params params;
  156. size_t size;
  157. int err = 0;
  158. info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
  159. if (!info) {
  160. err = -EIO;
  161. goto out;
  162. }
  163. size = *(u16 *) info->buffer.pointer;
  164. if (size < 10) {
  165. err = -EINVAL;
  166. goto out;
  167. }
  168. memset(&params, 0, sizeof(params));
  169. size = min(sizeof(params), size);
  170. memcpy(&params, info->buffer.pointer, size);
  171. DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
  172. params.flags, params.valid_mask);
  173. params.flags = params.flags & params.valid_mask;
  174. if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
  175. n->enabled = false;
  176. n->command_code = 0;
  177. } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
  178. n->enabled = true;
  179. n->command_code = 0x81;
  180. } else {
  181. if (size < 11) {
  182. err = -EINVAL;
  183. goto out;
  184. }
  185. n->enabled = true;
  186. n->command_code = params.command_code;
  187. }
  188. out:
  189. DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
  190. (n->enabled ? "enabled" : "disabled"),
  191. n->command_code);
  192. kfree(info);
  193. return err;
  194. }
  195. static int radeon_atif_get_sbios_requests(acpi_handle handle,
  196. struct atif_sbios_requests *req)
  197. {
  198. union acpi_object *info;
  199. size_t size;
  200. int count = 0;
  201. info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
  202. if (!info)
  203. return -EIO;
  204. size = *(u16 *)info->buffer.pointer;
  205. if (size < 0xd) {
  206. count = -EINVAL;
  207. goto out;
  208. }
  209. memset(req, 0, sizeof(*req));
  210. size = min(sizeof(*req), size);
  211. memcpy(req, info->buffer.pointer, size);
  212. DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
  213. count = hweight32(req->pending);
  214. out:
  215. kfree(info);
  216. return count;
  217. }
  218. int radeon_atif_handler(struct radeon_device *rdev,
  219. struct acpi_bus_event *event)
  220. {
  221. struct radeon_atif *atif = &rdev->atif;
  222. struct atif_sbios_requests req;
  223. acpi_handle handle;
  224. int count;
  225. DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
  226. event->device_class, event->type);
  227. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  228. return NOTIFY_DONE;
  229. if (!atif->notification_cfg.enabled ||
  230. event->type != atif->notification_cfg.command_code)
  231. /* Not our event */
  232. return NOTIFY_DONE;
  233. /* Check pending SBIOS requests */
  234. handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
  235. count = radeon_atif_get_sbios_requests(handle, &req);
  236. if (count <= 0)
  237. return NOTIFY_DONE;
  238. DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
  239. if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
  240. struct radeon_encoder *enc = atif->backlight_ctl;
  241. if (enc) {
  242. struct radeon_encoder_atom_dig *dig = enc->enc_priv;
  243. dig->backlight_level = req.backlight_level;
  244. DRM_DEBUG_DRIVER("Changing brightness to %d\n",
  245. req.backlight_level);
  246. atombios_set_panel_brightness(enc);
  247. backlight_force_update(dig->bl_dev,
  248. BACKLIGHT_UPDATE_HOTKEY);
  249. }
  250. }
  251. /* TODO: check other events */
  252. /* We've handled the event, stop the notifier chain. The ACPI interface
  253. * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
  254. * userspace if the event was generated only to signal a SBIOS
  255. * request.
  256. */
  257. return NOTIFY_BAD;
  258. }
  259. static int radeon_acpi_event(struct notifier_block *nb,
  260. unsigned long val,
  261. void *data)
  262. {
  263. struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
  264. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  265. if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
  266. if (power_supply_is_system_supplied() > 0)
  267. DRM_DEBUG_DRIVER("pm: AC\n");
  268. else
  269. DRM_DEBUG_DRIVER("pm: DC\n");
  270. radeon_pm_acpi_event_handler(rdev);
  271. }
  272. /* Check for pending SBIOS requests */
  273. return radeon_atif_handler(rdev, entry);
  274. }
  275. /* Call all ACPI methods here */
  276. int radeon_acpi_init(struct radeon_device *rdev)
  277. {
  278. acpi_handle handle;
  279. struct radeon_atif *atif = &rdev->atif;
  280. int ret;
  281. /* Get the device handle */
  282. handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
  283. /* No need to proceed if we're sure that ATIF is not supported */
  284. if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
  285. return 0;
  286. /* Call the ATIF method */
  287. ret = radeon_atif_verify_interface(handle, atif);
  288. if (ret) {
  289. DRM_DEBUG_DRIVER("Call to verify_interface failed: %d\n", ret);
  290. goto out;
  291. }
  292. if (atif->notifications.brightness_change) {
  293. struct drm_encoder *tmp;
  294. struct radeon_encoder *target = NULL;
  295. /* Find the encoder controlling the brightness */
  296. list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
  297. head) {
  298. struct radeon_encoder *enc = to_radeon_encoder(tmp);
  299. struct radeon_encoder_atom_dig *dig = enc->enc_priv;
  300. if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
  301. dig->bl_dev != NULL) {
  302. target = enc;
  303. break;
  304. }
  305. }
  306. atif->backlight_ctl = target;
  307. if (!target) {
  308. /* Brightness change notification is enabled, but we
  309. * didn't find a backlight controller, this should
  310. * never happen.
  311. */
  312. DRM_ERROR("Cannot find a backlight controller\n");
  313. }
  314. }
  315. if (atif->functions.sbios_requests && !atif->functions.system_params) {
  316. /* XXX check this workraround, if sbios request function is
  317. * present we have to see how it's configured in the system
  318. * params
  319. */
  320. atif->functions.system_params = true;
  321. }
  322. if (atif->functions.system_params) {
  323. ret = radeon_atif_get_notification_params(handle,
  324. &atif->notification_cfg);
  325. if (ret) {
  326. DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
  327. ret);
  328. /* Disable notification */
  329. atif->notification_cfg.enabled = false;
  330. }
  331. }
  332. out:
  333. rdev->acpi_nb.notifier_call = radeon_acpi_event;
  334. register_acpi_notifier(&rdev->acpi_nb);
  335. return ret;
  336. }
  337. void radeon_acpi_fini(struct radeon_device *rdev)
  338. {
  339. unregister_acpi_notifier(&rdev->acpi_nb);
  340. }