amdgpu_display.c 27 KB

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