Linux Audio

Check our new training course

Loading...
v4.6
  1
  2/*
  3 * Copyright 2012 Red Hat
  4 *
  5 * This file is subject to the terms and conditions of the GNU General
  6 * Public License version 2. See the file COPYING in the main
  7 * directory of this archive for more details.
  8 *
  9 * Authors: Matthew Garrett
 10 *          Dave Airlie
 11 *
 12 * Portions of this code derived from cirrusfb.c:
 13 * drivers/video/cirrusfb.c - driver for Cirrus Logic chipsets
 14 *
 15 * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
 16 */
 17#include <drm/drmP.h>
 18#include <drm/drm_crtc_helper.h>
 19#include <drm/drm_plane_helper.h>
 20
 21#include <video/cirrus.h>
 22
 23#include "cirrus_drv.h"
 24
 25#define CIRRUS_LUT_SIZE 256
 26
 27#define PALETTE_INDEX 0x8
 28#define PALETTE_DATA 0x9
 29
 30/*
 31 * This file contains setup code for the CRTC.
 32 */
 33
 34static void cirrus_crtc_load_lut(struct drm_crtc *crtc)
 35{
 36	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
 37	struct drm_device *dev = crtc->dev;
 38	struct cirrus_device *cdev = dev->dev_private;
 39	int i;
 40
 41	if (!crtc->enabled)
 42		return;
 43
 44	for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
 45		/* VGA registers */
 46		WREG8(PALETTE_INDEX, i);
 47		WREG8(PALETTE_DATA, cirrus_crtc->lut_r[i]);
 48		WREG8(PALETTE_DATA, cirrus_crtc->lut_g[i]);
 49		WREG8(PALETTE_DATA, cirrus_crtc->lut_b[i]);
 50	}
 51}
 52
 53/*
 54 * The DRM core requires DPMS functions, but they make little sense in our
 55 * case and so are just stubs
 56 */
 57
 58static void cirrus_crtc_dpms(struct drm_crtc *crtc, int mode)
 59{
 60	struct drm_device *dev = crtc->dev;
 61	struct cirrus_device *cdev = dev->dev_private;
 62	u8 sr01, gr0e;
 63
 64	switch (mode) {
 65	case DRM_MODE_DPMS_ON:
 66		sr01 = 0x00;
 67		gr0e = 0x00;
 68		break;
 69	case DRM_MODE_DPMS_STANDBY:
 70		sr01 = 0x20;
 71		gr0e = 0x02;
 72		break;
 73	case DRM_MODE_DPMS_SUSPEND:
 74		sr01 = 0x20;
 75		gr0e = 0x04;
 76		break;
 77	case DRM_MODE_DPMS_OFF:
 78		sr01 = 0x20;
 79		gr0e = 0x06;
 80		break;
 81	default:
 82		return;
 83	}
 84
 85	WREG8(SEQ_INDEX, 0x1);
 86	sr01 |= RREG8(SEQ_DATA) & ~0x20;
 87	WREG_SEQ(0x1, sr01);
 88
 89	WREG8(GFX_INDEX, 0xe);
 90	gr0e |= RREG8(GFX_DATA) & ~0x06;
 91	WREG_GFX(0xe, gr0e);
 92}
 93
 
 
 
 
 
 
 
 
 
 
 
 
 94static void cirrus_set_start_address(struct drm_crtc *crtc, unsigned offset)
 95{
 96	struct cirrus_device *cdev = crtc->dev->dev_private;
 97	u32 addr;
 98	u8 tmp;
 99
100	addr = offset >> 2;
101	WREG_CRT(0x0c, (u8)((addr >> 8) & 0xff));
102	WREG_CRT(0x0d, (u8)(addr & 0xff));
103
104	WREG8(CRT_INDEX, 0x1b);
105	tmp = RREG8(CRT_DATA);
106	tmp &= 0xf2;
107	tmp |= (addr >> 16) & 0x01;
108	tmp |= (addr >> 15) & 0x0c;
109	WREG_CRT(0x1b, tmp);
110	WREG8(CRT_INDEX, 0x1d);
111	tmp = RREG8(CRT_DATA);
112	tmp &= 0x7f;
113	tmp |= (addr >> 12) & 0x80;
114	WREG_CRT(0x1d, tmp);
115}
116
117/* cirrus is different - we will force move buffers out of VRAM */
118static int cirrus_crtc_do_set_base(struct drm_crtc *crtc,
119				struct drm_framebuffer *fb,
120				int x, int y, int atomic)
121{
122	struct cirrus_device *cdev = crtc->dev->dev_private;
123	struct drm_gem_object *obj;
124	struct cirrus_framebuffer *cirrus_fb;
125	struct cirrus_bo *bo;
126	int ret;
127	u64 gpu_addr;
128
129	/* push the previous fb to system ram */
130	if (!atomic && fb) {
131		cirrus_fb = to_cirrus_framebuffer(fb);
132		obj = cirrus_fb->obj;
133		bo = gem_to_cirrus_bo(obj);
134		ret = cirrus_bo_reserve(bo, false);
135		if (ret)
136			return ret;
137		cirrus_bo_push_sysram(bo);
138		cirrus_bo_unreserve(bo);
139	}
140
141	cirrus_fb = to_cirrus_framebuffer(crtc->primary->fb);
142	obj = cirrus_fb->obj;
143	bo = gem_to_cirrus_bo(obj);
144
145	ret = cirrus_bo_reserve(bo, false);
146	if (ret)
147		return ret;
148
149	ret = cirrus_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
150	if (ret) {
151		cirrus_bo_unreserve(bo);
152		return ret;
153	}
154
155	if (&cdev->mode_info.gfbdev->gfb == cirrus_fb) {
156		/* if pushing console in kmap it */
157		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
158		if (ret)
159			DRM_ERROR("failed to kmap fbcon\n");
160	}
161	cirrus_bo_unreserve(bo);
162
163	cirrus_set_start_address(crtc, (u32)gpu_addr);
164	return 0;
165}
166
167static int cirrus_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
168			     struct drm_framebuffer *old_fb)
169{
170	return cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
171}
172
173/*
174 * The meat of this driver. The core passes us a mode and we have to program
175 * it. The modesetting here is the bare minimum required to satisfy the qemu
176 * emulation of this hardware, and running this against a real device is
177 * likely to result in an inadequately programmed mode. We've already had
178 * the opportunity to modify the mode, so whatever we receive here should
179 * be something that can be correctly programmed and displayed
180 */
181static int cirrus_crtc_mode_set(struct drm_crtc *crtc,
182				struct drm_display_mode *mode,
183				struct drm_display_mode *adjusted_mode,
184				int x, int y, struct drm_framebuffer *old_fb)
185{
186	struct drm_device *dev = crtc->dev;
187	struct cirrus_device *cdev = dev->dev_private;
188	int hsyncstart, hsyncend, htotal, hdispend;
189	int vtotal, vdispend;
190	int tmp;
191	int sr07 = 0, hdr = 0;
192
193	htotal = mode->htotal / 8;
194	hsyncend = mode->hsync_end / 8;
195	hsyncstart = mode->hsync_start / 8;
196	hdispend = mode->hdisplay / 8;
197
198	vtotal = mode->vtotal;
199	vdispend = mode->vdisplay;
200
201	vdispend -= 1;
202	vtotal -= 2;
203
204	htotal -= 5;
205	hdispend -= 1;
206	hsyncstart += 1;
207	hsyncend += 1;
208
209	WREG_CRT(VGA_CRTC_V_SYNC_END, 0x20);
210	WREG_CRT(VGA_CRTC_H_TOTAL, htotal);
211	WREG_CRT(VGA_CRTC_H_DISP, hdispend);
212	WREG_CRT(VGA_CRTC_H_SYNC_START, hsyncstart);
213	WREG_CRT(VGA_CRTC_H_SYNC_END, hsyncend);
214	WREG_CRT(VGA_CRTC_V_TOTAL, vtotal & 0xff);
215	WREG_CRT(VGA_CRTC_V_DISP_END, vdispend & 0xff);
216
217	tmp = 0x40;
218	if ((vdispend + 1) & 512)
219		tmp |= 0x20;
220	WREG_CRT(VGA_CRTC_MAX_SCAN, tmp);
221
222	/*
223	 * Overflow bits for values that don't fit in the standard registers
224	 */
225	tmp = 16;
226	if (vtotal & 256)
227		tmp |= 1;
228	if (vdispend & 256)
229		tmp |= 2;
230	if ((vdispend + 1) & 256)
231		tmp |= 8;
232	if (vtotal & 512)
233		tmp |= 32;
234	if (vdispend & 512)
235		tmp |= 64;
236	WREG_CRT(VGA_CRTC_OVERFLOW, tmp);
237
238	tmp = 0;
239
240	/* More overflow bits */
241
242	if ((htotal + 5) & 64)
243		tmp |= 16;
244	if ((htotal + 5) & 128)
245		tmp |= 32;
246	if (vtotal & 256)
247		tmp |= 64;
248	if (vtotal & 512)
249		tmp |= 128;
250
251	WREG_CRT(CL_CRT1A, tmp);
252
253	/* Disable Hercules/CGA compatibility */
254	WREG_CRT(VGA_CRTC_MODE, 0x03);
255
256	WREG8(SEQ_INDEX, 0x7);
257	sr07 = RREG8(SEQ_DATA);
258	sr07 &= 0xe0;
259	hdr = 0;
260	switch (crtc->primary->fb->bits_per_pixel) {
261	case 8:
262		sr07 |= 0x11;
263		break;
264	case 16:
265		sr07 |= 0x17;
266		hdr = 0xc1;
267		break;
268	case 24:
269		sr07 |= 0x15;
270		hdr = 0xc5;
271		break;
272	case 32:
273		sr07 |= 0x19;
274		hdr = 0xc5;
275		break;
276	default:
277		return -1;
278	}
279
280	WREG_SEQ(0x7, sr07);
281
282	/* Program the pitch */
283	tmp = crtc->primary->fb->pitches[0] / 8;
284	WREG_CRT(VGA_CRTC_OFFSET, tmp);
285
286	/* Enable extended blanking and pitch bits, and enable full memory */
287	tmp = 0x22;
288	tmp |= (crtc->primary->fb->pitches[0] >> 7) & 0x10;
289	tmp |= (crtc->primary->fb->pitches[0] >> 6) & 0x40;
290	WREG_CRT(0x1b, tmp);
291
292	/* Enable high-colour modes */
293	WREG_GFX(VGA_GFX_MODE, 0x40);
294
295	/* And set graphics mode */
296	WREG_GFX(VGA_GFX_MISC, 0x01);
297
298	WREG_HDR(hdr);
299	cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
300
301	/* Unblank (needed on S3 resume, vgabios doesn't do it then) */
302	outb(0x20, 0x3c0);
303	return 0;
304}
305
306/*
307 * This is called before a mode is programmed. A typical use might be to
308 * enable DPMS during the programming to avoid seeing intermediate stages,
309 * but that's not relevant to us
310 */
311static void cirrus_crtc_prepare(struct drm_crtc *crtc)
312{
313}
314
315/*
316 * This is called after a mode is programmed. It should reverse anything done
317 * by the prepare function
318 */
319static void cirrus_crtc_commit(struct drm_crtc *crtc)
320{
321}
322
323/*
324 * The core can pass us a set of gamma values to program. We actually only
325 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
326 * but it's a requirement that we provide the function
327 */
328static void cirrus_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
329				  u16 *blue, uint32_t start, uint32_t size)
330{
331	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
332	int i;
333
334	if (size != CIRRUS_LUT_SIZE)
335		return;
336
337	for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
338		cirrus_crtc->lut_r[i] = red[i];
339		cirrus_crtc->lut_g[i] = green[i];
340		cirrus_crtc->lut_b[i] = blue[i];
341	}
342	cirrus_crtc_load_lut(crtc);
343}
344
345/* Simple cleanup function */
346static void cirrus_crtc_destroy(struct drm_crtc *crtc)
347{
348	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
349
350	drm_crtc_cleanup(crtc);
351	kfree(cirrus_crtc);
352}
353
354/* These provide the minimum set of functions required to handle a CRTC */
355static const struct drm_crtc_funcs cirrus_crtc_funcs = {
356	.gamma_set = cirrus_crtc_gamma_set,
357	.set_config = drm_crtc_helper_set_config,
358	.destroy = cirrus_crtc_destroy,
359};
360
361static const struct drm_crtc_helper_funcs cirrus_helper_funcs = {
362	.dpms = cirrus_crtc_dpms,
 
363	.mode_set = cirrus_crtc_mode_set,
364	.mode_set_base = cirrus_crtc_mode_set_base,
365	.prepare = cirrus_crtc_prepare,
366	.commit = cirrus_crtc_commit,
367	.load_lut = cirrus_crtc_load_lut,
368};
369
370/* CRTC setup */
371static void cirrus_crtc_init(struct drm_device *dev)
372{
373	struct cirrus_device *cdev = dev->dev_private;
374	struct cirrus_crtc *cirrus_crtc;
375	int i;
376
377	cirrus_crtc = kzalloc(sizeof(struct cirrus_crtc) +
378			      (CIRRUSFB_CONN_LIMIT * sizeof(struct drm_connector *)),
379			      GFP_KERNEL);
380
381	if (cirrus_crtc == NULL)
382		return;
383
384	drm_crtc_init(dev, &cirrus_crtc->base, &cirrus_crtc_funcs);
385
386	drm_mode_crtc_set_gamma_size(&cirrus_crtc->base, CIRRUS_LUT_SIZE);
387	cdev->mode_info.crtc = cirrus_crtc;
388
389	for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
390		cirrus_crtc->lut_r[i] = i;
391		cirrus_crtc->lut_g[i] = i;
392		cirrus_crtc->lut_b[i] = i;
393	}
394
395	drm_crtc_helper_add(&cirrus_crtc->base, &cirrus_helper_funcs);
396}
397
398/** Sets the color ramps on behalf of fbcon */
399void cirrus_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
400			      u16 blue, int regno)
401{
402	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
403
404	cirrus_crtc->lut_r[regno] = red;
405	cirrus_crtc->lut_g[regno] = green;
406	cirrus_crtc->lut_b[regno] = blue;
407}
408
409/** Gets the color ramps on behalf of fbcon */
410void cirrus_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
411			      u16 *blue, int regno)
412{
413	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
414
415	*red = cirrus_crtc->lut_r[regno];
416	*green = cirrus_crtc->lut_g[regno];
417	*blue = cirrus_crtc->lut_b[regno];
418}
419
 
 
 
 
 
 
 
 
420static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
421				struct drm_display_mode *mode,
422				struct drm_display_mode *adjusted_mode)
423{
424}
425
426static void cirrus_encoder_dpms(struct drm_encoder *encoder, int state)
427{
428	return;
429}
430
431static void cirrus_encoder_prepare(struct drm_encoder *encoder)
432{
433}
434
435static void cirrus_encoder_commit(struct drm_encoder *encoder)
436{
437}
438
439static void cirrus_encoder_destroy(struct drm_encoder *encoder)
440{
441	struct cirrus_encoder *cirrus_encoder = to_cirrus_encoder(encoder);
442	drm_encoder_cleanup(encoder);
443	kfree(cirrus_encoder);
444}
445
446static const struct drm_encoder_helper_funcs cirrus_encoder_helper_funcs = {
447	.dpms = cirrus_encoder_dpms,
 
448	.mode_set = cirrus_encoder_mode_set,
449	.prepare = cirrus_encoder_prepare,
450	.commit = cirrus_encoder_commit,
451};
452
453static const struct drm_encoder_funcs cirrus_encoder_encoder_funcs = {
454	.destroy = cirrus_encoder_destroy,
455};
456
457static struct drm_encoder *cirrus_encoder_init(struct drm_device *dev)
458{
459	struct drm_encoder *encoder;
460	struct cirrus_encoder *cirrus_encoder;
461
462	cirrus_encoder = kzalloc(sizeof(struct cirrus_encoder), GFP_KERNEL);
463	if (!cirrus_encoder)
464		return NULL;
465
466	encoder = &cirrus_encoder->base;
467	encoder->possible_crtcs = 0x1;
468
469	drm_encoder_init(dev, encoder, &cirrus_encoder_encoder_funcs,
470			 DRM_MODE_ENCODER_DAC, NULL);
471	drm_encoder_helper_add(encoder, &cirrus_encoder_helper_funcs);
472
473	return encoder;
474}
475
476
477static int cirrus_vga_get_modes(struct drm_connector *connector)
478{
479	int count;
480
481	/* Just add a static list of modes */
482	if (cirrus_bpp <= 24) {
483		count = drm_add_modes_noedid(connector, 1280, 1024);
484		drm_set_preferred_mode(connector, 1024, 768);
485	} else {
486		count = drm_add_modes_noedid(connector, 800, 600);
487		drm_set_preferred_mode(connector, 800, 600);
488	}
489	return count;
490}
491
 
 
 
 
 
 
 
