| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * drm kms/fb cma (contiguous memory allocator) helper functions |
|---|
| 3 | 4 | * |
|---|
| 4 | | - * Copyright (C) 2012 Analog Device Inc. |
|---|
| 5 | + * Copyright (C) 2012 Analog Devices Inc. |
|---|
| 5 | 6 | * Author: Lars-Peter Clausen <lars@metafoo.de> |
|---|
| 6 | 7 | * |
|---|
| 7 | 8 | * Based on udl_fbdev.c |
|---|
| 8 | 9 | * 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 | 10 | */ |
|---|
| 19 | 11 | |
|---|
| 20 | | -#include <drm/drmP.h> |
|---|
| 21 | | -#include <drm/drm_client.h> |
|---|
| 22 | | -#include <drm/drm_fb_helper.h> |
|---|
| 12 | +#include <drm/drm_fb_cma_helper.h> |
|---|
| 13 | +#include <drm/drm_fourcc.h> |
|---|
| 23 | 14 | #include <drm/drm_framebuffer.h> |
|---|
| 24 | 15 | #include <drm/drm_gem_cma_helper.h> |
|---|
| 25 | 16 | #include <drm/drm_gem_framebuffer_helper.h> |
|---|
| 26 | | -#include <drm/drm_fb_cma_helper.h> |
|---|
| 27 | | -#include <drm/drm_print.h> |
|---|
| 17 | +#include <drm/drm_plane.h> |
|---|
| 28 | 18 | #include <linux/module.h> |
|---|
| 29 | | - |
|---|
| 30 | | -struct drm_fbdev_cma { |
|---|
| 31 | | - struct drm_fb_helper fb_helper; |
|---|
| 32 | | -}; |
|---|
| 33 | 19 | |
|---|
| 34 | 20 | /** |
|---|
| 35 | 21 | * DOC: framebuffer cma helper functions |
|---|
| .. | .. |
|---|
| 39 | 25 | * |
|---|
| 40 | 26 | * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create |
|---|
| 41 | 27 | * callback function to create a cma backed framebuffer. |
|---|
| 42 | | - * |
|---|
| 43 | | - * An fbdev framebuffer backed by cma is also available by calling |
|---|
| 44 | | - * drm_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down. |
|---|
| 45 | 28 | */ |
|---|
| 46 | | - |
|---|
| 47 | | -static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper) |
|---|
| 48 | | -{ |
|---|
| 49 | | - return container_of(helper, struct drm_fbdev_cma, fb_helper); |
|---|
| 50 | | -} |
|---|
| 51 | 29 | |
|---|
| 52 | 30 | /** |
|---|
| 53 | 31 | * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer |
|---|
| .. | .. |
|---|
| 72 | 50 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj); |
|---|
| 73 | 51 | |
|---|
| 74 | 52 | /** |
|---|
| 75 | | - * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer |
|---|
| 53 | + * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer, for pixel |
|---|
| 54 | + * formats where values are grouped in blocks this will get you the beginning of |
|---|
| 55 | + * the block |
|---|
| 76 | 56 | * @fb: The framebuffer |
|---|
| 77 | 57 | * @state: Which state of drm plane |
|---|
| 78 | 58 | * @plane: Which plane |
|---|
| .. | .. |
|---|
| 86 | 66 | { |
|---|
| 87 | 67 | struct drm_gem_cma_object *obj; |
|---|
| 88 | 68 | dma_addr_t paddr; |
|---|
| 69 | + u8 h_div = 1, v_div = 1; |
|---|
| 70 | + u32 block_w = drm_format_info_block_width(fb->format, plane); |
|---|
| 71 | + u32 block_h = drm_format_info_block_height(fb->format, plane); |
|---|
| 72 | + u32 block_size = fb->format->char_per_block[plane]; |
|---|
| 73 | + u32 sample_x; |
|---|
| 74 | + u32 sample_y; |
|---|
| 75 | + u32 block_start_y; |
|---|
| 76 | + u32 num_hblocks; |
|---|
| 89 | 77 | |
|---|
| 90 | 78 | obj = drm_fb_cma_get_gem_obj(fb, plane); |
|---|
| 91 | 79 | if (!obj) |
|---|
| 92 | 80 | return 0; |
|---|
| 93 | 81 | |
|---|
| 94 | 82 | paddr = obj->paddr + fb->offsets[plane]; |
|---|
| 95 | | - paddr += fb->format->bpp[plane] / 8 * (state->src_x >> 16); |
|---|
| 96 | | - paddr += fb->pitches[plane] * (state->src_y >> 16); |
|---|
| 83 | + |
|---|
| 84 | + if (plane > 0) { |
|---|
| 85 | + h_div = fb->format->hsub; |
|---|
| 86 | + v_div = fb->format->vsub; |
|---|
| 87 | + } |
|---|
| 88 | + |
|---|
| 89 | + sample_x = (state->src_x >> 16) / h_div; |
|---|
| 90 | + sample_y = (state->src_y >> 16) / v_div; |
|---|
| 91 | + block_start_y = (sample_y / block_h) * block_h; |
|---|
| 92 | + num_hblocks = sample_x / block_w; |
|---|
| 93 | + |
|---|
| 94 | + paddr += fb->pitches[plane] * block_start_y; |
|---|
| 95 | + paddr += block_size * num_hblocks; |
|---|
| 97 | 96 | |
|---|
| 98 | 97 | return paddr; |
|---|
| 99 | 98 | } |
|---|
| 100 | 99 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr); |
|---|
| 101 | | - |
|---|
| 102 | | -/** |
|---|
| 103 | | - * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation |
|---|
| 104 | | - * @dev: DRM device |
|---|
| 105 | | - * @preferred_bpp: Preferred bits per pixel for the device. |
|---|
| 106 | | - * @dev->mode_config.preferred_depth is used if this is zero. |
|---|
| 107 | | - * @max_conn_count: Maximum number of connectors. |
|---|
| 108 | | - * @dev->mode_config.num_connector is used if this is zero. |
|---|
| 109 | | - * |
|---|
| 110 | | - * Returns: |
|---|
| 111 | | - * Zero on success or negative error code on failure. |
|---|
| 112 | | - */ |
|---|
| 113 | | -int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp, |
|---|
| 114 | | - unsigned int max_conn_count) |
|---|
| 115 | | -{ |
|---|
| 116 | | - struct drm_fbdev_cma *fbdev_cma; |
|---|
| 117 | | - |
|---|
| 118 | | - /* dev->fb_helper will indirectly point to fbdev_cma after this call */ |
|---|
| 119 | | - fbdev_cma = drm_fbdev_cma_init(dev, preferred_bpp, max_conn_count); |
|---|
| 120 | | - if (IS_ERR(fbdev_cma)) |
|---|
| 121 | | - return PTR_ERR(fbdev_cma); |
|---|
| 122 | | - |
|---|
| 123 | | - return 0; |
|---|
| 124 | | -} |
|---|
| 125 | | -EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init); |
|---|
| 126 | | - |
|---|
| 127 | | -/** |
|---|
| 128 | | - * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation |
|---|
| 129 | | - * @dev: DRM device |
|---|
| 130 | | - */ |
|---|
| 131 | | -void drm_fb_cma_fbdev_fini(struct drm_device *dev) |
|---|
| 132 | | -{ |
|---|
| 133 | | - if (dev->fb_helper) |
|---|
| 134 | | - drm_fbdev_cma_fini(to_fbdev_cma(dev->fb_helper)); |
|---|
| 135 | | -} |
|---|
| 136 | | -EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini); |
|---|
| 137 | | - |
|---|
| 138 | | -static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = { |
|---|
| 139 | | - .fb_probe = drm_fb_helper_generic_probe, |
|---|
| 140 | | -}; |
|---|
| 141 | | - |
|---|
| 142 | | -/** |
|---|
| 143 | | - * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct |
|---|
| 144 | | - * @dev: DRM device |
|---|
| 145 | | - * @preferred_bpp: Preferred bits per pixel for the device |
|---|
| 146 | | - * @max_conn_count: Maximum number of connectors |
|---|
| 147 | | - * |
|---|
| 148 | | - * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. |
|---|
| 149 | | - */ |
|---|
| 150 | | -struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev, |
|---|
| 151 | | - unsigned int preferred_bpp, unsigned int max_conn_count) |
|---|
| 152 | | -{ |
|---|
| 153 | | - struct drm_fbdev_cma *fbdev_cma; |
|---|
| 154 | | - struct drm_fb_helper *fb_helper; |
|---|
| 155 | | - int ret; |
|---|
| 156 | | - |
|---|
| 157 | | - fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL); |
|---|
| 158 | | - if (!fbdev_cma) |
|---|
| 159 | | - return ERR_PTR(-ENOMEM); |
|---|
| 160 | | - |
|---|
| 161 | | - fb_helper = &fbdev_cma->fb_helper; |
|---|
| 162 | | - |
|---|
| 163 | | - ret = drm_client_init(dev, &fb_helper->client, "fbdev", NULL); |
|---|
| 164 | | - if (ret) |
|---|
| 165 | | - goto err_free; |
|---|
| 166 | | - |
|---|
| 167 | | - ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_cma_helper_funcs, |
|---|
| 168 | | - preferred_bpp, max_conn_count); |
|---|
| 169 | | - if (ret) |
|---|
| 170 | | - goto err_client_put; |
|---|
| 171 | | - |
|---|
| 172 | | - drm_client_add(&fb_helper->client); |
|---|
| 173 | | - |
|---|
| 174 | | - return fbdev_cma; |
|---|
| 175 | | - |
|---|
| 176 | | -err_client_put: |
|---|
| 177 | | - drm_client_release(&fb_helper->client); |
|---|
| 178 | | -err_free: |
|---|
| 179 | | - kfree(fbdev_cma); |
|---|
| 180 | | - |
|---|
| 181 | | - return ERR_PTR(ret); |
|---|
| 182 | | -} |
|---|
| 183 | | -EXPORT_SYMBOL_GPL(drm_fbdev_cma_init); |
|---|
| 184 | | - |
|---|
| 185 | | -/** |
|---|
| 186 | | - * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct |
|---|
| 187 | | - * @fbdev_cma: The drm_fbdev_cma struct |
|---|
| 188 | | - */ |
|---|
| 189 | | -void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma) |
|---|
| 190 | | -{ |
|---|
| 191 | | - drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper); |
|---|
| 192 | | - /* All resources have now been freed by drm_fbdev_fb_destroy() */ |
|---|
| 193 | | -} |
|---|
| 194 | | -EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini); |
|---|
| 195 | | - |
|---|
| 196 | | -/** |
|---|
| 197 | | - * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode |
|---|
| 198 | | - * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
|---|
| 199 | | - * |
|---|
| 200 | | - * This function is usually called from the &drm_driver.lastclose callback. |
|---|
| 201 | | - */ |
|---|
| 202 | | -void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma) |
|---|
| 203 | | -{ |
|---|
| 204 | | - if (fbdev_cma) |
|---|
| 205 | | - drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper); |
|---|
| 206 | | -} |
|---|
| 207 | | -EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode); |
|---|
| 208 | | - |
|---|
| 209 | | -/** |
|---|
| 210 | | - * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events |
|---|
| 211 | | - * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
|---|
| 212 | | - * |
|---|
| 213 | | - * This function is usually called from the &drm_mode_config.output_poll_changed |
|---|
| 214 | | - * callback. |
|---|
| 215 | | - */ |
|---|
| 216 | | -void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma) |
|---|
| 217 | | -{ |
|---|
| 218 | | - if (fbdev_cma) |
|---|
| 219 | | - drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper); |
|---|
| 220 | | -} |
|---|
| 221 | | -EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event); |
|---|
| 222 | | - |
|---|
| 223 | | -/** |
|---|
| 224 | | - * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend |
|---|
| 225 | | - * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
|---|
| 226 | | - * @state: desired state, zero to resume, non-zero to suspend |
|---|
| 227 | | - * |
|---|
| 228 | | - * Calls drm_fb_helper_set_suspend, which is a wrapper around |
|---|
| 229 | | - * fb_set_suspend implemented by fbdev core. |
|---|
| 230 | | - */ |
|---|
| 231 | | -void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state) |
|---|
| 232 | | -{ |
|---|
| 233 | | - if (fbdev_cma) |
|---|
| 234 | | - drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state); |
|---|
| 235 | | -} |
|---|
| 236 | | -EXPORT_SYMBOL(drm_fbdev_cma_set_suspend); |
|---|
| 237 | | - |
|---|
| 238 | | -/** |
|---|
| 239 | | - * drm_fbdev_cma_set_suspend_unlocked - wrapper around |
|---|
| 240 | | - * drm_fb_helper_set_suspend_unlocked |
|---|
| 241 | | - * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
|---|
| 242 | | - * @state: desired state, zero to resume, non-zero to suspend |
|---|
| 243 | | - * |
|---|
| 244 | | - * Calls drm_fb_helper_set_suspend, which is a wrapper around |
|---|
| 245 | | - * fb_set_suspend implemented by fbdev core. |
|---|
| 246 | | - */ |
|---|
| 247 | | -void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma, |
|---|
| 248 | | - bool state) |
|---|
| 249 | | -{ |
|---|
| 250 | | - if (fbdev_cma) |
|---|
| 251 | | - drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper, |
|---|
| 252 | | - state); |
|---|
| 253 | | -} |
|---|
| 254 | | -EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked); |
|---|