drm_fb_cma_helper.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * drm kms/fb cma (contiguous memory allocator) helper functions
  3. *
  4. * Copyright (C) 2012 Analog Device Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Based on udl_fbdev.c
  8. * Copyright (C) 2012 Red Hat
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <drm/drmP.h>
  20. #include <drm/drm_fb_helper.h>
  21. #include <drm/drm_framebuffer.h>
  22. #include <drm/drm_gem_cma_helper.h>
  23. #include <drm/drm_gem_framebuffer_helper.h>
  24. #include <drm/drm_fb_cma_helper.h>
  25. #include <linux/module.h>
  26. #define DEFAULT_FBDEFIO_DELAY_MS 50
  27. struct drm_fbdev_cma {
  28. struct drm_fb_helper fb_helper;
  29. const struct drm_framebuffer_funcs *fb_funcs;
  30. };
  31. /**
  32. * DOC: framebuffer cma helper functions
  33. *
  34. * Provides helper functions for creating a cma (contiguous memory allocator)
  35. * backed framebuffer.
  36. *
  37. * drm_fb_cma_create() is used in the &drm_mode_config_funcs.fb_create
  38. * callback function to create a cma backed framebuffer.
  39. *
  40. * An fbdev framebuffer backed by cma is also available by calling
  41. * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
  42. * If the &drm_framebuffer_funcs.dirty callback is set, fb_deferred_io will be
  43. * set up automatically. &drm_framebuffer_funcs.dirty is called by
  44. * drm_fb_helper_deferred_io() in process context (&struct delayed_work).
  45. *
  46. * Example fbdev deferred io code::
  47. *
  48. * static int driver_fb_dirty(struct drm_framebuffer *fb,
  49. * struct drm_file *file_priv,
  50. * unsigned flags, unsigned color,
  51. * struct drm_clip_rect *clips,
  52. * unsigned num_clips)
  53. * {
  54. * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
  55. * ... push changes ...
  56. * return 0;
  57. * }
  58. *
  59. * static struct drm_framebuffer_funcs driver_fb_funcs = {
  60. * .destroy = drm_fb_cma_destroy,
  61. * .create_handle = drm_fb_cma_create_handle,
  62. * .dirty = driver_fb_dirty,
  63. * };
  64. *
  65. * Initialize::
  66. *
  67. * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
  68. * dev->mode_config.num_crtc,
  69. * dev->mode_config.num_connector,
  70. * &driver_fb_funcs);
  71. *
  72. */
  73. static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
  74. {
  75. return container_of(helper, struct drm_fbdev_cma, fb_helper);
  76. }
  77. void drm_fb_cma_destroy(struct drm_framebuffer *fb)
  78. {
  79. drm_gem_fb_destroy(fb);
  80. }
  81. EXPORT_SYMBOL(drm_fb_cma_destroy);
  82. int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
  83. struct drm_file *file_priv, unsigned int *handle)
  84. {
  85. return drm_gem_fb_create_handle(fb, file_priv, handle);
  86. }
  87. EXPORT_SYMBOL(drm_fb_cma_create_handle);
  88. /**
  89. * drm_fb_cma_create_with_funcs() - helper function for the
  90. * &drm_mode_config_funcs.fb_create
  91. * callback
  92. * @dev: DRM device
  93. * @file_priv: drm file for the ioctl call
  94. * @mode_cmd: metadata from the userspace fb creation request
  95. * @funcs: vtable to be used for the new framebuffer object
  96. *
  97. * This can be used to set &drm_framebuffer_funcs for drivers that need the
  98. * &drm_framebuffer_funcs.dirty callback. Use drm_fb_cma_create() if you don't
  99. * need to change &drm_framebuffer_funcs.
  100. */
  101. struct drm_framebuffer *drm_fb_cma_create_with_funcs(struct drm_device *dev,
  102. struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd,
  103. const struct drm_framebuffer_funcs *funcs)
  104. {
  105. return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd, funcs);
  106. }
  107. EXPORT_SYMBOL_GPL(drm_fb_cma_create_with_funcs);
  108. /**
  109. * drm_fb_cma_create() - &drm_mode_config_funcs.fb_create callback function
  110. * @dev: DRM device
  111. * @file_priv: drm file for the ioctl call
  112. * @mode_cmd: metadata from the userspace fb creation request
  113. *
  114. * If your hardware has special alignment or pitch requirements these should be
  115. * checked before calling this function. Use drm_fb_cma_create_with_funcs() if
  116. * you need to set &drm_framebuffer_funcs.dirty.
  117. */
  118. struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
  119. struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
  120. {
  121. return drm_gem_fb_create(dev, file_priv, mode_cmd);
  122. }
  123. EXPORT_SYMBOL_GPL(drm_fb_cma_create);
  124. /**
  125. * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
  126. * @fb: The framebuffer
  127. * @plane: Which plane
  128. *
  129. * Return the CMA GEM object for given framebuffer.
  130. *
  131. * This function will usually be called from the CRTC callback functions.
  132. */
  133. struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
  134. unsigned int plane)
  135. {
  136. struct drm_gem_object *gem;
  137. gem = drm_gem_fb_get_obj(fb, plane);
  138. if (!gem)
  139. return NULL;
  140. return to_drm_gem_cma_obj(gem);
  141. }
  142. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
  143. /**
  144. * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
  145. * @fb: The framebuffer
  146. * @state: Which state of drm plane
  147. * @plane: Which plane
  148. * Return the CMA GEM address for given framebuffer.
  149. *
  150. * This function will usually be called from the PLANE callback functions.
  151. */
  152. dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
  153. struct drm_plane_state *state,
  154. unsigned int plane)
  155. {
  156. struct drm_gem_cma_object *obj;
  157. dma_addr_t paddr;
  158. obj = drm_fb_cma_get_gem_obj(fb, plane);
  159. if (!obj)
  160. return 0;
  161. paddr = obj->paddr + fb->offsets[plane];
  162. paddr += fb->format->cpp[plane] * (state->src_x >> 16);
  163. paddr += fb->pitches[plane] * (state->src_y >> 16);
  164. return paddr;
  165. }
  166. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
  167. /**
  168. * drm_fb_cma_prepare_fb() - Prepare CMA framebuffer
  169. * @plane: Which plane
  170. * @state: Plane state attach fence to
  171. *
  172. * This should be set as the &struct drm_plane_helper_funcs.prepare_fb hook.
  173. *
  174. * This function checks if the plane FB has an dma-buf attached, extracts
  175. * the exclusive fence and attaches it to plane state for the atomic helper
  176. * to wait on.
  177. *
  178. * There is no need for cleanup_fb for CMA based framebuffer drivers.
  179. */
  180. int drm_fb_cma_prepare_fb(struct drm_plane *plane,
  181. struct drm_plane_state *state)
  182. {
  183. return drm_gem_fb_prepare_fb(plane, state);
  184. }
  185. EXPORT_SYMBOL_GPL(drm_fb_cma_prepare_fb);
  186. #ifdef CONFIG_DEBUG_FS
  187. static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
  188. {
  189. int i;
  190. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  191. (char *)&fb->format->format);
  192. for (i = 0; i < fb->format->num_planes; i++) {
  193. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  194. i, fb->offsets[i], fb->pitches[i]);
  195. drm_gem_cma_describe(drm_fb_cma_get_gem_obj(fb, i), m);
  196. }
  197. }
  198. /**
  199. * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
  200. * in debugfs.
  201. * @m: output file
  202. * @arg: private data for the callback
  203. */
  204. int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
  205. {
  206. struct drm_info_node *node = (struct drm_info_node *) m->private;
  207. struct drm_device *dev = node->minor->dev;
  208. struct drm_framebuffer *fb;
  209. mutex_lock(&dev->mode_config.fb_lock);
  210. drm_for_each_fb(fb, dev)
  211. drm_fb_cma_describe(fb, m);
  212. mutex_unlock(&dev->mode_config.fb_lock);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
  216. #endif
  217. static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
  218. {
  219. return dma_mmap_writecombine(info->device, vma, info->screen_base,
  220. info->fix.smem_start, info->fix.smem_len);
  221. }
  222. static struct fb_ops drm_fbdev_cma_ops = {
  223. .owner = THIS_MODULE,
  224. DRM_FB_HELPER_DEFAULT_OPS,
  225. .fb_fillrect = drm_fb_helper_sys_fillrect,
  226. .fb_copyarea = drm_fb_helper_sys_copyarea,
  227. .fb_imageblit = drm_fb_helper_sys_imageblit,
  228. .fb_mmap = drm_fb_cma_mmap,
  229. };
  230. static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
  231. struct vm_area_struct *vma)
  232. {
  233. fb_deferred_io_mmap(info, vma);
  234. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  235. return 0;
  236. }
  237. static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
  238. struct drm_gem_cma_object *cma_obj)
  239. {
  240. struct fb_deferred_io *fbdefio;
  241. struct fb_ops *fbops;
  242. /*
  243. * Per device structures are needed because:
  244. * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
  245. * fbdefio: individual delays
  246. */
  247. fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
  248. fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
  249. if (!fbdefio || !fbops) {
  250. kfree(fbdefio);
  251. kfree(fbops);
  252. return -ENOMEM;
  253. }
  254. /* can't be offset from vaddr since dirty() uses cma_obj */
  255. fbi->screen_buffer = cma_obj->vaddr;
  256. /* fb_deferred_io_fault() needs a physical address */
  257. fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
  258. *fbops = *fbi->fbops;
  259. fbi->fbops = fbops;
  260. fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
  261. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  262. fbi->fbdefio = fbdefio;
  263. fb_deferred_io_init(fbi);
  264. fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
  265. return 0;
  266. }
  267. static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
  268. {
  269. if (!fbi->fbdefio)
  270. return;
  271. fb_deferred_io_cleanup(fbi);
  272. kfree(fbi->fbdefio);
  273. kfree(fbi->fbops);
  274. }
  275. static int
  276. drm_fbdev_cma_create(struct drm_fb_helper *helper,
  277. struct drm_fb_helper_surface_size *sizes)
  278. {
  279. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  280. struct drm_device *dev = helper->dev;
  281. struct drm_gem_cma_object *obj;
  282. struct drm_framebuffer *fb;
  283. unsigned int bytes_per_pixel;
  284. unsigned long offset;
  285. struct fb_info *fbi;
  286. size_t size;
  287. int ret;
  288. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  289. sizes->surface_width, sizes->surface_height,
  290. sizes->surface_bpp);
  291. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  292. size = sizes->surface_width * sizes->surface_height * bytes_per_pixel;
  293. obj = drm_gem_cma_create(dev, size);
  294. if (IS_ERR(obj))
  295. return -ENOMEM;
  296. fbi = drm_fb_helper_alloc_fbi(helper);
  297. if (IS_ERR(fbi)) {
  298. ret = PTR_ERR(fbi);
  299. goto err_gem_free_object;
  300. }
  301. fb = drm_gem_fbdev_fb_create(dev, sizes, 0, &obj->base,
  302. fbdev_cma->fb_funcs);
  303. if (IS_ERR(fb)) {
  304. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  305. ret = PTR_ERR(fb);
  306. goto err_fb_info_destroy;
  307. }
  308. helper->fb = fb;
  309. fbi->par = helper;
  310. fbi->flags = FBINFO_FLAG_DEFAULT;
  311. fbi->fbops = &drm_fbdev_cma_ops;
  312. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  313. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  314. offset = fbi->var.xoffset * bytes_per_pixel;
  315. offset += fbi->var.yoffset * fb->pitches[0];
  316. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  317. fbi->screen_base = obj->vaddr + offset;
  318. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  319. fbi->screen_size = size;
  320. fbi->fix.smem_len = size;
  321. if (fbdev_cma->fb_funcs->dirty) {
  322. ret = drm_fbdev_cma_defio_init(fbi, obj);
  323. if (ret)
  324. goto err_cma_destroy;
  325. }
  326. return 0;
  327. err_cma_destroy:
  328. drm_framebuffer_remove(fb);
  329. err_fb_info_destroy:
  330. drm_fb_helper_fini(helper);
  331. err_gem_free_object:
  332. drm_gem_object_put_unlocked(&obj->base);
  333. return ret;
  334. }
  335. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  336. .fb_probe = drm_fbdev_cma_create,
  337. };
  338. /**
  339. * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
  340. * @dev: DRM device
  341. * @preferred_bpp: Preferred bits per pixel for the device
  342. * @max_conn_count: Maximum number of connectors
  343. * @funcs: fb helper functions, in particular a custom dirty() callback
  344. *
  345. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  346. */
  347. struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
  348. unsigned int preferred_bpp, unsigned int max_conn_count,
  349. const struct drm_framebuffer_funcs *funcs)
  350. {
  351. struct drm_fbdev_cma *fbdev_cma;
  352. struct drm_fb_helper *helper;
  353. int ret;
  354. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  355. if (!fbdev_cma) {
  356. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  357. return ERR_PTR(-ENOMEM);
  358. }
  359. fbdev_cma->fb_funcs = funcs;
  360. helper = &fbdev_cma->fb_helper;
  361. drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
  362. ret = drm_fb_helper_init(dev, helper, max_conn_count);
  363. if (ret < 0) {
  364. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  365. goto err_free;
  366. }
  367. ret = drm_fb_helper_single_add_all_connectors(helper);
  368. if (ret < 0) {
  369. dev_err(dev->dev, "Failed to add connectors.\n");
  370. goto err_drm_fb_helper_fini;
  371. }
  372. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  373. if (ret < 0) {
  374. dev_err(dev->dev, "Failed to set initial hw configuration.\n");
  375. goto err_drm_fb_helper_fini;
  376. }
  377. return fbdev_cma;
  378. err_drm_fb_helper_fini:
  379. drm_fb_helper_fini(helper);
  380. err_free:
  381. kfree(fbdev_cma);
  382. return ERR_PTR(ret);
  383. }
  384. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
  385. static const struct drm_framebuffer_funcs drm_fb_cma_funcs = {
  386. .destroy = drm_gem_fb_destroy,
  387. .create_handle = drm_gem_fb_create_handle,
  388. };
  389. /**
  390. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  391. * @dev: DRM device
  392. * @preferred_bpp: Preferred bits per pixel for the device
  393. * @max_conn_count: Maximum number of connectors
  394. *
  395. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  396. */
  397. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  398. unsigned int preferred_bpp, unsigned int max_conn_count)
  399. {
  400. return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
  401. max_conn_count,
  402. &drm_fb_cma_funcs);
  403. }
  404. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  405. /**
  406. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  407. * @fbdev_cma: The drm_fbdev_cma struct
  408. */
  409. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  410. {
  411. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  412. if (fbdev_cma->fb_helper.fbdev)
  413. drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
  414. if (fbdev_cma->fb_helper.fb)
  415. drm_framebuffer_remove(fbdev_cma->fb_helper.fb);
  416. drm_fb_helper_fini(&fbdev_cma->fb_helper);
  417. kfree(fbdev_cma);
  418. }
  419. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  420. /**
  421. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  422. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  423. *
  424. * This function is usually called from the &drm_driver.lastclose callback.
  425. */
  426. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  427. {
  428. if (fbdev_cma)
  429. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  430. }
  431. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  432. /**
  433. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  434. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  435. *
  436. * This function is usually called from the &drm_mode_config.output_poll_changed
  437. * callback.
  438. */
  439. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  440. {
  441. if (fbdev_cma)
  442. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  443. }
  444. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
  445. /**
  446. * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
  447. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  448. * @state: desired state, zero to resume, non-zero to suspend
  449. *
  450. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  451. * fb_set_suspend implemented by fbdev core.
  452. */
  453. void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
  454. {
  455. if (fbdev_cma)
  456. drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
  457. }
  458. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
  459. /**
  460. * drm_fbdev_cma_set_suspend_unlocked - wrapper around
  461. * drm_fb_helper_set_suspend_unlocked
  462. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  463. * @state: desired state, zero to resume, non-zero to suspend
  464. *
  465. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  466. * fb_set_suspend implemented by fbdev core.
  467. */
  468. void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
  469. bool state)
  470. {
  471. if (fbdev_cma)
  472. drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
  473. state);
  474. }
  475. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);