amdgpu_display.c 27 KB

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