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> |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 21 | #include <uapi/drm/exynos_drm.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 22 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 23 | #include "exynos_drm_drv.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 24 | #include "exynos_drm_fb.h" |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 25 | #include "exynos_drm_fbdev.h" |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 26 | #include "exynos_drm_iommu.h" |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 27 | #include "exynos_drm_crtc.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 28 | |
| 29 | #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb) |
| 30 | |
| 31 | /* |
| 32 | * exynos specific framebuffer structure. |
| 33 | * |
| 34 | * @fb: drm framebuffer obejct. |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 35 | * @exynos_gem: array of exynos specific gem object containing a gem object. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 36 | */ |
| 37 | struct exynos_drm_fb { |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 38 | struct drm_framebuffer fb; |
| 39 | struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER]; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 40 | }; |
| 41 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 42 | static int check_fb_gem_memory_type(struct drm_device *drm_dev, |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 43 | struct exynos_drm_gem *exynos_gem) |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 44 | { |
| 45 | unsigned int flags; |
| 46 | |
| 47 | /* |
| 48 | * if exynos drm driver supports iommu then framebuffer can use |
| 49 | * all the buffer types. |
| 50 | */ |
| 51 | if (is_drm_iommu_supported(drm_dev)) |
| 52 | return 0; |
| 53 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 54 | flags = exynos_gem->flags; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * without iommu support, not support physically non-continuous memory |
| 58 | * for framebuffer. |
| 59 | */ |
| 60 | if (IS_NONCONTIG_BUFFER(flags)) { |
| 61 | DRM_ERROR("cannot use this gem memory type for fb.\n"); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 68 | static void exynos_drm_fb_destroy(struct drm_framebuffer *fb) |
| 69 | { |
| 70 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 71 | unsigned int i; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 72 | |
Inki Dae | 1daa892 | 2012-11-22 17:41:23 +0900 | [diff] [blame] | 73 | /* make sure that overlay data are updated before relesing fb. */ |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 74 | exynos_drm_crtc_complete_scanout(fb); |
Inki Dae | 1daa892 | 2012-11-22 17:41:23 +0900 | [diff] [blame] | 75 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 76 | drm_framebuffer_cleanup(fb); |
| 77 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 78 | for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) { |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 79 | struct drm_gem_object *obj; |
| 80 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 81 | if (exynos_fb->exynos_gem[i] == NULL) |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 82 | continue; |
| 83 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 84 | obj = &exynos_fb->exynos_gem[i]->base; |
Laurent Pinchart | 07b6835 | 2012-05-16 17:08:56 +0200 | [diff] [blame] | 85 | drm_gem_object_unreference_unlocked(obj); |
| 86 | } |
| 87 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 88 | kfree(exynos_fb); |
| 89 | exynos_fb = NULL; |
| 90 | } |
| 91 | |
| 92 | static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb, |
| 93 | struct drm_file *file_priv, |
| 94 | unsigned int *handle) |
| 95 | { |
| 96 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
| 97 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 98 | return drm_gem_handle_create(file_priv, |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 99 | &exynos_fb->exynos_gem[0]->base, handle); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int exynos_drm_fb_dirty(struct drm_framebuffer *fb, |
| 103 | struct drm_file *file_priv, unsigned flags, |
| 104 | unsigned color, struct drm_clip_rect *clips, |
| 105 | unsigned num_clips) |
| 106 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 107 | /* TODO */ |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static struct drm_framebuffer_funcs exynos_drm_fb_funcs = { |
| 113 | .destroy = exynos_drm_fb_destroy, |
| 114 | .create_handle = exynos_drm_fb_create_handle, |
| 115 | .dirty = exynos_drm_fb_dirty, |
| 116 | }; |
| 117 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 118 | struct drm_framebuffer * |
| 119 | exynos_drm_framebuffer_init(struct drm_device *dev, |
| 120 | struct drm_mode_fb_cmd2 *mode_cmd, |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 121 | struct exynos_drm_gem **exynos_gem, |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 122 | int count) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 123 | { |
| 124 | struct exynos_drm_fb *exynos_fb; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 125 | int i; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 126 | int ret; |
| 127 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 128 | exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL); |
Sachin Kamat | 38bb525 | 2013-08-19 19:04:55 +0900 | [diff] [blame] | 129 | if (!exynos_fb) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 130 | return ERR_PTR(-ENOMEM); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 131 | |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 132 | for (i = 0; i < count; i++) { |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 133 | ret = check_fb_gem_memory_type(dev, exynos_gem[i]); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 134 | if (ret < 0) |
| 135 | goto err; |
| 136 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 137 | exynos_fb->exynos_gem[i] = exynos_gem[i]; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd); |
Joonyoung Shim | 94e30d9 | 2015-09-01 16:22:47 +0900 | [diff] [blame] | 141 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 142 | ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 143 | if (ret < 0) { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 144 | DRM_ERROR("failed to initialize framebuffer\n"); |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 145 | goto err; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 146 | } |
| 147 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 148 | return &exynos_fb->fb; |
Joonyoung Shim | d56125a | 2015-09-01 16:22:52 +0900 | [diff] [blame] | 149 | |
| 150 | err: |
| 151 | kfree(exynos_fb); |
| 152 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 155 | static struct drm_framebuffer * |
| 156 | exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv, |
| 157 | struct drm_mode_fb_cmd2 *mode_cmd) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 158 | { |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 159 | struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER]; |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 160 | struct drm_gem_object *obj; |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 161 | struct drm_framebuffer *fb; |
| 162 | int i; |
| 163 | int ret; |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 164 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 165 | for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) { |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 166 | obj = drm_gem_object_lookup(dev, file_priv, |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 167 | mode_cmd->handles[i]); |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 168 | if (!obj) { |
| 169 | DRM_ERROR("failed to lookup gem object\n"); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 170 | ret = -ENOENT; |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 171 | goto err; |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 174 | exynos_gem[i] = to_exynos_gem(obj); |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 177 | fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i); |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 178 | if (IS_ERR(fb)) { |
| 179 | ret = PTR_ERR(fb); |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 180 | goto err; |
Daniel Vetter | f2c0095 | 2012-12-14 13:39:03 +0900 | [diff] [blame] | 181 | } |
| 182 | |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 183 | return fb; |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 184 | |
Joonyoung Shim | dcbb85a | 2015-09-01 16:22:51 +0900 | [diff] [blame] | 185 | err: |
Joonyoung Shim | 8d31758 | 2015-09-01 16:22:53 +0900 | [diff] [blame] | 186 | while (i--) |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 187 | drm_gem_object_unreference_unlocked(&exynos_gem[i]->base); |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 188 | |
YoungJun Cho | 979c0c7 | 2013-02-12 21:23:54 +0900 | [diff] [blame] | 189 | return ERR_PTR(ret); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 192 | struct exynos_drm_gem *exynos_drm_fb_gem(struct drm_framebuffer *fb, int index) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 193 | { |
| 194 | struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb); |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 195 | struct exynos_drm_gem *exynos_gem; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 196 | |
Seung-Woo Kim | 229d353 | 2011-12-15 14:36:22 +0900 | [diff] [blame] | 197 | if (index >= MAX_FB_BUFFER) |
| 198 | return NULL; |
| 199 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 200 | exynos_gem = exynos_fb->exynos_gem[index]; |
| 201 | if (!exynos_gem) |
Inki Dae | 19c8b83 | 2011-10-14 13:29:46 +0900 | [diff] [blame] | 202 | return NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 203 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 204 | DRM_DEBUG_KMS("dma_addr: 0x%lx\n", (unsigned long)exynos_gem->dma_addr); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 205 | |
Joonyoung Shim | 813fd67b | 2015-10-02 09:33:47 +0900 | [diff] [blame] | 206 | return exynos_gem; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 209 | static void exynos_drm_output_poll_changed(struct drm_device *dev) |
| 210 | { |
| 211 | struct exynos_drm_private *private = dev->dev_private; |
| 212 | struct drm_fb_helper *fb_helper = private->fb_helper; |
| 213 | |
| 214 | if (fb_helper) |
| 215 | drm_fb_helper_hotplug_event(fb_helper); |
Andrzej Hajda | 25928a3 | 2014-03-17 11:27:17 +0100 | [diff] [blame] | 216 | else |
| 217 | exynos_drm_fbdev_init(dev); |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 218 | } |
| 219 | |
Laurent Pinchart | e6ecefa | 2012-05-17 13:27:23 +0200 | [diff] [blame] | 220 | static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = { |
Joonyoung Shim | e1533c0 | 2011-12-13 14:46:57 +0900 | [diff] [blame] | 221 | .fb_create = exynos_user_fb_create, |
Seung-Woo Kim | 7db3eba | 2011-10-18 16:58:05 +0900 | [diff] [blame] | 222 | .output_poll_changed = exynos_drm_output_poll_changed, |
Gustavo Padovan | 910874a | 2015-06-01 12:04:46 -0300 | [diff] [blame] | 223 | .atomic_check = drm_atomic_helper_check, |
Gustavo Padovan | 9d5ab6a | 2015-06-01 12:04:48 -0300 | [diff] [blame] | 224 | .atomic_commit = exynos_atomic_commit, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | void exynos_drm_mode_config_init(struct drm_device *dev) |
| 228 | { |
| 229 | dev->mode_config.min_width = 0; |
| 230 | dev->mode_config.min_height = 0; |
| 231 | |
| 232 | /* |
| 233 | * set max width and height as default value(4096x4096). |
| 234 | * this value would be used to check framebuffer size limitation |
| 235 | * at drm_mode_addfb(). |
| 236 | */ |
| 237 | dev->mode_config.max_width = 4096; |
| 238 | dev->mode_config.max_height = 4096; |
| 239 | |
| 240 | dev->mode_config.funcs = &exynos_drm_mode_config_funcs; |
| 241 | } |