drm_fb_cma_helper.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 <drm/drm_print.h>
  26. #include <linux/module.h>
  27. #define DEFAULT_FBDEFIO_DELAY_MS 50
  28. struct drm_fbdev_cma {
  29. struct drm_fb_helper fb_helper;
  30. const struct drm_framebuffer_funcs *fb_funcs;
  31. };
  32. /**
  33. * DOC: framebuffer cma helper functions
  34. *
  35. * Provides helper functions for creating a cma (contiguous memory allocator)
  36. * backed framebuffer.
  37. *
  38. * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
  39. * callback function to create a cma backed framebuffer.
  40. *
  41. * An fbdev framebuffer backed by cma is also available by calling
  42. * drm_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down.
  43. * If the &drm_framebuffer_funcs.dirty callback is set, fb_deferred_io will be
  44. * set up automatically. &drm_framebuffer_funcs.dirty is called by
  45. * drm_fb_helper_deferred_io() in process context (&struct delayed_work).
  46. *
  47. * Example fbdev deferred io code::
  48. *
  49. * static int driver_fb_dirty(struct drm_framebuffer *fb,
  50. * struct drm_file *file_priv,
  51. * unsigned flags, unsigned color,
  52. * struct drm_clip_rect *clips,
  53. * unsigned num_clips)
  54. * {
  55. * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
  56. * ... push changes ...
  57. * return 0;
  58. * }
  59. *
  60. * static struct drm_framebuffer_funcs driver_fb_funcs = {
  61. * .destroy = drm_gem_fb_destroy,
  62. * .create_handle = drm_gem_fb_create_handle,
  63. * .dirty = driver_fb_dirty,
  64. * };
  65. *
  66. * Initialize::
  67. *
  68. * fbdev = drm_fb_cma_fbdev_init_with_funcs(dev, 16,
  69. * dev->mode_config.num_crtc,
  70. * dev->mode_config.num_connector,
  71. * &driver_fb_funcs);
  72. *
  73. */
  74. static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
  75. {
  76. return container_of(helper, struct drm_fbdev_cma, fb_helper);
  77. }
  78. /**
  79. * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
  80. * @fb: The framebuffer
  81. * @plane: Which plane
  82. *
  83. * Return the CMA GEM object for given framebuffer.
  84. *
  85. * This function will usually be called from the CRTC callback functions.
  86. */
  87. struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
  88. unsigned int plane)
  89. {
  90. struct drm_gem_object *gem;
  91. gem = drm_gem_fb_get_obj(fb, plane);
  92. if (!gem)
  93. return NULL;
  94. return to_drm_gem_cma_obj(gem);
  95. }
  96. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
  97. /**
  98. * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
  99. * @fb: The framebuffer
  100. * @state: Which state of drm plane
  101. * @plane: Which plane
  102. * Return the CMA GEM address for given framebuffer.
  103. *
  104. * This function will usually be called from the PLANE callback functions.
  105. */
  106. dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
  107. struct drm_plane_state *state,
  108. unsigned int plane)
  109. {
  110. struct drm_gem_cma_object *obj;
  111. dma_addr_t paddr;
  112. obj = drm_fb_cma_get_gem_obj(fb, plane);
  113. if (!obj)
  114. return 0;
  115. paddr = obj->paddr + fb->offsets[plane];
  116. paddr += fb->format->cpp[plane] * (state->src_x >> 16);
  117. paddr += fb->pitches[plane] * (state->src_y >> 16);
  118. return paddr;
  119. }
  120. EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
  121. static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
  122. {
  123. return dma_mmap_writecombine(info->device, vma, info->screen_base,
  124. info->fix.smem_start, info->fix.smem_len);
  125. }
  126. static struct fb_ops drm_fbdev_cma_ops = {
  127. .owner = THIS_MODULE,
  128. DRM_FB_HELPER_DEFAULT_OPS,
  129. .fb_fillrect = drm_fb_helper_sys_fillrect,
  130. .fb_copyarea = drm_fb_helper_sys_copyarea,
  131. .fb_imageblit = drm_fb_helper_sys_imageblit,
  132. .fb_mmap = drm_fb_cma_mmap,
  133. };
  134. static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
  135. struct vm_area_struct *vma)
  136. {
  137. fb_deferred_io_mmap(info, vma);
  138. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  139. return 0;
  140. }
  141. static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
  142. struct drm_gem_cma_object *cma_obj)
  143. {
  144. struct fb_deferred_io *fbdefio;
  145. struct fb_ops *fbops;
  146. /*
  147. * Per device structures are needed because:
  148. * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
  149. * fbdefio: individual delays
  150. */
  151. fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
  152. fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
  153. if (!fbdefio || !fbops) {
  154. kfree(fbdefio);
  155. kfree(fbops);
  156. return -ENOMEM;
  157. }
  158. /* can't be offset from vaddr since dirty() uses cma_obj */
  159. fbi->screen_buffer = cma_obj->vaddr;
  160. /* fb_deferred_io_fault() needs a physical address */
  161. fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
  162. *fbops = *fbi->fbops;
  163. fbi->fbops = fbops;
  164. fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
  165. fbdefio->deferred_io = drm_fb_helper_deferred_io;
  166. fbi->fbdefio = fbdefio;
  167. fb_deferred_io_init(fbi);
  168. fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
  169. return 0;
  170. }
  171. static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
  172. {
  173. if (!fbi->fbdefio)
  174. return;
  175. fb_deferred_io_cleanup(fbi);
  176. kfree(fbi->fbdefio);
  177. kfree(fbi->fbops);
  178. }
  179. static int
  180. drm_fbdev_cma_create(struct drm_fb_helper *helper,
  181. struct drm_fb_helper_surface_size *sizes)
  182. {
  183. struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
  184. struct drm_device *dev = helper->dev;
  185. struct drm_gem_cma_object *obj;
  186. struct drm_framebuffer *fb;
  187. unsigned int bytes_per_pixel;
  188. unsigned long offset;
  189. struct fb_info *fbi;
  190. size_t size;
  191. int ret;
  192. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
  193. sizes->surface_width, sizes->surface_height,
  194. sizes->surface_bpp);
  195. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  196. size = sizes->surface_width * sizes->surface_height * bytes_per_pixel;
  197. obj = drm_gem_cma_create(dev, size);
  198. if (IS_ERR(obj))
  199. return -ENOMEM;
  200. fbi = drm_fb_helper_alloc_fbi(helper);
  201. if (IS_ERR(fbi)) {
  202. ret = PTR_ERR(fbi);
  203. goto err_gem_free_object;
  204. }
  205. fb = drm_gem_fbdev_fb_create(dev, sizes, 0, &obj->base,
  206. fbdev_cma->fb_funcs);
  207. if (IS_ERR(fb)) {
  208. dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
  209. ret = PTR_ERR(fb);
  210. goto err_fb_info_destroy;
  211. }
  212. helper->fb = fb;
  213. fbi->par = helper;
  214. fbi->flags = FBINFO_FLAG_DEFAULT;
  215. fbi->fbops = &drm_fbdev_cma_ops;
  216. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  217. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  218. offset = fbi->var.xoffset * bytes_per_pixel;
  219. offset += fbi->var.yoffset * fb->pitches[0];
  220. dev->mode_config.fb_base = (resource_size_t)obj->paddr;
  221. fbi->screen_base = obj->vaddr + offset;
  222. fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
  223. fbi->screen_size = size;
  224. fbi->fix.smem_len = size;
  225. if (fb->funcs->dirty) {
  226. ret = drm_fbdev_cma_defio_init(fbi, obj);
  227. if (ret)
  228. goto err_cma_destroy;
  229. }
  230. return 0;
  231. err_cma_destroy:
  232. drm_framebuffer_remove(fb);
  233. err_fb_info_destroy:
  234. drm_fb_helper_fini(helper);
  235. err_gem_free_object:
  236. drm_gem_object_put_unlocked(&obj->base);
  237. return ret;
  238. }
  239. static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
  240. .fb_probe = drm_fbdev_cma_create,
  241. };
  242. /**
  243. * drm_fb_cma_fbdev_init_with_funcs() - Allocate and initialize fbdev emulation
  244. * @dev: DRM device
  245. * @preferred_bpp: Preferred bits per pixel for the device.
  246. * @dev->mode_config.preferred_depth is used if this is zero.
  247. * @max_conn_count: Maximum number of connectors.
  248. * @dev->mode_config.num_connector is used if this is zero.
  249. * @funcs: Framebuffer functions, in particular a custom dirty() callback.
  250. * Can be NULL.
  251. *
  252. * Returns:
  253. * Zero on success or negative error code on failure.
  254. */
  255. int drm_fb_cma_fbdev_init_with_funcs(struct drm_device *dev,
  256. unsigned int preferred_bpp, unsigned int max_conn_count,
  257. const struct drm_framebuffer_funcs *funcs)
  258. {
  259. struct drm_fbdev_cma *fbdev_cma;
  260. struct drm_fb_helper *fb_helper;
  261. int ret;
  262. if (!preferred_bpp)
  263. preferred_bpp = dev->mode_config.preferred_depth;
  264. if (!preferred_bpp)
  265. preferred_bpp = 32;
  266. if (!max_conn_count)
  267. max_conn_count = dev->mode_config.num_connector;
  268. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  269. if (!fbdev_cma)
  270. return -ENOMEM;
  271. fbdev_cma->fb_funcs = funcs;
  272. fb_helper = &fbdev_cma->fb_helper;
  273. drm_fb_helper_prepare(dev, fb_helper, &drm_fb_cma_helper_funcs);
  274. ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
  275. if (ret < 0) {
  276. DRM_DEV_ERROR(dev->dev, "Failed to initialize fbdev helper.\n");
  277. goto err_free;
  278. }
  279. ret = drm_fb_helper_single_add_all_connectors(fb_helper);
  280. if (ret < 0) {
  281. DRM_DEV_ERROR(dev->dev, "Failed to add connectors.\n");
  282. goto err_drm_fb_helper_fini;
  283. }
  284. ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
  285. if (ret < 0) {
  286. DRM_DEV_ERROR(dev->dev, "Failed to set fbdev configuration.\n");
  287. goto err_drm_fb_helper_fini;
  288. }
  289. return 0;
  290. err_drm_fb_helper_fini:
  291. drm_fb_helper_fini(fb_helper);
  292. err_free:
  293. kfree(fbdev_cma);
  294. return ret;
  295. }
  296. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init_with_funcs);
  297. /**
  298. * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation
  299. * @dev: DRM device
  300. * @preferred_bpp: Preferred bits per pixel for the device.
  301. * @dev->mode_config.preferred_depth is used if this is zero.
  302. * @max_conn_count: Maximum number of connectors.
  303. * @dev->mode_config.num_connector is used if this is zero.
  304. *
  305. * Returns:
  306. * Zero on success or negative error code on failure.
  307. */
  308. int drm_fb_cma_fbdev_init(struct drm_device *dev, unsigned int preferred_bpp,
  309. unsigned int max_conn_count)
  310. {
  311. return drm_fb_cma_fbdev_init_with_funcs(dev, preferred_bpp,
  312. max_conn_count, NULL);
  313. }
  314. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init);
  315. /**
  316. * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation
  317. * @dev: DRM device
  318. */
  319. void drm_fb_cma_fbdev_fini(struct drm_device *dev)
  320. {
  321. struct drm_fb_helper *fb_helper = dev->fb_helper;
  322. if (!fb_helper)
  323. return;
  324. /* Unregister if it hasn't been done already */
  325. if (fb_helper->fbdev && fb_helper->fbdev->dev)
  326. drm_fb_helper_unregister_fbi(fb_helper);
  327. if (fb_helper->fbdev)
  328. drm_fbdev_cma_defio_fini(fb_helper->fbdev);
  329. if (fb_helper->fb)
  330. drm_framebuffer_remove(fb_helper->fb);
  331. drm_fb_helper_fini(fb_helper);
  332. kfree(to_fbdev_cma(fb_helper));
  333. }
  334. EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini);
  335. /**
  336. * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
  337. * @dev: DRM device
  338. * @preferred_bpp: Preferred bits per pixel for the device
  339. * @max_conn_count: Maximum number of connectors
  340. * @funcs: fb helper functions, in particular a custom dirty() callback
  341. *
  342. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  343. */
  344. struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
  345. unsigned int preferred_bpp, unsigned int max_conn_count,
  346. const struct drm_framebuffer_funcs *funcs)
  347. {
  348. struct drm_fbdev_cma *fbdev_cma;
  349. struct drm_fb_helper *helper;
  350. int ret;
  351. fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
  352. if (!fbdev_cma) {
  353. dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
  354. return ERR_PTR(-ENOMEM);
  355. }
  356. fbdev_cma->fb_funcs = funcs;
  357. helper = &fbdev_cma->fb_helper;
  358. drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
  359. ret = drm_fb_helper_init(dev, helper, max_conn_count);
  360. if (ret < 0) {
  361. dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
  362. goto err_free;
  363. }
  364. ret = drm_fb_helper_single_add_all_connectors(helper);
  365. if (ret < 0) {
  366. dev_err(dev->dev, "Failed to add connectors.\n");
  367. goto err_drm_fb_helper_fini;
  368. }
  369. ret = drm_fb_helper_initial_config(helper, preferred_bpp);
  370. if (ret < 0) {
  371. dev_err(dev->dev, "Failed to set initial hw configuration.\n");
  372. goto err_drm_fb_helper_fini;
  373. }
  374. return fbdev_cma;
  375. err_drm_fb_helper_fini:
  376. drm_fb_helper_fini(helper);
  377. err_free:
  378. kfree(fbdev_cma);
  379. return ERR_PTR(ret);
  380. }
  381. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
  382. static const struct drm_framebuffer_funcs drm_fb_cma_funcs = {
  383. .destroy = drm_gem_fb_destroy,
  384. .create_handle = drm_gem_fb_create_handle,
  385. };
  386. /**
  387. * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
  388. * @dev: DRM device
  389. * @preferred_bpp: Preferred bits per pixel for the device
  390. * @max_conn_count: Maximum number of connectors
  391. *
  392. * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  393. */
  394. struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
  395. unsigned int preferred_bpp, unsigned int max_conn_count)
  396. {
  397. return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
  398. max_conn_count,
  399. &drm_fb_cma_funcs);
  400. }
  401. EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
  402. /**
  403. * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
  404. * @fbdev_cma: The drm_fbdev_cma struct
  405. */
  406. void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
  407. {
  408. drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
  409. if (fbdev_cma->fb_helper.fbdev)
  410. drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
  411. if (fbdev_cma->fb_helper.fb)
  412. drm_framebuffer_remove(fbdev_cma->fb_helper.fb);
  413. drm_fb_helper_fini(&fbdev_cma->fb_helper);
  414. kfree(fbdev_cma);
  415. }
  416. EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
  417. /**
  418. * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
  419. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  420. *
  421. * This function is usually called from the &drm_driver.lastclose callback.
  422. */
  423. void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
  424. {
  425. if (fbdev_cma)
  426. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
  427. }
  428. EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
  429. /**
  430. * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
  431. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  432. *
  433. * This function is usually called from the &drm_mode_config.output_poll_changed
  434. * callback.
  435. */
  436. void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
  437. {
  438. if (fbdev_cma)
  439. drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
  440. }
  441. EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
  442. /**
  443. * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
  444. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  445. * @state: desired state, zero to resume, non-zero to suspend
  446. *
  447. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  448. * fb_set_suspend implemented by fbdev core.
  449. */
  450. void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
  451. {
  452. if (fbdev_cma)
  453. drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
  454. }
  455. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
  456. /**
  457. * drm_fbdev_cma_set_suspend_unlocked - wrapper around
  458. * drm_fb_helper_set_suspend_unlocked
  459. * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
  460. * @state: desired state, zero to resume, non-zero to suspend
  461. *
  462. * Calls drm_fb_helper_set_suspend, which is a wrapper around
  463. * fb_set_suspend implemented by fbdev core.
  464. */
  465. void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
  466. bool state)
  467. {
  468. if (fbdev_cma)
  469. drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
  470. state);
  471. }
  472. EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);