amdgpu_display.c 25 KB

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