amdgpu_display.c 27 KB

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