blob: 2cb53c24dec01bfdc648708acd4a30703fda71e3 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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
34#include <linux/pm_runtime.h>
35#include <drm/drm_crtc_helper.h>
36#include <drm/drm_edid.h>
37
Christian Königc3874b72016-02-11 15:48:30 +010038static void amdgpu_flip_callback(struct fence *f, struct fence_cb *cb)
Christian König1ffd2652015-08-11 17:29:52 +020039{
Christian Königc3874b72016-02-11 15:48:30 +010040 struct amdgpu_flip_work *work =
41 container_of(cb, struct amdgpu_flip_work, cb);
Christian König1ffd2652015-08-11 17:29:52 +020042
Christian Königc3874b72016-02-11 15:48:30 +010043 fence_put(f);
Christian König87d58c12016-02-11 17:31:37 +010044 schedule_work(&work->flip_work);
Christian Königc3874b72016-02-11 15:48:30 +010045}
Christian König1ffd2652015-08-11 17:29:52 +020046
Christian Königc3874b72016-02-11 15:48:30 +010047static bool amdgpu_flip_handle_fence(struct amdgpu_flip_work *work,
48 struct fence **f)
49{
50 struct fence *fence= *f;
Christian König1ffd2652015-08-11 17:29:52 +020051
Christian Königc3874b72016-02-11 15:48:30 +010052 if (fence == NULL)
53 return false;
54
Christian König1ffd2652015-08-11 17:29:52 +020055 *f = NULL;
Christian Königc3874b72016-02-11 15:48:30 +010056
57 if (!fence_add_callback(fence, &work->cb, amdgpu_flip_callback))
58 return true;
59
60 fence_put(*f);
61 return false;
Christian König1ffd2652015-08-11 17:29:52 +020062}
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063
64static void amdgpu_flip_work_func(struct work_struct *__work)
65{
66 struct amdgpu_flip_work *work =
67 container_of(__work, struct amdgpu_flip_work, flip_work);
68 struct amdgpu_device *adev = work->adev;
69 struct amdgpu_crtc *amdgpuCrtc = adev->mode_info.crtcs[work->crtc_id];
70
71 struct drm_crtc *crtc = &amdgpuCrtc->base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 unsigned long flags;
Christian König1ffd2652015-08-11 17:29:52 +020073 unsigned i;
Alex Deucher8e36f9d2015-12-03 12:31:56 -050074 int vpos, hpos, stat, min_udelay;
75 struct drm_vblank_crtc *vblank = &crtc->dev->vblank[work->crtc_id];
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076
Christian Königc3874b72016-02-11 15:48:30 +010077 if (amdgpu_flip_handle_fence(work, &work->excl))
78 return;
79
Christian König1ffd2652015-08-11 17:29:52 +020080 for (i = 0; i < work->shared_count; ++i)
Christian Königc3874b72016-02-11 15:48:30 +010081 if (amdgpu_flip_handle_fence(work, &work->shared[i]))
82 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083
84 /* We borrow the event spin lock for protecting flip_status */
85 spin_lock_irqsave(&crtc->dev->event_lock, flags);
86
Alex Deucher8e36f9d2015-12-03 12:31:56 -050087 /* If this happens to execute within the "virtually extended" vblank
88 * interval before the start of the real vblank interval then it needs
89 * to delay programming the mmio flip until the real vblank is entered.
90 * This prevents completing a flip too early due to the way we fudge
91 * our vblank counter and vblank timestamps in order to work around the
92 * problem that the hw fires vblank interrupts before actual start of
93 * vblank (when line buffer refilling is done for a frame). It
94 * complements the fudging logic in amdgpu_get_crtc_scanoutpos() for
95 * timestamping and amdgpu_get_vblank_counter_kms() for vblank counts.
96 *
97 * In practice this won't execute very often unless on very fast
98 * machines because the time window for this to happen is very small.
99 */
100 for (;;) {
101 /* GET_DISTANCE_TO_VBLANKSTART returns distance to real vblank
102 * start in hpos, and to the "fudged earlier" vblank start in
103 * vpos.
104 */
105 stat = amdgpu_get_crtc_scanoutpos(adev->ddev, work->crtc_id,
106 GET_DISTANCE_TO_VBLANKSTART,
107 &vpos, &hpos, NULL, NULL,
108 &crtc->hwmode);
109
110 if ((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
111 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE) ||
112 !(vpos >= 0 && hpos <= 0))
113 break;
114
115 /* Sleep at least until estimated real start of hw vblank */
116 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
117 min_udelay = (-hpos + 1) * max(vblank->linedur_ns / 1000, 5);
118 usleep_range(min_udelay, 2 * min_udelay);
119 spin_lock_irqsave(&crtc->dev->event_lock, flags);
120 };
121
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 /* set the flip status */
123 amdgpuCrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Vitaly Prosyak6bd9e872015-10-20 15:02:03 -0400125
126 /* Do the flip (mmio) */
127 adev->mode_info.funcs->page_flip(adev, work->crtc_id, work->base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128}
129
130/*
131 * Handle unpin events outside the interrupt handler proper.
132 */
133static void amdgpu_unpin_work_func(struct work_struct *__work)
134{
135 struct amdgpu_flip_work *work =
136 container_of(__work, struct amdgpu_flip_work, unpin_work);
137 int r;
138
139 /* unpin of the old buffer */
140 r = amdgpu_bo_reserve(work->old_rbo, false);
141 if (likely(r == 0)) {
142 r = amdgpu_bo_unpin(work->old_rbo);
143 if (unlikely(r != 0)) {
144 DRM_ERROR("failed to unpin buffer after flip\n");
145 }
146 amdgpu_bo_unreserve(work->old_rbo);
147 } else
148 DRM_ERROR("failed to reserve buffer after flip\n");
149
Christian Könige9d951a2015-12-03 19:55:51 +0100150 amdgpu_bo_unref(&work->old_rbo);
Christian König1ffd2652015-08-11 17:29:52 +0200151 kfree(work->shared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152 kfree(work);
153}
154
155int amdgpu_crtc_page_flip(struct drm_crtc *crtc,
156 struct drm_framebuffer *fb,
157 struct drm_pending_vblank_event *event,
158 uint32_t page_flip_flags)
159{
160 struct drm_device *dev = crtc->dev;
161 struct amdgpu_device *adev = dev->dev_private;
162 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
163 struct amdgpu_framebuffer *old_amdgpu_fb;
164 struct amdgpu_framebuffer *new_amdgpu_fb;
165 struct drm_gem_object *obj;
166 struct amdgpu_flip_work *work;
167 struct amdgpu_bo *new_rbo;
168 unsigned long flags;
169 u64 tiling_flags;
170 u64 base;
Christian König1ffd2652015-08-11 17:29:52 +0200171 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172
173 work = kzalloc(sizeof *work, GFP_KERNEL);
174 if (work == NULL)
175 return -ENOMEM;
176
177 INIT_WORK(&work->flip_work, amdgpu_flip_work_func);
178 INIT_WORK(&work->unpin_work, amdgpu_unpin_work_func);
179
180 work->event = event;
181 work->adev = adev;
182 work->crtc_id = amdgpu_crtc->crtc_id;
183
184 /* schedule unpin of the old buffer */
185 old_amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
186 obj = old_amdgpu_fb->obj;
187
188 /* take a reference to the old object */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189 work->old_rbo = gem_to_amdgpu_bo(obj);
Christian Könige9d951a2015-12-03 19:55:51 +0100190 amdgpu_bo_ref(work->old_rbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191
192 new_amdgpu_fb = to_amdgpu_framebuffer(fb);
193 obj = new_amdgpu_fb->obj;
194 new_rbo = gem_to_amdgpu_bo(obj);
195
196 /* pin the new buffer */
197 r = amdgpu_bo_reserve(new_rbo, false);
198 if (unlikely(r != 0)) {
199 DRM_ERROR("failed to reserve new rbo buffer before flip\n");
200 goto cleanup;
201 }
202
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800203 r = amdgpu_bo_pin_restricted(new_rbo, AMDGPU_GEM_DOMAIN_VRAM, 0, 0, &base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 if (unlikely(r != 0)) {
205 amdgpu_bo_unreserve(new_rbo);
206 r = -EINVAL;
207 DRM_ERROR("failed to pin new rbo buffer before flip\n");
208 goto cleanup;
209 }
210
Christian König1ffd2652015-08-11 17:29:52 +0200211 r = reservation_object_get_fences_rcu(new_rbo->tbo.resv, &work->excl,
212 &work->shared_count,
213 &work->shared);
214 if (unlikely(r != 0)) {
215 amdgpu_bo_unreserve(new_rbo);
216 DRM_ERROR("failed to get fences for buffer\n");
217 goto cleanup;
218 }
219
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220 amdgpu_bo_get_tiling_flags(new_rbo, &tiling_flags);
221 amdgpu_bo_unreserve(new_rbo);
222
223 work->base = base;
224
225 r = drm_vblank_get(crtc->dev, amdgpu_crtc->crtc_id);
226 if (r) {
227 DRM_ERROR("failed to get vblank before flip\n");
228 goto pflip_cleanup;
229 }
230
231 /* we borrow the event spin lock for protecting flip_wrok */
232 spin_lock_irqsave(&crtc->dev->event_lock, flags);
233 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_NONE) {
234 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
235 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
236 r = -EBUSY;
237 goto vblank_cleanup;
238 }
239
240 amdgpu_crtc->pflip_status = AMDGPU_FLIP_PENDING;
241 amdgpu_crtc->pflip_works = work;
242
243 /* update crtc fb */
244 crtc->primary->fb = fb;
245 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Christian Königc3874b72016-02-11 15:48:30 +0100246 amdgpu_flip_work_func(&work->flip_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400247 return 0;
248
249vblank_cleanup:
250 drm_vblank_put(crtc->dev, amdgpu_crtc->crtc_id);
251
252pflip_cleanup:
253 if (unlikely(amdgpu_bo_reserve(new_rbo, false) != 0)) {
254 DRM_ERROR("failed to reserve new rbo in error path\n");
255 goto cleanup;
256 }
257 if (unlikely(amdgpu_bo_unpin(new_rbo) != 0)) {
258 DRM_ERROR("failed to unpin new rbo in error path\n");
259 }
260 amdgpu_bo_unreserve(new_rbo);
261
262cleanup:
Christian Könige9d951a2015-12-03 19:55:51 +0100263 amdgpu_bo_unref(&work->old_rbo);
Christian König1ffd2652015-08-11 17:29:52 +0200264 fence_put(work->excl);
265 for (i = 0; i < work->shared_count; ++i)
266 fence_put(work->shared[i]);
267 kfree(work->shared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268 kfree(work);
269
270 return r;
271}
272
273int amdgpu_crtc_set_config(struct drm_mode_set *set)
274{
275 struct drm_device *dev;
276 struct amdgpu_device *adev;
277 struct drm_crtc *crtc;
278 bool active = false;
279 int ret;
280
281 if (!set || !set->crtc)
282 return -EINVAL;
283
284 dev = set->crtc->dev;
285
286 ret = pm_runtime_get_sync(dev->dev);
287 if (ret < 0)
288 return ret;
289
290 ret = drm_crtc_helper_set_config(set);
291
292 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
293 if (crtc->enabled)
294 active = true;
295
296 pm_runtime_mark_last_busy(dev->dev);
297
298 adev = dev->dev_private;
299 /* if we have active crtcs and we don't have a power ref,
300 take the current one */
301 if (active && !adev->have_disp_power_ref) {
302 adev->have_disp_power_ref = true;
303 return ret;
304 }
305 /* if we have no active crtcs, then drop the power ref
306 we got before */
307 if (!active && adev->have_disp_power_ref) {
308 pm_runtime_put_autosuspend(dev->dev);
309 adev->have_disp_power_ref = false;
310 }
311
312 /* drop the power reference we got coming in here */
313 pm_runtime_put_autosuspend(dev->dev);
314 return ret;
315}
316
317static const char *encoder_names[38] = {
318 "NONE",
319 "INTERNAL_LVDS",
320 "INTERNAL_TMDS1",
321 "INTERNAL_TMDS2",
322 "INTERNAL_DAC1",
323 "INTERNAL_DAC2",
324 "INTERNAL_SDVOA",
325 "INTERNAL_SDVOB",
326 "SI170B",
327 "CH7303",
328 "CH7301",
329 "INTERNAL_DVO1",
330 "EXTERNAL_SDVOA",
331 "EXTERNAL_SDVOB",
332 "TITFP513",
333 "INTERNAL_LVTM1",
334 "VT1623",
335 "HDMI_SI1930",
336 "HDMI_INTERNAL",
337 "INTERNAL_KLDSCP_TMDS1",
338 "INTERNAL_KLDSCP_DVO1",
339 "INTERNAL_KLDSCP_DAC1",
340 "INTERNAL_KLDSCP_DAC2",
341 "SI178",
342 "MVPU_FPGA",
343 "INTERNAL_DDI",
344 "VT1625",
345 "HDMI_SI1932",
346 "DP_AN9801",
347 "DP_DP501",
348 "INTERNAL_UNIPHY",
349 "INTERNAL_KLDSCP_LVTMA",
350 "INTERNAL_UNIPHY1",
351 "INTERNAL_UNIPHY2",
352 "NUTMEG",
353 "TRAVIS",
354 "INTERNAL_VCE",
355 "INTERNAL_UNIPHY3",
356};
357
358static const char *hpd_names[6] = {
359 "HPD1",
360 "HPD2",
361 "HPD3",
362 "HPD4",
363 "HPD5",
364 "HPD6",
365};
366
367void amdgpu_print_display_setup(struct drm_device *dev)
368{
369 struct drm_connector *connector;
370 struct amdgpu_connector *amdgpu_connector;
371 struct drm_encoder *encoder;
372 struct amdgpu_encoder *amdgpu_encoder;
373 uint32_t devices;
374 int i = 0;
375
376 DRM_INFO("AMDGPU Display Connectors\n");
377 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
378 amdgpu_connector = to_amdgpu_connector(connector);
379 DRM_INFO("Connector %d:\n", i);
380 DRM_INFO(" %s\n", connector->name);
381 if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE)
382 DRM_INFO(" %s\n", hpd_names[amdgpu_connector->hpd.hpd]);
383 if (amdgpu_connector->ddc_bus) {
384 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
385 amdgpu_connector->ddc_bus->rec.mask_clk_reg,
386 amdgpu_connector->ddc_bus->rec.mask_data_reg,
387 amdgpu_connector->ddc_bus->rec.a_clk_reg,
388 amdgpu_connector->ddc_bus->rec.a_data_reg,
389 amdgpu_connector->ddc_bus->rec.en_clk_reg,
390 amdgpu_connector->ddc_bus->rec.en_data_reg,
391 amdgpu_connector->ddc_bus->rec.y_clk_reg,
392 amdgpu_connector->ddc_bus->rec.y_data_reg);
393 if (amdgpu_connector->router.ddc_valid)
394 DRM_INFO(" DDC Router 0x%x/0x%x\n",
395 amdgpu_connector->router.ddc_mux_control_pin,
396 amdgpu_connector->router.ddc_mux_state);
397 if (amdgpu_connector->router.cd_valid)
398 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
399 amdgpu_connector->router.cd_mux_control_pin,
400 amdgpu_connector->router.cd_mux_state);
401 } else {
402 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
403 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
404 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
405 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
406 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
407 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
408 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
409 }
410 DRM_INFO(" Encoders:\n");
411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
412 amdgpu_encoder = to_amdgpu_encoder(encoder);
413 devices = amdgpu_encoder->devices & amdgpu_connector->devices;
414 if (devices) {
415 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
416 DRM_INFO(" CRT1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
417 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
418 DRM_INFO(" CRT2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
419 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
420 DRM_INFO(" LCD1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
421 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
422 DRM_INFO(" DFP1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
423 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
424 DRM_INFO(" DFP2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
425 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
426 DRM_INFO(" DFP3: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
427 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
428 DRM_INFO(" DFP4: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
429 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
430 DRM_INFO(" DFP5: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
431 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
432 DRM_INFO(" DFP6: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
433 if (devices & ATOM_DEVICE_TV1_SUPPORT)
434 DRM_INFO(" TV1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
435 if (devices & ATOM_DEVICE_CV_SUPPORT)
436 DRM_INFO(" CV: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
437 }
438 }
439 i++;
440 }
441}
442
443/**
444 * amdgpu_ddc_probe
445 *
446 */
447bool amdgpu_ddc_probe(struct amdgpu_connector *amdgpu_connector,
448 bool use_aux)
449{
450 u8 out = 0x0;
451 u8 buf[8];
452 int ret;
453 struct i2c_msg msgs[] = {
454 {
455 .addr = DDC_ADDR,
456 .flags = 0,
457 .len = 1,
458 .buf = &out,
459 },
460 {
461 .addr = DDC_ADDR,
462 .flags = I2C_M_RD,
463 .len = 8,
464 .buf = buf,
465 }
466 };
467
468 /* on hw with routers, select right port */
469 if (amdgpu_connector->router.ddc_valid)
470 amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
471
472 if (use_aux) {
473 ret = i2c_transfer(&amdgpu_connector->ddc_bus->aux.ddc, msgs, 2);
474 } else {
475 ret = i2c_transfer(&amdgpu_connector->ddc_bus->adapter, msgs, 2);
476 }
477
478 if (ret != 2)
479 /* Couldn't find an accessible DDC on this connector */
480 return false;
481 /* Probe also for valid EDID header
482 * EDID header starts with:
483 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
484 * Only the first 6 bytes must be valid as
485 * drm_edid_block_valid() can fix the last 2 bytes */
486 if (drm_edid_header_is_valid(buf) < 6) {
487 /* Couldn't find an accessible EDID on this
488 * connector */
489 return false;
490 }
491 return true;
492}
493
494static void amdgpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
495{
496 struct amdgpu_framebuffer *amdgpu_fb = to_amdgpu_framebuffer(fb);
497
498 if (amdgpu_fb->obj) {
499 drm_gem_object_unreference_unlocked(amdgpu_fb->obj);
500 }
501 drm_framebuffer_cleanup(fb);
502 kfree(amdgpu_fb);
503}
504
505static int amdgpu_user_framebuffer_create_handle(struct drm_framebuffer *fb,
506 struct drm_file *file_priv,
507 unsigned int *handle)
508{
509 struct amdgpu_framebuffer *amdgpu_fb = to_amdgpu_framebuffer(fb);
510
511 return drm_gem_handle_create(file_priv, amdgpu_fb->obj, handle);
512}
513
514static const struct drm_framebuffer_funcs amdgpu_fb_funcs = {
515 .destroy = amdgpu_user_framebuffer_destroy,
516 .create_handle = amdgpu_user_framebuffer_create_handle,
517};
518
519int
520amdgpu_framebuffer_init(struct drm_device *dev,
521 struct amdgpu_framebuffer *rfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200522 const struct drm_mode_fb_cmd2 *mode_cmd,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 struct drm_gem_object *obj)
524{
525 int ret;
526 rfb->obj = obj;
527 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
528 ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
529 if (ret) {
530 rfb->obj = NULL;
531 return ret;
532 }
533 return 0;
534}
535
536static struct drm_framebuffer *
537amdgpu_user_framebuffer_create(struct drm_device *dev,
538 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200539 const struct drm_mode_fb_cmd2 *mode_cmd)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540{
541 struct drm_gem_object *obj;
542 struct amdgpu_framebuffer *amdgpu_fb;
543 int ret;
544
545 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
546 if (obj == NULL) {
547 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
548 "can't create framebuffer\n", mode_cmd->handles[0]);
549 return ERR_PTR(-ENOENT);
550 }
551
552 amdgpu_fb = kzalloc(sizeof(*amdgpu_fb), GFP_KERNEL);
553 if (amdgpu_fb == NULL) {
554 drm_gem_object_unreference_unlocked(obj);
555 return ERR_PTR(-ENOMEM);
556 }
557
558 ret = amdgpu_framebuffer_init(dev, amdgpu_fb, mode_cmd, obj);
559 if (ret) {
560 kfree(amdgpu_fb);
561 drm_gem_object_unreference_unlocked(obj);
562 return ERR_PTR(ret);
563 }
564
565 return &amdgpu_fb->base;
566}
567
568static void amdgpu_output_poll_changed(struct drm_device *dev)
569{
570 struct amdgpu_device *adev = dev->dev_private;
571 amdgpu_fb_output_poll_changed(adev);
572}
573
574const struct drm_mode_config_funcs amdgpu_mode_funcs = {
575 .fb_create = amdgpu_user_framebuffer_create,
576 .output_poll_changed = amdgpu_output_poll_changed
577};
578
579static struct drm_prop_enum_list amdgpu_underscan_enum_list[] =
580{ { UNDERSCAN_OFF, "off" },
581 { UNDERSCAN_ON, "on" },
582 { UNDERSCAN_AUTO, "auto" },
583};
584
585static struct drm_prop_enum_list amdgpu_audio_enum_list[] =
586{ { AMDGPU_AUDIO_DISABLE, "off" },
587 { AMDGPU_AUDIO_ENABLE, "on" },
588 { AMDGPU_AUDIO_AUTO, "auto" },
589};
590
591/* XXX support different dither options? spatial, temporal, both, etc. */
592static struct drm_prop_enum_list amdgpu_dither_enum_list[] =
593{ { AMDGPU_FMT_DITHER_DISABLE, "off" },
594 { AMDGPU_FMT_DITHER_ENABLE, "on" },
595};
596
597int amdgpu_modeset_create_props(struct amdgpu_device *adev)
598{
599 int sz;
600
601 if (adev->is_atom_bios) {
602 adev->mode_info.coherent_mode_property =
603 drm_property_create_range(adev->ddev, 0 , "coherent", 0, 1);
604 if (!adev->mode_info.coherent_mode_property)
605 return -ENOMEM;
606 }
607
608 adev->mode_info.load_detect_property =
609 drm_property_create_range(adev->ddev, 0, "load detection", 0, 1);
610 if (!adev->mode_info.load_detect_property)
611 return -ENOMEM;
612
613 drm_mode_create_scaling_mode_property(adev->ddev);
614
615 sz = ARRAY_SIZE(amdgpu_underscan_enum_list);
616 adev->mode_info.underscan_property =
617 drm_property_create_enum(adev->ddev, 0,
618 "underscan",
619 amdgpu_underscan_enum_list, sz);
620
621 adev->mode_info.underscan_hborder_property =
622 drm_property_create_range(adev->ddev, 0,
623 "underscan hborder", 0, 128);
624 if (!adev->mode_info.underscan_hborder_property)
625 return -ENOMEM;
626
627 adev->mode_info.underscan_vborder_property =
628 drm_property_create_range(adev->ddev, 0,
629 "underscan vborder", 0, 128);
630 if (!adev->mode_info.underscan_vborder_property)
631 return -ENOMEM;
632
633 sz = ARRAY_SIZE(amdgpu_audio_enum_list);
634 adev->mode_info.audio_property =
635 drm_property_create_enum(adev->ddev, 0,
636 "audio",
637 amdgpu_audio_enum_list, sz);
638
639 sz = ARRAY_SIZE(amdgpu_dither_enum_list);
640 adev->mode_info.dither_property =
641 drm_property_create_enum(adev->ddev, 0,
642 "dither",
643 amdgpu_dither_enum_list, sz);
644
645 return 0;
646}
647
648void amdgpu_update_display_priority(struct amdgpu_device *adev)
649{
650 /* adjustment options for the display watermarks */
651 if ((amdgpu_disp_priority == 0) || (amdgpu_disp_priority > 2))
652 adev->mode_info.disp_priority = 0;
653 else
654 adev->mode_info.disp_priority = amdgpu_disp_priority;
655
656}
657
658static bool is_hdtv_mode(const struct drm_display_mode *mode)
659{
660 /* try and guess if this is a tv or a monitor */
661 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
662 (mode->vdisplay == 576) || /* 576p */
663 (mode->vdisplay == 720) || /* 720p */
664 (mode->vdisplay == 1080)) /* 1080p */
665 return true;
666 else
667 return false;
668}
669
670bool amdgpu_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
671 const struct drm_display_mode *mode,
672 struct drm_display_mode *adjusted_mode)
673{
674 struct drm_device *dev = crtc->dev;
675 struct drm_encoder *encoder;
676 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
677 struct amdgpu_encoder *amdgpu_encoder;
678 struct drm_connector *connector;
679 struct amdgpu_connector *amdgpu_connector;
680 u32 src_v = 1, dst_v = 1;
681 u32 src_h = 1, dst_h = 1;
682
683 amdgpu_crtc->h_border = 0;
684 amdgpu_crtc->v_border = 0;
685
686 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
687 if (encoder->crtc != crtc)
688 continue;
689 amdgpu_encoder = to_amdgpu_encoder(encoder);
690 connector = amdgpu_get_connector_for_encoder(encoder);
691 amdgpu_connector = to_amdgpu_connector(connector);
692
693 /* set scaling */
694 if (amdgpu_encoder->rmx_type == RMX_OFF)
695 amdgpu_crtc->rmx_type = RMX_OFF;
696 else if (mode->hdisplay < amdgpu_encoder->native_mode.hdisplay ||
697 mode->vdisplay < amdgpu_encoder->native_mode.vdisplay)
698 amdgpu_crtc->rmx_type = amdgpu_encoder->rmx_type;
699 else
700 amdgpu_crtc->rmx_type = RMX_OFF;
701 /* copy native mode */
702 memcpy(&amdgpu_crtc->native_mode,
703 &amdgpu_encoder->native_mode,
704 sizeof(struct drm_display_mode));
705 src_v = crtc->mode.vdisplay;
706 dst_v = amdgpu_crtc->native_mode.vdisplay;
707 src_h = crtc->mode.hdisplay;
708 dst_h = amdgpu_crtc->native_mode.hdisplay;
709
710 /* fix up for overscan on hdmi */
711 if ((!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
712 ((amdgpu_encoder->underscan_type == UNDERSCAN_ON) ||
713 ((amdgpu_encoder->underscan_type == UNDERSCAN_AUTO) &&
714 drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
715 is_hdtv_mode(mode)))) {
716 if (amdgpu_encoder->underscan_hborder != 0)
717 amdgpu_crtc->h_border = amdgpu_encoder->underscan_hborder;
718 else
719 amdgpu_crtc->h_border = (mode->hdisplay >> 5) + 16;
720 if (amdgpu_encoder->underscan_vborder != 0)
721 amdgpu_crtc->v_border = amdgpu_encoder->underscan_vborder;
722 else
723 amdgpu_crtc->v_border = (mode->vdisplay >> 5) + 16;
724 amdgpu_crtc->rmx_type = RMX_FULL;
725 src_v = crtc->mode.vdisplay;
726 dst_v = crtc->mode.vdisplay - (amdgpu_crtc->v_border * 2);
727 src_h = crtc->mode.hdisplay;
728 dst_h = crtc->mode.hdisplay - (amdgpu_crtc->h_border * 2);
729 }
730 }
731 if (amdgpu_crtc->rmx_type != RMX_OFF) {
732 fixed20_12 a, b;
733 a.full = dfixed_const(src_v);
734 b.full = dfixed_const(dst_v);
735 amdgpu_crtc->vsc.full = dfixed_div(a, b);
736 a.full = dfixed_const(src_h);
737 b.full = dfixed_const(dst_h);
738 amdgpu_crtc->hsc.full = dfixed_div(a, b);
739 } else {
740 amdgpu_crtc->vsc.full = dfixed_const(1);
741 amdgpu_crtc->hsc.full = dfixed_const(1);
742 }
743 return true;
744}
745
746/*
747 * Retrieve current video scanout position of crtc on a given gpu, and
748 * an optional accurate timestamp of when query happened.
749 *
750 * \param dev Device to query.
Thierry Reding88e72712015-09-24 18:35:31 +0200751 * \param pipe Crtc to query.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
Alex Deucher8e36f9d2015-12-03 12:31:56 -0500753 * For driver internal use only also supports these flags:
754 *
755 * USE_REAL_VBLANKSTART to use the real start of vblank instead
756 * of a fudged earlier start of vblank.
757 *
758 * GET_DISTANCE_TO_VBLANKSTART to return distance to the
759 * fudged earlier start of vblank in *vpos and the distance
760 * to true start of vblank in *hpos.
761 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762 * \param *vpos Location where vertical scanout position should be stored.
763 * \param *hpos Location where horizontal scanout position should go.
764 * \param *stime Target location for timestamp taken immediately before
765 * scanout position query. Can be NULL to skip timestamp.
766 * \param *etime Target location for timestamp taken immediately after
767 * scanout position query. Can be NULL to skip timestamp.
768 *
769 * Returns vpos as a positive number while in active scanout area.
770 * Returns vpos as a negative number inside vblank, counting the number
771 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
772 * until start of active scanout / end of vblank."
773 *
774 * \return Flags, or'ed together as follows:
775 *
776 * DRM_SCANOUTPOS_VALID = Query successful.
777 * DRM_SCANOUTPOS_INVBL = Inside vblank.
778 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
779 * this flag means that returned position may be offset by a constant but
780 * unknown small number of scanlines wrt. real scanout position.
781 *
782 */
Thierry Reding88e72712015-09-24 18:35:31 +0200783int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
784 unsigned int flags, int *vpos, int *hpos,
785 ktime_t *stime, ktime_t *etime,
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300786 const struct drm_display_mode *mode)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400787{
788 u32 vbl = 0, position = 0;
789 int vbl_start, vbl_end, vtotal, ret = 0;
790 bool in_vbl = true;
791
792 struct amdgpu_device *adev = dev->dev_private;
793
794 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
795
796 /* Get optional system timestamp before query. */
797 if (stime)
798 *stime = ktime_get();
799
Thierry Reding88e72712015-09-24 18:35:31 +0200800 if (amdgpu_display_page_flip_get_scanoutpos(adev, pipe, &vbl, &position) == 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 ret |= DRM_SCANOUTPOS_VALID;
802
803 /* Get optional system timestamp after query. */
804 if (etime)
805 *etime = ktime_get();
806
807 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
808
809 /* Decode into vertical and horizontal scanout position. */
810 *vpos = position & 0x1fff;
811 *hpos = (position >> 16) & 0x1fff;
812
813 /* Valid vblank area boundaries from gpu retrieved? */
814 if (vbl > 0) {
815 /* Yes: Decode. */
816 ret |= DRM_SCANOUTPOS_ACCURATE;
817 vbl_start = vbl & 0x1fff;
818 vbl_end = (vbl >> 16) & 0x1fff;
819 }
820 else {
821 /* No: Fake something reasonable which gives at least ok results. */
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300822 vbl_start = mode->crtc_vdisplay;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823 vbl_end = 0;
824 }
825
Alex Deucher8e36f9d2015-12-03 12:31:56 -0500826 /* Called from driver internal vblank counter query code? */
827 if (flags & GET_DISTANCE_TO_VBLANKSTART) {
828 /* Caller wants distance from real vbl_start in *hpos */
829 *hpos = *vpos - vbl_start;
830 }
831
832 /* Fudge vblank to start a few scanlines earlier to handle the
833 * problem that vblank irqs fire a few scanlines before start
834 * of vblank. Some driver internal callers need the true vblank
835 * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
836 *
837 * The cause of the "early" vblank irq is that the irq is triggered
838 * by the line buffer logic when the line buffer read position enters
839 * the vblank, whereas our crtc scanout position naturally lags the
840 * line buffer read position.
841 */
842 if (!(flags & USE_REAL_VBLANKSTART))
843 vbl_start -= adev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
844
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400845 /* Test scanout position against vblank region. */
846 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
847 in_vbl = false;
848
Alex Deucher8e36f9d2015-12-03 12:31:56 -0500849 /* In vblank? */
850 if (in_vbl)
851 ret |= DRM_SCANOUTPOS_IN_VBLANK;
852
853 /* Called from driver internal vblank counter query code? */
854 if (flags & GET_DISTANCE_TO_VBLANKSTART) {
855 /* Caller wants distance from fudged earlier vbl_start */
856 *vpos -= vbl_start;
857 return ret;
858 }
859
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400860 /* Check if inside vblank area and apply corrective offsets:
861 * vpos will then be >=0 in video scanout area, but negative
862 * within vblank area, counting down the number of lines until
863 * start of scanout.
864 */
865
866 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
867 if (in_vbl && (*vpos >= vbl_start)) {
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300868 vtotal = mode->crtc_vtotal;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400869 *vpos = *vpos - vtotal;
870 }
871
872 /* Correct for shifted end of vbl at vbl_end. */
873 *vpos = *vpos - vbl_end;
874
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 return ret;
876}
877
878int amdgpu_crtc_idx_to_irq_type(struct amdgpu_device *adev, int crtc)
879{
880 if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
881 return AMDGPU_CRTC_IRQ_NONE;
882
883 switch (crtc) {
884 case 0:
885 return AMDGPU_CRTC_IRQ_VBLANK1;
886 case 1:
887 return AMDGPU_CRTC_IRQ_VBLANK2;
888 case 2:
889 return AMDGPU_CRTC_IRQ_VBLANK3;
890 case 3:
891 return AMDGPU_CRTC_IRQ_VBLANK4;
892 case 4:
893 return AMDGPU_CRTC_IRQ_VBLANK5;
894 case 5:
895 return AMDGPU_CRTC_IRQ_VBLANK6;
896 default:
897 return AMDGPU_CRTC_IRQ_NONE;
898 }
899}