drm/vc4: drv: Skip BO Backend Initialization on BCM2711
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / vc4 / vc4_drv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014-2015 Broadcom
4  * Copyright (C) 2013 Red Hat
5  */
6
7 /**
8  * DOC: Broadcom VC4 Graphics Driver
9  *
10  * The Broadcom VideoCore 4 (present in the Raspberry Pi) contains a
11  * OpenGL ES 2.0-compatible 3D engine called V3D, and a highly
12  * configurable display output pipeline that supports HDMI, DSI, DPI,
13  * and Composite TV output.
14  *
15  * The 3D engine also has an interface for submitting arbitrary
16  * compute shader-style jobs using the same shader processor as is
17  * used for vertex and fragment shaders in GLES 2.0.  However, given
18  * that the hardware isn't able to expose any standard interfaces like
19  * OpenGL compute shaders or OpenCL, it isn't supported by this
20  * driver.
21  */
22
23 #include <linux/clk.h>
24 #include <linux/component.h>
25 #include <linux/device.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/io.h>
28 #include <linux/module.h>
29 #include <linux/of_platform.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
32
33 #include <drm/drm_aperture.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_fb_cma_helper.h>
37 #include <drm/drm_fb_helper.h>
38 #include <drm/drm_vblank.h>
39
40 #include <soc/bcm2835/raspberrypi-firmware.h>
41
42 #include "uapi/drm/vc4_drm.h"
43
44 #include "vc4_drv.h"
45 #include "vc4_regs.h"
46
47 #define DRIVER_NAME "vc4"
48 #define DRIVER_DESC "Broadcom VC4 graphics"
49 #define DRIVER_DATE "20140616"
50 #define DRIVER_MAJOR 0
51 #define DRIVER_MINOR 0
52 #define DRIVER_PATCHLEVEL 0
53
54 /* Helper function for mapping the regs on a platform device. */
55 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
56 {
57         struct resource *res;
58         void __iomem *map;
59
60         res = platform_get_resource(dev, IORESOURCE_MEM, index);
61         map = devm_ioremap_resource(&dev->dev, res);
62         if (IS_ERR(map))
63                 return map;
64
65         return map;
66 }
67
68 int vc4_dumb_fixup_args(struct drm_mode_create_dumb *args)
69 {
70         int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
71
72         if (args->pitch < min_pitch)
73                 args->pitch = min_pitch;
74
75         if (args->size < args->pitch * args->height)
76                 args->size = args->pitch * args->height;
77
78         return 0;
79 }
80
81 static int vc4_dumb_create(struct drm_file *file_priv,
82                            struct drm_device *dev,
83                            struct drm_mode_create_dumb *args)
84 {
85         int ret;
86
87         ret = vc4_dumb_fixup_args(args);
88         if (ret)
89                 return ret;
90
91         return drm_gem_cma_dumb_create_internal(file_priv, dev, args);
92 }
93
94 static int vc4_get_param_ioctl(struct drm_device *dev, void *data,
95                                struct drm_file *file_priv)
96 {
97         struct vc4_dev *vc4 = to_vc4_dev(dev);
98         struct drm_vc4_get_param *args = data;
99         int ret;
100
101         if (args->pad != 0)
102                 return -EINVAL;
103
104         if (!vc4->v3d)
105                 return -ENODEV;
106
107         switch (args->param) {
108         case DRM_VC4_PARAM_V3D_IDENT0:
109                 ret = vc4_v3d_pm_get(vc4);
110                 if (ret)
111                         return ret;
112                 args->value = V3D_READ(V3D_IDENT0);
113                 vc4_v3d_pm_put(vc4);
114                 break;
115         case DRM_VC4_PARAM_V3D_IDENT1:
116                 ret = vc4_v3d_pm_get(vc4);
117                 if (ret)
118                         return ret;
119                 args->value = V3D_READ(V3D_IDENT1);
120                 vc4_v3d_pm_put(vc4);
121                 break;
122         case DRM_VC4_PARAM_V3D_IDENT2:
123                 ret = vc4_v3d_pm_get(vc4);
124                 if (ret)
125                         return ret;
126                 args->value = V3D_READ(V3D_IDENT2);
127                 vc4_v3d_pm_put(vc4);
128                 break;
129         case DRM_VC4_PARAM_SUPPORTS_BRANCHES:
130         case DRM_VC4_PARAM_SUPPORTS_ETC1:
131         case DRM_VC4_PARAM_SUPPORTS_THREADED_FS:
132         case DRM_VC4_PARAM_SUPPORTS_FIXED_RCL_ORDER:
133         case DRM_VC4_PARAM_SUPPORTS_MADVISE:
134         case DRM_VC4_PARAM_SUPPORTS_PERFMON:
135                 args->value = true;
136                 break;
137         default:
138                 DRM_DEBUG("Unknown parameter %d\n", args->param);
139                 return -EINVAL;
140         }
141
142         return 0;
143 }
144
145 static int vc4_open(struct drm_device *dev, struct drm_file *file)
146 {
147         struct vc4_file *vc4file;
148
149         vc4file = kzalloc(sizeof(*vc4file), GFP_KERNEL);
150         if (!vc4file)
151                 return -ENOMEM;
152
153         vc4_perfmon_open_file(vc4file);
154         file->driver_priv = vc4file;
155         return 0;
156 }
157
158 static void vc4_close(struct drm_device *dev, struct drm_file *file)
159 {
160         struct vc4_dev *vc4 = to_vc4_dev(dev);
161         struct vc4_file *vc4file = file->driver_priv;
162
163         if (vc4file->bin_bo_used)
164                 vc4_v3d_bin_bo_put(vc4);
165
166         vc4_perfmon_close_file(vc4file);
167         kfree(vc4file);
168 }
169
170 DEFINE_DRM_GEM_FOPS(vc4_drm_fops);
171
172 static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
173         DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, DRM_RENDER_ALLOW),
174         DRM_IOCTL_DEF_DRV(VC4_WAIT_SEQNO, vc4_wait_seqno_ioctl, DRM_RENDER_ALLOW),
175         DRM_IOCTL_DEF_DRV(VC4_WAIT_BO, vc4_wait_bo_ioctl, DRM_RENDER_ALLOW),
176         DRM_IOCTL_DEF_DRV(VC4_CREATE_BO, vc4_create_bo_ioctl, DRM_RENDER_ALLOW),
177         DRM_IOCTL_DEF_DRV(VC4_MMAP_BO, vc4_mmap_bo_ioctl, DRM_RENDER_ALLOW),
178         DRM_IOCTL_DEF_DRV(VC4_CREATE_SHADER_BO, vc4_create_shader_bo_ioctl, DRM_RENDER_ALLOW),
179         DRM_IOCTL_DEF_DRV(VC4_GET_HANG_STATE, vc4_get_hang_state_ioctl,
180                           DRM_ROOT_ONLY),
181         DRM_IOCTL_DEF_DRV(VC4_GET_PARAM, vc4_get_param_ioctl, DRM_RENDER_ALLOW),
182         DRM_IOCTL_DEF_DRV(VC4_SET_TILING, vc4_set_tiling_ioctl, DRM_RENDER_ALLOW),
183         DRM_IOCTL_DEF_DRV(VC4_GET_TILING, vc4_get_tiling_ioctl, DRM_RENDER_ALLOW),
184         DRM_IOCTL_DEF_DRV(VC4_LABEL_BO, vc4_label_bo_ioctl, DRM_RENDER_ALLOW),
185         DRM_IOCTL_DEF_DRV(VC4_GEM_MADVISE, vc4_gem_madvise_ioctl, DRM_RENDER_ALLOW),
186         DRM_IOCTL_DEF_DRV(VC4_PERFMON_CREATE, vc4_perfmon_create_ioctl, DRM_RENDER_ALLOW),
187         DRM_IOCTL_DEF_DRV(VC4_PERFMON_DESTROY, vc4_perfmon_destroy_ioctl, DRM_RENDER_ALLOW),
188         DRM_IOCTL_DEF_DRV(VC4_PERFMON_GET_VALUES, vc4_perfmon_get_values_ioctl, DRM_RENDER_ALLOW),
189 };
190
191 static const struct drm_driver vc4_drm_driver = {
192         .driver_features = (DRIVER_MODESET |
193                             DRIVER_ATOMIC |
194                             DRIVER_GEM |
195                             DRIVER_RENDER |
196                             DRIVER_SYNCOBJ),
197         .open = vc4_open,
198         .postclose = vc4_close,
199
200 #if defined(CONFIG_DEBUG_FS)
201         .debugfs_init = vc4_debugfs_init,
202 #endif
203
204         .gem_create_object = vc4_create_object,
205
206         DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(vc4_bo_dumb_create),
207
208         .ioctls = vc4_drm_ioctls,
209         .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
210         .fops = &vc4_drm_fops,
211
212         .name = DRIVER_NAME,
213         .desc = DRIVER_DESC,
214         .date = DRIVER_DATE,
215         .major = DRIVER_MAJOR,
216         .minor = DRIVER_MINOR,
217         .patchlevel = DRIVER_PATCHLEVEL,
218 };
219
220 static const struct drm_driver vc5_drm_driver = {
221         .driver_features = (DRIVER_MODESET |
222                             DRIVER_ATOMIC |
223                             DRIVER_GEM),
224
225 #if defined(CONFIG_DEBUG_FS)
226         .debugfs_init = vc4_debugfs_init,
227 #endif
228
229         DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(vc4_dumb_create),
230
231         .fops = &vc4_drm_fops,
232
233         .name = DRIVER_NAME,
234         .desc = DRIVER_DESC,
235         .date = DRIVER_DATE,
236         .major = DRIVER_MAJOR,
237         .minor = DRIVER_MINOR,
238         .patchlevel = DRIVER_PATCHLEVEL,
239 };
240
241 static int compare_dev(struct device *dev, void *data)
242 {
243         return dev == data;
244 }
245
246 static void vc4_match_add_drivers(struct device *dev,
247                                   struct component_match **match,
248                                   struct platform_driver *const *drivers,
249                                   int count)
250 {
251         int i;
252
253         for (i = 0; i < count; i++) {
254                 struct device_driver *drv = &drivers[i]->driver;
255                 struct device *p = NULL, *d;
256
257                 while ((d = platform_find_device_by_driver(p, drv))) {
258                         put_device(p);
259                         component_match_add(dev, match, compare_dev, d);
260                         p = d;
261                 }
262                 put_device(p);
263         }
264 }
265
266 const struct of_device_id vc4_dma_range_matches[] = {
267         { .compatible = "brcm,bcm2835-hvs" },
268         { .compatible = "brcm,bcm2711-hvs" },
269         { .compatible = "raspberrypi,rpi-firmware-kms" },
270         { .compatible = "brcm,bcm2835-v3d" },
271         { .compatible = "brcm,cygnus-v3d" },
272         { .compatible = "brcm,vc4-v3d" },
273         {}
274 };
275
276 /*
277  * we need this helper function for determining presence of fkms
278  * before it's been bound
279  */
280 static bool firmware_kms(void)
281 {
282         return of_device_is_available(of_find_compatible_node(NULL, NULL,
283                "raspberrypi,rpi-firmware-kms")) ||
284                of_device_is_available(of_find_compatible_node(NULL, NULL,
285                "raspberrypi,rpi-firmware-kms-2711"));
286 }
287
288 static int vc4_drm_bind(struct device *dev)
289 {
290         struct platform_device *pdev = to_platform_device(dev);
291         const struct drm_driver *driver;
292         struct rpi_firmware *firmware = NULL;
293         struct drm_device *drm;
294         struct vc4_dev *vc4;
295         struct device_node *node;
296         struct drm_crtc *crtc;
297         bool is_vc5;
298         int ret = 0;
299
300         dev->coherent_dma_mask = DMA_BIT_MASK(32);
301
302         is_vc5 = of_device_is_compatible(dev->of_node, "brcm,bcm2711-vc5");
303         if (is_vc5)
304                 driver = &vc5_drm_driver;
305         else
306                 driver = &vc4_drm_driver;
307
308         node = of_find_matching_node_and_match(NULL, vc4_dma_range_matches,
309                                                NULL);
310         if (node) {
311                 ret = of_dma_configure(dev, node, true);
312                 of_node_put(node);
313
314                 if (ret)
315                         return ret;
316         }
317
318         vc4 = devm_drm_dev_alloc(dev, driver, struct vc4_dev, base);
319         if (IS_ERR(vc4))
320                 return PTR_ERR(vc4);
321         vc4->is_vc5 = is_vc5;
322
323         drm = &vc4->base;
324         platform_set_drvdata(pdev, drm);
325         INIT_LIST_HEAD(&vc4->debugfs_list);
326
327         if (!is_vc5) {
328                 mutex_init(&vc4->bin_bo_lock);
329
330                 ret = vc4_bo_cache_init(drm);
331                 if (ret)
332                         return ret;
333         }
334
335         ret = drmm_mode_config_init(drm);
336         if (ret)
337                 return ret;
338
339         if (!is_vc5) {
340                 ret = vc4_gem_init(drm);
341                 if (ret)
342                         return ret;
343         }
344
345         node = of_find_compatible_node(NULL, NULL, "raspberrypi,bcm2835-firmware");
346         if (node) {
347                 firmware = rpi_firmware_get(node);
348                 of_node_put(node);
349
350                 if (!firmware)
351                         return -EPROBE_DEFER;
352         }
353
354         ret = drm_aperture_remove_framebuffers(false, driver);
355         if (ret)
356                 return ret;
357
358         if (firmware && !firmware_kms()) {
359                 ret = rpi_firmware_property(firmware,
360                                             RPI_FIRMWARE_NOTIFY_DISPLAY_DONE,
361                                             NULL, 0);
362                 if (ret)
363                         drm_warn(drm, "Couldn't stop firmware display driver: %d\n", ret);
364
365                 rpi_firmware_put(firmware);
366         }
367
368         ret = component_bind_all(dev, drm);
369         if (ret)
370                 return ret;
371
372         if (!vc4->firmware_kms) {
373                 ret = vc4_plane_create_additional_planes(drm);
374                 if (ret)
375                         goto unbind_all;
376         }
377
378         ret = vc4_kms_load(drm);
379         if (ret < 0)
380                 goto unbind_all;
381
382         if (!vc4->firmware_kms) {
383                 drm_for_each_crtc(crtc, drm)
384                         vc4_crtc_disable_at_boot(crtc);
385         }
386
387         ret = drm_dev_register(drm, 0);
388         if (ret < 0)
389                 goto unbind_all;
390
391         drm_fbdev_generic_setup(drm, 16);
392
393         return 0;
394
395 unbind_all:
396         component_unbind_all(dev, drm);
397
398         return ret;
399 }
400
401 static void vc4_drm_unbind(struct device *dev)
402 {
403         struct drm_device *drm = dev_get_drvdata(dev);
404
405         drm_dev_unregister(drm);
406
407         drm_atomic_helper_shutdown(drm);
408 }
409
410 static const struct component_master_ops vc4_drm_ops = {
411         .bind = vc4_drm_bind,
412         .unbind = vc4_drm_unbind,
413 };
414
415 /*
416  * This list determines the binding order of our components, and we have
417  * a few constraints:
418  *   - The TXP driver needs to be bound before the PixelValves (CRTC)
419  *     but after the HVS to set the possible_crtc field properly
420  *   - The HDMI driver needs to be bound after the HVS so that we can
421  *     lookup the HVS maximum core clock rate and figure out if we
422  *     support 4kp60 or not.
423  */
424 static struct platform_driver *const component_drivers[] = {
425         &vc4_hvs_driver,
426         &vc4_hdmi_driver,
427         &vc4_vec_driver,
428         &vc4_dpi_driver,
429         &vc4_dsi_driver,
430         &vc4_txp_driver,
431         &vc4_crtc_driver,
432         &vc4_firmware_kms_driver,
433         &vc4_v3d_driver,
434 };
435
436 static int vc4_platform_drm_probe(struct platform_device *pdev)
437 {
438         struct component_match *match = NULL;
439         struct device *dev = &pdev->dev;
440
441         vc4_match_add_drivers(dev, &match,
442                               component_drivers, ARRAY_SIZE(component_drivers));
443
444         return component_master_add_with_match(dev, &vc4_drm_ops, match);
445 }
446
447 static int vc4_platform_drm_remove(struct platform_device *pdev)
448 {
449         component_master_del(&pdev->dev, &vc4_drm_ops);
450
451         return 0;
452 }
453
454 static const struct of_device_id vc4_of_match[] = {
455         { .compatible = "brcm,bcm2711-vc5", },
456         { .compatible = "brcm,bcm2835-vc4", },
457         { .compatible = "brcm,cygnus-vc4", },
458         {},
459 };
460 MODULE_DEVICE_TABLE(of, vc4_of_match);
461
462 static struct platform_driver vc4_platform_driver = {
463         .probe          = vc4_platform_drm_probe,
464         .remove         = vc4_platform_drm_remove,
465         .driver         = {
466                 .name   = "vc4-drm",
467                 .of_match_table = vc4_of_match,
468         },
469 };
470
471 static int __init vc4_drm_register(void)
472 {
473         int ret;
474
475         ret = platform_register_drivers(component_drivers,
476                                         ARRAY_SIZE(component_drivers));
477         if (ret)
478                 return ret;
479
480         return platform_driver_register(&vc4_platform_driver);
481 }
482
483 static void __exit vc4_drm_unregister(void)
484 {
485         platform_unregister_drivers(component_drivers,
486                                     ARRAY_SIZE(component_drivers));
487         platform_driver_unregister(&vc4_platform_driver);
488 }
489
490 module_init(vc4_drm_register);
491 module_exit(vc4_drm_unregister);
492
493 MODULE_ALIAS("platform:vc4-drm");
494 MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
495 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
496 MODULE_LICENSE("GPL v2");