Loading...
1/*
2 * Tegra host1x Channel
3 *
4 * Copyright (c) 2010-2013, NVIDIA Corporation.
5 *
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.
9 *
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
13 * more details.
14 *
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/>.
17 */
18
19#include <linux/slab.h>
20#include <linux/module.h>
21
22#include "channel.h"
23#include "dev.h"
24#include "job.h"
25
26/* Constructor for the host1x device list */
27int host1x_channel_list_init(struct host1x *host)
28{
29 INIT_LIST_HEAD(&host->chlist.list);
30 mutex_init(&host->chlist_mutex);
31
32 if (host->info->nb_channels > BITS_PER_LONG) {
33 WARN(1, "host1x hardware has more channels than supported by the driver\n");
34 return -ENOSYS;
35 }
36
37 return 0;
38}
39
40int 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}
46EXPORT_SYMBOL(host1x_job_submit);
47
48struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
49{
50 int err = 0;
51
52 mutex_lock(&channel->reflock);
53
54 if (channel->refcount == 0)
55 err = host1x_cdma_init(&channel->cdma);
56
57 if (!err)
58 channel->refcount++;
59
60 mutex_unlock(&channel->reflock);
61
62 return err ? NULL : channel;
63}
64EXPORT_SYMBOL(host1x_channel_get);
65
66void host1x_channel_put(struct host1x_channel *channel)
67{
68 mutex_lock(&channel->reflock);
69
70 if (channel->refcount == 1) {
71 struct host1x *host = dev_get_drvdata(channel->dev->parent);
72
73 host1x_hw_cdma_stop(host, &channel->cdma);
74 host1x_cdma_deinit(&channel->cdma);
75 }
76
77 channel->refcount--;
78
79 mutex_unlock(&channel->reflock);
80}
81EXPORT_SYMBOL(host1x_channel_put);
82
83struct host1x_channel *host1x_channel_request(struct device *dev)
84{
85 struct host1x *host = dev_get_drvdata(dev->parent);
86 int max_channels = host->info->nb_channels;
87 struct host1x_channel *channel = NULL;
88 int index, err;
89
90 mutex_lock(&host->chlist_mutex);
91
92 index = find_first_zero_bit(&host->allocated_channels, max_channels);
93 if (index >= max_channels)
94 goto fail;
95
96 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
97 if (!channel)
98 goto fail;
99
100 err = host1x_hw_channel_init(host, channel, index);
101 if (err < 0)
102 goto fail;
103
104 /* Link device to host1x_channel */
105 channel->dev = dev;
106
107 /* Add to channel list */
108 list_add_tail(&channel->list, &host->chlist.list);
109
110 host->allocated_channels |= BIT(index);
111
112 mutex_unlock(&host->chlist_mutex);
113 return channel;
114
115fail:
116 dev_err(dev, "failed to init channel\n");
117 kfree(channel);
118 mutex_unlock(&host->chlist_mutex);
119 return NULL;
120}
121EXPORT_SYMBOL(host1x_channel_request);
122
123void host1x_channel_free(struct host1x_channel *channel)
124{
125 struct host1x *host = dev_get_drvdata(channel->dev->parent);
126
127 host->allocated_channels &= ~BIT(channel->id);
128 list_del(&channel->list);
129 kfree(channel);
130}
131EXPORT_SYMBOL(host1x_channel_free);
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 */
16int host1x_channel_list_init(struct host1x_channel_list *chlist,
17 unsigned int num_channels)
18{
19 chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
20 GFP_KERNEL);
21 if (!chlist->channels)
22 return -ENOMEM;
23
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;
30 }
31
32 bitmap_zero(chlist->allocated_channels, num_channels);
33
34 return 0;
35}
36
37void host1x_channel_list_free(struct host1x_channel_list *chlist)
38{
39 kfree(chlist->allocated_channels);
40 kfree(chlist->channels);
41}
42
43int 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}
49EXPORT_SYMBOL(host1x_job_submit);
50
51struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
52{
53 kref_get(&channel->refcount);
54
55 return channel;
56}
57EXPORT_SYMBOL(host1x_channel_get);
58
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 */
67struct 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
78static void release_channel(struct kref *kref)
79{
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;
84
85 host1x_hw_cdma_stop(host, &channel->cdma);
86 host1x_cdma_deinit(&channel->cdma);
87
88 clear_bit(channel->id, chlist->allocated_channels);
89}
90
91void host1x_channel_put(struct host1x_channel *channel)
92{
93 kref_put(&channel->refcount, release_channel);
94}
95EXPORT_SYMBOL(host1x_channel_put);
96
97static struct host1x_channel *acquire_unused_channel(struct host1x *host)
98{
99 struct host1x_channel_list *chlist = &host->channel_list;
100 unsigned int max_channels = host->info->nb_channels;
101 unsigned int index;
102
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;
107 }
108
109 chlist->channels[index].id = index;
110
111 set_bit(index, chlist->allocated_channels);
112
113 return &chlist->channels[index];
114}
115
116/**
117 * host1x_channel_request() - Allocate a channel
118 * @client: Host1x client this channel will be used to send commands to
119 *
120 * Allocates a new host1x channel for @client. May return NULL if CDMA
121 * initialization fails.
122 */
123struct host1x_channel *host1x_channel_request(struct host1x_client *client)
124{
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;
129
130 channel = acquire_unused_channel(host);
131 if (!channel)
132 return NULL;
133
134 kref_init(&channel->refcount);
135 mutex_init(&channel->submitlock);
136 channel->client = client;
137 channel->dev = client->dev;
138
139 err = host1x_hw_channel_init(host, channel, channel->id);
140 if (err < 0)
141 goto fail;
142
143 err = host1x_cdma_init(&channel->cdma);
144 if (err < 0)
145 goto fail;
146
147 return channel;
148
149fail:
150 clear_bit(channel->id, chlist->allocated_channels);
151
152 dev_err(client->dev, "failed to initialize channel\n");
153
154 return NULL;
155}
156EXPORT_SYMBOL(host1x_channel_request);