1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/video/console/sticore.c -
4  *	core code for console driver using HP's STI firmware
5  *
6  *	Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
7  *	Copyright (C) 2001-2013 Helge Deller <deller@gmx.de>
8  *	Copyright (C) 2001-2002 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
9  *
10  * TODO:
11  * - call STI in virtual mode rather than in real mode
12  * - screen blanking with state_mgmt() in text mode STI ?
13  * - try to make it work on m68k hp workstations ;)
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/pci.h>
23 #include <linux/font.h>
24 
25 #include <asm/hardware.h>
26 #include <asm/page.h>
27 #include <asm/parisc-device.h>
28 #include <asm/pdc.h>
29 #include <asm/cacheflush.h>
30 #include <asm/grfioctl.h>
31 
32 #include "../fbdev/sticore.h"
33 
34 #define STI_DRIVERVERSION "Version 0.9b"
35 
36 static struct sti_struct *default_sti __read_mostly;
37 
38 /* number of STI ROMS found and their ptrs to each struct */
39 static int num_sti_roms __read_mostly;
40 static struct sti_struct *sti_roms[MAX_STI_ROMS] __read_mostly;
41 
42 
43 /* The colour indices used by STI are
44  *   0 - Black
45  *   1 - White
46  *   2 - Red
47  *   3 - Yellow/Brown
48  *   4 - Green
49  *   5 - Cyan
50  *   6 - Blue
51  *   7 - Magenta
52  *
53  * So we have the same colours as VGA (basically one bit each for R, G, B),
54  * but have to translate them, anyway. */
55 
56 static const u8 col_trans[8] = {
57         0, 6, 4, 5,
58         2, 7, 3, 1
59 };
60 
61 #define c_fg(sti, c) col_trans[((c>> 8) & 7)]
62 #define c_bg(sti, c) col_trans[((c>>11) & 7)]
63 #define c_index(sti, c) ((c) & 0xff)
64 
65 static const struct sti_init_flags default_init_flags = {
66 	.wait	= STI_WAIT,
67 	.reset	= 1,
68 	.text	= 1,
69 	.nontext = 1,
70 	.no_chg_bet = 1,
71 	.no_chg_bei = 1,
72 	.init_cmap_tx = 1,
73 };
74 
sti_init_graph(struct sti_struct * sti)75 static int sti_init_graph(struct sti_struct *sti)
76 {
77 	struct sti_init_inptr *inptr = &sti->sti_data->init_inptr;
78 	struct sti_init_inptr_ext *inptr_ext = &sti->sti_data->init_inptr_ext;
79 	struct sti_init_outptr *outptr = &sti->sti_data->init_outptr;
80 	unsigned long flags;
81 	int ret, err;
82 
83 	spin_lock_irqsave(&sti->lock, flags);
84 
85 	memset(inptr, 0, sizeof(*inptr));
86 	inptr->text_planes = 3; /* # of text planes (max 3 for STI) */
87 	memset(inptr_ext, 0, sizeof(*inptr_ext));
88 	inptr->ext_ptr = STI_PTR(inptr_ext);
89 	outptr->errno = 0;
90 
91 	ret = sti_call(sti, sti->init_graph, &default_init_flags, inptr,
92 		outptr, sti->glob_cfg);
93 
94 	if (ret >= 0)
95 		sti->text_planes = outptr->text_planes;
96 	err = outptr->errno;
97 
98 	spin_unlock_irqrestore(&sti->lock, flags);
99 
100 	if (ret < 0) {
101 		pr_err("STI init_graph failed (ret %d, errno %d)\n", ret, err);
102 		return -1;
103 	}
104 
105 	return 0;
106 }
107 
108 static const struct sti_conf_flags default_conf_flags = {
109 	.wait	= STI_WAIT,
110 };
111 
sti_inq_conf(struct sti_struct * sti)112 static void sti_inq_conf(struct sti_struct *sti)
113 {
114 	struct sti_conf_inptr *inptr = &sti->sti_data->inq_inptr;
115 	struct sti_conf_outptr *outptr = &sti->sti_data->inq_outptr;
116 	unsigned long flags;
117 	s32 ret;
118 
119 	outptr->ext_ptr = STI_PTR(&sti->sti_data->inq_outptr_ext);
120 
121 	do {
122 		spin_lock_irqsave(&sti->lock, flags);
123 		memset(inptr, 0, sizeof(*inptr));
124 		ret = sti_call(sti, sti->inq_conf, &default_conf_flags,
125 			inptr, outptr, sti->glob_cfg);
126 		spin_unlock_irqrestore(&sti->lock, flags);
127 	} while (ret == 1);
128 }
129 
130 static const struct sti_font_flags default_font_flags = {
131 	.wait		= STI_WAIT,
132 	.non_text	= 0,
133 };
134 
135 void
sti_putc(struct sti_struct * sti,int c,int y,int x)136 sti_putc(struct sti_struct *sti, int c, int y, int x)
137 {
138 	struct sti_font_inptr *inptr = &sti->sti_data->font_inptr;
139 	struct sti_font_inptr inptr_default = {
140 		.font_start_addr= STI_PTR(sti->font->raw),
141 		.index		= c_index(sti, c),
142 		.fg_color	= c_fg(sti, c),
143 		.bg_color	= c_bg(sti, c),
144 		.dest_x		= x * sti->font_width,
145 		.dest_y		= y * sti->font_height,
146 	};
147 	struct sti_font_outptr *outptr = &sti->sti_data->font_outptr;
148 	s32 ret;
149 	unsigned long flags;
150 
151 	do {
152 		spin_lock_irqsave(&sti->lock, flags);
153 		*inptr = inptr_default;
154 		ret = sti_call(sti, sti->font_unpmv, &default_font_flags,
155 			inptr, outptr, sti->glob_cfg);
156 		spin_unlock_irqrestore(&sti->lock, flags);
157 	} while (ret == 1);
158 }
159 
160 static const struct sti_blkmv_flags clear_blkmv_flags = {
161 	.wait	= STI_WAIT,
162 	.color	= 1,
163 	.clear	= 1,
164 };
165 
166 void
sti_set(struct sti_struct * sti,int src_y,int src_x,int height,int width,u8 color)167 sti_set(struct sti_struct *sti, int src_y, int src_x,
168 	int height, int width, u8 color)
169 {
170 	struct sti_blkmv_inptr *inptr = &sti->sti_data->blkmv_inptr;
171 	struct sti_blkmv_inptr inptr_default = {
172 		.fg_color	= color,
173 		.bg_color	= color,
174 		.src_x		= src_x,
175 		.src_y		= src_y,
176 		.dest_x		= src_x,
177 		.dest_y		= src_y,
178 		.width		= width,
179 		.height		= height,
180 	};
181 	struct sti_blkmv_outptr *outptr = &sti->sti_data->blkmv_outptr;
182 	s32 ret;
183 	unsigned long flags;
184 
185 	do {
186 		spin_lock_irqsave(&sti->lock, flags);
187 		*inptr = inptr_default;
188 		ret = sti_call(sti, sti->block_move, &clear_blkmv_flags,
189 			inptr, outptr, sti->glob_cfg);
190 		spin_unlock_irqrestore(&sti->lock, flags);
191 	} while (ret == 1);
192 }
193 
194 void
sti_clear(struct sti_struct * sti,int src_y,int src_x,int height,int width,int c)195 sti_clear(struct sti_struct *sti, int src_y, int src_x,
196 	  int height, int width, int c)
197 {
198 	struct sti_blkmv_inptr *inptr = &sti->sti_data->blkmv_inptr;
199 	struct sti_blkmv_inptr inptr_default = {
200 		.fg_color	= c_fg(sti, c),
201 		.bg_color	= c_bg(sti, c),
202 		.src_x		= src_x * sti->font_width,
203 		.src_y		= src_y * sti->font_height,
204 		.dest_x		= src_x * sti->font_width,
205 		.dest_y		= src_y * sti->font_height,
206 		.width		= width * sti->font_width,
207 		.height		= height* sti->font_height,
208 	};
209 	struct sti_blkmv_outptr *outptr = &sti->sti_data->blkmv_outptr;
210 	s32 ret;
211 	unsigned long flags;
212 
213 	do {
214 		spin_lock_irqsave(&sti->lock, flags);
215 		*inptr = inptr_default;
216 		ret = sti_call(sti, sti->block_move, &clear_blkmv_flags,
217 			inptr, outptr, sti->glob_cfg);
218 		spin_unlock_irqrestore(&sti->lock, flags);
219 	} while (ret == 1);
220 }
221 
222 static const struct sti_blkmv_flags default_blkmv_flags = {
223 	.wait = STI_WAIT,
224 };
225 
226 void
sti_bmove(struct sti_struct * sti,int src_y,int src_x,int dst_y,int dst_x,int height,int width)227 sti_bmove(struct sti_struct *sti, int src_y, int src_x,
228 	  int dst_y, int dst_x, int height, int width)
229 {
230 	struct sti_blkmv_inptr *inptr = &sti->sti_data->blkmv_inptr;
231 	struct sti_blkmv_inptr inptr_default = {
232 		.src_x		= src_x * sti->font_width,
233 		.src_y		= src_y * sti->font_height,
234 		.dest_x		= dst_x * sti->font_width,
235 		.dest_y		= dst_y * sti->font_height,
236 		.width		= width * sti->font_width,
237 		.height		= height* sti->font_height,
238 	};
239 	struct sti_blkmv_outptr *outptr = &sti->sti_data->blkmv_outptr;
240 	s32 ret;
241 	unsigned long flags;
242 
243 	do {
244 		spin_lock_irqsave(&sti->lock, flags);
245 		*inptr = inptr_default;
246 		ret = sti_call(sti, sti->block_move, &default_blkmv_flags,
247 			inptr, outptr, sti->glob_cfg);
248 		spin_unlock_irqrestore(&sti->lock, flags);
249 	} while (ret == 1);
250 }
251 
252 
sti_flush(unsigned long start,unsigned long end)253 static void sti_flush(unsigned long start, unsigned long end)
254 {
255 	flush_icache_range(start, end);
256 }
257 
sti_rom_copy(unsigned long base,unsigned long count,void * dest)258 static void sti_rom_copy(unsigned long base, unsigned long count, void *dest)
259 {
260 	unsigned long dest_start = (unsigned long) dest;
261 
262 	/* this still needs to be revisited (see arch/parisc/mm/init.c:246) ! */
263 	while (count >= 4) {
264 		count -= 4;
265 		*(u32 *)dest = gsc_readl(base);
266 		base += 4;
267 		dest += 4;
268 	}
269 	while (count) {
270 		count--;
271 		*(u8 *)dest = gsc_readb(base);
272 		base++;
273 		dest++;
274 	}
275 
276 	sti_flush(dest_start, (unsigned long)dest);
277 }
278 
279 
280 
281 
282 static char default_sti_path[21] __read_mostly;
283 
284 #ifndef MODULE
sti_setup(char * str)285 static int __init sti_setup(char *str)
286 {
287 	if (str)
288 		strlcpy (default_sti_path, str, sizeof (default_sti_path));
289 
290 	return 1;
291 }
292 
293 /*	Assuming the machine has multiple STI consoles (=graphic cards) which
294  *	all get detected by sticon, the user may define with the linux kernel
295  *	parameter sti=<x> which of them will be the initial boot-console.
296  *	<x> is a number between 0 and MAX_STI_ROMS, with 0 as the default
297  *	STI screen.
298  */
299 __setup("sti=", sti_setup);
300 #endif
301 
302 
303 
304 static char *font_name[MAX_STI_ROMS];
305 static int font_index[MAX_STI_ROMS],
306 	   font_height[MAX_STI_ROMS],
307 	   font_width[MAX_STI_ROMS];
308 #ifndef MODULE
sti_font_setup(char * str)309 static int sti_font_setup(char *str)
310 {
311 	char *x;
312 	int i = 0;
313 
314 	/* we accept sti_font=VGA8x16, sti_font=10x20, sti_font=10*20
315 	 * or sti_font=7 style command lines. */
316 
317 	while (i<MAX_STI_ROMS && str && *str) {
318 		if (*str>='0' && *str<='9') {
319 			if ((x = strchr(str, 'x')) || (x = strchr(str, '*'))) {
320 				font_height[i] = simple_strtoul(str, NULL, 0);
321 				font_width[i] = simple_strtoul(x+1, NULL, 0);
322 			} else {
323 				font_index[i] = simple_strtoul(str, NULL, 0);
324 			}
325 		} else {
326 			font_name[i] = str;	/* fb font name */
327 		}
328 
329 		if ((x = strchr(str, ',')))
330 			*x++ = 0;
331 		str = x;
332 
333 		i++;
334 	}
335 
336 	return 1;
337 }
338 
339 /*	The optional linux kernel parameter "sti_font" defines which font
340  *	should be used by the sticon driver to draw characters to the screen.
341  *	Possible values are:
342  *	- sti_font=<fb_fontname>:
343  *		<fb_fontname> is the name of one of the linux-kernel built-in
344  *		framebuffer font names (e.g. VGA8x16, SUN22x18).
345  *		This is only available if the fonts have been statically compiled
346  *		in with e.g. the CONFIG_FONT_8x16 or CONFIG_FONT_SUN12x22 options.
347  *	- sti_font=<number>
348  *		most STI ROMs have built-in HP specific fonts, which can be selected
349  *		by giving the desired number to the sticon driver.
350  *		NOTE: This number is machine and STI ROM dependend.
351  *	- sti_font=<height>x<width>  (e.g. sti_font=16x8)
352  *		<height> and <width> gives hints to the height and width of the
353  *		font which the user wants. The sticon driver will try to use
354  *		a font with this height and width, but if no suitable font is
355  *		found, sticon will use the default 8x8 font.
356  */
357 __setup("sti_font=", sti_font_setup);
358 #endif
359 
360 
361 
sti_dump_globcfg(struct sti_glob_cfg * glob_cfg,unsigned int sti_mem_request)362 static void sti_dump_globcfg(struct sti_glob_cfg *glob_cfg,
363 			     unsigned int sti_mem_request)
364 {
365 	struct sti_glob_cfg_ext *cfg;
366 
367 	DPRINTK((KERN_INFO
368 		"%d text planes\n"
369 		"%4d x %4d screen resolution\n"
370 		"%4d x %4d offscreen\n"
371 		"%4d x %4d layout\n"
372 		"regions at %08x %08x %08x %08x\n"
373 		"regions at %08x %08x %08x %08x\n"
374 		"reent_lvl %d\n"
375 		"save_addr %08x\n",
376 		glob_cfg->text_planes,
377 		glob_cfg->onscreen_x, glob_cfg->onscreen_y,
378 		glob_cfg->offscreen_x, glob_cfg->offscreen_y,
379 		glob_cfg->total_x, glob_cfg->total_y,
380 		glob_cfg->region_ptrs[0], glob_cfg->region_ptrs[1],
381 		glob_cfg->region_ptrs[2], glob_cfg->region_ptrs[3],
382 		glob_cfg->region_ptrs[4], glob_cfg->region_ptrs[5],
383 		glob_cfg->region_ptrs[6], glob_cfg->region_ptrs[7],
384 		glob_cfg->reent_lvl,
385 		glob_cfg->save_addr));
386 
387 	/* dump extended cfg */
388 	cfg = PTR_STI((unsigned long)glob_cfg->ext_ptr);
389 	DPRINTK(( KERN_INFO
390 		"monitor %d\n"
391 		"in friendly mode: %d\n"
392 		"power consumption %d watts\n"
393 		"freq ref %d\n"
394 		"sti_mem_addr %08x (size=%d bytes)\n",
395 		cfg->curr_mon,
396 		cfg->friendly_boot,
397 		cfg->power,
398 		cfg->freq_ref,
399 		cfg->sti_mem_addr, sti_mem_request));
400 }
401 
sti_dump_outptr(struct sti_struct * sti)402 static void sti_dump_outptr(struct sti_struct *sti)
403 {
404 	DPRINTK((KERN_INFO
405 		"%d bits per pixel\n"
406 		"%d used bits\n"
407 		"%d planes\n"
408 		"attributes %08x\n",
409 		 sti->sti_data->inq_outptr.bits_per_pixel,
410 		 sti->sti_data->inq_outptr.bits_used,
411 		 sti->sti_data->inq_outptr.planes,
412 		 sti->sti_data->inq_outptr.attributes));
413 }
414 
sti_init_glob_cfg(struct sti_struct * sti,unsigned long rom_address,unsigned long hpa)415 static int sti_init_glob_cfg(struct sti_struct *sti, unsigned long rom_address,
416 			     unsigned long hpa)
417 {
418 	struct sti_glob_cfg *glob_cfg;
419 	struct sti_glob_cfg_ext *glob_cfg_ext;
420 	void *save_addr;
421 	void *sti_mem_addr;
422 	int i, size;
423 
424 	if (sti->sti_mem_request < 256)
425 		sti->sti_mem_request = 256; /* STI default */
426 
427 	size = sizeof(struct sti_all_data) + sti->sti_mem_request - 256;
428 
429 	sti->sti_data = kzalloc(size, STI_LOWMEM);
430 	if (!sti->sti_data)
431 		return -ENOMEM;
432 
433 	glob_cfg	= &sti->sti_data->glob_cfg;
434 	glob_cfg_ext	= &sti->sti_data->glob_cfg_ext;
435 	save_addr	= &sti->sti_data->save_addr;
436 	sti_mem_addr	= &sti->sti_data->sti_mem_addr;
437 
438 	glob_cfg->ext_ptr = STI_PTR(glob_cfg_ext);
439 	glob_cfg->save_addr = STI_PTR(save_addr);
440 	for (i=0; i<8; i++) {
441 		unsigned long newhpa, len;
442 
443 		if (sti->pd) {
444 			unsigned char offs = sti->rm_entry[i];
445 
446 			if (offs == 0)
447 				continue;
448 			if (offs != PCI_ROM_ADDRESS &&
449 			    (offs < PCI_BASE_ADDRESS_0 ||
450 			     offs > PCI_BASE_ADDRESS_5)) {
451 				printk (KERN_WARNING
452 					"STI pci region mapping for region %d (%02x) can't be mapped\n",
453 					i,sti->rm_entry[i]);
454 				continue;
455 			}
456 			newhpa = pci_resource_start (sti->pd, (offs - PCI_BASE_ADDRESS_0) / 4);
457 		} else
458 			newhpa = (i == 0) ? rom_address : hpa;
459 
460 		sti->regions_phys[i] =
461 			REGION_OFFSET_TO_PHYS(sti->regions[i], newhpa);
462 
463 		len = sti->regions[i].region_desc.length * 4096;
464 		if (len)
465 			glob_cfg->region_ptrs[i] = sti->regions_phys[i];
466 
467 		DPRINTK(("region #%d: phys %08lx, region_ptr %08x, len=%lukB, "
468 			 "btlb=%d, sysonly=%d, cache=%d, last=%d\n",
469 			i, sti->regions_phys[i], glob_cfg->region_ptrs[i],
470 			len/1024,
471 			sti->regions[i].region_desc.btlb,
472 			sti->regions[i].region_desc.sys_only,
473 			sti->regions[i].region_desc.cache,
474 			sti->regions[i].region_desc.last));
475 
476 		/* last entry reached ? */
477 		if (sti->regions[i].region_desc.last)
478 			break;
479 	}
480 
481 	if (++i<8 && sti->regions[i].region)
482 		printk(KERN_WARNING "%s: *future ptr (0x%8x) not yet supported !\n",
483 				__FILE__, sti->regions[i].region);
484 
485 	glob_cfg_ext->sti_mem_addr = STI_PTR(sti_mem_addr);
486 
487 	sti->glob_cfg = glob_cfg;
488 
489 	return 0;
490 }
491 
492 #ifdef CONFIG_FONT_SUPPORT
493 static struct sti_cooked_font *
sti_select_fbfont(struct sti_cooked_rom * cooked_rom,const char * fbfont_name)494 sti_select_fbfont(struct sti_cooked_rom *cooked_rom, const char *fbfont_name)
495 {
496 	const struct font_desc *fbfont = NULL;
497 	unsigned int size, bpc;
498 	void *dest;
499 	struct sti_rom_font *nf;
500 	struct sti_cooked_font *cooked_font;
501 
502 	if (fbfont_name && strlen(fbfont_name))
503 		fbfont = find_font(fbfont_name);
504 	if (!fbfont)
505 		fbfont = get_default_font(1024,768, ~(u32)0, ~(u32)0);
506 	if (!fbfont)
507 		return NULL;
508 
509 	pr_info("STI selected %dx%d framebuffer font %s for sticon\n",
510 			fbfont->width, fbfont->height, fbfont->name);
511 
512 	bpc = ((fbfont->width+7)/8) * fbfont->height;
513 	size = bpc * 256;
514 	size += sizeof(struct sti_rom_font);
515 
516 	nf = kzalloc(size, STI_LOWMEM);
517 	if (!nf)
518 		return NULL;
519 
520 	nf->first_char = 0;
521 	nf->last_char = 255;
522 	nf->width = fbfont->width;
523 	nf->height = fbfont->height;
524 	nf->font_type = STI_FONT_HPROMAN8;
525 	nf->bytes_per_char = bpc;
526 	nf->next_font = 0;
527 	nf->underline_height = 1;
528 	nf->underline_pos = fbfont->height - nf->underline_height;
529 
530 	dest = nf;
531 	dest += sizeof(struct sti_rom_font);
532 	memcpy(dest, fbfont->data, bpc*256);
533 
534 	cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
535 	if (!cooked_font) {
536 		kfree(nf);
537 		return NULL;
538 	}
539 
540 	cooked_font->raw = nf;
541 	cooked_font->next_font = NULL;
542 
543 	cooked_rom->font_start = cooked_font;
544 
545 	return cooked_font;
546 }
547 #else
548 static struct sti_cooked_font *
sti_select_fbfont(struct sti_cooked_rom * cooked_rom,const char * fbfont_name)549 sti_select_fbfont(struct sti_cooked_rom *cooked_rom, const char *fbfont_name)
550 {
551 	return NULL;
552 }
553 #endif
554 
sti_select_font(struct sti_cooked_rom * rom,int (* search_font_fnc)(struct sti_cooked_rom *,int,int))555 static struct sti_cooked_font *sti_select_font(struct sti_cooked_rom *rom,
556 		int (*search_font_fnc)(struct sti_cooked_rom *, int, int))
557 {
558 	struct sti_cooked_font *font;
559 	int i;
560 	int index = num_sti_roms;
561 
562 	/* check for framebuffer-font first */
563 	if ((font = sti_select_fbfont(rom, font_name[index])))
564 		return font;
565 
566 	if (font_width[index] && font_height[index])
567 		font_index[index] = search_font_fnc(rom,
568 				font_height[index], font_width[index]);
569 
570 	for (font = rom->font_start, i = font_index[index];
571 	    font && (i > 0);
572 	    font = font->next_font, i--);
573 
574 	if (font)
575 		return font;
576 	else
577 		return rom->font_start;
578 }
579 
580 
sti_dump_rom(struct sti_rom * rom)581 static void sti_dump_rom(struct sti_rom *rom)
582 {
583 	printk(KERN_INFO "    id %04x-%04x, conforms to spec rev. %d.%02x\n",
584 		rom->graphics_id[0],
585 		rom->graphics_id[1],
586 		rom->revno[0] >> 4,
587 		rom->revno[0] & 0x0f);
588 	DPRINTK(("      supports %d monitors\n", rom->num_mons));
589 	DPRINTK(("      font start %08x\n", rom->font_start));
590 	DPRINTK(("      region list %08x\n", rom->region_list));
591 	DPRINTK(("      init_graph %08x\n", rom->init_graph));
592 	DPRINTK(("      bus support %02x\n", rom->bus_support));
593 	DPRINTK(("      ext bus support %02x\n", rom->ext_bus_support));
594 	DPRINTK(("      alternate code type %d\n", rom->alt_code_type));
595 }
596 
597 
sti_cook_fonts(struct sti_cooked_rom * cooked_rom,struct sti_rom * raw_rom)598 static int sti_cook_fonts(struct sti_cooked_rom *cooked_rom,
599 			  struct sti_rom *raw_rom)
600 {
601 	struct sti_rom_font *raw_font, *font_start;
602 	struct sti_cooked_font *cooked_font;
603 
604 	cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
605 	if (!cooked_font)
606 		return 0;
607 
608 	cooked_rom->font_start = cooked_font;
609 
610 	raw_font = ((void *)raw_rom) + (raw_rom->font_start);
611 
612 	font_start = raw_font;
613 	cooked_font->raw = raw_font;
614 
615 	while (raw_font->next_font) {
616 		raw_font = ((void *)font_start) + (raw_font->next_font);
617 
618 		cooked_font->next_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
619 		if (!cooked_font->next_font)
620 			return 1;
621 
622 		cooked_font = cooked_font->next_font;
623 
624 		cooked_font->raw = raw_font;
625 	}
626 
627 	cooked_font->next_font = NULL;
628 	return 1;
629 }
630 
631 
sti_search_font(struct sti_cooked_rom * rom,int height,int width)632 static int sti_search_font(struct sti_cooked_rom *rom, int height, int width)
633 {
634 	struct sti_cooked_font *font;
635 	int i = 0;
636 
637 	for (font = rom->font_start; font; font = font->next_font, i++) {
638 		if ((font->raw->width == width) &&
639 		    (font->raw->height == height))
640 			return i;
641 	}
642 	return 0;
643 }
644 
645 #define BMODE_RELOCATE(offset)		offset = (offset) / 4;
646 #define BMODE_LAST_ADDR_OFFS		0x50
647 
sti_bmode_font_raw(struct sti_cooked_font * f)648 static void *sti_bmode_font_raw(struct sti_cooked_font *f)
649 {
650 	unsigned char *n, *p, *q;
651 	int size = f->raw->bytes_per_char*256+sizeof(struct sti_rom_font);
652 
653 	n = kcalloc(4, size, STI_LOWMEM);
654 	if (!n)
655 		return NULL;
656 	p = n + 3;
657 	q = (unsigned char *)f->raw;
658 	while (size--) {
659 		*p = *q++;
660 		p+=4;
661 	}
662 	return n + 3;
663 }
664 
sti_bmode_rom_copy(unsigned long base,unsigned long count,void * dest)665 static void sti_bmode_rom_copy(unsigned long base, unsigned long count,
666 			       void *dest)
667 {
668 	unsigned long dest_start = (unsigned long) dest;
669 
670 	while (count) {
671 		count--;
672 		*(u8 *)dest = gsc_readl(base);
673 		base += 4;
674 		dest++;
675 	}
676 
677 	sti_flush(dest_start, (unsigned long)dest);
678 }
679 
sti_get_bmode_rom(unsigned long address)680 static struct sti_rom *sti_get_bmode_rom (unsigned long address)
681 {
682 	struct sti_rom *raw;
683 	u32 size;
684 	struct sti_rom_font *raw_font, *font_start;
685 
686 	sti_bmode_rom_copy(address + BMODE_LAST_ADDR_OFFS, sizeof(size), &size);
687 
688 	size = (size+3) / 4;
689 	raw = kmalloc(size, STI_LOWMEM);
690 	if (raw) {
691 		sti_bmode_rom_copy(address, size, raw);
692 		memmove (&raw->res004, &raw->type[0], 0x3c);
693 		raw->type[3] = raw->res004;
694 
695 		BMODE_RELOCATE (raw->region_list);
696 		BMODE_RELOCATE (raw->font_start);
697 
698 		BMODE_RELOCATE (raw->init_graph);
699 		BMODE_RELOCATE (raw->state_mgmt);
700 		BMODE_RELOCATE (raw->font_unpmv);
701 		BMODE_RELOCATE (raw->block_move);
702 		BMODE_RELOCATE (raw->inq_conf);
703 
704 		raw_font = ((void *)raw) + raw->font_start;
705 		font_start = raw_font;
706 
707 		while (raw_font->next_font) {
708 			BMODE_RELOCATE (raw_font->next_font);
709 			raw_font = ((void *)font_start) + raw_font->next_font;
710 		}
711 	}
712 	return raw;
713 }
714 
sti_get_wmode_rom(unsigned long address)715 static struct sti_rom *sti_get_wmode_rom(unsigned long address)
716 {
717 	struct sti_rom *raw;
718 	unsigned long size;
719 
720 	/* read the ROM size directly from the struct in ROM */
721 	size = gsc_readl(address + offsetof(struct sti_rom,last_addr));
722 
723 	raw = kmalloc(size, STI_LOWMEM);
724 	if (raw)
725 		sti_rom_copy(address, size, raw);
726 
727 	return raw;
728 }
729 
sti_read_rom(int wordmode,struct sti_struct * sti,unsigned long address)730 static int sti_read_rom(int wordmode, struct sti_struct *sti,
731 			unsigned long address)
732 {
733 	struct sti_cooked_rom *cooked;
734 	struct sti_rom *raw = NULL;
735 	unsigned long revno;
736 
737 	cooked = kmalloc(sizeof *cooked, GFP_KERNEL);
738 	if (!cooked)
739 		goto out_err;
740 
741 	if (wordmode)
742 		raw = sti_get_wmode_rom (address);
743 	else
744 		raw = sti_get_bmode_rom (address);
745 
746 	if (!raw)
747 		goto out_err;
748 
749 	if (!sti_cook_fonts(cooked, raw)) {
750 		printk(KERN_ERR "No font found for STI at %08lx\n", address);
751 		goto out_err;
752 	}
753 
754 	if (raw->region_list)
755 		memcpy(sti->regions, ((void *)raw)+raw->region_list, sizeof(sti->regions));
756 
757 	address = (unsigned long) STI_PTR(raw);
758 
759 	pr_info("STI ROM supports 32 %sbit firmware functions.\n",
760 		raw->alt_code_type == ALT_CODE_TYPE_PA_RISC_64
761 		? "and 64 " : "");
762 
763 	sti->font_unpmv = address + (raw->font_unpmv & 0x03ffffff);
764 	sti->block_move = address + (raw->block_move & 0x03ffffff);
765 	sti->init_graph = address + (raw->init_graph & 0x03ffffff);
766 	sti->inq_conf   = address + (raw->inq_conf   & 0x03ffffff);
767 
768 	sti->rom = cooked;
769 	sti->rom->raw = raw;
770 
771 	sti->font = sti_select_font(sti->rom, sti_search_font);
772 	sti->font_width = sti->font->raw->width;
773 	sti->font_height = sti->font->raw->height;
774 	if (!wordmode)
775 		sti->font->raw = sti_bmode_font_raw(sti->font);
776 
777 	sti->sti_mem_request = raw->sti_mem_req;
778 	sti->graphics_id[0] = raw->graphics_id[0];
779 	sti->graphics_id[1] = raw->graphics_id[1];
780 
781 	sti_dump_rom(raw);
782 
783 	/* check if the ROM routines in this card are compatible */
784 	if (wordmode || sti->graphics_id[1] != 0x09A02587)
785 		goto ok;
786 
787 	revno = (raw->revno[0] << 8) | raw->revno[1];
788 
789 	switch (sti->graphics_id[0]) {
790 	case S9000_ID_HCRX:
791 		/* HyperA or HyperB ? */
792 		if (revno == 0x8408 || revno == 0x840b)
793 			goto msg_not_supported;
794 		break;
795 	case CRT_ID_THUNDER:
796 		if (revno == 0x8509)
797 			goto msg_not_supported;
798 		break;
799 	case CRT_ID_THUNDER2:
800 		if (revno == 0x850c)
801 			goto msg_not_supported;
802 	}
803 ok:
804 	return 1;
805 
806 msg_not_supported:
807 	printk(KERN_ERR "Sorry, this GSC/STI card is not yet supported.\n");
808 	printk(KERN_ERR "Please see http://parisc-linux.org/faq/"
809 			"graphics-howto.html for more info.\n");
810 	/* fall through */
811 out_err:
812 	kfree(raw);
813 	kfree(cooked);
814 	return 0;
815 }
816 
sti_try_rom_generic(unsigned long address,unsigned long hpa,struct pci_dev * pd)817 static struct sti_struct *sti_try_rom_generic(unsigned long address,
818 					      unsigned long hpa,
819 					      struct pci_dev *pd)
820 {
821 	struct sti_struct *sti;
822 	int ok;
823 	u32 sig;
824 
825 	if (num_sti_roms >= MAX_STI_ROMS) {
826 		printk(KERN_WARNING "maximum number of STI ROMS reached !\n");
827 		return NULL;
828 	}
829 
830 	sti = kzalloc(sizeof(*sti), GFP_KERNEL);
831 	if (!sti)
832 		return NULL;
833 
834 	spin_lock_init(&sti->lock);
835 
836 test_rom:
837 	/* if we can't read the ROM, bail out early.  Not being able
838 	 * to read the hpa is okay, for romless sti */
839 	if (pdc_add_valid(address))
840 		goto out_err;
841 
842 	sig = gsc_readl(address);
843 
844 	/* check for a PCI ROM structure */
845 	if ((le32_to_cpu(sig)==0xaa55)) {
846 		unsigned int i, rm_offset;
847 		u32 *rm;
848 		i = gsc_readl(address+0x04);
849 		if (i != 1) {
850 			/* The ROM could have multiple architecture
851 			 * dependent images (e.g. i386, parisc,...) */
852 			printk(KERN_WARNING
853 				"PCI ROM is not a STI ROM type image (0x%8x)\n", i);
854 			goto out_err;
855 		}
856 
857 		sti->pd = pd;
858 
859 		i = gsc_readl(address+0x0c);
860 		DPRINTK(("PCI ROM size (from header) = %d kB\n",
861 			le16_to_cpu(i>>16)*512/1024));
862 		rm_offset = le16_to_cpu(i & 0xffff);
863 		if (rm_offset) {
864 			/* read 16 bytes from the pci region mapper array */
865 			rm = (u32*) &sti->rm_entry;
866 			*rm++ = gsc_readl(address+rm_offset+0x00);
867 			*rm++ = gsc_readl(address+rm_offset+0x04);
868 			*rm++ = gsc_readl(address+rm_offset+0x08);
869 			*rm++ = gsc_readl(address+rm_offset+0x0c);
870 			DPRINTK(("PCI region Mapper offset = %08x: ",
871 				rm_offset));
872 			for (i=0; i<16; i++)
873 				DPRINTK(("%02x ", sti->rm_entry[i]));
874 			DPRINTK(("\n"));
875 		}
876 
877 		address += le32_to_cpu(gsc_readl(address+8));
878 		DPRINTK(("sig %04x, PCI STI ROM at %08lx\n", sig, address));
879 		goto test_rom;
880 	}
881 
882 	ok = 0;
883 
884 	if ((sig & 0xff) == 0x01) {
885 		DPRINTK(("    byte mode ROM at %08lx, hpa at %08lx\n",
886 		       address, hpa));
887 		ok = sti_read_rom(0, sti, address);
888 	}
889 
890 	if ((sig & 0xffff) == 0x0303) {
891 		DPRINTK(("    word mode ROM at %08lx, hpa at %08lx\n",
892 		       address, hpa));
893 		ok = sti_read_rom(1, sti, address);
894 	}
895 
896 	if (!ok)
897 		goto out_err;
898 
899 	if (sti_init_glob_cfg(sti, address, hpa))
900 		goto out_err; /* not enough memory */
901 
902 	/* disable STI PCI ROM. ROM and card RAM overlap and
903 	 * leaving it enabled would force HPMCs
904 	 */
905 	if (sti->pd) {
906 		unsigned long rom_base;
907 		rom_base = pci_resource_start(sti->pd, PCI_ROM_RESOURCE);
908 		pci_write_config_dword(sti->pd, PCI_ROM_ADDRESS, rom_base & ~PCI_ROM_ADDRESS_ENABLE);
909 		DPRINTK((KERN_DEBUG "STI PCI ROM disabled\n"));
910 	}
911 
912 	if (sti_init_graph(sti))
913 		goto out_err;
914 
915 	sti_inq_conf(sti);
916 	sti_dump_globcfg(sti->glob_cfg, sti->sti_mem_request);
917 	sti_dump_outptr(sti);
918 
919 	pr_info("    graphics card name: %s\n",
920 		sti->sti_data->inq_outptr.dev_name);
921 
922 	sti_roms[num_sti_roms] = sti;
923 	num_sti_roms++;
924 
925 	return sti;
926 
927 out_err:
928 	kfree(sti);
929 	return NULL;
930 }
931 
sticore_check_for_default_sti(struct sti_struct * sti,char * path)932 static void sticore_check_for_default_sti(struct sti_struct *sti, char *path)
933 {
934 	if (strcmp (path, default_sti_path) == 0)
935 		default_sti = sti;
936 }
937 
938 /*
939  * on newer systems PDC gives the address of the ROM
940  * in the additional address field addr[1] while on
941  * older Systems the PDC stores it in page0->proc_sti
942  */
sticore_pa_init(struct parisc_device * dev)943 static int __init sticore_pa_init(struct parisc_device *dev)
944 {
945 	char pa_path[21];
946 	struct sti_struct *sti = NULL;
947 	int hpa = dev->hpa.start;
948 
949 	if (dev->num_addrs && dev->addr[0])
950 		sti = sti_try_rom_generic(dev->addr[0], hpa, NULL);
951 	if (!sti)
952 		sti = sti_try_rom_generic(hpa, hpa, NULL);
953 	if (!sti)
954 		sti = sti_try_rom_generic(PAGE0->proc_sti, hpa, NULL);
955 	if (!sti)
956 		return 1;
957 
958 	print_pa_hwpath(dev, pa_path);
959 	sticore_check_for_default_sti(sti, pa_path);
960 	return 0;
961 }
962 
963 
sticore_pci_init(struct pci_dev * pd,const struct pci_device_id * ent)964 static int sticore_pci_init(struct pci_dev *pd, const struct pci_device_id *ent)
965 {
966 #ifdef CONFIG_PCI
967 	unsigned long fb_base, rom_base;
968 	unsigned int fb_len, rom_len;
969 	int err;
970 	struct sti_struct *sti;
971 
972 	err = pci_enable_device(pd);
973 	if (err < 0) {
974 		dev_err(&pd->dev, "Cannot enable PCI device\n");
975 		return err;
976 	}
977 
978 	fb_base = pci_resource_start(pd, 0);
979 	fb_len = pci_resource_len(pd, 0);
980 	rom_base = pci_resource_start(pd, PCI_ROM_RESOURCE);
981 	rom_len = pci_resource_len(pd, PCI_ROM_RESOURCE);
982 	if (rom_base) {
983 		pci_write_config_dword(pd, PCI_ROM_ADDRESS, rom_base | PCI_ROM_ADDRESS_ENABLE);
984 		DPRINTK((KERN_DEBUG "STI PCI ROM enabled at 0x%08lx\n", rom_base));
985 	}
986 
987 	printk(KERN_INFO "STI PCI graphic ROM found at %08lx (%u kB), fb at %08lx (%u MB)\n",
988 		rom_base, rom_len/1024, fb_base, fb_len/1024/1024);
989 
990 	DPRINTK((KERN_DEBUG "Trying PCI STI ROM at %08lx, PCI hpa at %08lx\n",
991 		    rom_base, fb_base));
992 
993 	sti = sti_try_rom_generic(rom_base, fb_base, pd);
994 	if (sti) {
995 		char pa_path[30];
996 		print_pci_hwpath(pd, pa_path);
997 		sticore_check_for_default_sti(sti, pa_path);
998 	}
999 
1000 	if (!sti) {
1001 		printk(KERN_WARNING "Unable to handle STI device '%s'\n",
1002 			pci_name(pd));
1003 		return -ENODEV;
1004 	}
1005 #endif /* CONFIG_PCI */
1006 
1007 	return 0;
1008 }
1009 
1010 
sticore_pci_remove(struct pci_dev * pd)1011 static void __exit sticore_pci_remove(struct pci_dev *pd)
1012 {
1013 	BUG();
1014 }
1015 
1016 
1017 static struct pci_device_id sti_pci_tbl[] = {
1018 	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_EG) },
1019 	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX6) },
1020 	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX4) },
1021 	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FX2) },
1022 	{ PCI_DEVICE(PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_VISUALIZE_FXE) },
1023 	{ 0, } /* terminate list */
1024 };
1025 MODULE_DEVICE_TABLE(pci, sti_pci_tbl);
1026 
1027 static struct pci_driver pci_sti_driver = {
1028 	.name		= "sti",
1029 	.id_table	= sti_pci_tbl,
1030 	.probe		= sticore_pci_init,
1031 	.remove		= __exit_p(sticore_pci_remove),
1032 };
1033 
1034 static struct parisc_device_id sti_pa_tbl[] = {
1035 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00077 },
1036 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00085 },
1037 	{ 0, }
1038 };
1039 MODULE_DEVICE_TABLE(parisc, sti_pa_tbl);
1040 
1041 static struct parisc_driver pa_sti_driver __refdata = {
1042 	.name		= "sti",
1043 	.id_table	= sti_pa_tbl,
1044 	.probe		= sticore_pa_init,
1045 };
1046 
1047 
1048 /*
1049  * sti_init_roms() - detects all STI ROMs and stores them in sti_roms[]
1050  */
1051 
1052 static int sticore_initialized __read_mostly;
1053 
sti_init_roms(void)1054 static void sti_init_roms(void)
1055 {
1056 	if (sticore_initialized)
1057 		return;
1058 
1059 	sticore_initialized = 1;
1060 
1061 	printk(KERN_INFO "STI GSC/PCI core graphics driver "
1062 			STI_DRIVERVERSION "\n");
1063 
1064 	/* Register drivers for native & PCI cards */
1065 	register_parisc_driver(&pa_sti_driver);
1066 	WARN_ON(pci_register_driver(&pci_sti_driver));
1067 
1068 	/* if we didn't find the given default sti, take the first one */
1069 	if (!default_sti)
1070 		default_sti = sti_roms[0];
1071 
1072 }
1073 
1074 /*
1075  * index = 0 gives default sti
1076  * index > 0 gives other stis in detection order
1077  */
sti_get_rom(unsigned int index)1078 struct sti_struct * sti_get_rom(unsigned int index)
1079 {
1080 	if (!sticore_initialized)
1081 		sti_init_roms();
1082 
1083 	if (index == 0)
1084 		return default_sti;
1085 
1086 	if (index > num_sti_roms)
1087 		return NULL;
1088 
1089 	return sti_roms[index-1];
1090 }
1091 EXPORT_SYMBOL(sti_get_rom);
1092 
1093 
sti_call(const struct sti_struct * sti,unsigned long func,const void * flags,void * inptr,void * outptr,struct sti_glob_cfg * glob_cfg)1094 int sti_call(const struct sti_struct *sti, unsigned long func,
1095 		const void *flags, void *inptr, void *outptr,
1096 		struct sti_glob_cfg *glob_cfg)
1097 {
1098 	unsigned long _flags = STI_PTR(flags);
1099 	unsigned long _inptr = STI_PTR(inptr);
1100 	unsigned long _outptr = STI_PTR(outptr);
1101 	unsigned long _glob_cfg = STI_PTR(glob_cfg);
1102 	int ret;
1103 
1104 #ifdef CONFIG_64BIT
1105 	/* Check for overflow when using 32bit STI on 64bit kernel. */
1106 	if (WARN_ONCE(_flags>>32 || _inptr>>32 || _outptr>>32 || _glob_cfg>>32,
1107 			"Out of 32bit-range pointers!"))
1108 		return -1;
1109 #endif
1110 
1111 	ret = pdc_sti_call(func, _flags, _inptr, _outptr, _glob_cfg);
1112 
1113 	return ret;
1114 }
1115 
1116 MODULE_AUTHOR("Philipp Rumpf, Helge Deller, Thomas Bogendoerfer");
1117 MODULE_DESCRIPTION("Core STI driver for HP's NGLE series graphics cards in HP PARISC machines");
1118 MODULE_LICENSE("GPL v2");
1119 
1120