aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/drm/emgd_fb.c
blob: 4d668c8ac9bd610d5bac2968ec702c2e5e0e1461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: emgd_fb.c
 * $Revision: 1.4 $
 *-----------------------------------------------------------------------------
 * Copyright © 2002-2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *-----------------------------------------------------------------------------
 * Description:
 *  Framebuffer / kenrel mode setting functions.
 *-----------------------------------------------------------------------------
 */
#include "drm_emgd_private.h"
#include "emgd_drv.h"
#include "emgd_drm.h"
#include "memory.h"
/* #include <drmP.h> */
/* #include <drm.h> */
/* #include <drm_crtc.h> */
#include <drm_crtc_helper.h>


/*
 * Move this to emgd_drv.h?
 */
typedef struct _emgd_framebuffer {
	struct drm_framebuffer base;
	struct fb_info *info;
	unsigned long size;
	unsigned long offset;
} emgd_framebuffer_t;

#define to_emgd_framebuffer(x) container_of(x, struct _emgd_framebuffer, base)

typedef struct _emgdfb_par {
	struct drm_device *dev;
	emgd_framebuffer_t *emgd_fb;
} emgdfb_par_t;


typedef struct _emgd_crtc {
	struct drm_crtc base;
	emgd_framebuffer_t *fbdev_fb;
	struct drm_mode_set mode_set;
	struct drm_display_mode saved_mode;
	struct drm_display_mode saved_adjusted_mode;
	unsigned char lut_r[256];
	unsigned char lut_g[256];
	unsigned char lut_b[256];
	unsigned char lut_a[256];
} emgd_crtc_t;



static struct drm_framebuffer *emgd_fb_create(struct drm_device *dev,
		struct drm_file *filp, struct drm_mode_fb_cmd *r);
int  emgd_fb_probe(struct drm_device *dev);
static void emgd_fb_destroy (struct drm_framebuffer *fb);
static int emgd_fb_create_handle(struct drm_framebuffer *fb,
		struct drm_file *file_priv, unsigned int *handle);
static int emgd_fb_check_var(struct fb_var_screeninfo *var,
		struct fb_info *info);
static int emgd_fb_set_par(struct fb_info *info);
static int emgd_fb_setcolreg(unsigned int regno,
		unsigned int red, unsigned int green, unsigned int blue,
		unsigned int transp, struct fb_info *info);
static int emgd_fb_pan_display(struct fb_var_screeninfo *var,
		struct fb_info *info);
static int emgd_fb_blank(int blank, struct fb_info *info);

static const struct drm_mode_config_funcs emgd_mode_funcs = {
	.fb_create = emgd_fb_create,
};

static const struct drm_framebuffer_funcs emgd_fb_funcs = {
	.destroy = emgd_fb_destroy,
	.create_handle = emgd_fb_create_handle,
};

static const struct fb_ops emgd_fb_ops = {
	.owner = THIS_MODULE,
	.fb_check_var = emgd_fb_check_var,
	.fb_set_par = emgd_fb_set_par,
	.fb_setcolreg = emgd_fb_setcolreg,
	.fb_fillrect = cfb_fillrect,
	.fb_copyarea = cfb_copyarea,
	.fb_imageblit = cfb_imageblit,
	.fb_mmap = NULL,
	.fb_pan_display = emgd_fb_pan_display,
	.fb_blank = emgd_fb_blank,
};


static void emgd_crtc_dpms(struct drm_crtc *crtc, int mode);
static bool emgd_crtc_mode_fixup(struct drm_crtc *crtc,
		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode);
static int emgd_crtc_mode_set(struct drm_crtc *crtc,
		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode,
		int x, int y, struct drm_framebuffer *old_fb);
static int emgd_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
		struct drm_framebuffer *old_fb);
static void emgd_crtc_prepare(struct drm_crtc *crtc);
static void emgd_crtc_commit(struct drm_crtc *crtc);

