Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.9.
  1/*
  2 * Copyright 2012 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
  6 * "Software"), to deal in the Software without restriction, including
  7 * without limitation the rights to use, copy, modify, merge, publish,
  8 * distribute, sub license, and/or sell copies of the Software, and to
  9 * permit persons to whom the Software is furnished to do so, subject to
 10 * the following conditions:
 11 *
 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 19 *
 20 * The above copyright notice and this permission notice (including the
 21 * next paragraph) shall be included in all copies or substantial portions
 22 * of the Software.
 23 *
 24 */
 25/*
 26 * Authors: Dave Airlie <airlied@redhat.com>
 27 */
 28#include <linux/module.h>
 29#include <linux/kernel.h>
 30#include <linux/errno.h>
 31#include <linux/string.h>
 32#include <linux/mm.h>
 33#include <linux/tty.h>
 34#include <linux/sysrq.h>
 35#include <linux/delay.h>
 36#include <linux/fb.h>
 37#include <linux/init.h>
 38
 39
 40#include "drmP.h"
 41#include "drm.h"
 42#include "drm_crtc.h"
 43#include "drm_fb_helper.h"
 44#include "ast_drv.h"
 45
 46static void ast_dirty_update(struct ast_fbdev *afbdev,
 47			     int x, int y, int width, int height)
 48{
 49	int i;
 50	struct drm_gem_object *obj;
 51	struct ast_bo *bo;
 52	int src_offset, dst_offset;
 53	int bpp = (afbdev->afb.base.bits_per_pixel + 7)/8;
 54	int ret;
 55	bool unmap = false;
 56
 57	obj = afbdev->afb.obj;
 58	bo = gem_to_ast_bo(obj);
 59
 60	ret = ast_bo_reserve(bo, true);
 61	if (ret) {
 62		DRM_ERROR("failed to reserve fb bo\n");
 63		return;
 64	}
 65
 66	if (!bo->kmap.virtual) {
 67		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
 68		if (ret) {
 69			DRM_ERROR("failed to kmap fb updates\n");
 70			ast_bo_unreserve(bo);
 71			return;
 72		}
 73		unmap = true;
 74	}
 75	for (i = y; i < y + height; i++) {
 76		/* assume equal stride for now */
 77		src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
 78		memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
 79
 80	}
 81	if (unmap)
 82		ttm_bo_kunmap(&bo->kmap);
 83
 84	ast_bo_unreserve(bo);
 85}
 86
 87static void ast_fillrect(struct fb_info *info,
 88			 const struct fb_fillrect *rect)
 89{
 90	struct ast_fbdev *afbdev = info->par;
 91	sys_fillrect(info, rect);
 92	ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
 93			 rect->height);
 94}
 95
 96static void ast_copyarea(struct fb_info *info,
 97			 const struct fb_copyarea *area)
 98{
 99	struct ast_fbdev *afbdev = info->par;
100	sys_copyarea(info, area);
101	ast_dirty_update(afbdev, area->dx, area->dy, area->width,
102			 area->height);
103}
104
105static void ast_imageblit(struct fb_info *info,
106			  const struct fb_image *image)
107{
108	struct ast_fbdev *afbdev = info->par;
109	sys_imageblit(info, image);
110	ast_dirty_update(afbdev, image->dx, image->dy, image->width,
111			 image->height);
112}
113
114static struct fb_ops astfb_ops = {
115	.owner = THIS_MODULE,
116	.fb_check_var = drm_fb_helper_check_var,
117	.fb_set_par = drm_fb_helper_set_par,
118	.fb_fillrect = ast_fillrect,
119	.fb_copyarea = ast_copyarea,
120	.fb_imageblit = ast_imageblit,
121	.fb_pan_display = drm_fb_helper_pan_display,
122	.fb_blank = drm_fb_helper_blank,
123	.fb_setcmap = drm_fb_helper_setcmap,
124	.fb_debug_enter = drm_fb_helper_debug_enter,
125	.fb_debug_leave = drm_fb_helper_debug_leave,
126};
127
128static int astfb_create_object(struct ast_fbdev *afbdev,
129			       struct drm_mode_fb_cmd2 *mode_cmd,
130			       struct drm_gem_object **gobj_p)
131{
132	struct drm_device *dev = afbdev->helper.dev;
133	u32 bpp, depth;
134	u32 size;
135	struct drm_gem_object *gobj;
136
137	int ret = 0;
138	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
139
140	size = mode_cmd->pitches[0] * mode_cmd->height;
141	ret = ast_gem_create(dev, size, true, &gobj);
142	if (ret)
143		return ret;
144
145	*gobj_p = gobj;
146	return ret;
147}
148
149static int astfb_create(struct ast_fbdev *afbdev,
150			struct drm_fb_helper_surface_size *sizes)
151{
152	struct drm_device *dev = afbdev->helper.dev;
153	struct drm_mode_fb_cmd2 mode_cmd;
154	struct drm_framebuffer *fb;
155	struct fb_info *info;
156	int size, ret;
157	struct device *device = &dev->pdev->dev;
158	void *sysram;
159	struct drm_gem_object *gobj = NULL;
160	struct ast_bo *bo = NULL;
161	mode_cmd.width = sizes->surface_width;
162	mode_cmd.height = sizes->surface_height;
163	mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
164
165	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
166							  sizes->surface_depth);
167
168	size = mode_cmd.pitches[0] * mode_cmd.height;
169
170	ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
171	if (ret) {
172		DRM_ERROR("failed to create fbcon backing object %d\n", ret);
173		return ret;
174	}
175	bo = gem_to_ast_bo(gobj);
176
177	sysram = vmalloc(size);
178	if (!sysram)
179		return -ENOMEM;
180
181	info = framebuffer_alloc(0, device);
182	if (!info) {
183		ret = -ENOMEM;
184		goto out;
185	}
186	info->par = afbdev;
187
188	ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
189	if (ret)
190		goto out;
191
192	afbdev->sysram = sysram;
193	afbdev->size = size;
194
195	fb = &afbdev->afb.base;
196	afbdev->helper.fb = fb;
197	afbdev->helper.fbdev = info;
198
199	strcpy(info->fix.id, "astdrmfb");
200
201	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
202	info->fbops = &astfb_ops;
203
204	ret = fb_alloc_cmap(&info->cmap, 256, 0);
205	if (ret) {
206		ret = -ENOMEM;
207		goto out;
208	}
209
210	info->apertures = alloc_apertures(1);
211	if (!info->apertures) {
212		ret = -ENOMEM;
213		goto out;
214	}
215	info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
216	info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
217
218	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
219	drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
220
221	info->screen_base = sysram;
222	info->screen_size = size;
223
224	info->pixmap.flags = FB_PIXMAP_SYSTEM;
225
226	DRM_DEBUG_KMS("allocated %dx%d\n",
227		      fb->width, fb->height);
228
229	return 0;
230out:
231	return ret;
232}
233
234static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
235			       u16 blue, int regno)
236{
237	struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
238	ast_crtc->lut_r[regno] = red >> 8;
239	ast_crtc->lut_g[regno] = green >> 8;
240	ast_crtc->lut_b[regno] = blue >> 8;
241}
242
243static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
244			       u16 *blue, int regno)
245{
246	struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
247	*red = ast_crtc->lut_r[regno] << 8;
248	*green = ast_crtc->lut_g[regno] << 8;
249	*blue = ast_crtc->lut_b[regno] << 8;
250}
251
252static int ast_find_or_create_single(struct drm_fb_helper *helper,
253					  struct drm_fb_helper_surface_size *sizes)
254{
255	struct ast_fbdev *afbdev = (struct ast_fbdev *)helper;
256	int new_fb = 0;
257	int ret;
258
259	if (!helper->fb) {
260		ret = astfb_create(afbdev, sizes);
261		if (ret)
262			return ret;
263		new_fb = 1;
264	}
265	return new_fb;
266}
267
268static struct drm_fb_helper_funcs ast_fb_helper_funcs = {
269	.gamma_set = ast_fb_gamma_set,
270	.gamma_get = ast_fb_gamma_get,
271	.fb_probe = ast_find_or_create_single,
272};
273
274static void ast_fbdev_destroy(struct drm_device *dev,
275			      struct ast_fbdev *afbdev)
276{
277	struct fb_info *info;
278	struct ast_framebuffer *afb = &afbdev->afb;
279	if (afbdev->helper.fbdev) {
280		info = afbdev->helper.fbdev;
281		unregister_framebuffer(info);
282		if (info->cmap.len)
283			fb_dealloc_cmap(&info->cmap);
284		framebuffer_release(info);
285	}
286
287	if (afb->obj) {
288		drm_gem_object_unreference_unlocked(afb->obj);
289		afb->obj = NULL;
290	}
291	drm_fb_helper_fini(&afbdev->helper);
292
293	vfree(afbdev->sysram);
294	drm_framebuffer_cleanup(&afb->base);
295}
296
297int ast_fbdev_init(struct drm_device *dev)
298{
299	struct ast_private *ast = dev->dev_private;
300	struct ast_fbdev *afbdev;
301	int ret;
302
303	afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
304	if (!afbdev)
305		return -ENOMEM;
306
307	ast->fbdev = afbdev;
308	afbdev->helper.funcs = &ast_fb_helper_funcs;
309	ret = drm_fb_helper_init(dev, &afbdev->helper,
310				 1, 1);
311	if (ret) {
312		kfree(afbdev);
313		return ret;
314	}
315
316	drm_fb_helper_single_add_all_connectors(&afbdev->helper);
317	drm_fb_helper_initial_config(&afbdev->helper, 32);
318	return 0;
319}
320
321void ast_fbdev_fini(struct drm_device *dev)
322{
323	struct ast_private *ast = dev->dev_private;
324
325	if (!ast->fbdev)
326		return;
327
328	ast_fbdev_destroy(dev, ast->fbdev);
329	kfree(ast->fbdev);
330	ast->fbdev = NULL;
331}
332
333void ast_fbdev_set_suspend(struct drm_device *dev, int state)
334{
335	struct ast_private *ast = dev->dev_private;
336
337	if (!ast->fbdev)
338		return;
339
340	fb_set_suspend(ast->fbdev->helper.fbdev, state);
341}