amdgpu_display.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/amdgpu_drm.h>
  28. #include "amdgpu.h"
  29. #include "amdgpu_i2c.h"
  30. #include "atom.h"
  31. #include "amdgpu_connectors.h"
  32. #include "amdgpu_display.h"
  33. #include <asm/div64.h>
  34. #include <linux/pm_runtime.h>
  35. #include <drm/drm_crtc_helper.h>
  36. #include <drm/drm_edid.h>
  37. #include <drm/drm_gem_framebuffer_helper.h>
  38. #include <drm/drm_fb_helper.h>
  39. static void amdgpu_display_flip_callback(struct dma_fence *f,
  40. struct dma_fence_cb *cb)
  41. {
  42. struct amdgpu_flip_work *work =
  43. container_of(cb, struct amdgpu_flip_work, cb);
  44. dma_fence_put(f);
  45. schedule_work(&work->flip_work.work);
  46. }
  47. static bool amdgpu_display_flip_handle_fence(struct amdgpu_flip_work *work,
  48. struct dma_fence **f)
  49. {
  50. struct dma_fence *fence= *f;
  51. if (fence == NULL)
  52. return false;
  53. *f = NULL;
  54. if (!dma_fence_add_callback(fence, &work->cb,
  55. amdgpu_display_flip_callback))
  56. return true;
  57. dma_fence_put(fence);
  58. return false;
  59. }
  60. static void amdgpu_display_flip_work_func(struct work_struct *__work)
  61. {
  62. struct delayed_work *delayed_work =
  63. container_of(__work, struct delayed_work, work);
  64. struct amdgpu_flip_work *work =
  65. container_of(delayed_work, struct amdgpu_flip_work, flip_work);
  66. struct amdgpu_device *adev = work->adev;
  67. struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[work->crtc_id];
  68. struct drm_crtc *crtc = &amdgpu_crtc->base;
  69. unsigned long flags;
  70. unsigned i;
  71. int vpos, hpos;
  72. if (amdgpu_display_flip_handle_fence(work, &work->excl))
  73. return;
  74. for (i = 0; i < work->shared_count; ++i)
  75. if (amdgpu_display_flip_handle_fence(work, &work->shared[i]))
  76. return;
  77. /* Wait until we're out of the vertical blank period before the one
  78. * targeted by the flip
  79. */
  80. if (amdgpu_crtc->enabled &&
  81. (amdgpu_display_get_crtc_scanoutpos(adev->ddev, work->crtc_id, 0,
  82. &vpos, &hpos, NULL, NULL,
  83. &crtc->hwmode)
  84. & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
  85. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
  86. (int)(work->target_vblank -
  87. amdgpu_get_vblank_counter_kms(adev->ddev, amdgpu_crtc->crtc_id)) > 0) {
  88. schedule_delayed_work(&work->flip_work, usecs_to_jiffies(1000));
  89. return;
  90. }
  91. /* We borrow the event spin lock for protecting flip_status */
  92. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  93. /* Do the flip (mmio) */
  94. adev->mode_info.funcs->page_flip(adev, work->crtc_id, work->base, work->async);
  95. /* Set the flip status */
  96. amdgpu_crtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
  97. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  98. DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
  99. amdgpu_crtc->crtc_id, amdgpu_crtc, work);
  100. }
  101. /*
  102. * Handle unpin events outside the interrupt handler proper.
  103. */
  104. static void amdgpu_display_unpin_work_func(struct work_struct *__work)
  105. {
  106. struct amdgpu_flip_work *work =
  107. container_of(__work, struct amdgpu_flip_work, unpin_work);
  108. int r;
  109. /* unpin of the old buffer */
  110. r = amdgpu_bo_reserve(work->old_abo, true);
  111. if (likely(r == 0)) {
  112. r = amdgpu_bo_unpin(work->old_abo);
  113. if (unlikely(r != 0)) {
  114. DRM_ERROR("failed to unpin buffer after flip\n");
  115. }
  116. amdgpu_bo_unreserve(work->old_abo);
  117. } else
  118. DRM_ERROR("failed to reserve buffer after flip\n");
  119. amdgpu_bo_unref(&work->old_abo);
  120. kfree(work->shared);
  121. kfree(work);
  122. }
  123. int amdgpu_display_crtc_page_flip_target(struct drm_crtc *crtc,
  124. struct drm_framebuffer *fb,
  125. struct drm_pending_vblank_event *event,
  126. uint32_t page_flip_flags, uint32_t target,
  127. struct drm_modeset_acquire_ctx *ctx)
  128. {
  129. struct drm_device *dev = crtc->dev;
  130. struct amdgpu_device *adev = dev->dev_private;
  131. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  132. struct drm_gem_object *obj;
  133. struct amdgpu_flip_work *work;
  134. struct amdgpu_bo *new_abo;
  135. unsigned long flags;
  136. u64 tiling_flags;
  137. u64 base;
  138. int i, r;
  139. work = kzalloc(sizeof *work, GFP_KERNEL);
  140. if (work == NULL)
  141. return -ENOMEM;
  142. INIT_DELAYED_WORK(&work->flip_work, amdgpu_display_flip_work_func);
  143. INIT_WORK(&work->unpin_work, amdgpu_display_unpin_work_func);
  144. work->event = event;
  145. work->adev = adev;
  146. work->crtc_id = amdgpu_crtc->crtc_id;
  147. work->async = (page_flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
  148. /* schedule unpin of the old buffer */
  149. obj = crtc->primary->fb->obj[0];
  150. /* take a reference to the old object */
  151. work->old_abo = gem_to_amdgpu_bo(obj);
  152. amdgpu_bo_ref(work->old_abo);
  153. obj = fb->obj[0];
  154. new_abo = gem_to_amdgpu_bo(obj);
  155. /* pin the new buffer */
  156. r = amdgpu_bo_reserve(new_abo, false);
  157. if (unlikely(r != 0)) {
  158. DRM_ERROR("failed to reserve new abo buffer before flip\n");
  159. goto cleanup;
  160. }
  161. r = amdgpu_bo_pin(new_abo, amdgpu_display_supported_domains(adev), &base);
  162. if (unlikely(r != 0)) {
  163. DRM_ERROR("failed to pin new abo buffer before flip\n");
  164. goto unreserve;
  165. }
  166. r = reservation_object_get_fences_rcu(new_abo->tbo.resv, &work->excl,
  167. &work->shared_count,
  168. &work->shared);
  169. if (unlikely(r != 0)) {
  170. DRM_ERROR("failed to get fences for buffer\n");
  171. goto unpin;
  172. }
  173. amdgpu_bo_get_tiling_flags(new_abo, &tiling_flags);
  174. amdgpu_bo_unreserve(new_abo);
  175. work->base = base;
  176. work->target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
  177. amdgpu_get_vblank_counter_kms(dev, work->crtc_id);
  178. /* we borrow the event spin lock for protecting flip_wrok */
  179. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  180. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_NONE) {
  181. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  182. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  183. r = -EBUSY;
  184. goto pflip_cleanup;
  185. }
  186. amdgpu_crtc->pflip_status = AMDGPU_FLIP_PENDING;
  187. amdgpu_crtc->pflip_works = work;
  188. DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_PENDING, work: %p,\n",
  189. amdgpu_crtc->crtc_id, amdgpu_crtc, work);
  190. /* update crtc fb */
  191. crtc->primary->fb = fb;
  192. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  193. amdgpu_display_flip_work_func(&work->flip_work.work);
  194. return 0;
  195. pflip_cleanup:
  196. if (unlikely(amdgpu_bo_reserve(new_abo, false) != 0)) {
  197. DRM_ERROR("failed to reserve new abo in error path\n");
  198. goto cleanup;
  199. }
  200. unpin:
  201. if (unlikely(amdgpu_bo_unpin(new_abo) != 0)) {
  202. DRM_ERROR("failed to unpin new abo in error path\n");
  203. }
  204. unreserve:
  205. amdgpu_bo_unreserve(new_abo);
  206. cleanup:
  207. amdgpu_bo_unref(&work->old_abo);
  208. dma_fence_put(work->excl);
  209. for (i = 0; i < work->shared_count; ++i)
  210. dma_fence_put(work->shared[i]);
  211. kfree(work->shared);
  212. kfree(work);
  213. return r;
  214. }
  215. int amdgpu_display_crtc_set_config(struct drm_mode_set *set,
  216. struct drm_modeset_acquire_ctx *ctx)
  217. {
  218. struct drm_device *dev;
  219. struct amdgpu_device *adev;
  220. struct drm_crtc *crtc;
  221. bool active = false;
  222. int ret;
  223. if (!set || !set->crtc)
  224. return -EINVAL;
  225. dev = set->crtc->dev;
  226. ret = pm_runtime_get_sync(dev->dev);
  227. if (ret < 0)
  228. return ret;
  229. ret = drm_crtc_helper_set_config(set, ctx);
  230. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  231. if (crtc->enabled)
  232. active = true;
  233. pm_runtime_mark_last_busy(dev->dev);
  234. adev = dev->dev_private;
  235. /* if we have active crtcs and we don't have a power ref,
  236. take the current one */
  237. if (active && !adev->have_disp_power_ref) {
  238. adev->have_disp_power_ref = true;
  239. return ret;
  240. }
  241. /* if we have no active crtcs, then drop the power ref
  242. we got before */
  243. if (!active && adev->have_disp_power_ref) {
  244. pm_runtime_put_autosuspend(dev->dev);
  245. adev->have_disp_power_ref = false;
  246. }
  247. /* drop the power reference we got coming in here */
  248. pm_runtime_put_autosuspend(dev->dev);
  249. return ret;
  250. }
  251. static const char *encoder_names[41] = {
  252. "NONE",
  253. "INTERNAL_LVDS",
  254. "INTERNAL_TMDS1",
  255. "INTERNAL_TMDS2",
  256. "INTERNAL_DAC1",
  257. "INTERNAL_DAC2",
  258. "INTERNAL_SDVOA",
  259. "INTERNAL_SDVOB",
  260. "SI170B",
  261. "CH7303",
  262. "CH7301",
  263. "INTERNAL_DVO1",
  264. "EXTERNAL_SDVOA",
  265. "EXTERNAL_SDVOB",
  266. "TITFP513",
  267. "INTERNAL_LVTM1",
  268. "VT1623",
  269. "HDMI_SI1930",
  270. "HDMI_INTERNAL",
  271. "INTERNAL_KLDSCP_TMDS1",
  272. "INTERNAL_KLDSCP_DVO1",
  273. "INTERNAL_KLDSCP_DAC1",
  274. "INTERNAL_KLDSCP_DAC2",
  275. "SI178",
  276. "MVPU_FPGA",
  277. "INTERNAL_DDI",
  278. "VT1625",
  279. "HDMI_SI1932",
  280. "DP_AN9801",
  281. "DP_DP501",
  282. "INTERNAL_UNIPHY",
  283. "INTERNAL_KLDSCP_LVTMA",
  284. "INTERNAL_UNIPHY1",
  285. "INTERNAL_UNIPHY2",
  286. "NUTMEG",
  287. "TRAVIS",
  288. "INTERNAL_VCE",
  289. "INTERNAL_UNIPHY3",
  290. "HDMI_ANX9805",
  291. "INTERNAL_AMCLK",
  292. "VIRTUAL",
  293. };
  294. static const char *hpd_names[6] = {
  295. "HPD1",
  296. "HPD2",
  297. "HPD3",
  298. "HPD4",
  299. "HPD5",
  300. "HPD6",
  301. };
  302. void amdgpu_display_print_display_setup(struct drm_device *dev)
  303. {
  304. struct drm_connector *connector;
  305. struct amdgpu_connector *amdgpu_connector;
  306. struct drm_encoder *encoder;
  307. struct amdgpu_encoder *amdgpu_encoder;
  308. uint32_t devices;
  309. int i = 0;
  310. DRM_INFO("AMDGPU Display Connectors\n");
  311. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  312. amdgpu_connector = to_amdgpu_connector(connector);
  313. DRM_INFO("Connector %d:\n", i);
  314. DRM_INFO(" %s\n", connector->name);
  315. if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE)
  316. DRM_INFO(" %s\n", hpd_names[amdgpu_connector->hpd.hpd]);
  317. if (amdgpu_connector->ddc_bus) {
  318. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  319. amdgpu_connector->ddc_bus->rec.mask_clk_reg,
  320. amdgpu_connector->ddc_bus->rec.mask_data_reg,
  321. amdgpu_connector->ddc_bus->rec.a_clk_reg,
  322. amdgpu_connector->ddc_bus->rec.a_data_reg,
  323. amdgpu_connector->ddc_bus->rec.en_clk_reg,
  324. amdgpu_connector->ddc_bus->rec.en_data_reg,
  325. amdgpu_connector->ddc_bus->rec.y_clk_reg,
  326. amdgpu_connector->ddc_bus->rec.y_data_reg);
  327. if (amdgpu_connector->router.ddc_valid)
  328. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  329. amdgpu_connector->router.ddc_mux_control_pin,
  330. amdgpu_connector->router.ddc_mux_state);
  331. if (amdgpu_connector->router.cd_valid)
  332. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  333. amdgpu_connector->router.cd_mux_control_pin,
  334. amdgpu_connector->router.cd_mux_state);
  335. } else {
  336. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  337. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  338. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  339. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  340. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  341. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  342. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  343. }
  344. DRM_INFO(" Encoders:\n");
  345. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  346. amdgpu_encoder = to_amdgpu_encoder(encoder);
  347. devices = amdgpu_encoder->devices & amdgpu_connector->devices;
  348. if (devices) {
  349. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  350. DRM_INFO(" CRT1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  351. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  352. DRM_INFO(" CRT2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  353. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  354. DRM_INFO(" LCD1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  355. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  356. DRM_INFO(" DFP1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  357. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  358. DRM_INFO(" DFP2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  359. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  360. DRM_INFO(" DFP3: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  361. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  362. DRM_INFO(" DFP4: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  363. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  364. DRM_INFO(" DFP5: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  365. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  366. DRM_INFO(" DFP6: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  367. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  368. DRM_INFO(" TV1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  369. if (devices & ATOM_DEVICE_CV_SUPPORT)
  370. DRM_INFO(" CV: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
  371. }
  372. }
  373. i++;
  374. }
  375. }
  376. /**
  377. * amdgpu_display_ddc_probe
  378. *
  379. */
  380. bool amdgpu_display_ddc_probe(struct amdgpu_connector *amdgpu_connector,
  381. bool use_aux)
  382. {
  383. u8 out = 0x0;
  384. u8 buf[8];
  385. int ret;
  386. struct i2c_msg msgs[] = {
  387. {
  388. .addr = DDC_ADDR,
  389. .flags = 0,
  390. .len = 1,
  391. .buf = &out,
  392. },
  393. {
  394. .addr = DDC_ADDR,
  395. .flags = I2C_M_RD,
  396. .len = 8,
  397. .buf = buf,
  398. }
  399. };
  400. /* on hw with routers, select right port */
  401. if (amdgpu_connector->router.ddc_valid)
  402. amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
  403. if (use_aux) {
  404. ret = i2c_transfer(&amdgpu_connector->ddc_bus->aux.ddc, msgs, 2);
  405. } else {
  406. ret = i2c_transfer(&amdgpu_connector->ddc_bus->adapter, msgs, 2);
  407. }
  408. if (ret != 2)
  409. /* Couldn't find an accessible DDC on this connector */
  410. return false;
  411. /* Probe also for valid EDID header
  412. * EDID header starts with:
  413. * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
  414. * Only the first 6 bytes must be valid as
  415. * drm_edid_block_valid() can fix the last 2 bytes */
  416. if (drm_edid_header_is_valid(buf) < 6) {
  417. /* Couldn't find an accessible EDID on this
  418. * connector */
  419. return false;
  420. }
  421. return true;
  422. }
  423. static const struct drm_framebuffer_funcs amdgpu_fb_funcs = {
  424. .destroy = drm_gem_fb_destroy,
  425. .create_handle = drm_gem_fb_create_handle,
  426. };
  427. uint32_t amdgpu_display_supported_domains(struct amdgpu_device *adev)
  428. {
  429. uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
  430. #if defined(CONFIG_DRM_AMD_DC)
  431. if (adev->asic_type >= CHIP_CARRIZO && adev->asic_type < CHIP_RAVEN &&
  432. adev->flags & AMD_IS_APU &&
  433. amdgpu_device_asic_has_dc_support(adev->asic_type))
  434. domain |= AMDGPU_GEM_DOMAIN_GTT;
  435. #endif
  436. return domain;
  437. }
  438. int amdgpu_display_framebuffer_init(struct drm_device *dev,
  439. struct amdgpu_framebuffer *rfb,
  440. const struct drm_mode_fb_cmd2 *mode_cmd,
  441. struct drm_gem_object *obj)
  442. {
  443. int ret;
  444. rfb->base.obj[0] = obj;
  445. drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
  446. ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
  447. if (ret) {
  448. rfb->base.obj[0] = NULL;
  449. return ret;
  450. }
  451. return 0;
  452. }
  453. struct drm_framebuffer *
  454. amdgpu_display_user_framebuffer_create(struct drm_device *dev,
  455. struct drm_file *file_priv,
  456. const struct drm_mode_fb_cmd2 *mode_cmd)
  457. {
  458. struct drm_gem_object *obj;
  459. struct amdgpu_framebuffer *amdgpu_fb;
  460. int ret;
  461. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
  462. if (obj == NULL) {
  463. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  464. "can't create framebuffer\n", mode_cmd->handles[0]);
  465. return ERR_PTR(-ENOENT);
  466. }
  467. /* Handle is imported dma-buf, so cannot be migrated to VRAM for scanout */
  468. if (obj->import_attach) {
  469. DRM_DEBUG_KMS("Cannot create framebuffer from imported dma_buf\n");
  470. return ERR_PTR(-EINVAL);
  471. }
  472. amdgpu_fb = kzalloc(sizeof(*amdgpu_fb), GFP_KERNEL);
  473. if (amdgpu_fb == NULL) {
  474. drm_gem_object_put_unlocked(obj);
  475. return ERR_PTR(-ENOMEM);
  476. }
  477. ret = amdgpu_display_framebuffer_init(dev, amdgpu_fb, mode_cmd, obj);
  478. if (ret) {
  479. kfree(amdgpu_fb);
  480. drm_gem_object_put_unlocked(obj);
  481. return ERR_PTR(ret);
  482. }
  483. return &amdgpu_fb->base;
  484. }
  485. const struct drm_mode_config_funcs amdgpu_mode_funcs = {
  486. .fb_create = amdgpu_display_user_framebuffer_create,
  487. .output_poll_changed = drm_fb_helper_output_poll_changed,
  488. };
  489. static const struct drm_prop_enum_list amdgpu_underscan_enum_list[] =
  490. { { UNDERSCAN_OFF, "off" },
  491. { UNDERSCAN_ON, "on" },
  492. { UNDERSCAN_AUTO, "auto" },
  493. };
  494. static const struct drm_prop_enum_list amdgpu_audio_enum_list[] =
  495. { { AMDGPU_AUDIO_DISABLE, "off" },
  496. { AMDGPU_AUDIO_ENABLE, "on" },
  497. { AMDGPU_AUDIO_AUTO, "auto" },
  498. };
  499. /* XXX support different dither options? spatial, temporal, both, etc. */
  500. static const struct drm_prop_enum_list amdgpu_dither_enum_list[] =
  501. { { AMDGPU_FMT_DITHER_DISABLE, "off" },
  502. { AMDGPU_FMT_DITHER_ENABLE, "on" },
  503. };
  504. int amdgpu_display_modeset_create_props(struct amdgpu_device *adev)
  505. {
  506. int sz;
  507. adev->mode_info.coherent_mode_property =
  508. drm_property_create_range(adev->ddev, 0 , "coherent", 0, 1);
  509. if (!adev->mode_info.coherent_mode_property)
  510. return -ENOMEM;
  511. adev->mode_info.load_detect_property =
  512. drm_property_create_range(adev->ddev, 0, "load detection", 0, 1);
  513. if (!adev->mode_info.load_detect_property)
  514. return -ENOMEM;
  515. drm_mode_create_scaling_mode_property(adev->ddev);
  516. sz = ARRAY_SIZE(amdgpu_underscan_enum_list);
  517. adev->mode_info.underscan_property =
  518. drm_property_create_enum(adev->ddev, 0,
  519. "underscan",
  520. amdgpu_underscan_enum_list, sz);
  521. adev->mode_info.underscan_hborder_property =
  522. drm_property_create_range(adev->ddev, 0,
  523. "underscan hborder", 0, 128);
  524. if (!adev->mode_info.underscan_hborder_property)
  525. return -ENOMEM;
  526. adev->mode_info.underscan_vborder_property =
  527. drm_property_create_range(adev->ddev, 0,
  528. "underscan vborder", 0, 128);
  529. if (!adev->mode_info.underscan_vborder_property)
  530. return -ENOMEM;
  531. sz = ARRAY_SIZE(amdgpu_audio_enum_list);
  532. adev->mode_info.audio_property =
  533. drm_property_create_enum(adev->ddev, 0,
  534. "audio",
  535. amdgpu_audio_enum_list, sz);
  536. sz = ARRAY_SIZE(amdgpu_dither_enum_list);
  537. adev->mode_info.dither_property =
  538. drm_property_create_enum(adev->ddev, 0,
  539. "dither",
  540. amdgpu_dither_enum_list, sz);
  541. return 0;
  542. }
  543. void amdgpu_display_update_priority(struct amdgpu_device *adev)
  544. {
  545. /* adjustment options for the display watermarks */
  546. if ((amdgpu_disp_priority == 0) || (amdgpu_disp_priority > 2))
  547. adev->mode_info.disp_priority = 0;
  548. else
  549. adev->mode_info.disp_priority = amdgpu_disp_priority;
  550. }
  551. static bool amdgpu_display_is_hdtv_mode(const struct drm_display_mode *mode)
  552. {
  553. /* try and guess if this is a tv or a monitor */
  554. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  555. (mode->vdisplay == 576) || /* 576p */
  556. (mode->vdisplay == 720) || /* 720p */
  557. (mode->vdisplay == 1080)) /* 1080p */
  558. return true;
  559. else
  560. return false;
  561. }
  562. bool amdgpu_display_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  563. const struct drm_display_mode *mode,
  564. struct drm_display_mode *adjusted_mode)
  565. {
  566. struct drm_device *dev = crtc->dev;
  567. struct drm_encoder *encoder;
  568. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  569. struct amdgpu_encoder *amdgpu_encoder;
  570. struct drm_connector *connector;
  571. struct amdgpu_connector *amdgpu_connector;
  572. u32 src_v = 1, dst_v = 1;
  573. u32 src_h = 1, dst_h = 1;
  574. amdgpu_crtc->h_border = 0;
  575. amdgpu_crtc->v_border = 0;
  576. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  577. if (encoder->crtc != crtc)
  578. continue;
  579. amdgpu_encoder = to_amdgpu_encoder(encoder);
  580. connector = amdgpu_get_connector_for_encoder(encoder);
  581. amdgpu_connector = to_amdgpu_connector(connector);
  582. /* set scaling */
  583. if (amdgpu_encoder->rmx_type == RMX_OFF)
  584. amdgpu_crtc->rmx_type = RMX_OFF;
  585. else if (mode->hdisplay < amdgpu_encoder->native_mode.hdisplay ||
  586. mode->vdisplay < amdgpu_encoder->native_mode.vdisplay)
  587. amdgpu_crtc->rmx_type = amdgpu_encoder->rmx_type;
  588. else
  589. amdgpu_crtc->rmx_type = RMX_OFF;
  590. /* copy native mode */
  591. memcpy(&amdgpu_crtc->native_mode,
  592. &amdgpu_encoder->native_mode,
  593. sizeof(struct drm_display_mode));
  594. src_v = crtc->mode.vdisplay;
  595. dst_v = amdgpu_crtc->native_mode.vdisplay;
  596. src_h = crtc->mode.hdisplay;
  597. dst_h = amdgpu_crtc->native_mode.hdisplay;
  598. /* fix up for overscan on hdmi */
  599. if ((!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  600. ((amdgpu_encoder->underscan_type == UNDERSCAN_ON) ||
  601. ((amdgpu_encoder->underscan_type == UNDERSCAN_AUTO) &&
  602. drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
  603. amdgpu_display_is_hdtv_mode(mode)))) {
  604. if (amdgpu_encoder->underscan_hborder != 0)
  605. amdgpu_crtc->h_border = amdgpu_encoder->underscan_hborder;
  606. else
  607. amdgpu_crtc->h_border = (mode->hdisplay >> 5) + 16;
  608. if (amdgpu_encoder->underscan_vborder != 0)
  609. amdgpu_crtc->v_border = amdgpu_encoder->underscan_vborder;
  610. else
  611. amdgpu_crtc->v_border = (mode->vdisplay >> 5) + 16;
  612. amdgpu_crtc->rmx_type = RMX_FULL;
  613. src_v = crtc->mode.vdisplay;
  614. dst_v = crtc->mode.vdisplay - (amdgpu_crtc->v_border * 2);
  615. src_h = crtc->mode.hdisplay;
  616. dst_h = crtc->mode.hdisplay - (amdgpu_crtc->h_border * 2);
  617. }
  618. }
  619. if (amdgpu_crtc->rmx_type != RMX_OFF) {
  620. fixed20_12 a, b;
  621. a.full = dfixed_const(src_v);
  622. b.full = dfixed_const(dst_v);
  623. amdgpu_crtc->vsc.full = dfixed_div(a, b);
  624. a.full = dfixed_const(src_h);
  625. b.full = dfixed_const(dst_h);
  626. amdgpu_crtc->hsc.full = dfixed_div(a, b);
  627. } else {
  628. amdgpu_crtc->vsc.full = dfixed_const(1);
  629. amdgpu_crtc->hsc.full = dfixed_const(1);
  630. }
  631. return true;
  632. }
  633. /*
  634. * Retrieve current video scanout position of crtc on a given gpu, and
  635. * an optional accurate timestamp of when query happened.
  636. *
  637. * \param dev Device to query.
  638. * \param pipe Crtc to query.
  639. * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
  640. * For driver internal use only also supports these flags:
  641. *
  642. * USE_REAL_VBLANKSTART to use the real start of vblank instead
  643. * of a fudged earlier start of vblank.
  644. *
  645. * GET_DISTANCE_TO_VBLANKSTART to return distance to the
  646. * fudged earlier start of vblank in *vpos and the distance
  647. * to true start of vblank in *hpos.
  648. *
  649. * \param *vpos Location where vertical scanout position should be stored.
  650. * \param *hpos Location where horizontal scanout position should go.
  651. * \param *stime Target location for timestamp taken immediately before
  652. * scanout position query. Can be NULL to skip timestamp.
  653. * \param *etime Target location for timestamp taken immediately after
  654. * scanout position query. Can be NULL to skip timestamp.
  655. *
  656. * Returns vpos as a positive number while in active scanout area.
  657. * Returns vpos as a negative number inside vblank, counting the number
  658. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  659. * until start of active scanout / end of vblank."
  660. *
  661. * \return Flags, or'ed together as follows:
  662. *
  663. * DRM_SCANOUTPOS_VALID = Query successful.
  664. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  665. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  666. * this flag means that returned position may be offset by a constant but
  667. * unknown small number of scanlines wrt. real scanout position.
  668. *
  669. */
  670. int amdgpu_display_get_crtc_scanoutpos(struct drm_device *dev,
  671. unsigned int pipe, unsigned int flags, int *vpos,
  672. int *hpos, ktime_t *stime, ktime_t *etime,
  673. const struct drm_display_mode *mode)
  674. {
  675. u32 vbl = 0, position = 0;
  676. int vbl_start, vbl_end, vtotal, ret = 0;
  677. bool in_vbl = true;
  678. struct amdgpu_device *adev = dev->dev_private;
  679. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  680. /* Get optional system timestamp before query. */
  681. if (stime)
  682. *stime = ktime_get();
  683. if (amdgpu_display_page_flip_get_scanoutpos(adev, pipe, &vbl, &position) == 0)
  684. ret |= DRM_SCANOUTPOS_VALID;
  685. /* Get optional system timestamp after query. */
  686. if (etime)
  687. *etime = ktime_get();
  688. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  689. /* Decode into vertical and horizontal scanout position. */
  690. *vpos = position & 0x1fff;
  691. *hpos = (position >> 16) & 0x1fff;
  692. /* Valid vblank area boundaries from gpu retrieved? */
  693. if (vbl > 0) {
  694. /* Yes: Decode. */
  695. ret |= DRM_SCANOUTPOS_ACCURATE;
  696. vbl_start = vbl & 0x1fff;
  697. vbl_end = (vbl >> 16) & 0x1fff;
  698. }
  699. else {
  700. /* No: Fake something reasonable which gives at least ok results. */
  701. vbl_start = mode->crtc_vdisplay;
  702. vbl_end = 0;
  703. }
  704. /* Called from driver internal vblank counter query code? */
  705. if (flags & GET_DISTANCE_TO_VBLANKSTART) {
  706. /* Caller wants distance from real vbl_start in *hpos */
  707. *hpos = *vpos - vbl_start;
  708. }
  709. /* Fudge vblank to start a few scanlines earlier to handle the
  710. * problem that vblank irqs fire a few scanlines before start
  711. * of vblank. Some driver internal callers need the true vblank
  712. * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
  713. *
  714. * The cause of the "early" vblank irq is that the irq is triggered
  715. * by the line buffer logic when the line buffer read position enters
  716. * the vblank, whereas our crtc scanout position naturally lags the
  717. * line buffer read position.
  718. */
  719. if (!(flags & USE_REAL_VBLANKSTART))
  720. vbl_start -= adev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
  721. /* Test scanout position against vblank region. */
  722. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  723. in_vbl = false;
  724. /* In vblank? */
  725. if (in_vbl)
  726. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  727. /* Called from driver internal vblank counter query code? */
  728. if (flags & GET_DISTANCE_TO_VBLANKSTART) {
  729. /* Caller wants distance from fudged earlier vbl_start */
  730. *vpos -= vbl_start;
  731. return ret;
  732. }
  733. /* Check if inside vblank area and apply corrective offsets:
  734. * vpos will then be >=0 in video scanout area, but negative
  735. * within vblank area, counting down the number of lines until
  736. * start of scanout.
  737. */
  738. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  739. if (in_vbl && (*vpos >= vbl_start)) {
  740. vtotal = mode->crtc_vtotal;
  741. *vpos = *vpos - vtotal;
  742. }
  743. /* Correct for shifted end of vbl at vbl_end. */
  744. *vpos = *vpos - vbl_end;
  745. return ret;
  746. }
  747. int amdgpu_display_crtc_idx_to_irq_type(struct amdgpu_device *adev, int crtc)
  748. {
  749. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  750. return AMDGPU_CRTC_IRQ_NONE;
  751. switch (crtc) {
  752. case 0:
  753. return AMDGPU_CRTC_IRQ_VBLANK1;
  754. case 1:
  755. return AMDGPU_CRTC_IRQ_VBLANK2;
  756. case 2:
  757. return AMDGPU_CRTC_IRQ_VBLANK3;
  758. case 3:
  759. return AMDGPU_CRTC_IRQ_VBLANK4;
  760. case 4:
  761. return AMDGPU_CRTC_IRQ_VBLANK5;
  762. case 5:
  763. return AMDGPU_CRTC_IRQ_VBLANK6;
  764. default:
  765. return AMDGPU_CRTC_IRQ_NONE;
  766. }
  767. }