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