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