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