]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/omapdrm/omap_drv.c
ff9b49276b810ecfb8633e514e3e12ec138f7eec
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / omapdrm / omap_drv.c
1 /*
2 * drivers/gpu/drm/omapdrm/omap_drv.c
3 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "omap_drv.h"
21
22 #include "drm_crtc_helper.h"
23 #include "drm_fb_helper.h"
24 #include "omap_dmm_tiler.h"
25
26 #define DRIVER_NAME MODULE_NAME
27 #define DRIVER_DESC "OMAP DRM"
28 #define DRIVER_DATE "20110917"
29 #define DRIVER_MAJOR 1
30 #define DRIVER_MINOR 0
31 #define DRIVER_PATCHLEVEL 0
32
33 static int num_crtc = CONFIG_DRM_OMAP_NUM_CRTCS;
34
35 MODULE_PARM_DESC(num_crtc, "Number of overlays to use as CRTCs");
36 module_param(num_crtc, int, 0600);
37
38 /*
39 * mode config funcs
40 */
41
42 /* Notes about mapping DSS and DRM entities:
43 * CRTC: overlay
44 * encoder: manager.. with some extension to allow one primary CRTC
45 * and zero or more video CRTC's to be mapped to one encoder?
46 * connector: dssdev.. manager can be attached/detached from different
47 * devices
48 */
49
50 static void omap_fb_output_poll_changed(struct drm_device *dev)
51 {
52 struct omap_drm_private *priv = dev->dev_private;
53 DBG("dev=%p", dev);
54 if (priv->fbdev)
55 drm_fb_helper_hotplug_event(priv->fbdev);
56 }
57
58 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
59 .fb_create = omap_framebuffer_create,
60 .output_poll_changed = omap_fb_output_poll_changed,
61 };
62
63 static int get_connector_type(struct omap_dss_device *dssdev)
64 {
65 switch (dssdev->type) {
66 case OMAP_DISPLAY_TYPE_HDMI:
67 return DRM_MODE_CONNECTOR_HDMIA;
68 case OMAP_DISPLAY_TYPE_DPI:
69 if (!strcmp(dssdev->name, "dvi"))
70 return DRM_MODE_CONNECTOR_DVID;
71 /* fallthrough */
72 default:
73 return DRM_MODE_CONNECTOR_Unknown;
74 }
75 }
76
77 static bool channel_used(struct drm_device *dev, enum omap_channel channel)
78 {
79 struct omap_drm_private *priv = dev->dev_private;
80 int i;
81
82 for (i = 0; i < priv->num_crtcs; i++) {
83 struct drm_crtc *crtc = priv->crtcs[i];
84
85 if (omap_crtc_channel(crtc) == channel)
86 return true;
87 }
88
89 return false;
90 }
91
92 static int omap_modeset_init(struct drm_device *dev)
93 {
94 struct omap_drm_private *priv = dev->dev_private;
95 struct omap_dss_device *dssdev = NULL;
96 int num_ovls = dss_feat_get_num_ovls();
97 int num_mgrs = dss_feat_get_num_mgrs();
98 int num_crtcs;
99 int i, id = 0;
100
101 omap_crtc_pre_init();
102
103 drm_mode_config_init(dev);
104
105 omap_drm_irq_install(dev);
106
107 /*
108 * We usually don't want to create a CRTC for each manager, at least
109 * not until we have a way to expose private planes to userspace.
110 * Otherwise there would not be enough video pipes left for drm planes.
111 * We use the num_crtc argument to limit the number of crtcs we create.
112 */
113 num_crtcs = min3(num_crtc, num_mgrs, num_ovls);
114
115 dssdev = NULL;
116
117 for_each_dss_dev(dssdev) {
118 struct drm_connector *connector;
119 struct drm_encoder *encoder;
120 enum omap_channel channel;
121
122 if (!dssdev->driver) {
123 dev_warn(dev->dev, "%s has no driver.. skipping it\n",
124 dssdev->name);
125 continue;
126 }
127
128 if (!(dssdev->driver->get_timings ||
129 dssdev->driver->read_edid)) {
130 dev_warn(dev->dev, "%s driver does not support "
131 "get_timings or read_edid.. skipping it!\n",
132 dssdev->name);
133 continue;
134 }
135
136 encoder = omap_encoder_init(dev, dssdev);
137
138 if (!encoder) {
139 dev_err(dev->dev, "could not create encoder: %s\n",
140 dssdev->name);
141 return -ENOMEM;
142 }
143
144 connector = omap_connector_init(dev,
145 get_connector_type(dssdev), dssdev, encoder);
146
147 if (!connector) {
148 dev_err(dev->dev, "could not create connector: %s\n",
149 dssdev->name);
150 return -ENOMEM;
151 }
152
153 BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
154 BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
155
156 priv->encoders[priv->num_encoders++] = encoder;
157 priv->connectors[priv->num_connectors++] = connector;
158
159 drm_mode_connector_attach_encoder(connector, encoder);
160
161 /*
162 * if we have reached the limit of the crtcs we are allowed to
163 * create, let's not try to look for a crtc for this
164 * panel/encoder and onwards, we will, of course, populate the
165 * the possible_crtcs field for all the encoders with the final
166 * set of crtcs we create
167 */
168 if (id == num_crtcs)
169 continue;
170
171 /*
172 * get the recommended DISPC channel for this encoder. For now,
173 * we only try to get create a crtc out of the recommended, the
174 * other possible channels to which the encoder can connect are
175 * not considered.
176 */
177 channel = dssdev->output->dispc_channel;
178
179 /*
180 * if this channel hasn't already been taken by a previously
181 * allocated crtc, we create a new crtc for it
182 */
183 if (!channel_used(dev, channel)) {
184 struct drm_plane *plane;
185 struct drm_crtc *crtc;
186
187 plane = omap_plane_init(dev, id, true);
188 crtc = omap_crtc_init(dev, plane, channel, id);
189
190 BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
191 priv->crtcs[id] = crtc;
192 priv->num_crtcs++;
193
194 priv->planes[id] = plane;
195 priv->num_planes++;
196
197 id++;
198 }
199 }
200
201 /*
202 * we have allocated crtcs according to the need of the panels/encoders,
203 * adding more crtcs here if needed
204 */
205 for (; id < num_crtcs; id++) {
206
207 /* find a free manager for this crtc */
208 for (i = 0; i < num_mgrs; i++) {
209 if (!channel_used(dev, i)) {
210 struct drm_plane *plane;
211 struct drm_crtc *crtc;
212
213 plane = omap_plane_init(dev, id, true);
214 crtc = omap_crtc_init(dev, plane, i, id);
215
216 BUG_ON(priv->num_crtcs >=
217 ARRAY_SIZE(priv->crtcs));
218
219 priv->crtcs[id] = crtc;
220 priv->num_crtcs++;
221
222 priv->planes[id] = plane;
223 priv->num_planes++;
224
225 break;
226 } else {
227 continue;
228 }
229 }
230
231 if (i == num_mgrs) {
232 /* this shouldn't really happen */
233 dev_err(dev->dev, "no managers left for crtc\n");
234 return -ENOMEM;
235 }
236 }
237
238 /*
239 * Create normal planes for the remaining overlays:
240 */
241 for (; id < num_ovls; id++) {
242 struct drm_plane *plane = omap_plane_init(dev, id, false);
243
244 BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
245 priv->planes[priv->num_planes++] = plane;
246 }
247
248 for (i = 0; i < priv->num_encoders; i++) {
249 struct drm_encoder *encoder = priv->encoders[i];
250 struct omap_dss_device *dssdev =
251 omap_encoder_get_dssdev(encoder);
252 struct omap_dss_output *output;
253
254 output = omapdss_find_output_from_display(dssdev);
255
256 /* figure out which crtc's we can connect the encoder to: */
257 encoder->possible_crtcs = 0;
258 for (id = 0; id < priv->num_crtcs; id++) {
259 struct drm_crtc *crtc = priv->crtcs[id];
260 enum omap_channel crtc_channel;
261 enum omap_dss_output_id supported_outputs;
262
263 crtc_channel = omap_crtc_channel(crtc);
264 supported_outputs =
265 dss_feat_get_supported_outputs(crtc_channel);
266
267 if (supported_outputs & output->id)
268 encoder->possible_crtcs |= (1 << id);
269 }
270 }
271
272 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
273 priv->num_planes, priv->num_crtcs, priv->num_encoders,
274 priv->num_connectors);
275
276 dev->mode_config.min_width = 32;
277 dev->mode_config.min_height = 32;
278
279 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
280 * to fill in these limits properly on different OMAP generations..
281 */
282 dev->mode_config.max_width = 2048;
283 dev->mode_config.max_height = 2048;
284
285 dev->mode_config.funcs = &omap_mode_config_funcs;
286
287 return 0;
288 }
289
290 static void omap_modeset_free(struct drm_device *dev)
291 {
292 drm_mode_config_cleanup(dev);
293 }
294
295 /*
296 * drm ioctl funcs
297 */
298
299
300 static int ioctl_get_param(struct drm_device *dev, void *data,
301 struct drm_file *file_priv)
302 {
303 struct omap_drm_private *priv = dev->dev_private;
304 struct drm_omap_param *args = data;
305
306 DBG("%p: param=%llu", dev, args->param);
307
308 switch (args->param) {
309 case OMAP_PARAM_CHIPSET_ID:
310 args->value = priv->omaprev;
311 break;
312 default:
313 DBG("unknown parameter %lld", args->param);
314 return -EINVAL;
315 }
316
317 return 0;
318 }
319
320 static int ioctl_set_param(struct drm_device *dev, void *data,
321 struct drm_file *file_priv)
322 {
323 struct drm_omap_param *args = data;
324
325 switch (args->param) {
326 default:
327 DBG("unknown parameter %lld", args->param);
328 return -EINVAL;
329 }
330
331 return 0;
332 }
333
334 static int ioctl_gem_new(struct drm_device *dev, void *data,
335 struct drm_file *file_priv)
336 {
337 struct drm_omap_gem_new *args = data;
338 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
339 args->size.bytes, args->flags);
340 return omap_gem_new_handle(dev, file_priv, args->size,
341 args->flags, &args->handle);
342 }
343
344 static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
345 struct drm_file *file_priv)
346 {
347 struct drm_omap_gem_cpu_prep *args = data;
348 struct drm_gem_object *obj;
349 int ret;
350
351 VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
352
353 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
354 if (!obj)
355 return -ENOENT;
356
357 ret = omap_gem_op_sync(obj, args->op);
358
359 if (!ret)
360 ret = omap_gem_op_start(obj, args->op);
361
362 drm_gem_object_unreference_unlocked(obj);
363
364 return ret;
365 }
366
367 static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
368 struct drm_file *file_priv)
369 {
370 struct drm_omap_gem_cpu_fini *args = data;
371 struct drm_gem_object *obj;
372 int ret;
373
374 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
375
376 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
377 if (!obj)
378 return -ENOENT;
379
380 /* XXX flushy, flushy */
381 ret = 0;
382
383 if (!ret)
384 ret = omap_gem_op_finish(obj, args->op);
385
386 drm_gem_object_unreference_unlocked(obj);
387
388 return ret;
389 }
390
391 static int ioctl_gem_info(struct drm_device *dev, void *data,
392 struct drm_file *file_priv)
393 {
394 struct drm_omap_gem_info *args = data;
395 struct drm_gem_object *obj;
396 int ret = 0;
397
398 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
399
400 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
401 if (!obj)
402 return -ENOENT;
403
404 args->size = omap_gem_mmap_size(obj);
405 args->offset = omap_gem_mmap_offset(obj);
406
407 drm_gem_object_unreference_unlocked(obj);
408
409 return ret;
410 }
411
412 static struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
413 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param, DRM_UNLOCKED|DRM_AUTH),
414 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
415 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH),
416 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
417 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
418 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH),
419 };
420
421 /*
422 * drm driver funcs
423 */
424
425 /**
426 * load - setup chip and create an initial config
427 * @dev: DRM device
428 * @flags: startup flags
429 *
430 * The driver load routine has to do several things:
431 * - initialize the memory manager
432 * - allocate initial config memory
433 * - setup the DRM framebuffer with the allocated memory
434 */
435 static int dev_load(struct drm_device *dev, unsigned long flags)
436 {
437 struct omap_drm_platform_data *pdata = dev->dev->platform_data;
438 struct omap_drm_private *priv;
439 int ret;
440
441 DBG("load: dev=%p", dev);
442
443 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
444 if (!priv)
445 return -ENOMEM;
446
447 priv->omaprev = pdata->omaprev;
448
449 dev->dev_private = priv;
450
451 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
452
453 INIT_LIST_HEAD(&priv->obj_list);
454
455 omap_gem_init(dev);
456
457 ret = omap_modeset_init(dev);
458 if (ret) {
459 dev_err(dev->dev, "omap_modeset_init failed: ret=%d\n", ret);
460 dev->dev_private = NULL;
461 kfree(priv);
462 return ret;
463 }
464
465 ret = drm_vblank_init(dev, priv->num_crtcs);
466 if (ret)
467 dev_warn(dev->dev, "could not init vblank\n");
468
469 priv->fbdev = omap_fbdev_init(dev);
470 if (!priv->fbdev) {
471 dev_warn(dev->dev, "omap_fbdev_init failed\n");
472 /* well, limp along without an fbdev.. maybe X11 will work? */
473 }
474
475 /* store off drm_device for use in pm ops */
476 dev_set_drvdata(dev->dev, dev);
477
478 drm_kms_helper_poll_init(dev);
479
480 return 0;
481 }
482
483 static int dev_unload(struct drm_device *dev)
484 {
485 struct omap_drm_private *priv = dev->dev_private;
486
487 DBG("unload: dev=%p", dev);
488
489 drm_kms_helper_poll_fini(dev);
490 drm_vblank_cleanup(dev);
491 omap_drm_irq_uninstall(dev);
492
493 omap_fbdev_free(dev);
494 omap_modeset_free(dev);
495 omap_gem_deinit(dev);
496
497 flush_workqueue(priv->wq);
498 destroy_workqueue(priv->wq);
499
500 kfree(dev->dev_private);
501 dev->dev_private = NULL;
502
503 dev_set_drvdata(dev->dev, NULL);
504
505 return 0;
506 }
507
508 static int dev_open(struct drm_device *dev, struct drm_file *file)
509 {
510 file->driver_priv = NULL;
511
512 DBG("open: dev=%p, file=%p", dev, file);
513
514 return 0;
515 }
516
517 static int dev_firstopen(struct drm_device *dev)
518 {
519 DBG("firstopen: dev=%p", dev);
520 return 0;
521 }
522
523 /**
524 * lastclose - clean up after all DRM clients have exited
525 * @dev: DRM device
526 *
527 * Take care of cleaning up after all DRM clients have exited. In the
528 * mode setting case, we want to restore the kernel's initial mode (just
529 * in case the last client left us in a bad state).
530 */
531 static void dev_lastclose(struct drm_device *dev)
532 {
533 int i;
534
535 /* we don't support vga-switcheroo.. so just make sure the fbdev
536 * mode is active
537 */
538 struct omap_drm_private *priv = dev->dev_private;
539 int ret;
540
541 DBG("lastclose: dev=%p", dev);
542
543 if (priv->rotation_prop) {
544 /* need to restore default rotation state.. not sure
545 * if there is a cleaner way to restore properties to
546 * default state? Maybe a flag that properties should
547 * automatically be restored to default state on
548 * lastclose?
549 */
550 for (i = 0; i < priv->num_crtcs; i++) {
551 drm_object_property_set_value(&priv->crtcs[i]->base,
552 priv->rotation_prop, 0);
553 }
554
555 for (i = 0; i < priv->num_planes; i++) {
556 drm_object_property_set_value(&priv->planes[i]->base,
557 priv->rotation_prop, 0);
558 }
559 }
560
561 drm_modeset_lock_all(dev);
562 ret = drm_fb_helper_restore_fbdev_mode(priv->fbdev);
563 drm_modeset_unlock_all(dev);
564 if (ret)
565 DBG("failed to restore crtc mode");
566 }
567
568 static void dev_preclose(struct drm_device *dev, struct drm_file *file)
569 {
570 DBG("preclose: dev=%p", dev);
571 }
572
573 static void dev_postclose(struct drm_device *dev, struct drm_file *file)
574 {
575 DBG("postclose: dev=%p, file=%p", dev, file);
576 }
577
578 static const struct vm_operations_struct omap_gem_vm_ops = {
579 .fault = omap_gem_fault,
580 .open = drm_gem_vm_open,
581 .close = drm_gem_vm_close,
582 };
583
584 static const struct file_operations omapdriver_fops = {
585 .owner = THIS_MODULE,
586 .open = drm_open,
587 .unlocked_ioctl = drm_ioctl,
588 .release = drm_release,
589 .mmap = omap_gem_mmap,
590 .poll = drm_poll,
591 .fasync = drm_fasync,
592 .read = drm_read,
593 .llseek = noop_llseek,
594 };
595
596 static struct drm_driver omap_drm_driver = {
597 .driver_features =
598 DRIVER_HAVE_IRQ | DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
599 .load = dev_load,
600 .unload = dev_unload,
601 .open = dev_open,
602 .firstopen = dev_firstopen,
603 .lastclose = dev_lastclose,
604 .preclose = dev_preclose,
605 .postclose = dev_postclose,
606 .get_vblank_counter = drm_vblank_count,
607 .enable_vblank = omap_irq_enable_vblank,
608 .disable_vblank = omap_irq_disable_vblank,
609 .irq_preinstall = omap_irq_preinstall,
610 .irq_postinstall = omap_irq_postinstall,
611 .irq_uninstall = omap_irq_uninstall,
612 .irq_handler = omap_irq_handler,
613 #ifdef CONFIG_DEBUG_FS
614 .debugfs_init = omap_debugfs_init,
615 .debugfs_cleanup = omap_debugfs_cleanup,
616 #endif
617 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
618 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
619 .gem_prime_export = omap_gem_prime_export,
620 .gem_prime_import = omap_gem_prime_import,
621 .gem_init_object = omap_gem_init_object,
622 .gem_free_object = omap_gem_free_object,
623 .gem_vm_ops = &omap_gem_vm_ops,
624 .dumb_create = omap_gem_dumb_create,
625 .dumb_map_offset = omap_gem_dumb_map_offset,
626 .dumb_destroy = omap_gem_dumb_destroy,
627 .ioctls = ioctls,
628 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
629 .fops = &omapdriver_fops,
630 .name = DRIVER_NAME,
631 .desc = DRIVER_DESC,
632 .date = DRIVER_DATE,
633 .major = DRIVER_MAJOR,
634 .minor = DRIVER_MINOR,
635 .patchlevel = DRIVER_PATCHLEVEL,
636 };
637
638 static int pdev_suspend(struct platform_device *pDevice, pm_message_t state)
639 {
640 DBG("");
641 return 0;
642 }
643
644 static int pdev_resume(struct platform_device *device)
645 {
646 DBG("");
647 return 0;
648 }
649
650 static void pdev_shutdown(struct platform_device *device)
651 {
652 DBG("");
653 }
654
655 static int pdev_probe(struct platform_device *device)
656 {
657 if (omapdss_is_initialized() == false)
658 return -EPROBE_DEFER;
659
660 DBG("%s", device->name);
661 return drm_platform_init(&omap_drm_driver, device);
662 }
663
664 static int pdev_remove(struct platform_device *device)
665 {
666 DBG("");
667 drm_platform_exit(&omap_drm_driver, device);
668
669 platform_driver_unregister(&omap_dmm_driver);
670 return 0;
671 }
672
673 #ifdef CONFIG_PM
674 static const struct dev_pm_ops omapdrm_pm_ops = {
675 .resume = omap_gem_resume,
676 };
677 #endif
678
679 static struct platform_driver pdev = {
680 .driver = {
681 .name = DRIVER_NAME,
682 .owner = THIS_MODULE,
683 #ifdef CONFIG_PM
684 .pm = &omapdrm_pm_ops,
685 #endif
686 },
687 .probe = pdev_probe,
688 .remove = pdev_remove,
689 .suspend = pdev_suspend,
690 .resume = pdev_resume,
691 .shutdown = pdev_shutdown,
692 };
693
694 static int __init omap_drm_init(void)
695 {
696 DBG("init");
697 if (platform_driver_register(&omap_dmm_driver)) {
698 /* we can continue on without DMM.. so not fatal */
699 dev_err(NULL, "DMM registration failed\n");
700 }
701 return platform_driver_register(&pdev);
702 }
703
704 static void __exit omap_drm_fini(void)
705 {
706 DBG("fini");
707 platform_driver_unregister(&pdev);
708 }
709
710 /* need late_initcall() so we load after dss_driver's are loaded */
711 late_initcall(omap_drm_init);
712 module_exit(omap_drm_fini);
713
714 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
715 MODULE_DESCRIPTION("OMAP DRM Display Driver");
716 MODULE_ALIAS("platform:" DRIVER_NAME);
717 MODULE_LICENSE("GPL v2");