drm/radeon: add a dpm quirk for all R7 370 parts
[linux/fpc-iii.git] / drivers / gpu / drm / msm / msm_fb.c
blob81bafdf19ab39fc32428e6f5be692c597a6a8276
1 /*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "msm_drv.h"
19 #include "msm_kms.h"
21 #include "drm_crtc.h"
22 #include "drm_crtc_helper.h"
24 struct msm_framebuffer {
25 struct drm_framebuffer base;
26 const struct msm_format *format;
27 struct drm_gem_object *planes[2];
29 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
32 static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
33 struct drm_file *file_priv,
34 unsigned int *handle)
36 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
37 return drm_gem_handle_create(file_priv,
38 msm_fb->planes[0], handle);
41 static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
43 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
44 int i, n = drm_format_num_planes(fb->pixel_format);
46 DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
48 drm_framebuffer_cleanup(fb);
50 for (i = 0; i < n; i++) {
51 struct drm_gem_object *bo = msm_fb->planes[i];
52 if (bo)
53 drm_gem_object_unreference_unlocked(bo);
56 kfree(msm_fb);
59 static int msm_framebuffer_dirty(struct drm_framebuffer *fb,
60 struct drm_file *file_priv, unsigned flags, unsigned color,
61 struct drm_clip_rect *clips, unsigned num_clips)
63 return 0;
66 static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
67 .create_handle = msm_framebuffer_create_handle,
68 .destroy = msm_framebuffer_destroy,
69 .dirty = msm_framebuffer_dirty,
72 #ifdef CONFIG_DEBUG_FS
73 void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
75 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
76 int i, n = drm_format_num_planes(fb->pixel_format);
78 seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
79 fb->width, fb->height, (char *)&fb->pixel_format,
80 fb->refcount.refcount.counter, fb->base.id);
82 for (i = 0; i < n; i++) {
83 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
84 i, fb->offsets[i], fb->pitches[i]);
85 msm_gem_describe(msm_fb->planes[i], m);
88 #endif
90 struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
92 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
93 return msm_fb->planes[plane];
96 const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
98 struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
99 return msm_fb->format;
102 struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
103 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
105 struct drm_gem_object *bos[4] = {0};
106 struct drm_framebuffer *fb;
107 int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
109 for (i = 0; i < n; i++) {
110 bos[i] = drm_gem_object_lookup(dev, file,
111 mode_cmd->handles[i]);
112 if (!bos[i]) {
113 ret = -ENXIO;
114 goto out_unref;
118 fb = msm_framebuffer_init(dev, mode_cmd, bos);
119 if (IS_ERR(fb)) {
120 ret = PTR_ERR(fb);
121 goto out_unref;
124 return fb;
126 out_unref:
127 for (i = 0; i < n; i++)
128 drm_gem_object_unreference_unlocked(bos[i]);
129 return ERR_PTR(ret);
132 struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
133 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
135 struct msm_drm_private *priv = dev->dev_private;
136 struct msm_kms *kms = priv->kms;
137 struct msm_framebuffer *msm_fb;
138 struct drm_framebuffer *fb = NULL;
139 const struct msm_format *format;
140 int ret, i, n;
141 unsigned int hsub, vsub;
143 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
144 dev, mode_cmd, mode_cmd->width, mode_cmd->height,
145 (char *)&mode_cmd->pixel_format);
147 n = drm_format_num_planes(mode_cmd->pixel_format);
148 hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
149 vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
151 format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
152 if (!format) {
153 dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
154 (char *)&mode_cmd->pixel_format);
155 ret = -EINVAL;
156 goto fail;
159 msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
160 if (!msm_fb) {
161 ret = -ENOMEM;
162 goto fail;
165 fb = &msm_fb->base;
167 msm_fb->format = format;
169 for (i = 0; i < n; i++) {
170 unsigned int width = mode_cmd->width / (i ? hsub : 1);
171 unsigned int height = mode_cmd->height / (i ? vsub : 1);
172 unsigned int min_size;
174 min_size = (height - 1) * mode_cmd->pitches[i]
175 + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
176 + mode_cmd->offsets[i];
178 if (bos[i]->size < min_size) {
179 ret = -EINVAL;
180 goto fail;
183 msm_fb->planes[i] = bos[i];
186 drm_helper_mode_fill_fb_struct(fb, mode_cmd);
188 ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
189 if (ret) {
190 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
191 goto fail;
194 DBG("create: FB ID: %d (%p)", fb->base.id, fb);
196 return fb;
198 fail:
199 if (fb)
200 msm_framebuffer_destroy(fb);
202 return ERR_PTR(ret);