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