1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "msm_prop.h"
void msm_property_init(struct msm_property_info *info,
struct drm_mode_object *base,
struct drm_device *dev,
struct drm_property **property_array,
struct msm_property_data *property_data,
uint32_t property_count,
uint32_t blob_count,
uint32_t state_size)
{
int i;
/* prevent access if any of these are NULL */
if (!base || !dev || !property_array || !property_data) {
property_count = 0;
blob_count = 0;
DRM_ERROR("invalid arguments, forcing zero properties\n");
return;
}
/* can't have more blob properties than total properties */
if (blob_count > property_count) {
blob_count = property_count;
DBG("Capping number of blob properties to %d", blob_count);
}
if (!info) {
DRM_ERROR("info pointer is NULL\n");
} else {
info->base = base;
info->dev = dev;
info->property_array = property_array;
info->property_data = property_data;
info->property_count = property_count;
info->blob_count = blob_count;
info->install_request = 0;
info->install_count = 0;
info->recent_idx = 0;
info->is_active = false;
info->state_size = state_size;
info->state_cache_size = 0;
mutex_init(&info->property_lock);
memset(property_data,
0,
sizeof(struct msm_property_data) *
property_count);
INIT_LIST_HEAD(&info->dirty_list);
for (i = 0; i < property_count; ++i)
INIT_LIST_HEAD(&property_data[i].dirty_node);
}
}
void msm_property_destroy(struct msm_property_info *info)
{
if (!info)
return;
/* reset dirty list */
INIT_LIST_HEAD(&info->dirty_list);
/* free state cache */
while (info->state_cache_size > 0)
kfree(info->state_cache[--(info->state_cache_size)]);
mutex_destroy(&info->property_lock);
}
int msm_property_pop_dirty(struct msm_property_info *info)
{
struct list_head *item;
int rc = 0;
if (!info) {
DRM_ERROR("invalid info\n");
return -EINVAL;
}
mutex_lock(&info->property_lock);
if (list_empty(&info->dirty_list)) {
rc = -EAGAIN;
} else {
item = info->dirty_list.next;
list_del_init(item);
rc = container_of(item, struct msm_property_data, dirty_node)
- info->property_data;
DRM_DEBUG_KMS("property %d dirty\n", rc);
}
mutex_unlock(&info->property_lock);
return rc;
}
/**
* _msm_property_set_dirty_no_lock - flag given property as being dirty
* This function doesn't mutex protect the
* dirty linked list.
* @info: Pointer to property info container struct
* @property_idx: Property index
*/
static void _msm_property_set_dirty_no_lock(
struct msm_property_info *info,
uint32_t property_idx)
{
if (!info || property_idx >= info->property_count) {
DRM_ERROR("invalid argument(s), info %pK, idx %u\n",
info, property_idx);
return;
}
/* avoid re-inserting if already dirty */
if (!list_empty(&info->property_data[property_idx].dirty_node)) {
DRM_DEBUG_KMS("property %u already dirty\n", property_idx);
return;
}
list_add_tail(&info->property_data[property_idx].dirty_node,
&info->dirty_list);
}
/**
* _msm_property_install_integer - install standard drm range property
* @info: Pointer to property info container struct
* @name: Property name
* @flags: Other property type flags, e.g. DRM_MODE_PROP_IMMUTABLE
* @min: Min property value
* @max: Max property value
* @init: Default Property value
* @property_idx: Property index
* @force_dirty: Whether or not to filter 'dirty' status on unchanged values
*/
static void _msm_property_install_integer(struct msm_property_info *info,
const char *name, int flags, uint64_t min, uint64_t max,
uint64_t init, uint32_t property_idx, bool force_dirty)
{
struct drm_property **prop;
if (!info)
return;
++info->install_request;
if (!name || (property_idx >= info->property_count)) {
DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
} else {
prop = &info->property_array[property_idx];
/*
* Properties need to be attached to each drm object that
* uses them, but only need to be created once
*/
if (*prop == 0) {
*prop = drm_property_create_range(info->dev,
flags, name, min, max);
if (*prop == 0)
DRM_ERROR("create %s property failed\n", name);
}
/* save init value for later */
info->property_data[property_idx].default_value = init;
info->property_data[property_idx].force_dirty = force_dirty;
/* always attach property, if created */
if (*prop) {
drm_object_attach_property(info->base, *prop, init);
++info->install_count;
}
}
}
/**
* _msm_property_install_integer - install signed drm range property
* @info: Pointer to property info container struct
* @name: Property name
* @flags: Other property type flags, e.g. DRM_MODE_PROP_IMMUTABLE
* @min: Min property value
* @max: Max property value
* @init: Default Property value
* @property_idx: Property index
* @force_dirty: Whether or not to filter 'dirty' status on unchanged values
*/
static void _msm_property_install_signed_integer(struct msm_property_info *info,
const char *name, int flags, int64_t min, int64_t max,
int64_t init, uint32_t property_idx, bool force_dirty)
{
struct drm_property **prop;
if (!info)
return;
++info->install_request;
if (!name || (property_idx >= info->property_count)) {
DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
} else {
prop = &info->property_array[property_idx];
/*
* Properties need to be attached to each drm object that
* uses them, but only need to be created once
*/
if (*prop == 0) {
*prop = drm_property_create_signed_range(info->dev,
flags, name, min, max);
if (*prop == 0)
DRM_ERROR("create %s property failed\n", name);
}
/* save init value for later */
info->property_data[property_idx].default_value = I642U64(init);
info->property_data[property_idx].force_dirty = force_dirty;
/* always attach property, if created */
if (*prop) {
drm_object_attach_property(info->base, *prop, init);
++info->install_count;
}
}
}
void msm_property_install_range(struct msm_property_info *info,
const char *name, int flags, uint64_t min, uint64_t max,
uint64_t init, uint32_t property_idx)
{
_msm_property_install_integer(info, name, flags,
min, max, init, property_idx, false);
}
void msm_property_install_volatile_range(struct msm_property_info *info,
const char *name, int flags, uint64_t min, uint64_t max,
uint64_t init, uint32_t property_idx)
{
_msm_property_install_integer(info, name, flags,
min, max, init, property_idx, true);
}
void msm_property_install_signed_range(struct msm_property_info *info,
const char *name, int flags, int64_t min, int64_t max,
int64_t init, uint32_t property_idx)
{
_msm_property_install_signed_integer(info, name, flags,
min, max, init, property_idx, false);
}
void msm_property_install_volatile_signed_range(struct msm_property_info *info,
const char *name, int flags, int64_t min, int64_t max,
int64_t init, uint32_t property_idx)
{
_msm_property_install_signed_integer(info, name, flags,
min, max, init, property_idx, true);
}
void msm_property_install_rotation(struct msm_property_info *info,
unsigned int supported_rotations, uint32_t property_idx)
{
struct drm_property **prop;
if (!info)
return;
++info->install_request;
if (property_idx >= info->property_count) {
DRM_ERROR("invalid property index %d\n", property_idx);
} else {
prop = &info->property_array[property_idx];
/*
* Properties need to be attached to each drm object that
* uses them, but only need to be created once
*/
if (*prop == 0) {
*prop = drm_mode_create_rotation_property(info->dev,
supported_rotations);
if (*prop == 0)
DRM_ERROR("create rotation property failed\n");
}
/* save init value for later */
info->property_data[property_idx].default_value = 0;
info->property_data[property_idx].force_dirty = false;
/* always attach property, if created */
if (*prop) {
drm_object_attach_property(info->base, *prop, 0);
++info->install_count;
}
}
}
void msm_property_install_enum(struct msm_property_info *info,
const char *name, int flags, int is_bitmask,
const struct drm_prop_enum_list *values, int num_values,
uint32_t property_idx, uint64_t default_value)
{
struct drm_property **prop;
if (!info)
return;
++info->install_request;
if (!name || !values || !num_values ||
(property_idx >= info->property_count)) {
DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
} else {
prop = &info->property_array[property_idx];
/*
* Properties need to be attached to each drm object that
* uses them, but only need to be created once
*/
if (*prop == 0) {
/* 'bitmask' is a special type of 'enum' */
if (is_bitmask)
*prop = drm_property_create_bitmask(info->dev,
DRM_MODE_PROP_BITMASK | flags,
name, values, num_values, -1);
else
*prop = drm_property_create_enum(info->dev,
DRM_MODE_PROP_ENUM | flags,
name, values, num_values);
if (*prop == 0)
DRM_ERROR("create %s property failed\n", name);
}
/* save init value for later */
info->property_data[property_idx].default_value = default_value;
info->property_data[property_idx].force_dirty = false;
/* select first defined value for enums */
if (!is_bitmask)
info->property_data[property_idx].default_value =
values->type;
/* always attach property, if created */
if (*prop) {
drm_object_attach_property(info->base, *prop,
info->property_data
[property_idx].default_value);
++info->install_count;
}
}
}
void msm_property_install_blob(struct msm_property_info *info,
const char *name, int flags, uint32_t property_idx)
{
struct drm_property **prop;
if (!info)
return;
++info->install_request;
if (!name || (property_idx >= info->blob_count)) {
DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
} else {
prop = &info->property_array[property_idx];
/*
* Properties need to be attached to each drm object that
* uses them, but only need to be created once
*/
if (*prop == 0) {
/* use 'create' for blob property place holder */
*prop = drm_property_create(info->dev,
DRM_MODE_PROP_BLOB | flags, name, 0);
if (*prop == 0)
DRM_ERROR("create %s property failed\n", name);
}
/* save init value for later */
info->property_data[property_idx].default_value = 0;
info->property_data[property_idx].force_dirty = true;
/* always attach property, if created */
if (*prop) {
drm_object_attach_property(info->base, *prop, -1);
++info->install_count;
}
}
}
int msm_property_install_get_status(struct msm_property_info *info)
{
int rc = -ENOMEM;
if (info && (info->install_request == info->install_count))
rc = 0;
return rc;
}
int msm_property_index(struct msm_property_info *info,
struct drm_property *property)
{
uint32_t count;
int32_t idx;
int rc = -EINVAL;
if (!info || !property) {
DRM_ERROR("invalid argument(s)\n");
} else {
/*
* Linear search, but start from last found index. This will
* help if any single property is accessed multiple times in a
* row. Ideally, we could keep a list of properties sorted in
* the order of most recent access, but that may be overkill
* for now.
*/
mutex_lock(&info->property_lock);
idx = info->recent_idx;
count = info->property_count;
while (count) {
--count;
/* stop searching on match */
if (info->property_array[idx] == property) {
info->recent_idx = idx;
rc = idx;
break;
}
/* move to next valid index */
if (--idx < 0)
idx = info->property_count - 1;
}
mutex_unlock(&info->property_lock);
}
return rc;
}
int msm_property_atomic_set(struct msm_property_info *info,
uint64_t *property_values,
struct drm_property_blob **property_blobs,
struct drm_property *property, uint64_t val)
{
struct drm_property_blob *blob;
int property_idx, rc = -EINVAL;
property_idx = msm_property_index(info, property);
if (!info || (property_idx == -EINVAL) || !property_values) {
DRM_DEBUG("Invalid argument(s)\n");
} else {
/* extra handling for incoming properties */
mutex_lock(&info->property_lock);
if ((property->flags & DRM_MODE_PROP_BLOB) &&
(property_idx < info->blob_count) &&
property_blobs) {
/* DRM lookup also takes a reference */
blob = drm_property_lookup_blob(info->dev,
(uint32_t)val);
if (!blob) {
DRM_ERROR("blob not found\n");
val = 0;
} else {
DBG("Blob %u saved", blob->base.id);
val = blob->base.id;
/* save blob - need to clear previous ref */
if (property_blobs[property_idx])
drm_property_unreference_blob(
property_blobs[property_idx]);
property_blobs[property_idx] = blob;
}
}
/* update value and flag as dirty */
if (property_values[property_idx] != val ||
info->property_data[property_idx].force_dirty) {
property_values[property_idx] = val;
_msm_property_set_dirty_no_lock(info, property_idx);
DBG("%s - %lld", property->name, val);
}
mutex_unlock(&info->property_lock);
rc = 0;
}
return rc;
}
int msm_property_atomic_get(struct msm_property_info *info,
uint64_t *property_values,
struct drm_property_blob **property_blobs,
struct drm_property *property, uint64_t *val)
{
int property_idx, rc = -EINVAL;
property_idx = msm_property_index(info, property);
if (!info || (property_idx == -EINVAL) || !property_values || !val) {
DRM_DEBUG("Invalid argument(s)\n");
} else {
mutex_lock(&info->property_lock);
*val = property_values[property_idx];
mutex_unlock(&info->property_lock);
rc = 0;
}
return rc;
}
void *msm_property_alloc_state(struct msm_property_info *info)
{
void *state = NULL;
if (!info) {
DRM_ERROR("invalid property info\n");
return NULL;
}
mutex_lock(&info->property_lock);
if (info->state_cache_size)
state = info->state_cache[--(info->state_cache_size)];
mutex_unlock(&info->property_lock);
if (!state && info->state_size)
state = kmalloc(info->state_size, GFP_KERNEL);
if (!state)
DRM_ERROR("failed to allocate state\n");
return state;
}
/**
* _msm_property_free_state - helper function for freeing local state objects
* @info: Pointer to property info container struct
* @st: Pointer to state object
*/
static void _msm_property_free_state(struct msm_property_info *info, void *st)
{
if (!info || !st)
return;
mutex_lock(&info->property_lock);
if (info->state_cache_size < MSM_PROP_STATE_CACHE_SIZE)
info->state_cache[(info->state_cache_size)++] = st;
else
kfree(st);
mutex_unlock(&info->property_lock);
}
void msm_property_reset_state(struct msm_property_info *info, void *state,
uint64_t *property_values,
struct drm_property_blob **property_blobs)
{
uint32_t i;
if (!info) {
DRM_ERROR("invalid property info\n");
return;
}
if (state)
memset(state, 0, info->state_size);
/*
* Assign default property values. This helper is mostly used
* to initialize newly created state objects.
*/
if (property_values)
for (i = 0; i < info->property_count; ++i)
property_values[i] =
info->property_data[i].default_value;
if (property_blobs)
for (i = 0; i < info->blob_count; ++i)
property_blobs[i] = 0;
}
void msm_property_duplicate_state(struct msm_property_info *info,
void *old_state, void *state,
uint64_t *property_values,
struct drm_property_blob **property_blobs)
{
uint32_t i;
if (!info || !old_state || !state) {
DRM_ERROR("invalid argument(s)\n");
return;
}
memcpy(state, old_state, info->state_size);
if (property_blobs) {
/* add ref count for blobs */
for (i = 0; i < info->blob_count; ++i)
if (property_blobs[i])
drm_property_reference_blob(property_blobs[i]);
}
}
void msm_property_destroy_state(struct msm_property_info *info, void *state,
uint64_t *property_values,
struct drm_property_blob **property_blobs)
{
uint32_t i;
if (!info || !state) {
DRM_ERROR("invalid argument(s)\n");
return;
}
if (property_blobs) {
/* remove ref count for blobs */
for (i = 0; i < info->blob_count; ++i)
if (property_blobs[i])
drm_property_unreference_blob(
property_blobs[i]);
}
_msm_property_free_state(info, state);
}
void *msm_property_get_blob(struct msm_property_info *info,
struct drm_property_blob **property_blobs,
size_t *byte_len,
uint32_t property_idx)
{
struct drm_property_blob *blob;
size_t len = 0;
void *rc = 0;
if (!info || !property_blobs || (property_idx >= info->blob_count)) {
DRM_ERROR("invalid argument(s)\n");
} else {
blob = property_blobs[property_idx];
if (blob) {
len = blob->length;
rc = &blob->data;
}
}
if (byte_len)
*byte_len = len;
return rc;
}
int msm_property_set_blob(struct msm_property_info *info,
struct drm_property_blob **blob_reference,
void *blob_data,
size_t byte_len,
uint32_t property_idx)
{
struct drm_property_blob *blob = NULL;
int rc = -EINVAL;
if (!info || !blob_reference || (property_idx >= info->blob_count)) {
DRM_ERROR("invalid argument(s)\n");
} else {
/* create blob */
if (blob_data && byte_len) {
blob = drm_property_create_blob(info->dev,
byte_len,
blob_data);
if (IS_ERR_OR_NULL(blob)) {
rc = PTR_ERR(blob);
DRM_ERROR("failed to create blob, %d\n", rc);
goto exit;
}
}
/* update drm object */
rc = drm_object_property_set_value(info->base,
info->property_array[property_idx],
blob ? blob->base.id : 0);
if (rc) {
DRM_ERROR("failed to set blob to property\n");
if (blob)
drm_property_unreference_blob(blob);
goto exit;
}
/* update local reference */
if (*blob_reference)
drm_property_unreference_blob(*blob_reference);
*blob_reference = blob;
}
exit:
return rc;
}
int msm_property_set_property(struct msm_property_info *info,
uint64_t *property_values,
uint32_t property_idx,
uint64_t val)
{
int rc = -EINVAL;
if (!info || (property_idx >= info->property_count) ||
property_idx < info->blob_count || !property_values) {
DRM_ERROR("invalid argument(s)\n");
} else {
struct drm_property *drm_prop;
mutex_lock(&info->property_lock);
/* update cached value */
if (property_values)
property_values[property_idx] = val;
/* update the new default value for immutables */
drm_prop = info->property_array[property_idx];
if (drm_prop->flags & DRM_MODE_PROP_IMMUTABLE)
info->property_data[property_idx].default_value = val;
mutex_unlock(&info->property_lock);
/* update drm object */
rc = drm_object_property_set_value(info->base, drm_prop, val);
if (rc)
DRM_ERROR("failed set property value, idx %d rc %d\n",
property_idx, rc);
}
return rc;
}
|