drm_client.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018 Noralf Trønnes
  4. */
  5. #include <linux/list.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/slab.h>
  10. #include <drm/drm_client.h>
  11. #include <drm/drm_debugfs.h>
  12. #include <drm/drm_device.h>
  13. #include <drm/drm_drv.h>
  14. #include <drm/drm_file.h>
  15. #include <drm/drm_fourcc.h>
  16. #include <drm/drm_gem.h>
  17. #include <drm/drm_mode.h>
  18. #include <drm/drm_print.h>
  19. #include <drm/drmP.h>
  20. #include "drm_crtc_internal.h"
  21. #include "drm_internal.h"
  22. /**
  23. * DOC: overview
  24. *
  25. * This library provides support for clients running in the kernel like fbdev and bootsplash.
  26. * Currently it's only partially implemented, just enough to support fbdev.
  27. *
  28. * GEM drivers which provide a GEM based dumb buffer with a virtual address are supported.
  29. */
  30. static int drm_client_open(struct drm_client_dev *client)
  31. {
  32. struct drm_device *dev = client->dev;
  33. struct drm_file *file;
  34. file = drm_file_alloc(dev->primary);
  35. if (IS_ERR(file))
  36. return PTR_ERR(file);
  37. mutex_lock(&dev->filelist_mutex);
  38. list_add(&file->lhead, &dev->filelist_internal);
  39. mutex_unlock(&dev->filelist_mutex);
  40. client->file = file;
  41. return 0;
  42. }
  43. static void drm_client_close(struct drm_client_dev *client)
  44. {
  45. struct drm_device *dev = client->dev;
  46. mutex_lock(&dev->filelist_mutex);
  47. list_del(&client->file->lhead);
  48. mutex_unlock(&dev->filelist_mutex);
  49. drm_file_free(client->file);
  50. }
  51. EXPORT_SYMBOL(drm_client_close);
  52. /**
  53. * drm_client_init - Initialise a DRM client
  54. * @dev: DRM device
  55. * @client: DRM client
  56. * @name: Client name
  57. * @funcs: DRM client functions (optional)
  58. *
  59. * This initialises the client and opens a &drm_file. Use drm_client_add() to complete the process.
  60. * The caller needs to hold a reference on @dev before calling this function.
  61. * The client is freed when the &drm_device is unregistered. See drm_client_release().
  62. *
  63. * Returns:
  64. * Zero on success or negative error code on failure.
  65. */
  66. int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
  67. const char *name, const struct drm_client_funcs *funcs)
  68. {
  69. int ret;
  70. if (!drm_core_check_feature(dev, DRIVER_MODESET) ||
  71. !dev->driver->dumb_create || !dev->driver->gem_prime_vmap)
  72. return -ENOTSUPP;
  73. if (funcs && !try_module_get(funcs->owner))
  74. return -ENODEV;
  75. client->dev = dev;
  76. client->name = name;
  77. client->funcs = funcs;
  78. ret = drm_client_open(client);
  79. if (ret)
  80. goto err_put_module;
  81. drm_dev_get(dev);
  82. return 0;
  83. err_put_module:
  84. if (funcs)
  85. module_put(funcs->owner);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(drm_client_init);
  89. /**
  90. * drm_client_add - Add client to the device list
  91. * @client: DRM client
  92. *
  93. * Add the client to the &drm_device client list to activate its callbacks.
  94. * @client must be initialized by a call to drm_client_init(). After
  95. * drm_client_add() it is no longer permissible to call drm_client_release()
  96. * directly (outside the unregister callback), instead cleanup will happen
  97. * automatically on driver unload.
  98. */
  99. void drm_client_add(struct drm_client_dev *client)
  100. {
  101. struct drm_device *dev = client->dev;
  102. mutex_lock(&dev->clientlist_mutex);
  103. list_add(&client->list, &dev->clientlist);
  104. mutex_unlock(&dev->clientlist_mutex);
  105. }
  106. EXPORT_SYMBOL(drm_client_add);
  107. /**
  108. * drm_client_release - Release DRM client resources
  109. * @client: DRM client
  110. *
  111. * Releases resources by closing the &drm_file that was opened by drm_client_init().
  112. * It is called automatically if the &drm_client_funcs.unregister callback is _not_ set.
  113. *
  114. * This function should only be called from the unregister callback. An exception
  115. * is fbdev which cannot free the buffer if userspace has open file descriptors.
  116. *
  117. * Note:
  118. * Clients cannot initiate a release by themselves. This is done to keep the code simple.
  119. * The driver has to be unloaded before the client can be unloaded.
  120. */
  121. void drm_client_release(struct drm_client_dev *client)
  122. {
  123. struct drm_device *dev = client->dev;
  124. DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
  125. drm_client_close(client);
  126. drm_dev_put(dev);
  127. if (client->funcs)
  128. module_put(client->funcs->owner);
  129. }
  130. EXPORT_SYMBOL(drm_client_release);
  131. void drm_client_dev_unregister(struct drm_device *dev)
  132. {
  133. struct drm_client_dev *client, *tmp;
  134. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  135. return;
  136. mutex_lock(&dev->clientlist_mutex);
  137. list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
  138. list_del(&client->list);
  139. if (client->funcs && client->funcs->unregister) {
  140. client->funcs->unregister(client);
  141. } else {
  142. drm_client_release(client);
  143. kfree(client);
  144. }
  145. }
  146. mutex_unlock(&dev->clientlist_mutex);
  147. }
  148. /**
  149. * drm_client_dev_hotplug - Send hotplug event to clients
  150. * @dev: DRM device
  151. *
  152. * This function calls the &drm_client_funcs.hotplug callback on the attached clients.
  153. *
  154. * drm_kms_helper_hotplug_event() calls this function, so drivers that use it
  155. * don't need to call this function themselves.
  156. */
  157. void drm_client_dev_hotplug(struct drm_device *dev)
  158. {
  159. struct drm_client_dev *client;
  160. int ret;
  161. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  162. return;
  163. mutex_lock(&dev->clientlist_mutex);
  164. list_for_each_entry(client, &dev->clientlist, list) {
  165. if (!client->funcs || !client->funcs->hotplug)
  166. continue;
  167. ret = client->funcs->hotplug(client);
  168. DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
  169. }
  170. mutex_unlock(&dev->clientlist_mutex);
  171. }
  172. EXPORT_SYMBOL(drm_client_dev_hotplug);
  173. void drm_client_dev_restore(struct drm_device *dev)
  174. {
  175. struct drm_client_dev *client;
  176. int ret;
  177. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  178. return;
  179. mutex_lock(&dev->clientlist_mutex);
  180. list_for_each_entry(client, &dev->clientlist, list) {
  181. if (!client->funcs || !client->funcs->restore)
  182. continue;
  183. ret = client->funcs->restore(client);
  184. DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
  185. if (!ret) /* The first one to return zero gets the privilege to restore */
  186. break;
  187. }
  188. mutex_unlock(&dev->clientlist_mutex);
  189. }
  190. static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
  191. {
  192. struct drm_device *dev = buffer->client->dev;
  193. if (buffer->vaddr && dev->driver->gem_prime_vunmap)
  194. dev->driver->gem_prime_vunmap(buffer->gem, buffer->vaddr);
  195. if (buffer->gem)
  196. drm_gem_object_put_unlocked(buffer->gem);
  197. if (buffer->handle)
  198. drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
  199. kfree(buffer);
  200. }
  201. static struct drm_client_buffer *
  202. drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
  203. {
  204. struct drm_mode_create_dumb dumb_args = { };
  205. struct drm_device *dev = client->dev;
  206. struct drm_client_buffer *buffer;
  207. struct drm_gem_object *obj;
  208. void *vaddr;
  209. int ret;
  210. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  211. if (!buffer)
  212. return ERR_PTR(-ENOMEM);
  213. buffer->client = client;
  214. dumb_args.width = width;
  215. dumb_args.height = height;
  216. dumb_args.bpp = drm_format_plane_cpp(format, 0) * 8;
  217. ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
  218. if (ret)
  219. goto err_delete;
  220. buffer->handle = dumb_args.handle;
  221. buffer->pitch = dumb_args.pitch;
  222. obj = drm_gem_object_lookup(client->file, dumb_args.handle);
  223. if (!obj) {
  224. ret = -ENOENT;
  225. goto err_delete;
  226. }
  227. buffer->gem = obj;
  228. /*
  229. * FIXME: The dependency on GEM here isn't required, we could
  230. * convert the driver handle to a dma-buf instead and use the
  231. * backend-agnostic dma-buf vmap support instead. This would
  232. * require that the handle2fd prime ioctl is reworked to pull the
  233. * fd_install step out of the driver backend hooks, to make that
  234. * final step optional for internal users.
  235. */
  236. vaddr = dev->driver->gem_prime_vmap(obj);
  237. if (!vaddr) {
  238. ret = -ENOMEM;
  239. goto err_delete;
  240. }
  241. buffer->vaddr = vaddr;
  242. return buffer;
  243. err_delete:
  244. drm_client_buffer_delete(buffer);
  245. return ERR_PTR(ret);
  246. }
  247. static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
  248. {
  249. int ret;
  250. if (!buffer->fb)
  251. return;
  252. ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
  253. if (ret)
  254. DRM_DEV_ERROR(buffer->client->dev->dev,
  255. "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
  256. buffer->fb = NULL;
  257. }
  258. static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
  259. u32 width, u32 height, u32 format)
  260. {
  261. struct drm_client_dev *client = buffer->client;
  262. struct drm_mode_fb_cmd fb_req = { };
  263. const struct drm_format_info *info;
  264. int ret;
  265. info = drm_format_info(format);
  266. fb_req.bpp = info->cpp[0] * 8;
  267. fb_req.depth = info->depth;
  268. fb_req.width = width;
  269. fb_req.height = height;
  270. fb_req.handle = buffer->handle;
  271. fb_req.pitch = buffer->pitch;
  272. ret = drm_mode_addfb(client->dev, &fb_req, client->file);
  273. if (ret)
  274. return ret;
  275. buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
  276. if (WARN_ON(!buffer->fb))
  277. return -ENOENT;
  278. /* drop the reference we picked up in framebuffer lookup */
  279. drm_framebuffer_put(buffer->fb);
  280. strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
  281. return 0;
  282. }
  283. /**
  284. * drm_client_framebuffer_create - Create a client framebuffer
  285. * @client: DRM client
  286. * @width: Framebuffer width
  287. * @height: Framebuffer height
  288. * @format: Buffer format
  289. *
  290. * This function creates a &drm_client_buffer which consists of a
  291. * &drm_framebuffer backed by a dumb buffer.
  292. * Call drm_client_framebuffer_delete() to free the buffer.
  293. *
  294. * Returns:
  295. * Pointer to a client buffer or an error pointer on failure.
  296. */
  297. struct drm_client_buffer *
  298. drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
  299. {
  300. struct drm_client_buffer *buffer;
  301. int ret;
  302. buffer = drm_client_buffer_create(client, width, height, format);
  303. if (IS_ERR(buffer))
  304. return buffer;
  305. ret = drm_client_buffer_addfb(buffer, width, height, format);
  306. if (ret) {
  307. drm_client_buffer_delete(buffer);
  308. return ERR_PTR(ret);
  309. }
  310. return buffer;
  311. }
  312. EXPORT_SYMBOL(drm_client_framebuffer_create);
  313. /**
  314. * drm_client_framebuffer_delete - Delete a client framebuffer
  315. * @buffer: DRM client buffer (can be NULL)
  316. */
  317. void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
  318. {
  319. if (!buffer)
  320. return;
  321. drm_client_buffer_rmfb(buffer);
  322. drm_client_buffer_delete(buffer);
  323. }
  324. EXPORT_SYMBOL(drm_client_framebuffer_delete);
  325. #ifdef CONFIG_DEBUG_FS
  326. static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
  327. {
  328. struct drm_info_node *node = m->private;
  329. struct drm_device *dev = node->minor->dev;
  330. struct drm_printer p = drm_seq_file_printer(m);
  331. struct drm_client_dev *client;
  332. mutex_lock(&dev->clientlist_mutex);
  333. list_for_each_entry(client, &dev->clientlist, list)
  334. drm_printf(&p, "%s\n", client->name);
  335. mutex_unlock(&dev->clientlist_mutex);
  336. return 0;
  337. }
  338. static const struct drm_info_list drm_client_debugfs_list[] = {
  339. { "internal_clients", drm_client_debugfs_internal_clients, 0 },
  340. };
  341. int drm_client_debugfs_init(struct drm_minor *minor)
  342. {
  343. return drm_debugfs_create_files(drm_client_debugfs_list,
  344. ARRAY_SIZE(drm_client_debugfs_list),
  345. minor->debugfs_root, minor);
  346. }
  347. #endif