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
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/>.
21 #include "drm_crtc_helper.h"
23 struct msm_framebuffer
{
24 struct drm_framebuffer base
;
25 const struct msm_format
*format
;
26 struct drm_gem_object
*planes
[2];
28 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
31 static int msm_framebuffer_create_handle(struct drm_framebuffer
*fb
,
32 struct drm_file
*file_priv
,
35 struct msm_framebuffer
*msm_fb
= to_msm_framebuffer(fb
);
36 return drm_gem_handle_create(file_priv
,
37 msm_fb
->planes
[0], handle
);
40 static void msm_framebuffer_destroy(struct drm_framebuffer
*fb
)
42 struct msm_framebuffer
*msm_fb
= to_msm_framebuffer(fb
);
43 int i
, n
= drm_format_num_planes(fb
->pixel_format
);
45 DBG("destroy: FB ID: %d (%p)", fb
->base
.id
, fb
);
47 drm_framebuffer_cleanup(fb
);
49 for (i
= 0; i
< n
; i
++) {
50 struct drm_gem_object
*bo
= msm_fb
->planes
[i
];
52 drm_gem_object_unreference_unlocked(bo
);
58 static int msm_framebuffer_dirty(struct drm_framebuffer
*fb
,
59 struct drm_file
*file_priv
, unsigned flags
, unsigned color
,
60 struct drm_clip_rect
*clips
, unsigned num_clips
)
65 static const struct drm_framebuffer_funcs msm_framebuffer_funcs
= {
66 .create_handle
= msm_framebuffer_create_handle
,
67 .destroy
= msm_framebuffer_destroy
,
68 .dirty
= msm_framebuffer_dirty
,
71 #ifdef CONFIG_DEBUG_FS
72 void msm_framebuffer_describe(struct drm_framebuffer
*fb
, struct seq_file
*m
)
74 struct msm_framebuffer
*msm_fb
= to_msm_framebuffer(fb
);
75 int i
, n
= drm_format_num_planes(fb
->pixel_format
);
77 seq_printf(m
, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
78 fb
->width
, fb
->height
, (char *)&fb
->pixel_format
,
79 fb
->refcount
.refcount
.counter
, fb
->base
.id
);
81 for (i
= 0; i
< n
; i
++) {
82 seq_printf(m
, " %d: offset=%d pitch=%d, obj: ",
83 i
, fb
->offsets
[i
], fb
->pitches
[i
]);
84 msm_gem_describe(msm_fb
->planes
[i
], m
);
89 struct drm_gem_object
*msm_framebuffer_bo(struct drm_framebuffer
*fb
, int plane
)
91 struct msm_framebuffer
*msm_fb
= to_msm_framebuffer(fb
);
92 return msm_fb
->planes
[plane
];
95 const struct msm_format
*msm_framebuffer_format(struct drm_framebuffer
*fb
)
97 struct msm_framebuffer
*msm_fb
= to_msm_framebuffer(fb
);
98 return msm_fb
->format
;
101 struct drm_framebuffer
*msm_framebuffer_create(struct drm_device
*dev
,
102 struct drm_file
*file
, struct drm_mode_fb_cmd2
*mode_cmd
)
104 struct drm_gem_object
*bos
[4] = {0};
105 struct drm_framebuffer
*fb
;
106 int ret
, i
, n
= drm_format_num_planes(mode_cmd
->pixel_format
);
108 for (i
= 0; i
< n
; i
++) {
109 bos
[i
] = drm_gem_object_lookup(dev
, file
,
110 mode_cmd
->handles
[i
]);
117 fb
= msm_framebuffer_init(dev
, mode_cmd
, bos
);
126 for (i
= 0; i
< n
; i
++)
127 drm_gem_object_unreference_unlocked(bos
[i
]);
131 struct drm_framebuffer
*msm_framebuffer_init(struct drm_device
*dev
,
132 struct drm_mode_fb_cmd2
*mode_cmd
, struct drm_gem_object
**bos
)
134 struct msm_drm_private
*priv
= dev
->dev_private
;
135 struct msm_kms
*kms
= priv
->kms
;
136 struct msm_framebuffer
*msm_fb
;
137 struct drm_framebuffer
*fb
= NULL
;
138 const struct msm_format
*format
;
140 unsigned int hsub
, vsub
;
142 DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
143 dev
, mode_cmd
, mode_cmd
->width
, mode_cmd
->height
,
144 (char *)&mode_cmd
->pixel_format
);
146 n
= drm_format_num_planes(mode_cmd
->pixel_format
);
147 hsub
= drm_format_horz_chroma_subsampling(mode_cmd
->pixel_format
);
148 vsub
= drm_format_vert_chroma_subsampling(mode_cmd
->pixel_format
);
150 format
= kms
->funcs
->get_format(kms
, mode_cmd
->pixel_format
);
152 dev_err(dev
->dev
, "unsupported pixel format: %4.4s\n",
153 (char *)&mode_cmd
->pixel_format
);
158 msm_fb
= kzalloc(sizeof(*msm_fb
), GFP_KERNEL
);
166 msm_fb
->format
= format
;
168 for (i
= 0; i
< n
; i
++) {
169 unsigned int width
= mode_cmd
->width
/ (i
? hsub
: 1);
170 unsigned int height
= mode_cmd
->height
/ (i
? vsub
: 1);
171 unsigned int min_size
;
173 min_size
= (height
- 1) * mode_cmd
->pitches
[i
]
174 + width
* drm_format_plane_cpp(mode_cmd
->pixel_format
, i
)
175 + mode_cmd
->offsets
[i
];
177 if (bos
[i
]->size
< min_size
) {
182 msm_fb
->planes
[i
] = bos
[i
];
185 drm_helper_mode_fill_fb_struct(fb
, mode_cmd
);
187 ret
= drm_framebuffer_init(dev
, fb
, &msm_framebuffer_funcs
);
189 dev_err(dev
->dev
, "framebuffer init failed: %d\n", ret
);
193 DBG("create: FB ID: %d (%p)", fb
->base
.id
, fb
);
199 msm_framebuffer_destroy(fb
);