amdgpu_dm.c 38 KB

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