xref: /linux/drivers/gpu/drm/nouveau/dispnv04/disp.c (revision 94cad89ae4505672ae65457d12f77c44ca87655b)
1 /*
2  * Copyright 2009 Red Hat Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Author: Ben Skeggs
23  */
24 
25 #include <drm/drm_crtc_helper.h>
26 
27 #include "nouveau_drv.h"
28 #include "nouveau_reg.h"
29 #include "hw.h"
30 #include "nouveau_encoder.h"
31 #include "nouveau_connector.h"
32 #include "nouveau_bo.h"
33 #include "nouveau_gem.h"
34 
35 #include <nvif/if0004.h>
36 
37 static void
38 nv04_display_fini(struct drm_device *dev, bool suspend)
39 {
40 	struct nv04_display *disp = nv04_display(dev);
41 	struct drm_crtc *crtc;
42 
43 	/* Disable flip completion events. */
44 	nvif_notify_put(&disp->flip);
45 
46 	/* Disable vblank interrupts. */
47 	NVWriteCRTC(dev, 0, NV_PCRTC_INTR_EN_0, 0);
48 	if (nv_two_heads(dev))
49 		NVWriteCRTC(dev, 1, NV_PCRTC_INTR_EN_0, 0);
50 
51 	if (!suspend)
52 		return;
53 
54 	/* Un-pin FB and cursors so they'll be evicted to system memory. */
55 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
56 		struct drm_framebuffer *fb = crtc->primary->fb;
57 		struct nouveau_bo *nvbo;
58 
59 		if (!fb || !fb->obj[0])
60 			continue;
61 		nvbo = nouveau_gem_object(fb->obj[0]);
62 		nouveau_bo_unpin(nvbo);
63 	}
64 
65 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
66 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
67 		if (nv_crtc->cursor.nvbo) {
68 			if (nv_crtc->cursor.set_offset)
69 				nouveau_bo_unmap(nv_crtc->cursor.nvbo);
70 			nouveau_bo_unpin(nv_crtc->cursor.nvbo);
71 		}
72 	}
73 }
74 
75 static int
76 nv04_display_init(struct drm_device *dev, bool resume, bool runtime)
77 {
78 	struct nv04_display *disp = nv04_display(dev);
79 	struct nouveau_drm *drm = nouveau_drm(dev);
80 	struct nouveau_encoder *encoder;
81 	struct drm_crtc *crtc;
82 	int ret;
83 
84 	/* meh.. modeset apparently doesn't setup all the regs and depends
85 	 * on pre-existing state, for now load the state of the card *before*
86 	 * nouveau was loaded, and then do a modeset.
87 	 *
88 	 * best thing to do probably is to make save/restore routines not
89 	 * save/restore "pre-load" state, but more general so we can save
90 	 * on suspend too.
91 	 */
92 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
93 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
94 		nv_crtc->save(&nv_crtc->base);
95 	}
96 
97 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.base.head)
98 		encoder->enc_save(&encoder->base.base);
99 
100 	/* Enable flip completion events. */
101 	nvif_notify_get(&disp->flip);
102 
103 	if (!resume)
104 		return 0;
105 
106 	/* Re-pin FB/cursors. */
107 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
108 		struct drm_framebuffer *fb = crtc->primary->fb;
109 		struct nouveau_bo *nvbo;
110 
111 		if (!fb || !fb->obj[0])
112 			continue;
113 		nvbo = nouveau_gem_object(fb->obj[0]);
114 		ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, true);
115 		if (ret)
116 			NV_ERROR(drm, "Could not pin framebuffer\n");
117 	}
118 
119 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
120 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
121 		if (!nv_crtc->cursor.nvbo)
122 			continue;
123 
124 		ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
125 		if (!ret && nv_crtc->cursor.set_offset)
126 			ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
127 		if (ret)
128 			NV_ERROR(drm, "Could not pin/map cursor.\n");
129 	}
130 
131 	/* Force CLUT to get re-loaded during modeset. */
132 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
133 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
134 
135 		nv_crtc->lut.depth = 0;
136 	}
137 
138 	/* This should ensure we don't hit a locking problem when someone
139 	 * wakes us up via a connector.  We should never go into suspend
140 	 * while the display is on anyways.
141 	 */
142 	if (runtime)
143 		return 0;
144 
145 	/* Restore mode. */
146 	drm_helper_resume_force_mode(dev);
147 
148 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
149 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
150 
151 		if (!nv_crtc->cursor.nvbo)
152 			continue;
153 
154 		if (nv_crtc->cursor.set_offset)
155 			nv_crtc->cursor.set_offset(nv_crtc,
156 						   nv_crtc->cursor.nvbo->offset);
157 		nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
158 						 nv_crtc->cursor_saved_y);
159 	}
160 
161 	return 0;
162 }
163 
164 static void
165 nv04_display_destroy(struct drm_device *dev)
166 {
167 	struct nv04_display *disp = nv04_display(dev);
168 	struct nouveau_drm *drm = nouveau_drm(dev);
169 	struct nouveau_encoder *encoder;
170 	struct nouveau_crtc *nv_crtc;
171 
172 	/* Restore state */
173 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.base.head)
174 		encoder->enc_restore(&encoder->base.base);
175 
176 	list_for_each_entry(nv_crtc, &dev->mode_config.crtc_list, base.head)
177 		nv_crtc->restore(&nv_crtc->base);
178 
179 	nouveau_hw_save_vga_fonts(dev, 0);
180 
181 	nvif_notify_fini(&disp->flip);
182 
183 	nouveau_display(dev)->priv = NULL;
184 	kfree(disp);
185 
186 	nvif_object_unmap(&drm->client.device.object);
187 }
188 
189 int
190 nv04_display_create(struct drm_device *dev)
191 {
192 	struct nouveau_drm *drm = nouveau_drm(dev);
193 	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
194 	struct dcb_table *dcb = &drm->vbios.dcb;
195 	struct drm_connector *connector, *ct;
196 	struct drm_encoder *encoder;
197 	struct nouveau_encoder *nv_encoder;
198 	struct nouveau_crtc *crtc;
199 	struct nv04_display *disp;
200 	int i, ret;
201 
202 	disp = kzalloc(sizeof(*disp), GFP_KERNEL);
203 	if (!disp)
204 		return -ENOMEM;
205 
206 	nvif_object_map(&drm->client.device.object, NULL, 0);
207 
208 	nouveau_display(dev)->priv = disp;
209 	nouveau_display(dev)->dtor = nv04_display_destroy;
210 	nouveau_display(dev)->init = nv04_display_init;
211 	nouveau_display(dev)->fini = nv04_display_fini;
212 
213 	/* Pre-nv50 doesn't support atomic, so don't expose the ioctls */
214 	dev->driver_features &= ~DRIVER_ATOMIC;
215 
216 	/* Request page flip completion event. */
217 	if (drm->nvsw.client) {
218 		nvif_notify_init(&drm->nvsw, nv04_flip_complete,
219 				 false, NV04_NVSW_NTFY_UEVENT,
220 				 NULL, 0, 0, &disp->flip);
221 	}
222 
223 	nouveau_hw_save_vga_fonts(dev, 1);
224 
225 	nv04_crtc_create(dev, 0);
226 	if (nv_two_heads(dev))
227 		nv04_crtc_create(dev, 1);
228 
229 	for (i = 0; i < dcb->entries; i++) {
230 		struct dcb_output *dcbent = &dcb->entry[i];
231 
232 		connector = nouveau_connector_create(dev, dcbent);
233 		if (IS_ERR(connector))
234 			continue;
235 
236 		switch (dcbent->type) {
237 		case DCB_OUTPUT_ANALOG:
238 			ret = nv04_dac_create(connector, dcbent);
239 			break;
240 		case DCB_OUTPUT_LVDS:
241 		case DCB_OUTPUT_TMDS:
242 			ret = nv04_dfp_create(connector, dcbent);
243 			break;
244 		case DCB_OUTPUT_TV:
245 			if (dcbent->location == DCB_LOC_ON_CHIP)
246 				ret = nv17_tv_create(connector, dcbent);
247 			else
248 				ret = nv04_tv_create(connector, dcbent);
249 			break;
250 		default:
251 			NV_WARN(drm, "DCB type %d not known\n", dcbent->type);
252 			continue;
253 		}
254 
255 		if (ret)
256 			continue;
257 	}
258 
259 	list_for_each_entry_safe(connector, ct,
260 				 &dev->mode_config.connector_list, head) {
261 		if (!connector->possible_encoders) {
262 			NV_WARN(drm, "%s has no encoders, removing\n",
263 				connector->name);
264 			connector->funcs->destroy(connector);
265 		}
266 	}
267 
268 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
269 		struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
270 		struct nvkm_i2c_bus *bus =
271 			nvkm_i2c_bus_find(i2c, nv_encoder->dcb->i2c_index);
272 		nv_encoder->i2c = bus ? &bus->i2c : NULL;
273 	}
274 
275 	/* Save previous state */
276 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
277 		crtc->save(&crtc->base);
278 
279 	list_for_each_entry(nv_encoder, &dev->mode_config.encoder_list, base.base.head)
280 		nv_encoder->enc_save(&nv_encoder->base.base);
281 
282 	nouveau_overlay_init(dev);
283 
284 	return 0;
285 }
286