vermilion.c 29 KB

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