static void emgd_crtc_save(struct drm_crtc *crtc);
static void emgd_crtc_restore(struct drm_crtc *crtc);
static int emgd_crtc_cursor_set(struct drm_crtc *crtc,
		struct drm_file *file_priv, uint32_t handle,
		uint32_t width, uint32_t height);
static int emgd_crtc_cursor_move(struct drm_crtc *crtc, int x, int y);
static void emgd_crtc_gamma_set(struct drm_crtc *crtc,
		u16 *red, u16 *green, u16 *blue,
		uint32_t start, uint32_t size);
static void emgd_crtc_destroy(struct drm_crtc *crtc);



static const struct drm_crtc_helper_funcs emgd_helper_funcs = {
	.dpms = emgd_crtc_dpms,
	.mode_fixup = emgd_crtc_mode_fixup,
	.mode_set = emgd_crtc_mode_set,
	.mode_set_base = emgd_crtc_mode_set_base,
	.prepare = emgd_crtc_prepare,
	.commit = emgd_crtc_commit,
};

static const struct drm_crtc_funcs emgd_crtc_funcs = {
	.save = emgd_crtc_save,
	.restore = emgd_crtc_restore,
	.cursor_set = emgd_crtc_cursor_set,
	.cursor_move = emgd_crtc_cursor_move,
	.gamma_set = emgd_crtc_gamma_set,
	.set_config = drm_crtc_helper_set_config,
	.destroy = emgd_crtc_destroy,
};





static struct drm_mode_set panic_mode;

/*
 * emgd_fb_restore
 *
 * Restore the kernel's fbcon mode.
 */
void emgd_fb_restore(void)
{
	int ret;

	/*
	 * FIXME: Need to have the real crtc saved so it can be restored.
	 */
	if ((ret = drm_crtc_helper_set_config(&panic_mode)) != 0) {
		printk(KERN_ERR "Failed to restore crtc configuration: %d\n", ret);
	}
}


/*
 * Called if something fails while trying to set up framebuffer based
 * console.
 */
static int emgd_fb_panic(struct notifier_block *n,
		unsigned long res,
		void *panic_str)
{
	printk(KERN_ALERT "Panic occurred, switch back to text console.");

	emgd_fb_restore();

	return 0;
}


static struct notifier_block paniced = {
	.notifier_call = emgd_fb_panic,
};



/*
 * emgd_ms_init
 *
 * This is the main initialization entry point.  Called during driver load
 * and does basic setup.
 */
void emgd_ms_init(struct drm_device *dev)
{
	emgd_crtc_t *emgd_crtc;
	unsigned short *r, *g, *b;
	int i;

	drm_mode_config_init(dev);  /* drm helper function */

	dev->mode_config.min_width = 0;
	dev->mode_config.max_width = 2048;
	dev->mode_config.min_height = 0;
	dev->mode_config.max_height = 2048;
	dev->mode_config.funcs = (void *)&emgd_mode_funcs;
	dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);

	emgd_crtc = kzalloc(sizeof(emgd_crtc_t) +
			(INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
			GFP_KERNEL);

	if (emgd_crtc == NULL) {
		printk(KERN_ERR "emgd_ms_init: Failed to allocate CRTC structure.\n");
		return;
	}

	drm_crtc_init(dev, &emgd_crtc->base, &emgd_crtc_funcs);
	drm_mode_crtc_set_gamma_size(&emgd_crtc->base, 256);

	/* Set initial gamma values */
	r = emgd_crtc->base.gamma_store;
	g = emgd_crtc->base.gamma_store + 256;
	b = emgd_crtc->base.gamma_store + 512;
	for (i = 0; i < 256; i++) {
		emgd_crtc->lut_r[i] = i;
		emgd_crtc->lut_g[i] = i;
		emgd_crtc->lut_b[i] = i;
		emgd_crtc->lut_a[i] = 0;
		r[i] = i << 8;
		g[i] = i << 8;
		b[i] = i << 8;
	}

	drm_crtc_helper_add(&emgd_crtc->base, &emgd_helper_funcs);

	/* TODO: Create connector list */


	/* TODO: Is there more this needs to do? */
	/*
	 * i915 driver does the following:
	 *       Q? what is base?
	 *   drm_crtc_init(dev, base, crtc_funcs);
	 *   drm_mode_crtc_set_gamma_size(base, 256);
	 *   drm_crtc_helper_add(base, helper_funcs);
	 *   Builds dev->mode_config.connector_list
	 *
	 *   crtc_funcs:
	 *		cursor_set  - probabl a simple wrapper around alter_cursor
	 *		cursor_move - probabl a simple wrapper around alter_cursor
	 *		gamma_set   - set palette ?
	 *		set_config  - drm_crtc_helper_set_config
	 *		destroy     - clean up
	 *
	 *   drm_crtc_helper_funcs
	 *		dpms         - power management
	 *		mode_fixup   - ???
	 *		mode_set     - wrapper around alter_displays ?
	 *		mode_set_base- noop (handled in alter_displays) ?
	 *		prepare      - DPMS off
	 *		commit       - DPMS on
	 *
	 * This is a lot of the code that impliments KMS.
	 */

	emgd_fb_probe(dev);
}


