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
=
25 kcalloc(BITS_TO_LONGS(num_channels
), sizeof(unsigned long),
27 if (!chlist
->allocated_channels
) {
28 kfree(chlist
->channels
);
32 bitmap_zero(chlist
->allocated_channels
, num_channels
);
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
);
57 EXPORT_SYMBOL(host1x_channel_get
);
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
,
70 struct host1x_channel
*ch
= &host
->channel_list
.channels
[index
];
72 if (!kref_get_unless_zero(&ch
->refcount
))
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
;
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");
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
;
130 channel
= acquire_unused_channel(host
);
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
);
143 err
= host1x_cdma_init(&channel
->cdma
);
150 clear_bit(channel
->id
, chlist
->allocated_channels
);
152 dev_err(client
->dev
, "failed to initialize channel\n");
156 EXPORT_SYMBOL(host1x_channel_request
);