1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012-2013, NVIDIA Corporation. 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/iommu.h> 8 #include <linux/module.h> 9 #include <linux/of_device.h> 10 11 #include "drm.h" 12 #include "gem.h" 13 #include "gr2d.h" 14 15 struct gr2d_soc { 16 unsigned int version; 17 }; 18 19 struct gr2d { 20 struct iommu_group *group; 21 struct tegra_drm_client client; 22 struct host1x_channel *channel; 23 struct clk *clk; 24 25 const struct gr2d_soc *soc; 26 27 DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS); 28 }; 29 30 static inline struct gr2d *to_gr2d(struct tegra_drm_client *client) 31 { 32 return container_of(client, struct gr2d, client); 33 } 34 35 static int gr2d_init(struct host1x_client *client) 36 { 37 struct tegra_drm_client *drm = host1x_to_drm_client(client); 38 struct drm_device *dev = dev_get_drvdata(client->parent); 39 unsigned long flags = HOST1X_SYNCPT_HAS_BASE; 40 struct gr2d *gr2d = to_gr2d(drm); 41 int err; 42 43 gr2d->channel = host1x_channel_request(client->dev); 44 if (!gr2d->channel) 45 return -ENOMEM; 46 47 client->syncpts[0] = host1x_syncpt_request(client, flags); 48 if (!client->syncpts[0]) { 49 err = -ENOMEM; 50 dev_err(client->dev, "failed to request syncpoint: %d\n", err); 51 goto put; 52 } 53 54 gr2d->group = host1x_client_iommu_attach(client, false); 55 if (IS_ERR(gr2d->group)) { 56 err = PTR_ERR(gr2d->group); 57 dev_err(client->dev, "failed to attach to domain: %d\n", err); 58 goto free; 59 } 60 61 err = tegra_drm_register_client(dev->dev_private, drm); 62 if (err < 0) { 63 dev_err(client->dev, "failed to register client: %d\n", err); 64 goto detach; 65 } 66 67 return 0; 68 69 detach: 70 host1x_client_iommu_detach(client, gr2d->group); 71 free: 72 host1x_syncpt_free(client->syncpts[0]); 73 put: 74 host1x_channel_put(gr2d->channel); 75 return err; 76 } 77 78 static int gr2d_exit(struct host1x_client *client) 79 { 80 struct tegra_drm_client *drm = host1x_to_drm_client(client); 81 struct drm_device *dev = dev_get_drvdata(client->parent); 82 struct tegra_drm *tegra = dev->dev_private; 83 struct gr2d *gr2d = to_gr2d(drm); 84 int err; 85 86 err = tegra_drm_unregister_client(tegra, drm); 87 if (err < 0) 88 return err; 89 90 host1x_client_iommu_detach(client, gr2d->group); 91 host1x_syncpt_free(client->syncpts[0]); 92 host1x_channel_put(gr2d->channel); 93 94 return 0; 95 } 96 97 static const struct host1x_client_ops gr2d_client_ops = { 98 .init = gr2d_init, 99 .exit = gr2d_exit, 100 }; 101 102 static int gr2d_open_channel(struct tegra_drm_client *client, 103 struct tegra_drm_context *context) 104 { 105 struct gr2d *gr2d = to_gr2d(client); 106 107 context->channel = host1x_channel_get(gr2d->channel); 108 if (!context->channel) 109 return -ENOMEM; 110 111 return 0; 112 } 113 114 static void gr2d_close_channel(struct tegra_drm_context *context) 115 { 116 host1x_channel_put(context->channel); 117 } 118 119 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset) 120 { 121 struct gr2d *gr2d = dev_get_drvdata(dev); 122 123 switch (class) { 124 case HOST1X_CLASS_HOST1X: 125 if (offset == 0x2b) 126 return 1; 127 128 break; 129 130 case HOST1X_CLASS_GR2D: 131 case HOST1X_CLASS_GR2D_SB: 132 if (offset >= GR2D_NUM_REGS) 133 break; 134 135 if (test_bit(offset, gr2d->addr_regs)) 136 return 1; 137 138 break; 139 } 140 141 return 0; 142 } 143 144 static int gr2d_is_valid_class(u32 class) 145 { 146 return (class == HOST1X_CLASS_GR2D || 147 class == HOST1X_CLASS_GR2D_SB); 148 } 149 150 static const struct tegra_drm_client_ops gr2d_ops = { 151 .open_channel = gr2d_open_channel, 152 .close_channel = gr2d_close_channel, 153 .is_addr_reg = gr2d_is_addr_reg, 154 .is_valid_class = gr2d_is_valid_class, 155 .submit = tegra_drm_submit, 156 }; 157 158 static const struct gr2d_soc tegra20_gr2d_soc = { 159 .version = 0x20, 160 }; 161 162 static const struct gr2d_soc tegra30_gr2d_soc = { 163 .version = 0x30, 164 }; 165 166 static const struct of_device_id gr2d_match[] = { 167 { .compatible = "nvidia,tegra30-gr2d", .data = &tegra20_gr2d_soc }, 168 { .compatible = "nvidia,tegra20-gr2d", .data = &tegra30_gr2d_soc }, 169 { }, 170 }; 171 MODULE_DEVICE_TABLE(of, gr2d_match); 172 173 static const u32 gr2d_addr_regs[] = { 174 GR2D_UA_BASE_ADDR, 175 GR2D_VA_BASE_ADDR, 176 GR2D_PAT_BASE_ADDR, 177 GR2D_DSTA_BASE_ADDR, 178 GR2D_DSTB_BASE_ADDR, 179 GR2D_DSTC_BASE_ADDR, 180 GR2D_SRCA_BASE_ADDR, 181 GR2D_SRCB_BASE_ADDR, 182 GR2D_SRC_BASE_ADDR_SB, 183 GR2D_DSTA_BASE_ADDR_SB, 184 GR2D_DSTB_BASE_ADDR_SB, 185 GR2D_UA_BASE_ADDR_SB, 186 GR2D_VA_BASE_ADDR_SB, 187 }; 188 189 static int gr2d_probe(struct platform_device *pdev) 190 { 191 struct device *dev = &pdev->dev; 192 struct host1x_syncpt **syncpts; 193 struct gr2d *gr2d; 194 unsigned int i; 195 int err; 196 197 gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL); 198 if (!gr2d) 199 return -ENOMEM; 200 201 gr2d->soc = of_device_get_match_data(dev); 202 203 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL); 204 if (!syncpts) 205 return -ENOMEM; 206 207 gr2d->clk = devm_clk_get(dev, NULL); 208 if (IS_ERR(gr2d->clk)) { 209 dev_err(dev, "cannot get clock\n"); 210 return PTR_ERR(gr2d->clk); 211 } 212 213 err = clk_prepare_enable(gr2d->clk); 214 if (err) { 215 dev_err(dev, "cannot turn on clock\n"); 216 return err; 217 } 218 219 INIT_LIST_HEAD(&gr2d->client.base.list); 220 gr2d->client.base.ops = &gr2d_client_ops; 221 gr2d->client.base.dev = dev; 222 gr2d->client.base.class = HOST1X_CLASS_GR2D; 223 gr2d->client.base.syncpts = syncpts; 224 gr2d->client.base.num_syncpts = 1; 225 226 INIT_LIST_HEAD(&gr2d->client.list); 227 gr2d->client.version = gr2d->soc->version; 228 gr2d->client.ops = &gr2d_ops; 229 230 err = host1x_client_register(&gr2d->client.base); 231 if (err < 0) { 232 dev_err(dev, "failed to register host1x client: %d\n", err); 233 clk_disable_unprepare(gr2d->clk); 234 return err; 235 } 236 237 /* initialize address register map */ 238 for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++) 239 set_bit(gr2d_addr_regs[i], gr2d->addr_regs); 240 241 platform_set_drvdata(pdev, gr2d); 242 243 return 0; 244 } 245 246 static int gr2d_remove(struct platform_device *pdev) 247 { 248 struct gr2d *gr2d = platform_get_drvdata(pdev); 249 int err; 250 251 err = host1x_client_unregister(&gr2d->client.base); 252 if (err < 0) { 253 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n", 254 err); 255 return err; 256 } 257 258 clk_disable_unprepare(gr2d->clk); 259 260 return 0; 261 } 262 263 struct platform_driver tegra_gr2d_driver = { 264 .driver = { 265 .name = "tegra-gr2d", 266 .of_match_table = gr2d_match, 267 }, 268 .probe = gr2d_probe, 269 .remove = gr2d_remove, 270 }; 271