Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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/drmP.h> |
Noralf Trønnes | 894a677 | 2018-07-03 18:03:50 +0200 | [diff] [blame] | 21 | #include <drm/drm_client.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 22 | #include <drm/drm_fb_helper.h> |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 23 | #include <drm/drm_framebuffer.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 24 | #include <drm/drm_gem_cma_helper.h> |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 25 | #include <drm/drm_gem_framebuffer_helper.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 26 | #include <drm/drm_fb_cma_helper.h> |
Noralf Trønnes | 41b676e | 2017-11-15 15:19:41 +0100 | [diff] [blame] | 27 | #include <drm/drm_print.h> |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 30 | struct drm_fbdev_cma { |
| 31 | struct drm_fb_helper fb_helper; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 34 | /** |
| 35 | * DOC: framebuffer cma helper functions |
| 36 | * |
| 37 | * Provides helper functions for creating a cma (contiguous memory allocator) |
| 38 | * backed framebuffer. |
| 39 | * |
Noralf Trønnes | c0f095f | 2017-09-24 14:26:25 +0200 | [diff] [blame] | 40 | * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create |
Noralf Trønnes | 02da16d | 2016-05-11 18:09:18 +0200 | [diff] [blame] | 41 | * callback function to create a cma backed framebuffer. |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 42 | * |
| 43 | * An fbdev framebuffer backed by cma is also available by calling |
Noralf Trønnes | 41b676e | 2017-11-15 15:19:41 +0100 | [diff] [blame] | 44 | * drm_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down. |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 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 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 52 | /** |
| 53 | * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer |
| 54 | * @fb: The framebuffer |
| 55 | * @plane: Which plane |
| 56 | * |
| 57 | * Return the CMA GEM object for given framebuffer. |
| 58 | * |
| 59 | * This function will usually be called from the CRTC callback functions. |
| 60 | */ |
| 61 | struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb, |
Daniel Vetter | 890358a | 2016-05-31 23:11:12 +0200 | [diff] [blame] | 62 | unsigned int plane) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 63 | { |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 64 | struct drm_gem_object *gem; |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 65 | |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 66 | gem = drm_gem_fb_get_obj(fb, plane); |
| 67 | if (!gem) |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 68 | return NULL; |
| 69 | |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 70 | return to_drm_gem_cma_obj(gem); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 71 | } |
| 72 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj); |
| 73 | |
Marek Vasut | 14d7f96 | 2016-11-14 11:07:31 +0100 | [diff] [blame] | 74 | /** |
Alexandru Gheorghe | 042bf75 | 2018-11-01 17:02:05 +0000 | [diff] [blame^] | 75 | * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer, for pixel |
| 76 | * formats where values are grouped in blocks this will get you the beginning of |
| 77 | * the block |
Yannick Fertre | 4636ce9 | 2017-04-14 12:13:32 +0200 | [diff] [blame] | 78 | * @fb: The framebuffer |
| 79 | * @state: Which state of drm plane |
| 80 | * @plane: Which plane |
| 81 | * Return the CMA GEM address for given framebuffer. |
| 82 | * |
| 83 | * This function will usually be called from the PLANE callback functions. |
| 84 | */ |
| 85 | dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb, |
| 86 | struct drm_plane_state *state, |
| 87 | unsigned int plane) |
| 88 | { |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 89 | struct drm_gem_cma_object *obj; |
Yannick Fertre | 4636ce9 | 2017-04-14 12:13:32 +0200 | [diff] [blame] | 90 | dma_addr_t paddr; |
Ayan Kumar Halder | c76abab | 2018-08-17 17:54:00 +0100 | [diff] [blame] | 91 | u8 h_div = 1, v_div = 1; |
Alexandru Gheorghe | 042bf75 | 2018-11-01 17:02:05 +0000 | [diff] [blame^] | 92 | u32 block_w = drm_format_info_block_width(fb->format, plane); |
| 93 | u32 block_h = drm_format_info_block_height(fb->format, plane); |
| 94 | u32 block_size = fb->format->char_per_block[plane]; |
| 95 | u32 sample_x; |
| 96 | u32 sample_y; |
| 97 | u32 block_start_y; |
| 98 | u32 num_hblocks; |
Yannick Fertre | 4636ce9 | 2017-04-14 12:13:32 +0200 | [diff] [blame] | 99 | |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 100 | obj = drm_fb_cma_get_gem_obj(fb, plane); |
| 101 | if (!obj) |
Yannick Fertre | 4636ce9 | 2017-04-14 12:13:32 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 104 | paddr = obj->paddr + fb->offsets[plane]; |
Ayan Kumar Halder | c76abab | 2018-08-17 17:54:00 +0100 | [diff] [blame] | 105 | |
| 106 | if (plane > 0) { |
| 107 | h_div = fb->format->hsub; |
| 108 | v_div = fb->format->vsub; |
| 109 | } |
| 110 | |
Alexandru Gheorghe | 042bf75 | 2018-11-01 17:02:05 +0000 | [diff] [blame^] | 111 | sample_x = (state->src_x >> 16) / h_div; |
| 112 | sample_y = (state->src_y >> 16) / v_div; |
| 113 | block_start_y = (sample_y / block_h) * block_h; |
| 114 | num_hblocks = sample_x / block_w; |
| 115 | |
| 116 | paddr += fb->pitches[plane] * block_start_y; |
| 117 | paddr += block_size * num_hblocks; |
Yannick Fertre | 4636ce9 | 2017-04-14 12:13:32 +0200 | [diff] [blame] | 118 | |
| 119 | return paddr; |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr); |
| 122 | |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 123 | /** |
Noralf Trønnes | 41b676e | 2017-11-15 15:19:41 +0100 | [diff] [blame] | 124 | * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation |
| 125 | * @dev: DRM device |
| 126 | * @preferred_bpp: Preferred bits per pixel for the device. |
| 127 | * @dev->mode_config.preferred_depth is used if this is zero. |
| 128 | * @max_conn_count: Maximum number of connectors. |
| 129 | * @dev->mode_config.num_connector is used if this is zero. |
| 130 | * |
| 131 | * Returns: |
| 132 | * Zero on success or negative error code on failure. |
| 133 | */ |
| 134 | int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp, |
| 135 | unsigned int max_conn_count) |
| 136 | { |
Noralf Trønnes | 894a677 | 2018-07-03 18:03:50 +0200 | [diff] [blame] | 137 | struct drm_fbdev_cma *fbdev_cma; |
| 138 | |
| 139 | /* dev->fb_helper will indirectly point to fbdev_cma after this call */ |
| 140 | fbdev_cma = drm_fbdev_cma_init(dev, preferred_bpp, max_conn_count); |
YueHaibing | 2a7be4b | 2018-10-10 13:02:43 +0000 | [diff] [blame] | 141 | return PTR_ERR_OR_ZERO(fbdev_cma); |
Noralf Trønnes | 41b676e | 2017-11-15 15:19:41 +0100 | [diff] [blame] | 142 | } |
| 143 | EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init); |
| 144 | |
| 145 | /** |
| 146 | * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation |
| 147 | * @dev: DRM device |
| 148 | */ |
| 149 | void drm_fb_cma_fbdev_fini(struct drm_device *dev) |
| 150 | { |
Noralf Trønnes | 894a677 | 2018-07-03 18:03:50 +0200 | [diff] [blame] | 151 | if (dev->fb_helper) |
| 152 | drm_fbdev_cma_fini(to_fbdev_cma(dev->fb_helper)); |
Noralf Trønnes | 41b676e | 2017-11-15 15:19:41 +0100 | [diff] [blame] | 153 | } |
| 154 | EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini); |
| 155 | |
Noralf Trønnes | 894a677 | 2018-07-03 18:03:50 +0200 | [diff] [blame] | 156 | static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = { |
| 157 | .fb_probe = drm_fb_helper_generic_probe, |
Noralf Trønnes | 5628648 | 2017-08-13 15:31:45 +0200 | [diff] [blame] | 158 | }; |
| 159 | |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 160 | /** |
| 161 | * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct |
| 162 | * @dev: DRM device |
| 163 | * @preferred_bpp: Preferred bits per pixel for the device |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 164 | * @max_conn_count: Maximum number of connectors |
| 165 | * |
| 166 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. |
| 167 | */ |
| 168 | struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev, |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 169 | unsigned int preferred_bpp, unsigned int max_conn_count) |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 170 | { |
Noralf Trønnes | 894a677 | 2018-07-03 18:03:50 +0200 | [diff] [blame] | 171 | struct drm_fbdev_cma *fbdev_cma; |
| 172 | struct drm_fb_helper *fb_helper; |
| 173 | int ret; |
| 174 | |
| 175 | fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL); |
| 176 | if (!fbdev_cma) |
| 177 | return ERR_PTR(-ENOMEM); |
| 178 | |
| 179 | fb_helper = &fbdev_cma->fb_helper; |
| 180 | |
| 181 | ret = drm_client_new(dev, &fb_helper->client, "fbdev", NULL); |
| 182 | if (ret) |
| 183 | goto err_free; |
| 184 | |
| 185 | ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_cma_helper_funcs, |
| 186 | preferred_bpp, max_conn_count); |
| 187 | if (ret) |
| 188 | goto err_client_put; |
| 189 | |
| 190 | return fbdev_cma; |
| 191 | |
| 192 | err_client_put: |
| 193 | drm_client_release(&fb_helper->client); |
| 194 | err_free: |
| 195 | kfree(fbdev_cma); |
| 196 | |
| 197 | return ERR_PTR(ret); |
Noralf Trønnes | 199c771 | 2016-04-28 17:18:35 +0200 | [diff] [blame] | 198 | } |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 199 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_init); |
| 200 | |
| 201 | /** |
| 202 | * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct |
| 203 | * @fbdev_cma: The drm_fbdev_cma struct |
| 204 | */ |
| 205 | void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma) |
| 206 | { |
Archit Taneja | 85f2edf | 2015-07-22 14:58:20 +0530 | [diff] [blame] | 207 | drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper); |
Noralf Trønnes | 894a677 | 2018-07-03 18:03:50 +0200 | [diff] [blame] | 208 | /* All resources have now been freed by drm_fbdev_fb_destroy() */ |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 209 | } |
| 210 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini); |
| 211 | |
| 212 | /** |
| 213 | * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode |
| 214 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
| 215 | * |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 216 | * This function is usually called from the &drm_driver.lastclose callback. |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 217 | */ |
| 218 | void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma) |
| 219 | { |
Rob Clark | 5ea1f75 | 2014-05-30 12:29:48 -0400 | [diff] [blame] | 220 | if (fbdev_cma) |
| 221 | drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper); |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 222 | } |
| 223 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode); |
| 224 | |
| 225 | /** |
| 226 | * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events |
| 227 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL |
| 228 | * |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 229 | * This function is usually called from the &drm_mode_config.output_poll_changed |
Lars-Peter Clausen | 2e3b3c4 | 2012-07-02 16:37:47 +0200 | [diff] [blame] | 230 | * callback. |
| 231 | */ |
| 232 | void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma) |
| 233 | { |
| 234 | if (fbdev_cma) |
| 235 | drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper); |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event); |