492static struct drm_encoder *cirrus_connector_best_encoder(struct drm_connector
493						  *connector)
494{
495	int enc_id = connector->encoder_ids[0];
 
 
 
496	/* pick the encoder ids */
497	if (enc_id)
498		return drm_encoder_find(connector->dev, enc_id);
 
 
 
 
 
 
 
499	return NULL;
500}
501
502static enum drm_connector_status cirrus_vga_detect(struct drm_connector
503						   *connector, bool force)
504{
505	return connector_status_connected;
506}
507
508static void cirrus_connector_destroy(struct drm_connector *connector)
509{
510	drm_connector_cleanup(connector);
511	kfree(connector);
512}
513
514static const struct drm_connector_helper_funcs cirrus_vga_connector_helper_funcs = {
515	.get_modes = cirrus_vga_get_modes,
 
516	.best_encoder = cirrus_connector_best_encoder,
517};
518
519static const struct drm_connector_funcs cirrus_vga_connector_funcs = {
520	.dpms = drm_helper_connector_dpms,
521	.detect = cirrus_vga_detect,
522	.fill_modes = drm_helper_probe_single_connector_modes,
523	.destroy = cirrus_connector_destroy,
524};
525
526static struct drm_connector *cirrus_vga_init(struct drm_device *dev)
527{
528	struct drm_connector *connector;
529	struct cirrus_connector *cirrus_connector;
530
531	cirrus_connector = kzalloc(sizeof(struct cirrus_connector), GFP_KERNEL);
532	if (!cirrus_connector)
533		return NULL;
534
535	connector = &cirrus_connector->base;
536
537	drm_connector_init(dev, connector,
538			   &cirrus_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
539
540	drm_connector_helper_add(connector, &cirrus_vga_connector_helper_funcs);
541
542	drm_connector_register(connector);
543	return connector;
544}
545
546
547int cirrus_modeset_init(struct cirrus_device *cdev)
548{
549	struct drm_encoder *encoder;
550	struct drm_connector *connector;
551	int ret;
552
553	drm_mode_config_init(cdev->dev);
554	cdev->mode_info.mode_config_initialized = true;
555
556	cdev->dev->mode_config.max_width = CIRRUS_MAX_FB_WIDTH;
557	cdev->dev->mode_config.max_height = CIRRUS_MAX_FB_HEIGHT;
558
559	cdev->dev->mode_config.fb_base = cdev->mc.vram_base;
560	cdev->dev->mode_config.preferred_depth = 24;
561	/* don't prefer a shadow on virt GPU */
562	cdev->dev->mode_config.prefer_shadow = 0;
563
564	cirrus_crtc_init(cdev->dev);
565
566	encoder = cirrus_encoder_init(cdev->dev);
567	if (!encoder) {
568		DRM_ERROR("cirrus_encoder_init failed\n");
569		return -1;
570	}
571
572	connector = cirrus_vga_init(cdev->dev);
573	if (!connector) {
574		DRM_ERROR("cirrus_vga_init failed\n");
575		return -1;
576	}
577
578	drm_mode_connector_attach_encoder(connector, encoder);
579
580	ret = cirrus_fbdev_init(cdev);
581	if (ret) {
582		DRM_ERROR("cirrus_fbdev_init failed\n");
583		return ret;
584	}
585
586	return 0;
587}
588
589void cirrus_modeset_fini(struct cirrus_device *cdev)
590{
591	cirrus_fbdev_fini(cdev);
592
593	if (cdev->mode_info.mode_config_initialized) {
594		drm_mode_config_cleanup(cdev->dev);
595		cdev->mode_info.mode_config_initialized = false;
596	}
597}
v3.15
  1
  2/*
  3 * Copyright 2012 Red Hat
  4 *
  5 * This file is subject to the terms and conditions of the GNU General
  6 * Public License version 2. See the file COPYING in the main
  7 * directory of this archive for more details.
  8 *
  9 * Authors: Matthew Garrett
 10 *          Dave Airlie
 11 *
 12 * Portions of this code derived from cirrusfb.c:
 13 * drivers/video/cirrusfb.c - driver for Cirrus Logic chipsets
 14 *
 15 * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
 16 */
 17#include <drm/drmP.h>
 18#include <drm/drm_crtc_helper.h>
 
 19
 20#include <video/cirrus.h>
 21
 22#include "cirrus_drv.h"
 23
 24#define CIRRUS_LUT_SIZE 256
 25
 26#define PALETTE_INDEX 0x8
 27#define PALETTE_DATA 0x9
 28
 29/*
 30 * This file contains setup code for the CRTC.
 31 */
 32
 33static void cirrus_crtc_load_lut(struct drm_crtc *crtc)
 34{
 35	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
 36	struct drm_device *dev = crtc->dev;
 37	struct cirrus_device *cdev = dev->dev_private;
 38	int i;
 39
 40	if (!crtc->enabled)
 41		return;
 42
 43	for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
 44		/* VGA registers */
 45		WREG8(PALETTE_INDEX, i);
 46		WREG8(PALETTE_DATA, cirrus_crtc->lut_r[i]);
 47		WREG8(PALETTE_DATA, cirrus_crtc->lut_g[i]);
 48		WREG8(PALETTE_DATA, cirrus_crtc->lut_b[i]);
 49	}
 50}
 51
 52/*
 53 * The DRM core requires DPMS functions, but they make little sense in our
 54 * case and so are just stubs
 55 */
 56
 57static void cirrus_crtc_dpms(struct drm_crtc *crtc, int mode)
 58{
 59	struct drm_device *dev = crtc->dev;
 60	struct cirrus_device *cdev = dev->dev_private;
 61	u8 sr01, gr0e;
 62
 63	switch (mode) {
 64	case DRM_MODE_DPMS_ON:
 65		sr01 = 0x00;
 66		gr0e = 0x00;
 67		break;
 68	case DRM_MODE_DPMS_STANDBY:
 69		sr01 = 0x20;
 70		gr0e = 0x02;
 71		break;
 72	case DRM_MODE_DPMS_SUSPEND:
 73		sr01 = 0x20;
 74		gr0e = 0x04;
 75		break;
 76	case DRM_MODE_DPMS_OFF:
 77		sr01 = 0x20;
 78		gr0e = 0x06;
 79		break;
 80	default:
 81		return;
 82	}
 83
 84	WREG8(SEQ_INDEX, 0x1);
 85	sr01 |= RREG8(SEQ_DATA) & ~0x20;
 86	WREG_SEQ(0x1, sr01);
 87
 88	WREG8(GFX_INDEX, 0xe);
 89	gr0e |= RREG8(GFX_DATA) & ~0x06;
 90	WREG_GFX(0xe, gr0e);
 91}
 92
 93/*
 94 * The core passes the desired mode to the CRTC code to see whether any
 95 * CRTC-specific modifications need to be made to it. We're in a position
 96 * to just pass that straight through, so this does nothing
 97 */
 98static bool cirrus_crtc_mode_fixup(struct drm_crtc *crtc,
 99				   const struct drm_display_mode *mode,
100				   struct drm_display_mode *adjusted_mode)
101{
102	return true;
103}
104
105static void cirrus_set_start_address(struct drm_crtc *crtc, unsigned offset)
106{
107	struct cirrus_device *cdev = crtc->dev->dev_private;
108	u32 addr;
109	u8 tmp;
110
111	addr = offset >> 2;
112	WREG_CRT(0x0c, (u8)((addr >> 8) & 0xff));
113	WREG_CRT(0x0d, (u8)(addr & 0xff));
114
115	WREG8(CRT_INDEX, 0x1b);
116	tmp = RREG8(CRT_DATA);
117	tmp &= 0xf2;
118	tmp |= (addr >> 16) & 0x01;
119	tmp |= (addr >> 15) & 0x0c;
120	WREG_CRT(0x1b, tmp);
121	WREG8(CRT_INDEX, 0x1d);
122	tmp = RREG8(CRT_DATA);
123	tmp &= 0x7f;
124	tmp |= (addr >> 12) & 0x80;
125	WREG_CRT(0x1d, tmp);
126}
127
128/* cirrus is different - we will force move buffers out of VRAM */
129static int cirrus_crtc_do_set_base(struct drm_crtc *crtc,
130				struct drm_framebuffer *fb,
131				int x, int y, int atomic)
132{
133	struct cirrus_device *cdev = crtc->dev->dev_private;
134	struct drm_gem_object *obj;
135	struct cirrus_framebuffer *cirrus_fb;
136	struct cirrus_bo *bo;
137	int ret;
138	u64 gpu_addr;
139
140	/* push the previous fb to system ram */
141	if (!atomic && fb) {
142		cirrus_fb = to_cirrus_framebuffer(fb);
143		obj = cirrus_fb->obj;
144		bo = gem_to_cirrus_bo(obj);
145		ret = cirrus_bo_reserve(bo, false);
146		if (ret)
147			return ret;
148		cirrus_bo_push_sysram(bo);
149		cirrus_bo_unreserve(bo);
150	}
151
152	cirrus_fb = to_cirrus_framebuffer(crtc->primary->fb);
153	obj = cirrus_fb->obj;
154	bo = gem_to_cirrus_bo(obj);
155
156	ret = cirrus_bo_reserve(bo, false);
157	if (ret)
158		return ret;
159
160	ret = cirrus_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
161	if (ret) {
162		cirrus_bo_unreserve(bo);
163		return ret;
164	}
165
166	if (&cdev->mode_info.gfbdev->gfb == cirrus_fb) {
167		/* if pushing console in kmap it */
168		ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
169		if (ret)
170			DRM_ERROR("failed to kmap fbcon\n");
171	}
172	cirrus_bo_unreserve(bo);
173
174	cirrus_set_start_address(crtc, (u32)gpu_addr);
175	return 0;
176}
177
178static int cirrus_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
179			     struct drm_framebuffer *old_fb)
180{
181	return cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
182}
183
184/*
185 * The meat of this driver. The core passes us a mode and we have to program
186 * it. The modesetting here is the bare minimum required to satisfy the qemu
187 * emulation of this hardware, and running this against a real device is
188 * likely to result in an inadequately programmed mode. We've already had
189 * the opportunity to modify the mode, so whatever we receive here should
190 * be something that can be correctly programmed and displayed
191 */
192static int cirrus_crtc_mode_set(struct drm_crtc *crtc,
193				struct drm_display_mode *mode,
194				struct drm_display_mode *adjusted_mode,
195				int x, int y, struct drm_framebuffer *old_fb)
196{
197	struct drm_device *dev = crtc->dev;
198	struct cirrus_device *cdev = dev->dev_private;
199	int hsyncstart, hsyncend, htotal, hdispend;
200	int vtotal, vdispend;
201	int tmp;
202	int sr07 = 0, hdr = 0;
203
204	htotal = mode->htotal / 8;
205	hsyncend = mode->hsync_end / 8;
206	hsyncstart = mode->hsync_start / 8;
207	hdispend = mode->hdisplay / 8;
208
209	vtotal = mode->vtotal;
210	vdispend = mode->vdisplay;
211
212	vdispend -= 1;
213	vtotal -= 2;
214
215	htotal -= 5;
216	hdispend -= 1;
217	hsyncstart += 1;
218	hsyncend += 1;
219
220	WREG_CRT(VGA_CRTC_V_SYNC_END, 0x20);
221	WREG_CRT(VGA_CRTC_H_TOTAL, htotal);
222	WREG_CRT(VGA_CRTC_H_DISP, hdispend);
223	WREG_CRT(VGA_CRTC_H_SYNC_START, hsyncstart);
224	WREG_CRT(VGA_CRTC_H_SYNC_END, hsyncend);
225	WREG_CRT(VGA_CRTC_V_TOTAL, vtotal & 0xff);
226	WREG_CRT(VGA_CRTC_V_DISP_END, vdispend & 0xff);
227
228	tmp = 0x40;
229	if ((vdispend + 1) & 512)
230		tmp |= 0x20;
231	WREG_CRT(VGA_CRTC_MAX_SCAN, tmp);
232
233	/*
234	 * Overflow bits for values that don't fit in the standard registers
235	 */
236	tmp = 16;
237	if (vtotal & 256)
238		tmp |= 1;
239	if (vdispend & 256)
240		tmp |= 2;
241	if ((vdispend + 1) & 256)
242		tmp |= 8;
243	if (vtotal & 512)
244		tmp |= 32;
245	if (vdispend & 512)
246		tmp |= 64;
247	WREG_CRT(VGA_CRTC_OVERFLOW, tmp);
248
249	tmp = 0;
250
251	/* More overflow bits */
252
253	if ((htotal + 5) & 64)
254		tmp |= 16;
255	if ((htotal + 5) & 128)
256		tmp |= 32;
257	if (vtotal & 256)
258		tmp |= 64;
259	if (vtotal & 512)
260		tmp |= 128;
261
262	WREG_CRT(CL_CRT1A, tmp);
263
264	/* Disable Hercules/CGA compatibility */
265	WREG_CRT(VGA_CRTC_MODE, 0x03);
266
267	WREG8(SEQ_INDEX, 0x7);
268	sr07 = RREG8(SEQ_DATA);
269	sr07 &= 0xe0;
270	hdr = 0;
271	switch (crtc->primary->fb->bits_per_pixel) {
272	case 8:
273		sr07 |= 0x11;
274		break;
275	case 16:
276		sr07 |= 0x17;
277		hdr = 0xc1;
278		break;
279	case 24:
280		sr07 |= 0x15;
281		hdr = 0xc5;
282		break;
283	case 32:
284		sr07 |= 0x19;
285		hdr = 0xc5;
286		break;
287	default:
288		return -1;
289	}
290
291	WREG_SEQ(0x7, sr07);
292
293	/* Program the pitch */
294	tmp = crtc->primary->fb->pitches[0] / 8;
295	WREG_CRT(VGA_CRTC_OFFSET, tmp);
296
297	/* Enable extended blanking and pitch bits, and enable full memory */
298	tmp = 0x22;
299	tmp |= (crtc->primary->fb->pitches[0] >> 7) & 0x10;
300	tmp |= (crtc->primary->fb->pitches[0] >> 6) & 0x40;
301	WREG_CRT(0x1b, tmp);
302
303	/* Enable high-colour modes */
304	WREG_GFX(VGA_GFX_MODE, 0x40);
305
306	/* And set graphics mode */
307	WREG_GFX(VGA_GFX_MISC, 0x01);
308
309	WREG_HDR(hdr);
310	cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
311
312	/* Unblank (needed on S3 resume, vgabios doesn't do it then) */
313	outb(0x20, 0x3c0);
314	return 0;
315}
316
317/*
318 * This is called before a mode is programmed. A typical use might be to
319 * enable DPMS during the programming to avoid seeing intermediate stages,
320 * but that's not relevant to us
321 */
322static void cirrus_crtc_prepare(struct drm_crtc *crtc)
323{
324}
325
326/*
327 * This is called after a mode is programmed. It should reverse anything done
328 * by the prepare function
329 */
330static void cirrus_crtc_commit(struct drm_crtc *crtc)
331{
332}
333
334/*
335 * The core can pass us a set of gamma values to program. We actually only
336 * use this for 8-bit mode so can't perform smooth fades on deeper modes,
337 * but it's a requirement that we provide the function
338 */
339static void cirrus_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
340				  u16 *blue, uint32_t start, uint32_t size)
341{
342	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
343	int i;
344
345	if (size != CIRRUS_LUT_SIZE)
346		return;
347
348	for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
349		cirrus_crtc->lut_r[i] = red[i];
350		cirrus_crtc->lut_g[i] = green[i];
351		cirrus_crtc->lut_b[i] = blue[i];
352	}
353	cirrus_crtc_load_lut(crtc);
354}
355
356/* Simple cleanup function */
357static void cirrus_crtc_destroy(struct drm_crtc *crtc)
358{
359	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
360
361	drm_crtc_cleanup(crtc);
362	kfree(cirrus_crtc);
363}
364
365/* These provide the minimum set of functions required to handle a CRTC */
366static const struct drm_crtc_funcs cirrus_crtc_funcs = {
367	.gamma_set = cirrus_crtc_gamma_set,
368	.set_config = drm_crtc_helper_set_config,
369	.destroy = cirrus_crtc_destroy,
370};
371
372static const struct drm_crtc_helper_funcs cirrus_helper_funcs = {
373	.dpms = cirrus_crtc_dpms,
374	.mode_fixup = cirrus_crtc_mode_fixup,
375	.mode_set = cirrus_crtc_mode_set,
376	.mode_set_base = cirrus_crtc_mode_set_base,
377	.prepare = cirrus_crtc_prepare,
378	.commit = cirrus_crtc_commit,
379	.load_lut = cirrus_crtc_load_lut,
380};
381
382/* CRTC setup */
383static void cirrus_crtc_init(struct drm_device *dev)
384{
385	struct cirrus_device *cdev = dev->dev_private;
386	struct cirrus_crtc *cirrus_crtc;
387	int i;
388
389	cirrus_crtc = kzalloc(sizeof(struct cirrus_crtc) +
390			      (CIRRUSFB_CONN_LIMIT * sizeof(struct drm_connector *)),
391			      GFP_KERNEL);
392
393	if (cirrus_crtc == NULL)
394		return;
395
396	drm_crtc_init(dev, &cirrus_crtc->base, &cirrus_crtc_funcs);
397
398	drm_mode_crtc_set_gamma_size(&cirrus_crtc->base, CIRRUS_LUT_SIZE);
399	cdev->mode_info.crtc = cirrus_crtc;
400
401	for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
402		cirrus_crtc->lut_r[i] = i;
403		cirrus_crtc->lut_g[i] = i;
404		cirrus_crtc->lut_b[i] = i;
405	}
406
407	drm_crtc_helper_add(&cirrus_crtc->base, &cirrus_helper_funcs);
408}
409
410/** Sets the color ramps on behalf of fbcon */
411void cirrus_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
412			      u16 blue, int regno)
413{
414	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
415
416	cirrus_crtc->lut_r[regno] = red;
417	cirrus_crtc->lut_g[regno] = green;
418	cirrus_crtc->lut_b[regno] = blue;
419}
420
421/** Gets the color ramps on behalf of fbcon */
422void cirrus_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
423			      u16 *blue, int regno)
424{
425	struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
426
427	*red = cirrus_crtc->lut_r[regno];
428	*green = cirrus_crtc->lut_g[regno];
429	*blue = cirrus_crtc->lut_b[regno];
430}
431
432
433static bool cirrus_encoder_mode_fixup(struct drm_encoder *encoder,
434				      const struct drm_display_mode *mode,
435				      struct drm_display_mode *adjusted_mode)
436{
437	return true;
438}
439
440static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
441				struct drm_display_mode *mode,
442				struct drm_display_mode *adjusted_mode)
443{
444}
445
446static void cirrus_encoder_dpms(struct drm_encoder *encoder, int state)
447{
448	return;
449}
450
451static void cirrus_encoder_prepare(struct drm_encoder *encoder)
452{
453}
454
455static void cirrus_encoder_commit(struct drm_encoder *encoder)
456{
457}
458
459static void cirrus_encoder_destroy(struct drm_encoder *encoder)
460{
461	struct cirrus_encoder *cirrus_encoder = to_cirrus_encoder(encoder);
462	drm_encoder_cleanup(encoder);
463	kfree(cirrus_encoder);
464}
465
466static const struct drm_encoder_helper_funcs cirrus_encoder_helper_funcs = {
467	.dpms = cirrus_encoder_dpms,
468	.mode_fixup = cirrus_encoder_mode_fixup,
469	.mode_set = cirrus_encoder_mode_set,
470	.prepare = cirrus_encoder_prepare,
471	.commit = cirrus_encoder_commit,
472};
473
474static const struct drm_encoder_funcs cirrus_encoder_encoder_funcs = {
475	.destroy = cirrus_encoder_destroy,
476};
477
478static struct drm_encoder *cirrus_encoder_init(struct drm_device *dev)
479{
480	struct drm_encoder *encoder;
481	struct cirrus_encoder *cirrus_encoder;
482
483	cirrus_encoder = kzalloc(sizeof(struct cirrus_encoder), GFP_KERNEL);
484	if (!cirrus_encoder)
485		return NULL;
486
487	encoder = &cirrus_encoder->base;
488	encoder->possible_crtcs = 0x1;
489
490	drm_encoder_init(dev, encoder, &cirrus_encoder_encoder_funcs,
491			 DRM_MODE_ENCODER_DAC);
492	drm_encoder_helper_add(encoder, &cirrus_encoder_helper_funcs);
493
494	return encoder;
495}
496
497
498static int cirrus_vga_get_modes(struct drm_connector *connector)
499{
500	int count;
501
502	/* Just add a static list of modes */
503	count = drm_add_modes_noedid(connector, 1280, 1024);
504	drm_set_preferred_mode(connector, 1024, 768);
 
 
 
 
 
505	return count;
506}
507
508static int cirrus_vga_mode_valid(struct drm_connector *connector,
509				 struct drm_display_mode *mode)
510{
511	/* Any mode we've added is valid */
512	return MODE_OK;
513}
514
515static struct drm_encoder *cirrus_connector_best_encoder(struct drm_connector
516						  *connector)
517{
518	int enc_id = connector->encoder_ids[0];
519	struct drm_mode_object *obj;
520	struct drm_encoder *encoder;
521
522	/* pick the encoder ids */
523	if (enc_id) {
524		obj =
525		    drm_mode_object_find(connector->dev, enc_id,
526					 DRM_MODE_OBJECT_ENCODER);
527		if (!obj)
528			return NULL;
529		encoder = obj_to_encoder(obj);
530		return encoder;
531	}
532	return NULL;
533}
534
535static enum drm_connector_status cirrus_vga_detect(struct drm_connector
536						   *connector, bool force)
537{
538	return connector_status_connected;
539}
540
541static void cirrus_connector_destroy(struct drm_connector *connector)
542{
543	drm_connector_cleanup(connector);
544	kfree(connector);
545}
546
547struct drm_connector_helper_funcs cirrus_vga_connector_helper_funcs = {
548	.get_modes = cirrus_vga_get_modes,
549	.mode_valid = cirrus_vga_mode_valid,
550	.best_encoder = cirrus_connector_best_encoder,
551};
552
553struct drm_connector_funcs cirrus_vga_connector_funcs = {
554	.dpms = drm_helper_connector_dpms,
555	.detect = cirrus_vga_detect,
556	.fill_modes = drm_helper_probe_single_connector_modes,
557	.destroy = cirrus_connector_destroy,
558};
559
560static struct drm_connector *cirrus_vga_init(struct drm_device *dev)
561{
562	struct drm_connector *connector;
563	struct cirrus_connector *cirrus_connector;
564
565	cirrus_connector = kzalloc(sizeof(struct cirrus_connector), GFP_KERNEL);
566	if (!cirrus_connector)
567		return NULL;
568
569	connector = &cirrus_connector->base;
570
571	drm_connector_init(dev, connector,
572			   &cirrus_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
573
574	drm_connector_helper_add(connector, &cirrus_vga_connector_helper_funcs);
575
 
576	return connector;
577}
578
579
580int cirrus_modeset_init(struct cirrus_device *cdev)
581{
582	struct drm_encoder *encoder;
583	struct drm_connector *connector;
584	int ret;
585
586	drm_mode_config_init(cdev->dev);
587	cdev->mode_info.mode_config_initialized = true;
588
589	cdev->dev->mode_config.max_width = CIRRUS_MAX_FB_WIDTH;
590	cdev->dev->mode_config.max_height = CIRRUS_MAX_FB_HEIGHT;
591
592	cdev->dev->mode_config.fb_base = cdev->mc.vram_base;
593	cdev->dev->mode_config.preferred_depth = 24;
594	/* don't prefer a shadow on virt GPU */
595	cdev->dev->mode_config.prefer_shadow = 0;
596
597	cirrus_crtc_init(cdev->dev);
598
599	encoder = cirrus_encoder_init(cdev->dev);
600	if (!encoder) {
601		DRM_ERROR("cirrus_encoder_init failed\n");
602		return -1;
603	}
604
605	connector = cirrus_vga_init(cdev->dev);
606	if (!connector) {
607		DRM_ERROR("cirrus_vga_init failed\n");
608		return -1;
609	}
610
611	drm_mode_connector_attach_encoder(connector, encoder);
612
613	ret = cirrus_fbdev_init(cdev);
614	if (ret) {
615		DRM_ERROR("cirrus_fbdev_init failed\n");
616		return ret;
617	}
618
619	return 0;
620}
621
622void cirrus_modeset_fini(struct cirrus_device *cdev)
623{
624	cirrus_fbdev_fini(cdev);
625
626	if (cdev->mode_info.mode_config_initialized) {
627		drm_mode_config_cleanup(cdev->dev);
628		cdev->mode_info.mode_config_initialized = false;
629	}
630}