intel_atomic.c 12 KB

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