amdgpu_display.c 25 KB

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