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