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