1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6 * Inki Dae <inki.dae@samsung.com>
7 * Joonyoung Shim <jy0922.shim@samsung.com>
8 * Seung-Woo Kim <sw0312.kim@samsung.com>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_crtc.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_fourcc.h>
16 #include <drm/drm_gem_framebuffer_helper.h>
17 #include <drm/drm_probe_helper.h>
18 #include <drm/exynos_drm.h>
20 #include "exynos_drm_crtc.h"
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fb.h"
23 #include "exynos_drm_fbdev.h"
25 static int check_fb_gem_memory_type(struct drm_device
*drm_dev
,
26 struct exynos_drm_gem
*exynos_gem
)
31 * if exynos drm driver supports iommu then framebuffer can use
32 * all the buffer types.
34 if (is_drm_iommu_supported(drm_dev
))
37 flags
= exynos_gem
->flags
;
40 * Physically non-contiguous memory type for framebuffer is not
41 * supported without IOMMU.
43 if (IS_NONCONTIG_BUFFER(flags
)) {
44 DRM_DEV_ERROR(drm_dev
->dev
,
45 "Non-contiguous GEM memory is not supported.\n");
52 static const struct drm_framebuffer_funcs exynos_drm_fb_funcs
= {
53 .destroy
= drm_gem_fb_destroy
,
54 .create_handle
= drm_gem_fb_create_handle
,
57 struct drm_framebuffer
*
58 exynos_drm_framebuffer_init(struct drm_device
*dev
,
59 const struct drm_mode_fb_cmd2
*mode_cmd
,
60 struct exynos_drm_gem
**exynos_gem
,
63 struct drm_framebuffer
*fb
;
67 fb
= kzalloc(sizeof(*fb
), GFP_KERNEL
);
69 return ERR_PTR(-ENOMEM
);
71 for (i
= 0; i
< count
; i
++) {
72 ret
= check_fb_gem_memory_type(dev
, exynos_gem
[i
]);
76 fb
->obj
[i
] = &exynos_gem
[i
]->base
;
79 drm_helper_mode_fill_fb_struct(dev
, fb
, mode_cmd
);
81 ret
= drm_framebuffer_init(dev
, fb
, &exynos_drm_fb_funcs
);
83 DRM_DEV_ERROR(dev
->dev
,
84 "failed to initialize framebuffer\n");
95 static struct drm_framebuffer
*
96 exynos_user_fb_create(struct drm_device
*dev
, struct drm_file
*file_priv
,
97 const struct drm_mode_fb_cmd2
*mode_cmd
)
99 const struct drm_format_info
*info
= drm_get_format_info(dev
, mode_cmd
);
100 struct exynos_drm_gem
*exynos_gem
[MAX_FB_BUFFER
];
101 struct drm_framebuffer
*fb
;
105 for (i
= 0; i
< info
->num_planes
; i
++) {
106 unsigned int height
= (i
== 0) ? mode_cmd
->height
:
107 DIV_ROUND_UP(mode_cmd
->height
, info
->vsub
);
108 unsigned long size
= height
* mode_cmd
->pitches
[i
] +
109 mode_cmd
->offsets
[i
];
111 exynos_gem
[i
] = exynos_drm_gem_get(file_priv
,
112 mode_cmd
->handles
[i
]);
113 if (!exynos_gem
[i
]) {
114 DRM_DEV_ERROR(dev
->dev
,
115 "failed to lookup gem object\n");
120 if (size
> exynos_gem
[i
]->size
) {
127 fb
= exynos_drm_framebuffer_init(dev
, mode_cmd
, exynos_gem
, i
);
137 exynos_drm_gem_put(exynos_gem
[i
]);
142 dma_addr_t
exynos_drm_fb_dma_addr(struct drm_framebuffer
*fb
, int index
)
144 struct exynos_drm_gem
*exynos_gem
;
146 if (WARN_ON_ONCE(index
>= MAX_FB_BUFFER
))
149 exynos_gem
= to_exynos_gem(fb
->obj
[index
]);
150 return exynos_gem
->dma_addr
+ fb
->offsets
[index
];
153 static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers
= {
154 .atomic_commit_tail
= drm_atomic_helper_commit_tail_rpm
,
157 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs
= {
158 .fb_create
= exynos_user_fb_create
,
159 .output_poll_changed
= drm_fb_helper_output_poll_changed
,
160 .atomic_check
= drm_atomic_helper_check
,
161 .atomic_commit
= drm_atomic_helper_commit
,
164 void exynos_drm_mode_config_init(struct drm_device
*dev
)
166 dev
->mode_config
.min_width
= 0;
167 dev
->mode_config
.min_height
= 0;
170 * set max width and height as default value(4096x4096).
171 * this value would be used to check framebuffer size limitation
172 * at drm_mode_addfb().
174 dev
->mode_config
.max_width
= 4096;
175 dev
->mode_config
.max_height
= 4096;
177 dev
->mode_config
.funcs
= &exynos_drm_mode_config_funcs
;
178 dev
->mode_config
.helper_private
= &exynos_drm_mode_config_helpers
;
180 dev
->mode_config
.allow_fb_modifiers
= true;
182 dev
->mode_config
.normalize_zpos
= true;