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