drm_fb_cma_helper.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * drm kms/fb cma (contiguous memory allocator) helper functions
 *
 * Copyright (C) 2012 Analog Device Inc.
 *   Author: Lars-Peter Clausen <lars@metafoo.de>
 *
 * Based on udl_fbdev.c
 *  Copyright (C) 2012 Red Hat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <drm/drmP.h>
#include <drm/drm_fb_helper.h>
22
#include <drm/drm_framebuffer.h>
23
#include <drm/drm_gem_cma_helper.h>
24
#include <drm/drm_gem_framebuffer_helper.h>
25 26 27
#include <drm/drm_fb_cma_helper.h>
#include <linux/module.h>

28 29
#define DEFAULT_FBDEFIO_DELAY_MS 50

30 31
struct drm_fbdev_cma {
	struct drm_fb_helper	fb_helper;
32
	const struct drm_framebuffer_funcs *fb_funcs;
33 34
};

35 36 37 38 39 40
/**
 * DOC: framebuffer cma helper functions
 *
 * Provides helper functions for creating a cma (contiguous memory allocator)
 * backed framebuffer.
 *
41
 * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
42
 * callback function to create a cma backed framebuffer.
43 44 45
 *
 * An fbdev framebuffer backed by cma is also available by calling
 * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
46 47 48
 * If the &drm_framebuffer_funcs.dirty callback is set, fb_deferred_io will be
 * set up automatically. &drm_framebuffer_funcs.dirty is called by
 * drm_fb_helper_deferred_io() in process context (&struct delayed_work).
49
 *
50
 * Example fbdev deferred io code::
51
 *
52 53 54 55 56
 *     static int driver_fb_dirty(struct drm_framebuffer *fb,
 *                                struct drm_file *file_priv,
 *                                unsigned flags, unsigned color,
 *                                struct drm_clip_rect *clips,
 *                                unsigned num_clips)
57 58 59 60 61 62
 *     {
 *         struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
 *         ... push changes ...
 *         return 0;
 *     }
 *
63
 *     static struct drm_framebuffer_funcs driver_fb_funcs = {
64 65
 *         .destroy       = drm_gem_fb_destroy,
 *         .create_handle = drm_gem_fb_create_handle,
66
 *         .dirty         = driver_fb_dirty,
67 68
 *     };
 *
69
 * Initialize::
70 71 72 73
 *
 *     fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
 *                                           dev->mode_config.num_crtc,
 *                                           dev->mode_config.num_connector,
74
 *                                           &driver_fb_funcs);
75 76 77
 *
 */

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
{
	return container_of(helper, struct drm_fbdev_cma, fb_helper);
}

/**
 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
 * @fb: The framebuffer
 * @plane: Which plane
 *
 * Return the CMA GEM object for given framebuffer.
 *
 * This function will usually be called from the CRTC callback functions.
 */
struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
93
						  unsigned int plane)
94
{
95
	struct drm_gem_object *gem;
96

97 98
	gem = drm_gem_fb_get_obj(fb, plane);
	if (!gem)
99 100
		return NULL;

101
	return to_drm_gem_cma_obj(gem);
102 103 104
}
EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);

105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
 * @fb: The framebuffer
 * @state: Which state of drm plane
 * @plane: Which plane
 * Return the CMA GEM address for given framebuffer.
 *
 * This function will usually be called from the PLANE callback functions.
 */
dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
				   struct drm_plane_state *state,
				   unsigned int plane)
{
118
	struct drm_gem_cma_object *obj;
119 120
	dma_addr_t paddr;

121 122
	obj = drm_fb_cma_get_gem_obj(fb, plane);
	if (!obj)
123 124
		return 0;

125
	paddr = obj->paddr + fb->offsets[plane];
126 127 128 129 130 131 132
	paddr += fb->format->cpp[plane] * (state->src_x >> 16);
	paddr += fb->pitches[plane] * (state->src_y >> 16);

	return paddr;
}
EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);

133 134 135 136 137 138
static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
{
	return dma_mmap_writecombine(info->device, vma, info->screen_base,
				     info->fix.smem_start, info->fix.smem_len);
}

