vermilion.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Copyright (c) Intel Corp. 2007.
  3. * All Rights Reserved.
  4. *
  5. * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
  6. * develop this driver.
  7. *
  8. * This file is part of the Vermilion Range fb driver.
  9. * The Vermilion Range fb driver is free software;
  10. * you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * The Vermilion Range fb driver is distributed
  16. * in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this driver; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Authors:
  26. * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  27. * Michel Dänzer <michel-at-tungstengraphics-dot-com>
  28. * Alan Hourihane <alanh-at-tungstengraphics-dot-com>
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/errno.h>
  33. #include <linux/string.h>
  34. #include <linux/delay.h>
  35. #include <linux/mm.h>
  36. #include <linux/fb.h>
  37. #include <linux/pci.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/tlbflush.h>
  40. #include <linux/mmzone.h>
  41. #include <asm/uaccess.h>
  42. /* #define VERMILION_DEBUG */
  43. #include "vermilion.h"
  44. #define MODULE_NAME "vmlfb"
  45. #define VML_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
  46. static struct mutex vml_mutex;
  47. static struct list_head global_no_mode;
  48. static struct list_head global_has_mode;
  49. static struct fb_ops vmlfb_ops;
  50. static struct vml_sys *subsys = NULL;
  51. static char *vml_default_mode = "1024x768@60";
  52. static struct fb_videomode defaultmode = {
  53. NULL, 60, 1024, 768, 12896, 144, 24, 29, 3, 136, 6,
  54. 0, FB_VMODE_NONINTERLACED
  55. };
  56. static u32 vml_mem_requested = (10 * 1024 * 1024);
  57. static u32 vml_mem_contig = (4 * 1024 * 1024);
  58. static u32 vml_mem_min = (4 * 1024 * 1024);
  59. static u32 vml_clocks[] = {
  60. 6750,
  61. 13500,
  62. 27000,
  63. 29700,
  64. 37125,
  65. 54000,
  66. 59400,
  67. 74250,
  68. 120000,
  69. 148500
  70. };
  71. static u32 vml_num_clocks = ARRAY_SIZE(vml_clocks);
  72. /*
  73. * Allocate a contiguous vram area and make its linear kernel map
  74. * uncached.
  75. */
  76. static int vmlfb_alloc_vram_area(struct vram_area *va, unsigned max_order,
  77. unsigned min_order)
  78. {
  79. gfp_t flags;
  80. unsigned long i;
  81. pgprot_t wc_pageprot;
  82. wc_pageprot = PAGE_KERNEL_NOCACHE;
  83. max_order++;
  84. do {
  85. /*
  86. * Really try hard to get the needed memory.
  87. * We need memory below the first 32MB, so we
  88. * add the __GFP_DMA flag that guarantees that we are
  89. * below the first 16MB.
  90. */
  91. flags = __GFP_DMA | __GFP_HIGH;
  92. va->logical =
  93. __get_free_pages(flags, --max_order);
  94. } while (va->logical == 0 && max_order > min_order);
  95. if (!va->logical)
  96. return -ENOMEM;
  97. va->phys = virt_to_phys((void *)va->logical);
  98. va->size = PAGE_SIZE << max_order;
  99. va->order = max_order;
  100. /*
  101. * It seems like __get_free_pages only ups the usage count
  102. * of the first page. This doesn't work with nopage mapping, so
  103. * up the usage count once more.
  104. */
  105. memset((void *)va->logical, 0x00, va->size);
  106. for (i = va->logical; i < va->logical + va->size; i += PAGE_SIZE) {
  107. get_page(virt_to_page(i));
  108. }
  109. /*
  110. * Change caching policy of the linear kernel map to avoid
  111. * mapping type conflicts with user-space mappings.
  112. * The first global_flush_tlb() is really only there to do a global
  113. * wbinvd().
  114. */
  115. global_flush_tlb();
  116. change_page_attr(virt_to_page(va->logical), va->size >> PAGE_SHIFT,
  117. wc_pageprot);
  118. global_flush_tlb();
  119. printk(KERN_DEBUG MODULE_NAME
  120. ": Allocated %ld bytes vram area at 0x%08lx\n",
  121. va->size, va->phys);
  122. return 0;
  123. }
  124. /*
  125. * Free a contiguous vram area and reset its linear kernel map
  126. * mapping type.
  127. */
  128. static void vmlfb_free_vram_area(struct vram_area *va)
  129. {
  130. unsigned long j;
  131. if (va->logical) {
  132. /*
  133. * Reset the linear kernel map caching policy.
  134. */
  135. change_page_attr(virt_to_page(va->logical),
  136. va->size >> PAGE_SHIFT, PAGE_KERNEL);
  137. global_flush_tlb();
  138. /*
  139. * Decrease the usage count on the pages we've used
  140. * to compensate for upping when allocating.
  141. */
  142. for (j = va->logical; j < va->logical + va->size;
  143. j += PAGE_SIZE) {
  144. (void)put_page_testzero(virt_to_page(j));
  145. }
  146. printk(KERN_DEBUG MODULE_NAME
  147. ": Freeing %ld bytes vram area at 0x%08lx\n",
  148. va->size, va->phys);
  149. free_pages(va->logical, va->order);
  150. va->logical = 0;
  151. }
  152. }
  153. /*
  154. * Free allocated vram.
  155. */
  156. static void vmlfb_free_vram(struct vml_info *vinfo)
  157. {
  158. int i;
  159. for (i = 0; i < vinfo->num_areas; ++i) {
  160. vmlfb_free_vram_area(&vinfo->vram[i]);
  161. }
  162. vinfo->num_areas = 0;
  163. }
  164. /*
  165. * Allocate vram. Currently we try to allocate contiguous areas from the
  166. * __GFP_DMA zone and puzzle them together. A better approach would be to
  167. * allocate one contiguous area for scanout and use one-page allocations for
  168. * offscreen areas. This requires user-space and GPU virtual mappings.
  169. */
  170. static int vmlfb_alloc_vram(struct vml_info *vinfo,
  171. size_t requested,
  172. size_t min_total, size_t min_contig)
  173. {
  174. int i, j;
  175. int order;
  176. int contiguous;
  177. int err;
  178. struct vram_area *va;
  179. struct vram_area *va2;
  180. vinfo->num_areas = 0;
  181. for (i = 0; i < VML_VRAM_AREAS; ++i) {
  182. va = &vinfo->vram[i];
  183. order = 0;
  184. while (requested > (PAGE_SIZE << order) && order < MAX_ORDER)
  185. order++;
  186. err = vmlfb_alloc_vram_area(va, order, 0);
  187. if (err)
  188. break;
  189. if (i == 0) {
  190. vinfo->vram_start = va->phys;
  191. vinfo->vram_logical = (void __iomem *) va->logical;
  192. vinfo->vram_contig_size = va->size;
  193. vinfo->num_areas = 1;
  194. } else {
  195. contiguous = 0;
  196. for (j = 0; j < i; ++j) {
  197. va2 = &vinfo->vram[j];
  198. if (va->phys + va->size == va2->phys ||
  199. va2->phys + va2->size == va->phys) {
  200. contiguous = 1;
  201. break;
  202. }
  203. }
  204. if (contiguous) {
  205. vinfo->num_areas++;
  206. if (va->phys < vinfo->vram_start) {
  207. vinfo->vram_start = va->phys;
  208. vinfo->vram_logical =
  209. (void __iomem *)va->logical;
  210. }
  211. vinfo->vram_contig_size += va->size;
  212. } else {
  213. vmlfb_free_vram_area(va);
  214. break;
  215. }
  216. }
  217. if (requested < va->size)
  218. break;
  219. else
  220. requested -= va->size;
  221. }
  222. if (vinfo->vram_contig_size > min_total &&
  223. vinfo->vram_contig_size > min_contig) {
  224. printk(KERN_DEBUG MODULE_NAME
  225. ": Contiguous vram: %ld bytes at physical 0x%08lx.\n",
  226. (unsigned long)vinfo->vram_contig_size,
  227. (unsigned long)vinfo->vram_start);
  228. return 0;
  229. }
  230. printk(KERN_ERR MODULE_NAME
  231. ": Could not allocate requested minimal amount of vram.\n");
  232. vmlfb_free_vram(vinfo);
  233. return -ENOMEM;
  234. }
  235. /*
  236. * Find the GPU to use with our display controller.
  237. */
  238. static int vmlfb_get_gpu(struct vml_par *par)
  239. {
  240. mutex_lock(&vml_mutex);
  241. par->gpu = pci_get_device(PCI_VENDOR_ID_INTEL, VML_DEVICE_GPU, NULL);
  242. if (!par->gpu) {
  243. mutex_unlock(&vml_mutex);
  244. return -ENODEV;
  245. }
  246. mutex_unlock(&vml_mutex);
  247. if (pci_enable_device(par->gpu) < 0)
  248. return -ENODEV;
  249. return 0;
  250. }
  251. /*
  252. * Find a contiguous vram area that contains a given offset from vram start.
  253. */
  254. static int vmlfb_vram_offset(struct vml_info *vinfo, unsigned long offset)
  255. {
  256. unsigned long aoffset;
  257. unsigned i;
  258. for (i = 0; i < vinfo->num_areas; ++i) {
  259. aoffset = offset - (vinfo->vram[i].phys - vinfo->vram_start);
  260. if (aoffset < vinfo->vram[i].size) {
  261. return 0;
  262. }
  263. }
  264. return -EINVAL;
  265. }
  266. /*
  267. * Remap the MMIO register spaces of the VDC and the GPU.
  268. */
  269. static int vmlfb_enable_mmio(struct vml_par *par)
  270. {
  271. int err;
  272. par->vdc_mem_base = pci_resource_start(par->vdc, 0);
  273. par->vdc_mem_size = pci_resource_len(par->vdc, 0);
  274. if (!request_mem_region(par->vdc_mem_base, par->vdc_mem_size, "vmlfb")) {
  275. printk(KERN_ERR MODULE_NAME
  276. ": Could not claim display controller MMIO.\n");
  277. return -EBUSY;
  278. }
  279. par->vdc_mem = ioremap_nocache(par->vdc_mem_base, par->vdc_mem_size);
  280. if (par->vdc_mem == NULL) {
  281. printk(KERN_ERR MODULE_NAME
  282. ": Could not map display controller MMIO.\n");
  283. err = -ENOMEM;
  284. goto out_err_0;
  285. }
  286. par->gpu_mem_base = pci_resource_start(par->gpu, 0);
  287. par->gpu_mem_size = pci_resource_len(par->gpu, 0);
  288. if (!request_mem_region(par->gpu_mem_base, par->gpu_mem_size, "vmlfb")) {
  289. printk(KERN_ERR MODULE_NAME ": Could not claim GPU MMIO.\n");
  290. err = -EBUSY;
  291. goto out_err_1;
  292. }
  293. par->gpu_mem = ioremap_nocache(par->gpu_mem_base, par->gpu_mem_size);
  294. if (par->gpu_mem == NULL) {
  295. printk(KERN_ERR MODULE_NAME ": Could not map GPU MMIO.\n");
  296. err = -ENOMEM;
  297. goto out_err_2;
  298. }
  299. return 0;
  300. out_err_2:
  301. release_mem_region(par->gpu_mem_base, par->gpu_mem_size);
  302. out_err_1:
  303. iounmap(par->vdc_mem);
  304. out_err_0:
  305. release_mem_region(par->vdc_mem_base, par->vdc_mem_size);
  306. return err;
  307. }
  308. /*
  309. * Unmap the VDC and GPU register spaces.
  310. */
  311. static void vmlfb_disable_mmio(struct vml_par *par)
  312. {
  313. iounmap(par->gpu_mem);
  314. release_mem_region(par->gpu_mem_base, par->gpu_mem_size);
  315. iounmap(par->vdc_mem);
  316. release_mem_region(par->vdc_mem_base, par->vdc_mem_size);
  317. }
  318. /*
  319. * Release and uninit the VDC and GPU.
  320. */
  321. static void vmlfb_release_devices(struct vml_par *par)
  322. {
  323. if (atomic_dec_and_test(&par->refcount)) {
  324. pci_set_drvdata(par->vdc, NULL);
  325. pci_disable_device(par->gpu);
  326. pci_disable_device(par->vdc);
  327. }
  328. }
  329. /*
  330. * Free up allocated resources for a device.
  331. */
  332. static void __devexit vml_pci_remove(struct pci_dev *dev)
  333. {
  334. struct fb_info *info;
  335. struct vml_info *vinfo;
  336. struct vml_par *par;
  337. info = pci_get_drvdata(dev);
  338. if (info) {
  339. vinfo = container_of(info, struct vml_info, info);
  340. par = vinfo->par;
  341. mutex_lock(&vml_mutex);
  342. unregister_framebuffer(info);
  343. fb_dealloc_cmap(&info->cmap);
  344. vmlfb_free_vram(vinfo);
  345. vmlfb_disable_mmio(par);
  346. vmlfb_release_devices(par);
  347. kfree(vinfo);
  348. kfree(par);
  349. mutex_unlock(&vml_mutex);
  350. }
  351. }
  352. static void vmlfb_set_pref_pixel_format(struct fb_var_screeninfo *var)
  353. {
  354. switch (var->bits_per_pixel) {
  355. case 16:
  356. var->blue.offset = 0;
  357. var->blue.length = 5;
  358. var->green.offset = 5;
  359. var->green.length = 5;
  360. var->red.offset = 10;
  361. var->red.length = 5;
  362. var->transp.offset = 15;
  363. var->transp.length = 1;
  364. break;
  365. case 32:
  366. var->blue.offset = 0;
  367. var->blue.length = 8;
  368. var->green.offset = 8;
  369. var->green.length = 8;
  370. var->red.offset = 16;
  371. var->red.length = 8;
  372. var->transp.offset = 24;
  373. var->transp.length = 0;
  374. break;
  375. default:
  376. break;
  377. }
  378. var->blue.msb_right = var->green.msb_right =
  379. var->red.msb_right = var->transp.msb_right = 0;
  380. }
  381. /*
  382. * Device initialization.
  383. * We initialize one vml_par struct per device and one vml_info
  384. * struct per pipe. Currently we have only one pipe.
  385. */
  386. static int __devinit vml_pci_probe(struct pci_dev *dev,
  387. const struct pci_device_id *id)
  388. {
  389. struct vml_info *vinfo;
  390. struct fb_info *info;
  391. struct vml_par *par;
  392. int err = 0;
  393. par = kzalloc(sizeof(*par), GFP_KERNEL);
  394. if (par == NULL)
  395. return -ENOMEM;
  396. vinfo = kzalloc(sizeof(*vinfo), GFP_KERNEL);
  397. if (vinfo == NULL) {
  398. err = -ENOMEM;
  399. goto out_err_0;
  400. }
  401. vinfo->par = par;
  402. par->vdc = dev;
  403. atomic_set(&par->refcount, 1);
  404. switch (id->device) {
  405. case VML_DEVICE_VDC:
  406. if ((err = vmlfb_get_gpu(par)))
  407. goto out_err_1;
  408. pci_set_drvdata(dev, &vinfo->info);
  409. break;
  410. default:
  411. err = -ENODEV;
  412. goto out_err_1;
  413. break;
  414. }
  415. info = &vinfo->info;
  416. info->flags = FBINFO_DEFAULT | FBINFO_PARTIAL_PAN_OK;
  417. err = vmlfb_enable_mmio(par);
  418. if (err)
  419. goto out_err_2;
  420. err = vmlfb_alloc_vram(vinfo, vml_mem_requested,
  421. vml_mem_contig, vml_mem_min);
  422. if (err)
  423. goto out_err_3;
  424. strcpy(info->fix.id, "Vermilion Range");
  425. info->fix.mmio_start = 0;
  426. info->fix.mmio_len = 0;
  427. info->fix.smem_start = vinfo->vram_start;
  428. info->fix.smem_len = vinfo->vram_contig_size;
  429. info->fix.type = FB_TYPE_PACKED_PIXELS;
  430. info->fix.visual = FB_VISUAL_TRUECOLOR;
  431. info->fix.ypanstep = 1;
  432. info->fix.xpanstep = 1;
  433. info->fix.ywrapstep = 0;
  434. info->fix.accel = FB_ACCEL_NONE;
  435. info->screen_base = vinfo->vram_logical;
  436. info->pseudo_palette = vinfo->pseudo_palette;
  437. info->par = par;
  438. info->fbops = &vmlfb_ops;
  439. info->device = &dev->dev;
  440. INIT_LIST_HEAD(&vinfo->head);
  441. vinfo->pipe_disabled = 1;
  442. vinfo->cur_blank_mode = FB_BLANK_UNBLANK;
  443. info->var.grayscale = 0;
  444. info->var.bits_per_pixel = 16;
  445. vmlfb_set_pref_pixel_format(&info->var);
  446. if (!fb_find_mode
  447. (&info->var, info, vml_default_mode, NULL, 0, &defaultmode, 16)) {
  448. printk(KERN_ERR MODULE_NAME ": Could not find initial mode\n");
  449. }
  450. if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
  451. err = -ENOMEM;
  452. goto out_err_4;
  453. }
  454. err = register_framebuffer(info);
  455. if (err) {
  456. printk(KERN_ERR MODULE_NAME ": Register framebuffer error.\n");
  457. goto out_err_5;
  458. }
  459. printk("Initialized vmlfb\n");
  460. return 0;
  461. out_err_5:
  462. fb_dealloc_cmap(&info->cmap);
  463. out_err_4:
  464. vmlfb_free_vram(vinfo);
  465. out_err_3:
  466. vmlfb_disable_mmio(par);
  467. out_err_2:
  468. vmlfb_release_devices(par);
  469. out_err_1:
  470. kfree(vinfo);
  471. out_err_0:
  472. kfree(par);
  473. return err;
  474. }
  475. static int vmlfb_open(struct fb_info *info, int user)
  476. {
  477. /*
  478. * Save registers here?
  479. */
  480. return 0;
  481. }
  482. static int vmlfb_release(struct fb_info *info, int user)
  483. {
  484. /*
  485. * Restore registers here.
  486. */
  487. return 0;
  488. }
  489. static int vml_nearest_clock(int clock)
  490. {
  491. int i;
  492. int cur_index;
  493. int cur_diff;
  494. int diff;
  495. cur_index = 0;
  496. cur_diff = clock - vml_clocks[0];
  497. cur_diff = (cur_diff < 0) ? -cur_diff : cur_diff;
  498. for (i = 1; i < vml_num_clocks; ++i) {
  499. diff = clock - vml_clocks[i];
  500. diff = (diff < 0) ? -diff : diff;
  501. if (diff < cur_diff) {
  502. cur_index = i;
  503. cur_diff = diff;
  504. }
  505. }
  506. return vml_clocks[cur_index];
  507. }
  508. static int vmlfb_check_var_locked(struct fb_var_screeninfo *var,
  509. struct vml_info *vinfo)
  510. {
  511. u32 pitch;
  512. u64 mem;
  513. int nearest_clock;
  514. int clock;
  515. int clock_diff;
  516. struct fb_var_screeninfo v;
  517. v = *var;
  518. clock = PICOS2KHZ(var->pixclock);
  519. if (subsys && subsys->nearest_clock) {
  520. nearest_clock = subsys->nearest_clock(subsys, clock);
  521. } else {
  522. nearest_clock = vml_nearest_clock(clock);
  523. }
  524. /*
  525. * Accept a 20% diff.
  526. */
  527. clock_diff = nearest_clock - clock;
  528. clock_diff = (clock_diff < 0) ? -clock_diff : clock_diff;
  529. if (clock_diff > clock / 5) {
  530. #if 0
  531. printk(KERN_DEBUG MODULE_NAME ": Diff failure. %d %d\n",clock_diff,clock);
  532. #endif
  533. return -EINVAL;
  534. }
  535. v.pixclock = KHZ2PICOS(nearest_clock);
  536. if (var->xres > VML_MAX_XRES || var->yres > VML_MAX_YRES) {
  537. printk(KERN_DEBUG MODULE_NAME ": Resolution failure.\n");
  538. return -EINVAL;
  539. }
  540. if (var->xres_virtual > VML_MAX_XRES_VIRTUAL) {
  541. printk(KERN_DEBUG MODULE_NAME
  542. ": Virtual resolution failure.\n");
  543. return -EINVAL;
  544. }
  545. switch (v.bits_per_pixel) {
  546. case 0 ... 16:
  547. v.bits_per_pixel = 16;
  548. break;
  549. case 17 ... 32:
  550. v.bits_per_pixel = 32;
  551. break;
  552. default:
  553. printk(KERN_DEBUG MODULE_NAME ": Invalid bpp: %d.\n",
  554. var->bits_per_pixel);
  555. return -EINVAL;
  556. }
  557. pitch = __ALIGN_MASK((var->xres * var->bits_per_pixel) >> 3, 0x3F);
  558. mem = pitch * var->yres_virtual;
  559. if (mem > vinfo->vram_contig_size) {
  560. return -ENOMEM;
  561. }
  562. switch (v.bits_per_pixel) {
  563. case 16:
  564. if (var->blue.offset != 0 ||
  565. var->blue.length != 5 ||
  566. var->green.offset != 5 ||
  567. var->green.length != 5 ||
  568. var->red.offset != 10 ||
  569. var->red.length != 5 ||
  570. var->transp.offset != 15 || var->transp.length != 1) {
  571. vmlfb_set_pref_pixel_format(&v);
  572. }
  573. break;
  574. case 32:
  575. if (var->blue.offset != 0 ||
  576. var->blue.length != 8 ||
  577. var->green.offset != 8 ||
  578. var->green.length != 8 ||
  579. var->red.offset != 16 ||
  580. var->red.length != 8 ||
  581. (var->transp.length != 0 && var->transp.length != 8) ||
  582. (var->transp.length == 8 && var->transp.offset != 24)) {
  583. vmlfb_set_pref_pixel_format(&v);
  584. }
  585. break;
  586. default:
  587. return -EINVAL;
  588. }
  589. *var = v;
  590. return 0;
  591. }
  592. static int vmlfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  593. {
  594. struct vml_info *vinfo = container_of(info, struct vml_info, info);
  595. int ret;
  596. mutex_lock(&vml_mutex);
  597. ret = vmlfb_check_var_locked(var, vinfo);
  598. mutex_unlock(&vml_mutex);
  599. return ret;
  600. }
  601. static void vml_wait_vblank(struct vml_info *vinfo)
  602. {
  603. /* Wait for vblank. For now, just wait for a 50Hz cycle (20ms)) */
  604. mdelay(20);
  605. }
  606. static void vmlfb_disable_pipe(struct vml_info *vinfo)
  607. {
  608. struct vml_par *par = vinfo->par;
  609. /* Disable the MDVO pad */
  610. VML_WRITE32(par, VML_RCOMPSTAT, 0);
  611. while (!(VML_READ32(par, VML_RCOMPSTAT) & VML_MDVO_VDC_I_RCOMP)) ;
  612. /* Disable display planes */
  613. VML_WRITE32(par, VML_DSPCCNTR,
  614. VML_READ32(par, VML_DSPCCNTR) & ~VML_GFX_ENABLE);
  615. (void)VML_READ32(par, VML_DSPCCNTR);
  616. /* Wait for vblank for the disable to take effect */
  617. vml_wait_vblank(vinfo);
  618. /* Next, disable display pipes */
  619. VML_WRITE32(par, VML_PIPEACONF, 0);
  620. (void)VML_READ32(par, VML_PIPEACONF);
  621. vinfo->pipe_disabled = 1;
  622. }
  623. #ifdef VERMILION_DEBUG
  624. static void vml_dump_regs(struct vml_info *vinfo)
  625. {
  626. struct vml_par *par = vinfo->par;
  627. printk(KERN_DEBUG MODULE_NAME ": Modesetting register dump:\n");
  628. printk(KERN_DEBUG MODULE_NAME ": \tHTOTAL_A : 0x%08x\n",
  629. (unsigned)VML_READ32(par, VML_HTOTAL_A));
  630. printk(KERN_DEBUG MODULE_NAME ": \tHBLANK_A : 0x%08x\n",
  631. (unsigned)VML_READ32(par, VML_HBLANK_A));
  632. printk(KERN_DEBUG MODULE_NAME ": \tHSYNC_A : 0x%08x\n",
  633. (unsigned)VML_READ32(par, VML_HSYNC_A));
  634. printk(KERN_DEBUG MODULE_NAME ": \tVTOTAL_A : 0x%08x\n",
  635. (unsigned)VML_READ32(par, VML_VTOTAL_A));
  636. printk(KERN_DEBUG MODULE_NAME ": \tVBLANK_A : 0x%08x\n",
  637. (unsigned)VML_READ32(par, VML_VBLANK_A));
  638. printk(KERN_DEBUG MODULE_NAME ": \tVSYNC_A : 0x%08x\n",
  639. (unsigned)VML_READ32(par, VML_VSYNC_A));
  640. printk(KERN_DEBUG MODULE_NAME ": \tDSPCSTRIDE : 0x%08x\n",
  641. (unsigned)VML_READ32(par, VML_DSPCSTRIDE));
  642. printk(KERN_DEBUG MODULE_NAME ": \tDSPCSIZE : 0x%08x\n",
  643. (unsigned)VML_READ32(par, VML_DSPCSIZE));
  644. printk(KERN_DEBUG MODULE_NAME ": \tDSPCPOS : 0x%08x\n",
  645. (unsigned)VML_READ32(par, VML_DSPCPOS));
  646. printk(KERN_DEBUG MODULE_NAME ": \tDSPARB : 0x%08x\n",
  647. (unsigned)VML_READ32(par, VML_DSPARB));
  648. printk(KERN_DEBUG MODULE_NAME ": \tDSPCADDR : 0x%08x\n",
  649. (unsigned)VML_READ32(par, VML_DSPCADDR));
  650. printk(KERN_DEBUG MODULE_NAME ": \tBCLRPAT_A : 0x%08x\n",
  651. (unsigned)VML_READ32(par, VML_BCLRPAT_A));
  652. printk(KERN_DEBUG MODULE_NAME ": \tCANVSCLR_A : 0x%08x\n",
  653. (unsigned)VML_READ32(par, VML_CANVSCLR_A));
  654. printk(KERN_DEBUG MODULE_NAME ": \tPIPEASRC : 0x%08x\n",
  655. (unsigned)VML_READ32(par, VML_PIPEASRC));
  656. printk(KERN_DEBUG MODULE_NAME ": \tPIPEACONF : 0x%08x\n",
  657. (unsigned)VML_READ32(par, VML_PIPEACONF));
  658. printk(KERN_DEBUG MODULE_NAME ": \tDSPCCNTR : 0x%08x\n",
  659. (unsigned)VML_READ32(par, VML_DSPCCNTR));
  660. printk(KERN_DEBUG MODULE_NAME ": \tRCOMPSTAT : 0x%08x\n",
  661. (unsigned)VML_READ32(par, VML_RCOMPSTAT));
  662. printk(KERN_DEBUG MODULE_NAME ": End of modesetting register dump.\n");
  663. }
  664. #endif
  665. static int vmlfb_set_par_locked(struct vml_info *vinfo)
  666. {
  667. struct vml_par *par = vinfo->par;
  668. struct fb_info *info = &vinfo->info;
  669. struct fb_var_screeninfo *var = &info->var;
  670. u32 htotal, hactive, hblank_start, hblank_end, hsync_start, hsync_end;
  671. u32 vtotal, vactive, vblank_start, vblank_end, vsync_start, vsync_end;
  672. u32 dspcntr;
  673. int clock;
  674. vinfo->bytes_per_pixel = var->bits_per_pixel >> 3;
  675. vinfo->stride =
  676. __ALIGN_MASK(var->xres_virtual * vinfo->bytes_per_pixel, 0x3F);
  677. info->fix.line_length = vinfo->stride;
  678. if (!subsys)
  679. return 0;
  680. htotal =
  681. var->xres + var->right_margin + var->hsync_len + var->left_margin;
  682. hactive = var->xres;
  683. hblank_start = var->xres;
  684. hblank_end = htotal;
  685. hsync_start = hactive + var->right_margin;
  686. hsync_end = hsync_start + var->hsync_len;
  687. vtotal =
  688. var->yres + var->lower_margin + var->vsync_len + var->upper_margin;
  689. vactive = var->yres;
  690. vblank_start = var->yres;
  691. vblank_end = vtotal;
  692. vsync_start = vactive + var->lower_margin;
  693. vsync_end = vsync_start + var->vsync_len;
  694. dspcntr = VML_GFX_ENABLE | VML_GFX_GAMMABYPASS;
  695. clock = PICOS2KHZ(var->pixclock);
  696. if (subsys->nearest_clock) {
  697. clock = subsys->nearest_clock(subsys, clock);
  698. } else {
  699. clock = vml_nearest_clock(clock);
  700. }
  701. printk(KERN_DEBUG MODULE_NAME
  702. ": Set mode Hfreq : %d kHz, Vfreq : %d Hz.\n", clock / htotal,
  703. ((clock / htotal) * 1000) / vtotal);
  704. switch (var->bits_per_pixel) {
  705. case 16:
  706. dspcntr |= VML_GFX_ARGB1555;
  707. break;
  708. case 32:
  709. if (var->transp.length == 8)
  710. dspcntr |= VML_GFX_ARGB8888 | VML_GFX_ALPHAMULT;
  711. else
  712. dspcntr |= VML_GFX_RGB0888;
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. vmlfb_disable_pipe(vinfo);
  718. mb();
  719. if (subsys->set_clock)
  720. subsys->set_clock(subsys, clock);
  721. else
  722. return -EINVAL;
  723. VML_WRITE32(par, VML_HTOTAL_A, ((htotal - 1) << 16) | (hactive - 1));
  724. VML_WRITE32(par, VML_HBLANK_A,
  725. ((hblank_end - 1) << 16) | (hblank_start - 1));
  726. VML_WRITE32(par, VML_HSYNC_A,
  727. ((hsync_end - 1) << 16) | (hsync_start - 1));
  728. VML_WRITE32(par, VML_VTOTAL_A, ((vtotal - 1) << 16) | (vactive - 1));
  729. VML_WRITE32(par, VML_VBLANK_A,
  730. ((vblank_end - 1) << 16) | (vblank_start - 1));
  731. VML_WRITE32(par, VML_VSYNC_A,
  732. ((vsync_end - 1) << 16) | (vsync_start - 1));
  733. VML_WRITE32(par, VML_DSPCSTRIDE, vinfo->stride);
  734. VML_WRITE32(par, VML_DSPCSIZE,
  735. ((var->yres - 1) << 16) | (var->xres - 1));
  736. VML_WRITE32(par, VML_DSPCPOS, 0x00000000);
  737. VML_WRITE32(par, VML_DSPARB, VML_FIFO_DEFAULT);
  738. VML_WRITE32(par, VML_BCLRPAT_A, 0x00000000);
  739. VML_WRITE32(par, VML_CANVSCLR_A, 0x00000000);
  740. VML_WRITE32(par, VML_PIPEASRC,
  741. ((var->xres - 1) << 16) | (var->yres - 1));
  742. wmb();
  743. VML_WRITE32(par, VML_PIPEACONF, VML_PIPE_ENABLE);
  744. wmb();
  745. VML_WRITE32(par, VML_DSPCCNTR, dspcntr);
  746. wmb();
  747. VML_WRITE32(par, VML_DSPCADDR, (u32) vinfo->vram_start +
  748. var->yoffset * vinfo->stride +
  749. var->xoffset * vinfo->bytes_per_pixel);
  750. VML_WRITE32(par, VML_RCOMPSTAT, VML_MDVO_PAD_ENABLE);
  751. while (!(VML_READ32(par, VML_RCOMPSTAT) &
  752. (VML_MDVO_VDC_I_RCOMP | VML_MDVO_PAD_ENABLE))) ;
  753. vinfo->pipe_disabled = 0;
  754. #ifdef VERMILION_DEBUG
  755. vml_dump_regs(vinfo);
  756. #endif
  757. return 0;
  758. }
  759. static int vmlfb_set_par(struct fb_info *info)
  760. {
  761. struct vml_info *vinfo = container_of(info, struct vml_info, info);
  762. int ret;
  763. mutex_lock(&vml_mutex);
  764. list_del(&vinfo->head);
  765. list_add(&vinfo->head, (subsys) ? &global_has_mode : &global_no_mode);
  766. ret = vmlfb_set_par_locked(vinfo);
  767. mutex_unlock(&vml_mutex);
  768. return ret;
  769. }
  770. static int vmlfb_blank_locked(struct vml_info *vinfo)
  771. {
  772. struct vml_par *par = vinfo->par;
  773. u32 cur = VML_READ32(par, VML_PIPEACONF);
  774. switch (vinfo->cur_blank_mode) {
  775. case FB_BLANK_UNBLANK:
  776. if (vinfo->pipe_disabled) {
  777. vmlfb_set_par_locked(vinfo);
  778. }
  779. VML_WRITE32(par, VML_PIPEACONF, cur & ~VML_PIPE_FORCE_BORDER);
  780. (void)VML_READ32(par, VML_PIPEACONF);
  781. break;
  782. case FB_BLANK_NORMAL:
  783. if (vinfo->pipe_disabled) {
  784. vmlfb_set_par_locked(vinfo);
  785. }
  786. VML_WRITE32(par, VML_PIPEACONF, cur | VML_PIPE_FORCE_BORDER);
  787. (void)VML_READ32(par, VML_PIPEACONF);
  788. break;
  789. case FB_BLANK_VSYNC_SUSPEND:
  790. case FB_BLANK_HSYNC_SUSPEND:
  791. if (!vinfo->pipe_disabled) {
  792. vmlfb_disable_pipe(vinfo);
  793. }
  794. break;
  795. case FB_BLANK_POWERDOWN:
  796. if (!vinfo->pipe_disabled) {
  797. vmlfb_disable_pipe(vinfo);
  798. }
  799. break;
  800. default:
  801. return -EINVAL;
  802. }
  803. return 0;
  804. }
  805. static int vmlfb_blank(int blank_mode, struct fb_info *info)
  806. {
  807. struct vml_info *vinfo = container_of(info, struct vml_info, info);
  808. int ret;
  809. mutex_lock(&vml_mutex);
  810. vinfo->cur_blank_mode = blank_mode;
  811. ret = vmlfb_blank_locked(vinfo);
  812. mutex_unlock(&vml_mutex);
  813. return ret;
  814. }
  815. static int vmlfb_pan_display(struct fb_var_screeninfo *var,
  816. struct fb_info *info)
  817. {
  818. struct vml_info *vinfo = container_of(info, struct vml_info, info);
  819. struct vml_par *par = vinfo->par;
  820. mutex_lock(&vml_mutex);
  821. VML_WRITE32(par, VML_DSPCADDR, (u32) vinfo->vram_start +
  822. var->yoffset * vinfo->stride +
  823. var->xoffset * vinfo->bytes_per_pixel);
  824. (void)VML_READ32(par, VML_DSPCADDR);
  825. mutex_unlock(&vml_mutex);
  826. return 0;
  827. }
  828. static int vmlfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  829. u_int transp, struct fb_info *info)
  830. {
  831. u32 v;
  832. if (regno >= 16)
  833. return -EINVAL;
  834. if (info->var.grayscale) {
  835. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  836. }
  837. if (info->fix.visual != FB_VISUAL_TRUECOLOR)
  838. return -EINVAL;
  839. red = VML_TOHW(red, info->var.red.length);
  840. blue = VML_TOHW(blue, info->var.blue.length);
  841. green = VML_TOHW(green, info->var.green.length);
  842. transp = VML_TOHW(transp, info->var.transp.length);
  843. v = (red << info->var.red.offset) |
  844. (green << info->var.green.offset) |
  845. (blue << info->var.blue.offset) |
  846. (transp << info->var.transp.offset);
  847. switch (info->var.bits_per_pixel) {
  848. case 16:
  849. ((u32 *) info->pseudo_palette)[regno] = v;
  850. break;
  851. case 24:
  852. case 32:
  853. ((u32 *) info->pseudo_palette)[regno] = v;
  854. break;
  855. }
  856. return 0;
  857. }
  858. static int vmlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  859. {
  860. struct vml_info *vinfo = container_of(info, struct vml_info, info);
  861. unsigned long size = vma->vm_end - vma->vm_start;
  862. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  863. int ret;
  864. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  865. return -EINVAL;
  866. if (offset + size > vinfo->vram_contig_size)
  867. return -EINVAL;
  868. ret = vmlfb_vram_offset(vinfo, offset);
  869. if (ret)
  870. return -EINVAL;
  871. offset += vinfo->vram_start;
  872. pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
  873. pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT;
  874. vma->vm_flags |= VM_RESERVED | VM_IO;
  875. if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
  876. size, vma->vm_page_prot))
  877. return -EAGAIN;
  878. return 0;
  879. }
  880. static int vmlfb_sync(struct fb_info *info)
  881. {
  882. return 0;
  883. }
  884. static int vmlfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  885. {
  886. return -EINVAL; /* just to force soft_cursor() call */
  887. }
  888. static struct fb_ops vmlfb_ops = {
  889. .owner = THIS_MODULE,
  890. .fb_open = vmlfb_open,
  891. .fb_release = vmlfb_release,
  892. .fb_check_var = vmlfb_check_var,
  893. .fb_set_par = vmlfb_set_par,
  894. .fb_blank = vmlfb_blank,
  895. .fb_pan_display = vmlfb_pan_display,
  896. .fb_fillrect = cfb_fillrect,
  897. .fb_copyarea = cfb_copyarea,
  898. .fb_imageblit = cfb_imageblit,
  899. .fb_cursor = vmlfb_cursor,
  900. .fb_sync = vmlfb_sync,
  901. .fb_mmap = vmlfb_mmap,
  902. .fb_setcolreg = vmlfb_setcolreg
  903. };
  904. static struct pci_device_id vml_ids[] = {
  905. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, VML_DEVICE_VDC)},
  906. {0}
  907. };
  908. static struct pci_driver vmlfb_pci_driver = {
  909. .name = "vmlfb",
  910. .id_table = vml_ids,
  911. .probe = vml_pci_probe,
  912. .remove = __devexit_p(vml_pci_remove)
  913. };
  914. static void __exit vmlfb_cleanup(void)
  915. {
  916. pci_unregister_driver(&vmlfb_pci_driver);
  917. }
  918. static int __init vmlfb_init(void)
  919. {
  920. #ifndef MODULE
  921. char *option = NULL;
  922. if (fb_get_options(MODULE_NAME, &option))
  923. return -ENODEV;
  924. #endif
  925. printk(KERN_DEBUG MODULE_NAME ": initializing\n");
  926. mutex_init(&vml_mutex);
  927. INIT_LIST_HEAD(&global_no_mode);
  928. INIT_LIST_HEAD(&global_has_mode);
  929. return pci_register_driver(&vmlfb_pci_driver);
  930. }
  931. int vmlfb_register_subsys(struct vml_sys *sys)
  932. {
  933. struct vml_info *entry;
  934. struct list_head *list;
  935. u32 save_activate;
  936. mutex_lock(&vml_mutex);
  937. if (subsys != NULL) {
  938. subsys->restore(subsys);
  939. }
  940. subsys = sys;
  941. subsys->save(subsys);
  942. /*
  943. * We need to restart list traversal for each item, since we
  944. * release the list mutex in the loop.
  945. */
  946. list = global_no_mode.next;
  947. while (list != &global_no_mode) {
  948. list_del_init(list);
  949. entry = list_entry(list, struct vml_info, head);
  950. /*
  951. * First, try the current mode which might not be
  952. * completely validated with respect to the pixel clock.
  953. */
  954. if (!vmlfb_check_var_locked(&entry->info.var, entry)) {
  955. vmlfb_set_par_locked(entry);
  956. list_add_tail(list, &global_has_mode);
  957. } else {
  958. /*
  959. * Didn't work. Try to find another mode,
  960. * that matches this subsys.
  961. */
  962. mutex_unlock(&vml_mutex);
  963. save_activate = entry->info.var.activate;
  964. entry->info.var.bits_per_pixel = 16;
  965. vmlfb_set_pref_pixel_format(&entry->info.var);
  966. if (fb_find_mode(&entry->info.var,
  967. &entry->info,
  968. vml_default_mode, NULL, 0, NULL, 16)) {
  969. entry->info.var.activate |=
  970. FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
  971. fb_set_var(&entry->info, &entry->info.var);
  972. } else {
  973. printk(KERN_ERR MODULE_NAME
  974. ": Sorry. no mode found for this subsys.\n");
  975. }
  976. entry->info.var.activate = save_activate;
  977. mutex_lock(&vml_mutex);
  978. }
  979. vmlfb_blank_locked(entry);
  980. list = global_no_mode.next;
  981. }
  982. mutex_unlock(&vml_mutex);
  983. printk(KERN_DEBUG MODULE_NAME ": Registered %s subsystem.\n",
  984. subsys->name ? subsys->name : "unknown");
  985. return 0;
  986. }
  987. EXPORT_SYMBOL_GPL(vmlfb_register_subsys);
  988. void vmlfb_unregister_subsys(struct vml_sys *sys)
  989. {
  990. struct vml_info *entry, *next;
  991. mutex_lock(&vml_mutex);
  992. if (subsys != sys) {
  993. mutex_unlock(&vml_mutex);
  994. return;
  995. }
  996. subsys->restore(subsys);
  997. subsys = NULL;
  998. list_for_each_entry_safe(entry, next, &global_has_mode, head) {
  999. printk(KERN_DEBUG MODULE_NAME ": subsys disable pipe\n");
  1000. vmlfb_disable_pipe(entry);
  1001. list_del(&entry->head);
  1002. list_add_tail(&entry->head, &global_no_mode);
  1003. }
  1004. mutex_unlock(&vml_mutex);
  1005. }
  1006. EXPORT_SYMBOL_GPL(vmlfb_unregister_subsys);
  1007. module_init(vmlfb_init);
  1008. module_exit(vmlfb_cleanup);
  1009. MODULE_AUTHOR("Tungsten Graphics");
  1010. MODULE_DESCRIPTION("Initialization of the Vermilion display devices");
  1011. MODULE_VERSION("1.0.0");
  1012. MODULE_LICENSE("GPL");