intel_atomic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_atomic_check - validate state object
  37. * @dev: drm device
  38. * @state: state to validate
  39. */
  40. int intel_atomic_check(struct drm_device *dev,
  41. struct drm_atomic_state *state)
  42. {
  43. int nplanes = dev->mode_config.num_total_plane;
  44. int ncrtcs = dev->mode_config.num_crtc;
  45. int nconnectors = dev->mode_config.num_connector;
  46. enum pipe nuclear_pipe = INVALID_PIPE;
  47. struct intel_crtc *nuclear_crtc = NULL;
  48. struct intel_crtc_state *crtc_state = NULL;
  49. int ret;
  50. int i;
  51. bool not_nuclear = false;
  52. /*
  53. * FIXME: At the moment, we only support "nuclear pageflip" on a
  54. * single CRTC. Cross-crtc updates will be added later.
  55. */
  56. for (i = 0; i < nplanes; i++) {
  57. struct intel_plane *plane = to_intel_plane(state->planes[i]);
  58. if (!plane)
  59. continue;
  60. if (nuclear_pipe == INVALID_PIPE) {
  61. nuclear_pipe = plane->pipe;
  62. } else if (nuclear_pipe != plane->pipe) {
  63. DRM_DEBUG_KMS("i915 only support atomic plane operations on a single CRTC at the moment\n");
  64. return -EINVAL;
  65. }
  66. }
  67. /*
  68. * FIXME: We only handle planes for now; make sure there are no CRTC's
  69. * or connectors involved.
  70. */
  71. state->allow_modeset = false;
  72. for (i = 0; i < ncrtcs; i++) {
  73. struct intel_crtc *crtc = to_intel_crtc(state->crtcs[i]);
  74. if (crtc)
  75. memset(&crtc->atomic, 0, sizeof(crtc->atomic));
  76. if (crtc && crtc->pipe != nuclear_pipe)
  77. not_nuclear = true;
  78. if (crtc && crtc->pipe == nuclear_pipe) {
  79. nuclear_crtc = crtc;
  80. crtc_state = to_intel_crtc_state(state->crtc_states[i]);
  81. }
  82. }
  83. for (i = 0; i < nconnectors; i++)
  84. if (state->connectors[i] != NULL)
  85. not_nuclear = true;
  86. if (not_nuclear) {
  87. DRM_DEBUG_KMS("i915 only supports atomic plane operations at the moment\n");
  88. return -EINVAL;
  89. }
  90. ret = drm_atomic_helper_check_planes(dev, state);
  91. if (ret)
  92. return ret;
  93. /* FIXME: move to crtc atomic check function once it is ready */
  94. ret = intel_atomic_setup_scalers(dev, nuclear_crtc, crtc_state);
  95. if (ret)
  96. return ret;
  97. return ret;
  98. }
  99. /**
  100. * intel_atomic_commit - commit validated state object
  101. * @dev: DRM device
  102. * @state: the top-level driver state object
  103. * @async: asynchronous commit
  104. *
  105. * This function commits a top-level state object that has been validated
  106. * with drm_atomic_helper_check().
  107. *
  108. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  109. * we can only handle plane-related operations and do not yet support
  110. * asynchronous commit.
  111. *
  112. * RETURNS
  113. * Zero for success or -errno.
  114. */
  115. int intel_atomic_commit(struct drm_device *dev,
  116. struct drm_atomic_state *state,
  117. bool async)
  118. {
  119. struct drm_crtc_state *crtc_state;
  120. struct drm_crtc *crtc;
  121. int ret, i;
  122. if (async) {
  123. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  124. return -EINVAL;
  125. }
  126. ret = drm_atomic_helper_prepare_planes(dev, state);
  127. if (ret)
  128. return ret;
  129. /* Point of no return */
  130. drm_atomic_helper_swap_state(dev, state);
  131. /* swap crtc_scaler_state */
  132. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  133. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  134. if (INTEL_INFO(dev)->gen >= 9)
  135. skl_detach_scalers(to_intel_crtc(crtc));
  136. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  137. }
  138. drm_atomic_helper_wait_for_vblanks(dev, state);
  139. drm_atomic_helper_cleanup_planes(dev, state);
  140. drm_atomic_state_free(state);
  141. return 0;
  142. }
  143. /**
  144. * intel_connector_atomic_get_property - fetch connector property value
  145. * @connector: connector to fetch property for
  146. * @state: state containing the property value
  147. * @property: property to look up
  148. * @val: pointer to write property value into
  149. *
  150. * The DRM core does not store shadow copies of properties for
  151. * atomic-capable drivers. This entrypoint is used to fetch
  152. * the current value of a driver-specific connector property.
  153. */
  154. int
  155. intel_connector_atomic_get_property(struct drm_connector *connector,
  156. const struct drm_connector_state *state,
  157. struct drm_property *property,
  158. uint64_t *val)
  159. {
  160. int i;
  161. /*
  162. * TODO: We only have atomic modeset for planes at the moment, so the
  163. * crtc/connector code isn't quite ready yet. Until it's ready,
  164. * continue to look up all property values in the DRM's shadow copy
  165. * in obj->properties->values[].
  166. *
  167. * When the crtc/connector state work matures, this function should
  168. * be updated to read the values out of the state structure instead.
  169. */
  170. for (i = 0; i < connector->base.properties->count; i++) {
  171. if (connector->base.properties->properties[i] == property) {
  172. *val = connector->base.properties->values[i];
  173. return 0;
  174. }
  175. }
  176. return -EINVAL;
  177. }
  178. /*
  179. * intel_crtc_duplicate_state - duplicate crtc state
  180. * @crtc: drm crtc
  181. *
  182. * Allocates and returns a copy of the crtc state (both common and
  183. * Intel-specific) for the specified crtc.
  184. *
  185. * Returns: The newly allocated crtc state, or NULL on failure.
  186. */
  187. struct drm_crtc_state *
  188. intel_crtc_duplicate_state(struct drm_crtc *crtc)
  189. {
  190. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  191. struct intel_crtc_state *crtc_state;
  192. if (WARN_ON(!intel_crtc->config))
  193. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  194. else
  195. crtc_state = kmemdup(intel_crtc->config,
  196. sizeof(*intel_crtc->config), GFP_KERNEL);
  197. if (!crtc_state)
  198. return NULL;
  199. __drm_atomic_helper_crtc_duplicate_state(crtc, &crtc_state->base);
  200. crtc_state->base.crtc = crtc;
  201. return &crtc_state->base;
  202. }
  203. /**
  204. * intel_crtc_destroy_state - destroy crtc state
  205. * @crtc: drm crtc
  206. *
  207. * Destroys the crtc state (both common and Intel-specific) for the
  208. * specified crtc.
  209. */
  210. void
  211. intel_crtc_destroy_state(struct drm_crtc *crtc,
  212. struct drm_crtc_state *state)
  213. {
  214. drm_atomic_helper_crtc_destroy_state(crtc, state);
  215. }
  216. /**
  217. * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests
  218. * @dev: DRM device
  219. * @crtc: intel crtc
  220. * @crtc_state: incoming crtc_state to validate and setup scalers
  221. *
  222. * This function sets up scalers based on staged scaling requests for
  223. * a @crtc and its planes. It is called from crtc level check path. If request
  224. * is a supportable request, it attaches scalers to requested planes and crtc.
  225. *
  226. * This function takes into account the current scaler(s) in use by any planes
  227. * not being part of this atomic state
  228. *
  229. * Returns:
  230. * 0 - scalers were setup succesfully
  231. * error code - otherwise
  232. */
  233. int intel_atomic_setup_scalers(struct drm_device *dev,
  234. struct intel_crtc *intel_crtc,
  235. struct intel_crtc_state *crtc_state)
  236. {
  237. struct drm_plane *plane = NULL;
  238. struct intel_plane *intel_plane;
  239. struct intel_plane_state *plane_state = NULL;
  240. struct intel_crtc_scaler_state *scaler_state;
  241. struct drm_atomic_state *drm_state;
  242. int num_scalers_need;
  243. int i, j;
  244. if (INTEL_INFO(dev)->gen < 9 || !intel_crtc || !crtc_state)
  245. return 0;
  246. scaler_state = &crtc_state->scaler_state;
  247. drm_state = crtc_state->base.state;
  248. num_scalers_need = hweight32(scaler_state->scaler_users);
  249. DRM_DEBUG_KMS("crtc_state = %p need = %d avail = %d scaler_users = 0x%x\n",
  250. crtc_state, num_scalers_need, intel_crtc->num_scalers,
  251. scaler_state->scaler_users);
  252. /*
  253. * High level flow:
  254. * - staged scaler requests are already in scaler_state->scaler_users
  255. * - check whether staged scaling requests can be supported
  256. * - add planes using scalers that aren't in current transaction
  257. * - assign scalers to requested users
  258. * - as part of plane commit, scalers will be committed
  259. * (i.e., either attached or detached) to respective planes in hw
  260. * - as part of crtc_commit, scaler will be either attached or detached
  261. * to crtc in hw
  262. */
  263. /* fail if required scalers > available scalers */
  264. if (num_scalers_need > intel_crtc->num_scalers){
  265. DRM_DEBUG_KMS("Too many scaling requests %d > %d\n",
  266. num_scalers_need, intel_crtc->num_scalers);
  267. return -EINVAL;
  268. }
  269. /* walkthrough scaler_users bits and start assigning scalers */
  270. for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) {
  271. int *scaler_id;
  272. /* skip if scaler not required */
  273. if (!(scaler_state->scaler_users & (1 << i)))
  274. continue;
  275. if (i == SKL_CRTC_INDEX) {
  276. /* panel fitter case: assign as a crtc scaler */
  277. scaler_id = &scaler_state->scaler_id;
  278. } else {
  279. if (!drm_state)
  280. continue;
  281. /* plane scaler case: assign as a plane scaler */
  282. /* find the plane that set the bit as scaler_user */
  283. plane = drm_state->planes[i];
  284. /*
  285. * to enable/disable hq mode, add planes that are using scaler
  286. * into this transaction
  287. */
  288. if (!plane) {
  289. struct drm_plane_state *state;
  290. plane = drm_plane_from_index(dev, i);
  291. state = drm_atomic_get_plane_state(drm_state, plane);
  292. if (IS_ERR(state)) {
  293. DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n",
  294. plane->base.id);
  295. return PTR_ERR(state);
  296. }
  297. }
  298. intel_plane = to_intel_plane(plane);
  299. /* plane on different crtc cannot be a scaler user of this crtc */
  300. if (WARN_ON(intel_plane->pipe != intel_crtc->pipe)) {
  301. continue;
  302. }
  303. plane_state = to_intel_plane_state(drm_state->plane_states[i]);
  304. scaler_id = &plane_state->scaler_id;
  305. }
  306. if (*scaler_id < 0) {
  307. /* find a free scaler */
  308. for (j = 0; j < intel_crtc->num_scalers; j++) {
  309. if (!scaler_state->scalers[j].in_use) {
  310. scaler_state->scalers[j].in_use = 1;
  311. *scaler_id = scaler_state->scalers[j].id;
  312. DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n",
  313. intel_crtc->pipe,
  314. i == SKL_CRTC_INDEX ? scaler_state->scaler_id :
  315. plane_state->scaler_id,
  316. i == SKL_CRTC_INDEX ? "CRTC" : "PLANE",
  317. i == SKL_CRTC_INDEX ? intel_crtc->base.base.id :
  318. plane->base.id);
  319. break;
  320. }
  321. }
  322. }
  323. if (WARN_ON(*scaler_id < 0)) {
  324. DRM_DEBUG_KMS("Cannot find scaler for %s:%d\n",
  325. i == SKL_CRTC_INDEX ? "CRTC" : "PLANE",
  326. i == SKL_CRTC_INDEX ? intel_crtc->base.base.id:plane->base.id);
  327. continue;
  328. }
  329. /* set scaler mode */
  330. if (num_scalers_need == 1 && intel_crtc->pipe != PIPE_C) {
  331. /*
  332. * when only 1 scaler is in use on either pipe A or B,
  333. * scaler 0 operates in high quality (HQ) mode.
  334. * In this case use scaler 0 to take advantage of HQ mode
  335. */
  336. *scaler_id = 0;
  337. scaler_state->scalers[0].in_use = 1;
  338. scaler_state->scalers[0].mode = PS_SCALER_MODE_HQ;
  339. scaler_state->scalers[1].in_use = 0;
  340. } else {
  341. scaler_state->scalers[*scaler_id].mode = PS_SCALER_MODE_DYN;
  342. }
  343. }
  344. return 0;
  345. }