blob: 5c51f9a97811ea8db63fb914fc7f944dcf0ca316 [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 */
23#include "drmP.h"
24#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);
Emily Dengc6e14f42016-08-08 11:30:50 +080047
Emily Deng8e6de752016-08-08 11:31:13 +080048/**
49 * dce_virtual_vblank_wait - vblank wait asic callback.
50 *
51 * @adev: amdgpu_device pointer
52 * @crtc: crtc to wait for vblank on
53 *
54 * Wait for vblank on the requested crtc (evergreen+).
55 */
56static void dce_virtual_vblank_wait(struct amdgpu_device *adev, int crtc)
57{
58 return;
59}
60
61static u32 dce_virtual_vblank_get_counter(struct amdgpu_device *adev, int crtc)
62{
Emily Deng041aa652016-08-17 14:59:20 +080063 return 0;
Emily Deng8e6de752016-08-08 11:31:13 +080064}
65
66static void dce_virtual_page_flip(struct amdgpu_device *adev,
67 int crtc_id, u64 crtc_base, bool async)
68{
69 return;
70}
71
72static int dce_virtual_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
73 u32 *vbl, u32 *position)
74{
Emily Deng8e6de752016-08-08 11:31:13 +080075 *vbl = 0;
76 *position = 0;
77
Emily Deng041aa652016-08-17 14:59:20 +080078 return -EINVAL;
Emily Deng8e6de752016-08-08 11:31:13 +080079}
80
81static bool dce_virtual_hpd_sense(struct amdgpu_device *adev,
82 enum amdgpu_hpd_id hpd)
83{
84 return true;
85}
86
87static void dce_virtual_hpd_set_polarity(struct amdgpu_device *adev,
88 enum amdgpu_hpd_id hpd)
89{
90 return;
91}
92
93static u32 dce_virtual_hpd_get_gpio_reg(struct amdgpu_device *adev)
94{
95 return 0;
96}
97
Baoyou Xie4d446652016-09-18 22:09:35 +080098static void dce_virtual_stop_mc_access(struct amdgpu_device *adev,
Emily Deng8e6de752016-08-08 11:31:13 +080099 struct amdgpu_mode_mc_save *save)
100{
Emily Deng83c9b022016-08-08 11:33:11 +0800101 switch (adev->asic_type) {
Alex Deuchera1d37042016-09-29 23:36:12 -0400102#ifdef CONFIG_DRM_AMDGPU_SI
103 case CHIP_TAHITI:
104 case CHIP_PITCAIRN:
105 case CHIP_VERDE:
106 case CHIP_OLAND:
107 dce_v6_0_disable_dce(adev);
108 break;
109#endif
Alex Deucher8cb619d2016-09-29 23:20:29 -0400110#ifdef CONFIG_DRM_AMDGPU_CIK
Emily Deng83c9b022016-08-08 11:33:11 +0800111 case CHIP_BONAIRE:
112 case CHIP_HAWAII:
113 case CHIP_KAVERI:
114 case CHIP_KABINI:
115 case CHIP_MULLINS:
Emily Deng83c9b022016-08-08 11:33:11 +0800116 dce_v8_0_disable_dce(adev);
Emily Deng83c9b022016-08-08 11:33:11 +0800117 break;
Alex Deucher8cb619d2016-09-29 23:20:29 -0400118#endif
Emily Deng83c9b022016-08-08 11:33:11 +0800119 case CHIP_FIJI:
120 case CHIP_TONGA:
121 dce_v10_0_disable_dce(adev);
122 break;
123 case CHIP_CARRIZO:
124 case CHIP_STONEY:
Emily Deng83c9b022016-08-08 11:33:11 +0800125 case CHIP_POLARIS10:
Alex Deucher2fc53382017-03-03 12:57:37 -0500126 case CHIP_POLARIS11:
127 case CHIP_POLARIS12:
Emily Deng83c9b022016-08-08 11:33:11 +0800128 dce_v11_0_disable_dce(adev);
129 break;
Alex Deucher2579de42016-08-08 14:40:04 -0400130 case CHIP_TOPAZ:
Alex Deuchera1d37042016-09-29 23:36:12 -0400131#ifdef CONFIG_DRM_AMDGPU_SI
132 case CHIP_HAINAN:
133#endif
Alex Deucher2579de42016-08-08 14:40:04 -0400134 /* no DCE */
135 return;
Emily Deng83c9b022016-08-08 11:33:11 +0800136 default:
Alex Deucher2579de42016-08-08 14:40:04 -0400137 DRM_ERROR("Virtual display unsupported ASIC type: 0x%X\n", adev->asic_type);
Emily Deng83c9b022016-08-08 11:33:11 +0800138 }
139
Emily Deng8e6de752016-08-08 11:31:13 +0800140 return;
141}
Baoyou Xie4d446652016-09-18 22:09:35 +0800142static void dce_virtual_resume_mc_access(struct amdgpu_device *adev,
Emily Deng8e6de752016-08-08 11:31:13 +0800143 struct amdgpu_mode_mc_save *save)
144{
145 return;
146}
147
Baoyou Xie4d446652016-09-18 22:09:35 +0800148static void dce_virtual_set_vga_render_state(struct amdgpu_device *adev,
Emily Deng8e6de752016-08-08 11:31:13 +0800149 bool render)
150{
151 return;
152}
153
154/**
155 * dce_virtual_bandwidth_update - program display watermarks
156 *
157 * @adev: amdgpu_device pointer
158 *
159 * Calculate and program the display watermarks and line
160 * buffer allocation (CIK).
161 */
162static void dce_virtual_bandwidth_update(struct amdgpu_device *adev)
163{
164 return;
165}
166
Emily Deng0d43f3b2016-08-08 11:32:22 +0800167static int dce_virtual_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
168 u16 *green, u16 *blue, uint32_t size)
169{
170 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
171 int i;
172
173 /* userspace palettes are always correct as is */
174 for (i = 0; i < size; i++) {
175 amdgpu_crtc->lut_r[i] = red[i] >> 6;
176 amdgpu_crtc->lut_g[i] = green[i] >> 6;
177 amdgpu_crtc->lut_b[i] = blue[i] >> 6;
178 }
179
180 return 0;
181}
182
183static void dce_virtual_crtc_destroy(struct drm_crtc *crtc)
184{
185 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
186
187 drm_crtc_cleanup(crtc);
188 kfree(amdgpu_crtc);
189}
190
Emily Dengc6e14f42016-08-08 11:30:50 +0800191static const struct drm_crtc_funcs dce_virtual_crtc_funcs = {
192 .cursor_set2 = NULL,
193 .cursor_move = NULL,
Emily Deng0d43f3b2016-08-08 11:32:22 +0800194 .gamma_set = dce_virtual_crtc_gamma_set,
195 .set_config = amdgpu_crtc_set_config,
196 .destroy = dce_virtual_crtc_destroy,
Michel Dänzer325cbba2016-08-04 12:39:37 +0900197 .page_flip_target = amdgpu_crtc_page_flip_target,
Emily Dengc6e14f42016-08-08 11:30:50 +0800198};
199
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800200static void dce_virtual_crtc_dpms(struct drm_crtc *crtc, int mode)
201{
202 struct drm_device *dev = crtc->dev;
203 struct amdgpu_device *adev = dev->dev_private;
204 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
205 unsigned type;
206
Xiangliang Yuebe0a802017-02-14 16:08:18 +0800207 if (amdgpu_sriov_vf(adev))
208 return;
209
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800210 switch (mode) {
211 case DRM_MODE_DPMS_ON:
212 amdgpu_crtc->enabled = true;
Alex Deucher82b9f812016-09-30 11:19:41 -0400213 /* Make sure VBLANK interrupts are still enabled */
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800214 type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
215 amdgpu_irq_update(adev, &adev->crtc_irq, type);
Daniel Vetter2d1e3312016-11-14 10:02:54 +0100216 drm_crtc_vblank_on(crtc);
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800217 break;
218 case DRM_MODE_DPMS_STANDBY:
219 case DRM_MODE_DPMS_SUSPEND:
220 case DRM_MODE_DPMS_OFF:
Daniel Vetter2d1e3312016-11-14 10:02:54 +0100221 drm_crtc_vblank_off(crtc);
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800222 amdgpu_crtc->enabled = false;
223 break;
224 }
225}
226
227
228static void dce_virtual_crtc_prepare(struct drm_crtc *crtc)
229{
230 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
231}
232
233static void dce_virtual_crtc_commit(struct drm_crtc *crtc)
234{
235 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
236}
237
238static void dce_virtual_crtc_disable(struct drm_crtc *crtc)
239{
240 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
241
242 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
243 if (crtc->primary->fb) {
244 int r;
245 struct amdgpu_framebuffer *amdgpu_fb;
Christian König765e7fb2016-09-15 15:06:50 +0200246 struct amdgpu_bo *abo;
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800247
248 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
Christian König765e7fb2016-09-15 15:06:50 +0200249 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
250 r = amdgpu_bo_reserve(abo, false);
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800251 if (unlikely(r))
Christian König765e7fb2016-09-15 15:06:50 +0200252 DRM_ERROR("failed to reserve abo before unpin\n");
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800253 else {
Christian König765e7fb2016-09-15 15:06:50 +0200254 amdgpu_bo_unpin(abo);
255 amdgpu_bo_unreserve(abo);
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800256 }
257 }
258
259 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
260 amdgpu_crtc->encoder = NULL;
261 amdgpu_crtc->connector = NULL;
262}
263
264static int dce_virtual_crtc_mode_set(struct drm_crtc *crtc,
265 struct drm_display_mode *mode,
266 struct drm_display_mode *adjusted_mode,
267 int x, int y, struct drm_framebuffer *old_fb)
268{
269 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
270
271 /* update the hw version fpr dpm */
272 amdgpu_crtc->hw_mode = *adjusted_mode;
273
274 return 0;
275}
276
277static bool dce_virtual_crtc_mode_fixup(struct drm_crtc *crtc,
278 const struct drm_display_mode *mode,
279 struct drm_display_mode *adjusted_mode)
280{
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800281 return true;
282}
283
284
285static int dce_virtual_crtc_set_base(struct drm_crtc *crtc, int x, int y,
286 struct drm_framebuffer *old_fb)
287{
288 return 0;
289}
290
291static void dce_virtual_crtc_load_lut(struct drm_crtc *crtc)
292{
293 return;
294}
295
296static int dce_virtual_crtc_set_base_atomic(struct drm_crtc *crtc,
297 struct drm_framebuffer *fb,
298 int x, int y, enum mode_set_atomic state)
299{
300 return 0;
301}
302
Emily Dengc6e14f42016-08-08 11:30:50 +0800303static const struct drm_crtc_helper_funcs dce_virtual_crtc_helper_funcs = {
Emily Dengf1f5ef9262016-08-08 11:32:00 +0800304 .dpms = dce_virtual_crtc_dpms,
305 .mode_fixup = dce_virtual_crtc_mode_fixup,
306 .mode_set = dce_virtual_crtc_mode_set,
307 .mode_set_base = dce_virtual_crtc_set_base,
308 .mode_set_base_atomic = dce_virtual_crtc_set_base_atomic,
309 .prepare = dce_virtual_crtc_prepare,
310 .commit = dce_virtual_crtc_commit,
311 .load_lut = dce_virtual_crtc_load_lut,
312 .disable = dce_virtual_crtc_disable,
Emily Dengc6e14f42016-08-08 11:30:50 +0800313};
314
315static int dce_virtual_crtc_init(struct amdgpu_device *adev, int index)
316{
317 struct amdgpu_crtc *amdgpu_crtc;
318 int i;
319
320 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
321 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
322 if (amdgpu_crtc == NULL)
323 return -ENOMEM;
324
325 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_virtual_crtc_funcs);
326
327 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
328 amdgpu_crtc->crtc_id = index;
329 adev->mode_info.crtcs[index] = amdgpu_crtc;
330
331 for (i = 0; i < 256; i++) {
332 amdgpu_crtc->lut_r[i] = i << 2;
333 amdgpu_crtc->lut_g[i] = i << 2;
334 amdgpu_crtc->lut_b[i] = i << 2;
335 }
336
337 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
338 amdgpu_crtc->encoder = NULL;
339 amdgpu_crtc->connector = NULL;
Emily Deng0f663562016-09-30 13:02:18 -0400340 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
Emily Dengc6e14f42016-08-08 11:30:50 +0800341 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_virtual_crtc_helper_funcs);
342
343 return 0;
344}
345
346static int dce_virtual_early_init(void *handle)
347{
348 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
349
350 dce_virtual_set_display_funcs(adev);
351 dce_virtual_set_irq_funcs(adev);
352
Emily Dengc6e14f42016-08-08 11:30:50 +0800353 adev->mode_info.num_hpd = 1;
354 adev->mode_info.num_dig = 1;
355 return 0;
356}
357
Alex Deucher66264ba2016-09-30 12:37:36 -0400358static struct drm_encoder *
359dce_virtual_encoder(struct drm_connector *connector)
Emily Dengc6e14f42016-08-08 11:30:50 +0800360{
Alex Deucher66264ba2016-09-30 12:37:36 -0400361 int enc_id = connector->encoder_ids[0];
362 struct drm_encoder *encoder;
363 int i;
Emily Dengc6e14f42016-08-08 11:30:50 +0800364
Alex Deucher66264ba2016-09-30 12:37:36 -0400365 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
366 if (connector->encoder_ids[i] == 0)
367 break;
Emily Dengc6e14f42016-08-08 11:30:50 +0800368
Alex Deucher66264ba2016-09-30 12:37:36 -0400369 encoder = drm_encoder_find(connector->dev, connector->encoder_ids[i]);
370 if (!encoder)
371 continue;
Emily Dengc6e14f42016-08-08 11:30:50 +0800372
Alex Deucher66264ba2016-09-30 12:37:36 -0400373 if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
374 return encoder;
375 }
Emily Dengc6e14f42016-08-08 11:30:50 +0800376
Alex Deucher66264ba2016-09-30 12:37:36 -0400377 /* pick the first one */
378 if (enc_id)
379 return drm_encoder_find(connector->dev, enc_id);
380 return NULL;
Emily Dengc6e14f42016-08-08 11:30:50 +0800381}
382
Alex Deucher66264ba2016-09-30 12:37:36 -0400383static int dce_virtual_get_modes(struct drm_connector *connector)
384{
385 struct drm_device *dev = connector->dev;
386 struct drm_display_mode *mode = NULL;
387 unsigned i;
388 static const struct mode_size {
389 int w;
390 int h;
391 } common_modes[17] = {
392 { 640, 480},
393 { 720, 480},
394 { 800, 600},
395 { 848, 480},
396 {1024, 768},
397 {1152, 768},
398 {1280, 720},
399 {1280, 800},
400 {1280, 854},
401 {1280, 960},
402 {1280, 1024},
403 {1440, 900},
404 {1400, 1050},
405 {1680, 1050},
406 {1600, 1200},
407 {1920, 1080},
408 {1920, 1200}
409 };
410
411 for (i = 0; i < 17; i++) {
412 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
413 drm_mode_probed_add(connector, mode);
414 }
415
416 return 0;
417}
418
419static int dce_virtual_mode_valid(struct drm_connector *connector,
420 struct drm_display_mode *mode)
421{
422 return MODE_OK;
423}
424
425static int
426dce_virtual_dpms(struct drm_connector *connector, int mode)
427{
428 return 0;
429}
430
Alex Deucher66264ba2016-09-30 12:37:36 -0400431static int
432dce_virtual_set_property(struct drm_connector *connector,
433 struct drm_property *property,
434 uint64_t val)
435{
436 return 0;
437}
438
439static void dce_virtual_destroy(struct drm_connector *connector)
440{
441 drm_connector_unregister(connector);
442 drm_connector_cleanup(connector);
443 kfree(connector);
444}
445
446static void dce_virtual_force(struct drm_connector *connector)
447{
448 return;
449}
450
451static const struct drm_connector_helper_funcs dce_virtual_connector_helper_funcs = {
452 .get_modes = dce_virtual_get_modes,
453 .mode_valid = dce_virtual_mode_valid,
454 .best_encoder = dce_virtual_encoder,
455};
456
457static const struct drm_connector_funcs dce_virtual_connector_funcs = {
458 .dpms = dce_virtual_dpms,
Alex Deucher66264ba2016-09-30 12:37:36 -0400459 .fill_modes = drm_helper_probe_single_connector_modes,
460 .set_property = dce_virtual_set_property,
461 .destroy = dce_virtual_destroy,
462 .force = dce_virtual_force,
463};
464
Emily Dengc6e14f42016-08-08 11:30:50 +0800465static int dce_virtual_sw_init(void *handle)
466{
467 int r, i;
468 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
469
Alex Deucherd766e6a2016-03-29 18:28:50 -0400470 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 229, &adev->crtc_irq);
Emily Dengc6e14f42016-08-08 11:30:50 +0800471 if (r)
472 return r;
473
Emily Deng041aa652016-08-17 14:59:20 +0800474 adev->ddev->max_vblank_count = 0;
475
Emily Dengc6e14f42016-08-08 11:30:50 +0800476 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
477
478 adev->ddev->mode_config.max_width = 16384;
479 adev->ddev->mode_config.max_height = 16384;
480
481 adev->ddev->mode_config.preferred_depth = 24;
482 adev->ddev->mode_config.prefer_shadow = 1;
483
484 adev->ddev->mode_config.fb_base = adev->mc.aper_base;
485
486 r = amdgpu_modeset_create_props(adev);
487 if (r)
488 return r;
489
490 adev->ddev->mode_config.max_width = 16384;
491 adev->ddev->mode_config.max_height = 16384;
492
Alex Deucher66264ba2016-09-30 12:37:36 -0400493 /* allocate crtcs, encoders, connectors */
Emily Dengc6e14f42016-08-08 11:30:50 +0800494 for (i = 0; i < adev->mode_info.num_crtc; i++) {
495 r = dce_virtual_crtc_init(adev, i);
496 if (r)
497 return r;
Alex Deucher66264ba2016-09-30 12:37:36 -0400498 r = dce_virtual_connector_encoder_init(adev, i);
499 if (r)
500 return r;
Emily Dengc6e14f42016-08-08 11:30:50 +0800501 }
502
Emily Dengc6e14f42016-08-08 11:30:50 +0800503 drm_kms_helper_poll_init(adev->ddev);
504
505 adev->mode_info.mode_config_initialized = true;
506 return 0;
507}
508
509static int dce_virtual_sw_fini(void *handle)
510{
511 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
512
513 kfree(adev->mode_info.bios_hardcoded_edid);
514
515 drm_kms_helper_poll_fini(adev->ddev);
516
517 drm_mode_config_cleanup(adev->ddev);
518 adev->mode_info.mode_config_initialized = false;
519 return 0;
520}
521
522static int dce_virtual_hw_init(void *handle)
523{
524 return 0;
525}
526
527static int dce_virtual_hw_fini(void *handle)
528{
529 return 0;
530}
531
532static int dce_virtual_suspend(void *handle)
533{
534 return dce_virtual_hw_fini(handle);
535}
536
537static int dce_virtual_resume(void *handle)
538{
Masahiro Yamadad912ade2016-09-14 23:39:08 +0900539 return dce_virtual_hw_init(handle);
Emily Dengc6e14f42016-08-08 11:30:50 +0800540}
541
542static bool dce_virtual_is_idle(void *handle)
543{
544 return true;
545}
546
547static int dce_virtual_wait_for_idle(void *handle)
548{
549 return 0;
550}
551
552static int dce_virtual_soft_reset(void *handle)
553{
554 return 0;
555}
556
557static int dce_virtual_set_clockgating_state(void *handle,
558 enum amd_clockgating_state state)
559{
560 return 0;
561}
562
563static int dce_virtual_set_powergating_state(void *handle,
564 enum amd_powergating_state state)
565{
566 return 0;
567}
568
Alex Deuchera1255102016-10-13 17:41:13 -0400569static const struct amd_ip_funcs dce_virtual_ip_funcs = {
Emily Dengc6e14f42016-08-08 11:30:50 +0800570 .name = "dce_virtual",
571 .early_init = dce_virtual_early_init,
572 .late_init = NULL,
573 .sw_init = dce_virtual_sw_init,
574 .sw_fini = dce_virtual_sw_fini,
575 .hw_init = dce_virtual_hw_init,
576 .hw_fini = dce_virtual_hw_fini,
577 .suspend = dce_virtual_suspend,
578 .resume = dce_virtual_resume,
579 .is_idle = dce_virtual_is_idle,
580 .wait_for_idle = dce_virtual_wait_for_idle,
581 .soft_reset = dce_virtual_soft_reset,
582 .set_clockgating_state = dce_virtual_set_clockgating_state,
583 .set_powergating_state = dce_virtual_set_powergating_state,
584};
585
Emily Deng8e6de752016-08-08 11:31:13 +0800586/* these are handled by the primary encoders */
587static void dce_virtual_encoder_prepare(struct drm_encoder *encoder)
588{
589 return;
590}
591
592static void dce_virtual_encoder_commit(struct drm_encoder *encoder)
593{
594 return;
595}
596
597static void
598dce_virtual_encoder_mode_set(struct drm_encoder *encoder,
Alex Deucher66264ba2016-09-30 12:37:36 -0400599 struct drm_display_mode *mode,
600 struct drm_display_mode *adjusted_mode)
Emily Deng8e6de752016-08-08 11:31:13 +0800601{
602 return;
603}
604
605static void dce_virtual_encoder_disable(struct drm_encoder *encoder)
606{
607 return;
608}
609
610static void
611dce_virtual_encoder_dpms(struct drm_encoder *encoder, int mode)
612{
613 return;
614}
615
616static bool dce_virtual_encoder_mode_fixup(struct drm_encoder *encoder,
617 const struct drm_display_mode *mode,
618 struct drm_display_mode *adjusted_mode)
619{
Emily Deng8e6de752016-08-08 11:31:13 +0800620 return true;
621}
622
623static const struct drm_encoder_helper_funcs dce_virtual_encoder_helper_funcs = {
624 .dpms = dce_virtual_encoder_dpms,
625 .mode_fixup = dce_virtual_encoder_mode_fixup,
626 .prepare = dce_virtual_encoder_prepare,
627 .mode_set = dce_virtual_encoder_mode_set,
628 .commit = dce_virtual_encoder_commit,
629 .disable = dce_virtual_encoder_disable,
630};
631
632static void dce_virtual_encoder_destroy(struct drm_encoder *encoder)
633{
Emily Deng8e6de752016-08-08 11:31:13 +0800634 drm_encoder_cleanup(encoder);
Xiangliang Yu3a1d19a2017-01-19 09:57:41 +0800635 kfree(encoder);
Emily Deng8e6de752016-08-08 11:31:13 +0800636}
637
638static const struct drm_encoder_funcs dce_virtual_encoder_funcs = {
639 .destroy = dce_virtual_encoder_destroy,
640};
641
Alex Deucher66264ba2016-09-30 12:37:36 -0400642static int dce_virtual_connector_encoder_init(struct amdgpu_device *adev,
643 int index)
Emily Deng8e6de752016-08-08 11:31:13 +0800644{
Emily Deng8e6de752016-08-08 11:31:13 +0800645 struct drm_encoder *encoder;
Alex Deucher66264ba2016-09-30 12:37:36 -0400646 struct drm_connector *connector;
Emily Deng8e6de752016-08-08 11:31:13 +0800647
Alex Deucher66264ba2016-09-30 12:37:36 -0400648 /* add a new encoder */
649 encoder = kzalloc(sizeof(struct drm_encoder), GFP_KERNEL);
650 if (!encoder)
651 return -ENOMEM;
652 encoder->possible_crtcs = 1 << index;
653 drm_encoder_init(adev->ddev, encoder, &dce_virtual_encoder_funcs,
654 DRM_MODE_ENCODER_VIRTUAL, NULL);
655 drm_encoder_helper_add(encoder, &dce_virtual_encoder_helper_funcs);
Emily Deng8e6de752016-08-08 11:31:13 +0800656
Alex Deucher66264ba2016-09-30 12:37:36 -0400657 connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
658 if (!connector) {
659 kfree(encoder);
660 return -ENOMEM;
Emily Deng8e6de752016-08-08 11:31:13 +0800661 }
662
Alex Deucher66264ba2016-09-30 12:37:36 -0400663 /* add a new connector */
664 drm_connector_init(adev->ddev, connector, &dce_virtual_connector_funcs,
665 DRM_MODE_CONNECTOR_VIRTUAL);
666 drm_connector_helper_add(connector, &dce_virtual_connector_helper_funcs);
667 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
668 connector->interlace_allowed = false;
669 connector->doublescan_allowed = false;
670 drm_connector_register(connector);
Emily Deng8e6de752016-08-08 11:31:13 +0800671
Alex Deucher66264ba2016-09-30 12:37:36 -0400672 /* link them */
673 drm_mode_connector_attach_encoder(connector, encoder);
Emily Deng8e6de752016-08-08 11:31:13 +0800674
Alex Deucher66264ba2016-09-30 12:37:36 -0400675 return 0;
Emily Deng8e6de752016-08-08 11:31:13 +0800676}
677
Emily Dengc6e14f42016-08-08 11:30:50 +0800678static const struct amdgpu_display_funcs dce_virtual_display_funcs = {
Emily Deng8e6de752016-08-08 11:31:13 +0800679 .set_vga_render_state = &dce_virtual_set_vga_render_state,
680 .bandwidth_update = &dce_virtual_bandwidth_update,
681 .vblank_get_counter = &dce_virtual_vblank_get_counter,
682 .vblank_wait = &dce_virtual_vblank_wait,
Emily Dengc6e14f42016-08-08 11:30:50 +0800683 .backlight_set_level = NULL,
684 .backlight_get_level = NULL,
Emily Deng8e6de752016-08-08 11:31:13 +0800685 .hpd_sense = &dce_virtual_hpd_sense,
686 .hpd_set_polarity = &dce_virtual_hpd_set_polarity,
687 .hpd_get_gpio_reg = &dce_virtual_hpd_get_gpio_reg,
688 .page_flip = &dce_virtual_page_flip,
689 .page_flip_get_scanoutpos = &dce_virtual_crtc_get_scanoutpos,
Alex Deucher66264ba2016-09-30 12:37:36 -0400690 .add_encoder = NULL,
691 .add_connector = NULL,
Emily Deng8e6de752016-08-08 11:31:13 +0800692 .stop_mc_access = &dce_virtual_stop_mc_access,
693 .resume_mc_access = &dce_virtual_resume_mc_access,
Emily Dengc6e14f42016-08-08 11:30:50 +0800694};
695
696static void dce_virtual_set_display_funcs(struct amdgpu_device *adev)
697{
698 if (adev->mode_info.funcs == NULL)
699 adev->mode_info.funcs = &dce_virtual_display_funcs;
700}
701
Alex Deucher9405e472016-09-30 11:41:37 -0400702static int dce_virtual_pageflip(struct amdgpu_device *adev,
703 unsigned crtc_id)
704{
705 unsigned long flags;
706 struct amdgpu_crtc *amdgpu_crtc;
707 struct amdgpu_flip_work *works;
708
709 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
710
711 if (crtc_id >= adev->mode_info.num_crtc) {
712 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
713 return -EINVAL;
714 }
715
716 /* IRQ could occur when in initial stage */
717 if (amdgpu_crtc == NULL)
718 return 0;
719
720 spin_lock_irqsave(&adev->ddev->event_lock, flags);
721 works = amdgpu_crtc->pflip_works;
722 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
723 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
724 "AMDGPU_FLIP_SUBMITTED(%d)\n",
725 amdgpu_crtc->pflip_status,
726 AMDGPU_FLIP_SUBMITTED);
727 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
728 return 0;
729 }
730
731 /* page flip completed. clean up */
732 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
733 amdgpu_crtc->pflip_works = NULL;
734
735 /* wakeup usersapce */
736 if (works->event)
737 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
738
739 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
740
741 drm_crtc_vblank_put(&amdgpu_crtc->base);
742 schedule_work(&works->unpin_work);
743
744 return 0;
745}
746
Emily Deng46ac3622016-08-08 11:35:39 +0800747static enum hrtimer_restart dce_virtual_vblank_timer_handle(struct hrtimer *vblank_timer)
748{
Emily Deng0f663562016-09-30 13:02:18 -0400749 struct amdgpu_crtc *amdgpu_crtc = container_of(vblank_timer,
750 struct amdgpu_crtc, vblank_timer);
751 struct drm_device *ddev = amdgpu_crtc->base.dev;
752 struct amdgpu_device *adev = ddev->dev_private;
Alex Deucher9405e472016-09-30 11:41:37 -0400753
Emily Deng0f663562016-09-30 13:02:18 -0400754 drm_handle_vblank(ddev, amdgpu_crtc->crtc_id);
755 dce_virtual_pageflip(adev, amdgpu_crtc->crtc_id);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100756 hrtimer_start(vblank_timer, DCE_VIRTUAL_VBLANK_PERIOD,
Alex Deucher9405e472016-09-30 11:41:37 -0400757 HRTIMER_MODE_REL);
758
Emily Deng46ac3622016-08-08 11:35:39 +0800759 return HRTIMER_NORESTART;
760}
761
Emily Denge13273d2016-08-08 11:31:37 +0800762static void dce_virtual_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
Alex Deucher82b9f812016-09-30 11:19:41 -0400763 int crtc,
764 enum amdgpu_interrupt_state state)
Emily Denge13273d2016-08-08 11:31:37 +0800765{
766 if (crtc >= adev->mode_info.num_crtc) {
767 DRM_DEBUG("invalid crtc %d\n", crtc);
768 return;
769 }
Emily Deng46ac3622016-08-08 11:35:39 +0800770
Emily Deng0f663562016-09-30 13:02:18 -0400771 if (state && !adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
Emily Deng46ac3622016-08-08 11:35:39 +0800772 DRM_DEBUG("Enable software vsync timer\n");
Emily Deng0f663562016-09-30 13:02:18 -0400773 hrtimer_init(&adev->mode_info.crtcs[crtc]->vblank_timer,
774 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
775 hrtimer_set_expires(&adev->mode_info.crtcs[crtc]->vblank_timer,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100776 DCE_VIRTUAL_VBLANK_PERIOD);
Emily Deng0f663562016-09-30 13:02:18 -0400777 adev->mode_info.crtcs[crtc]->vblank_timer.function =
778 dce_virtual_vblank_timer_handle;
779 hrtimer_start(&adev->mode_info.crtcs[crtc]->vblank_timer,
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100780 DCE_VIRTUAL_VBLANK_PERIOD, HRTIMER_MODE_REL);
Emily Deng0f663562016-09-30 13:02:18 -0400781 } else if (!state && adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
Emily Deng46ac3622016-08-08 11:35:39 +0800782 DRM_DEBUG("Disable software vsync timer\n");
Emily Deng0f663562016-09-30 13:02:18 -0400783 hrtimer_cancel(&adev->mode_info.crtcs[crtc]->vblank_timer);
Emily Deng46ac3622016-08-08 11:35:39 +0800784 }
785
Emily Deng0f663562016-09-30 13:02:18 -0400786 adev->mode_info.crtcs[crtc]->vsync_timer_enabled = state;
Emily Deng46ac3622016-08-08 11:35:39 +0800787 DRM_DEBUG("[FM]set crtc %d vblank interrupt state %d\n", crtc, state);
Emily Denge13273d2016-08-08 11:31:37 +0800788}
789
Emily Deng46ac3622016-08-08 11:35:39 +0800790
Emily Denge13273d2016-08-08 11:31:37 +0800791static int dce_virtual_set_crtc_irq_state(struct amdgpu_device *adev,
Alex Deucher82b9f812016-09-30 11:19:41 -0400792 struct amdgpu_irq_src *source,
793 unsigned type,
794 enum amdgpu_interrupt_state state)
Emily Denge13273d2016-08-08 11:31:37 +0800795{
Emily Deng0f663562016-09-30 13:02:18 -0400796 if (type > AMDGPU_CRTC_IRQ_VBLANK6)
797 return -EINVAL;
798
799 dce_virtual_set_crtc_vblank_interrupt_state(adev, type, state);
800
Emily Denge13273d2016-08-08 11:31:37 +0800801 return 0;
802}
803
Emily Dengc6e14f42016-08-08 11:30:50 +0800804static const struct amdgpu_irq_src_funcs dce_virtual_crtc_irq_funcs = {
Emily Denge13273d2016-08-08 11:31:37 +0800805 .set = dce_virtual_set_crtc_irq_state,
Alex Deucherbf2335a2016-09-30 11:23:30 -0400806 .process = NULL,
Emily Dengc6e14f42016-08-08 11:30:50 +0800807};
808
Emily Dengc6e14f42016-08-08 11:30:50 +0800809static void dce_virtual_set_irq_funcs(struct amdgpu_device *adev)
810{
811 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
812 adev->crtc_irq.funcs = &dce_virtual_crtc_irq_funcs;
Emily Dengc6e14f42016-08-08 11:30:50 +0800813}
814
Alex Deuchera1255102016-10-13 17:41:13 -0400815const struct amdgpu_ip_block_version dce_virtual_ip_block =
816{
817 .type = AMD_IP_BLOCK_TYPE_DCE,
818 .major = 1,
819 .minor = 0,
820 .rev = 0,
821 .funcs = &dce_virtual_ip_funcs,
822};