blob: b51f05dc9582ced38e3239c9b4d6d154a6c507ff [file] [log] [blame]
Emily Dengc6e14f42016-08-08 11:30:50 +08001/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
Masahiro Yamada248a1d62017-04-24 13:50:21 +090023#include <drm/drmP.h>
Emily Dengc6e14f42016-08-08 11:30:50 +080024#include "amdgpu.h"
25#include "amdgpu_pm.h"
26#include "amdgpu_i2c.h"
27#include "atom.h"
Emily Dengc6e14f42016-08-08 11:30:50 +080028#include "amdgpu_pll.h"
29#include "amdgpu_connectors.h"
Alex Deuchera1d37042016-09-29 23:36:12 -040030#ifdef CONFIG_DRM_AMDGPU_SI
31#include "dce_v6_0.h"
32#endif
Emily Deng83c9b022016-08-08 11:33:11 +080033#ifdef CONFIG_DRM_AMDGPU_CIK
34#include "dce_v8_0.h"
35#endif
36#include "dce_v10_0.h"
37#include "dce_v11_0.h"
Emily Deng46ac3622016-08-08 11:35:39 +080038#include "dce_virtual.h"
Emily Dengc6e14f42016-08-08 11:30:50 +080039
Alex Deucher623fea12016-10-13 17:36:46 -040040#define DCE_VIRTUAL_VBLANK_PERIOD 16666666
41
42
Emily Dengc6e14f42016-08-08 11:30:50 +080043static void dce_virtual_set_display_funcs(struct amdgpu_device *adev);
44static void dce_virtual_set_irq_funcs(struct amdgpu_device *adev);
Alex Deucher66264ba2016-09-30 12:37:36 -040045static int dce_virtual_connector_encoder_init(struct amdgpu_device *adev,
46 int index);
Monk Liu1719efc2017-11-16 11:11:39 +080047static void dce_virtual_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
48 int crtc,
49 enum amdgpu_interrupt_state state);
Emily Dengc6e14f42016-08-08 11:30:50 +080050
Emily Deng8e6de752016-08-08 11:31:13 +080051static u32 dce_virtual_vblank_get_counter(struct amdgpu_device *adev, int crtc)
52{
Emily Deng041aa652016-08-17 14:59:20 +080053 return 0;
Emily Deng8e6de752016-08-08 11:31:13 +080054}
55
56static void dce_virtual_page_flip(struct amdgpu_device *adev,
57 int crtc_id, u64 crtc_base, bool async)
58{
59 return;
60}
61
62static int dce_virtual_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
63 u32 *vbl, u32 *position)
64{
Emily Deng8e6de752016-08-08 11:31:13 +080065 *vbl = 0;
66 *position = 0;
67
Emily Deng041aa652016-08-17 14:59:20 +080068 return -EINVAL;
Emily Deng8e6de752016-08-08 11:31:13 +080069}
70
71static bool dce_virtual_hpd_sense(struct amdgpu_device *adev,
72 enum amdgpu_hpd_id hpd)
73{
74 return true;
75}
76
77static void dce_virtual_hpd_set_polarity(struct amdgpu_device *adev,
78 enum amdgpu_hpd_id hpd)
79{
80 return;
81}
82
83static u32 dce_virtual_hpd_get_gpio_reg(struct amdgpu_device *adev)
84{
85 return 0;
86}
87
Emily Deng8e6de752016-08-08 11:31:13 +080088/**
89 * dce_virtual_bandwidth_update - program display watermarks
90 *
91 * @adev: amdgpu_device pointer
92 *
93 * Calculate and program the display watermarks and line
94 * buffer allocation (CIK).
95 */
96static void dce_virtual_bandwidth_update(struct amdgpu_device *adev)
97{
98 return;
99}
100
Emily Deng0d43f3b2016-08-08 11:32:22 +0800101static int dce_virtual_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
Daniel Vetter6d124ff2017-04-03 10:33:01 +0200102 u16 *green, u16 *blue, uint32_t size,
103 struct drm_modeset_acquire_ctx *ctx)
Emily Deng0d43f3b2016-08-08 11:32:22 +0800104{
Emily Deng0d43f3b2016-08-08 11:32:22 +0800105 return 0;
106}
107
108static void dce_virtual_crtc_destroy(struct drm_crtc *crtc)
109{
110 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
111
112 drm_crtc_cleanup(crtc);
113 kfree(amdgpu_crtc);
114}
115
Emily Dengc6e14f42016-08-08 11:30:50 +0800116static const struct drm_crtc_funcs dce_virtual_crtc_funcs = {
117 .cursor_set2 = NULL,
118 .cursor_move = NULL,
Emily Deng0d43f3b2016-08-08 11:32:22 +0800119 .gamma_set = dce_virtual_crtc_gamma_set,
Samuel Li775a8362018-01-19 11:53:31 -0500120 .set_config = amdgpu_display_crtc_set_config,
Emily Deng0d43f3b2016-08-08 11:32:22 +0800121 .destroy = dce_virtual_crtc_destroy,
Samuel Li0cd11932018-01-19 11:22:59 -0500122 .page_flip_target = amdgpu_display_crtc_page_flip_target,
Emily Dengc6e14f42016-08-08 11:30:50 +0800123};
124
Emily Dengf1f5ef92016-08-08 11:32:00 +0800125static void dce_virtual_crtc_dpms(struct drm_crtc *crtc, int mode)
126{
127 struct drm_device *dev = crtc->dev;
128 struct amdgpu_device *adev = dev->dev_private;
129 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
130 unsigned type;
131
Xiangliang Yuebe0a802017-02-14 16:08:18 +0800132 if (amdgpu_sriov_vf(adev))
133 return;
134
Emily Dengf1f5ef92016-08-08 11:32:00 +0800135 switch (mode) {
136 case DRM_MODE_DPMS_ON:
137 amdgpu_crtc->enabled = true;
Alex Deucher82b9f812016-09-30 11:19:41 -0400138 /* Make sure VBLANK interrupts are still enabled */
Samuel Li734dd012018-01-19 16:06:41 -0500139 type = amdgpu_display_crtc_idx_to_irq_type(adev,
140 amdgpu_crtc->crtc_id);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800141 amdgpu_irq_update(adev, &adev->crtc_irq, type);
Daniel Vetter2d1e3312016-11-14 10:02:54 +0100142 drm_crtc_vblank_on(crtc);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800143 break;
144 case DRM_MODE_DPMS_STANDBY:
145 case DRM_MODE_DPMS_SUSPEND:
146 case DRM_MODE_DPMS_OFF:
Daniel Vetter2d1e3312016-11-14 10:02:54 +0100147 drm_crtc_vblank_off(crtc);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800148 amdgpu_crtc->enabled = false;
149 break;
150 }
151}
152
153
154static void dce_virtual_crtc_prepare(struct drm_crtc *crtc)
155{
156 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
157}
158
159static void dce_virtual_crtc_commit(struct drm_crtc *crtc)
160{
161 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
162}
163
164static void dce_virtual_crtc_disable(struct drm_crtc *crtc)
165{
166 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
167
168 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
169 if (crtc->primary->fb) {
170 int r;
171 struct amdgpu_framebuffer *amdgpu_fb;
Christian König765e7fb2016-09-15 15:06:50 +0200172 struct amdgpu_bo *abo;
Emily Dengf1f5ef92016-08-08 11:32:00 +0800173
174 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
Christian König765e7fb2016-09-15 15:06:50 +0200175 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
Michel Dänzerc81a1a72017-04-28 17:28:14 +0900176 r = amdgpu_bo_reserve(abo, true);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800177 if (unlikely(r))
Christian König765e7fb2016-09-15 15:06:50 +0200178 DRM_ERROR("failed to reserve abo before unpin\n");
Emily Dengf1f5ef92016-08-08 11:32:00 +0800179 else {
Christian König765e7fb2016-09-15 15:06:50 +0200180 amdgpu_bo_unpin(abo);
181 amdgpu_bo_unreserve(abo);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800182 }
183 }
184
185 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
186 amdgpu_crtc->encoder = NULL;
187 amdgpu_crtc->connector = NULL;
188}
189
190static int dce_virtual_crtc_mode_set(struct drm_crtc *crtc,
191 struct drm_display_mode *mode,
192 struct drm_display_mode *adjusted_mode,
193 int x, int y, struct drm_framebuffer *old_fb)
194{
195 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
196
197 /* update the hw version fpr dpm */
198 amdgpu_crtc->hw_mode = *adjusted_mode;
199
200 return 0;
201}
202
203static bool dce_virtual_crtc_mode_fixup(struct drm_crtc *crtc,
204 const struct drm_display_mode *mode,
205 struct drm_display_mode *adjusted_mode)
206{
Emily Dengf1f5ef92016-08-08 11:32:00 +0800207 return true;
208}
209
210
211static int dce_virtual_crtc_set_base(struct drm_crtc *crtc, int x, int y,
212 struct drm_framebuffer *old_fb)
213{
214 return 0;
215}
216
Emily Dengf1f5ef92016-08-08 11:32:00 +0800217static int dce_virtual_crtc_set_base_atomic(struct drm_crtc *crtc,
218 struct drm_framebuffer *fb,
219 int x, int y, enum mode_set_atomic state)
220{
221 return 0;
222}
223
Emily Dengc6e14f42016-08-08 11:30:50 +0800224static const struct drm_crtc_helper_funcs dce_virtual_crtc_helper_funcs = {
Emily Dengf1f5ef92016-08-08 11:32:00 +0800225 .dpms = dce_virtual_crtc_dpms,
226 .mode_fixup = dce_virtual_crtc_mode_fixup,
227 .mode_set = dce_virtual_crtc_mode_set,
228 .mode_set_base = dce_virtual_crtc_set_base,
229 .mode_set_base_atomic = dce_virtual_crtc_set_base_atomic,
230 .prepare = dce_virtual_crtc_prepare,
231 .commit = dce_virtual_crtc_commit,
Emily Dengf1f5ef92016-08-08 11:32:00 +0800232 .disable = dce_virtual_crtc_disable,
Emily Dengc6e14f42016-08-08 11:30:50 +0800233};
234
235static int dce_virtual_crtc_init(struct amdgpu_device *adev, int index)
236{
237 struct amdgpu_crtc *amdgpu_crtc;
Emily Dengc6e14f42016-08-08 11:30:50 +0800238
239 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
240 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
241 if (amdgpu_crtc == NULL)
242 return -ENOMEM;
243
244 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_virtual_crtc_funcs);
245
246 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
247 amdgpu_crtc->crtc_id = index;
248 adev->mode_info.crtcs[index] = amdgpu_crtc;
249
Emily Dengc6e14f42016-08-08 11:30:50 +0800250 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
251 amdgpu_crtc->encoder = NULL;
252 amdgpu_crtc->connector = NULL;
Emily Deng0f663562016-09-30 13:02:18 -0400253 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
Emily Dengc6e14f42016-08-08 11:30:50 +0800254 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_virtual_crtc_helper_funcs);
255
256 return 0;
257}
258
259static int dce_virtual_early_init(void *handle)
260{
261 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
262
263 dce_virtual_set_display_funcs(adev);
264 dce_virtual_set_irq_funcs(adev);
265
Emily Dengc6e14f42016-08-08 11:30:50 +0800266 adev->mode_info.num_hpd = 1;
267 adev->mode_info.num_dig = 1;
268 return 0;
269}
270
Alex Deucher66264ba2016-09-30 12:37:36 -0400271static struct drm_encoder *
272dce_virtual_encoder(struct drm_connector *connector)
Emily Dengc6e14f42016-08-08 11:30:50 +0800273{
Alex Deucher66264ba2016-09-30 12:37:36 -0400274 int enc_id = connector->encoder_ids[0];
275 struct drm_encoder *encoder;
276 int i;
Emily Dengc6e14f42016-08-08 11:30:50 +0800277
Alex Deucher66264ba2016-09-30 12:37:36 -0400278 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
279 if (connector->encoder_ids[i] == 0)
280 break;
Emily Dengc6e14f42016-08-08 11:30:50 +0800281
Keith Packard418da172017-03-14 23:25:07 -0700282 encoder = drm_encoder_find(connector->dev, NULL, connector->encoder_ids[i]);
Alex Deucher66264ba2016-09-30 12:37:36 -0400283 if (!encoder)
284 continue;
Emily Dengc6e14f42016-08-08 11:30:50 +0800285
Alex Deucher66264ba2016-09-30 12:37:36 -0400286 if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
287 return encoder;
288 }
Emily Dengc6e14f42016-08-08 11:30:50 +0800289
Alex Deucher66264ba2016-09-30 12:37:36 -0400290 /* pick the first one */
291 if (enc_id)
Keith Packard418da172017-03-14 23:25:07 -0700292 return drm_encoder_find(connector->dev, NULL, enc_id);
Alex Deucher66264ba2016-09-30 12:37:36 -0400293 return NULL;
Emily Dengc6e14f42016-08-08 11:30:50 +0800294}
295
Alex Deucher66264ba2016-09-30 12:37:36 -0400296static int dce_virtual_get_modes(struct drm_connector *connector)
297{
298 struct drm_device *dev = connector->dev;
299 struct drm_display_mode *mode = NULL;
300 unsigned i;
301 static const struct mode_size {
302 int w;
303 int h;
304 } common_modes[17] = {
305 { 640, 480},
306 { 720, 480},
307 { 800, 600},
308 { 848, 480},
309 {1024, 768},
310 {1152, 768},
311 {1280, 720},
312 {1280, 800},
313 {1280, 854},
314 {1280, 960},
315 {1280, 1024},
316 {1440, 900},
317 {1400, 1050},
318 {1680, 1050},
319 {1600, 1200},
320 {1920, 1080},
321 {1920, 1200}
322 };
323
324 for (i = 0; i < 17; i++) {
325 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
326 drm_mode_probed_add(connector, mode);
327 }
328
329 return 0;
330}
331
332static int dce_virtual_mode_valid(struct drm_connector *connector,
333 struct drm_display_mode *mode)
334{
335 return MODE_OK;
336}
337
338static int
339dce_virtual_dpms(struct drm_connector *connector, int mode)
340{
341 return 0;
342}
343
Alex Deucher66264ba2016-09-30 12:37:36 -0400344static int
345dce_virtual_set_property(struct drm_connector *connector,
346 struct drm_property *property,
347 uint64_t val)
348{
349 return 0;
350}
351
352static void dce_virtual_destroy(struct drm_connector *connector)
353{
354 drm_connector_unregister(connector);
355 drm_connector_cleanup(connector);
356 kfree(connector);
357}
358
359static void dce_virtual_force(struct drm_connector *connector)
360{
361 return;
362}
363
364static const struct drm_connector_helper_funcs dce_virtual_connector_helper_funcs = {
365 .get_modes = dce_virtual_get_modes,
366 .mode_valid = dce_virtual_mode_valid,
367 .best_encoder = dce_virtual_encoder,
368};
369
370static const struct drm_connector_funcs dce_virtual_connector_funcs = {
371 .dpms = dce_virtual_dpms,
Alex Deucher66264ba2016-09-30 12:37:36 -0400372 .fill_modes = drm_helper_probe_single_connector_modes,
373 .set_property = dce_virtual_set_property,
374 .destroy = dce_virtual_destroy,
375 .force = dce_virtual_force,
376};
377
Emily Dengc6e14f42016-08-08 11:30:50 +0800378static int dce_virtual_sw_init(void *handle)
379{
380 int r, i;
381 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
382
Alex Deucherd766e6a2016-03-29 18:28:50 -0400383 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 229, &adev->crtc_irq);
Emily Dengc6e14f42016-08-08 11:30:50 +0800384 if (r)
385 return r;
386
Emily Deng041aa652016-08-17 14:59:20 +0800387 adev->ddev->max_vblank_count = 0;
388
Emily Dengc6e14f42016-08-08 11:30:50 +0800389 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
390
391 adev->ddev->mode_config.max_width = 16384;
392 adev->ddev->mode_config.max_height = 16384;
393
394 adev->ddev->mode_config.preferred_depth = 24;
395 adev->ddev->mode_config.prefer_shadow = 1;
396
Christian König770d13b2018-01-12 14:52:22 +0100397 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
Emily Dengc6e14f42016-08-08 11:30:50 +0800398
Samuel Li3dc9b1c2018-01-19 12:47:40 -0500399 r = amdgpu_display_modeset_create_props(adev);
Emily Dengc6e14f42016-08-08 11:30:50 +0800400 if (r)
401 return r;
402
403 adev->ddev->mode_config.max_width = 16384;
404 adev->ddev->mode_config.max_height = 16384;
405
Alex Deucher66264ba2016-09-30 12:37:36 -0400406 /* allocate crtcs, encoders, connectors */
Emily Dengc6e14f42016-08-08 11:30:50 +0800407 for (i = 0; i < adev->mode_info.num_crtc; i++) {
408 r = dce_virtual_crtc_init(adev, i);
409 if (r)
410 return r;
Alex Deucher66264ba2016-09-30 12:37:36 -0400411 r = dce_virtual_connector_encoder_init(adev, i);
412 if (r)
413 return r;
Emily Dengc6e14f42016-08-08 11:30:50 +0800414 }
415
Emily Dengc6e14f42016-08-08 11:30:50 +0800416 drm_kms_helper_poll_init(adev->ddev);
417
418 adev->mode_info.mode_config_initialized = true;
419 return 0;
420}
421
422static int dce_virtual_sw_fini(void *handle)
423{
424 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
425
426 kfree(adev->mode_info.bios_hardcoded_edid);
427
428 drm_kms_helper_poll_fini(adev->ddev);
429
430 drm_mode_config_cleanup(adev->ddev);
Monk Liu129d65c2017-11-15 17:10:13 +0800431 /* clear crtcs pointer to avoid dce irq finish routine access freed data */
432 memset(adev->mode_info.crtcs, 0, sizeof(adev->mode_info.crtcs[0]) * AMDGPU_MAX_CRTCS);
Emily Dengc6e14f42016-08-08 11:30:50 +0800433 adev->mode_info.mode_config_initialized = false;
434 return 0;
435}
436
437static int dce_virtual_hw_init(void *handle)
438{
Alex Deuchere4f6b392016-12-08 14:53:27 -0500439 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
440
441 switch (adev->asic_type) {
442#ifdef CONFIG_DRM_AMDGPU_SI
443 case CHIP_TAHITI:
444 case CHIP_PITCAIRN:
445 case CHIP_VERDE:
446 case CHIP_OLAND:
447 dce_v6_0_disable_dce(adev);
448 break;
449#endif
450#ifdef CONFIG_DRM_AMDGPU_CIK
451 case CHIP_BONAIRE:
452 case CHIP_HAWAII:
453 case CHIP_KAVERI:
454 case CHIP_KABINI:
455 case CHIP_MULLINS:
456 dce_v8_0_disable_dce(adev);
457 break;
458#endif
459 case CHIP_FIJI:
460 case CHIP_TONGA:
461 dce_v10_0_disable_dce(adev);
462 break;
463 case CHIP_CARRIZO:
464 case CHIP_STONEY:
465 case CHIP_POLARIS11:
466 case CHIP_POLARIS10:
467 dce_v11_0_disable_dce(adev);
468 break;
469 case CHIP_TOPAZ:
470#ifdef CONFIG_DRM_AMDGPU_SI
471 case CHIP_HAINAN:
472#endif
473 /* no DCE */
474 break;
Xiangliang.Yu4a70af42017-07-25 17:34:54 +0800475 case CHIP_VEGA10:
Alex Deucherf79f3fc2017-09-01 16:37:59 -0400476 case CHIP_VEGA12:
Xiangliang.Yu4a70af42017-07-25 17:34:54 +0800477 break;
Alex Deuchere4f6b392016-12-08 14:53:27 -0500478 default:
479 DRM_ERROR("Virtual display unsupported ASIC type: 0x%X\n", adev->asic_type);
480 }
Emily Dengc6e14f42016-08-08 11:30:50 +0800481 return 0;
482}
483
484static int dce_virtual_hw_fini(void *handle)
485{
Monk Liu1719efc2017-11-16 11:11:39 +0800486 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
487 int i = 0;
488
489 for (i = 0; i<adev->mode_info.num_crtc; i++)
490 if (adev->mode_info.crtcs[i])
491 dce_virtual_set_crtc_vblank_interrupt_state(adev, i, AMDGPU_IRQ_STATE_DISABLE);
492
Emily Dengc6e14f42016-08-08 11:30:50 +0800493 return 0;
494}
495
496static int dce_virtual_suspend(void *handle)
497{
498 return dce_virtual_hw_fini(handle);
499}
500
501static int dce_virtual_resume(void *handle)
502{
Masahiro Yamadad912ade2016-09-14 23:39:08 +0900503 return dce_virtual_hw_init(handle);
Emily Dengc6e14f42016-08-08 11:30:50 +0800504}
505
506static bool dce_virtual_is_idle(void *handle)
507{
508 return true;
509}
510
511static int dce_virtual_wait_for_idle(void *handle)
512{
513 return 0;
514}
515
516static int dce_virtual_soft_reset(void *handle)
517{
518 return 0;
519}
520
521static int dce_virtual_set_clockgating_state(void *handle,
522 enum amd_clockgating_state state)
523{
524 return 0;
525}
526
527static int dce_virtual_set_powergating_state(void *handle,
528 enum amd_powergating_state state)
529{
530 return 0;
531}
532
Alex Deuchera1255102016-10-13 17:41:13 -0400533static const struct amd_ip_funcs dce_virtual_ip_funcs = {
Emily Dengc6e14f42016-08-08 11:30:50 +0800534 .name = "dce_virtual",
535 .early_init = dce_virtual_early_init,
536 .late_init = NULL,
537 .sw_init = dce_virtual_sw_init,
538 .sw_fini = dce_virtual_sw_fini,
539 .hw_init = dce_virtual_hw_init,
540 .hw_fini = dce_virtual_hw_fini,
541 .suspend = dce_virtual_suspend,
542 .resume = dce_virtual_resume,
543 .is_idle = dce_virtual_is_idle,
544 .wait_for_idle = dce_virtual_wait_for_idle,
545 .soft_reset = dce_virtual_soft_reset,
546 .set_clockgating_state = dce_virtual_set_clockgating_state,
547 .set_powergating_state = dce_virtual_set_powergating_state,
548};
549
Emily Deng8e6de752016-08-08 11:31:13 +0800550/* these are handled by the primary encoders */
551static void dce_virtual_encoder_prepare(struct drm_encoder *encoder)
552{
553 return;
554}
555
556static void dce_virtual_encoder_commit(struct drm_encoder *encoder)
557{
558 return;
559}
560
561static void
562dce_virtual_encoder_mode_set(struct drm_encoder *encoder,
Alex Deucher66264ba2016-09-30 12:37:36 -0400563 struct drm_display_mode *mode,
564 struct drm_display_mode *adjusted_mode)
Emily Deng8e6de752016-08-08 11:31:13 +0800565{
566 return;
567}
568
569static void dce_virtual_encoder_disable(struct drm_encoder *encoder)
570{
571 return;
572}
573
574static void
575dce_virtual_encoder_dpms(struct drm_encoder *encoder, int mode)
576{
577 return;
578}
579
580static bool dce_virtual_encoder_mode_fixup(struct drm_encoder *encoder,
581 const struct drm_display_mode *mode,
582 struct drm_display_mode *adjusted_mode)
583{
Emily Deng8e6de752016-08-08 11:31:13 +0800584 return true;
585}
586
587static const struct drm_encoder_helper_funcs dce_virtual_encoder_helper_funcs = {
588 .dpms = dce_virtual_encoder_dpms,
589 .mode_fixup = dce_virtual_encoder_mode_fixup,
590 .prepare = dce_virtual_encoder_prepare,
591 .mode_set = dce_virtual_encoder_mode_set,
592 .commit = dce_virtual_encoder_commit,
593 .disable = dce_virtual_encoder_disable,
594};
595
596static void dce_virtual_encoder_destroy(struct drm_encoder *encoder)
597{
Emily Deng8e6de752016-08-08 11:31:13 +0800598 drm_encoder_cleanup(encoder);
Xiangliang Yu3a1d19a2017-01-19 09:57:41 +0800599 kfree(encoder);
Emily Deng8e6de752016-08-08 11:31:13 +0800600}
601
602static const struct drm_encoder_funcs dce_virtual_encoder_funcs = {
603 .destroy = dce_virtual_encoder_destroy,
604};
605
Alex Deucher66264ba2016-09-30 12:37:36 -0400606static int dce_virtual_connector_encoder_init(struct amdgpu_device *adev,
607 int index)
Emily Deng8e6de752016-08-08 11:31:13 +0800608{
Emily Deng8e6de752016-08-08 11:31:13 +0800609 struct drm_encoder *encoder;
Alex Deucher66264ba2016-09-30 12:37:36 -0400610 struct drm_connector *connector;
Emily Deng8e6de752016-08-08 11:31:13 +0800611
Alex Deucher66264ba2016-09-30 12:37:36 -0400612 /* add a new encoder */
613 encoder = kzalloc(sizeof(struct drm_encoder), GFP_KERNEL);
614 if (!encoder)
615 return -ENOMEM;
616 encoder->possible_crtcs = 1 << index;
617 drm_encoder_init(adev->ddev, encoder, &dce_virtual_encoder_funcs,
618 DRM_MODE_ENCODER_VIRTUAL, NULL);
619 drm_encoder_helper_add(encoder, &dce_virtual_encoder_helper_funcs);
Emily Deng8e6de752016-08-08 11:31:13 +0800620
Alex Deucher66264ba2016-09-30 12:37:36 -0400621 connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
622 if (!connector) {
623 kfree(encoder);
624 return -ENOMEM;
Emily Deng8e6de752016-08-08 11:31:13 +0800625 }
626
Alex Deucher66264ba2016-09-30 12:37:36 -0400627 /* add a new connector */
628 drm_connector_init(adev->ddev, connector, &dce_virtual_connector_funcs,
629 DRM_MODE_CONNECTOR_VIRTUAL);
630 drm_connector_helper_add(connector, &dce_virtual_connector_helper_funcs);
631 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
632 connector->interlace_allowed = false;
633 connector->doublescan_allowed = false;
634 drm_connector_register(connector);
Emily Deng8e6de752016-08-08 11:31:13 +0800635
Alex Deucher66264ba2016-09-30 12:37:36 -0400636 /* link them */
637 drm_mode_connector_attach_encoder(connector, encoder);
Emily Deng8e6de752016-08-08 11:31:13 +0800638
Alex Deucher66264ba2016-09-30 12:37:36 -0400639 return 0;
Emily Deng8e6de752016-08-08 11:31:13 +0800640}
641
Emily Dengc6e14f42016-08-08 11:30:50 +0800642static const struct amdgpu_display_funcs dce_virtual_display_funcs = {
Emily Deng8e6de752016-08-08 11:31:13 +0800643 .bandwidth_update = &dce_virtual_bandwidth_update,
644 .vblank_get_counter = &dce_virtual_vblank_get_counter,
Emily Dengc6e14f42016-08-08 11:30:50 +0800645 .backlight_set_level = NULL,
646 .backlight_get_level = NULL,
Emily Deng8e6de752016-08-08 11:31:13 +0800647 .hpd_sense = &dce_virtual_hpd_sense,
648 .hpd_set_polarity = &dce_virtual_hpd_set_polarity,
649 .hpd_get_gpio_reg = &dce_virtual_hpd_get_gpio_reg,
650 .page_flip = &dce_virtual_page_flip,
651 .page_flip_get_scanoutpos = &dce_virtual_crtc_get_scanoutpos,
Alex Deucher66264ba2016-09-30 12:37:36 -0400652 .add_encoder = NULL,
653 .add_connector = NULL,
Emily Dengc6e14f42016-08-08 11:30:50 +0800654};
655
656static void dce_virtual_set_display_funcs(struct amdgpu_device *adev)
657{
658 if (adev->mode_info.funcs == NULL)
659 adev->mode_info.funcs = &dce_virtual_display_funcs;
660}
661
Alex Deucher9405e472016-09-30 11:41:37 -0400662static int dce_virtual_pageflip(struct amdgpu_device *adev,
663 unsigned crtc_id)
664{
665 unsigned long flags;
666 struct amdgpu_crtc *amdgpu_crtc;
667 struct amdgpu_flip_work *works;
668
669 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
670
671 if (crtc_id >= adev->mode_info.num_crtc) {
672 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
673 return -EINVAL;
674 }
675
676 /* IRQ could occur when in initial stage */
677 if (amdgpu_crtc == NULL)
678 return 0;
679
680 spin_lock_irqsave(&adev->ddev->event_lock, flags);
681 works = amdgpu_crtc->pflip_works;
682 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
683 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
684 "AMDGPU_FLIP_SUBMITTED(%d)\n",
685 amdgpu_crtc->pflip_status,
686 AMDGPU_FLIP_SUBMITTED);
687 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
688 return 0;
689 }
690
691 /* page flip completed. clean up */
692 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
693 amdgpu_crtc->pflip_works = NULL;
694
695 /* wakeup usersapce */
696 if (works->event)
697 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
698
699 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
700
701 drm_crtc_vblank_put(&amdgpu_crtc->base);
702 schedule_work(&works->unpin_work);
703
704 return 0;
705}
706
Emily Deng46ac3622016-08-08 11:35:39 +0800707static enum hrtimer_restart dce_virtual_vblank_timer_handle(struct hrtimer *vblank_timer)
708{
Emily Deng0f663562016-09-30 13:02:18 -0400709 struct amdgpu_crtc *amdgpu_crtc = container_of(vblank_timer,
710 struct amdgpu_crtc, vblank_timer);
711 struct drm_device *ddev = amdgpu_crtc->base.dev;
712 struct amdgpu_device *adev = ddev->dev_private;
Alex Deucher9405e472016-09-30 11:41:37 -0400713
Emily Deng0f663562016-09-30 13:02:18 -0400714 drm_handle_vblank(ddev, amdgpu_crtc->crtc_id);
715 dce_virtual_pageflip(adev, amdgpu_crtc->crtc_id);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100716 hrtimer_start(vblank_timer, DCE_VIRTUAL_VBLANK_PERIOD,
Alex Deucher9405e472016-09-30 11:41:37 -0400717 HRTIMER_MODE_REL);
718
Emily Deng46ac3622016-08-08 11:35:39 +0800719 return HRTIMER_NORESTART;
720}
721
Emily Denge13273d2016-08-08 11:31:37 +0800722static void dce_virtual_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
Alex Deucher82b9f812016-09-30 11:19:41 -0400723 int crtc,
724 enum amdgpu_interrupt_state state)
Emily Denge13273d2016-08-08 11:31:37 +0800725{
Monk Liu129d65c2017-11-15 17:10:13 +0800726 if (crtc >= adev->mode_info.num_crtc || !adev->mode_info.crtcs[crtc]) {
Emily Denge13273d2016-08-08 11:31:37 +0800727 DRM_DEBUG("invalid crtc %d\n", crtc);
728 return;
729 }
Emily Deng46ac3622016-08-08 11:35:39 +0800730
Emily Deng0f663562016-09-30 13:02:18 -0400731 if (state && !adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
Emily Deng46ac3622016-08-08 11:35:39 +0800732 DRM_DEBUG("Enable software vsync timer\n");
Emily Deng0f663562016-09-30 13:02:18 -0400733 hrtimer_init(&adev->mode_info.crtcs[crtc]->vblank_timer,
734 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
735 hrtimer_set_expires(&adev->mode_info.crtcs[crtc]->vblank_timer,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100736 DCE_VIRTUAL_VBLANK_PERIOD);
Emily Deng0f663562016-09-30 13:02:18 -0400737 adev->mode_info.crtcs[crtc]->vblank_timer.function =
738 dce_virtual_vblank_timer_handle;
739 hrtimer_start(&adev->mode_info.crtcs[crtc]->vblank_timer,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100740 DCE_VIRTUAL_VBLANK_PERIOD, HRTIMER_MODE_REL);
Emily Deng0f663562016-09-30 13:02:18 -0400741 } else if (!state && adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
Emily Deng46ac3622016-08-08 11:35:39 +0800742 DRM_DEBUG("Disable software vsync timer\n");
Emily Deng0f663562016-09-30 13:02:18 -0400743 hrtimer_cancel(&adev->mode_info.crtcs[crtc]->vblank_timer);
Emily Deng46ac3622016-08-08 11:35:39 +0800744 }
745
Emily Deng0f663562016-09-30 13:02:18 -0400746 adev->mode_info.crtcs[crtc]->vsync_timer_enabled = state;
Emily Deng46ac3622016-08-08 11:35:39 +0800747 DRM_DEBUG("[FM]set crtc %d vblank interrupt state %d\n", crtc, state);
Emily Denge13273d2016-08-08 11:31:37 +0800748}
749
Emily Deng46ac3622016-08-08 11:35:39 +0800750
Emily Denge13273d2016-08-08 11:31:37 +0800751static int dce_virtual_set_crtc_irq_state(struct amdgpu_device *adev,
Alex Deucher82b9f812016-09-30 11:19:41 -0400752 struct amdgpu_irq_src *source,
753 unsigned type,
754 enum amdgpu_interrupt_state state)
Emily Denge13273d2016-08-08 11:31:37 +0800755{
Emily Deng0f663562016-09-30 13:02:18 -0400756 if (type > AMDGPU_CRTC_IRQ_VBLANK6)
757 return -EINVAL;
758
759 dce_virtual_set_crtc_vblank_interrupt_state(adev, type, state);
760
Emily Denge13273d2016-08-08 11:31:37 +0800761 return 0;
762}
763
Emily Dengc6e14f42016-08-08 11:30:50 +0800764static const struct amdgpu_irq_src_funcs dce_virtual_crtc_irq_funcs = {
Emily Denge13273d2016-08-08 11:31:37 +0800765 .set = dce_virtual_set_crtc_irq_state,
Alex Deucherbf2335a2016-09-30 11:23:30 -0400766 .process = NULL,
Emily Dengc6e14f42016-08-08 11:30:50 +0800767};
768
Emily Dengc6e14f42016-08-08 11:30:50 +0800769static void dce_virtual_set_irq_funcs(struct amdgpu_device *adev)
770{
Emily Deng89a6c2e2017-07-25 09:51:13 +0800771 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VBLANK6 + 1;
Emily Dengc6e14f42016-08-08 11:30:50 +0800772 adev->crtc_irq.funcs = &dce_virtual_crtc_irq_funcs;
Emily Dengc6e14f42016-08-08 11:30:50 +0800773}
774
Alex Deuchera1255102016-10-13 17:41:13 -0400775const struct amdgpu_ip_block_version dce_virtual_ip_block =
776{
777 .type = AMD_IP_BLOCK_TYPE_DCE,
778 .major = 1,
779 .minor = 0,
780 .rev = 0,
781 .funcs = &dce_virtual_ip_funcs,
782};