1 /*
2  * Copyright © 2013 Red Hat
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 #include <linux/module.h>
27 
28 #include <drm/drmP.h>
29 #include <drm/drm.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_fb_helper.h>
33 
34 #include "qxl_drv.h"
35 
36 #include "qxl_object.h"
37 
38 #define QXL_DIRTY_DELAY (HZ / 30)
39 
40 struct qxl_fbdev {
41 	struct drm_fb_helper helper;
42 	struct qxl_framebuffer	qfb;
43 	struct qxl_device	*qdev;
44 
45 	spinlock_t delayed_ops_lock;
46 	struct list_head delayed_ops;
47 	void *shadow;
48 	int size;
49 };
50 
qxl_fb_image_init(struct qxl_fb_image * qxl_fb_image,struct qxl_device * qdev,struct fb_info * info,const struct fb_image * image)51 static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
52 			      struct qxl_device *qdev, struct fb_info *info,
53 			      const struct fb_image *image)
54 {
55 	qxl_fb_image->qdev = qdev;
56 	if (info) {
57 		qxl_fb_image->visual = info->fix.visual;
58 		if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
59 		    qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
60 			memcpy(&qxl_fb_image->pseudo_palette,
61 			       info->pseudo_palette,
62 			       sizeof(qxl_fb_image->pseudo_palette));
63 	} else {
64 		 /* fallback */
65 		if (image->depth == 1)
66 			qxl_fb_image->visual = FB_VISUAL_MONO10;
67 		else
68 			qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
69 	}
70 	if (image) {
71 		memcpy(&qxl_fb_image->fb_image, image,
72 		       sizeof(qxl_fb_image->fb_image));
73 	}
74 }
75 
76 #ifdef CONFIG_DRM_FBDEV_EMULATION
77 static struct fb_deferred_io qxl_defio = {
78 	.delay		= QXL_DIRTY_DELAY,
79 	.deferred_io	= drm_fb_helper_deferred_io,
80 };
81 #endif
82 
83 static struct fb_ops qxlfb_ops = {
84 	.owner = THIS_MODULE,
85 	DRM_FB_HELPER_DEFAULT_OPS,
86 	.fb_fillrect = drm_fb_helper_sys_fillrect,
87 	.fb_copyarea = drm_fb_helper_sys_copyarea,
88 	.fb_imageblit = drm_fb_helper_sys_imageblit,
89 };
90 
qxlfb_destroy_pinned_object(struct drm_gem_object * gobj)91 static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
92 {
93 	struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
94 
95 	qxl_bo_kunmap(qbo);
96 	qxl_bo_unpin(qbo);
97 
98 	drm_gem_object_put_unlocked(gobj);
99 }
100 
qxl_get_handle_for_primary_fb(struct qxl_device * qdev,struct drm_file * file_priv,uint32_t * handle)101 int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
102 				  struct drm_file *file_priv,
103 				  uint32_t *handle)
104 {
105 	int r;
106 	struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
107 
108 	BUG_ON(!gobj);
109 	/* drm_get_handle_create adds a reference - good */
110 	r = drm_gem_handle_create(file_priv, gobj, handle);
111 	if (r)
112 		return r;
113 	return 0;
114 }
115 
qxlfb_create_pinned_object(struct qxl_fbdev * qfbdev,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** gobj_p)116 static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
117 				      const struct drm_mode_fb_cmd2 *mode_cmd,
118 				      struct drm_gem_object **gobj_p)
119 {
120 	struct qxl_device *qdev = qfbdev->qdev;
121 	struct drm_gem_object *gobj = NULL;
122 	struct qxl_bo *qbo = NULL;
123 	int ret;
124 	int aligned_size, size;
125 	int height = mode_cmd->height;
126 
127 	size = mode_cmd->pitches[0] * height;
128 	aligned_size = ALIGN(size, PAGE_SIZE);
129 	/* TODO: unallocate and reallocate surface0 for real. Hack to just
130 	 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
131 	ret = qxl_gem_object_create(qdev, aligned_size, 0,
132 				    QXL_GEM_DOMAIN_SURFACE,
133 				    false, /* is discardable */
134 				    false, /* is kernel (false means device) */
135 				    NULL,
136 				    &gobj);
137 	if (ret) {
138 		pr_err("failed to allocate framebuffer (%d)\n",
139 		       aligned_size);
140 		return -ENOMEM;
141 	}
142 	qbo = gem_to_qxl_bo(gobj);
143 
144 	qbo->surf.width = mode_cmd->width;
145 	qbo->surf.height = mode_cmd->height;
146 	qbo->surf.stride = mode_cmd->pitches[0];
147 	qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
148 
149 	ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
150 	if (ret) {
151 		goto out_unref;
152 	}
153 	ret = qxl_bo_kmap(qbo, NULL);
154 
155 	if (ret)
156 		goto out_unref;
157 
158 	*gobj_p = gobj;
159 	return 0;
160 out_unref:
161 	qxlfb_destroy_pinned_object(gobj);
162 	*gobj_p = NULL;
163 	return ret;
164 }
165 
166 /*
167  * FIXME
168  * It should not be necessary to have a special dirty() callback for fbdev.
169  */
qxlfb_framebuffer_dirty(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned flags,unsigned color,struct drm_clip_rect * clips,unsigned num_clips)170 static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
171 				   struct drm_file *file_priv,
172 				   unsigned flags, unsigned color,
173 				   struct drm_clip_rect *clips,
174 				   unsigned num_clips)
175 {
176 	struct qxl_device *qdev = fb->dev->dev_private;
177 	struct fb_info *info = qdev->fbdev_info;
178 	struct qxl_fbdev *qfbdev = info->par;
179 	struct qxl_fb_image qxl_fb_image;
180 	struct fb_image *image = &qxl_fb_image.fb_image;
181 
182 	/* TODO: hard coding 32 bpp */
183 	int stride = qfbdev->qfb.base.pitches[0];
184 
185 	/*
186 	 * we are using a shadow draw buffer, at qdev->surface0_shadow
187 	 */
188 	image->dx = clips->x1;
189 	image->dy = clips->y1;
190 	image->width = clips->x2 - clips->x1;
191 	image->height = clips->y2 - clips->y1;
192 	image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
193 					 warnings */
194 	image->bg_color = 0;
195 	image->depth = 32;	     /* TODO: take from somewhere? */
196 	image->cmap.start = 0;
197 	image->cmap.len = 0;
198 	image->cmap.red = NULL;
199 	image->cmap.green = NULL;
200 	image->cmap.blue = NULL;
201 	image->cmap.transp = NULL;
202 	image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
203 
204 	qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
205 	qxl_draw_opaque_fb(&qxl_fb_image, stride);
206 
207 	return 0;
208 }
209 
210 static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
211 	.destroy = qxl_user_framebuffer_destroy,
212 	.dirty = qxlfb_framebuffer_dirty,
213 };
214 
qxlfb_create(struct qxl_fbdev * qfbdev,struct drm_fb_helper_surface_size * sizes)215 static int qxlfb_create(struct qxl_fbdev *qfbdev,
216 			struct drm_fb_helper_surface_size *sizes)
217 {
218 	struct qxl_device *qdev = qfbdev->qdev;
219 	struct fb_info *info;
220 	struct drm_framebuffer *fb = NULL;
221 	struct drm_mode_fb_cmd2 mode_cmd;
222 	struct drm_gem_object *gobj = NULL;
223 	struct qxl_bo *qbo = NULL;
224 	int ret;
225 	int size;
226 	int bpp = sizes->surface_bpp;
227 	int depth = sizes->surface_depth;
228 	void *shadow;
229 
230 	mode_cmd.width = sizes->surface_width;
231 	mode_cmd.height = sizes->surface_height;
232 
233 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
234 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
235 
236 	ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
237 	if (ret < 0)
238 		return ret;
239 
240 	qbo = gem_to_qxl_bo(gobj);
241 	DRM_DEBUG_DRIVER("%dx%d %d\n", mode_cmd.width,
242 			 mode_cmd.height, mode_cmd.pitches[0]);
243 
244 	shadow = vmalloc(array_size(mode_cmd.pitches[0], mode_cmd.height));
245 	/* TODO: what's the usual response to memory allocation errors? */
246 	BUG_ON(!shadow);
247 	DRM_DEBUG_DRIVER("surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
248 			 qxl_bo_gpu_offset(qbo), qxl_bo_mmap_offset(qbo),
249 			 qbo->kptr, shadow);
250 	size = mode_cmd.pitches[0] * mode_cmd.height;
251 
252 	info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
253 	if (IS_ERR(info)) {
254 		ret = PTR_ERR(info);
255 		goto out_unref;
256 	}
257 
258 	info->par = qfbdev;
259 
260 	qxl_framebuffer_init(&qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
261 			     &qxlfb_fb_funcs);
262 
263 	fb = &qfbdev->qfb.base;
264 
265 	/* setup helper with fb data */
266 	qfbdev->helper.fb = fb;
267 
268 	qfbdev->shadow = shadow;
269 	strcpy(info->fix.id, "qxldrmfb");
270 
271 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
272 
273 	info->fbops = &qxlfb_ops;
274 
275 	/*
276 	 * TODO: using gobj->size in various places in this function. Not sure
277 	 * what the difference between the different sizes is.
278 	 */
279 	info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
280 	info->fix.smem_len = gobj->size;
281 	info->screen_base = qfbdev->shadow;
282 	info->screen_size = gobj->size;
283 
284 	drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
285 			       sizes->fb_height);
286 
287 	/* setup aperture base/size for vesafb takeover */
288 	info->apertures->ranges[0].base = qdev->ddev.mode_config.fb_base;
289 	info->apertures->ranges[0].size = qdev->vram_size;
290 
291 	info->fix.mmio_start = 0;
292 	info->fix.mmio_len = 0;
293 
294 	if (info->screen_base == NULL) {
295 		ret = -ENOSPC;
296 		goto out_unref;
297 	}
298 
299 #ifdef CONFIG_DRM_FBDEV_EMULATION
300 	info->fbdefio = &qxl_defio;
301 	fb_deferred_io_init(info);
302 #endif
303 
304 	qdev->fbdev_info = info;
305 	qdev->fbdev_qfb = &qfbdev->qfb;
306 	DRM_INFO("fb mappable at 0x%lX, size %lu\n",  info->fix.smem_start, (unsigned long)info->screen_size);
307 	DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n",
308 		 fb->format->depth, fb->pitches[0], fb->width, fb->height);
309 	return 0;
310 
311 out_unref:
312 	if (qbo) {
313 		qxl_bo_kunmap(qbo);
314 		qxl_bo_unpin(qbo);
315 	}
316 	if (fb && ret) {
317 		drm_gem_object_put_unlocked(gobj);
318 		drm_framebuffer_cleanup(fb);
319 		kfree(fb);
320 	}
321 	drm_gem_object_put_unlocked(gobj);
322 	return ret;
323 }
324 
qxl_fb_find_or_create_single(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)325 static int qxl_fb_find_or_create_single(
326 		struct drm_fb_helper *helper,
327 		struct drm_fb_helper_surface_size *sizes)
328 {
329 	struct qxl_fbdev *qfbdev =
330 		container_of(helper, struct qxl_fbdev, helper);
331 	int new_fb = 0;
332 	int ret;
333 
334 	if (!helper->fb) {
335 		ret = qxlfb_create(qfbdev, sizes);
336 		if (ret)
337 			return ret;
338 		new_fb = 1;
339 	}
340 	return new_fb;
341 }
342 
qxl_fbdev_destroy(struct drm_device * dev,struct qxl_fbdev * qfbdev)343 static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
344 {
345 	struct qxl_framebuffer *qfb = &qfbdev->qfb;
346 
347 	drm_fb_helper_unregister_fbi(&qfbdev->helper);
348 
349 	if (qfb->obj) {
350 		qxlfb_destroy_pinned_object(qfb->obj);
351 		qfb->obj = NULL;
352 	}
353 	drm_fb_helper_fini(&qfbdev->helper);
354 	vfree(qfbdev->shadow);
355 	drm_framebuffer_cleanup(&qfb->base);
356 
357 	return 0;
358 }
359 
360 static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
361 	.fb_probe = qxl_fb_find_or_create_single,
362 };
363 
qxl_fbdev_init(struct qxl_device * qdev)364 int qxl_fbdev_init(struct qxl_device *qdev)
365 {
366 	int ret = 0;
367 
368 #ifdef CONFIG_DRM_FBDEV_EMULATION
369 	struct qxl_fbdev *qfbdev;
370 	int bpp_sel = 32; /* TODO: parameter from somewhere? */
371 
372 	qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
373 	if (!qfbdev)
374 		return -ENOMEM;
375 
376 	qfbdev->qdev = qdev;
377 	qdev->mode_info.qfbdev = qfbdev;
378 	spin_lock_init(&qfbdev->delayed_ops_lock);
379 	INIT_LIST_HEAD(&qfbdev->delayed_ops);
380 
381 	drm_fb_helper_prepare(&qdev->ddev, &qfbdev->helper,
382 			      &qxl_fb_helper_funcs);
383 
384 	ret = drm_fb_helper_init(&qdev->ddev, &qfbdev->helper,
385 				 QXLFB_CONN_LIMIT);
386 	if (ret)
387 		goto free;
388 
389 	ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
390 	if (ret)
391 		goto fini;
392 
393 	ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
394 	if (ret)
395 		goto fini;
396 
397 	return 0;
398 
399 fini:
400 	drm_fb_helper_fini(&qfbdev->helper);
401 free:
402 	kfree(qfbdev);
403 #endif
404 
405 	return ret;
406 }
407 
qxl_fbdev_fini(struct qxl_device * qdev)408 void qxl_fbdev_fini(struct qxl_device *qdev)
409 {
410 	if (!qdev->mode_info.qfbdev)
411 		return;
412 
413 	qxl_fbdev_destroy(&qdev->ddev, qdev->mode_info.qfbdev);
414 	kfree(qdev->mode_info.qfbdev);
415 	qdev->mode_info.qfbdev = NULL;
416 }
417 
qxl_fbdev_set_suspend(struct qxl_device * qdev,int state)418 void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
419 {
420 	if (!qdev->mode_info.qfbdev)
421 		return;
422 
423 	drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
424 }
425 
qxl_fbdev_qobj_is_fb(struct qxl_device * qdev,struct qxl_bo * qobj)426 bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
427 {
428 	if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
429 		return true;
430 	return false;
431 }
432