bochs_kms.c 4.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
G
Gerd Hoffmann 已提交
2 3 4
/*
 */

S
Sam Ravnborg 已提交
5 6
#include <linux/moduleparam.h>

7
#include <drm/drm_atomic_helper.h>
8
#include <drm/drm_gem_framebuffer_helper.h>
9
#include <drm/drm_probe_helper.h>
S
Sam Ravnborg 已提交
10 11

#include "bochs.h"
G
Gerd Hoffmann 已提交
12 13 14 15 16 17 18 19 20 21 22

static int defx = 1024;
static int defy = 768;

module_param(defx, int, 0444);
module_param(defy, int, 0444);
MODULE_PARM_DESC(defx, "default x resolution");
MODULE_PARM_DESC(defy, "default y resolution");

/* ---------------------------------------------------------------------- */

23 24 25 26 27 28 29
static const uint32_t bochs_formats[] = {
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_BGRX8888,
};

static void bochs_plane_update(struct bochs_device *bochs,
			       struct drm_plane_state *state)
30
{
31
	struct drm_gem_vram_object *gbo;
32

33 34 35
	if (!state->fb || !bochs->stride)
		return;

36
	gbo = drm_gem_vram_of_gem(state->fb->obj[0]);
37 38 39
	bochs_hw_setbase(bochs,
			 state->crtc_x,
			 state->crtc_y,
40 41
			 state->fb->pitches[0],
			 state->fb->offsets[0] + gbo->bo.offset);
42
	bochs_hw_setformat(bochs, state->fb->format);
43 44
}

45 46 47
static void bochs_pipe_enable(struct drm_simple_display_pipe *pipe,
			      struct drm_crtc_state *crtc_state,
			      struct drm_plane_state *plane_state)
48
{
49 50 51 52
	struct bochs_device *bochs = pipe->crtc.dev->dev_private;

	bochs_hw_setmode(bochs, &crtc_state->mode);
	bochs_plane_update(bochs, plane_state);
53 54
}

55 56
static void bochs_pipe_update(struct drm_simple_display_pipe *pipe,
			      struct drm_plane_state *old_state)
57
{
58
	struct bochs_device *bochs = pipe->crtc.dev->dev_private;
59

60
	bochs_plane_update(bochs, pipe->plane.state);
61 62
}

63 64 65
static const struct drm_simple_display_pipe_funcs bochs_pipe_funcs = {
	.enable	    = bochs_pipe_enable,
	.update	    = bochs_pipe_update,
66 67
	.prepare_fb = drm_gem_vram_simple_display_pipe_prepare_fb,
	.cleanup_fb = drm_gem_vram_simple_display_pipe_cleanup_fb,
G
Gerd Hoffmann 已提交
68 69
};

70
static int bochs_connector_get_modes(struct drm_connector *connector)
G
Gerd Hoffmann 已提交
71
{
G
Gerd Hoffmann 已提交
72 73 74 75 76 77
	struct bochs_device *bochs =
		container_of(connector, struct bochs_device, connector);
	int count = 0;

	if (bochs->edid)
		count = drm_add_edid_modes(connector, bochs->edid);
G
Gerd Hoffmann 已提交
78

G
Gerd Hoffmann 已提交
79 80 81 82
	if (!count) {
		count = drm_add_modes_noedid(connector, 8192, 8192);
		drm_set_preferred_mode(connector, defx, defy);
	}
G
Gerd Hoffmann 已提交
83 84 85
	return count;
}

86
static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
G
Gerd Hoffmann 已提交
87 88 89
	.get_modes = bochs_connector_get_modes,
};

90
static const struct drm_connector_funcs bochs_connector_connector_funcs = {
G
Gerd Hoffmann 已提交
91 92
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = drm_connector_cleanup,
93 94 95
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
G
Gerd Hoffmann 已提交
96 97 98 99 100 101 102 103 104 105 106
};

static void bochs_connector_init(struct drm_device *dev)
{
	struct bochs_device *bochs = dev->dev_private;
	struct drm_connector *connector = &bochs->connector;

	drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
			   DRM_MODE_CONNECTOR_VIRTUAL);
	drm_connector_helper_add(connector,
				 &bochs_connector_connector_helper_funcs);
107
	drm_connector_register(connector);
G
Gerd Hoffmann 已提交
108 109 110 111 112 113 114

	bochs_hw_load_edid(bochs);
	if (bochs->edid) {
		DRM_INFO("Found EDID data blob.\n");
		drm_connector_attach_edid_property(connector);
		drm_connector_update_edid_property(connector, bochs->edid);
	}
G
Gerd Hoffmann 已提交
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129
static struct drm_framebuffer *
bochs_gem_fb_create(struct drm_device *dev, struct drm_file *file,
		    const struct drm_mode_fb_cmd2 *mode_cmd)
{
	if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888 &&
	    mode_cmd->pixel_format != DRM_FORMAT_BGRX8888)
		return ERR_PTR(-EINVAL);

	return drm_gem_fb_create(dev, file, mode_cmd);
}

const struct drm_mode_config_funcs bochs_mode_funcs = {
	.fb_create = bochs_gem_fb_create,
130
	.mode_valid = drm_vram_helper_mode_valid,
131 132 133
	.atomic_check = drm_atomic_helper_check,
	.atomic_commit = drm_atomic_helper_commit,
};
G
Gerd Hoffmann 已提交
134 135 136 137 138 139 140 141 142 143 144

int bochs_kms_init(struct bochs_device *bochs)
{
	drm_mode_config_init(bochs->dev);

	bochs->dev->mode_config.max_width = 8192;
	bochs->dev->mode_config.max_height = 8192;

	bochs->dev->mode_config.fb_base = bochs->fb_base;
	bochs->dev->mode_config.preferred_depth = 24;
	bochs->dev->mode_config.prefer_shadow = 0;
145
	bochs->dev->mode_config.prefer_shadow_fbdev = 1;
146
	bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
G
Gerd Hoffmann 已提交
147

148
	bochs->dev->mode_config.funcs = &bochs_mode_funcs;
G
Gerd Hoffmann 已提交
149 150

	bochs_connector_init(bochs->dev);
151 152 153 154 155 156 157
	drm_simple_display_pipe_init(bochs->dev,
				     &bochs->pipe,
				     &bochs_pipe_funcs,
				     bochs_formats,
				     ARRAY_SIZE(bochs_formats),
				     NULL,
				     &bochs->connector);
G
Gerd Hoffmann 已提交
158

159 160
	drm_mode_config_reset(bochs->dev);

G
Gerd Hoffmann 已提交
161 162 163 164 165
	return 0;
}

void bochs_kms_fini(struct bochs_device *bochs)
{
166 167
	drm_atomic_helper_shutdown(bochs->dev);
	drm_mode_config_cleanup(bochs->dev);
G
Gerd Hoffmann 已提交
168
}