Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Red Hat 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 | * Authors: Dave Airlie |
| 23 | * Alon Levy |
| 24 | */ |
| 25 | |
Randy Dunlap | c5416d66 | 2013-12-20 10:58:15 -0800 | [diff] [blame] | 26 | #include <linux/crc32.h> |
Masahiro Yamada | edaf492 | 2017-04-24 13:50:30 +0900 | [diff] [blame] | 27 | #include <drm/drm_crtc_helper.h> |
Daniel Vetter | 3cb9ae4 | 2014-10-29 10:03:57 +0100 | [diff] [blame] | 28 | #include <drm/drm_plane_helper.h> |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 29 | #include <drm/drm_atomic_helper.h> |
Gabriel Krisman Bertazi | 10a0bd8 | 2017-02-27 17:43:24 -0300 | [diff] [blame] | 30 | #include <drm/drm_atomic.h> |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 31 | |
Masahiro Yamada | edaf492 | 2017-04-24 13:50:30 +0900 | [diff] [blame] | 32 | #include "qxl_drv.h" |
| 33 | #include "qxl_object.h" |
| 34 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 35 | static bool qxl_head_enabled(struct qxl_head *head) |
| 36 | { |
| 37 | return head->width && head->height; |
| 38 | } |
| 39 | |
Christophe Fergeau | e4a7644 | 2016-11-08 10:12:03 +0100 | [diff] [blame] | 40 | static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 41 | { |
| 42 | if (qdev->client_monitors_config && |
| 43 | count > qdev->client_monitors_config->count) { |
| 44 | kfree(qdev->client_monitors_config); |
Dave Airlie | 62c8ba7 | 2013-04-16 13:36:00 +1000 | [diff] [blame] | 45 | qdev->client_monitors_config = NULL; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 46 | } |
| 47 | if (!qdev->client_monitors_config) { |
| 48 | qdev->client_monitors_config = kzalloc( |
| 49 | sizeof(struct qxl_monitors_config) + |
| 50 | sizeof(struct qxl_head) * count, GFP_KERNEL); |
| 51 | if (!qdev->client_monitors_config) { |
| 52 | qxl_io_log(qdev, |
| 53 | "%s: allocation failure for %u heads\n", |
| 54 | __func__, count); |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | qdev->client_monitors_config->count = count; |
| 59 | } |
| 60 | |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 61 | enum { |
| 62 | MONITORS_CONFIG_MODIFIED, |
| 63 | MONITORS_CONFIG_UNCHANGED, |
| 64 | MONITORS_CONFIG_BAD_CRC, |
| 65 | }; |
| 66 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 67 | static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev) |
| 68 | { |
| 69 | int i; |
| 70 | int num_monitors; |
| 71 | uint32_t crc; |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 72 | int status = MONITORS_CONFIG_UNCHANGED; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 73 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 74 | num_monitors = qdev->rom->client_monitors_config.count; |
| 75 | crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config, |
| 76 | sizeof(qdev->rom->client_monitors_config)); |
| 77 | if (crc != qdev->rom->client_monitors_config_crc) { |
Frediano Ziglio | 72ec565 | 2015-06-03 12:09:16 +0100 | [diff] [blame] | 78 | qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 79 | sizeof(qdev->rom->client_monitors_config), |
| 80 | qdev->rom->client_monitors_config_crc); |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 81 | return MONITORS_CONFIG_BAD_CRC; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 82 | } |
Gerd Hoffmann | c50fad8 | 2017-03-01 11:12:33 +0100 | [diff] [blame] | 83 | if (!num_monitors) { |
| 84 | DRM_DEBUG_KMS("no client monitors configured\n"); |
| 85 | return status; |
| 86 | } |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 87 | if (num_monitors > qdev->monitors_config->max_allowed) { |
Dave Airlie | 5b8788c | 2013-07-01 14:14:38 +1000 | [diff] [blame] | 88 | DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n", |
| 89 | qdev->monitors_config->max_allowed, num_monitors); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 90 | num_monitors = qdev->monitors_config->max_allowed; |
| 91 | } else { |
| 92 | num_monitors = qdev->rom->client_monitors_config.count; |
| 93 | } |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 94 | if (qdev->client_monitors_config |
| 95 | && (num_monitors != qdev->client_monitors_config->count)) { |
| 96 | status = MONITORS_CONFIG_MODIFIED; |
| 97 | } |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 98 | qxl_alloc_client_monitors_config(qdev, num_monitors); |
| 99 | /* we copy max from the client but it isn't used */ |
| 100 | qdev->client_monitors_config->max_allowed = |
| 101 | qdev->monitors_config->max_allowed; |
| 102 | for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) { |
| 103 | struct qxl_urect *c_rect = |
| 104 | &qdev->rom->client_monitors_config.heads[i]; |
| 105 | struct qxl_head *client_head = |
| 106 | &qdev->client_monitors_config->heads[i]; |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 107 | if (client_head->x != c_rect->left) { |
| 108 | client_head->x = c_rect->left; |
| 109 | status = MONITORS_CONFIG_MODIFIED; |
| 110 | } |
| 111 | if (client_head->y != c_rect->top) { |
| 112 | client_head->y = c_rect->top; |
| 113 | status = MONITORS_CONFIG_MODIFIED; |
| 114 | } |
| 115 | if (client_head->width != c_rect->right - c_rect->left) { |
| 116 | client_head->width = c_rect->right - c_rect->left; |
| 117 | status = MONITORS_CONFIG_MODIFIED; |
| 118 | } |
| 119 | if (client_head->height != c_rect->bottom - c_rect->top) { |
| 120 | client_head->height = c_rect->bottom - c_rect->top; |
| 121 | status = MONITORS_CONFIG_MODIFIED; |
| 122 | } |
| 123 | if (client_head->surface_id != 0) { |
| 124 | client_head->surface_id = 0; |
| 125 | status = MONITORS_CONFIG_MODIFIED; |
| 126 | } |
| 127 | if (client_head->id != i) { |
| 128 | client_head->id = i; |
| 129 | status = MONITORS_CONFIG_MODIFIED; |
| 130 | } |
| 131 | if (client_head->flags != 0) { |
| 132 | client_head->flags = 0; |
| 133 | status = MONITORS_CONFIG_MODIFIED; |
| 134 | } |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 135 | DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height, |
| 136 | client_head->x, client_head->y); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 137 | } |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 138 | |
| 139 | return status; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 140 | } |
| 141 | |
Dave Airlie | 7dea094 | 2014-10-28 11:28:44 +1000 | [diff] [blame] | 142 | static void qxl_update_offset_props(struct qxl_device *qdev) |
| 143 | { |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 144 | struct drm_device *dev = &qdev->ddev; |
Dave Airlie | 7dea094 | 2014-10-28 11:28:44 +1000 | [diff] [blame] | 145 | struct drm_connector *connector; |
| 146 | struct qxl_output *output; |
| 147 | struct qxl_head *head; |
| 148 | |
| 149 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 150 | output = drm_connector_to_qxl_output(connector); |
| 151 | |
| 152 | head = &qdev->client_monitors_config->heads[output->index]; |
| 153 | |
| 154 | drm_object_property_set_value(&connector->base, |
| 155 | dev->mode_config.suggested_x_property, head->x); |
| 156 | drm_object_property_set_value(&connector->base, |
| 157 | dev->mode_config.suggested_y_property, head->y); |
| 158 | } |
| 159 | } |
| 160 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 161 | void qxl_display_read_client_monitors_config(struct qxl_device *qdev) |
| 162 | { |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 163 | struct drm_device *dev = &qdev->ddev; |
Gerd Hoffmann | 9062155 | 2017-03-01 11:12:32 +0100 | [diff] [blame] | 164 | int status, retries; |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 165 | |
Gerd Hoffmann | 9062155 | 2017-03-01 11:12:32 +0100 | [diff] [blame] | 166 | for (retries = 0; retries < 10; retries++) { |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 167 | status = qxl_display_copy_rom_client_monitors_config(qdev); |
Gerd Hoffmann | 9062155 | 2017-03-01 11:12:32 +0100 | [diff] [blame] | 168 | if (status != MONITORS_CONFIG_BAD_CRC) |
| 169 | break; |
| 170 | udelay(5); |
| 171 | } |
| 172 | if (status == MONITORS_CONFIG_BAD_CRC) { |
| 173 | qxl_io_log(qdev, "config: bad crc\n"); |
| 174 | DRM_DEBUG_KMS("ignoring client monitors config: bad crc"); |
| 175 | return; |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 176 | } |
| 177 | if (status == MONITORS_CONFIG_UNCHANGED) { |
Gerd Hoffmann | 9062155 | 2017-03-01 11:12:32 +0100 | [diff] [blame] | 178 | qxl_io_log(qdev, "config: unchanged\n"); |
| 179 | DRM_DEBUG_KMS("ignoring client monitors config: unchanged"); |
Christophe Fergeau | 9e3b317 | 2016-11-08 10:12:08 +0100 | [diff] [blame] | 180 | return; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 181 | } |
Marc-André Lureau | 4fdb086 | 2013-10-18 16:11:29 +0200 | [diff] [blame] | 182 | |
Dave Airlie | 7dea094 | 2014-10-28 11:28:44 +1000 | [diff] [blame] | 183 | drm_modeset_lock_all(dev); |
| 184 | qxl_update_offset_props(qdev); |
| 185 | drm_modeset_unlock_all(dev); |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 186 | if (!drm_helper_hpd_irq_event(dev)) { |
Marc-André Lureau | 4fdb086 | 2013-10-18 16:11:29 +0200 | [diff] [blame] | 187 | /* notify that the monitor configuration changed, to |
| 188 | adjust at the arbitrary resolution */ |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 189 | drm_kms_helper_hotplug_event(dev); |
Marc-André Lureau | 4fdb086 | 2013-10-18 16:11:29 +0200 | [diff] [blame] | 190 | } |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 191 | } |
| 192 | |
Marc-André Lureau | b080742 | 2013-10-18 16:11:31 +0200 | [diff] [blame] | 193 | static int qxl_add_monitors_config_modes(struct drm_connector *connector, |
| 194 | unsigned *pwidth, |
| 195 | unsigned *pheight) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 196 | { |
| 197 | struct drm_device *dev = connector->dev; |
| 198 | struct qxl_device *qdev = dev->dev_private; |
| 199 | struct qxl_output *output = drm_connector_to_qxl_output(connector); |
| 200 | int h = output->index; |
| 201 | struct drm_display_mode *mode = NULL; |
| 202 | struct qxl_head *head; |
| 203 | |
Gerd Hoffmann | 2d856f94 | 2017-03-01 11:12:34 +0100 | [diff] [blame] | 204 | if (!qdev->monitors_config) |
| 205 | return 0; |
| 206 | if (h >= qdev->monitors_config->max_allowed) |
| 207 | return 0; |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 208 | if (!qdev->client_monitors_config) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 209 | return 0; |
Gerd Hoffmann | 2d856f94 | 2017-03-01 11:12:34 +0100 | [diff] [blame] | 210 | if (h >= qdev->client_monitors_config->count) |
| 211 | return 0; |
| 212 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 213 | head = &qdev->client_monitors_config->heads[h]; |
Gerd Hoffmann | 2d856f94 | 2017-03-01 11:12:34 +0100 | [diff] [blame] | 214 | DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 215 | |
| 216 | mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false, |
| 217 | false); |
| 218 | mode->type |= DRM_MODE_TYPE_PREFERRED; |
Christophe Fergeau | ff996e7 | 2016-11-08 10:12:09 +0100 | [diff] [blame] | 219 | mode->hdisplay = head->width; |
| 220 | mode->vdisplay = head->height; |
| 221 | drm_mode_set_name(mode); |
Marc-André Lureau | b080742 | 2013-10-18 16:11:31 +0200 | [diff] [blame] | 222 | *pwidth = head->width; |
| 223 | *pheight = head->height; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 224 | drm_mode_probed_add(connector, mode); |
Jonathon Jongsma | bd3e1c7 | 2015-08-20 14:04:32 -0500 | [diff] [blame] | 225 | /* remember the last custom size for mode validation */ |
| 226 | qdev->monitors_config_width = mode->hdisplay; |
| 227 | qdev->monitors_config_height = mode->vdisplay; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 228 | return 1; |
| 229 | } |
| 230 | |
Jonathon Jongsma | bd3e1c7 | 2015-08-20 14:04:32 -0500 | [diff] [blame] | 231 | static struct mode_size { |
| 232 | int w; |
| 233 | int h; |
| 234 | } common_modes[] = { |
| 235 | { 640, 480}, |
| 236 | { 720, 480}, |
| 237 | { 800, 600}, |
| 238 | { 848, 480}, |
| 239 | {1024, 768}, |
| 240 | {1152, 768}, |
| 241 | {1280, 720}, |
| 242 | {1280, 800}, |
| 243 | {1280, 854}, |
| 244 | {1280, 960}, |
| 245 | {1280, 1024}, |
| 246 | {1440, 900}, |
| 247 | {1400, 1050}, |
| 248 | {1680, 1050}, |
| 249 | {1600, 1200}, |
| 250 | {1920, 1080}, |
| 251 | {1920, 1200} |
| 252 | }; |
| 253 | |
Marc-André Lureau | b080742 | 2013-10-18 16:11:31 +0200 | [diff] [blame] | 254 | static int qxl_add_common_modes(struct drm_connector *connector, |
| 255 | unsigned pwidth, |
| 256 | unsigned pheight) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 257 | { |
| 258 | struct drm_device *dev = connector->dev; |
| 259 | struct drm_display_mode *mode = NULL; |
| 260 | int i; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 261 | for (i = 0; i < ARRAY_SIZE(common_modes); i++) { |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 262 | mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, |
| 263 | 60, false, false, false); |
Marc-André Lureau | b080742 | 2013-10-18 16:11:31 +0200 | [diff] [blame] | 264 | if (common_modes[i].w == pwidth && common_modes[i].h == pheight) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 265 | mode->type |= DRM_MODE_TYPE_PREFERRED; |
| 266 | drm_mode_probed_add(connector, mode); |
| 267 | } |
| 268 | return i - 1; |
| 269 | } |
| 270 | |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 271 | static void qxl_crtc_atomic_flush(struct drm_crtc *crtc, |
| 272 | struct drm_crtc_state *old_crtc_state) |
| 273 | { |
| 274 | struct drm_device *dev = crtc->dev; |
| 275 | struct drm_pending_vblank_event *event; |
| 276 | unsigned long flags; |
| 277 | |
| 278 | if (crtc->state && crtc->state->event) { |
| 279 | event = crtc->state->event; |
| 280 | crtc->state->event = NULL; |
| 281 | |
| 282 | spin_lock_irqsave(&dev->event_lock, flags); |
| 283 | drm_crtc_send_vblank_event(crtc, event); |
| 284 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 285 | } |
| 286 | } |
| 287 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 288 | static void qxl_crtc_destroy(struct drm_crtc *crtc) |
| 289 | { |
| 290 | struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc); |
| 291 | |
| 292 | drm_crtc_cleanup(crtc); |
| 293 | kfree(qxl_crtc); |
| 294 | } |
| 295 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 296 | static const struct drm_crtc_funcs qxl_crtc_funcs = { |
Gabriel Krisman Bertazi | bc8a00d | 2017-02-27 17:43:26 -0300 | [diff] [blame] | 297 | .set_config = drm_atomic_helper_set_config, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 298 | .destroy = qxl_crtc_destroy, |
Gabriel Krisman Bertazi | 9973c87 | 2017-02-27 17:43:27 -0300 | [diff] [blame] | 299 | .page_flip = drm_atomic_helper_page_flip, |
Gabriel Krisman Bertazi | 9ade8b9 | 2017-02-27 17:43:23 -0300 | [diff] [blame] | 300 | .reset = drm_atomic_helper_crtc_reset, |
| 301 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 302 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 303 | }; |
| 304 | |
Noralf Trønnes | 6819c3c | 2016-04-28 17:18:36 +0200 | [diff] [blame] | 305 | void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 306 | { |
| 307 | struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb); |
| 308 | |
Markus Elfring | dc3583c | 2016-07-22 14:14:54 +0200 | [diff] [blame] | 309 | drm_gem_object_unreference_unlocked(qxl_fb->obj); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 310 | drm_framebuffer_cleanup(fb); |
| 311 | kfree(qxl_fb); |
| 312 | } |
| 313 | |
Dave Airlie | 6d01f1f | 2013-04-16 13:24:25 +1000 | [diff] [blame] | 314 | static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb, |
| 315 | struct drm_file *file_priv, |
| 316 | unsigned flags, unsigned color, |
| 317 | struct drm_clip_rect *clips, |
| 318 | unsigned num_clips) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 319 | { |
| 320 | /* TODO: vmwgfx where this was cribbed from had locking. Why? */ |
| 321 | struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb); |
| 322 | struct qxl_device *qdev = qxl_fb->base.dev->dev_private; |
| 323 | struct drm_clip_rect norect; |
| 324 | struct qxl_bo *qobj; |
| 325 | int inc = 1; |
| 326 | |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 327 | drm_modeset_lock_all(fb->dev); |
| 328 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 329 | qobj = gem_to_qxl_bo(qxl_fb->obj); |
Dave Airlie | b2b4465 | 2013-05-13 12:48:40 +1000 | [diff] [blame] | 330 | /* if we aren't primary surface ignore this */ |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 331 | if (!qobj->is_primary) { |
| 332 | drm_modeset_unlock_all(fb->dev); |
Dave Airlie | b2b4465 | 2013-05-13 12:48:40 +1000 | [diff] [blame] | 333 | return 0; |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 334 | } |
Dave Airlie | b2b4465 | 2013-05-13 12:48:40 +1000 | [diff] [blame] | 335 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 336 | if (!num_clips) { |
| 337 | num_clips = 1; |
| 338 | clips = &norect; |
| 339 | norect.x1 = norect.y1 = 0; |
| 340 | norect.x2 = fb->width; |
| 341 | norect.y2 = fb->height; |
| 342 | } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { |
| 343 | num_clips /= 2; |
| 344 | inc = 2; /* skip source rects */ |
| 345 | } |
| 346 | |
| 347 | qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color, |
| 348 | clips, num_clips, inc); |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 349 | |
| 350 | drm_modeset_unlock_all(fb->dev); |
| 351 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static const struct drm_framebuffer_funcs qxl_fb_funcs = { |
| 356 | .destroy = qxl_user_framebuffer_destroy, |
| 357 | .dirty = qxl_framebuffer_surface_dirty, |
| 358 | /* TODO? |
| 359 | * .create_handle = qxl_user_framebuffer_create_handle, */ |
| 360 | }; |
| 361 | |
| 362 | int |
| 363 | qxl_framebuffer_init(struct drm_device *dev, |
| 364 | struct qxl_framebuffer *qfb, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 365 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Noralf Trønnes | 6819c3c | 2016-04-28 17:18:36 +0200 | [diff] [blame] | 366 | struct drm_gem_object *obj, |
| 367 | const struct drm_framebuffer_funcs *funcs) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 368 | { |
| 369 | int ret; |
| 370 | |
| 371 | qfb->obj = obj; |
Ville Syrjälä | 5360943 | 2016-11-18 21:52:51 +0200 | [diff] [blame] | 372 | drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd); |
Noralf Trønnes | 6819c3c | 2016-04-28 17:18:36 +0200 | [diff] [blame] | 373 | ret = drm_framebuffer_init(dev, &qfb->base, funcs); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 374 | if (ret) { |
| 375 | qfb->obj = NULL; |
| 376 | return ret; |
| 377 | } |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 381 | static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc, |
| 382 | const struct drm_display_mode *mode, |
| 383 | struct drm_display_mode *adjusted_mode) |
| 384 | { |
| 385 | struct drm_device *dev = crtc->dev; |
| 386 | struct qxl_device *qdev = dev->dev_private; |
| 387 | |
| 388 | qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n", |
| 389 | __func__, |
| 390 | mode->hdisplay, mode->vdisplay, |
| 391 | adjusted_mode->hdisplay, |
| 392 | adjusted_mode->vdisplay); |
| 393 | return true; |
| 394 | } |
| 395 | |
Christophe Fergeau | e4a7644 | 2016-11-08 10:12:03 +0100 | [diff] [blame] | 396 | static void |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 397 | qxl_send_monitors_config(struct qxl_device *qdev) |
| 398 | { |
| 399 | int i; |
| 400 | |
| 401 | BUG_ON(!qdev->ram_header->monitors_config); |
| 402 | |
| 403 | if (qdev->monitors_config->count == 0) { |
| 404 | qxl_io_log(qdev, "%s: 0 monitors??\n", __func__); |
| 405 | return; |
| 406 | } |
| 407 | for (i = 0 ; i < qdev->monitors_config->count ; ++i) { |
| 408 | struct qxl_head *head = &qdev->monitors_config->heads[i]; |
| 409 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 410 | if (head->y > 8192 || head->x > 8192 || |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 411 | head->width > 8192 || head->height > 8192) { |
| 412 | DRM_ERROR("head %d wrong: %dx%d+%d+%d\n", |
| 413 | i, head->width, head->height, |
| 414 | head->x, head->y); |
| 415 | return; |
| 416 | } |
| 417 | } |
| 418 | qxl_io_monitors_config(qdev); |
| 419 | } |
| 420 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 421 | static void qxl_monitors_config_set(struct qxl_device *qdev, |
| 422 | int index, |
| 423 | unsigned x, unsigned y, |
| 424 | unsigned width, unsigned height, |
| 425 | unsigned surf_id) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 426 | { |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 427 | DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y); |
| 428 | qdev->monitors_config->heads[index].x = x; |
| 429 | qdev->monitors_config->heads[index].y = y; |
| 430 | qdev->monitors_config->heads[index].width = width; |
| 431 | qdev->monitors_config->heads[index].height = height; |
| 432 | qdev->monitors_config->heads[index].surface_id = surf_id; |
| 433 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 434 | } |
| 435 | |
Gerd Hoffmann | 45dfe57 | 2017-06-20 13:39:15 +0200 | [diff] [blame] | 436 | static void qxl_mode_set_nofb(struct drm_crtc *crtc) |
Gabriel Krisman Bertazi | 3538e80 | 2017-02-27 17:43:21 -0300 | [diff] [blame] | 437 | { |
| 438 | struct qxl_device *qdev = crtc->dev->dev_private; |
| 439 | struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); |
| 440 | struct drm_display_mode *mode = &crtc->mode; |
| 441 | |
| 442 | DRM_DEBUG("Mode set (%d,%d)\n", |
| 443 | mode->hdisplay, mode->vdisplay); |
| 444 | |
| 445 | qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, |
| 446 | mode->hdisplay, mode->vdisplay, 0); |
| 447 | |
| 448 | } |
| 449 | |
Laurent Pinchart | 0b20a0f | 2017-06-30 12:36:44 +0300 | [diff] [blame] | 450 | static void qxl_crtc_atomic_enable(struct drm_crtc *crtc, |
| 451 | struct drm_crtc_state *old_state) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 452 | { |
| 453 | DRM_DEBUG("\n"); |
| 454 | } |
| 455 | |
Laurent Pinchart | 6458171 | 2017-06-30 12:36:45 +0300 | [diff] [blame] | 456 | static void qxl_crtc_atomic_disable(struct drm_crtc *crtc, |
| 457 | struct drm_crtc_state *old_state) |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 458 | { |
| 459 | struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); |
Gabriel Krisman Bertazi | 3723545 | 2017-02-27 17:43:22 -0300 | [diff] [blame] | 460 | struct qxl_device *qdev = crtc->dev->dev_private; |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 461 | |
| 462 | qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0); |
| 463 | |
| 464 | qxl_send_monitors_config(qdev); |
| 465 | } |
| 466 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 467 | static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = { |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 468 | .mode_fixup = qxl_crtc_mode_fixup, |
Gabriel Krisman Bertazi | 3538e80 | 2017-02-27 17:43:21 -0300 | [diff] [blame] | 469 | .mode_set_nofb = qxl_mode_set_nofb, |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 470 | .atomic_flush = qxl_crtc_atomic_flush, |
Laurent Pinchart | 0b20a0f | 2017-06-30 12:36:44 +0300 | [diff] [blame] | 471 | .atomic_enable = qxl_crtc_atomic_enable, |
Laurent Pinchart | 6458171 | 2017-06-30 12:36:45 +0300 | [diff] [blame] | 472 | .atomic_disable = qxl_crtc_atomic_disable, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 473 | }; |
| 474 | |
Gerd Hoffmann | 45dfe57 | 2017-06-20 13:39:15 +0200 | [diff] [blame] | 475 | static int qxl_primary_atomic_check(struct drm_plane *plane, |
| 476 | struct drm_plane_state *state) |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 477 | { |
| 478 | struct qxl_device *qdev = plane->dev->dev_private; |
| 479 | struct qxl_framebuffer *qfb; |
| 480 | struct qxl_bo *bo; |
| 481 | |
| 482 | if (!state->crtc || !state->fb) |
| 483 | return 0; |
| 484 | |
| 485 | qfb = to_qxl_framebuffer(state->fb); |
| 486 | bo = gem_to_qxl_bo(qfb->obj); |
| 487 | |
| 488 | if (bo->surf.stride * bo->surf.height > qdev->vram_size) { |
| 489 | DRM_ERROR("Mode doesn't fit in vram size (vgamem)"); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static void qxl_primary_atomic_update(struct drm_plane *plane, |
| 497 | struct drm_plane_state *old_state) |
| 498 | { |
| 499 | struct qxl_device *qdev = plane->dev->dev_private; |
| 500 | struct qxl_framebuffer *qfb = |
| 501 | to_qxl_framebuffer(plane->state->fb); |
| 502 | struct qxl_framebuffer *qfb_old; |
| 503 | struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj); |
| 504 | struct qxl_bo *bo_old; |
| 505 | struct drm_clip_rect norect = { |
| 506 | .x1 = 0, |
| 507 | .y1 = 0, |
| 508 | .x2 = qfb->base.width, |
| 509 | .y2 = qfb->base.height |
| 510 | }; |
| 511 | |
Gerd Hoffmann | b0e07da | 2017-09-11 11:39:50 +0200 | [diff] [blame] | 512 | if (old_state->fb) { |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 513 | qfb_old = to_qxl_framebuffer(old_state->fb); |
| 514 | bo_old = gem_to_qxl_bo(qfb_old->obj); |
Gerd Hoffmann | b0e07da | 2017-09-11 11:39:50 +0200 | [diff] [blame] | 515 | } else { |
| 516 | bo_old = NULL; |
| 517 | } |
| 518 | |
| 519 | if (bo == bo_old) |
| 520 | return; |
| 521 | |
| 522 | if (bo_old && bo_old->is_primary) { |
| 523 | qxl_io_destroy_primary(qdev); |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 524 | bo_old->is_primary = false; |
| 525 | } |
| 526 | |
Gerd Hoffmann | b0e07da | 2017-09-11 11:39:50 +0200 | [diff] [blame] | 527 | if (!bo->is_primary) { |
| 528 | qxl_io_create_primary(qdev, 0, bo); |
| 529 | bo->is_primary = true; |
| 530 | } |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 531 | qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1); |
| 532 | } |
| 533 | |
| 534 | static void qxl_primary_atomic_disable(struct drm_plane *plane, |
| 535 | struct drm_plane_state *old_state) |
| 536 | { |
| 537 | struct qxl_device *qdev = plane->dev->dev_private; |
| 538 | |
Gerd Hoffmann | b0e07da | 2017-09-11 11:39:50 +0200 | [diff] [blame] | 539 | if (old_state->fb) { |
| 540 | struct qxl_framebuffer *qfb = |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 541 | to_qxl_framebuffer(old_state->fb); |
| 542 | struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj); |
| 543 | |
Gerd Hoffmann | b0e07da | 2017-09-11 11:39:50 +0200 | [diff] [blame] | 544 | if (bo->is_primary) { |
| 545 | qxl_io_destroy_primary(qdev); |
| 546 | bo->is_primary = false; |
| 547 | } |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Gerd Hoffmann | 45dfe57 | 2017-06-20 13:39:15 +0200 | [diff] [blame] | 551 | static int qxl_plane_atomic_check(struct drm_plane *plane, |
| 552 | struct drm_plane_state *state) |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 553 | { |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static void qxl_cursor_atomic_update(struct drm_plane *plane, |
| 558 | struct drm_plane_state *old_state) |
| 559 | { |
| 560 | struct drm_device *dev = plane->dev; |
| 561 | struct qxl_device *qdev = dev->dev_private; |
| 562 | struct drm_framebuffer *fb = plane->state->fb; |
| 563 | struct qxl_release *release; |
| 564 | struct qxl_cursor_cmd *cmd; |
| 565 | struct qxl_cursor *cursor; |
| 566 | struct drm_gem_object *obj; |
| 567 | struct qxl_bo *cursor_bo, *user_bo = NULL; |
| 568 | int ret; |
| 569 | void *user_ptr; |
| 570 | int size = 64*64*4; |
| 571 | |
| 572 | ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), |
| 573 | QXL_RELEASE_CURSOR_CMD, |
| 574 | &release, NULL); |
Dan Carpenter | ee5cb7c | 2017-03-14 10:54:10 +0300 | [diff] [blame] | 575 | if (ret) |
| 576 | return; |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 577 | |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 578 | if (fb != old_state->fb) { |
| 579 | obj = to_qxl_framebuffer(fb)->obj; |
| 580 | user_bo = gem_to_qxl_bo(obj); |
| 581 | |
| 582 | /* pinning is done in the prepare/cleanup framevbuffer */ |
| 583 | ret = qxl_bo_kmap(user_bo, &user_ptr); |
| 584 | if (ret) |
| 585 | goto out_free_release; |
| 586 | |
| 587 | ret = qxl_alloc_bo_reserved(qdev, release, |
| 588 | sizeof(struct qxl_cursor) + size, |
| 589 | &cursor_bo); |
| 590 | if (ret) |
| 591 | goto out_kunmap; |
| 592 | |
| 593 | ret = qxl_release_reserve_list(release, true); |
| 594 | if (ret) |
| 595 | goto out_free_bo; |
| 596 | |
| 597 | ret = qxl_bo_kmap(cursor_bo, (void **)&cursor); |
| 598 | if (ret) |
| 599 | goto out_backoff; |
| 600 | |
| 601 | cursor->header.unique = 0; |
| 602 | cursor->header.type = SPICE_CURSOR_TYPE_ALPHA; |
| 603 | cursor->header.width = 64; |
| 604 | cursor->header.height = 64; |
| 605 | cursor->header.hot_spot_x = fb->hot_x; |
| 606 | cursor->header.hot_spot_y = fb->hot_y; |
| 607 | cursor->data_size = size; |
| 608 | cursor->chunk.next_chunk = 0; |
| 609 | cursor->chunk.prev_chunk = 0; |
| 610 | cursor->chunk.data_size = size; |
| 611 | memcpy(cursor->chunk.data, user_ptr, size); |
| 612 | qxl_bo_kunmap(cursor_bo); |
| 613 | qxl_bo_kunmap(user_bo); |
| 614 | |
Gabriel Krisman Bertazi | 429030b | 2017-05-19 14:58:19 -0300 | [diff] [blame] | 615 | cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 616 | cmd->u.set.visible = 1; |
| 617 | cmd->u.set.shape = qxl_bo_physical_address(qdev, |
| 618 | cursor_bo, 0); |
| 619 | cmd->type = QXL_CURSOR_SET; |
| 620 | } else { |
| 621 | |
| 622 | ret = qxl_release_reserve_list(release, true); |
| 623 | if (ret) |
| 624 | goto out_free_release; |
| 625 | |
Gabriel Krisman Bertazi | 429030b | 2017-05-19 14:58:19 -0300 | [diff] [blame] | 626 | cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 627 | cmd->type = QXL_CURSOR_MOVE; |
| 628 | } |
| 629 | |
| 630 | cmd->u.position.x = plane->state->crtc_x + fb->hot_x; |
| 631 | cmd->u.position.y = plane->state->crtc_y + fb->hot_y; |
| 632 | |
| 633 | qxl_release_unmap(qdev, release, &cmd->release_info); |
| 634 | qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); |
| 635 | qxl_release_fence_buffer_objects(release); |
| 636 | |
| 637 | return; |
| 638 | |
| 639 | out_backoff: |
| 640 | qxl_release_backoff_reserve_list(release); |
| 641 | out_free_bo: |
| 642 | qxl_bo_unref(&cursor_bo); |
| 643 | out_kunmap: |
| 644 | qxl_bo_kunmap(user_bo); |
| 645 | out_free_release: |
| 646 | qxl_release_free(qdev, release); |
| 647 | return; |
| 648 | |
| 649 | } |
| 650 | |
Gerd Hoffmann | 45dfe57 | 2017-06-20 13:39:15 +0200 | [diff] [blame] | 651 | static void qxl_cursor_atomic_disable(struct drm_plane *plane, |
| 652 | struct drm_plane_state *old_state) |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 653 | { |
| 654 | struct qxl_device *qdev = plane->dev->dev_private; |
| 655 | struct qxl_release *release; |
| 656 | struct qxl_cursor_cmd *cmd; |
| 657 | int ret; |
| 658 | |
| 659 | ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), |
| 660 | QXL_RELEASE_CURSOR_CMD, |
| 661 | &release, NULL); |
| 662 | if (ret) |
| 663 | return; |
| 664 | |
| 665 | ret = qxl_release_reserve_list(release, true); |
| 666 | if (ret) { |
| 667 | qxl_release_free(qdev, release); |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); |
| 672 | cmd->type = QXL_CURSOR_HIDE; |
| 673 | qxl_release_unmap(qdev, release, &cmd->release_info); |
| 674 | |
| 675 | qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); |
| 676 | qxl_release_fence_buffer_objects(release); |
| 677 | } |
| 678 | |
Gerd Hoffmann | 45dfe57 | 2017-06-20 13:39:15 +0200 | [diff] [blame] | 679 | static int qxl_plane_prepare_fb(struct drm_plane *plane, |
| 680 | struct drm_plane_state *new_state) |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 681 | { |
| 682 | struct drm_gem_object *obj; |
| 683 | struct qxl_bo *user_bo; |
| 684 | int ret; |
| 685 | |
| 686 | if (!new_state->fb) |
| 687 | return 0; |
| 688 | |
| 689 | obj = to_qxl_framebuffer(new_state->fb)->obj; |
| 690 | user_bo = gem_to_qxl_bo(obj); |
| 691 | |
| 692 | ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static void qxl_plane_cleanup_fb(struct drm_plane *plane, |
| 700 | struct drm_plane_state *old_state) |
| 701 | { |
| 702 | struct drm_gem_object *obj; |
| 703 | struct qxl_bo *user_bo; |
| 704 | |
Gerd Hoffmann | 5f3d862 | 2017-09-18 09:41:45 +0200 | [diff] [blame^] | 705 | if (!old_state->fb) { |
| 706 | /* |
| 707 | * we never executed prepare_fb, so there's nothing to |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 708 | * unpin. |
| 709 | */ |
| 710 | return; |
| 711 | } |
| 712 | |
Gerd Hoffmann | 5f3d862 | 2017-09-18 09:41:45 +0200 | [diff] [blame^] | 713 | obj = to_qxl_framebuffer(old_state->fb)->obj; |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 714 | user_bo = gem_to_qxl_bo(obj); |
| 715 | qxl_bo_unpin(user_bo); |
| 716 | } |
| 717 | |
| 718 | static const uint32_t qxl_cursor_plane_formats[] = { |
| 719 | DRM_FORMAT_ARGB8888, |
| 720 | }; |
| 721 | |
| 722 | static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = { |
| 723 | .atomic_check = qxl_plane_atomic_check, |
| 724 | .atomic_update = qxl_cursor_atomic_update, |
| 725 | .atomic_disable = qxl_cursor_atomic_disable, |
| 726 | .prepare_fb = qxl_plane_prepare_fb, |
| 727 | .cleanup_fb = qxl_plane_cleanup_fb, |
| 728 | }; |
| 729 | |
| 730 | static const struct drm_plane_funcs qxl_cursor_plane_funcs = { |
Gabriel Krisman Bertazi | 472e6d4 | 2017-02-27 17:43:25 -0300 | [diff] [blame] | 731 | .update_plane = drm_atomic_helper_update_plane, |
| 732 | .disable_plane = drm_atomic_helper_disable_plane, |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 733 | .destroy = drm_primary_helper_destroy, |
Gabriel Krisman Bertazi | 9ade8b9 | 2017-02-27 17:43:23 -0300 | [diff] [blame] | 734 | .reset = drm_atomic_helper_plane_reset, |
| 735 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 736 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 737 | }; |
| 738 | |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 739 | static const uint32_t qxl_primary_plane_formats[] = { |
| 740 | DRM_FORMAT_XRGB8888, |
| 741 | DRM_FORMAT_ARGB8888, |
| 742 | }; |
| 743 | |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 744 | static const struct drm_plane_helper_funcs primary_helper_funcs = { |
| 745 | .atomic_check = qxl_primary_atomic_check, |
| 746 | .atomic_update = qxl_primary_atomic_update, |
| 747 | .atomic_disable = qxl_primary_atomic_disable, |
| 748 | .prepare_fb = qxl_plane_prepare_fb, |
| 749 | .cleanup_fb = qxl_plane_cleanup_fb, |
| 750 | }; |
| 751 | |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 752 | static const struct drm_plane_funcs qxl_primary_plane_funcs = { |
Gabriel Krisman Bertazi | 472e6d4 | 2017-02-27 17:43:25 -0300 | [diff] [blame] | 753 | .update_plane = drm_atomic_helper_update_plane, |
| 754 | .disable_plane = drm_atomic_helper_disable_plane, |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 755 | .destroy = drm_primary_helper_destroy, |
Gabriel Krisman Bertazi | 9ade8b9 | 2017-02-27 17:43:23 -0300 | [diff] [blame] | 756 | .reset = drm_atomic_helper_plane_reset, |
| 757 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 758 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 759 | }; |
| 760 | |
| 761 | static struct drm_plane *qxl_create_plane(struct qxl_device *qdev, |
| 762 | unsigned int possible_crtcs, |
| 763 | enum drm_plane_type type) |
| 764 | { |
| 765 | const struct drm_plane_helper_funcs *helper_funcs = NULL; |
| 766 | struct drm_plane *plane; |
| 767 | const struct drm_plane_funcs *funcs; |
| 768 | const uint32_t *formats; |
| 769 | int num_formats; |
| 770 | int err; |
| 771 | |
| 772 | if (type == DRM_PLANE_TYPE_PRIMARY) { |
| 773 | funcs = &qxl_primary_plane_funcs; |
| 774 | formats = qxl_primary_plane_formats; |
| 775 | num_formats = ARRAY_SIZE(qxl_primary_plane_formats); |
Gabriel Krisman Bertazi | c2ff663 | 2017-02-27 17:43:20 -0300 | [diff] [blame] | 776 | helper_funcs = &primary_helper_funcs; |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 777 | } else if (type == DRM_PLANE_TYPE_CURSOR) { |
| 778 | funcs = &qxl_cursor_plane_funcs; |
| 779 | formats = qxl_cursor_plane_formats; |
| 780 | helper_funcs = &qxl_cursor_helper_funcs; |
| 781 | num_formats = ARRAY_SIZE(qxl_cursor_plane_formats); |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 782 | } else { |
| 783 | return ERR_PTR(-EINVAL); |
| 784 | } |
| 785 | |
| 786 | plane = kzalloc(sizeof(*plane), GFP_KERNEL); |
| 787 | if (!plane) |
| 788 | return ERR_PTR(-ENOMEM); |
| 789 | |
| 790 | err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs, |
| 791 | funcs, formats, num_formats, |
Ben Widawsky | e6fc3b6 | 2017-07-23 20:46:38 -0700 | [diff] [blame] | 792 | NULL, type, NULL); |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 793 | if (err) |
| 794 | goto free_plane; |
| 795 | |
| 796 | drm_plane_helper_add(plane, helper_funcs); |
| 797 | |
| 798 | return plane; |
| 799 | |
| 800 | free_plane: |
| 801 | kfree(plane); |
| 802 | return ERR_PTR(-EINVAL); |
| 803 | } |
| 804 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 805 | static int qdev_crtc_init(struct drm_device *dev, int crtc_id) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 806 | { |
| 807 | struct qxl_crtc *qxl_crtc; |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 808 | struct drm_plane *primary, *cursor; |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 809 | struct qxl_device *qdev = dev->dev_private; |
| 810 | int r; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 811 | |
| 812 | qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL); |
| 813 | if (!qxl_crtc) |
| 814 | return -ENOMEM; |
| 815 | |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 816 | primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY); |
| 817 | if (IS_ERR(primary)) { |
| 818 | r = -ENOMEM; |
| 819 | goto free_mem; |
| 820 | } |
| 821 | |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 822 | cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR); |
| 823 | if (IS_ERR(cursor)) { |
| 824 | r = -ENOMEM; |
| 825 | goto clean_primary; |
| 826 | } |
| 827 | |
| 828 | r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor, |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 829 | &qxl_crtc_funcs, NULL); |
| 830 | if (r) |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 831 | goto clean_cursor; |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 832 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 833 | qxl_crtc->index = crtc_id; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 834 | drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs); |
| 835 | return 0; |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 836 | |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 837 | clean_cursor: |
| 838 | drm_plane_cleanup(cursor); |
| 839 | kfree(cursor); |
Gabriel Krisman Bertazi | d3e7e42 | 2017-02-27 17:43:18 -0300 | [diff] [blame] | 840 | clean_primary: |
| 841 | drm_plane_cleanup(primary); |
| 842 | kfree(primary); |
| 843 | free_mem: |
| 844 | kfree(qxl_crtc); |
| 845 | return r; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static void qxl_enc_dpms(struct drm_encoder *encoder, int mode) |
| 849 | { |
| 850 | DRM_DEBUG("\n"); |
| 851 | } |
| 852 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 853 | static void qxl_enc_prepare(struct drm_encoder *encoder) |
| 854 | { |
| 855 | DRM_DEBUG("\n"); |
| 856 | } |
| 857 | |
| 858 | static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev, |
| 859 | struct drm_encoder *encoder) |
| 860 | { |
| 861 | int i; |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 862 | struct qxl_output *output = drm_encoder_to_qxl_output(encoder); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 863 | struct qxl_head *head; |
| 864 | struct drm_display_mode *mode; |
| 865 | |
| 866 | BUG_ON(!encoder); |
| 867 | /* TODO: ugly, do better */ |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 868 | i = output->index; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 869 | if (!qdev->monitors_config || |
| 870 | qdev->monitors_config->max_allowed <= i) { |
| 871 | DRM_ERROR( |
| 872 | "head number too large or missing monitors config: %p, %d", |
| 873 | qdev->monitors_config, |
| 874 | qdev->monitors_config ? |
| 875 | qdev->monitors_config->max_allowed : -1); |
| 876 | return; |
| 877 | } |
| 878 | if (!encoder->crtc) { |
| 879 | DRM_ERROR("missing crtc on encoder %p\n", encoder); |
| 880 | return; |
| 881 | } |
| 882 | if (i != 0) |
| 883 | DRM_DEBUG("missing for multiple monitors: no head holes\n"); |
| 884 | head = &qdev->monitors_config->heads[i]; |
| 885 | head->id = i; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 886 | if (encoder->crtc->enabled) { |
| 887 | mode = &encoder->crtc->mode; |
| 888 | head->width = mode->hdisplay; |
| 889 | head->height = mode->vdisplay; |
| 890 | head->x = encoder->crtc->x; |
| 891 | head->y = encoder->crtc->y; |
| 892 | if (qdev->monitors_config->count < i + 1) |
| 893 | qdev->monitors_config->count = i + 1; |
| 894 | } else { |
| 895 | head->width = 0; |
| 896 | head->height = 0; |
| 897 | head->x = 0; |
| 898 | head->y = 0; |
| 899 | } |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 900 | DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n", |
| 901 | i, head->x, head->y, head->width, head->height, qdev->monitors_config->count); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 902 | head->flags = 0; |
| 903 | /* TODO - somewhere else to call this for multiple monitors |
| 904 | * (config_commit?) */ |
| 905 | qxl_send_monitors_config(qdev); |
| 906 | } |
| 907 | |
| 908 | static void qxl_enc_commit(struct drm_encoder *encoder) |
| 909 | { |
| 910 | struct qxl_device *qdev = encoder->dev->dev_private; |
| 911 | |
| 912 | qxl_write_monitors_config_for_encoder(qdev, encoder); |
| 913 | DRM_DEBUG("\n"); |
| 914 | } |
| 915 | |
| 916 | static void qxl_enc_mode_set(struct drm_encoder *encoder, |
| 917 | struct drm_display_mode *mode, |
| 918 | struct drm_display_mode *adjusted_mode) |
| 919 | { |
| 920 | DRM_DEBUG("\n"); |
| 921 | } |
| 922 | |
| 923 | static int qxl_conn_get_modes(struct drm_connector *connector) |
| 924 | { |
Marc-André Lureau | b080742 | 2013-10-18 16:11:31 +0200 | [diff] [blame] | 925 | unsigned pwidth = 1024; |
| 926 | unsigned pheight = 768; |
Gerd Hoffmann | 2d856f94 | 2017-03-01 11:12:34 +0100 | [diff] [blame] | 927 | int ret = 0; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 928 | |
Gerd Hoffmann | 2d856f94 | 2017-03-01 11:12:34 +0100 | [diff] [blame] | 929 | ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight); |
| 930 | if (ret < 0) |
| 931 | return ret; |
Marc-André Lureau | b080742 | 2013-10-18 16:11:31 +0200 | [diff] [blame] | 932 | ret += qxl_add_common_modes(connector, pwidth, pheight); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | static int qxl_conn_mode_valid(struct drm_connector *connector, |
| 937 | struct drm_display_mode *mode) |
| 938 | { |
Jonathon Jongsma | bd3e1c7 | 2015-08-20 14:04:32 -0500 | [diff] [blame] | 939 | struct drm_device *ddev = connector->dev; |
| 940 | struct qxl_device *qdev = ddev->dev_private; |
| 941 | int i; |
| 942 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 943 | /* TODO: is this called for user defined modes? (xrandr --add-mode) |
| 944 | * TODO: check that the mode fits in the framebuffer */ |
Jonathon Jongsma | bd3e1c7 | 2015-08-20 14:04:32 -0500 | [diff] [blame] | 945 | |
| 946 | if(qdev->monitors_config_width == mode->hdisplay && |
| 947 | qdev->monitors_config_height == mode->vdisplay) |
| 948 | return MODE_OK; |
| 949 | |
| 950 | for (i = 0; i < ARRAY_SIZE(common_modes); i++) { |
| 951 | if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay) |
| 952 | return MODE_OK; |
| 953 | } |
| 954 | return MODE_BAD; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 955 | } |
| 956 | |
Dave Airlie | 6d01f1f | 2013-04-16 13:24:25 +1000 | [diff] [blame] | 957 | static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 958 | { |
| 959 | struct qxl_output *qxl_output = |
| 960 | drm_connector_to_qxl_output(connector); |
| 961 | |
| 962 | DRM_DEBUG("\n"); |
| 963 | return &qxl_output->enc; |
| 964 | } |
| 965 | |
| 966 | |
| 967 | static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = { |
| 968 | .dpms = qxl_enc_dpms, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 969 | .prepare = qxl_enc_prepare, |
| 970 | .mode_set = qxl_enc_mode_set, |
| 971 | .commit = qxl_enc_commit, |
| 972 | }; |
| 973 | |
| 974 | static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = { |
| 975 | .get_modes = qxl_conn_get_modes, |
| 976 | .mode_valid = qxl_conn_mode_valid, |
| 977 | .best_encoder = qxl_best_encoder, |
| 978 | }; |
| 979 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 980 | static enum drm_connector_status qxl_conn_detect( |
| 981 | struct drm_connector *connector, |
| 982 | bool force) |
| 983 | { |
| 984 | struct qxl_output *output = |
| 985 | drm_connector_to_qxl_output(connector); |
| 986 | struct drm_device *ddev = connector->dev; |
| 987 | struct qxl_device *qdev = ddev->dev_private; |
Dave Airlie | 69e5d3f | 2015-09-14 10:28:34 +1000 | [diff] [blame] | 988 | bool connected = false; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 989 | |
| 990 | /* The first monitor is always connected */ |
Dave Airlie | 69e5d3f | 2015-09-14 10:28:34 +1000 | [diff] [blame] | 991 | if (!qdev->client_monitors_config) { |
| 992 | if (output->index == 0) |
| 993 | connected = true; |
| 994 | } else |
| 995 | connected = qdev->client_monitors_config->count > output->index && |
| 996 | qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 997 | |
Marc-André Lureau | 5cab51c | 2013-10-18 16:11:33 +0200 | [diff] [blame] | 998 | DRM_DEBUG("#%d connected: %d\n", output->index, connected); |
| 999 | if (!connected) |
| 1000 | qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0); |
| 1001 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1002 | return connected ? connector_status_connected |
| 1003 | : connector_status_disconnected; |
| 1004 | } |
| 1005 | |
| 1006 | static int qxl_conn_set_property(struct drm_connector *connector, |
| 1007 | struct drm_property *property, |
| 1008 | uint64_t value) |
| 1009 | { |
| 1010 | DRM_DEBUG("\n"); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | static void qxl_conn_destroy(struct drm_connector *connector) |
| 1015 | { |
| 1016 | struct qxl_output *qxl_output = |
| 1017 | drm_connector_to_qxl_output(connector); |
| 1018 | |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 1019 | drm_connector_unregister(connector); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1020 | drm_connector_cleanup(connector); |
| 1021 | kfree(qxl_output); |
| 1022 | } |
| 1023 | |
| 1024 | static const struct drm_connector_funcs qxl_connector_funcs = { |
| 1025 | .dpms = drm_helper_connector_dpms, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1026 | .detect = qxl_conn_detect, |
Ville Syrjälä | 6af3e65 | 2015-12-03 23:14:14 +0200 | [diff] [blame] | 1027 | .fill_modes = drm_helper_probe_single_connector_modes, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1028 | .set_property = qxl_conn_set_property, |
| 1029 | .destroy = qxl_conn_destroy, |
Gabriel Krisman Bertazi | 9ade8b9 | 2017-02-27 17:43:23 -0300 | [diff] [blame] | 1030 | .reset = drm_atomic_helper_connector_reset, |
| 1031 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 1032 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1033 | }; |
| 1034 | |
| 1035 | static void qxl_enc_destroy(struct drm_encoder *encoder) |
| 1036 | { |
| 1037 | drm_encoder_cleanup(encoder); |
| 1038 | } |
| 1039 | |
| 1040 | static const struct drm_encoder_funcs qxl_enc_funcs = { |
| 1041 | .destroy = qxl_enc_destroy, |
| 1042 | }; |
| 1043 | |
Dave Airlie | 4695b03 | 2013-10-11 11:05:00 +1000 | [diff] [blame] | 1044 | static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev) |
| 1045 | { |
| 1046 | if (qdev->hotplug_mode_update_property) |
| 1047 | return 0; |
| 1048 | |
| 1049 | qdev->hotplug_mode_update_property = |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1050 | drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE, |
Dave Airlie | 4695b03 | 2013-10-11 11:05:00 +1000 | [diff] [blame] | 1051 | "hotplug_mode_update", 0, 1); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Dave Airlie | 6d01f1f | 2013-04-16 13:24:25 +1000 | [diff] [blame] | 1056 | static int qdev_output_init(struct drm_device *dev, int num_output) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1057 | { |
Dave Airlie | 4695b03 | 2013-10-11 11:05:00 +1000 | [diff] [blame] | 1058 | struct qxl_device *qdev = dev->dev_private; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1059 | struct qxl_output *qxl_output; |
| 1060 | struct drm_connector *connector; |
| 1061 | struct drm_encoder *encoder; |
| 1062 | |
| 1063 | qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL); |
| 1064 | if (!qxl_output) |
| 1065 | return -ENOMEM; |
| 1066 | |
| 1067 | qxl_output->index = num_output; |
| 1068 | |
| 1069 | connector = &qxl_output->base; |
| 1070 | encoder = &qxl_output->enc; |
| 1071 | drm_connector_init(dev, &qxl_output->base, |
| 1072 | &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL); |
| 1073 | |
| 1074 | drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs, |
Ville Syrjälä | 13a3d91 | 2015-12-09 16:20:18 +0200 | [diff] [blame] | 1075 | DRM_MODE_ENCODER_VIRTUAL, NULL); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1076 | |
Dave Airlie | 5ff91e4 | 2013-07-05 10:20:33 +1000 | [diff] [blame] | 1077 | /* we get HPD via client monitors config */ |
| 1078 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1079 | encoder->possible_crtcs = 1 << num_output; |
| 1080 | drm_mode_connector_attach_encoder(&qxl_output->base, |
| 1081 | &qxl_output->enc); |
| 1082 | drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs); |
| 1083 | drm_connector_helper_add(connector, &qxl_connector_helper_funcs); |
| 1084 | |
Dave Airlie | 4695b03 | 2013-10-11 11:05:00 +1000 | [diff] [blame] | 1085 | drm_object_attach_property(&connector->base, |
| 1086 | qdev->hotplug_mode_update_property, 0); |
Dave Airlie | 7dea094 | 2014-10-28 11:28:44 +1000 | [diff] [blame] | 1087 | drm_object_attach_property(&connector->base, |
| 1088 | dev->mode_config.suggested_x_property, 0); |
| 1089 | drm_object_attach_property(&connector->base, |
| 1090 | dev->mode_config.suggested_y_property, 0); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static struct drm_framebuffer * |
| 1095 | qxl_user_framebuffer_create(struct drm_device *dev, |
| 1096 | struct drm_file *file_priv, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 1097 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1098 | { |
| 1099 | struct drm_gem_object *obj; |
| 1100 | struct qxl_framebuffer *qxl_fb; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1101 | int ret; |
| 1102 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 1103 | obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]); |
| 1104 | if (!obj) |
| 1105 | return NULL; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1106 | |
| 1107 | qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL); |
| 1108 | if (qxl_fb == NULL) |
| 1109 | return NULL; |
| 1110 | |
Noralf Trønnes | 6819c3c | 2016-04-28 17:18:36 +0200 | [diff] [blame] | 1111 | ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1112 | if (ret) { |
| 1113 | kfree(qxl_fb); |
| 1114 | drm_gem_object_unreference_unlocked(obj); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1118 | return &qxl_fb->base; |
| 1119 | } |
| 1120 | |
| 1121 | static const struct drm_mode_config_funcs qxl_mode_funcs = { |
| 1122 | .fb_create = qxl_user_framebuffer_create, |
Gabriel Krisman Bertazi | 472e6d4 | 2017-02-27 17:43:25 -0300 | [diff] [blame] | 1123 | .atomic_check = drm_atomic_helper_check, |
| 1124 | .atomic_commit = drm_atomic_helper_commit, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1125 | }; |
| 1126 | |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1127 | int qxl_create_monitors_object(struct qxl_device *qdev) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1128 | { |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1129 | int ret; |
| 1130 | struct drm_gem_object *gobj; |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 1131 | int max_allowed = qxl_num_crtc; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1132 | int monitors_config_size = sizeof(struct qxl_monitors_config) + |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1133 | max_allowed * sizeof(struct qxl_head); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1134 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1135 | ret = qxl_gem_object_create(qdev, monitors_config_size, 0, |
| 1136 | QXL_GEM_DOMAIN_VRAM, |
| 1137 | false, false, NULL, &gobj); |
| 1138 | if (ret) { |
| 1139 | DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret); |
| 1140 | return -ENOMEM; |
| 1141 | } |
| 1142 | qdev->monitors_config_bo = gem_to_qxl_bo(gobj); |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1143 | |
Gabriel Krisman Bertazi | 715a11f | 2017-02-27 17:43:16 -0300 | [diff] [blame] | 1144 | ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL); |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1145 | if (ret) |
| 1146 | return ret; |
| 1147 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1148 | qxl_bo_kmap(qdev->monitors_config_bo, NULL); |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1149 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1150 | qdev->monitors_config = qdev->monitors_config_bo->kptr; |
| 1151 | qdev->ram_header->monitors_config = |
| 1152 | qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0); |
| 1153 | |
| 1154 | memset(qdev->monitors_config, 0, monitors_config_size); |
| 1155 | qdev->monitors_config->max_allowed = max_allowed; |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | int qxl_destroy_monitors_object(struct qxl_device *qdev) |
| 1160 | { |
| 1161 | int ret; |
| 1162 | |
| 1163 | qdev->monitors_config = NULL; |
| 1164 | qdev->ram_header->monitors_config = 0; |
| 1165 | |
| 1166 | qxl_bo_kunmap(qdev->monitors_config_bo); |
Gabriel Krisman Bertazi | 715a11f | 2017-02-27 17:43:16 -0300 | [diff] [blame] | 1167 | ret = qxl_bo_unpin(qdev->monitors_config_bo); |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1171 | qxl_bo_unref(&qdev->monitors_config_bo); |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | int qxl_modeset_init(struct qxl_device *qdev) |
| 1176 | { |
| 1177 | int i; |
| 1178 | int ret; |
| 1179 | |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1180 | drm_mode_config_init(&qdev->ddev); |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1181 | |
| 1182 | ret = qxl_create_monitors_object(qdev); |
| 1183 | if (ret) |
| 1184 | return ret; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1185 | |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1186 | qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1187 | |
| 1188 | /* modes will be validated against the framebuffer size */ |
Gabriel Krisman Bertazi | 1277eed | 2017-02-27 17:43:19 -0300 | [diff] [blame] | 1189 | qdev->ddev.mode_config.min_width = 0; |
| 1190 | qdev->ddev.mode_config.min_height = 0; |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1191 | qdev->ddev.mode_config.max_width = 8192; |
| 1192 | qdev->ddev.mode_config.max_height = 8192; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1193 | |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1194 | qdev->ddev.mode_config.fb_base = qdev->vram_base; |
Dave Airlie | 4695b03 | 2013-10-11 11:05:00 +1000 | [diff] [blame] | 1195 | |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1196 | drm_mode_create_suggested_offset_properties(&qdev->ddev); |
Dave Airlie | 4695b03 | 2013-10-11 11:05:00 +1000 | [diff] [blame] | 1197 | qxl_mode_create_hotplug_mode_update_property(qdev); |
| 1198 | |
Dave Airlie | 07f8d9b | 2013-07-02 06:37:13 +0100 | [diff] [blame] | 1199 | for (i = 0 ; i < qxl_num_crtc; ++i) { |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1200 | qdev_crtc_init(&qdev->ddev, i); |
| 1201 | qdev_output_init(&qdev->ddev, i); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1202 | } |
| 1203 | |
Gerd Hoffmann | c50fad8 | 2017-03-01 11:12:33 +0100 | [diff] [blame] | 1204 | qxl_display_read_client_monitors_config(qdev); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1205 | qdev->mode_info.mode_config_initialized = true; |
| 1206 | |
Gabriel Krisman Bertazi | 9ade8b9 | 2017-02-27 17:43:23 -0300 | [diff] [blame] | 1207 | drm_mode_config_reset(&qdev->ddev); |
| 1208 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1209 | /* primary surface must be created by this point, to allow |
| 1210 | * issuing command queue commands and having them read by |
| 1211 | * spice server. */ |
| 1212 | qxl_fbdev_init(qdev); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | void qxl_modeset_fini(struct qxl_device *qdev) |
| 1217 | { |
| 1218 | qxl_fbdev_fini(qdev); |
Dave Airlie | 2bd6ce8 | 2013-07-04 14:46:46 +1000 | [diff] [blame] | 1219 | |
| 1220 | qxl_destroy_monitors_object(qdev); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1221 | if (qdev->mode_info.mode_config_initialized) { |
Gabriel Krisman Bertazi | cbdded7 | 2017-01-26 23:05:48 -0200 | [diff] [blame] | 1222 | drm_mode_config_cleanup(&qdev->ddev); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1223 | qdev->mode_info.mode_config_initialized = false; |
| 1224 | } |
| 1225 | } |