/*
 * emgd_fb_probe
 *
 * Handle changes to the framebuffer.
 */
int emgd_fb_probe(struct drm_device *drm)
{

	/* FIXME: Need to set panic_mode to the a valid mode */

	/* Register a notifier to switch back to kernel console on panic */
	atomic_notifier_chain_register(&panic_notifier_list, &paniced);

	return 0;
}
EXPORT_SYMBOL(emgd_fb_probe);


/*
 * emgd_fb_remove
 *
 * clean up everything we've done to create the framebuffer instance.
 */
int emgd_fb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
{
	emgd_framebuffer_t *emgd_fb = to_emgd_framebuffer(fb);
	struct fb_info *info= emgd_fb->info;

	if (info) {
		unregister_framebuffer(info);
		framebuffer_release(info);
	}

	/* Remove the notifier that checks for failed framebuffers */
	atomic_notifier_chain_unregister(&panic_notifier_list, &paniced);
	memset(&panic_mode, 0, sizeof(struct drm_mode_set));

	return 0;
}
EXPORT_SYMBOL(emgd_fb_remove);


/*
 * emgd_fb_create
 */
static struct drm_framebuffer *emgd_fb_create(struct drm_device *dev,
		struct drm_file *filp,
		struct drm_mode_fb_cmd *r)
{
	int ret;
	struct fb_info *info;
	emgdfb_par_t *par;
	emgd_framebuffer_t *emgd_fb;

	emgd_fb = kzalloc(sizeof(emgd_framebuffer_t), GFP_KERNEL);

	/*
	 * TODO: What is drm_mode_fb_cmd?  UMG gets some kind of memory
	 *       block based on r->handle (psb_get_meminfo_by_handle())
	 */

	/* Create a framebuffer instance */
	ret = drm_framebuffer_init(dev, &emgd_fb->base, &emgd_fb_funcs);
	if (ret) {
		printk(KERN_ERR "Failed to create framebuffer instance.\n");
		return NULL;
	}

	drm_helper_mode_fill_fb_struct(&emgd_fb->base, r);

	info = framebuffer_alloc(sizeof(emgdfb_par_t), &dev->pdev->dev);
	if (info == NULL) {
		printk(KERN_ERR "Failed to allocate framebuffer info.\n");
		return NULL;
	}

	par = info->par;

	/* Fill in fb info structure */
	strcpy(info->fix.id, "emgdfb");
	info->fix.type = FB_TYPE_PACKED_PIXELS;
	info->fix.visual = FB_VISUAL_TRUECOLOR;
	info->fix.type_aux = 0;
	info->fix.xpanstep = 1;
	info->fix.ypanstep = 1;
	info->fix.ywrapstep = 0;
	info->fix.accel = FB_ACCEL_I830;
	info->fix.line_length = emgd_fb->base.pitch;
	info->fix.smem_start = dev->mode_config.fb_base;
	info->fix.smem_len = emgd_fb->size;
	info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
	info->fix.mmio_len = pci_resource_len(dev->pdev, 0);

