Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Tegra host1x Channel |
| 4 | * |
| 5 | * Copyright (c) 2010-2013, NVIDIA Corporation. |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/module.h> |
| 10 | |
| 11 | #include "channel.h" |
| 12 | #include "dev.h" |
| 13 | #include "job.h" |
| 14 | |
| 15 | /* Constructor for the host1x device list */ |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 16 | int host1x_channel_list_init(struct host1x_channel_list *chlist, |
| 17 | unsigned int num_channels) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 18 | { |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 19 | chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel), |
| 20 | GFP_KERNEL); |
| 21 | if (!chlist->channels) |
| 22 | return -ENOMEM; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 23 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 24 | chlist->allocated_channels = |
| 25 | kcalloc(BITS_TO_LONGS(num_channels), sizeof(unsigned long), |
| 26 | GFP_KERNEL); |
| 27 | if (!chlist->allocated_channels) { |
| 28 | kfree(chlist->channels); |
| 29 | return -ENOMEM; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 32 | bitmap_zero(chlist->allocated_channels, num_channels); |
| 33 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 37 | void host1x_channel_list_free(struct host1x_channel_list *chlist) |
| 38 | { |
| 39 | kfree(chlist->allocated_channels); |
| 40 | kfree(chlist->channels); |
| 41 | } |
| 42 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 43 | int host1x_job_submit(struct host1x_job *job) |
| 44 | { |
| 45 | struct host1x *host = dev_get_drvdata(job->channel->dev->parent); |
| 46 | |
| 47 | return host1x_hw_channel_submit(host, job); |
| 48 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 49 | EXPORT_SYMBOL(host1x_job_submit); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 50 | |
| 51 | struct host1x_channel *host1x_channel_get(struct host1x_channel *channel) |
| 52 | { |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 53 | kref_get(&channel->refcount); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 54 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 55 | return channel; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 56 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 57 | EXPORT_SYMBOL(host1x_channel_get); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 58 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 59 | /** |
| 60 | * host1x_channel_get_index() - Attempt to get channel reference by index |
| 61 | * @host: Host1x device object |
| 62 | * @index: Index of channel |
| 63 | * |
| 64 | * If channel number @index is currently allocated, increase its refcount |
| 65 | * and return a pointer to it. Otherwise, return NULL. |
| 66 | */ |
| 67 | struct host1x_channel *host1x_channel_get_index(struct host1x *host, |
| 68 | unsigned int index) |
| 69 | { |
| 70 | struct host1x_channel *ch = &host->channel_list.channels[index]; |
| 71 | |
| 72 | if (!kref_get_unless_zero(&ch->refcount)) |
| 73 | return NULL; |
| 74 | |
| 75 | return ch; |
| 76 | } |
| 77 | |
Dmitry Osipenko | 9ca790f | 2021-12-01 02:23:16 +0300 | [diff] [blame] | 78 | void host1x_channel_stop(struct host1x_channel *channel) |
| 79 | { |
| 80 | struct host1x *host = dev_get_drvdata(channel->dev->parent); |
| 81 | |
| 82 | host1x_hw_cdma_stop(host, &channel->cdma); |
| 83 | } |
| 84 | EXPORT_SYMBOL(host1x_channel_stop); |
| 85 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 86 | static void release_channel(struct kref *kref) |
| 87 | { |
| 88 | struct host1x_channel *channel = |
| 89 | container_of(kref, struct host1x_channel, refcount); |
| 90 | struct host1x *host = dev_get_drvdata(channel->dev->parent); |
| 91 | struct host1x_channel_list *chlist = &host->channel_list; |
| 92 | |
| 93 | host1x_hw_cdma_stop(host, &channel->cdma); |
| 94 | host1x_cdma_deinit(&channel->cdma); |
| 95 | |
| 96 | clear_bit(channel->id, chlist->allocated_channels); |
| 97 | } |
| 98 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 99 | void host1x_channel_put(struct host1x_channel *channel) |
| 100 | { |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 101 | kref_put(&channel->refcount, release_channel); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 102 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 103 | EXPORT_SYMBOL(host1x_channel_put); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 104 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 105 | static struct host1x_channel *acquire_unused_channel(struct host1x *host) |
| 106 | { |
| 107 | struct host1x_channel_list *chlist = &host->channel_list; |
| 108 | unsigned int max_channels = host->info->nb_channels; |
| 109 | unsigned int index; |
| 110 | |
| 111 | index = find_first_zero_bit(chlist->allocated_channels, max_channels); |
| 112 | if (index >= max_channels) { |
| 113 | dev_err(host->dev, "failed to find free channel\n"); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | chlist->channels[index].id = index; |
| 118 | |
| 119 | set_bit(index, chlist->allocated_channels); |
| 120 | |
| 121 | return &chlist->channels[index]; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * host1x_channel_request() - Allocate a channel |
Thierry Reding | caccddc | 2018-06-18 14:01:51 +0200 | [diff] [blame] | 126 | * @client: Host1x client this channel will be used to send commands to |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 127 | * |
Thierry Reding | caccddc | 2018-06-18 14:01:51 +0200 | [diff] [blame] | 128 | * Allocates a new host1x channel for @client. May return NULL if CDMA |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 129 | * initialization fails. |
| 130 | */ |
Thierry Reding | caccddc | 2018-06-18 14:01:51 +0200 | [diff] [blame] | 131 | struct host1x_channel *host1x_channel_request(struct host1x_client *client) |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 132 | { |
Thierry Reding | caccddc | 2018-06-18 14:01:51 +0200 | [diff] [blame] | 133 | struct host1x *host = dev_get_drvdata(client->dev->parent); |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 134 | struct host1x_channel_list *chlist = &host->channel_list; |
| 135 | struct host1x_channel *channel; |
Thierry Reding | e18e33a | 2016-06-22 16:47:30 +0200 | [diff] [blame] | 136 | int err; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 137 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 138 | channel = acquire_unused_channel(host); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 139 | if (!channel) |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 140 | return NULL; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 141 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 142 | kref_init(&channel->refcount); |
| 143 | mutex_init(&channel->submitlock); |
Thierry Reding | caccddc | 2018-06-18 14:01:51 +0200 | [diff] [blame] | 144 | channel->client = client; |
| 145 | channel->dev = client->dev; |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 146 | |
| 147 | err = host1x_hw_channel_init(host, channel, channel->id); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 148 | if (err < 0) |
| 149 | goto fail; |
| 150 | |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 151 | err = host1x_cdma_init(&channel->cdma); |
| 152 | if (err < 0) |
| 153 | goto fail; |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 154 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 155 | return channel; |
| 156 | |
| 157 | fail: |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 158 | clear_bit(channel->id, chlist->allocated_channels); |
| 159 | |
Thierry Reding | caccddc | 2018-06-18 14:01:51 +0200 | [diff] [blame] | 160 | dev_err(client->dev, "failed to initialize channel\n"); |
Mikko Perttunen | 8474b02 | 2017-06-15 02:18:42 +0300 | [diff] [blame] | 161 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
Thierry Reding | fae798a | 2013-11-08 11:41:42 +0100 | [diff] [blame] | 164 | EXPORT_SYMBOL(host1x_channel_request); |