1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4  * Author: James.Qian.Wang <james.qian.wang@arm.com>
5  *
6  */
7 #include <linux/component.h>
8 #include <linux/interrupt.h>
9 
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_fb_helper.h>
14 #include <drm/drm_gem_cma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_irq.h>
17 #include <drm/drm_managed.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_vblank.h>
20 
21 #include "komeda_dev.h"
22 #include "komeda_framebuffer.h"
23 #include "komeda_kms.h"
24 
25 DEFINE_DRM_GEM_CMA_FOPS(komeda_cma_fops);
26 
27 static int komeda_gem_cma_dumb_create(struct drm_file *file,
28 				      struct drm_device *dev,
29 				      struct drm_mode_create_dumb *args)
30 {
31 	struct komeda_dev *mdev = dev->dev_private;
32 	u32 pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
33 
34 	args->pitch = ALIGN(pitch, mdev->chip.bus_width);
35 
36 	return drm_gem_cma_dumb_create_internal(file, dev, args);
37 }
38 
39 static irqreturn_t komeda_kms_irq_handler(int irq, void *data)
40 {
41 	struct drm_device *drm = data;
42 	struct komeda_dev *mdev = drm->dev_private;
43 	struct komeda_kms_dev *kms = to_kdev(drm);
44 	struct komeda_events evts;
45 	irqreturn_t status;
46 	u32 i;
47 
48 	/* Call into the CHIP to recognize events */
49 	memset(&evts, 0, sizeof(evts));
50 	status = mdev->funcs->irq_handler(mdev, &evts);
51 
52 	komeda_print_events(&evts, drm);
53 
54 	/* Notify the crtc to handle the events */
55 	for (i = 0; i < kms->n_crtcs; i++)
56 		komeda_crtc_handle_event(&kms->crtcs[i], &evts);
57 
58 	return status;
59 }
60 
61 static struct drm_driver komeda_kms_driver = {
62 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
63 	.lastclose			= drm_fb_helper_lastclose,
64 	DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(komeda_gem_cma_dumb_create),
65 	.fops = &komeda_cma_fops,
66 	.name = "komeda",
67 	.desc = "Arm Komeda Display Processor driver",
68 	.date = "20181101",
69 	.major = 0,
70 	.minor = 1,
71 };
72 
73 static void komeda_kms_commit_tail(struct drm_atomic_state *old_state)
74 {
75 	struct drm_device *dev = old_state->dev;
76 
77 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
78 
79 	drm_atomic_helper_commit_planes(dev, old_state,
80 					DRM_PLANE_COMMIT_ACTIVE_ONLY);
81 
82 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
83 
84 	drm_atomic_helper_wait_for_flip_done(dev, old_state);
85 
86 	drm_atomic_helper_commit_hw_done(old_state);
87 
88 	drm_atomic_helper_cleanup_planes(dev, old_state);
89 }
90 
91 static const struct drm_mode_config_helper_funcs komeda_mode_config_helpers = {
92 	.atomic_commit_tail = komeda_kms_commit_tail,
93 };
94 
95 static int komeda_plane_state_list_add(struct drm_plane_state *plane_st,
96 				       struct list_head *zorder_list)
97 {
98 	struct komeda_plane_state *new = to_kplane_st(plane_st);
99 	struct komeda_plane_state *node, *last;
100 
101 	last = list_empty(zorder_list) ?
102 	       NULL : list_last_entry(zorder_list, typeof(*last), zlist_node);
103 
104 	/* Considering the list sequence is zpos increasing, so if list is empty
105 	 * or the zpos of new node bigger than the last node in list, no need
106 	 * loop and just insert the new one to the tail of the list.
107 	 */
108 	if (!last || (new->base.zpos > last->base.zpos)) {
109 		list_add_tail(&new->zlist_node, zorder_list);
110 		return 0;
111 	}
112 
113 	/* Build the list by zpos increasing */
114 	list_for_each_entry(node, zorder_list, zlist_node) {
115 		if (new->base.zpos < node->base.zpos) {
116 			list_add_tail(&new->zlist_node, &node->zlist_node);
117 			break;
118 		} else if (node->base.zpos == new->base.zpos) {
119 			struct drm_plane *a = node->base.plane;
120 			struct drm_plane *b = new->base.plane;
121 
122 			/* Komeda doesn't support setting a same zpos for
123 			 * different planes.
124 			 */
125 			DRM_DEBUG_ATOMIC("PLANE: %s and PLANE: %s are configured same zpos: %d.\n",
126 					 a->name, b->name, node->base.zpos);
127 			return -EINVAL;
128 		}
129 	}
130 
131 	return 0;
132 }
133 
134 static int komeda_crtc_normalize_zpos(struct drm_crtc *crtc,
135 				      struct drm_crtc_state *crtc_st)
136 {
137 	struct drm_atomic_state *state = crtc_st->state;
138 	struct komeda_crtc *kcrtc = to_kcrtc(crtc);
139 	struct komeda_crtc_state *kcrtc_st = to_kcrtc_st(crtc_st);
140 	struct komeda_plane_state *kplane_st;
141 	struct drm_plane_state *plane_st;
142 	struct drm_plane *plane;
143 	struct list_head zorder_list;
144 	int order = 0, err;
145 
146 	DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
147 			 crtc->base.id, crtc->name);
148 
149 	INIT_LIST_HEAD(&zorder_list);
150 
151 	/* This loop also added all effected planes into the new state */
152 	drm_for_each_plane_mask(plane, crtc->dev, crtc_st->plane_mask) {
153 		plane_st = drm_atomic_get_plane_state(state, plane);
154 		if (IS_ERR(plane_st))
155 			return PTR_ERR(plane_st);
156 
157 		/* Build a list by zpos increasing */
158 		err = komeda_plane_state_list_add(plane_st, &zorder_list);
159 		if (err)
160 			return err;
161 	}
162 
163 	kcrtc_st->max_slave_zorder = 0;
164 
165 	list_for_each_entry(kplane_st, &zorder_list, zlist_node) {
166 		plane_st = &kplane_st->base;
167 		plane = plane_st->plane;
168 
169 		plane_st->normalized_zpos = order++;
170 		/* When layer_split has been enabled, one plane will be handled
171 		 * by two separated komeda layers (left/right), which may needs
172 		 * two zorders.
173 		 * - zorder: for left_layer for left display part.
174 		 * - zorder + 1: will be reserved for right layer.
175 		 */
176 		if (to_kplane_st(plane_st)->layer_split)
177 			order++;
178 
179 		DRM_DEBUG_ATOMIC("[PLANE:%d:%s] zpos:%d, normalized zpos: %d\n",
180 				 plane->base.id, plane->name,
181 				 plane_st->zpos, plane_st->normalized_zpos);
182 
183 		/* calculate max slave zorder */
184 		if (has_bit(drm_plane_index(plane), kcrtc->slave_planes))
185 			kcrtc_st->max_slave_zorder =
186 				max(plane_st->normalized_zpos,
187 				    kcrtc_st->max_slave_zorder);
188 	}
189 
190 	crtc_st->zpos_changed = true;
191 
192 	return 0;
193 }
194 
195 static int komeda_kms_check(struct drm_device *dev,
196 			    struct drm_atomic_state *state)
197 {
198 	struct drm_crtc *crtc;
199 	struct drm_crtc_state *new_crtc_st;
200 	int i, err;
201 
202 	err = drm_atomic_helper_check_modeset(dev, state);
203 	if (err)
204 		return err;
205 
206 	/* Komeda need to re-calculate resource assumption in every commit
207 	 * so need to add all affected_planes (even unchanged) to
208 	 * drm_atomic_state.
209 	 */
210 	for_each_new_crtc_in_state(state, crtc, new_crtc_st, i) {
211 		err = drm_atomic_add_affected_planes(state, crtc);
212 		if (err)
213 			return err;
214 
215 		err = komeda_crtc_normalize_zpos(crtc, new_crtc_st);
216 		if (err)
217 			return err;
218 	}
219 
220 	err = drm_atomic_helper_check_planes(dev, state);
221 	if (err)
222 		return err;
223 
224 	return 0;
225 }
226 
227 static const struct drm_mode_config_funcs komeda_mode_config_funcs = {
228 	.fb_create		= komeda_fb_create,
229 	.atomic_check		= komeda_kms_check,
230 	.atomic_commit		= drm_atomic_helper_commit,
231 };
232 
233 static void komeda_kms_mode_config_init(struct komeda_kms_dev *kms,
234 					struct komeda_dev *mdev)
235 {
236 	struct drm_mode_config *config = &kms->base.mode_config;
237 
238 	drm_mode_config_init(&kms->base);
239 
240 	komeda_kms_setup_crtcs(kms, mdev);
241 
242 	/* Get value from dev */
243 	config->min_width	= 0;
244 	config->min_height	= 0;
245 	config->max_width	= 4096;
246 	config->max_height	= 4096;
247 	config->allow_fb_modifiers = true;
248 
249 	config->funcs = &komeda_mode_config_funcs;
250 	config->helper_private = &komeda_mode_config_helpers;
251 }
252 
253 struct komeda_kms_dev *komeda_kms_attach(struct komeda_dev *mdev)
254 {
255 	struct komeda_kms_dev *kms;
256 	struct drm_device *drm;
257 	int err;
258 
259 	kms = devm_drm_dev_alloc(mdev->dev, &komeda_kms_driver,
260 				 struct komeda_kms_dev, base);
261 	if (IS_ERR(kms))
262 		return kms;
263 
264 	drm = &kms->base;
265 
266 	drm->dev_private = mdev;
267 
268 	komeda_kms_mode_config_init(kms, mdev);
269 
270 	err = komeda_kms_add_private_objs(kms, mdev);
271 	if (err)
272 		goto cleanup_mode_config;
273 
274 	err = komeda_kms_add_planes(kms, mdev);
275 	if (err)
276 		goto cleanup_mode_config;
277 
278 	err = drm_vblank_init(drm, kms->n_crtcs);
279 	if (err)
280 		goto cleanup_mode_config;
281 
282 	err = komeda_kms_add_crtcs(kms, mdev);
283 	if (err)
284 		goto cleanup_mode_config;
285 
286 	err = komeda_kms_add_wb_connectors(kms, mdev);
287 	if (err)
288 		goto cleanup_mode_config;
289 
290 	err = component_bind_all(mdev->dev, kms);
291 	if (err)
292 		goto cleanup_mode_config;
293 
294 	drm_mode_config_reset(drm);
295 
296 	err = devm_request_irq(drm->dev, mdev->irq,
297 			       komeda_kms_irq_handler, IRQF_SHARED,
298 			       drm->driver->name, drm);
299 	if (err)
300 		goto free_component_binding;
301 
302 	drm->irq_enabled = true;
303 
304 	drm_kms_helper_poll_init(drm);
305 
306 	err = drm_dev_register(drm, 0);
307 	if (err)
308 		goto free_interrupts;
309 
310 	return kms;
311 
312 free_interrupts:
313 	drm_kms_helper_poll_fini(drm);
314 	drm->irq_enabled = false;
315 free_component_binding:
316 	component_unbind_all(mdev->dev, drm);
317 cleanup_mode_config:
318 	drm_mode_config_cleanup(drm);
319 	komeda_kms_cleanup_private_objs(kms);
320 	drm->dev_private = NULL;
321 	return ERR_PTR(err);
322 }
323 
324 void komeda_kms_detach(struct komeda_kms_dev *kms)
325 {
326 	struct drm_device *drm = &kms->base;
327 	struct komeda_dev *mdev = drm->dev_private;
328 
329 	drm_dev_unregister(drm);
330 	drm_kms_helper_poll_fini(drm);
331 	drm_atomic_helper_shutdown(drm);
332 	drm->irq_enabled = false;
333 	component_unbind_all(mdev->dev, drm);
334 	drm_mode_config_cleanup(drm);
335 	komeda_kms_cleanup_private_objs(kms);
336 	drm->dev_private = NULL;
337 }
338