4027fa91c8ab5b8c9a031efc0a702d00c16d268c
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * based on exynos_drm_drv.c
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/dma-iommu.h>
18
19 #include <drm/drmP.h>
20 #include <drm/drm_atomic.h>
21 #include <drm/drm_crtc_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_sync_helper.h>
24 #include <drm/rockchip_drm.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/memblock.h>
28 #include <linux/module.h>
29 #include <linux/of_address.h>
30 #include <linux/of_graph.h>
31 #include <linux/component.h>
32 #include <linux/fence.h>
33 #include <linux/console.h>
34
35 #include "rockchip_drm_drv.h"
36 #include "rockchip_drm_fb.h"
37 #include "rockchip_drm_fbdev.h"
38 #include "rockchip_drm_gem.h"
39 #include "rockchip_drm_rga.h"
40
41 #define DRIVER_NAME     "rockchip"
42 #define DRIVER_DESC     "RockChip Soc DRM"
43 #define DRIVER_DATE     "20140818"
44 #define DRIVER_MAJOR    1
45 #define DRIVER_MINOR    0
46
47 static LIST_HEAD(rockchip_drm_subdrv_list);
48 static DEFINE_MUTEX(subdrv_list_mutex);
49
50 struct rockchip_drm_mode_set {
51         struct list_head head;
52         struct drm_framebuffer *fb;
53         struct drm_connector *connector;
54         struct drm_crtc *crtc;
55         struct drm_display_mode *mode;
56         int hdisplay;
57         int vdisplay;
58         int vrefresh;
59
60         bool mode_changed;
61         int ratio;
62 };
63
64 static struct drm_crtc *find_crtc_by_node(struct drm_device *drm_dev,
65                                           struct device_node *node)
66 {
67         struct device_node *np_crtc;
68         struct drm_crtc *crtc;
69
70         np_crtc = of_get_parent(node);
71         if (!np_crtc || !of_device_is_available(np_crtc))
72                 return NULL;
73
74         drm_for_each_crtc(crtc, drm_dev) {
75                 if (crtc->port == np_crtc)
76                         return crtc;
77         }
78
79         return NULL;
80 }
81
82 static struct drm_connector *find_connector_by_node(struct drm_device *drm_dev,
83                                                     struct device_node *node)
84 {
85         struct device_node *np_connector;
86         struct drm_connector *connector;
87
88         np_connector = of_graph_get_remote_port_parent(node);
89         if (!np_connector || !of_device_is_available(np_connector))
90                 return NULL;
91
92         drm_for_each_connector(connector, drm_dev) {
93                 if (connector->port == np_connector)
94                         return connector;
95         }
96
97         return NULL;
98 }
99
100 static struct drm_framebuffer *
101 get_framebuffer_by_node(struct drm_device *drm_dev, struct device_node *node)
102 {
103         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
104         struct device_node *memory;
105         struct resource res;
106         u32 val;
107         int bpp;
108
109         memory = of_parse_phandle(node, "logo,mem", 0);
110         if (!memory)
111                 return NULL;
112
113         if (of_address_to_resource(memory, 0, &res)) {
114                 pr_err("%s: could not get bootram phy addr\n", __func__);
115                 return NULL;
116         }
117
118         if (of_property_read_u32(node, "logo,offset", &val)) {
119                 pr_err("%s: failed to get logo,offset\n", __func__);
120                 return NULL;
121         }
122         mode_cmd.offsets[0] = val;
123
124         if (of_property_read_u32(node, "logo,width", &val)) {
125                 pr_err("%s: failed to get logo,width\n", __func__);
126                 return NULL;
127         }
128         mode_cmd.width = val;
129
130         if (of_property_read_u32(node, "logo,height", &val)) {
131                 pr_err("%s: failed to get logo,height\n", __func__);
132                 return NULL;
133         }
134         mode_cmd.height = val;
135
136         if (of_property_read_u32(node, "logo,bpp", &val)) {
137                 pr_err("%s: failed to get logo,bpp\n", __func__);
138                 return NULL;
139         }
140         bpp = val;
141
142         mode_cmd.pitches[0] = mode_cmd.width * bpp / 8;
143         mode_cmd.pixel_format = DRM_FORMAT_BGR888;
144
145         return rockchip_fb_alloc(drm_dev, &mode_cmd, NULL, &res, 1);
146 }
147
148 static struct rockchip_drm_mode_set *
149 of_parse_display_resource(struct drm_device *drm_dev, struct device_node *route)
150 {
151         struct rockchip_drm_mode_set *set;
152         struct device_node *connect;
153         struct drm_framebuffer *fb;
154         struct drm_connector *connector;
155         struct drm_crtc *crtc;
156         u32 val;
157
158         connect = of_parse_phandle(route, "connect", 0);
159         if (!connect)
160                 return NULL;
161
162         fb = get_framebuffer_by_node(drm_dev, route);
163         if (IS_ERR_OR_NULL(fb))
164                 return NULL;
165
166         crtc = find_crtc_by_node(drm_dev, connect);
167         connector = find_connector_by_node(drm_dev, connect);
168         if (!crtc || !connector) {
169                 dev_warn(drm_dev->dev,
170                          "No available crtc or connector for display");
171                 drm_framebuffer_unreference(fb);
172                 return NULL;
173         }
174
175         set = kzalloc(sizeof(*set), GFP_KERNEL);
176         if (!set)
177                 return NULL;
178
179         if (!of_property_read_u32(route, "video,hdisplay", &val))
180                 set->hdisplay = val;
181
182         if (!of_property_read_u32(route, "video,vdisplay", &val))
183                 set->vdisplay = val;
184
185         if (!of_property_read_u32(route, "video,vrefresh", &val))
186                 set->vrefresh = val;
187
188         set->fb = fb;
189         set->crtc = crtc;
190         set->connector = connector;
191         /* TODO: set display fullscreen or center */
192         set->ratio = 0;
193
194         return set;
195 }
196
197 int setup_initial_state(struct drm_device *drm_dev,
198                         struct drm_atomic_state *state,
199                         struct rockchip_drm_mode_set *set)
200 {
201         struct drm_connector *connector = set->connector;
202         struct drm_crtc *crtc = set->crtc;
203         struct drm_crtc_state *crtc_state;
204         struct drm_connector_state *conn_state;
205         struct drm_plane_state *primary_state;
206         struct drm_display_mode *mode = NULL;
207         const struct drm_connector_helper_funcs *funcs;
208         bool is_crtc_enabled = true;
209         int hdisplay, vdisplay;
210         int fb_width, fb_height;
211         int found = 0, match = 0;
212         int num_modes;
213         int ret = 0;
214
215         if (!set->hdisplay || !set->vdisplay || !set->vrefresh)
216                 is_crtc_enabled = false;
217
218         conn_state = drm_atomic_get_connector_state(state, connector);
219         if (IS_ERR(conn_state))
220                 return PTR_ERR(conn_state);
221
222         funcs = connector->helper_private;
223         conn_state->best_encoder = funcs->best_encoder(connector);
224         num_modes = connector->funcs->fill_modes(connector, 4096, 4096);
225         if (!num_modes) {
226                 dev_err(drm_dev->dev, "connector[%s] can't found any modes\n",
227                         connector->name);
228                 return -EINVAL;
229         }
230
231         list_for_each_entry(mode, &connector->modes, head) {
232                 if (mode->hdisplay == set->hdisplay &&
233                     mode->vdisplay == set->vdisplay &&
234                     drm_mode_vrefresh(mode) == set->vrefresh) {
235                         found = 1;
236                         match = 1;
237                         break;
238                 }
239         }
240
241         if (!found) {
242                 list_for_each_entry(mode, &connector->modes, head) {
243                         if (mode->type & DRM_MODE_TYPE_PREFERRED) {
244                                 found = 1;
245                                 break;
246                         }
247                 }
248
249                 if (!found) {
250                         mode = list_first_entry_or_null(&connector->modes,
251                                                         struct drm_display_mode,
252                                                         head);
253                         if (!mode) {
254                                 pr_err("failed to find available modes\n");
255                                 return -EINVAL;
256                         }
257                 }
258         }
259
260         set->mode = mode;
261         crtc_state = drm_atomic_get_crtc_state(state, crtc);
262         if (IS_ERR(crtc_state))
263                 return PTR_ERR(crtc_state);
264
265         drm_mode_copy(&crtc_state->adjusted_mode, mode);
266         if (!match || !is_crtc_enabled) {
267                 set->mode_changed = true;
268         } else {
269                 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
270                 if (ret)
271                         return ret;
272
273                 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
274                 if (ret)
275                         return ret;
276
277                 crtc_state->active = true;
278         }
279
280         if (!set->fb)
281                 return 0;
282         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
283         if (IS_ERR(primary_state))
284                 return PTR_ERR(primary_state);
285
286         hdisplay = mode->hdisplay;
287         vdisplay = mode->vdisplay;
288         fb_width = set->fb->width;
289         fb_height = set->fb->height;
290
291         primary_state->crtc = crtc;
292         primary_state->src_x = 0;
293         primary_state->src_y = 0;
294         primary_state->src_w = fb_width << 16;
295         primary_state->src_h = fb_height << 16;
296         if (set->ratio) {
297                 if (set->fb->width >= hdisplay) {
298                         primary_state->crtc_x = 0;
299                         primary_state->crtc_w = hdisplay;
300                 } else {
301                         primary_state->crtc_x = (hdisplay - fb_width) / 2;
302                         primary_state->crtc_w = set->fb->width;
303                 }
304
305                 if (set->fb->height >= vdisplay) {
306                         primary_state->crtc_y = 0;
307                         primary_state->crtc_h = vdisplay;
308                 } else {
309                         primary_state->crtc_y = (vdisplay - fb_height) / 2;
310                         primary_state->crtc_h = fb_height;
311                 }
312         } else {
313                 primary_state->crtc_x = 0;
314                 primary_state->crtc_y = 0;
315                 primary_state->crtc_w = hdisplay;
316                 primary_state->crtc_h = vdisplay;
317         }
318
319         return 0;
320 }
321
322 static int update_state(struct drm_device *drm_dev,
323                         struct drm_atomic_state *state,
324                         struct rockchip_drm_mode_set *set,
325                         unsigned int *plane_mask)
326 {
327         struct drm_mode_config *mode_config = &drm_dev->mode_config;
328         struct drm_crtc *crtc = set->crtc;
329         struct drm_connector *connector = set->connector;
330         struct drm_display_mode *mode = set->mode;
331         struct drm_plane_state *primary_state;
332         struct drm_crtc_state *crtc_state;
333         struct drm_connector_state *conn_state;
334         int ret;
335
336         crtc_state = drm_atomic_get_crtc_state(state, crtc);
337         if (IS_ERR(crtc_state))
338                 return PTR_ERR(crtc_state);
339         conn_state = drm_atomic_get_connector_state(state, connector);
340         if (IS_ERR(conn_state))
341                 return PTR_ERR(conn_state);
342
343         if (set->mode_changed) {
344                 ret = drm_atomic_set_crtc_for_connector(conn_state, crtc);
345                 if (ret)
346                         return ret;
347
348                 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
349                 if (ret)
350                         return ret;
351
352                 crtc_state->active = true;
353         } else {
354                 const struct drm_crtc_helper_funcs *funcs;
355
356                 funcs = crtc->helper_private;
357                 if (!funcs || !funcs->enable)
358                         return -ENXIO;
359                 funcs->enable(crtc);
360         }
361
362         primary_state = drm_atomic_get_plane_state(state, crtc->primary);
363         if (IS_ERR(primary_state))
364                 return PTR_ERR(primary_state);
365
366         crtc_state->plane_mask = 1 << drm_plane_index(crtc->primary);
367         *plane_mask |= crtc_state->plane_mask;
368
369         drm_atomic_set_fb_for_plane(primary_state, set->fb);
370         drm_framebuffer_unreference(set->fb);
371         ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
372
373         /*
374          * TODO:
375          * some vop maybe not support ymirror, but force use it now.
376          */
377         drm_atomic_plane_set_property(crtc->primary, primary_state,
378                                       mode_config->rotation_property,
379                                       BIT(DRM_REFLECT_Y));
380
381         return ret;
382 }
383
384 static void show_loader_logo(struct drm_device *drm_dev)
385 {
386         struct drm_atomic_state *state;
387         struct device_node *np = drm_dev->dev->of_node;
388         struct drm_mode_config *mode_config = &drm_dev->mode_config;
389         struct device_node *root, *route;
390         struct rockchip_drm_mode_set *set, *tmp;
391         struct list_head mode_set_list;
392         unsigned plane_mask = 0;
393         int ret;
394
395         root = of_get_child_by_name(np, "route");
396         if (!root) {
397                 dev_warn(drm_dev->dev, "failed to parse display resources\n");
398                 return;
399         }
400
401         INIT_LIST_HEAD(&mode_set_list);
402         drm_modeset_lock_all(drm_dev);
403         state = drm_atomic_state_alloc(drm_dev);
404         if (!state) {
405                 dev_err(drm_dev->dev, "failed to alloc atomic state\n");
406                 ret = -ENOMEM;
407                 goto err_unlock;
408         }
409
410         state->acquire_ctx = mode_config->acquire_ctx;
411         for_each_child_of_node(root, route) {
412                 set = of_parse_display_resource(drm_dev, route);
413                 if (!set)
414                         continue;
415
416                 if (setup_initial_state(drm_dev, state, set)) {
417                         drm_framebuffer_unreference(set->fb);
418                         kfree(set);
419                         continue;
420                 }
421                 INIT_LIST_HEAD(&set->head);
422                 list_add_tail(&set->head, &mode_set_list);
423         }
424
425         if (list_empty(&mode_set_list)) {
426                 dev_warn(drm_dev->dev, "can't not find any loader display\n");
427                 ret = -ENXIO;
428                 goto err_free_state;
429         }
430
431         /*
432          * The state save initial devices status, swap the state into
433          * drm deivces as old state, so if new state come, can compare
434          * with this state to judge which status need to update.
435          */
436         drm_atomic_helper_swap_state(drm_dev, state);
437         drm_atomic_state_free(state);
438         state = drm_atomic_helper_duplicate_state(drm_dev,
439                                                   mode_config->acquire_ctx);
440         if (IS_ERR(state)) {
441                 dev_err(drm_dev->dev, "failed to duplicate atomic state\n");
442                 ret = PTR_ERR_OR_ZERO(state);
443                 goto err_unlock;
444         }
445         state->acquire_ctx = mode_config->acquire_ctx;
446         list_for_each_entry(set, &mode_set_list, head)
447                 /*
448                  * We don't want to see any fail on update_state.
449                  */
450                 WARN_ON(update_state(drm_dev, state, set, &plane_mask));
451
452         ret = drm_atomic_commit(state);
453         drm_atomic_clean_old_fb(drm_dev, plane_mask, ret);
454
455         list_for_each_entry_safe(set, tmp, &mode_set_list, head) {
456                 struct drm_crtc *crtc = set->crtc;
457
458                 list_del(&set->head);
459                 kfree(set);
460
461                 /* FIXME:
462                  * primary plane state rotation is not BIT(0), but we only want
463                  * it effect on logo display, userspace may not known to clean
464                  * this property, would get unexpect display, so force set
465                  * primary rotation to BIT(0).
466                  */
467                 if (!crtc->primary || !crtc->primary->state)
468                         continue;
469
470                 drm_atomic_plane_set_property(crtc->primary,
471                                               crtc->primary->state,
472                                               mode_config->rotation_property,
473                                               BIT(0));
474         }
475
476         /*
477          * Is possible get deadlock here?
478          */
479         WARN_ON(ret == -EDEADLK);
480
481         if (ret)
482                 goto err_free_state;
483
484         drm_modeset_unlock_all(drm_dev);
485         return;
486
487 err_free_state:
488         drm_atomic_state_free(state);
489 err_unlock:
490         drm_modeset_unlock_all(drm_dev);
491         if (ret)
492                 dev_err(drm_dev->dev, "failed to show loader logo\n");
493 }
494
495 /*
496  * Attach a (component) device to the shared drm dma mapping from master drm
497  * device.  This is used by the VOPs to map GEM buffers to a common DMA
498  * mapping.
499  */
500 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
501                                    struct device *dev)
502 {
503         struct rockchip_drm_private *private = drm_dev->dev_private;
504         struct iommu_domain *domain = private->domain;
505         int ret;
506
507         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
508         if (ret)
509                 return ret;
510
511         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
512         ret = iommu_attach_device(domain, dev);
513         if (ret) {
514                 dev_err(dev, "Failed to attach iommu device\n");
515                 return ret;
516         }
517
518         if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
519                 dev_err(dev, "Failed to set dma_ops\n");
520                 iommu_detach_device(domain, dev);
521                 ret = -ENODEV;
522         }
523
524         return ret;
525 }
526
527 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
528                                     struct device *dev)
529 {
530         struct rockchip_drm_private *private = drm_dev->dev_private;
531         struct iommu_domain *domain = private->domain;
532
533         iommu_detach_device(domain, dev);
534 }
535
536 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
537                                  const struct rockchip_crtc_funcs *crtc_funcs)
538 {
539         int pipe = drm_crtc_index(crtc);
540         struct rockchip_drm_private *priv = crtc->dev->dev_private;
541
542         if (pipe > ROCKCHIP_MAX_CRTC)
543                 return -EINVAL;
544
545         priv->crtc_funcs[pipe] = crtc_funcs;
546
547         return 0;
548 }
549
550 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
551 {
552         int pipe = drm_crtc_index(crtc);
553         struct rockchip_drm_private *priv = crtc->dev->dev_private;
554
555         if (pipe > ROCKCHIP_MAX_CRTC)
556                 return;
557
558         priv->crtc_funcs[pipe] = NULL;
559 }
560
561 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
562                                                 int pipe)
563 {
564         struct drm_crtc *crtc;
565         int i = 0;
566
567         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
568                 if (i++ == pipe)
569                         return crtc;
570
571         return NULL;
572 }
573
574 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
575                                            unsigned int pipe)
576 {
577         struct rockchip_drm_private *priv = dev->dev_private;
578         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
579
580         if (crtc && priv->crtc_funcs[pipe] &&
581             priv->crtc_funcs[pipe]->enable_vblank)
582                 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
583
584         return 0;
585 }
586
587 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
588                                              unsigned int pipe)
589 {
590         struct rockchip_drm_private *priv = dev->dev_private;
591         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
592
593         if (crtc && priv->crtc_funcs[pipe] &&
594             priv->crtc_funcs[pipe]->enable_vblank)
595                 priv->crtc_funcs[pipe]->disable_vblank(crtc);
596 }
597
598 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
599 {
600         struct rockchip_drm_private *private;
601         struct device *dev = drm_dev->dev;
602         struct drm_connector *connector;
603         struct iommu_group *group;
604         int ret;
605
606         private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
607         if (!private)
608                 return -ENOMEM;
609
610         mutex_init(&private->commit.lock);
611         INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
612
613         drm_dev->dev_private = private;
614
615 #ifdef CONFIG_DRM_DMA_SYNC
616         private->cpu_fence_context = fence_context_alloc(1);
617         atomic_set(&private->cpu_fence_seqno, 0);
618 #endif
619
620         drm_mode_config_init(drm_dev);
621
622         rockchip_drm_mode_config_init(drm_dev);
623
624         dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
625                                       GFP_KERNEL);
626         if (!dev->dma_parms) {
627                 ret = -ENOMEM;
628                 goto err_config_cleanup;
629         }
630
631         private->domain = iommu_domain_alloc(&platform_bus_type);
632         if (!private->domain)
633                 return -ENOMEM;
634
635         ret = iommu_get_dma_cookie(private->domain);
636         if (ret)
637                 goto err_free_domain;
638
639         group = iommu_group_get(dev);
640         if (!group) {
641                 group = iommu_group_alloc();
642                 if (IS_ERR(group)) {
643                         dev_err(dev, "Failed to allocate IOMMU group\n");
644                         goto err_put_cookie;
645                 }
646
647                 ret = iommu_group_add_device(group, dev);
648                 iommu_group_put(group);
649                 if (ret) {
650                         dev_err(dev, "failed to add device to IOMMU group\n");
651                         goto err_put_cookie;
652                 }
653         }
654         /*
655          * Attach virtual iommu device, sub iommu device can share the same
656          * mapping with it.
657          */
658         ret = rockchip_drm_dma_attach_device(drm_dev, dev);
659         if (ret)
660                 goto err_group_remove_device;
661
662         /* Try to bind all sub drivers. */
663         ret = component_bind_all(dev, drm_dev);
664         if (ret)
665                 goto err_detach_device;
666
667         /*
668          * All components are now added, we can publish the connector sysfs
669          * entries to userspace.  This will generate hotplug events and so
670          * userspace will expect to be able to access DRM at this point.
671          */
672         list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
673                         head) {
674                 ret = drm_connector_register(connector);
675                 if (ret) {
676                         dev_err(drm_dev->dev,
677                                 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
678                                 connector->base.id,
679                                 connector->name, ret);
680                         goto err_unbind;
681                 }
682         }
683
684         /* init kms poll for handling hpd */
685         drm_kms_helper_poll_init(drm_dev);
686
687         /*
688          * enable drm irq mode.
689          * - with irq_enabled = true, we can use the vblank feature.
690          */
691         drm_dev->irq_enabled = true;
692
693         ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
694         if (ret)
695                 goto err_kms_helper_poll_fini;
696
697         /*
698          * with vblank_disable_allowed = true, vblank interrupt will be disabled
699          * by drm timer once a current process gives up ownership of
700          * vblank event.(after drm_vblank_put function is called)
701          */
702         drm_dev->vblank_disable_allowed = true;
703
704         drm_mode_config_reset(drm_dev);
705
706         show_loader_logo(drm_dev);
707
708         ret = rockchip_drm_fbdev_init(drm_dev);
709         if (ret)
710                 goto err_vblank_cleanup;
711
712         return 0;
713 err_vblank_cleanup:
714         drm_vblank_cleanup(drm_dev);
715 err_kms_helper_poll_fini:
716         drm_kms_helper_poll_fini(drm_dev);
717 err_unbind:
718         component_unbind_all(dev, drm_dev);
719 err_detach_device:
720         rockchip_drm_dma_detach_device(drm_dev, dev);
721 err_group_remove_device:
722         iommu_group_remove_device(dev);
723 err_put_cookie:
724         iommu_put_dma_cookie(private->domain);
725 err_free_domain:
726         iommu_domain_free(private->domain);
727 err_config_cleanup:
728         drm_mode_config_cleanup(drm_dev);
729         drm_dev->dev_private = NULL;
730         return ret;
731 }
732
733 static int rockchip_drm_unload(struct drm_device *drm_dev)
734 {
735         struct device *dev = drm_dev->dev;
736         struct rockchip_drm_private *private = drm_dev->dev_private;
737
738         rockchip_drm_fbdev_fini(drm_dev);
739         drm_vblank_cleanup(drm_dev);
740         drm_kms_helper_poll_fini(drm_dev);
741         component_unbind_all(dev, drm_dev);
742         rockchip_drm_dma_detach_device(drm_dev, dev);
743         iommu_group_remove_device(dev);
744         iommu_put_dma_cookie(private->domain);
745         iommu_domain_free(private->domain);
746         drm_mode_config_cleanup(drm_dev);
747         drm_dev->dev_private = NULL;
748
749         return 0;
750 }
751
752 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
753                                                     struct drm_file *file_priv)
754 {
755         struct rockchip_drm_private *priv = crtc->dev->dev_private;
756         int pipe = drm_crtc_index(crtc);
757
758         if (pipe < ROCKCHIP_MAX_CRTC &&
759             priv->crtc_funcs[pipe] &&
760             priv->crtc_funcs[pipe]->cancel_pending_vblank)
761                 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
762 }
763
764 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
765 {
766         if (!subdrv)
767                 return -EINVAL;
768
769         mutex_lock(&subdrv_list_mutex);
770         list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
771         mutex_unlock(&subdrv_list_mutex);
772
773         return 0;
774 }
775 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
776
777 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
778 {
779         if (!subdrv)
780                 return -EINVAL;
781
782         mutex_lock(&subdrv_list_mutex);
783         list_del(&subdrv->list);
784         mutex_unlock(&subdrv_list_mutex);
785
786         return 0;
787 }
788 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
789
790 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
791 {
792         struct rockchip_drm_file_private *file_priv;
793         struct drm_rockchip_subdrv *subdrv;
794         int ret = 0;
795
796         file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
797         if (!file_priv)
798                 return -ENOMEM;
799         INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
800
801         file->driver_priv = file_priv;
802
803         mutex_lock(&subdrv_list_mutex);
804         list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
805                 ret = subdrv->open(dev, subdrv->dev, file);
806                 if (ret) {
807                         mutex_unlock(&subdrv_list_mutex);
808                         goto err_free_file_priv;
809                 }
810         }
811         mutex_unlock(&subdrv_list_mutex);
812
813         return 0;
814
815 err_free_file_priv:
816         kfree(file_priv);
817         file_priv = NULL;
818
819         return ret;
820 }
821
822 static void rockchip_drm_preclose(struct drm_device *dev,
823                                   struct drm_file *file_priv)
824 {
825         struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
826         struct rockchip_gem_object_node *cur, *d;
827         struct drm_rockchip_subdrv *subdrv;
828         struct drm_crtc *crtc;
829
830         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
831                 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
832
833         mutex_lock(&dev->struct_mutex);
834         list_for_each_entry_safe(cur, d,
835                         &file_private->gem_cpu_acquire_list, list) {
836 #ifdef CONFIG_DRM_DMA_SYNC
837                 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
838                 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
839 #endif
840                 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
841                 kfree(cur);
842         }
843         /* since we are deleting the whole list, just initialize the header
844          * instead of calling list_del for every element
845          */
846         INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
847         mutex_unlock(&dev->struct_mutex);
848
849         mutex_lock(&subdrv_list_mutex);
850         list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
851                 subdrv->close(dev, subdrv->dev, file_priv);
852         mutex_unlock(&subdrv_list_mutex);
853 }
854
855 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
856 {
857         kfree(file->driver_priv);
858         file->driver_priv = NULL;
859 }
860
861 void rockchip_drm_lastclose(struct drm_device *dev)
862 {
863         struct rockchip_drm_private *priv = dev->dev_private;
864
865         drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
866 }
867
868 static const struct drm_ioctl_desc rockchip_ioctls[] = {
869         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
870                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
871         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
872                           rockchip_gem_map_offset_ioctl,
873                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
874         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
875                           rockchip_gem_cpu_acquire_ioctl,
876                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
877         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
878                           rockchip_gem_cpu_release_ioctl,
879                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
880         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
881                           DRM_AUTH | DRM_RENDER_ALLOW),
882         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
883                           rockchip_rga_set_cmdlist_ioctl,
884                           DRM_AUTH | DRM_RENDER_ALLOW),
885         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
886                           DRM_AUTH | DRM_RENDER_ALLOW),
887 };
888
889 static const struct file_operations rockchip_drm_driver_fops = {
890         .owner = THIS_MODULE,
891         .open = drm_open,
892         .mmap = rockchip_gem_mmap,
893         .poll = drm_poll,
894         .read = drm_read,
895         .unlocked_ioctl = drm_ioctl,
896 #ifdef CONFIG_COMPAT
897         .compat_ioctl = drm_compat_ioctl,
898 #endif
899         .release = drm_release,
900 };
901
902 const struct vm_operations_struct rockchip_drm_vm_ops = {
903         .open = drm_gem_vm_open,
904         .close = drm_gem_vm_close,
905 };
906
907 static struct drm_driver rockchip_drm_driver = {
908         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
909                                   DRIVER_PRIME | DRIVER_ATOMIC |
910                                   DRIVER_RENDER,
911         .load                   = rockchip_drm_load,
912         .unload                 = rockchip_drm_unload,
913         .preclose               = rockchip_drm_preclose,
914         .lastclose              = rockchip_drm_lastclose,
915         .get_vblank_counter     = drm_vblank_no_hw_counter,
916         .open                   = rockchip_drm_open,
917         .postclose              = rockchip_drm_postclose,
918         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
919         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
920         .gem_vm_ops             = &rockchip_drm_vm_ops,
921         .gem_free_object        = rockchip_gem_free_object,
922         .dumb_create            = rockchip_gem_dumb_create,
923         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
924         .dumb_destroy           = drm_gem_dumb_destroy,
925         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
926         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
927         .gem_prime_import       = drm_gem_prime_import,
928         .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
929         .gem_prime_export       = drm_gem_prime_export,
930         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
931         .gem_prime_vmap         = rockchip_gem_prime_vmap,
932         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
933         .gem_prime_mmap         = rockchip_gem_mmap_buf,
934         .ioctls                 = rockchip_ioctls,
935         .num_ioctls             = ARRAY_SIZE(rockchip_ioctls),
936         .fops                   = &rockchip_drm_driver_fops,
937         .name   = DRIVER_NAME,
938         .desc   = DRIVER_DESC,
939         .date   = DRIVER_DATE,
940         .major  = DRIVER_MAJOR,
941         .minor  = DRIVER_MINOR,
942 };
943
944 #ifdef CONFIG_PM_SLEEP
945 void rockchip_drm_fb_suspend(struct drm_device *drm)
946 {
947         struct rockchip_drm_private *priv = drm->dev_private;
948
949         console_lock();
950         drm_fb_helper_set_suspend(priv->fbdev_helper, 1);
951         console_unlock();
952 }
953
954 void rockchip_drm_fb_resume(struct drm_device *drm)
955 {
956         struct rockchip_drm_private *priv = drm->dev_private;
957
958         console_lock();
959         drm_fb_helper_set_suspend(priv->fbdev_helper, 0);
960         console_unlock();
961 }
962
963 static int rockchip_drm_sys_suspend(struct device *dev)
964 {
965         struct drm_device *drm = dev_get_drvdata(dev);
966         struct rockchip_drm_private *priv = drm->dev_private;
967
968         drm_kms_helper_poll_disable(drm);
969         rockchip_drm_fb_suspend(drm);
970
971         priv->state = drm_atomic_helper_suspend(drm);
972         if (IS_ERR(priv->state)) {
973                 rockchip_drm_fb_resume(drm);
974                 drm_kms_helper_poll_enable(drm);
975                 return PTR_ERR(priv->state);
976         }
977
978         return 0;
979 }
980
981 static int rockchip_drm_sys_resume(struct device *dev)
982 {
983         struct drm_device *drm = dev_get_drvdata(dev);
984         struct rockchip_drm_private *priv = drm->dev_private;
985
986         drm_atomic_helper_resume(drm, priv->state);
987         rockchip_drm_fb_resume(drm);
988         drm_kms_helper_poll_enable(drm);
989
990         return 0;
991 }
992 #endif
993
994 static const struct dev_pm_ops rockchip_drm_pm_ops = {
995         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
996                                 rockchip_drm_sys_resume)
997 };
998
999 static int compare_of(struct device *dev, void *data)
1000 {
1001         struct device_node *np = data;
1002
1003         return dev->of_node == np;
1004 }
1005
1006 static void rockchip_add_endpoints(struct device *dev,
1007                                    struct component_match **match,
1008                                    struct device_node *port)
1009 {
1010         struct device_node *ep, *remote;
1011
1012         for_each_child_of_node(port, ep) {
1013                 remote = of_graph_get_remote_port_parent(ep);
1014                 if (!remote || !of_device_is_available(remote)) {
1015                         of_node_put(remote);
1016                         continue;
1017                 } else if (!of_device_is_available(remote->parent)) {
1018                         dev_warn(dev, "parent device of %s is not available\n",
1019                                  remote->full_name);
1020                         of_node_put(remote);
1021                         continue;
1022                 }
1023
1024                 component_match_add(dev, match, compare_of, remote);
1025                 of_node_put(remote);
1026         }
1027 }
1028
1029 static int rockchip_drm_bind(struct device *dev)
1030 {
1031         struct drm_device *drm;
1032         int ret;
1033
1034         drm = drm_dev_alloc(&rockchip_drm_driver, dev);
1035         if (!drm)
1036                 return -ENOMEM;
1037
1038         ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
1039         if (ret)
1040                 goto err_free;
1041
1042         ret = drm_dev_register(drm, 0);
1043         if (ret)
1044                 goto err_free;
1045
1046         dev_set_drvdata(dev, drm);
1047
1048         return 0;
1049
1050 err_free:
1051         drm_dev_unref(drm);
1052         return ret;
1053 }
1054
1055 static void rockchip_drm_unbind(struct device *dev)
1056 {
1057         struct drm_device *drm = dev_get_drvdata(dev);
1058
1059         drm_dev_unregister(drm);
1060         drm_dev_unref(drm);
1061         dev_set_drvdata(dev, NULL);
1062 }
1063
1064 static const struct component_master_ops rockchip_drm_ops = {
1065         .bind = rockchip_drm_bind,
1066         .unbind = rockchip_drm_unbind,
1067 };
1068
1069 static int rockchip_drm_platform_probe(struct platform_device *pdev)
1070 {
1071         struct device *dev = &pdev->dev;
1072         struct component_match *match = NULL;
1073         struct device_node *np = dev->of_node;
1074         struct device_node *port;
1075         int i;
1076
1077         if (!np)
1078                 return -ENODEV;
1079         /*
1080          * Bind the crtc ports first, so that
1081          * drm_of_find_possible_crtcs called from encoder .bind callbacks
1082          * works as expected.
1083          */
1084         for (i = 0;; i++) {
1085                 port = of_parse_phandle(np, "ports", i);
1086                 if (!port)
1087                         break;
1088
1089                 if (!of_device_is_available(port->parent)) {
1090                         of_node_put(port);
1091                         continue;
1092                 }
1093
1094                 component_match_add(dev, &match, compare_of, port->parent);
1095                 of_node_put(port);
1096         }
1097
1098         if (i == 0) {
1099                 dev_err(dev, "missing 'ports' property\n");
1100                 return -ENODEV;
1101         }
1102
1103         if (!match) {
1104                 dev_err(dev, "No available vop found for display-subsystem.\n");
1105                 return -ENODEV;
1106         }
1107         /*
1108          * For each bound crtc, bind the encoders attached to its
1109          * remote endpoint.
1110          */
1111         for (i = 0;; i++) {
1112                 port = of_parse_phandle(np, "ports", i);
1113                 if (!port)
1114                         break;
1115
1116                 if (!of_device_is_available(port->parent)) {
1117                         of_node_put(port);
1118                         continue;
1119                 }
1120
1121                 rockchip_add_endpoints(dev, &match, port);
1122                 of_node_put(port);
1123         }
1124
1125         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
1126 }
1127
1128 static int rockchip_drm_platform_remove(struct platform_device *pdev)
1129 {
1130         component_master_del(&pdev->dev, &rockchip_drm_ops);
1131
1132         return 0;
1133 }
1134
1135 static const struct of_device_id rockchip_drm_dt_ids[] = {
1136         { .compatible = "rockchip,display-subsystem", },
1137         { /* sentinel */ },
1138 };
1139 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
1140
1141 static struct platform_driver rockchip_drm_platform_driver = {
1142         .probe = rockchip_drm_platform_probe,
1143         .remove = rockchip_drm_platform_remove,
1144         .driver = {
1145                 .name = "rockchip-drm",
1146                 .of_match_table = rockchip_drm_dt_ids,
1147                 .pm = &rockchip_drm_pm_ops,
1148         },
1149 };
1150
1151 module_platform_driver(rockchip_drm_platform_driver);
1152
1153 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1154 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
1155 MODULE_LICENSE("GPL v2");