framebuffer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
  39. static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  40. struct drm_file *file_priv,
  41. unsigned int *handle);
  42. static const struct drm_framebuffer_funcs psb_fb_funcs = {
  43. .destroy = psb_user_framebuffer_destroy,
  44. .create_handle = psb_user_framebuffer_create_handle,
  45. };
  46. #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
  47. static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  48. unsigned blue, unsigned transp,
  49. struct fb_info *info)
  50. {
  51. struct psb_fbdev *fbdev = info->par;
  52. struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
  53. uint32_t v;
  54. if (!fb)
  55. return -ENOMEM;
  56. if (regno > 255)
  57. return 1;
  58. red = CMAP_TOHW(red, info->var.red.length);
  59. blue = CMAP_TOHW(blue, info->var.blue.length);
  60. green = CMAP_TOHW(green, info->var.green.length);
  61. transp = CMAP_TOHW(transp, info->var.transp.length);
  62. v = (red << info->var.red.offset) |
  63. (green << info->var.green.offset) |
  64. (blue << info->var.blue.offset) |
  65. (transp << info->var.transp.offset);
  66. if (regno < 16) {
  67. switch (fb->bits_per_pixel) {
  68. case 16:
  69. ((uint32_t *) info->pseudo_palette)[regno] = v;
  70. break;
  71. case 24:
  72. case 32:
  73. ((uint32_t *) info->pseudo_palette)[regno] = v;
  74. break;
  75. }
  76. }
  77. return 0;
  78. }
  79. void psbfb_suspend(struct drm_device *dev)
  80. {
  81. struct drm_framebuffer *fb = 0;
  82. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  83. console_lock();
  84. mutex_lock(&dev->mode_config.mutex);
  85. list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
  86. struct fb_info *info = psbfb->fbdev;
  87. fb_set_suspend(info, 1);
  88. drm_fb_helper_blank(FB_BLANK_POWERDOWN, info);
  89. }
  90. mutex_unlock(&dev->mode_config.mutex);
  91. console_unlock();
  92. }
  93. void psbfb_resume(struct drm_device *dev)
  94. {
  95. struct drm_framebuffer *fb = 0;
  96. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  97. console_lock();
  98. mutex_lock(&dev->mode_config.mutex);
  99. list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
  100. struct fb_info *info = psbfb->fbdev;
  101. fb_set_suspend(info, 0);
  102. drm_fb_helper_blank(FB_BLANK_UNBLANK, info);
  103. }
  104. mutex_unlock(&dev->mode_config.mutex);
  105. console_unlock();
  106. drm_helper_disable_unused_functions(dev);
  107. }
  108. static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  109. {
  110. struct psb_framebuffer *psbfb = vma->vm_private_data;
  111. struct drm_device *dev = psbfb->base.dev;
  112. struct drm_psb_private *dev_priv = dev->dev_private;
  113. int page_num;
  114. int i;
  115. unsigned long address;
  116. int ret;
  117. unsigned long pfn;
  118. /* FIXME: assumes fb at stolen base which may not be true */
  119. unsigned long phys_addr = (unsigned long)dev_priv->stolen_base;
  120. page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  121. address = (unsigned long)vmf->virtual_address;
  122. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  123. for (i = 0; i < page_num; i++) {
  124. pfn = (phys_addr >> PAGE_SHIFT);
  125. ret = vm_insert_mixed(vma, address, pfn);
  126. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  127. break;
  128. else if (unlikely(ret != 0)) {
  129. ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  130. return ret;
  131. }
  132. address += PAGE_SIZE;
  133. phys_addr += PAGE_SIZE;
  134. }
  135. return VM_FAULT_NOPAGE;
  136. }
  137. static void psbfb_vm_open(struct vm_area_struct *vma)
  138. {
  139. }
  140. static void psbfb_vm_close(struct vm_area_struct *vma)
  141. {
  142. }
  143. static struct vm_operations_struct psbfb_vm_ops = {
  144. .fault = psbfb_vm_fault,
  145. .open = psbfb_vm_open,
  146. .close = psbfb_vm_close
  147. };
  148. static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  149. {
  150. struct psb_fbdev *fbdev = info->par;
  151. struct psb_framebuffer *psbfb = &fbdev->pfb;
  152. if (vma->vm_pgoff != 0)
  153. return -EINVAL;
  154. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  155. return -EINVAL;
  156. if (!psbfb->addr_space)
  157. psbfb->addr_space = vma->vm_file->f_mapping;
  158. /*
  159. * If this is a GEM object then info->screen_base is the virtual
  160. * kernel remapping of the object. FIXME: Review if this is
  161. * suitable for our mmap work
  162. */
  163. vma->vm_ops = &psbfb_vm_ops;
  164. vma->vm_private_data = (void *)psbfb;
  165. vma->vm_flags |= VM_RESERVED | VM_IO |
  166. VM_MIXEDMAP | VM_DONTEXPAND;
  167. return 0;
  168. }
  169. static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
  170. unsigned long arg)
  171. {
  172. return -ENOTTY;
  173. }
  174. static struct fb_ops psbfb_ops = {
  175. .owner = THIS_MODULE,
  176. .fb_check_var = drm_fb_helper_check_var,
  177. .fb_set_par = drm_fb_helper_set_par,
  178. .fb_blank = drm_fb_helper_blank,
  179. .fb_setcolreg = psbfb_setcolreg,
  180. .fb_fillrect = cfb_fillrect,
  181. .fb_copyarea = psbfb_copyarea,
  182. .fb_imageblit = cfb_imageblit,
  183. .fb_mmap = psbfb_mmap,
  184. .fb_sync = psbfb_sync,
  185. .fb_ioctl = psbfb_ioctl,
  186. };
  187. static struct fb_ops psbfb_unaccel_ops = {
  188. .owner = THIS_MODULE,
  189. .fb_check_var = drm_fb_helper_check_var,
  190. .fb_set_par = drm_fb_helper_set_par,
  191. .fb_blank = drm_fb_helper_blank,
  192. .fb_setcolreg = psbfb_setcolreg,
  193. .fb_fillrect = cfb_fillrect,
  194. .fb_copyarea = cfb_copyarea,
  195. .fb_imageblit = cfb_imageblit,
  196. .fb_mmap = psbfb_mmap,
  197. .fb_ioctl = psbfb_ioctl,
  198. };
  199. /**
  200. * psb_framebuffer_init - initialize a framebuffer
  201. * @dev: our DRM device
  202. * @fb: framebuffer to set up
  203. * @mode_cmd: mode description
  204. * @gt: backing object
  205. *
  206. * Configure and fill in the boilerplate for our frame buffer. Return
  207. * 0 on success or an error code if we fail.
  208. */
  209. static int psb_framebuffer_init(struct drm_device *dev,
  210. struct psb_framebuffer *fb,
  211. struct drm_mode_fb_cmd2 *mode_cmd,
  212. struct gtt_range *gt)
  213. {
  214. u32 bpp, depth;
  215. int ret;
  216. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  217. if (mode_cmd->pitches[0] & 63)
  218. return -EINVAL;
  219. switch (bpp) {
  220. case 8:
  221. case 16:
  222. case 24:
  223. case 32:
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
  229. if (ret) {
  230. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  231. return ret;
  232. }
  233. drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
  234. fb->gtt = gt;
  235. return 0;
  236. }
  237. /**
  238. * psb_framebuffer_create - create a framebuffer backed by gt
  239. * @dev: our DRM device
  240. * @mode_cmd: the description of the requested mode
  241. * @gt: the backing object
  242. *
  243. * Create a framebuffer object backed by the gt, and fill in the
  244. * boilerplate required
  245. *
  246. * TODO: review object references
  247. */
  248. static struct drm_framebuffer *psb_framebuffer_create
  249. (struct drm_device *dev,
  250. struct drm_mode_fb_cmd2 *mode_cmd,
  251. struct gtt_range *gt)
  252. {
  253. struct psb_framebuffer *fb;
  254. int ret;
  255. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  256. if (!fb)
  257. return ERR_PTR(-ENOMEM);
  258. ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
  259. if (ret) {
  260. kfree(fb);
  261. return ERR_PTR(ret);
  262. }
  263. return &fb->base;
  264. }
  265. /**
  266. * psbfb_alloc - allocate frame buffer memory
  267. * @dev: the DRM device
  268. * @aligned_size: space needed
  269. *
  270. * Allocate the frame buffer. In the usual case we get a GTT range that
  271. * is stolen memory backed and life is simple. If there isn't sufficient
  272. * we fail as we don't have the virtual mapping space to really vmap it
  273. * and the kernel console code can't handle non linear framebuffers.
  274. *
  275. * Re-address this as and if the framebuffer layer grows this ability.
  276. */
  277. static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
  278. {
  279. struct gtt_range *backing;
  280. /* Begin by trying to use stolen memory backing */
  281. backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1);
  282. if (backing) {
  283. if (drm_gem_private_object_init(dev,
  284. &backing->gem, aligned_size) == 0)
  285. return backing;
  286. psb_gtt_free_range(dev, backing);
  287. }
  288. return NULL;
  289. }
  290. /**
  291. * psbfb_create - create a framebuffer
  292. * @fbdev: the framebuffer device
  293. * @sizes: specification of the layout
  294. *
  295. * Create a framebuffer to the specifications provided
  296. */
  297. static int psbfb_create(struct psb_fbdev *fbdev,
  298. struct drm_fb_helper_surface_size *sizes)
  299. {
  300. struct drm_device *dev = fbdev->psb_fb_helper.dev;
  301. struct drm_psb_private *dev_priv = dev->dev_private;
  302. struct fb_info *info;
  303. struct drm_framebuffer *fb;
  304. struct psb_framebuffer *psbfb = &fbdev->pfb;
  305. struct drm_mode_fb_cmd2 mode_cmd;
  306. struct device *device = &dev->pdev->dev;
  307. int size;
  308. int ret;
  309. struct gtt_range *backing;
  310. u32 bpp, depth;
  311. mode_cmd.width = sizes->surface_width;
  312. mode_cmd.height = sizes->surface_height;
  313. bpp = sizes->surface_bpp;
  314. /* No 24bit packed */
  315. if (bpp == 24)
  316. bpp = 32;
  317. /* HW requires pitch to be 64 byte aligned */
  318. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
  319. depth = sizes->surface_depth;
  320. size = mode_cmd.pitches[0] * mode_cmd.height;
  321. size = ALIGN(size, PAGE_SIZE);
  322. /* Allocate the framebuffer in the GTT with stolen page backing */
  323. backing = psbfb_alloc(dev, size);
  324. if (backing == NULL)
  325. return -ENOMEM;
  326. mutex_lock(&dev->struct_mutex);
  327. info = framebuffer_alloc(0, device);
  328. if (!info) {
  329. ret = -ENOMEM;
  330. goto out_err1;
  331. }
  332. info->par = fbdev;
  333. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  334. ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
  335. if (ret)
  336. goto out_unref;
  337. fb = &psbfb->base;
  338. psbfb->fbdev = info;
  339. fbdev->psb_fb_helper.fb = fb;
  340. fbdev->psb_fb_helper.fbdev = info;
  341. strcpy(info->fix.id, "psbfb");
  342. info->flags = FBINFO_DEFAULT;
  343. /* No 2D engine */
  344. if (!dev_priv->ops->accel_2d)
  345. info->fbops = &psbfb_unaccel_ops;
  346. else
  347. info->fbops = &psbfb_ops;
  348. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  349. if (ret) {
  350. ret = -ENOMEM;
  351. goto out_unref;
  352. }
  353. info->fix.smem_start = dev->mode_config.fb_base;
  354. info->fix.smem_len = size;
  355. /* Accessed stolen memory directly */
  356. info->screen_base = (char *)dev_priv->vram_addr +
  357. backing->offset;
  358. info->screen_size = size;
  359. if (dev_priv->gtt.stolen_size) {
  360. info->apertures = alloc_apertures(1);
  361. if (!info->apertures) {
  362. ret = -ENOMEM;
  363. goto out_unref;
  364. }
  365. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  366. info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
  367. }
  368. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  369. drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
  370. sizes->fb_width, sizes->fb_height);
  371. info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
  372. info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
  373. info->pixmap.size = 64 * 1024;
  374. info->pixmap.buf_align = 8;
  375. info->pixmap.access_align = 32;
  376. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  377. info->pixmap.scan_align = 1;
  378. dev_info(dev->dev, "allocated %dx%d fb\n",
  379. psbfb->base.width, psbfb->base.height);
  380. mutex_unlock(&dev->struct_mutex);
  381. return 0;
  382. out_unref:
  383. if (backing->stolen)
  384. psb_gtt_free_range(dev, backing);
  385. else
  386. drm_gem_object_unreference(&backing->gem);
  387. out_err1:
  388. mutex_unlock(&dev->struct_mutex);
  389. psb_gtt_free_range(dev, backing);
  390. return ret;
  391. }
  392. /**
  393. * psb_user_framebuffer_create - create framebuffer
  394. * @dev: our DRM device
  395. * @filp: client file
  396. * @cmd: mode request
  397. *
  398. * Create a new framebuffer backed by a userspace GEM object
  399. */
  400. static struct drm_framebuffer *psb_user_framebuffer_create
  401. (struct drm_device *dev, struct drm_file *filp,
  402. struct drm_mode_fb_cmd2 *cmd)
  403. {
  404. struct gtt_range *r;
  405. struct drm_gem_object *obj;
  406. /*
  407. * Find the GEM object and thus the gtt range object that is
  408. * to back this space
  409. */
  410. obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
  411. if (obj == NULL)
  412. return ERR_PTR(-ENOENT);
  413. /* Let the core code do all the work */
  414. r = container_of(obj, struct gtt_range, gem);
  415. return psb_framebuffer_create(dev, cmd, r);
  416. }
  417. static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  418. u16 blue, int regno)
  419. {
  420. }
  421. static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
  422. u16 *green, u16 *blue, int regno)
  423. {
  424. }
  425. static int psbfb_probe(struct drm_fb_helper *helper,
  426. struct drm_fb_helper_surface_size *sizes)
  427. {
  428. struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
  429. int new_fb = 0;
  430. int ret;
  431. if (!helper->fb) {
  432. ret = psbfb_create(psb_fbdev, sizes);
  433. if (ret)
  434. return ret;
  435. new_fb = 1;
  436. }
  437. return new_fb;
  438. }
  439. struct drm_fb_helper_funcs psb_fb_helper_funcs = {
  440. .gamma_set = psbfb_gamma_set,
  441. .gamma_get = psbfb_gamma_get,
  442. .fb_probe = psbfb_probe,
  443. };
  444. int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
  445. {
  446. struct fb_info *info;
  447. struct psb_framebuffer *psbfb = &fbdev->pfb;
  448. if (fbdev->psb_fb_helper.fbdev) {
  449. info = fbdev->psb_fb_helper.fbdev;
  450. unregister_framebuffer(info);
  451. if (info->cmap.len)
  452. fb_dealloc_cmap(&info->cmap);
  453. framebuffer_release(info);
  454. }
  455. drm_fb_helper_fini(&fbdev->psb_fb_helper);
  456. drm_framebuffer_cleanup(&psbfb->base);
  457. if (psbfb->gtt)
  458. drm_gem_object_unreference(&psbfb->gtt->gem);
  459. return 0;
  460. }
  461. int psb_fbdev_init(struct drm_device *dev)
  462. {
  463. struct psb_fbdev *fbdev;
  464. struct drm_psb_private *dev_priv = dev->dev_private;
  465. fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
  466. if (!fbdev) {
  467. dev_err(dev->dev, "no memory\n");
  468. return -ENOMEM;
  469. }
  470. dev_priv->fbdev = fbdev;
  471. fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
  472. drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
  473. INTELFB_CONN_LIMIT);
  474. drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
  475. drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
  476. return 0;
  477. }
  478. void psb_fbdev_fini(struct drm_device *dev)
  479. {
  480. struct drm_psb_private *dev_priv = dev->dev_private;
  481. if (!dev_priv->fbdev)
  482. return;
  483. psb_fbdev_destroy(dev, dev_priv->fbdev);
  484. kfree(dev_priv->fbdev);
  485. dev_priv->fbdev = NULL;
  486. }
  487. static void psbfb_output_poll_changed(struct drm_device *dev)
  488. {
  489. struct drm_psb_private *dev_priv = dev->dev_private;
  490. struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
  491. drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
  492. }
  493. /**
  494. * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
  495. * @fb: framebuffer
  496. * @file_priv: our DRM file
  497. * @handle: returned handle
  498. *
  499. * Our framebuffer object is a GTT range which also contains a GEM
  500. * object. We need to turn it into a handle for userspace. GEM will do
  501. * the work for us
  502. */
  503. static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  504. struct drm_file *file_priv,
  505. unsigned int *handle)
  506. {
  507. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  508. struct gtt_range *r = psbfb->gtt;
  509. return drm_gem_handle_create(file_priv, &r->gem, handle);
  510. }
  511. /**
  512. * psb_user_framebuffer_destroy - destruct user created fb
  513. * @fb: framebuffer
  514. *
  515. * User framebuffers are backed by GEM objects so all we have to do is
  516. * clean up a bit and drop the reference, GEM will handle the fallout
  517. */
  518. static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
  519. {
  520. struct psb_framebuffer *psbfb = to_psb_fb(fb);
  521. struct gtt_range *r = psbfb->gtt;
  522. struct drm_device *dev = fb->dev;
  523. struct drm_psb_private *dev_priv = dev->dev_private;
  524. struct psb_fbdev *fbdev = dev_priv->fbdev;
  525. struct drm_crtc *crtc;
  526. int reset = 0;
  527. /* Should never get stolen memory for a user fb */
  528. WARN_ON(r->stolen);
  529. /* Check if we are erroneously live */
  530. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  531. if (crtc->fb == fb)
  532. reset = 1;
  533. if (reset)
  534. /*
  535. * Now force a sane response before we permit the DRM CRTC
  536. * layer to do stupid things like blank the display. Instead
  537. * we reset this framebuffer as if the user had forced a reset.
  538. * We must do this before the cleanup so that the DRM layer
  539. * doesn't get a chance to stick its oar in where it isn't
  540. * wanted.
  541. */
  542. drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
  543. /* Let DRM do its clean up */
  544. drm_framebuffer_cleanup(fb);
  545. /* We are no longer using the resource in GEM */
  546. drm_gem_object_unreference_unlocked(&r->gem);
  547. kfree(fb);
  548. }
  549. static const struct drm_mode_config_funcs psb_mode_funcs = {
  550. .fb_create = psb_user_framebuffer_create,
  551. .output_poll_changed = psbfb_output_poll_changed,
  552. };
  553. static int psb_create_backlight_property(struct drm_device *dev)
  554. {
  555. struct drm_psb_private *dev_priv = dev->dev_private;
  556. struct drm_property *backlight;
  557. if (dev_priv->backlight_property)
  558. return 0;
  559. backlight = drm_property_create(dev, DRM_MODE_PROP_RANGE,
  560. "backlight", 2);
  561. backlight->values[0] = 0;
  562. backlight->values[1] = 100;
  563. dev_priv->backlight_property = backlight;
  564. return 0;
  565. }
  566. static void psb_setup_outputs(struct drm_device *dev)
  567. {
  568. struct drm_psb_private *dev_priv = dev->dev_private;
  569. struct drm_connector *connector;
  570. drm_mode_create_scaling_mode_property(dev);
  571. psb_create_backlight_property(dev);
  572. dev_priv->ops->output_init(dev);
  573. list_for_each_entry(connector, &dev->mode_config.connector_list,
  574. head) {
  575. struct psb_intel_output *psb_intel_output =
  576. to_psb_intel_output(connector);
  577. struct drm_encoder *encoder = &psb_intel_output->enc;
  578. int crtc_mask = 0, clone_mask = 0;
  579. /* valid crtcs */
  580. switch (psb_intel_output->type) {
  581. case INTEL_OUTPUT_ANALOG:
  582. crtc_mask = (1 << 0);
  583. clone_mask = (1 << INTEL_OUTPUT_ANALOG);
  584. break;
  585. case INTEL_OUTPUT_SDVO:
  586. crtc_mask = ((1 << 0) | (1 << 1));
  587. clone_mask = (1 << INTEL_OUTPUT_SDVO);
  588. break;
  589. case INTEL_OUTPUT_LVDS:
  590. if (IS_MRST(dev))
  591. crtc_mask = (1 << 0);
  592. else
  593. crtc_mask = (1 << 1);
  594. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  595. break;
  596. case INTEL_OUTPUT_MIPI:
  597. crtc_mask = (1 << 0);
  598. clone_mask = (1 << INTEL_OUTPUT_MIPI);
  599. break;
  600. case INTEL_OUTPUT_MIPI2:
  601. crtc_mask = (1 << 2);
  602. clone_mask = (1 << INTEL_OUTPUT_MIPI2);
  603. break;
  604. case INTEL_OUTPUT_HDMI:
  605. if (IS_MFLD(dev))
  606. crtc_mask = (1 << 1);
  607. else
  608. crtc_mask = (1 << 0);
  609. clone_mask = (1 << INTEL_OUTPUT_HDMI);
  610. break;
  611. }
  612. encoder->possible_crtcs = crtc_mask;
  613. encoder->possible_clones =
  614. psb_intel_connector_clones(dev, clone_mask);
  615. }
  616. }
  617. void psb_modeset_init(struct drm_device *dev)
  618. {
  619. struct drm_psb_private *dev_priv = dev->dev_private;
  620. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  621. int i;
  622. drm_mode_config_init(dev);
  623. dev->mode_config.min_width = 0;
  624. dev->mode_config.min_height = 0;
  625. dev->mode_config.funcs = (void *) &psb_mode_funcs;
  626. /* set memory base */
  627. /* Oaktrail and Poulsbo should use BAR 2*/
  628. pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
  629. &(dev->mode_config.fb_base));
  630. /* num pipes is 2 for PSB but 1 for Mrst */
  631. for (i = 0; i < dev_priv->num_pipe; i++)
  632. psb_intel_crtc_init(dev, i, mode_dev);
  633. dev->mode_config.max_width = 2048;
  634. dev->mode_config.max_height = 2048;
  635. psb_setup_outputs(dev);
  636. }
  637. void psb_modeset_cleanup(struct drm_device *dev)
  638. {
  639. mutex_lock(&dev->struct_mutex);
  640. drm_kms_helper_poll_fini(dev);
  641. psb_fbdev_fini(dev);
  642. drm_mode_config_cleanup(dev);
  643. mutex_unlock(&dev->struct_mutex);
  644. }