blob: 2a9a3a8d5931a2b5597393fdfe7155475cd5d06b [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Terje Bergstrom65793242013-03-22 16:34:03 +02002/*
3 * Tegra host1x Channel
4 *
5 * Copyright (c) 2010-2013, NVIDIA Corporation.
Terje Bergstrom65793242013-03-22 16:34:03 +02006 */
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 */
Mikko Perttunen8474b022017-06-15 02:18:42 +030016int host1x_channel_list_init(struct host1x_channel_list *chlist,
17 unsigned int num_channels)
Terje Bergstrom65793242013-03-22 16:34:03 +020018{
Mikko Perttunen8474b022017-06-15 02:18:42 +030019 chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
20 GFP_KERNEL);
21 if (!chlist->channels)
22 return -ENOMEM;
Terje Bergstrom65793242013-03-22 16:34:03 +020023
Mikko Perttunen8474b022017-06-15 02:18:42 +030024 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;
Terje Bergstrom65793242013-03-22 16:34:03 +020030 }
31
Mikko Perttunen8474b022017-06-15 02:18:42 +030032 bitmap_zero(chlist->allocated_channels, num_channels);
33
Terje Bergstrom65793242013-03-22 16:34:03 +020034 return 0;
35}
36
Mikko Perttunen8474b022017-06-15 02:18:42 +030037void host1x_channel_list_free(struct host1x_channel_list *chlist)
38{
39 kfree(chlist->allocated_channels);
40 kfree(chlist->channels);
41}
42
Terje Bergstrom65793242013-03-22 16:34:03 +020043int 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}
Thierry Redingfae798a2013-11-08 11:41:42 +010049EXPORT_SYMBOL(host1x_job_submit);
Terje Bergstrom65793242013-03-22 16:34:03 +020050
51struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
52{
Mikko Perttunen8474b022017-06-15 02:18:42 +030053 kref_get(&channel->refcount);
Terje Bergstrom65793242013-03-22 16:34:03 +020054
Mikko Perttunen8474b022017-06-15 02:18:42 +030055 return channel;
Terje Bergstrom65793242013-03-22 16:34:03 +020056}
Thierry Redingfae798a2013-11-08 11:41:42 +010057EXPORT_SYMBOL(host1x_channel_get);
Terje Bergstrom65793242013-03-22 16:34:03 +020058
Mikko Perttunen8474b022017-06-15 02:18:42 +030059/**
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
Dmitry Osipenko9ca790f2021-12-01 02:23:16 +030078void host1x_channel_stop(struct host1x_channel *channel)
79{
80 struct host1x *host = dev_get_drvdata(channel->dev->parent);
81
82 host1x_hw_cdma_stop(host, &channel->cdma);
83}
84EXPORT_SYMBOL(host1x_channel_stop);
85
Mikko Perttunen8474b022017-06-15 02:18:42 +030086static void release_channel(struct kref *kref)
87{
88 struct host1x_channel *channel =
89 container_of(kref, struct host1x_channel, refcount);
90 struct host1x *host = dev_get_drvdata(channel->dev->parent);
91 struct host1x_channel_list *chlist = &host->channel_list;
92
93 host1x_hw_cdma_stop(host, &channel->cdma);
94 host1x_cdma_deinit(&channel->cdma);
95
96 clear_bit(channel->id, chlist->allocated_channels);
97}
98
Terje Bergstrom65793242013-03-22 16:34:03 +020099void host1x_channel_put(struct host1x_channel *channel)
100{
Mikko Perttunen8474b022017-06-15 02:18:42 +0300101 kref_put(&channel->refcount, release_channel);
Terje Bergstrom65793242013-03-22 16:34:03 +0200102}
Thierry Redingfae798a2013-11-08 11:41:42 +0100103EXPORT_SYMBOL(host1x_channel_put);
Terje Bergstrom65793242013-03-22 16:34:03 +0200104
Mikko Perttunen8474b022017-06-15 02:18:42 +0300105static struct host1x_channel *acquire_unused_channel(struct host1x *host)
106{
107 struct host1x_channel_list *chlist = &host->channel_list;
108 unsigned int max_channels = host->info->nb_channels;
109 unsigned int index;
110
111 index = find_first_zero_bit(chlist->allocated_channels, max_channels);
112 if (index >= max_channels) {
113 dev_err(host->dev, "failed to find free channel\n");
114 return NULL;
115 }
116
117 chlist->channels[index].id = index;
118
119 set_bit(index, chlist->allocated_channels);
120
121 return &chlist->channels[index];
122}
123
124/**
125 * host1x_channel_request() - Allocate a channel
Thierry Redingcaccddc2018-06-18 14:01:51 +0200126 * @client: Host1x client this channel will be used to send commands to
Mikko Perttunen8474b022017-06-15 02:18:42 +0300127 *
Thierry Redingcaccddc2018-06-18 14:01:51 +0200128 * Allocates a new host1x channel for @client. May return NULL if CDMA
Mikko Perttunen8474b022017-06-15 02:18:42 +0300129 * initialization fails.
130 */
Thierry Redingcaccddc2018-06-18 14:01:51 +0200131struct host1x_channel *host1x_channel_request(struct host1x_client *client)
Terje Bergstrom65793242013-03-22 16:34:03 +0200132{
Thierry Redingcaccddc2018-06-18 14:01:51 +0200133 struct host1x *host = dev_get_drvdata(client->dev->parent);
Mikko Perttunen8474b022017-06-15 02:18:42 +0300134 struct host1x_channel_list *chlist = &host->channel_list;
135 struct host1x_channel *channel;
Thierry Redinge18e33a2016-06-22 16:47:30 +0200136 int err;
Terje Bergstrom65793242013-03-22 16:34:03 +0200137
Mikko Perttunen8474b022017-06-15 02:18:42 +0300138 channel = acquire_unused_channel(host);
Terje Bergstrom65793242013-03-22 16:34:03 +0200139 if (!channel)
Mikko Perttunen8474b022017-06-15 02:18:42 +0300140 return NULL;
Terje Bergstrom65793242013-03-22 16:34:03 +0200141
Mikko Perttunen8474b022017-06-15 02:18:42 +0300142 kref_init(&channel->refcount);
143 mutex_init(&channel->submitlock);
Thierry Redingcaccddc2018-06-18 14:01:51 +0200144 channel->client = client;
145 channel->dev = client->dev;
Mikko Perttunen8474b022017-06-15 02:18:42 +0300146
147 err = host1x_hw_channel_init(host, channel, channel->id);
Terje Bergstrom65793242013-03-22 16:34:03 +0200148 if (err < 0)
149 goto fail;
150
Mikko Perttunen8474b022017-06-15 02:18:42 +0300151 err = host1x_cdma_init(&channel->cdma);
152 if (err < 0)
153 goto fail;
Terje Bergstrom65793242013-03-22 16:34:03 +0200154
Terje Bergstrom65793242013-03-22 16:34:03 +0200155 return channel;
156
157fail:
Mikko Perttunen8474b022017-06-15 02:18:42 +0300158 clear_bit(channel->id, chlist->allocated_channels);
159
Thierry Redingcaccddc2018-06-18 14:01:51 +0200160 dev_err(client->dev, "failed to initialize channel\n");
Mikko Perttunen8474b022017-06-15 02:18:42 +0300161
Terje Bergstrom65793242013-03-22 16:34:03 +0200162 return NULL;
163}
Thierry Redingfae798a2013-11-08 11:41:42 +0100164EXPORT_SYMBOL(host1x_channel_request);