intel_atomic.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright © 2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * DOC: atomic modeset support
  25. *
  26. * The functions here implement the state management and hardware programming
  27. * dispatch required by the atomic modeset infrastructure.
  28. * See intel_atomic_plane.c for the plane-specific atomic functionality.
  29. */
  30. #include <drm/drmP.h>
  31. #include <drm/drm_atomic.h>
  32. #include <drm/drm_atomic_helper.h>
  33. #include <drm/drm_plane_helper.h>
  34. #include "intel_drv.h"
  35. /**
  36. * intel_connector_atomic_get_property - fetch connector property value
  37. * @connector: connector to fetch property for
  38. * @state: state containing the property value
  39. * @property: property to look up
  40. * @val: pointer to write property value into
  41. *
  42. * The DRM core does not store shadow copies of properties for
  43. * atomic-capable drivers. This entrypoint is used to fetch
  44. * the current value of a driver-specific connector property.
  45. */
  46. int
  47. intel_connector_atomic_get_property(struct drm_connector *connector,
  48. const struct drm_connector_state *state,
  49. struct drm_property *property,
  50. uint64_t *val)
  51. {
  52. int i;
  53. /*
  54. * TODO: We only have atomic modeset for planes at the moment, so the
  55. * crtc/connector code isn't quite ready yet. Until it's ready,
  56. * continue to look up all property values in the DRM's shadow copy
  57. * in obj->properties->values[].
  58. *
  59. * When the crtc/connector state work matures, this function should
  60. * be updated to read the values out of the state structure instead.
  61. */
  62. for (i = 0; i < connector->base.properties->count; i++) {
  63. if (connector->base.properties->properties[i] == property) {
  64. *val = connector->base.properties->values[i];
  65. return 0;
  66. }
  67. }
  68. return -EINVAL;
  69. }
  70. /*
  71. * intel_crtc_duplicate_state - duplicate crtc state
  72. * @crtc: drm crtc
  73. *
  74. * Allocates and returns a copy of the crtc state (both common and
  75. * Intel-specific) for the specified crtc.
  76. *
  77. * Returns: The newly allocated crtc state, or NULL on failure.
  78. */
  79. struct drm_crtc_state *
  80. intel_crtc_duplicate_state(struct drm_crtc *crtc)
  81. {
  82. struct intel_crtc_state *crtc_state;
  83. crtc_state = kmemdup(crtc->state, sizeof(*crtc_state), GFP_KERNEL);
  84. if (!crtc_state)
  85. return NULL;
  86. __drm_atomic_helper_crtc_duplicate_state(crtc, &crtc_state->base);
  87. crtc_state->update_pipe = false;
  88. return &crtc_state->base;
  89. }
  90. /**
  91. * intel_crtc_destroy_state - destroy crtc state
  92. * @crtc: drm crtc
  93. *
  94. * Destroys the crtc state (both common and Intel-specific) for the
  95. * specified crtc.
  96. */
  97. void
  98. intel_crtc_destroy_state(struct drm_crtc *crtc,
  99. struct drm_crtc_state *state)
  100. {
  101. drm_atomic_helper_crtc_destroy_state(crtc, state);
  102. }
  103. /**
  104. * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests
  105. * @dev: DRM device
  106. * @crtc: intel crtc
  107. * @crtc_state: incoming crtc_state to validate and setup scalers
  108. *
  109. * This function sets up scalers based on staged scaling requests for
  110. * a @crtc and its planes. It is called from crtc level check path. If request
  111. * is a supportable request, it attaches scalers to requested planes and crtc.
  112. *
  113. * This function takes into account the current scaler(s) in use by any planes
  114. * not being part of this atomic state
  115. *
  116. * Returns:
  117. * 0 - scalers were setup succesfully
  118. * error code - otherwise
  119. */
  120. int intel_atomic_setup_scalers(struct drm_device *dev,
  121. struct intel_crtc *intel_crtc,
  122. struct intel_crtc_state *crtc_state)
  123. {
  124. struct drm_plane *plane = NULL;
  125. struct intel_plane *intel_plane;
  126. struct intel_plane_state *plane_state = NULL;
  127. struct intel_crtc_scaler_state *scaler_state =
  128. &crtc_state->scaler_state;
  129. struct drm_atomic_state *drm_state = crtc_state->base.state;
  130. int num_scalers_need;
  131. int i, j;
  132. num_scalers_need = hweight32(scaler_state->scaler_users);
  133. /*
  134. * High level flow:
  135. * - staged scaler requests are already in scaler_state->scaler_users
  136. * - check whether staged scaling requests can be supported
  137. * - add planes using scalers that aren't in current transaction
  138. * - assign scalers to requested users
  139. * - as part of plane commit, scalers will be committed
  140. * (i.e., either attached or detached) to respective planes in hw
  141. * - as part of crtc_commit, scaler will be either attached or detached
  142. * to crtc in hw
  143. */
  144. /* fail if required scalers > available scalers */
  145. if (num_scalers_need > intel_crtc->num_scalers){
  146. DRM_DEBUG_KMS("Too many scaling requests %d > %d\n",
  147. num_scalers_need, intel_crtc->num_scalers);
  148. return -EINVAL;
  149. }
  150. /* walkthrough scaler_users bits and start assigning scalers */
  151. for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) {
  152. int *scaler_id;
  153. const char *name;
  154. int idx;
  155. /* skip if scaler not required */
  156. if (!(scaler_state->scaler_users & (1 << i)))
  157. continue;
  158. if (i == SKL_CRTC_INDEX) {
  159. name = "CRTC";
  160. idx = intel_crtc->base.base.id;
  161. /* panel fitter case: assign as a crtc scaler */
  162. scaler_id = &scaler_state->scaler_id;
  163. } else {
  164. name = "PLANE";
  165. /* plane scaler case: assign as a plane scaler */
  166. /* find the plane that set the bit as scaler_user */
  167. plane = drm_state->planes[i];
  168. /*
  169. * to enable/disable hq mode, add planes that are using scaler
  170. * into this transaction
  171. */
  172. if (!plane) {
  173. struct drm_plane_state *state;
  174. plane = drm_plane_from_index(dev, i);
  175. state = drm_atomic_get_plane_state(drm_state, plane);
  176. if (IS_ERR(state)) {
  177. DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n",
  178. plane->base.id);
  179. return PTR_ERR(state);
  180. }
  181. /*
  182. * the plane is added after plane checks are run,
  183. * but since this plane is unchanged just do the
  184. * minimum required validation.
  185. */
  186. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  187. intel_crtc->atomic.wait_for_flips = true;
  188. crtc_state->base.planes_changed = true;
  189. }
  190. intel_plane = to_intel_plane(plane);
  191. idx = plane->base.id;
  192. /* plane on different crtc cannot be a scaler user of this crtc */
  193. if (WARN_ON(intel_plane->pipe != intel_crtc->pipe)) {
  194. continue;
  195. }
  196. plane_state = to_intel_plane_state(drm_state->plane_states[i]);
  197. scaler_id = &plane_state->scaler_id;
  198. }
  199. if (*scaler_id < 0) {
  200. /* find a free scaler */
  201. for (j = 0; j < intel_crtc->num_scalers; j++) {
  202. if (!scaler_state->scalers[j].in_use) {
  203. scaler_state->scalers[j].in_use = 1;
  204. *scaler_id = j;
  205. DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n",
  206. intel_crtc->pipe, *scaler_id, name, idx);
  207. break;
  208. }
  209. }
  210. }
  211. if (WARN_ON(*scaler_id < 0)) {
  212. DRM_DEBUG_KMS("Cannot find scaler for %s:%d\n", name, idx);
  213. continue;
  214. }
  215. /* set scaler mode */
  216. if (num_scalers_need == 1 && intel_crtc->pipe != PIPE_C) {
  217. /*
  218. * when only 1 scaler is in use on either pipe A or B,
  219. * scaler 0 operates in high quality (HQ) mode.
  220. * In this case use scaler 0 to take advantage of HQ mode
  221. */
  222. *scaler_id = 0;
  223. scaler_state->scalers[0].in_use = 1;
  224. scaler_state->scalers[0].mode = PS_SCALER_MODE_HQ;
  225. scaler_state->scalers[1].in_use = 0;
  226. } else {
  227. scaler_state->scalers[*scaler_id].mode = PS_SCALER_MODE_DYN;
  228. }
  229. }
  230. return 0;
  231. }
  232. static void
  233. intel_atomic_duplicate_dpll_state(struct drm_i915_private *dev_priv,
  234. struct intel_shared_dpll_config *shared_dpll)
  235. {
  236. enum intel_dpll_id i;
  237. /* Copy shared dpll state */
  238. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  239. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  240. shared_dpll[i] = pll->config;
  241. }
  242. }
  243. struct intel_shared_dpll_config *
  244. intel_atomic_get_shared_dpll_state(struct drm_atomic_state *s)
  245. {
  246. struct intel_atomic_state *state = to_intel_atomic_state(s);
  247. WARN_ON(!drm_modeset_is_locked(&s->dev->mode_config.connection_mutex));
  248. if (!state->dpll_set) {
  249. state->dpll_set = true;
  250. intel_atomic_duplicate_dpll_state(to_i915(s->dev),
  251. state->shared_dpll);
  252. }
  253. return state->shared_dpll;
  254. }
  255. struct drm_atomic_state *
  256. intel_atomic_state_alloc(struct drm_device *dev)
  257. {
  258. struct intel_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  259. if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
  260. kfree(state);
  261. return NULL;
  262. }
  263. return &state->base;
  264. }
  265. void intel_atomic_state_clear(struct drm_atomic_state *s)
  266. {
  267. struct intel_atomic_state *state = to_intel_atomic_state(s);
  268. drm_atomic_state_default_clear(&state->base);
  269. state->dpll_set = false;
  270. }