Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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