amdgpu_dm.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /*
  2. * Copyright 2015 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. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services_types.h"
  26. #include "dc.h"
  27. #include "vid.h"
  28. #include "amdgpu.h"
  29. #include "atom.h"
  30. #include "amdgpu_dm.h"
  31. #include "amdgpu_dm_types.h"
  32. #include "amd_shared.h"
  33. #include "amdgpu_dm_irq.h"
  34. #include "dm_helpers.h"
  35. #include "ivsrcid/ivsrcid_vislands30.h"
  36. #include <linux/module.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/version.h>
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_mst_helper.h>
  42. #include "modules/inc/mod_freesync.h"
  43. /*
  44. * dm_vblank_get_counter
  45. *
  46. * @brief
  47. * Get counter for number of vertical blanks
  48. *
  49. * @param
  50. * struct amdgpu_device *adev - [in] desired amdgpu device
  51. * int disp_idx - [in] which CRTC to get the counter from
  52. *
  53. * @return
  54. * Counter for vertical blanks
  55. */
  56. static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  57. {
  58. if (crtc >= adev->mode_info.num_crtc)
  59. return 0;
  60. else {
  61. struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
  62. if (NULL == acrtc->target) {
  63. DRM_ERROR("dc_target is NULL for crtc '%d'!\n", crtc);
  64. return 0;
  65. }
  66. return dc_target_get_vblank_counter(acrtc->target);
  67. }
  68. }
  69. static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  70. u32 *vbl, u32 *position)
  71. {
  72. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  73. return -EINVAL;
  74. else {
  75. struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
  76. if (NULL == acrtc->target) {
  77. DRM_ERROR("dc_target is NULL for crtc '%d'!\n", crtc);
  78. return 0;
  79. }
  80. return dc_target_get_scanoutpos(acrtc->target, vbl, position);
  81. }
  82. return 0;
  83. }
  84. static bool dm_is_idle(void *handle)
  85. {
  86. /* XXX todo */
  87. return true;
  88. }
  89. static int dm_wait_for_idle(void *handle)
  90. {
  91. /* XXX todo */
  92. return 0;
  93. }
  94. static bool dm_check_soft_reset(void *handle)
  95. {
  96. return false;
  97. }
  98. static int dm_soft_reset(void *handle)
  99. {
  100. /* XXX todo */
  101. return 0;
  102. }
  103. static struct amdgpu_crtc *get_crtc_by_otg_inst(
  104. struct amdgpu_device *adev,
  105. int otg_inst)
  106. {
  107. struct drm_device *dev = adev->ddev;
  108. struct drm_crtc *crtc;
  109. struct amdgpu_crtc *amdgpu_crtc;
  110. /*
  111. * following if is check inherited from both functions where this one is
  112. * used now. Need to be checked why it could happen.
  113. */
  114. if (otg_inst == -1) {
  115. WARN_ON(1);
  116. return adev->mode_info.crtcs[0];
  117. }
  118. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  119. amdgpu_crtc = to_amdgpu_crtc(crtc);
  120. if (amdgpu_crtc->otg_inst == otg_inst)
  121. return amdgpu_crtc;
  122. }
  123. return NULL;
  124. }
  125. static void dm_pflip_high_irq(void *interrupt_params)
  126. {
  127. struct amdgpu_flip_work *works;
  128. struct amdgpu_crtc *amdgpu_crtc;
  129. struct common_irq_params *irq_params = interrupt_params;
  130. struct amdgpu_device *adev = irq_params->adev;
  131. unsigned long flags;
  132. amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
  133. /* IRQ could occur when in initial stage */
  134. /*TODO work and BO cleanup */
  135. if (amdgpu_crtc == NULL) {
  136. DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
  137. return;
  138. }
  139. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  140. works = amdgpu_crtc->pflip_works;
  141. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  142. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
  143. amdgpu_crtc->pflip_status,
  144. AMDGPU_FLIP_SUBMITTED,
  145. amdgpu_crtc->crtc_id,
  146. amdgpu_crtc);
  147. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  148. return;
  149. }
  150. /* page flip completed. clean up */
  151. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  152. amdgpu_crtc->pflip_works = NULL;
  153. /* wakeup usersapce */
  154. if (works->event)
  155. drm_crtc_send_vblank_event(&amdgpu_crtc->base,
  156. works->event);
  157. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  158. DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE, work: %p,\n",
  159. __func__, amdgpu_crtc->crtc_id, amdgpu_crtc, works);
  160. drm_crtc_vblank_put(&amdgpu_crtc->base);
  161. schedule_work(&works->unpin_work);
  162. }
  163. static void dm_crtc_high_irq(void *interrupt_params)
  164. {
  165. struct common_irq_params *irq_params = interrupt_params;
  166. struct amdgpu_device *adev = irq_params->adev;
  167. uint8_t crtc_index = 0;
  168. struct amdgpu_crtc *acrtc;
  169. acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
  170. if (acrtc)
  171. crtc_index = acrtc->crtc_id;
  172. drm_handle_vblank(adev->ddev, crtc_index);
  173. }
  174. static int dm_set_clockgating_state(void *handle,
  175. enum amd_clockgating_state state)
  176. {
  177. return 0;
  178. }
  179. static int dm_set_powergating_state(void *handle,
  180. enum amd_powergating_state state)
  181. {
  182. return 0;
  183. }
  184. /* Prototypes of private functions */
  185. static int dm_early_init(void* handle);
  186. static void hotplug_notify_work_func(struct work_struct *work)
  187. {
  188. struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
  189. struct drm_device *dev = dm->ddev;
  190. drm_kms_helper_hotplug_event(dev);
  191. }
  192. /* Init display KMS
  193. *
  194. * Returns 0 on success
  195. */
  196. int amdgpu_dm_init(struct amdgpu_device *adev)
  197. {
  198. struct dc_init_data init_data;
  199. adev->dm.ddev = adev->ddev;
  200. adev->dm.adev = adev;
  201. DRM_INFO("DAL is enabled\n");
  202. /* Zero all the fields */
  203. memset(&init_data, 0, sizeof(init_data));
  204. /* initialize DAL's lock (for SYNC context use) */
  205. spin_lock_init(&adev->dm.dal_lock);
  206. /* initialize DAL's mutex */
  207. mutex_init(&adev->dm.dal_mutex);
  208. if(amdgpu_dm_irq_init(adev)) {
  209. DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
  210. goto error;
  211. }
  212. init_data.asic_id.chip_family = adev->family;
  213. init_data.asic_id.pci_revision_id = adev->rev_id;
  214. init_data.asic_id.hw_internal_rev = adev->external_rev_id;
  215. init_data.asic_id.vram_width = adev->mc.vram_width;
  216. /* TODO: initialize init_data.asic_id.vram_type here!!!! */
  217. init_data.asic_id.atombios_base_address =
  218. adev->mode_info.atom_context->bios;
  219. init_data.driver = adev;
  220. adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
  221. if (!adev->dm.cgs_device) {
  222. DRM_ERROR("amdgpu: failed to create cgs device.\n");
  223. goto error;
  224. }
  225. init_data.cgs_device = adev->dm.cgs_device;
  226. adev->dm.dal = NULL;
  227. init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
  228. /* Display Core create. */
  229. adev->dm.dc = dc_create(&init_data);
  230. if (!adev->dm.dc)
  231. DRM_INFO("Display Core failed to initialize!\n");
  232. INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
  233. adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
  234. if (!adev->dm.freesync_module) {
  235. DRM_ERROR(
  236. "amdgpu: failed to initialize freesync_module.\n");
  237. } else
  238. DRM_INFO("amdgpu: freesync_module init done %p.\n",
  239. adev->dm.freesync_module);
  240. if (amdgpu_dm_initialize_drm_device(adev)) {
  241. DRM_ERROR(
  242. "amdgpu: failed to initialize sw for display support.\n");
  243. goto error;
  244. }
  245. /* Update the actual used number of crtc */
  246. adev->mode_info.num_crtc = adev->dm.display_indexes_num;
  247. /* TODO: Add_display_info? */
  248. /* TODO use dynamic cursor width */
  249. adev->ddev->mode_config.cursor_width = 128;
  250. adev->ddev->mode_config.cursor_height = 128;
  251. if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
  252. DRM_ERROR(
  253. "amdgpu: failed to initialize sw for display support.\n");
  254. goto error;
  255. }
  256. DRM_INFO("KMS initialized.\n");
  257. return 0;
  258. error:
  259. amdgpu_dm_fini(adev);
  260. return -1;
  261. }
  262. void amdgpu_dm_fini(struct amdgpu_device *adev)
  263. {
  264. amdgpu_dm_destroy_drm_device(&adev->dm);
  265. /*
  266. * TODO: pageflip, vlank interrupt
  267. *
  268. * amdgpu_dm_irq_fini(adev);
  269. */
  270. if (adev->dm.cgs_device) {
  271. amdgpu_cgs_destroy_device(adev->dm.cgs_device);
  272. adev->dm.cgs_device = NULL;
  273. }
  274. if (adev->dm.freesync_module) {
  275. mod_freesync_destroy(adev->dm.freesync_module);
  276. adev->dm.freesync_module = NULL;
  277. }
  278. /* DC Destroy TODO: Replace destroy DAL */
  279. {
  280. dc_destroy(&adev->dm.dc);
  281. }
  282. return;
  283. }
  284. /* moved from amdgpu_dm_kms.c */
  285. void amdgpu_dm_destroy()
  286. {
  287. }
  288. static int dm_sw_init(void *handle)
  289. {
  290. return 0;
  291. }
  292. static int dm_sw_fini(void *handle)
  293. {
  294. return 0;
  295. }
  296. static void detect_link_for_all_connectors(struct drm_device *dev)
  297. {
  298. struct amdgpu_connector *aconnector;
  299. struct drm_connector *connector;
  300. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  301. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  302. aconnector = to_amdgpu_connector(connector);
  303. if (aconnector->dc_link->type == dc_connection_mst_branch) {
  304. DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
  305. aconnector, aconnector->base.base.id);
  306. if (drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true) < 0) {
  307. DRM_ERROR("DM_MST: Failed to start MST\n");
  308. ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
  309. }
  310. }
  311. }
  312. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  313. }
  314. static void s3_handle_mst(struct drm_device *dev, bool suspend)
  315. {
  316. struct amdgpu_connector *aconnector;
  317. struct drm_connector *connector;
  318. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  319. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  320. aconnector = to_amdgpu_connector(connector);
  321. if (aconnector->dc_link->type == dc_connection_mst_branch &&
  322. !aconnector->mst_port) {
  323. if (suspend)
  324. drm_dp_mst_topology_mgr_suspend(&aconnector->mst_mgr);
  325. else
  326. drm_dp_mst_topology_mgr_resume(&aconnector->mst_mgr);
  327. }
  328. }
  329. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  330. }
  331. static int dm_hw_init(void *handle)
  332. {
  333. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  334. /* Create DAL display manager */
  335. amdgpu_dm_init(adev);
  336. amdgpu_dm_hpd_init(adev);
  337. detect_link_for_all_connectors(adev->ddev);
  338. return 0;
  339. }
  340. static int dm_hw_fini(void *handle)
  341. {
  342. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  343. amdgpu_dm_hpd_fini(adev);
  344. amdgpu_dm_irq_fini(adev);
  345. return 0;
  346. }
  347. static int dm_suspend(void *handle)
  348. {
  349. struct amdgpu_device *adev = handle;
  350. struct amdgpu_display_manager *dm = &adev->dm;
  351. int ret = 0;
  352. struct drm_crtc *crtc;
  353. s3_handle_mst(adev->ddev, true);
  354. /* flash all pending vblank events and turn interrupt off
  355. * before disabling CRTCs. They will be enabled back in
  356. * dm_display_resume
  357. */
  358. drm_modeset_lock_all(adev->ddev);
  359. list_for_each_entry(crtc, &adev->ddev->mode_config.crtc_list, head) {
  360. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  361. if (acrtc->target)
  362. drm_crtc_vblank_off(crtc);
  363. }
  364. drm_modeset_unlock_all(adev->ddev);
  365. amdgpu_dm_irq_suspend(adev);
  366. dc_set_power_state(
  367. dm->dc,
  368. DC_ACPI_CM_POWER_STATE_D3,
  369. DC_VIDEO_POWER_SUSPEND);
  370. return ret;
  371. }
  372. struct amdgpu_connector *amdgpu_dm_find_first_crct_matching_connector(
  373. struct drm_atomic_state *state,
  374. struct drm_crtc *crtc,
  375. bool from_state_var)
  376. {
  377. uint32_t i;
  378. struct drm_connector_state *conn_state;
  379. struct drm_connector *connector;
  380. struct drm_crtc *crtc_from_state;
  381. for_each_connector_in_state(
  382. state,
  383. connector,
  384. conn_state,
  385. i) {
  386. crtc_from_state =
  387. from_state_var ?
  388. conn_state->crtc :
  389. connector->state->crtc;
  390. if (crtc_from_state == crtc)
  391. return to_amdgpu_connector(connector);
  392. }
  393. return NULL;
  394. }
  395. static int dm_display_resume(struct drm_device *ddev)
  396. {
  397. int ret = 0;
  398. struct drm_connector *connector;
  399. struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
  400. struct drm_plane *plane;
  401. struct drm_crtc *crtc;
  402. struct amdgpu_connector *aconnector;
  403. struct drm_connector_state *conn_state;
  404. if (!state)
  405. return ENOMEM;
  406. state->acquire_ctx = ddev->mode_config.acquire_ctx;
  407. /* Construct an atomic state to restore previous display setting */
  408. /*
  409. * Attach connectors to drm_atomic_state
  410. * Should be done in the first place in order to make connectors
  411. * available in state during crtc state processing. It is used for
  412. * making decision if crtc should be disabled in case sink got
  413. * disconnected.
  414. *
  415. * Connectors state crtc with NULL dc_sink should be cleared, because it
  416. * will fail validation during commit
  417. */
  418. list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
  419. aconnector = to_amdgpu_connector(connector);
  420. conn_state = drm_atomic_get_connector_state(state, connector);
  421. ret = PTR_ERR_OR_ZERO(conn_state);
  422. if (ret)
  423. goto err;
  424. }
  425. /* Attach crtcs to drm_atomic_state*/
  426. list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
  427. struct drm_crtc_state *crtc_state =
  428. drm_atomic_get_crtc_state(state, crtc);
  429. ret = PTR_ERR_OR_ZERO(crtc_state);
  430. if (ret)
  431. goto err;
  432. /* force a restore */
  433. crtc_state->mode_changed = true;
  434. }
  435. /* Attach planes to drm_atomic_state */
  436. list_for_each_entry(plane, &ddev->mode_config.plane_list, head) {
  437. struct drm_crtc *crtc;
  438. struct drm_gem_object *obj;
  439. struct drm_framebuffer *fb;
  440. struct amdgpu_framebuffer *afb;
  441. struct amdgpu_bo *rbo;
  442. int r;
  443. struct drm_plane_state *plane_state = drm_atomic_get_plane_state(state, plane);
  444. ret = PTR_ERR_OR_ZERO(plane_state);
  445. if (ret)
  446. goto err;
  447. crtc = plane_state->crtc;
  448. fb = plane_state->fb;
  449. if (!crtc || !crtc->state || !crtc->state->active)
  450. continue;
  451. if (!fb) {
  452. DRM_DEBUG_KMS("No FB bound\n");
  453. return 0;
  454. }
  455. /*
  456. * Pin back the front buffers, cursor buffer was already pinned
  457. * back in amdgpu_resume_kms
  458. */
  459. afb = to_amdgpu_framebuffer(fb);
  460. obj = afb->obj;
  461. rbo = gem_to_amdgpu_bo(obj);
  462. r = amdgpu_bo_reserve(rbo, false);
  463. if (unlikely(r != 0))
  464. return r;
  465. r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, NULL);
  466. amdgpu_bo_unreserve(rbo);
  467. if (unlikely(r != 0)) {
  468. DRM_ERROR("Failed to pin framebuffer\n");
  469. return r;
  470. }
  471. }
  472. /* Call commit internally with the state we just constructed */
  473. ret = drm_atomic_commit(state);
  474. if (!ret)
  475. return 0;
  476. err:
  477. DRM_ERROR("Restoring old state failed with %i\n", ret);
  478. drm_atomic_state_put(state);
  479. return ret;
  480. }
  481. static int dm_resume(void *handle)
  482. {
  483. struct amdgpu_device *adev = handle;
  484. struct amdgpu_display_manager *dm = &adev->dm;
  485. /* power on hardware */
  486. dc_set_power_state(
  487. dm->dc,
  488. DC_ACPI_CM_POWER_STATE_D0,
  489. DC_VIDEO_POWER_ON);
  490. return 0;
  491. }
  492. int amdgpu_dm_display_resume(struct amdgpu_device *adev )
  493. {
  494. struct drm_device *ddev = adev->ddev;
  495. struct amdgpu_display_manager *dm = &adev->dm;
  496. struct amdgpu_connector *aconnector;
  497. struct drm_connector *connector;
  498. int ret = 0;
  499. struct drm_crtc *crtc;
  500. /* program HPD filter */
  501. dc_resume(dm->dc);
  502. /* On resume we need to rewrite the MSTM control bits to enamble MST*/
  503. s3_handle_mst(ddev, false);
  504. /*
  505. * early enable HPD Rx IRQ, should be done before set mode as short
  506. * pulse interrupts are used for MST
  507. */
  508. amdgpu_dm_irq_resume_early(adev);
  509. drm_modeset_lock_all(ddev);
  510. list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
  511. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  512. if (acrtc->target)
  513. drm_crtc_vblank_on(crtc);
  514. }
  515. drm_modeset_unlock_all(ddev);
  516. /* Do detection*/
  517. list_for_each_entry(connector,
  518. &ddev->mode_config.connector_list, head) {
  519. aconnector = to_amdgpu_connector(connector);
  520. /*
  521. * this is the case when traversing through already created
  522. * MST connectors, should be skipped
  523. */
  524. if (aconnector->mst_port)
  525. continue;
  526. dc_link_detect(aconnector->dc_link, false);
  527. aconnector->dc_sink = NULL;
  528. amdgpu_dm_update_connector_after_detect(aconnector);
  529. }
  530. drm_modeset_lock_all(ddev);
  531. ret = dm_display_resume(ddev);
  532. drm_modeset_unlock_all(ddev);
  533. amdgpu_dm_irq_resume(adev);
  534. return ret;
  535. }
  536. static const struct amd_ip_funcs amdgpu_dm_funcs = {
  537. .name = "dm",
  538. .early_init = dm_early_init,
  539. .late_init = NULL,
  540. .sw_init = dm_sw_init,
  541. .sw_fini = dm_sw_fini,
  542. .hw_init = dm_hw_init,
  543. .hw_fini = dm_hw_fini,
  544. .suspend = dm_suspend,
  545. .resume = dm_resume,
  546. .is_idle = dm_is_idle,
  547. .wait_for_idle = dm_wait_for_idle,
  548. .check_soft_reset = dm_check_soft_reset,
  549. .soft_reset = dm_soft_reset,
  550. .set_clockgating_state = dm_set_clockgating_state,
  551. .set_powergating_state = dm_set_powergating_state,
  552. };
  553. const struct amdgpu_ip_block_version dm_ip_block =
  554. {
  555. .type = AMD_IP_BLOCK_TYPE_DCE,
  556. .major = 1,
  557. .minor = 0,
  558. .rev = 0,
  559. .funcs = &amdgpu_dm_funcs,
  560. };
  561. /* TODO: it is temporary non-const, should fixed later */
  562. static struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
  563. .atomic_check = amdgpu_dm_atomic_check,
  564. .atomic_commit = amdgpu_dm_atomic_commit
  565. };
  566. void amdgpu_dm_update_connector_after_detect(
  567. struct amdgpu_connector *aconnector)
  568. {
  569. struct drm_connector *connector = &aconnector->base;
  570. struct drm_device *dev = connector->dev;
  571. const struct dc_sink *sink;
  572. /* MST handled by drm_mst framework */
  573. if (aconnector->mst_mgr.mst_state == true)
  574. return;
  575. sink = aconnector->dc_link->local_sink;
  576. /* Edid mgmt connector gets first update only in mode_valid hook and then
  577. * the connector sink is set to either fake or physical sink depends on link status.
  578. * don't do it here if u are during boot
  579. */
  580. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
  581. && aconnector->dc_em_sink) {
  582. /* For S3 resume with headless use eml_sink to fake target
  583. * because on resume connecotr->sink is set ti NULL
  584. */
  585. mutex_lock(&dev->mode_config.mutex);
  586. if (sink) {
  587. if (aconnector->dc_sink) {
  588. amdgpu_dm_remove_sink_from_freesync_module(
  589. connector);
  590. /* retain and release bellow are used for
  591. * bump up refcount for sink because the link don't point
  592. * to it anymore after disconnect so on next crtc to connector
  593. * reshuffle by UMD we will get into unwanted dc_sink release
  594. */
  595. if (aconnector->dc_sink != aconnector->dc_em_sink)
  596. dc_sink_release(aconnector->dc_sink);
  597. }
  598. aconnector->dc_sink = sink;
  599. amdgpu_dm_add_sink_to_freesync_module(
  600. connector, aconnector->edid);
  601. } else {
  602. amdgpu_dm_remove_sink_from_freesync_module(connector);
  603. if (!aconnector->dc_sink)
  604. aconnector->dc_sink = aconnector->dc_em_sink;
  605. else if (aconnector->dc_sink != aconnector->dc_em_sink)
  606. dc_sink_retain(aconnector->dc_sink);
  607. }
  608. mutex_unlock(&dev->mode_config.mutex);
  609. return;
  610. }
  611. /*
  612. * TODO: temporary guard to look for proper fix
  613. * if this sink is MST sink, we should not do anything
  614. */
  615. if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
  616. return;
  617. if (aconnector->dc_sink == sink) {
  618. /* We got a DP short pulse (Link Loss, DP CTS, etc...).
  619. * Do nothing!! */
  620. DRM_INFO("DCHPD: connector_id=%d: dc_sink didn't change.\n",
  621. aconnector->connector_id);
  622. return;
  623. }
  624. DRM_INFO("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
  625. aconnector->connector_id, aconnector->dc_sink, sink);
  626. mutex_lock(&dev->mode_config.mutex);
  627. /* 1. Update status of the drm connector
  628. * 2. Send an event and let userspace tell us what to do */
  629. if (sink) {
  630. /* TODO: check if we still need the S3 mode update workaround.
  631. * If yes, put it here. */
  632. if (aconnector->dc_sink)
  633. amdgpu_dm_remove_sink_from_freesync_module(
  634. connector);
  635. aconnector->dc_sink = sink;
  636. if (sink->dc_edid.length == 0)
  637. aconnector->edid = NULL;
  638. else {
  639. aconnector->edid =
  640. (struct edid *) sink->dc_edid.raw_edid;
  641. drm_mode_connector_update_edid_property(connector,
  642. aconnector->edid);
  643. }
  644. amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
  645. } else {
  646. amdgpu_dm_remove_sink_from_freesync_module(connector);
  647. drm_mode_connector_update_edid_property(connector, NULL);
  648. aconnector->num_modes = 0;
  649. aconnector->dc_sink = NULL;
  650. }
  651. mutex_unlock(&dev->mode_config.mutex);
  652. }
  653. static void handle_hpd_irq(void *param)
  654. {
  655. struct amdgpu_connector *aconnector = (struct amdgpu_connector *)param;
  656. struct drm_connector *connector = &aconnector->base;
  657. struct drm_device *dev = connector->dev;
  658. /* In case of failure or MST no need to update connector status or notify the OS
  659. * since (for MST case) MST does this in it's own context.
  660. */
  661. mutex_lock(&aconnector->hpd_lock);
  662. if (dc_link_detect(aconnector->dc_link, false)) {
  663. amdgpu_dm_update_connector_after_detect(aconnector);
  664. drm_modeset_lock_all(dev);
  665. dm_restore_drm_connector_state(dev, connector);
  666. drm_modeset_unlock_all(dev);
  667. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
  668. drm_kms_helper_hotplug_event(dev);
  669. }
  670. mutex_unlock(&aconnector->hpd_lock);
  671. }
  672. static void dm_handle_hpd_rx_irq(struct amdgpu_connector *aconnector)
  673. {
  674. uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
  675. uint8_t dret;
  676. bool new_irq_handled = false;
  677. int dpcd_addr;
  678. int dpcd_bytes_to_read;
  679. const int max_process_count = 30;
  680. int process_count = 0;
  681. const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
  682. if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
  683. dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
  684. /* DPCD 0x200 - 0x201 for downstream IRQ */
  685. dpcd_addr = DP_SINK_COUNT;
  686. } else {
  687. dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
  688. /* DPCD 0x2002 - 0x2005 for downstream IRQ */
  689. dpcd_addr = DP_SINK_COUNT_ESI;
  690. }
  691. dret = drm_dp_dpcd_read(
  692. &aconnector->dm_dp_aux.aux,
  693. dpcd_addr,
  694. esi,
  695. dpcd_bytes_to_read);
  696. while (dret == dpcd_bytes_to_read &&
  697. process_count < max_process_count) {
  698. uint8_t retry;
  699. dret = 0;
  700. process_count++;
  701. DRM_DEBUG_KMS("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
  702. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
  703. /* handle HPD short pulse irq */
  704. if (aconnector->mst_mgr.mst_state)
  705. drm_dp_mst_hpd_irq(
  706. &aconnector->mst_mgr,
  707. esi,
  708. &new_irq_handled);
  709. #endif
  710. if (new_irq_handled) {
  711. /* ACK at DPCD to notify down stream */
  712. const int ack_dpcd_bytes_to_write =
  713. dpcd_bytes_to_read - 1;
  714. for (retry = 0; retry < 3; retry++) {
  715. uint8_t wret;
  716. wret = drm_dp_dpcd_write(
  717. &aconnector->dm_dp_aux.aux,
  718. dpcd_addr + 1,
  719. &esi[1],
  720. ack_dpcd_bytes_to_write);
  721. if (wret == ack_dpcd_bytes_to_write)
  722. break;
  723. }
  724. /* check if there is new irq to be handle */
  725. dret = drm_dp_dpcd_read(
  726. &aconnector->dm_dp_aux.aux,
  727. dpcd_addr,
  728. esi,
  729. dpcd_bytes_to_read);
  730. new_irq_handled = false;
  731. } else
  732. break;
  733. }
  734. if (process_count == max_process_count)
  735. DRM_DEBUG_KMS("Loop exceeded max iterations\n");
  736. }
  737. static void handle_hpd_rx_irq(void *param)
  738. {
  739. struct amdgpu_connector *aconnector = (struct amdgpu_connector *)param;
  740. struct drm_connector *connector = &aconnector->base;
  741. struct drm_device *dev = connector->dev;
  742. const struct dc_link *dc_link = aconnector->dc_link;
  743. bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
  744. /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
  745. * conflict, after implement i2c helper, this mutex should be
  746. * retired.
  747. */
  748. if (aconnector->dc_link->type != dc_connection_mst_branch)
  749. mutex_lock(&aconnector->hpd_lock);
  750. if (dc_link_handle_hpd_rx_irq(aconnector->dc_link) &&
  751. !is_mst_root_connector) {
  752. /* Downstream Port status changed. */
  753. if (dc_link_detect(aconnector->dc_link, false)) {
  754. amdgpu_dm_update_connector_after_detect(aconnector);
  755. drm_modeset_lock_all(dev);
  756. dm_restore_drm_connector_state(dev, connector);
  757. drm_modeset_unlock_all(dev);
  758. drm_kms_helper_hotplug_event(dev);
  759. }
  760. }
  761. if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
  762. (dc_link->type == dc_connection_mst_branch))
  763. dm_handle_hpd_rx_irq(aconnector);
  764. if (aconnector->dc_link->type != dc_connection_mst_branch)
  765. mutex_unlock(&aconnector->hpd_lock);
  766. }
  767. static void register_hpd_handlers(struct amdgpu_device *adev)
  768. {
  769. struct drm_device *dev = adev->ddev;
  770. struct drm_connector *connector;
  771. struct amdgpu_connector *aconnector;
  772. const struct dc_link *dc_link;
  773. struct dc_interrupt_params int_params = {0};
  774. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  775. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  776. list_for_each_entry(connector,
  777. &dev->mode_config.connector_list, head) {
  778. aconnector = to_amdgpu_connector(connector);
  779. dc_link = aconnector->dc_link;
  780. if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
  781. int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
  782. int_params.irq_source = dc_link->irq_source_hpd;
  783. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  784. handle_hpd_irq,
  785. (void *) aconnector);
  786. }
  787. if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
  788. /* Also register for DP short pulse (hpd_rx). */
  789. int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
  790. int_params.irq_source = dc_link->irq_source_hpd_rx;
  791. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  792. handle_hpd_rx_irq,
  793. (void *) aconnector);
  794. }
  795. }
  796. }
  797. /* Register IRQ sources and initialize IRQ callbacks */
  798. static int dce110_register_irq_handlers(struct amdgpu_device *adev)
  799. {
  800. struct dc *dc = adev->dm.dc;
  801. struct common_irq_params *c_irq_params;
  802. struct dc_interrupt_params int_params = {0};
  803. int r;
  804. int i;
  805. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  806. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  807. /* Actions of amdgpu_irq_add_id():
  808. * 1. Register a set() function with base driver.
  809. * Base driver will call set() function to enable/disable an
  810. * interrupt in DC hardware.
  811. * 2. Register amdgpu_dm_irq_handler().
  812. * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
  813. * coming from DC hardware.
  814. * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
  815. * for acknowledging and handling. */
  816. for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT;
  817. i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
  818. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i, &adev->crtc_irq);
  819. if (r) {
  820. DRM_ERROR("Failed to add crtc irq id!\n");
  821. return r;
  822. }
  823. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  824. int_params.irq_source =
  825. dc_interrupt_to_irq_source(dc, i, 0);
  826. c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
  827. c_irq_params->adev = adev;
  828. c_irq_params->irq_src = int_params.irq_source;
  829. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  830. dm_crtc_high_irq, c_irq_params);
  831. }
  832. for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
  833. i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
  834. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i, &adev->pageflip_irq);
  835. if (r) {
  836. DRM_ERROR("Failed to add page flip irq id!\n");
  837. return r;
  838. }
  839. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  840. int_params.irq_source =
  841. dc_interrupt_to_irq_source(dc, i, 0);
  842. c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
  843. c_irq_params->adev = adev;
  844. c_irq_params->irq_src = int_params.irq_source;
  845. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  846. dm_pflip_high_irq, c_irq_params);
  847. }
  848. /* HPD */
  849. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A,
  850. &adev->hpd_irq);
  851. if (r) {
  852. DRM_ERROR("Failed to add hpd irq id!\n");
  853. return r;
  854. }
  855. register_hpd_handlers(adev);
  856. return 0;
  857. }
  858. static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
  859. {
  860. int r;
  861. adev->mode_info.mode_config_initialized = true;
  862. amdgpu_dm_mode_funcs.fb_create =
  863. amdgpu_mode_funcs.fb_create;
  864. amdgpu_dm_mode_funcs.output_poll_changed =
  865. amdgpu_mode_funcs.output_poll_changed;
  866. adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
  867. adev->ddev->mode_config.max_width = 16384;
  868. adev->ddev->mode_config.max_height = 16384;
  869. adev->ddev->mode_config.preferred_depth = 24;
  870. adev->ddev->mode_config.prefer_shadow = 1;
  871. /* indicate support of immediate flip */
  872. adev->ddev->mode_config.async_page_flip = true;
  873. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  874. r = amdgpu_modeset_create_props(adev);
  875. if (r)
  876. return r;
  877. return 0;
  878. }
  879. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  880. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  881. static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
  882. {
  883. struct amdgpu_display_manager *dm = bl_get_data(bd);
  884. if (dc_link_set_backlight_level(dm->backlight_link,
  885. bd->props.brightness, 0, 0))
  886. return 0;
  887. else
  888. return 1;
  889. }
  890. static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
  891. {
  892. return bd->props.brightness;
  893. }
  894. static const struct backlight_ops amdgpu_dm_backlight_ops = {
  895. .get_brightness = amdgpu_dm_backlight_get_brightness,
  896. .update_status = amdgpu_dm_backlight_update_status,
  897. };
  898. void amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
  899. {
  900. char bl_name[16];
  901. struct backlight_properties props = { 0 };
  902. props.max_brightness = AMDGPU_MAX_BL_LEVEL;
  903. props.type = BACKLIGHT_RAW;
  904. snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
  905. dm->adev->ddev->primary->index);
  906. dm->backlight_dev = backlight_device_register(bl_name,
  907. dm->adev->ddev->dev,
  908. dm,
  909. &amdgpu_dm_backlight_ops,
  910. &props);
  911. if (NULL == dm->backlight_dev)
  912. DRM_ERROR("DM: Backlight registration failed!\n");
  913. else
  914. DRM_INFO("DM: Registered Backlight device: %s\n", bl_name);
  915. }
  916. #endif
  917. /* In this architecture, the association
  918. * connector -> encoder -> crtc
  919. * id not really requried. The crtc and connector will hold the
  920. * display_index as an abstraction to use with DAL component
  921. *
  922. * Returns 0 on success
  923. */
  924. int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
  925. {
  926. struct amdgpu_display_manager *dm = &adev->dm;
  927. uint32_t i;
  928. struct amdgpu_connector *aconnector;
  929. struct amdgpu_encoder *aencoder;
  930. struct amdgpu_crtc *acrtc;
  931. uint32_t link_cnt;
  932. link_cnt = dm->dc->caps.max_links;
  933. if (amdgpu_dm_mode_config_init(dm->adev)) {
  934. DRM_ERROR("DM: Failed to initialize mode config\n");
  935. return -1;
  936. }
  937. for (i = 0; i < dm->dc->caps.max_targets; i++) {
  938. acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
  939. if (!acrtc)
  940. goto fail;
  941. if (amdgpu_dm_crtc_init(
  942. dm,
  943. acrtc,
  944. i)) {
  945. DRM_ERROR("KMS: Failed to initialize crtc\n");
  946. kfree(acrtc);
  947. goto fail;
  948. }
  949. }
  950. dm->display_indexes_num = dm->dc->caps.max_targets;
  951. /* loops over all connectors on the board */
  952. for (i = 0; i < link_cnt; i++) {
  953. if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
  954. DRM_ERROR(
  955. "KMS: Cannot support more than %d display indexes\n",
  956. AMDGPU_DM_MAX_DISPLAY_INDEX);
  957. continue;
  958. }
  959. aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
  960. if (!aconnector)
  961. goto fail;
  962. aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
  963. if (!aencoder) {
  964. goto fail_free_connector;
  965. }
  966. if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
  967. DRM_ERROR("KMS: Failed to initialize encoder\n");
  968. goto fail_free_encoder;
  969. }
  970. if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
  971. DRM_ERROR("KMS: Failed to initialize connector\n");
  972. goto fail_free_connector;
  973. }
  974. if (dc_link_detect(dc_get_link_at_index(dm->dc, i), true))
  975. amdgpu_dm_update_connector_after_detect(aconnector);
  976. }
  977. /* Software is initialized. Now we can register interrupt handlers. */
  978. switch (adev->asic_type) {
  979. case CHIP_BONAIRE:
  980. case CHIP_HAWAII:
  981. case CHIP_TONGA:
  982. case CHIP_FIJI:
  983. case CHIP_CARRIZO:
  984. case CHIP_STONEY:
  985. case CHIP_POLARIS11:
  986. case CHIP_POLARIS10:
  987. if (dce110_register_irq_handlers(dm->adev)) {
  988. DRM_ERROR("DM: Failed to initialize IRQ\n");
  989. return -1;
  990. }
  991. break;
  992. default:
  993. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  994. return -1;
  995. }
  996. drm_mode_config_reset(dm->ddev);
  997. return 0;
  998. fail_free_encoder:
  999. kfree(aencoder);
  1000. fail_free_connector:
  1001. kfree(aconnector);
  1002. fail:
  1003. return -1;
  1004. }
  1005. void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
  1006. {
  1007. drm_mode_config_cleanup(dm->ddev);
  1008. return;
  1009. }
  1010. /******************************************************************************
  1011. * amdgpu_display_funcs functions
  1012. *****************************************************************************/
  1013. /**
  1014. * dm_bandwidth_update - program display watermarks
  1015. *
  1016. * @adev: amdgpu_device pointer
  1017. *
  1018. * Calculate and program the display watermarks and line buffer allocation.
  1019. */
  1020. static void dm_bandwidth_update(struct amdgpu_device *adev)
  1021. {
  1022. /* TODO: implement later */
  1023. }
  1024. static void dm_set_backlight_level(struct amdgpu_encoder *amdgpu_encoder,
  1025. u8 level)
  1026. {
  1027. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1028. }
  1029. static u8 dm_get_backlight_level(struct amdgpu_encoder *amdgpu_encoder)
  1030. {
  1031. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1032. return 0;
  1033. }
  1034. /******************************************************************************
  1035. * Page Flip functions
  1036. ******************************************************************************/
  1037. /**
  1038. * dm_page_flip - called by amdgpu_flip_work_func(), which is triggered
  1039. * via DRM IOCTL, by user mode.
  1040. *
  1041. * @adev: amdgpu_device pointer
  1042. * @crtc_id: crtc to cleanup pageflip on
  1043. * @crtc_base: new address of the crtc (GPU MC address)
  1044. *
  1045. * Does the actual pageflip (surface address update).
  1046. */
  1047. static void dm_page_flip(struct amdgpu_device *adev,
  1048. int crtc_id, u64 crtc_base, bool async)
  1049. {
  1050. struct amdgpu_crtc *acrtc;
  1051. struct dc_target *target;
  1052. struct dc_flip_addrs addr = { {0} };
  1053. /*
  1054. * TODO risk of concurrency issues
  1055. *
  1056. * This should guarded by the dal_mutex but we can't do this since the
  1057. * caller uses a spin_lock on event_lock.
  1058. *
  1059. * If we wait on the dal_mutex a second page flip interrupt might come,
  1060. * spin on the event_lock, disabling interrupts while it does so. At
  1061. * this point the core can no longer be pre-empted and return to the
  1062. * thread that waited on the dal_mutex and we're deadlocked.
  1063. *
  1064. * With multiple cores the same essentially happens but might just take
  1065. * a little longer to lock up all cores.
  1066. *
  1067. * The reason we should lock on dal_mutex is so that we can be sure
  1068. * nobody messes with acrtc->target after we read and check its value.
  1069. *
  1070. * We might be able to fix our concurrency issues with a work queue
  1071. * where we schedule all work items (mode_set, page_flip, etc.) and
  1072. * execute them one by one. Care needs to be taken to still deal with
  1073. * any potential concurrency issues arising from interrupt calls.
  1074. */
  1075. acrtc = adev->mode_info.crtcs[crtc_id];
  1076. target = acrtc->target;
  1077. /*
  1078. * Received a page flip call after the display has been reset.
  1079. * Just return in this case. Everything should be clean-up on reset.
  1080. */
  1081. if (!target) {
  1082. WARN_ON(1);
  1083. return;
  1084. }
  1085. addr.address.grph.addr.low_part = lower_32_bits(crtc_base);
  1086. addr.address.grph.addr.high_part = upper_32_bits(crtc_base);
  1087. addr.flip_immediate = async;
  1088. DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
  1089. __func__,
  1090. addr.address.grph.addr.high_part,
  1091. addr.address.grph.addr.low_part);
  1092. dc_flip_surface_addrs(
  1093. adev->dm.dc,
  1094. dc_target_get_status(target)->surfaces,
  1095. &addr, 1);
  1096. }
  1097. static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
  1098. struct drm_file *filp)
  1099. {
  1100. struct mod_freesync_params freesync_params;
  1101. uint8_t num_targets;
  1102. uint8_t i;
  1103. struct dc_target *target;
  1104. struct amdgpu_device *adev = dev->dev_private;
  1105. int r = 0;
  1106. /* Get freesync enable flag from DRM */
  1107. num_targets = dc_get_current_target_count(adev->dm.dc);
  1108. for (i = 0; i < num_targets; i++) {
  1109. target = dc_get_target_at_index(adev->dm.dc, i);
  1110. mod_freesync_update_state(adev->dm.freesync_module,
  1111. target->streams,
  1112. target->stream_count,
  1113. &freesync_params);
  1114. }
  1115. return r;
  1116. }
  1117. static const struct amdgpu_display_funcs dm_display_funcs = {
  1118. .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
  1119. .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
  1120. .vblank_wait = NULL,
  1121. .backlight_set_level =
  1122. dm_set_backlight_level,/* called unconditionally */
  1123. .backlight_get_level =
  1124. dm_get_backlight_level,/* called unconditionally */
  1125. .hpd_sense = NULL,/* called unconditionally */
  1126. .hpd_set_polarity = NULL, /* called unconditionally */
  1127. .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
  1128. .page_flip = dm_page_flip, /* called unconditionally */
  1129. .page_flip_get_scanoutpos =
  1130. dm_crtc_get_scanoutpos,/* called unconditionally */
  1131. .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
  1132. .add_connector = NULL, /* VBIOS parsing. DAL does it. */
  1133. .notify_freesync = amdgpu_notify_freesync,
  1134. };
  1135. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1136. static ssize_t s3_debug_store(
  1137. struct device *device,
  1138. struct device_attribute *attr,
  1139. const char *buf,
  1140. size_t count)
  1141. {
  1142. int ret;
  1143. int s3_state;
  1144. struct pci_dev *pdev = to_pci_dev(device);
  1145. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  1146. struct amdgpu_device *adev = drm_dev->dev_private;
  1147. ret = kstrtoint(buf, 0, &s3_state);
  1148. if (ret == 0) {
  1149. if (s3_state) {
  1150. dm_resume(adev);
  1151. amdgpu_dm_display_resume(adev);
  1152. drm_kms_helper_hotplug_event(adev->ddev);
  1153. } else
  1154. dm_suspend(adev);
  1155. }
  1156. return ret == 0 ? count : 0;
  1157. }
  1158. DEVICE_ATTR_WO(s3_debug);
  1159. #endif
  1160. static int dm_early_init(void *handle)
  1161. {
  1162. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1163. amdgpu_dm_set_irq_funcs(adev);
  1164. switch (adev->asic_type) {
  1165. case CHIP_BONAIRE:
  1166. case CHIP_HAWAII:
  1167. adev->mode_info.num_crtc = 6;
  1168. adev->mode_info.num_hpd = 6;
  1169. adev->mode_info.num_dig = 6;
  1170. break;
  1171. case CHIP_FIJI:
  1172. case CHIP_TONGA:
  1173. adev->mode_info.num_crtc = 6;
  1174. adev->mode_info.num_hpd = 6;
  1175. adev->mode_info.num_dig = 7;
  1176. break;
  1177. case CHIP_CARRIZO:
  1178. adev->mode_info.num_crtc = 3;
  1179. adev->mode_info.num_hpd = 6;
  1180. adev->mode_info.num_dig = 9;
  1181. break;
  1182. case CHIP_STONEY:
  1183. adev->mode_info.num_crtc = 2;
  1184. adev->mode_info.num_hpd = 6;
  1185. adev->mode_info.num_dig = 9;
  1186. break;
  1187. case CHIP_POLARIS11:
  1188. adev->mode_info.num_crtc = 5;
  1189. adev->mode_info.num_hpd = 5;
  1190. adev->mode_info.num_dig = 5;
  1191. break;
  1192. case CHIP_POLARIS10:
  1193. adev->mode_info.num_crtc = 6;
  1194. adev->mode_info.num_hpd = 6;
  1195. adev->mode_info.num_dig = 6;
  1196. break;
  1197. default:
  1198. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1199. return -EINVAL;
  1200. }
  1201. if (adev->mode_info.funcs == NULL)
  1202. adev->mode_info.funcs = &dm_display_funcs;
  1203. /* Note: Do NOT change adev->audio_endpt_rreg and
  1204. * adev->audio_endpt_wreg because they are initialised in
  1205. * amdgpu_device_init() */
  1206. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1207. device_create_file(
  1208. adev->ddev->dev,
  1209. &dev_attr_s3_debug);
  1210. #endif
  1211. return 0;
  1212. }
  1213. bool amdgpu_dm_acquire_dal_lock(struct amdgpu_display_manager *dm)
  1214. {
  1215. /* TODO */
  1216. return true;
  1217. }
  1218. bool amdgpu_dm_release_dal_lock(struct amdgpu_display_manager *dm)
  1219. {
  1220. /* TODO */
  1221. return true;
  1222. }