amdgpu_display.c 25 KB

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