framebuffer.c 21 KB

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