Merge branch 'spi-5.1' into spi-5.2
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_fb_cma_helper.c
1 /*
2  * drm kms/fb cma (contiguous memory allocator) helper functions
3  *
4  * Copyright (C) 2012 Analog Device Inc.
5  *   Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Based on udl_fbdev.c
8  *  Copyright (C) 2012 Red Hat
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #include <drm/drm_fourcc.h>
21 #include <drm/drm_framebuffer.h>
22 #include <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_plane.h>
25 #include <linux/module.h>
26
27 /**
28  * DOC: framebuffer cma helper functions
29  *
30  * Provides helper functions for creating a cma (contiguous memory allocator)
31  * backed framebuffer.
32  *
33  * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
34  * callback function to create a cma backed framebuffer.
35  */
36
37 /**
38  * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
39  * @fb: The framebuffer
40  * @plane: Which plane
41  *
42  * Return the CMA GEM object for given framebuffer.
43  *
44  * This function will usually be called from the CRTC callback functions.
45  */
46 struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
47                                                   unsigned int plane)
48 {
49         struct drm_gem_object *gem;
50
51         gem = drm_gem_fb_get_obj(fb, plane);
52         if (!gem)
53                 return NULL;
54
55         return to_drm_gem_cma_obj(gem);
56 }
57 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
58
59 /**
60  * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer, for pixel
61  * formats where values are grouped in blocks this will get you the beginning of
62  * the block
63  * @fb: The framebuffer
64  * @state: Which state of drm plane
65  * @plane: Which plane
66  * Return the CMA GEM address for given framebuffer.
67  *
68  * This function will usually be called from the PLANE callback functions.
69  */
70 dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
71                                    struct drm_plane_state *state,
72                                    unsigned int plane)
73 {
74         struct drm_gem_cma_object *obj;
75         dma_addr_t paddr;
76         u8 h_div = 1, v_div = 1;
77         u32 block_w = drm_format_info_block_width(fb->format, plane);
78         u32 block_h = drm_format_info_block_height(fb->format, plane);
79         u32 block_size = fb->format->char_per_block[plane];
80         u32 sample_x;
81         u32 sample_y;
82         u32 block_start_y;
83         u32 num_hblocks;
84
85         obj = drm_fb_cma_get_gem_obj(fb, plane);
86         if (!obj)
87                 return 0;
88
89         paddr = obj->paddr + fb->offsets[plane];
90
91         if (plane > 0) {
92                 h_div = fb->format->hsub;
93                 v_div = fb->format->vsub;
94         }
95
96         sample_x = (state->src_x >> 16) / h_div;
97         sample_y = (state->src_y >> 16) / v_div;
98         block_start_y = (sample_y / block_h) * block_h;
99         num_hblocks = sample_x / block_w;
100
101         paddr += fb->pitches[plane] * block_start_y;
102         paddr += block_size * num_hblocks;
103
104         return paddr;
105 }
106 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);