amdgpu_dm.c 40 KB

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