radeon_acpi.c 22 KB

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