blob: 7d041b9a4d61e0bdb4c450d44625d5827806514f [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
98static bool dce_virtual_is_display_hung(struct amdgpu_device *adev)
99{
100 return false;
101}
102
Baoyou Xie4d446652016-09-18 22:09:35 +0800103static void dce_virtual_stop_mc_access(struct amdgpu_device *adev,
Emily Deng8e6de752016-08-08 11:31:13 +0800104 struct amdgpu_mode_mc_save *save)
105{
Emily Deng83c9b022016-08-08 11:33:11 +0800106 switch (adev->asic_type) {
Alex Deuchera1d37042016-09-29 23:36:12 -0400107#ifdef CONFIG_DRM_AMDGPU_SI
108 case CHIP_TAHITI:
109 case CHIP_PITCAIRN:
110 case CHIP_VERDE:
111 case CHIP_OLAND:
112 dce_v6_0_disable_dce(adev);
113 break;
114#endif
Alex Deucher8cb619d2016-09-29 23:20:29 -0400115#ifdef CONFIG_DRM_AMDGPU_CIK
Emily Deng83c9b022016-08-08 11:33:11 +0800116 case CHIP_BONAIRE:
117 case CHIP_HAWAII:
118 case CHIP_KAVERI:
119 case CHIP_KABINI:
120 case CHIP_MULLINS:
Emily Deng83c9b022016-08-08 11:33:11 +0800121 dce_v8_0_disable_dce(adev);
Emily Deng83c9b022016-08-08 11:33:11 +0800122 break;
Alex Deucher8cb619d2016-09-29 23:20:29 -0400123#endif
Emily Deng83c9b022016-08-08 11:33:11 +0800124 case CHIP_FIJI:
125 case CHIP_TONGA:
126 dce_v10_0_disable_dce(adev);
127 break;
128 case CHIP_CARRIZO:
129 case CHIP_STONEY:
130 case CHIP_POLARIS11:
131 case CHIP_POLARIS10:
132 dce_v11_0_disable_dce(adev);
133 break;
Alex Deucher2579de42016-08-08 14:40:04 -0400134 case CHIP_TOPAZ:
Alex Deuchera1d37042016-09-29 23:36:12 -0400135#ifdef CONFIG_DRM_AMDGPU_SI
136 case CHIP_HAINAN:
137#endif
Alex Deucher2579de42016-08-08 14:40:04 -0400138 /* no DCE */
139 return;
Emily Deng83c9b022016-08-08 11:33:11 +0800140 default:
Alex Deucher2579de42016-08-08 14:40:04 -0400141 DRM_ERROR("Virtual display unsupported ASIC type: 0x%X\n", adev->asic_type);
Emily Deng83c9b022016-08-08 11:33:11 +0800142 }
143
Emily Deng8e6de752016-08-08 11:31:13 +0800144 return;
145}
Baoyou Xie4d446652016-09-18 22:09:35 +0800146static void dce_virtual_resume_mc_access(struct amdgpu_device *adev,
Emily Deng8e6de752016-08-08 11:31:13 +0800147 struct amdgpu_mode_mc_save *save)
148{
149 return;
150}
151
Baoyou Xie4d446652016-09-18 22:09:35 +0800152static void dce_virtual_set_vga_render_state(struct amdgpu_device *adev,
Emily Deng8e6de752016-08-08 11:31:13 +0800153 bool render)
154{
155 return;
156}
157
158/**
159 * dce_virtual_bandwidth_update - program display watermarks
160 *
161 * @adev: amdgpu_device pointer
162 *
163 * Calculate and program the display watermarks and line
164 * buffer allocation (CIK).
165 */
166static void dce_virtual_bandwidth_update(struct amdgpu_device *adev)
167{
168 return;
169}
170
Emily Deng0d43f3b2016-08-08 11:32:22 +0800171static int dce_virtual_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
172 u16 *green, u16 *blue, uint32_t size)
173{
174 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
175 int i;
176
177 /* userspace palettes are always correct as is */
178 for (i = 0; i < size; i++) {
179 amdgpu_crtc->lut_r[i] = red[i] >> 6;
180 amdgpu_crtc->lut_g[i] = green[i] >> 6;
181 amdgpu_crtc->lut_b[i] = blue[i] >> 6;
182 }
183
184 return 0;
185}
186
187static void dce_virtual_crtc_destroy(struct drm_crtc *crtc)
188{
189 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
190
191 drm_crtc_cleanup(crtc);
192 kfree(amdgpu_crtc);
193}
194
Emily Dengc6e14f42016-08-08 11:30:50 +0800195static const struct drm_crtc_funcs dce_virtual_crtc_funcs = {
196 .cursor_set2 = NULL,
197 .cursor_move = NULL,
Emily Deng0d43f3b2016-08-08 11:32:22 +0800198 .gamma_set = dce_virtual_crtc_gamma_set,
199 .set_config = amdgpu_crtc_set_config,
200 .destroy = dce_virtual_crtc_destroy,
Michel Dänzer325cbba2016-08-04 12:39:37 +0900201 .page_flip_target = amdgpu_crtc_page_flip_target,
Emily Dengc6e14f42016-08-08 11:30:50 +0800202};
203
Emily Dengf1f5ef92016-08-08 11:32:00 +0800204static void dce_virtual_crtc_dpms(struct drm_crtc *crtc, int mode)
205{
206 struct drm_device *dev = crtc->dev;
207 struct amdgpu_device *adev = dev->dev_private;
208 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
209 unsigned type;
210
211 switch (mode) {
212 case DRM_MODE_DPMS_ON:
213 amdgpu_crtc->enabled = true;
Alex Deucher82b9f812016-09-30 11:19:41 -0400214 /* Make sure VBLANK interrupts are still enabled */
Emily Dengf1f5ef92016-08-08 11:32:00 +0800215 type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
216 amdgpu_irq_update(adev, &adev->crtc_irq, type);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800217 drm_vblank_on(dev, amdgpu_crtc->crtc_id);
218 break;
219 case DRM_MODE_DPMS_STANDBY:
220 case DRM_MODE_DPMS_SUSPEND:
221 case DRM_MODE_DPMS_OFF:
222 drm_vblank_off(dev, amdgpu_crtc->crtc_id);
223 amdgpu_crtc->enabled = false;
224 break;
225 }
226}
227
228
229static void dce_virtual_crtc_prepare(struct drm_crtc *crtc)
230{
231 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
232}
233
234static void dce_virtual_crtc_commit(struct drm_crtc *crtc)
235{
236 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
237}
238
239static void dce_virtual_crtc_disable(struct drm_crtc *crtc)
240{
241 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
242
243 dce_virtual_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
244 if (crtc->primary->fb) {
245 int r;
246 struct amdgpu_framebuffer *amdgpu_fb;
Christian König765e7fb2016-09-15 15:06:50 +0200247 struct amdgpu_bo *abo;
Emily Dengf1f5ef92016-08-08 11:32:00 +0800248
249 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
Christian König765e7fb2016-09-15 15:06:50 +0200250 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
251 r = amdgpu_bo_reserve(abo, false);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800252 if (unlikely(r))
Christian König765e7fb2016-09-15 15:06:50 +0200253 DRM_ERROR("failed to reserve abo before unpin\n");
Emily Dengf1f5ef92016-08-08 11:32:00 +0800254 else {
Christian König765e7fb2016-09-15 15:06:50 +0200255 amdgpu_bo_unpin(abo);
256 amdgpu_bo_unreserve(abo);
Emily Dengf1f5ef92016-08-08 11:32:00 +0800257 }
258 }
259
260 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
261 amdgpu_crtc->encoder = NULL;
262 amdgpu_crtc->connector = NULL;
263}
264
265static int dce_virtual_crtc_mode_set(struct drm_crtc *crtc,
266 struct drm_display_mode *mode,
267 struct drm_display_mode *adjusted_mode,
268 int x, int y, struct drm_framebuffer *old_fb)
269{
270 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
271
272 /* update the hw version fpr dpm */
273 amdgpu_crtc->hw_mode = *adjusted_mode;
274
275 return 0;
276}
277
278static bool dce_virtual_crtc_mode_fixup(struct drm_crtc *crtc,
279 const struct drm_display_mode *mode,
280 struct drm_display_mode *adjusted_mode)
281{
Emily Dengf1f5ef92016-08-08 11:32:00 +0800282 return true;
283}
284
285
286static int dce_virtual_crtc_set_base(struct drm_crtc *crtc, int x, int y,
287 struct drm_framebuffer *old_fb)
288{
289 return 0;
290}
291
292static void dce_virtual_crtc_load_lut(struct drm_crtc *crtc)
293{
294 return;
295}
296
297static int dce_virtual_crtc_set_base_atomic(struct drm_crtc *crtc,
298 struct drm_framebuffer *fb,
299 int x, int y, enum mode_set_atomic state)
300{
301 return 0;
302}
303
Emily Dengc6e14f42016-08-08 11:30:50 +0800304static const struct drm_crtc_helper_funcs dce_virtual_crtc_helper_funcs = {
Emily Dengf1f5ef92016-08-08 11:32:00 +0800305 .dpms = dce_virtual_crtc_dpms,
306 .mode_fixup = dce_virtual_crtc_mode_fixup,
307 .mode_set = dce_virtual_crtc_mode_set,
308 .mode_set_base = dce_virtual_crtc_set_base,
309 .mode_set_base_atomic = dce_virtual_crtc_set_base_atomic,
310 .prepare = dce_virtual_crtc_prepare,
311 .commit = dce_virtual_crtc_commit,
312 .load_lut = dce_virtual_crtc_load_lut,
313 .disable = dce_virtual_crtc_disable,
Emily Dengc6e14f42016-08-08 11:30:50 +0800314};
315
316static int dce_virtual_crtc_init(struct amdgpu_device *adev, int index)
317{
318 struct amdgpu_crtc *amdgpu_crtc;
319 int i;
320
321 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
322 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
323 if (amdgpu_crtc == NULL)
324 return -ENOMEM;
325
326 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_virtual_crtc_funcs);
327
328 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
329 amdgpu_crtc->crtc_id = index;
330 adev->mode_info.crtcs[index] = amdgpu_crtc;
331
332 for (i = 0; i < 256; i++) {
333 amdgpu_crtc->lut_r[i] = i << 2;
334 amdgpu_crtc->lut_g[i] = i << 2;
335 amdgpu_crtc->lut_b[i] = i << 2;
336 }
337
338 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
339 amdgpu_crtc->encoder = NULL;
340 amdgpu_crtc->connector = NULL;
Emily Deng0f663562016-09-30 13:02:18 -0400341 amdgpu_crtc->vsync_timer_enabled = AMDGPU_IRQ_STATE_DISABLE;
Emily Dengc6e14f42016-08-08 11:30:50 +0800342 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_virtual_crtc_helper_funcs);
343
344 return 0;
345}
346
347static int dce_virtual_early_init(void *handle)
348{
349 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
350
351 dce_virtual_set_display_funcs(adev);
352 dce_virtual_set_irq_funcs(adev);
353
Emily Dengc6e14f42016-08-08 11:30:50 +0800354 adev->mode_info.num_hpd = 1;
355 adev->mode_info.num_dig = 1;
356 return 0;
357}
358
Alex Deucher66264ba2016-09-30 12:37:36 -0400359static struct drm_encoder *
360dce_virtual_encoder(struct drm_connector *connector)
Emily Dengc6e14f42016-08-08 11:30:50 +0800361{
Alex Deucher66264ba2016-09-30 12:37:36 -0400362 int enc_id = connector->encoder_ids[0];
363 struct drm_encoder *encoder;
364 int i;
Emily Dengc6e14f42016-08-08 11:30:50 +0800365
Alex Deucher66264ba2016-09-30 12:37:36 -0400366 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
367 if (connector->encoder_ids[i] == 0)
368 break;
Emily Dengc6e14f42016-08-08 11:30:50 +0800369
Alex Deucher66264ba2016-09-30 12:37:36 -0400370 encoder = drm_encoder_find(connector->dev, connector->encoder_ids[i]);
371 if (!encoder)
372 continue;
Emily Dengc6e14f42016-08-08 11:30:50 +0800373
Alex Deucher66264ba2016-09-30 12:37:36 -0400374 if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
375 return encoder;
376 }
Emily Dengc6e14f42016-08-08 11:30:50 +0800377
Alex Deucher66264ba2016-09-30 12:37:36 -0400378 /* pick the first one */
379 if (enc_id)
380 return drm_encoder_find(connector->dev, enc_id);
381 return NULL;
Emily Dengc6e14f42016-08-08 11:30:50 +0800382}
383
Alex Deucher66264ba2016-09-30 12:37:36 -0400384static int dce_virtual_get_modes(struct drm_connector *connector)
385{
386 struct drm_device *dev = connector->dev;
387 struct drm_display_mode *mode = NULL;
388 unsigned i;
389 static const struct mode_size {
390 int w;
391 int h;
392 } common_modes[17] = {
393 { 640, 480},
394 { 720, 480},
395 { 800, 600},
396 { 848, 480},
397 {1024, 768},
398 {1152, 768},
399 {1280, 720},
400 {1280, 800},
401 {1280, 854},
402 {1280, 960},
403 {1280, 1024},
404 {1440, 900},
405 {1400, 1050},
406 {1680, 1050},
407 {1600, 1200},
408 {1920, 1080},
409 {1920, 1200}
410 };
411
412 for (i = 0; i < 17; i++) {
413 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
414 drm_mode_probed_add(connector, mode);
415 }
416
417 return 0;
418}
419
420static int dce_virtual_mode_valid(struct drm_connector *connector,
421 struct drm_display_mode *mode)
422{
423 return MODE_OK;
424}
425
426static int
427dce_virtual_dpms(struct drm_connector *connector, int mode)
428{
429 return 0;
430}
431
432static enum drm_connector_status
433dce_virtual_detect(struct drm_connector *connector, bool force)
434{
435 return connector_status_connected;
436}
437
438static int
439dce_virtual_set_property(struct drm_connector *connector,
440 struct drm_property *property,
441 uint64_t val)
442{
443 return 0;
444}
445
446static void dce_virtual_destroy(struct drm_connector *connector)
447{
448 drm_connector_unregister(connector);
449 drm_connector_cleanup(connector);
450 kfree(connector);
451}
452
453static void dce_virtual_force(struct drm_connector *connector)
454{
455 return;
456}
457
458static const struct drm_connector_helper_funcs dce_virtual_connector_helper_funcs = {
459 .get_modes = dce_virtual_get_modes,
460 .mode_valid = dce_virtual_mode_valid,
461 .best_encoder = dce_virtual_encoder,
462};
463
464static const struct drm_connector_funcs dce_virtual_connector_funcs = {
465 .dpms = dce_virtual_dpms,
466 .detect = dce_virtual_detect,
467 .fill_modes = drm_helper_probe_single_connector_modes,
468 .set_property = dce_virtual_set_property,
469 .destroy = dce_virtual_destroy,
470 .force = dce_virtual_force,
471};
472
Emily Dengc6e14f42016-08-08 11:30:50 +0800473static int dce_virtual_sw_init(void *handle)
474{
475 int r, i;
476 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
477
478 r = amdgpu_irq_add_id(adev, 229, &adev->crtc_irq);
479 if (r)
480 return r;
481
Emily Deng041aa652016-08-17 14:59:20 +0800482 adev->ddev->max_vblank_count = 0;
483
Emily Dengc6e14f42016-08-08 11:30:50 +0800484 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
485
486 adev->ddev->mode_config.max_width = 16384;
487 adev->ddev->mode_config.max_height = 16384;
488
489 adev->ddev->mode_config.preferred_depth = 24;
490 adev->ddev->mode_config.prefer_shadow = 1;
491
492 adev->ddev->mode_config.fb_base = adev->mc.aper_base;
493
494 r = amdgpu_modeset_create_props(adev);
495 if (r)
496 return r;
497
498 adev->ddev->mode_config.max_width = 16384;
499 adev->ddev->mode_config.max_height = 16384;
500
Alex Deucher66264ba2016-09-30 12:37:36 -0400501 /* allocate crtcs, encoders, connectors */
Emily Dengc6e14f42016-08-08 11:30:50 +0800502 for (i = 0; i < adev->mode_info.num_crtc; i++) {
503 r = dce_virtual_crtc_init(adev, i);
504 if (r)
505 return r;
Alex Deucher66264ba2016-09-30 12:37:36 -0400506 r = dce_virtual_connector_encoder_init(adev, i);
507 if (r)
508 return r;
Emily Dengc6e14f42016-08-08 11:30:50 +0800509 }
510
Emily Dengc6e14f42016-08-08 11:30:50 +0800511 drm_kms_helper_poll_init(adev->ddev);
512
513 adev->mode_info.mode_config_initialized = true;
514 return 0;
515}
516
517static int dce_virtual_sw_fini(void *handle)
518{
519 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
520
521 kfree(adev->mode_info.bios_hardcoded_edid);
522
523 drm_kms_helper_poll_fini(adev->ddev);
524
525 drm_mode_config_cleanup(adev->ddev);
526 adev->mode_info.mode_config_initialized = false;
527 return 0;
528}
529
530static int dce_virtual_hw_init(void *handle)
531{
532 return 0;
533}
534
535static int dce_virtual_hw_fini(void *handle)
536{
537 return 0;
538}
539
540static int dce_virtual_suspend(void *handle)
541{
542 return dce_virtual_hw_fini(handle);
543}
544
545static int dce_virtual_resume(void *handle)
546{
Masahiro Yamadad912ade2016-09-14 23:39:08 +0900547 return dce_virtual_hw_init(handle);
Emily Dengc6e14f42016-08-08 11:30:50 +0800548}
549
550static bool dce_virtual_is_idle(void *handle)
551{
552 return true;
553}
554
555static int dce_virtual_wait_for_idle(void *handle)
556{
557 return 0;
558}
559
560static int dce_virtual_soft_reset(void *handle)
561{
562 return 0;
563}
564
565static int dce_virtual_set_clockgating_state(void *handle,
566 enum amd_clockgating_state state)
567{
568 return 0;
569}
570
571static int dce_virtual_set_powergating_state(void *handle,
572 enum amd_powergating_state state)
573{
574 return 0;
575}
576
577const struct amd_ip_funcs dce_virtual_ip_funcs = {
578 .name = "dce_virtual",
579 .early_init = dce_virtual_early_init,
580 .late_init = NULL,
581 .sw_init = dce_virtual_sw_init,
582 .sw_fini = dce_virtual_sw_fini,
583 .hw_init = dce_virtual_hw_init,
584 .hw_fini = dce_virtual_hw_fini,
585 .suspend = dce_virtual_suspend,
586 .resume = dce_virtual_resume,
587 .is_idle = dce_virtual_is_idle,
588 .wait_for_idle = dce_virtual_wait_for_idle,
589 .soft_reset = dce_virtual_soft_reset,
590 .set_clockgating_state = dce_virtual_set_clockgating_state,
591 .set_powergating_state = dce_virtual_set_powergating_state,
592};
593
Emily Deng8e6de752016-08-08 11:31:13 +0800594/* these are handled by the primary encoders */
595static void dce_virtual_encoder_prepare(struct drm_encoder *encoder)
596{
597 return;
598}
599
600static void dce_virtual_encoder_commit(struct drm_encoder *encoder)
601{
602 return;
603}
604
605static void
606dce_virtual_encoder_mode_set(struct drm_encoder *encoder,
Alex Deucher66264ba2016-09-30 12:37:36 -0400607 struct drm_display_mode *mode,
608 struct drm_display_mode *adjusted_mode)
Emily Deng8e6de752016-08-08 11:31:13 +0800609{
610 return;
611}
612
613static void dce_virtual_encoder_disable(struct drm_encoder *encoder)
614{
615 return;
616}
617
618static void
619dce_virtual_encoder_dpms(struct drm_encoder *encoder, int mode)
620{
621 return;
622}
623
624static bool dce_virtual_encoder_mode_fixup(struct drm_encoder *encoder,
625 const struct drm_display_mode *mode,
626 struct drm_display_mode *adjusted_mode)
627{
Emily Deng8e6de752016-08-08 11:31:13 +0800628 return true;
629}
630
631static const struct drm_encoder_helper_funcs dce_virtual_encoder_helper_funcs = {
632 .dpms = dce_virtual_encoder_dpms,
633 .mode_fixup = dce_virtual_encoder_mode_fixup,
634 .prepare = dce_virtual_encoder_prepare,
635 .mode_set = dce_virtual_encoder_mode_set,
636 .commit = dce_virtual_encoder_commit,
637 .disable = dce_virtual_encoder_disable,
638};
639
640static void dce_virtual_encoder_destroy(struct drm_encoder *encoder)
641{
642 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
643
644 kfree(amdgpu_encoder->enc_priv);
645 drm_encoder_cleanup(encoder);
646 kfree(amdgpu_encoder);
647}
648
649static const struct drm_encoder_funcs dce_virtual_encoder_funcs = {
650 .destroy = dce_virtual_encoder_destroy,
651};
652
Alex Deucher66264ba2016-09-30 12:37:36 -0400653static int dce_virtual_connector_encoder_init(struct amdgpu_device *adev,
654 int index)
Emily Deng8e6de752016-08-08 11:31:13 +0800655{
Emily Deng8e6de752016-08-08 11:31:13 +0800656 struct drm_encoder *encoder;
Alex Deucher66264ba2016-09-30 12:37:36 -0400657 struct drm_connector *connector;
Emily Deng8e6de752016-08-08 11:31:13 +0800658
Alex Deucher66264ba2016-09-30 12:37:36 -0400659 /* add a new encoder */
660 encoder = kzalloc(sizeof(struct drm_encoder), GFP_KERNEL);
661 if (!encoder)
662 return -ENOMEM;
663 encoder->possible_crtcs = 1 << index;
664 drm_encoder_init(adev->ddev, encoder, &dce_virtual_encoder_funcs,
665 DRM_MODE_ENCODER_VIRTUAL, NULL);
666 drm_encoder_helper_add(encoder, &dce_virtual_encoder_helper_funcs);
Emily Deng8e6de752016-08-08 11:31:13 +0800667
Alex Deucher66264ba2016-09-30 12:37:36 -0400668 connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
669 if (!connector) {
670 kfree(encoder);
671 return -ENOMEM;
Emily Deng8e6de752016-08-08 11:31:13 +0800672 }
673
Alex Deucher66264ba2016-09-30 12:37:36 -0400674 /* add a new connector */
675 drm_connector_init(adev->ddev, connector, &dce_virtual_connector_funcs,
676 DRM_MODE_CONNECTOR_VIRTUAL);
677 drm_connector_helper_add(connector, &dce_virtual_connector_helper_funcs);
678 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
679 connector->interlace_allowed = false;
680 connector->doublescan_allowed = false;
681 drm_connector_register(connector);
Emily Deng8e6de752016-08-08 11:31:13 +0800682
Alex Deucher66264ba2016-09-30 12:37:36 -0400683 /* link them */
684 drm_mode_connector_attach_encoder(connector, encoder);
Emily Deng8e6de752016-08-08 11:31:13 +0800685
Alex Deucher66264ba2016-09-30 12:37:36 -0400686 return 0;
Emily Deng8e6de752016-08-08 11:31:13 +0800687}
688
Emily Dengc6e14f42016-08-08 11:30:50 +0800689static const struct amdgpu_display_funcs dce_virtual_display_funcs = {
Emily Deng8e6de752016-08-08 11:31:13 +0800690 .set_vga_render_state = &dce_virtual_set_vga_render_state,
691 .bandwidth_update = &dce_virtual_bandwidth_update,
692 .vblank_get_counter = &dce_virtual_vblank_get_counter,
693 .vblank_wait = &dce_virtual_vblank_wait,
694 .is_display_hung = &dce_virtual_is_display_hung,
Emily Dengc6e14f42016-08-08 11:30:50 +0800695 .backlight_set_level = NULL,
696 .backlight_get_level = NULL,
Emily Deng8e6de752016-08-08 11:31:13 +0800697 .hpd_sense = &dce_virtual_hpd_sense,
698 .hpd_set_polarity = &dce_virtual_hpd_set_polarity,
699 .hpd_get_gpio_reg = &dce_virtual_hpd_get_gpio_reg,
700 .page_flip = &dce_virtual_page_flip,
701 .page_flip_get_scanoutpos = &dce_virtual_crtc_get_scanoutpos,
Alex Deucher66264ba2016-09-30 12:37:36 -0400702 .add_encoder = NULL,
703 .add_connector = NULL,
Emily Deng8e6de752016-08-08 11:31:13 +0800704 .stop_mc_access = &dce_virtual_stop_mc_access,
705 .resume_mc_access = &dce_virtual_resume_mc_access,
Emily Dengc6e14f42016-08-08 11:30:50 +0800706};
707
708static void dce_virtual_set_display_funcs(struct amdgpu_device *adev)
709{
710 if (adev->mode_info.funcs == NULL)
711 adev->mode_info.funcs = &dce_virtual_display_funcs;
712}
713
Alex Deucher9405e472016-09-30 11:41:37 -0400714static int dce_virtual_pageflip(struct amdgpu_device *adev,
715 unsigned crtc_id)
716{
717 unsigned long flags;
718 struct amdgpu_crtc *amdgpu_crtc;
719 struct amdgpu_flip_work *works;
720
721 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
722
723 if (crtc_id >= adev->mode_info.num_crtc) {
724 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
725 return -EINVAL;
726 }
727
728 /* IRQ could occur when in initial stage */
729 if (amdgpu_crtc == NULL)
730 return 0;
731
732 spin_lock_irqsave(&adev->ddev->event_lock, flags);
733 works = amdgpu_crtc->pflip_works;
734 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
735 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
736 "AMDGPU_FLIP_SUBMITTED(%d)\n",
737 amdgpu_crtc->pflip_status,
738 AMDGPU_FLIP_SUBMITTED);
739 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
740 return 0;
741 }
742
743 /* page flip completed. clean up */
744 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
745 amdgpu_crtc->pflip_works = NULL;
746
747 /* wakeup usersapce */
748 if (works->event)
749 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
750
751 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
752
753 drm_crtc_vblank_put(&amdgpu_crtc->base);
754 schedule_work(&works->unpin_work);
755
756 return 0;
757}
758
Emily Deng46ac3622016-08-08 11:35:39 +0800759static enum hrtimer_restart dce_virtual_vblank_timer_handle(struct hrtimer *vblank_timer)
760{
Emily Deng0f663562016-09-30 13:02:18 -0400761 struct amdgpu_crtc *amdgpu_crtc = container_of(vblank_timer,
762 struct amdgpu_crtc, vblank_timer);
763 struct drm_device *ddev = amdgpu_crtc->base.dev;
764 struct amdgpu_device *adev = ddev->dev_private;
Alex Deucher9405e472016-09-30 11:41:37 -0400765
Emily Deng0f663562016-09-30 13:02:18 -0400766 drm_handle_vblank(ddev, amdgpu_crtc->crtc_id);
767 dce_virtual_pageflip(adev, amdgpu_crtc->crtc_id);
Alex Deucher9405e472016-09-30 11:41:37 -0400768 hrtimer_start(vblank_timer, ktime_set(0, DCE_VIRTUAL_VBLANK_PERIOD),
769 HRTIMER_MODE_REL);
770
Emily Deng46ac3622016-08-08 11:35:39 +0800771 return HRTIMER_NORESTART;
772}
773
Emily Denge13273d2016-08-08 11:31:37 +0800774static void dce_virtual_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
Alex Deucher82b9f812016-09-30 11:19:41 -0400775 int crtc,
776 enum amdgpu_interrupt_state state)
Emily Denge13273d2016-08-08 11:31:37 +0800777{
778 if (crtc >= adev->mode_info.num_crtc) {
779 DRM_DEBUG("invalid crtc %d\n", crtc);
780 return;
781 }
Emily Deng46ac3622016-08-08 11:35:39 +0800782
Emily Deng0f663562016-09-30 13:02:18 -0400783 if (state && !adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
Emily Deng46ac3622016-08-08 11:35:39 +0800784 DRM_DEBUG("Enable software vsync timer\n");
Emily Deng0f663562016-09-30 13:02:18 -0400785 hrtimer_init(&adev->mode_info.crtcs[crtc]->vblank_timer,
786 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
787 hrtimer_set_expires(&adev->mode_info.crtcs[crtc]->vblank_timer,
788 ktime_set(0, DCE_VIRTUAL_VBLANK_PERIOD));
789 adev->mode_info.crtcs[crtc]->vblank_timer.function =
790 dce_virtual_vblank_timer_handle;
791 hrtimer_start(&adev->mode_info.crtcs[crtc]->vblank_timer,
792 ktime_set(0, DCE_VIRTUAL_VBLANK_PERIOD), HRTIMER_MODE_REL);
793 } else if (!state && adev->mode_info.crtcs[crtc]->vsync_timer_enabled) {
Emily Deng46ac3622016-08-08 11:35:39 +0800794 DRM_DEBUG("Disable software vsync timer\n");
Emily Deng0f663562016-09-30 13:02:18 -0400795 hrtimer_cancel(&adev->mode_info.crtcs[crtc]->vblank_timer);
Emily Deng46ac3622016-08-08 11:35:39 +0800796 }
797
Emily Deng0f663562016-09-30 13:02:18 -0400798 adev->mode_info.crtcs[crtc]->vsync_timer_enabled = state;
Emily Deng46ac3622016-08-08 11:35:39 +0800799 DRM_DEBUG("[FM]set crtc %d vblank interrupt state %d\n", crtc, state);
Emily Denge13273d2016-08-08 11:31:37 +0800800}
801
Emily Deng46ac3622016-08-08 11:35:39 +0800802
Emily Denge13273d2016-08-08 11:31:37 +0800803static int dce_virtual_set_crtc_irq_state(struct amdgpu_device *adev,
Alex Deucher82b9f812016-09-30 11:19:41 -0400804 struct amdgpu_irq_src *source,
805 unsigned type,
806 enum amdgpu_interrupt_state state)
Emily Denge13273d2016-08-08 11:31:37 +0800807{
Emily Deng0f663562016-09-30 13:02:18 -0400808 if (type > AMDGPU_CRTC_IRQ_VBLANK6)
809 return -EINVAL;
810
811 dce_virtual_set_crtc_vblank_interrupt_state(adev, type, state);
812
Emily Denge13273d2016-08-08 11:31:37 +0800813 return 0;
814}
815
Emily Dengc6e14f42016-08-08 11:30:50 +0800816static const struct amdgpu_irq_src_funcs dce_virtual_crtc_irq_funcs = {
Emily Denge13273d2016-08-08 11:31:37 +0800817 .set = dce_virtual_set_crtc_irq_state,
Alex Deucherbf2335a2016-09-30 11:23:30 -0400818 .process = NULL,
Emily Dengc6e14f42016-08-08 11:30:50 +0800819};
820
Emily Dengc6e14f42016-08-08 11:30:50 +0800821static void dce_virtual_set_irq_funcs(struct amdgpu_device *adev)
822{
823 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
824 adev->crtc_irq.funcs = &dce_virtual_crtc_irq_funcs;
Emily Dengc6e14f42016-08-08 11:30:50 +0800825}
826