radeon_acpi.c 22 KB

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