framebuffer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /**************************************************************************
  2. * Copyright (c) 2007-2011, Intel Corporation.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. **************************************************************************/
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/tty.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/fb.h>
  28. #include <linux/init.h>
  29. #include <linux/console.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm.h>
  32. #include <drm/drm_crtc.h>
  33. #include <drm/drm_fb_helper.h>
  34. #include "psb_drv.h"
  35. #include "psb_intel_reg.h"
  36. #include "psb_intel_drv.h"
  37. #include "framebuffer.h"
  38. #include "gtt.h"
  39. static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
  40. static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  41. struct drm_file *file_priv,
  42. unsigned int *handle);
  43. static const struct drm_framebuffer_funcs psb_fb_funcs = {
  44. .destroy = psb_user_framebuffer_destroy,
  45. .create_handle = psb_user_framebuffer_create_handle,
  46. };
  47. #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
  48. static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  49. unsigned blue, unsigned transp,
  50. struct fb_info *info)
  51. {
  52. struct psb_fbdev *fbdev = info->par;
  53. struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
  54. uint32_t v;
  55. if (!fb)
  56. return -ENOMEM;
  57. if (regno > 255)
  58. return 1;
  59. red = CMAP_TOHW(red, info->var.red.length);
  60. blue = CMAP_TOHW(blue, info->var.blue.length);
  61. green = CMAP_TOHW(green, info->var.green.length);
  62. transp = CMAP_TOHW(transp, info->var.transp.length);
  63. v = (red << info->var.red.offset) |
  64. (green << info->var.green.offset) |
  65. (blue << info->var.blue.offset) |
  66. (transp << info->var.transp.offset);
  67. if (regno < 16) {
  68. switch (fb->bits_per_pixel) {
  69. case 16:
  70. ((uint32_t *) info->pseudo_palette)[regno] = v;
  71. break;
  72. case 24:
  73. case 32:
  74. ((uint32_t *) info->pseudo_palette)[regno] = v;
  75. break;
  76. }
  77. }
  78. return 0;
  79. }
  80. static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
  81. {
  82. struct psb_fbdev *fbdev = info->par;
  83. struct psb_framebuffer *psbfb = &fbdev->pfb;
  84. struct drm_device *dev = psbfb->base.dev;
  85. /*
  86. * We have to poke our nose in here. The core fb code assumes
  87. * panning is part of the hardware that can be invoked before
  88. * the actual fb is mapped. In our case that isn't quite true.
  89. */
  90. if (psbfb->gtt->npage) {
  91. /* GTT roll shifts in 4K pages, we need to shift the right
  92. number of pages */
  93. int pages = info->fix.line_length >> 12;
  94. psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages);
  95. }
  96. return 0;
  97. }
  98. static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  99. {
  100. struct psb_framebuffer *psbfb = vma->vm_private_data;
  101. struct drm_device *dev = psbfb->base.dev;
  102. struct drm_psb_private *dev_priv = dev->dev_private;
  103. int page_num;
  104. int i;
  105. unsigned long address;
  106. int ret;
  107. unsigned long pfn;
  108. unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
  109. psbfb->gtt->offset;
  110. page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  111. address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT);
  112. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  113. for (i = 0; i < page_num; i++) {
  114. pfn = (phys_addr >> PAGE_SHIFT);
  115. ret = vm_insert_mixed(vma, address, pfn);
  116. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  117. break;
  118. else if (unlikely(ret != 0)) {
  119. ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  120. return ret;
  121. }
  122. address += PAGE_SIZE;
  123. phys_addr += PAGE_SIZE;
  124. }
  125. return VM_FAULT_NOPAGE;
  126. }
  127. static void psbfb_vm_open(struct vm_area_struct *vma)
  128. {
  129. }
  130. static void psbfb_vm_close(struct vm_area_struct *vma)
  131. {
  132. }
  133. static const struct vm_operations_struct psbfb_vm_ops = {
  134. .fault = psbfb_vm_fault,
  135. .open = psbfb_vm_open,
  136. .close = psbfb_vm_close
  137. };
  138. static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  139. {
  140. struct psb_fbdev *fbdev = info->par;
  141. struct psb_framebuffer *psbfb = &fbdev->pfb;
  142. if (vma->vm_pgoff != 0)
  143. return -EINVAL;
  144. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  145. return -EINVAL;
  146. if (!psbfb->addr_space)
  147. psbfb->addr_space = vma->vm_file->f_mapping;
  148. /*
  149. * If this is a GEM object then info->screen_base is the virtual
  150. * kernel remapping of the object. FIXME: Review if this is
  151. * suitable for our mmap work
  152. */
  153. vma->vm_ops = &psbfb_vm_ops;
  154. vma->vm_private_data = (void *)psbfb;
  155. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
  156. return 0;
  157. }
  158. static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
  159. unsigned long arg)
  160. {
  161. return -ENOTTY;
  162. }
  163. static struct fb_ops psbfb_ops = {
  164. .owner = THIS_MODULE,
  165. .fb_check_var = drm_fb_helper_check_var,
  166. .fb_set_par = drm_fb_helper_set_par,
  167. .fb_blank = drm_fb_helper_blank,
  168. .fb_setcolreg = psbfb_setcolreg,
  169. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  170. .fb_copyarea = psbfb_copyarea,
  171. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  172. .fb_mmap = psbfb_mmap,
  173. .fb_sync = psbfb_sync,
  174. .fb_ioctl = psbfb_ioctl,
  175. };
  176. static struct fb_ops psbfb_roll_ops = {
  177. .owner = THIS_MODULE,
  178. .fb_check_var = drm_fb_helper_check_var,
  179. .fb_set_par = drm_fb_helper_set_par,
  180. .fb_blank = drm_fb_helper_blank,
  181. .fb_setcolreg = psbfb_setcolreg,
  182. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  183. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  184. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  185. .fb_pan_display = psbfb_pan,
  186. .fb_mmap = psbfb_mmap,
  187. .fb_ioctl = psbfb_ioctl,
  188. };
  189. static struct fb_ops psbfb_unaccel_ops = {
  190. .owner = THIS_MODULE,
  191. .fb_check_var = drm_fb_helper_check_var,
  192. .fb_set_par = drm_fb_helper_set_par,
  193. .fb_blank = drm_fb_helper_blank,
  194. .fb_setcolreg = psbfb_setcolreg,
  195. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  196. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  197. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  198. .fb_mmap = psbfb_mmap,
  199. .fb_ioctl = psbfb_ioctl,
  200. };
  201. /**
  202. * psb_framebuffer_init - initialize a framebuffer
  203. * @dev: our DRM device
  204. * @fb: framebuffer to set up
  205. * @mode_cmd: mode description
  206. * @gt: backing object
  207. *
  208. * Configure and fill in the boilerplate for our frame buffer. Return
  209. * 0 on success or an error code if we fail.
  210. */
  211. static int psb_framebuffer_init(struct drm_device *dev,
  212. struct psb_framebuffer *fb,
  213. struct drm_mode_fb_cmd2 *mode_cmd,
  214. struct gtt_range *gt)
  215. {
  216. u32 bpp, depth;
  217. int ret;
  218. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  219. if (mode_cmd->pitches[0] & 63)
  220. return -EINVAL;
  221. switch (bpp) {
  222. case 8:
  223. case 16:
  224. case 24:
  225. case 32:
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
  231. fb->gtt = gt;
  232. ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
  233. if (ret) {
  234. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  235. return ret;
  236. }
  237. return 0;
  238. }
  239. /**
  240. * psb_framebuffer_create - create a framebuffer backed by gt
  241. * @dev: our DRM device
  242. * @mode_cmd: the description of the requested mode
  243. * @gt: the backing object
  244. *
  245. * Create a framebuffer object backed by the gt, and fill in the
  246. * boilerplate required
  247. *
  248. * TODO: review object references
  249. */
  250. static struct drm_framebuffer *psb_framebuffer_create
  251. (struct drm_device *dev,
  252. struct drm_mode_fb_cmd2 *mode_cmd,
  253. struct gtt_range *gt)
  254. {
  255. struct psb_framebuffer *fb;
  256. int ret;
  257. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  258. if (!fb)
  259. return ERR_PTR(-ENOMEM);
  260. ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
  261. if (ret) {
  262. kfree(fb);
  263. return ERR_PTR(ret);
  264. }
  265. return &fb->base;
  266. }
  267. /**
  268. * psbfb_alloc - allocate frame buffer memory
  269. * @dev: the DRM device
  270. * @aligned_size: space needed
  271. * @force: fall back to GEM buffers if need be
  272. *
  273. * Allocate the frame buffer. In the usual case we get a GTT range that
  274. * is stolen memory backed and life is simple. If there isn't sufficient
  275. * we fail as we don't have the virtual mapping space to really vmap it
  276. * and the kernel console code can't handle non linear framebuffers.
  277. *
  278. * Re-address this as and if the framebuffer layer grows this ability.
  279. */
  280. static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
  281. {
  282. struct gtt_range *backing;
  283. /* Begin by trying to use stolen memory backing */
  284. backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
  285. if (backing) {
  286. drm_gem_private_object_init(dev, &backing->gem, aligned_size);
  287. return backing;
  288. }
  289. return NULL;
  290. }
  291. /**
  292. * psbfb_create - create a framebuffer
  293. * @fbdev: the framebuffer device
  294. * @sizes: specification of the layout
  295. *
  296. * Create a framebuffer to the specifications provided
  297. */
  298. static int psbfb_create(struct psb_fbdev *fbdev,
  299. struct drm_fb_helper_surface_size *sizes)
  300. {
  301. struct drm_device *dev = fbdev->psb_fb_helper.dev;
  302. struct drm_psb_private *dev_priv = dev->dev_private;
  303. struct fb_info *info;
  304. struct drm_framebuffer *fb;
  305. struct psb_framebuffer *psbfb = &fbdev->pfb;
  306. struct drm_mode_fb_cmd2 mode_cmd;
  307. int size;
  308. int ret;
  309. struct gtt_range *backing;
  310. u32 bpp, depth;
  311. int gtt_roll = 0;
  312. int pitch_lines = 0;
  313. mode_cmd.width = sizes->surface_width;
  314. mode_cmd.height = sizes->surface_height;
  315. bpp = sizes->surface_bpp;
  316. depth = sizes->surface_depth;
  317. /* No 24bit packed */
  318. if (bpp == 24)
  319. bpp = 32;
  320. do {
  321. /*
  322. * Acceleration via the GTT requires pitch to be
  323. * power of two aligned. Preferably page but less
  324. * is ok with some fonts
  325. */
  326. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
  327. size = mode_cmd.pitches[0] * mode_cmd.height;
  328. size = ALIGN(size, PAGE_SIZE);
  329. /* Allocate the fb in the GTT with stolen page backing */
  330. backing = psbfb_alloc(dev, size);
  331. if (pitch_lines)
  332. pitch_lines *= 2;
  333. else
  334. pitch_lines = 1;
  335. gtt_roll++;
  336. } while (backing == NULL && pitch_lines <= 16);
  337. /* The final pitch we accepted if we succeeded */
  338. pitch_lines /= 2;
  339. if (backing == NULL) {
  340. /*
  341. * We couldn't get the space we wanted, fall back to the
  342. * display engine requirement instead. The HW requires
  343. * the pitch to be 64 byte aligned
  344. */
  345. gtt_roll = 0; /* Don't use GTT accelerated scrolling */
  346. pitch_lines = 64;
  347. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
  348. size = mode_cmd.pitches[0] * mode_cmd.height;
  349. size = ALIGN(size, PAGE_SIZE);
  350. /* Allocate the framebuffer in the GTT with stolen page backing */
  351. backing = psbfb_alloc(dev, size);
  352. if (backing == NULL)
  353. return -ENOMEM;
  354. }
  355. memset(dev_priv->vram_addr + backing->offset, 0, size);
  356. mutex_lock(&dev->struct_mutex);
  357. info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
  358. if (IS_ERR(info)) {
  359. ret = PTR_ERR(info);
  360. goto out_err1;
  361. }
  362. info->par = fbdev;
  363. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  364. ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
  365. if (ret)
  366. goto out_unref;
  367. fb = &psbfb->base;
  368. psbfb->fbdev = info;
  369. fbdev->psb_fb_helper.fb = fb;
  370. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  371. strcpy(info->fix.id, "psbdrmfb");
  372. info->flags = FBINFO_DEFAULT;
  373. if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
  374. info->fbops = &psbfb_ops;
  375. else if (gtt_roll) { /* GTT rolling seems best */
  376. info->fbops = &psbfb_roll_ops;
  377. info->flags |= FBINFO_HWACCEL_YPAN;
  378. } else /* Software */
  379. info->fbops = &psbfb_unaccel_ops;
  380. info->fix.smem_start = dev->mode_config.fb_base;
  381. info->fix.smem_len = size;
  382. info->fix.ywrapstep = gtt_roll;
  383. info->fix.ypanstep = 0;
  384. /* Accessed stolen memory directly */
  385. info->screen_base = dev_priv->vram_addr + backing->offset;
  386. info->screen_size = size;
  387. if (dev_priv->gtt.stolen_size) {
  388. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  389. info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
  390. }
  391. drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
  392. sizes->fb_width, sizes->fb_height);
  393. info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
  394. info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
  395. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  396. dev_dbg(dev->dev, "allocated %dx%d fb\n",
  397. psbfb->base.width, psbfb->base.height);
  398. mutex_unlock(&dev->struct_mutex);
  399. return 0;
  400. out_unref:
  401. if (backing->stolen)
  402. psb_gtt_free_range(dev, backing);
  403. else
  404. drm_gem_object_unreference(&backing->gem);
  405. drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
  406. out_err1:
  407. mutex_unlock(&dev->struct_mutex);
  408. psb_gtt_free_range(dev, backing);
  409. return ret;
  410. }
  411. /**
  412. * psb_user_framebuffer_create - create framebuffer
  413. * @dev: our DRM device
  414. * @filp: client file
  415. * @cmd: mode request
  416. *
  417. * Create a new framebuffer backed by a userspace GEM object
  418. */
  419. static struct drm_framebuffer *psb_user_framebuffer_create
  420. (struct drm_device *dev, struct drm_file *filp,
  421. struct drm_mode_fb_cmd2 *cmd)
  422. {
  423. struct gtt_range *r;
  424. struct drm_gem_object *obj;
  425. /*
  426. * Find the GEM object and thus the gtt range object that is
  427. * to back this space
  428. */
  429. obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
  430. if (obj == NULL)
  431. return ERR_PTR(-ENOENT);
  432. /* Let the core code do all the work */
  433. r = container_of(obj, struct gtt_range, gem);
  434. return psb_framebuffer_create(dev, cmd, r);
  435. }
  436. static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  437. u16 blue, int regno)
  438. {
  439. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  440. gma_crtc->lut_r[regno] = red >> 8;
  441. gma_crtc->lut_g[regno] = green >> 8;
  442. gma_crtc->lut_b[regno] = blue >> 8;
  443. }
  444. static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
  445. u16 *green, u16 *blue, int regno)
  446. {
  447. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  448. *red = gma_crtc->lut_r[regno] << 8;
  449. *green = gma_crtc->lut_g[regno] << 8;
  450. *blue = gma_crtc->lut_b[regno] << 8;
  451. }
  452. static int psbfb_probe(struct drm_fb_helper *helper,
  453. struct drm_fb_helper_surface_size *sizes)
  454. {
  455. struct psb_fbdev *psb_fbdev =
  456. container_of(helper, struct psb_fbdev, psb_fb_helper);
  457. struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
  458. struct drm_psb_private *dev_priv = dev->dev_private;
  459. int bytespp;
  460. bytespp = sizes->surface_bpp / 8;
  461. if (bytespp == 3) /* no 24bit packed */
  462. bytespp = 4;
  463. /* If the mode will not fit in 32bit then switch to 16bit to get
  464. a console on full resolution. The X mode setting server will
  465. allocate its own 32bit GEM framebuffer */
  466. if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
  467. dev_priv->vram_stolen_size) {
  468. sizes->surface_bpp = 16;
  469. sizes->surface_depth = 16;
  470. }
  471. return psbfb_create(psb_fbdev, sizes);
  472. }
  473. static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
  474. .gamma_set = psbfb_gamma_set,
  475. .gamma_get = psbfb_gamma_get,
  476. .fb_probe = psbfb_probe,
  477. };
  478. static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
  479. {
  480. struct psb_framebuffer *psbfb = &fbdev->pfb;
  481. drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
  482. drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
  483. drm_fb_helper_fini(&fbdev->psb_fb_helper);
  484. drm_framebuffer_unregister_private(&psbfb->base);
  485. drm_framebuffer_cleanup(&psbfb->base);
  486. if (psbfb->gtt)
  487. drm_gem_object_unreference(&psbfb->gtt->gem);
  488. return 0;
  489. }
  490. int psb_fbdev_init(struct drm_device *dev)
  491. {
  492. struct psb_fbdev *fbdev;
  493. struct drm_psb_private *dev_priv = dev->dev_private;
  494. int ret;
  495. fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
  496. if (!fbdev) {
  497. dev_err(dev->dev, "no memory\n");
  498. return -ENOMEM;
  499. }
  500. dev_priv->fbdev = fbdev;
  501. drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
  502. ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
  503. dev_priv->ops->crtcs, INTELFB_CONN_LIMIT);
  504. if (ret)
  505. goto free;
  506. ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
  507. if (ret)
  508. goto fini;
  509. /* disable all the possible outputs/crtcs before entering KMS mode */
  510. drm_helper_disable_unused_functions(dev);
  511. ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
  512. if (ret)
  513. goto fini;
  514. return 0;
  515. fini:
  516. drm_fb_helper_fini(&fbdev->psb_fb_helper);
  517. free:
  518. kfree(fbdev);
  519. return ret;
  520. }
  521. static void psb_fbdev_fini(struct drm_device *dev)
  522. {
  523. struct drm_psb_private *dev_priv = dev->dev_private;
  524. if (!dev_priv->fbdev)
  525. return;
  526. psb_fbdev_destroy(dev, dev_priv->fbdev);
  527. kfree(dev_priv->fbdev);
  528. dev_priv->fbdev = NULL;
  529. }
  530. static void psbfb_output_poll_changed(struct drm_device *dev)
  531. {
  532. struct drm_psb_private *dev_priv = dev->dev_private;
  533. struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
  534. drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
  535. }
  536. /**
  537. * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
  538. * @fb: framebuffer
  539. * @file_priv: our DRM file
  540. * @handle: returned handle
  541. *
  542. * Our framebuffer object is a GTT range which also contains a GEM
  543. * object. We need to turn it into a handle for userspace. GEM will do
  544. * the work for us
  545. */
  546. static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  547. struct drm_file *file_priv,
  548. unsigned int *handle)
  549. {
  550. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  551. struct gtt_range *r = psbfb->gtt;
  552. return drm_gem_handle_create(file_priv, &r->gem, handle);
  553. }
  554. /**
  555. * psb_user_framebuffer_destroy - destruct user created fb
  556. * @fb: framebuffer
  557. *
  558. * User framebuffers are backed by GEM objects so all we have to do is
  559. * clean up a bit and drop the reference, GEM will handle the fallout
  560. */
  561. static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
  562. {
  563. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  564. struct gtt_range *r = psbfb->gtt;
  565. /* Let DRM do its clean up */
  566. drm_framebuffer_cleanup(fb);
  567. /* We are no longer using the resource in GEM */
  568. drm_gem_object_unreference_unlocked(&r->gem);
  569. kfree(fb);
  570. }
  571. static const struct drm_mode_config_funcs psb_mode_funcs = {
  572. .fb_create = psb_user_framebuffer_create,
  573. .output_poll_changed = psbfb_output_poll_changed,
  574. };
  575. static int psb_create_backlight_property(struct drm_device *dev)
  576. {
  577. struct drm_psb_private *dev_priv = dev->dev_private;
  578. struct drm_property *backlight;
  579. if (dev_priv->backlight_property)
  580. return 0;
  581. backlight = drm_property_create_range(dev, 0, "backlight", 0, 100);
  582. dev_priv->backlight_property = backlight;
  583. return 0;
  584. }
  585. static void psb_setup_outputs(struct drm_device *dev)
  586. {
  587. struct drm_psb_private *dev_priv = dev->dev_private;
  588. struct drm_connector *connector;
  589. drm_mode_create_scaling_mode_property(dev);
  590. psb_create_backlight_property(dev);
  591. dev_priv->ops->output_init(dev);
  592. list_for_each_entry(connector, &dev->mode_config.connector_list,
  593. head) {
  594. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  595. struct drm_encoder *encoder = &gma_encoder->base;
  596. int crtc_mask = 0, clone_mask = 0;
  597. /* valid crtcs */
  598. switch (gma_encoder->type) {
  599. case INTEL_OUTPUT_ANALOG:
  600. crtc_mask = (1 << 0);
  601. clone_mask = (1 << INTEL_OUTPUT_ANALOG);
  602. break;
  603. case INTEL_OUTPUT_SDVO:
  604. crtc_mask = dev_priv->ops->sdvo_mask;
  605. clone_mask = (1 << INTEL_OUTPUT_SDVO);
  606. break;
  607. case INTEL_OUTPUT_LVDS:
  608. crtc_mask = dev_priv->ops->lvds_mask;
  609. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  610. break;
  611. case INTEL_OUTPUT_MIPI:
  612. crtc_mask = (1 << 0);
  613. clone_mask = (1 << INTEL_OUTPUT_MIPI);
  614. break;
  615. case INTEL_OUTPUT_MIPI2:
  616. crtc_mask = (1 << 2);
  617. clone_mask = (1 << INTEL_OUTPUT_MIPI2);
  618. break;
  619. case INTEL_OUTPUT_HDMI:
  620. crtc_mask = dev_priv->ops->hdmi_mask;
  621. clone_mask = (1 << INTEL_OUTPUT_HDMI);
  622. break;
  623. case INTEL_OUTPUT_DISPLAYPORT:
  624. crtc_mask = (1 << 0) | (1 << 1);
  625. clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
  626. break;
  627. case INTEL_OUTPUT_EDP:
  628. crtc_mask = (1 << 1);
  629. clone_mask = (1 << INTEL_OUTPUT_EDP);
  630. }
  631. encoder->possible_crtcs = crtc_mask;
  632. encoder->possible_clones =
  633. gma_connector_clones(dev, clone_mask);
  634. }
  635. }
  636. void psb_modeset_init(struct drm_device *dev)
  637. {
  638. struct drm_psb_private *dev_priv = dev->dev_private;
  639. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  640. int i;
  641. drm_mode_config_init(dev);
  642. dev->mode_config.min_width = 0;
  643. dev->mode_config.min_height = 0;
  644. dev->mode_config.funcs = &psb_mode_funcs;
  645. /* set memory base */
  646. /* Oaktrail and Poulsbo should use BAR 2*/
  647. pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
  648. &(dev->mode_config.fb_base));
  649. /* num pipes is 2 for PSB but 1 for Mrst */
  650. for (i = 0; i < dev_priv->num_pipe; i++)
  651. psb_intel_crtc_init(dev, i, mode_dev);
  652. dev->mode_config.max_width = 4096;
  653. dev->mode_config.max_height = 4096;
  654. psb_setup_outputs(dev);
  655. if (dev_priv->ops->errata)
  656. dev_priv->ops->errata(dev);
  657. dev_priv->modeset = true;
  658. }
  659. void psb_modeset_cleanup(struct drm_device *dev)
  660. {
  661. struct drm_psb_private *dev_priv = dev->dev_private;
  662. if (dev_priv->modeset) {
  663. mutex_lock(&dev->struct_mutex);
  664. drm_kms_helper_poll_fini(dev);
  665. psb_fbdev_fini(dev);
  666. drm_mode_config_cleanup(dev);
  667. mutex_unlock(&dev->struct_mutex);
  668. }
  669. }