amdgpu_display.c 27 KB

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