blob: f589f8499eea8eee2f969f4478da1223922fed58 [file] [log] [blame]
Clarence Ipdd8021c2016-07-20 16:39:47 -04001/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "sde-drm:[%s] " fmt, __func__
14#include "msm_drv.h"
15
16#include "sde_kms.h"
17#include "sde_connector.h"
18
Clarence Ipcb3afd42016-07-15 16:25:34 -040019int sde_connector_get_info(struct drm_connector *connector,
20 struct msm_display_info *info)
21{
22 struct sde_connector *c_conn;
23
24 if (!connector || !info) {
25 SDE_ERROR("invalid argument(s), conn %pK, info %pK\n",
26 connector, info);
27 return -EINVAL;
28 }
29
30 c_conn = to_sde_connector(connector);
31
32 if (!c_conn->display || !c_conn->ops.get_info) {
33 SDE_ERROR("display info not supported for %pK\n",
34 c_conn->display);
35 return -EINVAL;
36 }
37
38 return c_conn->ops.get_info(info, c_conn->display);
39}
40
Clarence Ipdd8021c2016-07-20 16:39:47 -040041static void sde_connector_destroy(struct drm_connector *connector)
42{
43 struct sde_connector *c_conn;
44
45 if (!connector) {
46 SDE_ERROR("invalid connector\n");
47 return;
48 }
49
50 c_conn = to_sde_connector(connector);
51
52 if (c_conn->blob_sde_info)
53 drm_property_unreference_blob(c_conn->blob_sde_info);
54 msm_property_destroy(&c_conn->property_info);
55
56 drm_connector_unregister(connector);
Clarence Ipe59fb3f2016-07-26 13:39:59 -040057 sde_fence_deinit(&c_conn->retire_fence);
Clarence Ipdd8021c2016-07-20 16:39:47 -040058 drm_connector_cleanup(connector);
59 kfree(c_conn);
60}
61
62/**
63 * _sde_connector_destroy_fb - clean up connector state's out_fb buffer
64 * @c_conn: Pointer to sde connector structure
65 * @c_state: Pointer to sde connector state structure
66 */
67static void _sde_connector_destroy_fb(struct sde_connector *c_conn,
68 struct sde_connector_state *c_state)
69{
70 if (!c_state || !c_state->out_fb) {
71 SDE_ERROR("invalid state %pK\n", c_state);
72 return;
73 }
74
75 msm_framebuffer_cleanup(c_state->out_fb,
76 c_state->mmu_id);
77 drm_framebuffer_unreference(c_state->out_fb);
78 c_state->out_fb = NULL;
79
80 if (c_conn) {
81 c_state->property_values[CONNECTOR_PROP_OUT_FB] =
82 msm_property_get_default(&c_conn->property_info,
83 CONNECTOR_PROP_OUT_FB);
84 } else {
85 c_state->property_values[CONNECTOR_PROP_OUT_FB] = ~0;
86 }
87}
88
89static void sde_connector_atomic_destroy_state(struct drm_connector *connector,
90 struct drm_connector_state *state)
91{
92 struct sde_connector *c_conn = NULL;
93 struct sde_connector_state *c_state = NULL;
94
95 if (!state) {
96 SDE_ERROR("invalid state\n");
97 return;
98 }
99
100 /*
101 * The base DRM framework currently always passes in a NULL
102 * connector pointer. This is not correct, but attempt to
103 * handle that case as much as possible.
104 */
105 if (connector)
106 c_conn = to_sde_connector(connector);
107 c_state = to_sde_connector_state(state);
108
109 if (c_state->out_fb)
110 _sde_connector_destroy_fb(c_conn, c_state);
111
112 if (!c_conn) {
113 kfree(c_state);
114 } else {
115 /* destroy value helper */
116 msm_property_destroy_state(&c_conn->property_info, c_state,
117 c_state->property_values, 0);
118 }
119}
120
121static void sde_connector_atomic_reset(struct drm_connector *connector)
122{
123 struct sde_connector *c_conn;
124 struct sde_connector_state *c_state;
125
126 if (!connector) {
127 SDE_ERROR("invalid connector\n");
128 return;
129 }
130
131 c_conn = to_sde_connector(connector);
132
133 if (connector->state) {
134 sde_connector_atomic_destroy_state(connector, connector->state);
135 connector->state = 0;
136 }
137
138 c_state = msm_property_alloc_state(&c_conn->property_info);
139 if (!c_state) {
140 SDE_ERROR("state alloc failed\n");
141 return;
142 }
143
144 /* reset value helper, zero out state structure and reset properties */
145 msm_property_reset_state(&c_conn->property_info, c_state,
146 c_state->property_values, 0);
147
148 c_state->base.connector = connector;
149 connector->state = &c_state->base;
150}
151
152static struct drm_connector_state *
153sde_connector_atomic_duplicate_state(struct drm_connector *connector)
154{
155 struct sde_connector *c_conn;
156 struct sde_connector_state *c_state, *c_oldstate;
157 int rc;
158
159 if (!connector || !connector->state) {
160 SDE_ERROR("invalid connector %pK\n", connector);
161 return NULL;
162 }
163
164 c_conn = to_sde_connector(connector);
165 c_oldstate = to_sde_connector_state(connector->state);
166 c_state = msm_property_alloc_state(&c_conn->property_info);
167 if (!c_state) {
168 SDE_ERROR("state alloc failed\n");
169 return NULL;
170 }
171
172 /* duplicate value helper */
173 msm_property_duplicate_state(&c_conn->property_info,
174 c_oldstate, c_state, c_state->property_values, 0);
175
176 /* additional handling for drm framebuffer objects */
177 if (c_state->out_fb) {
178 drm_framebuffer_reference(c_state->out_fb);
179 rc = msm_framebuffer_prepare(c_state->out_fb,
180 c_state->mmu_id);
181 if (rc)
182 SDE_ERROR("failed to prepare fb, %d\n", rc);
183 }
184
185 return &c_state->base;
186}
187
188static int sde_connector_atomic_set_property(struct drm_connector *connector,
189 struct drm_connector_state *state,
190 struct drm_property *property,
191 uint64_t val)
192{
193 struct sde_connector *c_conn;
194 struct sde_connector_state *c_state;
195 int idx, rc;
196
197 if (!connector || !state || !property) {
198 SDE_ERROR("invalid argument(s), conn %pK, state %pK, prp %pK\n",
199 connector, state, property);
200 return -EINVAL;
201 }
202
203 c_conn = to_sde_connector(connector);
204 c_state = to_sde_connector_state(state);
205
206 /* generic property handling */
207 rc = msm_property_atomic_set(&c_conn->property_info,
208 c_state->property_values, 0, property, val);
209 if (rc)
210 goto end;
211
212 /* connector-specific property handling */
213 idx = msm_property_index(&c_conn->property_info, property);
214
215 if (idx == CONNECTOR_PROP_OUT_FB) {
216 /* clear old fb, if present */
217 if (c_state->out_fb)
218 _sde_connector_destroy_fb(c_conn, c_state);
219
220 /* convert fb val to drm framebuffer and prepare it */
221 c_state->out_fb =
222 drm_framebuffer_lookup(connector->dev, val);
223 if (!c_state->out_fb) {
224 SDE_ERROR("failed to look up fb %lld\n", val);
225 rc = -EFAULT;
226 } else {
227 c_state->mmu_id = c_conn->mmu_id;
228
229 rc = msm_framebuffer_prepare(c_state->out_fb,
230 c_state->mmu_id);
231 if (rc)
232 SDE_ERROR("prep fb failed, %d\n", rc);
233 }
234 }
235
236 /* check for custom property handling */
237 if (!rc && c_conn->ops.set_property) {
238 rc = c_conn->ops.set_property(connector,
239 state,
240 idx,
241 val,
242 c_conn->display);
243
244 /* potentially clean up out_fb if rc != 0 */
245 if ((idx == CONNECTOR_PROP_OUT_FB) && rc)
246 _sde_connector_destroy_fb(c_conn, c_state);
247 }
248end:
249 return rc;
250}
251
252static int sde_connector_set_property(struct drm_connector *connector,
253 struct drm_property *property,
254 uint64_t val)
255{
256 if (!connector) {
257 SDE_ERROR("invalid connector\n");
258 return -EINVAL;
259 }
260
261 return sde_connector_atomic_set_property(connector,
262 connector->state, property, val);
263}
264
265static int sde_connector_atomic_get_property(struct drm_connector *connector,
266 const struct drm_connector_state *state,
267 struct drm_property *property,
268 uint64_t *val)
269{
270 struct sde_connector *c_conn;
271 struct sde_connector_state *c_state;
272 int idx, rc = -EINVAL;
273
274 if (!connector || !state) {
275 SDE_ERROR("invalid argument(s), conn %pK, state %pK\n",
276 connector, state);
277 return -EINVAL;
278 }
279
280 c_conn = to_sde_connector(connector);
281 c_state = to_sde_connector_state(state);
282
283 idx = msm_property_index(&c_conn->property_info, property);
Clarence Ipe59fb3f2016-07-26 13:39:59 -0400284 if (idx == CONNECTOR_PROP_RETIRE_FENCE)
285 rc = sde_fence_create(&c_conn->retire_fence, val);
286 else
287 /* get cached property value */
288 rc = msm_property_atomic_get(&c_conn->property_info,
289 c_state->property_values, 0, property, val);
Clarence Ipdd8021c2016-07-20 16:39:47 -0400290
291 /* allow for custom override */
292 if (c_conn->ops.get_property)
293 rc = c_conn->ops.get_property(connector,
294 (struct drm_connector_state *)state,
295 idx,
296 val,
297 c_conn->display);
298 return rc;
299}
300
Clarence Ipe59fb3f2016-07-26 13:39:59 -0400301void sde_connector_prepare_fence(struct drm_connector *connector)
302{
303 if (!connector) {
304 SDE_ERROR("invalid connector\n");
305 return;
306 }
307
308 sde_fence_prepare(&to_sde_connector(connector)->retire_fence);
309}
310
311void sde_connector_complete_commit(struct drm_connector *connector)
312{
313 if (!connector) {
314 SDE_ERROR("invalid connector\n");
315 return;
316 }
317
318 /* signal connector's retire fence */
319 sde_fence_signal(&to_sde_connector(connector)->retire_fence, 0);
320}
321
Clarence Ipdd8021c2016-07-20 16:39:47 -0400322static enum drm_connector_status
323sde_connector_detect(struct drm_connector *connector, bool force)
324{
325 enum drm_connector_status status = connector_status_unknown;
326 struct sde_connector *c_conn;
327
328 if (!connector) {
329 SDE_ERROR("invalid connector\n");
330 return status;
331 }
332
333 c_conn = to_sde_connector(connector);
334
335 if (c_conn->ops.detect)
336 status = c_conn->ops.detect(connector,
337 force,
338 c_conn->display);
339
340 return status;
341}
342
343static const struct drm_connector_funcs sde_connector_ops = {
344 .dpms = drm_atomic_helper_connector_dpms,
345 .reset = sde_connector_atomic_reset,
346 .detect = sde_connector_detect,
347 .destroy = sde_connector_destroy,
348 .fill_modes = drm_helper_probe_single_connector_modes,
349 .atomic_duplicate_state = sde_connector_atomic_duplicate_state,
350 .atomic_destroy_state = sde_connector_atomic_destroy_state,
351 .atomic_set_property = sde_connector_atomic_set_property,
352 .atomic_get_property = sde_connector_atomic_get_property,
353 .set_property = sde_connector_set_property,
354};
355
356static int sde_connector_get_modes(struct drm_connector *connector)
357{
358 struct sde_connector *c_conn;
359
360 if (!connector) {
361 SDE_ERROR("invalid connector\n");
362 return 0;
363 }
364
365 c_conn = to_sde_connector(connector);
366 if (!c_conn->ops.get_modes) {
367 SDE_DEBUG("missing get_modes callback\n");
368 return 0;
369 }
370
371 return c_conn->ops.get_modes(connector, c_conn->display);
372}
373
374static enum drm_mode_status
375sde_connector_mode_valid(struct drm_connector *connector,
376 struct drm_display_mode *mode)
377{
378 struct sde_connector *c_conn;
379
380 if (!connector || !mode) {
381 SDE_ERROR("invalid argument(s), conn %pK, mode %pK\n",
382 connector, mode);
383 return MODE_ERROR;
384 }
385
386 c_conn = to_sde_connector(connector);
387
388 if (c_conn->ops.mode_valid)
389 return c_conn->ops.mode_valid(connector, mode, c_conn->display);
390
391 /* assume all modes okay by default */
392 return MODE_OK;
393}
394
395static struct drm_encoder *
396sde_connector_best_encoder(struct drm_connector *connector)
397{
398 struct sde_connector *c_conn = to_sde_connector(connector);
399
400 if (!connector) {
401 SDE_ERROR("invalid connector\n");
402 return NULL;
403 }
404
405 /*
406 * This is true for now, revisit this code when multiple encoders are
407 * supported.
408 */
409 return c_conn->encoder;
410}
411
412static const struct drm_connector_helper_funcs sde_connector_helper_ops = {
413 .get_modes = sde_connector_get_modes,
414 .mode_valid = sde_connector_mode_valid,
415 .best_encoder = sde_connector_best_encoder,
416};
417
418struct drm_connector *sde_connector_init(struct drm_device *dev,
419 struct drm_encoder *encoder,
420 struct drm_panel *panel,
421 void *display,
422 const struct sde_connector_ops *ops,
423 int connector_poll,
424 int connector_type)
425{
426 struct msm_drm_private *priv;
427 struct sde_kms *sde_kms;
428 struct sde_kms_info *info;
429 struct sde_connector *c_conn = NULL;
430 int rc;
431
432 if (!dev || !dev->dev_private || !encoder) {
433 SDE_ERROR("invalid argument(s), dev %pK, enc %pK\n",
434 dev, encoder);
435 return ERR_PTR(-EINVAL);
436 }
437
438 priv = dev->dev_private;
439 if (!priv->kms) {
440 SDE_ERROR("invalid kms reference\n");
441 return ERR_PTR(-EINVAL);
442 }
443
444 c_conn = kzalloc(sizeof(*c_conn), GFP_KERNEL);
445 if (!c_conn) {
446 SDE_ERROR("failed to alloc sde connector\n");
447 return ERR_PTR(-ENOMEM);
448 }
449
450 rc = drm_connector_init(dev,
451 &c_conn->base,
452 &sde_connector_ops,
453 connector_type);
454 if (rc)
455 goto error_free_conn;
456
457 c_conn->connector_type = connector_type;
458 c_conn->encoder = encoder;
459 c_conn->panel = panel;
460 c_conn->display = display;
461
462 sde_kms = to_sde_kms(priv->kms);
463 c_conn->mmu_id = sde_kms->mmu_id;
464
465 if (ops)
466 c_conn->ops = *ops;
467
468 c_conn->base.helper_private = &sde_connector_helper_ops;
469 c_conn->base.polled = connector_poll;
470 c_conn->base.interlace_allowed = 0;
471 c_conn->base.doublescan_allowed = 0;
472
473 snprintf(c_conn->name,
474 SDE_CONNECTOR_NAME_SIZE,
475 "conn%u",
476 c_conn->base.base.id);
477
Clarence Ipe59fb3f2016-07-26 13:39:59 -0400478 /*
479 * Initialize retire fence support. Set fence offset to 0 for virtual
480 * connectors so that the fence signals at the end of the current commit
481 * and 1 for others so that the fence signals after one additional
482 * commit.
483 */
484 rc = sde_fence_init(dev, &c_conn->retire_fence, c_conn->name,
485 connector_type == DRM_MODE_CONNECTOR_VIRTUAL ? 0 : 1);
486 if (rc) {
487 SDE_ERROR("failed to init fence, %d\n", rc);
488 goto error_cleanup_conn;
489 }
490
Clarence Ipdd8021c2016-07-20 16:39:47 -0400491 rc = drm_connector_register(&c_conn->base);
492 if (rc) {
493 SDE_ERROR("failed to register drm connector, %d\n", rc);
Clarence Ipe59fb3f2016-07-26 13:39:59 -0400494 goto error_cleanup_fence;
Clarence Ipdd8021c2016-07-20 16:39:47 -0400495 }
496
497 rc = drm_mode_connector_attach_encoder(&c_conn->base, encoder);
498 if (rc) {
499 SDE_ERROR("failed to attach encoder to connector, %d\n", rc);
500 goto error_unregister_conn;
501 }
502
503 /* create properties */
504 msm_property_init(&c_conn->property_info, &c_conn->base.base, dev,
505 priv->conn_property, c_conn->property_data,
506 CONNECTOR_PROP_COUNT, CONNECTOR_PROP_BLOBCOUNT,
507 sizeof(struct sde_connector_state));
508
509 if (c_conn->ops.post_init) {
510 info = kmalloc(sizeof(*info), GFP_KERNEL);
511 if (!info) {
512 SDE_ERROR("failed to allocate info buffer\n");
513 rc = -ENOMEM;
514 goto error_unregister_conn;
515 }
516
517 sde_kms_info_reset(info);
518 rc = c_conn->ops.post_init(&c_conn->base, info, display);
519 if (rc) {
520 SDE_ERROR("post-init failed, %d\n", rc);
521 kfree(info);
522 goto error_unregister_conn;
523 }
524
525 msm_property_install_blob(&c_conn->property_info,
526 "sde_info",
527 DRM_MODE_PROP_IMMUTABLE,
528 CONNECTOR_PROP_SDE_INFO);
529
530 msm_property_set_blob(&c_conn->property_info,
531 &c_conn->blob_sde_info,
532 SDE_KMS_INFO_DATA(info),
533 SDE_KMS_INFO_DATALEN(info),
534 CONNECTOR_PROP_SDE_INFO);
535 kfree(info);
536 }
537
Clarence Ipe59fb3f2016-07-26 13:39:59 -0400538 msm_property_install_range(&c_conn->property_info, "RETIRE_FENCE",
539 0, ~0, ~0, CONNECTOR_PROP_RETIRE_FENCE);
540
Clarence Ipdd8021c2016-07-20 16:39:47 -0400541 rc = msm_property_install_get_status(&c_conn->property_info);
542 if (rc) {
543 SDE_ERROR("failed to create one or more properties\n");
544 goto error_destroy_property;
545 }
546
547 priv->connectors[priv->num_connectors++] = &c_conn->base;
548
549 return &c_conn->base;
550
551error_destroy_property:
552 if (c_conn->blob_sde_info)
553 drm_property_unreference_blob(c_conn->blob_sde_info);
554 msm_property_destroy(&c_conn->property_info);
555error_unregister_conn:
556 drm_connector_unregister(&c_conn->base);
Clarence Ipe59fb3f2016-07-26 13:39:59 -0400557error_cleanup_fence:
558 sde_fence_deinit(&c_conn->retire_fence);
Clarence Ipdd8021c2016-07-20 16:39:47 -0400559error_cleanup_conn:
560 drm_connector_cleanup(&c_conn->base);
561error_free_conn:
562 kfree(c_conn);
563
564 return ERR_PTR(rc);
565}
566