1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drm kms/fb dma helper functions
5 * Copyright (C) 2012 Analog Devices Inc.
9 * Copyright (C) 2012 Red Hat
12 #include <drm/drm_damage_helper.h>
13 #include <drm/drm_fb_dma_helper.h>
14 #include <drm/drm_fourcc.h>
15 #include <drm/drm_framebuffer.h>
16 #include <drm/drm_gem_dma_helper.h>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_panic.h>
19 #include <drm/drm_plane.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/module.h>
24 * DOC: framebuffer dma helper functions
26 * Provides helper functions for creating a DMA-contiguous framebuffer.
28 * Depending on the platform, the buffers may be physically non-contiguous and
29 * mapped through an IOMMU or a similar mechanism, or allocated from
30 * physically-contiguous memory (using, for instance, CMA or a pool of memory
31 * reserved at early boot). This is handled behind the scenes by the DMA mapping
34 * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
35 * callback function to create a DMA-contiguous framebuffer.
39 * drm_fb_dma_get_gem_obj() - Get DMA GEM object for framebuffer
40 * @fb: The framebuffer
43 * Return the DMA GEM object for given framebuffer.
45 * This function will usually be called from the CRTC callback functions.
47 struct drm_gem_dma_object *drm_fb_dma_get_gem_obj(struct drm_framebuffer *fb,
50 struct drm_gem_object *gem;
52 gem = drm_gem_fb_get_obj(fb, plane);
56 return to_drm_gem_dma_obj(gem);
58 EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_obj);
61 * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for framebuffer, for pixel
62 * formats where values are grouped in blocks this will get you the beginning of
64 * @fb: The framebuffer
65 * @state: Which state of drm plane
67 * Return the DMA GEM address for given framebuffer.
69 * This function will usually be called from the PLANE callback functions.
71 dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
72 struct drm_plane_state *state,
75 struct drm_gem_dma_object *obj;
77 u8 h_div = 1, v_div = 1;
78 u32 block_w = drm_format_info_block_width(fb->format, plane);
79 u32 block_h = drm_format_info_block_height(fb->format, plane);
80 u32 block_size = fb->format->char_per_block[plane];
86 obj = drm_fb_dma_get_gem_obj(fb, plane);
90 dma_addr = obj->dma_addr + fb->offsets[plane];
93 h_div = fb->format->hsub;
94 v_div = fb->format->vsub;
97 sample_x = (state->src_x >> 16) / h_div;
98 sample_y = (state->src_y >> 16) / v_div;
99 block_start_y = (sample_y / block_h) * block_h;
100 num_hblocks = sample_x / block_w;
102 dma_addr += fb->pitches[plane] * block_start_y;
103 dma_addr += block_size * num_hblocks;
107 EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);
110 * drm_fb_dma_sync_non_coherent - Sync GEM object to non-coherent backing
113 * @old_state: Old plane state
114 * @state: New plane state
116 * This function can be used by drivers that use damage clips and have
117 * DMA GEM objects backed by non-coherent memory. Calling this function
118 * in a plane's .atomic_update ensures that all the data in the backing
119 * memory have been written to RAM.
121 void drm_fb_dma_sync_non_coherent(struct drm_device *drm,
122 struct drm_plane_state *old_state,
123 struct drm_plane_state *state)
125 const struct drm_format_info *finfo = state->fb->format;
126 struct drm_atomic_helper_damage_iter iter;
127 const struct drm_gem_dma_object *dma_obj;
128 unsigned int offset, i;
129 struct drm_rect clip;
133 for (i = 0; i < finfo->num_planes; i++) {
134 dma_obj = drm_fb_dma_get_gem_obj(state->fb, i);
135 if (!dma_obj->map_noncoherent)
138 daddr = drm_fb_dma_get_gem_addr(state->fb, state, i);
139 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
141 drm_atomic_for_each_plane_damage(&iter, &clip) {
142 /* Ignore x1/x2 values, invalidate complete lines */
143 offset = clip.y1 * state->fb->pitches[i];
145 nb_bytes = (clip.y2 - clip.y1) * state->fb->pitches[i];
146 dma_sync_single_for_device(drm->dev, daddr + offset,
147 nb_bytes, DMA_TO_DEVICE);
151 EXPORT_SYMBOL_GPL(drm_fb_dma_sync_non_coherent);
154 * drm_fb_dma_get_scanout_buffer - Provide a scanout buffer in case of panic
155 * @plane: DRM primary plane
156 * @sb: scanout buffer for the panic handler
157 * Returns: 0 or negative error code
159 * Generic get_scanout_buffer() implementation, for drivers that uses the
160 * drm_fb_dma_helper. It won't call vmap in the panic context, so the driver
161 * should make sure the primary plane is vmapped, otherwise the panic screen
162 * won't get displayed.
164 int drm_fb_dma_get_scanout_buffer(struct drm_plane *plane,
165 struct drm_scanout_buffer *sb)
167 struct drm_gem_dma_object *dma_obj;
168 struct drm_framebuffer *fb;
170 if (!plane->state || !plane->state->fb)
173 fb = plane->state->fb;
174 /* Only support linear modifier */
175 if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
178 dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
180 /* Buffer should be accessible from the CPU */
181 if (dma_obj->base.import_attach)
184 /* Buffer should be already mapped to CPU */
188 iosys_map_set_vaddr(&sb->map[0], dma_obj->vaddr);
189 sb->format = fb->format;
190 sb->height = fb->height;
191 sb->width = fb->width;
192 sb->pitch[0] = fb->pitches[0];
195 EXPORT_SYMBOL(drm_fb_dma_get_scanout_buffer);