aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/core/init/plb/init_plb.c
blob: 061fefb996756164feb6bda6fdbaf8a41ee7a6a8 (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
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: init_plb.c
 * $Revision: 1.16 $
 *-----------------------------------------------------------------------------
 * 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:
 *  
 *-----------------------------------------------------------------------------
 */

#define MODULE_NAME hal.init

#include <io.h>
#include <pci.h>
#include <memmap.h>
#include <gart.h>

#include <memory.h>
#include <igd.h>
#include <igd_errno.h>
#include <igd_init.h>
#include <igd_gart.h>

#include <context.h>
#include <intelpci.h>
#include <general.h>

#include <plb/regs.h>
#include <plb/context.h>

#include "../cmn/init_dispatch.h"
#include "/usr/include/linux/pci_regs.h"
/*!
 * @addtogroup core_group
 * @{
 */

static int bus_master_enable_plb(platform_context_plb_t *platform_context);
static int full_config_vga_plb(igd_context_t *context,
	init_dispatch_t *dispatch);
static int get_stolen_mem_plb(igd_context_t *context, unsigned long *pages);

int full_get_param_plb(igd_context_t *context,
	unsigned long id,
	unsigned long *value);

/*!
 * 
 * @param context
 * @param dispatch
 * @param vga_dev
 * 
 * @return -IGD_ERROR_NODEV on failure
 * @return 0 on success
 */
int get_revision_id_plb(igd_context_t *context,
	os_pci_dev_t vga_dev)
{
	EMGD_TRACE_ENTER;

	/* Read RID */
	if(OS_PCI_READ_CONFIG_8(vga_dev, PCI_RID,
			(unsigned char *)&context->device_context.rid)) {
		EMGD_ERROR_EXIT("Error occured reading RID");
		return -IGD_ERROR_NODEV;
	}

	EMGD_DEBUG(" rid = 0x%lx", context->device_context.rid);

	EMGD_TRACE_EXIT;
	return 0;
}

/*!
 * 
 * @param context
 * @param dispatch
 * 
 * @return 1 on failure
 * @return 0 on success
 */
int full_config_plb(igd_context_t *context,
	init_dispatch_t *dispatch)
{
	unsigned long /* FIXME - reserved_mem, */ graphics_frequency ;
	platform_context_plb_t *platform_context;
	int ret;

	EMGD_TRACE_ENTER;

	platform_context = (platform_context_plb_t *)context->platform_context;

	_sgx_base = 0x40000;	
	_msvdx_base = 0x50000;	

	/*
	 * Enable bus mastering for platforms whose BIOS did not perform this
	 * task for us.
	 */
	ret = bus_master_enable_plb(platform_context);
	if(ret) {
		EMGD_ERROR("Error: Enabling bus master");
	}

	/* Config VGA */
	ret = full_config_vga_plb(context, dispatch);
	if(ret) {
		EMGD_ERROR_EXIT("Config VGA Failed");
		return ret;
	}

	get_stolen_mem_plb(context, &context->device_context.reserved_mem);

#if 0 /* FIXME - WHY IS THIS RETURNING 0 AND SETTING reserved_mem TO 0 ALSO? */
	/* Get mem reservation param if it exists */
	if(!full_get_param_plb(context, IGD_PARAM_MEM_RESERVATION,
			&reserved_mem)) {
		context->device_context.reserved_mem = reserved_mem;
	}
#endif

	/* Get graphics frequency param if it exists */
	if(!full_get_param_plb(context, IGD_PARAM_GFX_FREQ,
			&graphics_frequency)) {
		context->device_context.gfx_freq = (unsigned short)graphics_frequency;
	}

	EMGD_TRACE_EXIT;
	return 0;
}

/*!
 * 
 * @param platform_context
 * 
 * @return -1 on failure
 * @return 0 on success
 */
static int bus_master_enable_plb(platform_context_plb_t *platform_context){
	int ret;
	unsigned char tmp;

	EMGD_TRACE_ENTER;

	ret = OS_PCI_READ_CONFIG_8(platform_context->pcidev0, PCI_COMMAND_MASTER, &tmp);
	if(ret) {
		EMGD_ERROR_EXIT("PCI read of bus master");
		return -1;
	}

	/*
	 * Get Bit 2, 1, and 0 and see if it is == 1
	 * all 3 bits has to be enabled. This is to enable register read/write
	 * in the case of a PCI card being added
	 */
	if((tmp & 0x7) != 0x7 ) {

		tmp |= 0x7;
		ret = OS_PCI_WRITE_CONFIG_8(platform_context->pcidev0, PCI_COMMAND_MASTER, tmp);
		if(ret) {
			EMGD_ERROR_EXIT("PCI write of bus master");
			return -1;
		}
	}

	EMGD_TRACE_EXIT;
	return 0;
}

/*!
 * 
 * @param context
 * @param dispatch
 * 
 * @return -IGD_ERROR_NODEV on failure
 * @return 0 on success
 */
static int full_config_vga_plb(igd_context_t *context,
	init_dispatch_t *dispatch)
{
	platform_context_plb_t *platform_context =
		(platform_context_plb_t *)context->platform_context;

	EMGD_TRACE_ENTER;

	if(OS_PCI_READ_CONFIG_32(platform_context->pcidev0,
			PLB_PCI_MMADR, (void*)&context->device_context.mmadr)) {
		EMGD_ERROR_EXIT("Reading MMADR");
		return -IGD_ERROR_NODEV;
	}

	context->device_context.mmadr &= 0xfffffff9;
	context->device_context.virt_mmadr =
		OS_MAP_IO_TO_MEM_NOCACHE(context->device_context.mmadr, PLB_MMIO_SIZE);

	if (!context->device_context.virt_mmadr) {
		EMGD_ERROR_EXIT("Failed to map MMADR");
		return -IGD_ERROR_NODEV;
	}

	EMGD_DEBUG("mmadr mapped %dKB @           (phys):0x%lx (virt):%p",
		PLB_MMIO_SIZE/1024,
		context->device_context.mmadr,
		context->device_context.virt_mmadr);

	/* PCI Interrupt Line */
	if(OS_PCI_READ_CONFIG_8(platform_context->pcidev0,
			PCI_INTERRUPT_LINE, (void*)&platform_context->irq)) {
		platform_context->irq = 0;
	}

	EMGD_TRACE_EXIT;
	return 0;
}

/*!
 * Get the # of pages used for video memory. This does not use information from
 * the scratch register, since this is done later if it exists.
 * 
 * @param context
 * @param pages
 * 
 * @return -IGD_ERROR_INVAL on failure
 * @return 0 on success
 */
static int get_stolen_mem_plb(igd_context_t *context, unsigned long *pages)
{
	platform_context_plb_t *platform_context;
	os_pci_dev_t           vga_dev;
	unsigned short         gmch_ctl;
	unsigned long          stolen_mem; /* in bytes */
	int ret;

	EMGD_TRACE_ENTER;

	platform_context = (platform_context_plb_t *)context->platform_context;
	vga_dev = platform_context->pcidev0;

	ret = OS_PCI_READ_CONFIG_16(vga_dev, PLB_PCI_GC, &gmch_ctl);
	if (ret) {
		EMGD_ERROR_EXIT("Unable to read PLB_PCI_GC");
		return -IGD_ERROR_INVAL;
	}

	switch (gmch_ctl & 0x70) {
	case 0x00:
		stolen_mem = 0;
		break;
	case 0x10:
		/* 1M */
		stolen_mem = 1*1024*1024;
		break;
	case 0x20:
		/* 4M */
		stolen_mem = 4*1024*1024;
		break;
	case 0x30:
		/* 8M */
		stolen_mem = 8*1024*1024;
		break;
	case 0x40:
		/* 16M */
		stolen_mem = 16*1024*1024;
		break;
	case 0x50:
		/* 32M */
		stolen_mem = 32*1024*1024;
		break;
	case 0x60:
		/* 48M */
		stolen_mem = 48*1024*1024;
	case 0x70:
		/* 64M */
		stolen_mem = 64*1024*1024;
		break;
	default:
		EMGD_ERROR_EXIT("Unknown Stolen Memory Size");
		return -IGD_ERROR_INVAL;
	}

	if (stolen_mem) {
		/*
		 * Subtract off the size of the GTT which is
		 * (number of entries in DWORDS) * 4 to get it into bytes
		 */
		stolen_mem -= context->device_context.gatt_pages*4;
		/* Subtract off 1 page for the scratch page */
		stolen_mem -= 4*1024;
	}

	/* Convert to the # of pages available for stolen memory */
	*pages = stolen_mem / 4096;

	EMGD_DEBUG("Stolen memory: 0x%lx pages", *pages);

	EMGD_TRACE_EXIT;
	return 0;
}

/*!
 * 
 * @param context
 * @param id
 * @param value
 * 
 * @return -IGD_ERROR_INVAL on failure
 * @return 0 on success
 */
int full_get_param_plb(igd_context_t *context,
	unsigned long id,
	unsigned long *value)
{
	int ret = 0;
	unsigned char *mmio;
	unsigned long control_reg;
	
	EMGD_TRACE_ENTER;

	EMGD_DEBUG("ID: 0x%lx", id);

	/* Scratch registers used as below:
	 *
	 * 0x71410:
	 * --------
	 *   Bits 31-16 - EID Firmware identifier 0xE1DF
	 *   Bits 15-00 - Tell what data is present.
	 * Here are bits for what we are using know:
	 *   Bit 0 - Panel id
	 *   Bit 1 - List of ports for which displays are attached
	 *   Bit 2 - Memory reservation
	 * If any of the above bits is set that mean data is followed
	 * in the next registers.
	 *
	 * 0x71414:
	 * --------
	 *   Bits 07-00 - Panel Id
	 *   Bits 11-08 - Port list
	 * Information for Port list: If any of the bit is set means,
	 *   a display is attached to that port as follows:
	 *    Bit 08 - CRT
	 *    Bit 09 - DVOA/Internal LVDS
	 *    Bit 10 - DVOB/RGBA
	 *    Bit 11 - DVOC
	 *
	 * 0x71418:
	 * --------
	 *	 Bits 15-00 - Reserved Memory value in number of 4k size pages
	 */
	mmio = context->device_context.virt_mmadr;
	control_reg = EMGD_READ32(EMGD_MMIO(mmio) + 0x71410);
	*value = 0;

	switch(id) {
	case IGD_PARAM_PANEL_ID:
		/*
		 * Check for Embedded firmware
		 */
		if ((control_reg>>16) != 0xE1DF) {
			EMGD_DEBUG("No Embedded vBIOS found");
			EMGD_TRACE_EXIT;
			return -IGD_ERROR_INVAL;
		}

		/*
		 * The panel id bit must be set in the control register
		 * to indicate valid panel (config) ID value.
		 */
		if (control_reg & 0x1) {
			*value = EMGD_READ32(EMGD_MMIO(mmio) + 0x71414) & 0xFF;
			if(!(*value)) {
				/* we cannot allow for config id = 0 */
				ret = -IGD_ERROR_INVAL;
			}
		} else {
			EMGD_DEBUG("Panel ID read failed: Incorrect Operation");
			ret = -IGD_ERROR_INVAL;
		}
		break;
	case IGD_PARAM_MEM_RESERVATION:
		/*
		 * Check for Embedded firmware
		 */
		if ((control_reg>>16) != 0xE1DF) {
			EMGD_DEBUG("No Embedded vBIOS found");
			EMGD_TRACE_EXIT;
			return -IGD_ERROR_INVAL;
		}

		/*
		 * The mem reservation bit must be set in the control register
		 * to indicate valid mem reservation value.
		 */
		if (control_reg & 0x4) {
			*value = (EMGD_READ32(EMGD_MMIO(mmio) + 0x71418) & 0xFFFF);
		} else {
			EMGD_DEBUG("Mem Reservation read failed: Incorrect Operation");
			ret = -IGD_ERROR_INVAL;
		}
		break;
	default:
		ret = -IGD_ERROR_INVAL;
		break;
	}

	EMGD_TRACE_EXIT;
	return ret;
}

/*!
 * 
 * @param context
 * 
 * @return void
 */
void full_shutdown_plb(igd_context_t *context)
{
	platform_context_plb_t *platform_context =
		(platform_context_plb_t *)context->platform_context;

	EMGD_TRACE_ENTER;

	/* unmap registers */
	if(context->device_context.virt_mmadr) {
		EMGD_DEBUG("Unmapping Gfx registers and GTT Table...");
		OS_UNMAP_IO_FROM_MEM((void *) context->device_context.virt_mmadr,
			PLB_MMIO_SIZE);
		OS_UNMAP_IO_FROM_MEM((void *)context->device_context.virt_gttadr,
			context->device_context.gatt_pages * 4);
	} else {
		printk(KERN_ERR "Unmapping MMIO space failed.\n");
	}

	if (platform_context) {
		OS_PCI_FREE_DEVICE(platform_context->pcidev0);
	}
	EMGD_TRACE_EXIT;
}