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