xref: /titanic_50/usr/src/uts/intel/io/drm/i915_drv.c (revision ff3124eff995e6cd8ebd8c6543648e0670920034)
1 /* BEGIN CSTYLED */
2 
3 /*
4  * i915_drv.c -- Intel i915 driver -*- linux-c -*-
5  * Created: Wed Feb 14 17:10:04 2001 by gareth@valinux.com
6  */
7 
8 /*
9  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
10  * All Rights Reserved.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the next
20  * paragraph) shall be included in all copies or substantial portions of the
21  * Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  *
31  * Authors:
32  *    Gareth Hughes <gareth@valinux.com>
33  *
34  */
35 
36 /*
37  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
38  * Use is subject to license terms.
39  */
40 
41 /*
42  * I915 DRM Driver for Solaris
43  *
44  * This driver provides the hardware 3D acceleration support for Intel
45  * integrated video devices (e.g. i8xx/i915/i945 series chipsets), under the
46  * DRI (Direct Rendering Infrastructure). DRM (Direct Rendering Manager) here
47  * means the kernel device driver in DRI.
48  *
49  * I915 driver is a device dependent driver only, it depends on a misc module
50  * named drm for generic DRM operations.
51  */
52 #pragma ident	"%Z%%M%	%I%	%E% SMI"
53 
54 #include "drmP.h"
55 #include "i915_drm.h"
56 #include "i915_drv.h"
57 #include "drm_pciids.h"
58 
59 #define	i915_max_ioctl  0x20 /* changed from 15 */
60 
61 /*
62  * copied from vgasubr.h
63  */
64 
65 struct vgaregmap {
66 	uint8_t			*addr;
67 	ddi_acc_handle_t	handle;
68 	boolean_t		mapped;
69 };
70 
71 enum pipe {
72 	PIPE_A = 0,
73 	PIPE_B,
74 };
75 
76 
77 /*
78  * cb_ops entrypoint
79  */
80 extern struct cb_ops drm_cb_ops;
81 
82 /*
83  * module entrypoint
84  */
85 static int i915_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
86 static int i915_attach(dev_info_t *, ddi_attach_cmd_t);
87 static int i915_detach(dev_info_t *, ddi_detach_cmd_t);
88 
89 
90 /* drv_PCI_IDs comes from drm_pciids.h */
91 static drm_pci_id_list_t i915_pciidlist[] = {
92 	i915_PCI_IDS
93 };
94 
95 drm_ioctl_desc_t i915_ioctls[i915_max_ioctl];
96 
97 extern void i915_init_ioctl_arrays(void);
98 
99 /*
100  * Local routines
101  */
102 static void i915_configure(drm_driver_t *);
103 
104 /*
105  * DRM driver
106  */
107 static drm_driver_t	i915_driver = {0};
108 
109 
110 static struct dev_ops i915_dev_ops = {
111 	DEVO_REV,			/* devo_rev */
112 	0,				/* devo_refcnt */
113 	i915_info,			/* devo_getinfo */
114 	nulldev,			/* devo_identify */
115 	nulldev,			/* devo_probe */
116 	i915_attach,			/* devo_attach */
117 	i915_detach,			/* devo_detach */
118 	nodev,				/* devo_reset */
119 	&drm_cb_ops,		/* devo_cb_ops */
120 	NULL,				/* devo_bus_ops */
121 	NULL				/* power */
122 };
123 
124 static struct modldrv modldrv = {
125 	&mod_driverops,			/* drv_modops */
126 	"I915 DRM driver",	/* drv_linkinfo */
127 	&i915_dev_ops,			/* drv_dev_ops */
128 };
129 
130 static struct modlinkage modlinkage = {
131 	MODREV_1, (void *) &modldrv, NULL
132 };
133 
134 static ddi_device_acc_attr_t s3_attr = {
135         DDI_DEVICE_ATTR_V0,
136         DDI_NEVERSWAP_ACC,
137         DDI_STRICTORDER_ACC     /* must be DDI_STRICTORDER_ACC */
138 };
139 
140 /*
141  * softstate head
142  */
143 static void 	*i915_statep;
144 
145 int
146 _init(void)
147 {
148 	int error;
149 
150 	i915_configure(&i915_driver);
151 
152 	if ((error = ddi_soft_state_init(&i915_statep,
153 	    sizeof (drm_device_t), DRM_MAX_INSTANCES)) != 0)
154 		return (error);
155 
156 	if ((error = mod_install(&modlinkage)) != 0) {
157 		ddi_soft_state_fini(&i915_statep);
158 		return (error);
159 	}
160 
161 	return (error);
162 
163 }	/* _init() */
164 
165 int
166 _fini(void)
167 {
168 	int error;
169 
170 	if ((error = mod_remove(&modlinkage)) != 0)
171 		return (error);
172 
173 	(void) ddi_soft_state_fini(&i915_statep);
174 
175 	return (0);
176 
177 }	/* _fini() */
178 
179 int
180 _info(struct modinfo *modinfop)
181 {
182 	return (mod_info(&modlinkage, modinfop));
183 
184 }	/* _info() */
185 
186 /*
187  * off range: 0x3b0 ~ 0x3ff
188  */
189 
190 static void
191 vga_reg_put8(struct vgaregmap *regmap, uint16_t off, uint8_t val)
192 {
193 	ASSERT((off >= 0x3b0) && (off <= 0x3ff));
194 
195 	ddi_put8(regmap->handle, regmap->addr + off, val);
196 }
197 
198 /*
199  * off range: 0x3b0 ~ 0x3ff
200  */
201 static uint8_t
202 vga_reg_get8(struct vgaregmap *regmap, uint16_t off)
203 {
204 
205 	ASSERT((off >= 0x3b0) && (off <= 0x3ff));
206 
207 	return (ddi_get8(regmap->handle, regmap->addr + off));
208 }
209 
210 static void
211 i915_write_indexed(struct vgaregmap *regmap,
212     uint16_t index_port, uint16_t data_port, uint8_t index, uint8_t val)
213 {
214 	vga_reg_put8(regmap, index_port, index);
215 	vga_reg_put8(regmap, data_port, val);
216 }
217 
218 static uint8_t
219 i915_read_indexed(struct vgaregmap *regmap,
220     uint16_t index_port, uint16_t data_port, uint8_t index)
221 {
222 	vga_reg_put8(regmap, index_port, index);
223 	return (vga_reg_get8(regmap, data_port));
224 }
225 
226 static void
227 i915_write_ar(struct vgaregmap *regmap, uint16_t st01,
228     uint8_t reg, uint8_t val, uint8_t palette_enable)
229 {
230 	(void) vga_reg_get8(regmap, st01);
231 	vga_reg_put8(regmap, VGA_AR_INDEX, palette_enable | reg);
232 	vga_reg_put8(regmap, VGA_AR_DATA_WRITE, val);
233 }
234 
235 static uint8_t
236 i915_read_ar(struct vgaregmap *regmap, uint16_t st01,
237     uint8_t index, uint8_t palette_enable)
238 {
239 	(void) vga_reg_get8(regmap, st01);
240 	vga_reg_put8(regmap, VGA_AR_INDEX, index | palette_enable);
241 	return (vga_reg_get8(regmap, VGA_AR_DATA_READ));
242 }
243 
244 static int
245 i915_pipe_enabled(struct drm_device *dev, enum pipe pipe)
246 {
247 	struct s3_i915_private *s3_priv = dev->s3_private;
248 
249 	if (pipe == PIPE_A)
250 		return (S3_READ(DPLL_A) & DPLL_VCO_ENABLE);
251 	else
252 		return (S3_READ(DPLL_B) & DPLL_VCO_ENABLE);
253 }
254 
255 static void
256 i915_save_palette(struct drm_device *dev, enum pipe pipe)
257 {
258 	struct s3_i915_private *s3_priv = dev->s3_private;
259 	unsigned long reg = (pipe == PIPE_A ? PALETTE_A : PALETTE_B);
260 	uint32_t *array;
261 	int i;
262 
263 	if (!i915_pipe_enabled(dev, pipe))
264 		return;
265 
266 	if (pipe == PIPE_A)
267 		array = s3_priv->save_palette_a;
268 	else
269 		array = s3_priv->save_palette_b;
270 
271 	for(i = 0; i < 256; i++)
272 		array[i] = S3_READ(reg + (i << 2));
273 
274 }
275 
276 static void
277 i915_restore_palette(struct drm_device *dev, enum pipe pipe)
278 {
279 	struct s3_i915_private *s3_priv = dev->s3_private;
280 	unsigned long reg = (pipe == PIPE_A ? PALETTE_A : PALETTE_B);
281 	uint32_t *array;
282 	int i;
283 
284 	if (!i915_pipe_enabled(dev, pipe))
285 		return;
286 
287 	if (pipe == PIPE_A)
288 		array = s3_priv->save_palette_a;
289 	else
290 		array = s3_priv->save_palette_b;
291 
292 	for(i = 0; i < 256; i++)
293 		S3_WRITE(reg + (i << 2), array[i]);
294 }
295 
296 static void
297 i915_save_vga(struct drm_device *dev)
298 {
299 	struct s3_i915_private *s3_priv = dev->s3_private;
300 	int i;
301 	uint16_t cr_index, cr_data, st01;
302 	struct vgaregmap regmap;
303 
304 	regmap.addr = (uint8_t *)s3_priv->saveAddr;
305 	regmap.handle = s3_priv->saveHandle;
306 
307 	/* VGA color palette registers */
308         s3_priv->saveDACMASK = vga_reg_get8(&regmap, VGA_DACMASK);
309 	/* DACCRX automatically increments during read */
310 	vga_reg_put8(&regmap, VGA_DACRX, 0);
311 	/* Read 3 bytes of color data from each index */
312 	for (i = 0; i < 256 * 3; i++)
313 		s3_priv->saveDACDATA[i] = vga_reg_get8(&regmap, VGA_DACDATA);
314 
315 	/* MSR bits */
316 	s3_priv->saveMSR = vga_reg_get8(&regmap, VGA_MSR_READ);
317 	if (s3_priv->saveMSR & VGA_MSR_CGA_MODE) {
318 		cr_index = VGA_CR_INDEX_CGA;
319 		cr_data = VGA_CR_DATA_CGA;
320 		st01 = VGA_ST01_CGA;
321 	} else {
322 		cr_index = VGA_CR_INDEX_MDA;
323 		cr_data = VGA_CR_DATA_MDA;
324 		st01 = VGA_ST01_MDA;
325 	}
326 
327 	/* CRT controller regs */
328 	i915_write_indexed(&regmap, cr_index, cr_data, 0x11,
329 	    i915_read_indexed(&regmap, cr_index, cr_data, 0x11) & (~0x80));
330 	for (i = 0; i < 0x24; i++)
331 		s3_priv->saveCR[i] =
332 		    i915_read_indexed(&regmap, cr_index, cr_data, i);
333 	/* Make sure we don't turn off CR group 0 writes */
334 	s3_priv->saveCR[0x11] &= ~0x80;
335 
336 	/* Attribute controller registers */
337 	(void) vga_reg_get8(&regmap, st01);
338 	s3_priv->saveAR_INDEX = vga_reg_get8(&regmap, VGA_AR_INDEX);
339 	for (i = 0; i < 20; i++)
340 		s3_priv->saveAR[i] = i915_read_ar(&regmap, st01, i, 0);
341 	(void) vga_reg_get8(&regmap, st01);
342 	vga_reg_put8(&regmap, VGA_AR_INDEX, s3_priv->saveAR_INDEX);
343 	(void) vga_reg_get8(&regmap, st01);
344 
345 	/* Graphics controller registers */
346 	for (i = 0; i < 9; i++)
347 		s3_priv->saveGR[i] =
348 		    i915_read_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, i);
349 
350 	s3_priv->saveGR[0x10] =
351 		i915_read_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, 0x10);
352 	s3_priv->saveGR[0x11] =
353 		i915_read_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, 0x11);
354 	s3_priv->saveGR[0x18] =
355 		i915_read_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, 0x18);
356 
357 	/* Sequencer registers */
358 	for (i = 0; i < 8; i++)
359 		s3_priv->saveSR[i] =
360 		    i915_read_indexed(&regmap, VGA_SR_INDEX, VGA_SR_DATA, i);
361 }
362 
363 static void
364 i915_restore_vga(struct drm_device *dev)
365 {
366 	struct s3_i915_private *s3_priv = dev->s3_private;
367 	int i;
368 	uint16_t cr_index, cr_data, st01;
369 	struct vgaregmap regmap;
370 
371 	regmap.addr = (uint8_t *)s3_priv->saveAddr;
372 	regmap.handle = s3_priv->saveHandle;
373 
374 	/*
375 	 * I/O Address Select. This bit selects 3Bxh or 3Dxh as the
376 	 * I/O address for the CRT Controller registers,
377 	 * the Feature Control Register (FCR), and Input Status Register
378 	 * 1 (ST01). Presently ignored (whole range is claimed), but
379 	 * will "ignore" 3Bx for color configuration or 3Dx for monochrome.
380 	 * Note that it is typical in AGP chipsets to shadow this bit
381 	 * and properly steer I/O cycles to the proper bus for operation
382 	 * where a MDA exists on another bus such as ISA.
383 	 * 0 = Select 3Bxh I/O address (MDA emulation) (default).
384 	 * 1 = Select 3Dxh I/O address (CGA emulation).
385 	 */
386 	vga_reg_put8(&regmap, VGA_MSR_WRITE, s3_priv->saveMSR);
387 
388 	if (s3_priv->saveMSR & VGA_MSR_CGA_MODE) {
389 		cr_index = VGA_CR_INDEX_CGA;
390 		cr_data = VGA_CR_DATA_CGA;
391 		st01 = VGA_ST01_CGA;
392         } else {
393 		cr_index = VGA_CR_INDEX_MDA;
394 		cr_data = VGA_CR_DATA_MDA;
395 		st01 = VGA_ST01_MDA;
396         }
397 
398 	/* Sequencer registers, don't write SR07 */
399         for (i = 0; i < 7; i++)
400 		i915_write_indexed(&regmap, VGA_SR_INDEX, VGA_SR_DATA, i,
401 		    s3_priv->saveSR[i]);
402 	/* CRT controller regs */
403 	/* Enable CR group 0 writes */
404 	i915_write_indexed(&regmap, cr_index, cr_data,
405 	    0x11, s3_priv->saveCR[0x11]);
406 	for (i = 0; i < 0x24; i++)
407 		i915_write_indexed(&regmap, cr_index,
408 		    cr_data, i, s3_priv->saveCR[i]);
409 
410 	/* Graphics controller regs */
411 	for (i = 0; i < 9; i++)
412 		i915_write_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, i,
413 		    s3_priv->saveGR[i]);
414 
415 	i915_write_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, 0x10,
416 	    s3_priv->saveGR[0x10]);
417 	i915_write_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, 0x11,
418 	    s3_priv->saveGR[0x11]);
419 	i915_write_indexed(&regmap, VGA_GR_INDEX, VGA_GR_DATA, 0x18,
420 	    s3_priv->saveGR[0x18]);
421 
422 	/* Attribute controller registers */
423 	(void) vga_reg_get8(&regmap, st01); /* switch back to index mode */
424 	for (i = 0; i < 20; i++)
425 	    i915_write_ar(&regmap, st01, i, s3_priv->saveAR[i], 0);
426 	(void) vga_reg_get8(&regmap, st01); /* switch back to index mode */
427 	vga_reg_put8(&regmap, VGA_AR_INDEX, s3_priv->saveAR_INDEX | 0x20);
428 	(void) vga_reg_get8(&regmap, st01); /* switch back to index mode */
429 
430 	/* VGA color palette registers */
431 	vga_reg_put8(&regmap, VGA_DACMASK, s3_priv->saveDACMASK);
432 	/* DACCRX automatically increments during read */
433 	vga_reg_put8(&regmap, VGA_DACWX, 0);
434 	/* Read 3 bytes of color data from each index */
435 	for (i = 0; i < 256 * 3; i++)
436 		vga_reg_put8(&regmap, VGA_DACDATA, s3_priv->saveDACDATA[i]);
437 }
438 
439 static int
440 i915_resume(struct drm_device *dev)
441 {
442 	ddi_acc_handle_t conf_hdl;
443 	struct s3_i915_private *s3_priv = dev->s3_private;
444 	int i;
445 
446 	if (pci_config_setup(dev->dip, &conf_hdl) != DDI_SUCCESS) {
447 		DRM_ERROR(("i915_resume: pci_config_setup fail"));
448 		return (DDI_FAILURE);
449 	}
450 	/*
451 	 * Nexus driver will resume pci config space and set the power state
452 	 * for its children. So we needn't resume them explicitly here.
453 	 * see pci_pre_resume for detail.
454 	 */
455 	pci_config_put8(conf_hdl, LBB, s3_priv->saveLBB);
456 	/*
457 	 * Pipe & plane A info
458 	 * Prime the clock
459 	 */
460 	if (s3_priv->saveDPLL_A & DPLL_VCO_ENABLE) {
461 		S3_WRITE(DPLL_A, s3_priv->saveDPLL_A &
462 		    ~DPLL_VCO_ENABLE);
463 		drv_usecwait(150);
464         }
465 	S3_WRITE(FPA0, s3_priv->saveFPA0);
466 	S3_WRITE(FPA1, s3_priv->saveFPA1);
467 	/* Actually enable it */
468 	S3_WRITE(DPLL_A, s3_priv->saveDPLL_A);
469 	drv_usecwait(150);
470 	if (IS_I965G(dev))
471 		S3_WRITE(DPLL_A_MD, s3_priv->saveDPLL_A_MD);
472 	drv_usecwait(150);
473 
474 	/* Restore mode */
475 	S3_WRITE(HTOTAL_A, s3_priv->saveHTOTAL_A);
476 	S3_WRITE(HBLANK_A, s3_priv->saveHBLANK_A);
477 	S3_WRITE(HSYNC_A, s3_priv->saveHSYNC_A);
478 	S3_WRITE(VTOTAL_A, s3_priv->saveVTOTAL_A);
479 	S3_WRITE(VBLANK_A, s3_priv->saveVBLANK_A);
480 	S3_WRITE(VSYNC_A, s3_priv->saveVSYNC_A);
481 	S3_WRITE(BCLRPAT_A, s3_priv->saveBCLRPAT_A);
482 
483 	/* Restore plane info */
484 	S3_WRITE(DSPASIZE, s3_priv->saveDSPASIZE);
485 	S3_WRITE(DSPAPOS, s3_priv->saveDSPAPOS);
486 	S3_WRITE(PIPEASRC, s3_priv->savePIPEASRC);
487 	S3_WRITE(DSPABASE, s3_priv->saveDSPABASE);
488 	S3_WRITE(DSPASTRIDE, s3_priv->saveDSPASTRIDE);
489 	if (IS_I965G(dev)) {
490 		S3_WRITE(DSPASURF, s3_priv->saveDSPASURF);
491 		S3_WRITE(DSPATILEOFF, s3_priv->saveDSPATILEOFF);
492 	}
493 	S3_WRITE(PIPEACONF, s3_priv->savePIPEACONF);
494 	i915_restore_palette(dev, PIPE_A);
495 	/* Enable the plane */
496 	S3_WRITE(DSPACNTR, s3_priv->saveDSPACNTR);
497 	S3_WRITE(DSPABASE, S3_READ(DSPABASE));
498 
499 	/* Pipe & plane B info */
500 	if (s3_priv->saveDPLL_B & DPLL_VCO_ENABLE) {
501 		S3_WRITE(DPLL_B, s3_priv->saveDPLL_B &
502 		    ~DPLL_VCO_ENABLE);
503 		drv_usecwait(150);
504 	}
505 	S3_WRITE(FPB0, s3_priv->saveFPB0);
506 	S3_WRITE(FPB1, s3_priv->saveFPB1);
507 	/* Actually enable it */
508 	S3_WRITE(DPLL_B, s3_priv->saveDPLL_B);
509 	drv_usecwait(150);
510 	if (IS_I965G(dev))
511 		S3_WRITE(DPLL_B_MD, s3_priv->saveDPLL_B_MD);
512 	drv_usecwait(150);
513 
514 	/* Restore mode */
515 	S3_WRITE(HTOTAL_B, s3_priv->saveHTOTAL_B);
516 	S3_WRITE(HBLANK_B, s3_priv->saveHBLANK_B);
517 	S3_WRITE(HSYNC_B, s3_priv->saveHSYNC_B);
518 	S3_WRITE(VTOTAL_B, s3_priv->saveVTOTAL_B);
519 	S3_WRITE(VBLANK_B, s3_priv->saveVBLANK_B);
520 	S3_WRITE(VSYNC_B, s3_priv->saveVSYNC_B);
521 	S3_WRITE(BCLRPAT_B, s3_priv->saveBCLRPAT_B);
522 
523 	/* Restore plane info */
524 	S3_WRITE(DSPBSIZE, s3_priv->saveDSPBSIZE);
525 	S3_WRITE(DSPBPOS, s3_priv->saveDSPBPOS);
526 	S3_WRITE(PIPEBSRC, s3_priv->savePIPEBSRC);
527 	S3_WRITE(DSPBBASE, s3_priv->saveDSPBBASE);
528 	S3_WRITE(DSPBSTRIDE, s3_priv->saveDSPBSTRIDE);
529 	if (IS_I965G(dev)) {
530 		S3_WRITE(DSPBSURF, s3_priv->saveDSPBSURF);
531 		S3_WRITE(DSPBTILEOFF, s3_priv->saveDSPBTILEOFF);
532         }
533 	S3_WRITE(PIPEBCONF, s3_priv->savePIPEBCONF);
534 	i915_restore_palette(dev, PIPE_B);
535 	/* Enable the plane */
536 	S3_WRITE(DSPBCNTR, s3_priv->saveDSPBCNTR);
537         S3_WRITE(DSPBBASE, S3_READ(DSPBBASE));
538 
539 	/* CRT state */
540 	S3_WRITE(ADPA, s3_priv->saveADPA);
541 
542 	/* LVDS state */
543 	if (IS_I965G(dev))
544 		S3_WRITE(BLC_PWM_CTL2, s3_priv->saveBLC_PWM_CTL2);
545 	if (IS_MOBILE(dev) && !IS_I830(dev))
546 		S3_WRITE(LVDS, s3_priv->saveLVDS);
547 	if (!IS_I830(dev) && !IS_845G(dev))
548 		S3_WRITE(PFIT_CONTROL, s3_priv->savePFIT_CONTROL);
549 
550 	S3_WRITE(PFIT_PGM_RATIOS, s3_priv->savePFIT_PGM_RATIOS);
551 	S3_WRITE(BLC_PWM_CTL, s3_priv->saveBLC_PWM_CTL);
552         S3_WRITE(LVDSPP_ON, s3_priv->saveLVDSPP_ON);
553         S3_WRITE(LVDSPP_OFF, s3_priv->saveLVDSPP_OFF);
554         S3_WRITE(PP_CYCLE, s3_priv->savePP_CYCLE);
555         S3_WRITE(PP_CONTROL, s3_priv->savePP_CONTROL);
556 
557 	/* FIXME: restore TV & SDVO state */
558 
559 	/* FBC info */
560 	S3_WRITE(FBC_CFB_BASE, s3_priv->saveFBC_CFB_BASE);
561 	S3_WRITE(FBC_LL_BASE, s3_priv->saveFBC_LL_BASE);
562 	S3_WRITE(FBC_CONTROL2, s3_priv->saveFBC_CONTROL2);
563 	S3_WRITE(FBC_CONTROL, s3_priv->saveFBC_CONTROL);
564 
565 	/* VGA state */
566 	S3_WRITE(VGACNTRL, s3_priv->saveVGACNTRL);
567 	S3_WRITE(VCLK_DIVISOR_VGA0, s3_priv->saveVCLK_DIVISOR_VGA0);
568 	S3_WRITE(VCLK_DIVISOR_VGA1, s3_priv->saveVCLK_DIVISOR_VGA1);
569 	S3_WRITE(VCLK_POST_DIV, s3_priv->saveVCLK_POST_DIV);
570 	drv_usecwait(150);
571 
572 	 /* Clock gating state */
573 	S3_WRITE (DSPCLK_GATE_D, s3_priv->saveDSPCLK_GATE_D);
574 
575 	/* Cache mode state */
576 	S3_WRITE (CACHE_MODE_0, s3_priv->saveCACHE_MODE_0 | 0xffff0000);
577 
578 	/* Memory arbitration state */
579 	S3_WRITE (MI_ARB_STATE, s3_priv->saveMI_ARB_STATE | 0xffff0000);
580 
581 	for (i = 0; i < 16; i++) {
582 		S3_WRITE(SWF0 + (i << 2), s3_priv->saveSWF0[i]);
583 		S3_WRITE(SWF10 + (i << 2), s3_priv->saveSWF1[i+7]);
584         }
585 	for (i = 0; i < 3; i++)
586 		S3_WRITE(SWF30 + (i << 2), s3_priv->saveSWF2[i]);
587 
588 	i915_restore_vga(dev);
589 
590 	S3_WRITE(I915REG_PGTBL_CTRL, s3_priv->pgtbl_ctl);
591 
592 	(void) pci_config_teardown(&conf_hdl);
593 
594 	return (DDI_SUCCESS);
595 }
596 static int
597 i915_suspend(struct drm_device *dev)
598 {
599 	ddi_acc_handle_t conf_hdl;
600 	struct s3_i915_private *s3_priv = dev->s3_private;
601 	int i;
602 
603 
604 	if (pci_config_setup(dev->dip, &conf_hdl) != DDI_SUCCESS) {
605 		DRM_ERROR(("i915_suspend: pci_config_setup fail"));
606 		return (DDI_FAILURE);
607 	}
608 
609 	/*
610 	 * Nexus driver will resume pci config space for its children.
611 	 * So pci config registers are not saved here.
612 	 */
613 	s3_priv->saveLBB = pci_config_get8(conf_hdl, LBB);
614 
615 	/*
616 	 * Pipe & plane A info.
617 	 */
618 	s3_priv->savePIPEACONF = S3_READ(PIPEACONF);
619 	s3_priv->savePIPEASRC = S3_READ(PIPEASRC);
620 	s3_priv->saveFPA0 = S3_READ(FPA0);
621 	s3_priv->saveFPA1 = S3_READ(FPA1);
622 	s3_priv->saveDPLL_A = S3_READ(DPLL_A);
623 	if (IS_I965G(dev))
624 		s3_priv->saveDPLL_A_MD = S3_READ(DPLL_A_MD);
625 	s3_priv->saveHTOTAL_A = S3_READ(HTOTAL_A);
626 	s3_priv->saveHBLANK_A = S3_READ(HBLANK_A);
627 	s3_priv->saveHSYNC_A = S3_READ(HSYNC_A);
628 	s3_priv->saveVTOTAL_A = S3_READ(VTOTAL_A);
629 	s3_priv->saveVBLANK_A = S3_READ(VBLANK_A);
630 	s3_priv->saveVSYNC_A = S3_READ(VSYNC_A);
631 	s3_priv->saveBCLRPAT_A = S3_READ(BCLRPAT_A);
632 
633 	s3_priv->saveDSPACNTR = S3_READ(DSPACNTR);
634 	s3_priv->saveDSPASTRIDE = S3_READ(DSPASTRIDE);
635 	s3_priv->saveDSPASIZE = S3_READ(DSPASIZE);
636 	s3_priv->saveDSPAPOS = S3_READ(DSPAPOS);
637 	s3_priv->saveDSPABASE = S3_READ(DSPABASE);
638 	if (IS_I965G(dev)) {
639 		s3_priv->saveDSPASURF = S3_READ(DSPASURF);
640 		s3_priv->saveDSPATILEOFF = S3_READ(DSPATILEOFF);
641 	}
642 	i915_save_palette(dev, PIPE_A);
643 	s3_priv->savePIPEASTAT = S3_READ(I915REG_PIPEASTAT);
644 
645 	/*
646 	 * Pipe & plane B info
647 	 */
648 	s3_priv->savePIPEBCONF = S3_READ(PIPEBCONF);
649 	s3_priv->savePIPEBSRC = S3_READ(PIPEBSRC);
650 	s3_priv->saveFPB0 = S3_READ(FPB0);
651 	s3_priv->saveFPB1 = S3_READ(FPB1);
652 	s3_priv->saveDPLL_B = S3_READ(DPLL_B);
653 	if (IS_I965G(dev))
654 		s3_priv->saveDPLL_B_MD = S3_READ(DPLL_B_MD);
655 	s3_priv->saveHTOTAL_B = S3_READ(HTOTAL_B);
656 	s3_priv->saveHBLANK_B = S3_READ(HBLANK_B);
657 	s3_priv->saveHSYNC_B = S3_READ(HSYNC_B);
658 	s3_priv->saveVTOTAL_B = S3_READ(VTOTAL_B);
659 	s3_priv->saveVBLANK_B = S3_READ(VBLANK_B);
660 	s3_priv->saveVSYNC_B = S3_READ(VSYNC_B);
661 	s3_priv->saveBCLRPAT_A = S3_READ(BCLRPAT_A);
662 
663 	s3_priv->saveDSPBCNTR = S3_READ(DSPBCNTR);
664 	s3_priv->saveDSPBSTRIDE = S3_READ(DSPBSTRIDE);
665 	s3_priv->saveDSPBSIZE = S3_READ(DSPBSIZE);
666 	s3_priv->saveDSPBPOS = S3_READ(DSPBPOS);
667 	s3_priv->saveDSPBBASE = S3_READ(DSPBBASE);
668 	if (IS_I965GM(dev) || IS_IGD_GM(dev)) {
669 		s3_priv->saveDSPBSURF = S3_READ(DSPBSURF);
670 		s3_priv->saveDSPBTILEOFF = S3_READ(DSPBTILEOFF);
671 	}
672 	i915_save_palette(dev, PIPE_B);
673 	s3_priv->savePIPEBSTAT = S3_READ(I915REG_PIPEBSTAT);
674 
675 	/*
676 	 * CRT state
677 	 */
678 	s3_priv->saveADPA = S3_READ(ADPA);
679 
680 	/*
681 	 * LVDS state
682 	 */
683 	s3_priv->savePP_CONTROL = S3_READ(PP_CONTROL);
684 	s3_priv->savePFIT_PGM_RATIOS = S3_READ(PFIT_PGM_RATIOS);
685 	s3_priv->saveBLC_PWM_CTL = S3_READ(BLC_PWM_CTL);
686 	if (IS_I965G(dev))
687 		s3_priv->saveBLC_PWM_CTL2 = S3_READ(BLC_PWM_CTL2);
688 	if (IS_MOBILE(dev) && !IS_I830(dev))
689 		s3_priv->saveLVDS = S3_READ(LVDS);
690 	if (!IS_I830(dev) && !IS_845G(dev))
691 		s3_priv->savePFIT_CONTROL = S3_READ(PFIT_CONTROL);
692 	s3_priv->saveLVDSPP_ON = S3_READ(LVDSPP_ON);
693 	s3_priv->saveLVDSPP_OFF = S3_READ(LVDSPP_OFF);
694 	s3_priv->savePP_CYCLE = S3_READ(PP_CYCLE);
695 
696 	/* FIXME: save TV & SDVO state */
697 
698 	/* FBC state */
699 	s3_priv->saveFBC_CFB_BASE = S3_READ(FBC_CFB_BASE);
700 	s3_priv->saveFBC_LL_BASE = S3_READ(FBC_LL_BASE);
701 	s3_priv->saveFBC_CONTROL2 = S3_READ(FBC_CONTROL2);
702 	s3_priv->saveFBC_CONTROL = S3_READ(FBC_CONTROL);
703 
704 	/* Interrupt state */
705 	s3_priv->saveIIR = S3_READ(I915REG_INT_IDENTITY_R);
706 	s3_priv->saveIER = S3_READ(I915REG_INT_ENABLE_R);
707 	s3_priv->saveIMR = S3_READ(I915REG_INT_MASK_R);
708 
709 	/* VGA state */
710 	s3_priv->saveVCLK_DIVISOR_VGA0 = S3_READ(VCLK_DIVISOR_VGA0);
711 	s3_priv->saveVCLK_DIVISOR_VGA1 = S3_READ(VCLK_DIVISOR_VGA1);
712 	s3_priv->saveVCLK_POST_DIV = S3_READ(VCLK_POST_DIV);
713 	s3_priv->saveVGACNTRL = S3_READ(VGACNTRL);
714 
715 	/* Clock gating state */
716 	s3_priv->saveDSPCLK_GATE_D = S3_READ(DSPCLK_GATE_D);
717 
718 	/* Cache mode state */
719 	s3_priv->saveCACHE_MODE_0 = S3_READ(CACHE_MODE_0);
720 
721 	/* Memory Arbitration state */
722 	s3_priv->saveMI_ARB_STATE = S3_READ(MI_ARB_STATE);
723 
724 	/* Scratch space */
725 	for (i = 0; i < 16; i++) {
726 		s3_priv->saveSWF0[i] = S3_READ(SWF0 + (i << 2));
727 		s3_priv->saveSWF1[i] = S3_READ(SWF10 + (i << 2));
728 	}
729 	for (i = 0; i < 3; i++)
730 		s3_priv->saveSWF2[i] = S3_READ(SWF30 + (i << 2));
731 
732 
733 	i915_save_vga(dev);
734 	/*
735 	 * Save page table control register
736 	 */
737 	s3_priv->pgtbl_ctl = S3_READ(I915REG_PGTBL_CTRL);
738 
739 	(void) pci_config_teardown(&conf_hdl);
740 
741 	return (DDI_SUCCESS);
742 }
743 
744 /*
745  * This funtion check the length of memory mapped IO space to get the right bar. * And There are two possibilities here.
746  * 1. The MMIO registers is in memory map IO bar with 1M size. The bottom half
747  *    of the 1M space is the MMIO registers.
748  * 2. The MMIO register is in memory map IO with 512K size. The whole 512K
749  *    space is the MMIO registers.
750  */
751 static int
752 i915_map_regs(dev_info_t *dip, caddr_t *save_addr, ddi_acc_handle_t *handlep)
753 {
754 	int	rnumber;
755 	int	nregs;
756 	off_t	size = 0;
757 
758 	if (ddi_dev_nregs(dip, &nregs)) {
759 		cmn_err(CE_WARN, "i915_map_regs: failed to get nregs");
760 		return (DDI_FAILURE);
761 	}
762 
763 	for (rnumber = 1; rnumber < nregs; rnumber++) {
764 		(void) ddi_dev_regsize(dip, rnumber, &size);
765 		if ((size == 0x80000) ||
766 		    (size == 0x100000))
767 			break;
768 	}
769 
770 	if (rnumber >= nregs) {
771 		cmn_err(CE_WARN,
772 		    "i915_map_regs: failed to find MMIO registers");
773 		return (DDI_FAILURE);
774 	}
775 
776 	if (ddi_regs_map_setup(dip, rnumber, save_addr,
777 	    0, 0x80000, &s3_attr, handlep)) {
778 		cmn_err(CE_WARN,
779 		    "i915_map_regs: failed to map bar %d", rnumber);
780 		return (DDI_FAILURE);
781 	}
782 
783 	return (DDI_SUCCESS);
784 }
785 static void
786 i915_unmap_regs(ddi_acc_handle_t *handlep)
787 {
788 	ddi_regs_map_free(handlep);
789 }
790 static int
791 i915_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
792 {
793 	drm_device_t		*statep;
794 	s3_i915_private_t	*s3_private;
795 	void		*handle;
796 	int			unit;
797 
798 	unit =  ddi_get_instance(dip);
799 	switch (cmd) {
800 	case DDI_ATTACH:
801 		break;
802 	case DDI_RESUME:
803 		statep = ddi_get_soft_state(i915_statep, unit);
804 		return (i915_resume(statep));
805 	default:
806 		DRM_ERROR("i915_attach: attach and resume ops are supported");
807 		return (DDI_FAILURE);
808 
809 	}
810 
811 	if (ddi_soft_state_zalloc(i915_statep, unit) != DDI_SUCCESS) {
812 			cmn_err(CE_WARN,
813 			    "i915_attach: failed to alloc softstate");
814 			return (DDI_FAILURE);
815 	}
816 	statep = ddi_get_soft_state(i915_statep, unit);
817 	statep->dip = dip;
818 	statep->driver = &i915_driver;
819 
820 	statep->s3_private = drm_alloc(sizeof(s3_i915_private_t),
821 	    DRM_MEM_DRIVER);
822 
823 	if (statep->s3_private == NULL) {
824 		cmn_err(CE_WARN, "i915_attach: failed to allocate s3 priv");
825 		goto err_exit1;
826 	}
827 
828 	/*
829 	 * Map in the mmio register space for s3.
830 	 */
831 	s3_private = (s3_i915_private_t *)statep->s3_private;
832 
833 	if (i915_map_regs(dip, &s3_private->saveAddr,
834 	    &s3_private->saveHandle)) {
835 		cmn_err(CE_WARN, "i915_attach: failed to map MMIO");
836 		goto err_exit2;
837 	}
838 
839 	/*
840 	 * Call drm_supp_register to create minor nodes for us
841 	 */
842 	handle = drm_supp_register(dip, statep);
843 	if ( handle == NULL) {
844 		DRM_ERROR("i915_attach: drm_supp_register failed");
845 		goto err_exit3;
846 	}
847 	statep->drm_handle = handle;
848 
849 	/*
850 	 * After drm_supp_register, we can call drm_xxx routine
851 	 */
852 	statep->drm_supported = DRM_UNSUPPORT;
853 	if (
854 		    drm_probe(statep, i915_pciidlist) != DDI_SUCCESS) {
855 		DRM_ERROR("i915_open: "
856 		    "DRM current don't support this graphics card");
857 		goto err_exit4;
858 	}
859 	statep->drm_supported = DRM_SUPPORT;
860 
861 	/* call common attach code */
862 	if (drm_attach(statep) != DDI_SUCCESS) {
863 		DRM_ERROR("i915_attach: drm_attach failed");
864 		goto err_exit4;
865 	}
866 	return (DDI_SUCCESS);
867 err_exit4:
868 	(void) drm_supp_unregister(handle);
869 err_exit3:
870 	i915_unmap_regs(&s3_private->saveHandle);
871 err_exit2:
872 	drm_free(statep->s3_private, sizeof(s3_i915_private_t),
873 	    DRM_MEM_DRIVER);
874 err_exit1:
875 	(void) ddi_soft_state_free(i915_statep, unit);
876 
877 	return (DDI_FAILURE);
878 
879 }	/* i915_attach() */
880 
881 static int
882 i915_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
883 {
884 	drm_device_t		*statep;
885 	int		unit;
886 	s3_i915_private_t	*s3_private;
887 
888 	if ((cmd != DDI_SUSPEND) && (cmd != DDI_DETACH)) {
889 			DRM_ERROR("i915_detach: "
890 			    "only detach and resume ops are supported");
891 			return (DDI_FAILURE);
892 	}
893 
894 	unit =  ddi_get_instance(dip);
895 	statep = ddi_get_soft_state(i915_statep, unit);
896 	if (statep == NULL) {
897 		DRM_ERROR("i915_detach: can not get soft state");
898 		return (DDI_FAILURE);
899 	}
900 
901 	if (cmd == DDI_SUSPEND)
902 			return (i915_suspend(statep));
903 
904 	s3_private = (s3_i915_private_t *)statep->s3_private;
905 	ddi_regs_map_free(&s3_private->saveHandle);
906 
907 	/*
908 	 * Free the struct for context saving in S3
909 	 */
910 	drm_free(statep->s3_private, sizeof(s3_i915_private_t),
911 	    DRM_MEM_DRIVER);
912 
913 	(void) drm_detach(statep);
914 	(void) drm_supp_unregister(statep->drm_handle);
915 	(void) ddi_soft_state_free(i915_statep, unit);
916 
917 	return (DDI_SUCCESS);
918 
919 }	/* i915_detach() */
920 
921 
922 /*ARGSUSED*/
923 static int
924 i915_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
925 {
926 	drm_device_t		*statep;
927 	int 	error = DDI_SUCCESS;
928 	int 	unit;
929 
930 	unit = drm_dev_to_instance((dev_t)arg);
931 	switch (infocmd) {
932 	case DDI_INFO_DEVT2DEVINFO:
933 		statep = ddi_get_soft_state(i915_statep, unit);
934 		if (statep == NULL || statep->dip == NULL) {
935 			error = DDI_FAILURE;
936 		} else {
937 			*result = (void *) statep->dip;
938 			error = DDI_SUCCESS;
939 		}
940 		break;
941 	case DDI_INFO_DEVT2INSTANCE:
942 		*result = (void *)(uintptr_t)unit;
943 		error = DDI_SUCCESS;
944 		break;
945 	default:
946 		error = DDI_FAILURE;
947 		break;
948 	}
949 	return (error);
950 
951 }	/* i915_info() */
952 
953 
954 static void i915_configure(drm_driver_t *driver)
955 {
956 	i915_init_ioctl_arrays();
957 
958 	driver->buf_priv_size	=	1;	/* No dev_priv */
959 	driver->load	=	i915_driver_load;
960 	driver->unload	=	i915_driver_unload;
961 	driver->preclose	=	i915_driver_preclose;
962 	driver->lastclose	=	i915_driver_lastclose;
963 	driver->device_is_agp	=	i915_driver_device_is_agp;
964 	driver->vblank_wait		=	i915_driver_vblank_wait;
965 	driver->vblank_wait2		=	i915_driver_vblank_wait2;
966 	driver->irq_preinstall	=	i915_driver_irq_preinstall;
967 	driver->irq_postinstall	=	i915_driver_irq_postinstall;
968 	driver->irq_uninstall	=	i915_driver_irq_uninstall;
969 	driver->irq_handler 		=	i915_driver_irq_handler;
970 
971 	driver->driver_ioctls	=	i915_ioctls;
972 	driver->max_driver_ioctl	=	i915_max_ioctl;
973 
974 	driver->driver_name	=	DRIVER_NAME;
975 	driver->driver_desc	=	DRIVER_DESC;
976 	driver->driver_date	=	DRIVER_DATE;
977 	driver->driver_major	=	DRIVER_MAJOR;
978 	driver->driver_minor	=	DRIVER_MINOR;
979 	driver->driver_patchlevel	=	DRIVER_PATCHLEVEL;
980 
981 	driver->use_agp	=	1;
982 	driver->require_agp	=	1;
983 	driver->use_irq	=	1;
984 	driver->use_vbl_irq	=	1;
985 	driver->use_vbl_irq2	=	1;
986 }
987