amdgpu_display.c 25 KB

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