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
);
30 mutex_init(&chlist
->lock
);
35 void host1x_channel_list_free(struct host1x_channel_list
*chlist
)
37 bitmap_free(chlist
->allocated_channels
);
38 kfree(chlist
->channels
);
41 int host1x_job_submit(struct host1x_job
*job
)
43 struct host1x
*host
= dev_get_drvdata(job
->channel
->dev
->parent
);
45 return host1x_hw_channel_submit(host
, job
);
47 EXPORT_SYMBOL(host1x_job_submit
);
49 struct host1x_channel
*host1x_channel_get(struct host1x_channel
*channel
)
51 kref_get(&channel
->refcount
);
55 EXPORT_SYMBOL(host1x_channel_get
);
58 * host1x_channel_get_index() - Attempt to get channel reference by index
59 * @host: Host1x device object
60 * @index: Index of channel
62 * If channel number @index is currently allocated, increase its refcount
63 * and return a pointer to it. Otherwise, return NULL.
65 struct host1x_channel
*host1x_channel_get_index(struct host1x
*host
,
68 struct host1x_channel
*ch
= &host
->channel_list
.channels
[index
];
70 if (!kref_get_unless_zero(&ch
->refcount
))
76 void host1x_channel_stop(struct host1x_channel
*channel
)
78 struct host1x
*host
= dev_get_drvdata(channel
->dev
->parent
);
80 host1x_hw_cdma_stop(host
, &channel
->cdma
);
82 EXPORT_SYMBOL(host1x_channel_stop
);
85 * host1x_channel_stop_all() - disable CDMA on allocated channels
86 * @host: host1x instance
88 * Stop CDMA on allocated channels
90 void host1x_channel_stop_all(struct host1x
*host
)
92 struct host1x_channel_list
*chlist
= &host
->channel_list
;
95 mutex_lock(&chlist
->lock
);
97 for_each_set_bit(bit
, chlist
->allocated_channels
, host
->info
->nb_channels
)
98 host1x_channel_stop(&chlist
->channels
[bit
]);
100 mutex_unlock(&chlist
->lock
);
103 static void release_channel(struct kref
*kref
)
105 struct host1x_channel
*channel
=
106 container_of(kref
, struct host1x_channel
, refcount
);
107 struct host1x
*host
= dev_get_drvdata(channel
->dev
->parent
);
108 struct host1x_channel_list
*chlist
= &host
->channel_list
;
110 host1x_hw_cdma_stop(host
, &channel
->cdma
);
111 host1x_cdma_deinit(&channel
->cdma
);
113 clear_bit(channel
->id
, chlist
->allocated_channels
);
116 void host1x_channel_put(struct host1x_channel
*channel
)
118 kref_put(&channel
->refcount
, release_channel
);
120 EXPORT_SYMBOL(host1x_channel_put
);
122 static struct host1x_channel
*acquire_unused_channel(struct host1x
*host
)
124 struct host1x_channel_list
*chlist
= &host
->channel_list
;
125 unsigned int max_channels
= host
->info
->nb_channels
;
128 mutex_lock(&chlist
->lock
);
130 index
= find_first_zero_bit(chlist
->allocated_channels
, max_channels
);
131 if (index
>= max_channels
) {
132 mutex_unlock(&chlist
->lock
);
133 dev_err(host
->dev
, "failed to find free channel\n");
137 chlist
->channels
[index
].id
= index
;
139 set_bit(index
, chlist
->allocated_channels
);
141 mutex_unlock(&chlist
->lock
);
143 return &chlist
->channels
[index
];
147 * host1x_channel_request() - Allocate a channel
148 * @client: Host1x client this channel will be used to send commands to
150 * Allocates a new host1x channel for @client. May return NULL if CDMA
151 * initialization fails.
153 struct host1x_channel
*host1x_channel_request(struct host1x_client
*client
)
155 struct host1x
*host
= dev_get_drvdata(client
->dev
->parent
);
156 struct host1x_channel_list
*chlist
= &host
->channel_list
;
157 struct host1x_channel
*channel
;
160 channel
= acquire_unused_channel(host
);
164 kref_init(&channel
->refcount
);
165 mutex_init(&channel
->submitlock
);
166 channel
->client
= client
;
167 channel
->dev
= client
->dev
;
169 err
= host1x_hw_channel_init(host
, channel
, channel
->id
);
173 err
= host1x_cdma_init(&channel
->cdma
);
180 clear_bit(channel
->id
, chlist
->allocated_channels
);
182 dev_err(client
->dev
, "failed to initialize channel\n");
186 EXPORT_SYMBOL(host1x_channel_request
);