Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / host1x / channel.c
blob4cd212bb570d5237c76518ea648a901790a90146
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Tegra host1x Channel
5 * Copyright (c) 2010-2013, NVIDIA Corporation.
6 */
8 #include <linux/slab.h>
9 #include <linux/module.h>
11 #include "channel.h"
12 #include "dev.h"
13 #include "job.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),
20 GFP_KERNEL);
21 if (!chlist->channels)
22 return -ENOMEM;
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;
32 bitmap_zero(chlist->allocated_channels, num_channels);
34 return 0;
37 void host1x_channel_list_free(struct host1x_channel_list *chlist)
39 kfree(chlist->allocated_channels);
40 kfree(chlist->channels);
43 int host1x_job_submit(struct host1x_job *job)
45 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
47 return host1x_hw_channel_submit(host, job);
49 EXPORT_SYMBOL(host1x_job_submit);
51 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
53 kref_get(&channel->refcount);
55 return channel;
57 EXPORT_SYMBOL(host1x_channel_get);
59 /**
60 * host1x_channel_get_index() - Attempt to get channel reference by index
61 * @host: Host1x device object
62 * @index: Index of channel
64 * If channel number @index is currently allocated, increase its refcount
65 * and return a pointer to it. Otherwise, return NULL.
67 struct host1x_channel *host1x_channel_get_index(struct host1x *host,
68 unsigned int index)
70 struct host1x_channel *ch = &host->channel_list.channels[index];
72 if (!kref_get_unless_zero(&ch->refcount))
73 return NULL;
75 return ch;
78 static void release_channel(struct kref *kref)
80 struct host1x_channel *channel =
81 container_of(kref, struct host1x_channel, refcount);
82 struct host1x *host = dev_get_drvdata(channel->dev->parent);
83 struct host1x_channel_list *chlist = &host->channel_list;
85 host1x_hw_cdma_stop(host, &channel->cdma);
86 host1x_cdma_deinit(&channel->cdma);
88 clear_bit(channel->id, chlist->allocated_channels);
91 void host1x_channel_put(struct host1x_channel *channel)
93 kref_put(&channel->refcount, release_channel);
95 EXPORT_SYMBOL(host1x_channel_put);
97 static struct host1x_channel *acquire_unused_channel(struct host1x *host)
99 struct host1x_channel_list *chlist = &host->channel_list;
100 unsigned int max_channels = host->info->nb_channels;
101 unsigned int index;
103 index = find_first_zero_bit(chlist->allocated_channels, max_channels);
104 if (index >= max_channels) {
105 dev_err(host->dev, "failed to find free channel\n");
106 return NULL;
109 chlist->channels[index].id = index;
111 set_bit(index, chlist->allocated_channels);
113 return &chlist->channels[index];
117 * host1x_channel_request() - Allocate a channel
118 * @client: Host1x client this channel will be used to send commands to
120 * Allocates a new host1x channel for @client. May return NULL if CDMA
121 * initialization fails.
123 struct host1x_channel *host1x_channel_request(struct host1x_client *client)
125 struct host1x *host = dev_get_drvdata(client->dev->parent);
126 struct host1x_channel_list *chlist = &host->channel_list;
127 struct host1x_channel *channel;
128 int err;
130 channel = acquire_unused_channel(host);
131 if (!channel)
132 return NULL;
134 kref_init(&channel->refcount);
135 mutex_init(&channel->submitlock);
136 channel->client = client;
137 channel->dev = client->dev;
139 err = host1x_hw_channel_init(host, channel, channel->id);
140 if (err < 0)
141 goto fail;
143 err = host1x_cdma_init(&channel->cdma);
144 if (err < 0)
145 goto fail;
147 return channel;
149 fail:
150 clear_bit(channel->id, chlist->allocated_channels);
152 dev_err(client->dev, "failed to initialize channel\n");
154 return NULL;
156 EXPORT_SYMBOL(host1x_channel_request);