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