radeon_acpi.c 11 KB

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