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/**
25 * DOC: atomic modeset support
26 *
27 * The functions here implement the state management and hardware programming
28 * dispatch required by the atomic modeset infrastructure.
29 * See intel_atomic_plane.c for the plane-specific atomic functionality.
30 */
31
32#include <drm/drmP.h>
33#include <drm/drm_atomic.h>
34#include <drm/drm_atomic_helper.h>
35#include <drm/drm_plane_helper.h>
36#include "intel_drv.h"
37
38
39/**
40 * intel_atomic_check - validate state object
41 * @dev: drm device
42 * @state: state to validate
43 */
44int intel_atomic_check(struct drm_device *dev,
45		       struct drm_atomic_state *state)
46{
47	int nplanes = dev->mode_config.num_total_plane;
48	int ncrtcs = dev->mode_config.num_crtc;
49	int nconnectors = dev->mode_config.num_connector;
50	enum pipe nuclear_pipe = INVALID_PIPE;
51	int ret;
52	int i;
53	bool not_nuclear = false;
54
55	/*
56	 * FIXME:  At the moment, we only support "nuclear pageflip" on a
57	 * single CRTC.  Cross-crtc updates will be added later.
58	 */
59	for (i = 0; i < nplanes; i++) {
60		struct intel_plane *plane = to_intel_plane(state->planes[i]);
61		if (!plane)
62			continue;
63
64		if (nuclear_pipe == INVALID_PIPE) {
65			nuclear_pipe = plane->pipe;
66		} else if (nuclear_pipe != plane->pipe) {
67			DRM_DEBUG_KMS("i915 only support atomic plane operations on a single CRTC at the moment\n");
68			return -EINVAL;
69		}
70	}
71
72	/*
73	 * FIXME:  We only handle planes for now; make sure there are no CRTC's
74	 * or connectors involved.
75	 */
76	state->allow_modeset = false;
77	for (i = 0; i < ncrtcs; i++) {
78		struct intel_crtc *crtc = to_intel_crtc(state->crtcs[i]);
79		if (crtc && crtc->pipe != nuclear_pipe)
80			not_nuclear = true;
81	}
82	for (i = 0; i < nconnectors; i++)
83		if (state->connectors[i] != NULL)
84			not_nuclear = true;
85
86	if (not_nuclear) {
87		DRM_DEBUG_KMS("i915 only supports atomic plane operations at the moment\n");
88		return -EINVAL;
89	}
90
91	ret = drm_atomic_helper_check_planes(dev, state);
92	if (ret)
93		return ret;
94
95	return ret;
96}
97
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 */
115int intel_atomic_commit(struct drm_device *dev,
116			struct drm_atomic_state *state,
117			bool async)
118{
119	int ret;
120	int i;
121
122	if (async) {
123		DRM_DEBUG_KMS("i915 does not yet support async commit\n");
124		return -EINVAL;
125	}
126
127	ret = drm_atomic_helper_prepare_planes(dev, state);
128	if (ret)
129		return ret;
130
131	/* Point of no return */
132
133	/*
134	 * FIXME:  The proper sequence here will eventually be:
135	 *
136	 * drm_atomic_helper_swap_state(dev, state)
137	 * drm_atomic_helper_commit_modeset_disables(dev, state);
138	 * drm_atomic_helper_commit_planes(dev, state);
139	 * drm_atomic_helper_commit_modeset_enables(dev, state);
140	 * drm_atomic_helper_wait_for_vblanks(dev, state);
141	 * drm_atomic_helper_cleanup_planes(dev, state);
142	 * drm_atomic_state_free(state);
143	 *
144	 * once we have full atomic modeset.  For now, just manually update
145	 * plane states to avoid clobbering good states with dummy states
146	 * while nuclear pageflipping.
147	 */
148	for (i = 0; i < dev->mode_config.num_total_plane; i++) {
149		struct drm_plane *plane = state->planes[i];
150
151		if (!plane)
152			continue;
153
154		plane->state->state = state;
155		swap(state->plane_states[i], plane->state);
156		plane->state->state = NULL;
157	}
158	drm_atomic_helper_commit_planes(dev, state);
159	drm_atomic_helper_wait_for_vblanks(dev, state);
160	drm_atomic_helper_cleanup_planes(dev, state);
161	drm_atomic_state_free(state);
162
163	return 0;
164}
165
166/**
167 * intel_connector_atomic_get_property - fetch connector property value
168 * @connector: connector to fetch property for
169 * @state: state containing the property value
170 * @property: property to look up
171 * @val: pointer to write property value into
172 *
173 * The DRM core does not store shadow copies of properties for
174 * atomic-capable drivers.  This entrypoint is used to fetch
175 * the current value of a driver-specific connector property.
176 */
177int
178intel_connector_atomic_get_property(struct drm_connector *connector,
179				    const struct drm_connector_state *state,
180				    struct drm_property *property,
181				    uint64_t *val)
182{
183	int i;
184
185	/*
186	 * TODO: We only have atomic modeset for planes at the moment, so the
187	 * crtc/connector code isn't quite ready yet.  Until it's ready,
188	 * continue to look up all property values in the DRM's shadow copy
189	 * in obj->properties->values[].
190	 *
191	 * When the crtc/connector state work matures, this function should
192	 * be updated to read the values out of the state structure instead.
193	 */
194	for (i = 0; i < connector->base.properties->count; i++) {
195		if (connector->base.properties->properties[i] == property) {
196			*val = connector->base.properties->values[i];
197			return 0;
198		}
199	}
200
201	return -EINVAL;
202}
203
204/*
205 * intel_crtc_duplicate_state - duplicate crtc state
206 * @crtc: drm crtc
207 *
208 * Allocates and returns a copy of the crtc state (both common and
209 * Intel-specific) for the specified crtc.
210 *
211 * Returns: The newly allocated crtc state, or NULL on failure.
212 */
213struct drm_crtc_state *
214intel_crtc_duplicate_state(struct drm_crtc *crtc)
215{
216	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
217	struct intel_crtc_state *crtc_state;
218
219	if (WARN_ON(!intel_crtc->config))
220		crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
221	else
222		crtc_state = kmemdup(intel_crtc->config,
223				     sizeof(*intel_crtc->config), GFP_KERNEL);
224
225	if (crtc_state)
226		crtc_state->base.crtc = crtc;
227
228	return &crtc_state->base;
229}
230
231/**
232 * intel_crtc_destroy_state - destroy crtc state
233 * @crtc: drm crtc
234 *
235 * Destroys the crtc state (both common and Intel-specific) for the
236 * specified crtc.
237 */
238void
239intel_crtc_destroy_state(struct drm_crtc *crtc,
240			  struct drm_crtc_state *state)
241{
242	drm_atomic_helper_crtc_destroy_state(crtc, state);
243}
244