intel_atomic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_digital_connector_atomic_get_property - hook for connector->atomic_get_property.
  37. * @connector: Connector to get the property for.
  38. * @state: Connector state to retrieve the property from.
  39. * @property: Property to retrieve.
  40. * @val: Return value for the property.
  41. *
  42. * Returns the atomic property value for a digital connector.
  43. */
  44. int intel_digital_connector_atomic_get_property(struct drm_connector *connector,
  45. const struct drm_connector_state *state,
  46. struct drm_property *property,
  47. uint64_t *val)
  48. {
  49. struct drm_device *dev = connector->dev;
  50. struct drm_i915_private *dev_priv = to_i915(dev);
  51. struct intel_digital_connector_state *intel_conn_state =
  52. to_intel_digital_connector_state(state);
  53. if (property == dev_priv->force_audio_property)
  54. *val = intel_conn_state->force_audio;
  55. else if (property == dev_priv->broadcast_rgb_property)
  56. *val = intel_conn_state->broadcast_rgb;
  57. else {
  58. DRM_DEBUG_ATOMIC("Unknown property %s\n", property->name);
  59. return -EINVAL;
  60. }
  61. return 0;
  62. }
  63. /**
  64. * intel_digital_connector_atomic_set_property - hook for connector->atomic_set_property.
  65. * @connector: Connector to set the property for.
  66. * @state: Connector state to set the property on.
  67. * @property: Property to set.
  68. * @val: New value for the property.
  69. *
  70. * Sets the atomic property value for a digital connector.
  71. */
  72. int intel_digital_connector_atomic_set_property(struct drm_connector *connector,
  73. struct drm_connector_state *state,
  74. struct drm_property *property,
  75. uint64_t val)
  76. {
  77. struct drm_device *dev = connector->dev;
  78. struct drm_i915_private *dev_priv = to_i915(dev);
  79. struct intel_digital_connector_state *intel_conn_state =
  80. to_intel_digital_connector_state(state);
  81. if (property == dev_priv->force_audio_property) {
  82. intel_conn_state->force_audio = val;
  83. return 0;
  84. }
  85. if (property == dev_priv->broadcast_rgb_property) {
  86. intel_conn_state->broadcast_rgb = val;
  87. return 0;
  88. }
  89. DRM_DEBUG_ATOMIC("Unknown property %s\n", property->name);
  90. return -EINVAL;
  91. }
  92. int intel_digital_connector_atomic_check(struct drm_connector *conn,
  93. struct drm_connector_state *new_state)
  94. {
  95. struct intel_digital_connector_state *new_conn_state =
  96. to_intel_digital_connector_state(new_state);
  97. struct drm_connector_state *old_state =
  98. drm_atomic_get_old_connector_state(new_state->state, conn);
  99. struct intel_digital_connector_state *old_conn_state =
  100. to_intel_digital_connector_state(old_state);
  101. struct drm_crtc_state *crtc_state;
  102. if (!new_state->crtc)
  103. return 0;
  104. crtc_state = drm_atomic_get_new_crtc_state(new_state->state, new_state->crtc);
  105. /*
  106. * These properties are handled by fastset, and might not end
  107. * up in a modeset.
  108. */
  109. if (new_conn_state->force_audio != old_conn_state->force_audio ||
  110. new_conn_state->broadcast_rgb != old_conn_state->broadcast_rgb ||
  111. new_conn_state->base.picture_aspect_ratio != old_conn_state->base.picture_aspect_ratio ||
  112. new_conn_state->base.scaling_mode != old_conn_state->base.scaling_mode)
  113. crtc_state->mode_changed = true;
  114. return 0;
  115. }
  116. /**
  117. * intel_digital_connector_duplicate_state - duplicate connector state
  118. * @connector: digital connector
  119. *
  120. * Allocates and returns a copy of the connector state (both common and
  121. * digital connector specific) for the specified connector.
  122. *
  123. * Returns: The newly allocated connector state, or NULL on failure.
  124. */
  125. struct drm_connector_state *
  126. intel_digital_connector_duplicate_state(struct drm_connector *connector)
  127. {
  128. struct intel_digital_connector_state *state;
  129. state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL);
  130. if (!state)
  131. return NULL;
  132. __drm_atomic_helper_connector_duplicate_state(connector, &state->base);
  133. return &state->base;
  134. }
  135. /**
  136. * intel_crtc_duplicate_state - duplicate crtc state
  137. * @crtc: drm crtc
  138. *
  139. * Allocates and returns a copy of the crtc state (both common and
  140. * Intel-specific) for the specified crtc.
  141. *
  142. * Returns: The newly allocated crtc state, or NULL on failure.
  143. */
  144. struct drm_crtc_state *
  145. intel_crtc_duplicate_state(struct drm_crtc *crtc)
  146. {
  147. struct intel_crtc_state *crtc_state;
  148. crtc_state = kmemdup(crtc->state, sizeof(*crtc_state), GFP_KERNEL);
  149. if (!crtc_state)
  150. return NULL;
  151. __drm_atomic_helper_crtc_duplicate_state(crtc, &crtc_state->base);
  152. crtc_state->update_pipe = false;
  153. crtc_state->disable_lp_wm = false;
  154. crtc_state->disable_cxsr = false;
  155. crtc_state->update_wm_pre = false;
  156. crtc_state->update_wm_post = false;
  157. crtc_state->fb_changed = false;
  158. crtc_state->fifo_changed = false;
  159. crtc_state->wm.need_postvbl_update = false;
  160. crtc_state->fb_bits = 0;
  161. return &crtc_state->base;
  162. }
  163. /**
  164. * intel_crtc_destroy_state - destroy crtc state
  165. * @crtc: drm crtc
  166. *
  167. * Destroys the crtc state (both common and Intel-specific) for the
  168. * specified crtc.
  169. */
  170. void
  171. intel_crtc_destroy_state(struct drm_crtc *crtc,
  172. struct drm_crtc_state *state)
  173. {
  174. drm_atomic_helper_crtc_destroy_state(crtc, state);
  175. }
  176. /**
  177. * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests
  178. * @dev_priv: i915 device
  179. * @crtc: intel crtc
  180. * @crtc_state: incoming crtc_state to validate and setup scalers
  181. *
  182. * This function sets up scalers based on staged scaling requests for
  183. * a @crtc and its planes. It is called from crtc level check path. If request
  184. * is a supportable request, it attaches scalers to requested planes and crtc.
  185. *
  186. * This function takes into account the current scaler(s) in use by any planes
  187. * not being part of this atomic state
  188. *
  189. * Returns:
  190. * 0 - scalers were setup succesfully
  191. * error code - otherwise
  192. */
  193. int intel_atomic_setup_scalers(struct drm_i915_private *dev_priv,
  194. struct intel_crtc *intel_crtc,
  195. struct intel_crtc_state *crtc_state)
  196. {
  197. struct drm_plane *plane = NULL;
  198. struct intel_plane *intel_plane;
  199. struct intel_plane_state *plane_state = NULL;
  200. struct intel_crtc_scaler_state *scaler_state =
  201. &crtc_state->scaler_state;
  202. struct drm_atomic_state *drm_state = crtc_state->base.state;
  203. int num_scalers_need;
  204. int i, j;
  205. num_scalers_need = hweight32(scaler_state->scaler_users);
  206. /*
  207. * High level flow:
  208. * - staged scaler requests are already in scaler_state->scaler_users
  209. * - check whether staged scaling requests can be supported
  210. * - add planes using scalers that aren't in current transaction
  211. * - assign scalers to requested users
  212. * - as part of plane commit, scalers will be committed
  213. * (i.e., either attached or detached) to respective planes in hw
  214. * - as part of crtc_commit, scaler will be either attached or detached
  215. * to crtc in hw
  216. */
  217. /* fail if required scalers > available scalers */
  218. if (num_scalers_need > intel_crtc->num_scalers){
  219. DRM_DEBUG_KMS("Too many scaling requests %d > %d\n",
  220. num_scalers_need, intel_crtc->num_scalers);
  221. return -EINVAL;
  222. }
  223. /* walkthrough scaler_users bits and start assigning scalers */
  224. for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) {
  225. int *scaler_id;
  226. const char *name;
  227. int idx;
  228. /* skip if scaler not required */
  229. if (!(scaler_state->scaler_users & (1 << i)))
  230. continue;
  231. if (i == SKL_CRTC_INDEX) {
  232. name = "CRTC";
  233. idx = intel_crtc->base.base.id;
  234. /* panel fitter case: assign as a crtc scaler */
  235. scaler_id = &scaler_state->scaler_id;
  236. } else {
  237. name = "PLANE";
  238. /* plane scaler case: assign as a plane scaler */
  239. /* find the plane that set the bit as scaler_user */
  240. plane = drm_state->planes[i].ptr;
  241. /*
  242. * to enable/disable hq mode, add planes that are using scaler
  243. * into this transaction
  244. */
  245. if (!plane) {
  246. struct drm_plane_state *state;
  247. plane = drm_plane_from_index(&dev_priv->drm, i);
  248. state = drm_atomic_get_plane_state(drm_state, plane);
  249. if (IS_ERR(state)) {
  250. DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n",
  251. plane->base.id);
  252. return PTR_ERR(state);
  253. }
  254. /*
  255. * the plane is added after plane checks are run,
  256. * but since this plane is unchanged just do the
  257. * minimum required validation.
  258. */
  259. crtc_state->base.planes_changed = true;
  260. }
  261. intel_plane = to_intel_plane(plane);
  262. idx = plane->base.id;
  263. /* plane on different crtc cannot be a scaler user of this crtc */
  264. if (WARN_ON(intel_plane->pipe != intel_crtc->pipe)) {
  265. continue;
  266. }
  267. plane_state = intel_atomic_get_existing_plane_state(drm_state,
  268. intel_plane);
  269. scaler_id = &plane_state->scaler_id;
  270. }
  271. if (*scaler_id < 0) {
  272. /* find a free scaler */
  273. for (j = 0; j < intel_crtc->num_scalers; j++) {
  274. if (!scaler_state->scalers[j].in_use) {
  275. scaler_state->scalers[j].in_use = 1;
  276. *scaler_id = j;
  277. DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n",
  278. intel_crtc->pipe, *scaler_id, name, idx);
  279. break;
  280. }
  281. }
  282. }
  283. if (WARN_ON(*scaler_id < 0)) {
  284. DRM_DEBUG_KMS("Cannot find scaler for %s:%d\n", name, idx);
  285. continue;
  286. }
  287. /* set scaler mode */
  288. if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
  289. scaler_state->scalers[*scaler_id].mode = 0;
  290. } else if (num_scalers_need == 1 && intel_crtc->pipe != PIPE_C) {
  291. /*
  292. * when only 1 scaler is in use on either pipe A or B,
  293. * scaler 0 operates in high quality (HQ) mode.
  294. * In this case use scaler 0 to take advantage of HQ mode
  295. */
  296. *scaler_id = 0;
  297. scaler_state->scalers[0].in_use = 1;
  298. scaler_state->scalers[0].mode = PS_SCALER_MODE_HQ;
  299. scaler_state->scalers[1].in_use = 0;
  300. } else {
  301. scaler_state->scalers[*scaler_id].mode = PS_SCALER_MODE_DYN;
  302. }
  303. }
  304. return 0;
  305. }
  306. struct drm_atomic_state *
  307. intel_atomic_state_alloc(struct drm_device *dev)
  308. {
  309. struct intel_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  310. if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
  311. kfree(state);
  312. return NULL;
  313. }
  314. return &state->base;
  315. }
  316. void intel_atomic_state_clear(struct drm_atomic_state *s)
  317. {
  318. struct intel_atomic_state *state = to_intel_atomic_state(s);
  319. drm_atomic_state_default_clear(&state->base);
  320. state->dpll_set = state->modeset = false;
  321. }