intel_atomic.c 9.4 KB

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