139 140
static struct fb_ops drm_fbdev_cma_ops = {
	.owner		= THIS_MODULE,
141
	DRM_FB_HELPER_DEFAULT_OPS,
142 143 144
	.fb_fillrect	= drm_fb_helper_sys_fillrect,
	.fb_copyarea	= drm_fb_helper_sys_copyarea,
	.fb_imageblit	= drm_fb_helper_sys_imageblit,
145
	.fb_mmap	= drm_fb_cma_mmap,
146 147
};

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
					  struct vm_area_struct *vma)
{
	fb_deferred_io_mmap(info, vma);
	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);

	return 0;
}

static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
				    struct drm_gem_cma_object *cma_obj)
{
	struct fb_deferred_io *fbdefio;
	struct fb_ops *fbops;

	/*
	 * Per device structures are needed because:
	 * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
	 * fbdefio: individual delays
	 */
	fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
	fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
	if (!fbdefio || !fbops) {
		kfree(fbdefio);
S
Sudip Mukherjee 已提交
172
		kfree(fbops);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		return -ENOMEM;
	}

	/* can't be offset from vaddr since dirty() uses cma_obj */
	fbi->screen_buffer = cma_obj->vaddr;
	/* fb_deferred_io_fault() needs a physical address */
	fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));

	*fbops = *fbi->fbops;
	fbi->fbops = fbops;

	fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
	fbdefio->deferred_io = drm_fb_helper_deferred_io;
	fbi->fbdefio = fbdefio;
	fb_deferred_io_init(fbi);
	fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;

	return 0;
}

static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
{
	if (!fbi->fbdefio)
		return;

	fb_deferred_io_cleanup(fbi);
	kfree(fbi->fbdefio);
	kfree(fbi->fbops);
}

203 204 205
static int
drm_fbdev_cma_create(struct drm_fb_helper *helper,
	struct drm_fb_helper_surface_size *sizes)
206 207 208 209 210 211 212 213 214 215 216
{
	struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
	struct drm_device *dev = helper->dev;
	struct drm_gem_cma_object *obj;
	struct drm_framebuffer *fb;
	unsigned int bytes_per_pixel;
	unsigned long offset;
	struct fb_info *fbi;
	size_t size;
	int ret;

217
	DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
218 219 220 221
			sizes->surface_width, sizes->surface_height,
			sizes->surface_bpp);

	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
222
	size = sizes->surface_width * sizes->surface_height * bytes_per_pixel;
223
	obj = drm_gem_cma_create(dev, size);
224
	if (IS_ERR(obj))
225 226
		return -ENOMEM;

227 228 229
	fbi = drm_fb_helper_alloc_fbi(helper);
	if (IS_ERR(fbi)) {
		ret = PTR_ERR(fbi);
230
		goto err_gem_free_object;
231 232
	}

233 234 235
	fb = drm_gem_fbdev_fb_create(dev, sizes, 0, &obj->base,
				     fbdev_cma->fb_funcs);
	if (IS_ERR(fb)) {
236
		dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
237
		ret = PTR_ERR(fb);
238
		goto err_fb_info_destroy;
239 240 241 242 243 244 245 246
	}

	helper->fb = fb;

	fbi->par = helper;
	fbi->flags = FBINFO_FLAG_DEFAULT;
	fbi->fbops = &drm_fbdev_cma_ops;

V
Ville Syrjälä 已提交
247
	drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
R
Rob Clark 已提交
248
	drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
249 250 251 252 253 254 255 256 257 258

	offset = fbi->var.xoffset * bytes_per_pixel;
	offset += fbi->var.yoffset * fb->pitches[0];

	dev->mode_config.fb_base = (resource_size_t)obj->paddr;
	fbi->screen_base = obj->vaddr + offset;
	fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
	fbi->screen_size = size;
	fbi->fix.smem_len = size;

259
	if (fbdev_cma->fb_funcs->dirty) {
260 261 262 263 264
		ret = drm_fbdev_cma_defio_init(fbi, obj);
		if (ret)
			goto err_cma_destroy;
	}

265 266
	return 0;

267
err_cma_destroy:
268
	drm_framebuffer_remove(fb);
269
err_fb_info_destroy:
270
	drm_fb_helper_fini(helper);
271
err_gem_free_object:
272
	drm_gem_object_put_unlocked(&obj->base);
273 274 275
	return ret;
}

276
static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
277
	.fb_probe = drm_fbdev_cma_create,
278 279 280
};

