msm_prop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include "msm_prop.h"
  6. void msm_property_init(struct msm_property_info *info,
  7. struct drm_mode_object *base,
  8. struct drm_device *dev,
  9. struct drm_property **property_array,
  10. struct msm_property_data *property_data,
  11. uint32_t property_count,
  12. uint32_t blob_count,
  13. uint32_t state_size)
  14. {
  15. /* prevent access if any of these are NULL */
  16. if (!base || !dev || !property_array || !property_data) {
  17. property_count = 0;
  18. blob_count = 0;
  19. DRM_ERROR("invalid arguments, forcing zero properties\n");
  20. return;
  21. }
  22. /* can't have more blob properties than total properties */
  23. if (blob_count > property_count) {
  24. blob_count = property_count;
  25. DBG("Capping number of blob properties to %d", blob_count);
  26. }
  27. if (!info) {
  28. DRM_ERROR("info pointer is NULL\n");
  29. } else {
  30. info->base = base;
  31. info->dev = dev;
  32. info->property_array = property_array;
  33. info->property_data = property_data;
  34. info->property_count = property_count;
  35. info->blob_count = blob_count;
  36. info->install_request = 0;
  37. info->install_count = 0;
  38. info->recent_idx = 0;
  39. info->is_active = false;
  40. info->state_size = state_size;
  41. info->state_cache_size = 0;
  42. mutex_init(&info->property_lock);
  43. memset(property_data,
  44. 0,
  45. sizeof(struct msm_property_data) *
  46. property_count);
  47. }
  48. }
  49. void msm_property_destroy(struct msm_property_info *info)
  50. {
  51. if (!info)
  52. return;
  53. /* free state cache */
  54. while (info->state_cache_size > 0)
  55. kfree(info->state_cache[--(info->state_cache_size)]);
  56. mutex_destroy(&info->property_lock);
  57. }
  58. int msm_property_pop_dirty(struct msm_property_info *info,
  59. struct msm_property_state *property_state)
  60. {
  61. struct list_head *item;
  62. int rc = 0;
  63. if (!info || !property_state || !property_state->values) {
  64. DRM_ERROR("invalid argument(s)\n");
  65. return -EINVAL;
  66. }
  67. mutex_lock(&info->property_lock);
  68. if (list_empty(&property_state->dirty_list)) {
  69. rc = -EAGAIN;
  70. } else {
  71. item = property_state->dirty_list.next;
  72. list_del_init(item);
  73. rc = container_of(item, struct msm_property_value, dirty_node)
  74. - property_state->values;
  75. DRM_DEBUG_KMS("property %d dirty\n", rc);
  76. }
  77. mutex_unlock(&info->property_lock);
  78. return rc;
  79. }
  80. /**
  81. * _msm_property_set_dirty_no_lock - flag given property as being dirty
  82. * This function doesn't mutex protect the
  83. * dirty linked list.
  84. * @info: Pointer to property info container struct
  85. * @property_state: Pointer to property state container struct
  86. * @property_idx: Property index
  87. */
  88. static void _msm_property_set_dirty_no_lock(
  89. struct msm_property_info *info,
  90. struct msm_property_state *property_state,
  91. uint32_t property_idx)
  92. {
  93. if (!info || !property_state || !property_state->values ||
  94. property_idx >= info->property_count) {
  95. DRM_ERROR("invalid argument(s), idx %u\n", property_idx);
  96. return;
  97. }
  98. /* avoid re-inserting if already dirty */
  99. if (!list_empty(&property_state->values[property_idx].dirty_node)) {
  100. DRM_DEBUG_KMS("property %u already dirty\n", property_idx);
  101. return;
  102. }
  103. list_add_tail(&property_state->values[property_idx].dirty_node,
  104. &property_state->dirty_list);
  105. }
  106. bool msm_property_is_dirty(
  107. struct msm_property_info *info,
  108. struct msm_property_state *property_state,
  109. uint32_t property_idx)
  110. {
  111. if (!info || !property_state || !property_state->values ||
  112. property_idx >= info->property_count) {
  113. DRM_ERROR("invalid argument(s), idx %u\n", property_idx);
  114. return false;
  115. }
  116. return !list_empty(&property_state->values[property_idx].dirty_node);
  117. }
  118. /**
  119. * _msm_property_install_integer - install standard drm range property
  120. * @info: Pointer to property info container struct
  121. * @name: Property name
  122. * @flags: Other property type flags, e.g. DRM_MODE_PROP_IMMUTABLE
  123. * @min: Min property value
  124. * @max: Max property value
  125. * @init: Default Property value
  126. * @property_idx: Property index
  127. * @force_dirty: Whether or not to filter 'dirty' status on unchanged values
  128. */
  129. static void _msm_property_install_integer(struct msm_property_info *info,
  130. const char *name, int flags, uint64_t min, uint64_t max,
  131. uint64_t init, uint32_t property_idx, bool force_dirty)
  132. {
  133. struct drm_property **prop;
  134. if (!info)
  135. return;
  136. ++info->install_request;
  137. if (!name || (property_idx >= info->property_count)) {
  138. DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
  139. } else {
  140. prop = &info->property_array[property_idx];
  141. /*
  142. * Properties need to be attached to each drm object that
  143. * uses them, but only need to be created once
  144. */
  145. if (!*prop) {
  146. *prop = drm_property_create_range(info->dev,
  147. flags, name, min, max);
  148. if (!*prop)
  149. DRM_ERROR("create %s property failed\n", name);
  150. }
  151. /* save init value for later */
  152. info->property_data[property_idx].default_value = init;
  153. info->property_data[property_idx].force_dirty = force_dirty;
  154. /* always attach property, if created */
  155. if (*prop) {
  156. drm_object_attach_property(info->base, *prop, init);
  157. ++info->install_count;
  158. }
  159. }
  160. }
  161. void msm_property_install_range(struct msm_property_info *info,
  162. const char *name, int flags, uint64_t min, uint64_t max,
  163. uint64_t init, uint32_t property_idx)
  164. {
  165. _msm_property_install_integer(info, name, flags,
  166. min, max, init, property_idx, false);
  167. }
  168. void msm_property_install_volatile_range(struct msm_property_info *info,
  169. const char *name, int flags, uint64_t min, uint64_t max,
  170. uint64_t init, uint32_t property_idx)
  171. {
  172. _msm_property_install_integer(info, name, flags,
  173. min, max, init, property_idx, true);
  174. }
  175. void msm_property_install_enum(struct msm_property_info *info,
  176. const char *name, int flags, int is_bitmask,
  177. const struct drm_prop_enum_list *values, int num_values,
  178. uint32_t property_idx)
  179. {
  180. struct drm_property **prop;
  181. if (!info)
  182. return;
  183. ++info->install_request;
  184. if (!name || !values || !num_values ||
  185. (property_idx >= info->property_count)) {
  186. DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
  187. } else {
  188. prop = &info->property_array[property_idx];
  189. /*
  190. * Properties need to be attached to each drm object that
  191. * uses them, but only need to be created once
  192. */
  193. if (!*prop) {
  194. /* 'bitmask' is a special type of 'enum' */
  195. if (is_bitmask)
  196. *prop = drm_property_create_bitmask(info->dev,
  197. DRM_MODE_PROP_BITMASK | flags,
  198. name, values, num_values, -1);
  199. else
  200. *prop = drm_property_create_enum(info->dev,
  201. DRM_MODE_PROP_ENUM | flags,
  202. name, values, num_values);
  203. if (!*prop)
  204. DRM_ERROR("create %s property failed\n", name);
  205. }
  206. /* save init value for later */
  207. info->property_data[property_idx].default_value = 0;
  208. info->property_data[property_idx].force_dirty = false;
  209. /* select first defined value for enums */
  210. if (!is_bitmask)
  211. info->property_data[property_idx].default_value =
  212. values->type;
  213. /* always attach property, if created */
  214. if (*prop) {
  215. drm_object_attach_property(info->base, *prop,
  216. info->property_data
  217. [property_idx].default_value);
  218. ++info->install_count;
  219. }
  220. }
  221. }
  222. void msm_property_install_blob(struct msm_property_info *info,
  223. const char *name, int flags, uint32_t property_idx)
  224. {
  225. struct drm_property **prop;
  226. if (!info)
  227. return;
  228. ++info->install_request;
  229. if (!name || (property_idx >= info->blob_count)) {
  230. DRM_ERROR("invalid argument(s), %s\n", name ? name : "null");
  231. } else {
  232. prop = &info->property_array[property_idx];
  233. /*
  234. * Properties need to be attached to each drm object that
  235. * uses them, but only need to be created once
  236. */
  237. if (!*prop) {
  238. /* use 'create' for blob property place holder */
  239. *prop = drm_property_create(info->dev,
  240. DRM_MODE_PROP_BLOB | flags, name, 0);
  241. if (!*prop)
  242. DRM_ERROR("create %s property failed\n", name);
  243. }
  244. /* save init value for later */
  245. info->property_data[property_idx].default_value = 0;
  246. info->property_data[property_idx].force_dirty = true;
  247. /* always attach property, if created */
  248. if (*prop) {
  249. drm_object_attach_property(info->base, *prop, -1);
  250. ++info->install_count;
  251. }
  252. }
  253. }
  254. int msm_property_install_get_status(struct msm_property_info *info)
  255. {
  256. int rc = -ENOMEM;
  257. if (info && (info->install_request == info->install_count))
  258. rc = 0;
  259. return rc;
  260. }
  261. int msm_property_index(struct msm_property_info *info,
  262. struct drm_property *property)
  263. {
  264. uint32_t count;
  265. int32_t idx;
  266. int rc = -EINVAL;
  267. if (!info || !property) {
  268. DRM_ERROR("invalid argument(s)\n");
  269. } else {
  270. /*
  271. * Linear search, but start from last found index. This will
  272. * help if any single property is accessed multiple times in a
  273. * row. Ideally, we could keep a list of properties sorted in
  274. * the order of most recent access, but that may be overkill
  275. * for now.
  276. */
  277. mutex_lock(&info->property_lock);
  278. idx = info->recent_idx;
  279. count = info->property_count;
  280. while (count) {
  281. --count;
  282. /* stop searching on match */
  283. if (info->property_array[idx] == property) {
  284. info->recent_idx = idx;
  285. rc = idx;
  286. break;
  287. }
  288. /* move to next valid index */
  289. if (--idx < 0)
  290. idx = info->property_count - 1;
  291. }
  292. mutex_unlock(&info->property_lock);
  293. }
  294. return rc;
  295. }
  296. int msm_property_set_dirty(struct msm_property_info *info,
  297. struct msm_property_state *property_state,
  298. int property_idx)
  299. {
  300. if (!info || !property_state || !property_state->values) {
  301. DRM_ERROR("invalid argument(s)\n");
  302. return -EINVAL;
  303. }
  304. mutex_lock(&info->property_lock);
  305. _msm_property_set_dirty_no_lock(info, property_state, property_idx);
  306. mutex_unlock(&info->property_lock);
  307. return 0;
  308. }
  309. int msm_property_atomic_set(struct msm_property_info *info,
  310. struct msm_property_state *property_state,
  311. struct drm_property *property, uint64_t val)
  312. {
  313. struct drm_property_blob *blob;
  314. int property_idx, rc = -EINVAL;
  315. if (!info || !property_state) {
  316. DRM_ERROR("invalid argument(s)\n");
  317. return -EINVAL;
  318. }
  319. property_idx = msm_property_index(info, property);
  320. if ((property_idx == -EINVAL) || !property_state->values) {
  321. DRM_ERROR("invalid argument(s)\n");
  322. } else {
  323. /* extra handling for incoming properties */
  324. mutex_lock(&info->property_lock);
  325. if ((property->flags & DRM_MODE_PROP_BLOB) &&
  326. (property_idx < info->blob_count)) {
  327. /* need to clear previous ref */
  328. if (property_state->values[property_idx].blob)
  329. drm_property_blob_put(
  330. property_state->values[
  331. property_idx].blob);
  332. /* DRM lookup also takes a reference */
  333. blob = drm_property_lookup_blob(info->dev,
  334. (uint32_t)val);
  335. if (val && !blob) {
  336. DRM_ERROR("prop %d blob id 0x%llx not found\n",
  337. property_idx, val);
  338. val = 0;
  339. } else {
  340. if (blob) {
  341. DBG("Blob %u saved", blob->base.id);
  342. val = blob->base.id;
  343. }
  344. /* save the new blob */
  345. property_state->values[property_idx].blob =
  346. blob;
  347. }
  348. }
  349. /* update value and flag as dirty */
  350. if (property_state->values[property_idx].value != val ||
  351. info->property_data[property_idx].force_dirty) {
  352. property_state->values[property_idx].value = val;
  353. _msm_property_set_dirty_no_lock(info, property_state,
  354. property_idx);
  355. DBG("%s - %lld", property->name, val);
  356. }
  357. mutex_unlock(&info->property_lock);
  358. rc = 0;
  359. }
  360. return rc;
  361. }
  362. int msm_property_atomic_get(struct msm_property_info *info,
  363. struct msm_property_state *property_state,
  364. struct drm_property *property, uint64_t *val)
  365. {
  366. int property_idx, rc = -EINVAL;
  367. property_idx = msm_property_index(info, property);
  368. if (!info || (property_idx == -EINVAL) ||
  369. !property_state->values || !val) {
  370. DRM_DEBUG("Invalid argument(s)\n");
  371. } else {
  372. mutex_lock(&info->property_lock);
  373. *val = property_state->values[property_idx].value;
  374. mutex_unlock(&info->property_lock);
  375. rc = 0;
  376. }
  377. return rc;
  378. }
  379. void *msm_property_alloc_state(struct msm_property_info *info)
  380. {
  381. void *state = NULL;
  382. if (!info) {
  383. DRM_ERROR("invalid property info\n");
  384. return NULL;
  385. }
  386. mutex_lock(&info->property_lock);
  387. if (info->state_cache_size)
  388. state = info->state_cache[--(info->state_cache_size)];
  389. mutex_unlock(&info->property_lock);
  390. if (!state && info->state_size)
  391. state = kmalloc(info->state_size, GFP_KERNEL);
  392. if (!state)
  393. DRM_ERROR("failed to allocate state\n");
  394. return state;
  395. }
  396. /**
  397. * _msm_property_free_state - helper function for freeing local state objects
  398. * @info: Pointer to property info container struct
  399. * @st: Pointer to state object
  400. */
  401. static void _msm_property_free_state(struct msm_property_info *info, void *st)
  402. {
  403. if (!info || !st)
  404. return;
  405. mutex_lock(&info->property_lock);
  406. if (info->state_cache_size < MSM_PROP_STATE_CACHE_SIZE)
  407. info->state_cache[(info->state_cache_size)++] = st;
  408. else
  409. kfree(st);
  410. mutex_unlock(&info->property_lock);
  411. }
  412. void msm_property_reset_state(struct msm_property_info *info, void *state,
  413. struct msm_property_state *property_state,
  414. struct msm_property_value *property_values)
  415. {
  416. uint32_t i;
  417. if (!info) {
  418. DRM_ERROR("invalid property info\n");
  419. return;
  420. }
  421. if (state)
  422. memset(state, 0, info->state_size);
  423. if (property_state) {
  424. property_state->property_count = info->property_count;
  425. property_state->values = property_values;
  426. INIT_LIST_HEAD(&property_state->dirty_list);
  427. }
  428. /*
  429. * Assign default property values. This helper is mostly used
  430. * to initialize newly created state objects.
  431. */
  432. if (property_values)
  433. for (i = 0; i < info->property_count; ++i) {
  434. property_values[i].value =
  435. info->property_data[i].default_value;
  436. property_values[i].blob = NULL;
  437. INIT_LIST_HEAD(&property_values[i].dirty_node);
  438. }
  439. }
  440. void msm_property_duplicate_state(struct msm_property_info *info,
  441. void *old_state, void *state,
  442. struct msm_property_state *property_state,
  443. struct msm_property_value *property_values)
  444. {
  445. uint32_t i;
  446. if (!info || !old_state || !state) {
  447. DRM_ERROR("invalid argument(s)\n");
  448. return;
  449. }
  450. memcpy(state, old_state, info->state_size);
  451. if (!property_state)
  452. return;
  453. INIT_LIST_HEAD(&property_state->dirty_list);
  454. property_state->values = property_values;
  455. if (property_state->values)
  456. /* add ref count for blobs and initialize dirty nodes */
  457. for (i = 0; i < info->property_count; ++i) {
  458. if (property_state->values[i].blob)
  459. drm_property_blob_get(
  460. property_state->values[i].blob);
  461. INIT_LIST_HEAD(&property_state->values[i].dirty_node);
  462. }
  463. }
  464. void msm_property_destroy_state(struct msm_property_info *info, void *state,
  465. struct msm_property_state *property_state)
  466. {
  467. uint32_t i;
  468. if (!info || !state) {
  469. DRM_ERROR("invalid argument(s)\n");
  470. return;
  471. }
  472. if (property_state && property_state->values) {
  473. /* remove ref count for blobs */
  474. for (i = 0; i < info->property_count; ++i)
  475. if (property_state->values[i].blob) {
  476. drm_property_blob_put(
  477. property_state->values[i].blob);
  478. property_state->values[i].blob = NULL;
  479. }
  480. }
  481. _msm_property_free_state(info, state);
  482. }
  483. void *msm_property_get_blob(struct msm_property_info *info,
  484. struct msm_property_state *property_state,
  485. size_t *byte_len,
  486. uint32_t property_idx)
  487. {
  488. struct drm_property_blob *blob;
  489. size_t len = 0;
  490. void *rc = 0;
  491. if (!info || !property_state || !property_state->values ||
  492. (property_idx >= info->blob_count)) {
  493. DRM_ERROR("invalid argument(s)\n");
  494. } else {
  495. blob = property_state->values[property_idx].blob;
  496. if (blob) {
  497. len = blob->length;
  498. rc = blob->data;
  499. }
  500. }
  501. if (byte_len)
  502. *byte_len = len;
  503. return rc;
  504. }
  505. int msm_property_set_blob(struct msm_property_info *info,
  506. struct drm_property_blob **blob_reference,
  507. void *blob_data,
  508. size_t byte_len,
  509. uint32_t property_idx)
  510. {
  511. struct drm_property_blob *blob = NULL;
  512. int rc = -EINVAL;
  513. if (!info || !blob_reference || (property_idx >= info->blob_count)) {
  514. DRM_ERROR("invalid argument(s)\n");
  515. } else {
  516. /* create blob */
  517. if (blob_data && byte_len) {
  518. blob = drm_property_create_blob(info->dev,
  519. byte_len,
  520. blob_data);
  521. if (IS_ERR_OR_NULL(blob)) {
  522. rc = PTR_ERR(blob);
  523. DRM_ERROR("failed to create blob, %d\n", rc);
  524. goto exit;
  525. }
  526. }
  527. /* update drm object */
  528. rc = drm_object_property_set_value(info->base,
  529. info->property_array[property_idx],
  530. blob ? blob->base.id : 0);
  531. if (rc) {
  532. DRM_ERROR("failed to set blob to property\n");
  533. if (blob)
  534. drm_property_blob_put(blob);
  535. goto exit;
  536. }
  537. /* update local reference */
  538. if (*blob_reference)
  539. drm_property_blob_put(*blob_reference);
  540. *blob_reference = blob;
  541. }
  542. exit:
  543. return rc;
  544. }
  545. int msm_property_set_property(struct msm_property_info *info,
  546. struct msm_property_state *property_state,
  547. uint32_t property_idx,
  548. uint64_t val)
  549. {
  550. int rc = -EINVAL;
  551. if (!info || (property_idx >= info->property_count) ||
  552. property_idx < info->blob_count ||
  553. !property_state || !property_state->values) {
  554. DRM_ERROR("invalid argument(s)\n");
  555. } else {
  556. struct drm_property *drm_prop;
  557. mutex_lock(&info->property_lock);
  558. /* update cached value */
  559. property_state->values[property_idx].value = val;
  560. /* update the new default value for immutables */
  561. drm_prop = info->property_array[property_idx];
  562. if (drm_prop->flags & DRM_MODE_PROP_IMMUTABLE)
  563. info->property_data[property_idx].default_value = val;
  564. mutex_unlock(&info->property_lock);
  565. /* update drm object */
  566. rc = drm_object_property_set_value(info->base, drm_prop, val);
  567. if (rc)
  568. DRM_ERROR("failed set property value, idx %d rc %d\n",
  569. property_idx, rc);
  570. }
  571. return rc;
  572. }