]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/vmwgfx/vmwgfx_ldu.c
drm/radeon: allow pcie gen2 speed on Cayman
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / vmwgfx / vmwgfx_ldu.c
CommitLineData
fb1d9738
JB
1/**************************************************************************
2 *
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "vmwgfx_kms.h"
29
7a1c2f6c 30
fb1d9738
JB
31#define vmw_crtc_to_ldu(x) \
32 container_of(x, struct vmw_legacy_display_unit, base.crtc)
33#define vmw_encoder_to_ldu(x) \
34 container_of(x, struct vmw_legacy_display_unit, base.encoder)
35#define vmw_connector_to_ldu(x) \
36 container_of(x, struct vmw_legacy_display_unit, base.connector)
37
38struct vmw_legacy_display {
39 struct list_head active;
40
41 unsigned num_active;
d7e1958d 42 unsigned last_num_active;
fb1d9738
JB
43
44 struct vmw_framebuffer *fb;
45};
46
47/**
48 * Display unit using the legacy register interface.
49 */
50struct vmw_legacy_display_unit {
51 struct vmw_display_unit base;
52
53 struct list_head active;
fb1d9738
JB
54};
55
56static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
57{
58 list_del_init(&ldu->active);
59 vmw_display_unit_cleanup(&ldu->base);
60 kfree(ldu);
61}
62
63
64/*
65 * Legacy Display Unit CRTC functions
66 */
67
fb1d9738
JB
68static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
69{
70 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
71}
72
73static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
74{
75 struct vmw_legacy_display *lds = dev_priv->ldu_priv;
76 struct vmw_legacy_display_unit *entry;
d7e1958d
JB
77 struct drm_framebuffer *fb = NULL;
78 struct drm_crtc *crtc = NULL;
fb1d9738
JB
79 int i = 0;
80
d7e1958d
JB
81 /* If there is no display topology the host just assumes
82 * that the guest will set the same layout as the host.
83 */
84 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
85 int w = 0, h = 0;
86 list_for_each_entry(entry, &lds->active, active) {
87 crtc = &entry->base.crtc;
88 w = max(w, crtc->x + crtc->mode.hdisplay);
89 h = max(h, crtc->y + crtc->mode.vdisplay);
90 i++;
91 }
92
93 if (crtc == NULL)
94 return 0;
95 fb = entry->base.crtc.fb;
96
0bef23f9
MD
97 return vmw_kms_write_svga(dev_priv, w, h, fb->pitch,
98 fb->bits_per_pixel, fb->depth);
d7e1958d
JB
99 }
100
d7e1958d
JB
101 if (!list_empty(&lds->active)) {
102 entry = list_entry(lds->active.next, typeof(*entry), active);
103 fb = entry->base.crtc.fb;
104
105 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitch,
106 fb->bits_per_pixel, fb->depth);
107 }
108
259600d5
JB
109 /* Make sure we always show something. */
110 vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
111 lds->num_active ? lds->num_active : 1);
112
fb1d9738
JB
113 i = 0;
114 list_for_each_entry(entry, &lds->active, active) {
115 crtc = &entry->base.crtc;
116
117 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
118 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
119 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
120 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
121 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
122 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
123 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
124
125 i++;
126 }
127
d7e1958d
JB
128 BUG_ON(i != lds->num_active);
129
130 lds->last_num_active = lds->num_active;
131
fb1d9738
JB
132 return 0;
133}
134
135static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
136 struct vmw_legacy_display_unit *ldu)
137{
138 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
139 if (list_empty(&ldu->active))
140 return 0;
141
6a591a96 142 /* Must init otherwise list_empty(&ldu->active) will not work. */
fb1d9738
JB
143 list_del_init(&ldu->active);
144 if (--(ld->num_active) == 0) {
145 BUG_ON(!ld->fb);
146 if (ld->fb->unpin)
147 ld->fb->unpin(ld->fb);
148 ld->fb = NULL;
149 }
150
151 return 0;
152}
153
154static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
155 struct vmw_legacy_display_unit *ldu,
156 struct vmw_framebuffer *vfb)
157{
158 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
159 struct vmw_legacy_display_unit *entry;
160 struct list_head *at;
161
04e9e94d
JB
162 BUG_ON(!ld->num_active && ld->fb);
163 if (vfb != ld->fb) {
164 if (ld->fb && ld->fb->unpin)
165 ld->fb->unpin(ld->fb);
166 if (vfb->pin)
167 vfb->pin(vfb);
168 ld->fb = vfb;
169 }
170
fb1d9738
JB
171 if (!list_empty(&ldu->active))
172 return 0;
173
174 at = &ld->active;
175 list_for_each_entry(entry, &ld->active, active) {
bbfad336 176 if (entry->base.unit > ldu->base.unit)
fb1d9738
JB
177 break;
178
179 at = &entry->active;
180 }
181
182 list_add(&ldu->active, at);
04e9e94d
JB
183
184 ld->num_active++;
fb1d9738
JB
185
186 return 0;
187}
188
189static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
190{
191 struct vmw_private *dev_priv;
192 struct vmw_legacy_display_unit *ldu;
193 struct drm_connector *connector;
194 struct drm_display_mode *mode;
195 struct drm_encoder *encoder;
196 struct vmw_framebuffer *vfb;
197 struct drm_framebuffer *fb;
198 struct drm_crtc *crtc;
199
200 if (!set)
201 return -EINVAL;
202
203 if (!set->crtc)
204 return -EINVAL;
205
206 /* get the ldu */
207 crtc = set->crtc;
208 ldu = vmw_crtc_to_ldu(crtc);
209 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
210 dev_priv = vmw_priv(crtc->dev);
211
212 if (set->num_connectors > 1) {
213 DRM_ERROR("to many connectors\n");
214 return -EINVAL;
215 }
216
217 if (set->num_connectors == 1 &&
218 set->connectors[0] != &ldu->base.connector) {
219 DRM_ERROR("connector doesn't match %p %p\n",
220 set->connectors[0], &ldu->base.connector);
221 return -EINVAL;
222 }
223
224 /* ldu only supports one fb active at the time */
225 if (dev_priv->ldu_priv->fb && vfb &&
6a591a96
JB
226 !(dev_priv->ldu_priv->num_active == 1 &&
227 !list_empty(&ldu->active)) &&
fb1d9738
JB
228 dev_priv->ldu_priv->fb != vfb) {
229 DRM_ERROR("Multiple framebuffers not supported\n");
230 return -EINVAL;
231 }
232
233 /* since they always map one to one these are safe */
234 connector = &ldu->base.connector;
235 encoder = &ldu->base.encoder;
236
237 /* should we turn the crtc off? */
238 if (set->num_connectors == 0 || !set->mode || !set->fb) {
239
240 connector->encoder = NULL;
241 encoder->crtc = NULL;
242 crtc->fb = NULL;
243
244 vmw_ldu_del_active(dev_priv, ldu);
245
0bef23f9 246 return vmw_ldu_commit_list(dev_priv);
fb1d9738
JB
247 }
248
249
250 /* we now know we want to set a mode */
251 mode = set->mode;
252 fb = set->fb;
253
254 if (set->x + mode->hdisplay > fb->width ||
255 set->y + mode->vdisplay > fb->height) {
256 DRM_ERROR("set outside of framebuffer\n");
257 return -EINVAL;
258 }
259
260 vmw_fb_off(dev_priv);
261
262 crtc->fb = fb;
263 encoder->crtc = crtc;
264 connector->encoder = encoder;
265 crtc->x = set->x;
266 crtc->y = set->y;
267 crtc->mode = *mode;
268
269 vmw_ldu_add_active(dev_priv, ldu, vfb);
270
0bef23f9 271 return vmw_ldu_commit_list(dev_priv);
fb1d9738
JB
272}
273
274static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
626ab771
JB
275 .save = vmw_du_crtc_save,
276 .restore = vmw_du_crtc_restore,
fb1d9738
JB
277 .cursor_set = vmw_du_crtc_cursor_set,
278 .cursor_move = vmw_du_crtc_cursor_move,
626ab771 279 .gamma_set = vmw_du_crtc_gamma_set,
fb1d9738
JB
280 .destroy = vmw_ldu_crtc_destroy,
281 .set_config = vmw_ldu_crtc_set_config,
282};
283
626ab771 284
fb1d9738
JB
285/*
286 * Legacy Display Unit encoder functions
287 */
288
289static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
290{
291 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
292}
293
294static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
295 .destroy = vmw_ldu_encoder_destroy,
296};
297
298/*
299 * Legacy Display Unit connector functions
300 */
301
fb1d9738
JB
302static void vmw_ldu_connector_destroy(struct drm_connector *connector)
303{
304 vmw_ldu_destroy(vmw_connector_to_ldu(connector));
305}
306
307static struct drm_connector_funcs vmw_legacy_connector_funcs = {
626ab771
JB
308 .dpms = vmw_du_connector_dpms,
309 .save = vmw_du_connector_save,
310 .restore = vmw_du_connector_restore,
311 .detect = vmw_du_connector_detect,
312 .fill_modes = vmw_du_connector_fill_modes,
313 .set_property = vmw_du_connector_set_property,
fb1d9738
JB
314 .destroy = vmw_ldu_connector_destroy,
315};
316
317static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
318{
319 struct vmw_legacy_display_unit *ldu;
320 struct drm_device *dev = dev_priv->dev;
321 struct drm_connector *connector;
322 struct drm_encoder *encoder;
323 struct drm_crtc *crtc;
324
325 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
326 if (!ldu)
327 return -ENOMEM;
328
bbfad336 329 ldu->base.unit = unit;
fb1d9738
JB
330 crtc = &ldu->base.crtc;
331 encoder = &ldu->base.encoder;
332 connector = &ldu->base.connector;
333
1ae1ddd5
JB
334 INIT_LIST_HEAD(&ldu->active);
335
626ab771
JB
336 ldu->base.pref_active = (unit == 0);
337 ldu->base.pref_width = 800;
338 ldu->base.pref_height = 600;
339 ldu->base.pref_mode = NULL;
d8bd19d2 340
fb1d9738
JB
341 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
342 DRM_MODE_CONNECTOR_LVDS);
626ab771 343 connector->status = vmw_du_connector_detect(connector, true);
fb1d9738
JB
344
345 drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
346 DRM_MODE_ENCODER_LVDS);
347 drm_mode_connector_attach_encoder(connector, encoder);
348 encoder->possible_crtcs = (1 << unit);
349 encoder->possible_clones = 0;
350
fb1d9738
JB
351 drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
352
f01b7ba0
MD
353 drm_mode_crtc_set_gamma_size(crtc, 256);
354
fb1d9738
JB
355 drm_connector_attach_property(connector,
356 dev->mode_config.dirty_info_property,
357 1);
358
359 return 0;
360}
361
362int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
363{
7a1c2f6c
TH
364 struct drm_device *dev = dev_priv->dev;
365 int i;
366 int ret;
367
fb1d9738
JB
368 if (dev_priv->ldu_priv) {
369 DRM_INFO("ldu system already on\n");
370 return -EINVAL;
371 }
372
85b54e0c 373 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
fb1d9738
JB
374
375 if (!dev_priv->ldu_priv)
376 return -ENOMEM;
377
378 INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
379 dev_priv->ldu_priv->num_active = 0;
d7e1958d 380 dev_priv->ldu_priv->last_num_active = 0;
fb1d9738
JB
381 dev_priv->ldu_priv->fb = NULL;
382
383 drm_mode_create_dirty_info_property(dev_priv->dev);
384
d7e1958d 385 if (dev_priv->capabilities & SVGA_CAP_MULTIMON) {
56d1c78d 386 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
7a1c2f6c 387 vmw_ldu_init(dev_priv, i);
56d1c78d 388 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
7a1c2f6c
TH
389 } else {
390 /* for old hardware without multimon only enable one display */
391 vmw_ldu_init(dev_priv, 0);
392 ret = drm_vblank_init(dev, 1);
d7e1958d 393 }
fb1d9738 394
7a1c2f6c 395 return ret;
fb1d9738
JB
396}
397
398int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
399{
7a1c2f6c
TH
400 struct drm_device *dev = dev_priv->dev;
401
402 drm_vblank_cleanup(dev);
fb1d9738
JB
403 if (!dev_priv->ldu_priv)
404 return -ENOSYS;
405
406 BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
407
408 kfree(dev_priv->ldu_priv);
409
410 return 0;
411}