vermilion.c 29 KB

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