1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2010-2013, NVIDIA Corporation.
8 #include <linux/slab.h>
9 #include <linux/module.h>
15 /* Constructor for the host1x device list */
16 int host1x_channel_list_init(struct host1x_channel_list *chlist,
17 unsigned int num_channels)
19 chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
21 if (!chlist->channels)
24 chlist->allocated_channels = bitmap_zalloc(num_channels, GFP_KERNEL);
25 if (!chlist->allocated_channels) {
26 kfree(chlist->channels);
33 void host1x_channel_list_free(struct host1x_channel_list *chlist)
35 bitmap_free(chlist->allocated_channels);
36 kfree(chlist->channels);
39 int host1x_job_submit(struct host1x_job *job)
41 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
43 return host1x_hw_channel_submit(host, job);
45 EXPORT_SYMBOL(host1x_job_submit);
47 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
49 kref_get(&channel->refcount);
53 EXPORT_SYMBOL(host1x_channel_get);
56 * host1x_channel_get_index() - Attempt to get channel reference by index
57 * @host: Host1x device object
58 * @index: Index of channel
60 * If channel number @index is currently allocated, increase its refcount
61 * and return a pointer to it. Otherwise, return NULL.
63 struct host1x_channel *host1x_channel_get_index(struct host1x *host,
66 struct host1x_channel *ch = &host->channel_list.channels[index];
68 if (!kref_get_unless_zero(&ch->refcount))
74 void host1x_channel_stop(struct host1x_channel *channel)
76 struct host1x *host = dev_get_drvdata(channel->dev->parent);
78 host1x_hw_cdma_stop(host, &channel->cdma);
80 EXPORT_SYMBOL(host1x_channel_stop);
82 static void release_channel(struct kref *kref)
84 struct host1x_channel *channel =
85 container_of(kref, struct host1x_channel, refcount);
86 struct host1x *host = dev_get_drvdata(channel->dev->parent);
87 struct host1x_channel_list *chlist = &host->channel_list;
89 host1x_hw_cdma_stop(host, &channel->cdma);
90 host1x_cdma_deinit(&channel->cdma);
92 clear_bit(channel->id, chlist->allocated_channels);
95 void host1x_channel_put(struct host1x_channel *channel)
97 kref_put(&channel->refcount, release_channel);
99 EXPORT_SYMBOL(host1x_channel_put);
101 static struct host1x_channel *acquire_unused_channel(struct host1x *host)
103 struct host1x_channel_list *chlist = &host->channel_list;
104 unsigned int max_channels = host->info->nb_channels;
107 index = find_first_zero_bit(chlist->allocated_channels, max_channels);
108 if (index >= max_channels) {
109 dev_err(host->dev, "failed to find free channel\n");
113 chlist->channels[index].id = index;
115 set_bit(index, chlist->allocated_channels);
117 return &chlist->channels[index];
121 * host1x_channel_request() - Allocate a channel
122 * @client: Host1x client this channel will be used to send commands to
124 * Allocates a new host1x channel for @client. May return NULL if CDMA
125 * initialization fails.
127 struct host1x_channel *host1x_channel_request(struct host1x_client *client)
129 struct host1x *host = dev_get_drvdata(client->dev->parent);
130 struct host1x_channel_list *chlist = &host->channel_list;
131 struct host1x_channel *channel;
134 channel = acquire_unused_channel(host);
138 kref_init(&channel->refcount);
139 mutex_init(&channel->submitlock);
140 channel->client = client;
141 channel->dev = client->dev;
143 err = host1x_hw_channel_init(host, channel, channel->id);
147 err = host1x_cdma_init(&channel->cdma);
154 clear_bit(channel->id, chlist->allocated_channels);
156 dev_err(client->dev, "failed to initialize channel\n");
160 EXPORT_SYMBOL(host1x_channel_request);