blob: dbf2ccd0c7447c5c6b9d887c740897fdc0a82a04 [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;
Christian König765e7fb2016-09-15 15:06:50 +0200171 struct amdgpu_bo *abo;
Emily Dengf1f5ef92016-08-08 11:32:00 +0800172
Daniel Stonee68d14d2018-03-30 15:11:38 +0100173 abo = gem_to_amdgpu_bo(crtc->primary->fb->obj[0]);
Michel Dänzerc81a1a72017-04-28 17:28:14 +0900174 r = amdgpu_bo_reserve(abo, true);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800175 if (unlikely(r))
Christian König765e7fb2016-09-15 15:06:50 +0200176 DRM_ERROR("failed to reserve abo before unpin\n");
Emily Dengf1f5ef92016-08-08 11:32:00 +0800177 else {
Christian König765e7fb2016-09-15 15:06:50 +0200178 amdgpu_bo_unpin(abo);
179 amdgpu_bo_unreserve(abo);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800180 }
181 }
182
183 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
184 amdgpu_crtc->encoder = NULL;
185 amdgpu_crtc->connector = NULL;
186}
187
188static int dce_virtual_crtc_mode_set(struct drm_crtc *crtc,
189 struct drm_display_mode *mode,
190 struct drm_display_mode *adjusted_mode,
191 int x, int y, struct drm_framebuffer *old_fb)
192{
193 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
194
195 /* update the hw version fpr dpm */
196 amdgpu_crtc->hw_mode = *adjusted_mode;
197
198 return 0;
199}
200
201static bool dce_virtual_crtc_mode_fixup(struct drm_crtc *crtc,
202 const struct drm_display_mode *mode,
203 struct drm_display_mode *adjusted_mode)
204{
Emily Dengf1f5ef92016-08-08 11:32:00 +0800205 return true;
206}
207
208
209static int dce_virtual_crtc_set_base(struct drm_crtc *crtc, int x, int y,
210 struct drm_framebuffer *old_fb)
211{
212 return 0;
213}
214
Emily Dengf1f5ef92016-08-08 11:32:00 +0800215static int dce_virtual_crtc_set_base_atomic(struct drm_crtc *crtc,
216 struct drm_framebuffer *fb,
217 int x, int y, enum mode_set_atomic state)
218{
219 return 0;
220}
221
Emily Dengc6e14f42016-08-08 11:30:50 +0800222static const struct drm_crtc_helper_funcs dce_virtual_crtc_helper_funcs = {
Emily Dengf1f5ef92016-08-08 11:32:00 +0800223 .dpms = dce_virtual_crtc_dpms,
224 .mode_fixup = dce_virtual_crtc_mode_fixup,
225 .mode_set = dce_virtual_crtc_mode_set,
226 .mode_set_base = dce_virtual_crtc_set_base,
227 .mode_set_base_atomic = dce_virtual_crtc_set_base_atomic,
228 .prepare = dce_virtual_crtc_prepare,
229 .commit = dce_virtual_crtc_commit,
Emily Dengf1f5ef92016-08-08 11:32:00 +0800230 .disable = dce_virtual_crtc_disable,
Emily Dengc6e14f42016-08-08 11:30:50 +0800231};
232
233static int dce_virtual_crtc_init(struct amdgpu_device *adev, int index)
234{
235 struct amdgpu_crtc *amdgpu_crtc;
Emily Dengc6e14f42016-08-08 11:30:50 +0800236
237 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
238 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
239 if (amdgpu_crtc == NULL)
240 return -ENOMEM;
241
242 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_virtual_crtc_funcs);
243
244 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
245 amdgpu_crtc->crtc_id = index;
246 adev->mode_info.crtcs[index] = amdgpu_crtc;
247
Emily Dengc6e14f42016-08-08 11:30:50 +0800248 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
249 amdgpu_crtc->encoder = NULL;
250 amdgpu_crtc->connector = NULL;
Emily Deng0f663562016-09-30 13:02:18 -0400251 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
Emily Dengc6e14f42016-08-08 11:30:50 +0800252 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_virtual_crtc_helper_funcs);
253
254 return 0;
255}
256
257static int dce_virtual_early_init(void *handle)
258{
259 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
260
261 dce_virtual_set_display_funcs(adev);
262 dce_virtual_set_irq_funcs(adev);
263
Emily Dengc6e14f42016-08-08 11:30:50 +0800264 adev->mode_info.num_hpd = 1;
265 adev->mode_info.num_dig = 1;
266 return 0;
267}
268
Alex Deucher66264ba2016-09-30 12:37:36 -0400269static struct drm_encoder *
270dce_virtual_encoder(struct drm_connector *connector)
Emily Dengc6e14f42016-08-08 11:30:50 +0800271{
Alex Deucher66264ba2016-09-30 12:37:36 -0400272 int enc_id = connector->encoder_ids[0];
273 struct drm_encoder *encoder;
274 int i;
Emily Dengc6e14f42016-08-08 11:30:50 +0800275
Alex Deucher66264ba2016-09-30 12:37:36 -0400276 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
277 if (connector->encoder_ids[i] == 0)
278 break;
Emily Dengc6e14f42016-08-08 11:30:50 +0800279
Keith Packard418da172017-03-14 23:25:07 -0700280 encoder = drm_encoder_find(connector->dev, NULL, connector->encoder_ids[i]);
Alex Deucher66264ba2016-09-30 12:37:36 -0400281 if (!encoder)
282 continue;
Emily Dengc6e14f42016-08-08 11:30:50 +0800283
Alex Deucher66264ba2016-09-30 12:37:36 -0400284 if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
285 return encoder;
286 }
Emily Dengc6e14f42016-08-08 11:30:50 +0800287
Alex Deucher66264ba2016-09-30 12:37:36 -0400288 /* pick the first one */
289 if (enc_id)
Keith Packard418da172017-03-14 23:25:07 -0700290 return drm_encoder_find(connector->dev, NULL, enc_id);
Alex Deucher66264ba2016-09-30 12:37:36 -0400291 return NULL;
Emily Dengc6e14f42016-08-08 11:30:50 +0800292}
293
Alex Deucher66264ba2016-09-30 12:37:36 -0400294static int dce_virtual_get_modes(struct drm_connector *connector)
295{
296 struct drm_device *dev = connector->dev;
297 struct drm_display_mode *mode = NULL;
298 unsigned i;
299 static const struct mode_size {
300 int w;
301 int h;
302 } common_modes[17] = {
303 { 640, 480},
304 { 720, 480},
305 { 800, 600},
306 { 848, 480},
307 {1024, 768},
308 {1152, 768},
309 {1280, 720},
310 {1280, 800},
311 {1280, 854},
312 {1280, 960},
313 {1280, 1024},
314 {1440, 900},
315 {1400, 1050},
316 {1680, 1050},
317 {1600, 1200},
318 {1920, 1080},
319 {1920, 1200}
320 };
321
322 for (i = 0; i < 17; i++) {
323 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
324 drm_mode_probed_add(connector, mode);
325 }
326
327 return 0;
328}
329
Luc Van Oostenryckba9ca082018-04-24 15:14:18 +0200330static enum drm_mode_status dce_virtual_mode_valid(struct drm_connector *connector,
Alex Deucher66264ba2016-09-30 12:37:36 -0400331 struct drm_display_mode *mode)
332{
333 return MODE_OK;
334}
335
336static int
337dce_virtual_dpms(struct drm_connector *connector, int mode)
338{
339 return 0;
340}
341
Alex Deucher66264ba2016-09-30 12:37:36 -0400342static int
343dce_virtual_set_property(struct drm_connector *connector,
344 struct drm_property *property,
345 uint64_t val)
346{
347 return 0;
348}
349
350static void dce_virtual_destroy(struct drm_connector *connector)
351{
352 drm_connector_unregister(connector);
353 drm_connector_cleanup(connector);
354 kfree(connector);
355}
356
357static void dce_virtual_force(struct drm_connector *connector)
358{
359 return;
360}
361
362static const struct drm_connector_helper_funcs dce_virtual_connector_helper_funcs = {
363 .get_modes = dce_virtual_get_modes,
364 .mode_valid = dce_virtual_mode_valid,
365 .best_encoder = dce_virtual_encoder,
366};
367
368static const struct drm_connector_funcs dce_virtual_connector_funcs = {
369 .dpms = dce_virtual_dpms,
Alex Deucher66264ba2016-09-30 12:37:36 -0400370 .fill_modes = drm_helper_probe_single_connector_modes,
371 .set_property = dce_virtual_set_property,
372 .destroy = dce_virtual_destroy,
373 .force = dce_virtual_force,
374};
375
Emily Dengc6e14f42016-08-08 11:30:50 +0800376static int dce_virtual_sw_init(void *handle)
377{
378 int r, i;
379 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
380
Alex Deucherd766e6a2016-03-29 18:28:50 -0400381 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 229, &adev->crtc_irq);
Emily Dengc6e14f42016-08-08 11:30:50 +0800382 if (r)
383 return r;
384
Emily Deng041aa652016-08-17 14:59:20 +0800385 adev->ddev->max_vblank_count = 0;
386
Emily Dengc6e14f42016-08-08 11:30:50 +0800387 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
388
389 adev->ddev->mode_config.max_width = 16384;
390 adev->ddev->mode_config.max_height = 16384;
391
392 adev->ddev->mode_config.preferred_depth = 24;
393 adev->ddev->mode_config.prefer_shadow = 1;
394
Christian König770d13b2018-01-12 14:52:22 +0100395 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
Emily Dengc6e14f42016-08-08 11:30:50 +0800396
Samuel Li3dc9b1c2018-01-19 12:47:40 -0500397 r = amdgpu_display_modeset_create_props(adev);
Emily Dengc6e14f42016-08-08 11:30:50 +0800398 if (r)
399 return r;
400
401 adev->ddev->mode_config.max_width = 16384;
402 adev->ddev->mode_config.max_height = 16384;
403
Alex Deucher66264ba2016-09-30 12:37:36 -0400404 /* allocate crtcs, encoders, connectors */
Emily Dengc6e14f42016-08-08 11:30:50 +0800405 for (i = 0; i < adev->mode_info.num_crtc; i++) {
406 r = dce_virtual_crtc_init(adev, i);
407 if (r)
408 return r;
Alex Deucher66264ba2016-09-30 12:37:36 -0400409 r = dce_virtual_connector_encoder_init(adev, i);
410 if (r)
411 return r;
Emily Dengc6e14f42016-08-08 11:30:50 +0800412 }
413
Emily Dengc6e14f42016-08-08 11:30:50 +0800414 drm_kms_helper_poll_init(adev->ddev);
415
416 adev->mode_info.mode_config_initialized = true;
417 return 0;
418}
419
420static int dce_virtual_sw_fini(void *handle)
421{
422 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
423
424 kfree(adev->mode_info.bios_hardcoded_edid);
425
426 drm_kms_helper_poll_fini(adev->ddev);
427
428 drm_mode_config_cleanup(adev->ddev);
Monk Liu129d65c2017-11-15 17:10:13 +0800429 /* clear crtcs pointer to avoid dce irq finish routine access freed data */
430 memset(adev->mode_info.crtcs, 0, sizeof(adev->mode_info.crtcs[0]) * AMDGPU_MAX_CRTCS);
Emily Dengc6e14f42016-08-08 11:30:50 +0800431 adev->mode_info.mode_config_initialized = false;
432 return 0;
433}
434
435static int dce_virtual_hw_init(void *handle)
436{
Alex Deuchere4f6b392016-12-08 14:53:27 -0500437 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
438
439 switch (adev->asic_type) {
440#ifdef CONFIG_DRM_AMDGPU_SI
441 case CHIP_TAHITI:
442 case CHIP_PITCAIRN:
443 case CHIP_VERDE:
444 case CHIP_OLAND:
445 dce_v6_0_disable_dce(adev);
446 break;
447#endif
448#ifdef CONFIG_DRM_AMDGPU_CIK
449 case CHIP_BONAIRE:
450 case CHIP_HAWAII:
451 case CHIP_KAVERI:
452 case CHIP_KABINI:
453 case CHIP_MULLINS:
454 dce_v8_0_disable_dce(adev);
455 break;
456#endif
457 case CHIP_FIJI:
458 case CHIP_TONGA:
459 dce_v10_0_disable_dce(adev);
460 break;
461 case CHIP_CARRIZO:
462 case CHIP_STONEY:
Alex Deuchere4f6b392016-12-08 14:53:27 -0500463 case CHIP_POLARIS10:
Leo Liube2c8cd2017-11-03 14:22:16 -0400464 case CHIP_POLARIS11:
465 case CHIP_VEGAM:
Alex Deuchere4f6b392016-12-08 14:53:27 -0500466 dce_v11_0_disable_dce(adev);
467 break;
468 case CHIP_TOPAZ:
469#ifdef CONFIG_DRM_AMDGPU_SI
470 case CHIP_HAINAN:
471#endif
472 /* no DCE */
473 break;
Xiangliang.Yu4a70af42017-07-25 17:34:54 +0800474 case CHIP_VEGA10:
Alex Deucherf79f3fc2017-09-01 16:37:59 -0400475 case CHIP_VEGA12:
Feifei Xua2c319b2018-04-20 13:48:23 +0800476 case CHIP_VEGA20:
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};