	info->flags = FBINFO_DEFAULT;
	info->fbops = (struct fb_ops*)&emgd_fb_ops; 
	info->screen_base = 0;  /* FIXME: This is kernel memory address */
	info->screen_size = emgd_fb->size;

	info->var.xres_virtual = emgd_fb->base.width;
	info->var.yres_virtual = emgd_fb->base.height;
	info->var.bits_per_pixel = emgd_fb->base.bits_per_pixel;
	info->var.xoffset = 0;
	info->var.yoffset = 0;
	info->var.activate = FB_ACTIVATE_NOW;
	info->var.width = -1;
	info->var.height = -1;
	info->var.xres = r->width;
	info->var.yres = r->height;

	info->pixmap.size = 64 * 1024;
	info->pixmap.buf_align = 8;
	info->pixmap.access_align = 32;
	info->pixmap.flags = FB_PIXMAP_SYSTEM;
	info->pixmap.scan_align = 1;

	switch (emgd_fb->base.depth) {
		case 8:
			info->var.red.offset = 0;
			info->var.green.offset = 0;
			info->var.blue.offset = 0;
			info->var.red.length = 8;
			info->var.green.length = 8;
			info->var.blue.length = 8;
			info->var.transp.offset = 0;
			info->var.transp.length = 0;
			break;
		case 15:
			info->var.red.offset = 10;
			info->var.green.offset = 5;
			info->var.blue.offset = 0;
			info->var.red.length = 5;
			info->var.green.length = 5;
			info->var.blue.length = 5;
			info->var.transp.offset = 15;
			info->var.transp.length = 1;
			break;
		case 16:
			info->var.red.offset = 11;
			info->var.green.offset = 5;
			info->var.blue.offset = 0;
			info->var.red.length = 5;
			info->var.green.length = 6;
			info->var.blue.length = 5;
			info->var.transp.offset = 0;
			info->var.transp.length = 0;
			break;
		case 24:
			info->var.red.offset = 16;
			info->var.green.offset = 8;
			info->var.blue.offset = 0;
			info->var.red.length = 8;
			info->var.green.length = 8;
			info->var.blue.length = 8;
			info->var.transp.offset = 0;
			info->var.transp.length = 0;
			break;
		case 32:
			info->var.red.offset = 16;
			info->var.green.offset = 8;
			info->var.blue.offset = 0;
			info->var.red.length = 8;
			info->var.green.length = 8;
			info->var.blue.length = 8;
			info->var.transp.offset = 24;
			info->var.transp.length = 8;
			break;
		default:
			break;
	}

	register_framebuffer(info);

	emgd_fb->info = info;
	par->emgd_fb = emgd_fb;
	par->dev = dev;

	return &(emgd_fb->base);
}



/*
 * emgd_fb_destroy
 *
 * clean up and remove a framebuffer instance.
 */
static void emgd_fb_destroy (struct drm_framebuffer *fb)
{
	emgd_framebuffer_t *emgd_fb = to_emgd_framebuffer(fb);
	struct drm_device *dev = fb->dev;

	/* TODO: Unmap any pages mapped to the GTT */

	if (emgd_fb->info) {
		emgd_fb_remove(dev, fb);
	}

	drm_framebuffer_cleanup(fb);

	kfree(fb);
}


/*
 * emgd_fb_create_handle
 *
 * TODO: Figure out what this is suppose to do. The intel driver creates
 * a handle to a GEM object.  The UMG driver does nothing.
 */
static int emgd_fb_create_handle(struct drm_framebuffer *fb,
		struct drm_file *file_priv,
		unsigned int *handle)
{
	(void)file_priv;
	*handle = 0;

	return 0;
}


