Merge tag 'pci-v5.3-fixes-1' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci
[sfrench/cifs-2.6.git] / drivers / gpu / drm / bochs / bochs_kms.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4
5 #include "bochs.h"
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_plane_helper.h>
8 #include <drm/drm_atomic_uapi.h>
9 #include <drm/drm_gem_framebuffer_helper.h>
10 #include <drm/drm_probe_helper.h>
11
12 static int defx = 1024;
13 static int defy = 768;
14
15 module_param(defx, int, 0444);
16 module_param(defy, int, 0444);
17 MODULE_PARM_DESC(defx, "default x resolution");
18 MODULE_PARM_DESC(defy, "default y resolution");
19
20 /* ---------------------------------------------------------------------- */
21
22 static const uint32_t bochs_formats[] = {
23         DRM_FORMAT_XRGB8888,
24         DRM_FORMAT_BGRX8888,
25 };
26
27 static void bochs_plane_update(struct bochs_device *bochs,
28                                struct drm_plane_state *state)
29 {
30         struct drm_gem_vram_object *gbo;
31
32         if (!state->fb || !bochs->stride)
33                 return;
34
35         gbo = drm_gem_vram_of_gem(state->fb->obj[0]);
36         bochs_hw_setbase(bochs,
37                          state->crtc_x,
38                          state->crtc_y,
39                          state->fb->pitches[0],
40                          state->fb->offsets[0] + gbo->bo.offset);
41         bochs_hw_setformat(bochs, state->fb->format);
42 }
43
44 static void bochs_pipe_enable(struct drm_simple_display_pipe *pipe,
45                               struct drm_crtc_state *crtc_state,
46                               struct drm_plane_state *plane_state)
47 {
48         struct bochs_device *bochs = pipe->crtc.dev->dev_private;
49
50         bochs_hw_setmode(bochs, &crtc_state->mode);
51         bochs_plane_update(bochs, plane_state);
52 }
53
54 static void bochs_pipe_update(struct drm_simple_display_pipe *pipe,
55                               struct drm_plane_state *old_state)
56 {
57         struct bochs_device *bochs = pipe->crtc.dev->dev_private;
58         struct drm_crtc *crtc = &pipe->crtc;
59
60         bochs_plane_update(bochs, pipe->plane.state);
61
62         if (crtc->state->event) {
63                 spin_lock_irq(&crtc->dev->event_lock);
64                 drm_crtc_send_vblank_event(crtc, crtc->state->event);
65                 crtc->state->event = NULL;
66                 spin_unlock_irq(&crtc->dev->event_lock);
67         }
68 }
69
70 static int bochs_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
71                                  struct drm_plane_state *new_state)
72 {
73         struct drm_gem_vram_object *gbo;
74
75         if (!new_state->fb)
76                 return 0;
77         gbo = drm_gem_vram_of_gem(new_state->fb->obj[0]);
78         return drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
79 }
80
81 static void bochs_pipe_cleanup_fb(struct drm_simple_display_pipe *pipe,
82                                   struct drm_plane_state *old_state)
83 {
84         struct drm_gem_vram_object *gbo;
85
86         if (!old_state->fb)
87                 return;
88         gbo = drm_gem_vram_of_gem(old_state->fb->obj[0]);
89         drm_gem_vram_unpin(gbo);
90 }
91
92 static const struct drm_simple_display_pipe_funcs bochs_pipe_funcs = {
93         .enable     = bochs_pipe_enable,
94         .update     = bochs_pipe_update,
95         .prepare_fb = bochs_pipe_prepare_fb,
96         .cleanup_fb = bochs_pipe_cleanup_fb,
97 };
98
99 static int bochs_connector_get_modes(struct drm_connector *connector)
100 {
101         struct bochs_device *bochs =
102                 container_of(connector, struct bochs_device, connector);
103         int count = 0;
104
105         if (bochs->edid)
106                 count = drm_add_edid_modes(connector, bochs->edid);
107
108         if (!count) {
109                 count = drm_add_modes_noedid(connector, 8192, 8192);
110                 drm_set_preferred_mode(connector, defx, defy);
111         }
112         return count;
113 }
114
115 static enum drm_mode_status bochs_connector_mode_valid(struct drm_connector *connector,
116                                       struct drm_display_mode *mode)
117 {
118         struct bochs_device *bochs =
119                 container_of(connector, struct bochs_device, connector);
120         unsigned long size = mode->hdisplay * mode->vdisplay * 4;
121
122         /*
123          * Make sure we can fit two framebuffers into video memory.
124          * This allows up to 1600x1200 with 16 MB (default size).
125          * If you want more try this:
126          *     'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
127          */
128         if (size * 2 > bochs->fb_size)
129                 return MODE_BAD;
130
131         return MODE_OK;
132 }
133
134 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
135         .get_modes = bochs_connector_get_modes,
136         .mode_valid = bochs_connector_mode_valid,
137 };
138
139 static const struct drm_connector_funcs bochs_connector_connector_funcs = {
140         .dpms = drm_helper_connector_dpms,
141         .fill_modes = drm_helper_probe_single_connector_modes,
142         .destroy = drm_connector_cleanup,
143         .reset = drm_atomic_helper_connector_reset,
144         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
145         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
146 };
147
148 static void bochs_connector_init(struct drm_device *dev)
149 {
150         struct bochs_device *bochs = dev->dev_private;
151         struct drm_connector *connector = &bochs->connector;
152
153         drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
154                            DRM_MODE_CONNECTOR_VIRTUAL);
155         drm_connector_helper_add(connector,
156                                  &bochs_connector_connector_helper_funcs);
157         drm_connector_register(connector);
158
159         bochs_hw_load_edid(bochs);
160         if (bochs->edid) {
161                 DRM_INFO("Found EDID data blob.\n");
162                 drm_connector_attach_edid_property(connector);
163                 drm_connector_update_edid_property(connector, bochs->edid);
164         }
165 }
166
167 static struct drm_framebuffer *
168 bochs_gem_fb_create(struct drm_device *dev, struct drm_file *file,
169                     const struct drm_mode_fb_cmd2 *mode_cmd)
170 {
171         if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888 &&
172             mode_cmd->pixel_format != DRM_FORMAT_BGRX8888)
173                 return ERR_PTR(-EINVAL);
174
175         return drm_gem_fb_create(dev, file, mode_cmd);
176 }
177
178 const struct drm_mode_config_funcs bochs_mode_funcs = {
179         .fb_create = bochs_gem_fb_create,
180         .atomic_check = drm_atomic_helper_check,
181         .atomic_commit = drm_atomic_helper_commit,
182 };
183
184 int bochs_kms_init(struct bochs_device *bochs)
185 {
186         drm_mode_config_init(bochs->dev);
187
188         bochs->dev->mode_config.max_width = 8192;
189         bochs->dev->mode_config.max_height = 8192;
190
191         bochs->dev->mode_config.fb_base = bochs->fb_base;
192         bochs->dev->mode_config.preferred_depth = 24;
193         bochs->dev->mode_config.prefer_shadow = 0;
194         bochs->dev->mode_config.prefer_shadow_fbdev = 1;
195         bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
196
197         bochs->dev->mode_config.funcs = &bochs_mode_funcs;
198
199         bochs_connector_init(bochs->dev);
200         drm_simple_display_pipe_init(bochs->dev,
201                                      &bochs->pipe,
202                                      &bochs_pipe_funcs,
203                                      bochs_formats,
204                                      ARRAY_SIZE(bochs_formats),
205                                      NULL,
206                                      &bochs->connector);
207
208         drm_mode_config_reset(bochs->dev);
209
210         return 0;
211 }
212
213 void bochs_kms_fini(struct bochs_device *bochs)
214 {
215         drm_atomic_helper_shutdown(bochs->dev);
216         drm_mode_config_cleanup(bochs->dev);
217 }