intel_atomic.c 9.5 KB

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