amdgpu_display.c 27 KB

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