amdgpu_display.c 26 KB

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