dce_virtual.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_pm.h"
  26. #include "amdgpu_i2c.h"
  27. #include "atom.h"
  28. #include "amdgpu_pll.h"
  29. #include "amdgpu_connectors.h"
  30. #ifdef CONFIG_DRM_AMDGPU_SI
  31. #include "dce_v6_0.h"
  32. #endif
  33. #ifdef CONFIG_DRM_AMDGPU_CIK
  34. #include "dce_v8_0.h"
  35. #endif
  36. #include "dce_v10_0.h"
  37. #include "dce_v11_0.h"
  38. #include "dce_virtual.h"
  39. #define DCE_VIRTUAL_VBLANK_PERIOD 16666666
  40. static void dce_virtual_set_display_funcs(struct amdgpu_device *adev);
  41. static void dce_virtual_set_irq_funcs(struct amdgpu_device *adev);
  42. static int dce_virtual_connector_encoder_init(struct amdgpu_device *adev,
  43. int index);
  44. /**
  45. * dce_virtual_vblank_wait - vblank wait asic callback.
  46. *
  47. * @adev: amdgpu_device pointer
  48. * @crtc: crtc to wait for vblank on
  49. *
  50. * Wait for vblank on the requested crtc (evergreen+).
  51. */
  52. static void dce_virtual_vblank_wait(struct amdgpu_device *adev, int crtc)
  53. {
  54. return;
  55. }
  56. static u32 dce_virtual_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  57. {
  58. return 0;
  59. }
  60. static void dce_virtual_page_flip(struct amdgpu_device *adev,
  61. int crtc_id, u64 crtc_base, bool async)
  62. {
  63. return;
  64. }
  65. static int dce_virtual_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  66. u32 *vbl, u32 *position)
  67. {
  68. *vbl = 0;
  69. *position = 0;
  70. return -EINVAL;
  71. }
  72. static bool dce_virtual_hpd_sense(struct amdgpu_device *adev,
  73. enum amdgpu_hpd_id hpd)
  74. {
  75. return true;
  76. }
  77. static void dce_virtual_hpd_set_polarity(struct amdgpu_device *adev,
  78. enum amdgpu_hpd_id hpd)
  79. {
  80. return;
  81. }
  82. static u32 dce_virtual_hpd_get_gpio_reg(struct amdgpu_device *adev)
  83. {
  84. return 0;
  85. }
  86. /**
  87. * dce_virtual_bandwidth_update - program display watermarks
  88. *
  89. * @adev: amdgpu_device pointer
  90. *
  91. * Calculate and program the display watermarks and line
  92. * buffer allocation (CIK).
  93. */
  94. static void dce_virtual_bandwidth_update(struct amdgpu_device *adev)
  95. {
  96. return;
  97. }
  98. static int dce_virtual_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  99. u16 *green, u16 *blue, uint32_t size,
  100. struct drm_modeset_acquire_ctx *ctx)
  101. {
  102. return 0;
  103. }
  104. static void dce_virtual_crtc_destroy(struct drm_crtc *crtc)
  105. {
  106. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  107. drm_crtc_cleanup(crtc);
  108. kfree(amdgpu_crtc);
  109. }
  110. static const struct drm_crtc_funcs dce_virtual_crtc_funcs = {
  111. .cursor_set2 = NULL,
  112. .cursor_move = NULL,
  113. .gamma_set = dce_virtual_crtc_gamma_set,
  114. .set_config = amdgpu_crtc_set_config,
  115. .destroy = dce_virtual_crtc_destroy,
  116. .page_flip_target = amdgpu_crtc_page_flip_target,
  117. };
  118. static void dce_virtual_crtc_dpms(struct drm_crtc *crtc, int mode)
  119. {
  120. struct drm_device *dev = crtc->dev;
  121. struct amdgpu_device *adev = dev->dev_private;
  122. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  123. unsigned type;
  124. if (amdgpu_sriov_vf(adev))
  125. return;
  126. switch (mode) {
  127. case DRM_MODE_DPMS_ON:
  128. amdgpu_crtc->enabled = true;
  129. /* Make sure VBLANK interrupts are still enabled */
  130. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  131. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  132. drm_crtc_vblank_on(crtc);
  133. break;
  134. case DRM_MODE_DPMS_STANDBY:
  135. case DRM_MODE_DPMS_SUSPEND:
  136. case DRM_MODE_DPMS_OFF:
  137. drm_crtc_vblank_off(crtc);
  138. amdgpu_crtc->enabled = false;
  139. break;
  140. }
  141. }
  142. static void dce_virtual_crtc_prepare(struct drm_crtc *crtc)
  143. {
  144. dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  145. }
  146. static void dce_virtual_crtc_commit(struct drm_crtc *crtc)
  147. {
  148. dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  149. }
  150. static void dce_virtual_crtc_disable(struct drm_crtc *crtc)
  151. {
  152. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  153. dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  154. if (crtc->primary->fb) {
  155. int r;
  156. struct amdgpu_framebuffer *amdgpu_fb;
  157. struct amdgpu_bo *abo;
  158. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  159. abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  160. r = amdgpu_bo_reserve(abo, true);
  161. if (unlikely(r))
  162. DRM_ERROR("failed to reserve abo before unpin\n");
  163. else {
  164. amdgpu_bo_unpin(abo);
  165. amdgpu_bo_unreserve(abo);
  166. }
  167. }
  168. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  169. amdgpu_crtc->encoder = NULL;
  170. amdgpu_crtc->connector = NULL;
  171. }
  172. static int dce_virtual_crtc_mode_set(struct drm_crtc *crtc,
  173. struct drm_display_mode *mode,
  174. struct drm_display_mode *adjusted_mode,
  175. int x, int y, struct drm_framebuffer *old_fb)
  176. {
  177. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  178. /* update the hw version fpr dpm */
  179. amdgpu_crtc->hw_mode = *adjusted_mode;
  180. return 0;
  181. }
  182. static bool dce_virtual_crtc_mode_fixup(struct drm_crtc *crtc,
  183. const struct drm_display_mode *mode,
  184. struct drm_display_mode *adjusted_mode)
  185. {
  186. return true;
  187. }
  188. static int dce_virtual_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  189. struct drm_framebuffer *old_fb)
  190. {
  191. return 0;
  192. }
  193. static int dce_virtual_crtc_set_base_atomic(struct drm_crtc *crtc,
  194. struct drm_framebuffer *fb,
  195. int x, int y, enum mode_set_atomic state)
  196. {
  197. return 0;
  198. }
  199. static const struct drm_crtc_helper_funcs dce_virtual_crtc_helper_funcs = {
  200. .dpms = dce_virtual_crtc_dpms,
  201. .mode_fixup = dce_virtual_crtc_mode_fixup,
  202. .mode_set = dce_virtual_crtc_mode_set,
  203. .mode_set_base = dce_virtual_crtc_set_base,
  204. .mode_set_base_atomic = dce_virtual_crtc_set_base_atomic,
  205. .prepare = dce_virtual_crtc_prepare,
  206. .commit = dce_virtual_crtc_commit,
  207. .disable = dce_virtual_crtc_disable,
  208. };
  209. static int dce_virtual_crtc_init(struct amdgpu_device *adev, int index)
  210. {
  211. struct amdgpu_crtc *amdgpu_crtc;
  212. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  213. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  214. if (amdgpu_crtc == NULL)
  215. return -ENOMEM;
  216. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_virtual_crtc_funcs);
  217. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  218. amdgpu_crtc->crtc_id = index;
  219. adev->mode_info.crtcs[index] = amdgpu_crtc;
  220. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  221. amdgpu_crtc->encoder = NULL;
  222. amdgpu_crtc->connector = NULL;
  223. amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
  224. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_virtual_crtc_helper_funcs);
  225. return 0;
  226. }
  227. static int dce_virtual_early_init(void *handle)
  228. {
  229. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  230. dce_virtual_set_display_funcs(adev);
  231. dce_virtual_set_irq_funcs(adev);
  232. adev->mode_info.num_hpd = 1;
  233. adev->mode_info.num_dig = 1;
  234. return 0;
  235. }
  236. static struct drm_encoder *
  237. dce_virtual_encoder(struct drm_connector *connector)
  238. {
  239. int enc_id = connector->encoder_ids[0];
  240. struct drm_encoder *encoder;
  241. int i;
  242. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  243. if (connector->encoder_ids[i] == 0)
  244. break;
  245. encoder = drm_encoder_find(connector->dev, NULL,
  246. connector->encoder_ids[i]);
  247. if (!encoder)
  248. continue;
  249. if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
  250. return encoder;
  251. }
  252. /* pick the first one */
  253. if (enc_id)
  254. return drm_encoder_find(connector->dev, NULL, enc_id);
  255. return NULL;
  256. }
  257. static int dce_virtual_get_modes(struct drm_connector *connector)
  258. {
  259. struct drm_device *dev = connector->dev;
  260. struct drm_display_mode *mode = NULL;
  261. unsigned i;
  262. static const struct mode_size {
  263. int w;
  264. int h;
  265. } common_modes[17] = {
  266. { 640, 480},
  267. { 720, 480},
  268. { 800, 600},
  269. { 848, 480},
  270. {1024, 768},
  271. {1152, 768},
  272. {1280, 720},
  273. {1280, 800},
  274. {1280, 854},
  275. {1280, 960},
  276. {1280, 1024},
  277. {1440, 900},
  278. {1400, 1050},
  279. {1680, 1050},
  280. {1600, 1200},
  281. {1920, 1080},
  282. {1920, 1200}
  283. };
  284. for (i = 0; i < 17; i++) {
  285. mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
  286. drm_mode_probed_add(connector, mode);
  287. }
  288. return 0;
  289. }
  290. static int dce_virtual_mode_valid(struct drm_connector *connector,
  291. struct drm_display_mode *mode)
  292. {
  293. return MODE_OK;
  294. }
  295. static int
  296. dce_virtual_dpms(struct drm_connector *connector, int mode)
  297. {
  298. return 0;
  299. }
  300. static int
  301. dce_virtual_set_property(struct drm_connector *connector,
  302. struct drm_property *property,
  303. uint64_t val)
  304. {
  305. return 0;
  306. }
  307. static void dce_virtual_destroy(struct drm_connector *connector)
  308. {
  309. drm_connector_unregister(connector);
  310. drm_connector_cleanup(connector);
  311. kfree(connector);
  312. }
  313. static void dce_virtual_force(struct drm_connector *connector)
  314. {
  315. return;
  316. }
  317. static const struct drm_connector_helper_funcs dce_virtual_connector_helper_funcs = {
  318. .get_modes = dce_virtual_get_modes,
  319. .mode_valid = dce_virtual_mode_valid,
  320. .best_encoder = dce_virtual_encoder,
  321. };
  322. static const struct drm_connector_funcs dce_virtual_connector_funcs = {
  323. .dpms = dce_virtual_dpms,
  324. .fill_modes = drm_helper_probe_single_connector_modes,
  325. .set_property = dce_virtual_set_property,
  326. .destroy = dce_virtual_destroy,
  327. .force = dce_virtual_force,
  328. };
  329. static int dce_virtual_sw_init(void *handle)
  330. {
  331. int r, i;
  332. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  333. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 229, &adev->crtc_irq);
  334. if (r)
  335. return r;
  336. adev->ddev->max_vblank_count = 0;
  337. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  338. adev->ddev->mode_config.max_width = 16384;
  339. adev->ddev->mode_config.max_height = 16384;
  340. adev->ddev->mode_config.preferred_depth = 24;
  341. adev->ddev->mode_config.prefer_shadow = 1;
  342. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  343. r = amdgpu_modeset_create_props(adev);
  344. if (r)
  345. return r;
  346. adev->ddev->mode_config.max_width = 16384;
  347. adev->ddev->mode_config.max_height = 16384;
  348. /* allocate crtcs, encoders, connectors */
  349. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  350. r = dce_virtual_crtc_init(adev, i);
  351. if (r)
  352. return r;
  353. r = dce_virtual_connector_encoder_init(adev, i);
  354. if (r)
  355. return r;
  356. }
  357. drm_kms_helper_poll_init(adev->ddev);
  358. adev->mode_info.mode_config_initialized = true;
  359. return 0;
  360. }
  361. static int dce_virtual_sw_fini(void *handle)
  362. {
  363. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  364. kfree(adev->mode_info.bios_hardcoded_edid);
  365. drm_kms_helper_poll_fini(adev->ddev);
  366. drm_mode_config_cleanup(adev->ddev);
  367. /* clear crtcs pointer to avoid dce irq finish routine access freed data */
  368. memset(adev->mode_info.crtcs, 0, sizeof(adev->mode_info.crtcs[0]) * AMDGPU_MAX_CRTCS);
  369. adev->mode_info.mode_config_initialized = false;
  370. return 0;
  371. }
  372. static int dce_virtual_hw_init(void *handle)
  373. {
  374. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  375. switch (adev->asic_type) {
  376. #ifdef CONFIG_DRM_AMDGPU_SI
  377. case CHIP_TAHITI:
  378. case CHIP_PITCAIRN:
  379. case CHIP_VERDE:
  380. case CHIP_OLAND:
  381. dce_v6_0_disable_dce(adev);
  382. break;
  383. #endif
  384. #ifdef CONFIG_DRM_AMDGPU_CIK
  385. case CHIP_BONAIRE:
  386. case CHIP_HAWAII:
  387. case CHIP_KAVERI:
  388. case CHIP_KABINI:
  389. case CHIP_MULLINS:
  390. dce_v8_0_disable_dce(adev);
  391. break;
  392. #endif
  393. case CHIP_FIJI:
  394. case CHIP_TONGA:
  395. dce_v10_0_disable_dce(adev);
  396. break;
  397. case CHIP_CARRIZO:
  398. case CHIP_STONEY:
  399. case CHIP_POLARIS11:
  400. case CHIP_POLARIS10:
  401. dce_v11_0_disable_dce(adev);
  402. break;
  403. case CHIP_TOPAZ:
  404. #ifdef CONFIG_DRM_AMDGPU_SI
  405. case CHIP_HAINAN:
  406. #endif
  407. /* no DCE */
  408. break;
  409. case CHIP_VEGA10:
  410. break;
  411. default:
  412. DRM_ERROR("Virtual display unsupported ASIC type: 0x%X\n", adev->asic_type);
  413. }
  414. return 0;
  415. }
  416. static int dce_virtual_hw_fini(void *handle)
  417. {
  418. return 0;
  419. }
  420. static int dce_virtual_suspend(void *handle)
  421. {
  422. return dce_virtual_hw_fini(handle);
  423. }
  424. static int dce_virtual_resume(void *handle)
  425. {
  426. return dce_virtual_hw_init(handle);
  427. }
  428. static bool dce_virtual_is_idle(void *handle)
  429. {
  430. return true;
  431. }
  432. static int dce_virtual_wait_for_idle(void *handle)
  433. {
  434. return 0;
  435. }
  436. static int dce_virtual_soft_reset(void *handle)
  437. {
  438. return 0;
  439. }
  440. static int dce_virtual_set_clockgating_state(void *handle,
  441. enum amd_clockgating_state state)
  442. {
  443. return 0;
  444. }
  445. static int dce_virtual_set_powergating_state(void *handle,
  446. enum amd_powergating_state state)
  447. {
  448. return 0;
  449. }
  450. static const struct amd_ip_funcs dce_virtual_ip_funcs = {
  451. .name = "dce_virtual",
  452. .early_init = dce_virtual_early_init,
  453. .late_init = NULL,
  454. .sw_init = dce_virtual_sw_init,
  455. .sw_fini = dce_virtual_sw_fini,
  456. .hw_init = dce_virtual_hw_init,
  457. .hw_fini = dce_virtual_hw_fini,
  458. .suspend = dce_virtual_suspend,
  459. .resume = dce_virtual_resume,
  460. .is_idle = dce_virtual_is_idle,
  461. .wait_for_idle = dce_virtual_wait_for_idle,
  462. .soft_reset = dce_virtual_soft_reset,
  463. .set_clockgating_state = dce_virtual_set_clockgating_state,
  464. .set_powergating_state = dce_virtual_set_powergating_state,
  465. };
  466. /* these are handled by the primary encoders */
  467. static void dce_virtual_encoder_prepare(struct drm_encoder *encoder)
  468. {
  469. return;
  470. }
  471. static void dce_virtual_encoder_commit(struct drm_encoder *encoder)
  472. {
  473. return;
  474. }
  475. static void
  476. dce_virtual_encoder_mode_set(struct drm_encoder *encoder,
  477. struct drm_display_mode *mode,
  478. struct drm_display_mode *adjusted_mode)
  479. {
  480. return;
  481. }
  482. static void dce_virtual_encoder_disable(struct drm_encoder *encoder)
  483. {
  484. return;
  485. }
  486. static void
  487. dce_virtual_encoder_dpms(struct drm_encoder *encoder, int mode)
  488. {
  489. return;
  490. }
  491. static bool dce_virtual_encoder_mode_fixup(struct drm_encoder *encoder,
  492. const struct drm_display_mode *mode,
  493. struct drm_display_mode *adjusted_mode)
  494. {
  495. return true;
  496. }
  497. static const struct drm_encoder_helper_funcs dce_virtual_encoder_helper_funcs = {
  498. .dpms = dce_virtual_encoder_dpms,
  499. .mode_fixup = dce_virtual_encoder_mode_fixup,
  500. .prepare = dce_virtual_encoder_prepare,
  501. .mode_set = dce_virtual_encoder_mode_set,
  502. .commit = dce_virtual_encoder_commit,
  503. .disable = dce_virtual_encoder_disable,
  504. };
  505. static void dce_virtual_encoder_destroy(struct drm_encoder *encoder)
  506. {
  507. drm_encoder_cleanup(encoder);
  508. kfree(encoder);
  509. }
  510. static const struct drm_encoder_funcs dce_virtual_encoder_funcs = {
  511. .destroy = dce_virtual_encoder_destroy,
  512. };
  513. static int dce_virtual_connector_encoder_init(struct amdgpu_device *adev,
  514. int index)
  515. {
  516. struct drm_encoder *encoder;
  517. struct drm_connector *connector;
  518. /* add a new encoder */
  519. encoder = kzalloc(sizeof(struct drm_encoder), GFP_KERNEL);
  520. if (!encoder)
  521. return -ENOMEM;
  522. encoder->possible_crtcs = 1 << index;
  523. drm_encoder_init(adev->ddev, encoder, &dce_virtual_encoder_funcs,
  524. DRM_MODE_ENCODER_VIRTUAL, NULL);
  525. drm_encoder_helper_add(encoder, &dce_virtual_encoder_helper_funcs);
  526. connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
  527. if (!connector) {
  528. kfree(encoder);
  529. return -ENOMEM;
  530. }
  531. /* add a new connector */
  532. drm_connector_init(adev->ddev, connector, &dce_virtual_connector_funcs,
  533. DRM_MODE_CONNECTOR_VIRTUAL);
  534. drm_connector_helper_add(connector, &dce_virtual_connector_helper_funcs);
  535. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  536. connector->interlace_allowed = false;
  537. connector->doublescan_allowed = false;
  538. drm_connector_register(connector);
  539. /* link them */
  540. drm_mode_connector_attach_encoder(connector, encoder);
  541. return 0;
  542. }
  543. static const struct amdgpu_display_funcs dce_virtual_display_funcs = {
  544. .bandwidth_update = &dce_virtual_bandwidth_update,
  545. .vblank_get_counter = &dce_virtual_vblank_get_counter,
  546. .vblank_wait = &dce_virtual_vblank_wait,
  547. .backlight_set_level = NULL,
  548. .backlight_get_level = NULL,
  549. .hpd_sense = &dce_virtual_hpd_sense,
  550. .hpd_set_polarity = &dce_virtual_hpd_set_polarity,
  551. .hpd_get_gpio_reg = &dce_virtual_hpd_get_gpio_reg,
  552. .page_flip = &dce_virtual_page_flip,
  553. .page_flip_get_scanoutpos = &dce_virtual_crtc_get_scanoutpos,
  554. .add_encoder = NULL,
  555. .add_connector = NULL,
  556. };
  557. static void dce_virtual_set_display_funcs(struct amdgpu_device *adev)
  558. {
  559. if (adev->mode_info.funcs == NULL)
  560. adev->mode_info.funcs = &dce_virtual_display_funcs;
  561. }
  562. static int dce_virtual_pageflip(struct amdgpu_device *adev,
  563. unsigned crtc_id)
  564. {
  565. unsigned long flags;
  566. struct amdgpu_crtc *amdgpu_crtc;
  567. struct amdgpu_flip_work *works;
  568. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  569. if (crtc_id >= adev->mode_info.num_crtc) {
  570. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  571. return -EINVAL;
  572. }
  573. /* IRQ could occur when in initial stage */
  574. if (amdgpu_crtc == NULL)
  575. return 0;
  576. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  577. works = amdgpu_crtc->pflip_works;
  578. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
  579. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  580. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  581. amdgpu_crtc->pflip_status,
  582. AMDGPU_FLIP_SUBMITTED);
  583. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  584. return 0;
  585. }
  586. /* page flip completed. clean up */
  587. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  588. amdgpu_crtc->pflip_works = NULL;
  589. /* wakeup usersapce */
  590. if (works->event)
  591. drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
  592. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  593. drm_crtc_vblank_put(&amdgpu_crtc->base);
  594. schedule_work(&works->unpin_work);
  595. return 0;
  596. }
  597. static enum hrtimer_restart dce_virtual_vblank_timer_handle(struct hrtimer *vblank_timer)
  598. {
  599. struct amdgpu_crtc *amdgpu_crtc = container_of(vblank_timer,
  600. struct amdgpu_crtc, vblank_timer);
  601. struct drm_device *ddev = amdgpu_crtc->base.dev;
  602. struct amdgpu_device *adev = ddev->dev_private;
  603. drm_handle_vblank(ddev, amdgpu_crtc->crtc_id);
  604. dce_virtual_pageflip(adev, amdgpu_crtc->crtc_id);
  605. hrtimer_start(vblank_timer, DCE_VIRTUAL_VBLANK_PERIOD,
  606. HRTIMER_MODE_REL);
  607. return HRTIMER_NORESTART;
  608. }
  609. static void dce_virtual_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  610. int crtc,
  611. enum amdgpu_interrupt_state state)
  612. {
  613. if (crtc >= adev->mode_info.num_crtc || !adev->mode_info.crtcs[crtc]) {
  614. DRM_DEBUG("invalid crtc %d\n", crtc);
  615. return;
  616. }
  617. if (state && !adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
  618. DRM_DEBUG("Enable software vsync timer\n");
  619. hrtimer_init(&adev->mode_info.crtcs[crtc]->vblank_timer,
  620. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  621. hrtimer_set_expires(&adev->mode_info.crtcs[crtc]->vblank_timer,
  622. DCE_VIRTUAL_VBLANK_PERIOD);
  623. adev->mode_info.crtcs[crtc]->vblank_timer.function =
  624. dce_virtual_vblank_timer_handle;
  625. hrtimer_start(&adev->mode_info.crtcs[crtc]->vblank_timer,
  626. DCE_VIRTUAL_VBLANK_PERIOD, HRTIMER_MODE_REL);
  627. } else if (!state && adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
  628. DRM_DEBUG("Disable software vsync timer\n");
  629. hrtimer_cancel(&adev->mode_info.crtcs[crtc]->vblank_timer);
  630. }
  631. adev->mode_info.crtcs[crtc]->vsync_timer_enabled = state;
  632. DRM_DEBUG("[FM]set crtc %d vblank interrupt state %d\n", crtc, state);
  633. }
  634. static int dce_virtual_set_crtc_irq_state(struct amdgpu_device *adev,
  635. struct amdgpu_irq_src *source,
  636. unsigned type,
  637. enum amdgpu_interrupt_state state)
  638. {
  639. if (type > AMDGPU_CRTC_IRQ_VBLANK6)
  640. return -EINVAL;
  641. dce_virtual_set_crtc_vblank_interrupt_state(adev, type, state);
  642. return 0;
  643. }
  644. static const struct amdgpu_irq_src_funcs dce_virtual_crtc_irq_funcs = {
  645. .set = dce_virtual_set_crtc_irq_state,
  646. .process = NULL,
  647. };
  648. static void dce_virtual_set_irq_funcs(struct amdgpu_device *adev)
  649. {
  650. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VBLANK6 + 1;
  651. adev->crtc_irq.funcs = &dce_virtual_crtc_irq_funcs;
  652. }
  653. const struct amdgpu_ip_block_version dce_virtual_ip_block =
  654. {
  655. .type = AMD_IP_BLOCK_TYPE_DCE,
  656. .major = 1,
  657. .minor = 0,
  658. .rev = 0,
  659. .funcs = &dce_virtual_ip_funcs,
  660. };