amdgpu_display.c 28 KB

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