static int emgd_fb_check_var(struct fb_var_screeninfo *var,
		struct fb_info *info)
{
	printk(KERN_ALERT "STUBED emgd_fb_check_var\n");
	return 0;
}

static int emgd_fb_set_par(struct fb_info *info)
{
	printk(KERN_ALERT "STUBED emgd_fb_set_par\n");
	return 0;
}

static int emgd_fb_setcolreg(unsigned int regno,
		unsigned int red, unsigned int green, unsigned int blue,
		unsigned int transp, struct fb_info *info)
{
	printk(KERN_ALERT "STUBED emgd_fb_setcolreg\n");
	return 0;
}

static int emgd_fb_pan_display(struct fb_var_screeninfo *var,
		struct fb_info *info)
{
	printk(KERN_ALERT "STUBED emgd_fb_pan_display\n");
	return 0;
}

static int emgd_fb_blank(int blank, struct fb_info *info)
{
	switch(blank) {
	case FB_BLANK_UNBLANK:
		printk(KERN_ALERT "Should call turn on display\n");
		break;
	case FB_BLANK_NORMAL:
		printk(KERN_ALERT "Should call standby\n");
		break;
	case FB_BLANK_HSYNC_SUSPEND:
		printk(KERN_ALERT "Should call standby\n");
		break;
	case FB_BLANK_VSYNC_SUSPEND:
		printk(KERN_ALERT "Should call suspend\n");
		break;
	case FB_BLANK_POWERDOWN:
		printk(KERN_ALERT "Should call DRM_MODE_DPMS_OFF\n");
		break;
	}

	return 0;
}



/****************************************************************************
 * CRTC functions
 ****************************************************************************/

static void emgd_crtc_dpms(struct drm_crtc *crtc, int mode)
{
	printk(KERN_ALERT "emgd_crtc_dpms: STUB\n");
}

static bool emgd_crtc_mode_fixup(struct drm_crtc *crtc,
		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
{
	printk(KERN_ALERT "emgd_crtc_mode_fixup: STUB\n");
	return 0;
}

static int emgd_crtc_mode_set(struct drm_crtc *crtc,
		struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode,
		int x, int y, struct drm_framebuffer *old_fb)
{
	printk(KERN_ALERT "emgd_crtc_mode_set: STUB\n");
	return 0;
}

static int emgd_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
		struct drm_framebuffer *old_fb)
{
	printk(KERN_ALERT "emgd_pipe_set_base: STUB\n");
	return 0;
}

static void emgd_crtc_prepare(struct drm_crtc *crtc)
{
	printk(KERN_ALERT "emgd_crtc_prepare: STUB\n");
}

static void emgd_crtc_commit(struct drm_crtc *crtc)
{
	printk(KERN_ALERT "emgd_crtc_commit: STUB\n");
}


static void emgd_crtc_save(struct drm_crtc *crtc)
{
	printk(KERN_ALERT "emgd_crtc_save: STUB\n");
}

static void emgd_crtc_restore(struct drm_crtc *crtc)
{
	printk(KERN_ALERT "emgd_crtc_restore: STUB\n");
}

static int emgd_crtc_cursor_set(struct drm_crtc *crtc,
		struct drm_file *file_priv, uint32_t handle,
		uint32_t width, uint32_t height)
{
	printk(KERN_ALERT "emgd_crtc_cursor_set: STUB\n");
	return 0;
}

static int emgd_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
{
	printk(KERN_ALERT "emgd_crtc_cursor_move: STUB\n");
	return 0;
}

static void emgd_crtc_gamma_set(struct drm_crtc *crtc,
		u16 *red, u16 *green, u16 *blue,
		uint32_t start, uint32_t size)
{
	printk(KERN_ALERT "emgd_crtc_gamma_set: STUB\n");
}

static void emgd_crtc_destroy(struct drm_crtc *crtc)
{
	printk(KERN_ALERT "emgd_crtc_destroy: STUB\n");
}