framebuffer.c 21 KB

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