amdgpu_display.c 26 KB

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