This source file includes following definitions.
- drm_client_open
- drm_client_close
- drm_client_init
- drm_client_register
- drm_client_release
- drm_client_dev_unregister
- drm_client_dev_hotplug
- drm_client_dev_restore
- drm_client_buffer_delete
- drm_client_buffer_create
- drm_client_buffer_vmap
- drm_client_buffer_vunmap
- drm_client_buffer_rmfb
- drm_client_buffer_addfb
- drm_client_framebuffer_create
- drm_client_framebuffer_delete
- drm_client_debugfs_internal_clients
- drm_client_debugfs_init
1
2
3
4
5
6 #include <linux/list.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/seq_file.h>
10 #include <linux/slab.h>
11
12 #include <drm/drm_client.h>
13 #include <drm/drm_debugfs.h>
14 #include <drm/drm_device.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_file.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_gem.h>
20 #include <drm/drm_mode.h>
21 #include <drm/drm_print.h>
22
23 #include "drm_crtc_internal.h"
24 #include "drm_internal.h"
25
26
27
28
29
30
31
32
33
34 static int drm_client_open(struct drm_client_dev *client)
35 {
36 struct drm_device *dev = client->dev;
37 struct drm_file *file;
38
39 file = drm_file_alloc(dev->primary);
40 if (IS_ERR(file))
41 return PTR_ERR(file);
42
43 mutex_lock(&dev->filelist_mutex);
44 list_add(&file->lhead, &dev->filelist_internal);
45 mutex_unlock(&dev->filelist_mutex);
46
47 client->file = file;
48
49 return 0;
50 }
51
52 static void drm_client_close(struct drm_client_dev *client)
53 {
54 struct drm_device *dev = client->dev;
55
56 mutex_lock(&dev->filelist_mutex);
57 list_del(&client->file->lhead);
58 mutex_unlock(&dev->filelist_mutex);
59
60 drm_file_free(client->file);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
79 const char *name, const struct drm_client_funcs *funcs)
80 {
81 int ret;
82
83 if (!drm_core_check_feature(dev, DRIVER_MODESET) || !dev->driver->dumb_create)
84 return -EOPNOTSUPP;
85
86 if (funcs && !try_module_get(funcs->owner))
87 return -ENODEV;
88
89 client->dev = dev;
90 client->name = name;
91 client->funcs = funcs;
92
93 ret = drm_client_modeset_create(client);
94 if (ret)
95 goto err_put_module;
96
97 ret = drm_client_open(client);
98 if (ret)
99 goto err_free;
100
101 drm_dev_get(dev);
102
103 return 0;
104
105 err_free:
106 drm_client_modeset_free(client);
107 err_put_module:
108 if (funcs)
109 module_put(funcs->owner);
110
111 return ret;
112 }
113 EXPORT_SYMBOL(drm_client_init);
114
115
116
117
118
119
120
121
122
123
124
125 void drm_client_register(struct drm_client_dev *client)
126 {
127 struct drm_device *dev = client->dev;
128
129 mutex_lock(&dev->clientlist_mutex);
130 list_add(&client->list, &dev->clientlist);
131 mutex_unlock(&dev->clientlist_mutex);
132 }
133 EXPORT_SYMBOL(drm_client_register);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 void drm_client_release(struct drm_client_dev *client)
150 {
151 struct drm_device *dev = client->dev;
152
153 DRM_DEV_DEBUG_KMS(dev->dev, "%s\n", client->name);
154
155 drm_client_modeset_free(client);
156 drm_client_close(client);
157 drm_dev_put(dev);
158 if (client->funcs)
159 module_put(client->funcs->owner);
160 }
161 EXPORT_SYMBOL(drm_client_release);
162
163 void drm_client_dev_unregister(struct drm_device *dev)
164 {
165 struct drm_client_dev *client, *tmp;
166
167 if (!drm_core_check_feature(dev, DRIVER_MODESET))
168 return;
169
170 mutex_lock(&dev->clientlist_mutex);
171 list_for_each_entry_safe(client, tmp, &dev->clientlist, list) {
172 list_del(&client->list);
173 if (client->funcs && client->funcs->unregister) {
174 client->funcs->unregister(client);
175 } else {
176 drm_client_release(client);
177 kfree(client);
178 }
179 }
180 mutex_unlock(&dev->clientlist_mutex);
181 }
182
183
184
185
186
187
188
189
190
191
192 void drm_client_dev_hotplug(struct drm_device *dev)
193 {
194 struct drm_client_dev *client;
195 int ret;
196
197 if (!drm_core_check_feature(dev, DRIVER_MODESET))
198 return;
199
200 mutex_lock(&dev->clientlist_mutex);
201 list_for_each_entry(client, &dev->clientlist, list) {
202 if (!client->funcs || !client->funcs->hotplug)
203 continue;
204
205 ret = client->funcs->hotplug(client);
206 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
207 }
208 mutex_unlock(&dev->clientlist_mutex);
209 }
210 EXPORT_SYMBOL(drm_client_dev_hotplug);
211
212 void drm_client_dev_restore(struct drm_device *dev)
213 {
214 struct drm_client_dev *client;
215 int ret;
216
217 if (!drm_core_check_feature(dev, DRIVER_MODESET))
218 return;
219
220 mutex_lock(&dev->clientlist_mutex);
221 list_for_each_entry(client, &dev->clientlist, list) {
222 if (!client->funcs || !client->funcs->restore)
223 continue;
224
225 ret = client->funcs->restore(client);
226 DRM_DEV_DEBUG_KMS(dev->dev, "%s: ret=%d\n", client->name, ret);
227 if (!ret)
228 break;
229 }
230 mutex_unlock(&dev->clientlist_mutex);
231 }
232
233 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
234 {
235 struct drm_device *dev = buffer->client->dev;
236
237 drm_gem_vunmap(buffer->gem, buffer->vaddr);
238
239 if (buffer->gem)
240 drm_gem_object_put_unlocked(buffer->gem);
241
242 if (buffer->handle)
243 drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
244
245 kfree(buffer);
246 }
247
248 static struct drm_client_buffer *
249 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
250 {
251 const struct drm_format_info *info = drm_format_info(format);
252 struct drm_mode_create_dumb dumb_args = { };
253 struct drm_device *dev = client->dev;
254 struct drm_client_buffer *buffer;
255 struct drm_gem_object *obj;
256 int ret;
257
258 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
259 if (!buffer)
260 return ERR_PTR(-ENOMEM);
261
262 buffer->client = client;
263
264 dumb_args.width = width;
265 dumb_args.height = height;
266 dumb_args.bpp = info->cpp[0] * 8;
267 ret = drm_mode_create_dumb(dev, &dumb_args, client->file);
268 if (ret)
269 goto err_delete;
270
271 buffer->handle = dumb_args.handle;
272 buffer->pitch = dumb_args.pitch;
273
274 obj = drm_gem_object_lookup(client->file, dumb_args.handle);
275 if (!obj) {
276 ret = -ENOENT;
277 goto err_delete;
278 }
279
280 buffer->gem = obj;
281
282 return buffer;
283
284 err_delete:
285 drm_client_buffer_delete(buffer);
286
287 return ERR_PTR(ret);
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305 void *drm_client_buffer_vmap(struct drm_client_buffer *buffer)
306 {
307 void *vaddr;
308
309 if (buffer->vaddr)
310 return buffer->vaddr;
311
312
313
314
315
316
317
318
319
320 vaddr = drm_gem_vmap(buffer->gem);
321 if (IS_ERR(vaddr))
322 return vaddr;
323
324 buffer->vaddr = vaddr;
325
326 return vaddr;
327 }
328 EXPORT_SYMBOL(drm_client_buffer_vmap);
329
330
331
332
333
334
335
336
337
338 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer)
339 {
340 drm_gem_vunmap(buffer->gem, buffer->vaddr);
341 buffer->vaddr = NULL;
342 }
343 EXPORT_SYMBOL(drm_client_buffer_vunmap);
344
345 static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
346 {
347 int ret;
348
349 if (!buffer->fb)
350 return;
351
352 ret = drm_mode_rmfb(buffer->client->dev, buffer->fb->base.id, buffer->client->file);
353 if (ret)
354 DRM_DEV_ERROR(buffer->client->dev->dev,
355 "Error removing FB:%u (%d)\n", buffer->fb->base.id, ret);
356
357 buffer->fb = NULL;
358 }
359
360 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
361 u32 width, u32 height, u32 format)
362 {
363 struct drm_client_dev *client = buffer->client;
364 struct drm_mode_fb_cmd fb_req = { };
365 const struct drm_format_info *info;
366 int ret;
367
368 info = drm_format_info(format);
369 fb_req.bpp = info->cpp[0] * 8;
370 fb_req.depth = info->depth;
371 fb_req.width = width;
372 fb_req.height = height;
373 fb_req.handle = buffer->handle;
374 fb_req.pitch = buffer->pitch;
375
376 ret = drm_mode_addfb(client->dev, &fb_req, client->file);
377 if (ret)
378 return ret;
379
380 buffer->fb = drm_framebuffer_lookup(client->dev, buffer->client->file, fb_req.fb_id);
381 if (WARN_ON(!buffer->fb))
382 return -ENOENT;
383
384
385 drm_framebuffer_put(buffer->fb);
386
387 strscpy(buffer->fb->comm, client->name, TASK_COMM_LEN);
388
389 return 0;
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 struct drm_client_buffer *
407 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
408 {
409 struct drm_client_buffer *buffer;
410 int ret;
411
412 buffer = drm_client_buffer_create(client, width, height, format);
413 if (IS_ERR(buffer))
414 return buffer;
415
416 ret = drm_client_buffer_addfb(buffer, width, height, format);
417 if (ret) {
418 drm_client_buffer_delete(buffer);
419 return ERR_PTR(ret);
420 }
421
422 return buffer;
423 }
424 EXPORT_SYMBOL(drm_client_framebuffer_create);
425
426
427
428
429
430 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer)
431 {
432 if (!buffer)
433 return;
434
435 drm_client_buffer_rmfb(buffer);
436 drm_client_buffer_delete(buffer);
437 }
438 EXPORT_SYMBOL(drm_client_framebuffer_delete);
439
440 #ifdef CONFIG_DEBUG_FS
441 static int drm_client_debugfs_internal_clients(struct seq_file *m, void *data)
442 {
443 struct drm_info_node *node = m->private;
444 struct drm_device *dev = node->minor->dev;
445 struct drm_printer p = drm_seq_file_printer(m);
446 struct drm_client_dev *client;
447
448 mutex_lock(&dev->clientlist_mutex);
449 list_for_each_entry(client, &dev->clientlist, list)
450 drm_printf(&p, "%s\n", client->name);
451 mutex_unlock(&dev->clientlist_mutex);
452
453 return 0;
454 }
455
456 static const struct drm_info_list drm_client_debugfs_list[] = {
457 { "internal_clients", drm_client_debugfs_internal_clients, 0 },
458 };
459
460 int drm_client_debugfs_init(struct drm_minor *minor)
461 {
462 return drm_debugfs_create_files(drm_client_debugfs_list,
463 ARRAY_SIZE(drm_client_debugfs_list),
464 minor->debugfs_root, minor);
465 }
466 #endif