amdgpu_display.c 27 KB

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