framebuffer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. /* FIXME: assumes fb at stolen base which may not be true */
  109. unsigned long phys_addr = (unsigned long)dev_priv->stolen_base;
  110. page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  111. address = (unsigned long)vmf->virtual_address;
  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 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_RESERVED | VM_IO |
  156. VM_MIXEDMAP | VM_DONTEXPAND;
  157. return 0;
  158. }
  159. static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
  160. unsigned long arg)
  161. {
  162. return -ENOTTY;
  163. }
  164. static struct fb_ops psbfb_ops = {
  165. .owner = THIS_MODULE,
  166. .fb_check_var = drm_fb_helper_check_var,
  167. .fb_set_par = drm_fb_helper_set_par,
  168. .fb_blank = drm_fb_helper_blank,
  169. .fb_setcolreg = psbfb_setcolreg,
  170. .fb_fillrect = cfb_fillrect,
  171. .fb_copyarea = psbfb_copyarea,
  172. .fb_imageblit = cfb_imageblit,
  173. .fb_mmap = psbfb_mmap,
  174. .fb_sync = psbfb_sync,
  175. .fb_ioctl = psbfb_ioctl,
  176. };
  177. static struct fb_ops psbfb_roll_ops = {
  178. .owner = THIS_MODULE,
  179. .fb_check_var = drm_fb_helper_check_var,
  180. .fb_set_par = drm_fb_helper_set_par,
  181. .fb_blank = drm_fb_helper_blank,
  182. .fb_setcolreg = psbfb_setcolreg,
  183. .fb_fillrect = cfb_fillrect,
  184. .fb_copyarea = cfb_copyarea,
  185. .fb_imageblit = cfb_imageblit,
  186. .fb_pan_display = psbfb_pan,
  187. .fb_mmap = psbfb_mmap,
  188. .fb_sync = psbfb_sync,
  189. .fb_ioctl = psbfb_ioctl,
  190. };
  191. static struct fb_ops psbfb_unaccel_ops = {
  192. .owner = THIS_MODULE,
  193. .fb_check_var = drm_fb_helper_check_var,
  194. .fb_set_par = drm_fb_helper_set_par,
  195. .fb_blank = drm_fb_helper_blank,
  196. .fb_setcolreg = psbfb_setcolreg,
  197. .fb_fillrect = cfb_fillrect,
  198. .fb_copyarea = cfb_copyarea,
  199. .fb_imageblit = cfb_imageblit,
  200. .fb_mmap = psbfb_mmap,
  201. .fb_ioctl = psbfb_ioctl,
  202. };
  203. /**
  204. * psb_framebuffer_init - initialize a framebuffer
  205. * @dev: our DRM device
  206. * @fb: framebuffer to set up
  207. * @mode_cmd: mode description
  208. * @gt: backing object
  209. *
  210. * Configure and fill in the boilerplate for our frame buffer. Return
  211. * 0 on success or an error code if we fail.
  212. */
  213. static int psb_framebuffer_init(struct drm_device *dev,
  214. struct psb_framebuffer *fb,
  215. struct drm_mode_fb_cmd2 *mode_cmd,
  216. struct gtt_range *gt)
  217. {
  218. u32 bpp, depth;
  219. int ret;
  220. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  221. if (mode_cmd->pitches[0] & 63)
  222. return -EINVAL;
  223. switch (bpp) {
  224. case 8:
  225. case 16:
  226. case 24:
  227. case 32:
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  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. drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
  238. fb->gtt = gt;
  239. return 0;
  240. }
  241. /**
  242. * psb_framebuffer_create - create a framebuffer backed by gt
  243. * @dev: our DRM device
  244. * @mode_cmd: the description of the requested mode
  245. * @gt: the backing object
  246. *
  247. * Create a framebuffer object backed by the gt, and fill in the
  248. * boilerplate required
  249. *
  250. * TODO: review object references
  251. */
  252. static struct drm_framebuffer *psb_framebuffer_create
  253. (struct drm_device *dev,
  254. struct drm_mode_fb_cmd2 *mode_cmd,
  255. struct gtt_range *gt)
  256. {
  257. struct psb_framebuffer *fb;
  258. int ret;
  259. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  260. if (!fb)
  261. return ERR_PTR(-ENOMEM);
  262. ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
  263. if (ret) {
  264. kfree(fb);
  265. return ERR_PTR(ret);
  266. }
  267. return &fb->base;
  268. }
  269. /**
  270. * psbfb_alloc - allocate frame buffer memory
  271. * @dev: the DRM device
  272. * @aligned_size: space needed
  273. * @force: fall back to GEM buffers if need be
  274. *
  275. * Allocate the frame buffer. In the usual case we get a GTT range that
  276. * is stolen memory backed and life is simple. If there isn't sufficient
  277. * we fail as we don't have the virtual mapping space to really vmap it
  278. * and the kernel console code can't handle non linear framebuffers.
  279. *
  280. * Re-address this as and if the framebuffer layer grows this ability.
  281. */
  282. static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
  283. {
  284. struct gtt_range *backing;
  285. /* Begin by trying to use stolen memory backing */
  286. backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1);
  287. if (backing) {
  288. if (drm_gem_private_object_init(dev,
  289. &backing->gem, aligned_size) == 0)
  290. return backing;
  291. psb_gtt_free_range(dev, backing);
  292. }
  293. return NULL;
  294. }
  295. /**
  296. * psbfb_create - create a framebuffer
  297. * @fbdev: the framebuffer device
  298. * @sizes: specification of the layout
  299. *
  300. * Create a framebuffer to the specifications provided
  301. */
  302. static int psbfb_create(struct psb_fbdev *fbdev,
  303. struct drm_fb_helper_surface_size *sizes)
  304. {
  305. struct drm_device *dev = fbdev->psb_fb_helper.dev;
  306. struct drm_psb_private *dev_priv = dev->dev_private;
  307. struct fb_info *info;
  308. struct drm_framebuffer *fb;
  309. struct psb_framebuffer *psbfb = &fbdev->pfb;
  310. struct drm_mode_fb_cmd2 mode_cmd;
  311. struct device *device = &dev->pdev->dev;
  312. int size;
  313. int ret;
  314. struct gtt_range *backing;
  315. u32 bpp, depth;
  316. int gtt_roll = 0;
  317. int pitch_lines = 0;
  318. mode_cmd.width = sizes->surface_width;
  319. mode_cmd.height = sizes->surface_height;
  320. bpp = sizes->surface_bpp;
  321. depth = sizes->surface_depth;
  322. /* No 24bit packed */
  323. if (bpp == 24)
  324. bpp = 32;
  325. do {
  326. /*
  327. * Acceleration via the GTT requires pitch to be
  328. * power of two aligned. Preferably page but less
  329. * is ok with some fonts
  330. */
  331. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
  332. size = mode_cmd.pitches[0] * mode_cmd.height;
  333. size = ALIGN(size, PAGE_SIZE);
  334. /* Allocate the fb in the GTT with stolen page backing */
  335. backing = psbfb_alloc(dev, size);
  336. if (pitch_lines)
  337. pitch_lines *= 2;
  338. else
  339. pitch_lines = 1;
  340. gtt_roll++;
  341. } while (backing == NULL && pitch_lines <= 16);
  342. /* The final pitch we accepted if we succeeded */
  343. pitch_lines /= 2;
  344. if (backing == NULL) {
  345. /*
  346. * We couldn't get the space we wanted, fall back to the
  347. * display engine requirement instead. The HW requires
  348. * the pitch to be 64 byte aligned
  349. */
  350. gtt_roll = 0; /* Don't use GTT accelerated scrolling */
  351. pitch_lines = 64;
  352. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
  353. size = mode_cmd.pitches[0] * mode_cmd.height;
  354. size = ALIGN(size, PAGE_SIZE);
  355. /* Allocate the framebuffer in the GTT with stolen page backing */
  356. backing = psbfb_alloc(dev, size);
  357. if (backing == NULL)
  358. return -ENOMEM;
  359. }
  360. mutex_lock(&dev->struct_mutex);
  361. info = framebuffer_alloc(0, device);
  362. if (!info) {
  363. ret = -ENOMEM;
  364. goto out_err1;
  365. }
  366. info->par = fbdev;
  367. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  368. ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
  369. if (ret)
  370. goto out_unref;
  371. fb = &psbfb->base;
  372. psbfb->fbdev = info;
  373. fbdev->psb_fb_helper.fb = fb;
  374. fbdev->psb_fb_helper.fbdev = info;
  375. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  376. strcpy(info->fix.id, "psbfb");
  377. info->flags = FBINFO_DEFAULT;
  378. if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
  379. info->fbops = &psbfb_ops;
  380. else if (gtt_roll) { /* GTT rolling seems best */
  381. info->fbops = &psbfb_roll_ops;
  382. info->flags |= FBINFO_HWACCEL_YPAN;
  383. } else /* Software */
  384. info->fbops = &psbfb_unaccel_ops;
  385. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  386. if (ret) {
  387. ret = -ENOMEM;
  388. goto out_unref;
  389. }
  390. info->fix.smem_start = dev->mode_config.fb_base;
  391. info->fix.smem_len = size;
  392. info->fix.ywrapstep = gtt_roll;
  393. info->fix.ypanstep = 0;
  394. /* Accessed stolen memory directly */
  395. info->screen_base = (char *)dev_priv->vram_addr +
  396. backing->offset;
  397. info->screen_size = size;
  398. if (dev_priv->gtt.stolen_size) {
  399. info->apertures = alloc_apertures(1);
  400. if (!info->apertures) {
  401. ret = -ENOMEM;
  402. goto out_unref;
  403. }
  404. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  405. info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
  406. }
  407. drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
  408. sizes->fb_width, sizes->fb_height);
  409. info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
  410. info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
  411. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  412. dev_info(dev->dev, "allocated %dx%d fb\n",
  413. psbfb->base.width, psbfb->base.height);
  414. mutex_unlock(&dev->struct_mutex);
  415. return 0;
  416. out_unref:
  417. if (backing->stolen)
  418. psb_gtt_free_range(dev, backing);
  419. else
  420. drm_gem_object_unreference(&backing->gem);
  421. out_err1:
  422. mutex_unlock(&dev->struct_mutex);
  423. psb_gtt_free_range(dev, backing);
  424. return ret;
  425. }
  426. /**
  427. * psb_user_framebuffer_create - create framebuffer
  428. * @dev: our DRM device
  429. * @filp: client file
  430. * @cmd: mode request
  431. *
  432. * Create a new framebuffer backed by a userspace GEM object
  433. */
  434. static struct drm_framebuffer *psb_user_framebuffer_create
  435. (struct drm_device *dev, struct drm_file *filp,
  436. struct drm_mode_fb_cmd2 *cmd)
  437. {
  438. struct gtt_range *r;
  439. struct drm_gem_object *obj;
  440. /*
  441. * Find the GEM object and thus the gtt range object that is
  442. * to back this space
  443. */
  444. obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
  445. if (obj == NULL)
  446. return ERR_PTR(-ENOENT);
  447. /* Let the core code do all the work */
  448. r = container_of(obj, struct gtt_range, gem);
  449. return psb_framebuffer_create(dev, cmd, r);
  450. }
  451. static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  452. u16 blue, int regno)
  453. {
  454. struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
  455. intel_crtc->lut_r[regno] = red >> 8;
  456. intel_crtc->lut_g[regno] = green >> 8;
  457. intel_crtc->lut_b[regno] = blue >> 8;
  458. }
  459. static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
  460. u16 *green, u16 *blue, int regno)
  461. {
  462. struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
  463. *red = intel_crtc->lut_r[regno] << 8;
  464. *green = intel_crtc->lut_g[regno] << 8;
  465. *blue = intel_crtc->lut_b[regno] << 8;
  466. }
  467. static int psbfb_probe(struct drm_fb_helper *helper,
  468. struct drm_fb_helper_surface_size *sizes)
  469. {
  470. struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
  471. int new_fb = 0;
  472. int ret;
  473. if (!helper->fb) {
  474. ret = psbfb_create(psb_fbdev, sizes);
  475. if (ret)
  476. return ret;
  477. new_fb = 1;
  478. }
  479. return new_fb;
  480. }
  481. struct drm_fb_helper_funcs psb_fb_helper_funcs = {
  482. .gamma_set = psbfb_gamma_set,
  483. .gamma_get = psbfb_gamma_get,
  484. .fb_probe = psbfb_probe,
  485. };
  486. static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
  487. {
  488. struct fb_info *info;
  489. struct psb_framebuffer *psbfb = &fbdev->pfb;
  490. if (fbdev->psb_fb_helper.fbdev) {
  491. info = fbdev->psb_fb_helper.fbdev;
  492. unregister_framebuffer(info);
  493. if (info->cmap.len)
  494. fb_dealloc_cmap(&info->cmap);
  495. framebuffer_release(info);
  496. }
  497. drm_fb_helper_fini(&fbdev->psb_fb_helper);
  498. drm_framebuffer_cleanup(&psbfb->base);
  499. if (psbfb->gtt)
  500. drm_gem_object_unreference(&psbfb->gtt->gem);
  501. return 0;
  502. }
  503. int psb_fbdev_init(struct drm_device *dev)
  504. {
  505. struct psb_fbdev *fbdev;
  506. struct drm_psb_private *dev_priv = dev->dev_private;
  507. fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
  508. if (!fbdev) {
  509. dev_err(dev->dev, "no memory\n");
  510. return -ENOMEM;
  511. }
  512. dev_priv->fbdev = fbdev;
  513. fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
  514. drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
  515. INTELFB_CONN_LIMIT);
  516. drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
  517. drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
  518. return 0;
  519. }
  520. static void psb_fbdev_fini(struct drm_device *dev)
  521. {
  522. struct drm_psb_private *dev_priv = dev->dev_private;
  523. if (!dev_priv->fbdev)
  524. return;
  525. psb_fbdev_destroy(dev, dev_priv->fbdev);
  526. kfree(dev_priv->fbdev);
  527. dev_priv->fbdev = NULL;
  528. }
  529. static void psbfb_output_poll_changed(struct drm_device *dev)
  530. {
  531. struct drm_psb_private *dev_priv = dev->dev_private;
  532. struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
  533. drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
  534. }
  535. /**
  536. * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
  537. * @fb: framebuffer
  538. * @file_priv: our DRM file
  539. * @handle: returned handle
  540. *
  541. * Our framebuffer object is a GTT range which also contains a GEM
  542. * object. We need to turn it into a handle for userspace. GEM will do
  543. * the work for us
  544. */
  545. static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  546. struct drm_file *file_priv,
  547. unsigned int *handle)
  548. {
  549. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  550. struct gtt_range *r = psbfb->gtt;
  551. return drm_gem_handle_create(file_priv, &r->gem, handle);
  552. }
  553. /**
  554. * psb_user_framebuffer_destroy - destruct user created fb
  555. * @fb: framebuffer
  556. *
  557. * User framebuffers are backed by GEM objects so all we have to do is
  558. * clean up a bit and drop the reference, GEM will handle the fallout
  559. */
  560. static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
  561. {
  562. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  563. struct gtt_range *r = psbfb->gtt;
  564. struct drm_device *dev = fb->dev;
  565. struct drm_psb_private *dev_priv = dev->dev_private;
  566. struct psb_fbdev *fbdev = dev_priv->fbdev;
  567. struct drm_crtc *crtc;
  568. int reset = 0;
  569. /* Should never get stolen memory for a user fb */
  570. WARN_ON(r->stolen);
  571. /* Check if we are erroneously live */
  572. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  573. if (crtc->fb == fb)
  574. reset = 1;
  575. if (reset)
  576. /*
  577. * Now force a sane response before we permit the DRM CRTC
  578. * layer to do stupid things like blank the display. Instead
  579. * we reset this framebuffer as if the user had forced a reset.
  580. * We must do this before the cleanup so that the DRM layer
  581. * doesn't get a chance to stick its oar in where it isn't
  582. * wanted.
  583. */
  584. drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
  585. /* Let DRM do its clean up */
  586. drm_framebuffer_cleanup(fb);
  587. /* We are no longer using the resource in GEM */
  588. drm_gem_object_unreference_unlocked(&r->gem);
  589. kfree(fb);
  590. }
  591. static const struct drm_mode_config_funcs psb_mode_funcs = {
  592. .fb_create = psb_user_framebuffer_create,
  593. .output_poll_changed = psbfb_output_poll_changed,
  594. };
  595. static int psb_create_backlight_property(struct drm_device *dev)
  596. {
  597. struct drm_psb_private *dev_priv = dev->dev_private;
  598. struct drm_property *backlight;
  599. if (dev_priv->backlight_property)
  600. return 0;
  601. backlight = drm_property_create_range(dev, 0, "backlight", 0, 100);
  602. dev_priv->backlight_property = backlight;
  603. return 0;
  604. }
  605. static void psb_setup_outputs(struct drm_device *dev)
  606. {
  607. struct drm_psb_private *dev_priv = dev->dev_private;
  608. struct drm_connector *connector;
  609. drm_mode_create_scaling_mode_property(dev);
  610. psb_create_backlight_property(dev);
  611. dev_priv->ops->output_init(dev);
  612. list_for_each_entry(connector, &dev->mode_config.connector_list,
  613. head) {
  614. struct psb_intel_encoder *psb_intel_encoder =
  615. psb_intel_attached_encoder(connector);
  616. struct drm_encoder *encoder = &psb_intel_encoder->base;
  617. int crtc_mask = 0, clone_mask = 0;
  618. /* valid crtcs */
  619. switch (psb_intel_encoder->type) {
  620. case INTEL_OUTPUT_ANALOG:
  621. crtc_mask = (1 << 0);
  622. clone_mask = (1 << INTEL_OUTPUT_ANALOG);
  623. break;
  624. case INTEL_OUTPUT_SDVO:
  625. crtc_mask = ((1 << 0) | (1 << 1));
  626. clone_mask = (1 << INTEL_OUTPUT_SDVO);
  627. break;
  628. case INTEL_OUTPUT_LVDS:
  629. if (IS_MRST(dev))
  630. crtc_mask = (1 << 0);
  631. else
  632. crtc_mask = (1 << 1);
  633. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  634. break;
  635. case INTEL_OUTPUT_MIPI:
  636. crtc_mask = (1 << 0);
  637. clone_mask = (1 << INTEL_OUTPUT_MIPI);
  638. break;
  639. case INTEL_OUTPUT_MIPI2:
  640. crtc_mask = (1 << 2);
  641. clone_mask = (1 << INTEL_OUTPUT_MIPI2);
  642. break;
  643. case INTEL_OUTPUT_HDMI:
  644. if (IS_MFLD(dev))
  645. crtc_mask = (1 << 1);
  646. else
  647. crtc_mask = (1 << 0);
  648. clone_mask = (1 << INTEL_OUTPUT_HDMI);
  649. break;
  650. }
  651. encoder->possible_crtcs = crtc_mask;
  652. encoder->possible_clones =
  653. psb_intel_connector_clones(dev, clone_mask);
  654. }
  655. }
  656. void psb_modeset_init(struct drm_device *dev)
  657. {
  658. struct drm_psb_private *dev_priv = dev->dev_private;
  659. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  660. int i;
  661. drm_mode_config_init(dev);
  662. dev->mode_config.min_width = 0;
  663. dev->mode_config.min_height = 0;
  664. dev->mode_config.funcs = (void *) &psb_mode_funcs;
  665. /* set memory base */
  666. /* Oaktrail and Poulsbo should use BAR 2*/
  667. pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
  668. &(dev->mode_config.fb_base));
  669. /* num pipes is 2 for PSB but 1 for Mrst */
  670. for (i = 0; i < dev_priv->num_pipe; i++)
  671. psb_intel_crtc_init(dev, i, mode_dev);
  672. dev->mode_config.max_width = 2048;
  673. dev->mode_config.max_height = 2048;
  674. psb_setup_outputs(dev);
  675. }
  676. void psb_modeset_cleanup(struct drm_device *dev)
  677. {
  678. mutex_lock(&dev->struct_mutex);
  679. drm_kms_helper_poll_fini(dev);
  680. psb_fbdev_fini(dev);
  681. drm_mode_config_cleanup(dev);
  682. mutex_unlock(&dev->struct_mutex);
  683. }