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