amdgpu_display.c 27 KB

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