4 * Copyright (c) 2010-2013, NVIDIA Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/slab.h>
20 #include <linux/module.h>
26 /* Constructor for the host1x device list */
27 int host1x_channel_list_init(struct host1x_channel_list *chlist,
28 unsigned int num_channels)
30 chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
32 if (!chlist->channels)
35 chlist->allocated_channels =
36 kcalloc(BITS_TO_LONGS(num_channels), sizeof(unsigned long),
38 if (!chlist->allocated_channels) {
39 kfree(chlist->channels);
43 bitmap_zero(chlist->allocated_channels, num_channels);
48 void host1x_channel_list_free(struct host1x_channel_list *chlist)
50 kfree(chlist->allocated_channels);
51 kfree(chlist->channels);
54 int host1x_job_submit(struct host1x_job *job)
56 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
58 return host1x_hw_channel_submit(host, job);
60 EXPORT_SYMBOL(host1x_job_submit);
62 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
64 kref_get(&channel->refcount);
68 EXPORT_SYMBOL(host1x_channel_get);
71 * host1x_channel_get_index() - Attempt to get channel reference by index
72 * @host: Host1x device object
73 * @index: Index of channel
75 * If channel number @index is currently allocated, increase its refcount
76 * and return a pointer to it. Otherwise, return NULL.
78 struct host1x_channel *host1x_channel_get_index(struct host1x *host,
81 struct host1x_channel *ch = &host->channel_list.channels[index];
83 if (!kref_get_unless_zero(&ch->refcount))
89 static void release_channel(struct kref *kref)
91 struct host1x_channel *channel =
92 container_of(kref, struct host1x_channel, refcount);
93 struct host1x *host = dev_get_drvdata(channel->dev->parent);
94 struct host1x_channel_list *chlist = &host->channel_list;
96 host1x_hw_cdma_stop(host, &channel->cdma);
97 host1x_cdma_deinit(&channel->cdma);
99 clear_bit(channel->id, chlist->allocated_channels);
102 void host1x_channel_put(struct host1x_channel *channel)
104 kref_put(&channel->refcount, release_channel);
106 EXPORT_SYMBOL(host1x_channel_put);
108 static struct host1x_channel *acquire_unused_channel(struct host1x *host)
110 struct host1x_channel_list *chlist = &host->channel_list;
111 unsigned int max_channels = host->info->nb_channels;
114 index = find_first_zero_bit(chlist->allocated_channels, max_channels);
115 if (index >= max_channels) {
116 dev_err(host->dev, "failed to find free channel\n");
120 chlist->channels[index].id = index;
122 set_bit(index, chlist->allocated_channels);
124 return &chlist->channels[index];
128 * host1x_channel_request() - Allocate a channel
129 * @device: Host1x unit this channel will be used to send commands to
131 * Allocates a new host1x channel for @device. May return NULL if CDMA
132 * initialization fails.
134 struct host1x_channel *host1x_channel_request(struct device *dev)
136 struct host1x *host = dev_get_drvdata(dev->parent);
137 struct host1x_channel_list *chlist = &host->channel_list;
138 struct host1x_channel *channel;
141 channel = acquire_unused_channel(host);
145 kref_init(&channel->refcount);
146 mutex_init(&channel->submitlock);
149 err = host1x_hw_channel_init(host, channel, channel->id);
153 err = host1x_cdma_init(&channel->cdma);
160 clear_bit(channel->id, chlist->allocated_channels);
162 dev_err(dev, "failed to initialize channel\n");
166 EXPORT_SYMBOL(host1x_channel_request);