amdgpu_display.c 27 KB

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