intel_atomic.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. crtc_state->disable_lp_wm = false;
  89. crtc_state->disable_cxsr = false;
  90. crtc_state->wm_changed = false;
  91. crtc_state->fb_changed = false;
  92. return &crtc_state->base;
  93. }
  94. /**
  95. * intel_crtc_destroy_state - destroy crtc state
  96. * @crtc: drm crtc
  97. *
  98. * Destroys the crtc state (both common and Intel-specific) for the
  99. * specified crtc.
  100. */
  101. void
  102. intel_crtc_destroy_state(struct drm_crtc *crtc,
  103. struct drm_crtc_state *state)
  104. {
  105. drm_atomic_helper_crtc_destroy_state(crtc, state);
  106. }
  107. /**
  108. * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests
  109. * @dev: DRM device
  110. * @crtc: intel crtc
  111. * @crtc_state: incoming crtc_state to validate and setup scalers
  112. *
  113. * This function sets up scalers based on staged scaling requests for
  114. * a @crtc and its planes. It is called from crtc level check path. If request
  115. * is a supportable request, it attaches scalers to requested planes and crtc.
  116. *
  117. * This function takes into account the current scaler(s) in use by any planes
  118. * not being part of this atomic state
  119. *
  120. * Returns:
  121. * 0 - scalers were setup succesfully
  122. * error code - otherwise
  123. */
  124. int intel_atomic_setup_scalers(struct drm_device *dev,
  125. struct intel_crtc *intel_crtc,
  126. struct intel_crtc_state *crtc_state)
  127. {
  128. struct drm_plane *plane = NULL;
  129. struct intel_plane *intel_plane;
  130. struct intel_plane_state *plane_state = NULL;
  131. struct intel_crtc_scaler_state *scaler_state =
  132. &crtc_state->scaler_state;
  133. struct drm_atomic_state *drm_state = crtc_state->base.state;
  134. int num_scalers_need;
  135. int i, j;
  136. num_scalers_need = hweight32(scaler_state->scaler_users);
  137. /*
  138. * High level flow:
  139. * - staged scaler requests are already in scaler_state->scaler_users
  140. * - check whether staged scaling requests can be supported
  141. * - add planes using scalers that aren't in current transaction
  142. * - assign scalers to requested users
  143. * - as part of plane commit, scalers will be committed
  144. * (i.e., either attached or detached) to respective planes in hw
  145. * - as part of crtc_commit, scaler will be either attached or detached
  146. * to crtc in hw
  147. */
  148. /* fail if required scalers > available scalers */
  149. if (num_scalers_need > intel_crtc->num_scalers){
  150. DRM_DEBUG_KMS("Too many scaling requests %d > %d\n",
  151. num_scalers_need, intel_crtc->num_scalers);
  152. return -EINVAL;
  153. }
  154. /* walkthrough scaler_users bits and start assigning scalers */
  155. for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) {
  156. int *scaler_id;
  157. const char *name;
  158. int idx;
  159. /* skip if scaler not required */
  160. if (!(scaler_state->scaler_users & (1 << i)))
  161. continue;
  162. if (i == SKL_CRTC_INDEX) {
  163. name = "CRTC";
  164. idx = intel_crtc->base.base.id;
  165. /* panel fitter case: assign as a crtc scaler */
  166. scaler_id = &scaler_state->scaler_id;
  167. } else {
  168. name = "PLANE";
  169. /* plane scaler case: assign as a plane scaler */
  170. /* find the plane that set the bit as scaler_user */
  171. plane = drm_state->planes[i];
  172. /*
  173. * to enable/disable hq mode, add planes that are using scaler
  174. * into this transaction
  175. */
  176. if (!plane) {
  177. struct drm_plane_state *state;
  178. plane = drm_plane_from_index(dev, i);
  179. state = drm_atomic_get_plane_state(drm_state, plane);
  180. if (IS_ERR(state)) {
  181. DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n",
  182. plane->base.id);
  183. return PTR_ERR(state);
  184. }
  185. /*
  186. * the plane is added after plane checks are run,
  187. * but since this plane is unchanged just do the
  188. * minimum required validation.
  189. */
  190. crtc_state->base.planes_changed = true;
  191. }
  192. intel_plane = to_intel_plane(plane);
  193. idx = plane->base.id;
  194. /* plane on different crtc cannot be a scaler user of this crtc */
  195. if (WARN_ON(intel_plane->pipe != intel_crtc->pipe)) {
  196. continue;
  197. }
  198. plane_state = to_intel_plane_state(drm_state->plane_states[i]);
  199. scaler_id = &plane_state->scaler_id;
  200. }
  201. if (*scaler_id < 0) {
  202. /* find a free scaler */
  203. for (j = 0; j < intel_crtc->num_scalers; j++) {
  204. if (!scaler_state->scalers[j].in_use) {
  205. scaler_state->scalers[j].in_use = 1;
  206. *scaler_id = j;
  207. DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n",
  208. intel_crtc->pipe, *scaler_id, name, idx);
  209. break;
  210. }
  211. }
  212. }
  213. if (WARN_ON(*scaler_id < 0)) {
  214. DRM_DEBUG_KMS("Cannot find scaler for %s:%d\n", name, idx);
  215. continue;
  216. }
  217. /* set scaler mode */
  218. if (num_scalers_need == 1 && intel_crtc->pipe != PIPE_C) {
  219. /*
  220. * when only 1 scaler is in use on either pipe A or B,
  221. * scaler 0 operates in high quality (HQ) mode.
  222. * In this case use scaler 0 to take advantage of HQ mode
  223. */
  224. *scaler_id = 0;
  225. scaler_state->scalers[0].in_use = 1;
  226. scaler_state->scalers[0].mode = PS_SCALER_MODE_HQ;
  227. scaler_state->scalers[1].in_use = 0;
  228. } else {
  229. scaler_state->scalers[*scaler_id].mode = PS_SCALER_MODE_DYN;
  230. }
  231. }
  232. return 0;
  233. }
  234. static void
  235. intel_atomic_duplicate_dpll_state(struct drm_i915_private *dev_priv,
  236. struct intel_shared_dpll_config *shared_dpll)
  237. {
  238. enum intel_dpll_id i;
  239. /* Copy shared dpll state */
  240. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  241. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  242. shared_dpll[i] = pll->config;
  243. }
  244. }
  245. struct intel_shared_dpll_config *
  246. intel_atomic_get_shared_dpll_state(struct drm_atomic_state *s)
  247. {
  248. struct intel_atomic_state *state = to_intel_atomic_state(s);
  249. WARN_ON(!drm_modeset_is_locked(&s->dev->mode_config.connection_mutex));
  250. if (!state->dpll_set) {
  251. state->dpll_set = true;
  252. intel_atomic_duplicate_dpll_state(to_i915(s->dev),
  253. state->shared_dpll);
  254. }
  255. return state->shared_dpll;
  256. }
  257. struct drm_atomic_state *
  258. intel_atomic_state_alloc(struct drm_device *dev)
  259. {
  260. struct intel_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  261. if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
  262. kfree(state);
  263. return NULL;
  264. }
  265. return &state->base;
  266. }
  267. void intel_atomic_state_clear(struct drm_atomic_state *s)
  268. {
  269. struct intel_atomic_state *state = to_intel_atomic_state(s);
  270. drm_atomic_state_default_clear(&state->base);
  271. state->dpll_set = state->modeset = false;
  272. }