intelfbdrv.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * intelfb
  3. *
  4. * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G/915G/915GM/
  5. * 945G/945GM/945GME/965G/965GM integrated graphics chips.
  6. *
  7. * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
  8. * 2004 Sylvain Meyer
  9. * 2006 David Airlie
  10. *
  11. * This driver consists of two parts. The first part (intelfbdrv.c) provides
  12. * the basic fbdev interfaces, is derived in part from the radeonfb and
  13. * vesafb drivers, and is covered by the GPL. The second part (intelfbhw.c)
  14. * provides the code to program the hardware. Most of it is derived from
  15. * the i810/i830 XFree86 driver. The HW-specific code is covered here
  16. * under a dual license (GPL and MIT/XFree86 license).
  17. *
  18. * Author: David Dawes
  19. *
  20. */
  21. /* $DHD: intelfb/intelfbdrv.c,v 1.20 2003/06/27 15:17:40 dawes Exp $ */
  22. /*
  23. * Changes:
  24. * 01/2003 - Initial driver (0.1.0), no mode switching, no acceleration.
  25. * This initial version is a basic core that works a lot like
  26. * the vesafb driver. It must be built-in to the kernel,
  27. * and the initial video mode must be set with vga=XXX at
  28. * boot time. (David Dawes)
  29. *
  30. * 01/2003 - Version 0.2.0: Mode switching added, colormap support
  31. * implemented, Y panning, and soft screen blanking implemented.
  32. * No acceleration yet. (David Dawes)
  33. *
  34. * 01/2003 - Version 0.3.0: fbcon acceleration support added. Module
  35. * option handling added. (David Dawes)
  36. *
  37. * 01/2003 - Version 0.4.0: fbcon HW cursor support added. (David Dawes)
  38. *
  39. * 01/2003 - Version 0.4.1: Add auto-generation of built-in modes.
  40. * (David Dawes)
  41. *
  42. * 02/2003 - Version 0.4.2: Add check for active non-CRT devices, and
  43. * mode validation checks. (David Dawes)
  44. *
  45. * 02/2003 - Version 0.4.3: Check when the VC is in graphics mode so that
  46. * acceleration is disabled while an XFree86 server is running.
  47. * (David Dawes)
  48. *
  49. * 02/2003 - Version 0.4.4: Monitor DPMS support. (David Dawes)
  50. *
  51. * 02/2003 - Version 0.4.5: Basic XFree86 + fbdev working. (David Dawes)
  52. *
  53. * 02/2003 - Version 0.5.0: Modify to work with the 2.5.32 kernel as well
  54. * as 2.4.x kernels. (David Dawes)
  55. *
  56. * 02/2003 - Version 0.6.0: Split out HW-specifics into a separate file.
  57. * (David Dawes)
  58. *
  59. * 02/2003 - Version 0.7.0: Test on 852GM/855GM. Acceleration and HW
  60. * cursor are disabled on this platform. (David Dawes)
  61. *
  62. * 02/2003 - Version 0.7.1: Test on 845G. Acceleration is disabled
  63. * on this platform. (David Dawes)
  64. *
  65. * 02/2003 - Version 0.7.2: Test on 830M. Acceleration and HW
  66. * cursor are disabled on this platform. (David Dawes)
  67. *
  68. * 02/2003 - Version 0.7.3: Fix 8-bit modes for mobile platforms
  69. * (David Dawes)
  70. *
  71. * 02/2003 - Version 0.7.4: Add checks for FB and FBCON_HAS_CFB* configured
  72. * in the kernel, and add mode bpp verification and default
  73. * bpp selection based on which FBCON_HAS_CFB* are configured.
  74. * (David Dawes)
  75. *
  76. * 02/2003 - Version 0.7.5: Add basic package/install scripts based on the
  77. * DRI packaging scripts. (David Dawes)
  78. *
  79. * 04/2003 - Version 0.7.6: Fix typo that affects builds with SMP-enabled
  80. * kernels. (David Dawes, reported by Anupam).
  81. *
  82. * 06/2003 - Version 0.7.7:
  83. * Fix Makefile.kernel build problem (Tsutomu Yasuda).
  84. * Fix mis-placed #endif (2.4.21 kernel).
  85. *
  86. * 09/2004 - Version 0.9.0 - by Sylvain Meyer
  87. * Port to linux 2.6 kernel fbdev
  88. * Fix HW accel and HW cursor on i845G
  89. * Use of agpgart for fb memory reservation
  90. * Add mtrr support
  91. *
  92. * 10/2004 - Version 0.9.1
  93. * Use module_param instead of old MODULE_PARM
  94. * Some cleanup
  95. *
  96. * 11/2004 - Version 0.9.2
  97. * Add vram option to reserve more memory than stolen by BIOS
  98. * Fix intelfbhw_pan_display typo
  99. * Add __initdata annotations
  100. *
  101. * 04/2008 - Version 0.9.5
  102. * Add support for 965G/965GM. (Maik Broemme <mbroemme@plusserver.de>)
  103. *
  104. * 08/2008 - Version 0.9.6
  105. * Add support for 945GME. (Phil Endecott <spam_from_intelfb@chezphil.org>)
  106. */
  107. #include <linux/module.h>
  108. #include <linux/kernel.h>
  109. #include <linux/errno.h>
  110. #include <linux/string.h>
  111. #include <linux/mm.h>
  112. #include <linux/slab.h>
  113. #include <linux/delay.h>
  114. #include <linux/fb.h>
  115. #include <linux/ioport.h>
  116. #include <linux/init.h>
  117. #include <linux/pci.h>
  118. #include <linux/vmalloc.h>
  119. #include <linux/pagemap.h>
  120. #include <linux/screen_info.h>
  121. #include <asm/io.h>
  122. #ifdef CONFIG_MTRR
  123. #include <asm/mtrr.h>
  124. #endif
  125. #include "intelfb.h"
  126. #include "intelfbhw.h"
  127. #include "../edid.h"
  128. static void __devinit get_initial_mode(struct intelfb_info *dinfo);
  129. static void update_dinfo(struct intelfb_info *dinfo,
  130. struct fb_var_screeninfo *var);
  131. static int intelfb_open(struct fb_info *info, int user);
  132. static int intelfb_release(struct fb_info *info, int user);
  133. static int intelfb_check_var(struct fb_var_screeninfo *var,
  134. struct fb_info *info);
  135. static int intelfb_set_par(struct fb_info *info);
  136. static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  137. unsigned blue, unsigned transp,
  138. struct fb_info *info);
  139. static int intelfb_blank(int blank, struct fb_info *info);
  140. static int intelfb_pan_display(struct fb_var_screeninfo *var,
  141. struct fb_info *info);
  142. static void intelfb_fillrect(struct fb_info *info,
  143. const struct fb_fillrect *rect);
  144. static void intelfb_copyarea(struct fb_info *info,
  145. const struct fb_copyarea *region);
  146. static void intelfb_imageblit(struct fb_info *info,
  147. const struct fb_image *image);
  148. static int intelfb_cursor(struct fb_info *info,
  149. struct fb_cursor *cursor);
  150. static int intelfb_sync(struct fb_info *info);
  151. static int intelfb_ioctl(struct fb_info *info,
  152. unsigned int cmd, unsigned long arg);
  153. static int __devinit intelfb_pci_register(struct pci_dev *pdev,
  154. const struct pci_device_id *ent);
  155. static void __devexit intelfb_pci_unregister(struct pci_dev *pdev);
  156. static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo);
  157. /*
  158. * Limiting the class to PCI_CLASS_DISPLAY_VGA prevents function 1 of the
  159. * mobile chipsets from being registered.
  160. */
  161. #if DETECT_VGA_CLASS_ONLY
  162. #define INTELFB_CLASS_MASK ~0 << 8
  163. #else
  164. #define INTELFB_CLASS_MASK 0
  165. #endif
  166. static struct pci_device_id intelfb_pci_table[] __devinitdata = {
  167. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_830M, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_830M },
  168. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_845G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_845G },
  169. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_85XGM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_85XGM },
  170. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_865G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_865G },
  171. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_854, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_854 },
  172. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_915G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_915G },
  173. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_915GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_915GM },
  174. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945G },
  175. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945GM },
  176. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945GME, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945GME },
  177. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_965G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_965G },
  178. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_965GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_965GM },
  179. { 0, }
  180. };
  181. /* Global data */
  182. static int num_registered = 0;
  183. /* fb ops */
  184. static struct fb_ops intel_fb_ops = {
  185. .owner = THIS_MODULE,
  186. .fb_open = intelfb_open,
  187. .fb_release = intelfb_release,
  188. .fb_check_var = intelfb_check_var,
  189. .fb_set_par = intelfb_set_par,
  190. .fb_setcolreg = intelfb_setcolreg,
  191. .fb_blank = intelfb_blank,
  192. .fb_pan_display = intelfb_pan_display,
  193. .fb_fillrect = intelfb_fillrect,
  194. .fb_copyarea = intelfb_copyarea,
  195. .fb_imageblit = intelfb_imageblit,
  196. .fb_cursor = intelfb_cursor,
  197. .fb_sync = intelfb_sync,
  198. .fb_ioctl = intelfb_ioctl
  199. };
  200. /* PCI driver module table */
  201. static struct pci_driver intelfb_driver = {
  202. .name = "intelfb",
  203. .id_table = intelfb_pci_table,
  204. .probe = intelfb_pci_register,
  205. .remove = __devexit_p(intelfb_pci_unregister)
  206. };
  207. /* Module description/parameters */
  208. MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>, "
  209. "Sylvain Meyer <sylvain.meyer@worldonline.fr>");
  210. MODULE_DESCRIPTION("Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS
  211. " chipsets");
  212. MODULE_LICENSE("Dual BSD/GPL");
  213. MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
  214. static bool accel = 1;
  215. static int vram = 4;
  216. static bool hwcursor = 0;
  217. static bool mtrr = 1;
  218. static bool fixed = 0;
  219. static bool noinit = 0;
  220. static bool noregister = 0;
  221. static bool probeonly = 0;
  222. static bool idonly = 0;
  223. static int bailearly = 0;
  224. static int voffset = 48;
  225. static char *mode = NULL;
  226. module_param(accel, bool, S_IRUGO);
  227. MODULE_PARM_DESC(accel, "Enable hardware acceleration");
  228. module_param(vram, int, S_IRUGO);
  229. MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
  230. module_param(voffset, int, S_IRUGO);
  231. MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
  232. module_param(hwcursor, bool, S_IRUGO);
  233. MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
  234. module_param(mtrr, bool, S_IRUGO);
  235. MODULE_PARM_DESC(mtrr, "Enable MTRR support");
  236. module_param(fixed, bool, S_IRUGO);
  237. MODULE_PARM_DESC(fixed, "Disable mode switching");
  238. module_param(noinit, bool, 0);
  239. MODULE_PARM_DESC(noinit, "Don't initialise graphics mode when loading");
  240. module_param(noregister, bool, 0);
  241. MODULE_PARM_DESC(noregister, "Don't register, just probe and exit (debug)");
  242. module_param(probeonly, bool, 0);
  243. MODULE_PARM_DESC(probeonly, "Do a minimal probe (debug)");
  244. module_param(idonly, bool, 0);
  245. MODULE_PARM_DESC(idonly, "Just identify without doing anything else (debug)");
  246. module_param(bailearly, int, 0);
  247. MODULE_PARM_DESC(bailearly, "Bail out early, depending on value (debug)");
  248. module_param(mode, charp, S_IRUGO);
  249. MODULE_PARM_DESC(mode,
  250. "Initial video mode \"<xres>x<yres>[-<depth>][@<refresh>]\"");
  251. #ifndef MODULE
  252. #define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
  253. #define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name) + 1, NULL, 0)
  254. #define OPT_STRVAL(opt, name) (opt + strlen(name))
  255. static __inline__ char * get_opt_string(const char *this_opt, const char *name)
  256. {
  257. const char *p;
  258. int i;
  259. char *ret;
  260. p = OPT_STRVAL(this_opt, name);
  261. i = 0;
  262. while (p[i] && p[i] != ' ' && p[i] != ',')
  263. i++;
  264. ret = kmalloc(i + 1, GFP_KERNEL);
  265. if (ret) {
  266. strncpy(ret, p, i);
  267. ret[i] = '\0';
  268. }
  269. return ret;
  270. }
  271. static __inline__ int get_opt_int(const char *this_opt, const char *name,
  272. int *ret)
  273. {
  274. if (!ret)
  275. return 0;
  276. if (!OPT_EQUAL(this_opt, name))
  277. return 0;
  278. *ret = OPT_INTVAL(this_opt, name);
  279. return 1;
  280. }
  281. static __inline__ int get_opt_bool(const char *this_opt, const char *name,
  282. int *ret)
  283. {
  284. if (!ret)
  285. return 0;
  286. if (OPT_EQUAL(this_opt, name)) {
  287. if (this_opt[strlen(name)] == '=')
  288. *ret = simple_strtoul(this_opt + strlen(name) + 1,
  289. NULL, 0);
  290. else
  291. *ret = 1;
  292. } else {
  293. if (OPT_EQUAL(this_opt, "no") && OPT_EQUAL(this_opt + 2, name))
  294. *ret = 0;
  295. else
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. static int __init intelfb_setup(char *options)
  301. {
  302. char *this_opt;
  303. DBG_MSG("intelfb_setup\n");
  304. if (!options || !*options) {
  305. DBG_MSG("no options\n");
  306. return 0;
  307. } else
  308. DBG_MSG("options: %s\n", options);
  309. /*
  310. * These are the built-in options analogous to the module parameters
  311. * defined above.
  312. *
  313. * The syntax is:
  314. *
  315. * video=intelfb:[mode][,<param>=<val>] ...
  316. *
  317. * e.g.,
  318. *
  319. * video=intelfb:1024x768-16@75,accel=0
  320. */
  321. while ((this_opt = strsep(&options, ","))) {
  322. if (!*this_opt)
  323. continue;
  324. if (get_opt_bool(this_opt, "accel", &accel))
  325. ;
  326. else if (get_opt_int(this_opt, "vram", &vram))
  327. ;
  328. else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
  329. ;
  330. else if (get_opt_bool(this_opt, "mtrr", &mtrr))
  331. ;
  332. else if (get_opt_bool(this_opt, "fixed", &fixed))
  333. ;
  334. else if (get_opt_bool(this_opt, "init", &noinit))
  335. noinit = !noinit;
  336. else if (OPT_EQUAL(this_opt, "mode="))
  337. mode = get_opt_string(this_opt, "mode=");
  338. else
  339. mode = this_opt;
  340. }
  341. return 0;
  342. }
  343. #endif
  344. static int __init intelfb_init(void)
  345. {
  346. #ifndef MODULE
  347. char *option = NULL;
  348. #endif
  349. DBG_MSG("intelfb_init\n");
  350. INF_MSG("Framebuffer driver for "
  351. "Intel(R) " SUPPORTED_CHIPSETS " chipsets\n");
  352. INF_MSG("Version " INTELFB_VERSION "\n");
  353. if (idonly)
  354. return -ENODEV;
  355. #ifndef MODULE
  356. if (fb_get_options("intelfb", &option))
  357. return -ENODEV;
  358. intelfb_setup(option);
  359. #endif
  360. return pci_register_driver(&intelfb_driver);
  361. }
  362. static void __exit intelfb_exit(void)
  363. {
  364. DBG_MSG("intelfb_exit\n");
  365. pci_unregister_driver(&intelfb_driver);
  366. }
  367. module_init(intelfb_init);
  368. module_exit(intelfb_exit);
  369. /***************************************************************
  370. * mtrr support functions *
  371. ***************************************************************/
  372. #ifdef CONFIG_MTRR
  373. static inline void __devinit set_mtrr(struct intelfb_info *dinfo)
  374. {
  375. dinfo->mtrr_reg = mtrr_add(dinfo->aperture.physical,
  376. dinfo->aperture.size, MTRR_TYPE_WRCOMB, 1);
  377. if (dinfo->mtrr_reg < 0) {
  378. ERR_MSG("unable to set MTRR\n");
  379. return;
  380. }
  381. dinfo->has_mtrr = 1;
  382. }
  383. static inline void unset_mtrr(struct intelfb_info *dinfo)
  384. {
  385. if (dinfo->has_mtrr)
  386. mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
  387. dinfo->aperture.size);
  388. }
  389. #else
  390. #define set_mtrr(x) WRN_MSG("MTRR is disabled in the kernel\n")
  391. #define unset_mtrr(x) do { } while (0)
  392. #endif /* CONFIG_MTRR */
  393. /***************************************************************
  394. * driver init / cleanup *
  395. ***************************************************************/
  396. static void cleanup(struct intelfb_info *dinfo)
  397. {
  398. DBG_MSG("cleanup\n");
  399. if (!dinfo)
  400. return;
  401. intelfbhw_disable_irq(dinfo);
  402. fb_dealloc_cmap(&dinfo->info->cmap);
  403. kfree(dinfo->info->pixmap.addr);
  404. if (dinfo->registered)
  405. unregister_framebuffer(dinfo->info);
  406. unset_mtrr(dinfo);
  407. if (dinfo->fbmem_gart && dinfo->gtt_fb_mem) {
  408. agp_unbind_memory(dinfo->gtt_fb_mem);
  409. agp_free_memory(dinfo->gtt_fb_mem);
  410. }
  411. if (dinfo->gtt_cursor_mem) {
  412. agp_unbind_memory(dinfo->gtt_cursor_mem);
  413. agp_free_memory(dinfo->gtt_cursor_mem);
  414. }
  415. if (dinfo->gtt_ring_mem) {
  416. agp_unbind_memory(dinfo->gtt_ring_mem);
  417. agp_free_memory(dinfo->gtt_ring_mem);
  418. }
  419. #ifdef CONFIG_FB_INTEL_I2C
  420. /* un-register I2C bus */
  421. intelfb_delete_i2c_busses(dinfo);
  422. #endif
  423. if (dinfo->mmio_base)
  424. iounmap((void __iomem *)dinfo->mmio_base);
  425. if (dinfo->aperture.virtual)
  426. iounmap((void __iomem *)dinfo->aperture.virtual);
  427. if (dinfo->flag & INTELFB_MMIO_ACQUIRED)
  428. release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
  429. if (dinfo->flag & INTELFB_FB_ACQUIRED)
  430. release_mem_region(dinfo->aperture.physical,
  431. dinfo->aperture.size);
  432. framebuffer_release(dinfo->info);
  433. }
  434. #define bailout(dinfo) do { \
  435. DBG_MSG("bailout\n"); \
  436. cleanup(dinfo); \
  437. INF_MSG("Not going to register framebuffer, exiting...\n"); \
  438. return -ENODEV; \
  439. } while (0)
  440. static int __devinit intelfb_pci_register(struct pci_dev *pdev,
  441. const struct pci_device_id *ent)
  442. {
  443. struct fb_info *info;
  444. struct intelfb_info *dinfo;
  445. int i, err, dvo;
  446. int aperture_size, stolen_size;
  447. struct agp_kern_info gtt_info;
  448. int agp_memtype;
  449. const char *s;
  450. struct agp_bridge_data *bridge;
  451. int aperture_bar = 0;
  452. int mmio_bar = 1;
  453. int offset;
  454. DBG_MSG("intelfb_pci_register\n");
  455. num_registered++;
  456. if (num_registered != 1) {
  457. ERR_MSG("Attempted to register %d devices "
  458. "(should be only 1).\n", num_registered);
  459. return -ENODEV;
  460. }
  461. info = framebuffer_alloc(sizeof(struct intelfb_info), &pdev->dev);
  462. if (!info) {
  463. ERR_MSG("Could not allocate memory for intelfb_info.\n");
  464. return -ENODEV;
  465. }
  466. if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
  467. ERR_MSG("Could not allocate cmap for intelfb_info.\n");
  468. goto err_out_cmap;
  469. }
  470. dinfo = info->par;
  471. dinfo->info = info;
  472. dinfo->fbops = &intel_fb_ops;
  473. dinfo->pdev = pdev;
  474. /* Reserve pixmap space. */
  475. info->pixmap.addr = kzalloc(64 * 1024, GFP_KERNEL);
  476. if (info->pixmap.addr == NULL) {
  477. ERR_MSG("Cannot reserve pixmap memory.\n");
  478. goto err_out_pixmap;
  479. }
  480. /* set early this option because it could be changed by tv encoder
  481. driver */
  482. dinfo->fixed_mode = fixed;
  483. /* Enable device. */
  484. if ((err = pci_enable_device(pdev))) {
  485. ERR_MSG("Cannot enable device.\n");
  486. cleanup(dinfo);
  487. return -ENODEV;
  488. }
  489. /* Set base addresses. */
  490. if ((ent->device == PCI_DEVICE_ID_INTEL_915G) ||
  491. (ent->device == PCI_DEVICE_ID_INTEL_915GM) ||
  492. (ent->device == PCI_DEVICE_ID_INTEL_945G) ||
  493. (ent->device == PCI_DEVICE_ID_INTEL_945GM) ||
  494. (ent->device == PCI_DEVICE_ID_INTEL_945GME) ||
  495. (ent->device == PCI_DEVICE_ID_INTEL_965G) ||
  496. (ent->device == PCI_DEVICE_ID_INTEL_965GM)) {
  497. aperture_bar = 2;
  498. mmio_bar = 0;
  499. }
  500. dinfo->aperture.physical = pci_resource_start(pdev, aperture_bar);
  501. dinfo->aperture.size = pci_resource_len(pdev, aperture_bar);
  502. dinfo->mmio_base_phys = pci_resource_start(pdev, mmio_bar);
  503. DBG_MSG("fb aperture: 0x%llx/0x%llx, MMIO region: 0x%llx/0x%llx\n",
  504. (unsigned long long)pci_resource_start(pdev, aperture_bar),
  505. (unsigned long long)pci_resource_len(pdev, aperture_bar),
  506. (unsigned long long)pci_resource_start(pdev, mmio_bar),
  507. (unsigned long long)pci_resource_len(pdev, mmio_bar));
  508. /* Reserve the fb and MMIO regions */
  509. if (!request_mem_region(dinfo->aperture.physical, dinfo->aperture.size,
  510. INTELFB_MODULE_NAME)) {
  511. ERR_MSG("Cannot reserve FB region.\n");
  512. cleanup(dinfo);
  513. return -ENODEV;
  514. }
  515. dinfo->flag |= INTELFB_FB_ACQUIRED;
  516. if (!request_mem_region(dinfo->mmio_base_phys,
  517. INTEL_REG_SIZE,
  518. INTELFB_MODULE_NAME)) {
  519. ERR_MSG("Cannot reserve MMIO region.\n");
  520. cleanup(dinfo);
  521. return -ENODEV;
  522. }
  523. dinfo->flag |= INTELFB_MMIO_ACQUIRED;
  524. /* Get the chipset info. */
  525. dinfo->pci_chipset = pdev->device;
  526. if (intelfbhw_get_chipset(pdev, dinfo)) {
  527. cleanup(dinfo);
  528. return -ENODEV;
  529. }
  530. if (intelfbhw_get_memory(pdev, &aperture_size,&stolen_size)) {
  531. cleanup(dinfo);
  532. return -ENODEV;
  533. }
  534. INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
  535. "stolen memory %dkB\n",
  536. pdev->bus->number, PCI_SLOT(pdev->devfn),
  537. PCI_FUNC(pdev->devfn), dinfo->name,
  538. BtoMB(aperture_size), BtoKB(stolen_size));
  539. /* Set these from the options. */
  540. dinfo->accel = accel;
  541. dinfo->hwcursor = hwcursor;
  542. if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
  543. INF_MSG("Acceleration is not supported for the %s chipset.\n",
  544. dinfo->name);
  545. dinfo->accel = 0;
  546. }
  547. /* Framebuffer parameters - Use all the stolen memory if >= vram */
  548. if (ROUND_UP_TO_PAGE(stolen_size) >= MB(vram)) {
  549. dinfo->fb.size = ROUND_UP_TO_PAGE(stolen_size);
  550. dinfo->fbmem_gart = 0;
  551. } else {
  552. dinfo->fb.size = MB(vram);
  553. dinfo->fbmem_gart = 1;
  554. }
  555. /* Allocate space for the ring buffer and HW cursor if enabled. */
  556. if (dinfo->accel) {
  557. dinfo->ring.size = RINGBUFFER_SIZE;
  558. dinfo->ring_tail_mask = dinfo->ring.size - 1;
  559. }
  560. if (dinfo->hwcursor)
  561. dinfo->cursor.size = HW_CURSOR_SIZE;
  562. /* Use agpgart to manage the GATT */
  563. if (!(bridge = agp_backend_acquire(pdev))) {
  564. ERR_MSG("cannot acquire agp\n");
  565. cleanup(dinfo);
  566. return -ENODEV;
  567. }
  568. /* get the current gatt info */
  569. if (agp_copy_info(bridge, &gtt_info)) {
  570. ERR_MSG("cannot get agp info\n");
  571. agp_backend_release(bridge);
  572. cleanup(dinfo);
  573. return -ENODEV;
  574. }
  575. if (MB(voffset) < stolen_size)
  576. offset = (stolen_size >> 12);
  577. else
  578. offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
  579. /* set the mem offsets - set them after the already used pages */
  580. if (dinfo->accel)
  581. dinfo->ring.offset = offset + gtt_info.current_memory;
  582. if (dinfo->hwcursor)
  583. dinfo->cursor.offset = offset +
  584. + gtt_info.current_memory + (dinfo->ring.size >> 12);
  585. if (dinfo->fbmem_gart)
  586. dinfo->fb.offset = offset +
  587. + gtt_info.current_memory + (dinfo->ring.size >> 12)
  588. + (dinfo->cursor.size >> 12);
  589. /* Allocate memories (which aren't stolen) */
  590. /* Map the fb and MMIO regions */
  591. /* ioremap only up to the end of used aperture */
  592. dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
  593. (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
  594. + dinfo->fb.size);
  595. if (!dinfo->aperture.virtual) {
  596. ERR_MSG("Cannot remap FB region.\n");
  597. cleanup(dinfo);
  598. return -ENODEV;
  599. }
  600. dinfo->mmio_base =
  601. (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
  602. INTEL_REG_SIZE);
  603. if (!dinfo->mmio_base) {
  604. ERR_MSG("Cannot remap MMIO region.\n");
  605. cleanup(dinfo);
  606. return -ENODEV;
  607. }
  608. if (dinfo->accel) {
  609. if (!(dinfo->gtt_ring_mem =
  610. agp_allocate_memory(bridge, dinfo->ring.size >> 12,
  611. AGP_NORMAL_MEMORY))) {
  612. ERR_MSG("cannot allocate ring buffer memory\n");
  613. agp_backend_release(bridge);
  614. cleanup(dinfo);
  615. return -ENOMEM;
  616. }
  617. if (agp_bind_memory(dinfo->gtt_ring_mem,
  618. dinfo->ring.offset)) {
  619. ERR_MSG("cannot bind ring buffer memory\n");
  620. agp_backend_release(bridge);
  621. cleanup(dinfo);
  622. return -EBUSY;
  623. }
  624. dinfo->ring.physical = dinfo->aperture.physical
  625. + (dinfo->ring.offset << 12);
  626. dinfo->ring.virtual = dinfo->aperture.virtual
  627. + (dinfo->ring.offset << 12);
  628. dinfo->ring_head = 0;
  629. }
  630. if (dinfo->hwcursor) {
  631. agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
  632. : AGP_NORMAL_MEMORY;
  633. if (!(dinfo->gtt_cursor_mem =
  634. agp_allocate_memory(bridge, dinfo->cursor.size >> 12,
  635. agp_memtype))) {
  636. ERR_MSG("cannot allocate cursor memory\n");
  637. agp_backend_release(bridge);
  638. cleanup(dinfo);
  639. return -ENOMEM;
  640. }
  641. if (agp_bind_memory(dinfo->gtt_cursor_mem,
  642. dinfo->cursor.offset)) {
  643. ERR_MSG("cannot bind cursor memory\n");
  644. agp_backend_release(bridge);
  645. cleanup(dinfo);
  646. return -EBUSY;
  647. }
  648. if (dinfo->mobile)
  649. dinfo->cursor.physical
  650. = dinfo->gtt_cursor_mem->physical;
  651. else
  652. dinfo->cursor.physical = dinfo->aperture.physical
  653. + (dinfo->cursor.offset << 12);
  654. dinfo->cursor.virtual = dinfo->aperture.virtual
  655. + (dinfo->cursor.offset << 12);
  656. }
  657. if (dinfo->fbmem_gart) {
  658. if (!(dinfo->gtt_fb_mem =
  659. agp_allocate_memory(bridge, dinfo->fb.size >> 12,
  660. AGP_NORMAL_MEMORY))) {
  661. WRN_MSG("cannot allocate framebuffer memory - use "
  662. "the stolen one\n");
  663. dinfo->fbmem_gart = 0;
  664. }
  665. if (agp_bind_memory(dinfo->gtt_fb_mem,
  666. dinfo->fb.offset)) {
  667. WRN_MSG("cannot bind framebuffer memory - use "
  668. "the stolen one\n");
  669. dinfo->fbmem_gart = 0;
  670. }
  671. }
  672. /* update framebuffer memory parameters */
  673. if (!dinfo->fbmem_gart)
  674. dinfo->fb.offset = 0; /* starts at offset 0 */
  675. dinfo->fb.physical = dinfo->aperture.physical
  676. + (dinfo->fb.offset << 12);
  677. dinfo->fb.virtual = dinfo->aperture.virtual + (dinfo->fb.offset << 12);
  678. dinfo->fb_start = dinfo->fb.offset << 12;
  679. /* release agpgart */
  680. agp_backend_release(bridge);
  681. if (mtrr)
  682. set_mtrr(dinfo);
  683. DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%p)\n",
  684. dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
  685. dinfo->fb.virtual);
  686. DBG_MSG("MMIO: 0x%x/0x%x (0x%p)\n",
  687. dinfo->mmio_base_phys, INTEL_REG_SIZE,
  688. dinfo->mmio_base);
  689. DBG_MSG("ring buffer: 0x%x/0x%x (0x%p)\n",
  690. dinfo->ring.physical, dinfo->ring.size,
  691. dinfo->ring.virtual);
  692. DBG_MSG("HW cursor: 0x%x/0x%x (0x%p) (offset 0x%x) (phys 0x%x)\n",
  693. dinfo->cursor.physical, dinfo->cursor.size,
  694. dinfo->cursor.virtual, dinfo->cursor.offset,
  695. dinfo->cursor.physical);
  696. DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
  697. "noinit = %d\n", vram, accel, hwcursor, fixed, noinit);
  698. DBG_MSG("options: mode = \"%s\"\n", mode ? mode : "");
  699. if (probeonly)
  700. bailout(dinfo);
  701. /*
  702. * Check if the LVDS port or any DVO ports are enabled. If so,
  703. * don't allow mode switching
  704. */
  705. dvo = intelfbhw_check_non_crt(dinfo);
  706. if (dvo) {
  707. dinfo->fixed_mode = 1;
  708. WRN_MSG("Non-CRT device is enabled ( ");
  709. i = 0;
  710. while (dvo) {
  711. if (dvo & 1) {
  712. s = intelfbhw_dvo_to_string(1 << i);
  713. if (s)
  714. printk("%s ", s);
  715. }
  716. dvo >>= 1;
  717. ++i;
  718. }
  719. printk("). Disabling mode switching.\n");
  720. }
  721. if (bailearly == 1)
  722. bailout(dinfo);
  723. if (FIXED_MODE(dinfo) &&
  724. screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB) {
  725. ERR_MSG("Video mode must be programmed at boot time.\n");
  726. cleanup(dinfo);
  727. return -ENODEV;
  728. }
  729. if (bailearly == 2)
  730. bailout(dinfo);
  731. /* Initialise dinfo and related data. */
  732. /* If an initial mode was programmed at boot time, get its details. */
  733. if (screen_info.orig_video_isVGA == VIDEO_TYPE_VLFB)
  734. get_initial_mode(dinfo);
  735. if (bailearly == 3)
  736. bailout(dinfo);
  737. if (FIXED_MODE(dinfo)) /* remap fb address */
  738. update_dinfo(dinfo, &dinfo->initial_var);
  739. if (bailearly == 4)
  740. bailout(dinfo);
  741. if (intelfb_set_fbinfo(dinfo)) {
  742. cleanup(dinfo);
  743. return -ENODEV;
  744. }
  745. if (bailearly == 5)
  746. bailout(dinfo);
  747. #ifdef CONFIG_FB_INTEL_I2C
  748. /* register I2C bus */
  749. intelfb_create_i2c_busses(dinfo);
  750. #endif
  751. if (bailearly == 6)
  752. bailout(dinfo);
  753. pci_set_drvdata(pdev, dinfo);
  754. /* Save the initial register state. */
  755. i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
  756. bailearly > 6 ? bailearly - 6 : 0);
  757. if (i != 0) {
  758. DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
  759. bailout(dinfo);
  760. }
  761. intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
  762. if (bailearly == 18)
  763. bailout(dinfo);
  764. /* read active pipe */
  765. dinfo->pipe = intelfbhw_active_pipe(&dinfo->save_state);
  766. /* Cursor initialisation */
  767. if (dinfo->hwcursor) {
  768. intelfbhw_cursor_init(dinfo);
  769. intelfbhw_cursor_reset(dinfo);
  770. }
  771. if (bailearly == 19)
  772. bailout(dinfo);
  773. /* 2d acceleration init */
  774. if (dinfo->accel)
  775. intelfbhw_2d_start(dinfo);
  776. if (bailearly == 20)
  777. bailout(dinfo);
  778. if (noregister)
  779. bailout(dinfo);
  780. if (register_framebuffer(dinfo->info) < 0) {
  781. ERR_MSG("Cannot register framebuffer.\n");
  782. cleanup(dinfo);
  783. return -ENODEV;
  784. }
  785. dinfo->registered = 1;
  786. dinfo->open = 0;
  787. init_waitqueue_head(&dinfo->vsync.wait);
  788. spin_lock_init(&dinfo->int_lock);
  789. dinfo->irq_flags = 0;
  790. dinfo->vsync.pan_display = 0;
  791. dinfo->vsync.pan_offset = 0;
  792. return 0;
  793. err_out_pixmap:
  794. fb_dealloc_cmap(&info->cmap);
  795. err_out_cmap:
  796. framebuffer_release(info);
  797. return -ENODEV;
  798. }
  799. static void __devexit
  800. intelfb_pci_unregister(struct pci_dev *pdev)
  801. {
  802. struct intelfb_info *dinfo = pci_get_drvdata(pdev);
  803. DBG_MSG("intelfb_pci_unregister\n");
  804. if (!dinfo)
  805. return;
  806. cleanup(dinfo);
  807. pci_set_drvdata(pdev, NULL);
  808. }
  809. /***************************************************************
  810. * helper functions *
  811. ***************************************************************/
  812. int __inline__ intelfb_var_to_depth(const struct fb_var_screeninfo *var)
  813. {
  814. DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
  815. var->bits_per_pixel, var->green.length);
  816. switch (var->bits_per_pixel) {
  817. case 16:
  818. return (var->green.length == 6) ? 16 : 15;
  819. case 32:
  820. return 24;
  821. default:
  822. return var->bits_per_pixel;
  823. }
  824. }
  825. static __inline__ int var_to_refresh(const struct fb_var_screeninfo *var)
  826. {
  827. int xtot = var->xres + var->left_margin + var->right_margin +
  828. var->hsync_len;
  829. int ytot = var->yres + var->upper_margin + var->lower_margin +
  830. var->vsync_len;
  831. return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
  832. }
  833. /***************************************************************
  834. * Various intialisation functions *
  835. ***************************************************************/
  836. static void __devinit get_initial_mode(struct intelfb_info *dinfo)
  837. {
  838. struct fb_var_screeninfo *var;
  839. int xtot, ytot;
  840. DBG_MSG("get_initial_mode\n");
  841. dinfo->initial_vga = 1;
  842. dinfo->initial_fb_base = screen_info.lfb_base;
  843. dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
  844. dinfo->initial_pitch = screen_info.lfb_linelength;
  845. var = &dinfo->initial_var;
  846. memset(var, 0, sizeof(*var));
  847. var->xres = screen_info.lfb_width;
  848. var->yres = screen_info.lfb_height;
  849. var->bits_per_pixel = screen_info.lfb_depth;
  850. switch (screen_info.lfb_depth) {
  851. case 15:
  852. var->bits_per_pixel = 16;
  853. break;
  854. case 24:
  855. var->bits_per_pixel = 32;
  856. break;
  857. }
  858. DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
  859. dinfo->initial_fb_base, dinfo->initial_video_ram,
  860. BtoKB(dinfo->initial_video_ram));
  861. DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
  862. var->xres, var->yres, var->bits_per_pixel,
  863. dinfo->initial_pitch);
  864. /* Dummy timing values (assume 60Hz) */
  865. var->left_margin = (var->xres / 8) & 0xf8;
  866. var->right_margin = 32;
  867. var->upper_margin = 16;
  868. var->lower_margin = 4;
  869. var->hsync_len = (var->xres / 8) & 0xf8;
  870. var->vsync_len = 4;
  871. xtot = var->xres + var->left_margin +
  872. var->right_margin + var->hsync_len;
  873. ytot = var->yres + var->upper_margin +
  874. var->lower_margin + var->vsync_len;
  875. var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
  876. var->height = -1;
  877. var->width = -1;
  878. if (var->bits_per_pixel > 8) {
  879. var->red.offset = screen_info.red_pos;
  880. var->red.length = screen_info.red_size;
  881. var->green.offset = screen_info.green_pos;
  882. var->green.length = screen_info.green_size;
  883. var->blue.offset = screen_info.blue_pos;
  884. var->blue.length = screen_info.blue_size;
  885. var->transp.offset = screen_info.rsvd_pos;
  886. var->transp.length = screen_info.rsvd_size;
  887. } else {
  888. var->red.length = 8;
  889. var->green.length = 8;
  890. var->blue.length = 8;
  891. }
  892. }
  893. static int __devinit intelfb_init_var(struct intelfb_info *dinfo)
  894. {
  895. struct fb_var_screeninfo *var;
  896. int msrc = 0;
  897. DBG_MSG("intelfb_init_var\n");
  898. var = &dinfo->info->var;
  899. if (FIXED_MODE(dinfo)) {
  900. memcpy(var, &dinfo->initial_var,
  901. sizeof(struct fb_var_screeninfo));
  902. msrc = 5;
  903. } else {
  904. const u8 *edid_s = fb_firmware_edid(&dinfo->pdev->dev);
  905. u8 *edid_d = NULL;
  906. if (edid_s) {
  907. edid_d = kmemdup(edid_s, EDID_LENGTH, GFP_KERNEL);
  908. if (edid_d) {
  909. fb_edid_to_monspecs(edid_d,
  910. &dinfo->info->monspecs);
  911. kfree(edid_d);
  912. }
  913. }
  914. if (mode) {
  915. printk("intelfb: Looking for mode in private "
  916. "database\n");
  917. msrc = fb_find_mode(var, dinfo->info, mode,
  918. dinfo->info->monspecs.modedb,
  919. dinfo->info->monspecs.modedb_len,
  920. NULL, 0);
  921. if (msrc && msrc > 1) {
  922. printk("intelfb: No mode in private database, "
  923. "intelfb: looking for mode in global "
  924. "database ");
  925. msrc = fb_find_mode(var, dinfo->info, mode,
  926. NULL, 0, NULL, 0);
  927. if (msrc)
  928. msrc |= 8;
  929. }
  930. }
  931. if (!msrc)
  932. msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
  933. NULL, 0, NULL, 0);
  934. }
  935. if (!msrc) {
  936. ERR_MSG("Cannot find a suitable video mode.\n");
  937. return 1;
  938. }
  939. INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
  940. var->bits_per_pixel, var_to_refresh(var));
  941. DBG_MSG("Initial video mode is from %d.\n", msrc);
  942. #if ALLOCATE_FOR_PANNING
  943. /* Allow use of half of the video ram for panning */
  944. var->xres_virtual = var->xres;
  945. var->yres_virtual =
  946. dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
  947. if (var->yres_virtual < var->yres)
  948. var->yres_virtual = var->yres;
  949. #else
  950. var->yres_virtual = var->yres;
  951. #endif
  952. if (dinfo->accel)
  953. var->accel_flags |= FB_ACCELF_TEXT;
  954. else
  955. var->accel_flags &= ~FB_ACCELF_TEXT;
  956. return 0;
  957. }
  958. static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo)
  959. {
  960. struct fb_info *info = dinfo->info;
  961. DBG_MSG("intelfb_set_fbinfo\n");
  962. info->flags = FBINFO_FLAG_DEFAULT;
  963. info->fbops = &intel_fb_ops;
  964. info->pseudo_palette = dinfo->pseudo_palette;
  965. info->pixmap.size = 64*1024;
  966. info->pixmap.buf_align = 8;
  967. info->pixmap.access_align = 32;
  968. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  969. if (intelfb_init_var(dinfo))
  970. return 1;
  971. info->pixmap.scan_align = 1;
  972. strcpy(info->fix.id, dinfo->name);
  973. info->fix.smem_start = dinfo->fb.physical;
  974. info->fix.smem_len = dinfo->fb.size;
  975. info->fix.type = FB_TYPE_PACKED_PIXELS;
  976. info->fix.type_aux = 0;
  977. info->fix.xpanstep = 8;
  978. info->fix.ypanstep = 1;
  979. info->fix.ywrapstep = 0;
  980. info->fix.mmio_start = dinfo->mmio_base_phys;
  981. info->fix.mmio_len = INTEL_REG_SIZE;
  982. info->fix.accel = FB_ACCEL_I830;
  983. update_dinfo(dinfo, &info->var);
  984. return 0;
  985. }
  986. /* Update dinfo to match the active video mode. */
  987. static void update_dinfo(struct intelfb_info *dinfo,
  988. struct fb_var_screeninfo *var)
  989. {
  990. DBG_MSG("update_dinfo\n");
  991. dinfo->bpp = var->bits_per_pixel;
  992. dinfo->depth = intelfb_var_to_depth(var);
  993. dinfo->xres = var->xres;
  994. dinfo->yres = var->xres;
  995. dinfo->pixclock = var->pixclock;
  996. dinfo->info->fix.visual = dinfo->visual;
  997. dinfo->info->fix.line_length = dinfo->pitch;
  998. switch (dinfo->bpp) {
  999. case 8:
  1000. dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
  1001. dinfo->pitch = var->xres_virtual;
  1002. break;
  1003. case 16:
  1004. dinfo->visual = FB_VISUAL_TRUECOLOR;
  1005. dinfo->pitch = var->xres_virtual * 2;
  1006. break;
  1007. case 32:
  1008. dinfo->visual = FB_VISUAL_TRUECOLOR;
  1009. dinfo->pitch = var->xres_virtual * 4;
  1010. break;
  1011. }
  1012. /* Make sure the line length is a aligned correctly. */
  1013. if (IS_I9XX(dinfo))
  1014. dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT_I9XX);
  1015. else
  1016. dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
  1017. if (FIXED_MODE(dinfo))
  1018. dinfo->pitch = dinfo->initial_pitch;
  1019. dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
  1020. dinfo->info->fix.line_length = dinfo->pitch;
  1021. dinfo->info->fix.visual = dinfo->visual;
  1022. }
  1023. /* fbops functions */
  1024. /***************************************************************
  1025. * fbdev interface *
  1026. ***************************************************************/
  1027. static int intelfb_open(struct fb_info *info, int user)
  1028. {
  1029. struct intelfb_info *dinfo = GET_DINFO(info);
  1030. if (user)
  1031. dinfo->open++;
  1032. return 0;
  1033. }
  1034. static int intelfb_release(struct fb_info *info, int user)
  1035. {
  1036. struct intelfb_info *dinfo = GET_DINFO(info);
  1037. if (user) {
  1038. dinfo->open--;
  1039. msleep(1);
  1040. if (!dinfo->open)
  1041. intelfbhw_disable_irq(dinfo);
  1042. }
  1043. return 0;
  1044. }
  1045. static int intelfb_check_var(struct fb_var_screeninfo *var,
  1046. struct fb_info *info)
  1047. {
  1048. int change_var = 0;
  1049. struct fb_var_screeninfo v;
  1050. struct intelfb_info *dinfo;
  1051. static int first = 1;
  1052. int i;
  1053. /* Good pitches to allow tiling. Don't care about pitches < 1024. */
  1054. static const int pitches[] = {
  1055. 128 * 8,
  1056. 128 * 16,
  1057. 128 * 32,
  1058. 128 * 64,
  1059. 0
  1060. };
  1061. DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
  1062. dinfo = GET_DINFO(info);
  1063. /* update the pitch */
  1064. if (intelfbhw_validate_mode(dinfo, var) != 0)
  1065. return -EINVAL;
  1066. v = *var;
  1067. for (i = 0; pitches[i] != 0; i++) {
  1068. if (pitches[i] >= v.xres_virtual) {
  1069. v.xres_virtual = pitches[i];
  1070. break;
  1071. }
  1072. }
  1073. /* Check for a supported bpp. */
  1074. if (v.bits_per_pixel <= 8)
  1075. v.bits_per_pixel = 8;
  1076. else if (v.bits_per_pixel <= 16) {
  1077. if (v.bits_per_pixel == 16)
  1078. v.green.length = 6;
  1079. v.bits_per_pixel = 16;
  1080. } else if (v.bits_per_pixel <= 32)
  1081. v.bits_per_pixel = 32;
  1082. else
  1083. return -EINVAL;
  1084. change_var = ((info->var.xres != var->xres) ||
  1085. (info->var.yres != var->yres) ||
  1086. (info->var.xres_virtual != var->xres_virtual) ||
  1087. (info->var.yres_virtual != var->yres_virtual) ||
  1088. (info->var.bits_per_pixel != var->bits_per_pixel) ||
  1089. memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
  1090. memcmp(&info->var.green, &var->green,
  1091. sizeof(var->green)) ||
  1092. memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
  1093. if (FIXED_MODE(dinfo) &&
  1094. (change_var ||
  1095. var->yres_virtual > dinfo->initial_var.yres_virtual ||
  1096. var->yres_virtual < dinfo->initial_var.yres ||
  1097. var->xoffset || var->nonstd)) {
  1098. if (first) {
  1099. ERR_MSG("Changing the video mode is not supported.\n");
  1100. first = 0;
  1101. }
  1102. return -EINVAL;
  1103. }
  1104. switch (intelfb_var_to_depth(&v)) {
  1105. case 8:
  1106. v.red.offset = v.green.offset = v.blue.offset = 0;
  1107. v.red.length = v.green.length = v.blue.length = 8;
  1108. v.transp.offset = v.transp.length = 0;
  1109. break;
  1110. case 15:
  1111. v.red.offset = 10;
  1112. v.green.offset = 5;
  1113. v.blue.offset = 0;
  1114. v.red.length = v.green.length = v.blue.length = 5;
  1115. v.transp.offset = v.transp.length = 0;
  1116. break;
  1117. case 16:
  1118. v.red.offset = 11;
  1119. v.green.offset = 5;
  1120. v.blue.offset = 0;
  1121. v.red.length = 5;
  1122. v.green.length = 6;
  1123. v.blue.length = 5;
  1124. v.transp.offset = v.transp.length = 0;
  1125. break;
  1126. case 24:
  1127. v.red.offset = 16;
  1128. v.green.offset = 8;
  1129. v.blue.offset = 0;
  1130. v.red.length = v.green.length = v.blue.length = 8;
  1131. v.transp.offset = v.transp.length = 0;
  1132. break;
  1133. case 32:
  1134. v.red.offset = 16;
  1135. v.green.offset = 8;
  1136. v.blue.offset = 0;
  1137. v.red.length = v.green.length = v.blue.length = 8;
  1138. v.transp.offset = 24;
  1139. v.transp.length = 8;
  1140. break;
  1141. }
  1142. if (v.xoffset < 0)
  1143. v.xoffset = 0;
  1144. if (v.yoffset < 0)
  1145. v.yoffset = 0;
  1146. if (v.xoffset > v.xres_virtual - v.xres)
  1147. v.xoffset = v.xres_virtual - v.xres;
  1148. if (v.yoffset > v.yres_virtual - v.yres)
  1149. v.yoffset = v.yres_virtual - v.yres;
  1150. v.red.msb_right = v.green.msb_right = v.blue.msb_right =
  1151. v.transp.msb_right = 0;
  1152. *var = v;
  1153. return 0;
  1154. }
  1155. static int intelfb_set_par(struct fb_info *info)
  1156. {
  1157. struct intelfb_hwstate *hw;
  1158. struct intelfb_info *dinfo = GET_DINFO(info);
  1159. if (FIXED_MODE(dinfo)) {
  1160. ERR_MSG("Changing the video mode is not supported.\n");
  1161. return -EINVAL;
  1162. }
  1163. hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
  1164. if (!hw)
  1165. return -ENOMEM;
  1166. DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
  1167. info->var.yres, info->var.bits_per_pixel);
  1168. /*
  1169. * Disable VCO prior to timing register change.
  1170. */
  1171. OUTREG(DPLL_A, INREG(DPLL_A) & ~DPLL_VCO_ENABLE);
  1172. intelfb_blank(FB_BLANK_POWERDOWN, info);
  1173. if (ACCEL(dinfo, info))
  1174. intelfbhw_2d_stop(dinfo);
  1175. memcpy(hw, &dinfo->save_state, sizeof(*hw));
  1176. if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
  1177. goto invalid_mode;
  1178. if (intelfbhw_program_mode(dinfo, hw, 0))
  1179. goto invalid_mode;
  1180. #if REGDUMP > 0
  1181. intelfbhw_read_hw_state(dinfo, hw, 0);
  1182. intelfbhw_print_hw_state(dinfo, hw);
  1183. #endif
  1184. update_dinfo(dinfo, &info->var);
  1185. if (ACCEL(dinfo, info))
  1186. intelfbhw_2d_start(dinfo);
  1187. intelfb_pan_display(&info->var, info);
  1188. intelfb_blank(FB_BLANK_UNBLANK, info);
  1189. if (ACCEL(dinfo, info)) {
  1190. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
  1191. FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
  1192. FBINFO_HWACCEL_IMAGEBLIT;
  1193. } else
  1194. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  1195. kfree(hw);
  1196. return 0;
  1197. invalid_mode:
  1198. kfree(hw);
  1199. return -EINVAL;
  1200. }
  1201. static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  1202. unsigned blue, unsigned transp,
  1203. struct fb_info *info)
  1204. {
  1205. struct intelfb_info *dinfo = GET_DINFO(info);
  1206. #if VERBOSE > 0
  1207. DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
  1208. #endif
  1209. if (regno > 255)
  1210. return 1;
  1211. if (dinfo->depth == 8) {
  1212. red >>= 8;
  1213. green >>= 8;
  1214. blue >>= 8;
  1215. intelfbhw_setcolreg(dinfo, regno, red, green, blue,
  1216. transp);
  1217. }
  1218. if (regno < 16) {
  1219. switch (dinfo->depth) {
  1220. case 15:
  1221. dinfo->pseudo_palette[regno] = ((red & 0xf800) >> 1) |
  1222. ((green & 0xf800) >> 6) |
  1223. ((blue & 0xf800) >> 11);
  1224. break;
  1225. case 16:
  1226. dinfo->pseudo_palette[regno] = (red & 0xf800) |
  1227. ((green & 0xfc00) >> 5) |
  1228. ((blue & 0xf800) >> 11);
  1229. break;
  1230. case 24:
  1231. dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
  1232. (green & 0xff00) |
  1233. ((blue & 0xff00) >> 8);
  1234. break;
  1235. }
  1236. }
  1237. return 0;
  1238. }
  1239. static int intelfb_blank(int blank, struct fb_info *info)
  1240. {
  1241. intelfbhw_do_blank(blank, info);
  1242. return 0;
  1243. }
  1244. static int intelfb_pan_display(struct fb_var_screeninfo *var,
  1245. struct fb_info *info)
  1246. {
  1247. intelfbhw_pan_display(var, info);
  1248. return 0;
  1249. }
  1250. /* When/if we have our own ioctls. */
  1251. static int intelfb_ioctl(struct fb_info *info, unsigned int cmd,
  1252. unsigned long arg)
  1253. {
  1254. int retval = 0;
  1255. struct intelfb_info *dinfo = GET_DINFO(info);
  1256. u32 pipe = 0;
  1257. switch (cmd) {
  1258. case FBIO_WAITFORVSYNC:
  1259. if (get_user(pipe, (__u32 __user *)arg))
  1260. return -EFAULT;
  1261. retval = intelfbhw_wait_for_vsync(dinfo, pipe);
  1262. break;
  1263. default:
  1264. break;
  1265. }
  1266. return retval;
  1267. }
  1268. static void intelfb_fillrect (struct fb_info *info,
  1269. const struct fb_fillrect *rect)
  1270. {
  1271. struct intelfb_info *dinfo = GET_DINFO(info);
  1272. u32 rop, color;
  1273. #if VERBOSE > 0
  1274. DBG_MSG("intelfb_fillrect\n");
  1275. #endif
  1276. if (!ACCEL(dinfo, info) || dinfo->depth == 4) {
  1277. cfb_fillrect(info, rect);
  1278. return;
  1279. }
  1280. if (rect->rop == ROP_COPY)
  1281. rop = PAT_ROP_GXCOPY;
  1282. else /* ROP_XOR */
  1283. rop = PAT_ROP_GXXOR;
  1284. if (dinfo->depth != 8)
  1285. color = dinfo->pseudo_palette[rect->color];
  1286. else
  1287. color = rect->color;
  1288. intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
  1289. rect->width, rect->height, color,
  1290. dinfo->pitch, info->var.bits_per_pixel,
  1291. rop);
  1292. }
  1293. static void intelfb_copyarea(struct fb_info *info,
  1294. const struct fb_copyarea *region)
  1295. {
  1296. struct intelfb_info *dinfo = GET_DINFO(info);
  1297. #if VERBOSE > 0
  1298. DBG_MSG("intelfb_copyarea\n");
  1299. #endif
  1300. if (!ACCEL(dinfo, info) || dinfo->depth == 4) {
  1301. cfb_copyarea(info, region);
  1302. return;
  1303. }
  1304. intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
  1305. region->dy, region->width, region->height,
  1306. dinfo->pitch, info->var.bits_per_pixel);
  1307. }
  1308. static void intelfb_imageblit(struct fb_info *info,
  1309. const struct fb_image *image)
  1310. {
  1311. struct intelfb_info *dinfo = GET_DINFO(info);
  1312. u32 fgcolor, bgcolor;
  1313. #if VERBOSE > 0
  1314. DBG_MSG("intelfb_imageblit\n");
  1315. #endif
  1316. if (!ACCEL(dinfo, info) || dinfo->depth == 4
  1317. || image->depth != 1) {
  1318. cfb_imageblit(info, image);
  1319. return;
  1320. }
  1321. if (dinfo->depth != 8) {
  1322. fgcolor = dinfo->pseudo_palette[image->fg_color];
  1323. bgcolor = dinfo->pseudo_palette[image->bg_color];
  1324. } else {
  1325. fgcolor = image->fg_color;
  1326. bgcolor = image->bg_color;
  1327. }
  1328. if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
  1329. image->height, image->data,
  1330. image->dx, image->dy,
  1331. dinfo->pitch, info->var.bits_per_pixel)) {
  1332. cfb_imageblit(info, image);
  1333. return;
  1334. }
  1335. }
  1336. static int intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  1337. {
  1338. struct intelfb_info *dinfo = GET_DINFO(info);
  1339. u32 physical;
  1340. #if VERBOSE > 0
  1341. DBG_MSG("intelfb_cursor\n");
  1342. #endif
  1343. if (!dinfo->hwcursor)
  1344. return -ENODEV;
  1345. intelfbhw_cursor_hide(dinfo);
  1346. /* If XFree killed the cursor - restore it */
  1347. physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
  1348. (dinfo->cursor.offset << 12);
  1349. if (INREG(CURSOR_A_BASEADDR) != physical) {
  1350. u32 fg, bg;
  1351. DBG_MSG("the cursor was killed - restore it !!\n");
  1352. DBG_MSG("size %d, %d pos %d, %d\n",
  1353. cursor->image.width, cursor->image.height,
  1354. cursor->image.dx, cursor->image.dy);
  1355. intelfbhw_cursor_init(dinfo);
  1356. intelfbhw_cursor_reset(dinfo);
  1357. intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
  1358. cursor->image.dy);
  1359. if (dinfo->depth != 8) {
  1360. fg =dinfo->pseudo_palette[cursor->image.fg_color];
  1361. bg =dinfo->pseudo_palette[cursor->image.bg_color];
  1362. } else {
  1363. fg = cursor->image.fg_color;
  1364. bg = cursor->image.bg_color;
  1365. }
  1366. intelfbhw_cursor_setcolor(dinfo, bg, fg);
  1367. intelfbhw_cursor_load(dinfo, cursor->image.width,
  1368. cursor->image.height,
  1369. dinfo->cursor_src);
  1370. if (cursor->enable)
  1371. intelfbhw_cursor_show(dinfo);
  1372. return 0;
  1373. }
  1374. if (cursor->set & FB_CUR_SETPOS) {
  1375. u32 dx, dy;
  1376. dx = cursor->image.dx - info->var.xoffset;
  1377. dy = cursor->image.dy - info->var.yoffset;
  1378. intelfbhw_cursor_setpos(dinfo, dx, dy);
  1379. }
  1380. if (cursor->set & FB_CUR_SETSIZE) {
  1381. if (cursor->image.width > 64 || cursor->image.height > 64)
  1382. return -ENXIO;
  1383. intelfbhw_cursor_reset(dinfo);
  1384. }
  1385. if (cursor->set & FB_CUR_SETCMAP) {
  1386. u32 fg, bg;
  1387. if (dinfo->depth != 8) {
  1388. fg = dinfo->pseudo_palette[cursor->image.fg_color];
  1389. bg = dinfo->pseudo_palette[cursor->image.bg_color];
  1390. } else {
  1391. fg = cursor->image.fg_color;
  1392. bg = cursor->image.bg_color;
  1393. }
  1394. intelfbhw_cursor_setcolor(dinfo, bg, fg);
  1395. }
  1396. if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
  1397. u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
  1398. u32 size = s_pitch * cursor->image.height;
  1399. u8 *dat = (u8 *) cursor->image.data;
  1400. u8 *msk = (u8 *) cursor->mask;
  1401. u8 src[64];
  1402. u32 i;
  1403. if (cursor->image.depth != 1)
  1404. return -ENXIO;
  1405. switch (cursor->rop) {
  1406. case ROP_XOR:
  1407. for (i = 0; i < size; i++)
  1408. src[i] = dat[i] ^ msk[i];
  1409. break;
  1410. case ROP_COPY:
  1411. default:
  1412. for (i = 0; i < size; i++)
  1413. src[i] = dat[i] & msk[i];
  1414. break;
  1415. }
  1416. /* save the bitmap to restore it when XFree will
  1417. make the cursor dirty */
  1418. memcpy(dinfo->cursor_src, src, size);
  1419. intelfbhw_cursor_load(dinfo, cursor->image.width,
  1420. cursor->image.height, src);
  1421. }
  1422. if (cursor->enable)
  1423. intelfbhw_cursor_show(dinfo);
  1424. return 0;
  1425. }
  1426. static int intelfb_sync(struct fb_info *info)
  1427. {
  1428. struct intelfb_info *dinfo = GET_DINFO(info);
  1429. #if VERBOSE > 0
  1430. DBG_MSG("intelfb_sync\n");
  1431. #endif
  1432. if (dinfo->ring_lockup)
  1433. return 0;
  1434. intelfbhw_do_sync(dinfo);
  1435. return 0;
  1436. }