66629f398f113fe41132fde973f0b89b25dea02c
[deliverable/linux.git] / drivers / gpu / host1x / drm / drm.c
1 /*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013 NVIDIA CORPORATION. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13
14 #include <linux/dma-mapping.h>
15 #include <asm/dma-iommu.h>
16
17 #include <drm/drm.h>
18 #include <drm/drmP.h>
19
20 #include "host1x_client.h"
21 #include "dev.h"
22 #include "drm.h"
23 #include "gem.h"
24 #include "syncpt.h"
25
26 #define DRIVER_NAME "tegra"
27 #define DRIVER_DESC "NVIDIA Tegra graphics"
28 #define DRIVER_DATE "20120330"
29 #define DRIVER_MAJOR 0
30 #define DRIVER_MINOR 0
31 #define DRIVER_PATCHLEVEL 0
32
33 struct host1x_drm_client {
34 struct host1x_client *client;
35 struct device_node *np;
36 struct list_head list;
37 };
38
39 static int host1x_add_drm_client(struct host1x_drm *host1x,
40 struct device_node *np)
41 {
42 struct host1x_drm_client *client;
43
44 client = kzalloc(sizeof(*client), GFP_KERNEL);
45 if (!client)
46 return -ENOMEM;
47
48 INIT_LIST_HEAD(&client->list);
49 client->np = of_node_get(np);
50
51 list_add_tail(&client->list, &host1x->drm_clients);
52
53 return 0;
54 }
55
56 static int host1x_activate_drm_client(struct host1x_drm *host1x,
57 struct host1x_drm_client *drm,
58 struct host1x_client *client)
59 {
60 mutex_lock(&host1x->drm_clients_lock);
61 list_del_init(&drm->list);
62 list_add_tail(&drm->list, &host1x->drm_active);
63 drm->client = client;
64 mutex_unlock(&host1x->drm_clients_lock);
65
66 return 0;
67 }
68
69 static int host1x_remove_drm_client(struct host1x_drm *host1x,
70 struct host1x_drm_client *client)
71 {
72 mutex_lock(&host1x->drm_clients_lock);
73 list_del_init(&client->list);
74 mutex_unlock(&host1x->drm_clients_lock);
75
76 of_node_put(client->np);
77 kfree(client);
78
79 return 0;
80 }
81
82 static int host1x_parse_dt(struct host1x_drm *host1x)
83 {
84 static const char * const compat[] = {
85 "nvidia,tegra20-dc",
86 "nvidia,tegra20-hdmi",
87 "nvidia,tegra20-gr2d",
88 "nvidia,tegra30-dc",
89 "nvidia,tegra30-hdmi",
90 "nvidia,tegra30-gr2d",
91 };
92 unsigned int i;
93 int err;
94
95 for (i = 0; i < ARRAY_SIZE(compat); i++) {
96 struct device_node *np;
97
98 for_each_child_of_node(host1x->dev->of_node, np) {
99 if (of_device_is_compatible(np, compat[i]) &&
100 of_device_is_available(np)) {
101 err = host1x_add_drm_client(host1x, np);
102 if (err < 0)
103 return err;
104 }
105 }
106 }
107
108 return 0;
109 }
110
111 int host1x_drm_alloc(struct platform_device *pdev)
112 {
113 struct host1x_drm *host1x;
114 int err;
115
116 host1x = devm_kzalloc(&pdev->dev, sizeof(*host1x), GFP_KERNEL);
117 if (!host1x)
118 return -ENOMEM;
119
120 mutex_init(&host1x->drm_clients_lock);
121 INIT_LIST_HEAD(&host1x->drm_clients);
122 INIT_LIST_HEAD(&host1x->drm_active);
123 mutex_init(&host1x->clients_lock);
124 INIT_LIST_HEAD(&host1x->clients);
125 host1x->dev = &pdev->dev;
126
127 err = host1x_parse_dt(host1x);
128 if (err < 0) {
129 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
130 return err;
131 }
132
133 host1x_set_drm_data(&pdev->dev, host1x);
134
135 return 0;
136 }
137
138 int host1x_drm_init(struct host1x_drm *host1x, struct drm_device *drm)
139 {
140 struct host1x_client *client;
141
142 mutex_lock(&host1x->clients_lock);
143
144 list_for_each_entry(client, &host1x->clients, list) {
145 if (client->ops && client->ops->drm_init) {
146 int err = client->ops->drm_init(client, drm);
147 if (err < 0) {
148 dev_err(host1x->dev,
149 "DRM setup failed for %s: %d\n",
150 dev_name(client->dev), err);
151 mutex_unlock(&host1x->clients_lock);
152 return err;
153 }
154 }
155 }
156
157 mutex_unlock(&host1x->clients_lock);
158
159 return 0;
160 }
161
162 int host1x_drm_exit(struct host1x_drm *host1x)
163 {
164 struct platform_device *pdev = to_platform_device(host1x->dev);
165 struct host1x_client *client;
166
167 if (!host1x->drm)
168 return 0;
169
170 mutex_lock(&host1x->clients_lock);
171
172 list_for_each_entry_reverse(client, &host1x->clients, list) {
173 if (client->ops && client->ops->drm_exit) {
174 int err = client->ops->drm_exit(client);
175 if (err < 0) {
176 dev_err(host1x->dev,
177 "DRM cleanup failed for %s: %d\n",
178 dev_name(client->dev), err);
179 mutex_unlock(&host1x->clients_lock);
180 return err;
181 }
182 }
183 }
184
185 mutex_unlock(&host1x->clients_lock);
186
187 drm_platform_exit(&tegra_drm_driver, pdev);
188 host1x->drm = NULL;
189
190 return 0;
191 }
192
193 int host1x_register_client(struct host1x_drm *host1x,
194 struct host1x_client *client)
195 {
196 struct host1x_drm_client *drm, *tmp;
197 int err;
198
199 mutex_lock(&host1x->clients_lock);
200 list_add_tail(&client->list, &host1x->clients);
201 mutex_unlock(&host1x->clients_lock);
202
203 list_for_each_entry_safe(drm, tmp, &host1x->drm_clients, list)
204 if (drm->np == client->dev->of_node)
205 host1x_activate_drm_client(host1x, drm, client);
206
207 if (list_empty(&host1x->drm_clients)) {
208 struct platform_device *pdev = to_platform_device(host1x->dev);
209
210 err = drm_platform_init(&tegra_drm_driver, pdev);
211 if (err < 0) {
212 dev_err(host1x->dev, "drm_platform_init(): %d\n", err);
213 return err;
214 }
215 }
216
217 return 0;
218 }
219
220 int host1x_unregister_client(struct host1x_drm *host1x,
221 struct host1x_client *client)
222 {
223 struct host1x_drm_client *drm, *tmp;
224 int err;
225
226 list_for_each_entry_safe(drm, tmp, &host1x->drm_active, list) {
227 if (drm->client == client) {
228 err = host1x_drm_exit(host1x);
229 if (err < 0) {
230 dev_err(host1x->dev, "host1x_drm_exit(): %d\n",
231 err);
232 return err;
233 }
234
235 host1x_remove_drm_client(host1x, drm);
236 break;
237 }
238 }
239
240 mutex_lock(&host1x->clients_lock);
241 list_del_init(&client->list);
242 mutex_unlock(&host1x->clients_lock);
243
244 return 0;
245 }
246
247 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
248 {
249 struct host1x_drm *host1x;
250 int err;
251
252 host1x = host1x_get_drm_data(drm->dev);
253 drm->dev_private = host1x;
254 host1x->drm = drm;
255
256 drm_mode_config_init(drm);
257
258 err = host1x_drm_init(host1x, drm);
259 if (err < 0)
260 return err;
261
262 /*
263 * We don't use the drm_irq_install() helpers provided by the DRM
264 * core, so we need to set this manually in order to allow the
265 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
266 */
267 drm->irq_enabled = 1;
268
269 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
270 if (err < 0)
271 return err;
272
273 err = tegra_drm_fb_init(drm);
274 if (err < 0)
275 return err;
276
277 drm_kms_helper_poll_init(drm);
278
279 return 0;
280 }
281
282 static int tegra_drm_unload(struct drm_device *drm)
283 {
284 drm_kms_helper_poll_fini(drm);
285 tegra_drm_fb_exit(drm);
286
287 drm_mode_config_cleanup(drm);
288
289 return 0;
290 }
291
292 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
293 {
294 struct host1x_drm_file *fpriv;
295
296 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
297 if (!fpriv)
298 return -ENOMEM;
299
300 INIT_LIST_HEAD(&fpriv->contexts);
301 filp->driver_priv = fpriv;
302
303 return 0;
304 }
305
306 static void host1x_drm_context_free(struct host1x_drm_context *context)
307 {
308 context->client->ops->close_channel(context);
309 kfree(context);
310 }
311
312 static void tegra_drm_lastclose(struct drm_device *drm)
313 {
314 struct host1x_drm *host1x = drm->dev_private;
315
316 tegra_fbdev_restore_mode(host1x->fbdev);
317 }
318
319 #ifdef CONFIG_DRM_TEGRA_STAGING
320 static bool host1x_drm_file_owns_context(struct host1x_drm_file *file,
321 struct host1x_drm_context *context)
322 {
323 struct host1x_drm_context *ctx;
324
325 list_for_each_entry(ctx, &file->contexts, list)
326 if (ctx == context)
327 return true;
328
329 return false;
330 }
331
332 static int tegra_gem_create(struct drm_device *drm, void *data,
333 struct drm_file *file)
334 {
335 struct drm_tegra_gem_create *args = data;
336 struct tegra_bo *bo;
337
338 bo = tegra_bo_create_with_handle(file, drm, args->size,
339 &args->handle);
340 if (IS_ERR(bo))
341 return PTR_ERR(bo);
342
343 return 0;
344 }
345
346 static int tegra_gem_mmap(struct drm_device *drm, void *data,
347 struct drm_file *file)
348 {
349 struct drm_tegra_gem_mmap *args = data;
350 struct drm_gem_object *gem;
351 struct tegra_bo *bo;
352
353 gem = drm_gem_object_lookup(drm, file, args->handle);
354 if (!gem)
355 return -EINVAL;
356
357 bo = to_tegra_bo(gem);
358
359 args->offset = tegra_bo_get_mmap_offset(bo);
360
361 drm_gem_object_unreference(gem);
362
363 return 0;
364 }
365
366 static int tegra_syncpt_read(struct drm_device *drm, void *data,
367 struct drm_file *file)
368 {
369 struct drm_tegra_syncpt_read *args = data;
370 struct host1x *host = dev_get_drvdata(drm->dev);
371 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
372
373 if (!sp)
374 return -EINVAL;
375
376 args->value = host1x_syncpt_read_min(sp);
377 return 0;
378 }
379
380 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
381 struct drm_file *file)
382 {
383 struct drm_tegra_syncpt_incr *args = data;
384 struct host1x *host = dev_get_drvdata(drm->dev);
385 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
386
387 if (!sp)
388 return -EINVAL;
389
390 host1x_syncpt_incr(sp);
391 return 0;
392 }
393
394 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
395 struct drm_file *file)
396 {
397 struct drm_tegra_syncpt_wait *args = data;
398 struct host1x *host = dev_get_drvdata(drm->dev);
399 struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
400
401 if (!sp)
402 return -EINVAL;
403
404 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
405 &args->value);
406 }
407
408 static int tegra_open_channel(struct drm_device *drm, void *data,
409 struct drm_file *file)
410 {
411 struct drm_tegra_open_channel *args = data;
412 struct host1x_client *client;
413 struct host1x_drm_context *context;
414 struct host1x_drm_file *fpriv = file->driver_priv;
415 struct host1x_drm *host1x = drm->dev_private;
416 int err = -ENODEV;
417
418 context = kzalloc(sizeof(*context), GFP_KERNEL);
419 if (!context)
420 return -ENOMEM;
421
422 list_for_each_entry(client, &host1x->clients, list)
423 if (client->class == args->client) {
424 err = client->ops->open_channel(client, context);
425 if (err)
426 break;
427
428 context->client = client;
429 list_add(&context->list, &fpriv->contexts);
430 args->context = (uintptr_t)context;
431 return 0;
432 }
433
434 kfree(context);
435 return err;
436 }
437
438 static int tegra_close_channel(struct drm_device *drm, void *data,
439 struct drm_file *file)
440 {
441 struct drm_tegra_close_channel *args = data;
442 struct host1x_drm_file *fpriv = file->driver_priv;
443 struct host1x_drm_context *context =
444 (struct host1x_drm_context *)(uintptr_t)args->context;
445
446 if (!host1x_drm_file_owns_context(fpriv, context))
447 return -EINVAL;
448
449 list_del(&context->list);
450 host1x_drm_context_free(context);
451
452 return 0;
453 }
454
455 static int tegra_get_syncpt(struct drm_device *drm, void *data,
456 struct drm_file *file)
457 {
458 struct drm_tegra_get_syncpt *args = data;
459 struct host1x_drm_file *fpriv = file->driver_priv;
460 struct host1x_drm_context *context =
461 (struct host1x_drm_context *)(uintptr_t)args->context;
462 struct host1x_syncpt *syncpt;
463
464 if (!host1x_drm_file_owns_context(fpriv, context))
465 return -ENODEV;
466
467 if (args->index >= context->client->num_syncpts)
468 return -EINVAL;
469
470 syncpt = context->client->syncpts[args->index];
471 args->id = host1x_syncpt_id(syncpt);
472
473 return 0;
474 }
475
476 static int tegra_submit(struct drm_device *drm, void *data,
477 struct drm_file *file)
478 {
479 struct drm_tegra_submit *args = data;
480 struct host1x_drm_file *fpriv = file->driver_priv;
481 struct host1x_drm_context *context =
482 (struct host1x_drm_context *)(uintptr_t)args->context;
483
484 if (!host1x_drm_file_owns_context(fpriv, context))
485 return -ENODEV;
486
487 return context->client->ops->submit(context, args, drm, file);
488 }
489 #endif
490
491 static struct drm_ioctl_desc tegra_drm_ioctls[] = {
492 #ifdef CONFIG_DRM_TEGRA_STAGING
493 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
494 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
495 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
496 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
497 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
498 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
499 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
500 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
501 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
502 #endif
503 };
504
505 static const struct file_operations tegra_drm_fops = {
506 .owner = THIS_MODULE,
507 .open = drm_open,
508 .release = drm_release,
509 .unlocked_ioctl = drm_ioctl,
510 .mmap = tegra_drm_mmap,
511 .poll = drm_poll,
512 .fasync = drm_fasync,
513 .read = drm_read,
514 #ifdef CONFIG_COMPAT
515 .compat_ioctl = drm_compat_ioctl,
516 #endif
517 .llseek = noop_llseek,
518 };
519
520 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
521 {
522 struct drm_crtc *crtc;
523
524 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
525 struct tegra_dc *dc = to_tegra_dc(crtc);
526
527 if (dc->pipe == pipe)
528 return crtc;
529 }
530
531 return NULL;
532 }
533
534 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
535 {
536 /* TODO: implement real hardware counter using syncpoints */
537 return drm_vblank_count(dev, crtc);
538 }
539
540 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
541 {
542 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
543 struct tegra_dc *dc = to_tegra_dc(crtc);
544
545 if (!crtc)
546 return -ENODEV;
547
548 tegra_dc_enable_vblank(dc);
549
550 return 0;
551 }
552
553 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
554 {
555 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
556 struct tegra_dc *dc = to_tegra_dc(crtc);
557
558 if (crtc)
559 tegra_dc_disable_vblank(dc);
560 }
561
562 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
563 {
564 struct host1x_drm_file *fpriv = file->driver_priv;
565 struct host1x_drm_context *context, *tmp;
566 struct drm_crtc *crtc;
567
568 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
569 tegra_dc_cancel_page_flip(crtc, file);
570
571 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
572 host1x_drm_context_free(context);
573
574 kfree(fpriv);
575 }
576
577 #ifdef CONFIG_DEBUG_FS
578 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
579 {
580 struct drm_info_node *node = (struct drm_info_node *)s->private;
581 struct drm_device *drm = node->minor->dev;
582 struct drm_framebuffer *fb;
583
584 mutex_lock(&drm->mode_config.fb_lock);
585
586 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
587 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
588 fb->base.id, fb->width, fb->height, fb->depth,
589 fb->bits_per_pixel,
590 atomic_read(&fb->refcount.refcount));
591 }
592
593 mutex_unlock(&drm->mode_config.fb_lock);
594
595 return 0;
596 }
597
598 static struct drm_info_list tegra_debugfs_list[] = {
599 { "framebuffers", tegra_debugfs_framebuffers, 0 },
600 };
601
602 static int tegra_debugfs_init(struct drm_minor *minor)
603 {
604 return drm_debugfs_create_files(tegra_debugfs_list,
605 ARRAY_SIZE(tegra_debugfs_list),
606 minor->debugfs_root, minor);
607 }
608
609 static void tegra_debugfs_cleanup(struct drm_minor *minor)
610 {
611 drm_debugfs_remove_files(tegra_debugfs_list,
612 ARRAY_SIZE(tegra_debugfs_list), minor);
613 }
614 #endif
615
616 struct drm_driver tegra_drm_driver = {
617 .driver_features = DRIVER_BUS_PLATFORM | DRIVER_MODESET | DRIVER_GEM,
618 .load = tegra_drm_load,
619 .unload = tegra_drm_unload,
620 .open = tegra_drm_open,
621 .preclose = tegra_drm_preclose,
622 .lastclose = tegra_drm_lastclose,
623
624 .get_vblank_counter = tegra_drm_get_vblank_counter,
625 .enable_vblank = tegra_drm_enable_vblank,
626 .disable_vblank = tegra_drm_disable_vblank,
627
628 #if defined(CONFIG_DEBUG_FS)
629 .debugfs_init = tegra_debugfs_init,
630 .debugfs_cleanup = tegra_debugfs_cleanup,
631 #endif
632
633 .gem_free_object = tegra_bo_free_object,
634 .gem_vm_ops = &tegra_bo_vm_ops,
635 .dumb_create = tegra_bo_dumb_create,
636 .dumb_map_offset = tegra_bo_dumb_map_offset,
637 .dumb_destroy = tegra_bo_dumb_destroy,
638
639 .ioctls = tegra_drm_ioctls,
640 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
641 .fops = &tegra_drm_fops,
642
643 .name = DRIVER_NAME,
644 .desc = DRIVER_DESC,
645 .date = DRIVER_DATE,
646 .major = DRIVER_MAJOR,
647 .minor = DRIVER_MINOR,
648 .patchlevel = DRIVER_PATCHLEVEL,
649 };
This page took 0.106025 seconds and 5 git commands to generate.