radeon_acpi.c 22 KB

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