1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Tegra host1x Channel 4 * 5 * Copyright (c) 2010-2013, NVIDIA Corporation. 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 */ 16 int host1x_channel_list_init(struct host1x_channel_list *chlist, 17 unsigned int num_channels) 18 { 19 chlist->channels = kzalloc_objs(struct host1x_channel, num_channels); 20 if (!chlist->channels) 21 return -ENOMEM; 22 23 chlist->allocated_channels = bitmap_zalloc(num_channels, GFP_KERNEL); 24 if (!chlist->allocated_channels) { 25 kfree(chlist->channels); 26 return -ENOMEM; 27 } 28 29 mutex_init(&chlist->lock); 30 31 return 0; 32 } 33 34 void host1x_channel_list_free(struct host1x_channel_list *chlist) 35 { 36 bitmap_free(chlist->allocated_channels); 37 kfree(chlist->channels); 38 } 39 40 int host1x_job_submit(struct host1x_job *job) 41 { 42 struct host1x *host = dev_get_drvdata(job->channel->dev->parent); 43 44 return host1x_hw_channel_submit(host, job); 45 } 46 EXPORT_SYMBOL(host1x_job_submit); 47 48 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel) 49 { 50 kref_get(&channel->refcount); 51 52 return channel; 53 } 54 EXPORT_SYMBOL(host1x_channel_get); 55 56 /** 57 * host1x_channel_get_index() - Attempt to get channel reference by index 58 * @host: Host1x device object 59 * @index: Index of channel 60 * 61 * If channel number @index is currently allocated, increase its refcount 62 * and return a pointer to it. Otherwise, return NULL. 63 */ 64 struct host1x_channel *host1x_channel_get_index(struct host1x *host, 65 unsigned int index) 66 { 67 struct host1x_channel *ch = &host->channel_list.channels[index]; 68 69 if (!kref_get_unless_zero(&ch->refcount)) 70 return NULL; 71 72 return ch; 73 } 74 75 void host1x_channel_stop(struct host1x_channel *channel) 76 { 77 struct host1x *host = dev_get_drvdata(channel->dev->parent); 78 79 host1x_hw_cdma_stop(host, &channel->cdma); 80 } 81 EXPORT_SYMBOL(host1x_channel_stop); 82 83 /** 84 * host1x_channel_stop_all() - disable CDMA on allocated channels 85 * @host: host1x instance 86 * 87 * Stop CDMA on allocated channels 88 */ 89 void host1x_channel_stop_all(struct host1x *host) 90 { 91 struct host1x_channel_list *chlist = &host->channel_list; 92 int bit; 93 94 mutex_lock(&chlist->lock); 95 96 for_each_set_bit(bit, chlist->allocated_channels, host->info->nb_channels) 97 host1x_channel_stop(&chlist->channels[bit]); 98 99 mutex_unlock(&chlist->lock); 100 } 101 102 static void release_channel(struct kref *kref) 103 { 104 struct host1x_channel *channel = 105 container_of(kref, struct host1x_channel, refcount); 106 struct host1x *host = dev_get_drvdata(channel->dev->parent); 107 struct host1x_channel_list *chlist = &host->channel_list; 108 109 host1x_hw_cdma_stop(host, &channel->cdma); 110 host1x_cdma_deinit(&channel->cdma); 111 112 clear_bit(channel->id, chlist->allocated_channels); 113 } 114 115 void host1x_channel_put(struct host1x_channel *channel) 116 { 117 kref_put(&channel->refcount, release_channel); 118 } 119 EXPORT_SYMBOL(host1x_channel_put); 120 121 static struct host1x_channel *acquire_unused_channel(struct host1x *host) 122 { 123 struct host1x_channel_list *chlist = &host->channel_list; 124 unsigned int max_channels = host->info->nb_channels; 125 unsigned int index; 126 127 mutex_lock(&chlist->lock); 128 129 index = find_first_zero_bit(chlist->allocated_channels, max_channels); 130 if (index >= max_channels) { 131 mutex_unlock(&chlist->lock); 132 dev_err(host->dev, "failed to find free channel\n"); 133 return NULL; 134 } 135 136 chlist->channels[index].id = index; 137 138 set_bit(index, chlist->allocated_channels); 139 140 mutex_unlock(&chlist->lock); 141 142 return &chlist->channels[index]; 143 } 144 145 /** 146 * host1x_channel_request() - Allocate a channel 147 * @client: Host1x client this channel will be used to send commands to 148 * 149 * Allocates a new host1x channel for @client. May return NULL if CDMA 150 * initialization fails. 151 */ 152 struct host1x_channel *host1x_channel_request(struct host1x_client *client) 153 { 154 struct host1x *host = dev_get_drvdata(client->dev->parent); 155 struct host1x_channel_list *chlist = &host->channel_list; 156 struct host1x_channel *channel; 157 int err; 158 159 channel = acquire_unused_channel(host); 160 if (!channel) 161 return NULL; 162 163 kref_init(&channel->refcount); 164 mutex_init(&channel->submitlock); 165 channel->client = client; 166 channel->dev = client->dev; 167 168 err = host1x_hw_channel_init(host, channel, channel->id); 169 if (err < 0) 170 goto fail; 171 172 err = host1x_cdma_init(&channel->cdma); 173 if (err < 0) 174 goto fail; 175 176 return channel; 177 178 fail: 179 clear_bit(channel->id, chlist->allocated_channels); 180 181 dev_err(client->dev, "failed to initialize channel\n"); 182 183 return NULL; 184 } 185 EXPORT_SYMBOL(host1x_channel_request); 186