bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / gpu / drm / imx / imx-drm-core.c
blob1d053bbefc02c694af593008ae19312a7826c994
1 /*
2 * Freescale i.MX drm driver
4 * Copyright (C) 2011 Sascha Hauer, Pengutronix
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/component.h>
17 #include <linux/device.h>
18 #include <linux/dma-buf.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <drm/drmP.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_fb_helper.h>
25 #include <drm/drm_crtc_helper.h>
26 #include <drm/drm_gem_cma_helper.h>
27 #include <drm/drm_gem_framebuffer_helper.h>
28 #include <drm/drm_fb_cma_helper.h>
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_of.h>
31 #include <video/imx-ipu-v3.h>
33 #include "imx-drm.h"
34 #include "ipuv3-plane.h"
36 #define MAX_CRTC 4
38 struct imx_drm_device {
39 struct drm_device *drm;
40 unsigned int pipes;
41 struct drm_atomic_state *state;
44 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
45 static int legacyfb_depth = 16;
46 module_param(legacyfb_depth, int, 0444);
47 #endif
49 DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
51 void imx_drm_connector_destroy(struct drm_connector *connector)
53 drm_connector_unregister(connector);
54 drm_connector_cleanup(connector);
56 EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
58 void imx_drm_encoder_destroy(struct drm_encoder *encoder)
60 drm_encoder_cleanup(encoder);
62 EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
64 static int imx_drm_atomic_check(struct drm_device *dev,
65 struct drm_atomic_state *state)
67 int ret;
69 ret = drm_atomic_helper_check_modeset(dev, state);
70 if (ret)
71 return ret;
73 ret = drm_atomic_helper_check_planes(dev, state);
74 if (ret)
75 return ret;
78 * Check modeset again in case crtc_state->mode_changed is
79 * updated in plane's ->atomic_check callback.
81 ret = drm_atomic_helper_check_modeset(dev, state);
82 if (ret)
83 return ret;
85 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
86 ret = ipu_planes_assign_pre(dev, state);
87 if (ret)
88 return ret;
90 return ret;
93 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
94 .fb_create = drm_gem_fb_create,
95 .output_poll_changed = drm_fb_helper_output_poll_changed,
96 .atomic_check = imx_drm_atomic_check,
97 .atomic_commit = drm_atomic_helper_commit,
100 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
102 struct drm_device *dev = state->dev;
103 struct drm_plane *plane;
104 struct drm_plane_state *old_plane_state, *new_plane_state;
105 bool plane_disabling = false;
106 int i;
108 drm_atomic_helper_commit_modeset_disables(dev, state);
110 drm_atomic_helper_commit_planes(dev, state,
111 DRM_PLANE_COMMIT_ACTIVE_ONLY |
112 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
114 drm_atomic_helper_commit_modeset_enables(dev, state);
116 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
117 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
118 plane_disabling = true;
122 * The flip done wait is only strictly required by imx-drm if a deferred
123 * plane disable is in-flight. As the core requires blocking commits
124 * to wait for the flip it is done here unconditionally. This keeps the
125 * workitem around a bit longer than required for the majority of
126 * non-blocking commits, but we accept that for the sake of simplicity.
128 drm_atomic_helper_wait_for_flip_done(dev, state);
130 if (plane_disabling) {
131 for_each_old_plane_in_state(state, plane, old_plane_state, i)
132 ipu_plane_disable_deferred(plane);
136 drm_atomic_helper_commit_hw_done(state);
139 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
140 .atomic_commit_tail = imx_drm_atomic_commit_tail,
144 int imx_drm_encoder_parse_of(struct drm_device *drm,
145 struct drm_encoder *encoder, struct device_node *np)
147 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
150 * If we failed to find the CRTC(s) which this encoder is
151 * supposed to be connected to, it's because the CRTC has
152 * not been registered yet. Defer probing, and hope that
153 * the required CRTC is added later.
155 if (crtc_mask == 0)
156 return -EPROBE_DEFER;
158 encoder->possible_crtcs = crtc_mask;
160 /* FIXME: this is the mask of outputs which can clone this output. */
161 encoder->possible_clones = ~0;
163 return 0;
165 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
167 static const struct drm_ioctl_desc imx_drm_ioctls[] = {
168 /* none so far */
171 static struct drm_driver imx_drm_driver = {
172 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
173 DRIVER_ATOMIC,
174 .lastclose = drm_fb_helper_lastclose,
175 .gem_free_object_unlocked = drm_gem_cma_free_object,
176 .gem_vm_ops = &drm_gem_cma_vm_ops,
177 .dumb_create = drm_gem_cma_dumb_create,
179 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
180 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
181 .gem_prime_import = drm_gem_prime_import,
182 .gem_prime_export = drm_gem_prime_export,
183 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
184 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
185 .gem_prime_vmap = drm_gem_cma_prime_vmap,
186 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
187 .gem_prime_mmap = drm_gem_cma_prime_mmap,
188 .ioctls = imx_drm_ioctls,
189 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
190 .fops = &imx_drm_driver_fops,
191 .name = "imx-drm",
192 .desc = "i.MX DRM graphics",
193 .date = "20120507",
194 .major = 1,
195 .minor = 0,
196 .patchlevel = 0,
199 static int compare_of(struct device *dev, void *data)
201 struct device_node *np = data;
203 /* Special case for DI, dev->of_node may not be set yet */
204 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
205 struct ipu_client_platformdata *pdata = dev->platform_data;
207 return pdata->of_node == np;
210 /* Special case for LDB, one device for two channels */
211 if (of_node_cmp(np->name, "lvds-channel") == 0) {
212 np = of_get_parent(np);
213 of_node_put(np);
216 return dev->of_node == np;
219 static int imx_drm_bind(struct device *dev)
221 struct drm_device *drm;
222 struct imx_drm_device *imxdrm;
223 int ret;
225 drm = drm_dev_alloc(&imx_drm_driver, dev);
226 if (IS_ERR(drm))
227 return PTR_ERR(drm);
229 imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL);
230 if (!imxdrm) {
231 ret = -ENOMEM;
232 goto err_unref;
235 imxdrm->drm = drm;
236 drm->dev_private = imxdrm;
239 * enable drm irq mode.
240 * - with irq_enabled = true, we can use the vblank feature.
242 * P.S. note that we wouldn't use drm irq handler but
243 * just specific driver own one instead because
244 * drm framework supports only one irq handler and
245 * drivers can well take care of their interrupts
247 drm->irq_enabled = true;
250 * set max width and height as default value(4096x4096).
251 * this value would be used to check framebuffer size limitation
252 * at drm_mode_addfb().
254 drm->mode_config.min_width = 1;
255 drm->mode_config.min_height = 1;
256 drm->mode_config.max_width = 4096;
257 drm->mode_config.max_height = 4096;
258 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
259 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
260 drm->mode_config.allow_fb_modifiers = true;
262 drm_mode_config_init(drm);
264 ret = drm_vblank_init(drm, MAX_CRTC);
265 if (ret)
266 goto err_kms;
268 dev_set_drvdata(dev, drm);
270 /* Now try and bind all our sub-components */
271 ret = component_bind_all(dev, drm);
272 if (ret)
273 goto err_kms;
275 drm_mode_config_reset(drm);
278 * All components are now initialised, so setup the fb helper.
279 * The fb helper takes copies of key hardware information, so the
280 * crtcs/connectors/encoders must not change after this point.
282 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
283 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
284 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
285 legacyfb_depth = 16;
287 ret = drm_fb_cma_fbdev_init(drm, legacyfb_depth, MAX_CRTC);
288 if (ret)
289 goto err_unbind;
290 #endif
292 drm_kms_helper_poll_init(drm);
294 ret = drm_dev_register(drm, 0);
295 if (ret)
296 goto err_fbhelper;
298 return 0;
300 err_fbhelper:
301 drm_kms_helper_poll_fini(drm);
302 #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
303 drm_fb_cma_fbdev_fini(drm);
304 err_unbind:
305 #endif
306 component_unbind_all(drm->dev, drm);
307 err_kms:
308 drm_mode_config_cleanup(drm);
309 err_unref:
310 drm_dev_unref(drm);
312 return ret;
315 static void imx_drm_unbind(struct device *dev)
317 struct drm_device *drm = dev_get_drvdata(dev);
319 drm_dev_unregister(drm);
321 drm_kms_helper_poll_fini(drm);
323 drm_fb_cma_fbdev_fini(drm);
325 drm_mode_config_cleanup(drm);
327 component_unbind_all(drm->dev, drm);
328 dev_set_drvdata(dev, NULL);
330 drm_dev_unref(drm);
333 static const struct component_master_ops imx_drm_ops = {
334 .bind = imx_drm_bind,
335 .unbind = imx_drm_unbind,
338 static int imx_drm_platform_probe(struct platform_device *pdev)
340 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
342 if (!ret)
343 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
345 return ret;
348 static int imx_drm_platform_remove(struct platform_device *pdev)
350 component_master_del(&pdev->dev, &imx_drm_ops);
351 return 0;
354 #ifdef CONFIG_PM_SLEEP
355 static int imx_drm_suspend(struct device *dev)
357 struct drm_device *drm_dev = dev_get_drvdata(dev);
358 struct imx_drm_device *imxdrm;
360 /* The drm_dev is NULL before .load hook is called */
361 if (drm_dev == NULL)
362 return 0;
364 drm_kms_helper_poll_disable(drm_dev);
366 imxdrm = drm_dev->dev_private;
367 imxdrm->state = drm_atomic_helper_suspend(drm_dev);
368 if (IS_ERR(imxdrm->state)) {
369 drm_kms_helper_poll_enable(drm_dev);
370 return PTR_ERR(imxdrm->state);
373 return 0;
376 static int imx_drm_resume(struct device *dev)
378 struct drm_device *drm_dev = dev_get_drvdata(dev);
379 struct imx_drm_device *imx_drm;
381 if (drm_dev == NULL)
382 return 0;
384 imx_drm = drm_dev->dev_private;
385 drm_atomic_helper_resume(drm_dev, imx_drm->state);
386 drm_kms_helper_poll_enable(drm_dev);
388 return 0;
390 #endif
392 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
394 static const struct of_device_id imx_drm_dt_ids[] = {
395 { .compatible = "fsl,imx-display-subsystem", },
396 { /* sentinel */ },
398 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
400 static struct platform_driver imx_drm_pdrv = {
401 .probe = imx_drm_platform_probe,
402 .remove = imx_drm_platform_remove,
403 .driver = {
404 .name = "imx-drm",
405 .pm = &imx_drm_pm_ops,
406 .of_match_table = imx_drm_dt_ids,
410 static struct platform_driver * const drivers[] = {
411 &imx_drm_pdrv,
412 &ipu_drm_driver,
415 static int __init imx_drm_init(void)
417 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
419 module_init(imx_drm_init);
421 static void __exit imx_drm_exit(void)
423 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
425 module_exit(imx_drm_exit);
427 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
428 MODULE_DESCRIPTION("i.MX drm driver core");
429 MODULE_LICENSE("GPL");