Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_fb.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Authors: |
| 5 | * Inki Dae <inki.dae@samsung.com> |
| 6 | * Joonyoung Shim <jy0922.shim@samsung.com> |
| 7 | * Seung-Woo Kim <sw0312.kim@samsung.com> |
| 8 | * |
Inki Dae | d81aecb | 2012-12-18 02:30:17 +0900 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 13 | */ |
| 14 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 15 | #include <drm/drmP.h> |
| 16 | #include <drm/drm_crtc.h> |
| 17 | #include <drm/drm_crtc_helper.h> |
| 18 | #include <drm/drm_fb_helper.h> |
Gustavo Padovan | d6562a2 | 2015-06-01 12:04:52 -0300 | [diff] [blame] | 19 | #include <drm/drm_atomic.h> |
Gustavo Padovan | 910874a | 2015-06-01 12:04:46 -0300 | [diff] [blame] | 20 | #include <drm/drm_atomic_helper.h> |
Daniel Stone | b11954a | 2018-03-30 15:11:30 +0100 | [diff] [blame] | 21 | #include <drm/drm_gem_framebuffer_helper.h> |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 22 | #include <uapi/drm/exynos_drm.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 23 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 24 | #include "exynos_drm_drv.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 25 | #include "exynos_drm_fb.h" |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 26 | #include "exynos_drm_fbdev.h" |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 27 | #include "exynos_drm_iommu.h" |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 28 | #include "exynos_drm_crtc.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 29 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 30 | static int check_fb_gem_memory_type(struct drm_device *drm_dev, |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 31 | struct exynos_drm_gem *exynos_gem) |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 32 | { |
| 33 | unsigned int flags; |
| 34 | |
| 35 | /* |
| 36 | * if exynos drm driver supports iommu then framebuffer can use |
| 37 | * all the buffer types. |
| 38 | */ |
| 39 | if (is_drm_iommu_supported(drm_dev)) |
| 40 | return 0; |
| 41 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 42 | flags = exynos_gem->flags; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
Shuah Khan | 6244bd6 | 2016-08-08 17:48:20 -0600 | [diff] [blame] | 45 | * Physically non-contiguous memory type for framebuffer is not |
| 46 | * supported without IOMMU. |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 47 | */ |
| 48 | if (IS_NONCONTIG_BUFFER(flags)) { |
Shuah Khan | 6244bd6 | 2016-08-08 17:48:20 -0600 | [diff] [blame] | 49 | DRM_ERROR("Non-contiguous GEM memory is not supported.\n"); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 50 | return -EINVAL; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Ville Syrjälä | 800ba2b | 2015-12-15 12:21:06 +0100 | [diff] [blame] | 56 | static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = { |
Daniel Stone | b11954a | 2018-03-30 15:11:30 +0100 | [diff] [blame] | 57 | .destroy = drm_gem_fb_destroy, |
| 58 | .create_handle = drm_gem_fb_create_handle, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 61 | struct drm_framebuffer * |
| 62 | exynos_drm_framebuffer_init(struct drm_device *dev, |
Ville Syrjälä | 1eb83451 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 63 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 64 | struct exynos_drm_gem **exynos_gem, |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 65 | int count) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 66 | { |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 67 | struct drm_framebuffer *fb; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 68 | int i; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 69 | int ret; |
| 70 | |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 71 | fb = kzalloc(sizeof(*fb), GFP_KERNEL); |
| 72 | if (!fb) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 73 | return ERR_PTR(-ENOMEM); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 74 | |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 75 | for (i = 0; i < count; i++) { |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 76 | ret = check_fb_gem_memory_type(dev, exynos_gem[i]); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 77 | if (ret < 0) |
| 78 | goto err; |
| 79 | |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 80 | fb->obj[i] = &exynos_gem[i]->base; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 83 | drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd); |
Joonyoung Shim | 94e30d9 | 2015-09-01 16:22:47 +0900 | [diff] [blame] | 84 | |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 85 | ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 86 | if (ret < 0) { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 87 | DRM_ERROR("failed to initialize framebuffer\n"); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 88 | goto err; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 91 | return fb; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 92 | |
| 93 | err: |
Daniel Stone | ff059fc | 2018-03-30 15:11:32 +0100 | [diff] [blame] | 94 | kfree(fb); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 95 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 98 | static struct drm_framebuffer * |
| 99 | exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
Ville Syrjälä | 1eb83451 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 100 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 101 | { |
Marek Szyprowski | 1899bd5 | 2017-07-12 12:09:22 +0200 | [diff] [blame] | 102 | const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd); |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 103 | struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER]; |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 104 | struct drm_framebuffer *fb; |
| 105 | int i; |
| 106 | int ret; |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 107 | |
Marek Szyprowski | 1899bd5 | 2017-07-12 12:09:22 +0200 | [diff] [blame] | 108 | for (i = 0; i < info->num_planes; i++) { |
| 109 | unsigned int height = (i == 0) ? mode_cmd->height : |
| 110 | DIV_ROUND_UP(mode_cmd->height, info->vsub); |
| 111 | unsigned long size = height * mode_cmd->pitches[i] + |
| 112 | mode_cmd->offsets[i]; |
| 113 | |
Marek Szyprowski | e978de5 | 2018-07-09 15:44:32 +0200 | [diff] [blame] | 114 | exynos_gem[i] = exynos_drm_gem_get(file_priv, |
| 115 | mode_cmd->handles[i]); |
| 116 | if (!exynos_gem[i]) { |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 117 | DRM_ERROR("failed to lookup gem object\n"); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 118 | ret = -ENOENT; |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 119 | goto err; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 120 | } |
| 121 | |
Marek Szyprowski | 1899bd5 | 2017-07-12 12:09:22 +0200 | [diff] [blame] | 122 | if (size > exynos_gem[i]->size) { |
| 123 | i++; |
| 124 | ret = -EINVAL; |
| 125 | goto err; |
| 126 | } |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 129 | fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i); |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 130 | if (IS_ERR(fb)) { |
| 131 | ret = PTR_ERR(fb); |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 132 | goto err; |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 135 | return fb; |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 136 | |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 137 | err: |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 138 | while (i--) |
Marek Szyprowski | e978de5 | 2018-07-09 15:44:32 +0200 | [diff] [blame] | 139 | exynos_drm_gem_put(exynos_gem[i]); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 140 | |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 141 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Marek Szyprowski | 0488f50 | 2015-11-30 14:53:21 +0100 | [diff] [blame] | 144 | dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 145 | { |
Daniel Stone | 7b30508 | 2018-03-30 15:11:31 +0100 | [diff] [blame] | 146 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 147 | |
Christoph Hellwig | e0c7a51 | 2017-05-22 10:40:52 +0200 | [diff] [blame] | 148 | if (WARN_ON_ONCE(index >= MAX_FB_BUFFER)) |
| 149 | return 0; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 150 | |
Daniel Stone | 7b30508 | 2018-03-30 15:11:31 +0100 | [diff] [blame] | 151 | exynos_gem = to_exynos_gem(fb->obj[index]); |
| 152 | return exynos_gem->dma_addr + fb->offsets[index]; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Inki Dae | 41cbf0f | 2017-01-20 12:51:41 +0900 | [diff] [blame] | 155 | static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = { |
Maxime Ripard | 81a099a | 2017-07-20 15:01:16 +0200 | [diff] [blame] | 156 | .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, |
Inki Dae | 41cbf0f | 2017-01-20 12:51:41 +0900 | [diff] [blame] | 157 | }; |
| 158 | |
Laurent Pinchart | e6ecefa | 2012-05-17 13:27:23 +0200 | [diff] [blame] | 159 | static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 160 | .fb_create = exynos_user_fb_create, |
Noralf Trønnes | d2936153 | 2017-12-05 19:24:57 +0100 | [diff] [blame] | 161 | .output_poll_changed = drm_fb_helper_output_poll_changed, |
Peter Ujfalusi | a7da5cf | 2018-03-21 12:20:25 +0200 | [diff] [blame] | 162 | .atomic_check = drm_atomic_helper_check, |
Inki Dae | 41cbf0f | 2017-01-20 12:51:41 +0900 | [diff] [blame] | 163 | .atomic_commit = drm_atomic_helper_commit, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | void exynos_drm_mode_config_init(struct drm_device *dev) |
| 167 | { |
| 168 | dev->mode_config.min_width = 0; |
| 169 | dev->mode_config.min_height = 0; |
| 170 | |
| 171 | /* |
| 172 | * set max width and height as default value(4096x4096). |
| 173 | * this value would be used to check framebuffer size limitation |
| 174 | * at drm_mode_addfb(). |
| 175 | */ |
| 176 | dev->mode_config.max_width = 4096; |
| 177 | dev->mode_config.max_height = 4096; |
| 178 | |
| 179 | dev->mode_config.funcs = &exynos_drm_mode_config_funcs; |
Inki Dae | 41cbf0f | 2017-01-20 12:51:41 +0900 | [diff] [blame] | 180 | dev->mode_config.helper_private = &exynos_drm_mode_config_helpers; |
Tobias Jakobi | f40031c | 2017-08-22 16:19:37 +0200 | [diff] [blame] | 181 | |
| 182 | dev->mode_config.allow_fb_modifiers = true; |
Peter Ujfalusi | a7da5cf | 2018-03-21 12:20:25 +0200 | [diff] [blame] | 183 | |
| 184 | dev->mode_config.normalize_zpos = true; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 185 | } |