1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drm kms/fb cma (contiguous memory allocator) helper functions
5 * Copyright (C) 2012 Analog Device Inc.
9 * Copyright (C) 2012 Red Hat
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_framebuffer.h>
14 #include <drm/drm_gem_cma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_plane.h>
17 #include <linux/module.h>
20 * DOC: framebuffer cma helper functions
22 * Provides helper functions for creating a cma (contiguous memory allocator)
25 * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
26 * callback function to create a cma backed framebuffer.
30 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
31 * @fb: The framebuffer
34 * Return the CMA GEM object for given framebuffer.
36 * This function will usually be called from the CRTC callback functions.
38 struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
41 struct drm_gem_object *gem;
43 gem = drm_gem_fb_get_obj(fb, plane);
47 return to_drm_gem_cma_obj(gem);
49 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
52 * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer, for pixel
53 * formats where values are grouped in blocks this will get you the beginning of
55 * @fb: The framebuffer
56 * @state: Which state of drm plane
58 * Return the CMA GEM address for given framebuffer.
60 * This function will usually be called from the PLANE callback functions.
62 dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
63 struct drm_plane_state *state,
66 struct drm_gem_cma_object *obj;
68 u8 h_div = 1, v_div = 1;
69 u32 block_w = drm_format_info_block_width(fb->format, plane);
70 u32 block_h = drm_format_info_block_height(fb->format, plane);
71 u32 block_size = fb->format->char_per_block[plane];
77 obj = drm_fb_cma_get_gem_obj(fb, plane);
81 paddr = obj->paddr + fb->offsets[plane];
84 h_div = fb->format->hsub;
85 v_div = fb->format->vsub;
88 sample_x = (state->src_x >> 16) / h_div;
89 sample_y = (state->src_y >> 16) / v_div;
90 block_start_y = (sample_y / block_h) * block_h;
91 num_hblocks = sample_x / block_w;
93 paddr += fb->pitches[plane] * block_start_y;
94 paddr += block_size * num_hblocks;
98 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);