/**
281
 * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
282 283 284
 * @dev: DRM device
 * @preferred_bpp: Preferred bits per pixel for the device
 * @max_conn_count: Maximum number of connectors
285
 * @funcs: fb helper functions, in particular a custom dirty() callback
286 287 288
 *
 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
 */
289
struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
290 291
	unsigned int preferred_bpp, unsigned int max_conn_count,
	const struct drm_framebuffer_funcs *funcs)
292 293 294 295 296 297 298 299 300 301
{
	struct drm_fbdev_cma *fbdev_cma;
	struct drm_fb_helper *helper;
	int ret;

	fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
	if (!fbdev_cma) {
		dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
		return ERR_PTR(-ENOMEM);
	}
302
	fbdev_cma->fb_funcs = funcs;
303 304 305

	helper = &fbdev_cma->fb_helper;

306
	drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
307

308
	ret = drm_fb_helper_init(dev, helper, max_conn_count);
309 310 311 312 313 314 315 316 317 318 319 320 321 322
	if (ret < 0) {
		dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
		goto err_free;
	}

	ret = drm_fb_helper_single_add_all_connectors(helper);
	if (ret < 0) {
		dev_err(dev->dev, "Failed to add connectors.\n");
		goto err_drm_fb_helper_fini;

	}

	ret = drm_fb_helper_initial_config(helper, preferred_bpp);
	if (ret < 0) {
M
Masanari Iida 已提交
323
		dev_err(dev->dev, "Failed to set initial hw configuration.\n");
324 325 326 327 328 329 330 331 332 333 334 335
		goto err_drm_fb_helper_fini;
	}

	return fbdev_cma;

err_drm_fb_helper_fini:
	drm_fb_helper_fini(helper);
err_free:
	kfree(fbdev_cma);

	return ERR_PTR(ret);
}
336 337
EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);

338 339 340 341 342
static const struct drm_framebuffer_funcs drm_fb_cma_funcs = {
	.destroy	= drm_gem_fb_destroy,
	.create_handle	= drm_gem_fb_create_handle,
};

343 344 345 346 347 348 349 350 351
/**
 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
 * @dev: DRM device
 * @preferred_bpp: Preferred bits per pixel for the device
 * @max_conn_count: Maximum number of connectors
 *
 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
 */
struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
352
	unsigned int preferred_bpp, unsigned int max_conn_count)
353
{
354 355 356
	return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
					     max_conn_count,
					     &drm_fb_cma_funcs);
357
}
358 359 360 361 362 363 364 365
EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);

/**
 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
 * @fbdev_cma: The drm_fbdev_cma struct
 */
void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
{
366
	drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
367 368
	if (fbdev_cma->fb_helper.fbdev)
		drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
369

370 371
	if (fbdev_cma->fb_helper.fb)
		drm_framebuffer_remove(fbdev_cma->fb_helper.fb);
372 373 374 375 376 377 378 379 380 381

	drm_fb_helper_fini(&fbdev_cma->fb_helper);
	kfree(fbdev_cma);
}
EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);

/**
 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
 *
382
 * This function is usually called from the &drm_driver.lastclose callback.
383 384 385
 */
void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
{
386 387
	if (fbdev_cma)
		drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
388 389 390 391 392 393 394
}
EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);

/**
 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
 *
395
 * This function is usually called from the &drm_mode_config.output_poll_changed
396 397 398 399 400 401 402 403
 * callback.
 */
void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
{
	if (fbdev_cma)
		drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
}
EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
404 405 406 407 408 409 410 411 412

/**
 * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
 * @state: desired state, zero to resume, non-zero to suspend
 *
 * Calls drm_fb_helper_set_suspend, which is a wrapper around
 * fb_set_suspend implemented by fbdev core.
 */
413
void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
414 415 416 417 418
{
	if (fbdev_cma)
		drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
}
EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
419 420 421 422 423 424 425 426 427 428 429

/**
 * drm_fbdev_cma_set_suspend_unlocked - wrapper around
 *                                      drm_fb_helper_set_suspend_unlocked
 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
 * @state: desired state, zero to resume, non-zero to suspend
 *
 * Calls drm_fb_helper_set_suspend, which is a wrapper around
 * fb_set_suspend implemented by fbdev core.
 */
void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
430
					bool state)
431 432 433 434 435 436
{
	if (fbdev_cma)
		drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
						   state);
}
EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);