amdgpu_display.c 24 KB

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