radeon_acpi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 <linux/vga_switcheroo.h>
  28. #include <acpi/video.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include "radeon.h"
  32. #include "radeon_acpi.h"
  33. #include "atom.h"
  34. #define ACPI_AC_CLASS "ac_adapter"
  35. extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
  36. struct atif_verify_interface {
  37. u16 size; /* structure size in bytes (includes size field) */
  38. u16 version; /* version */
  39. u32 notification_mask; /* supported notifications mask */
  40. u32 function_bits; /* supported functions bit vector */
  41. } __packed;
  42. struct atif_system_params {
  43. u16 size; /* structure size in bytes (includes size field) */
  44. u32 valid_mask; /* valid flags mask */
  45. u32 flags; /* flags */
  46. u8 command_code; /* notify command code */
  47. } __packed;
  48. struct atif_sbios_requests {
  49. u16 size; /* structure size in bytes (includes size field) */
  50. u32 pending; /* pending sbios requests */
  51. u8 panel_exp_mode; /* panel expansion mode */
  52. u8 thermal_gfx; /* thermal state: target gfx controller */
  53. u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
  54. u8 forced_power_gfx; /* forced power state: target gfx controller */
  55. u8 forced_power_state; /* forced power state: state id */
  56. u8 system_power_src; /* system power source */
  57. u8 backlight_level; /* panel backlight level (0-255) */
  58. } __packed;
  59. #define ATIF_NOTIFY_MASK 0x3
  60. #define ATIF_NOTIFY_NONE 0
  61. #define ATIF_NOTIFY_81 1
  62. #define ATIF_NOTIFY_N 2
  63. struct atcs_verify_interface {
  64. u16 size; /* structure size in bytes (includes size field) */
  65. u16 version; /* version */
  66. u32 function_bits; /* supported functions bit vector */
  67. } __packed;
  68. #define ATCS_VALID_FLAGS_MASK 0x3
  69. struct atcs_pref_req_input {
  70. u16 size; /* structure size in bytes (includes size field) */
  71. u16 client_id; /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
  72. u16 valid_flags_mask; /* valid flags mask */
  73. u16 flags; /* flags */
  74. u8 req_type; /* request type */
  75. u8 perf_req; /* performance request */
  76. } __packed;
  77. struct atcs_pref_req_output {
  78. u16 size; /* structure size in bytes (includes size field) */
  79. u8 ret_val; /* return value */
  80. } __packed;
  81. /* Call the ATIF method
  82. */
  83. /**
  84. * radeon_atif_call - call an ATIF method
  85. *
  86. * @handle: acpi handle
  87. * @function: the ATIF function to execute
  88. * @params: ATIF function params
  89. *
  90. * Executes the requested ATIF function (all asics).
  91. * Returns a pointer to the acpi output buffer.
  92. */
  93. static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
  94. struct acpi_buffer *params)
  95. {
  96. acpi_status status;
  97. union acpi_object atif_arg_elements[2];
  98. struct acpi_object_list atif_arg;
  99. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  100. atif_arg.count = 2;
  101. atif_arg.pointer = &atif_arg_elements[0];
  102. atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
  103. atif_arg_elements[0].integer.value = function;
  104. if (params) {
  105. atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
  106. atif_arg_elements[1].buffer.length = params->length;
  107. atif_arg_elements[1].buffer.pointer = params->pointer;
  108. } else {
  109. /* We need a second fake parameter */
  110. atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
  111. atif_arg_elements[1].integer.value = 0;
  112. }
  113. status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
  114. /* Fail only if calling the method fails and ATIF is supported */
  115. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  116. DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
  117. acpi_format_exception(status));
  118. kfree(buffer.pointer);
  119. return NULL;
  120. }
  121. return buffer.pointer;
  122. }
  123. /**
  124. * radeon_atif_parse_notification - parse supported notifications
  125. *
  126. * @n: supported notifications struct
  127. * @mask: supported notifications mask from ATIF
  128. *
  129. * Use the supported notifications mask from ATIF function
  130. * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
  131. * are supported (all asics).
  132. */
  133. static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
  134. {
  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;
  144. }
  145. /**
  146. * radeon_atif_parse_functions - parse supported functions
  147. *
  148. * @f: supported functions struct
  149. * @mask: supported functions mask from ATIF
  150. *
  151. * Use the supported functions mask from ATIF function
  152. * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
  153. * are supported (all asics).
  154. */
  155. static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
  156. {
  157. f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
  158. f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
  159. f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
  160. f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
  161. f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
  162. f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
  163. f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
  164. f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
  165. f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
  166. f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
  167. }
  168. /**
  169. * radeon_atif_verify_interface - verify ATIF
  170. *
  171. * @handle: acpi handle
  172. * @atif: radeon atif struct
  173. *
  174. * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
  175. * to initialize ATIF and determine what features are supported
  176. * (all asics).
  177. * returns 0 on success, error on failure.
  178. */
  179. static int radeon_atif_verify_interface(acpi_handle handle,
  180. struct radeon_atif *atif)
  181. {
  182. union acpi_object *info;
  183. struct atif_verify_interface output;
  184. size_t size;
  185. int err = 0;
  186. info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
  187. if (!info)
  188. return -EIO;
  189. memset(&output, 0, sizeof(output));
  190. size = *(u16 *) info->buffer.pointer;
  191. if (size < 12) {
  192. DRM_INFO("ATIF buffer is too small: %zu\n", size);
  193. err = -EINVAL;
  194. goto out;
  195. }
  196. size = min(sizeof(output), size);
  197. memcpy(&output, info->buffer.pointer, size);
  198. /* TODO: check version? */
  199. DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
  200. radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
  201. radeon_atif_parse_functions(&atif->functions, output.function_bits);
  202. out:
  203. kfree(info);
  204. return err;
  205. }
  206. /**
  207. * radeon_atif_get_notification_params - determine notify configuration
  208. *
  209. * @handle: acpi handle
  210. * @n: atif notification configuration struct
  211. *
  212. * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
  213. * to determine if a notifier is used and if so which one
  214. * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
  215. * where n is specified in the result if a notifier is used.
  216. * Returns 0 on success, error on failure.
  217. */
  218. static int radeon_atif_get_notification_params(acpi_handle handle,
  219. struct radeon_atif_notification_cfg *n)
  220. {
  221. union acpi_object *info;
  222. struct atif_system_params params;
  223. size_t size;
  224. int err = 0;
  225. info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
  226. if (!info) {
  227. err = -EIO;
  228. goto out;
  229. }
  230. size = *(u16 *) info->buffer.pointer;
  231. if (size < 10) {
  232. err = -EINVAL;
  233. goto out;
  234. }
  235. memset(&params, 0, sizeof(params));
  236. size = min(sizeof(params), size);
  237. memcpy(&params, info->buffer.pointer, size);
  238. DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
  239. params.flags, params.valid_mask);
  240. params.flags = params.flags & params.valid_mask;
  241. if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
  242. n->enabled = false;
  243. n->command_code = 0;
  244. } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
  245. n->enabled = true;
  246. n->command_code = 0x81;
  247. } else {
  248. if (size < 11) {
  249. err = -EINVAL;
  250. goto out;
  251. }
  252. n->enabled = true;
  253. n->command_code = params.command_code;
  254. }
  255. out:
  256. DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
  257. (n->enabled ? "enabled" : "disabled"),
  258. n->command_code);
  259. kfree(info);
  260. return err;
  261. }
  262. /**
  263. * radeon_atif_get_sbios_requests - get requested sbios event
  264. *
  265. * @handle: acpi handle
  266. * @req: atif sbios request struct
  267. *
  268. * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
  269. * to determine what requests the sbios is making to the driver
  270. * (all asics).
  271. * Returns 0 on success, error on failure.
  272. */
  273. static int radeon_atif_get_sbios_requests(acpi_handle handle,
  274. struct atif_sbios_requests *req)
  275. {
  276. union acpi_object *info;
  277. size_t size;
  278. int count = 0;
  279. info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
  280. if (!info)
  281. return -EIO;
  282. size = *(u16 *)info->buffer.pointer;
  283. if (size < 0xd) {
  284. count = -EINVAL;
  285. goto out;
  286. }
  287. memset(req, 0, sizeof(*req));
  288. size = min(sizeof(*req), size);
  289. memcpy(req, info->buffer.pointer, size);
  290. DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
  291. count = hweight32(req->pending);
  292. out:
  293. kfree(info);
  294. return count;
  295. }
  296. /**
  297. * radeon_atif_handler - handle ATIF notify requests
  298. *
  299. * @rdev: radeon_device pointer
  300. * @event: atif sbios request struct
  301. *
  302. * Checks the acpi event and if it matches an atif event,
  303. * handles it.
  304. * Returns NOTIFY code
  305. */
  306. int radeon_atif_handler(struct radeon_device *rdev,
  307. struct acpi_bus_event *event)
  308. {
  309. struct radeon_atif *atif = &rdev->atif;
  310. struct atif_sbios_requests req;
  311. acpi_handle handle;
  312. int count;
  313. DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
  314. event->device_class, event->type);
  315. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  316. return NOTIFY_DONE;
  317. if (!atif->notification_cfg.enabled ||
  318. event->type != atif->notification_cfg.command_code)
  319. /* Not our event */
  320. return NOTIFY_DONE;
  321. /* Check pending SBIOS requests */
  322. handle = ACPI_HANDLE(&rdev->pdev->dev);
  323. count = radeon_atif_get_sbios_requests(handle, &req);
  324. if (count <= 0)
  325. return NOTIFY_DONE;
  326. DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
  327. if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
  328. struct radeon_encoder *enc = atif->encoder_for_bl;
  329. if (enc) {
  330. DRM_DEBUG_DRIVER("Changing brightness to %d\n",
  331. req.backlight_level);
  332. radeon_set_backlight_level(rdev, enc, req.backlight_level);
  333. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  334. if (rdev->is_atom_bios) {
  335. struct radeon_encoder_atom_dig *dig = enc->enc_priv;
  336. backlight_force_update(dig->bl_dev,
  337. BACKLIGHT_UPDATE_HOTKEY);
  338. } else {
  339. struct radeon_encoder_lvds *dig = enc->enc_priv;
  340. backlight_force_update(dig->bl_dev,
  341. BACKLIGHT_UPDATE_HOTKEY);
  342. }
  343. #endif
  344. }
  345. }
  346. /* TODO: check other events */
  347. /* We've handled the event, stop the notifier chain. The ACPI interface
  348. * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
  349. * userspace if the event was generated only to signal a SBIOS
  350. * request.
  351. */
  352. return NOTIFY_BAD;
  353. }
  354. /* Call the ATCS method
  355. */
  356. /**
  357. * radeon_atcs_call - call an ATCS method
  358. *
  359. * @handle: acpi handle
  360. * @function: the ATCS function to execute
  361. * @params: ATCS function params
  362. *
  363. * Executes the requested ATCS function (all asics).
  364. * Returns a pointer to the acpi output buffer.
  365. */
  366. static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
  367. struct acpi_buffer *params)
  368. {
  369. acpi_status status;
  370. union acpi_object atcs_arg_elements[2];
  371. struct acpi_object_list atcs_arg;
  372. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  373. atcs_arg.count = 2;
  374. atcs_arg.pointer = &atcs_arg_elements[0];
  375. atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
  376. atcs_arg_elements[0].integer.value = function;
  377. if (params) {
  378. atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
  379. atcs_arg_elements[1].buffer.length = params->length;
  380. atcs_arg_elements[1].buffer.pointer = params->pointer;
  381. } else {
  382. /* We need a second fake parameter */
  383. atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
  384. atcs_arg_elements[1].integer.value = 0;
  385. }
  386. status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
  387. /* Fail only if calling the method fails and ATIF is supported */
  388. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  389. DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
  390. acpi_format_exception(status));
  391. kfree(buffer.pointer);
  392. return NULL;
  393. }
  394. return buffer.pointer;
  395. }
  396. /**
  397. * radeon_atcs_parse_functions - parse supported functions
  398. *
  399. * @f: supported functions struct
  400. * @mask: supported functions mask from ATCS
  401. *
  402. * Use the supported functions mask from ATCS function
  403. * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
  404. * are supported (all asics).
  405. */
  406. static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
  407. {
  408. f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
  409. f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
  410. f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
  411. f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
  412. }
  413. /**
  414. * radeon_atcs_verify_interface - verify ATCS
  415. *
  416. * @handle: acpi handle
  417. * @atcs: radeon atcs struct
  418. *
  419. * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
  420. * to initialize ATCS and determine what features are supported
  421. * (all asics).
  422. * returns 0 on success, error on failure.
  423. */
  424. static int radeon_atcs_verify_interface(acpi_handle handle,
  425. struct radeon_atcs *atcs)
  426. {
  427. union acpi_object *info;
  428. struct atcs_verify_interface output;
  429. size_t size;
  430. int err = 0;
  431. info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
  432. if (!info)
  433. return -EIO;
  434. memset(&output, 0, sizeof(output));
  435. size = *(u16 *) info->buffer.pointer;
  436. if (size < 8) {
  437. DRM_INFO("ATCS buffer is too small: %zu\n", size);
  438. err = -EINVAL;
  439. goto out;
  440. }
  441. size = min(sizeof(output), size);
  442. memcpy(&output, info->buffer.pointer, size);
  443. /* TODO: check version? */
  444. DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
  445. radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
  446. out:
  447. kfree(info);
  448. return err;
  449. }
  450. /**
  451. * radeon_acpi_is_pcie_performance_request_supported
  452. *
  453. * @rdev: radeon_device pointer
  454. *
  455. * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
  456. * are supported (all asics).
  457. * returns true if supported, false if not.
  458. */
  459. bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
  460. {
  461. struct radeon_atcs *atcs = &rdev->atcs;
  462. if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
  463. return true;
  464. return false;
  465. }
  466. /**
  467. * radeon_acpi_pcie_notify_device_ready
  468. *
  469. * @rdev: radeon_device pointer
  470. *
  471. * Executes the PCIE_DEVICE_READY_NOTIFICATION method
  472. * (all asics).
  473. * returns 0 on success, error on failure.
  474. */
  475. int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
  476. {
  477. acpi_handle handle;
  478. union acpi_object *info;
  479. struct radeon_atcs *atcs = &rdev->atcs;
  480. /* Get the device handle */
  481. handle = ACPI_HANDLE(&rdev->pdev->dev);
  482. if (!handle)
  483. return -EINVAL;
  484. if (!atcs->functions.pcie_dev_rdy)
  485. return -EINVAL;
  486. info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
  487. if (!info)
  488. return -EIO;
  489. kfree(info);
  490. return 0;
  491. }
  492. /**
  493. * radeon_acpi_pcie_performance_request
  494. *
  495. * @rdev: radeon_device pointer
  496. * @perf_req: requested perf level (pcie gen speed)
  497. * @advertise: set advertise caps flag if set
  498. *
  499. * Executes the PCIE_PERFORMANCE_REQUEST method to
  500. * change the pcie gen speed (all asics).
  501. * returns 0 on success, error on failure.
  502. */
  503. int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
  504. u8 perf_req, bool advertise)
  505. {
  506. acpi_handle handle;
  507. union acpi_object *info;
  508. struct radeon_atcs *atcs = &rdev->atcs;
  509. struct atcs_pref_req_input atcs_input;
  510. struct atcs_pref_req_output atcs_output;
  511. struct acpi_buffer params;
  512. size_t size;
  513. u32 retry = 3;
  514. /* Get the device handle */
  515. handle = ACPI_HANDLE(&rdev->pdev->dev);
  516. if (!handle)
  517. return -EINVAL;
  518. if (!atcs->functions.pcie_perf_req)
  519. return -EINVAL;
  520. atcs_input.size = sizeof(struct atcs_pref_req_input);
  521. /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
  522. atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
  523. atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
  524. atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
  525. if (advertise)
  526. atcs_input.flags |= ATCS_ADVERTISE_CAPS;
  527. atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
  528. atcs_input.perf_req = perf_req;
  529. params.length = sizeof(struct atcs_pref_req_input);
  530. params.pointer = &atcs_input;
  531. while (retry--) {
  532. info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
  533. if (!info)
  534. return -EIO;
  535. memset(&atcs_output, 0, sizeof(atcs_output));
  536. size = *(u16 *) info->buffer.pointer;
  537. if (size < 3) {
  538. DRM_INFO("ATCS buffer is too small: %zu\n", size);
  539. kfree(info);
  540. return -EINVAL;
  541. }
  542. size = min(sizeof(atcs_output), size);
  543. memcpy(&atcs_output, info->buffer.pointer, size);
  544. kfree(info);
  545. switch (atcs_output.ret_val) {
  546. case ATCS_REQUEST_REFUSED:
  547. default:
  548. return -EINVAL;
  549. case ATCS_REQUEST_COMPLETE:
  550. return 0;
  551. case ATCS_REQUEST_IN_PROGRESS:
  552. udelay(10);
  553. break;
  554. }
  555. }
  556. return 0;
  557. }
  558. /**
  559. * radeon_acpi_event - handle notify events
  560. *
  561. * @nb: notifier block
  562. * @val: val
  563. * @data: acpi event
  564. *
  565. * Calls relevant radeon functions in response to various
  566. * acpi events.
  567. * Returns NOTIFY code
  568. */
  569. static int radeon_acpi_event(struct notifier_block *nb,
  570. unsigned long val,
  571. void *data)
  572. {
  573. struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
  574. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  575. if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
  576. if (power_supply_is_system_supplied() > 0)
  577. DRM_DEBUG_DRIVER("pm: AC\n");
  578. else
  579. DRM_DEBUG_DRIVER("pm: DC\n");
  580. radeon_pm_acpi_event_handler(rdev);
  581. }
  582. /* Check for pending SBIOS requests */
  583. return radeon_atif_handler(rdev, entry);
  584. }
  585. /* Call all ACPI methods here */
  586. /**
  587. * radeon_acpi_init - init driver acpi support
  588. *
  589. * @rdev: radeon_device pointer
  590. *
  591. * Verifies the AMD ACPI interfaces and registers with the acpi
  592. * notifier chain (all asics).
  593. * Returns 0 on success, error on failure.
  594. */
  595. int radeon_acpi_init(struct radeon_device *rdev)
  596. {
  597. acpi_handle handle;
  598. struct radeon_atif *atif = &rdev->atif;
  599. struct radeon_atcs *atcs = &rdev->atcs;
  600. int ret;
  601. /* Get the device handle */
  602. handle = ACPI_HANDLE(&rdev->pdev->dev);
  603. /* No need to proceed if we're sure that ATIF is not supported */
  604. if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
  605. return 0;
  606. /* Call the ATCS method */
  607. ret = radeon_atcs_verify_interface(handle, atcs);
  608. if (ret) {
  609. DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
  610. }
  611. /* Call the ATIF method */
  612. ret = radeon_atif_verify_interface(handle, atif);
  613. if (ret) {
  614. DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
  615. goto out;
  616. }
  617. if (atif->notifications.brightness_change) {
  618. struct drm_encoder *tmp;
  619. struct radeon_encoder *target = NULL;
  620. /* Find the encoder controlling the brightness */
  621. list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
  622. head) {
  623. struct radeon_encoder *enc = to_radeon_encoder(tmp);
  624. if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
  625. enc->enc_priv) {
  626. if (rdev->is_atom_bios) {
  627. struct radeon_encoder_atom_dig *dig = enc->enc_priv;
  628. if (dig->bl_dev) {
  629. target = enc;
  630. break;
  631. }
  632. } else {
  633. struct radeon_encoder_lvds *dig = enc->enc_priv;
  634. if (dig->bl_dev) {
  635. target = enc;
  636. break;
  637. }
  638. }
  639. }
  640. }
  641. atif->encoder_for_bl = target;
  642. if (!target) {
  643. /* Brightness change notification is enabled, but we
  644. * didn't find a backlight controller, this should
  645. * never happen.
  646. */
  647. DRM_ERROR("Cannot find a backlight controller\n");
  648. }
  649. }
  650. if (atif->functions.sbios_requests && !atif->functions.system_params) {
  651. /* XXX check this workraround, if sbios request function is
  652. * present we have to see how it's configured in the system
  653. * params
  654. */
  655. atif->functions.system_params = true;
  656. }
  657. if (atif->functions.system_params) {
  658. ret = radeon_atif_get_notification_params(handle,
  659. &atif->notification_cfg);
  660. if (ret) {
  661. DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
  662. ret);
  663. /* Disable notification */
  664. atif->notification_cfg.enabled = false;
  665. }
  666. }
  667. out:
  668. rdev->acpi_nb.notifier_call = radeon_acpi_event;
  669. register_acpi_notifier(&rdev->acpi_nb);
  670. return ret;
  671. }
  672. /**
  673. * radeon_acpi_fini - tear down driver acpi support
  674. *
  675. * @rdev: radeon_device pointer
  676. *
  677. * Unregisters with the acpi notifier chain (all asics).
  678. */
  679. void radeon_acpi_fini(struct radeon_device *rdev)
  680. {
  681. unregister_acpi_notifier(&rdev->acpi_nb);
  682. }