xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/qxl/qxl_dumb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2013 Red Hat Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*4882a593Smuzhiyun  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*4882a593Smuzhiyun  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*4882a593Smuzhiyun  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*4882a593Smuzhiyun  * OTHER DEALINGS IN THE SOFTWARE.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Authors: Dave Airlie
23*4882a593Smuzhiyun  *          Alon Levy
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include "qxl_drv.h"
27*4882a593Smuzhiyun #include "qxl_object.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* dumb ioctls implementation */
30*4882a593Smuzhiyun 
qxl_mode_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)31*4882a593Smuzhiyun int qxl_mode_dumb_create(struct drm_file *file_priv,
32*4882a593Smuzhiyun 			    struct drm_device *dev,
33*4882a593Smuzhiyun 			    struct drm_mode_create_dumb *args)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	struct qxl_device *qdev = to_qxl(dev);
36*4882a593Smuzhiyun 	struct qxl_bo *qobj;
37*4882a593Smuzhiyun 	uint32_t handle;
38*4882a593Smuzhiyun 	int r;
39*4882a593Smuzhiyun 	struct qxl_surface surf;
40*4882a593Smuzhiyun 	uint32_t pitch, format;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	pitch = args->width * ((args->bpp + 1) / 8);
43*4882a593Smuzhiyun 	args->size = pitch * args->height;
44*4882a593Smuzhiyun 	args->size = ALIGN(args->size, PAGE_SIZE);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	switch (args->bpp) {
47*4882a593Smuzhiyun 	case 16:
48*4882a593Smuzhiyun 		format = SPICE_SURFACE_FMT_16_565;
49*4882a593Smuzhiyun 		break;
50*4882a593Smuzhiyun 	case 32:
51*4882a593Smuzhiyun 		format = SPICE_SURFACE_FMT_32_xRGB;
52*4882a593Smuzhiyun 		break;
53*4882a593Smuzhiyun 	default:
54*4882a593Smuzhiyun 		return -EINVAL;
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	surf.width = args->width;
58*4882a593Smuzhiyun 	surf.height = args->height;
59*4882a593Smuzhiyun 	surf.stride = pitch;
60*4882a593Smuzhiyun 	surf.format = format;
61*4882a593Smuzhiyun 	surf.data = 0;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	r = qxl_gem_object_create_with_handle(qdev, file_priv,
64*4882a593Smuzhiyun 					      QXL_GEM_DOMAIN_SURFACE,
65*4882a593Smuzhiyun 					      args->size, &surf, &qobj,
66*4882a593Smuzhiyun 					      &handle);
67*4882a593Smuzhiyun 	if (r)
68*4882a593Smuzhiyun 		return r;
69*4882a593Smuzhiyun 	qobj->is_dumb = true;
70*4882a593Smuzhiyun 	args->pitch = pitch;
71*4882a593Smuzhiyun 	args->handle = handle;
72*4882a593Smuzhiyun 	return 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
qxl_mode_dumb_mmap(struct drm_file * file_priv,struct drm_device * dev,uint32_t handle,uint64_t * offset_p)75*4882a593Smuzhiyun int qxl_mode_dumb_mmap(struct drm_file *file_priv,
76*4882a593Smuzhiyun 		       struct drm_device *dev,
77*4882a593Smuzhiyun 		       uint32_t handle, uint64_t *offset_p)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	struct drm_gem_object *gobj;
80*4882a593Smuzhiyun 	struct qxl_bo *qobj;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	BUG_ON(!offset_p);
83*4882a593Smuzhiyun 	gobj = drm_gem_object_lookup(file_priv, handle);
84*4882a593Smuzhiyun 	if (gobj == NULL)
85*4882a593Smuzhiyun 		return -ENOENT;
86*4882a593Smuzhiyun 	qobj = gem_to_qxl_bo(gobj);
87*4882a593Smuzhiyun 	*offset_p = qxl_bo_mmap_offset(qobj);
88*4882a593Smuzhiyun 	drm_gem_object_put(gobj);
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91