sticore.c 30 KB

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