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